i3
src/click.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  *
00006  * © 2009-2011 Michael Stapelberg and contributors
00007  *
00008  * See file LICENSE for license information.
00009  *
00010  * src/click.c: Contains the handlers for button press (mouse click) events
00011  *              because they are quite large.
00012  *
00013  */
00014 #include <time.h>
00015 #include <math.h>
00016 
00017 #include <xcb/xcb_atom.h>
00018 #include <xcb/xcb_icccm.h>
00019 
00020 #include <X11/XKBlib.h>
00021 
00022 #include "all.h"
00023 
00024 
00025 typedef enum { CLICK_BORDER = 0, CLICK_DECORATION = 1, CLICK_INSIDE = 2 } click_destination_t;
00026 
00027 /*
00028  * Finds the correct pair of first/second cons between the resize will take
00029  * place according to the passed border position (top, left, right, bottom),
00030  * then calls resize_graphical_handler().
00031  *
00032  */
00033 static bool tiling_resize_for_border(Con *con, border_t border, xcb_button_press_event_t *event) {
00034     DLOG("border = %d\n", border);
00035     char way = (border == BORDER_TOP || border == BORDER_LEFT ? 'p' : 'n');
00036     orientation_t orientation = (border == BORDER_TOP || border == BORDER_BOTTOM ? VERT : HORIZ);
00037 
00038     /* look for a parent container with the right orientation */
00039     Con *first = NULL, *second = NULL;
00040     Con *resize_con = con;
00041     while (resize_con->type != CT_WORKSPACE &&
00042            resize_con->type != CT_FLOATING_CON &&
00043            resize_con->parent->orientation != orientation)
00044         resize_con = resize_con->parent;
00045 
00046     if (resize_con->type != CT_WORKSPACE &&
00047         resize_con->type != CT_FLOATING_CON &&
00048         resize_con->parent->orientation == orientation) {
00049         first = resize_con;
00050         second = (way == 'n') ? TAILQ_NEXT(first, nodes) : TAILQ_PREV(first, nodes_head, nodes);
00051         if (second == TAILQ_END(&(first->nodes_head))) {
00052             second = NULL;
00053         }
00054         else if (way == 'p') {
00055             Con *tmp = first;
00056             first = second;
00057             second = tmp;
00058         }
00059     }
00060 
00061     if (first == NULL || second == NULL) {
00062         DLOG("Resize not possible\n");
00063         return false;
00064     }
00065     else {
00066         assert(first != second);
00067         assert(first->parent == second->parent);
00068         resize_graphical_handler(first, second, orientation, event);
00069     }
00070 
00071     DLOG("After resize handler, rendering\n");
00072     tree_render();
00073     return true;
00074 }
00075 
00076 /*
00077  * Called when the user clicks using the floating_modifier, but the client is in
00078  * tiling layout.
00079  *
00080  * Returns false if it does not do anything (that is, the click should be sent
00081  * to the client).
00082  *
00083  */
00084 static bool floating_mod_on_tiled_client(Con *con, xcb_button_press_event_t *event) {
00085     /* The client is in tiling layout. We can still initiate a resize with the
00086      * right mouse button, by chosing the border which is the most near one to
00087      * the position of the mouse pointer */
00088     int to_right = con->rect.width - event->event_x,
00089         to_left = event->event_x,
00090         to_top = event->event_y,
00091         to_bottom = con->rect.height - event->event_y;
00092 
00093     DLOG("click was %d px to the right, %d px to the left, %d px to top, %d px to bottom\n",
00094                     to_right, to_left, to_top, to_bottom);
00095 
00096     if (to_right < to_left &&
00097         to_right < to_top &&
00098         to_right < to_bottom)
00099         return tiling_resize_for_border(con, BORDER_RIGHT, event);
00100 
00101     if (to_left < to_right &&
00102         to_left < to_top &&
00103         to_left < to_bottom)
00104         return tiling_resize_for_border(con, BORDER_LEFT, event);
00105 
00106     if (to_top < to_right &&
00107         to_top < to_left &&
00108         to_top < to_bottom)
00109         return tiling_resize_for_border(con, BORDER_TOP, event);
00110 
00111     if (to_bottom < to_right &&
00112         to_bottom < to_left &&
00113         to_bottom < to_top)
00114         return tiling_resize_for_border(con, BORDER_BOTTOM, event);
00115 
00116     return false;
00117 }
00118 
00119 /*
00120  * Finds out which border was clicked on and calls tiling_resize_for_border().
00121  *
00122  */
00123 static bool tiling_resize(Con *con, xcb_button_press_event_t *event, click_destination_t dest) {
00124     /* check if this was a click on the window border (and on which one) */
00125     Rect bsr = con_border_style_rect(con);
00126     DLOG("BORDER x = %d, y = %d for con %p, window 0x%08x\n",
00127             event->event_x, event->event_y, con, event->event);
00128     DLOG("checks for right >= %d\n", con->window_rect.x + con->window_rect.width);
00129     if (dest == CLICK_DECORATION)
00130         return tiling_resize_for_border(con, BORDER_TOP, event);
00131 
00132     if (event->event_x >= 0 && event->event_x <= bsr.x &&
00133         event->event_y >= bsr.y && event->event_y <= con->rect.height + bsr.height)
00134         return tiling_resize_for_border(con, BORDER_LEFT, event);
00135 
00136     if (event->event_x >= (con->window_rect.x + con->window_rect.width) &&
00137         event->event_y >= bsr.y && event->event_y <= con->rect.height + bsr.height)
00138         return tiling_resize_for_border(con, BORDER_RIGHT, event);
00139 
00140     if (event->event_y >= (con->window_rect.y + con->window_rect.height))
00141         return tiling_resize_for_border(con, BORDER_BOTTOM, event);
00142 
00143     return false;
00144 }
00145 
00146 /*
00147  * Being called by handle_button_press, this function calls the appropriate
00148  * functions for resizing/dragging.
00149  *
00150  */
00151 static int route_click(Con *con, xcb_button_press_event_t *event, bool mod_pressed, click_destination_t dest) {
00152     DLOG("--> click properties: mod = %d, destination = %d\n", mod_pressed, dest);
00153     DLOG("--> OUTCOME = %p\n", con);
00154     DLOG("type = %d, name = %s\n", con->type, con->name);
00155 
00156     /* don’t handle dockarea cons, they must not be focused */
00157     if (con->parent->type == CT_DOCKAREA)
00158         goto done;
00159 
00160     /* get the floating con */
00161     Con *floatingcon = con_inside_floating(con);
00162     const bool proportional = (event->state & BIND_SHIFT);
00163     const bool in_stacked = (con->parent->layout == L_STACKED || con->parent->layout == L_TABBED);
00164 
00165     /* 1: see if the user scrolled on the decoration of a stacked/tabbed con */
00166     if (in_stacked &&
00167         dest == CLICK_DECORATION &&
00168         (event->detail == XCB_BUTTON_INDEX_4 ||
00169          event->detail == XCB_BUTTON_INDEX_5)) {
00170         DLOG("Scrolling on a window decoration\n");
00171         orientation_t orientation = (con->parent->layout == L_STACKED ? VERT : HORIZ);
00172         if (event->detail == XCB_BUTTON_INDEX_4)
00173             tree_next('p', orientation);
00174         else tree_next('n', orientation);
00175         goto done;
00176     }
00177 
00178     /* 2: focus this con */
00179     con_focus(con);
00180 
00181     /* 3: for floating containers, we also want to raise them on click */
00182     if (floatingcon != NULL) {
00183         floating_raise_con(floatingcon);
00184 
00185         /* 4: floating_modifier plus left mouse button drags */
00186         if (mod_pressed && event->detail == 1) {
00187             floating_drag_window(floatingcon, event);
00188             return 1;
00189         }
00190 
00191         /* 5: resize (floating) if this was a click on the left/right/bottom
00192          * border. also try resizing (tiling) if it was a click on the top
00193          * border, but continue if that does not work */
00194         if (mod_pressed && event->detail == 3) {
00195             DLOG("floating resize due to floatingmodifier\n");
00196             floating_resize_window(floatingcon, proportional, event);
00197             return 1;
00198         }
00199 
00200         if (!in_stacked && dest == CLICK_DECORATION) {
00201             /* try tiling resize, but continue if it doesn’t work */
00202             DLOG("tiling resize with fallback\n");
00203             if (tiling_resize(con, event, dest))
00204                 goto done;
00205         }
00206 
00207         if (dest == CLICK_BORDER) {
00208             DLOG("floating resize due to border click\n");
00209             floating_resize_window(floatingcon, proportional, event);
00210             return 1;
00211         }
00212 
00213         /* 6: dragging, if this was a click on a decoration (which did not lead
00214          * to a resize) */
00215         if (!in_stacked && dest == CLICK_DECORATION) {
00216             floating_drag_window(floatingcon, event);
00217             return 1;
00218         }
00219 
00220         goto done;
00221     }
00222 
00223     if (in_stacked) {
00224         /* for stacked/tabbed cons, the resizing applies to the parent
00225          * container */
00226         con = con->parent;
00227     }
00228 
00229     /* 7: floating modifier pressed, initiate a resize */
00230     if (mod_pressed && event->detail == 3) {
00231         if (floating_mod_on_tiled_client(con, event))
00232             return 1;
00233     }
00234     /* 8: otherwise, check for border/decoration clicks and resize */
00235     else if (dest == CLICK_BORDER || dest == CLICK_DECORATION) {
00236         DLOG("Trying to resize (tiling)\n");
00237         tiling_resize(con, event, dest);
00238     }
00239 
00240 done:
00241     xcb_allow_events(conn, XCB_ALLOW_REPLAY_POINTER, event->time);
00242     xcb_flush(conn);
00243     tree_render();
00244     return 0;
00245 }
00246 
00247 /*
00248  * The button press X callback. This function determines whether the floating
00249  * modifier is pressed and where the user clicked (decoration, border, inside
00250  * the window).
00251  *
00252  * Then, route_click is called on the appropriate con.
00253  *
00254  */
00255 int handle_button_press(xcb_button_press_event_t *event) {
00256     Con *con;
00257     DLOG("Button %d pressed on window 0x%08x\n", event->state, event->event);
00258 
00259     const uint32_t mod = config.floating_modifier;
00260     bool mod_pressed = (mod != 0 && (event->state & mod) == mod);
00261     DLOG("floating_mod = %d, detail = %d\n", mod_pressed, event->detail);
00262     if ((con = con_by_window_id(event->event)))
00263         return route_click(con, event, mod_pressed, CLICK_INSIDE);
00264 
00265     if (!(con = con_by_frame_id(event->event))) {
00266         ELOG("Clicked into unknown window?!\n");
00267         xcb_allow_events(conn, XCB_ALLOW_REPLAY_POINTER, event->time);
00268         xcb_flush(conn);
00269         return 0;
00270     }
00271 
00272     /* Check if the click was on the decoration of a child */
00273     Con *child;
00274     TAILQ_FOREACH(child, &(con->nodes_head), nodes) {
00275         if (!rect_contains(child->deco_rect, event->event_x, event->event_y))
00276             continue;
00277 
00278         return route_click(child, event, mod_pressed, CLICK_DECORATION);
00279     }
00280 
00281     return route_click(con, event, mod_pressed, CLICK_BORDER);
00282 }