]> git.sur5r.net Git - i3/i3/blob - i3bar/src/xcb.c
Implement dock-positioning
[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;
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;
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         xkb_dpy = XkbOpenDisplay(":0",
488                                  &xkb_event_base,
489                                  &xkb_errbase,
490                                  &xkb_major,
491                                  &xkb_minor,
492                                  &xkb_err);
493
494         if (xkb_dpy == NULL) {
495             ELOG("No XKB!\n");
496             exit(EXIT_FAILURE);
497         }
498
499         if (fcntl(ConnectionNumber(xkb_dpy), F_SETFD, FD_CLOEXEC) == -1) {
500             ELOG("Could not set FD_CLOEXEC on xkbdpy: %s\n", strerror(errno));
501             exit(EXIT_FAILURE);
502         }
503
504         int i1;
505         if (!XkbQueryExtension(xkb_dpy, &i1, &xkb_event_base, &xkb_errbase, &xkb_major, &xkb_minor)) {
506             ELOG("XKB not supported by X-server!\n");
507             exit(EXIT_FAILURE);
508         }
509
510         if (!XkbSelectEvents(xkb_dpy, XkbUseCoreKbd, XkbStateNotifyMask, XkbStateNotifyMask)) {
511             ELOG("Could not grab Key!\n");
512             exit(EXIT_FAILURE);
513         }
514
515         xkb_io = malloc(sizeof(ev_io));
516         ev_io_init(xkb_io, &xkb_io_cb, ConnectionNumber(xkb_dpy), EV_READ);
517         ev_io_start(main_loop, xkb_io);
518         XFlush(xkb_dpy);
519     }
520
521     /* We draw the statusline to a seperate pixmap, because it looks the same on all bars and
522      * this way, we can choose to crop it */
523     statusline_ctx = xcb_generate_id(xcb_connection);
524     uint32_t mask = XCB_GC_FOREGROUND |
525                     XCB_GC_BACKGROUND |
526                     XCB_GC_FONT;
527     uint32_t vals[3] = { colors.bar_fg, colors.bar_bg, xcb_font };
528
529     xcb_void_cookie_t sl_ctx_cookie = xcb_create_gc_checked(xcb_connection,
530                                                             statusline_ctx,
531                                                             xcb_root,
532                                                             mask,
533                                                             vals);
534
535     /* We only generate an id for the pixmap, because the width of it is dependent on the
536      * input we get */
537     statusline_pm = xcb_generate_id(xcb_connection);
538
539     /* The varios Watchers to communicate with xcb */
540     xcb_io = malloc(sizeof(ev_io));
541     xcb_prep = malloc(sizeof(ev_prepare));
542     xcb_chk = malloc(sizeof(ev_check));
543
544     ev_io_init(xcb_io, &xcb_io_cb, xcb_get_file_descriptor(xcb_connection), EV_READ);
545     ev_prepare_init(xcb_prep, &xcb_prep_cb);
546     ev_check_init(xcb_chk, &xcb_chk_cb);
547
548     ev_io_start(main_loop, xcb_io);
549     ev_prepare_start(main_loop, xcb_prep);
550     ev_check_start(main_loop, xcb_chk);
551
552     /* Now we get the atoms and save them in a nice data-structure */
553     get_atoms();
554
555     /* Now we save the font-infos */
556     font_info = xcb_query_font_reply(xcb_connection,
557                                      query_font_cookie,
558                                      NULL);
559
560     if (xcb_request_failed(open_font_cookie, "Could not open font")) {
561         exit(EXIT_FAILURE);
562     }
563
564     font_height = font_info->font_ascent + font_info->font_descent;
565
566     if (xcb_query_font_char_infos_length(font_info) == 0) {
567         font_table = NULL;
568     } else {
569         font_table = xcb_query_font_char_infos(font_info);
570     }
571
572     DLOG("Calculated Font-height: %d\n", font_height);
573
574     if (xcb_request_failed(sl_ctx_cookie, "Could not create context for statusline")) {
575         exit(EXIT_FAILURE);
576     }
577 }
578
579 /*
580  * Cleanup the xcb-stuff.
581  * Called once, before the program terminates.
582  *
583  */
584 void clean_xcb() {
585     i3_output *o_walk;
586     free_workspaces();
587     SLIST_FOREACH(o_walk, outputs, slist) {
588         destroy_window(o_walk);
589         FREE(o_walk->workspaces);
590         FREE(o_walk->name);
591     }
592     FREE_SLIST(outputs, i3_output);
593     FREE(outputs);
594
595     xcb_disconnect(xcb_connection);
596
597     ev_check_stop(main_loop, xcb_chk);
598     ev_prepare_stop(main_loop, xcb_prep);
599     ev_io_stop(main_loop, xcb_io);
600
601     FREE(xcb_chk);
602     FREE(xcb_prep);
603     FREE(xcb_io);
604     FREE(font_info);
605 }
606
607 /*
608  * Get the earlier requested atoms and save them in the prepared data-structure
609  *
610  */
611 void get_atoms() {
612     xcb_intern_atom_reply_t *reply;
613     #define ATOM_DO(name) reply = xcb_intern_atom_reply(xcb_connection, atom_cookies[name], NULL); \
614         if (reply == NULL) { \
615             ELOG("Could not get atom %s\n", #name); \
616             exit(EXIT_FAILURE); \
617         } \
618         atoms[name] = reply->atom; \
619         free(reply);
620
621     #include "xcb_atoms.def"
622     DLOG("Got Atoms\n");
623 }
624
625 /*
626  * Destroy the bar of the specified output
627  *
628  */
629 void destroy_window(i3_output *output) {
630     if (output == NULL) {
631         return;
632     }
633     if (output->bar == XCB_NONE) {
634         return;
635     }
636     xcb_destroy_window(xcb_connection, output->bar);
637     output->bar = XCB_NONE;
638 }
639
640 /*
641  * Reconfigure all bars and create new for newly activated outputs
642  *
643  */
644 void reconfig_windows() {
645     uint32_t mask;
646     uint32_t values[5];
647
648     i3_output *walk;
649     SLIST_FOREACH(walk, outputs, slist) {
650         if (!walk->active) {
651             /* If an output is not active, we destroy it's bar */
652             /* FIXME: Maybe we rather want to unmap? */
653             DLOG("Destroying window for output %s\n", walk->name);
654             destroy_window(walk);
655             continue;
656         }
657         if (walk->bar == XCB_NONE) {
658             DLOG("Creating Window for output %s\n", walk->name);
659
660             walk->bar = xcb_generate_id(xcb_connection);
661             walk->buffer = xcb_generate_id(xcb_connection);
662             mask = XCB_CW_BACK_PIXEL | XCB_CW_OVERRIDE_REDIRECT | XCB_CW_EVENT_MASK;
663             /* Black background */
664             values[0] = colors.bar_bg;
665             /* If hide_on_modifier is set, i3 is not supposed to manage our bar-windows */
666             values[1] = config.hide_on_modifier;
667             /* The events we want to receive */
668             values[2] = XCB_EVENT_MASK_EXPOSURE |
669                         XCB_EVENT_MASK_BUTTON_PRESS;
670             xcb_void_cookie_t win_cookie = xcb_create_window_checked(xcb_connection,
671                                                                      xcb_screen->root_depth,
672                                                                      walk->bar,
673                                                                      xcb_root,
674                                                                      walk->rect.x, walk->rect.y + walk->rect.h - font_height - 6,
675                                                                      walk->rect.w, font_height + 6,
676                                                                      1,
677                                                                      XCB_WINDOW_CLASS_INPUT_OUTPUT,
678                                                                      xcb_screen->root_visual,
679                                                                      mask,
680                                                                      values);
681
682             /* The double-buffer we use to render stuff off-screen */
683             xcb_void_cookie_t pm_cookie = xcb_create_pixmap_checked(xcb_connection,
684                                                                     xcb_screen->root_depth,
685                                                                     walk->buffer,
686                                                                     walk->bar,
687                                                                     walk->rect.w,
688                                                                     walk->rect.h);
689
690             /* We want dock-windows (for now). When override_redirect is set, i3 is ignoring
691              * this one */
692             xcb_void_cookie_t dock_cookie = xcb_change_property(xcb_connection,
693                                                                 XCB_PROP_MODE_REPLACE,
694                                                                 walk->bar,
695                                                                 atoms[_NET_WM_WINDOW_TYPE],
696                                                                 XCB_ATOM_ATOM,
697                                                                 32,
698                                                                 1,
699                                                                 (unsigned char*) &atoms[_NET_WM_WINDOW_TYPE_DOCK]);
700
701             /* We need to tell i3, where to reserve space for i3bar */
702             /* left, right, top, bottom, left_start_y, left_end_y,
703              * right_start_y, right_end_y, top_start_x, top_end_x, bottom_start_x,
704              * bottom_end_x */
705             /* A local struct to save the strut_partial property */
706             struct {
707                 uint32_t left;
708                 uint32_t right;
709                 uint32_t top;
710                 uint32_t bottom;
711                 uint32_t left_start_y;
712                 uint32_t left_end_y;
713                 uint32_t right_start_y;
714                 uint32_t right_end_y;
715                 uint32_t top_start_x;
716                 uint32_t top_end_x;
717                 uint32_t bottom_start_x;
718                 uint32_t bottom_end_x;
719             } __attribute__((__packed__)) strut_partial = {0,};
720             switch (config.dockpos) {
721                 case DOCKPOS_NONE:
722                     break;
723                 case DOCKPOS_TOP:
724                     strut_partial.top = font_height + 6;
725                     strut_partial.top_start_x = walk->rect.x;
726                     strut_partial.top_end_x = walk->rect.x + walk->rect.w;
727                     break;
728                 case DOCKPOS_BOT:
729                     strut_partial.bottom = font_height + 6;
730                     strut_partial.bottom_start_x = walk->rect.x;
731                     strut_partial.bottom_end_x = walk->rect.x + walk->rect.w;
732                     break;
733             }
734             xcb_void_cookie_t strut_cookie = xcb_change_property(xcb_connection,
735                                                                  XCB_PROP_MODE_REPLACE,
736                                                                  walk->bar,
737                                                                  atoms[_NET_WM_STRUT_PARTIAL],
738                                                                  XCB_ATOM_CARDINAL,
739                                                                  32,
740                                                                  12,
741                                                                  &strut_partial);
742
743             /* We also want a graphics-context for the bars (it defines the properties
744              * with which we draw to them) */
745             walk->bargc = xcb_generate_id(xcb_connection);
746             mask = XCB_GC_FONT;
747             values[0] = xcb_font;
748             xcb_void_cookie_t gc_cookie = xcb_create_gc_checked(xcb_connection,
749                                                                 walk->bargc,
750                                                                 walk->bar,
751                                                                 mask,
752                                                                 values);
753
754             /* We finally map the bar (display it on screen), unless the modifier-switch is on */
755             xcb_void_cookie_t map_cookie;
756             if (!config.hide_on_modifier) {
757                 map_cookie = xcb_map_window_checked(xcb_connection, walk->bar);
758             }
759
760             if (xcb_request_failed(win_cookie,   "Could not create window") ||
761                 xcb_request_failed(pm_cookie,    "Could not create pixmap") ||
762                 xcb_request_failed(dock_cookie,  "Could not set dock mode") ||
763                 xcb_request_failed(strut_cookie, "Could not set strut")     ||
764                 xcb_request_failed(gc_cookie,    "Could not create graphical context") ||
765                 (!config.hide_on_modifier && xcb_request_failed(map_cookie, "Could not map window"))) {
766                 exit(EXIT_FAILURE);
767             }
768         } else {
769             /* We already have a bar, so we just reconfigure it */
770             mask = XCB_CONFIG_WINDOW_X |
771                    XCB_CONFIG_WINDOW_Y |
772                    XCB_CONFIG_WINDOW_WIDTH |
773                    XCB_CONFIG_WINDOW_HEIGHT |
774                    XCB_CONFIG_WINDOW_STACK_MODE;
775             values[0] = walk->rect.x;
776             values[1] = walk->rect.y + walk->rect.h - font_height - 6;
777             values[2] = walk->rect.w;
778             values[3] = font_height + 6;
779             values[4] = XCB_STACK_MODE_ABOVE;
780
781             DLOG("Destroying buffer for output %s", walk->name);
782             xcb_free_pixmap(xcb_connection, walk->buffer);
783
784             DLOG("Reconfiguring Window for output %s to %d,%d\n", walk->name, values[0], values[1]);
785             xcb_void_cookie_t cfg_cookie = xcb_configure_window_checked(xcb_connection,
786                                                                         walk->bar,
787                                                                         mask,
788                                                                         values);
789
790             DLOG("Recreating buffer for output %s", walk->name);
791             xcb_void_cookie_t pm_cookie = xcb_create_pixmap_checked(xcb_connection,
792                                                                     xcb_screen->root_depth,
793                                                                     walk->buffer,
794                                                                     walk->bar,
795                                                                     walk->rect.w,
796                                                                     walk->rect.h);
797
798             if (xcb_request_failed(cfg_cookie, "Could not reconfigure window")) {
799                 exit(EXIT_FAILURE);
800             }
801             if (xcb_request_failed(pm_cookie,  "Could not create pixmap")) {
802                 exit(EXIT_FAILURE);
803             }
804         }
805     }
806 }
807
808 /*
809  * Render the bars, with buttons and statusline
810  *
811  */
812 void draw_bars() {
813     DLOG("Drawing Bars...\n");
814     int i = 0;
815
816     refresh_statusline();
817
818     i3_output *outputs_walk;
819     SLIST_FOREACH(outputs_walk, outputs, slist) {
820         if (!outputs_walk->active) {
821             DLOG("Output %s inactive, skipping...\n", outputs_walk->name);
822             continue;
823         }
824         if (outputs_walk->bar == XCB_NONE) {
825             /* Oh shit, an active output without an own bar. Create it now! */
826             reconfig_windows();
827         }
828         /* First things first: clear the backbuffer */
829         uint32_t color = colors.bar_bg;
830         xcb_change_gc(xcb_connection,
831                       outputs_walk->bargc,
832                       XCB_GC_FOREGROUND,
833                       &color);
834         xcb_rectangle_t rect = { 0, 0, outputs_walk->rect.w, font_height + 6 };
835         xcb_poly_fill_rectangle(xcb_connection,
836                                 outputs_walk->buffer,
837                                 outputs_walk->bargc,
838                                 1,
839                                 &rect);
840
841         if (statusline != NULL) {
842             DLOG("Printing statusline!\n");
843
844             /* Luckily we already prepared a seperate pixmap containing the rendered
845              * statusline, we just have to copy the relevant parts to the relevant
846              * position */
847             xcb_copy_area(xcb_connection,
848                           statusline_pm,
849                           outputs_walk->buffer,
850                           outputs_walk->bargc,
851                           MAX(0, (int16_t)(statusline_width - outputs_walk->rect.w + 4)), 0,
852                           MAX(0, (int16_t)(outputs_walk->rect.w - statusline_width - 4)), 3,
853                           MIN(outputs_walk->rect.w - 4, statusline_width), font_height);
854         }
855
856         i3_ws *ws_walk;
857         TAILQ_FOREACH(ws_walk, outputs_walk->workspaces, tailq) {
858             DLOG("Drawing Button for WS %s at x = %d\n", ws_walk->name, i);
859             uint32_t fg_color = colors.inactive_ws_fg;
860             uint32_t bg_color = colors.inactive_ws_bg;
861             if (ws_walk->visible) {
862                 fg_color = colors.active_ws_fg;
863                 bg_color = colors.active_ws_bg;
864             }
865             if (ws_walk->urgent) {
866                 DLOG("WS %s is urgent!\n", ws_walk->name);
867                 fg_color = colors.urgent_ws_fg;
868                 bg_color = colors.urgent_ws_bg;
869                 /* The urgent-hint should get noticed, so we unhide the bars shortly */
870                 unhide_bars();
871             }
872             xcb_change_gc(xcb_connection,
873                           outputs_walk->bargc,
874                           XCB_GC_FOREGROUND,
875                           &bg_color);
876             xcb_change_gc(xcb_connection,
877                           outputs_walk->bargc,
878                           XCB_GC_BACKGROUND,
879                           &bg_color);
880             xcb_rectangle_t rect = { i + 1, 1, ws_walk->name_width + 8, font_height + 4 };
881             xcb_poly_fill_rectangle(xcb_connection,
882                                     outputs_walk->buffer,
883                                     outputs_walk->bargc,
884                                     1,
885                                     &rect);
886             xcb_change_gc(xcb_connection,
887                           outputs_walk->bargc,
888                           XCB_GC_FOREGROUND,
889                           &fg_color);
890             xcb_image_text_16(xcb_connection,
891                               ws_walk->name_glyphs,
892                               outputs_walk->buffer,
893                               outputs_walk->bargc,
894                               i + 5, font_info->font_ascent + 2,
895                               ws_walk->ucs2_name);
896             i += 10 + ws_walk->name_width;
897         }
898
899         redraw_bars();
900
901         i = 0;
902     }
903 }
904
905 /*
906  * Redraw the bars, i.e. simply copy the buffer to the barwindow
907  *
908  */
909 void redraw_bars() {
910     i3_output *outputs_walk;
911     SLIST_FOREACH(outputs_walk, outputs, slist) {
912         xcb_copy_area(xcb_connection,
913                       outputs_walk->buffer,
914                       outputs_walk->bar,
915                       outputs_walk->bargc,
916                       0, 0,
917                       0, 0,
918                       outputs_walk->rect.w,
919                       outputs_walk->rect.h);
920         xcb_flush(xcb_connection);
921     }
922 }