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