Repository navigation
Expand file tree
/
Copy pathSystemSleep_Linux.tpp
More file actions
165 lines (141 loc) · 4.71 KB
/
Copy pathSystemSleep_Linux.tpp
File metadata and controls
165 lines (141 loc) · 4.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/* OS Sleep (Linux)
*
* From: https://github.com/PokemonAutomation/Arduino-Source
*
*/
#include <sdbus-c++/sdbus-c++.h>
#include "Common/Cpp/Concurrency/ConditionVariable.h"
#include "Common/Cpp/Concurrency/AsyncTask.h"
#include "Common/Cpp/Concurrency/ThreadPool.h"
#include "CommonFramework/Logging/Logger.h"
#include "CommonFramework/Tools/GlobalThreadPools.h"
#include "SystemSleep.h"
namespace PokemonAutomation{
class LinuxSleepController final : public SystemSleepController{
public:
LinuxSleepController()
: m_screen_on_requests(0)
, m_no_sleep_requests(0)
, m_stopping(false)
, m_thread(GlobalThreadPools::unlimited_normal().dispatch_now_blocking(
[this]{ thread_loop(); })
)
{}
virtual ~LinuxSleepController(){
stop();
}
virtual void stop() noexcept override{
if (!m_thread){
return;
}
{
std::lock_guard<Mutex> lg(m_lock);
m_stopping = true;
}
m_cv.notify_all();
m_thread.wait_and_ignore_exceptions();
m_inhibit_fd.reset();
if (m_state.load(std::memory_order_relaxed) != SleepSuppress::NONE){
try{
global_logger_tagged().log(
"Destroying LinuxSleepController with active requests...",
COLOR_RED
);
}catch (...){}
}
}
virtual void push_screen_on() override{
std::lock_guard<Mutex> lg(m_lock);
m_screen_on_requests++;
m_cv.notify_all();
}
virtual void pop_screen_on() override{
std::lock_guard<Mutex> lg(m_lock);
m_screen_on_requests--;
m_cv.notify_all();
}
virtual void push_no_sleep() override{
std::lock_guard<Mutex> lg(m_lock);
m_no_sleep_requests++;
m_cv.notify_all();
}
virtual void pop_no_sleep() override{
std::lock_guard<Mutex> lg(m_lock);
m_no_sleep_requests--;
m_cv.notify_all();
}
private:
void thread_loop()
{
auto connection = sdbus::createSystemBusConnection();
auto proxy = sdbus::createProxy(
*connection,
sdbus::BusName{"org.freedesktop.login1"},
sdbus::ObjectPath{"/org/freedesktop/login1"}
);
while (true)
{
std::unique_lock<std::mutex> lg(m_lock);
if (m_stopping)
{
return;
}
SleepSuppress before_state = m_state.load(std::memory_order_relaxed);
SleepSuppress after_state = SleepSuppress::NONE;
bool need_inhibit = false;
std::string what;
if (m_no_sleep_requests > 0)
{
need_inhibit = true;
what = "sleep";
after_state = SleepSuppress::NO_SLEEP;
}
else if (m_screen_on_requests > 0)
{
need_inhibit = true;
what = "idle";
after_state = SleepSuppress::SCREEN_ON;
}
if (need_inhibit && !m_inhibit_fd)
{
try
{
sdbus::UnixFd fd;
proxy->callMethod("Inhibit")
.onInterface("org.freedesktop.login1.Manager")
.withArguments(what, "PokemonAutomation", "Keeping system awake", "block")
.storeResultsTo(fd);
m_inhibit_fd = std::make_unique<sdbus::UnixFd>(std::move(fd));
global_logger_tagged().log("Acquired sleep inhibitor", COLOR_BLUE);
}
catch (const sdbus::Error &e)
{
global_logger_tagged().log(std::string("DBus error: ") + e.what(), COLOR_RED);
}
}
else if (!need_inhibit && m_inhibit_fd)
{
m_inhibit_fd.reset();
global_logger_tagged().log("Released sleep inhibitor", COLOR_BLUE);
}
if (before_state != after_state)
{
m_state.store(after_state, std::memory_order_release);
notify_listeners(after_state);
}
m_cv.wait(lg);
}
}
private:
size_t m_screen_on_requests = 0;
size_t m_no_sleep_requests = 0;
bool m_stopping;
ConditionVariable m_cv;
AsyncTask m_thread;
std::unique_ptr<sdbus::UnixFd> m_inhibit_fd;
};
SystemSleepController& SystemSleepController::instance(){
static LinuxSleepController controller;
return controller;
}
}