]> git.sur5r.net Git - i3/i3/blob - src/click.c
handle ButtonPress events with child != XCB_NONE (Thanks xeen)
[i3/i3] / src / click.c
1 #undef I3__FILE__
2 #define I3__FILE__ "click.c"
3 /*
4  * vim:ts=4:sw=4:expandtab
5  *
6  * i3 - an improved dynamic tiling window manager
7  * © 2009-2012 Michael Stapelberg and contributors (see also: LICENSE)
8  *
9  * click.c: Button press (mouse click) events.
10  *
11  */
12 #include "all.h"
13
14 #include <time.h>
15 #include <math.h>
16
17 #include <xcb/xcb_icccm.h>
18
19 #include <X11/XKBlib.h>
20
21 typedef enum { CLICK_BORDER = 0, CLICK_DECORATION = 1, CLICK_INSIDE = 2 } click_destination_t;
22
23 /*
24  * Finds the correct pair of first/second cons between the resize will take
25  * place according to the passed border position (top, left, right, bottom),
26  * then calls resize_graphical_handler().
27  *
28  */
29 static bool tiling_resize_for_border(Con *con, border_t border, xcb_button_press_event_t *event) {
30     DLOG("border = %d, con = %p\n", border, con);
31     Con *second = NULL;
32     Con *first = con;
33     direction_t search_direction;
34     switch (border) {
35         case BORDER_LEFT:
36             search_direction = D_LEFT;
37             break;
38         case BORDER_RIGHT:
39             search_direction = D_RIGHT;
40             break;
41         case BORDER_TOP:
42             search_direction = D_UP;
43             break;
44         case BORDER_BOTTOM:
45             search_direction = D_DOWN;
46             break;
47     }
48
49     bool res = resize_find_tiling_participants(&first, &second, search_direction);
50     if (!res) {
51         LOG("No second container in this direction found.\n");
52         return false;
53     }
54
55     assert(first != second);
56     assert(first->parent == second->parent);
57
58     /* The first container should always be in front of the second container */
59     if (search_direction == D_UP || search_direction == D_LEFT) {
60         Con *tmp = first;
61         first = second;
62         second = tmp;
63     }
64
65     const orientation_t orientation = ((border == BORDER_LEFT || border == BORDER_RIGHT) ? HORIZ : VERT);
66
67     resize_graphical_handler(first, second, orientation, event);
68
69     DLOG("After resize handler, rendering\n");
70     tree_render();
71     return true;
72 }
73
74 /*
75  * Called when the user clicks using the floating_modifier, but the client is in
76  * tiling layout.
77  *
78  * Returns false if it does not do anything (that is, the click should be sent
79  * to the client).
80  *
81  */
82 static bool floating_mod_on_tiled_client(Con *con, xcb_button_press_event_t *event) {
83     /* The client is in tiling layout. We can still initiate a resize with the
84      * right mouse button, by chosing the border which is the most near one to
85      * the position of the mouse pointer */
86     int to_right = con->rect.width - event->event_x,
87         to_left = event->event_x,
88         to_top = event->event_y,
89         to_bottom = con->rect.height - event->event_y;
90
91     DLOG("click was %d px to the right, %d px to the left, %d px to top, %d px to bottom\n",
92                     to_right, to_left, to_top, to_bottom);
93
94     if (to_right < to_left &&
95         to_right < to_top &&
96         to_right < to_bottom)
97         return tiling_resize_for_border(con, BORDER_RIGHT, event);
98
99     if (to_left < to_right &&
100         to_left < to_top &&
101         to_left < to_bottom)
102         return tiling_resize_for_border(con, BORDER_LEFT, event);
103
104     if (to_top < to_right &&
105         to_top < to_left &&
106         to_top < to_bottom)
107         return tiling_resize_for_border(con, BORDER_TOP, event);
108
109     if (to_bottom < to_right &&
110         to_bottom < to_left &&
111         to_bottom < to_top)
112         return tiling_resize_for_border(con, BORDER_BOTTOM, event);
113
114     return false;
115 }
116
117 /*
118  * Finds out which border was clicked on and calls tiling_resize_for_border().
119  *
120  */
121 static bool tiling_resize(Con *con, xcb_button_press_event_t *event, const click_destination_t dest) {
122     /* check if this was a click on the window border (and on which one) */
123     Rect bsr = con_border_style_rect(con);
124     DLOG("BORDER x = %d, y = %d for con %p, window 0x%08x\n",
125             event->event_x, event->event_y, con, event->event);
126     DLOG("checks for right >= %d\n", con->window_rect.x + con->window_rect.width);
127     if (dest == CLICK_DECORATION) {
128         /* The user clicked on a window decoration. We ignore the following case:
129          * The container is a h-split, tabbed or stacked container with > 1
130          * window. Decorations will end up next to each other and the user
131          * expects to switch to a window by clicking on its decoration. */
132
133         /* Since the container might either be the child *or* already a split
134          * container (in the case of a nested split container), we need to make
135          * sure that we are dealing with the split container here. */
136         Con *check_con = con;
137         if (con_is_leaf(check_con) && check_con->parent->type == CT_CON)
138             check_con = check_con->parent;
139
140         if ((check_con->layout == L_STACKED ||
141              check_con->layout == L_TABBED ||
142              con_orientation(check_con) == HORIZ) &&
143             con_num_children(check_con) > 1) {
144             DLOG("Not handling this resize, this container has > 1 child.\n");
145             return false;
146         }
147         return tiling_resize_for_border(con, BORDER_TOP, event);
148     }
149
150     if (event->event_x >= 0 && event->event_x <= (int32_t)bsr.x &&
151         event->event_y >= (int32_t)bsr.y && event->event_y <= (int32_t)(con->rect.height + bsr.height))
152         return tiling_resize_for_border(con, BORDER_LEFT, event);
153
154     if (event->event_x >= (int32_t)(con->window_rect.x + con->window_rect.width) &&
155         event->event_y >= (int32_t)bsr.y && event->event_y <= (int32_t)(con->rect.height + bsr.height))
156         return tiling_resize_for_border(con, BORDER_RIGHT, event);
157
158     if (event->event_y >= (int32_t)(con->window_rect.y + con->window_rect.height))
159         return tiling_resize_for_border(con, BORDER_BOTTOM, event);
160
161     return false;
162 }
163
164 /*
165  * Being called by handle_button_press, this function calls the appropriate
166  * functions for resizing/dragging.
167  *
168  */
169 static int route_click(Con *con, xcb_button_press_event_t *event, const bool mod_pressed, const click_destination_t dest) {
170     DLOG("--> click properties: mod = %d, destination = %d\n", mod_pressed, dest);
171     DLOG("--> OUTCOME = %p\n", con);
172     DLOG("type = %d, name = %s\n", con->type, con->name);
173
174     /* Any click in a workspace should focus that workspace. If the
175      * workspace is on another output we need to do a workspace_show in
176      * order for i3bar (and others) to notice the change in workspace. */
177     Con *ws = con_get_workspace(con);
178     Con *focused_workspace = con_get_workspace(focused);
179
180     if (!ws) {
181         ws = TAILQ_FIRST(&(output_get_content(con_get_output(con))->focus_head));
182         if (!ws)
183             goto done;
184     }
185
186     if (ws != focused_workspace)
187         workspace_show(ws);
188     focused_id = XCB_NONE;
189
190     /* don’t handle dockarea cons, they must not be focused */
191     if (con->parent->type == CT_DOCKAREA)
192         goto done;
193
194     /* get the floating con */
195     Con *floatingcon = con_inside_floating(con);
196     const bool proportional = (event->state & BIND_SHIFT);
197     const bool in_stacked = (con->parent->layout == L_STACKED || con->parent->layout == L_TABBED);
198
199     /* 1: see if the user scrolled on the decoration of a stacked/tabbed con */
200     if (in_stacked &&
201         dest == CLICK_DECORATION &&
202         (event->detail == XCB_BUTTON_INDEX_4 ||
203          event->detail == XCB_BUTTON_INDEX_5)) {
204         DLOG("Scrolling on a window decoration\n");
205         orientation_t orientation = (con->parent->layout == L_STACKED ? VERT : HORIZ);
206         /* Focus the currently focused container on the same level that the
207          * user scrolled on. e.g. the tabbed decoration contains
208          * "urxvt | i3: V[xterm geeqie] | firefox",
209          * focus is on the xterm, but the user scrolled on urxvt.
210          * The splitv container will be focused. */
211         Con *focused = con->parent;
212         focused = TAILQ_FIRST(&(focused->focus_head));
213         con_focus(focused);
214         /* To prevent scrolling from going outside the container (see ticket
215          * #557), we first check if scrolling is possible at all. */
216         bool scroll_prev_possible = (TAILQ_PREV(focused, nodes_head, nodes) != NULL);
217         bool scroll_next_possible = (TAILQ_NEXT(focused, nodes) != NULL);
218         if (event->detail == XCB_BUTTON_INDEX_4 && scroll_prev_possible)
219             tree_next('p', orientation);
220         else if (event->detail == XCB_BUTTON_INDEX_5 && scroll_next_possible)
221             tree_next('n', orientation);
222         goto done;
223     }
224
225     /* 2: focus this con. */
226     con_focus(con);
227
228     /* 3: For floating containers, we also want to raise them on click.
229      * We will skip handling events on floating cons in fullscreen mode */
230     Con *fs = (ws ? con_get_fullscreen_con(ws, CF_OUTPUT) : NULL);
231     if (floatingcon != NULL && fs != con) {
232         floating_raise_con(floatingcon);
233
234         /* 4: floating_modifier plus left mouse button drags */
235         if (mod_pressed && event->detail == XCB_BUTTON_INDEX_1) {
236             floating_drag_window(floatingcon, event);
237             return 1;
238         }
239
240         /*  5: resize (floating) if this was a (left or right) click on the
241          * left/right/bottom border, or a right click on the decoration.
242          * also try resizing (tiling) if it was a click on the top */
243         if (mod_pressed && event->detail == 3) {
244             DLOG("floating resize due to floatingmodifier\n");
245             floating_resize_window(floatingcon, proportional, event);
246             return 1;
247         }
248
249         if (!in_stacked && dest == CLICK_DECORATION) {
250             /* try tiling resize, but continue if it doesn’t work */
251             DLOG("tiling resize with fallback\n");
252             if (tiling_resize(con, event, dest))
253                 goto done;
254         }
255
256         if (dest == CLICK_DECORATION && event->detail == 3) {
257             DLOG("floating resize due to decoration right click\n");
258             floating_resize_window(floatingcon, proportional, event);
259             return 1;
260         }
261
262         if (dest == CLICK_BORDER) {
263             DLOG("floating resize due to border click\n");
264             floating_resize_window(floatingcon, proportional, event);
265             return 1;
266         }
267
268         /* 6: dragging, if this was a click on a decoration (which did not lead
269          * to a resize) */
270         if (!in_stacked && dest == CLICK_DECORATION) {
271             floating_drag_window(floatingcon, event);
272             return 1;
273         }
274
275         goto done;
276     }
277
278     if (in_stacked) {
279         /* for stacked/tabbed cons, the resizing applies to the parent
280          * container */
281         con = con->parent;
282     }
283
284     /* 7: floating modifier pressed, initiate a resize */
285     if (dest == CLICK_INSIDE && mod_pressed && event->detail == 3) {
286         if (floating_mod_on_tiled_client(con, event))
287             return 1;
288     }
289     /* 8: otherwise, check for border/decoration clicks and resize */
290     else if ((dest == CLICK_BORDER || dest == CLICK_DECORATION) &&
291              (event->detail == XCB_BUTTON_INDEX_1 ||
292               event->detail == XCB_BUTTON_INDEX_3)) {
293         DLOG("Trying to resize (tiling)\n");
294         tiling_resize(con, event, dest);
295     }
296
297 done:
298     xcb_allow_events(conn, XCB_ALLOW_REPLAY_POINTER, event->time);
299     xcb_flush(conn);
300     tree_render();
301     return 0;
302 }
303
304 /*
305  * The button press X callback. This function determines whether the floating
306  * modifier is pressed and where the user clicked (decoration, border, inside
307  * the window).
308  *
309  * Then, route_click is called on the appropriate con.
310  *
311  */
312 int handle_button_press(xcb_button_press_event_t *event) {
313     Con *con;
314     DLOG("Button %d pressed on window 0x%08x (child 0x%08x) at (%d, %d) (root %d, %d)\n",
315          event->state, event->event, event->child, event->event_x, event->event_y,
316          event->root_x, event->root_y);
317
318     last_timestamp = event->time;
319
320     const uint32_t mod = config.floating_modifier;
321     const bool mod_pressed = (mod != 0 && (event->state & mod) == mod);
322     DLOG("floating_mod = %d, detail = %d\n", mod_pressed, event->detail);
323     if ((con = con_by_window_id(event->event)))
324         return route_click(con, event, mod_pressed, CLICK_INSIDE);
325
326     if (!(con = con_by_frame_id(event->event))) {
327         /* If the root window is clicked, find the relevant output from the
328          * click coordinates and focus the output's active workspace. */
329         if (event->event == root) {
330             Con *output, *ws;
331             TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
332                 if (con_is_internal(output) ||
333                     !rect_contains(output->rect, event->event_x, event->event_y))
334                     continue;
335
336                 ws = TAILQ_FIRST(&(output_get_content(output)->focus_head));
337                 if (ws != con_get_workspace(focused)) {
338                     workspace_show(ws);
339                     tree_render();
340                 }
341                 return 1;
342             }
343             return 0;
344         }
345
346         ELOG("Clicked into unknown window?!\n");
347         xcb_allow_events(conn, XCB_ALLOW_REPLAY_POINTER, event->time);
348         xcb_flush(conn);
349         return 0;
350     }
351
352     if (event->child != XCB_NONE) {
353         DLOG("event->child not XCB_NONE, so this is an event which originated from a click into the application, but the application did not handle it.\n");
354         return route_click(con, event, mod_pressed, CLICK_INSIDE);
355     }
356
357     /* Check if the click was on the decoration of a child */
358     Con *child;
359     TAILQ_FOREACH(child, &(con->nodes_head), nodes) {
360         if (!rect_contains(child->deco_rect, event->event_x, event->event_y))
361             continue;
362
363         return route_click(child, event, mod_pressed, CLICK_DECORATION);
364     }
365
366     return route_click(con, event, mod_pressed, CLICK_BORDER);
367 }