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