]> git.sur5r.net Git - i3/i3/blob - i3bar/src/xcb.c
dd19feafefe62aba7a805fde3ca22097b605b55a
[i3/i3] / i3bar / src / xcb.c
1 #include <xcb/xcb.h>
2 #include <xcb/xproto.h>
3 #include <xcb/xcb_event.h>
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <string.h>
7 #include <i3/ipc.h>
8 #include <ev.h>
9
10 #include "common.h"
11
12 /* We save the Atoms in an easy to access array, indexed by an enum */
13 #define NUM_ATOMS 3
14
15 enum {
16     #define ATOM_DO(name) name,
17     #include "xcb_atoms.def"
18 };
19
20 xcb_intern_atom_cookie_t atom_cookies[NUM_ATOMS];
21 xcb_atom_t               atoms[NUM_ATOMS];
22
23 /* Variables, that are the same for all functions at all times */
24 xcb_connection_t *xcb_connection;
25 xcb_screen_t     *xcb_screens;
26 xcb_window_t     xcb_root;
27 xcb_font_t       xcb_font;
28
29 /* Event-Watchers, to interact with the user */
30 ev_prepare *xcb_prep;
31 ev_check   *xcb_chk;
32 ev_io      *xcb_io;
33
34 /*
35  * Converts a colorstring to a colorpixel as expected from xcb_change_gc.
36  * s is assumed to be in the format "rrggbb"
37  *
38  */
39 uint32_t get_colorpixel(const char *s) {
40     char strings[3][3] = { { s[0], s[1], '\0'} ,
41                            { s[2], s[3], '\0'} ,
42                            { s[4], s[5], '\0'} };
43     uint8_t r = strtol(strings[0], NULL, 16);
44     uint8_t g = strtol(strings[1], NULL, 16);
45     uint8_t b = strtol(strings[2], NULL, 16);
46     return (r << 16 | g << 8 | b);
47 }
48
49 /*
50  * Handle a button-press-event (i.c. a mouse click on one of our bars).
51  * We determine, wether the click occured on a ws-button or if the scroll-
52  * wheel was used and change the workspace appropriately
53  *
54  */
55 void handle_button(xcb_button_press_event_t *event) {
56     i3_ws *cur_ws;
57
58     /* Determine, which bar was clicked */
59     i3_output *walk;
60     xcb_window_t bar = event->event;
61     SLIST_FOREACH(walk, outputs, slist) {
62         if (walk->bar == bar) {
63             break;
64         }
65     }
66
67     if (walk == NULL) {
68         printf("Unknown Bar klicked!\n");
69         return;
70     }
71
72     /* TODO: Move this to exern get_ws_for_output() */
73     TAILQ_FOREACH(cur_ws, walk->workspaces, tailq) {
74         if (cur_ws->visible) {
75             break;
76         }
77     }
78
79     if (cur_ws == NULL) {
80         printf("No Workspace active?\n");
81         return;
82     }
83
84     int32_t x = event->event_x;
85
86     printf("Got Button %d\n", event->detail);
87
88     switch (event->detail) {
89         case 1:
90             /* Left Mousbutton. We determine, which button was clicked
91              * and set cur_ws accordingly */
92             TAILQ_FOREACH(cur_ws, walk->workspaces, tailq) {
93                 printf("x = %d\n", x);
94                 if (x < cur_ws->name_width + 10) {
95                     break;
96                 }
97                 x -= cur_ws->name_width + 10;
98             }
99             if (cur_ws == NULL) {
100                 return;
101             }
102             break;
103         case 4:
104             /* Mouse wheel down. We select the next ws */
105             if (cur_ws == TAILQ_FIRST(walk->workspaces)) {
106                 cur_ws = TAILQ_LAST(walk->workspaces, ws_head);
107             } else {
108                 cur_ws = TAILQ_PREV(cur_ws, ws_head, tailq);
109             }
110             break;
111         case 5:
112             /* Mouse wheel up. We select the previos ws */
113             if (cur_ws == TAILQ_LAST(walk->workspaces, ws_head)) {
114                 cur_ws = TAILQ_FIRST(walk->workspaces);
115             } else {
116                 cur_ws = TAILQ_NEXT(cur_ws, tailq);
117             }
118             break;
119     }
120
121     char buffer[50];
122     snprintf(buffer, 50, "%d", cur_ws->num);
123     i3_send_msg(I3_IPC_MESSAGE_TYPE_COMMAND, buffer);
124 }
125
126 /*
127  * This function is called immediately bevor the main loop locks. We flush xcb
128  * then (and only then)
129  *
130  */
131 void xcb_prep_cb(struct ev_loop *loop, ev_prepare *watcher, int revenst) {
132     xcb_flush(xcb_connection);
133 }
134
135 /*
136  * This function is called immediately after the main loop locks, so when one
137  * of the watchers registered an event.
138  * We check wether an X-Event arrived and handle it.
139  *
140  */
141 void xcb_chk_cb(struct ev_loop *loop, ev_check *watcher, int revents) {
142     xcb_generic_event_t *event;
143     while ((event = xcb_poll_for_event(xcb_connection)) == NULL) {
144         return;
145     }
146
147     switch (event->response_type & ~0x80) {
148         case XCB_EXPOSE:
149             /* Expose-events happen, when the window needs to be redrawn */
150             draw_bars();
151             break;
152         case XCB_BUTTON_PRESS:
153             /* Button-press-events are mouse-buttons clicked on one of our bars */
154             handle_button((xcb_button_press_event_t*) event);
155             break;
156     }
157     FREE(event);
158 }
159
160 /*
161  * Dummy Callback. We only need this, so that the Prepare- and Check-Watchers
162  * are triggered
163  *
164  */
165 void xcb_io_cb(struct ev_loop *loop, ev_io *watcher, int revents) {
166 }
167
168 /*
169  * Calculate the rendered width of a string with the configured font.
170  * The string has to be encoded in ucs2 and glyph_len has to be the length
171  * of the string (in width)
172  *
173  */
174 int get_string_width(xcb_char2b_t *string, int glyph_len) {
175     xcb_query_text_extents_cookie_t cookie;
176     xcb_query_text_extents_reply_t *reply;
177     xcb_generic_error_t *error;
178     int width;
179
180     cookie = xcb_query_text_extents(xcb_connection, xcb_font, glyph_len, string);
181     reply = xcb_query_text_extents_reply(xcb_connection, cookie, &error);
182     if (reply == NULL) {
183         printf("ERROR: Could not get text extents!");
184         return 7;
185     }
186
187     width = reply->overall_width;
188     free(reply);
189     return width;
190 }
191
192 /*
193  * Initialize xcb and use the specified fontname for text-rendering
194  *
195  */
196 void init_xcb(char *fontname) {
197     /* FIXME: xcb_connect leaks Memory */
198     xcb_connection = xcb_connect(NULL, NULL);
199     if (xcb_connection_has_error(xcb_connection)) {
200         printf("Cannot open display\n");
201         exit(EXIT_FAILURE);
202     }
203     printf("Connected to xcb\n");
204
205     /* We have to request the atoms we need */
206     #define ATOM_DO(name) atom_cookies[name] = xcb_intern_atom(xcb_connection, 0, strlen(#name), #name);
207     #include "xcb_atoms.def"
208
209     xcb_screens = xcb_setup_roots_iterator(xcb_get_setup(xcb_connection)).data;
210     xcb_root = xcb_screens->root;
211
212     /* We load and allocate the font */
213     xcb_font = xcb_generate_id(xcb_connection);
214     xcb_open_font(xcb_connection,
215                   xcb_font,
216                   strlen(fontname),
217                   fontname);
218
219     /* We also need the fontheight to configure our bars accordingly */
220     xcb_list_fonts_with_info_cookie_t cookie;
221     cookie = xcb_list_fonts_with_info(xcb_connection,
222                                       1,
223                                       strlen(fontname),
224                                       fontname);
225
226     /* The varios Watchers to communicate with xcb */
227     xcb_io = malloc(sizeof(ev_io));
228     xcb_prep = malloc(sizeof(ev_prepare));
229     xcb_chk = malloc(sizeof(ev_check));
230
231     ev_io_init(xcb_io, &xcb_io_cb, xcb_get_file_descriptor(xcb_connection), EV_READ);
232     ev_prepare_init(xcb_prep, &xcb_prep_cb);
233     ev_check_init(xcb_chk, &xcb_chk_cb);
234
235     ev_io_start(main_loop, xcb_io);
236     ev_prepare_start(main_loop, xcb_prep);
237     ev_check_start(main_loop, xcb_chk);
238
239     /* Now we get the atoms and save them in a nice data-structure */
240     get_atoms();
241
242     /* Now we calculate the font-height */
243     xcb_list_fonts_with_info_reply_t *reply;
244     reply = xcb_list_fonts_with_info_reply(xcb_connection,
245                                            cookie,
246                                            NULL);
247     font_height = reply->font_ascent + reply->font_descent;
248     FREE(reply);
249
250     printf("Calculated Font-height: %d\n", font_height);
251 }
252
253 /*
254  * Cleanup the xcb-stuff.
255  * Called once, before the program terminates.
256  *
257  */
258 void clean_xcb() {
259     i3_output *walk;
260     SLIST_FOREACH(walk, outputs, slist) {
261         destroy_window(walk);
262     }
263     FREE_SLIST(outputs, i3_output);
264
265     xcb_disconnect(xcb_connection);
266
267     ev_check_stop(main_loop, xcb_chk);
268     ev_prepare_stop(main_loop, xcb_prep);
269     ev_io_stop(main_loop, xcb_io);
270
271     FREE(xcb_chk);
272     FREE(xcb_prep);
273     FREE(xcb_io);
274 }
275
276 /*
277  * Get the earlier requested atoms and save them in the prepared data-structure
278  *
279  */
280 void get_atoms() {
281     xcb_intern_atom_reply_t *reply;
282     #define ATOM_DO(name) reply = xcb_intern_atom_reply(xcb_connection, atom_cookies[name], NULL); \
283         atoms[name] = reply->atom; \
284         free(reply);
285
286     #include "xcb_atoms.def"
287     printf("Got Atoms\n");
288 }
289
290 /*
291  * Destroy the bar of the specified output
292  *
293  */
294 void destroy_window(i3_output *output) {
295     if (output == NULL) {
296         return;
297     }
298     if (output->bar == XCB_NONE) {
299         return;
300     }
301     xcb_destroy_window(xcb_connection, output->bar);
302     output->bar = XCB_NONE;
303 }
304
305 /*
306  * Reconfigure all bars and create new for newly activated outputs
307  *
308  */
309 void reconfig_windows() {
310     uint32_t mask;
311     uint32_t values[4];
312
313     i3_output *walk;
314     SLIST_FOREACH(walk, outputs, slist) {
315         if (!walk->active) {
316             /* If an output is not active, we destroy it's bar */
317             /* FIXME: Maybe we rather want to unmap? */
318             printf("Destroying window for output %s\n", walk->name);
319             destroy_window(walk);
320             continue;
321         }
322         if (walk->bar == XCB_NONE) {
323             printf("Creating Window for output %s\n", walk->name);
324
325             walk->bar = xcb_generate_id(xcb_connection);
326             mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
327             /* Black background */
328             values[0] = xcb_screens->black_pixel;
329             /* The events we want to receive */
330             values[1] = XCB_EVENT_MASK_EXPOSURE |
331                         XCB_EVENT_MASK_BUTTON_PRESS;
332             xcb_create_window(xcb_connection,
333                               xcb_screens->root_depth,
334                               walk->bar,
335                               xcb_root,
336                               walk->rect.x, walk->rect.y,
337                               walk->rect.w, font_height + 6,
338                               1,
339                               XCB_WINDOW_CLASS_INPUT_OUTPUT,
340                               xcb_screens->root_visual,
341                               mask,
342                               values);
343
344             /* We want dock-windows (for now) */
345             xcb_change_property(xcb_connection,
346                                 XCB_PROP_MODE_REPLACE,
347                                 walk->bar,
348                                 atoms[_NET_WM_WINDOW_TYPE],
349                                 atoms[ATOM],
350                                 32,
351                                 1,
352                                 (unsigned char*) &atoms[_NET_WM_WINDOW_TYPE_DOCK]);
353
354             /* We also want a graphics-context (the "canvas" on which we draw) */
355             walk->bargc = xcb_generate_id(xcb_connection);
356             mask = XCB_GC_FONT;
357             values[0] = xcb_font;
358             xcb_create_gc(xcb_connection,
359                           walk->bargc,
360                           walk->bar,
361                           mask,
362                           values);
363
364             /* We finally map the bar (display it on screen) */
365             xcb_map_window(xcb_connection, walk->bar);
366         } else {
367             /* We already have a bar, so we just reconfigure it */
368             mask = XCB_CONFIG_WINDOW_X |
369                    XCB_CONFIG_WINDOW_Y |
370                    XCB_CONFIG_WINDOW_WIDTH |
371                    XCB_CONFIG_WINDOW_HEIGHT;
372             values[0] = walk->rect.x;
373             values[1] = walk->rect.y + walk->rect.h - font_height - 6;
374             values[2] = walk->rect.w;
375             values[3] = font_height + 6;
376             printf("Reconfiguring Window for output %s to %d,%d\n", walk->name, values[0], values[1]);
377             xcb_configure_window(xcb_connection,
378                                  walk->bar,
379                                  mask,
380                                  values);
381         }
382     }
383 }
384
385 /*
386  * Render the bars, with buttons and statusline
387  *
388  */
389 void draw_bars() {
390     printf("Drawing Bars...\n");
391     int i = 0;
392     i3_output *outputs_walk;
393     SLIST_FOREACH(outputs_walk, outputs, slist) {
394         if (!outputs_walk->active) {
395             printf("Output %s inactive, skipping...\n", outputs_walk->name);
396             continue;
397         }
398         if (outputs_walk->bar == XCB_NONE) {
399             reconfig_windows();
400         }
401         uint32_t color = get_colorpixel("000000");
402         xcb_change_gc(xcb_connection,
403                       outputs_walk->bargc,
404                       XCB_GC_FOREGROUND,
405                       &color);
406         xcb_rectangle_t rect = { 0, 0, outputs_walk->rect.w, font_height + 6 };
407         xcb_poly_fill_rectangle(xcb_connection,
408                                 outputs_walk->bar,
409                                 outputs_walk->bargc,
410                                 1,
411                                 &rect);
412         if (statusline != NULL) {
413             printf("Printing statusline!\n");
414             xcb_change_gc(xcb_connection,
415                           outputs_walk->bargc,
416                           XCB_GC_BACKGROUND,
417                           &color);
418             color = get_colorpixel("FFFFFF");
419             xcb_change_gc(xcb_connection,
420                           outputs_walk->bargc,
421                           XCB_GC_FOREGROUND,
422                           &color);
423
424             int glyph_count;
425             xcb_char2b_t *text = (xcb_char2b_t*) convert_utf8_to_ucs2(statusline, &glyph_count);
426
427             xcb_void_cookie_t cookie;
428             cookie = xcb_image_text_16(xcb_connection,
429                                        glyph_count,
430                                        outputs_walk->bar,
431                                        outputs_walk->bargc,
432                                        outputs_walk->rect.w - get_string_width(text, glyph_count) - 4,
433                                        font_height + 1,
434                                        (xcb_char2b_t*) text);
435
436             xcb_generic_error_t *err = xcb_request_check(xcb_connection, cookie);
437
438             if (err != NULL) {
439                 printf("XCB-Error: %d\n", err->error_code);
440             }
441         }
442         i3_ws *ws_walk;
443         TAILQ_FOREACH(ws_walk, outputs_walk->workspaces, tailq) {
444             printf("Drawing Button for WS %s at x = %d\n", ws_walk->name, i);
445             uint32_t color = get_colorpixel("240000");
446             if (ws_walk->visible) {
447                 color = get_colorpixel("480000");
448             }
449             if (ws_walk->urgent) {
450                 printf("WS %s is urgent!\n", ws_walk->name);
451                 color = get_colorpixel("002400");
452             }
453             xcb_change_gc(xcb_connection,
454                           outputs_walk->bargc,
455                           XCB_GC_FOREGROUND,
456                           &color);
457             xcb_change_gc(xcb_connection,
458                           outputs_walk->bargc,
459                           XCB_GC_BACKGROUND,
460                           &color);
461             xcb_rectangle_t rect = { i + 1, 1, ws_walk->name_width + 8, font_height + 4 };
462             xcb_poly_fill_rectangle(xcb_connection,
463                                     outputs_walk->bar,
464                                     outputs_walk->bargc,
465                                     1,
466                                     &rect);
467             color = get_colorpixel("FFFFFF");
468             xcb_change_gc(xcb_connection,
469                           outputs_walk->bargc,
470                           XCB_GC_FOREGROUND,
471                           &color);
472             xcb_image_text_16(xcb_connection,
473                               ws_walk->name_glyphs,
474                               outputs_walk->bar,
475                               outputs_walk->bargc,
476                               i + 5, font_height + 1,
477                               ws_walk->ucs2_name);
478             i += 10 + ws_walk->name_width;
479         }
480
481         i = 0;
482     }
483 }