4 * i3 - an improved dynamic tiling window manager
6 * © 2009 Michael Stapelberg and contributors
8 * See file LICENSE for license information.
17 #include <xcb/xcb_wm.h>
18 #include <X11/XKBlib.h>
32 /* After mapping/unmapping windows, a notify event is generated. However, we don’t want it,
33 since it’d trigger an infinite loop of switching between the different windows when
34 changing workspaces */
35 int ignore_notify_event = -1;
38 * Due to bindings like Mode_switch + <a>, we need to bind some keys in XCB_GRAB_MODE_SYNC.
39 * Therefore, we just replay all key presses.
42 int handle_key_release(void *ignored, xcb_connection_t *conn, xcb_key_release_event_t *event) {
43 printf("got key release, just passing\n");
44 xcb_allow_events(conn, XCB_ALLOW_REPLAY_KEYBOARD, event->time);
50 * There was a key press. We compare this key code with our bindings table and pass
51 * the bound action to parse_command().
54 int handle_key_press(void *ignored, xcb_connection_t *conn, xcb_key_press_event_t *event) {
55 printf("Keypress %d\n", event->detail);
57 /* We need to get the keysym group (There are group 1 to group 4, each holding
58 two keysyms (without shift and with shift) using Xkb because X fails to
59 provide them reliably (it works in Xephyr, it does not in real X) */
61 if (XkbGetState(xkbdpy, XkbUseCoreKbd, &state) == Success && (state.group+1) == 2)
64 printf("state %d\n", event->state);
66 /* Remove the numlock bit, all other bits are modifiers we can bind to */
67 uint16_t state_filtered = event->state & ~XCB_MOD_MASK_LOCK;
69 /* Find the binding */
71 TAILQ_FOREACH(bind, &bindings, bindings)
72 if (bind->keycode == event->detail && bind->mods == state_filtered)
75 /* No match? Then it was an actively grabbed key, that is with Mode_switch, and
76 the user did not press Mode_switch, so just pass it… */
77 if (bind == TAILQ_END(&bindings)) {
78 xcb_allow_events(conn, ReplayKeyboard, event->time);
83 parse_command(conn, bind->command);
84 if (event->state & 0x2) {
85 printf("Mode_switch -> allow_events(SyncKeyboard)\n");
86 xcb_allow_events(conn, SyncKeyboard, event->time);
94 * When the user moves the mouse pointer onto a window, this callback gets called.
97 int handle_enter_notify(void *ignored, xcb_connection_t *conn, xcb_enter_notify_event_t *event) {
98 printf("enter_notify for %08x, serial %d\n", event->event, event->sequence);
99 /* Some events are not interesting, because they were not generated actively by the
100 user, but be reconfiguration of windows */
101 if (event->sequence == ignore_notify_event) {
102 printf("Ignoring, because of previous map\n");
106 /* This was either a focus for a client’s parent (= titlebar)… */
107 Client *client = table_get(byParent, event->event);
108 /* …or the client itself */
110 client = table_get(byChild, event->event);
112 /* If not, then the user moved his cursor to the root window. In that case, we adjust c_ws */
113 if (client == NULL) {
114 printf("Getting screen at %d x %d\n", event->root_x, event->root_y);
115 i3Screen *screen = get_screen_containing(event->root_x, event->root_y);
116 if (screen == NULL) {
117 printf("ERROR: No such screen\n");
120 c_ws = &workspaces[screen->current_workspace];
121 printf("We're now on virtual screen number %d\n", screen->num);
125 set_focus(conn, client);
130 int handle_button_press(void *ignored, xcb_connection_t *conn, xcb_button_press_event_t *event) {
131 printf("button press!\n");
132 /* This was either a focus for a client’s parent (= titlebar)… */
133 Client *client = table_get(byChild, event->event);
134 bool border_click = false;
135 if (client == NULL) {
136 client = table_get(byParent, event->event);
139 if (client == NULL) {
140 /* The client was neither on a client’s titlebar nor on a client itself, maybe on a stack_window? */
141 struct Stack_Window *stack_win;
142 SLIST_FOREACH(stack_win, &stack_wins, stack_windows)
143 if (stack_win->window == event->event) {
144 /* A stack window was clicked. We calculate the destination client by
145 dividing the Y position of the event through the height of a window
146 decoration and then set the focus to this client. */
147 i3Font *font = load_font(conn, config.font);
148 int decoration_height = (font->height + 2 + 2);
149 int destination = (event->event_y / decoration_height),
153 printf("Click on stack_win for client %d\n", destination);
154 CIRCLEQ_FOREACH(client, &(stack_win->container->clients), clients)
155 if (c++ == destination) {
156 set_focus(conn, client);
166 xcb_window_t root = xcb_setup_roots_iterator(xcb_get_setup(conn)).data->root;
167 xcb_screen_t *root_screen = xcb_setup_roots_iterator(xcb_get_setup(conn)).data;
169 /* Set focus in any case */
170 set_focus(conn, client);
172 /* Let’s see if this was on the borders (= resize). If not, we’re done */
173 printf("press button on x=%d, y=%d\n", event->event_x, event->event_y);
175 Container *con = client->container,
178 enum { O_HORIZONTAL, O_VERTICAL } orientation = O_VERTICAL;
181 printf("dock. done.\n");
182 xcb_allow_events(conn, XCB_ALLOW_REPLAY_POINTER, event->time);
187 printf("event->event_x = %d, client->rect.width = %d\n", event->event_x, client->rect.width);
190 printf("client. done.\n");
191 xcb_allow_events(conn, XCB_ALLOW_REPLAY_POINTER, event->time);
196 if (event->event_y < 2) {
197 /* This was a press on the top border */
200 first = con->workspace->table[con->col][con->row-1];
202 orientation = O_HORIZONTAL;
203 } else if (event->event_y >= (client->rect.height - 2)) {
205 if (con->row == (con->workspace->rows-1))
208 second = con->workspace->table[con->col][con->row+1];
209 orientation = O_HORIZONTAL;
210 } else if (event->event_x < 2) {
214 first = con->workspace->table[con->col-1][con->row];
216 } else if (event->event_x > 2) {
218 if (con->col == (con->workspace->cols-1))
221 second = con->workspace->table[con->col+1][con->row];
224 /* Open a new window, the resizebar. Grab the pointer and move the window around
225 as the user moves the pointer. */
226 Rect grabrect = {0, 0, root_screen->width_in_pixels, root_screen->height_in_pixels};
227 xcb_window_t grabwin = create_window(conn, grabrect, XCB_WINDOW_CLASS_INPUT_ONLY, 0, NULL);
230 if (orientation == O_VERTICAL) {
231 helprect.x = event->root_x;
234 helprect.height = root_screen->height_in_pixels; /* this has to be the cell’s height */
237 helprect.y = event->root_y;
238 helprect.width = root_screen->width_in_pixels; /* this has to be the cell’s width*/
241 xcb_window_t helpwin = create_window(conn, helprect, XCB_WINDOW_CLASS_INPUT_OUTPUT, 0, NULL);
243 uint32_t values[1] = {get_colorpixel(conn, NULL, helpwin, "#4c7899")};
244 xcb_void_cookie_t cookie = xcb_change_window_attributes_checked(conn, helpwin, XCB_CW_BACK_PIXEL, values);
245 check_error(conn, cookie, "Could not change window attributes (background color)");
247 xcb_circulate_window(conn, XCB_CIRCULATE_RAISE_LOWEST, helpwin);
249 xcb_grab_pointer(conn, false, root, XCB_EVENT_MASK_BUTTON_RELEASE | XCB_EVENT_MASK_POINTER_MOTION,
250 XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC, grabwin, XCB_NONE, XCB_CURRENT_TIME);
254 xcb_generic_event_t *inside_event;
255 /* I’ve always wanted to have my own eventhandler… */
256 while ((inside_event = xcb_wait_for_event(conn))) {
257 /* Same as get_event_handler in xcb */
258 int nr = inside_event->response_type;
260 /* An error occured */
261 handle_event(NULL, conn, inside_event);
266 nr &= XCB_EVENT_RESPONSE_TYPE_MASK;
269 /* Check if we need to escape this loop */
270 if (nr == XCB_BUTTON_RELEASE)
274 case XCB_MOTION_NOTIFY:
275 if (orientation == O_VERTICAL) {
276 values[0] = ((xcb_motion_notify_event_t*)inside_event)->root_x;
277 xcb_configure_window(conn, helpwin, XCB_CONFIG_WINDOW_X, values);
279 values[0] = ((xcb_motion_notify_event_t*)inside_event)->root_y;
280 xcb_configure_window(conn, helpwin, XCB_CONFIG_WINDOW_Y, values);
286 /* Use original handler */
287 xcb_event_handle(&evenths, inside_event);
290 printf("Ignoring event of type %d\n", nr);
296 xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
297 xcb_destroy_window(conn, helpwin);
298 xcb_destroy_window(conn, grabwin);
301 Workspace *ws = con->workspace;
302 if (orientation == O_VERTICAL) {
303 printf("Resize was from X = %d to X = %d\n", event->root_x, values[0]);
305 /* Convert 0 (for default width_factor) to actual numbers */
306 if (first->width_factor == 0)
307 first->width_factor = ((float)ws->rect.width / ws->cols) / ws->rect.width;
308 if (second->width_factor == 0)
309 second->width_factor = ((float)ws->rect.width / ws->cols) / ws->rect.width;
311 first->width_factor *= (float)(first->width + (values[0] - event->root_x)) / first->width;
312 second->width_factor *= (float)(second->width - (values[0] - event->root_x)) / second->width;
314 printf("Resize was from Y = %d to Y = %d\n", event->root_y, values[0]);
316 /* Convert 0 (for default height_factor) to actual numbers */
317 if (first->height_factor == 0)
318 first->height_factor = ((float)ws->rect.height / ws->rows) / ws->rect.height;
319 if (second->height_factor == 0)
320 second->height_factor = ((float)ws->rect.height / ws->rows) / ws->rect.height;
322 first->height_factor *= (float)(first->height + (values[0] - event->root_y)) / first->height;
323 second->height_factor *= (float)(second->height - (values[0] - event->root_y)) / second->height;
332 * A new window appeared on the screen (=was mapped), so let’s manage it.
335 int handle_map_notify_event(void *prophs, xcb_connection_t *conn, xcb_map_notify_event_t *event) {
336 window_attributes_t wa = { TAG_VALUE };
337 wa.u.override_redirect = event->override_redirect;
338 printf("MapNotify for 0x%08x, serial is %d.\n", event->window, event->sequence);
339 ignore_notify_event = event->sequence;
340 manage_window(prophs, conn, event->window, wa);
345 * Configuration notifies are only handled because we need to set up ignore for the following
346 * enter notify events
349 int handle_configure_event(void *prophs, xcb_connection_t *conn, xcb_configure_notify_event_t *event) {
350 xcb_window_t root = xcb_setup_roots_iterator(xcb_get_setup(conn)).data->root;
352 printf("handle_configure_event\n");
353 printf("event->x = %d, ->y = %d, ->width = %d, ->height = %d\n", event->x, event->y, event->width, event->height);
354 if (event->event == root) {
355 printf("reconfigure of the root window, need to xinerama\n");
356 xinerama_requery_screens(conn);
360 ignore_notify_event = event->sequence;
362 Client *client = table_get(byChild, event->window);
363 if (client == NULL) {
364 printf("client not managed, ignoring\n");
368 if (client->fullscreen) {
369 printf("client in fullscreen, not touching\n");
373 /* Let’s see if the application has changed size/position on its own *sigh*… */
374 if ((event->x != client->child_rect.x) ||
375 (event->y != client->child_rect.y) ||
376 (event->width != client->child_rect.width) ||
377 (event->height != client->child_rect.height)) {
378 /* Who is your window manager? Who’s that, huh? I AM YOUR WINDOW MANAGER! */
379 printf("Application wanted to resize itself. Fixed that.\n");
380 client->force_reconfigure = true;
381 render_container(conn, client->container);
389 * Our window decorations were unmapped. That means, the window will be killed now,
390 * so we better clean up before.
393 int handle_unmap_notify_event(void *data, xcb_connection_t *c, xcb_unmap_notify_event_t *e) {
394 xcb_window_t root = xcb_setup_roots_iterator(xcb_get_setup(c)).data->root;
396 ignore_notify_event = e->sequence;
398 Client *client = table_get(byChild, e->window);
399 /* First, we need to check if the client is awaiting an unmap-request which
400 was generated by us reparenting the window. In that case, we just ignore it. */
401 if (client != NULL && client->awaiting_useless_unmap) {
402 printf("Dropping this unmap request, it was generated by reparenting\n");
403 client->awaiting_useless_unmap = false;
406 client = table_remove(byChild, e->window);
408 printf("UnmapNotify for 0x%08x (received from 0x%08x): ", e->window, e->event);
409 if (client == NULL) {
410 printf("not a managed window. Ignoring.\n");
414 if (client->container != NULL) {
415 Client *to_focus = NULL;
416 Container *con = client->container;
418 /* If the client which is being unmapped was the currently active, we need to find
419 the next possible window to focus in this container */
420 if (con->currently_focused == client) {
421 to_focus = CIRCLEQ_NEXT_OR_NULL(&(con->clients), client, clients);
422 if (to_focus == NULL)
423 to_focus = CIRCLEQ_PREV_OR_NULL(&(con->clients), client, clients);
425 /* Set focus in data structure to the next/previous window, if any (else NULL) */
426 con->currently_focused = to_focus;
429 /* If this was the fullscreen client, we need to unset it */
430 if (client->fullscreen)
431 con->workspace->fullscreen_client = NULL;
433 /* If the container will be empty now and is in stacking mode, we need to
434 correctly resize the stack_win */
435 if (con->currently_focused == NULL && con->mode == MODE_STACK) {
436 struct Stack_Window *stack_win = &(con->stack_win);
437 stack_win->height = 0;
438 xcb_unmap_window(c, stack_win->window);
441 /* Remove the client from the list of clients */
442 CIRCLEQ_REMOVE(&(con->clients), client, clients);
444 /* Actually set focus, if there is a window which should get it */
445 if (to_focus != NULL)
446 set_focus(c, to_focus);
449 printf("child of 0x%08x.\n", client->frame);
450 xcb_reparent_window(c, client->child, root, 0, 0);
451 xcb_destroy_window(c, client->frame);
453 table_remove(byParent, client->frame);
455 cleanup_table(c, client->container->workspace);
465 * Called when a window changes its title
468 int handle_windowname_change(void *data, xcb_connection_t *conn, uint8_t state,
469 xcb_window_t window, xcb_atom_t atom, xcb_get_property_reply_t *prop) {
470 printf("window's name changed.\n");
471 Client *client = table_get(byChild, window);
475 client->name_len = xcb_get_property_value_length(prop);
476 client->name = malloc(client->name_len);
477 strncpy(client->name, xcb_get_property_value(prop), client->name_len);
478 printf("rename to \"%.*s\".\n", client->name_len, client->name);
480 if (client->container->mode == MODE_STACK)
481 render_container(conn, client->container);
482 else decorate_window(conn, client, client->frame, client->titlegc, 0);
489 * Expose event means we should redraw our windows (= title bar)
492 int handle_expose_event(void *data, xcb_connection_t *conn, xcb_expose_event_t *e) {
493 printf("got expose_event\n");
494 /* e->count is the number of minimum remaining expose events for this window, so we
495 skip all events but the last one */
499 Client *client = table_get(byParent, e->window);
500 if (client == NULL) {
501 /* There was no client in the table, so this is probably an expose event for
502 one of our stack_windows. */
503 struct Stack_Window *stack_win;
504 SLIST_FOREACH(stack_win, &stack_wins, stack_windows)
505 if (stack_win->window == e->window) {
506 render_container(conn, stack_win->container);
512 printf("handle_expose_event()\n");
513 if (client->container->mode != MODE_STACK)
514 decorate_window(conn, client, client->frame, client->titlegc, 0);
516 xcb_change_gc_single(conn, client->titlegc, XCB_GC_FOREGROUND,
517 get_colorpixel(conn, client, client->frame, "#285577"));
519 xcb_rectangle_t rect = {0, 0, client->rect.width, client->rect.height};
520 xcb_poly_fill_rectangle(conn, client->frame, client->titlegc, 1, &rect);
528 * Handle client messages (EWMH)
531 int handle_client_message(void *data, xcb_connection_t *conn, xcb_client_message_event_t *event) {
532 printf("client_message\n");
534 if (event->type == atoms[_NET_WM_STATE]) {
535 if (event->format != 32 || event->data.data32[1] != atoms[_NET_WM_STATE_FULLSCREEN])
538 printf("fullscreen\n");
540 Client *client = table_get(byChild, event->window);
544 /* Check if the fullscreen state should be toggled */
545 if ((client->fullscreen &&
546 (event->data.data32[0] == _NET_WM_STATE_REMOVE ||
547 event->data.data32[0] == _NET_WM_STATE_TOGGLE)) ||
548 (!client->fullscreen &&
549 (event->data.data32[0] == _NET_WM_STATE_ADD ||
550 event->data.data32[0] == _NET_WM_STATE_TOGGLE)))
551 toggle_fullscreen(conn, client);
553 printf("unhandled clientmessage\n");
560 int window_type_handler(void *data, xcb_connection_t *conn, uint8_t state, xcb_window_t window,
561 xcb_atom_t atom, xcb_get_property_reply_t *property) {
562 /* TODO: Implement this one. To do this, implement a little test program which sleep(1)s
563 before changing this property. */
564 printf("_NET_WM_WINDOW_TYPE changed, this is not yet implemented.\n");