-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
401 lines (337 loc) · 12.3 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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
#include <assert.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <wayland-client.h>
#include <wayland-cursor.h>
#include "chayang.h"
#include "single-pixel-buffer-v1-protocol.h"
#include "viewporter-protocol.h"
#include "wlr-layer-shell-unstable-v1-protocol.h"
static void repaint_output(struct chayang_output *output);
static int64_t now_ms(void) {
struct timespec ts = {0};
if (clock_gettime(CLOCK_MONOTONIC, &ts) != 0) {
perror("clock_gettime() failed");
exit(1);
}
return (int64_t)ts.tv_sec * 1000 + ts.tv_nsec / 1000000;
}
static void cancel(struct chayang *state) {
state->running = false;
state->cancelled = true;
}
static void frame_callback_handle_done(void *data, struct wl_callback *callback, uint32_t time) {
struct chayang_output *output = data;
assert(callback == output->frame_callback);
wl_callback_destroy(callback);
output->frame_callback = NULL;
repaint_output(output);
}
static const struct wl_callback_listener frame_callback_listener = {
.done = frame_callback_handle_done,
};
static void repaint_output(struct chayang_output *output) {
int64_t delta = now_ms() - output->chayang->start_time_ms;
double progress = (double)delta / output->chayang->delay_ms;
if (progress >= 1) {
output->chayang->running = false;
return;
}
uint32_t alpha = progress * UINT32_MAX;
struct wl_buffer *buffer = wp_single_pixel_buffer_manager_v1_create_u32_rgba_buffer(output->chayang->single_pixel_buffer_manager, 0, 0, 0, alpha);
wl_surface_attach(output->surface, buffer, 0, 0);
wp_viewport_set_destination(output->viewport, output->surface_width, output->surface_height);
if (output->frame_callback != NULL) {
wl_callback_destroy(output->frame_callback);
}
output->frame_callback = wl_surface_frame(output->surface);
wl_callback_add_listener(output->frame_callback, &frame_callback_listener, output);
wl_surface_damage(output->surface, 0, 0, INT32_MAX, INT32_MAX);
wl_surface_commit(output->surface);
wl_buffer_destroy(buffer);
}
static void destroy_output(struct chayang_output *output) {
if (output->frame_callback != NULL) {
wl_callback_destroy(output->frame_callback);
}
wp_viewport_destroy(output->viewport);
zwlr_layer_surface_v1_destroy(output->layer_surface);
wl_surface_destroy(output->surface);
wl_list_remove(&output->link);
wl_output_destroy(output->wl);
free(output);
}
static void destroy_seat(struct chayang_seat *seat) {
wl_list_remove(&seat->link);
if (seat->pointer != NULL) {
wl_pointer_destroy(seat->pointer);
}
if (seat->keyboard != NULL) {
wl_keyboard_destroy(seat->keyboard);
}
wl_seat_destroy(seat->wl);
free(seat);
}
static void layer_surface_handle_configure(void *data, struct zwlr_layer_surface_v1 *surface, uint32_t serial, uint32_t width, uint32_t height) {
struct chayang_output *output = data;
output->surface_width = width;
output->surface_height = height;
zwlr_layer_surface_v1_ack_configure(surface, serial);
repaint_output(output);
}
static void layer_surface_handle_closed(void *data, struct zwlr_layer_surface_v1 *surface) {
struct chayang_output *output = data;
destroy_output(output);
}
static const struct zwlr_layer_surface_v1_listener layer_surface_listener = {
.configure = layer_surface_handle_configure,
.closed = layer_surface_handle_closed,
};
static void pointer_handle_enter(void *data, struct wl_pointer *wl_pointer, uint32_t serial, struct wl_surface *surface, wl_fixed_t surface_x, wl_fixed_t surface_y) {
// No-op
}
static void pointer_handle_leave(void *data, struct wl_pointer *wl_pointer, uint32_t serial, struct wl_surface *surface) {
// No-op
}
static void pointer_handle_motion(void *data, struct wl_pointer *wl_pointer, uint32_t time, wl_fixed_t surface_x, wl_fixed_t surface_y) {
struct chayang_seat *seat = data;
if (!seat->chayang->ignore_mouse) {
cancel(seat->chayang);
}
}
static void pointer_handle_button(void *data, struct wl_pointer *wl_pointer, uint32_t serial, uint32_t time, uint32_t button, uint32_t state) {
struct chayang_seat *seat = data;
if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
cancel(seat->chayang);
}
}
static void pointer_handle_axis(void *data, struct wl_pointer *wl_pointer, uint32_t time, uint32_t axis, wl_fixed_t value) {
struct chayang_seat *seat = data;
cancel(seat->chayang);
}
static const struct wl_pointer_listener pointer_listener = {
.enter = pointer_handle_enter,
.leave = pointer_handle_leave,
.motion = pointer_handle_motion,
.button = pointer_handle_button,
.axis = pointer_handle_axis,
};
static void keyboard_handle_keymap(void *data, struct wl_keyboard *wl_keyboard, uint32_t format, int fd, uint32_t size) {
close(fd);
}
static void keyboard_handle_enter(void *data, struct wl_keyboard *wl_keyboard, uint32_t serial, struct wl_surface *wl_surface, struct wl_array *keys) {
// No-op
}
static void keyboard_handle_leave(void *data, struct wl_keyboard *wl_keyboard, uint32_t serial, struct wl_surface *wl_surface) {
// No-op
}
static void keyboard_handle_key(void *data, struct wl_keyboard *wl_keyboard, uint32_t serial, uint32_t time, uint32_t key, uint32_t state) {
struct chayang_seat *seat = data;
if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
cancel(seat->chayang);
}
}
static void keyboard_handle_modifiers(void *data, struct wl_keyboard *wl_keyboard, uint32_t serial, uint32_t mods_depressed, uint32_t mods_latched, uint32_t mods_locked, uint32_t group) {
// No-op
}
static const struct wl_keyboard_listener keyboard_listener = {
.keymap = keyboard_handle_keymap,
.enter = keyboard_handle_enter,
.leave = keyboard_handle_leave,
.key = keyboard_handle_key,
.modifiers = keyboard_handle_modifiers,
};
static void seat_handle_capabilities(void *data, struct wl_seat *wl_seat, uint32_t caps) {
struct chayang_seat *seat = data;
uint32_t removed_caps = seat->caps & ~caps;
uint32_t added_caps = caps & ~seat->caps;
if (removed_caps & WL_SEAT_CAPABILITY_POINTER) {
wl_pointer_destroy(seat->pointer);
seat->pointer = NULL;
}
if (removed_caps & WL_SEAT_CAPABILITY_KEYBOARD) {
wl_keyboard_destroy(seat->keyboard);
seat->keyboard = NULL;
}
if (added_caps & WL_SEAT_CAPABILITY_POINTER) {
seat->pointer = wl_seat_get_pointer(seat->wl);
wl_pointer_add_listener(seat->pointer, &pointer_listener, seat);
}
if (added_caps & WL_SEAT_CAPABILITY_KEYBOARD) {
seat->keyboard = wl_seat_get_keyboard(seat->wl);
wl_keyboard_add_listener(seat->keyboard, &keyboard_listener, seat);
}
seat->caps = caps;
}
static const struct wl_seat_listener seat_listener = {
.capabilities = seat_handle_capabilities,
};
static void registry_handle_global(void *data, struct wl_registry *registry, uint32_t name, const char *interface, uint32_t version) {
struct chayang *state = data;
if (strcmp(interface, wl_compositor_interface.name) == 0) {
state->compositor = wl_registry_bind(registry, name, &wl_compositor_interface, 4);
} else if (strcmp(interface, zwlr_layer_shell_v1_interface.name) == 0) {
state->layer_shell = wl_registry_bind(registry, name, &zwlr_layer_shell_v1_interface, 1);
} else if (strcmp(interface, wp_viewporter_interface.name) == 0) {
state->viewporter = wl_registry_bind(registry, name, &wp_viewporter_interface, 1);
} else if (strcmp(interface, wp_single_pixel_buffer_manager_v1_interface.name) == 0) {
state->single_pixel_buffer_manager = wl_registry_bind(registry, name, &wp_single_pixel_buffer_manager_v1_interface, 1);
} else if (strcmp(interface, wl_output_interface.name) == 0) {
struct chayang_output *output = calloc(1, sizeof(*output));
output->global_name = name;
output->chayang = state;
output->wl = wl_registry_bind(registry, name, &wl_output_interface, 1);
wl_list_insert(&state->outputs, &output->link);
} else if (strcmp(interface, wl_seat_interface.name) == 0) {
struct chayang_seat *seat = calloc(1, sizeof(*seat));
seat->global_name = name;
seat->chayang = state;
seat->wl = wl_registry_bind(registry, name, &wl_seat_interface, 1);
wl_seat_add_listener(seat->wl, &seat_listener, seat);
wl_list_insert(&state->seats, &seat->link);
}
}
static void registry_handle_global_remove(void *data, struct wl_registry *registry, uint32_t name) {
struct chayang *state = data;
struct chayang_output *output;
wl_list_for_each(output, &state->outputs, link) {
if (output->global_name == name) {
destroy_output(output);
break;
}
}
struct chayang_seat *seat;
wl_list_for_each(seat, &state->seats, link) {
if (seat->global_name == name) {
destroy_seat(seat);
break;
}
}
}
static const struct wl_registry_listener registry_listener = {
.global = registry_handle_global,
.global_remove = registry_handle_global_remove,
};
int main(int argc, char *argv[]) {
struct chayang state = {0};
wl_list_init(&state.outputs);
wl_list_init(&state.seats);
state.ignore_mouse = false;
double delay_sec = 3;
char *command = NULL;
while (1) {
int opt = getopt(argc, argv, "hvMc:d:");
if (opt < 0) {
break;
}
switch (opt) {
case 'd':;
char *end = NULL;
errno = 0;
delay_sec = strtod(optarg, &end);
if (errno != 0 || end == optarg || end != &optarg[strlen(optarg)]) {
fprintf(stderr, "invalid -d value\n");
return 1;
}
break;
case 'M':;
state.ignore_mouse = true;
break;
case 'c':;
command = optarg;
break;
case 'v':;
printf("chayang %s\n", CHAYANG_VERSION);
return(0);
default:
fprintf(stderr, "usage: chayang [-v] [-M] [-d seconds] [-c command]\n");
return opt == 'h' ? 0 : 1;
}
}
state.delay_ms = delay_sec * 1000;
struct wl_display *display = wl_display_connect(NULL);
if (display == NULL) {
fprintf(stderr, "failed to connect to display\n");
return 1;
}
struct wl_registry *registry = wl_display_get_registry(display);
wl_registry_add_listener(registry, ®istry_listener, &state);
if (wl_display_roundtrip(display) < 0) {
fprintf(stderr, "wl_display_roundtrip() failed\n");
return 1;
}
struct {
const char *name;
bool found;
} required_globals[] = {
{ "wl_compositor", state.compositor != NULL },
{ "zwlr_layer_shell_v1", state.layer_shell != NULL },
{ "wp_viewporter", state.viewporter != NULL },
{ "wp_single_pixel_buffer_manager_v1", state.single_pixel_buffer_manager != NULL },
};
for (size_t i = 0; i < sizeof(required_globals) / sizeof(required_globals[0]); i++) {
if (!required_globals[i].found) {
fprintf(stderr, "missing %s global\n", required_globals[i].name);
return 1;
}
}
struct chayang_output *output;
wl_list_for_each(output, &state.outputs, link) {
output->surface = wl_compositor_create_surface(state.compositor);
output->layer_surface = zwlr_layer_shell_v1_get_layer_surface(state.layer_shell, output->surface, output->wl, ZWLR_LAYER_SHELL_V1_LAYER_OVERLAY, "dim");
zwlr_layer_surface_v1_add_listener(output->layer_surface, &layer_surface_listener, output);
output->viewport = wp_viewporter_get_viewport(state.viewporter, output->surface);
zwlr_layer_surface_v1_set_anchor(output->layer_surface,
ZWLR_LAYER_SURFACE_V1_ANCHOR_TOP |
ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT |
ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT |
ZWLR_LAYER_SURFACE_V1_ANCHOR_BOTTOM);
zwlr_layer_surface_v1_set_keyboard_interactivity(output->layer_surface, true);
zwlr_layer_surface_v1_set_exclusive_zone(output->layer_surface, -1);
wl_surface_commit(output->surface);
}
int ret = 0;
state.running = true;
state.start_time_ms = now_ms();
while (state.running) {
if (wl_display_dispatch(display) < 0) {
fprintf(stderr, "wl_display_dispatch() failed\n");
ret = 1;
break;
}
}
if (ret == 0 && state.cancelled) {
ret = 2;
}
if (command && ret == 0) {
int status;
pid_t pid = fork();
if (pid == 0)
execl("/bin/sh", "/bin/sh", "-c", command, NULL);
if (waitpid(pid, &status, 0) < 0 || WEXITSTATUS(status) != 0) {
fprintf(stderr, "error running command `%s`\n", command);
ret = 1;
}
}
struct chayang_output *output_tmp;
wl_list_for_each_safe(output, output_tmp, &state.outputs, link) {
destroy_output(output);
}
struct chayang_seat *seat, *seat_tmp;
wl_list_for_each_safe(seat, seat_tmp, &state.seats, link) {
destroy_seat(seat);
}
wl_compositor_destroy(state.compositor);
zwlr_layer_shell_v1_destroy(state.layer_shell);
wp_viewporter_destroy(state.viewporter);
wp_single_pixel_buffer_manager_v1_destroy(state.single_pixel_buffer_manager);
wl_registry_destroy(registry);
wl_display_disconnect(display);
return ret;
}