• Main Page
  • Data Structures
  • Files
  • File List
  • Globals

src/handlers.c

Go to the documentation of this file.
00001 /*
00002  * vim:ts=8:expandtab
00003  *
00004  * i3 - an improved dynamic tiling window manager
00005  *
00006  * © 2009-2010 Michael Stapelberg and contributors
00007  *
00008  * See file LICENSE for license information.
00009  *
00010  */
00011 #include <stdio.h>
00012 #include <assert.h>
00013 #include <string.h>
00014 #include <stdlib.h>
00015 #include <time.h>
00016 
00017 #include <xcb/xcb.h>
00018 #include <xcb/xcb_atom.h>
00019 #include <xcb/xcb_icccm.h>
00020 #include <xcb/randr.h>
00021 
00022 #include <X11/XKBlib.h>
00023 
00024 #include "i3.h"
00025 #include "debug.h"
00026 #include "table.h"
00027 #include "layout.h"
00028 #include "commands.h"
00029 #include "data.h"
00030 #include "xcb.h"
00031 #include "util.h"
00032 #include "randr.h"
00033 #include "config.h"
00034 #include "queue.h"
00035 #include "resize.h"
00036 #include "client.h"
00037 #include "manage.h"
00038 #include "floating.h"
00039 #include "workspace.h"
00040 #include "log.h"
00041 #include "container.h"
00042 #include "ipc.h"
00043 
00044 /* After mapping/unmapping windows, a notify event is generated. However, we don’t want it,
00045    since it’d trigger an infinite loop of switching between the different windows when
00046    changing workspaces */
00047 static SLIST_HEAD(ignore_head, Ignore_Event) ignore_events;
00048 
00049 static void add_ignore_event(const int sequence) {
00050         struct Ignore_Event *event = smalloc(sizeof(struct Ignore_Event));
00051 
00052         event->sequence = sequence;
00053         event->added = time(NULL);
00054 
00055         SLIST_INSERT_HEAD(&ignore_events, event, ignore_events);
00056 }
00057 
00058 /*
00059  * Checks if the given sequence is ignored and returns true if so.
00060  *
00061  */
00062 static bool event_is_ignored(const int sequence) {
00063         struct Ignore_Event *event;
00064         time_t now = time(NULL);
00065         for (event = SLIST_FIRST(&ignore_events); event != SLIST_END(&ignore_events);) {
00066                 if ((now - event->added) > 5) {
00067                         struct Ignore_Event *save = event;
00068                         event = SLIST_NEXT(event, ignore_events);
00069                         SLIST_REMOVE(&ignore_events, save, Ignore_Event, ignore_events);
00070                         free(save);
00071                 } else event = SLIST_NEXT(event, ignore_events);
00072         }
00073 
00074         SLIST_FOREACH(event, &ignore_events, ignore_events) {
00075                 if (event->sequence == sequence) {
00076                         SLIST_REMOVE(&ignore_events, event, Ignore_Event, ignore_events);
00077                         free(event);
00078                         return true;
00079                 }
00080         }
00081 
00082         return false;
00083 }
00084 
00085 /*
00086  * There was a key press. We compare this key code with our bindings table and pass
00087  * the bound action to parse_command().
00088  *
00089  */
00090 int handle_key_press(void *ignored, xcb_connection_t *conn, xcb_key_press_event_t *event) {
00091         DLOG("Keypress %d, state raw = %d\n", event->detail, event->state);
00092 
00093         /* Remove the numlock bit, all other bits are modifiers we can bind to */
00094         uint16_t state_filtered = event->state & ~(xcb_numlock_mask | XCB_MOD_MASK_LOCK);
00095         DLOG("(removed numlock, state = %d)\n", state_filtered);
00096         /* Only use the lower 8 bits of the state (modifier masks) so that mouse
00097          * button masks are filtered out */
00098         state_filtered &= 0xFF;
00099         DLOG("(removed upper 8 bits, state = %d)\n", state_filtered);
00100 
00101         if (xkb_current_group == XkbGroup2Index)
00102                 state_filtered |= BIND_MODE_SWITCH;
00103 
00104         DLOG("(checked mode_switch, state %d)\n", state_filtered);
00105 
00106         /* Find the binding */
00107         Binding *bind = get_binding(state_filtered, event->detail);
00108 
00109         /* No match? Then the user has Mode_switch enabled but does not have a
00110          * specific keybinding. Fall back to the default keybindings (without
00111          * Mode_switch). Makes it much more convenient for users of a hybrid
00112          * layout (like us, ru). */
00113         if (bind == NULL) {
00114                 state_filtered &= ~(BIND_MODE_SWITCH);
00115                 DLOG("no match, new state_filtered = %d\n", state_filtered);
00116                 if ((bind = get_binding(state_filtered, event->detail)) == NULL) {
00117                         ELOG("Could not lookup key binding (modifiers %d, keycode %d)\n",
00118                              state_filtered, event->detail);
00119                         return 1;
00120                 }
00121         }
00122 
00123         parse_command(conn, bind->command);
00124         return 1;
00125 }
00126 
00127 /*
00128  * Called with coordinates of an enter_notify event or motion_notify event
00129  * to check if the user crossed virtual screen boundaries and adjust the
00130  * current workspace, if so.
00131  *
00132  */
00133 static void check_crossing_screen_boundary(uint32_t x, uint32_t y) {
00134         Output *output;
00135 
00136         if ((output = get_output_containing(x, y)) == NULL) {
00137                 ELOG("ERROR: No such screen\n");
00138                 return;
00139         }
00140         if (output == c_ws->output)
00141                 return;
00142 
00143         c_ws->current_row = current_row;
00144         c_ws->current_col = current_col;
00145         c_ws = output->current_workspace;
00146         current_row = c_ws->current_row;
00147         current_col = c_ws->current_col;
00148         DLOG("We're now on output %p\n", output);
00149 
00150         /* While usually this function is only called when the user switches
00151          * to a different output using his mouse (and thus the output is
00152          * empty), it may be that the following race condition occurs:
00153          * 1) the user actives a new output (say VGA1).
00154          * 2) the cursor is sent to the first pixel of the new VGA1, thus
00155          *    generating an enter_notify for the screen (the enter_notify
00156          *    is not yet received by i3).
00157          * 3) i3 requeries screen configuration and maps a workspace onto the
00158          *    new output.
00159          * 4) the enter_notify event arrives and c_ws is set to the new
00160          *    workspace but the existing windows on the new workspace are not
00161          *    focused.
00162          *
00163          * Therefore, we re-set the focus here to be sure it’s correct. */
00164         Client *first_client = SLIST_FIRST(&(c_ws->focus_stack));
00165         if (first_client != NULL)
00166                 set_focus(global_conn, first_client, true);
00167 }
00168 
00169 /*
00170  * When the user moves the mouse pointer onto a window, this callback gets called.
00171  *
00172  */
00173 int handle_enter_notify(void *ignored, xcb_connection_t *conn, xcb_enter_notify_event_t *event) {
00174         DLOG("enter_notify for %08x, mode = %d, detail %d, serial %d\n", event->event, event->mode, event->detail, event->sequence);
00175         if (event->mode != XCB_NOTIFY_MODE_NORMAL) {
00176                 DLOG("This was not a normal notify, ignoring\n");
00177                 return 1;
00178         }
00179         /* Some events are not interesting, because they were not generated actively by the
00180            user, but by reconfiguration of windows */
00181         if (event_is_ignored(event->sequence))
00182                 return 1;
00183 
00184         /* This was either a focus for a client’s parent (= titlebar)… */
00185         Client *client = table_get(&by_parent, event->event);
00186         /* …or the client itself */
00187         if (client == NULL)
00188                 client = table_get(&by_child, event->event);
00189 
00190         /* Check for stack windows */
00191         if (client == NULL) {
00192                 struct Stack_Window *stack_win;
00193                 SLIST_FOREACH(stack_win, &stack_wins, stack_windows)
00194                         if (stack_win->window == event->event) {
00195                                 client = stack_win->container->currently_focused;
00196                                 break;
00197                         }
00198         }
00199 
00200 
00201         /* If not, then the user moved his cursor to the root window. In that case, we adjust c_ws */
00202         if (client == NULL) {
00203                 DLOG("Getting screen at %d x %d\n", event->root_x, event->root_y);
00204                 check_crossing_screen_boundary(event->root_x, event->root_y);
00205                 return 1;
00206         }
00207 
00208         /* Do plausibility checks: This event may be useless for us if it occurs on a window
00209            which is in a stacked container but not the focused one */
00210         if (client->container != NULL &&
00211             client->container->mode == MODE_STACK &&
00212             client->container->currently_focused != client) {
00213                 DLOG("Plausibility check says: no\n");
00214                 return 1;
00215         }
00216 
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 
00225         if (!config.disable_focus_follows_mouse)
00226                 set_focus(conn, client, false);
00227 
00228         return 1;
00229 }
00230 
00231 /*
00232  * When the user moves the mouse but does not change the active window
00233  * (e.g. when having no windows opened but moving mouse on the root screen
00234  * and crossing virtual screen boundaries), this callback gets called.
00235  *
00236  */
00237 int handle_motion_notify(void *ignored, xcb_connection_t *conn, xcb_motion_notify_event_t *event) {
00238         /* Skip events where the pointer was over a child window, we are only
00239          * interested in events on the root window. */
00240         if (event->child != 0)
00241                 return 1;
00242 
00243         check_crossing_screen_boundary(event->root_x, event->root_y);
00244 
00245         return 1;
00246 }
00247 
00248 /*
00249  * Called when the keyboard mapping changes (for example by using Xmodmap),
00250  * we need to update our key bindings then (re-translate symbols).
00251  *
00252  */
00253 int handle_mapping_notify(void *ignored, xcb_connection_t *conn, xcb_mapping_notify_event_t *event) {
00254         if (event->request != XCB_MAPPING_KEYBOARD &&
00255             event->request != XCB_MAPPING_MODIFIER)
00256                 return 0;
00257 
00258         DLOG("Received mapping_notify for keyboard or modifier mapping, re-grabbing keys\n");
00259         xcb_refresh_keyboard_mapping(keysyms, event);
00260 
00261         xcb_get_numlock_mask(conn);
00262 
00263         ungrab_all_keys(conn);
00264         translate_keysyms();
00265         grab_all_keys(conn, false);
00266 
00267         return 0;
00268 }
00269 
00270 /*
00271  * A new window appeared on the screen (=was mapped), so let’s manage it.
00272  *
00273  */
00274 int handle_map_request(void *prophs, xcb_connection_t *conn, xcb_map_request_event_t *event) {
00275         xcb_get_window_attributes_cookie_t cookie;
00276 
00277         cookie = xcb_get_window_attributes_unchecked(conn, event->window);
00278 
00279         DLOG("window = 0x%08x, serial is %d.\n", event->window, event->sequence);
00280         add_ignore_event(event->sequence);
00281 
00282         manage_window(prophs, conn, event->window, cookie, false);
00283         return 1;
00284 }
00285 
00286 /*
00287  * Configure requests are received when the application wants to resize windows on their own.
00288  *
00289  * We generate a synthethic configure notify event to signalize the client its "new" position.
00290  *
00291  */
00292 int handle_configure_request(void *prophs, xcb_connection_t *conn, xcb_configure_request_event_t *event) {
00293         DLOG("window 0x%08x wants to be at %dx%d with %dx%d\n",
00294             event->window, event->x, event->y, event->width, event->height);
00295 
00296         Client *client = table_get(&by_child, event->window);
00297         if (client == NULL) {
00298                 uint32_t mask = 0;
00299                 uint32_t values[7];
00300                 int c = 0;
00301 #define COPY_MASK_MEMBER(mask_member, event_member) do { \
00302                 if (event->value_mask & mask_member) { \
00303                         mask |= mask_member; \
00304                         values[c++] = event->event_member; \
00305                 } \
00306 } while (0)
00307 
00308                 COPY_MASK_MEMBER(XCB_CONFIG_WINDOW_X, x);
00309                 COPY_MASK_MEMBER(XCB_CONFIG_WINDOW_Y, y);
00310                 COPY_MASK_MEMBER(XCB_CONFIG_WINDOW_WIDTH, width);
00311                 COPY_MASK_MEMBER(XCB_CONFIG_WINDOW_HEIGHT, height);
00312                 COPY_MASK_MEMBER(XCB_CONFIG_WINDOW_BORDER_WIDTH, border_width);
00313                 COPY_MASK_MEMBER(XCB_CONFIG_WINDOW_SIBLING, sibling);
00314                 COPY_MASK_MEMBER(XCB_CONFIG_WINDOW_STACK_MODE, stack_mode);
00315 
00316                 xcb_configure_window(conn, event->window, mask, values);
00317                 xcb_flush(conn);
00318 
00319                 return 1;
00320         }
00321 
00322         if (client->fullscreen) {
00323                 DLOG("Client is in fullscreen mode\n");
00324 
00325                 Rect child_rect = client->workspace->rect;
00326                 child_rect.x = child_rect.y = 0;
00327                 fake_configure_notify(conn, child_rect, client->child);
00328 
00329                 return 1;
00330         }
00331 
00332         /* Floating clients can be reconfigured */
00333         if (client_is_floating(client)) {
00334                 i3Font *font = load_font(conn, config.font);
00335                 int mode = (client->container != NULL ? client->container->mode : MODE_DEFAULT);
00336                 /* TODO: refactor this code. we need a function to translate
00337                  * coordinates of child_rect/rect. */
00338 
00339                 if (event->value_mask & XCB_CONFIG_WINDOW_X) {
00340                         if (mode == MODE_STACK || mode == MODE_TABBED) {
00341                                 client->rect.x = event->x - 2;
00342                         } else {
00343                                 if (client->titlebar_position == TITLEBAR_OFF && client->borderless)
00344                                         client->rect.x = event->x;
00345                                 else if (client->titlebar_position == TITLEBAR_OFF && !client->borderless)
00346                                         client->rect.x = event->x - 1;
00347                                 else client->rect.x = event->x - 2;
00348                         }
00349                 }
00350                 if (event->value_mask & XCB_CONFIG_WINDOW_Y) {
00351                         if (mode == MODE_STACK || mode == MODE_TABBED) {
00352                                 client->rect.y = event->y - 2;
00353                         } else {
00354                                 if (client->titlebar_position == TITLEBAR_OFF && client->borderless)
00355                                         client->rect.y = event->y;
00356                                 else if (client->titlebar_position == TITLEBAR_OFF && !client->borderless)
00357                                         client->rect.y = event->y - 1;
00358                                 else client->rect.y = event->y - font->height - 2 - 2;
00359                         }
00360                 }
00361                 if (event->value_mask & XCB_CONFIG_WINDOW_WIDTH) {
00362                         if (mode == MODE_STACK || mode == MODE_TABBED) {
00363                                 client->rect.width = event->width + 2 + 2;
00364                         } else {
00365                                 if (client->titlebar_position == TITLEBAR_OFF && client->borderless)
00366                                         client->rect.width = event->width;
00367                                 else if (client->titlebar_position == TITLEBAR_OFF && !client->borderless)
00368                                         client->rect.width = event->width + (1 + 1);
00369                                 else client->rect.width = event->width + (2 + 2);
00370                         }
00371                 }
00372                 if (event->value_mask & XCB_CONFIG_WINDOW_HEIGHT) {
00373                         if (mode == MODE_STACK || mode == MODE_TABBED) {
00374                                 client->rect.height = event->height + 2;
00375                         } else {
00376                                 if (client->titlebar_position == TITLEBAR_OFF && client->borderless)
00377                                         client->rect.height = event->height;
00378                                 else if (client->titlebar_position == TITLEBAR_OFF && !client->borderless)
00379                                         client->rect.height = event->height + (1 + 1);
00380                                 else client->rect.height = event->height + (font->height + 2 + 2) + 2;
00381                         }
00382                 }
00383 
00384                 DLOG("Accepted new position/size for floating client: (%d, %d) size %d x %d\n",
00385                     client->rect.x, client->rect.y, client->rect.width, client->rect.height);
00386 
00387                 /* Push the new position/size to X11 */
00388                 reposition_client(conn, client);
00389                 resize_client(conn, client);
00390                 xcb_flush(conn);
00391 
00392                 return 1;
00393         }
00394 
00395         /* Dock clients can be reconfigured in their height */
00396         if (client->dock) {
00397                 DLOG("Reconfiguring height of this dock client\n");
00398 
00399                 if (!(event->value_mask & XCB_CONFIG_WINDOW_HEIGHT)) {
00400                         DLOG("Ignoring configure request, no height given\n");
00401                         return 1;
00402                 }
00403 
00404                 client->desired_height = event->height;
00405                 render_workspace(conn, c_ws->output, c_ws);
00406                 xcb_flush(conn);
00407 
00408                 return 1;
00409         }
00410 
00411         if (client->fullscreen) {
00412                 DLOG("Client is in fullscreen mode\n");
00413 
00414                 Rect child_rect = client->container->workspace->rect;
00415                 child_rect.x = child_rect.y = 0;
00416                 fake_configure_notify(conn, child_rect, client->child);
00417 
00418                 return 1;
00419         }
00420 
00421         fake_absolute_configure_notify(conn, client);
00422 
00423         return 1;
00424 }
00425 
00426 /*
00427  * Configuration notifies are only handled because we need to set up ignore for
00428  * the following enter notify events.
00429  *
00430  */
00431 int handle_configure_event(void *prophs, xcb_connection_t *conn, xcb_configure_notify_event_t *event) {
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 
00439 /*
00440  * Gets triggered upon a RandR screen change event, that is when the user
00441  * changes the screen configuration in any way (mode, position, …)
00442  *
00443  */
00444 int handle_screen_change(void *prophs, xcb_connection_t *conn,
00445                          xcb_generic_event_t *e) {
00446         DLOG("RandR screen change\n");
00447 
00448         randr_query_outputs(conn);
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 now,
00457  * so we better clean up before.
00458  *
00459  */
00460 int handle_unmap_notify_event(void *data, xcb_connection_t *conn, xcb_unmap_notify_event_t *event) {
00461         add_ignore_event(event->sequence);
00462 
00463         Client *client = table_get(&by_child, event->window);
00464         /* First, we need to check if the client is awaiting an unmap-request which
00465            was generated by us reparenting the window. In that case, we just ignore it. */
00466         if (client != NULL && client->awaiting_useless_unmap) {
00467                 client->awaiting_useless_unmap = false;
00468                 return 1;
00469         }
00470 
00471         DLOG("event->window = %08x, event->event = %08x\n", event->window, event->event);
00472         DLOG("UnmapNotify for 0x%08x (received from 0x%08x)\n", event->window, event->event);
00473         if (client == NULL) {
00474                 DLOG("not a managed window. Ignoring.\n");
00475 
00476                 /* This was most likely the destroyed frame of a client which is
00477                  * currently being unmapped, so we add this sequence (again!) to
00478                  * the ignore list (enter_notify events will get sent for both,
00479                  * the child and its frame). */
00480                 add_ignore_event(event->sequence);
00481 
00482                 return 0;
00483         }
00484 
00485         client = table_remove(&by_child, event->window);
00486 
00487         /* If this was the fullscreen client, we need to unset it from all
00488          * workspaces it was on (global fullscreen) */
00489         if (client->fullscreen) {
00490                 Workspace *ws;
00491                 TAILQ_FOREACH(ws, workspaces, workspaces)
00492                         if (ws->fullscreen_client == client)
00493                                 ws->fullscreen_client = NULL;
00494         }
00495 
00496         /* Clients without a container are either floating or dock windows */
00497         if (client->container != NULL) {
00498                 Container *con = client->container;
00499 
00500                 /* Remove the client from the list of clients */
00501                 client_remove_from_container(conn, client, con, true);
00502 
00503                 /* Set focus to the last focused client in this container */
00504                 con->currently_focused = get_last_focused_client(conn, con, NULL);
00505 
00506                 /* Only if this is the active container, we need to really change focus */
00507                 if ((con->currently_focused != NULL) && ((con == CUR_CELL) || client->fullscreen))
00508                         set_focus(conn, con->currently_focused, true);
00509         } else if (client_is_floating(client)) {
00510                 DLOG("Removing from floating clients\n");
00511                 TAILQ_REMOVE(&(client->workspace->floating_clients), client, floating_clients);
00512                 SLIST_REMOVE(&(client->workspace->focus_stack), client, Client, focus_clients);
00513         }
00514 
00515         if (client->dock) {
00516                 DLOG("Removing from dock clients\n");
00517                 SLIST_REMOVE(&(client->workspace->output->dock_clients), client, Client, dock_clients);
00518         }
00519 
00520         DLOG("child of 0x%08x.\n", client->frame);
00521         xcb_reparent_window(conn, client->child, root, 0, 0);
00522 
00523         client_unmap(conn, client);
00524 
00525         xcb_destroy_window(conn, client->frame);
00526         xcb_flush(conn);
00527         table_remove(&by_parent, client->frame);
00528 
00529         if (client->container != NULL) {
00530                 Workspace *workspace = client->container->workspace;
00531                 cleanup_table(conn, workspace);
00532                 fix_colrowspan(conn, workspace);
00533         }
00534 
00535         /* Let’s see how many clients there are left on the workspace to delete it if it’s empty */
00536         bool workspace_empty = SLIST_EMPTY(&(client->workspace->focus_stack));
00537         bool workspace_focused = (c_ws == client->workspace);
00538         Client *to_focus = (!workspace_empty ? SLIST_FIRST(&(client->workspace->focus_stack)) : NULL);
00539 
00540         /* If this workspace is currently visible, we don’t delete it */
00541         if (workspace_is_visible(client->workspace))
00542                 workspace_empty = false;
00543 
00544         if (workspace_empty) {
00545                 client->workspace->output = NULL;
00546                 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"empty\"}");
00547         }
00548 
00549         /* Remove the urgency flag if set */
00550         client->urgent = false;
00551         workspace_update_urgent_flag(client->workspace);
00552 
00553         FREE(client->window_class_instance);
00554         FREE(client->window_class_class);
00555         FREE(client->name);
00556         free(client);
00557 
00558         render_layout(conn);
00559 
00560         /* Ensure the focus is set to the next client in the focus stack or to
00561          * the screen itself (if we do not focus the screen, it can happen that
00562          * the focus is "nowhere" and thus keypress events will not be received
00563          * by i3, thus the user cannot use any hotkeys). */
00564         if (workspace_focused) {
00565                 if (to_focus != NULL)
00566                         set_focus(conn, to_focus, true);
00567                 else {
00568                         DLOG("Restoring focus to root screen\n");
00569                         xcb_set_input_focus(conn, XCB_INPUT_FOCUS_POINTER_ROOT, root, XCB_CURRENT_TIME);
00570                         xcb_flush(conn);
00571                 }
00572         }
00573 
00574         return 1;
00575 }
00576 
00577 /*
00578  * A destroy notify event is sent when the window is not unmapped, but
00579  * immediately destroyed (for example when starting a window and immediately
00580  * killing the program which started it).
00581  *
00582  * We just pass on the event to the unmap notify handler (by copying the
00583  * important fields in the event data structure).
00584  *
00585  */
00586 int handle_destroy_notify_event(void *data, xcb_connection_t *conn, xcb_destroy_notify_event_t *event) {
00587         DLOG("destroy notify for 0x%08x, 0x%08x\n", event->event, event->window);
00588 
00589         xcb_unmap_notify_event_t unmap;
00590         unmap.sequence = event->sequence;
00591         unmap.event = event->event;
00592         unmap.window = event->window;
00593 
00594         return handle_unmap_notify_event(NULL, conn, &unmap);
00595 }
00596 
00597 /*
00598  * Called when a window changes its title
00599  *
00600  */
00601 int handle_windowname_change(void *data, xcb_connection_t *conn, uint8_t state,
00602                                 xcb_window_t window, xcb_atom_t atom, xcb_get_property_reply_t *prop) {
00603         if (prop == NULL || xcb_get_property_value_length(prop) == 0) {
00604                 DLOG("_NET_WM_NAME not specified, not changing\n");
00605                 return 1;
00606         }
00607         Client *client = table_get(&by_child, window);
00608         if (client == NULL)
00609                 return 1;
00610 
00611         /* Save the old pointer to make the update atomic */
00612         char *new_name;
00613         int new_len;
00614         if (asprintf(&new_name, "%.*s", xcb_get_property_value_length(prop), (char*)xcb_get_property_value(prop)) == -1) {
00615                 perror("asprintf");
00616                 LOG("Could not format _NET_WM_NAME, ignoring new hint\n");
00617                 return 1;
00618         }
00619         /* Convert it to UCS-2 here for not having to convert it later every time we want to pass it to X */
00620         char *ucs2_name = convert_utf8_to_ucs2(new_name, &new_len);
00621         LOG("_NET_WM_NAME changed to \"%s\"\n", new_name);
00622         free(new_name);
00623         if (ucs2_name == NULL) {
00624                 LOG("Could not convert _NET_WM_NAME to UCS-2, ignoring new hint\n");
00625                 return 1;
00626         }
00627 
00628         /* Check if they are the same and don’t update if so.
00629            Note the use of new_len * 2 to check all bytes as each glyph takes 2 bytes.
00630            Also note the use of memcmp() instead of strncmp() because the latter stops on nullbytes,
00631            but UCS-2 uses nullbytes to fill up glyphs which only use one byte. */
00632         if ((new_len == client->name_len) &&
00633             (client->name != NULL) &&
00634             (memcmp(client->name, ucs2_name, new_len * 2) == 0)) {
00635                 free(ucs2_name);
00636                 return 1;
00637         }
00638 
00639         char *old_name = client->name;
00640         client->name = ucs2_name;
00641         client->name_len = new_len;
00642         client->uses_net_wm_name = true;
00643 
00644         FREE(old_name);
00645 
00646         /* If the client is a dock window, we don’t need to render anything */
00647         if (client->dock)
00648                 return 1;
00649 
00650         if (!workspace_is_visible(client->workspace))
00651                 return 1;
00652 
00653         int mode = container_mode(client->container, true);
00654         if (mode == MODE_STACK || mode == MODE_TABBED)
00655                 render_container(conn, client->container);
00656         else decorate_window(conn, client, client->frame, client->titlegc, 0, 0);
00657         xcb_flush(conn);
00658 
00659         return 1;
00660 }
00661 
00662 /*
00663  * We handle legacy window names (titles) which are in COMPOUND_TEXT encoding. However, we
00664  * just pass them along, so when containing non-ASCII characters, those will be rendering
00665  * incorrectly. In order to correctly render unicode window titles in i3, an application
00666  * has to set _NET_WM_NAME, which is in UTF-8 encoding.
00667  *
00668  * On every update, a message is put out to the user, so he may improve the situation and
00669  * update applications which display filenames in their title to correctly use
00670  * _NET_WM_NAME and therefore support unicode.
00671  *
00672  */
00673 int handle_windowname_change_legacy(void *data, xcb_connection_t *conn, uint8_t state,
00674                                 xcb_window_t window, xcb_atom_t atom, xcb_get_property_reply_t *prop) {
00675         if (prop == NULL || xcb_get_property_value_length(prop) == 0) {
00676                 DLOG("prop == NULL\n");
00677                 return 1;
00678         }
00679         Client *client = table_get(&by_child, window);
00680         if (client == NULL)
00681                 return 1;
00682 
00683         /* Client capable of _NET_WM_NAME, ignore legacy name changes */
00684         if (client->uses_net_wm_name)
00685                 return 1;
00686 
00687         /* Save the old pointer to make the update atomic */
00688         char *new_name;
00689         if (asprintf(&new_name, "%.*s", xcb_get_property_value_length(prop), (char*)xcb_get_property_value(prop)) == -1) {
00690                 perror("Could not get old name");
00691                 DLOG("Could not get old name\n");
00692                 return 1;
00693         }
00694         /* Convert it to UCS-2 here for not having to convert it later every time we want to pass it to X */
00695         LOG("WM_NAME changed to \"%s\"\n", new_name);
00696 
00697         /* Check if they are the same and don’t update if so. */
00698         if (client->name != NULL &&
00699             strlen(new_name) == strlen(client->name) &&
00700             strcmp(client->name, new_name) == 0) {
00701                 free(new_name);
00702                 return 1;
00703         }
00704 
00705         LOG("Using legacy window title. Note that in order to get Unicode window titles in i3, "
00706             "the application has to set _NET_WM_NAME which is in UTF-8 encoding.\n");
00707 
00708         char *old_name = client->name;
00709         client->name = new_name;
00710         client->name_len = -1;
00711 
00712         if (old_name != NULL)
00713                 free(old_name);
00714 
00715         /* If the client is a dock window, we don’t need to render anything */
00716         if (client->dock)
00717                 return 1;
00718 
00719         if (!workspace_is_visible(client->workspace))
00720                 return 1;
00721 
00722         if (client->container != NULL &&
00723             (client->container->mode == MODE_STACK ||
00724              client->container->mode == MODE_TABBED))
00725                 render_container(conn, client->container);
00726         else decorate_window(conn, client, client->frame, client->titlegc, 0, 0);
00727         xcb_flush(conn);
00728 
00729         return 1;
00730 }
00731 
00732 /*
00733  * Updates the client’s WM_CLASS property
00734  *
00735  */
00736 int handle_windowclass_change(void *data, xcb_connection_t *conn, uint8_t state,
00737                              xcb_window_t window, xcb_atom_t atom, xcb_get_property_reply_t *prop) {
00738         if (prop == NULL || xcb_get_property_value_length(prop) == 0) {
00739                 DLOG("prop == NULL\n");
00740                 return 1;
00741         }
00742         Client *client = table_get(&by_child, window);
00743         if (client == NULL)
00744                 return 1;
00745 
00746         /* We cannot use asprintf here since this property contains two
00747          * null-terminated strings (for compatibility reasons). Instead, we
00748          * use strdup() on both strings */
00749         char *new_class = xcb_get_property_value(prop);
00750 
00751         FREE(client->window_class_instance);
00752         FREE(client->window_class_class);
00753 
00754         client->window_class_instance = strdup(new_class);
00755         if ((strlen(new_class) + 1) < xcb_get_property_value_length(prop))
00756                 client->window_class_class = strdup(new_class + strlen(new_class) + 1);
00757         else client->window_class_class = NULL;
00758         LOG("WM_CLASS changed to %s (instance), %s (class)\n",
00759             client->window_class_instance, client->window_class_class);
00760 
00761         return 0;
00762 }
00763 
00764 /*
00765  * Expose event means we should redraw our windows (= title bar)
00766  *
00767  */
00768 int handle_expose_event(void *data, xcb_connection_t *conn, xcb_expose_event_t *event) {
00769         /* event->count is the number of minimum remaining expose events for this window, so we
00770            skip all events but the last one */
00771         if (event->count != 0)
00772                 return 1;
00773         DLOG("window = %08x\n", event->window);
00774 
00775         Client *client = table_get(&by_parent, event->window);
00776         if (client == NULL) {
00777                 /* There was no client in the table, so this is probably an expose event for
00778                    one of our stack_windows. */
00779                 struct Stack_Window *stack_win;
00780                 SLIST_FOREACH(stack_win, &stack_wins, stack_windows)
00781                         if (stack_win->window == event->window) {
00782                                 render_container(conn, stack_win->container);
00783                                 return 1;
00784                         }
00785 
00786                 /* …or one of the bars? */
00787                 Output *output;
00788                 TAILQ_FOREACH(output, &outputs, outputs)
00789                         if (output->bar == event->window)
00790                                 render_layout(conn);
00791                 return 1;
00792         }
00793 
00794         if (client->dock)
00795                 return 1;
00796 
00797         if (container_mode(client->container, true) == MODE_DEFAULT)
00798                 decorate_window(conn, client, client->frame, client->titlegc, 0, 0);
00799         else {
00800                 uint32_t background_color;
00801                 if (client->urgent)
00802                         background_color = config.client.urgent.background;
00803                 /* Distinguish if the window is currently focused… */
00804                 else if (CUR_CELL != NULL && CUR_CELL->currently_focused == client)
00805                         background_color = config.client.focused.background;
00806                 /* …or if it is the focused window in a not focused container */
00807                 else background_color = config.client.focused_inactive.background;
00808 
00809                 /* Set foreground color to current focused color, line width to 2 */
00810                 uint32_t values[] = {background_color, 2};
00811                 xcb_change_gc(conn, client->titlegc, XCB_GC_FOREGROUND | XCB_GC_LINE_WIDTH, values);
00812 
00813                 /* Draw the border, the ±1 is for line width = 2 */
00814                 xcb_point_t points[] = {{1, 0},                                           /* left upper edge */
00815                                         {1, client->rect.height-1},                       /* left bottom edge */
00816                                         {client->rect.width-1, client->rect.height-1},    /* right bottom edge */
00817                                         {client->rect.width-1, 0}};                       /* right upper edge */
00818                 xcb_poly_line(conn, XCB_COORD_MODE_ORIGIN, client->frame, client->titlegc, 4, points);
00819 
00820                 /* Draw the background */
00821                 xcb_change_gc_single(conn, client->titlegc, XCB_GC_FOREGROUND, config.client.background);
00822                 if (client->titlebar_position == TITLEBAR_OFF && !client->borderless) {
00823                         xcb_rectangle_t crect = {1, 0, client->rect.width - (1 + 1), client->rect.height - 1};
00824                         xcb_poly_fill_rectangle(conn, client->frame, client->titlegc, 1, &crect);
00825                 } else {
00826                         xcb_rectangle_t crect = {2, 0, client->rect.width - (2 + 2), client->rect.height - 2};
00827                         xcb_poly_fill_rectangle(conn, client->frame, client->titlegc, 1, &crect);
00828                 }
00829         }
00830         xcb_flush(conn);
00831         return 1;
00832 }
00833 
00834 /*
00835  * Handle client messages (EWMH)
00836  *
00837  */
00838 int handle_client_message(void *data, xcb_connection_t *conn, xcb_client_message_event_t *event) {
00839         if (event->type == atoms[_NET_WM_STATE]) {
00840                 if (event->format != 32 || event->data.data32[1] != atoms[_NET_WM_STATE_FULLSCREEN])
00841                         return 0;
00842 
00843                 Client *client = table_get(&by_child, event->window);
00844                 if (client == NULL)
00845                         return 0;
00846 
00847                 /* Check if the fullscreen state should be toggled */
00848                 if ((client->fullscreen &&
00849                      (event->data.data32[0] == _NET_WM_STATE_REMOVE ||
00850                       event->data.data32[0] == _NET_WM_STATE_TOGGLE)) ||
00851                     (!client->fullscreen &&
00852                      (event->data.data32[0] == _NET_WM_STATE_ADD ||
00853                       event->data.data32[0] == _NET_WM_STATE_TOGGLE)))
00854                         client_toggle_fullscreen(conn, client);
00855         } else {
00856                 ELOG("unhandled clientmessage\n");
00857                 return 0;
00858         }
00859 
00860         return 1;
00861 }
00862 
00863 int handle_window_type(void *data, xcb_connection_t *conn, uint8_t state, xcb_window_t window,
00864                         xcb_atom_t atom, xcb_get_property_reply_t *property) {
00865         /* TODO: Implement this one. To do this, implement a little test program which sleep(1)s
00866          before changing this property. */
00867         ELOG("_NET_WM_WINDOW_TYPE changed, this is not yet implemented.\n");
00868         return 0;
00869 }
00870 
00871 /*
00872  * Handles the size hints set by a window, but currently only the part necessary for displaying
00873  * clients proportionally inside their frames (mplayer for example)
00874  *
00875  * See ICCCM 4.1.2.3 for more details
00876  *
00877  */
00878 int handle_normal_hints(void *data, xcb_connection_t *conn, uint8_t state, xcb_window_t window,
00879                         xcb_atom_t name, xcb_get_property_reply_t *reply) {
00880         Client *client = table_get(&by_child, window);
00881         if (client == NULL) {
00882                 DLOG("Received WM_SIZE_HINTS for unknown client\n");
00883                 return 1;
00884         }
00885         xcb_size_hints_t size_hints;
00886 
00887         CLIENT_LOG(client);
00888 
00889         /* If the hints were already in this event, use them, if not, request them */
00890         if (reply != NULL)
00891                 xcb_get_wm_size_hints_from_reply(&size_hints, reply);
00892         else
00893                 xcb_get_wm_normal_hints_reply(conn, xcb_get_wm_normal_hints_unchecked(conn, client->child), &size_hints, NULL);
00894 
00895         if ((size_hints.flags & XCB_SIZE_HINT_P_MIN_SIZE)) {
00896                 // TODO: Minimum size is not yet implemented
00897                 DLOG("Minimum size: %d (width) x %d (height)\n", size_hints.min_width, size_hints.min_height);
00898         }
00899 
00900         bool changed = false;
00901         if ((size_hints.flags & XCB_SIZE_HINT_P_RESIZE_INC)) {
00902                 if (size_hints.width_inc > 0 && size_hints.width_inc < 0xFFFF)
00903                         if (client->width_increment != size_hints.width_inc) {
00904                                 client->width_increment = size_hints.width_inc;
00905                                 changed = true;
00906                         }
00907                 if (size_hints.height_inc > 0 && size_hints.height_inc < 0xFFFF)
00908                         if (client->height_increment != size_hints.height_inc) {
00909                                 client->height_increment = size_hints.height_inc;
00910                                 changed = true;
00911                         }
00912 
00913                 if (changed)
00914                         DLOG("resize increments changed\n");
00915         }
00916 
00917         int base_width = 0, base_height = 0;
00918 
00919         /* base_width/height are the desired size of the window.
00920            We check if either the program-specified size or the program-specified
00921            min-size is available */
00922         if (size_hints.flags & XCB_SIZE_HINT_BASE_SIZE) {
00923                 base_width = size_hints.base_width;
00924                 base_height = size_hints.base_height;
00925         } else if (size_hints.flags & XCB_SIZE_HINT_P_MIN_SIZE) {
00926                 /* TODO: is this right? icccm says not */
00927                 base_width = size_hints.min_width;
00928                 base_height = size_hints.min_height;
00929         }
00930 
00931         if (base_width != client->base_width ||
00932             base_height != client->base_height) {
00933                 client->base_width = base_width;
00934                 client->base_height = base_height;
00935                 DLOG("client's base_height changed to %d\n", base_height);
00936                 DLOG("client's base_width changed to %d\n", base_width);
00937                 changed = true;
00938         }
00939 
00940         if (changed) {
00941                 if (client->fullscreen)
00942                         DLOG("Not resizing client, it is in fullscreen mode\n");
00943                 else {
00944                         resize_client(conn, client);
00945                         xcb_flush(conn);
00946                 }
00947         }
00948 
00949         /* If no aspect ratio was set or if it was invalid, we ignore the hints */
00950         if (!(size_hints.flags & XCB_SIZE_HINT_P_ASPECT) ||
00951             (size_hints.min_aspect_num <= 0) ||
00952             (size_hints.min_aspect_den <= 0)) {
00953                 return 1;
00954         }
00955 
00956         double width = client->rect.width - base_width;
00957         double height = client->rect.height - base_height;
00958         /* Convert numerator/denominator to a double */
00959         double min_aspect = (double)size_hints.min_aspect_num / size_hints.min_aspect_den;
00960         double max_aspect = (double)size_hints.max_aspect_num / size_hints.min_aspect_den;
00961 
00962         DLOG("Aspect ratio set: minimum %f, maximum %f\n", min_aspect, max_aspect);
00963         DLOG("width = %f, height = %f\n", width, height);
00964 
00965         /* Sanity checks, this is user-input, in a way */
00966         if (max_aspect <= 0 || min_aspect <= 0 || height == 0 || (width / height) <= 0)
00967                 return 1;
00968 
00969         /* Check if we need to set proportional_* variables using the correct ratio */
00970         if ((width / height) < min_aspect) {
00971                 client->proportional_width = width;
00972                 client->proportional_height = width / min_aspect;
00973         } else if ((width / height) > max_aspect) {
00974                 client->proportional_width = width;
00975                 client->proportional_height = width / max_aspect;
00976         } else return 1;
00977 
00978         client->force_reconfigure = true;
00979 
00980         if (client->container != NULL && workspace_is_visible(client->workspace)) {
00981                 render_container(conn, client->container);
00982                 xcb_flush(conn);
00983         }
00984 
00985         return 1;
00986 }
00987 
00988 /*
00989  * Handles the WM_HINTS property for extracting the urgency state of the window.
00990  *
00991  */
00992 int handle_hints(void *data, xcb_connection_t *conn, uint8_t state, xcb_window_t window,
00993                   xcb_atom_t name, xcb_get_property_reply_t *reply) {
00994         Client *client = table_get(&by_child, window);
00995         if (client == NULL) {
00996                 DLOG("Received WM_HINTS for unknown client\n");
00997                 return 1;
00998         }
00999         xcb_wm_hints_t hints;
01000 
01001         if (reply != NULL) {
01002                 if (!xcb_get_wm_hints_from_reply(&hints, reply))
01003                         return 1;
01004         } else {
01005                 if (!xcb_get_wm_hints_reply(conn, xcb_get_wm_hints_unchecked(conn, client->child), &hints, NULL))
01006                         return 1;
01007         }
01008 
01009         Client *last_focused = SLIST_FIRST(&(c_ws->focus_stack));
01010         if (!client->urgent && client == last_focused) {
01011                 DLOG("Ignoring urgency flag for current client\n");
01012                 return 1;
01013         }
01014 
01015         /* Update the flag on the client directly */
01016         client->urgent = (xcb_wm_hints_get_urgency(&hints) != 0);
01017         CLIENT_LOG(client);
01018         LOG("Urgency flag changed to %d\n", client->urgent);
01019 
01020         workspace_update_urgent_flag(client->workspace);
01021 
01022         /* If the workspace this client is on is not visible, we need to redraw
01023          * the workspace bar */
01024         if (!workspace_is_visible(client->workspace)) {
01025                 Output *output = client->workspace->output;
01026                 render_workspace(conn, output, output->current_workspace);
01027                 xcb_flush(conn);
01028         } else {
01029                 redecorate_window(conn, client);
01030         }
01031 
01032         return 1;
01033 }
01034 
01035 /*
01036  * Handles the transient for hints set by a window, signalizing that this window is a popup window
01037  * for some other window.
01038  *
01039  * See ICCCM 4.1.2.6 for more details
01040  *
01041  */
01042 int handle_transient_for(void *data, xcb_connection_t *conn, uint8_t state, xcb_window_t window,
01043                          xcb_atom_t name, xcb_get_property_reply_t *reply) {
01044         Client *client = table_get(&by_child, window);
01045         if (client == NULL) {
01046                 DLOG("No such client\n");
01047                 return 1;
01048         }
01049 
01050         xcb_window_t transient_for;
01051 
01052         if (reply != NULL) {
01053                 if (!xcb_get_wm_transient_for_from_reply(&transient_for, reply))
01054                         return 1;
01055         } else {
01056                 if (!xcb_get_wm_transient_for_reply(conn, xcb_get_wm_transient_for_unchecked(conn, window),
01057                                                     &transient_for, NULL))
01058                         return 1;
01059         }
01060 
01061         if (client->floating == FLOATING_AUTO_OFF) {
01062                 DLOG("This is a popup window, putting into floating\n");
01063                 toggle_floating_mode(conn, client, true);
01064         }
01065 
01066         return 1;
01067 }
01068 
01069 /*
01070  * Handles changes of the WM_CLIENT_LEADER atom which specifies if this is a
01071  * toolwindow (or similar) and to which window it belongs (logical parent).
01072  *
01073  */
01074 int handle_clientleader_change(void *data, xcb_connection_t *conn, uint8_t state, xcb_window_t window,
01075                         xcb_atom_t name, xcb_get_property_reply_t *prop) {
01076         if (prop == NULL) {
01077                 prop = xcb_get_property_reply(conn, xcb_get_property_unchecked(conn,
01078                                         false, window, WM_CLIENT_LEADER, WINDOW, 0, 32), NULL);
01079                 if (prop == NULL)
01080                         return 1;
01081         }
01082 
01083         Client *client = table_get(&by_child, window);
01084         if (client == NULL)
01085                 return 1;
01086 
01087         xcb_window_t *leader = xcb_get_property_value(prop);
01088         if (leader == NULL)
01089                 return 1;
01090 
01091         DLOG("Client leader changed to %08x\n", *leader);
01092 
01093         client->leader = *leader;
01094 
01095         return 1;
01096 }

Generated on Fri Feb 18 2011 for i3 by  doxygen 1.7.1