-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.c
275 lines (236 loc) · 6.73 KB
/
main.c
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/* Copyright 2019-2020 Corey Hinshaw
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
#include <bsd/libutil.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "version.h"
#ifdef ELOGIND
#include <elogind/sd-bus.h>
#else
#include <systemd/sd-bus.h>
#endif
#ifdef WAYLAND
#include <wayland-client.h>
#include "idle-inhibit-unstable-v1-client-protocol.h"
#endif
#define PROGNAME "caffeinated"
static unsigned char daemonize = 0;
static char *pid_path = NULL;
static struct pidfh *pfh = NULL;
static signed int lock_fd = -1;
static struct sd_bus *bus = NULL;
#ifdef WAYLAND
static struct wl_compositor *compositor = NULL;
static struct wl_surface *surface = NULL;
static struct wl_display *display = NULL;
static struct zwp_idle_inhibit_manager_v1 *wl_idle_inhibit_manager = NULL;
static struct zwp_idle_inhibitor_v1 *wl_idle_inhibitor = NULL;
static void handle_wl_global(void *data, struct wl_registry *registry,
uint32_t name, const char *interface, uint32_t version) {
if (strcmp(interface, wl_compositor_interface.name) == 0) {
compositor = wl_registry_bind(registry, name,
&wl_compositor_interface, 1);
} else if (strcmp(interface, zwp_idle_inhibit_manager_v1_interface.name) == 0) {
wl_idle_inhibit_manager = wl_registry_bind(registry, name,
&zwp_idle_inhibit_manager_v1_interface, 1);
}
}
static void handle_wl_global_remove(void *data, struct wl_registry *registry,
uint32_t name) {
// No op
}
static const struct wl_registry_listener registry_listener = {
.global = handle_wl_global,
.global_remove = handle_wl_global_remove,
};
static void init_wl_surface(void) {
display = wl_display_connect(NULL);
if (display == NULL) {
errx(EXIT_FAILURE, "failed to create wl_display");
}
struct wl_registry *registry = wl_display_get_registry(display);
wl_registry_add_listener(registry, ®istry_listener, NULL);
wl_display_roundtrip(display);
if (wl_idle_inhibit_manager == NULL || compositor == NULL) {
errx(EXIT_FAILURE, "no wl_idle_inhibit_manager or wl_compositor support");
}
surface = wl_compositor_create_surface(compositor);
}
#endif
static void acquire_lock(void) {
sd_bus_message *msg = NULL;
sd_bus_error error = SD_BUS_ERROR_NULL;
signed int ret = sd_bus_call_method(bus, "org.freedesktop.login1",
"/org/freedesktop/login1", "org.freedesktop.login1.Manager",
"Inhibit", &error, &msg, "ssss",
"idle", PROGNAME, "Prevent system idle", "block");
if (ret < 0) {
warnx("Failed to send idle inhibit signal: %s", error.message);
goto cleanup;
}
ret = sd_bus_message_read(msg, "h", &lock_fd);
if (ret < 0) {
warnx("Failed to parse D-Bus response for idle inhibit");
goto cleanup;
}
lock_fd = fcntl(lock_fd, F_DUPFD_CLOEXEC, 3);
if (lock_fd < 0) {
ret = -errno;
warn("Failed to copy lock file descriptor");
}
#ifdef WAYLAND
if (wl_idle_inhibitor == NULL) {
wl_idle_inhibitor =
zwp_idle_inhibit_manager_v1_create_inhibitor(wl_idle_inhibit_manager, surface);
wl_display_roundtrip(display);
}
#endif
cleanup:
sd_bus_error_free(&error);
sd_bus_message_unref(msg);
if (ret < 0) {
exit(-ret);
}
}
static void release_lock(void) {
if (lock_fd >= 0) {
close(lock_fd);
}
lock_fd = -1;
#ifdef WAYLAND
if (wl_idle_inhibitor) {
zwp_idle_inhibitor_v1_destroy(wl_idle_inhibitor);
wl_idle_inhibitor = NULL;
wl_display_roundtrip(display);
}
#endif
}
static void connect_to_bus(void) {
signed int ret = sd_bus_default_system(&bus);
if (sd_bus_default_system(&bus) < 0) {
errx(-ret, "Failed to open D-Bus connection");
}
}
static void cleanup(void) {
release_lock();
pidfile_remove(pfh);
#ifdef WAYLAND
wl_surface_destroy(surface);
#endif
}
void handle_signal(int sig) {
switch (sig) {
case SIGUSR1:
if (lock_fd >= 0) {
release_lock();
} else {
acquire_lock();
}
break;
case SIGINT:
case SIGTERM:
case SIGHUP:
cleanup();
signal(sig, SIG_DFL);
raise(sig);
break;
}
}
static void print_version(void) {
printf("%s %s\n", PROGNAME, VERSION);
}
static void print_help(void) {
printf("Usage: %s [OPTIONS]\n\
Prevents system idle.\n\
\n\
Options:\n\
-h print this help message\n\
-v print program version information\n\
-d run as background daemon\n\
-p PATH create pidfile at PATH\n\
", PROGNAME);
}
static void set_pid_path(void) {
char *dir = getenv("XDG_RUNTIME_DIR");
if (geteuid() == 0) {
pid_path = "/var/run/" PROGNAME ".pid";
} else if (dir != NULL) {
pid_path = malloc(strlen(dir) + strlen("/" PROGNAME ".pid") + 1);
strcpy(pid_path, dir);
strcat(pid_path, "/" PROGNAME ".pid");
}
}
static void parse_args(int argc, char *argv[]) {
signed char c;
while ((c = getopt(argc, argv, ":hvdp:")) != -1) {
switch (c) {
case 'h':
print_help();
exit(EXIT_SUCCESS);
case 'v':
print_version();
exit(EXIT_SUCCESS);
case 'd':
daemonize = 1;
break;
case 'p':
pid_path = optarg;
break;
case '?':
errx(EXIT_FAILURE, "Unknown option `-%c'.", optopt);
case ':':
errx(EXIT_FAILURE, "Option -%c requires an argument.", optopt);
}
}
}
int main(int argc, char *argv[]) {
set_pid_path();
parse_args(argc, argv);
if (pid_path != NULL) {
pfh = pidfile_open(pid_path, 0644, NULL);
if (pfh == NULL) {
if (errno == EEXIST) {
errx(EXIT_FAILURE, "process is already running");
}
warn("Cannot open or create pidfile");
}
}
if (daemonize && daemon(1, 0) < 0) {
warn("Cannot daemonize");
pidfile_remove(pfh);
exit(EXIT_FAILURE);
}
atexit(cleanup);
signal(SIGINT, handle_signal);
signal(SIGTERM, handle_signal);
signal(SIGHUP, handle_signal);
signal(SIGUSR1, handle_signal);
pidfile_write(pfh);
connect_to_bus();
#ifdef WAYLAND
init_wl_surface();
#endif
acquire_lock();
for (;;) {
pause();
}
return EXIT_SUCCESS;
}