i3
src/handlers.c
Go to the documentation of this file.
00001 /*
00002  * vim:ts=4:sw=4:expandtab
00003  *
00004  * i3 - an improved dynamic tiling window manager
00005  * © 2009-2012 Michael Stapelberg and contributors (see also: LICENSE)
00006  *
00007  * handlers.c: Small handlers for various events (keypresses, focus changes,
00008  *             …).
00009  *
00010  */
00011 #include "all.h"
00012 
00013 #include <time.h>
00014 #include <sys/time.h>
00015 #include <xcb/randr.h>
00016 #include <X11/XKBlib.h>
00017 #define SN_API_NOT_YET_FROZEN 1
00018 #include <libsn/sn-monitor.h>
00019 
00020 int randr_base = -1;
00021 
00022 /* After mapping/unmapping windows, a notify event is generated. However, we don’t want it,
00023    since it’d trigger an infinite loop of switching between the different windows when
00024    changing workspaces */
00025 static SLIST_HEAD(ignore_head, Ignore_Event) ignore_events;
00026 
00027 /*
00028  * Adds the given sequence to the list of events which are ignored.
00029  * If this ignore should only affect a specific response_type, pass
00030  * response_type, otherwise, pass -1.
00031  *
00032  * Every ignored sequence number gets garbage collected after 5 seconds.
00033  *
00034  */
00035 void add_ignore_event(const int sequence, const int response_type) {
00036     struct Ignore_Event *event = smalloc(sizeof(struct Ignore_Event));
00037 
00038     event->sequence = sequence;
00039     event->response_type = response_type;
00040     event->added = time(NULL);
00041 
00042     SLIST_INSERT_HEAD(&ignore_events, event, ignore_events);
00043 }
00044 
00045 /*
00046  * Checks if the given sequence is ignored and returns true if so.
00047  *
00048  */
00049 bool event_is_ignored(const int sequence, const int response_type) {
00050     struct Ignore_Event *event;
00051     time_t now = time(NULL);
00052     for (event = SLIST_FIRST(&ignore_events); event != SLIST_END(&ignore_events);) {
00053         if ((now - event->added) > 5) {
00054             struct Ignore_Event *save = event;
00055             event = SLIST_NEXT(event, ignore_events);
00056             SLIST_REMOVE(&ignore_events, save, Ignore_Event, ignore_events);
00057             free(save);
00058         } else event = SLIST_NEXT(event, ignore_events);
00059     }
00060 
00061     SLIST_FOREACH(event, &ignore_events, ignore_events) {
00062         if (event->sequence != sequence)
00063             continue;
00064 
00065         if (event->response_type != -1 &&
00066             event->response_type != response_type)
00067             continue;
00068 
00069         /* instead of removing a sequence number we better wait until it gets
00070          * garbage collected. it may generate multiple events (there are multiple
00071          * enter_notifies for one configure_request, for example). */
00072         //SLIST_REMOVE(&ignore_events, event, Ignore_Event, ignore_events);
00073         //free(event);
00074         return true;
00075     }
00076 
00077     return false;
00078 }
00079 
00080 
00081 /*
00082  * There was a key press. We compare this key code with our bindings table and pass
00083  * the bound action to parse_command().
00084  *
00085  */
00086 static void handle_key_press(xcb_key_press_event_t *event) {
00087 
00088     last_timestamp = event->time;
00089 
00090     DLOG("Keypress %d, state raw = %d\n", event->detail, event->state);
00091 
00092     /* Remove the numlock bit, all other bits are modifiers we can bind to */
00093     uint16_t state_filtered = event->state & ~(xcb_numlock_mask | XCB_MOD_MASK_LOCK);
00094     DLOG("(removed numlock, state = %d)\n", state_filtered);
00095     /* Only use the lower 8 bits of the state (modifier masks) so that mouse
00096      * button masks are filtered out */
00097     state_filtered &= 0xFF;
00098     DLOG("(removed upper 8 bits, state = %d)\n", state_filtered);
00099 
00100     if (xkb_current_group == XkbGroup2Index)
00101         state_filtered |= BIND_MODE_SWITCH;
00102 
00103     DLOG("(checked mode_switch, state %d)\n", state_filtered);
00104 
00105     /* Find the binding */
00106     Binding *bind = get_binding(state_filtered, event->detail);
00107 
00108     /* No match? Then the user has Mode_switch enabled but does not have a
00109      * specific keybinding. Fall back to the default keybindings (without
00110      * Mode_switch). Makes it much more convenient for users of a hybrid
00111      * layout (like us, ru). */
00112     if (bind == NULL) {
00113         state_filtered &= ~(BIND_MODE_SWITCH);
00114         DLOG("no match, new state_filtered = %d\n", state_filtered);
00115         if ((bind = get_binding(state_filtered, event->detail)) == NULL) {
00116             ELOG("Could not lookup key binding (modifiers %d, keycode %d)\n",
00117                  state_filtered, event->detail);
00118             return;
00119         }
00120     }
00121 
00122     struct CommandResult *command_output = parse_command(bind->command);
00123 
00124     if (command_output->needs_tree_render)
00125         tree_render();
00126 
00127     free(command_output->json_output);
00128 }
00129 
00130 /*
00131  * Called with coordinates of an enter_notify event or motion_notify event
00132  * to check if the user crossed virtual screen boundaries and adjust the
00133  * current workspace, if so.
00134  *
00135  */
00136 static void check_crossing_screen_boundary(uint32_t x, uint32_t y) {
00137     Output *output;
00138 
00139     /* If the user disable focus follows mouse, we have nothing to do here */
00140     if (config.disable_focus_follows_mouse)
00141         return;
00142 
00143     if ((output = get_output_containing(x, y)) == NULL) {
00144         ELOG("ERROR: No such screen\n");
00145         return;
00146     }
00147 
00148     if (output->con == NULL) {
00149         ELOG("ERROR: The screen is not recognized by i3 (no container associated)\n");
00150         return;
00151     }
00152 
00153     /* Focus the output on which the user moved his cursor */
00154     Con *old_focused = focused;
00155     Con *next = con_descend_focused(output_get_content(output->con));
00156     /* Since we are switching outputs, this *must* be a different workspace, so
00157      * call workspace_show() */
00158     workspace_show(con_get_workspace(next));
00159     con_focus(next);
00160 
00161     /* If the focus changed, we re-render to get updated decorations */
00162     if (old_focused != focused)
00163         tree_render();
00164 }
00165 
00166 /*
00167  * When the user moves the mouse pointer onto a window, this callback gets called.
00168  *
00169  */
00170 static void handle_enter_notify(xcb_enter_notify_event_t *event) {
00171     Con *con;
00172 
00173     last_timestamp = event->time;
00174 
00175     DLOG("enter_notify for %08x, mode = %d, detail %d, serial %d\n",
00176          event->event, event->mode, event->detail, event->sequence);
00177     DLOG("coordinates %d, %d\n", event->event_x, event->event_y);
00178     if (event->mode != XCB_NOTIFY_MODE_NORMAL) {
00179         DLOG("This was not a normal notify, ignoring\n");
00180         return;
00181     }
00182     /* Some events are not interesting, because they were not generated
00183      * actively by the user, but by reconfiguration of windows */
00184     if (event_is_ignored(event->sequence, XCB_ENTER_NOTIFY)) {
00185         DLOG("Event ignored\n");
00186         return;
00187     }
00188 
00189     bool enter_child = false;
00190     /* Get container by frame or by child window */
00191     if ((con = con_by_frame_id(event->event)) == NULL) {
00192         con = con_by_window_id(event->event);
00193         enter_child = true;
00194     }
00195 
00196     /* If not, then the user moved his cursor to the root window. In that case, we adjust c_ws */
00197     if (con == NULL) {
00198         DLOG("Getting screen at %d x %d\n", event->root_x, event->root_y);
00199         check_crossing_screen_boundary(event->root_x, event->root_y);
00200         return;
00201     }
00202 
00203     if (con->parent->type == CT_DOCKAREA) {
00204         DLOG("Ignoring, this is a dock client\n");
00205         return;
00206     }
00207 
00208     /* see if the user entered the window on a certain window decoration */
00209     int layout = (enter_child ? con->parent->layout : con->layout);
00210     if (layout == L_DEFAULT) {
00211         Con *child;
00212         TAILQ_FOREACH(child, &(con->nodes_head), nodes)
00213             if (rect_contains(child->deco_rect, event->event_x, event->event_y)) {
00214                 LOG("using child %p / %s instead!\n", child, child->name);
00215                 con = child;
00216                 break;
00217             }
00218     }
00219 
00220 #if 0
00221     if (client->workspace != c_ws && client->workspace->output == c_ws->output) {
00222             /* This can happen when a client gets assigned to a different workspace than
00223              * the current one (see src/mainx.c:reparent_window). Shortly after it was created,
00224              * an enter_notify will follow. */
00225             DLOG("enter_notify for a client on a different workspace but the same screen, ignoring\n");
00226             return 1;
00227     }
00228 #endif
00229 
00230     if (config.disable_focus_follows_mouse)
00231         return;
00232 
00233     /* Get the currently focused workspace to check if the focus change also
00234      * involves changing workspaces. If so, we need to call workspace_show() to
00235      * correctly update state and send the IPC event. */
00236     Con *ws = con_get_workspace(con);
00237     if (ws != con_get_workspace(focused))
00238         workspace_show(ws);
00239 
00240     focused_id = XCB_NONE;
00241     con_focus(con_descend_focused(con));
00242     tree_render();
00243 
00244     return;
00245 }
00246 
00247 /*
00248  * When the user moves the mouse but does not change the active window
00249  * (e.g. when having no windows opened but moving mouse on the root screen
00250  * and crossing virtual screen boundaries), this callback gets called.
00251  *
00252  */
00253 static void handle_motion_notify(xcb_motion_notify_event_t *event) {
00254 
00255     last_timestamp = event->time;
00256 
00257     /* Skip events where the pointer was over a child window, we are only
00258      * interested in events on the root window. */
00259     if (event->child != 0)
00260         return;
00261 
00262     Con *con;
00263     if ((con = con_by_frame_id(event->event)) == NULL) {
00264         check_crossing_screen_boundary(event->root_x, event->root_y);
00265         return;
00266     }
00267 
00268     if (config.disable_focus_follows_mouse)
00269         return;
00270 
00271     if (con->layout != L_DEFAULT)
00272         return;
00273 
00274     /* see over which rect the user is */
00275     Con *current;
00276     TAILQ_FOREACH(current, &(con->nodes_head), nodes) {
00277         if (!rect_contains(current->deco_rect, event->event_x, event->event_y))
00278             continue;
00279 
00280         /* We found the rect, let’s see if this window is focused */
00281         if (TAILQ_FIRST(&(con->focus_head)) == current)
00282             return;
00283 
00284         con_focus(current);
00285         x_push_changes(croot);
00286         return;
00287     }
00288 
00289     return;
00290 }
00291 
00292 /*
00293  * Called when the keyboard mapping changes (for example by using Xmodmap),
00294  * we need to update our key bindings then (re-translate symbols).
00295  *
00296  */
00297 static void handle_mapping_notify(xcb_mapping_notify_event_t *event) {
00298     if (event->request != XCB_MAPPING_KEYBOARD &&
00299         event->request != XCB_MAPPING_MODIFIER)
00300         return;
00301 
00302     DLOG("Received mapping_notify for keyboard or modifier mapping, re-grabbing keys\n");
00303     xcb_refresh_keyboard_mapping(keysyms, event);
00304 
00305     xcb_numlock_mask = aio_get_mod_mask_for(XCB_NUM_LOCK, keysyms);
00306 
00307     ungrab_all_keys(conn);
00308     translate_keysyms();
00309     grab_all_keys(conn, false);
00310 
00311     return;
00312 }
00313 
00314 /*
00315  * A new window appeared on the screen (=was mapped), so let’s manage it.
00316  *
00317  */
00318 static void handle_map_request(xcb_map_request_event_t *event) {
00319     xcb_get_window_attributes_cookie_t cookie;
00320 
00321     cookie = xcb_get_window_attributes_unchecked(conn, event->window);
00322 
00323     DLOG("window = 0x%08x, serial is %d.\n", event->window, event->sequence);
00324     add_ignore_event(event->sequence, -1);
00325 
00326     manage_window(event->window, cookie, false);
00327     x_push_changes(croot);
00328     return;
00329 }
00330 
00331 /*
00332  * Configure requests are received when the application wants to resize windows
00333  * on their own.
00334  *
00335  * We generate a synthethic configure notify event to signalize the client its
00336  * "new" position.
00337  *
00338  */
00339 static void handle_configure_request(xcb_configure_request_event_t *event) {
00340     Con *con;
00341 
00342     DLOG("window 0x%08x wants to be at %dx%d with %dx%d\n",
00343         event->window, event->x, event->y, event->width, event->height);
00344 
00345     /* For unmanaged windows, we just execute the configure request. As soon as
00346      * it gets mapped, we will take over anyways. */
00347     if ((con = con_by_window_id(event->window)) == NULL) {
00348         DLOG("Configure request for unmanaged window, can do that.\n");
00349 
00350         uint32_t mask = 0;
00351         uint32_t values[7];
00352         int c = 0;
00353 #define COPY_MASK_MEMBER(mask_member, event_member) do { \
00354         if (event->value_mask & mask_member) { \
00355             mask |= mask_member; \
00356             values[c++] = event->event_member; \
00357         } \
00358 } while (0)
00359 
00360         COPY_MASK_MEMBER(XCB_CONFIG_WINDOW_X, x);
00361         COPY_MASK_MEMBER(XCB_CONFIG_WINDOW_Y, y);
00362         COPY_MASK_MEMBER(XCB_CONFIG_WINDOW_WIDTH, width);
00363         COPY_MASK_MEMBER(XCB_CONFIG_WINDOW_HEIGHT, height);
00364         COPY_MASK_MEMBER(XCB_CONFIG_WINDOW_BORDER_WIDTH, border_width);
00365         COPY_MASK_MEMBER(XCB_CONFIG_WINDOW_SIBLING, sibling);
00366         COPY_MASK_MEMBER(XCB_CONFIG_WINDOW_STACK_MODE, stack_mode);
00367 
00368         xcb_configure_window(conn, event->window, mask, values);
00369         xcb_flush(conn);
00370 
00371         return;
00372     }
00373 
00374     DLOG("Configure request!\n");
00375     if (con_is_floating(con) && con_is_leaf(con)) {
00376         /* find the height for the decorations */
00377         int deco_height = config.font.height + 5;
00378         /* we actually need to apply the size/position changes to the *parent*
00379          * container */
00380         Rect bsr = con_border_style_rect(con);
00381         if (con->border_style == BS_NORMAL) {
00382             bsr.y += deco_height;
00383             bsr.height -= deco_height;
00384         }
00385         Con *floatingcon = con->parent;
00386 
00387         Rect newrect = floatingcon->rect;
00388 
00389         if (event->value_mask & XCB_CONFIG_WINDOW_X) {
00390             newrect.x = event->x + (-1) * bsr.x;
00391             DLOG("proposed x = %d, new x is %d\n", event->x, newrect.x);
00392         }
00393         if (event->value_mask & XCB_CONFIG_WINDOW_Y) {
00394             newrect.y = event->y + (-1) * bsr.y;
00395             DLOG("proposed y = %d, new y is %d\n", event->y, newrect.y);
00396         }
00397         if (event->value_mask & XCB_CONFIG_WINDOW_WIDTH) {
00398             newrect.width = event->width + (-1) * bsr.width;
00399             newrect.width += con->border_width * 2;
00400             DLOG("proposed width = %d, new width is %d (x11 border %d)\n",
00401                  event->width, newrect.width, con->border_width);
00402         }
00403         if (event->value_mask & XCB_CONFIG_WINDOW_HEIGHT) {
00404             newrect.height = event->height + (-1) * bsr.height;
00405             newrect.height += con->border_width * 2;
00406             DLOG("proposed height = %d, new height is %d (x11 border %d)\n",
00407                  event->height, newrect.height, con->border_width);
00408         }
00409 
00410         DLOG("Container is a floating leaf node, will do that.\n");
00411         floating_reposition(floatingcon, newrect);
00412         return;
00413     }
00414 
00415     /* Dock windows can be reconfigured in their height */
00416     if (con->parent && con->parent->type == CT_DOCKAREA) {
00417         DLOG("Dock window, only height reconfiguration allowed\n");
00418         if (event->value_mask & XCB_CONFIG_WINDOW_HEIGHT) {
00419             DLOG("Height given, changing\n");
00420 
00421             con->geometry.height = event->height;
00422             tree_render();
00423         }
00424     }
00425 
00426     fake_absolute_configure_notify(con);
00427 
00428     return;
00429 }
00430 #if 0
00431 
00432 /*
00433  * Configuration notifies are only handled because we need to set up ignore for
00434  * the following enter notify events.
00435  *
00436  */
00437 int handle_configure_event(void *prophs, xcb_connection_t *conn, xcb_configure_notify_event_t *event) {
00438     DLOG("configure_event, sequence %d\n", event->sequence);
00439         /* We ignore this sequence twice because events for child and frame should be ignored */
00440         add_ignore_event(event->sequence);
00441         add_ignore_event(event->sequence);
00442 
00443         return 1;
00444 }
00445 #endif
00446 
00447 /*
00448  * Gets triggered upon a RandR screen change event, that is when the user
00449  * changes the screen configuration in any way (mode, position, …)
00450  *
00451  */
00452 static void handle_screen_change(xcb_generic_event_t *e) {
00453     DLOG("RandR screen change\n");
00454 
00455     randr_query_outputs();
00456 
00457     ipc_send_event("output", I3_IPC_EVENT_OUTPUT, "{\"change\":\"unspecified\"}");
00458 
00459     return;
00460 }
00461 
00462 /*
00463  * Our window decorations were unmapped. That means, the window will be killed
00464  * now, so we better clean up before.
00465  *
00466  */
00467 static void handle_unmap_notify_event(xcb_unmap_notify_event_t *event) {
00468     DLOG("UnmapNotify for 0x%08x (received from 0x%08x), serial %d\n", event->window, event->event, event->sequence);
00469     xcb_get_input_focus_cookie_t cookie;
00470     Con *con = con_by_window_id(event->window);
00471     if (con == NULL) {
00472         /* This could also be an UnmapNotify for the frame. We need to
00473          * decrement the ignore_unmap counter. */
00474         con = con_by_frame_id(event->window);
00475         if (con == NULL) {
00476             LOG("Not a managed window, ignoring UnmapNotify event\n");
00477             return;
00478         }
00479 
00480         if (con->ignore_unmap > 0)
00481             con->ignore_unmap--;
00482         /* See the end of this function. */
00483         cookie = xcb_get_input_focus(conn);
00484         DLOG("ignore_unmap = %d for frame of container %p\n", con->ignore_unmap, con);
00485         goto ignore_end;
00486     }
00487 
00488     /* See the end of this function. */
00489     cookie = xcb_get_input_focus(conn);
00490 
00491     if (con->ignore_unmap > 0) {
00492         DLOG("ignore_unmap = %d, dec\n", con->ignore_unmap);
00493         con->ignore_unmap--;
00494         goto ignore_end;
00495     }
00496 
00497     tree_close(con, DONT_KILL_WINDOW, false, false);
00498     tree_render();
00499     x_push_changes(croot);
00500 
00501 ignore_end:
00502     /* If the client (as opposed to i3) destroyed or unmapped a window, an
00503      * EnterNotify event will follow (indistinguishable from an EnterNotify
00504      * event caused by moving your mouse), causing i3 to set focus to whichever
00505      * window is now visible.
00506      *
00507      * In a complex stacked or tabbed layout (take two v-split containers in a
00508      * tabbed container), when the bottom window in tab2 is closed, the bottom
00509      * window of tab1 is visible instead. X11 will thus send an EnterNotify
00510      * event for the bottom window of tab1, while the focus should be set to
00511      * the remaining window of tab2.
00512      *
00513      * Therefore, we ignore all EnterNotify events which have the same sequence
00514      * as an UnmapNotify event. */
00515     add_ignore_event(event->sequence, XCB_ENTER_NOTIFY);
00516 
00517     /* Since we just ignored the sequence of this UnmapNotify, we want to make
00518      * sure that following events use a different sequence. When putting xterm
00519      * into fullscreen and moving the pointer to a different window, without
00520      * using GetInputFocus, subsequent (legitimate) EnterNotify events arrived
00521      * with the same sequence and thus were ignored (see ticket #609). */
00522     free(xcb_get_input_focus_reply(conn, cookie, NULL));
00523 }
00524 
00525 /*
00526  * A destroy notify event is sent when the window is not unmapped, but
00527  * immediately destroyed (for example when starting a window and immediately
00528  * killing the program which started it).
00529  *
00530  * We just pass on the event to the unmap notify handler (by copying the
00531  * important fields in the event data structure).
00532  *
00533  */
00534 static void handle_destroy_notify_event(xcb_destroy_notify_event_t *event) {
00535     DLOG("destroy notify for 0x%08x, 0x%08x\n", event->event, event->window);
00536 
00537     xcb_unmap_notify_event_t unmap;
00538     unmap.sequence = event->sequence;
00539     unmap.event = event->event;
00540     unmap.window = event->window;
00541 
00542     handle_unmap_notify_event(&unmap);
00543 }
00544 
00545 /*
00546  * Called when a window changes its title
00547  *
00548  */
00549 static bool handle_windowname_change(void *data, xcb_connection_t *conn, uint8_t state,
00550                                 xcb_window_t window, xcb_atom_t atom, xcb_get_property_reply_t *prop) {
00551     Con *con;
00552     if ((con = con_by_window_id(window)) == NULL || con->window == NULL)
00553         return false;
00554 
00555     window_update_name(con->window, prop, false);
00556 
00557     x_push_changes(croot);
00558 
00559     return true;
00560 }
00561 
00562 /*
00563  * Handles legacy window name updates (WM_NAME), see also src/window.c,
00564  * window_update_name_legacy().
00565  *
00566  */
00567 static bool handle_windowname_change_legacy(void *data, xcb_connection_t *conn, uint8_t state,
00568                                 xcb_window_t window, xcb_atom_t atom, xcb_get_property_reply_t *prop) {
00569     Con *con;
00570     if ((con = con_by_window_id(window)) == NULL || con->window == NULL)
00571         return false;
00572 
00573     window_update_name_legacy(con->window, prop, false);
00574 
00575     x_push_changes(croot);
00576 
00577     return true;
00578 }
00579 
00580 /*
00581  * Called when a window changes its WM_WINDOW_ROLE.
00582  *
00583  */
00584 static bool handle_windowrole_change(void *data, xcb_connection_t *conn, uint8_t state,
00585                                      xcb_window_t window, xcb_atom_t atom, xcb_get_property_reply_t *prop) {
00586     Con *con;
00587     if ((con = con_by_window_id(window)) == NULL || con->window == NULL)
00588         return false;
00589 
00590     window_update_role(con->window, prop, false);
00591 
00592     return true;
00593 }
00594 
00595 #if 0
00596 /*
00597  * Updates the client’s WM_CLASS property
00598  *
00599  */
00600 static int handle_windowclass_change(void *data, xcb_connection_t *conn, uint8_t state,
00601                              xcb_window_t window, xcb_atom_t atom, xcb_get_property_reply_t *prop) {
00602     Con *con;
00603     if ((con = con_by_window_id(window)) == NULL || con->window == NULL)
00604         return 1;
00605 
00606     window_update_class(con->window, prop, false);
00607 
00608     return 0;
00609 }
00610 #endif
00611 
00612 /*
00613  * Expose event means we should redraw our windows (= title bar)
00614  *
00615  */
00616 static void handle_expose_event(xcb_expose_event_t *event) {
00617     Con *parent;
00618 
00619     DLOG("window = %08x\n", event->window);
00620 
00621     if ((parent = con_by_frame_id(event->window)) == NULL) {
00622         LOG("expose event for unknown window, ignoring\n");
00623         return;
00624     }
00625 
00626     /* Since we render to our pixmap on every change anyways, expose events
00627      * only tell us that the X server lost (parts of) the window contents. We
00628      * can handle that by copying the appropriate part from our pixmap to the
00629      * window. */
00630     xcb_copy_area(conn, parent->pixmap, parent->frame, parent->pm_gc,
00631                   event->x, event->y, event->x, event->y,
00632                   event->width, event->height);
00633     xcb_flush(conn);
00634 
00635     return;
00636 }
00637 
00638 /*
00639  * Handle client messages (EWMH)
00640  *
00641  */
00642 static void handle_client_message(xcb_client_message_event_t *event) {
00643     /* If this is a startup notification ClientMessage, the library will handle
00644      * it and call our monitor_event() callback. */
00645     if (sn_xcb_display_process_event(sndisplay, (xcb_generic_event_t*)event))
00646         return;
00647 
00648     LOG("ClientMessage for window 0x%08x\n", event->window);
00649     if (event->type == A__NET_WM_STATE) {
00650         if (event->format != 32 || event->data.data32[1] != A__NET_WM_STATE_FULLSCREEN) {
00651             DLOG("atom in clientmessage is %d, fullscreen is %d\n",
00652                     event->data.data32[1], A__NET_WM_STATE_FULLSCREEN);
00653             DLOG("not about fullscreen atom\n");
00654             return;
00655         }
00656 
00657         Con *con = con_by_window_id(event->window);
00658         if (con == NULL) {
00659             DLOG("Could not get window for client message\n");
00660             return;
00661         }
00662 
00663         /* Check if the fullscreen state should be toggled */
00664         if ((con->fullscreen_mode != CF_NONE &&
00665              (event->data.data32[0] == _NET_WM_STATE_REMOVE ||
00666               event->data.data32[0] == _NET_WM_STATE_TOGGLE)) ||
00667             (con->fullscreen_mode == CF_NONE &&
00668              (event->data.data32[0] == _NET_WM_STATE_ADD ||
00669               event->data.data32[0] == _NET_WM_STATE_TOGGLE))) {
00670             DLOG("toggling fullscreen\n");
00671             con_toggle_fullscreen(con, CF_OUTPUT);
00672         }
00673 
00674         tree_render();
00675         x_push_changes(croot);
00676     } else if (event->type == A_I3_SYNC) {
00677         DLOG("i3 sync, yay\n");
00678         xcb_window_t window = event->data.data32[0];
00679         uint32_t rnd = event->data.data32[1];
00680         DLOG("Sending random value %d back to X11 window 0x%08x\n", rnd, window);
00681 
00682         void *reply = scalloc(32);
00683         xcb_client_message_event_t *ev = reply;
00684 
00685         ev->response_type = XCB_CLIENT_MESSAGE;
00686         ev->window = window;
00687         ev->type = A_I3_SYNC;
00688         ev->format = 32;
00689         ev->data.data32[0] = window;
00690         ev->data.data32[1] = rnd;
00691 
00692         xcb_send_event(conn, false, window, XCB_EVENT_MASK_NO_EVENT, (char*)ev);
00693         xcb_flush(conn);
00694         free(reply);
00695     } else {
00696         DLOG("unhandled clientmessage\n");
00697         return;
00698     }
00699 }
00700 
00701 #if 0
00702 int handle_window_type(void *data, xcb_connection_t *conn, uint8_t state, xcb_window_t window,
00703                         xcb_atom_t atom, xcb_get_property_reply_t *property) {
00704         /* TODO: Implement this one. To do this, implement a little test program which sleep(1)s
00705          before changing this property. */
00706         ELOG("_NET_WM_WINDOW_TYPE changed, this is not yet implemented.\n");
00707         return 0;
00708 }
00709 #endif
00710 
00711 /*
00712  * Handles the size hints set by a window, but currently only the part necessary for displaying
00713  * clients proportionally inside their frames (mplayer for example)
00714  *
00715  * See ICCCM 4.1.2.3 for more details
00716  *
00717  */
00718 static bool handle_normal_hints(void *data, xcb_connection_t *conn, uint8_t state, xcb_window_t window,
00719                         xcb_atom_t name, xcb_get_property_reply_t *reply) {
00720     Con *con = con_by_window_id(window);
00721     if (con == NULL) {
00722         DLOG("Received WM_NORMAL_HINTS for unknown client\n");
00723         return false;
00724     }
00725 
00726     xcb_size_hints_t size_hints;
00727 
00728         //CLIENT_LOG(client);
00729 
00730     /* If the hints were already in this event, use them, if not, request them */
00731     if (reply != NULL)
00732         xcb_icccm_get_wm_size_hints_from_reply(&size_hints, reply);
00733     else
00734         xcb_icccm_get_wm_normal_hints_reply(conn, xcb_icccm_get_wm_normal_hints_unchecked(conn, con->window->id), &size_hints, NULL);
00735 
00736     if ((size_hints.flags & XCB_ICCCM_SIZE_HINT_P_MIN_SIZE)) {
00737         // TODO: Minimum size is not yet implemented
00738         DLOG("Minimum size: %d (width) x %d (height)\n", size_hints.min_width, size_hints.min_height);
00739     }
00740 
00741     bool changed = false;
00742     if ((size_hints.flags & XCB_ICCCM_SIZE_HINT_P_RESIZE_INC)) {
00743         if (size_hints.width_inc > 0 && size_hints.width_inc < 0xFFFF)
00744             if (con->width_increment != size_hints.width_inc) {
00745                 con->width_increment = size_hints.width_inc;
00746                 changed = true;
00747             }
00748         if (size_hints.height_inc > 0 && size_hints.height_inc < 0xFFFF)
00749             if (con->height_increment != size_hints.height_inc) {
00750                 con->height_increment = size_hints.height_inc;
00751                 changed = true;
00752             }
00753 
00754         if (changed)
00755             DLOG("resize increments changed\n");
00756     }
00757 
00758     int base_width = 0, base_height = 0;
00759 
00760     /* base_width/height are the desired size of the window.
00761        We check if either the program-specified size or the program-specified
00762        min-size is available */
00763     if (size_hints.flags & XCB_ICCCM_SIZE_HINT_BASE_SIZE) {
00764         base_width = size_hints.base_width;
00765         base_height = size_hints.base_height;
00766     } else if (size_hints.flags & XCB_ICCCM_SIZE_HINT_P_MIN_SIZE) {
00767         /* TODO: is this right? icccm says not */
00768         base_width = size_hints.min_width;
00769         base_height = size_hints.min_height;
00770     }
00771 
00772     if (base_width != con->base_width ||
00773         base_height != con->base_height) {
00774         con->base_width = base_width;
00775         con->base_height = base_height;
00776         DLOG("client's base_height changed to %d\n", base_height);
00777         DLOG("client's base_width changed to %d\n", base_width);
00778         changed = true;
00779     }
00780 
00781     /* If no aspect ratio was set or if it was invalid, we ignore the hints */
00782     if (!(size_hints.flags & XCB_ICCCM_SIZE_HINT_P_ASPECT) ||
00783         (size_hints.min_aspect_num <= 0) ||
00784         (size_hints.min_aspect_den <= 0)) {
00785         goto render_and_return;
00786     }
00787 
00788     /* XXX: do we really use rect here, not window_rect? */
00789     double width = con->rect.width - base_width;
00790     double height = con->rect.height - base_height;
00791     /* Convert numerator/denominator to a double */
00792     double min_aspect = (double)size_hints.min_aspect_num / size_hints.min_aspect_den;
00793     double max_aspect = (double)size_hints.max_aspect_num / size_hints.min_aspect_den;
00794 
00795     DLOG("Aspect ratio set: minimum %f, maximum %f\n", min_aspect, max_aspect);
00796     DLOG("width = %f, height = %f\n", width, height);
00797 
00798     /* Sanity checks, this is user-input, in a way */
00799     if (max_aspect <= 0 || min_aspect <= 0 || height == 0 || (width / height) <= 0)
00800         goto render_and_return;
00801 
00802     /* Check if we need to set proportional_* variables using the correct ratio */
00803     if ((width / height) < min_aspect) {
00804         if (con->proportional_width != width ||
00805             con->proportional_height != (width / min_aspect)) {
00806             con->proportional_width = width;
00807             con->proportional_height = width / min_aspect;
00808             changed = true;
00809         }
00810     } else if ((width / height) > max_aspect) {
00811         if (con->proportional_width != width ||
00812             con->proportional_height != (width / max_aspect)) {
00813             con->proportional_width = width;
00814             con->proportional_height = width / max_aspect;
00815             changed = true;
00816         }
00817     } else goto render_and_return;
00818 
00819 render_and_return:
00820     if (changed)
00821         tree_render();
00822     FREE(reply);
00823     return true;
00824 }
00825 
00826 /*
00827  * Handles the WM_HINTS property for extracting the urgency state of the window.
00828  *
00829  */
00830 static bool handle_hints(void *data, xcb_connection_t *conn, uint8_t state, xcb_window_t window,
00831                   xcb_atom_t name, xcb_get_property_reply_t *reply) {
00832     Con *con = con_by_window_id(window);
00833     if (con == NULL) {
00834         DLOG("Received WM_HINTS for unknown client\n");
00835         return false;
00836     }
00837 
00838     xcb_icccm_wm_hints_t hints;
00839 
00840     if (reply == NULL)
00841         if (!(reply = xcb_get_property_reply(conn, xcb_icccm_get_wm_hints(conn, window), NULL)))
00842             return false;
00843 
00844     if (!xcb_icccm_get_wm_hints_from_reply(&hints, reply))
00845         return false;
00846 
00847     if (!con->urgent && focused == con) {
00848         DLOG("Ignoring urgency flag for current client\n");
00849         con->window->urgent.tv_sec = 0;
00850         con->window->urgent.tv_usec = 0;
00851         goto end;
00852     }
00853 
00854     /* Update the flag on the client directly */
00855     con->urgent = (xcb_icccm_wm_hints_get_urgency(&hints) != 0);
00856     //CLIENT_LOG(con);
00857     if (con->window) {
00858         if (con->urgent) {
00859             gettimeofday(&con->window->urgent, NULL);
00860         } else {
00861             con->window->urgent.tv_sec = 0;
00862             con->window->urgent.tv_usec = 0;
00863         }
00864     }
00865     LOG("Urgency flag changed to %d\n", con->urgent);
00866 
00867     Con *ws;
00868     /* Set the urgency flag on the workspace, if a workspace could be found
00869      * (for dock clients, that is not the case). */
00870     if ((ws = con_get_workspace(con)) != NULL)
00871         workspace_update_urgent_flag(ws);
00872 
00873     tree_render();
00874 
00875 end:
00876     if (con->window)
00877         window_update_hints(con->window, reply);
00878     else free(reply);
00879     return true;
00880 }
00881 
00882 /*
00883  * Handles the transient for hints set by a window, signalizing that this window is a popup window
00884  * for some other window.
00885  *
00886  * See ICCCM 4.1.2.6 for more details
00887  *
00888  */
00889 static bool handle_transient_for(void *data, xcb_connection_t *conn, uint8_t state, xcb_window_t window,
00890                          xcb_atom_t name, xcb_get_property_reply_t *prop) {
00891     Con *con;
00892 
00893     if ((con = con_by_window_id(window)) == NULL || con->window == NULL) {
00894         DLOG("No such window\n");
00895         return false;
00896     }
00897 
00898     if (prop == NULL) {
00899         prop = xcb_get_property_reply(conn, xcb_get_property_unchecked(conn,
00900                                 false, window, XCB_ATOM_WM_TRANSIENT_FOR, XCB_ATOM_WINDOW, 0, 32), NULL);
00901         if (prop == NULL)
00902             return false;
00903     }
00904 
00905     window_update_transient_for(con->window, prop);
00906 
00907     return true;
00908 }
00909 
00910 /*
00911  * Handles changes of the WM_CLIENT_LEADER atom which specifies if this is a
00912  * toolwindow (or similar) and to which window it belongs (logical parent).
00913  *
00914  */
00915 static bool handle_clientleader_change(void *data, xcb_connection_t *conn, uint8_t state, xcb_window_t window,
00916                         xcb_atom_t name, xcb_get_property_reply_t *prop) {
00917     Con *con;
00918     if ((con = con_by_window_id(window)) == NULL || con->window == NULL)
00919         return false;
00920 
00921     if (prop == NULL) {
00922         prop = xcb_get_property_reply(conn, xcb_get_property_unchecked(conn,
00923                                 false, window, A_WM_CLIENT_LEADER, XCB_ATOM_WINDOW, 0, 32), NULL);
00924         if (prop == NULL)
00925             return false;
00926     }
00927 
00928     window_update_leader(con->window, prop);
00929 
00930     return true;
00931 }
00932 
00933 /*
00934  * Handles FocusIn events which are generated by clients (i3’s focus changes
00935  * don’t generate FocusIn events due to a different EventMask) and updates the
00936  * decorations accordingly.
00937  *
00938  */
00939 static void handle_focus_in(xcb_focus_in_event_t *event) {
00940     DLOG("focus change in, for window 0x%08x\n", event->event);
00941     Con *con;
00942     if ((con = con_by_window_id(event->event)) == NULL || con->window == NULL)
00943         return;
00944     DLOG("That is con %p / %s\n", con, con->name);
00945 
00946     if (event->mode == XCB_NOTIFY_MODE_GRAB ||
00947         event->mode == XCB_NOTIFY_MODE_UNGRAB) {
00948         DLOG("FocusIn event for grab/ungrab, ignoring\n");
00949         return;
00950     }
00951 
00952     if (event->detail == XCB_NOTIFY_DETAIL_POINTER) {
00953         DLOG("notify detail is pointer, ignoring this event\n");
00954         return;
00955     }
00956 
00957     if (focused_id == event->event) {
00958         DLOG("focus matches the currently focused window, not doing anything\n");
00959         return;
00960     }
00961 
00962     /* Skip dock clients, they cannot get the i3 focus. */
00963     if (con->parent->type == CT_DOCKAREA) {
00964         DLOG("This is a dock client, not focusing.\n");
00965         return;
00966     }
00967 
00968     DLOG("focus is different, updating decorations\n");
00969 
00970     /* Get the currently focused workspace to check if the focus change also
00971      * involves changing workspaces. If so, we need to call workspace_show() to
00972      * correctly update state and send the IPC event. */
00973     Con *ws = con_get_workspace(con);
00974     if (ws != con_get_workspace(focused))
00975         workspace_show(ws);
00976 
00977     con_focus(con);
00978     /* We update focused_id because we don’t need to set focus again */
00979     focused_id = event->event;
00980     x_push_changes(croot);
00981     return;
00982 }
00983 
00984 /* Returns false if the event could not be processed (e.g. the window could not
00985  * be found), true otherwise */
00986 typedef bool (*cb_property_handler_t)(void *data, xcb_connection_t *c, uint8_t state, xcb_window_t window, xcb_atom_t atom, xcb_get_property_reply_t *property);
00987 
00988 struct property_handler_t {
00989     xcb_atom_t atom;
00990     uint32_t long_len;
00991     cb_property_handler_t cb;
00992 };
00993 
00994 static struct property_handler_t property_handlers[] = {
00995     { 0, 128, handle_windowname_change },
00996     { 0, UINT_MAX, handle_hints },
00997     { 0, 128, handle_windowname_change_legacy },
00998     { 0, UINT_MAX, handle_normal_hints },
00999     { 0, UINT_MAX, handle_clientleader_change },
01000     { 0, UINT_MAX, handle_transient_for },
01001     { 0, 128, handle_windowrole_change }
01002 };
01003 #define NUM_HANDLERS (sizeof(property_handlers) / sizeof(struct property_handler_t))
01004 
01005 /*
01006  * Sets the appropriate atoms for the property handlers after the atoms were
01007  * received from X11
01008  *
01009  */
01010 void property_handlers_init(void) {
01011 
01012     sn_monitor_context_new(sndisplay, conn_screen, startup_monitor_event, NULL, NULL);
01013 
01014     property_handlers[0].atom = A__NET_WM_NAME;
01015     property_handlers[1].atom = XCB_ATOM_WM_HINTS;
01016     property_handlers[2].atom = XCB_ATOM_WM_NAME;
01017     property_handlers[3].atom = XCB_ATOM_WM_NORMAL_HINTS;
01018     property_handlers[4].atom = A_WM_CLIENT_LEADER;
01019     property_handlers[5].atom = XCB_ATOM_WM_TRANSIENT_FOR;
01020     property_handlers[6].atom = A_WM_WINDOW_ROLE;
01021 }
01022 
01023 static void property_notify(uint8_t state, xcb_window_t window, xcb_atom_t atom) {
01024     struct property_handler_t *handler = NULL;
01025     xcb_get_property_reply_t *propr = NULL;
01026 
01027     for (int c = 0; c < sizeof(property_handlers) / sizeof(struct property_handler_t); c++) {
01028         if (property_handlers[c].atom != atom)
01029             continue;
01030 
01031         handler = &property_handlers[c];
01032         break;
01033     }
01034 
01035     if (handler == NULL) {
01036         //DLOG("Unhandled property notify for atom %d (0x%08x)\n", atom, atom);
01037         return;
01038     }
01039 
01040     if (state != XCB_PROPERTY_DELETE) {
01041         xcb_get_property_cookie_t cookie = xcb_get_property(conn, 0, window, atom, XCB_GET_PROPERTY_TYPE_ANY, 0, handler->long_len);
01042         propr = xcb_get_property_reply(conn, cookie, 0);
01043     }
01044 
01045     /* the handler will free() the reply unless it returns false */
01046     if (!handler->cb(NULL, conn, state, window, atom, propr))
01047         FREE(propr);
01048 }
01049 
01050 /*
01051  * Takes an xcb_generic_event_t and calls the appropriate handler, based on the
01052  * event type.
01053  *
01054  */
01055 void handle_event(int type, xcb_generic_event_t *event) {
01056     if (randr_base > -1 &&
01057         type == randr_base + XCB_RANDR_SCREEN_CHANGE_NOTIFY) {
01058         handle_screen_change(event);
01059         return;
01060     }
01061 
01062     switch (type) {
01063         case XCB_KEY_PRESS:
01064             handle_key_press((xcb_key_press_event_t*)event);
01065             break;
01066 
01067         case XCB_BUTTON_PRESS:
01068             handle_button_press((xcb_button_press_event_t*)event);
01069             break;
01070 
01071         case XCB_MAP_REQUEST:
01072             handle_map_request((xcb_map_request_event_t*)event);
01073             break;
01074 
01075         case XCB_UNMAP_NOTIFY:
01076             handle_unmap_notify_event((xcb_unmap_notify_event_t*)event);
01077             break;
01078 
01079         case XCB_DESTROY_NOTIFY:
01080             handle_destroy_notify_event((xcb_destroy_notify_event_t*)event);
01081             break;
01082 
01083         case XCB_EXPOSE:
01084             handle_expose_event((xcb_expose_event_t*)event);
01085             break;
01086 
01087         case XCB_MOTION_NOTIFY:
01088             handle_motion_notify((xcb_motion_notify_event_t*)event);
01089             break;
01090 
01091         /* Enter window = user moved his mouse over the window */
01092         case XCB_ENTER_NOTIFY:
01093             handle_enter_notify((xcb_enter_notify_event_t*)event);
01094             break;
01095 
01096         /* Client message are sent to the root window. The only interesting
01097          * client message for us is _NET_WM_STATE, we honour
01098          * _NET_WM_STATE_FULLSCREEN */
01099         case XCB_CLIENT_MESSAGE:
01100             handle_client_message((xcb_client_message_event_t*)event);
01101             break;
01102 
01103         /* Configure request = window tried to change size on its own */
01104         case XCB_CONFIGURE_REQUEST:
01105             handle_configure_request((xcb_configure_request_event_t*)event);
01106             break;
01107 
01108         /* Mapping notify = keyboard mapping changed (Xmodmap), re-grab bindings */
01109         case XCB_MAPPING_NOTIFY:
01110             handle_mapping_notify((xcb_mapping_notify_event_t*)event);
01111             break;
01112 
01113         case XCB_FOCUS_IN:
01114             handle_focus_in((xcb_focus_in_event_t*)event);
01115             break;
01116 
01117         case XCB_PROPERTY_NOTIFY: {
01118             xcb_property_notify_event_t *e = (xcb_property_notify_event_t*)event;
01119             last_timestamp = e->time;
01120             property_notify(e->state, e->window, e->atom);
01121             break;
01122         }
01123 
01124         default:
01125             //DLOG("Unhandled event of type %d\n", type);
01126             break;
01127     }
01128 }