2 #include <xcb/xproto.h>
3 #include <xcb/xcb_event.h>
12 /* We save the Atoms in an easy to access array, indexed by an enum */
16 #define ATOM_DO(name) name,
17 #include "xcb_atoms.def"
20 xcb_intern_atom_cookie_t atom_cookies[NUM_ATOMS];
21 xcb_atom_t atoms[NUM_ATOMS];
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;
29 /* Event-Watchers, to interact with the user */
35 * Converts a colorstring to a colorpixel as expected from xcb_change_gc.
36 * s is assumed to be in the format "rrggbb"
39 uint32_t get_colorpixel(const char *s) {
40 char strings[3][3] = { { s[0], s[1], '\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);
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
55 void handle_button(xcb_button_press_event_t *event) {
58 /* Determine, which bar was clicked */
60 xcb_window_t bar = event->event;
61 SLIST_FOREACH(walk, outputs, slist) {
62 if (walk->bar == bar) {
68 printf("Unknown Bar klicked!\n");
72 /* TODO: Move this to exern get_ws_for_output() */
73 TAILQ_FOREACH(cur_ws, walk->workspaces, tailq) {
74 if (cur_ws->visible) {
80 printf("No Workspace active?\n");
84 int32_t x = event->event_x;
86 printf("Got Button %d\n", event->detail);
88 switch (event->detail) {
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) {
97 x -= cur_ws->name_width + 10;
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);
108 cur_ws = TAILQ_PREV(cur_ws, ws_head, tailq);
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);
116 cur_ws = TAILQ_NEXT(cur_ws, tailq);
122 snprintf(buffer, 50, "%d", cur_ws->num);
123 i3_send_msg(I3_IPC_MESSAGE_TYPE_COMMAND, buffer);
127 * This function is called immediately bevor the main loop locks. We flush xcb
128 * then (and only then)
131 void xcb_prep_cb(struct ev_loop *loop, ev_prepare *watcher, int revenst) {
132 xcb_flush(xcb_connection);
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.
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) {
147 switch (event->response_type & ~0x80) {
149 /* Expose-events happen, when the window needs to be redrawn */
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);
161 * Dummy Callback. We only need this, so that the Prepare- and Check-Watchers
165 void xcb_io_cb(struct ev_loop *loop, ev_io *watcher, int revents) {
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)
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;
180 cookie = xcb_query_text_extents(xcb_connection, xcb_font, glyph_len, string);
181 reply = xcb_query_text_extents_reply(xcb_connection, cookie, &error);
183 printf("ERROR: Could not get text extents!");
187 width = reply->overall_width;
193 * Initialize xcb and use the specified fontname for text-rendering
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");
203 printf("Connected to xcb\n");
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"
209 xcb_screens = xcb_setup_roots_iterator(xcb_get_setup(xcb_connection)).data;
210 xcb_root = xcb_screens->root;
212 /* We load and allocate the font */
213 xcb_font = xcb_generate_id(xcb_connection);
214 xcb_open_font(xcb_connection,
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,
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));
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);
235 ev_io_start(main_loop, xcb_io);
236 ev_prepare_start(main_loop, xcb_prep);
237 ev_check_start(main_loop, xcb_chk);
239 /* Now we get the atoms and save them in a nice data-structure */
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,
247 font_height = reply->font_ascent + reply->font_descent;
250 printf("Calculated Font-height: %d\n", font_height);
254 * Cleanup the xcb-stuff.
255 * Called once, before the program terminates.
260 SLIST_FOREACH(walk, outputs, slist) {
261 destroy_window(walk);
263 FREE_SLIST(outputs, i3_output);
265 xcb_disconnect(xcb_connection);
267 ev_check_stop(main_loop, xcb_chk);
268 ev_prepare_stop(main_loop, xcb_prep);
269 ev_io_stop(main_loop, xcb_io);
277 * Get the earlier requested atoms and save them in the prepared data-structure
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; \
286 #include "xcb_atoms.def"
287 printf("Got Atoms\n");
291 * Destroy the bar of the specified output
294 void destroy_window(i3_output *output) {
295 if (output == NULL) {
298 if (output->bar == XCB_NONE) {
301 xcb_destroy_window(xcb_connection, output->bar);
302 output->bar = XCB_NONE;
306 * Reconfigure all bars and create new for newly activated outputs
309 void reconfig_windows() {
314 SLIST_FOREACH(walk, outputs, slist) {
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);
322 if (walk->bar == XCB_NONE) {
323 printf("Creating Window for output %s\n", walk->name);
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,
336 walk->rect.x, walk->rect.y,
337 walk->rect.w, font_height + 6,
339 XCB_WINDOW_CLASS_INPUT_OUTPUT,
340 xcb_screens->root_visual,
344 /* We want dock-windows (for now) */
345 xcb_change_property(xcb_connection,
346 XCB_PROP_MODE_REPLACE,
348 atoms[_NET_WM_WINDOW_TYPE],
352 (unsigned char*) &atoms[_NET_WM_WINDOW_TYPE_DOCK]);
354 /* We also want a graphics-context (the "canvas" on which we draw) */
355 walk->bargc = xcb_generate_id(xcb_connection);
357 values[0] = xcb_font;
358 xcb_create_gc(xcb_connection,
364 /* We finally map the bar (display it on screen) */
365 xcb_map_window(xcb_connection, walk->bar);
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,
386 * Render the bars, with buttons and statusline
390 printf("Drawing Bars...\n");
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);
398 if (outputs_walk->bar == XCB_NONE) {
401 uint32_t color = get_colorpixel("000000");
402 xcb_change_gc(xcb_connection,
406 xcb_rectangle_t rect = { 0, 0, outputs_walk->rect.w, font_height + 6 };
407 xcb_poly_fill_rectangle(xcb_connection,
412 if (statusline != NULL) {
413 printf("Printing statusline!\n");
414 xcb_change_gc(xcb_connection,
418 color = get_colorpixel("FFFFFF");
419 xcb_change_gc(xcb_connection,
425 xcb_char2b_t *text = (xcb_char2b_t*) convert_utf8_to_ucs2(statusline, &glyph_count);
427 xcb_void_cookie_t cookie;
428 cookie = xcb_image_text_16(xcb_connection,
432 outputs_walk->rect.w - get_string_width(text, glyph_count) - 4,
434 (xcb_char2b_t*) text);
436 xcb_generic_error_t *err = xcb_request_check(xcb_connection, cookie);
439 printf("XCB-Error: %d\n", err->error_code);
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");
449 if (ws_walk->urgent) {
450 printf("WS %s is urgent!\n", ws_walk->name);
451 color = get_colorpixel("002400");
453 xcb_change_gc(xcb_connection,
457 xcb_change_gc(xcb_connection,
461 xcb_rectangle_t rect = { i + 1, 1, ws_walk->name_width + 8, font_height + 4 };
462 xcb_poly_fill_rectangle(xcb_connection,
467 color = get_colorpixel("FFFFFF");
468 xcb_change_gc(xcb_connection,
472 xcb_image_text_16(xcb_connection,
473 ws_walk->name_glyphs,
476 i + 5, font_height + 1,
478 i += 10 + ws_walk->name_width;