13 #include <X11/XKBlib.h>
14 #include <X11/extensions/XKB.h>
18 #include "xcb_event.h"
19 #include "xcb_property.h"
20 #include "xcb_keysyms.h"
27 #define TERMINAL "/usr/pkg/bin/urxvt"
32 static const int TOP = 20;
33 static const int LEFT = 5;
34 static const int BOTTOM = 5;
35 static const int RIGHT = 5;
37 /* This is the filtered environment which will be passed to opened applications.
38 * It contains DISPLAY (naturally) and locales stuff (LC_*, LANG) */
39 static char **environment;
41 /* hm, xcb_wm wants us to implement this. */
43 table_t *byParent = 0;
44 xcb_window_t root_win;
46 char *pattern = "-misc-fixed-medium-r-normal--13-120-75-75-C-70-iso8859-1";
56 static const char *labelError[] = {
77 static const char *labelRequest[] = {
80 "ChangeWindowAttributes",
81 "GetWindowAttributes",
108 "ChangeActivePointerGrab",
158 "CopyColormapAndFree",
161 "ListInstalledColormaps",
178 "ChangeKeyboardMapping",
179 "GetKeyboardMapping",
180 "ChangeKeyboardControl",
181 "GetKeyboardControl",
183 "ChangePointerControl",
196 "SetModifierMapping",
197 "GetModifierMapping",
208 static const char *labelEvent[] = {
246 static const char *labelSendEvent[] = {
253 * TODO: what exactly does this, what happens if we leave stuff out?
256 void manage_window(xcb_property_handlers_t *prophs, xcb_connection_t *c, xcb_window_t window, window_attributes_t wa)
258 printf("managing window.\n");
259 xcb_drawable_t d = { window };
260 xcb_get_geometry_cookie_t geomc;
261 xcb_get_geometry_reply_t *geom;
262 xcb_get_window_attributes_reply_t *attr = 0;
263 if(wa.tag == TAG_COOKIE)
265 attr = xcb_get_window_attributes_reply(c, wa.u.cookie, 0);
268 if(attr->map_state != XCB_MAP_STATE_VIEWABLE)
270 printf("Window 0x%08x is not mapped. Ignoring.\n", window);
275 wa.u.override_redirect = attr->override_redirect;
277 if(!wa.u.override_redirect && table_get(byChild, window))
279 printf("Window 0x%08x already managed. Ignoring.\n", window);
283 if(wa.u.override_redirect)
285 printf("Window 0x%08x has override-redirect set. Ignoring.\n", window);
289 geomc = xcb_get_geometry(c, d);
293 wa.u.cookie = xcb_get_window_attributes(c, window);
294 attr = xcb_get_window_attributes_reply(c, wa.u.cookie, 0);
296 geom = xcb_get_geometry_reply(c, geomc, 0);
299 reparent_window(c, window, attr->visual, geom->root, geom->depth, geom->x, geom->y, geom->width, geom->height);
300 xcb_property_changed(prophs, XCB_PROPERTY_NEW_VALUE, window, WM_NAME);
308 * Returns the colorpixel to use for the given hex color (think of HTML).
310 * The hex_color has to start with #, for example #FF00FF.
312 * NOTE that get_colorpixel() does _NOT_ check the given color code for validity.
313 * This has to be done by the caller.
316 uint32_t get_colorpixel(xcb_connection_t *conn, xcb_window_t window, char *hex) {
317 #define RGB_8_TO_16(i) (65535 * ((i) & 0xFF) / 255)
318 char strgroups[3][3] = {{hex[1], hex[2], '\0'},
319 {hex[3], hex[4], '\0'},
320 {hex[5], hex[6], '\0'}};
321 int rgb16[3] = {RGB_8_TO_16(strtol(strgroups[0], NULL, 16)),
322 RGB_8_TO_16(strtol(strgroups[1], NULL, 16)),
323 RGB_8_TO_16(strtol(strgroups[2], NULL, 16))};
325 xcb_screen_t *root_screen = xcb_setup_roots_iterator(xcb_get_setup(conn)).data;
327 xcb_colormap_t colormapId = xcb_generate_id(conn);
328 xcb_create_colormap(conn, XCB_COLORMAP_ALLOC_NONE, colormapId, window, root_screen->root_visual);
329 xcb_alloc_color_reply_t *reply = xcb_alloc_color_reply(conn,
330 xcb_alloc_color(conn, colormapId, rgb16[0], rgb16[1], rgb16[2]), NULL);
333 printf("color fail\n");
337 uint32_t pixel = reply->pixel;
339 xcb_free_colormap(conn, colormapId);
344 * (Re-)draws window decorations for a given Client
347 void decorate_window(xcb_connection_t *conn, Client *client) {
350 xcb_screen_t *root_screen = xcb_setup_roots_iterator(xcb_get_setup(conn)).data;
351 i3Font *font = load_font(conn, pattern);
352 uint32_t background_color,
356 if (client->container->currently_focused == client) {
357 background_color = get_colorpixel(conn, client->frame, "#285577");
358 text_color = get_colorpixel(conn, client->frame, "#ffffff");
359 border_color = get_colorpixel(conn, client->frame, "#4c7899");
361 background_color = get_colorpixel(conn, client->frame, "#222222");
362 text_color = get_colorpixel(conn, client->frame, "#888888");
363 border_color = get_colorpixel(conn, client->frame, "#333333");
366 /* Our plan is the following:
367 - Draw a rect around the whole client in background_color
368 - Draw two lines in a lighter color
369 - Draw the window’s title
371 Note that xcb_image_text apparently adds 1xp border around the font? Can anyone confirm this?
374 /* Draw a green rectangle around the window */
375 mask = XCB_GC_FOREGROUND;
376 values[0] = background_color;
377 xcb_change_gc(conn, client->titlegc, mask, values);
379 xcb_rectangle_t rect = {0, 0, client->width, client->height};
380 xcb_poly_fill_rectangle(conn, client->frame, client->titlegc, 1, &rect);
383 /* TODO: this needs to be more beautiful somewhen. maybe stdarg + change_gc(gc, ...) ? */
384 #define DRAW_LINE(colorpixel, x, y, to_x, to_y) { \
385 uint32_t draw_values[1]; \
386 draw_values[0] = colorpixel; \
387 xcb_change_gc(conn, client->titlegc, XCB_GC_FOREGROUND, draw_values); \
388 xcb_point_t points[] = {{x, y}, {to_x, to_y}}; \
389 xcb_poly_line(conn, XCB_COORD_MODE_ORIGIN, client->frame, client->titlegc, 2, points); \
392 DRAW_LINE(border_color, 2, 0, client->width, 0);
393 DRAW_LINE(border_color, 2, font->height + 3, 2 + client->width, font->height + 3);
396 mask = XCB_GC_FOREGROUND | XCB_GC_BACKGROUND | XCB_GC_FONT;
398 values[0] = text_color;
399 values[1] = background_color;
400 values[2] = font->id;
402 xcb_change_gc(conn, client->titlegc, mask, values);
406 asprintf(&label, "gots win %08x", client->frame);
407 xcb_void_cookie_t text_cookie = xcb_image_text_8_checked(conn, strlen(label), client->frame,
408 client->titlegc, 3 /* X */, font->height /* Y = baseline of font */, label);
412 void render_container(xcb_connection_t *connection, Container *container) {
415 uint32_t mask = XCB_CONFIG_WINDOW_X |
416 XCB_CONFIG_WINDOW_Y |
417 XCB_CONFIG_WINDOW_WIDTH |
418 XCB_CONFIG_WINDOW_HEIGHT;
419 i3Font *font = load_font(connection, pattern);
421 if (container->mode == MODE_DEFAULT) {
423 CIRCLEQ_FOREACH(client, &(container->clients), clients)
425 printf("got %d clients in this default container.\n", num_clients);
427 int current_client = 0;
428 CIRCLEQ_FOREACH(client, &(container->clients), clients) {
429 /* TODO: at the moment, every column/row is 200px. This
430 * needs to be changed to "percentage of the screen" by
431 * default and adjustable by the user if necessary.
433 values[0] = container->col * container->width; /* x */
434 values[1] = container->row * container->height +
435 (container->height / num_clients) * current_client; /* y */
436 /* TODO: vertical default layout */
437 values[2] = container->width; /* width */
438 values[3] = container->height / num_clients; /* height */
439 printf("frame will be at %dx%d with size %dx%d\n",
440 values[0], values[1], values[2], values[3]);
442 client->width = values[2];
443 client->height = values[3];
445 /* TODO: update only if necessary */
446 xcb_configure_window(connection, client->frame, mask, values);
448 /* The coordinates of the child are relative to its frame, we
449 * add a border of 2 pixel to each value */
451 values[1] = font->height + 2 + 2;
452 values[2] -= values[0] + 2;
453 values[3] -= values[1] + 2;
454 printf("child itself will be at %dx%d with size %dx%d\n",
455 values[0], values[1], values[2], values[3]);
457 xcb_configure_window(connection, client->child, mask, values);
459 decorate_window(connection, client);
463 /* TODO: Implement stacking */
467 void render_layout(xcb_connection_t *conn) {
469 xcb_screen_t *root_screen = xcb_setup_roots_iterator(xcb_get_setup(conn)).data;
470 int width = root_screen->width_in_pixels;
471 int height = root_screen->height_in_pixels;
473 int num_cols = table_dims.x, num_rows = table_dims.y;
475 printf("got %d rows and %d cols\n", num_rows, num_cols);
476 printf("each of them therefore is %d px width and %d px height\n",
477 width / num_cols, height / num_rows);
479 /* Go through the whole table and render what’s necessary */
480 for (cols = 0; cols < table_dims.x; cols++)
481 for (rows = 0; rows < table_dims.y; rows++)
482 if (table[cols][rows] != NULL) {
483 Container *con = table[cols][rows];
484 printf("container has %d colspan, %d rowspan\n",
485 con->colspan, con->rowspan);
486 /* Update position of the container */
489 con->width = (width / num_cols) * con->colspan;
490 con->height = (height / num_rows) * con->rowspan;
493 render_container(conn, table[cols][rows]);
500 * Let’s own this window…
503 void reparent_window(xcb_connection_t *conn, xcb_window_t child,
504 xcb_visualid_t visual, xcb_window_t root, uint8_t depth,
505 int16_t x, int16_t y, uint16_t width, uint16_t height) {
507 Client *new = table_get(byChild, child);
509 printf("oh, it's new\n");
510 new = calloc(sizeof(Client), 1);
514 xcb_screen_t *root_screen = xcb_setup_roots_iterator(xcb_get_setup(conn)).data;
516 /* Insert into the currently active container */
517 CIRCLEQ_INSERT_TAIL(&(CUR_CELL->clients), new, clients);
519 printf("currently_focused = %p\n", new);
520 CUR_CELL->currently_focused = new;
521 new->container = CUR_CELL;
523 new->frame = xcb_generate_id(conn);
526 new->height = height;
528 /* TODO: what do these mean? */
529 mask |= XCB_CW_BACK_PIXEL;
530 values[0] = root_screen->white_pixel;
532 mask |= XCB_CW_OVERRIDE_REDIRECT;
535 mask |= XCB_CW_EVENT_MASK;
536 values[2] = XCB_EVENT_MASK_BUTTON_PRESS | XCB_EVENT_MASK_BUTTON_RELEASE
537 | XCB_EVENT_MASK_EXPOSURE | XCB_EVENT_MASK_ENTER_WINDOW;
539 printf("Reparenting 0x%08x under 0x%08x.\n", child, new->frame);
541 /* Yo dawg, I heard you like windows, so I create a window around your window… */
542 xcb_create_window(conn,
548 width + LEFT + RIGHT,
549 height + TOP + BOTTOM,
550 /* border_width */ 0,
551 XCB_WINDOW_CLASS_INPUT_OUTPUT,
555 xcb_change_save_set(conn, XCB_SET_MODE_INSERT, child);
557 /* Map the window on the screen (= make it visible) */
558 xcb_map_window(conn, new->frame);
560 /* Generate a graphics context for the titlebar */
561 new->titlegc = xcb_generate_id(conn);
562 xcb_create_gc(conn, new->titlegc, new->frame, 0, 0);
564 /* Draw decorations */
565 decorate_window(conn, new);
567 /* Put our data structure (Client) into the table */
568 table_put(byParent, new->frame, new);
569 table_put(byChild, child, new);
571 /* Moves the original window into the new frame we've created for it */
572 i3Font *font = load_font(conn, pattern);
573 xcb_reparent_window(conn, child, new->frame, 0, font->height);
575 /* We are interested in property changes */
576 mask = XCB_CW_EVENT_MASK;
577 values[0] = XCB_EVENT_MASK_PROPERTY_CHANGE |
578 XCB_EVENT_MASK_STRUCTURE_NOTIFY |
579 XCB_EVENT_MASK_ENTER_WINDOW;
580 xcb_change_window_attributes(conn, child, mask, values);
582 /* TODO: At the moment, new windows just get focus */
583 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_NONE, new->frame, XCB_CURRENT_TIME);
590 static bool focus_window_in_container(xcb_connection_t *connection, Container *container,
591 direction_t direction) {
592 /* If this container is empty, we’re done */
593 if (container->currently_focused == NULL)
597 if (direction == D_UP)
598 candidad = CIRCLEQ_PREV(container->currently_focused, clients);
599 else if (direction == D_DOWN)
600 candidad = CIRCLEQ_NEXT(container->currently_focused, clients);
602 /* If we don’t have anything to select, we’re done */
603 if (candidad == CIRCLEQ_END(&(container->clients)))
606 /* Set focus if we could successfully move */
607 container->currently_focused = candidad;
608 xcb_set_input_focus(connection, XCB_INPUT_FOCUS_NONE, candidad->child, XCB_CURRENT_TIME);
609 render_layout(connection);
610 xcb_flush(connection);
615 static void focus_window(xcb_connection_t *connection, direction_t direction) {
616 printf("focusing direction %d\n", direction);
617 /* TODO: for horizontal default layout, this has to be expanded to LEFT/RIGHT */
618 if (direction == D_UP || direction == D_DOWN) {
619 /* Let’s see if we can perform up/down focus in the current container */
620 Container *container = CUR_CELL;
622 /* There always is a container. If not, current_col or current_row is wrong */
623 assert(container != NULL);
625 if (focus_window_in_container(connection, container, direction))
627 } else if (direction == D_LEFT || direction == D_RIGHT) {
628 if (direction == D_RIGHT && cell_exists(current_col+1, current_row))
630 else if (direction == D_LEFT && cell_exists(current_col-1, current_row))
633 printf("nah, not possible\n");
636 if (CUR_CELL->currently_focused != NULL) {
637 xcb_set_input_focus(connection, XCB_INPUT_FOCUS_NONE,
638 CUR_CELL->currently_focused->child, XCB_CURRENT_TIME);
639 render_layout(connection);
640 xcb_flush(connection);
644 printf("direction unhandled\n");
649 * Tries to move the window inside its current container.
651 * Returns true if the window could be moved, false otherwise.
654 static bool move_current_window_in_container(xcb_connection_t *connection, Client *client,
655 direction_t direction) {
656 Client *other = (direction == D_UP ? CIRCLEQ_PREV(client, clients) :
657 CIRCLEQ_NEXT(client, clients));
659 if (other == CIRCLEQ_END(&(client->container->clients)))
662 printf("i can do that\n");
663 /* We can move the client inside its current container */
664 CIRCLEQ_REMOVE(&(client->container->clients), client, clients);
665 if (direction == D_UP)
666 CIRCLEQ_INSERT_BEFORE(&(client->container->clients), other, client, clients);
667 else CIRCLEQ_INSERT_AFTER(&(client->container->clients), other, client, clients);
668 render_layout(connection);
673 * Moves the current window to the given direction, creating a column/row if
677 static void move_current_window(xcb_connection_t *connection, direction_t direction) {
678 printf("moving window to direction %d\n", direction);
679 /* Get current window */
680 Container *container = CUR_CELL,
683 /* There has to be a container, see focus_window() */
684 assert(container != NULL);
686 /* If there is no window, we’re done */
687 if (container->currently_focused == NULL)
690 /* As soon as the client is moved away, the next client in the old
691 * container needs to get focus, if any. Therefore, we save it here. */
692 Client *current_client = container->currently_focused;
693 Client *to_focus = CIRCLEQ_NEXT(current_client, clients);
694 if (to_focus == CIRCLEQ_END(&(container->clients)))
699 if (current_col == 0)
702 new = table[--current_col][current_row];
705 if (current_col == (table_dims.x-1))
708 new = table[++current_col][current_row];
711 if (move_current_window_in_container(connection, current_client, D_UP) ||
715 new = table[current_col][--current_row];
718 if (move_current_window_in_container(connection, current_client, D_DOWN))
721 if (current_row == (table_dims.y-1))
724 new = table[current_col][++current_row];
728 /* Remove it from the old container and put it into the new one */
729 CIRCLEQ_REMOVE(&(container->clients), current_client, clients);
730 CIRCLEQ_INSERT_TAIL(&(new->clients), current_client, clients);
732 /* Update data structures */
733 current_client->container = new;
734 container->currently_focused = to_focus;
735 new->currently_focused = current_client;
737 /* TODO: delete all empty columns/rows */
739 render_layout(connection);
742 int format_event(xcb_generic_event_t *e)
747 sendEvent = (e->response_type & 0x80) ? 1 : 0;
748 e->response_type &= ~0x80;
749 seqnum = *((uint16_t *) e + 1);
751 switch(e->response_type)
754 printf("Error %s on seqnum %d (%s).\n",
755 labelError[*((uint8_t *) e + 1)],
757 labelRequest[*((uint8_t *) e + 10)]);
760 printf("Event %s following seqnum %d%s.\n",
761 labelEvent[e->response_type],
763 labelSendEvent[sendEvent]);
765 case XCB_KEYMAP_NOTIFY:
766 printf("Event %s%s.\n",
767 labelEvent[e->response_type],
768 labelSendEvent[sendEvent]);
777 static int handleEvent(void *ignored, xcb_connection_t *c, xcb_generic_event_t *e)
779 return format_event(e);
783 * Starts the given application with the given args.
786 static void start_application(char *path, char *args) {
788 if ((pid = vfork()) == 0) {
789 /* This is the child */
791 /* TODO: For now, we ignore args. Later on, they should be parsed
792 correctly (like in the shell?) */
795 execve(path, argv, environment);
801 * There was a key press. We lookup the key symbol and see if there are any bindings
802 * on that. This allows to do things like binding special characters (think of ä) to
803 * functions to get one more modifier while not losing AltGr :-)
806 static int handle_key_press(void *ignored, xcb_connection_t *conn, xcb_key_press_event_t *event) {
807 /* FIXME: We need to translate the keypress + state into a string (like, ä)
808 because they do not generate keysyms (use xev and see for yourself) */
811 printf("gots press %d\n", event->detail);
813 /* We need to get the keysym group (There are group 1 to group 4, each holding
814 two keysyms (without shift and with shift) using Xkb because X fails to
815 provide them reliably (it works in Xephyr, it does not in real X) */
817 if (XkbGetState(xkbdpy, XkbUseCoreKbd, &state) == Success) {
818 if (state.group+1 == 2)
821 printf("i'm in state %d\n", event->state);
824 xcb_key_symbols_t *keysyms = xcb_key_symbols_alloc(conn);
826 xcb_keysym_t k0 = xcb_key_symbols_get_keysym(keysyms, event->detail, event->state);
828 printf("couldn't get k0\n");
830 printf("gots keysym %d and \n", k0);
839 * …uhm, I should probably say that I’ve remapped my keys in hardware :)
841 direction_t direction;
842 if (event->detail == 30) {
844 start_application(TERMINAL, NULL);
847 } else if (event->detail == 57) {
849 } else if (event->detail == 27) {
851 } else if (event->detail == 28) {
853 } else if (event->detail == 40) {
855 } else if (event->detail == 25) {
856 Container *con = CUR_CELL;
857 if (con->colspan == 1)
864 printf("don't want this.\n");
868 /* TODO: ctrl -> focus_container(conn, direction) */
869 /* FIXME: actually wrong but i'm too lazy to grab my keys all the time */
870 if (event->state & XCB_MOD_MASK_CONTROL) {
871 move_current_window(conn, direction);
872 } else if (event->state & XCB_MOD_MASK_1)
873 focus_window(conn, direction);
874 /* TODO: shift -> move_current_window(conn, direction) */
875 /* TODO: shift + ctrl -> move_current_container(conn, direction) */
881 * When the user moves the mouse pointer onto a window, this callback gets called.
884 static int handle_enter_notify(void *ignored, xcb_connection_t *conn, xcb_enter_notify_event_t *event) {
885 /* This was either a focus for a client’s parent (= titlebar)… */
886 Client *client = table_get(byParent, event->event),
888 /* …or the client itself */
890 client = table_get(byChild, event->event);
892 /* If not, then this event is not interesting. This should not happen */
893 if (client == NULL) {
894 printf("DEBUG: Uninteresting enter_notify-event?\n");
898 /* Update container */
899 old_client = client->container->currently_focused;
900 client->container->currently_focused = client;
902 current_col = client->container->col;
903 current_row = client->container->row;
905 /* Set focus to the entered window, and flush xcb buffer immediately */
906 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_POINTER_ROOT, client->child, XCB_CURRENT_TIME);
907 /* Update last/current client’s titlebar */
908 if (old_client != NULL)
909 decorate_window(conn, old_client);
910 decorate_window(conn, client);
917 int handle_map_notify_event(void *prophs, xcb_connection_t *c, xcb_map_notify_event_t *e)
919 window_attributes_t wa = { TAG_VALUE };
920 wa.u.override_redirect = e->override_redirect;
921 printf("MapNotify for 0x%08x.\n", e->window);
922 manage_window(prophs, c, e->window, wa);
927 * Our window decorations were unmapped. That means, the window will be killed now,
928 * so we better clean up before.
931 int handle_unmap_notify_event(void *data, xcb_connection_t *c, xcb_unmap_notify_event_t *e) {
932 Client *client = table_remove(byChild, e->event);
934 printf("UnmapNotify for 0x%08x (received from 0x%08x): ", e->window, e->event);
937 printf("not a managed window. Ignoring.\n");
943 for (cols = 0; cols < table_dims.x; cols++)
944 for (rows = 0; rows < table_dims.y; rows++)
945 CIRCLEQ_FOREACH(con_client, &(table[cols][rows]->clients), clients)
946 if (con_client == client) {
947 printf("removing from container\n");
948 if (client->container->currently_focused == client)
949 client->container->currently_focused = NULL;
950 CIRCLEQ_REMOVE(&(table[cols][rows]->clients), con_client, clients);
956 root = xcb_setup_roots_iterator(xcb_get_setup(c)).data->root;
957 printf("child of 0x%08x.\n", client->frame);
958 xcb_reparent_window(c, client->child, root, 0, 0);
959 xcb_destroy_window(c, client->frame);
961 table_remove(byParent, client->frame);
971 static int handleExposeEvent(void *data, xcb_connection_t *c, xcb_expose_event_t *e) {
972 printf("exposeevent\n");
973 Client *client = table_get(byParent, e->window);
974 if(!client || e->count != 0)
976 decorate_window(c, client);
979 void manage_existing_windows(xcb_connection_t *c, xcb_property_handlers_t *prophs, xcb_window_t root) {
980 xcb_query_tree_cookie_t wintree;
981 xcb_query_tree_reply_t *rep;
983 xcb_window_t *children;
984 xcb_get_window_attributes_cookie_t *cookies;
986 wintree = xcb_query_tree(c, root);
987 rep = xcb_query_tree_reply(c, wintree, 0);
990 len = xcb_query_tree_children_length(rep);
991 cookies = malloc(len * sizeof(*cookies));
997 children = xcb_query_tree_children(rep);
998 for(i = 0; i < len; ++i)
999 cookies[i] = xcb_get_window_attributes(c, children[i]);
1000 for(i = 0; i < len; ++i)
1002 window_attributes_t wa = { TAG_COOKIE, { cookies[i] } };
1003 manage_window(prophs, c, children[i], wa);
1008 int main(int argc, char *argv[], char *env[]) {
1011 for (i = 0; (env[i] != NULL); i++)
1012 if (strncmp(env[i], "LC_", strlen("LC_")) == 0 ||
1013 strncmp(env[i], "LANG=", strlen("LANG=")) == 0 ||
1014 strncmp(env[i], "DISPLAY=", strlen("DISPLAY=")) == 0) {
1015 printf("Passing environment \"%s\"\n", env[i]);
1016 environment = realloc(environment, sizeof(char*) * ++e);
1017 environment[e-1] = env[i];
1020 /* environment has to be NULL-terminated */
1021 environment = realloc(environment, sizeof(char*) * ++e);
1022 environment[e-1] = NULL;
1026 xcb_connection_t *c;
1027 xcb_event_handlers_t evenths;
1028 xcb_property_handlers_t prophs;
1033 memset(&evenths, 0, sizeof(xcb_event_handlers_t));
1034 memset(&prophs, 0, sizeof(xcb_property_handlers_t));
1036 byChild = alloc_table();
1037 byParent = alloc_table();
1039 c = xcb_connect(NULL, &screens);
1041 printf("x screen is %d\n", screens);
1043 int major, minor, error;
1045 major = XkbMajorVersion;
1046 minor = XkbMinorVersion;
1048 int evBase, errBase;
1050 if ((xkbdpy = XkbOpenDisplay(getenv("DISPLAY"), &evBase, &errBase, &major, &minor, &error)) == NULL) {
1051 fprintf(stderr, "XkbOpenDisplay() failed\n");
1056 if (!XkbQueryExtension(xkbdpy,&i1,&evBase,&errBase,&major,&minor)) {
1057 fprintf(stderr, "XKB not supported by X-server\n");
1062 myfont = load_font(c, pattern);
1064 xcb_event_handlers_init(c, &evenths);
1065 for(i = 2; i < 128; ++i)
1066 xcb_event_set_handler(&evenths, i, handleEvent, 0);
1068 for(i = 0; i < 256; ++i)
1069 xcb_event_set_error_handler(&evenths, i, (xcb_generic_error_handler_t) handleEvent, 0);
1071 /* Expose = an Application should redraw itself. That is, we have to redraw our
1072 * contents (= top/bottom bar, titlebars for each window) */
1073 xcb_event_set_expose_handler(&evenths, handleExposeEvent, 0);
1075 /* Key presses are pretty obvious, I think */
1076 xcb_event_set_key_press_handler(&evenths, handle_key_press, 0);
1078 /* Enter window = user moved his mouse over the window */
1079 xcb_event_set_enter_notify_handler(&evenths, handle_enter_notify, 0);
1081 xcb_event_set_unmap_notify_handler(&evenths, handle_unmap_notify_event, 0);
1083 xcb_property_handlers_init(&prophs, &evenths);
1084 xcb_event_set_map_notify_handler(&evenths, handle_map_notify_event, &prophs);
1086 root = xcb_aux_get_screen(c, screens)->root;
1089 uint32_t mask = XCB_CW_EVENT_MASK;
1090 uint32_t values[] = { XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY | XCB_EVENT_MASK_PROPERTY_CHANGE };
1091 xcb_change_window_attributes(c, root, mask, values);
1094 //xcb_grab_key(c, 0, root, 0, 38, XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_ASYNC);
1096 xcb_grab_key(c, 0, root, 0, 30, XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_ASYNC);
1097 xcb_grab_key(c, 0, root, 0, 38, XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_ASYNC);
1100 xcb_grab_key(c, 0, root, XCB_MOD_MASK_1, 57, XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_ASYNC);
1101 xcb_grab_key(c, 0, root, XCB_MOD_MASK_1, 28, XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_ASYNC);
1102 xcb_grab_key(c, 0, root, XCB_MOD_MASK_1, 27, XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_ASYNC);
1103 xcb_grab_key(c, 0, root, XCB_MOD_MASK_1, 40, XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_ASYNC);
1105 xcb_grab_key(c, 0, root, XCB_MOD_MASK_CONTROL, 57, XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_ASYNC);
1106 xcb_grab_key(c, 0, root, XCB_MOD_MASK_CONTROL, 28, XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_ASYNC);
1107 xcb_grab_key(c, 0, root, XCB_MOD_MASK_CONTROL, 27, XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_ASYNC);
1108 xcb_grab_key(c, 0, root, XCB_MOD_MASK_CONTROL, 40, XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_ASYNC);
1109 xcb_grab_key(c, 0, root, XCB_MOD_MASK_CONTROL, 25, XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_ASYNC);
1113 //xcb_grab_key(c, 0, root, XCB_BUTTON_MASK_ANY, 40, XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_ASYNC);
1114 start_application(TERMINAL, NULL);
1118 manage_existing_windows(c, &prophs, root);
1120 xcb_event_wait_for_event_loop(&evenths);