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