]> git.sur5r.net Git - i3/i3/blob - src/click.c
Bugfix: don’t focus dock clients with new click handling code (Thanks mseed)
[i3/i3] / src / click.c
1 /*
2  * vim:ts=4:sw=4:expandtab
3  *
4  * i3 - an improved dynamic tiling window manager
5  *
6  * © 2009-2011 Michael Stapelberg and contributors
7  *
8  * See file LICENSE for license information.
9  *
10  * src/click.c: Contains the handlers for button press (mouse click) events
11  *              because they are quite large.
12  *
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 #include "all.h"
23
24
25 typedef enum { CLICK_BORDER = 0, CLICK_DECORATION = 1, CLICK_INSIDE = 2 } click_destination_t;
26
27 /*
28  * Finds the correct pair of first/second cons between the resize will take
29  * place according to the passed border position (top, left, right, bottom),
30  * then calls resize_graphical_handler().
31  *
32  */
33 static bool tiling_resize_for_border(Con *con, border_t border, xcb_button_press_event_t *event) {
34     DLOG("border = %d\n", border);
35     char way = (border == BORDER_TOP || border == BORDER_LEFT ? 'p' : 'n');
36     orientation_t orientation = (border == BORDER_TOP || border == BORDER_BOTTOM ? VERT : HORIZ);
37
38     /* look for a parent container with the right orientation */
39     Con *first = NULL, *second = NULL;
40     Con *resize_con = con;
41     while (resize_con->type != CT_WORKSPACE &&
42         resize_con->parent->orientation != orientation)
43         resize_con = resize_con->parent;
44     if (resize_con->type != CT_WORKSPACE &&
45         resize_con->parent->orientation == orientation) {
46         first = resize_con;
47         second = (way == 'n') ? TAILQ_NEXT(first, nodes) : TAILQ_PREV(first, nodes_head, nodes);
48         if (second == TAILQ_END(&(first->nodes_head))) {
49             second = NULL;
50         }
51         else if (way == 'p') {
52             Con *tmp = first;
53             first = second;
54             second = tmp;
55         }
56     }
57
58     if (first == NULL || second == NULL) {
59         DLOG("Resize not possible\n");
60         return false;
61     }
62     else {
63         assert(first != second);
64         assert(first->parent == second->parent);
65         resize_graphical_handler(first, second, orientation, event);
66     }
67
68     DLOG("After resize handler, rendering\n");
69     tree_render();
70     return true;
71 }
72
73 /*
74  * Called when the user clicks using the floating_modifier, but the client is in
75  * tiling layout.
76  *
77  * Returns false if it does not do anything (that is, the click should be sent
78  * to the client).
79  *
80  */
81 static bool floating_mod_on_tiled_client(Con *con, xcb_button_press_event_t *event) {
82     /* The client is in tiling layout. We can still initiate a resize with the
83      * right mouse button, by chosing the border which is the most near one to
84      * the position of the mouse pointer */
85     int to_right = con->rect.width - event->event_x,
86         to_left = event->event_x,
87         to_top = event->event_y,
88         to_bottom = con->rect.height - event->event_y;
89
90     DLOG("click was %d px to the right, %d px to the left, %d px to top, %d px to bottom\n",
91                     to_right, to_left, to_top, to_bottom);
92
93     if (to_right < to_left &&
94         to_right < to_top &&
95         to_right < to_bottom)
96         return tiling_resize_for_border(con, BORDER_RIGHT, event);
97
98     if (to_left < to_right &&
99         to_left < to_top &&
100         to_left < to_bottom)
101         return tiling_resize_for_border(con, BORDER_LEFT, event);
102
103     if (to_top < to_right &&
104         to_top < to_left &&
105         to_top < to_bottom)
106         return tiling_resize_for_border(con, BORDER_TOP, event);
107
108     if (to_bottom < to_right &&
109         to_bottom < to_left &&
110         to_bottom < to_top)
111         return tiling_resize_for_border(con, BORDER_BOTTOM, event);
112
113     return false;
114 }
115
116 /*
117  * Finds out which border was clicked on and calls tiling_resize_for_border().
118  *
119  */
120 static bool tiling_resize(Con *con, xcb_button_press_event_t *event, click_destination_t dest) {
121     /* check if this was a click on the window border (and on which one) */
122     Rect bsr = con_border_style_rect(con);
123     DLOG("BORDER x = %d, y = %d for con %p, window 0x%08x\n",
124             event->event_x, event->event_y, con, event->event);
125     DLOG("checks for right >= %d\n", con->window_rect.x + con->window_rect.width);
126     if (dest == CLICK_DECORATION)
127         return tiling_resize_for_border(con, BORDER_TOP, event);
128
129     if (event->event_x >= 0 && event->event_x <= bsr.x &&
130         event->event_y >= bsr.y && event->event_y <= con->rect.height + bsr.height)
131         return tiling_resize_for_border(con, BORDER_LEFT, event);
132
133     if (event->event_x >= (con->window_rect.x + con->window_rect.width) &&
134         event->event_y >= bsr.y && event->event_y <= con->rect.height + bsr.height)
135         return tiling_resize_for_border(con, BORDER_RIGHT, event);
136
137     if (event->event_y >= (con->window_rect.y + con->window_rect.height))
138         return tiling_resize_for_border(con, BORDER_BOTTOM, event);
139
140     return false;
141 }
142
143 /*
144  * Being called by handle_button_press, this function calls the appropriate
145  * functions for resizing/dragging.
146  *
147  */
148 static int route_click(Con *con, xcb_button_press_event_t *event, bool mod_pressed, click_destination_t dest) {
149     DLOG("--> click properties: mod = %d, destination = %d\n", mod_pressed, dest);
150     DLOG("--> OUTCOME = %p\n", con);
151     DLOG("type = %d, name = %s\n", con->type, con->name);
152
153     /* don’t handle dockarea cons, they must not be focused */
154     if (con->parent->type == CT_DOCKAREA)
155         goto done;
156
157     /* get the floating con */
158     Con *floatingcon = con_inside_floating(con);
159     const bool proportional = (event->state & BIND_SHIFT);
160
161     /* 1: focus this con */
162     con_focus(con);
163
164     const bool in_stacked = (con->parent->layout == L_STACKED || con->parent->layout == L_TABBED);
165
166     /* 2: for floating containers, we also want to raise them on click */
167     if (floatingcon != NULL) {
168         floating_raise_con(floatingcon);
169
170         /* 3: floating_modifier plus left mouse button drags */
171         if (mod_pressed && event->detail == 1) {
172             floating_drag_window(floatingcon, event);
173             return 1;
174         }
175
176         /* 3: resize (floating) if this was a click on the left/right/bottom
177          * border. also try resizing (tiling) if it was a click on the top
178          * border, but continue if that does not work */
179         if (mod_pressed && event->detail == 3) {
180             DLOG("floating resize due to floatingmodifier\n");
181             floating_resize_window(floatingcon, proportional, event);
182             return 1;
183         }
184
185         if (!in_stacked && dest == CLICK_DECORATION) {
186             /* try tiling resize, but continue if it doesn’t work */
187             DLOG("tiling resize with fallback\n");
188             if (tiling_resize(con, event, dest))
189                 goto done;
190         }
191
192         if (dest == CLICK_BORDER) {
193             DLOG("floating resize due to border click\n");
194             floating_resize_window(floatingcon, proportional, event);
195             return 1;
196         }
197
198         /* 4: dragging, if this was a click on a decoration (which did not lead
199          * to a resize) */
200         if (!in_stacked && dest == CLICK_DECORATION) {
201             floating_drag_window(floatingcon, event);
202             return 1;
203         }
204
205         goto done;
206     }
207
208     if (in_stacked) {
209         /* for stacked/tabbed cons, floating modifier + right click resizes the
210          * parent container */
211         if (mod_pressed && event->detail == 3)
212             if (floating_mod_on_tiled_client(con->parent, event))
213                 return 1;
214         goto done;
215     }
216
217     /* 3: floating modifier pressed, initiate a resize */
218     if (mod_pressed && event->detail == 3) {
219         if (floating_mod_on_tiled_client(con, event))
220             return 1;
221     }
222     /* 4: otherwise, check for border/decoration clicks and resize */
223     else if (dest == CLICK_BORDER || dest == CLICK_DECORATION) {
224         DLOG("Should trry resizing (tiling)\n");
225         tiling_resize(con, event, dest);
226     }
227
228 done:
229     xcb_allow_events(conn, XCB_ALLOW_REPLAY_POINTER, event->time);
230     xcb_flush(conn);
231     tree_render();
232     return 0;
233 }
234
235 /*
236  * The button press X callback. This function determines whether the floating
237  * modifier is pressed and where the user clicked (decoration, border, inside
238  * the window).
239  *
240  * Then, route_click is called on the appropriate con.
241  *
242  */
243 int handle_button_press(void *ignored, xcb_connection_t *conn, xcb_button_press_event_t *event) {
244     Con *con;
245     DLOG("Button %d pressed on window 0x%08x\n", event->state, event->event);
246
247     const uint32_t mod = config.floating_modifier;
248     bool mod_pressed = (mod != 0 && (event->state & mod) == mod);
249     DLOG("floating_mod = %d, detail = %d\n", mod_pressed, event->detail);
250     if ((con = con_by_window_id(event->event)))
251         return route_click(con, event, mod_pressed, CLICK_INSIDE);
252
253     if (!(con = con_by_frame_id(event->event))) {
254         ELOG("Clicked into unknown window?!\n");
255         xcb_allow_events(conn, XCB_ALLOW_REPLAY_POINTER, event->time);
256         xcb_flush(conn);
257         return 0;
258     }
259
260     /* Check if the click was on the decoration of a child */
261     Con *child;
262     TAILQ_FOREACH(child, &(con->nodes_head), nodes) {
263         if (!rect_contains(child->deco_rect, event->event_x, event->event_y))
264             continue;
265
266         return route_click(child, event, mod_pressed, CLICK_DECORATION);
267     }
268
269     return route_click(con, event, mod_pressed, CLICK_BORDER);
270 }