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