]> git.sur5r.net Git - i3/i3/blob - src/click.c
refactor handlers.{c,h}: declare the handlers static, remove unnecessary parameters
[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->type != CT_FLOATING_CON &&
43            resize_con->parent->orientation != orientation)
44         resize_con = resize_con->parent;
45
46     if (resize_con->type != CT_WORKSPACE &&
47         resize_con->type != CT_FLOATING_CON &&
48         resize_con->parent->orientation == orientation) {
49         first = resize_con;
50         second = (way == 'n') ? TAILQ_NEXT(first, nodes) : TAILQ_PREV(first, nodes_head, nodes);
51         if (second == TAILQ_END(&(first->nodes_head))) {
52             second = NULL;
53         }
54         else if (way == 'p') {
55             Con *tmp = first;
56             first = second;
57             second = tmp;
58         }
59     }
60
61     if (first == NULL || second == NULL) {
62         DLOG("Resize not possible\n");
63         return false;
64     }
65     else {
66         assert(first != second);
67         assert(first->parent == second->parent);
68         resize_graphical_handler(first, second, orientation, event);
69     }
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, 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         return tiling_resize_for_border(con, BORDER_TOP, event);
131
132     if (event->event_x >= 0 && event->event_x <= bsr.x &&
133         event->event_y >= bsr.y && event->event_y <= con->rect.height + bsr.height)
134         return tiling_resize_for_border(con, BORDER_LEFT, event);
135
136     if (event->event_x >= (con->window_rect.x + con->window_rect.width) &&
137         event->event_y >= bsr.y && event->event_y <= con->rect.height + bsr.height)
138         return tiling_resize_for_border(con, BORDER_RIGHT, event);
139
140     if (event->event_y >= (con->window_rect.y + con->window_rect.height))
141         return tiling_resize_for_border(con, BORDER_BOTTOM, event);
142
143     return false;
144 }
145
146 /*
147  * Being called by handle_button_press, this function calls the appropriate
148  * functions for resizing/dragging.
149  *
150  */
151 static int route_click(Con *con, xcb_button_press_event_t *event, bool mod_pressed, click_destination_t dest) {
152     DLOG("--> click properties: mod = %d, destination = %d\n", mod_pressed, dest);
153     DLOG("--> OUTCOME = %p\n", con);
154     DLOG("type = %d, name = %s\n", con->type, con->name);
155
156     /* don’t handle dockarea cons, they must not be focused */
157     if (con->parent->type == CT_DOCKAREA)
158         goto done;
159
160     /* get the floating con */
161     Con *floatingcon = con_inside_floating(con);
162     const bool proportional = (event->state & BIND_SHIFT);
163
164     /* 1: focus this con */
165     con_focus(con);
166
167     const bool in_stacked = (con->parent->layout == L_STACKED || con->parent->layout == L_TABBED);
168
169     /* 2: for floating containers, we also want to raise them on click */
170     if (floatingcon != NULL) {
171         floating_raise_con(floatingcon);
172
173         /* 3: floating_modifier plus left mouse button drags */
174         if (mod_pressed && event->detail == 1) {
175             floating_drag_window(floatingcon, event);
176             return 1;
177         }
178
179         /* 3: resize (floating) if this was a click on the left/right/bottom
180          * border. also try resizing (tiling) if it was a click on the top
181          * border, but continue if that does not work */
182         if (mod_pressed && event->detail == 3) {
183             DLOG("floating resize due to floatingmodifier\n");
184             floating_resize_window(floatingcon, proportional, event);
185             return 1;
186         }
187
188         if (!in_stacked && dest == CLICK_DECORATION) {
189             /* try tiling resize, but continue if it doesn’t work */
190             DLOG("tiling resize with fallback\n");
191             if (tiling_resize(con, event, dest))
192                 goto done;
193         }
194
195         if (dest == CLICK_BORDER) {
196             DLOG("floating resize due to border click\n");
197             floating_resize_window(floatingcon, proportional, event);
198             return 1;
199         }
200
201         /* 4: dragging, if this was a click on a decoration (which did not lead
202          * to a resize) */
203         if (!in_stacked && dest == CLICK_DECORATION) {
204             floating_drag_window(floatingcon, event);
205             return 1;
206         }
207
208         goto done;
209     }
210
211     if (in_stacked) {
212         /* for stacked/tabbed cons, floating modifier + right click resizes the
213          * parent container */
214         if (mod_pressed && event->detail == 3)
215             if (floating_mod_on_tiled_client(con->parent, event))
216                 return 1;
217         goto done;
218     }
219
220     /* 3: floating modifier pressed, initiate a resize */
221     if (mod_pressed && event->detail == 3) {
222         if (floating_mod_on_tiled_client(con, event))
223             return 1;
224     }
225     /* 4: otherwise, check for border/decoration clicks and resize */
226     else if (dest == CLICK_BORDER || dest == CLICK_DECORATION) {
227         DLOG("Should trry resizing (tiling)\n");
228         tiling_resize(con, event, dest);
229     }
230
231 done:
232     xcb_allow_events(conn, XCB_ALLOW_REPLAY_POINTER, event->time);
233     xcb_flush(conn);
234     tree_render();
235     return 0;
236 }
237
238 /*
239  * The button press X callback. This function determines whether the floating
240  * modifier is pressed and where the user clicked (decoration, border, inside
241  * the window).
242  *
243  * Then, route_click is called on the appropriate con.
244  *
245  */
246 int handle_button_press(xcb_button_press_event_t *event) {
247     Con *con;
248     DLOG("Button %d pressed on window 0x%08x\n", event->state, event->event);
249
250     const uint32_t mod = config.floating_modifier;
251     bool mod_pressed = (mod != 0 && (event->state & mod) == mod);
252     DLOG("floating_mod = %d, detail = %d\n", mod_pressed, event->detail);
253     if ((con = con_by_window_id(event->event)))
254         return route_click(con, event, mod_pressed, CLICK_INSIDE);
255
256     if (!(con = con_by_frame_id(event->event))) {
257         ELOG("Clicked into unknown window?!\n");
258         xcb_allow_events(conn, XCB_ALLOW_REPLAY_POINTER, event->time);
259         xcb_flush(conn);
260         return 0;
261     }
262
263     /* Check if the click was on the decoration of a child */
264     Con *child;
265     TAILQ_FOREACH(child, &(con->nodes_head), nodes) {
266         if (!rect_contains(child->deco_rect, event->event_x, event->event_y))
267             continue;
268
269         return route_click(child, event, mod_pressed, CLICK_DECORATION);
270     }
271
272     return route_click(con, event, mod_pressed, CLICK_BORDER);
273 }