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