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"
31 TAILQ_HEAD(bindings_head, Binding) bindings;
33 static const int TOP = 20;
34 static const int LEFT = 5;
35 static const int BOTTOM = 5;
36 static const int RIGHT = 5;
38 /* This is the filtered environment which will be passed to opened applications.
39 * It contains DISPLAY (naturally) and locales stuff (LC_*, LANG) */
40 static char **environment;
42 /* hm, xcb_wm wants us to implement this. */
44 table_t *byParent = 0;
45 xcb_window_t root_win;
47 char *pattern = "-misc-fixed-medium-r-normal--13-120-75-75-C-70-iso8859-1";
57 static const char *labelError[] = {
78 static const char *labelRequest[] = {
81 "ChangeWindowAttributes",
82 "GetWindowAttributes",
109 "ChangeActivePointerGrab",
159 "CopyColormapAndFree",
162 "ListInstalledColormaps",
179 "ChangeKeyboardMapping",
180 "GetKeyboardMapping",
181 "ChangeKeyboardControl",
182 "GetKeyboardControl",
184 "ChangePointerControl",
197 "SetModifierMapping",
198 "GetModifierMapping",
209 static const char *labelEvent[] = {
247 static const char *labelSendEvent[] = {
254 * TODO: what exactly does this, what happens if we leave stuff out?
257 void manage_window(xcb_property_handlers_t *prophs, xcb_connection_t *c, xcb_window_t window, window_attributes_t wa)
259 printf("managing window.\n");
260 xcb_drawable_t d = { window };
261 xcb_get_geometry_cookie_t geomc;
262 xcb_get_geometry_reply_t *geom;
263 xcb_get_window_attributes_reply_t *attr = 0;
264 if(wa.tag == TAG_COOKIE)
266 attr = xcb_get_window_attributes_reply(c, wa.u.cookie, 0);
269 if(attr->map_state != XCB_MAP_STATE_VIEWABLE)
271 printf("Window 0x%08x is not mapped. Ignoring.\n", window);
276 wa.u.override_redirect = attr->override_redirect;
278 if(!wa.u.override_redirect && table_get(byChild, window))
280 printf("Window 0x%08x already managed. Ignoring.\n", window);
284 if(wa.u.override_redirect)
286 printf("Window 0x%08x has override-redirect set. Ignoring.\n", window);
290 geomc = xcb_get_geometry(c, d);
294 wa.u.cookie = xcb_get_window_attributes(c, window);
295 attr = xcb_get_window_attributes_reply(c, wa.u.cookie, 0);
297 geom = xcb_get_geometry_reply(c, geomc, 0);
300 reparent_window(c, window, attr->visual, geom->root, geom->depth, geom->x, geom->y, geom->width, geom->height);
301 xcb_property_changed(prophs, XCB_PROPERTY_NEW_VALUE, window, WM_NAME);
309 * Returns the colorpixel to use for the given hex color (think of HTML).
311 * The hex_color has to start with #, for example #FF00FF.
313 * NOTE that get_colorpixel() does _NOT_ check the given color code for validity.
314 * This has to be done by the caller.
317 uint32_t get_colorpixel(xcb_connection_t *conn, xcb_window_t window, char *hex) {
318 #define RGB_8_TO_16(i) (65535 * ((i) & 0xFF) / 255)
319 char strgroups[3][3] = {{hex[1], hex[2], '\0'},
320 {hex[3], hex[4], '\0'},
321 {hex[5], hex[6], '\0'}};
322 int rgb16[3] = {RGB_8_TO_16(strtol(strgroups[0], NULL, 16)),
323 RGB_8_TO_16(strtol(strgroups[1], NULL, 16)),
324 RGB_8_TO_16(strtol(strgroups[2], NULL, 16))};
326 xcb_screen_t *root_screen = xcb_setup_roots_iterator(xcb_get_setup(conn)).data;
328 xcb_colormap_t colormapId = xcb_generate_id(conn);
329 xcb_create_colormap(conn, XCB_COLORMAP_ALLOC_NONE, colormapId, window, root_screen->root_visual);
330 xcb_alloc_color_reply_t *reply = xcb_alloc_color_reply(conn,
331 xcb_alloc_color(conn, colormapId, rgb16[0], rgb16[1], rgb16[2]), NULL);
334 printf("color fail\n");
338 uint32_t pixel = reply->pixel;
340 xcb_free_colormap(conn, colormapId);
345 * (Re-)draws window decorations for a given Client
348 void decorate_window(xcb_connection_t *conn, Client *client) {
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: rewrite this block so that the need to puke vanishes :) */
430 /* TODO: at the moment, every column/row is 200px. This
431 * needs to be changed to "percentage of the screen" by
432 * default and adjustable by the user if necessary.
434 values[0] = container->col * container->width; /* x */
435 values[1] = container->row * container->height +
436 (container->height / num_clients) * current_client; /* y */
438 if (client->x != values[0] || client->y != values[1]) {
439 printf("frame needs to be pushed to %dx%d\n",
440 values[0], values[1]);
441 client->x = values[0];
442 client->y = values[1];
443 xcb_configure_window(connection, client->frame,
444 XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y, values);
446 /* TODO: vertical default layout */
447 values[0] = container->width; /* width */
448 values[1] = container->height / num_clients; /* height */
450 if (client->width != values[0] || client->height != values[1]) {
451 client->width = values[0];
452 client->height = values[1];
453 xcb_configure_window(connection, client->frame,
454 XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT, values);
457 /* TODO: hmm, only do this for new wins */
458 /* The coordinates of the child are relative to its frame, we
459 * add a border of 2 pixel to each value */
461 values[1] = font->height + 2 + 2;
462 values[2] = client->width - (values[0] + 2);
463 values[3] = client->height - (values[1] + 2);
464 printf("child itself will be at %dx%d with size %dx%d\n",
465 values[0], values[1], values[2], values[3]);
467 xcb_configure_window(connection, client->child, mask, values);
469 decorate_window(connection, client);
473 /* TODO: Implement stacking */
477 void render_layout(xcb_connection_t *conn) {
479 xcb_screen_t *root_screen = xcb_setup_roots_iterator(xcb_get_setup(conn)).data;
480 int width = root_screen->width_in_pixels;
481 int height = root_screen->height_in_pixels;
483 int num_cols = table_dims.x, num_rows = table_dims.y;
485 printf("got %d rows and %d cols\n", num_rows, num_cols);
486 printf("each of them therefore is %d px width and %d px height\n",
487 width / num_cols, height / num_rows);
489 /* Go through the whole table and render what’s necessary */
490 for (cols = 0; cols < table_dims.x; cols++)
491 for (rows = 0; rows < table_dims.y; rows++)
492 if (table[cols][rows] != NULL) {
493 Container *con = table[cols][rows];
494 printf("container has %d colspan, %d rowspan\n",
495 con->colspan, con->rowspan);
496 /* Update position of the container */
499 con->width = (width / num_cols) * con->colspan;
500 con->height = (height / num_rows) * con->rowspan;
503 render_container(conn, table[cols][rows]);
510 * Let’s own this window…
513 void reparent_window(xcb_connection_t *conn, xcb_window_t child,
514 xcb_visualid_t visual, xcb_window_t root, uint8_t depth,
515 int16_t x, int16_t y, uint16_t width, uint16_t height) {
517 Client *new = table_get(byChild, child);
519 printf("oh, it's new\n");
520 new = calloc(sizeof(Client), 1);
527 /* Insert into the currently active container */
528 CIRCLEQ_INSERT_TAIL(&(CUR_CELL->clients), new, clients);
530 printf("currently_focused = %p\n", new);
531 CUR_CELL->currently_focused = new;
532 new->container = CUR_CELL;
534 new->frame = xcb_generate_id(conn);
537 new->height = height;
539 /* Don’t generate events for our new window, it should *not* be managed */
540 mask |= XCB_CW_OVERRIDE_REDIRECT;
543 /* We want to know when… */
544 mask |= XCB_CW_EVENT_MASK;
545 values[1] = XCB_EVENT_MASK_BUTTON_PRESS | /* …mouse is pressed/released */
546 XCB_EVENT_MASK_BUTTON_RELEASE |
547 XCB_EVENT_MASK_EXPOSURE | /* …our window needs to be redrawn */
548 XCB_EVENT_MASK_ENTER_WINDOW /* …user moves cursor inside our window */;
550 printf("Reparenting 0x%08x under 0x%08x.\n", child, new->frame);
552 /* Yo dawg, I heard you like windows, so I create a window around your window… */
553 xcb_create_window(conn,
559 width + LEFT + RIGHT,
560 height + TOP + BOTTOM,
561 /* border_width */ 0,
562 XCB_WINDOW_CLASS_INPUT_OUTPUT,
566 xcb_change_save_set(conn, XCB_SET_MODE_INSERT, child);
568 /* Map the window on the screen (= make it visible) */
569 xcb_map_window(conn, new->frame);
571 /* Generate a graphics context for the titlebar */
572 new->titlegc = xcb_generate_id(conn);
573 xcb_create_gc(conn, new->titlegc, new->frame, 0, 0);
575 /* Draw decorations */
576 decorate_window(conn, new);
578 /* Put our data structure (Client) into the table */
579 table_put(byParent, new->frame, new);
580 table_put(byChild, child, new);
582 /* Moves the original window into the new frame we've created for it */
583 i3Font *font = load_font(conn, pattern);
584 xcb_reparent_window(conn, child, new->frame, 0, font->height);
586 /* We are interested in property changes */
587 mask = XCB_CW_EVENT_MASK;
588 values[0] = XCB_EVENT_MASK_PROPERTY_CHANGE |
589 XCB_EVENT_MASK_STRUCTURE_NOTIFY |
590 XCB_EVENT_MASK_ENTER_WINDOW;
591 xcb_change_window_attributes(conn, child, mask, values);
593 /* Focus the new window */
594 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_NONE, new->child, XCB_CURRENT_TIME);
599 static bool focus_window_in_container(xcb_connection_t *connection, Container *container,
600 direction_t direction) {
601 /* If this container is empty, we’re done */
602 if (container->currently_focused == NULL)
606 if (direction == D_UP)
607 candidad = CIRCLEQ_PREV(container->currently_focused, clients);
608 else if (direction == D_DOWN)
609 candidad = CIRCLEQ_NEXT(container->currently_focused, clients);
611 /* If we don’t have anything to select, we’re done */
612 if (candidad == CIRCLEQ_END(&(container->clients)))
615 /* Set focus if we could successfully move */
616 container->currently_focused = candidad;
617 xcb_set_input_focus(connection, XCB_INPUT_FOCUS_NONE, candidad->child, XCB_CURRENT_TIME);
618 render_layout(connection);
623 static void focus_window(xcb_connection_t *connection, direction_t direction) {
624 printf("focusing direction %d\n", direction);
625 /* TODO: for horizontal default layout, this has to be expanded to LEFT/RIGHT */
626 if (direction == D_UP || direction == D_DOWN) {
627 /* Let’s see if we can perform up/down focus in the current container */
628 Container *container = CUR_CELL;
630 /* There always is a container. If not, current_col or current_row is wrong */
631 assert(container != NULL);
633 if (focus_window_in_container(connection, container, direction))
635 } else if (direction == D_LEFT || direction == D_RIGHT) {
636 if (direction == D_RIGHT && cell_exists(current_col+1, current_row))
638 else if (direction == D_LEFT && cell_exists(current_col-1, current_row))
641 printf("nah, not possible\n");
644 if (CUR_CELL->currently_focused != NULL) {
645 xcb_set_input_focus(connection, XCB_INPUT_FOCUS_NONE,
646 CUR_CELL->currently_focused->child, XCB_CURRENT_TIME);
647 render_layout(connection);
651 printf("direction unhandled\n");
656 * Tries to move the window inside its current container.
658 * Returns true if the window could be moved, false otherwise.
661 static bool move_current_window_in_container(xcb_connection_t *connection, Client *client,
662 direction_t direction) {
663 Client *other = (direction == D_UP ? CIRCLEQ_PREV(client, clients) :
664 CIRCLEQ_NEXT(client, clients));
666 if (other == CIRCLEQ_END(&(client->container->clients)))
669 printf("i can do that\n");
670 /* We can move the client inside its current container */
671 CIRCLEQ_REMOVE(&(client->container->clients), client, clients);
672 if (direction == D_UP)
673 CIRCLEQ_INSERT_BEFORE(&(client->container->clients), other, client, clients);
674 else CIRCLEQ_INSERT_AFTER(&(client->container->clients), other, client, clients);
675 render_layout(connection);
680 * Moves the current window to the given direction, creating a column/row if
684 static void move_current_window(xcb_connection_t *connection, direction_t direction) {
685 printf("moving window to direction %d\n", direction);
686 /* Get current window */
687 Container *container = CUR_CELL,
690 /* There has to be a container, see focus_window() */
691 assert(container != NULL);
693 /* If there is no window, we’re done */
694 if (container->currently_focused == NULL)
697 /* As soon as the client is moved away, the next client in the old
698 * container needs to get focus, if any. Therefore, we save it here. */
699 Client *current_client = container->currently_focused;
700 Client *to_focus = CIRCLEQ_NEXT(current_client, clients);
701 if (to_focus == CIRCLEQ_END(&(container->clients)))
706 if (current_col == 0)
709 new = table[--current_col][current_row];
712 if (current_col == (table_dims.x-1))
715 new = table[++current_col][current_row];
718 if (move_current_window_in_container(connection, current_client, D_UP) ||
722 new = table[current_col][--current_row];
725 if (move_current_window_in_container(connection, current_client, D_DOWN))
728 if (current_row == (table_dims.y-1))
731 new = table[current_col][++current_row];
735 /* Remove it from the old container and put it into the new one */
736 CIRCLEQ_REMOVE(&(container->clients), current_client, clients);
737 CIRCLEQ_INSERT_TAIL(&(new->clients), current_client, clients);
739 /* Update data structures */
740 current_client->container = new;
741 container->currently_focused = to_focus;
742 new->currently_focused = current_client;
744 /* TODO: delete all empty columns/rows */
746 render_layout(connection);
750 * "Snaps" the current container (not possible for windows, because it works at table base)
751 * to the given direction, that is, adjusts cellspan/rowspan
754 static void snap_current_container(xcb_connection_t *connection, direction_t direction) {
755 printf("snapping container to direction %d\n", direction);
757 Container *container = CUR_CELL;
760 assert(container != NULL);
764 /* Snap to the left is actually a move to the left and then a snap right */
765 move_current_window(connection, D_LEFT);
766 snap_current_container(connection, D_RIGHT);
769 /* Check if the cell is used */
770 if (!cell_exists(container->col + 1, container->row) ||
771 table[container->col+1][container->row]->currently_focused != NULL) {
772 printf("cannot snap to right - the cell is already used\n");
776 /* Check if there are other cells with rowspan, which are in our way.
777 * If so, reduce their rowspan. */
778 for (i = container->row-1; i >= 0; i--) {
779 printf("we got cell %d, %d with rowspan %d\n",
780 container->col+1, i, table[container->col+1][i]->rowspan);
781 while ((table[container->col+1][i]->rowspan-1) >= (container->row - i))
782 table[container->col+1][i]->rowspan--;
783 printf("new rowspan = %d\n", table[container->col+1][i]->rowspan);
786 container->colspan++;
789 move_current_window(connection, D_UP);
790 snap_current_container(connection, D_DOWN);
793 printf("snapping down\n");
794 if (!cell_exists(container->col, container->row+1) ||
795 table[container->col][container->row+1]->currently_focused != NULL) {
796 printf("cannot snap down - the cell is already used\n");
800 for (i = container->col-1; i >= 0; i--) {
801 printf("we got cell %d, %d with colspan %d\n",
802 i, container->row+1, table[i][container->row+1]->colspan);
803 while ((table[i][container->row+1]->colspan-1) >= (container->col - i))
804 table[i][container->row+1]->colspan--;
805 printf("new colspan = %d\n", table[i][container->row+1]->colspan);
809 container->rowspan++;
813 render_layout(connection);
816 int format_event(xcb_generic_event_t *e)
821 sendEvent = (e->response_type & 0x80) ? 1 : 0;
822 e->response_type &= ~0x80;
823 seqnum = *((uint16_t *) e + 1);
825 switch(e->response_type)
828 printf("Error %s on seqnum %d (%s).\n",
829 labelError[*((uint8_t *) e + 1)],
831 labelRequest[*((uint8_t *) e + 10)]);
834 printf("Event %s following seqnum %d%s.\n",
835 labelEvent[e->response_type],
837 labelSendEvent[sendEvent]);
839 case XCB_KEYMAP_NOTIFY:
840 printf("Event %s%s.\n",
841 labelEvent[e->response_type],
842 labelSendEvent[sendEvent]);
851 static int handleEvent(void *ignored, xcb_connection_t *c, xcb_generic_event_t *e)
853 return format_event(e);
857 * Starts the given application with the given args.
860 static void start_application(char *path, char *args) {
862 if ((pid = vfork()) == 0) {
863 /* This is the child */
865 /* TODO: For now, we ignore args. Later on, they should be parsed
866 correctly (like in the shell?) */
869 execve(path, argv, environment);
875 * Due to bindings like Mode_switch + <a>, we need to bind some keys in XCB_GRAB_MODE_SYNC.
876 * Therefore, we just replay all key presses.
879 static int handle_key_release(void *ignored, xcb_connection_t *conn, xcb_key_release_event_t *event) {
880 printf("got key release, just passing\n");
881 xcb_allow_events(conn, ReplayKeyboard, event->time);
887 * Parses a command, see file CMDMODE for more information
890 static void parse_command(xcb_connection_t *conn, const char *command) {
891 printf("--- parsing command \"%s\" ---\n", command);
892 /* Hmm, just to be sure */
893 if (command[0] == '\0')
896 /* Is it an <exec>? */
897 if (strncmp(command, "exec ", strlen("exec ")) == 0) {
898 printf("starting \"%s\"\n", command + strlen("exec "));
899 start_application(command+strlen("exec "), NULL);
903 /* Is it a <with>? */
904 if (command[0] == 'w') {
905 /* TODO: implement */
906 printf("not yet implemented.\n");
910 /* It's a normal <cmd> */
913 enum { ACTION_FOCUS, ACTION_MOVE, ACTION_SNAP } action = ACTION_FOCUS;
914 direction_t direction;
915 times = strtol(command, &rest, 10);
917 printf("Invalid command: Consists only of a movement\n");
920 if (*rest == 'm' || *rest == 's') {
921 action = (*rest == 'm' ? ACTION_MOVE : ACTION_SNAP);
925 /* Now perform action to <where> */
926 while (*rest != '\0') {
930 else if (*rest == 'j')
932 else if (*rest == 'k')
934 else if (*rest == 'l')
937 printf("unknown direction: %c\n", *rest);
941 if (action == ACTION_FOCUS)
942 focus_window(conn, direction);
943 else if (action == ACTION_MOVE)
944 move_current_window(conn, direction);
945 else if (action == ACTION_SNAP)
946 snap_current_container(conn, direction);
952 printf("--- done ---\n");
956 * There was a key press. We lookup the key symbol and see if there are any bindings
957 * on that. This allows to do things like binding special characters (think of ä) to
958 * functions to get one more modifier while not losing AltGr :-)
959 * TODO: this description needs to be more understandable
962 static int handle_key_press(void *ignored, xcb_connection_t *conn, xcb_key_press_event_t *event) {
963 printf("Keypress %d\n", event->detail);
965 /* We need to get the keysym group (There are group 1 to group 4, each holding
966 two keysyms (without shift and with shift) using Xkb because X fails to
967 provide them reliably (it works in Xephyr, it does not in real X) */
969 if (XkbGetState(xkbdpy, XkbUseCoreKbd, &state) == Success && (state.group+1) == 2)
972 printf("state %d\n", event->state);
974 /* Find the binding */
975 Binding *bind, *best_match = TAILQ_END(&bindings);
976 TAILQ_FOREACH(bind, &bindings, bindings) {
977 if (bind->keycode == event->detail &&
978 (bind->mods & event->state) == bind->mods) {
979 if (best_match == TAILQ_END(&bindings) ||
980 bind->mods > best_match->mods)
985 /* No match? Then it was an actively grabbed key, that is with Mode_switch, and
986 the user did not press Mode_switch, so just pass it… */
987 if (best_match == TAILQ_END(&bindings)) {
988 xcb_allow_events(conn, ReplayKeyboard, event->time);
993 if (event->state & 0x2) {
994 printf("that's mode_switch\n");
995 parse_command(conn, best_match->command);
996 printf("ok, hiding this event.\n");
997 xcb_allow_events(conn, SyncKeyboard, event->time);
1002 parse_command(conn, best_match->command);
1007 * When the user moves the mouse pointer onto a window, this callback gets called.
1010 static int handle_enter_notify(void *ignored, xcb_connection_t *conn, xcb_enter_notify_event_t *event) {
1011 printf("enter_notify\n");
1013 /* This was either a focus for a client’s parent (= titlebar)… */
1014 Client *client = table_get(byParent, event->event),
1016 /* …or the client itself */
1018 client = table_get(byChild, event->event);
1020 /* If not, then this event is not interesting. This should not happen */
1021 if (client == NULL) {
1022 printf("DEBUG: Uninteresting enter_notify-event?\n");
1026 /* Update container */
1027 old_client = client->container->currently_focused;
1028 client->container->currently_focused = client;
1030 current_col = client->container->col;
1031 current_row = client->container->row;
1033 /* Set focus to the entered window, and flush xcb buffer immediately */
1034 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_NONE, client->child, XCB_CURRENT_TIME);
1035 /* Update last/current client’s titlebar */
1036 if (old_client != NULL)
1037 decorate_window(conn, old_client);
1038 decorate_window(conn, client);
1044 int handle_map_notify_event(void *prophs, xcb_connection_t *c, xcb_map_notify_event_t *e)
1046 window_attributes_t wa = { TAG_VALUE };
1047 wa.u.override_redirect = e->override_redirect;
1048 printf("MapNotify for 0x%08x.\n", e->window);
1049 manage_window(prophs, c, e->window, wa);
1054 * Our window decorations were unmapped. That means, the window will be killed now,
1055 * so we better clean up before.
1058 int handle_unmap_notify_event(void *data, xcb_connection_t *c, xcb_unmap_notify_event_t *e) {
1059 Client *client = table_remove(byChild, e->event);
1061 printf("UnmapNotify for 0x%08x (received from 0x%08x): ", e->window, e->event);
1064 printf("not a managed window. Ignoring.\n");
1070 for (cols = 0; cols < table_dims.x; cols++)
1071 for (rows = 0; rows < table_dims.y; rows++)
1072 CIRCLEQ_FOREACH(con_client, &(table[cols][rows]->clients), clients)
1073 if (con_client == client) {
1074 printf("removing from container\n");
1075 if (client->container->currently_focused == client)
1076 client->container->currently_focused = NULL;
1077 CIRCLEQ_REMOVE(&(table[cols][rows]->clients), con_client, clients);
1083 root = xcb_setup_roots_iterator(xcb_get_setup(c)).data->root;
1084 printf("child of 0x%08x.\n", client->frame);
1085 xcb_reparent_window(c, client->child, root, 0, 0);
1086 xcb_destroy_window(c, client->frame);
1088 table_remove(byParent, client->frame);
1098 static int handleExposeEvent(void *data, xcb_connection_t *c, xcb_expose_event_t *e) {
1099 printf("exposeevent\n");
1100 Client *client = table_get(byParent, e->window);
1101 if(!client || e->count != 0)
1103 decorate_window(c, client);
1106 void manage_existing_windows(xcb_connection_t *c, xcb_property_handlers_t *prophs, xcb_window_t root) {
1107 xcb_query_tree_cookie_t wintree;
1108 xcb_query_tree_reply_t *rep;
1110 xcb_window_t *children;
1111 xcb_get_window_attributes_cookie_t *cookies;
1113 wintree = xcb_query_tree(c, root);
1114 rep = xcb_query_tree_reply(c, wintree, 0);
1117 len = xcb_query_tree_children_length(rep);
1118 cookies = malloc(len * sizeof(*cookies));
1124 children = xcb_query_tree_children(rep);
1125 for(i = 0; i < len; ++i)
1126 cookies[i] = xcb_get_window_attributes(c, children[i]);
1127 for(i = 0; i < len; ++i)
1129 window_attributes_t wa = { TAG_COOKIE, { cookies[i] } };
1130 manage_window(prophs, c, children[i], wa);
1135 int main(int argc, char *argv[], char *env[]) {
1138 for (i = 0; (env[i] != NULL); i++)
1139 if (strncmp(env[i], "LC_", strlen("LC_")) == 0 ||
1140 strncmp(env[i], "LANG=", strlen("LANG=")) == 0 ||
1141 strncmp(env[i], "DISPLAY=", strlen("DISPLAY=")) == 0) {
1142 printf("Passing environment \"%s\"\n", env[i]);
1143 environment = realloc(environment, sizeof(char*) * ++e);
1144 environment[e-1] = env[i];
1147 /* environment has to be NULL-terminated */
1148 environment = realloc(environment, sizeof(char*) * ++e);
1149 environment[e-1] = NULL;
1153 xcb_connection_t *c;
1154 xcb_event_handlers_t evenths;
1155 xcb_property_handlers_t prophs;
1160 memset(&evenths, 0, sizeof(xcb_event_handlers_t));
1161 memset(&prophs, 0, sizeof(xcb_property_handlers_t));
1163 byChild = alloc_table();
1164 byParent = alloc_table();
1166 TAILQ_INIT(&bindings);
1168 c = xcb_connect(NULL, &screens);
1170 printf("x screen is %d\n", screens);
1172 /* TODO: this has to be more beautiful somewhen */
1173 int major, minor, error;
1175 major = XkbMajorVersion;
1176 minor = XkbMinorVersion;
1178 int evBase, errBase;
1180 if ((xkbdpy = XkbOpenDisplay(getenv("DISPLAY"), &evBase, &errBase, &major, &minor, &error)) == NULL) {
1181 fprintf(stderr, "XkbOpenDisplay() failed\n");
1186 if (!XkbQueryExtension(xkbdpy,&i1,&evBase,&errBase,&major,&minor)) {
1187 fprintf(stderr, "XKB not supported by X-server\n");
1190 /* end of ugliness */
1192 xcb_event_handlers_init(c, &evenths);
1193 for(i = 2; i < 128; ++i)
1194 xcb_event_set_handler(&evenths, i, handleEvent, 0);
1196 for(i = 0; i < 256; ++i)
1197 xcb_event_set_error_handler(&evenths, i, (xcb_generic_error_handler_t) handleEvent, 0);
1199 /* Expose = an Application should redraw itself. That is, we have to redraw our
1200 * contents (= top/bottom bar, titlebars for each window) */
1201 xcb_event_set_expose_handler(&evenths, handleExposeEvent, 0);
1203 /* Key presses/releases are pretty obvious, I think */
1204 xcb_event_set_key_press_handler(&evenths, handle_key_press, 0);
1205 xcb_event_set_key_release_handler(&evenths, handle_key_release, 0);
1207 /* Enter window = user moved his mouse over the window */
1208 xcb_event_set_enter_notify_handler(&evenths, handle_enter_notify, 0);
1210 xcb_event_set_unmap_notify_handler(&evenths, handle_unmap_notify_event, 0);
1212 xcb_property_handlers_init(&prophs, &evenths);
1213 xcb_event_set_map_notify_handler(&evenths, handle_map_notify_event, &prophs);
1215 root = xcb_aux_get_screen(c, screens)->root;
1218 uint32_t mask = XCB_CW_EVENT_MASK;
1219 uint32_t values[] = { XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY | XCB_EVENT_MASK_PROPERTY_CHANGE };
1220 xcb_change_window_attributes(c, root, mask, values);
1222 #define BIND(key, modifier, cmd) { \
1223 Binding *new = malloc(sizeof(Binding)); \
1224 new->keycode = key; \
1225 new->mods = modifier; \
1226 new->command = cmd; \
1227 TAILQ_INSERT_TAIL(&bindings, new, bindings); \
1231 BIND(38, BIND_MODE_SWITCH, "foo");
1233 BIND(30, 0, "exec /usr/pkg/bin/urxvt");
1235 BIND(44, BIND_MOD_1, "h");
1236 BIND(45, BIND_MOD_1, "j");
1237 BIND(46, BIND_MOD_1, "k");
1238 BIND(47, BIND_MOD_1, "l");
1240 BIND(44, BIND_MOD_1 | BIND_CONTROL, "sh");
1241 BIND(45, BIND_MOD_1 | BIND_CONTROL, "sj");
1242 BIND(46, BIND_MOD_1 | BIND_CONTROL, "sk");
1243 BIND(47, BIND_MOD_1 | BIND_CONTROL, "sl");
1245 BIND(44, BIND_MOD_1 | BIND_SHIFT, "mh");
1246 BIND(45, BIND_MOD_1 | BIND_SHIFT, "mj");
1247 BIND(46, BIND_MOD_1 | BIND_SHIFT, "mk");
1248 BIND(47, BIND_MOD_1 | BIND_SHIFT, "ml");
1251 TAILQ_FOREACH(bind, &bindings, bindings) {
1252 printf("Grabbing %d\n", bind->keycode);
1253 if (bind->mods & BIND_MODE_SWITCH)
1254 xcb_grab_key(c, 0, root, 0, bind->keycode, XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_SYNC);
1255 else xcb_grab_key(c, 0, root, bind->mods, bind->keycode, XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_ASYNC);
1258 start_application(TERMINAL, NULL);
1262 manage_existing_windows(c, &prophs, root);
1264 xcb_event_wait_for_event_loop(&evenths);