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