-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInhibitor.h
52 lines (42 loc) · 1.26 KB
/
Inhibitor.h
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
#pragma once
#include <sdbus-c++/sdbus-c++.h>
#include <unistd.h>
#include <system_error>
#include <memory>
class Inhibitor {
public:
Inhibitor(std::unique_ptr<sdbus::IObjectProxy>& proxy, std::string what, std::string who, std::string why,
std::string type) : what(std::move(what)), who(std::move(who)), why(std::move(why)),
type(std::move(type)), fd(-1), proxy(proxy) {
acquire();
}
void acquire() {
if (fd == -1) {
sdbus::FileHandle lockDescriptor;
proxy->callMethod("Inhibit")
.onInterface("org.freedesktop.login1.Manager")
.withArguments(what, who, why, type)
.storeResultsTo(lockDescriptor);
fd = lockDescriptor.get();
}
}
void release() {
if (fd != -1) {
int r = close(fd);
if (r < 0) {
throw std::system_error(errno, std::generic_category(), "Failed to close inhibitor file descriptor");
}
fd = -1;
}
}
~Inhibitor() {
release();
}
private:
std::string what;
std::string who;
std::string why;
std::string type;
int fd;
std::unique_ptr<sdbus::IObjectProxy>& proxy;
};