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>
31 /* After mapping/unmapping windows, a notify event is generated. However, we don’t want it,
32 since it’d trigger an infinite loop of switching between the different windows when
33 changing workspaces */
34 int ignore_notify_event = -1;
37 * Due to bindings like Mode_switch + <a>, we need to bind some keys in XCB_GRAB_MODE_SYNC.
38 * Therefore, we just replay all key presses.
41 int handle_key_release(void *ignored, xcb_connection_t *conn, xcb_key_release_event_t *event) {
42 printf("got key release, just passing\n");
43 xcb_allow_events(conn, XCB_ALLOW_REPLAY_KEYBOARD, event->time);
49 * There was a key press. We compare this key code with our bindings table and pass
50 * the bound action to parse_command().
53 int handle_key_press(void *ignored, xcb_connection_t *conn, xcb_key_press_event_t *event) {
54 printf("Keypress %d\n", event->detail);
56 /* We need to get the keysym group (There are group 1 to group 4, each holding
57 two keysyms (without shift and with shift) using Xkb because X fails to
58 provide them reliably (it works in Xephyr, it does not in real X) */
60 if (XkbGetState(xkbdpy, XkbUseCoreKbd, &state) == Success && (state.group+1) == 2)
63 printf("state %d\n", event->state);
65 /* Remove the numlock bit, all other bits are modifiers we can bind to */
66 uint16_t state_filtered = event->state & ~XCB_MOD_MASK_LOCK;
68 /* Find the binding */
70 TAILQ_FOREACH(bind, &bindings, bindings)
71 if (bind->keycode == event->detail && bind->mods == state_filtered)
74 /* No match? Then it was an actively grabbed key, that is with Mode_switch, and
75 the user did not press Mode_switch, so just pass it… */
76 if (bind == TAILQ_END(&bindings)) {
77 xcb_allow_events(conn, ReplayKeyboard, event->time);
82 parse_command(conn, bind->command);
83 if (event->state & 0x2) {
84 printf("Mode_switch -> allow_events(SyncKeyboard)\n");
85 xcb_allow_events(conn, SyncKeyboard, event->time);
93 * When the user moves the mouse pointer onto a window, this callback gets called.
96 int handle_enter_notify(void *ignored, xcb_connection_t *conn, xcb_enter_notify_event_t *event) {
97 printf("enter_notify for %08x, serial %d\n", event->event, event->sequence);
98 /* Some events are not interesting, because they were not generated actively by the
99 user, but be reconfiguration of windows */
100 if (event->sequence == ignore_notify_event) {
101 printf("Ignoring, because of previous map\n");
105 /* This was either a focus for a client’s parent (= titlebar)… */
106 Client *client = table_get(byParent, event->event);
107 /* …or the client itself */
109 client = table_get(byChild, event->event);
111 /* If not, then the user moved his cursor to the root window. In that case, we adjust c_ws */
112 if (client == NULL) {
113 printf("Getting screen at %d x %d\n", event->root_x, event->root_y);
114 i3Screen *screen = get_screen_containing(event->root_x, event->root_y);
115 if (screen == NULL) {
116 printf("ERROR: No such screen\n");
119 c_ws = &workspaces[screen->current_workspace];
120 printf("We're now on virtual screen number %d\n", screen->num);
124 set_focus(conn, client);
129 int handle_button_press(void *ignored, xcb_connection_t *conn, xcb_button_press_event_t *event) {
130 printf("button press!\n");
131 /* This was either a focus for a client’s parent (= titlebar)… */
132 Client *client = table_get(byChild, event->event);
133 bool border_click = false;
134 if (client == NULL) {
135 client = table_get(byParent, event->event);
138 if (client == NULL) {
139 /* The client was neither on a client’s titlebar nor on a client itself, maybe on a stack_window? */
140 struct Stack_Window *stack_win;
141 SLIST_FOREACH(stack_win, &stack_wins, stack_windows)
142 if (stack_win->window == event->event) {
143 /* A stack window was clicked. We calculate the destination client by
144 dividing the Y position of the event through the height of a window
145 decoration and then set the focus to this client. */
146 i3Font *font = load_font(conn, config.font);
147 int decoration_height = (font->height + 2 + 2);
148 int destination = (event->event_y / decoration_height),
152 printf("Click on stack_win for client %d\n", destination);
153 CIRCLEQ_FOREACH(client, &(stack_win->container->clients), clients)
154 if (c++ == destination) {
155 set_focus(conn, client);
165 xcb_window_t root = xcb_setup_roots_iterator(xcb_get_setup(conn)).data->root;
166 xcb_screen_t *root_screen = xcb_setup_roots_iterator(xcb_get_setup(conn)).data;
168 /* Set focus in any case */
169 set_focus(conn, client);
171 /* Let’s see if this was on the borders (= resize). If not, we’re done */
172 printf("press button on x=%d, y=%d\n", event->event_x, event->event_y);
174 Container *con = client->container,
177 enum { O_HORIZONTAL, O_VERTICAL } orientation = O_VERTICAL;
180 printf("dock. done.\n");
181 xcb_allow_events(conn, XCB_ALLOW_REPLAY_POINTER, event->time);
186 printf("event->event_x = %d, client->rect.width = %d\n", event->event_x, client->rect.width);
189 printf("client. done.\n");
190 xcb_allow_events(conn, XCB_ALLOW_REPLAY_POINTER, event->time);
195 if (event->event_y < 2) {
196 /* This was a press on the top border */
199 first = con->workspace->table[con->col][con->row-1];
201 orientation = O_HORIZONTAL;
202 } else if (event->event_y >= (client->rect.height - 2)) {
204 if (con->row == (con->workspace->rows-1))
207 second = con->workspace->table[con->col][con->row+1];
208 orientation = O_HORIZONTAL;
209 } else if (event->event_x <= 2) {
213 first = con->workspace->table[con->col-1][con->row];
215 } else if (event->event_x > 2) {
217 if (con->col == (con->workspace->cols-1))
220 second = con->workspace->table[con->col+1][con->row];
223 /* Open a new window, the resizebar. Grab the pointer and move the window around
224 as the user moves the pointer. */
225 Rect grabrect = {0, 0, root_screen->width_in_pixels, root_screen->height_in_pixels};
226 xcb_window_t grabwin = create_window(conn, grabrect, XCB_WINDOW_CLASS_INPUT_ONLY, -1, 0, NULL);
229 if (orientation == O_VERTICAL) {
230 helprect.x = event->root_x;
233 helprect.height = root_screen->height_in_pixels; /* this has to be the cell’s height */
236 helprect.y = event->root_y;
237 helprect.width = root_screen->width_in_pixels; /* this has to be the cell’s width */
240 xcb_window_t helpwin = create_window(conn, helprect, XCB_WINDOW_CLASS_INPUT_OUTPUT,
241 (orientation == O_VERTICAL ?
242 XCB_CURSOR_SB_V_DOUBLE_ARROW :
243 XCB_CURSOR_SB_H_DOUBLE_ARROW), 0, NULL);
245 uint32_t values[1] = {get_colorpixel(conn, NULL, helpwin, "#4c7899")};
246 xcb_void_cookie_t cookie = xcb_change_window_attributes_checked(conn, helpwin, XCB_CW_BACK_PIXEL, values);
247 check_error(conn, cookie, "Could not change window attributes (background color)");
249 xcb_circulate_window(conn, XCB_CIRCULATE_RAISE_LOWEST, helpwin);
251 xcb_grab_pointer(conn, false, root, XCB_EVENT_MASK_BUTTON_RELEASE | XCB_EVENT_MASK_POINTER_MOTION,
252 XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC, grabwin, XCB_NONE, XCB_CURRENT_TIME);
256 xcb_generic_event_t *inside_event;
257 /* I’ve always wanted to have my own eventhandler… */
258 while ((inside_event = xcb_wait_for_event(conn))) {
259 /* Same as get_event_handler in xcb */
260 int nr = inside_event->response_type;
262 /* An error occured */
263 handle_event(NULL, conn, inside_event);
268 nr &= XCB_EVENT_RESPONSE_TYPE_MASK;
271 /* Check if we need to escape this loop */
272 if (nr == XCB_BUTTON_RELEASE)
276 case XCB_MOTION_NOTIFY:
277 if (orientation == O_VERTICAL) {
278 values[0] = ((xcb_motion_notify_event_t*)inside_event)->root_x;
279 xcb_configure_window(conn, helpwin, XCB_CONFIG_WINDOW_X, values);
281 values[0] = ((xcb_motion_notify_event_t*)inside_event)->root_y;
282 xcb_configure_window(conn, helpwin, XCB_CONFIG_WINDOW_Y, values);
288 /* Use original handler */
289 xcb_event_handle(&evenths, inside_event);
292 printf("Ignoring event of type %d\n", nr);
298 xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
299 xcb_destroy_window(conn, helpwin);
300 xcb_destroy_window(conn, grabwin);
303 Workspace *ws = con->workspace;
304 if (orientation == O_VERTICAL) {
305 printf("Resize was from X = %d to X = %d\n", event->root_x, values[0]);
307 /* Convert 0 (for default width_factor) to actual numbers */
308 if (first->width_factor == 0)
309 first->width_factor = ((float)ws->rect.width / ws->cols) / ws->rect.width;
310 if (second->width_factor == 0)
311 second->width_factor = ((float)ws->rect.width / ws->cols) / ws->rect.width;
313 first->width_factor *= (float)(first->width + (values[0] - event->root_x)) / first->width;
314 second->width_factor *= (float)(second->width - (values[0] - event->root_x)) / second->width;
316 printf("Resize was from Y = %d to Y = %d\n", event->root_y, values[0]);
318 /* Convert 0 (for default height_factor) to actual numbers */
319 if (first->height_factor == 0)
320 first->height_factor = ((float)ws->rect.height / ws->rows) / ws->rect.height;
321 if (second->height_factor == 0)
322 second->height_factor = ((float)ws->rect.height / ws->rows) / ws->rect.height;
324 first->height_factor *= (float)(first->height + (values[0] - event->root_y)) / first->height;
325 second->height_factor *= (float)(second->height - (values[0] - event->root_y)) / second->height;
334 * A new window appeared on the screen (=was mapped), so let’s manage it.
337 int handle_map_notify_event(void *prophs, xcb_connection_t *conn, xcb_map_notify_event_t *event) {
338 window_attributes_t wa = { TAG_VALUE };
339 wa.u.override_redirect = event->override_redirect;
340 printf("MapNotify for 0x%08x, serial is %d.\n", event->window, event->sequence);
341 printf("setting ignore_notify_event = %d\n", event->sequence);
342 ignore_notify_event = event->sequence;
343 manage_window(prophs, conn, event->window, wa);
348 * Configuration notifies are only handled because we need to set up ignore for the following
349 * enter notify events
352 int handle_configure_event(void *prophs, xcb_connection_t *conn, xcb_configure_notify_event_t *event) {
353 xcb_window_t root = xcb_setup_roots_iterator(xcb_get_setup(conn)).data->root;
355 printf("handle_configure_event\n");
356 printf("event->x = %d, ->y = %d, ->width = %d, ->height = %d\n", event->x, event->y, event->width, event->height);
357 printf("sequence = %d\n", event->sequence);
359 ignore_notify_event = event->sequence;
361 if (event->event == root) {
362 printf("reconfigure of the root window, need to xinerama\n");
363 /* FIXME: Somehow, this is occuring too often. Therefore, we check for 0/0,
364 but is there a better way? */
365 if (event->x == 0 && event->y == 0)
366 xinerama_requery_screens(conn);
370 Client *client = table_get(byChild, event->window);
371 if (client == NULL) {
372 printf("client not managed, ignoring\n");
376 if (client->fullscreen) {
377 printf("client in fullscreen, not touching\n");
381 /* Let’s see if the application has changed size/position on its own *sigh*… */
382 if ((event->x != client->child_rect.x) ||
383 (event->y != client->child_rect.y) ||
384 (event->width != client->child_rect.width) ||
385 (event->height != client->child_rect.height)) {
386 /* Who is your window manager? Who’s that, huh? I AM YOUR WINDOW MANAGER! */
387 printf("Application wanted to resize itself. Fixed that.\n");
388 client->force_reconfigure = true;
389 render_container(conn, client->container);
397 * Our window decorations were unmapped. That means, the window will be killed now,
398 * so we better clean up before.
401 int handle_unmap_notify_event(void *data, xcb_connection_t *conn, xcb_unmap_notify_event_t *event) {
402 xcb_window_t root = xcb_setup_roots_iterator(xcb_get_setup(conn)).data->root;
404 printf("setting ignore_notify_event = %d\n", event->sequence);
406 ignore_notify_event = event->sequence;
408 Client *client = table_get(byChild, event->window);
409 /* First, we need to check if the client is awaiting an unmap-request which
410 was generated by us reparenting the window. In that case, we just ignore it. */
411 if (client != NULL && client->awaiting_useless_unmap) {
412 printf("Dropping this unmap request, it was generated by reparenting\n");
413 client->awaiting_useless_unmap = false;
416 client = table_remove(byChild, event->window);
418 printf("UnmapNotify for 0x%08x (received from 0x%08x): ", event->window, event->event);
419 if (client == NULL) {
420 printf("not a managed window. Ignoring.\n");
424 /* Free the client’s colorpixel cache */
425 struct Colorpixel *colorpixel;
426 while (!SLIST_EMPTY(&(client->colorpixels))) {
427 colorpixel = SLIST_FIRST(&(client->colorpixels));
428 SLIST_REMOVE_HEAD(&(client->colorpixels), colorpixels);
429 free(colorpixel->hex);
433 if (client->name != NULL)
436 if (client->container != NULL) {
437 Container *con = client->container;
439 /* If this was the fullscreen client, we need to unset it */
440 if (client->fullscreen)
441 con->workspace->fullscreen_client = NULL;
443 /* If the container will be empty now and is in stacking mode, we need to
444 correctly resize the stack_win */
445 if (CIRCLEQ_EMPTY(&(con->clients)) && con->mode == MODE_STACK) {
446 struct Stack_Window *stack_win = &(con->stack_win);
447 stack_win->rect.height = 0;
448 xcb_unmap_window(conn, stack_win->window);
451 /* Remove the client from the list of clients */
452 CIRCLEQ_REMOVE(&(con->clients), client, clients);
454 /* Remove from the focus stack */
455 printf("Removing from focus stack\n");
456 SLIST_REMOVE(&(con->workspace->focus_stack), client, Client, focus_clients);
458 /* Remove from currently_focused */
459 con->currently_focused = NULL;
461 /* Actually set focus, if there is a window which should get it */
462 if (!SLIST_EMPTY(&(con->workspace->focus_stack)))
463 set_focus(conn, SLIST_FIRST(&(con->workspace->focus_stack)));
466 printf("child of 0x%08x.\n", client->frame);
467 xcb_reparent_window(conn, client->child, root, 0, 0);
468 xcb_destroy_window(conn, client->frame);
470 table_remove(byParent, client->frame);
472 cleanup_table(conn, client->container->workspace);
482 * Called when a window changes its title
485 int handle_windowname_change(void *data, xcb_connection_t *conn, uint8_t state,
486 xcb_window_t window, xcb_atom_t atom, xcb_get_property_reply_t *prop) {
487 printf("window's name changed.\n");
488 Client *client = table_get(byChild, window);
492 if (client->name != NULL)
495 client->name_len = xcb_get_property_value_length(prop);
496 client->name = smalloc(client->name_len);
497 strncpy(client->name, xcb_get_property_value(prop), client->name_len);
498 printf("rename to \"%.*s\".\n", client->name_len, client->name);
500 if (client->container->mode == MODE_STACK)
501 render_container(conn, client->container);
502 else decorate_window(conn, client, client->frame, client->titlegc, 0);
509 * Expose event means we should redraw our windows (= title bar)
512 int handle_expose_event(void *data, xcb_connection_t *conn, xcb_expose_event_t *event) {
513 printf("got expose_event\n");
514 /* event->count is the number of minimum remaining expose events for this window, so we
515 skip all events but the last one */
516 if (event->count != 0)
519 Client *client = table_get(byParent, event->window);
520 if (client == NULL) {
521 /* There was no client in the table, so this is probably an expose event for
522 one of our stack_windows. */
523 struct Stack_Window *stack_win;
524 SLIST_FOREACH(stack_win, &stack_wins, stack_windows)
525 if (stack_win->window == event->window) {
526 render_container(conn, stack_win->container);
532 printf("handle_expose_event()\n");
533 if (client->container->mode != MODE_STACK)
534 decorate_window(conn, client, client->frame, client->titlegc, 0);
536 xcb_change_gc_single(conn, client->titlegc, XCB_GC_FOREGROUND,
537 get_colorpixel(conn, client, client->frame, "#285577"));
539 xcb_rectangle_t rect = {0, 0, client->rect.width, client->rect.height};
540 xcb_poly_fill_rectangle(conn, client->frame, client->titlegc, 1, &rect);
548 * Handle client messages (EWMH)
551 int handle_client_message(void *data, xcb_connection_t *conn, xcb_client_message_event_t *event) {
552 printf("client_message\n");
554 if (event->type == atoms[_NET_WM_STATE]) {
555 if (event->format != 32 || event->data.data32[1] != atoms[_NET_WM_STATE_FULLSCREEN])
558 printf("fullscreen\n");
560 Client *client = table_get(byChild, event->window);
564 /* Check if the fullscreen state should be toggled */
565 if ((client->fullscreen &&
566 (event->data.data32[0] == _NET_WM_STATE_REMOVE ||
567 event->data.data32[0] == _NET_WM_STATE_TOGGLE)) ||
568 (!client->fullscreen &&
569 (event->data.data32[0] == _NET_WM_STATE_ADD ||
570 event->data.data32[0] == _NET_WM_STATE_TOGGLE)))
571 toggle_fullscreen(conn, client);
573 printf("unhandled clientmessage\n");
580 int window_type_handler(void *data, xcb_connection_t *conn, uint8_t state, xcb_window_t window,
581 xcb_atom_t atom, xcb_get_property_reply_t *property) {
582 /* TODO: Implement this one. To do this, implement a little test program which sleep(1)s
583 before changing this property. */
584 printf("_NET_WM_WINDOW_TYPE changed, this is not yet implemented.\n");