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