diff options
author | Brian Ashworth <bosrsf04@gmail.com> | 2018-07-28 23:15:12 -0400 |
---|---|---|
committer | Brian Ashworth <bosrsf04@gmail.com> | 2018-08-01 22:47:54 -0400 |
commit | a6145914c60351d8e541192c7fe35556f8e02507 (patch) | |
tree | 8bb5cacc6e91ed4483c8a4fd0b903aacb76abf15 /swaynag/swaynag.c | |
parent | swaynag: split config into own file and fix optind (diff) | |
download | sway-a6145914c60351d8e541192c7fe35556f8e02507.tar.gz sway-a6145914c60351d8e541192c7fe35556f8e02507.tar.zst sway-a6145914c60351d8e541192c7fe35556f8e02507.zip |
swaynag: refactor {sway_,}nagbar to swaynag
Diffstat (limited to 'swaynag/swaynag.c')
-rw-r--r-- | swaynag/swaynag.c | 413 |
1 files changed, 413 insertions, 0 deletions
diff --git a/swaynag/swaynag.c b/swaynag/swaynag.c new file mode 100644 index 00000000..367e0854 --- /dev/null +++ b/swaynag/swaynag.c | |||
@@ -0,0 +1,413 @@ | |||
1 | #define _XOPEN_SOURCE 500 | ||
2 | #include <assert.h> | ||
3 | #include <sys/stat.h> | ||
4 | #include <sys/wait.h> | ||
5 | #include <wayland-client.h> | ||
6 | #include <wayland-cursor.h> | ||
7 | #include "log.h" | ||
8 | #include "list.h" | ||
9 | #include "swaynag/render.h" | ||
10 | #include "swaynag/swaynag.h" | ||
11 | #include "swaynag/types.h" | ||
12 | #include "wlr-layer-shell-unstable-v1-client-protocol.h" | ||
13 | |||
14 | static void nop() { | ||
15 | // Intentionally left blank | ||
16 | } | ||
17 | |||
18 | static bool terminal_execute(char *terminal, char *command) { | ||
19 | char fname[] = "/tmp/swaynagXXXXXX"; | ||
20 | FILE *tmp= fdopen(mkstemp(fname), "w"); | ||
21 | if (!tmp) { | ||
22 | wlr_log(WLR_ERROR, "Failed to create temp script"); | ||
23 | return false; | ||
24 | } | ||
25 | wlr_log(WLR_DEBUG, "Created temp script: %s", fname); | ||
26 | fprintf(tmp, "#!/bin/sh\nrm %s\n%s", fname, command); | ||
27 | fclose(tmp); | ||
28 | chmod(fname, S_IRUSR | S_IWUSR | S_IXUSR); | ||
29 | char cmd[strlen(terminal) + strlen(" -e ") + strlen(fname) + 1]; | ||
30 | sprintf(cmd, "%s -e %s", terminal, fname); | ||
31 | execl("/bin/sh", "/bin/sh", "-c", cmd, NULL); | ||
32 | return true; | ||
33 | } | ||
34 | |||
35 | static void swaynag_button_execute(struct swaynag *swaynag, | ||
36 | struct swaynag_button *button) { | ||
37 | wlr_log(WLR_DEBUG, "Executing [%s]: %s", button->text, button->action); | ||
38 | if (button->type == SWAYNAG_ACTION_DISMISS) { | ||
39 | swaynag->run_display = false; | ||
40 | } else if (button->type == SWAYNAG_ACTION_EXPAND) { | ||
41 | swaynag->details.visible = !swaynag->details.visible; | ||
42 | render_frame(swaynag); | ||
43 | } else { | ||
44 | if (fork() == 0) { | ||
45 | // Child process. Will be used to prevent zombie processes | ||
46 | setsid(); | ||
47 | if (fork() == 0) { | ||
48 | // Child of the child. Will be reparented to the init process | ||
49 | char *terminal = getenv("TERMINAL"); | ||
50 | if (terminal && strlen(terminal)) { | ||
51 | wlr_log(WLR_DEBUG, "Found $TERMINAL: %s", terminal); | ||
52 | if (!terminal_execute(terminal, button->action)) { | ||
53 | swaynag_destroy(swaynag); | ||
54 | exit(EXIT_FAILURE); | ||
55 | } | ||
56 | } else { | ||
57 | wlr_log(WLR_DEBUG, "$TERMINAL not found. Running directly"); | ||
58 | execl("/bin/sh", "/bin/sh", "-c", button->action, NULL); | ||
59 | } | ||
60 | } | ||
61 | exit(EXIT_SUCCESS); | ||
62 | } | ||
63 | } | ||
64 | wait(0); | ||
65 | } | ||
66 | |||
67 | static void layer_surface_configure(void *data, | ||
68 | struct zwlr_layer_surface_v1 *surface, | ||
69 | uint32_t serial, uint32_t width, uint32_t height) { | ||
70 | struct swaynag *swaynag = data; | ||
71 | swaynag->width = width; | ||
72 | swaynag->height = height; | ||
73 | zwlr_layer_surface_v1_ack_configure(surface, serial); | ||
74 | render_frame(swaynag); | ||
75 | } | ||
76 | |||
77 | static void layer_surface_closed(void *data, | ||
78 | struct zwlr_layer_surface_v1 *surface) { | ||
79 | struct swaynag *swaynag = data; | ||
80 | swaynag_destroy(swaynag); | ||
81 | } | ||
82 | |||
83 | static struct zwlr_layer_surface_v1_listener layer_surface_listener = { | ||
84 | .configure = layer_surface_configure, | ||
85 | .closed = layer_surface_closed, | ||
86 | }; | ||
87 | |||
88 | static void wl_pointer_enter(void *data, struct wl_pointer *wl_pointer, | ||
89 | uint32_t serial, struct wl_surface *surface, | ||
90 | wl_fixed_t surface_x, wl_fixed_t surface_y) { | ||
91 | struct swaynag *swaynag = data; | ||
92 | struct swaynag_pointer *pointer = &swaynag->pointer; | ||
93 | wl_surface_set_buffer_scale(pointer->cursor_surface, swaynag->scale); | ||
94 | wl_surface_attach(pointer->cursor_surface, | ||
95 | wl_cursor_image_get_buffer(pointer->cursor_image), 0, 0); | ||
96 | wl_pointer_set_cursor(wl_pointer, serial, pointer->cursor_surface, | ||
97 | pointer->cursor_image->hotspot_x / swaynag->scale, | ||
98 | pointer->cursor_image->hotspot_y / swaynag->scale); | ||
99 | wl_surface_commit(pointer->cursor_surface); | ||
100 | } | ||
101 | |||
102 | static void wl_pointer_motion(void *data, struct wl_pointer *wl_pointer, | ||
103 | uint32_t time, wl_fixed_t surface_x, wl_fixed_t surface_y) { | ||
104 | struct swaynag *swaynag = data; | ||
105 | swaynag->pointer.x = wl_fixed_to_int(surface_x); | ||
106 | swaynag->pointer.y = wl_fixed_to_int(surface_y); | ||
107 | } | ||
108 | |||
109 | static void wl_pointer_button(void *data, struct wl_pointer *wl_pointer, | ||
110 | uint32_t serial, uint32_t time, uint32_t button, uint32_t state) { | ||
111 | struct swaynag *swaynag = data; | ||
112 | |||
113 | if (state != WL_POINTER_BUTTON_STATE_PRESSED) { | ||
114 | return; | ||
115 | } | ||
116 | |||
117 | double x = swaynag->pointer.x * swaynag->scale; | ||
118 | double y = swaynag->pointer.y * swaynag->scale; | ||
119 | for (int i = 0; i < swaynag->buttons->length; i++) { | ||
120 | struct swaynag_button *nagbutton = swaynag->buttons->items[i]; | ||
121 | if (x >= nagbutton->x | ||
122 | && y >= nagbutton->y | ||
123 | && x < nagbutton->x + nagbutton->width | ||
124 | && y < nagbutton->y + nagbutton->height) { | ||
125 | swaynag_button_execute(swaynag, nagbutton); | ||
126 | return; | ||
127 | } | ||
128 | } | ||
129 | |||
130 | if (swaynag->details.visible && | ||
131 | swaynag->details.total_lines != swaynag->details.visible_lines) { | ||
132 | struct swaynag_button button_up = swaynag->details.button_up; | ||
133 | if (x >= button_up.x | ||
134 | && y >= button_up.y | ||
135 | && x < button_up.x + button_up.width | ||
136 | && y < button_up.y + button_up.height | ||
137 | && swaynag->details.offset > 0) { | ||
138 | swaynag->details.offset--; | ||
139 | render_frame(swaynag); | ||
140 | return; | ||
141 | } | ||
142 | |||
143 | struct swaynag_button button_down = swaynag->details.button_down; | ||
144 | int bot = swaynag->details.total_lines - swaynag->details.visible_lines; | ||
145 | if (x >= button_down.x | ||
146 | && y >= button_down.y | ||
147 | && x < button_down.x + button_down.width | ||
148 | && y < button_down.y + button_down.height | ||
149 | && swaynag->details.offset < bot) { | ||
150 | swaynag->details.offset++; | ||
151 | render_frame(swaynag); | ||
152 | return; | ||
153 | } | ||
154 | } | ||
155 | } | ||
156 | |||
157 | static void wl_pointer_axis(void *data, struct wl_pointer *wl_pointer, | ||
158 | uint32_t time, uint32_t axis, wl_fixed_t value) { | ||
159 | struct swaynag *swaynag = data; | ||
160 | if (!swaynag->details.visible | ||
161 | || swaynag->pointer.x < swaynag->details.x | ||
162 | || swaynag->pointer.y < swaynag->details.y | ||
163 | || swaynag->pointer.x >= swaynag->details.x + swaynag->details.width | ||
164 | || swaynag->pointer.y >= swaynag->details.y + swaynag->details.height | ||
165 | || swaynag->details.total_lines == swaynag->details.visible_lines) { | ||
166 | return; | ||
167 | } | ||
168 | |||
169 | int direction = wl_fixed_to_int(value); | ||
170 | int bot = swaynag->details.total_lines - swaynag->details.visible_lines; | ||
171 | if (direction < 0 && swaynag->details.offset > 0) { | ||
172 | swaynag->details.offset--; | ||
173 | } else if (direction > 0 && swaynag->details.offset < bot) { | ||
174 | swaynag->details.offset++; | ||
175 | } | ||
176 | |||
177 | render_frame(swaynag); | ||
178 | } | ||
179 | |||
180 | static struct wl_pointer_listener pointer_listener = { | ||
181 | .enter = wl_pointer_enter, | ||
182 | .leave = nop, | ||
183 | .motion = wl_pointer_motion, | ||
184 | .button = wl_pointer_button, | ||
185 | .axis = wl_pointer_axis, | ||
186 | .frame = nop, | ||
187 | .axis_source = nop, | ||
188 | .axis_stop = nop, | ||
189 | .axis_discrete = nop, | ||
190 | }; | ||
191 | |||
192 | static void seat_handle_capabilities(void *data, struct wl_seat *wl_seat, | ||
193 | enum wl_seat_capability caps) { | ||
194 | struct swaynag *swaynag = data; | ||
195 | if ((caps & WL_SEAT_CAPABILITY_POINTER)) { | ||
196 | swaynag->pointer.pointer = wl_seat_get_pointer(wl_seat); | ||
197 | wl_pointer_add_listener(swaynag->pointer.pointer, &pointer_listener, | ||
198 | swaynag); | ||
199 | } | ||
200 | } | ||
201 | |||
202 | const struct wl_seat_listener seat_listener = { | ||
203 | .capabilities = seat_handle_capabilities, | ||
204 | .name = nop, | ||
205 | }; | ||
206 | |||
207 | static void output_scale(void *data, struct wl_output *output, | ||
208 | int32_t factor) { | ||
209 | struct swaynag *swaynag = data; | ||
210 | swaynag->scale = factor; | ||
211 | render_frame(swaynag); | ||
212 | } | ||
213 | |||
214 | static struct wl_output_listener output_listener = { | ||
215 | .geometry = nop, | ||
216 | .mode = nop, | ||
217 | .done = nop, | ||
218 | .scale = output_scale, | ||
219 | }; | ||
220 | |||
221 | struct output_state { | ||
222 | struct wl_output *wl_output; | ||
223 | uint32_t wl_name; | ||
224 | struct zxdg_output_v1 *xdg_output; | ||
225 | struct swaynag *swaynag; | ||
226 | }; | ||
227 | |||
228 | static void xdg_output_handle_name(void *data, | ||
229 | struct zxdg_output_v1 *xdg_output, const char *name) { | ||
230 | struct output_state *state = data; | ||
231 | char *outname = state->swaynag->output.name; | ||
232 | wlr_log(WLR_DEBUG, "Checking against output %s for %s", name, outname); | ||
233 | if (outname && !state->swaynag->output.wl_output) { | ||
234 | wlr_log(WLR_DEBUG, "Using output %s", name); | ||
235 | state->swaynag->output.wl_output = state->wl_output; | ||
236 | state->swaynag->output.wl_name = state->wl_name; | ||
237 | wl_output_add_listener(state->swaynag->output.wl_output, | ||
238 | &output_listener, state->swaynag); | ||
239 | wl_display_roundtrip(state->swaynag->display); | ||
240 | zxdg_output_v1_destroy(state->xdg_output); | ||
241 | } else { | ||
242 | zxdg_output_v1_destroy(state->xdg_output); | ||
243 | wl_output_destroy(state->wl_output); | ||
244 | } | ||
245 | state->swaynag->querying_outputs--; | ||
246 | free(state); | ||
247 | } | ||
248 | |||
249 | static struct zxdg_output_v1_listener xdg_output_listener = { | ||
250 | .logical_position = nop, | ||
251 | .logical_size = nop, | ||
252 | .done = nop, | ||
253 | .name = xdg_output_handle_name, | ||
254 | .description = nop, | ||
255 | }; | ||
256 | |||
257 | static void handle_global(void *data, struct wl_registry *registry, | ||
258 | uint32_t name, const char *interface, uint32_t version) { | ||
259 | struct swaynag *swaynag = data; | ||
260 | if (strcmp(interface, wl_compositor_interface.name) == 0) { | ||
261 | swaynag->compositor = wl_registry_bind(registry, name, | ||
262 | &wl_compositor_interface, 3); | ||
263 | } else if (strcmp(interface, wl_seat_interface.name) == 0) { | ||
264 | swaynag->seat = wl_registry_bind(registry, name, &wl_seat_interface, 1); | ||
265 | wl_seat_add_listener(swaynag->seat, &seat_listener, swaynag); | ||
266 | } else if (strcmp(interface, wl_shm_interface.name) == 0) { | ||
267 | swaynag->shm = wl_registry_bind(registry, name, &wl_shm_interface, 1); | ||
268 | } else if (strcmp(interface, wl_output_interface.name) == 0) { | ||
269 | if (!swaynag->output.wl_output && swaynag->xdg_output_manager) { | ||
270 | swaynag->querying_outputs++; | ||
271 | struct output_state *state = | ||
272 | calloc(1, sizeof(struct output_state)); | ||
273 | state->swaynag = swaynag; | ||
274 | state->wl_output = wl_registry_bind(registry, name, | ||
275 | &wl_output_interface, 3); | ||
276 | state->wl_name = name; | ||
277 | state->xdg_output = zxdg_output_manager_v1_get_xdg_output( | ||
278 | swaynag->xdg_output_manager, state->wl_output); | ||
279 | zxdg_output_v1_add_listener(state->xdg_output, | ||
280 | &xdg_output_listener, state); | ||
281 | } | ||
282 | } else if (strcmp(interface, zwlr_layer_shell_v1_interface.name) == 0) { | ||
283 | swaynag->layer_shell = wl_registry_bind( | ||
284 | registry, name, &zwlr_layer_shell_v1_interface, 1); | ||
285 | } else if (strcmp(interface, zxdg_output_manager_v1_interface.name) == 0 | ||
286 | && version >= ZXDG_OUTPUT_V1_NAME_SINCE_VERSION) { | ||
287 | swaynag->xdg_output_manager = wl_registry_bind(registry, name, | ||
288 | &zxdg_output_manager_v1_interface, | ||
289 | ZXDG_OUTPUT_V1_NAME_SINCE_VERSION); | ||
290 | } | ||
291 | } | ||
292 | |||
293 | static void handle_global_remove(void *data, struct wl_registry *registry, | ||
294 | uint32_t name) { | ||
295 | struct swaynag *swaynag = data; | ||
296 | if (swaynag->output.wl_name == name) { | ||
297 | swaynag->run_display = false; | ||
298 | } | ||
299 | } | ||
300 | |||
301 | static const struct wl_registry_listener registry_listener = { | ||
302 | .global = handle_global, | ||
303 | .global_remove = handle_global_remove, | ||
304 | }; | ||
305 | |||
306 | void swaynag_setup(struct swaynag *swaynag) { | ||
307 | swaynag->display = wl_display_connect(NULL); | ||
308 | assert(swaynag->display); | ||
309 | |||
310 | swaynag->scale = 1; | ||
311 | |||
312 | struct wl_registry *registry = wl_display_get_registry(swaynag->display); | ||
313 | wl_registry_add_listener(registry, ®istry_listener, swaynag); | ||
314 | wl_display_roundtrip(swaynag->display); | ||
315 | assert(swaynag->compositor && swaynag->layer_shell && swaynag->shm); | ||
316 | |||
317 | while (swaynag->querying_outputs > 0) { | ||
318 | wl_display_roundtrip(swaynag->display); | ||
319 | } | ||
320 | |||
321 | if (!swaynag->output.wl_output && swaynag->output.name) { | ||
322 | wlr_log(WLR_ERROR, "Output '%s' not found", swaynag->output.name); | ||
323 | swaynag_destroy(swaynag); | ||
324 | exit(EXIT_FAILURE); | ||
325 | } | ||
326 | |||
327 | struct swaynag_pointer *pointer = &swaynag->pointer; | ||
328 | int scale = swaynag->scale < 1 ? 1 : swaynag->scale; | ||
329 | pointer->cursor_theme = wl_cursor_theme_load( | ||
330 | NULL, 24 * scale, swaynag->shm); | ||
331 | assert(pointer->cursor_theme); | ||
332 | struct wl_cursor *cursor = | ||
333 | wl_cursor_theme_get_cursor(pointer->cursor_theme, "left_ptr"); | ||
334 | assert(cursor); | ||
335 | pointer->cursor_image = cursor->images[0]; | ||
336 | pointer->cursor_surface = wl_compositor_create_surface(swaynag->compositor); | ||
337 | assert(pointer->cursor_surface); | ||
338 | |||
339 | swaynag->surface = wl_compositor_create_surface(swaynag->compositor); | ||
340 | assert(swaynag->surface); | ||
341 | swaynag->layer_surface = zwlr_layer_shell_v1_get_layer_surface( | ||
342 | swaynag->layer_shell, swaynag->surface, swaynag->output.wl_output, | ||
343 | ZWLR_LAYER_SHELL_V1_LAYER_TOP, "swaynag"); | ||
344 | assert(swaynag->layer_surface); | ||
345 | zwlr_layer_surface_v1_add_listener(swaynag->layer_surface, | ||
346 | &layer_surface_listener, swaynag); | ||
347 | zwlr_layer_surface_v1_set_anchor(swaynag->layer_surface, swaynag->anchors); | ||
348 | |||
349 | wl_registry_destroy(registry); | ||
350 | } | ||
351 | |||
352 | void swaynag_run(struct swaynag *swaynag) { | ||
353 | swaynag->run_display = true; | ||
354 | render_frame(swaynag); | ||
355 | while (swaynag->run_display | ||
356 | && wl_display_dispatch(swaynag->display) != -1) { | ||
357 | // This is intentionally left blank | ||
358 | } | ||
359 | } | ||
360 | |||
361 | void swaynag_destroy(struct swaynag *swaynag) { | ||
362 | swaynag->run_display = false; | ||
363 | |||
364 | free(swaynag->message); | ||
365 | free(swaynag->font); | ||
366 | while (swaynag->buttons->length) { | ||
367 | struct swaynag_button *button = swaynag->buttons->items[0]; | ||
368 | list_del(swaynag->buttons, 0); | ||
369 | free(button->text); | ||
370 | free(button->action); | ||
371 | free(button); | ||
372 | } | ||
373 | list_free(swaynag->buttons); | ||
374 | free(swaynag->details.message); | ||
375 | free(swaynag->details.button_up.text); | ||
376 | free(swaynag->details.button_down.text); | ||
377 | |||
378 | if (swaynag->type) { | ||
379 | swaynag_type_free(swaynag->type); | ||
380 | } | ||
381 | |||
382 | if (swaynag->layer_surface) { | ||
383 | zwlr_layer_surface_v1_destroy(swaynag->layer_surface); | ||
384 | } | ||
385 | |||
386 | if (swaynag->surface) { | ||
387 | wl_surface_destroy(swaynag->surface); | ||
388 | } | ||
389 | |||
390 | if (swaynag->output.wl_output) { | ||
391 | wl_output_destroy(swaynag->output.wl_output); | ||
392 | } | ||
393 | |||
394 | if (&swaynag->buffers[0]) { | ||
395 | destroy_buffer(&swaynag->buffers[0]); | ||
396 | } | ||
397 | |||
398 | if (&swaynag->buffers[1]) { | ||
399 | destroy_buffer(&swaynag->buffers[1]); | ||
400 | } | ||
401 | |||
402 | if (swaynag->compositor) { | ||
403 | wl_compositor_destroy(swaynag->compositor); | ||
404 | } | ||
405 | |||
406 | if (swaynag->shm) { | ||
407 | wl_shm_destroy(swaynag->shm); | ||
408 | } | ||
409 | |||
410 | if (swaynag->display) { | ||
411 | wl_display_disconnect(swaynag->display); | ||
412 | } | ||
413 | } | ||