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 xcb_screen_t *root_screen = xcb_setup_roots_iterator(xcb_get_setup(conn)).data;
352 i3Font *font = load_font(conn, pattern);
353 uint32_t background_color,
357 if (client->container->currently_focused == client) {
358 background_color = get_colorpixel(conn, client->frame, "#285577");
359 text_color = get_colorpixel(conn, client->frame, "#ffffff");
360 border_color = get_colorpixel(conn, client->frame, "#4c7899");
362 background_color = get_colorpixel(conn, client->frame, "#222222");
363 text_color = get_colorpixel(conn, client->frame, "#888888");
364 border_color = get_colorpixel(conn, client->frame, "#333333");
367 /* Our plan is the following:
368 - Draw a rect around the whole client in background_color
369 - Draw two lines in a lighter color
370 - Draw the window’s title
372 Note that xcb_image_text apparently adds 1xp border around the font? Can anyone confirm this?
375 /* Draw a green rectangle around the window */
376 mask = XCB_GC_FOREGROUND;
377 values[0] = background_color;
378 xcb_change_gc(conn, client->titlegc, mask, values);
380 xcb_rectangle_t rect = {0, 0, client->width, client->height};
381 xcb_poly_fill_rectangle(conn, client->frame, client->titlegc, 1, &rect);
384 /* TODO: this needs to be more beautiful somewhen. maybe stdarg + change_gc(gc, ...) ? */
385 #define DRAW_LINE(colorpixel, x, y, to_x, to_y) { \
386 uint32_t draw_values[1]; \
387 draw_values[0] = colorpixel; \
388 xcb_change_gc(conn, client->titlegc, XCB_GC_FOREGROUND, draw_values); \
389 xcb_point_t points[] = {{x, y}, {to_x, to_y}}; \
390 xcb_poly_line(conn, XCB_COORD_MODE_ORIGIN, client->frame, client->titlegc, 2, points); \
393 DRAW_LINE(border_color, 2, 0, client->width, 0);
394 DRAW_LINE(border_color, 2, font->height + 3, 2 + client->width, font->height + 3);
397 mask = XCB_GC_FOREGROUND | XCB_GC_BACKGROUND | XCB_GC_FONT;
399 values[0] = text_color;
400 values[1] = background_color;
401 values[2] = font->id;
403 xcb_change_gc(conn, client->titlegc, mask, values);
407 asprintf(&label, "gots win %08x", client->frame);
408 xcb_void_cookie_t text_cookie = xcb_image_text_8_checked(conn, strlen(label), client->frame,
409 client->titlegc, 3 /* X */, font->height /* Y = baseline of font */, label);
413 void render_container(xcb_connection_t *connection, Container *container) {
416 uint32_t mask = XCB_CONFIG_WINDOW_X |
417 XCB_CONFIG_WINDOW_Y |
418 XCB_CONFIG_WINDOW_WIDTH |
419 XCB_CONFIG_WINDOW_HEIGHT;
420 i3Font *font = load_font(connection, pattern);
422 if (container->mode == MODE_DEFAULT) {
424 CIRCLEQ_FOREACH(client, &(container->clients), clients)
426 printf("got %d clients in this default container.\n", num_clients);
428 int current_client = 0;
429 CIRCLEQ_FOREACH(client, &(container->clients), clients) {
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 */
437 /* TODO: vertical default layout */
438 values[2] = container->width; /* width */
439 values[3] = container->height / num_clients; /* height */
440 printf("frame will be at %dx%d with size %dx%d\n",
441 values[0], values[1], values[2], values[3]);
443 client->width = values[2];
444 client->height = values[3];
446 /* TODO: update only if necessary */
447 xcb_configure_window(connection, client->frame, mask, values);
449 /* The coordinates of the child are relative to its frame, we
450 * add a border of 2 pixel to each value */
452 values[1] = font->height + 2 + 2;
453 values[2] -= values[0] + 2;
454 values[3] -= values[1] + 2;
455 printf("child itself will be at %dx%d with size %dx%d\n",
456 values[0], values[1], values[2], values[3]);
458 xcb_configure_window(connection, client->child, mask, values);
460 decorate_window(connection, client);
464 /* TODO: Implement stacking */
468 void render_layout(xcb_connection_t *conn) {
470 xcb_screen_t *root_screen = xcb_setup_roots_iterator(xcb_get_setup(conn)).data;
471 int width = root_screen->width_in_pixels;
472 int height = root_screen->height_in_pixels;
474 int num_cols = table_dims.x, num_rows = table_dims.y;
476 printf("got %d rows and %d cols\n", num_rows, num_cols);
477 printf("each of them therefore is %d px width and %d px height\n",
478 width / num_cols, height / num_rows);
480 /* Go through the whole table and render what’s necessary */
481 for (cols = 0; cols < table_dims.x; cols++)
482 for (rows = 0; rows < table_dims.y; rows++)
483 if (table[cols][rows] != NULL) {
484 Container *con = table[cols][rows];
485 printf("container has %d colspan, %d rowspan\n",
486 con->colspan, con->rowspan);
487 /* Update position of the container */
490 con->width = (width / num_cols) * con->colspan;
491 con->height = (height / num_rows) * con->rowspan;
494 render_container(conn, table[cols][rows]);
501 * Let’s own this window…
504 void reparent_window(xcb_connection_t *conn, xcb_window_t child,
505 xcb_visualid_t visual, xcb_window_t root, uint8_t depth,
506 int16_t x, int16_t y, uint16_t width, uint16_t height) {
508 Client *new = table_get(byChild, child);
510 printf("oh, it's new\n");
511 new = calloc(sizeof(Client), 1);
515 xcb_screen_t *root_screen = xcb_setup_roots_iterator(xcb_get_setup(conn)).data;
517 /* Insert into the currently active container */
518 CIRCLEQ_INSERT_TAIL(&(CUR_CELL->clients), new, clients);
520 printf("currently_focused = %p\n", new);
521 CUR_CELL->currently_focused = new;
522 new->container = CUR_CELL;
524 new->frame = xcb_generate_id(conn);
527 new->height = height;
529 /* TODO: what do these mean? */
530 mask |= XCB_CW_BACK_PIXEL;
531 values[0] = root_screen->white_pixel;
533 mask |= XCB_CW_OVERRIDE_REDIRECT;
536 mask |= XCB_CW_EVENT_MASK;
537 values[2] = XCB_EVENT_MASK_BUTTON_PRESS | XCB_EVENT_MASK_BUTTON_RELEASE
538 | XCB_EVENT_MASK_EXPOSURE | XCB_EVENT_MASK_ENTER_WINDOW;
540 printf("Reparenting 0x%08x under 0x%08x.\n", child, new->frame);
542 /* Yo dawg, I heard you like windows, so I create a window around your window… */
543 xcb_create_window(conn,
549 width + LEFT + RIGHT,
550 height + TOP + BOTTOM,
551 /* border_width */ 0,
552 XCB_WINDOW_CLASS_INPUT_OUTPUT,
556 xcb_change_save_set(conn, XCB_SET_MODE_INSERT, child);
558 /* Map the window on the screen (= make it visible) */
559 xcb_map_window(conn, new->frame);
561 /* Generate a graphics context for the titlebar */
562 new->titlegc = xcb_generate_id(conn);
563 xcb_create_gc(conn, new->titlegc, new->frame, 0, 0);
565 /* Draw decorations */
566 decorate_window(conn, new);
568 /* Put our data structure (Client) into the table */
569 table_put(byParent, new->frame, new);
570 table_put(byChild, child, new);
572 /* Moves the original window into the new frame we've created for it */
573 i3Font *font = load_font(conn, pattern);
574 xcb_reparent_window(conn, child, new->frame, 0, font->height);
576 /* We are interested in property changes */
577 mask = XCB_CW_EVENT_MASK;
578 values[0] = XCB_EVENT_MASK_PROPERTY_CHANGE |
579 XCB_EVENT_MASK_STRUCTURE_NOTIFY |
580 XCB_EVENT_MASK_ENTER_WINDOW;
581 xcb_change_window_attributes(conn, child, mask, values);
583 /* TODO: At the moment, new windows just get focus */
584 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_NONE, new->frame, XCB_CURRENT_TIME);
591 static bool focus_window_in_container(xcb_connection_t *connection, Container *container,
592 direction_t direction) {
593 /* If this container is empty, we’re done */
594 if (container->currently_focused == NULL)
598 if (direction == D_UP)
599 candidad = CIRCLEQ_PREV(container->currently_focused, clients);
600 else if (direction == D_DOWN)
601 candidad = CIRCLEQ_NEXT(container->currently_focused, clients);
603 /* If we don’t have anything to select, we’re done */
604 if (candidad == CIRCLEQ_END(&(container->clients)))
607 /* Set focus if we could successfully move */
608 container->currently_focused = candidad;
609 xcb_set_input_focus(connection, XCB_INPUT_FOCUS_NONE, candidad->child, XCB_CURRENT_TIME);
610 render_layout(connection);
611 xcb_flush(connection);
616 static void focus_window(xcb_connection_t *connection, direction_t direction) {
617 printf("focusing direction %d\n", direction);
618 /* TODO: for horizontal default layout, this has to be expanded to LEFT/RIGHT */
619 if (direction == D_UP || direction == D_DOWN) {
620 /* Let’s see if we can perform up/down focus in the current container */
621 Container *container = CUR_CELL;
623 /* There always is a container. If not, current_col or current_row is wrong */
624 assert(container != NULL);
626 if (focus_window_in_container(connection, container, direction))
628 } else if (direction == D_LEFT || direction == D_RIGHT) {
629 if (direction == D_RIGHT && cell_exists(current_col+1, current_row))
631 else if (direction == D_LEFT && cell_exists(current_col-1, current_row))
634 printf("nah, not possible\n");
637 if (CUR_CELL->currently_focused != NULL) {
638 xcb_set_input_focus(connection, XCB_INPUT_FOCUS_NONE,
639 CUR_CELL->currently_focused->child, XCB_CURRENT_TIME);
640 render_layout(connection);
641 xcb_flush(connection);
645 printf("direction unhandled\n");
650 * Tries to move the window inside its current container.
652 * Returns true if the window could be moved, false otherwise.
655 static bool move_current_window_in_container(xcb_connection_t *connection, Client *client,
656 direction_t direction) {
657 Client *other = (direction == D_UP ? CIRCLEQ_PREV(client, clients) :
658 CIRCLEQ_NEXT(client, clients));
660 if (other == CIRCLEQ_END(&(client->container->clients)))
663 printf("i can do that\n");
664 /* We can move the client inside its current container */
665 CIRCLEQ_REMOVE(&(client->container->clients), client, clients);
666 if (direction == D_UP)
667 CIRCLEQ_INSERT_BEFORE(&(client->container->clients), other, client, clients);
668 else CIRCLEQ_INSERT_AFTER(&(client->container->clients), other, client, clients);
669 render_layout(connection);
674 * Moves the current window to the given direction, creating a column/row if
678 static void move_current_window(xcb_connection_t *connection, direction_t direction) {
679 printf("moving window to direction %d\n", direction);
680 /* Get current window */
681 Container *container = CUR_CELL,
684 /* There has to be a container, see focus_window() */
685 assert(container != NULL);
687 /* If there is no window, we’re done */
688 if (container->currently_focused == NULL)
691 /* As soon as the client is moved away, the next client in the old
692 * container needs to get focus, if any. Therefore, we save it here. */
693 Client *current_client = container->currently_focused;
694 Client *to_focus = CIRCLEQ_NEXT(current_client, clients);
695 if (to_focus == CIRCLEQ_END(&(container->clients)))
700 if (current_col == 0)
703 new = table[--current_col][current_row];
706 if (current_col == (table_dims.x-1))
709 new = table[++current_col][current_row];
712 if (move_current_window_in_container(connection, current_client, D_UP) ||
716 new = table[current_col][--current_row];
719 if (move_current_window_in_container(connection, current_client, D_DOWN))
722 if (current_row == (table_dims.y-1))
725 new = table[current_col][++current_row];
729 /* Remove it from the old container and put it into the new one */
730 CIRCLEQ_REMOVE(&(container->clients), current_client, clients);
731 CIRCLEQ_INSERT_TAIL(&(new->clients), current_client, clients);
733 /* Update data structures */
734 current_client->container = new;
735 container->currently_focused = to_focus;
736 new->currently_focused = current_client;
738 /* TODO: delete all empty columns/rows */
740 render_layout(connection);
743 int format_event(xcb_generic_event_t *e)
748 sendEvent = (e->response_type & 0x80) ? 1 : 0;
749 e->response_type &= ~0x80;
750 seqnum = *((uint16_t *) e + 1);
752 switch(e->response_type)
755 printf("Error %s on seqnum %d (%s).\n",
756 labelError[*((uint8_t *) e + 1)],
758 labelRequest[*((uint8_t *) e + 10)]);
761 printf("Event %s following seqnum %d%s.\n",
762 labelEvent[e->response_type],
764 labelSendEvent[sendEvent]);
766 case XCB_KEYMAP_NOTIFY:
767 printf("Event %s%s.\n",
768 labelEvent[e->response_type],
769 labelSendEvent[sendEvent]);
778 static int handleEvent(void *ignored, xcb_connection_t *c, xcb_generic_event_t *e)
780 return format_event(e);
784 * Starts the given application with the given args.
787 static void start_application(char *path, char *args) {
789 if ((pid = vfork()) == 0) {
790 /* This is the child */
792 /* TODO: For now, we ignore args. Later on, they should be parsed
793 correctly (like in the shell?) */
796 execve(path, argv, environment);
802 * Due to bindings like Mode_switch + <a>, we need to bind some keys in XCB_GRAB_MODE_SYNC.
803 * Therefore, we just replay all key presses.
806 static int handle_key_release(void *ignored, xcb_connection_t *conn, xcb_key_release_event_t *event) {
807 printf("got key release, just passing\n");
808 xcb_allow_events(conn, ReplayKeyboard, event->time);
814 * Parses a command, see file CMDMODE for more information
817 static void parse_command(xcb_connection_t *conn, const char *command) {
818 printf("--- parsing command \"%s\" ---\n", command);
819 /* Hmm, just to be sure */
820 if (command[0] == '\0')
823 /* Is it an <exec>? */
824 if (strncmp(command, "exec ", strlen("exec ")) == 0) {
825 printf("starting \"%s\"\n", command + strlen("exec "));
826 start_application(command+strlen("exec "), NULL);
830 /* Is it a <with>? */
831 if (command[0] == 'w') {
832 /* TODO: implement */
833 printf("not yet implemented.\n");
837 /* It's a normal <cmd> */
840 enum { ACTION_FOCUS, ACTION_MOVE, ACTION_SNAP } action = ACTION_FOCUS;
841 direction_t direction;
842 times = strtol(command, &rest, 10);
844 printf("Invalid command: Consists only of a movement\n");
847 if (*rest == 'm' || *rest == 's') {
848 action = (*rest == 'm' ? ACTION_MOVE : ACTION_SNAP);
852 /* Now perform action to <where> */
853 while (*rest != '\0') {
857 else if (*rest == 'j')
859 else if (*rest == 'k')
861 else if (*rest == 'l')
864 printf("unknown direction: %c\n", *rest);
868 if (action == ACTION_FOCUS)
869 focus_window(conn, direction);
870 else if (action == ACTION_MOVE)
871 move_current_window(conn, direction);
872 else if (action == ACTION_SNAP)
873 /* TODO: implement */
874 printf("snap not yet implemented\n");
880 printf("--- done ---\n");
884 * There was a key press. We lookup the key symbol and see if there are any bindings
885 * on that. This allows to do things like binding special characters (think of ä) to
886 * functions to get one more modifier while not losing AltGr :-)
887 * TODO: this description needs to be more understandable
890 static int handle_key_press(void *ignored, xcb_connection_t *conn, xcb_key_press_event_t *event) {
891 printf("Keypress %d\n", event->detail);
893 /* We need to get the keysym group (There are group 1 to group 4, each holding
894 two keysyms (without shift and with shift) using Xkb because X fails to
895 provide them reliably (it works in Xephyr, it does not in real X) */
897 if (XkbGetState(xkbdpy, XkbUseCoreKbd, &state) == Success && (state.group+1) == 2)
900 printf("state %d\n", event->state);
902 /* Find the binding */
903 Binding *bind, *best_match = TAILQ_END(&bindings);
904 TAILQ_FOREACH(bind, &bindings, bindings) {
905 if (bind->keycode == event->detail &&
906 (bind->mods & event->state) == bind->mods) {
907 if (best_match == TAILQ_END(&bindings) ||
908 bind->mods > best_match->mods)
913 if (best_match == TAILQ_END(&bindings)) {
914 printf("This key was not bound by us?! (most likely a bug)\n");
915 return 1; /* TODO: return 0? what do the codes mean? */
918 if (event->state & 0x2) {
919 printf("that's mode_switch\n");
920 parse_command(conn, best_match->command);
921 printf("ok, hiding this event.\n");
922 xcb_allow_events(conn, SyncKeyboard, event->time);
927 /* If this was an actively grabbed key, and we did not handle it, we need to pass it */
928 if (best_match->mods & BIND_MODE_SWITCH) {
929 printf("passing...\n");
930 xcb_allow_events(conn, ReplayKeyboard, event->time);
935 parse_command(conn, best_match->command);
940 * When the user moves the mouse pointer onto a window, this callback gets called.
943 static int handle_enter_notify(void *ignored, xcb_connection_t *conn, xcb_enter_notify_event_t *event) {
944 /* This was either a focus for a client’s parent (= titlebar)… */
945 Client *client = table_get(byParent, event->event),
947 /* …or the client itself */
949 client = table_get(byChild, event->event);
951 /* If not, then this event is not interesting. This should not happen */
952 if (client == NULL) {
953 printf("DEBUG: Uninteresting enter_notify-event?\n");
957 /* Update container */
958 old_client = client->container->currently_focused;
959 client->container->currently_focused = client;
961 current_col = client->container->col;
962 current_row = client->container->row;
964 /* Set focus to the entered window, and flush xcb buffer immediately */
965 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_POINTER_ROOT, client->child, XCB_CURRENT_TIME);
966 /* Update last/current client’s titlebar */
967 if (old_client != NULL)
968 decorate_window(conn, old_client);
969 decorate_window(conn, client);
976 int handle_map_notify_event(void *prophs, xcb_connection_t *c, xcb_map_notify_event_t *e)
978 window_attributes_t wa = { TAG_VALUE };
979 wa.u.override_redirect = e->override_redirect;
980 printf("MapNotify for 0x%08x.\n", e->window);
981 manage_window(prophs, c, e->window, wa);
986 * Our window decorations were unmapped. That means, the window will be killed now,
987 * so we better clean up before.
990 int handle_unmap_notify_event(void *data, xcb_connection_t *c, xcb_unmap_notify_event_t *e) {
991 Client *client = table_remove(byChild, e->event);
993 printf("UnmapNotify for 0x%08x (received from 0x%08x): ", e->window, e->event);
996 printf("not a managed window. Ignoring.\n");
1002 for (cols = 0; cols < table_dims.x; cols++)
1003 for (rows = 0; rows < table_dims.y; rows++)
1004 CIRCLEQ_FOREACH(con_client, &(table[cols][rows]->clients), clients)
1005 if (con_client == client) {
1006 printf("removing from container\n");
1007 if (client->container->currently_focused == client)
1008 client->container->currently_focused = NULL;
1009 CIRCLEQ_REMOVE(&(table[cols][rows]->clients), con_client, clients);
1015 root = xcb_setup_roots_iterator(xcb_get_setup(c)).data->root;
1016 printf("child of 0x%08x.\n", client->frame);
1017 xcb_reparent_window(c, client->child, root, 0, 0);
1018 xcb_destroy_window(c, client->frame);
1020 table_remove(byParent, client->frame);
1030 static int handleExposeEvent(void *data, xcb_connection_t *c, xcb_expose_event_t *e) {
1031 printf("exposeevent\n");
1032 Client *client = table_get(byParent, e->window);
1033 if(!client || e->count != 0)
1035 decorate_window(c, client);
1038 void manage_existing_windows(xcb_connection_t *c, xcb_property_handlers_t *prophs, xcb_window_t root) {
1039 xcb_query_tree_cookie_t wintree;
1040 xcb_query_tree_reply_t *rep;
1042 xcb_window_t *children;
1043 xcb_get_window_attributes_cookie_t *cookies;
1045 wintree = xcb_query_tree(c, root);
1046 rep = xcb_query_tree_reply(c, wintree, 0);
1049 len = xcb_query_tree_children_length(rep);
1050 cookies = malloc(len * sizeof(*cookies));
1056 children = xcb_query_tree_children(rep);
1057 for(i = 0; i < len; ++i)
1058 cookies[i] = xcb_get_window_attributes(c, children[i]);
1059 for(i = 0; i < len; ++i)
1061 window_attributes_t wa = { TAG_COOKIE, { cookies[i] } };
1062 manage_window(prophs, c, children[i], wa);
1067 int main(int argc, char *argv[], char *env[]) {
1070 for (i = 0; (env[i] != NULL); i++)
1071 if (strncmp(env[i], "LC_", strlen("LC_")) == 0 ||
1072 strncmp(env[i], "LANG=", strlen("LANG=")) == 0 ||
1073 strncmp(env[i], "DISPLAY=", strlen("DISPLAY=")) == 0) {
1074 printf("Passing environment \"%s\"\n", env[i]);
1075 environment = realloc(environment, sizeof(char*) * ++e);
1076 environment[e-1] = env[i];
1079 /* environment has to be NULL-terminated */
1080 environment = realloc(environment, sizeof(char*) * ++e);
1081 environment[e-1] = NULL;
1085 xcb_connection_t *c;
1086 xcb_event_handlers_t evenths;
1087 xcb_property_handlers_t prophs;
1092 memset(&evenths, 0, sizeof(xcb_event_handlers_t));
1093 memset(&prophs, 0, sizeof(xcb_property_handlers_t));
1095 byChild = alloc_table();
1096 byParent = alloc_table();
1098 TAILQ_INIT(&bindings);
1100 c = xcb_connect(NULL, &screens);
1102 printf("x screen is %d\n", screens);
1104 /* TODO: this has to be more beautiful somewhen */
1105 int major, minor, error;
1107 major = XkbMajorVersion;
1108 minor = XkbMinorVersion;
1110 int evBase, errBase;
1112 if ((xkbdpy = XkbOpenDisplay(getenv("DISPLAY"), &evBase, &errBase, &major, &minor, &error)) == NULL) {
1113 fprintf(stderr, "XkbOpenDisplay() failed\n");
1118 if (!XkbQueryExtension(xkbdpy,&i1,&evBase,&errBase,&major,&minor)) {
1119 fprintf(stderr, "XKB not supported by X-server\n");
1122 /* end of ugliness */
1124 xcb_event_handlers_init(c, &evenths);
1125 for(i = 2; i < 128; ++i)
1126 xcb_event_set_handler(&evenths, i, handleEvent, 0);
1128 for(i = 0; i < 256; ++i)
1129 xcb_event_set_error_handler(&evenths, i, (xcb_generic_error_handler_t) handleEvent, 0);
1131 /* Expose = an Application should redraw itself. That is, we have to redraw our
1132 * contents (= top/bottom bar, titlebars for each window) */
1133 xcb_event_set_expose_handler(&evenths, handleExposeEvent, 0);
1135 /* Key presses/releases are pretty obvious, I think */
1136 xcb_event_set_key_press_handler(&evenths, handle_key_press, 0);
1137 xcb_event_set_key_release_handler(&evenths, handle_key_release, 0);
1139 /* Enter window = user moved his mouse over the window */
1140 xcb_event_set_enter_notify_handler(&evenths, handle_enter_notify, 0);
1142 xcb_event_set_unmap_notify_handler(&evenths, handle_unmap_notify_event, 0);
1144 xcb_property_handlers_init(&prophs, &evenths);
1145 xcb_event_set_map_notify_handler(&evenths, handle_map_notify_event, &prophs);
1147 root = xcb_aux_get_screen(c, screens)->root;
1150 uint32_t mask = XCB_CW_EVENT_MASK;
1151 uint32_t values[] = { XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY | XCB_EVENT_MASK_PROPERTY_CHANGE };
1152 xcb_change_window_attributes(c, root, mask, values);
1154 #define BIND(key, modifier, cmd) { \
1155 Binding *new = malloc(sizeof(Binding)); \
1156 new->keycode = key; \
1157 new->mods = modifier; \
1158 new->command = cmd; \
1159 TAILQ_INSERT_TAIL(&bindings, new, bindings); \
1163 BIND(38, BIND_MODE_SWITCH, "foo");
1165 BIND(30, 0, "exec /usr/pkg/bin/urxvt");
1167 BIND(44, BIND_MOD_1, "h");
1168 BIND(45, BIND_MOD_1, "j");
1169 BIND(46, BIND_MOD_1, "k");
1170 BIND(47, BIND_MOD_1, "l");
1172 BIND(44, BIND_MOD_1 | BIND_CONTROL, "mh");
1173 BIND(45, BIND_MOD_1 | BIND_CONTROL, "mj");
1174 BIND(46, BIND_MOD_1 | BIND_CONTROL, "mk");
1175 BIND(47, BIND_MOD_1 | BIND_CONTROL, "ml");
1178 TAILQ_FOREACH(bind, &bindings, bindings) {
1179 printf("Grabbing %d\n", bind->keycode);
1180 if (bind->mods & BIND_MODE_SWITCH)
1181 xcb_grab_key(c, 0, root, 0, bind->keycode, XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_SYNC);
1182 else xcb_grab_key(c, 0, root, bind->mods, bind->keycode, XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_ASYNC);
1185 start_application(TERMINAL, NULL);
1189 manage_existing_windows(c, &prophs, root);
1191 xcb_event_wait_for_event_loop(&evenths);