]> git.sur5r.net Git - i3/i3/blob - i3-nagbar/main.c
Merge branch 'next' into master
[i3/i3] / i3-nagbar / main.c
1 /*
2  * vim:ts=4:sw=4:expandtab
3  *
4  * i3 - an improved dynamic tiling window manager
5  * © 2009 Michael Stapelberg and contributors (see also: LICENSE)
6  *
7  * i3-nagbar is a utility which displays a nag message, for example in the case
8  * when the user has an error in their configuration file.
9  *
10  */
11 #include "libi3.h"
12
13 #include <stdio.h>
14 #include <sys/types.h>
15 #include <sys/stat.h>
16 #include <sys/wait.h>
17 #include <stdlib.h>
18 #include <stdbool.h>
19 #include <unistd.h>
20 #include <string.h>
21 #include <errno.h>
22 #include <err.h>
23 #include <stdint.h>
24 #include <getopt.h>
25 #include <limits.h>
26 #include <fcntl.h>
27 #include <paths.h>
28
29 #include <xcb/xcb.h>
30 #include <xcb/xcb_aux.h>
31 #include <xcb/xcb_event.h>
32 #include <xcb/randr.h>
33 #include <xcb/xcb_cursor.h>
34
35 #include "i3-nagbar.h"
36
37 /** This is the equivalent of XC_left_ptr. I’m not sure why xcb doesn’t have a
38  * constant for that. */
39 #define XCB_CURSOR_LEFT_PTR 68
40
41 static char *argv0 = NULL;
42
43 typedef struct {
44     i3String *label;
45     char *action;
46     int16_t x;
47     uint16_t width;
48 } button_t;
49
50 static xcb_window_t win;
51 static xcb_pixmap_t pixmap;
52 static xcb_gcontext_t pixmap_gc;
53 static xcb_rectangle_t rect = {0, 0, 600, 20};
54 static i3Font font;
55 static i3String *prompt;
56 static button_t *buttons;
57 static int buttoncnt;
58
59 /* Result of get_colorpixel() for the various colors. */
60 static color_t color_background;        /* background of the bar */
61 static color_t color_button_background; /* background for buttons */
62 static color_t color_border;            /* color of the button border */
63 static color_t color_border_bottom;     /* color of the bottom border */
64 static color_t color_text;              /* color of the text */
65
66 xcb_window_t root;
67 xcb_connection_t *conn;
68 xcb_screen_t *root_screen;
69
70 /*
71  * Having verboselog(), errorlog() and debuglog() is necessary when using libi3.
72  *
73  */
74 void verboselog(char *fmt, ...) {
75     va_list args;
76
77     va_start(args, fmt);
78     vfprintf(stdout, fmt, args);
79     va_end(args);
80 }
81
82 void errorlog(char *fmt, ...) {
83     va_list args;
84
85     va_start(args, fmt);
86     vfprintf(stderr, fmt, args);
87     va_end(args);
88 }
89
90 void debuglog(char *fmt, ...) {
91 }
92
93 /*
94  * Starts the given application by passing it through a shell. We use double fork
95  * to avoid zombie processes. As the started application’s parent exits (immediately),
96  * the application is reparented to init (process-id 1), which correctly handles
97  * childs, so we don’t have to do it :-).
98  *
99  * The shell is determined by looking for the SHELL environment variable. If it
100  * does not exist, /bin/sh is used.
101  *
102  */
103 static void start_application(const char *command) {
104     printf("executing: %s\n", command);
105     if (fork() == 0) {
106         /* Child process */
107         setsid();
108         if (fork() == 0) {
109             /* This is the child */
110             execl(_PATH_BSHELL, _PATH_BSHELL, "-c", command, (void *)NULL);
111             /* not reached */
112         }
113         exit(0);
114     }
115     wait(0);
116 }
117
118 static button_t *get_button_at(int16_t x, int16_t y) {
119     for (int c = 0; c < buttoncnt; c++)
120         if (x >= (buttons[c].x) && x <= (buttons[c].x + buttons[c].width))
121             return &buttons[c];
122
123     return NULL;
124 }
125
126 static void handle_button_press(xcb_connection_t *conn, xcb_button_press_event_t *event) {
127     printf("button pressed on x = %d, y = %d\n",
128            event->event_x, event->event_y);
129     /* TODO: set a flag for the button, re-render */
130 }
131
132 /*
133  * Called when the user releases the mouse button. Checks whether the
134  * coordinates are over a button and executes the appropriate action.
135  *
136  */
137 static void handle_button_release(xcb_connection_t *conn, xcb_button_release_event_t *event) {
138     printf("button released on x = %d, y = %d\n",
139            event->event_x, event->event_y);
140     /* If the user hits the close button, we exit(0) */
141     if (event->event_x >= (rect.width - logical_px(32)))
142         exit(0);
143     button_t *button = get_button_at(event->event_x, event->event_y);
144     if (!button)
145         return;
146
147     /* We need to create a custom script containing our actual command
148      * since not every terminal emulator which is contained in
149      * i3-sensible-terminal supports -e with multiple arguments (and not
150      * all of them support -e with one quoted argument either).
151      *
152      * NB: The paths need to be unique, that is, don’t assume users close
153      * their nagbars at any point in time (and they still need to work).
154      * */
155     char *script_path = get_process_filename("nagbar-cmd");
156
157     int fd = open(script_path, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
158     if (fd == -1) {
159         warn("Could not create temporary script to store the nagbar command");
160         return;
161     }
162     FILE *script = fdopen(fd, "w");
163     if (script == NULL) {
164         warn("Could not fdopen() temporary script to store the nagbar command");
165         return;
166     }
167     fprintf(script, "#!/bin/sh\nrm %s\n%s", script_path, button->action);
168     /* Also closes fd */
169     fclose(script);
170
171     char *link_path;
172     char *exe_path = get_exe_path(argv0);
173     sasprintf(&link_path, "%s.nagbar_cmd", script_path);
174     if (symlink(exe_path, link_path) == -1) {
175         err(EXIT_FAILURE, "Failed to symlink %s to %s", link_path, exe_path);
176     }
177
178     char *terminal_cmd;
179     sasprintf(&terminal_cmd, "i3-sensible-terminal -e %s", link_path);
180     printf("argv0 = %s\n", argv0);
181     printf("terminal_cmd = %s\n", terminal_cmd);
182
183     start_application(terminal_cmd);
184
185     free(link_path);
186     free(terminal_cmd);
187     free(script_path);
188     free(exe_path);
189
190     /* TODO: unset flag, re-render */
191 }
192
193 /*
194  * Handles expose events (redraws of the window) and rendering in general. Will
195  * be called from the code with event == NULL or from X with event != NULL.
196  *
197  */
198 static int handle_expose(xcb_connection_t *conn, xcb_expose_event_t *event) {
199     /* re-draw the background */
200     xcb_change_gc(conn, pixmap_gc, XCB_GC_FOREGROUND, (uint32_t[]){color_background.colorpixel});
201     xcb_poly_fill_rectangle(conn, pixmap, pixmap_gc, 1, &rect);
202
203     /* restore font color */
204     set_font_colors(pixmap_gc, color_text, color_background);
205     draw_text(prompt, pixmap, pixmap_gc, NULL,
206               logical_px(4) + logical_px(4),
207               logical_px(4) + logical_px(4),
208               rect.width - logical_px(4) - logical_px(4));
209
210     /* render close button */
211     const char *close_button_label = "X";
212     int line_width = logical_px(4);
213     /* set width to the width of the label */
214     int w = predict_text_width(i3string_from_utf8(close_button_label));
215     /* account for left/right padding, which seems to be set to 8px (total) below */
216     w += logical_px(8);
217     int y = rect.width;
218     uint32_t values[3];
219     values[0] = color_button_background.colorpixel;
220     values[1] = line_width;
221     xcb_change_gc(conn, pixmap_gc, XCB_GC_FOREGROUND | XCB_GC_LINE_WIDTH, values);
222
223     xcb_rectangle_t close = {y - w - (2 * line_width), 0, w + (2 * line_width), rect.height};
224     xcb_poly_fill_rectangle(conn, pixmap, pixmap_gc, 1, &close);
225
226     xcb_change_gc(conn, pixmap_gc, XCB_GC_FOREGROUND, (uint32_t[]){color_border.colorpixel});
227     xcb_point_t points[] = {
228         {y - w - (2 * line_width), line_width / 2},
229         {y - (line_width / 2), line_width / 2},
230         {y - (line_width / 2), (rect.height - (line_width / 2)) - logical_px(2)},
231         {y - w - (2 * line_width), (rect.height - (line_width / 2)) - logical_px(2)},
232         {y - w - (2 * line_width), line_width / 2}};
233     xcb_poly_line(conn, XCB_COORD_MODE_ORIGIN, pixmap, pixmap_gc, 5, points);
234
235     values[0] = 1;
236     set_font_colors(pixmap_gc, color_text, color_button_background);
237     /* the x term here seems to set left/right padding */
238     draw_text_ascii(close_button_label, pixmap, pixmap_gc,
239                     y - w - line_width + w / 2 - logical_px(4),
240                     logical_px(4) + logical_px(3),
241                     rect.width - y + w + line_width - w / 2 + logical_px(4));
242     y -= w;
243
244     y -= logical_px(20);
245
246     /* render custom buttons */
247     line_width = 1;
248     for (int c = 0; c < buttoncnt; c++) {
249         /* set w to the width of the label */
250         w = predict_text_width(buttons[c].label);
251         /* account for left/right padding, which seems to be set to 12px (total) below */
252         w += logical_px(12);
253         y -= logical_px(30);
254         xcb_change_gc(conn, pixmap_gc, XCB_GC_FOREGROUND, (uint32_t[]){color_button_background.colorpixel});
255         close = (xcb_rectangle_t){y - w - (2 * line_width), logical_px(2), w + (2 * line_width), rect.height - logical_px(6)};
256         xcb_poly_fill_rectangle(conn, pixmap, pixmap_gc, 1, &close);
257
258         xcb_change_gc(conn, pixmap_gc, XCB_GC_FOREGROUND, (uint32_t[]){color_border.colorpixel});
259         buttons[c].x = y - w - (2 * line_width);
260         buttons[c].width = w;
261         xcb_point_t points2[] = {
262             {y - w - (2 * line_width), (line_width / 2) + logical_px(2)},
263             {y - (line_width / 2), (line_width / 2) + logical_px(2)},
264             {y - (line_width / 2), (rect.height - logical_px(4) - (line_width / 2))},
265             {y - w - (2 * line_width), (rect.height - logical_px(4) - (line_width / 2))},
266             {y - w - (2 * line_width), (line_width / 2) + logical_px(2)}};
267         xcb_poly_line(conn, XCB_COORD_MODE_ORIGIN, pixmap, pixmap_gc, 5, points2);
268
269         values[0] = color_text.colorpixel;
270         values[1] = color_button_background.colorpixel;
271         set_font_colors(pixmap_gc, color_text, color_button_background);
272         /* the x term seems to set left/right padding */
273         draw_text(buttons[c].label, pixmap, pixmap_gc, NULL,
274                   y - w - line_width + logical_px(6),
275                   logical_px(4) + logical_px(3),
276                   rect.width - y + w + line_width - logical_px(6));
277
278         y -= w;
279     }
280
281     /* border line at the bottom */
282     line_width = logical_px(2);
283     values[0] = color_border_bottom.colorpixel;
284     values[1] = line_width;
285     xcb_change_gc(conn, pixmap_gc, XCB_GC_FOREGROUND | XCB_GC_LINE_WIDTH, values);
286     xcb_point_t bottom[] = {
287         {0, rect.height - 0},
288         {rect.width, rect.height - 0}};
289     xcb_poly_line(conn, XCB_COORD_MODE_ORIGIN, pixmap, pixmap_gc, 2, bottom);
290
291     /* Copy the contents of the pixmap to the real window */
292     xcb_copy_area(conn, pixmap, win, pixmap_gc, 0, 0, 0, 0, rect.width, rect.height);
293     xcb_flush(conn);
294
295     return 1;
296 }
297
298 /**
299  * Return the position and size the i3-nagbar window should use.
300  * This will be the primary output or a fallback if it cannot be determined.
301  */
302 static xcb_rectangle_t get_window_position(void) {
303     /* Default values if we cannot determine the primary output or its CRTC info. */
304     xcb_rectangle_t result = (xcb_rectangle_t){50, 50, 500, font.height + logical_px(8) + logical_px(8)};
305
306     xcb_randr_get_screen_resources_current_cookie_t rcookie = xcb_randr_get_screen_resources_current(conn, root);
307     xcb_randr_get_output_primary_cookie_t pcookie = xcb_randr_get_output_primary(conn, root);
308
309     xcb_randr_get_output_primary_reply_t *primary = NULL;
310     xcb_randr_get_screen_resources_current_reply_t *res = NULL;
311
312     if ((primary = xcb_randr_get_output_primary_reply(conn, pcookie, NULL)) == NULL) {
313         DLOG("Could not determine the primary output.\n");
314         goto free_resources;
315     }
316
317     if ((res = xcb_randr_get_screen_resources_current_reply(conn, rcookie, NULL)) == NULL) {
318         goto free_resources;
319     }
320
321     xcb_randr_get_output_info_reply_t *output =
322         xcb_randr_get_output_info_reply(conn,
323                                         xcb_randr_get_output_info(conn, primary->output, res->config_timestamp),
324                                         NULL);
325     if (output == NULL || output->crtc == XCB_NONE)
326         goto free_resources;
327
328     xcb_randr_get_crtc_info_reply_t *crtc =
329         xcb_randr_get_crtc_info_reply(conn,
330                                       xcb_randr_get_crtc_info(conn, output->crtc, res->config_timestamp),
331                                       NULL);
332     if (crtc == NULL)
333         goto free_resources;
334
335     DLOG("Found primary output on position x = %i / y = %i / w = %i / h = %i.\n",
336          crtc->x, crtc->y, crtc->width, crtc->height);
337     if (crtc->width == 0 || crtc->height == 0) {
338         DLOG("Primary output is not active, ignoring it.\n");
339         goto free_resources;
340     }
341
342     result.x = crtc->x;
343     result.y = crtc->y;
344     goto free_resources;
345
346 free_resources:
347     FREE(res);
348     FREE(primary);
349     return result;
350 }
351
352 int main(int argc, char *argv[]) {
353     /* The following lines are a terribly horrible kludge. Because terminal
354      * emulators have different ways of interpreting the -e command line
355      * argument (some need -e "less /etc/fstab", others need -e less
356      * /etc/fstab), we need to write commands to a script and then just run
357      * that script. However, since on some machines, $XDG_RUNTIME_DIR and
358      * $TMPDIR are mounted with noexec, we cannot directly execute the script
359      * either.
360      *
361      * Initially, we tried to pass the command via the environment variable
362      * _I3_NAGBAR_CMD. But turns out that some terminal emulators such as
363      * xfce4-terminal run all windows from a single master process and only
364      * pass on the command (not the environment) to that master process.
365      *
366      * Therefore, we symlink i3-nagbar (which MUST reside on an executable
367      * filesystem) with a special name and run that symlink. When i3-nagbar
368      * recognizes it’s started as a binary ending in .nagbar_cmd, it strips off
369      * the .nagbar_cmd suffix and runs /bin/sh on argv[0]. That way, we can run
370      * a shell script on a noexec filesystem.
371      *
372      * From a security point of view, i3-nagbar is just an alias to /bin/sh in
373      * certain circumstances. This should not open any new security issues, I
374      * hope. */
375     char *cmd = NULL;
376     const size_t argv0_len = strlen(argv[0]);
377     if (argv0_len > strlen(".nagbar_cmd") &&
378         strcmp(argv[0] + argv0_len - strlen(".nagbar_cmd"), ".nagbar_cmd") == 0) {
379         unlink(argv[0]);
380         cmd = sstrdup(argv[0]);
381         *(cmd + argv0_len - strlen(".nagbar_cmd")) = '\0';
382         execl("/bin/sh", "/bin/sh", cmd, NULL);
383         err(EXIT_FAILURE, "execv(/bin/sh, /bin/sh, %s)", cmd);
384     }
385
386     argv0 = argv[0];
387
388     char *pattern = sstrdup("pango:monospace 8");
389     int o, option_index = 0;
390     enum { TYPE_ERROR = 0,
391            TYPE_WARNING = 1 } bar_type = TYPE_ERROR;
392
393     static struct option long_options[] = {
394         {"version", no_argument, 0, 'v'},
395         {"font", required_argument, 0, 'f'},
396         {"button", required_argument, 0, 'b'},
397         {"help", no_argument, 0, 'h'},
398         {"message", required_argument, 0, 'm'},
399         {"type", required_argument, 0, 't'},
400         {0, 0, 0, 0}};
401
402     char *options_string = "b:f:m:t:vh";
403
404     prompt = i3string_from_utf8("Please do not run this program.");
405
406     while ((o = getopt_long(argc, argv, options_string, long_options, &option_index)) != -1) {
407         switch (o) {
408             case 'v':
409                 printf("i3-nagbar " I3_VERSION "\n");
410                 return 0;
411             case 'f':
412                 FREE(pattern);
413                 pattern = sstrdup(optarg);
414                 break;
415             case 'm':
416                 i3string_free(prompt);
417                 prompt = i3string_from_utf8(optarg);
418                 break;
419             case 't':
420                 bar_type = (strcasecmp(optarg, "warning") == 0 ? TYPE_WARNING : TYPE_ERROR);
421                 break;
422             case 'h':
423                 printf("i3-nagbar " I3_VERSION "\n");
424                 printf("i3-nagbar [-m <message>] [-b <button> <action>] [-t warning|error] [-f <font>] [-v]\n");
425                 return 0;
426             case 'b':
427                 buttons = srealloc(buttons, sizeof(button_t) * (buttoncnt + 1));
428                 buttons[buttoncnt].label = i3string_from_utf8(optarg);
429                 buttons[buttoncnt].action = argv[optind];
430                 printf("button with label *%s* and action *%s*\n",
431                        i3string_as_utf8(buttons[buttoncnt].label),
432                        buttons[buttoncnt].action);
433                 buttoncnt++;
434                 printf("now %d buttons\n", buttoncnt);
435                 if (optind < argc)
436                     optind++;
437                 break;
438         }
439     }
440
441     int screens;
442     if ((conn = xcb_connect(NULL, &screens)) == NULL ||
443         xcb_connection_has_error(conn))
444         die("Cannot open display\n");
445
446 /* Place requests for the atoms we need as soon as possible */
447 #define xmacro(atom) \
448     xcb_intern_atom_cookie_t atom##_cookie = xcb_intern_atom(conn, 0, strlen(#atom), #atom);
449 #include "atoms.xmacro"
450 #undef xmacro
451
452     root_screen = xcb_aux_get_screen(conn, screens);
453     root = root_screen->root;
454
455     if (bar_type == TYPE_ERROR) {
456         /* Red theme for error messages */
457         color_button_background = draw_util_hex_to_color("#680a0a");
458         color_background = draw_util_hex_to_color("#900000");
459         color_text = draw_util_hex_to_color("#ffffff");
460         color_border = draw_util_hex_to_color("#d92424");
461         color_border_bottom = draw_util_hex_to_color("#470909");
462     } else {
463         /* Yellowish theme for warnings */
464         color_button_background = draw_util_hex_to_color("#ffc100");
465         color_background = draw_util_hex_to_color("#ffa8000");
466         color_text = draw_util_hex_to_color("#000000");
467         color_border = draw_util_hex_to_color("#ab7100");
468         color_border_bottom = draw_util_hex_to_color("#ab7100");
469     }
470
471     font = load_font(pattern, true);
472     set_font(&font);
473
474 #if defined(__OpenBSD__)
475     if (pledge("stdio rpath wpath cpath getpw proc exec", NULL) == -1)
476         err(EXIT_FAILURE, "pledge");
477 #endif
478
479     xcb_rectangle_t win_pos = get_window_position();
480
481     xcb_cursor_t cursor;
482     xcb_cursor_context_t *cursor_ctx;
483     if (xcb_cursor_context_new(conn, root_screen, &cursor_ctx) == 0) {
484         cursor = xcb_cursor_load_cursor(cursor_ctx, "left_ptr");
485         xcb_cursor_context_free(cursor_ctx);
486     } else {
487         cursor = xcb_generate_id(conn);
488         i3Font cursor_font = load_font("cursor", false);
489         xcb_create_glyph_cursor(
490             conn,
491             cursor,
492             cursor_font.specific.xcb.id,
493             cursor_font.specific.xcb.id,
494             XCB_CURSOR_LEFT_PTR,
495             XCB_CURSOR_LEFT_PTR + 1,
496             0, 0, 0,
497             65535, 65535, 65535);
498     }
499
500     /* Open an input window */
501     win = xcb_generate_id(conn);
502
503     xcb_create_window(
504         conn,
505         XCB_COPY_FROM_PARENT,
506         win,                                                 /* the window id */
507         root,                                                /* parent == root */
508         win_pos.x, win_pos.y, win_pos.width, win_pos.height, /* dimensions */
509         0,                                                   /* x11 border = 0, we draw our own */
510         XCB_WINDOW_CLASS_INPUT_OUTPUT,
511         XCB_WINDOW_CLASS_COPY_FROM_PARENT, /* copy visual from parent */
512         XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK | XCB_CW_CURSOR,
513         (uint32_t[]){
514             0, /* back pixel: black */
515             XCB_EVENT_MASK_EXPOSURE |
516                 XCB_EVENT_MASK_STRUCTURE_NOTIFY |
517                 XCB_EVENT_MASK_BUTTON_PRESS |
518                 XCB_EVENT_MASK_BUTTON_RELEASE,
519             cursor});
520
521     /* Map the window (make it visible) */
522     xcb_map_window(conn, win);
523
524 /* Setup NetWM atoms */
525 #define xmacro(name)                                                                       \
526     do {                                                                                   \
527         xcb_intern_atom_reply_t *reply = xcb_intern_atom_reply(conn, name##_cookie, NULL); \
528         if (!reply)                                                                        \
529             die("Could not get atom " #name "\n");                                         \
530                                                                                            \
531         A_##name = reply->atom;                                                            \
532         free(reply);                                                                       \
533     } while (0);
534 #include "atoms.xmacro"
535 #undef xmacro
536
537     /* Set dock mode */
538     xcb_change_property(conn,
539                         XCB_PROP_MODE_REPLACE,
540                         win,
541                         A__NET_WM_WINDOW_TYPE,
542                         A_ATOM,
543                         32,
544                         1,
545                         (unsigned char *)&A__NET_WM_WINDOW_TYPE_DOCK);
546
547     /* Reserve some space at the top of the screen */
548     struct {
549         uint32_t left;
550         uint32_t right;
551         uint32_t top;
552         uint32_t bottom;
553         uint32_t left_start_y;
554         uint32_t left_end_y;
555         uint32_t right_start_y;
556         uint32_t right_end_y;
557         uint32_t top_start_x;
558         uint32_t top_end_x;
559         uint32_t bottom_start_x;
560         uint32_t bottom_end_x;
561     } __attribute__((__packed__)) strut_partial;
562     memset(&strut_partial, 0, sizeof(strut_partial));
563
564     strut_partial.top = font.height + logical_px(6);
565     strut_partial.top_start_x = 0;
566     strut_partial.top_end_x = 800;
567
568     xcb_change_property(conn,
569                         XCB_PROP_MODE_REPLACE,
570                         win,
571                         A__NET_WM_STRUT_PARTIAL,
572                         A_CARDINAL,
573                         32,
574                         12,
575                         &strut_partial);
576
577     /* Create pixmap */
578     pixmap = xcb_generate_id(conn);
579     pixmap_gc = xcb_generate_id(conn);
580     xcb_create_pixmap(conn, root_screen->root_depth, pixmap, win, 500, font.height + logical_px(8));
581     xcb_create_gc(conn, pixmap_gc, pixmap, 0, 0);
582
583     /* Grab the keyboard to get all input */
584     xcb_flush(conn);
585
586     xcb_generic_event_t *event;
587     while ((event = xcb_wait_for_event(conn)) != NULL) {
588         if (event->response_type == 0) {
589             fprintf(stderr, "X11 Error received! sequence %x\n", event->sequence);
590             continue;
591         }
592
593         /* Strip off the highest bit (set if the event is generated) */
594         int type = (event->response_type & 0x7F);
595
596         switch (type) {
597             case XCB_EXPOSE:
598                 handle_expose(conn, (xcb_expose_event_t *)event);
599                 break;
600
601             case XCB_BUTTON_PRESS:
602                 handle_button_press(conn, (xcb_button_press_event_t *)event);
603                 break;
604
605             case XCB_BUTTON_RELEASE:
606                 handle_button_release(conn, (xcb_button_release_event_t *)event);
607                 break;
608
609             case XCB_CONFIGURE_NOTIFY: {
610                 xcb_configure_notify_event_t *configure_notify = (xcb_configure_notify_event_t *)event;
611                 rect = (xcb_rectangle_t){
612                     configure_notify->x,
613                     configure_notify->y,
614                     configure_notify->width,
615                     configure_notify->height};
616
617                 /* Recreate the pixmap / gc */
618                 xcb_free_pixmap(conn, pixmap);
619                 xcb_free_gc(conn, pixmap_gc);
620
621                 xcb_create_pixmap(conn, root_screen->root_depth, pixmap, win, rect.width, rect.height);
622                 xcb_create_gc(conn, pixmap_gc, pixmap, 0, 0);
623                 break;
624             }
625         }
626
627         free(event);
628     }
629
630     FREE(pattern);
631
632     return 0;
633 }