]> git.sur5r.net Git - i3/i3/blob - i3bar/src/xcb.c
Use DISPLAY in XKB-code
[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
23 #include <X11/Xlib.h>
24 #include <X11/XKBlib.h>
25 #include <X11/extensions/XKB.h>
26
27 #include "common.h"
28
29 /* We save the Atoms in an easy to access array, indexed by an enum */
30 #define NUM_ATOMS 3
31
32 enum {
33     #define ATOM_DO(name) name,
34     #include "xcb_atoms.def"
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_free_pixmap(xcb_connection, statusline_pm);
191     statusline_pm = xcb_generate_id(xcb_connection);
192     xcb_void_cookie_t sl_pm_cookie = xcb_create_pixmap_checked(xcb_connection,
193                                                                xcb_screen->root_depth,
194                                                                statusline_pm,
195                                                                xcb_root,
196                                                                statusline_width,
197                                                                font_height);
198
199     draw_text(statusline_pm, statusline_ctx, 0, 0, text, glyph_count);
200
201     FREE(text);
202
203     if (xcb_request_failed(sl_pm_cookie, "Could not allocate statusline-buffer")) {
204         exit(EXIT_FAILURE);
205     }
206 }
207
208 /*
209  * Hides all bars (unmaps them)
210  *
211  */
212 void hide_bars() {
213     if (!config.hide_on_modifier) {
214         return;
215     }
216
217     i3_output *walk;
218     SLIST_FOREACH(walk, outputs, slist) {
219         xcb_unmap_window(xcb_connection, walk->bar);
220     }
221     stop_child();
222 }
223
224 /*
225  * Unhides all bars (maps them)
226  *
227  */
228 void unhide_bars() {
229     if (!config.hide_on_modifier) {
230         return;
231     }
232
233     i3_output           *walk;
234     xcb_void_cookie_t   cookie;
235     uint32_t            mask;
236     uint32_t            values[5];
237
238     cont_child();
239
240     SLIST_FOREACH(walk, outputs, slist) {
241         if (walk->bar == XCB_NONE) {
242             continue;
243         }
244         mask = XCB_CONFIG_WINDOW_X |
245                XCB_CONFIG_WINDOW_Y |
246                XCB_CONFIG_WINDOW_WIDTH |
247                XCB_CONFIG_WINDOW_HEIGHT |
248                XCB_CONFIG_WINDOW_STACK_MODE;
249         values[0] = walk->rect.x;
250         values[1] = walk->rect.y + walk->rect.h - font_height - 6;
251         values[2] = walk->rect.w;
252         values[3] = font_height + 6;
253         values[4] = XCB_STACK_MODE_ABOVE;
254         DLOG("Reconfiguring Window for output %s to %d,%d\n", walk->name, values[0], values[1]);
255         cookie = xcb_configure_window_checked(xcb_connection,
256                                               walk->bar,
257                                               mask,
258                                               values);
259
260         if (xcb_request_failed(cookie, "Could not reconfigure window")) {
261             exit(EXIT_FAILURE);
262         }
263         xcb_map_window(xcb_connection, walk->bar);
264     }
265 }
266
267 /*
268  * Parse the colors into a format that we can use
269  *
270  */
271 void init_colors(const struct xcb_color_strings_t *new_colors) {
272 #define PARSE_COLOR(name, def) \
273     do { \
274         colors.name = get_colorpixel(new_colors->name ? new_colors->name : def); \
275     } while  (0)
276     PARSE_COLOR(bar_fg, "FFFFFF");
277     PARSE_COLOR(bar_bg, "000000");
278     PARSE_COLOR(active_ws_fg, "FFFFFF");
279     PARSE_COLOR(active_ws_bg, "480000");
280     PARSE_COLOR(inactive_ws_fg, "FFFFFF");
281     PARSE_COLOR(inactive_ws_bg, "240000");
282     PARSE_COLOR(urgent_ws_fg, "FFFFFF");
283     PARSE_COLOR(urgent_ws_bg, "002400");
284 #undef PARSE_COLOR
285 }
286
287 /*
288  * Handle a button-press-event (i.c. a mouse click on one of our bars).
289  * We determine, wether the click occured on a ws-button or if the scroll-
290  * wheel was used and change the workspace appropriately
291  *
292  */
293 void handle_button(xcb_button_press_event_t *event) {
294     i3_ws *cur_ws;
295
296     /* Determine, which bar was clicked */
297     i3_output *walk;
298     xcb_window_t bar = event->event;
299     SLIST_FOREACH(walk, outputs, slist) {
300         if (walk->bar == bar) {
301             break;
302         }
303     }
304
305     if (walk == NULL) {
306         DLOG("Unknown Bar klicked!\n");
307         return;
308     }
309
310     /* TODO: Move this to extern get_ws_for_output() */
311     TAILQ_FOREACH(cur_ws, walk->workspaces, tailq) {
312         if (cur_ws->visible) {
313             break;
314         }
315     }
316
317     if (cur_ws == NULL) {
318         DLOG("No Workspace active?\n");
319         return;
320     }
321
322     int32_t x = event->event_x;
323
324     DLOG("Got Button %d\n", event->detail);
325
326     switch (event->detail) {
327         case 1:
328             /* Left Mousbutton. We determine, which button was clicked
329              * and set cur_ws accordingly */
330             TAILQ_FOREACH(cur_ws, walk->workspaces, tailq) {
331                 DLOG("x = %d\n", x);
332                 if (x < cur_ws->name_width + 10) {
333                     break;
334                 }
335                 x -= cur_ws->name_width + 10;
336             }
337             if (cur_ws == NULL) {
338                 return;
339             }
340             break;
341         case 4:
342             /* Mouse wheel down. We select the next ws */
343             if (cur_ws == TAILQ_FIRST(walk->workspaces)) {
344                 cur_ws = TAILQ_LAST(walk->workspaces, ws_head);
345             } else {
346                 cur_ws = TAILQ_PREV(cur_ws, ws_head, tailq);
347             }
348             break;
349         case 5:
350             /* Mouse wheel up. We select the previos ws */
351             if (cur_ws == TAILQ_LAST(walk->workspaces, ws_head)) {
352                 cur_ws = TAILQ_FIRST(walk->workspaces);
353             } else {
354                 cur_ws = TAILQ_NEXT(cur_ws, tailq);
355             }
356             break;
357     }
358
359     char buffer[strlen(cur_ws->name) + 11];
360     snprintf(buffer, 50, "workspace %s", cur_ws->name);
361     i3_send_msg(I3_IPC_MESSAGE_TYPE_COMMAND, buffer);
362 }
363
364 /*
365  * This function is called immediately bevor the main loop locks. We flush xcb
366  * then (and only then)
367  *
368  */
369 void xcb_prep_cb(struct ev_loop *loop, ev_prepare *watcher, int revents) {
370     xcb_flush(xcb_connection);
371 }
372
373 /*
374  * This function is called immediately after the main loop locks, so when one
375  * of the watchers registered an event.
376  * We check wether an X-Event arrived and handle it.
377  *
378  */
379 void xcb_chk_cb(struct ev_loop *loop, ev_check *watcher, int revents) {
380     xcb_generic_event_t *event;
381     while ((event = xcb_poll_for_event(xcb_connection)) == NULL) {
382         return;
383     }
384
385     switch (event->response_type & ~0x80) {
386         case XCB_EXPOSE:
387             /* Expose-events happen, when the window needs to be redrawn */
388             redraw_bars();
389             break;
390         case XCB_BUTTON_PRESS:
391             /* Button-press-events are mouse-buttons clicked on one of our bars */
392             handle_button((xcb_button_press_event_t*) event);
393             break;
394     }
395     FREE(event);
396 }
397
398 /*
399  * Dummy Callback. We only need this, so that the Prepare- and Check-Watchers
400  * are triggered
401  *
402  */
403 void xcb_io_cb(struct ev_loop *loop, ev_io *watcher, int revents) {
404 }
405
406 /*
407  * We need to bind to the modifier per XKB. Sadly, XCB does not implement this
408  *
409  */
410 void xkb_io_cb(struct ev_loop *loop, ev_io *watcher, int revents) {
411     XkbEvent ev;
412     int modstate = 0;
413
414     DLOG("Got XKB-Event!\n");
415
416     while (XPending(xkb_dpy)) {
417         XNextEvent(xkb_dpy, (XEvent*)&ev);
418
419         if (ev.type != xkb_event_base) {
420             ELOG("No Xkb-Event!\n");
421             continue;
422         }
423
424         if (ev.any.xkb_type != XkbStateNotify) {
425             ELOG("No State Notify!\n");
426             continue;
427         }
428
429         unsigned int mods = ev.state.mods;
430         modstate = mods & Mod4Mask;
431     }
432
433     if (modstate != mod_pressed) {
434         if (modstate == 0) {
435             DLOG("Mod4 got released!\n");
436             hide_bars();
437         } else {
438             DLOG("Mod4 got pressed!\n");
439             unhide_bars();
440         }
441         mod_pressed = modstate;
442     }
443 }
444
445 /*
446  * Initialize xcb and use the specified fontname for text-rendering
447  *
448  */
449 void init_xcb(char *fontname) {
450     /* FIXME: xcb_connect leaks Memory */
451     xcb_connection = xcb_connect(NULL, NULL);
452     if (xcb_connection_has_error(xcb_connection)) {
453         ELOG("Cannot open display\n");
454         exit(EXIT_FAILURE);
455     }
456     DLOG("Connected to xcb\n");
457
458     /* We have to request the atoms we need */
459     #define ATOM_DO(name) atom_cookies[name] = xcb_intern_atom(xcb_connection, 0, strlen(#name), #name);
460     #include "xcb_atoms.def"
461
462     xcb_screen = xcb_setup_roots_iterator(xcb_get_setup(xcb_connection)).data;
463     xcb_root = xcb_screen->root;
464
465     /* We load and allocate the font */
466     xcb_font = xcb_generate_id(xcb_connection);
467     xcb_void_cookie_t open_font_cookie;
468     open_font_cookie = xcb_open_font_checked(xcb_connection,
469                                              xcb_font,
470                                              strlen(fontname),
471                                              fontname);
472
473     /* We need to save info about the font, because we need the fonts height and
474      * information about the width of characters */
475     xcb_query_font_cookie_t query_font_cookie;
476     query_font_cookie = xcb_query_font(xcb_connection,
477                                        xcb_font);
478
479     /* To grab modifiers without blocking other applications from receiving key-events
480      * involving that modifier, we sadly have to use xkb which is not yet fully supported
481      * in xcb */
482     if (config.hide_on_modifier) {
483         int xkb_major, xkb_minor, xkb_errbase, xkb_err;
484         xkb_major = XkbMajorVersion;
485         xkb_minor = XkbMinorVersion;
486
487         char *dispname = getenv("DISPLAY");
488         if (dispname == NULL) {
489             dispname = ":0";
490         }
491         xkb_dpy = XkbOpenDisplay(dispname,
492                                  &xkb_event_base,
493                                  &xkb_errbase,
494                                  &xkb_major,
495                                  &xkb_minor,
496                                  &xkb_err);
497
498         if (xkb_dpy == NULL) {
499             ELOG("No XKB!\n");
500             exit(EXIT_FAILURE);
501         }
502
503         if (fcntl(ConnectionNumber(xkb_dpy), F_SETFD, FD_CLOEXEC) == -1) {
504             ELOG("Could not set FD_CLOEXEC on xkbdpy: %s\n", strerror(errno));
505             exit(EXIT_FAILURE);
506         }
507
508         int i1;
509         if (!XkbQueryExtension(xkb_dpy, &i1, &xkb_event_base, &xkb_errbase, &xkb_major, &xkb_minor)) {
510             ELOG("XKB not supported by X-server!\n");
511             exit(EXIT_FAILURE);
512         }
513
514         if (!XkbSelectEvents(xkb_dpy, XkbUseCoreKbd, XkbStateNotifyMask, XkbStateNotifyMask)) {
515             ELOG("Could not grab Key!\n");
516             exit(EXIT_FAILURE);
517         }
518
519         xkb_io = malloc(sizeof(ev_io));
520         ev_io_init(xkb_io, &xkb_io_cb, ConnectionNumber(xkb_dpy), EV_READ);
521         ev_io_start(main_loop, xkb_io);
522         XFlush(xkb_dpy);
523     }
524
525     /* We draw the statusline to a seperate pixmap, because it looks the same on all bars and
526      * this way, we can choose to crop it */
527     statusline_ctx = xcb_generate_id(xcb_connection);
528     uint32_t mask = XCB_GC_FOREGROUND |
529                     XCB_GC_BACKGROUND |
530                     XCB_GC_FONT;
531     uint32_t vals[3] = { colors.bar_fg, colors.bar_bg, xcb_font };
532
533     xcb_void_cookie_t sl_ctx_cookie = xcb_create_gc_checked(xcb_connection,
534                                                             statusline_ctx,
535                                                             xcb_root,
536                                                             mask,
537                                                             vals);
538
539     /* We only generate an id for the pixmap, because the width of it is dependent on the
540      * input we get */
541     statusline_pm = xcb_generate_id(xcb_connection);
542
543     /* The varios Watchers to communicate with xcb */
544     xcb_io = malloc(sizeof(ev_io));
545     xcb_prep = malloc(sizeof(ev_prepare));
546     xcb_chk = malloc(sizeof(ev_check));
547
548     ev_io_init(xcb_io, &xcb_io_cb, xcb_get_file_descriptor(xcb_connection), EV_READ);
549     ev_prepare_init(xcb_prep, &xcb_prep_cb);
550     ev_check_init(xcb_chk, &xcb_chk_cb);
551
552     ev_io_start(main_loop, xcb_io);
553     ev_prepare_start(main_loop, xcb_prep);
554     ev_check_start(main_loop, xcb_chk);
555
556     /* Now we get the atoms and save them in a nice data-structure */
557     get_atoms();
558
559     /* Now we save the font-infos */
560     font_info = xcb_query_font_reply(xcb_connection,
561                                      query_font_cookie,
562                                      NULL);
563
564     if (xcb_request_failed(open_font_cookie, "Could not open font")) {
565         exit(EXIT_FAILURE);
566     }
567
568     font_height = font_info->font_ascent + font_info->font_descent;
569
570     if (xcb_query_font_char_infos_length(font_info) == 0) {
571         font_table = NULL;
572     } else {
573         font_table = xcb_query_font_char_infos(font_info);
574     }
575
576     DLOG("Calculated Font-height: %d\n", font_height);
577
578     if (xcb_request_failed(sl_ctx_cookie, "Could not create context for statusline")) {
579         exit(EXIT_FAILURE);
580     }
581 }
582
583 /*
584  * Cleanup the xcb-stuff.
585  * Called once, before the program terminates.
586  *
587  */
588 void clean_xcb() {
589     i3_output *o_walk;
590     free_workspaces();
591     SLIST_FOREACH(o_walk, outputs, slist) {
592         destroy_window(o_walk);
593         FREE(o_walk->workspaces);
594         FREE(o_walk->name);
595     }
596     FREE_SLIST(outputs, i3_output);
597     FREE(outputs);
598
599     xcb_disconnect(xcb_connection);
600
601     ev_check_stop(main_loop, xcb_chk);
602     ev_prepare_stop(main_loop, xcb_prep);
603     ev_io_stop(main_loop, xcb_io);
604
605     FREE(xcb_chk);
606     FREE(xcb_prep);
607     FREE(xcb_io);
608     FREE(font_info);
609 }
610
611 /*
612  * Get the earlier requested atoms and save them in the prepared data-structure
613  *
614  */
615 void get_atoms() {
616     xcb_intern_atom_reply_t *reply;
617     #define ATOM_DO(name) reply = xcb_intern_atom_reply(xcb_connection, atom_cookies[name], NULL); \
618         if (reply == NULL) { \
619             ELOG("Could not get atom %s\n", #name); \
620             exit(EXIT_FAILURE); \
621         } \
622         atoms[name] = reply->atom; \
623         free(reply);
624
625     #include "xcb_atoms.def"
626     DLOG("Got Atoms\n");
627 }
628
629 /*
630  * Destroy the bar of the specified output
631  *
632  */
633 void destroy_window(i3_output *output) {
634     if (output == NULL) {
635         return;
636     }
637     if (output->bar == XCB_NONE) {
638         return;
639     }
640     xcb_destroy_window(xcb_connection, output->bar);
641     output->bar = XCB_NONE;
642 }
643
644 /*
645  * Reconfigure all bars and create new for newly activated outputs
646  *
647  */
648 void reconfig_windows() {
649     uint32_t mask;
650     uint32_t values[5];
651
652     i3_output *walk;
653     SLIST_FOREACH(walk, outputs, slist) {
654         if (!walk->active) {
655             /* If an output is not active, we destroy it's bar */
656             /* FIXME: Maybe we rather want to unmap? */
657             DLOG("Destroying window for output %s\n", walk->name);
658             destroy_window(walk);
659             continue;
660         }
661         if (walk->bar == XCB_NONE) {
662             DLOG("Creating Window for output %s\n", walk->name);
663
664             walk->bar = xcb_generate_id(xcb_connection);
665             walk->buffer = xcb_generate_id(xcb_connection);
666             mask = XCB_CW_BACK_PIXEL | XCB_CW_OVERRIDE_REDIRECT | XCB_CW_EVENT_MASK;
667             /* Black background */
668             values[0] = colors.bar_bg;
669             /* If hide_on_modifier is set, i3 is not supposed to manage our bar-windows */
670             values[1] = config.hide_on_modifier;
671             /* The events we want to receive */
672             values[2] = XCB_EVENT_MASK_EXPOSURE |
673                         XCB_EVENT_MASK_BUTTON_PRESS;
674             xcb_void_cookie_t win_cookie = xcb_create_window_checked(xcb_connection,
675                                                                      xcb_screen->root_depth,
676                                                                      walk->bar,
677                                                                      xcb_root,
678                                                                      walk->rect.x, walk->rect.y + walk->rect.h - font_height - 6,
679                                                                      walk->rect.w, font_height + 6,
680                                                                      1,
681                                                                      XCB_WINDOW_CLASS_INPUT_OUTPUT,
682                                                                      xcb_screen->root_visual,
683                                                                      mask,
684                                                                      values);
685
686             /* The double-buffer we use to render stuff off-screen */
687             xcb_void_cookie_t pm_cookie = xcb_create_pixmap_checked(xcb_connection,
688                                                                     xcb_screen->root_depth,
689                                                                     walk->buffer,
690                                                                     walk->bar,
691                                                                     walk->rect.w,
692                                                                     walk->rect.h);
693
694             /* We want dock-windows (for now). When override_redirect is set, i3 is ignoring
695              * this one */
696             xcb_void_cookie_t dock_cookie = xcb_change_property(xcb_connection,
697                                                                 XCB_PROP_MODE_REPLACE,
698                                                                 walk->bar,
699                                                                 atoms[_NET_WM_WINDOW_TYPE],
700                                                                 XCB_ATOM_ATOM,
701                                                                 32,
702                                                                 1,
703                                                                 (unsigned char*) &atoms[_NET_WM_WINDOW_TYPE_DOCK]);
704
705             /* We need to tell i3, where to reserve space for i3bar */
706             /* left, right, top, bottom, left_start_y, left_end_y,
707              * right_start_y, right_end_y, top_start_x, top_end_x, bottom_start_x,
708              * bottom_end_x */
709             /* A local struct to save the strut_partial property */
710             struct {
711                 uint32_t left;
712                 uint32_t right;
713                 uint32_t top;
714                 uint32_t bottom;
715                 uint32_t left_start_y;
716                 uint32_t left_end_y;
717                 uint32_t right_start_y;
718                 uint32_t right_end_y;
719                 uint32_t top_start_x;
720                 uint32_t top_end_x;
721                 uint32_t bottom_start_x;
722                 uint32_t bottom_end_x;
723             } __attribute__((__packed__)) strut_partial = {0,};
724             switch (config.dockpos) {
725                 case DOCKPOS_NONE:
726                     break;
727                 case DOCKPOS_TOP:
728                     strut_partial.top = font_height + 6;
729                     strut_partial.top_start_x = walk->rect.x;
730                     strut_partial.top_end_x = walk->rect.x + walk->rect.w;
731                     break;
732                 case DOCKPOS_BOT:
733                     strut_partial.bottom = font_height + 6;
734                     strut_partial.bottom_start_x = walk->rect.x;
735                     strut_partial.bottom_end_x = walk->rect.x + walk->rect.w;
736                     break;
737             }
738             xcb_void_cookie_t strut_cookie = xcb_change_property(xcb_connection,
739                                                                  XCB_PROP_MODE_REPLACE,
740                                                                  walk->bar,
741                                                                  atoms[_NET_WM_STRUT_PARTIAL],
742                                                                  XCB_ATOM_CARDINAL,
743                                                                  32,
744                                                                  12,
745                                                                  &strut_partial);
746
747             /* We also want a graphics-context for the bars (it defines the properties
748              * with which we draw to them) */
749             walk->bargc = xcb_generate_id(xcb_connection);
750             mask = XCB_GC_FONT;
751             values[0] = xcb_font;
752             xcb_void_cookie_t gc_cookie = xcb_create_gc_checked(xcb_connection,
753                                                                 walk->bargc,
754                                                                 walk->bar,
755                                                                 mask,
756                                                                 values);
757
758             /* We finally map the bar (display it on screen), unless the modifier-switch is on */
759             xcb_void_cookie_t map_cookie;
760             if (!config.hide_on_modifier) {
761                 map_cookie = xcb_map_window_checked(xcb_connection, walk->bar);
762             }
763
764             if (xcb_request_failed(win_cookie,   "Could not create window") ||
765                 xcb_request_failed(pm_cookie,    "Could not create pixmap") ||
766                 xcb_request_failed(dock_cookie,  "Could not set dock mode") ||
767                 xcb_request_failed(strut_cookie, "Could not set strut")     ||
768                 xcb_request_failed(gc_cookie,    "Could not create graphical context") ||
769                 (!config.hide_on_modifier && xcb_request_failed(map_cookie, "Could not map window"))) {
770                 exit(EXIT_FAILURE);
771             }
772         } else {
773             /* We already have a bar, so we just reconfigure it */
774             mask = XCB_CONFIG_WINDOW_X |
775                    XCB_CONFIG_WINDOW_Y |
776                    XCB_CONFIG_WINDOW_WIDTH |
777                    XCB_CONFIG_WINDOW_HEIGHT |
778                    XCB_CONFIG_WINDOW_STACK_MODE;
779             values[0] = walk->rect.x;
780             values[1] = walk->rect.y + walk->rect.h - font_height - 6;
781             values[2] = walk->rect.w;
782             values[3] = font_height + 6;
783             values[4] = XCB_STACK_MODE_ABOVE;
784
785             DLOG("Destroying buffer for output %s", walk->name);
786             xcb_free_pixmap(xcb_connection, walk->buffer);
787
788             DLOG("Reconfiguring Window for output %s to %d,%d\n", walk->name, values[0], values[1]);
789             xcb_void_cookie_t cfg_cookie = xcb_configure_window_checked(xcb_connection,
790                                                                         walk->bar,
791                                                                         mask,
792                                                                         values);
793
794             DLOG("Recreating buffer for output %s", walk->name);
795             xcb_void_cookie_t pm_cookie = xcb_create_pixmap_checked(xcb_connection,
796                                                                     xcb_screen->root_depth,
797                                                                     walk->buffer,
798                                                                     walk->bar,
799                                                                     walk->rect.w,
800                                                                     walk->rect.h);
801
802             if (xcb_request_failed(cfg_cookie, "Could not reconfigure window")) {
803                 exit(EXIT_FAILURE);
804             }
805             if (xcb_request_failed(pm_cookie,  "Could not create pixmap")) {
806                 exit(EXIT_FAILURE);
807             }
808         }
809     }
810 }
811
812 /*
813  * Render the bars, with buttons and statusline
814  *
815  */
816 void draw_bars() {
817     DLOG("Drawing Bars...\n");
818     int i = 0;
819
820     refresh_statusline();
821
822     i3_output *outputs_walk;
823     SLIST_FOREACH(outputs_walk, outputs, slist) {
824         if (!outputs_walk->active) {
825             DLOG("Output %s inactive, skipping...\n", outputs_walk->name);
826             continue;
827         }
828         if (outputs_walk->bar == XCB_NONE) {
829             /* Oh shit, an active output without an own bar. Create it now! */
830             reconfig_windows();
831         }
832         /* First things first: clear the backbuffer */
833         uint32_t color = colors.bar_bg;
834         xcb_change_gc(xcb_connection,
835                       outputs_walk->bargc,
836                       XCB_GC_FOREGROUND,
837                       &color);
838         xcb_rectangle_t rect = { 0, 0, outputs_walk->rect.w, font_height + 6 };
839         xcb_poly_fill_rectangle(xcb_connection,
840                                 outputs_walk->buffer,
841                                 outputs_walk->bargc,
842                                 1,
843                                 &rect);
844
845         if (statusline != NULL) {
846             DLOG("Printing statusline!\n");
847
848             /* Luckily we already prepared a seperate pixmap containing the rendered
849              * statusline, we just have to copy the relevant parts to the relevant
850              * position */
851             xcb_copy_area(xcb_connection,
852                           statusline_pm,
853                           outputs_walk->buffer,
854                           outputs_walk->bargc,
855                           MAX(0, (int16_t)(statusline_width - outputs_walk->rect.w + 4)), 0,
856                           MAX(0, (int16_t)(outputs_walk->rect.w - statusline_width - 4)), 3,
857                           MIN(outputs_walk->rect.w - 4, statusline_width), font_height);
858         }
859
860         i3_ws *ws_walk;
861         TAILQ_FOREACH(ws_walk, outputs_walk->workspaces, tailq) {
862             DLOG("Drawing Button for WS %s at x = %d\n", ws_walk->name, i);
863             uint32_t fg_color = colors.inactive_ws_fg;
864             uint32_t bg_color = colors.inactive_ws_bg;
865             if (ws_walk->visible) {
866                 fg_color = colors.active_ws_fg;
867                 bg_color = colors.active_ws_bg;
868             }
869             if (ws_walk->urgent) {
870                 DLOG("WS %s is urgent!\n", ws_walk->name);
871                 fg_color = colors.urgent_ws_fg;
872                 bg_color = colors.urgent_ws_bg;
873                 /* The urgent-hint should get noticed, so we unhide the bars shortly */
874                 unhide_bars();
875             }
876             xcb_change_gc(xcb_connection,
877                           outputs_walk->bargc,
878                           XCB_GC_FOREGROUND,
879                           &bg_color);
880             xcb_change_gc(xcb_connection,
881                           outputs_walk->bargc,
882                           XCB_GC_BACKGROUND,
883                           &bg_color);
884             xcb_rectangle_t rect = { i + 1, 1, ws_walk->name_width + 8, font_height + 4 };
885             xcb_poly_fill_rectangle(xcb_connection,
886                                     outputs_walk->buffer,
887                                     outputs_walk->bargc,
888                                     1,
889                                     &rect);
890             xcb_change_gc(xcb_connection,
891                           outputs_walk->bargc,
892                           XCB_GC_FOREGROUND,
893                           &fg_color);
894             xcb_image_text_16(xcb_connection,
895                               ws_walk->name_glyphs,
896                               outputs_walk->buffer,
897                               outputs_walk->bargc,
898                               i + 5, font_info->font_ascent + 2,
899                               ws_walk->ucs2_name);
900             i += 10 + ws_walk->name_width;
901         }
902
903         redraw_bars();
904
905         i = 0;
906     }
907 }
908
909 /*
910  * Redraw the bars, i.e. simply copy the buffer to the barwindow
911  *
912  */
913 void redraw_bars() {
914     i3_output *outputs_walk;
915     SLIST_FOREACH(outputs_walk, outputs, slist) {
916         xcb_copy_area(xcb_connection,
917                       outputs_walk->buffer,
918                       outputs_walk->bar,
919                       outputs_walk->bargc,
920                       0, 0,
921                       0, 0,
922                       outputs_walk->rect.w,
923                       outputs_walk->rect.h);
924         xcb_flush(xcb_connection);
925     }
926 }