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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
|
#include <csignal>
#include <cstring>
#include <iostream>
#include <unistd.h>
#include <wayland-client-protocol.h>
#include <wayland-client.h>
// You'll need to generate these headers from the protocol XML files:
// wayland-scanner client-header < idle-inhibit-unstable-v1.xml >
// idle-inhibit-client-protocol.h wayland-scanner private-code <
// idle-inhibit-unstable-v1.xml > idle-inhibit-protocol.c
extern "C" {
#include "idle-inhibitor.h"
}
class WaylandIdleInhibitor {
private:
struct wl_display *display;
struct wl_registry *registry;
struct wl_compositor *compositor;
struct wl_surface *surface;
struct zwp_idle_inhibit_manager_v1 *idle_inhibit_manager;
struct zwp_idle_inhibitor_v1 *idle_inhibitor;
static WaylandIdleInhibitor *instance;
// Registry listener to get global objects
static void registry_global(void *data, struct wl_registry *registry,
uint32_t id, const char *interface,
uint32_t version) {
WaylandIdleInhibitor *inhibitor = static_cast<WaylandIdleInhibitor *>(data);
if (strcmp(interface, wl_compositor_interface.name) == 0) {
inhibitor->compositor = static_cast<struct wl_compositor *>(
wl_registry_bind(registry, id, &wl_compositor_interface, 1));
} else if (strcmp(interface, zwp_idle_inhibit_manager_v1_interface.name) ==
0) {
inhibitor->idle_inhibit_manager =
static_cast<struct zwp_idle_inhibit_manager_v1 *>(wl_registry_bind(
registry, id, &zwp_idle_inhibit_manager_v1_interface, 1));
}
}
static void registry_global_remove(void *data, struct wl_registry *registry,
uint32_t id) {}
static const struct wl_registry_listener registry_listener;
public:
WaylandIdleInhibitor()
: display(nullptr), registry(nullptr), compositor(nullptr),
surface(nullptr), idle_inhibit_manager(nullptr),
idle_inhibitor(nullptr) {
instance = this;
}
~WaylandIdleInhibitor() { cleanup(); }
bool initialize() {
display = wl_display_connect(nullptr);
if (!display) {
return false;
}
registry = wl_display_get_registry(display);
if (!registry) {
return false;
}
wl_registry_add_listener(registry, ®istry_listener, this);
// Roundtrip to get all globals
wl_display_roundtrip(display);
if (!compositor || !idle_inhibit_manager) {
return false;
}
return true;
}
bool createInvisibleSurface() {
surface = wl_compositor_create_surface(compositor);
if (!surface) {
return false;
}
return true;
}
bool inhibitIdle() {
if (!surface || !idle_inhibit_manager) {
return false;
}
idle_inhibitor = zwp_idle_inhibit_manager_v1_create_inhibitor(
idle_inhibit_manager, surface);
if (!idle_inhibitor) {
std::cerr << "Failed to create idle inhibitor\n";
return false;
}
wl_display_roundtrip(display);
std::cout << "Idle inhibition activated\n";
return true;
}
void cleanup() {
if (idle_inhibitor) {
zwp_idle_inhibitor_v1_destroy(idle_inhibitor);
idle_inhibitor = nullptr;
}
if (surface) {
wl_surface_destroy(surface);
surface = nullptr;
}
if (idle_inhibit_manager) {
zwp_idle_inhibit_manager_v1_destroy(idle_inhibit_manager);
idle_inhibit_manager = nullptr;
}
if (compositor) {
wl_compositor_destroy(compositor);
compositor = nullptr;
}
if (registry) {
wl_registry_destroy(registry);
registry = nullptr;
}
if (display) {
wl_display_disconnect(display);
display = nullptr;
}
}
void run() {
while (wl_display_dispatch(display) != -1)
;
}
static WaylandIdleInhibitor *getInstance() { return instance; }
};
WaylandIdleInhibitor *WaylandIdleInhibitor::instance = nullptr;
const struct wl_registry_listener WaylandIdleInhibitor::registry_listener = {
WaylandIdleInhibitor::registry_global,
WaylandIdleInhibitor::registry_global_remove};
void signalHandler(int signal) {
WaylandIdleInhibitor *inhibitor = WaylandIdleInhibitor::getInstance();
if (inhibitor) {
inhibitor->cleanup();
}
exit(0);
}
int main() {
signal(SIGINT, signalHandler);
signal(SIGTERM, signalHandler);
signal(SIGHUP, signalHandler);
WaylandIdleInhibitor inhibitor;
if (!(inhibitor.initialize() && inhibitor.createInvisibleSurface() &&
inhibitor.inhibitIdle())) {
std::cerr << "Cannot inhibit idle!" << std::endl;
return 1;
}
inhibitor.run();
return 0;
}
|