]> git.sur5r.net Git - i3/i3/blob - i3bar/src/xcb.c
f717f487f7591d4d2ddaa95888018e0257ee0675
[i3/i3] / i3bar / src / xcb.c
1 /*
2  * i3bar - an xcb-based status- and ws-bar for i3
3  *
4  * © 2010 Axel Wagner and contributors
5  *
6  * See file LICNSE for license information
7  *
8  * src/xcb.c: Communicating with X
9  *
10  */
11 #include <xcb/xcb.h>
12 #include <xcb/xproto.h>
13 #include <xcb/xcb_atom.h>
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <unistd.h>
17 #include <fcntl.h>
18 #include <string.h>
19 #include <i3/ipc.h>
20 #include <ev.h>
21 #include <errno.h>
22 #include <limits.h>
23
24 #include <X11/Xlib.h>
25 #include <X11/XKBlib.h>
26 #include <X11/extensions/XKB.h>
27
28 #include "common.h"
29
30 /* We save the Atoms in an easy to access array, indexed by an enum */
31 enum {
32     #define ATOM_DO(name) name,
33     #include "xcb_atoms.def"
34     NUM_ATOMS
35 };
36
37 xcb_intern_atom_cookie_t atom_cookies[NUM_ATOMS];
38 xcb_atom_t               atoms[NUM_ATOMS];
39
40 /* Variables, that are the same for all functions at all times */
41 xcb_connection_t *xcb_connection;
42 xcb_screen_t     *xcb_screen;
43 xcb_window_t     xcb_root;
44 xcb_font_t       xcb_font;
45
46 /* We need to cache some data to speed up text-width-prediction */
47 xcb_query_font_reply_t *font_info;
48 int                    font_height;
49 xcb_charinfo_t         *font_table;
50
51 /* These are only relevant for XKB, which we only need for grabbing modifiers */
52 Display          *xkb_dpy;
53 int              xkb_event_base;
54 int              mod_pressed = 0;
55
56 /* Because the statusline is the same on all outputs, we have
57  * global buffer to render it on */
58 xcb_gcontext_t   statusline_ctx;
59 xcb_pixmap_t     statusline_pm;
60 uint32_t         statusline_width;
61
62 /* Event-Watchers, to interact with the user */
63 ev_prepare *xcb_prep;
64 ev_check   *xcb_chk;
65 ev_io      *xcb_io;
66 ev_io      *xkb_io;
67
68 /* The parsed colors */
69 struct xcb_colors_t {
70     uint32_t bar_fg;
71     uint32_t bar_bg;
72     uint32_t active_ws_fg;
73     uint32_t active_ws_bg;
74     uint32_t inactive_ws_fg;
75     uint32_t inactive_ws_bg;
76     uint32_t urgent_ws_bg;
77     uint32_t urgent_ws_fg;
78 };
79 struct xcb_colors_t colors;
80
81 /* We define xcb_request_failed as a macro to include the relevant line-number */
82 #define xcb_request_failed(cookie, err_msg) _xcb_request_failed(cookie, err_msg, __LINE__)
83 int _xcb_request_failed(xcb_void_cookie_t cookie, char *err_msg, int line) {
84     xcb_generic_error_t *err;
85     if ((err = xcb_request_check(xcb_connection, cookie)) != NULL) {
86         ELOG("%s. X Error Code: %d\n", err_msg, err->error_code);
87         return err->error_code;
88     }
89     return 0;
90 }
91
92 /*
93  * Predicts the length of text based on cached data.
94  * The string has to be encoded in ucs2 and glyph_len has to be the length
95  * of the string (in glyphs).
96  *
97  */
98 uint32_t predict_text_extents(xcb_char2b_t *text, uint32_t length) {
99     /* If we don't have per-character data, return the maximum width */
100     if (font_table == NULL) {
101         return (font_info->max_bounds.character_width * length);
102     }
103
104     uint32_t width = 0;
105     uint32_t i;
106
107     for (i = 0; i < length; i++) {
108         xcb_charinfo_t *info;
109         int row = text[i].byte1;
110         int col = text[i].byte2;
111
112         if (row < font_info->min_byte1 || row > font_info->max_byte1 ||
113             col < font_info->min_char_or_byte2 || col > font_info->max_char_or_byte2) {
114             continue;
115         }
116
117         /* Don't you ask me, how this one works… */
118         info = &font_table[((row - font_info->min_byte1) *
119                             (font_info->max_char_or_byte2 - font_info->min_char_or_byte2 + 1)) +
120                            (col - font_info->min_char_or_byte2)];
121
122         if (info->character_width != 0 ||
123             (info->right_side_bearing |
124              info->left_side_bearing |
125              info->ascent |
126              info->descent) != 0) {
127             width += info->character_width;
128         }
129     }
130
131     return width;
132 }
133
134 /*
135  * Draws text given in UCS-2-encoding to a given drawable and position
136  *
137  */
138 void draw_text(xcb_drawable_t drawable, xcb_gcontext_t ctx, int16_t x, int16_t y,
139                xcb_char2b_t *text, uint32_t glyph_count) {
140     int offset = 0;
141     int16_t pos_x = x;
142     int16_t font_ascent = font_info->font_ascent;
143
144     while (glyph_count > 0) {
145         uint8_t chunk_size = MIN(255, glyph_count);
146         uint32_t chunk_width = predict_text_extents(text + offset, chunk_size);
147
148         xcb_image_text_16(xcb_connection,
149                           chunk_size,
150                           drawable,
151                           ctx,
152                           pos_x, y + font_ascent,
153                           text + offset);
154
155         offset += chunk_size;
156         pos_x += chunk_width;
157         glyph_count -= chunk_size;
158     }
159 }
160
161 /*
162  * Converts a colorstring to a colorpixel as expected from xcb_change_gc.
163  * s is assumed to be in the format "rrggbb"
164  *
165  */
166 uint32_t get_colorpixel(const char *s) {
167     char strings[3][3] = { { s[0], s[1], '\0'} ,
168                            { s[2], s[3], '\0'} ,
169                            { s[4], s[5], '\0'} };
170     uint8_t r = strtol(strings[0], NULL, 16);
171     uint8_t g = strtol(strings[1], NULL, 16);
172     uint8_t b = strtol(strings[2], NULL, 16);
173     return (r << 16 | g << 8 | b);
174 }
175
176 /*
177  * Redraws the statusline to the buffer
178  *
179  */
180 void refresh_statusline() {
181     int glyph_count;
182
183     if (statusline == NULL) {
184         return;
185     }
186
187     xcb_char2b_t *text = (xcb_char2b_t*) convert_utf8_to_ucs2(statusline, &glyph_count);
188     statusline_width = predict_text_extents(text, glyph_count);
189
190     xcb_clear_area(xcb_connection, 0, statusline_pm, 0, 0, xcb_screen->width_in_pixels, font_height);
191     draw_text(statusline_pm, statusline_ctx, 0, 0, text, glyph_count);
192
193     FREE(text);
194 }
195
196 /*
197  * Hides all bars (unmaps them)
198  *
199  */
200 void hide_bars() {
201     if (!config.hide_on_modifier) {
202         return;
203     }
204
205     i3_output *walk;
206     SLIST_FOREACH(walk, outputs, slist) {
207         xcb_unmap_window(xcb_connection, walk->bar);
208     }
209     stop_child();
210 }
211
212 /*
213  * Unhides all bars (maps them)
214  *
215  */
216 void unhide_bars() {
217     if (!config.hide_on_modifier) {
218         return;
219     }
220
221     i3_output           *walk;
222     xcb_void_cookie_t   cookie;
223     uint32_t            mask;
224     uint32_t            values[5];
225
226     cont_child();
227
228     SLIST_FOREACH(walk, outputs, slist) {
229         if (walk->bar == XCB_NONE) {
230             continue;
231         }
232         mask = XCB_CONFIG_WINDOW_X |
233                XCB_CONFIG_WINDOW_Y |
234                XCB_CONFIG_WINDOW_WIDTH |
235                XCB_CONFIG_WINDOW_HEIGHT |
236                XCB_CONFIG_WINDOW_STACK_MODE;
237         values[0] = walk->rect.x;
238         values[1] = walk->rect.y + walk->rect.h - font_height - 6;
239         values[2] = walk->rect.w;
240         values[3] = font_height + 6;
241         values[4] = XCB_STACK_MODE_ABOVE;
242         DLOG("Reconfiguring Window for output %s to %d,%d\n", walk->name, values[0], values[1]);
243         cookie = xcb_configure_window_checked(xcb_connection,
244                                               walk->bar,
245                                               mask,
246                                               values);
247
248         if (xcb_request_failed(cookie, "Could not reconfigure window")) {
249             exit(EXIT_FAILURE);
250         }
251         xcb_map_window(xcb_connection, walk->bar);
252     }
253 }
254
255 /*
256  * Parse the colors into a format that we can use
257  *
258  */
259 void init_colors(const struct xcb_color_strings_t *new_colors) {
260 #define PARSE_COLOR(name, def) \
261     do { \
262         colors.name = get_colorpixel(new_colors->name ? new_colors->name : def); \
263     } while  (0)
264     PARSE_COLOR(bar_fg, "FFFFFF");
265     PARSE_COLOR(bar_bg, "000000");
266     PARSE_COLOR(active_ws_fg, "FFFFFF");
267     PARSE_COLOR(active_ws_bg, "480000");
268     PARSE_COLOR(inactive_ws_fg, "FFFFFF");
269     PARSE_COLOR(inactive_ws_bg, "240000");
270     PARSE_COLOR(urgent_ws_fg, "FFFFFF");
271     PARSE_COLOR(urgent_ws_bg, "002400");
272 #undef PARSE_COLOR
273 }
274
275 /*
276  * Handle a button-press-event (i.c. a mouse click on one of our bars).
277  * We determine, wether the click occured on a ws-button or if the scroll-
278  * wheel was used and change the workspace appropriately
279  *
280  */
281 void handle_button(xcb_button_press_event_t *event) {
282     i3_ws *cur_ws;
283
284     /* Determine, which bar was clicked */
285     i3_output *walk;
286     xcb_window_t bar = event->event;
287     SLIST_FOREACH(walk, outputs, slist) {
288         if (walk->bar == bar) {
289             break;
290         }
291     }
292
293     if (walk == NULL) {
294         DLOG("Unknown Bar klicked!\n");
295         return;
296     }
297
298     /* TODO: Move this to extern get_ws_for_output() */
299     TAILQ_FOREACH(cur_ws, walk->workspaces, tailq) {
300         if (cur_ws->visible) {
301             break;
302         }
303     }
304
305     if (cur_ws == NULL) {
306         DLOG("No Workspace active?\n");
307         return;
308     }
309
310     int32_t x = event->event_x;
311
312     DLOG("Got Button %d\n", event->detail);
313
314     switch (event->detail) {
315         case 1:
316             /* Left Mousbutton. We determine, which button was clicked
317              * and set cur_ws accordingly */
318             TAILQ_FOREACH(cur_ws, walk->workspaces, tailq) {
319                 DLOG("x = %d\n", x);
320                 if (x < cur_ws->name_width + 10) {
321                     break;
322                 }
323                 x -= cur_ws->name_width + 10;
324             }
325             if (cur_ws == NULL) {
326                 return;
327             }
328             break;
329         case 4:
330             /* Mouse wheel down. We select the next ws */
331             if (cur_ws == TAILQ_FIRST(walk->workspaces)) {
332                 cur_ws = TAILQ_LAST(walk->workspaces, ws_head);
333             } else {
334                 cur_ws = TAILQ_PREV(cur_ws, ws_head, tailq);
335             }
336             break;
337         case 5:
338             /* Mouse wheel up. We select the previos ws */
339             if (cur_ws == TAILQ_LAST(walk->workspaces, ws_head)) {
340                 cur_ws = TAILQ_FIRST(walk->workspaces);
341             } else {
342                 cur_ws = TAILQ_NEXT(cur_ws, tailq);
343             }
344             break;
345     }
346
347     char buffer[strlen(cur_ws->name) + 11];
348     snprintf(buffer, 50, "workspace %s", cur_ws->name);
349     i3_send_msg(I3_IPC_MESSAGE_TYPE_COMMAND, buffer);
350 }
351
352 /*
353  * This function is called immediately bevor the main loop locks. We flush xcb
354  * then (and only then)
355  *
356  */
357 void xcb_prep_cb(struct ev_loop *loop, ev_prepare *watcher, int revents) {
358     xcb_flush(xcb_connection);
359 }
360
361 /*
362  * This function is called immediately after the main loop locks, so when one
363  * of the watchers registered an event.
364  * We check wether an X-Event arrived and handle it.
365  *
366  */
367 void xcb_chk_cb(struct ev_loop *loop, ev_check *watcher, int revents) {
368     xcb_generic_event_t *event;
369     while ((event = xcb_poll_for_event(xcb_connection)) == NULL) {
370         return;
371     }
372
373     switch (event->response_type & ~0x80) {
374         case XCB_EXPOSE:
375             /* Expose-events happen, when the window needs to be redrawn */
376             redraw_bars();
377             break;
378         case XCB_BUTTON_PRESS:
379             /* Button-press-events are mouse-buttons clicked on one of our bars */
380             handle_button((xcb_button_press_event_t*) event);
381             break;
382     }
383     FREE(event);
384 }
385
386 /*
387  * Dummy Callback. We only need this, so that the Prepare- and Check-Watchers
388  * are triggered
389  *
390  */
391 void xcb_io_cb(struct ev_loop *loop, ev_io *watcher, int revents) {
392 }
393
394 /*
395  * We need to bind to the modifier per XKB. Sadly, XCB does not implement this
396  *
397  */
398 void xkb_io_cb(struct ev_loop *loop, ev_io *watcher, int revents) {
399     XkbEvent ev;
400     int modstate = 0;
401
402     DLOG("Got XKB-Event!\n");
403
404     while (XPending(xkb_dpy)) {
405         XNextEvent(xkb_dpy, (XEvent*)&ev);
406
407         if (ev.type != xkb_event_base) {
408             ELOG("No Xkb-Event!\n");
409             continue;
410         }
411
412         if (ev.any.xkb_type != XkbStateNotify) {
413             ELOG("No State Notify!\n");
414             continue;
415         }
416
417         unsigned int mods = ev.state.mods;
418         modstate = mods & Mod4Mask;
419     }
420
421     if (modstate != mod_pressed) {
422         if (modstate == 0) {
423             DLOG("Mod4 got released!\n");
424             hide_bars();
425         } else {
426             DLOG("Mod4 got pressed!\n");
427             unhide_bars();
428         }
429         mod_pressed = modstate;
430     }
431 }
432
433 /*
434  * Initialize xcb and use the specified fontname for text-rendering
435  *
436  */
437 char *init_xcb(char *fontname) {
438     /* FIXME: xcb_connect leaks Memory */
439     xcb_connection = xcb_connect(NULL, NULL);
440     if (xcb_connection_has_error(xcb_connection)) {
441         ELOG("Cannot open display\n");
442         exit(EXIT_FAILURE);
443     }
444     DLOG("Connected to xcb\n");
445
446     /* We have to request the atoms we need */
447     #define ATOM_DO(name) atom_cookies[name] = xcb_intern_atom(xcb_connection, 0, strlen(#name), #name);
448     #include "xcb_atoms.def"
449
450     xcb_screen = xcb_setup_roots_iterator(xcb_get_setup(xcb_connection)).data;
451     xcb_root = xcb_screen->root;
452
453     /* We load and allocate the font */
454     xcb_font = xcb_generate_id(xcb_connection);
455     xcb_void_cookie_t open_font_cookie;
456     open_font_cookie = xcb_open_font_checked(xcb_connection,
457                                              xcb_font,
458                                              strlen(fontname),
459                                              fontname);
460
461     /* We need to save info about the font, because we need the fonts height and
462      * information about the width of characters */
463     xcb_query_font_cookie_t query_font_cookie;
464     query_font_cookie = xcb_query_font(xcb_connection,
465                                        xcb_font);
466
467     /* To grab modifiers without blocking other applications from receiving key-events
468      * involving that modifier, we sadly have to use xkb which is not yet fully supported
469      * in xcb */
470     if (config.hide_on_modifier) {
471         int xkb_major, xkb_minor, xkb_errbase, xkb_err;
472         xkb_major = XkbMajorVersion;
473         xkb_minor = XkbMinorVersion;
474
475         char *dispname = getenv("DISPLAY");
476         if (dispname == NULL) {
477             dispname = ":0";
478         }
479         xkb_dpy = XkbOpenDisplay(dispname,
480                                  &xkb_event_base,
481                                  &xkb_errbase,
482                                  &xkb_major,
483                                  &xkb_minor,
484                                  &xkb_err);
485
486         if (xkb_dpy == NULL) {
487             ELOG("No XKB!\n");
488             exit(EXIT_FAILURE);
489         }
490
491         if (fcntl(ConnectionNumber(xkb_dpy), F_SETFD, FD_CLOEXEC) == -1) {
492             ELOG("Could not set FD_CLOEXEC on xkbdpy: %s\n", strerror(errno));
493             exit(EXIT_FAILURE);
494         }
495
496         int i1;
497         if (!XkbQueryExtension(xkb_dpy, &i1, &xkb_event_base, &xkb_errbase, &xkb_major, &xkb_minor)) {
498             ELOG("XKB not supported by X-server!\n");
499             exit(EXIT_FAILURE);
500         }
501
502         if (!XkbSelectEvents(xkb_dpy, XkbUseCoreKbd, XkbStateNotifyMask, XkbStateNotifyMask)) {
503             ELOG("Could not grab Key!\n");
504             exit(EXIT_FAILURE);
505         }
506
507         xkb_io = malloc(sizeof(ev_io));
508         ev_io_init(xkb_io, &xkb_io_cb, ConnectionNumber(xkb_dpy), EV_READ);
509         ev_io_start(main_loop, xkb_io);
510         XFlush(xkb_dpy);
511     }
512
513     /* We draw the statusline to a seperate pixmap, because it looks the same on all bars and
514      * this way, we can choose to crop it */
515     statusline_ctx = xcb_generate_id(xcb_connection);
516     uint32_t mask = XCB_GC_FOREGROUND |
517                     XCB_GC_BACKGROUND |
518                     XCB_GC_FONT;
519     uint32_t vals[3] = { colors.bar_fg, colors.bar_bg, xcb_font };
520
521     xcb_void_cookie_t sl_ctx_cookie = xcb_create_gc_checked(xcb_connection,
522                                                             statusline_ctx,
523                                                             xcb_root,
524                                                             mask,
525                                                             vals);
526
527     statusline_pm = xcb_generate_id(xcb_connection);
528     xcb_void_cookie_t sl_pm_cookie = xcb_create_pixmap_checked(xcb_connection,
529                                                                xcb_screen->root_depth,
530                                                                statusline_pm,
531                                                                xcb_root,
532                                                                xcb_screen->width_in_pixels,
533                                                                xcb_screen->height_in_pixels);
534
535
536     /* The varios Watchers to communicate with xcb */
537     xcb_io = malloc(sizeof(ev_io));
538     xcb_prep = malloc(sizeof(ev_prepare));
539     xcb_chk = malloc(sizeof(ev_check));
540
541     ev_io_init(xcb_io, &xcb_io_cb, xcb_get_file_descriptor(xcb_connection), EV_READ);
542     ev_prepare_init(xcb_prep, &xcb_prep_cb);
543     ev_check_init(xcb_chk, &xcb_chk_cb);
544
545     ev_io_start(main_loop, xcb_io);
546     ev_prepare_start(main_loop, xcb_prep);
547     ev_check_start(main_loop, xcb_chk);
548
549     /* Now we get the atoms and save them in a nice data-structure */
550     get_atoms();
551
552     xcb_get_property_cookie_t path_cookie;
553     path_cookie = xcb_get_property_unchecked(xcb_connection,
554                                    0,
555                                    xcb_root,
556                                    atoms[I3_SOCKET_PATH],
557                                    XCB_GET_PROPERTY_TYPE_ANY,
558                                    0, PATH_MAX);
559
560     /* We check, if i3 set it's socket-path */
561     xcb_get_property_reply_t *path_reply = xcb_get_property_reply(xcb_connection,
562                                                                   path_cookie,
563                                                                   NULL);
564     char *path = NULL;
565     if (path_reply) {
566         int len = xcb_get_property_value_length(path_reply);
567         if (len != 0) {
568             path = strndup(xcb_get_property_value(path_reply), len);
569         }
570     }
571
572     /* Now we save the font-infos */
573     font_info = xcb_query_font_reply(xcb_connection,
574                                      query_font_cookie,
575                                      NULL);
576
577     if (xcb_request_failed(open_font_cookie, "Could not open font")) {
578         exit(EXIT_FAILURE);
579     }
580
581     font_height = font_info->font_ascent + font_info->font_descent;
582
583     if (xcb_query_font_char_infos_length(font_info) == 0) {
584         font_table = NULL;
585     } else {
586         font_table = xcb_query_font_char_infos(font_info);
587     }
588
589     DLOG("Calculated Font-height: %d\n", font_height);
590
591     if (xcb_request_failed(sl_pm_cookie, "Could not allocate statusline-buffer")) {
592         exit(EXIT_FAILURE);
593     }
594
595     if (xcb_request_failed(sl_ctx_cookie, "Could not create context for statusline")) {
596         exit(EXIT_FAILURE);
597     }
598
599     return path;
600 }
601
602 /*
603  * Cleanup the xcb-stuff.
604  * Called once, before the program terminates.
605  *
606  */
607 void clean_xcb() {
608     i3_output *o_walk;
609     free_workspaces();
610     SLIST_FOREACH(o_walk, outputs, slist) {
611         destroy_window(o_walk);
612         FREE(o_walk->workspaces);
613         FREE(o_walk->name);
614     }
615     FREE_SLIST(outputs, i3_output);
616     FREE(outputs);
617
618     xcb_disconnect(xcb_connection);
619
620     ev_check_stop(main_loop, xcb_chk);
621     ev_prepare_stop(main_loop, xcb_prep);
622     ev_io_stop(main_loop, xcb_io);
623
624     FREE(xcb_chk);
625     FREE(xcb_prep);
626     FREE(xcb_io);
627     FREE(font_info);
628 }
629
630 /*
631  * Get the earlier requested atoms and save them in the prepared data-structure
632  *
633  */
634 void get_atoms() {
635     xcb_intern_atom_reply_t *reply;
636     #define ATOM_DO(name) reply = xcb_intern_atom_reply(xcb_connection, atom_cookies[name], NULL); \
637         if (reply == NULL) { \
638             ELOG("Could not get atom %s\n", #name); \
639             exit(EXIT_FAILURE); \
640         } \
641         atoms[name] = reply->atom; \
642         free(reply);
643
644     #include "xcb_atoms.def"
645     DLOG("Got Atoms\n");
646 }
647
648 /*
649  * Destroy the bar of the specified output
650  *
651  */
652 void destroy_window(i3_output *output) {
653     if (output == NULL) {
654         return;
655     }
656     if (output->bar == XCB_NONE) {
657         return;
658     }
659     xcb_destroy_window(xcb_connection, output->bar);
660     output->bar = XCB_NONE;
661 }
662
663 /*
664  * Reallocate the statusline-buffer
665  *
666  */
667 void realloc_sl_buffer() {
668     xcb_free_pixmap(xcb_connection, statusline_pm);
669     statusline_pm = xcb_generate_id(xcb_connection);
670     xcb_void_cookie_t sl_pm_cookie = xcb_create_pixmap_checked(xcb_connection,
671                                                                xcb_screen->root_depth,
672                                                                statusline_pm,
673                                                                xcb_root,
674                                                                xcb_screen->width_in_pixels,
675                                                                xcb_screen->height_in_pixels);
676     if (xcb_request_failed(sl_pm_cookie, "Could not allocate statusline-buffer")) {
677         exit(EXIT_FAILURE);
678     }
679
680 }
681
682 /*
683  * Reconfigure all bars and create new for newly activated outputs
684  *
685  */
686 void reconfig_windows() {
687     uint32_t mask;
688     uint32_t values[5];
689
690     i3_output *walk;
691     SLIST_FOREACH(walk, outputs, slist) {
692         if (!walk->active) {
693             /* If an output is not active, we destroy it's bar */
694             /* FIXME: Maybe we rather want to unmap? */
695             DLOG("Destroying window for output %s\n", walk->name);
696             destroy_window(walk);
697             continue;
698         }
699         if (walk->bar == XCB_NONE) {
700             DLOG("Creating Window for output %s\n", walk->name);
701
702             walk->bar = xcb_generate_id(xcb_connection);
703             walk->buffer = xcb_generate_id(xcb_connection);
704             mask = XCB_CW_BACK_PIXEL | XCB_CW_OVERRIDE_REDIRECT | XCB_CW_EVENT_MASK;
705             /* Black background */
706             values[0] = colors.bar_bg;
707             /* If hide_on_modifier is set, i3 is not supposed to manage our bar-windows */
708             values[1] = config.hide_on_modifier;
709             /* The events we want to receive */
710             values[2] = XCB_EVENT_MASK_EXPOSURE |
711                         XCB_EVENT_MASK_BUTTON_PRESS;
712             xcb_void_cookie_t win_cookie = xcb_create_window_checked(xcb_connection,
713                                                                      xcb_screen->root_depth,
714                                                                      walk->bar,
715                                                                      xcb_root,
716                                                                      walk->rect.x, walk->rect.y + walk->rect.h - font_height - 6,
717                                                                      walk->rect.w, font_height + 6,
718                                                                      1,
719                                                                      XCB_WINDOW_CLASS_INPUT_OUTPUT,
720                                                                      xcb_screen->root_visual,
721                                                                      mask,
722                                                                      values);
723
724             /* The double-buffer we use to render stuff off-screen */
725             xcb_void_cookie_t pm_cookie = xcb_create_pixmap_checked(xcb_connection,
726                                                                     xcb_screen->root_depth,
727                                                                     walk->buffer,
728                                                                     walk->bar,
729                                                                     walk->rect.w,
730                                                                     walk->rect.h);
731
732             /* We want dock-windows (for now). When override_redirect is set, i3 is ignoring
733              * this one */
734             xcb_void_cookie_t dock_cookie = xcb_change_property(xcb_connection,
735                                                                 XCB_PROP_MODE_REPLACE,
736                                                                 walk->bar,
737                                                                 atoms[_NET_WM_WINDOW_TYPE],
738                                                                 XCB_ATOM_ATOM,
739                                                                 32,
740                                                                 1,
741                                                                 (unsigned char*) &atoms[_NET_WM_WINDOW_TYPE_DOCK]);
742
743             /* We need to tell i3, where to reserve space for i3bar */
744             /* left, right, top, bottom, left_start_y, left_end_y,
745              * right_start_y, right_end_y, top_start_x, top_end_x, bottom_start_x,
746              * bottom_end_x */
747             /* A local struct to save the strut_partial property */
748             struct {
749                 uint32_t left;
750                 uint32_t right;
751                 uint32_t top;
752                 uint32_t bottom;
753                 uint32_t left_start_y;
754                 uint32_t left_end_y;
755                 uint32_t right_start_y;
756                 uint32_t right_end_y;
757                 uint32_t top_start_x;
758                 uint32_t top_end_x;
759                 uint32_t bottom_start_x;
760                 uint32_t bottom_end_x;
761             } __attribute__((__packed__)) strut_partial = {0,};
762             switch (config.dockpos) {
763                 case DOCKPOS_NONE:
764                     break;
765                 case DOCKPOS_TOP:
766                     strut_partial.top = font_height + 6;
767                     strut_partial.top_start_x = walk->rect.x;
768                     strut_partial.top_end_x = walk->rect.x + walk->rect.w;
769                     break;
770                 case DOCKPOS_BOT:
771                     strut_partial.bottom = font_height + 6;
772                     strut_partial.bottom_start_x = walk->rect.x;
773                     strut_partial.bottom_end_x = walk->rect.x + walk->rect.w;
774                     break;
775             }
776             xcb_void_cookie_t strut_cookie = xcb_change_property(xcb_connection,
777                                                                  XCB_PROP_MODE_REPLACE,
778                                                                  walk->bar,
779                                                                  atoms[_NET_WM_STRUT_PARTIAL],
780                                                                  XCB_ATOM_CARDINAL,
781                                                                  32,
782                                                                  12,
783                                                                  &strut_partial);
784
785             /* We also want a graphics-context for the bars (it defines the properties
786              * with which we draw to them) */
787             walk->bargc = xcb_generate_id(xcb_connection);
788             mask = XCB_GC_FONT;
789             values[0] = xcb_font;
790             xcb_void_cookie_t gc_cookie = xcb_create_gc_checked(xcb_connection,
791                                                                 walk->bargc,
792                                                                 walk->bar,
793                                                                 mask,
794                                                                 values);
795
796             /* We finally map the bar (display it on screen), unless the modifier-switch is on */
797             xcb_void_cookie_t map_cookie;
798             if (!config.hide_on_modifier) {
799                 map_cookie = xcb_map_window_checked(xcb_connection, walk->bar);
800             }
801
802             if (xcb_request_failed(win_cookie,   "Could not create window") ||
803                 xcb_request_failed(pm_cookie,    "Could not create pixmap") ||
804                 xcb_request_failed(dock_cookie,  "Could not set dock mode") ||
805                 xcb_request_failed(strut_cookie, "Could not set strut")     ||
806                 xcb_request_failed(gc_cookie,    "Could not create graphical context") ||
807                 (!config.hide_on_modifier && xcb_request_failed(map_cookie, "Could not map window"))) {
808                 exit(EXIT_FAILURE);
809             }
810         } else {
811             /* We already have a bar, so we just reconfigure it */
812             mask = XCB_CONFIG_WINDOW_X |
813                    XCB_CONFIG_WINDOW_Y |
814                    XCB_CONFIG_WINDOW_WIDTH |
815                    XCB_CONFIG_WINDOW_HEIGHT |
816                    XCB_CONFIG_WINDOW_STACK_MODE;
817             values[0] = walk->rect.x;
818             values[1] = walk->rect.y + walk->rect.h - font_height - 6;
819             values[2] = walk->rect.w;
820             values[3] = font_height + 6;
821             values[4] = XCB_STACK_MODE_ABOVE;
822
823             DLOG("Destroying buffer for output %s", walk->name);
824             xcb_free_pixmap(xcb_connection, walk->buffer);
825
826             DLOG("Reconfiguring Window for output %s to %d,%d\n", walk->name, values[0], values[1]);
827             xcb_void_cookie_t cfg_cookie = xcb_configure_window_checked(xcb_connection,
828                                                                         walk->bar,
829                                                                         mask,
830                                                                         values);
831
832             DLOG("Recreating buffer for output %s", walk->name);
833             xcb_void_cookie_t pm_cookie = xcb_create_pixmap_checked(xcb_connection,
834                                                                     xcb_screen->root_depth,
835                                                                     walk->buffer,
836                                                                     walk->bar,
837                                                                     walk->rect.w,
838                                                                     walk->rect.h);
839
840             if (xcb_request_failed(cfg_cookie, "Could not reconfigure window")) {
841                 exit(EXIT_FAILURE);
842             }
843             if (xcb_request_failed(pm_cookie,  "Could not create pixmap")) {
844                 exit(EXIT_FAILURE);
845             }
846         }
847     }
848 }
849
850 /*
851  * Render the bars, with buttons and statusline
852  *
853  */
854 void draw_bars() {
855     DLOG("Drawing Bars...\n");
856     int i = 0;
857
858     refresh_statusline();
859
860     i3_output *outputs_walk;
861     SLIST_FOREACH(outputs_walk, outputs, slist) {
862         if (!outputs_walk->active) {
863             DLOG("Output %s inactive, skipping...\n", outputs_walk->name);
864             continue;
865         }
866         if (outputs_walk->bar == XCB_NONE) {
867             /* Oh shit, an active output without an own bar. Create it now! */
868             reconfig_windows();
869         }
870         /* First things first: clear the backbuffer */
871         uint32_t color = colors.bar_bg;
872         xcb_change_gc(xcb_connection,
873                       outputs_walk->bargc,
874                       XCB_GC_FOREGROUND,
875                       &color);
876         xcb_rectangle_t rect = { 0, 0, outputs_walk->rect.w, font_height + 6 };
877         xcb_poly_fill_rectangle(xcb_connection,
878                                 outputs_walk->buffer,
879                                 outputs_walk->bargc,
880                                 1,
881                                 &rect);
882
883         if (statusline != NULL) {
884             DLOG("Printing statusline!\n");
885
886             /* Luckily we already prepared a seperate pixmap containing the rendered
887              * statusline, we just have to copy the relevant parts to the relevant
888              * position */
889             xcb_copy_area(xcb_connection,
890                           statusline_pm,
891                           outputs_walk->buffer,
892                           outputs_walk->bargc,
893                           MAX(0, (int16_t)(statusline_width - outputs_walk->rect.w + 4)), 0,
894                           MAX(0, (int16_t)(outputs_walk->rect.w - statusline_width - 4)), 3,
895                           MIN(outputs_walk->rect.w - 4, statusline_width), font_height);
896         }
897
898         i3_ws *ws_walk;
899         TAILQ_FOREACH(ws_walk, outputs_walk->workspaces, tailq) {
900             DLOG("Drawing Button for WS %s at x = %d\n", ws_walk->name, i);
901             uint32_t fg_color = colors.inactive_ws_fg;
902             uint32_t bg_color = colors.inactive_ws_bg;
903             if (ws_walk->visible) {
904                 fg_color = colors.active_ws_fg;
905                 bg_color = colors.active_ws_bg;
906             }
907             if (ws_walk->urgent) {
908                 DLOG("WS %s is urgent!\n", ws_walk->name);
909                 fg_color = colors.urgent_ws_fg;
910                 bg_color = colors.urgent_ws_bg;
911                 /* The urgent-hint should get noticed, so we unhide the bars shortly */
912                 unhide_bars();
913             }
914             xcb_change_gc(xcb_connection,
915                           outputs_walk->bargc,
916                           XCB_GC_FOREGROUND,
917                           &bg_color);
918             xcb_change_gc(xcb_connection,
919                           outputs_walk->bargc,
920                           XCB_GC_BACKGROUND,
921                           &bg_color);
922             xcb_rectangle_t rect = { i + 1, 1, ws_walk->name_width + 8, font_height + 4 };
923             xcb_poly_fill_rectangle(xcb_connection,
924                                     outputs_walk->buffer,
925                                     outputs_walk->bargc,
926                                     1,
927                                     &rect);
928             xcb_change_gc(xcb_connection,
929                           outputs_walk->bargc,
930                           XCB_GC_FOREGROUND,
931                           &fg_color);
932             xcb_image_text_16(xcb_connection,
933                               ws_walk->name_glyphs,
934                               outputs_walk->buffer,
935                               outputs_walk->bargc,
936                               i + 5, font_info->font_ascent + 2,
937                               ws_walk->ucs2_name);
938             i += 10 + ws_walk->name_width;
939         }
940
941         redraw_bars();
942
943         i = 0;
944     }
945 }
946
947 /*
948  * Redraw the bars, i.e. simply copy the buffer to the barwindow
949  *
950  */
951 void redraw_bars() {
952     i3_output *outputs_walk;
953     SLIST_FOREACH(outputs_walk, outputs, slist) {
954         xcb_copy_area(xcb_connection,
955                       outputs_walk->buffer,
956                       outputs_walk->bar,
957                       outputs_walk->bargc,
958                       0, 0,
959                       0, 0,
960                       outputs_walk->rect.w,
961                       outputs_walk->rect.h);
962         xcb_flush(xcb_connection);
963     }
964 }