i3
click.c
Go to the documentation of this file.
1/*
2 * vim:ts=4:sw=4:expandtab
3 *
4 * i3 - an improved dynamic tiling window manager
5 * © 2009 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
14typedef enum { CLICK_BORDER = 0,
17
18/*
19 * Finds the correct pair of first/second cons between the resize will take
20 * place according to the passed border position (top, left, right, bottom),
21 * then calls resize_graphical_handler().
22 *
23 */
24static bool tiling_resize_for_border(Con *con, border_t border, xcb_button_press_event_t *event, bool use_threshold) {
25 DLOG("border = %d, con = %p\n", border, con);
26 Con *second = NULL;
27 Con *first = con;
28 direction_t search_direction;
29 switch (border) {
30 case BORDER_LEFT:
31 search_direction = D_LEFT;
32 break;
33 case BORDER_RIGHT:
34 search_direction = D_RIGHT;
35 break;
36 case BORDER_TOP:
37 search_direction = D_UP;
38 break;
39 case BORDER_BOTTOM:
40 search_direction = D_DOWN;
41 break;
42 default:
43 ELOG("BUG: invalid border value %d\n", border);
44 return false;
45 }
46
47 bool res = resize_find_tiling_participants(&first, &second, search_direction, false);
48 if (!res) {
49 DLOG("No second container in this direction found.\n");
50 return false;
51 }
52 if (first->fullscreen_mode != second->fullscreen_mode) {
53 DLOG("Avoiding resize between containers with different fullscreen modes, %d != %d\n", first->fullscreen_mode, second->fullscreen_mode);
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, use_threshold);
70
71 DLOG("After resize handler, rendering\n");
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 */
84static 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 choosing 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, false);
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, false);
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, false);
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, false);
115
116 return false;
117}
118
119/*
120 * Finds out which border was clicked on and calls tiling_resize_for_border().
121 *
122 */
123static bool tiling_resize(Con *con, xcb_button_press_event_t *event, const click_destination_t dest, bool use_threshold) {
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, use_threshold);
131 } else if (dest == CLICK_BORDER) {
132 if (event->event_y >= 0 && event->event_y <= (int32_t)bsr.y &&
133 event->event_x >= (int32_t)bsr.x && event->event_x <= (int32_t)(con->rect.width + bsr.width))
134 return tiling_resize_for_border(con, BORDER_TOP, event, false);
135 }
136 if (event->event_x >= 0 && event->event_x <= (int32_t)bsr.x &&
137 event->event_y >= (int32_t)bsr.y && event->event_y <= (int32_t)(con->rect.height + bsr.height))
138 return tiling_resize_for_border(con, BORDER_LEFT, event, false);
139
140 if (event->event_x >= (int32_t)(con->window_rect.x + con->window_rect.width) &&
141 event->event_y >= (int32_t)bsr.y && event->event_y <= (int32_t)(con->rect.height + bsr.height))
142 return tiling_resize_for_border(con, BORDER_RIGHT, event, false);
143
144 if (event->event_y >= (int32_t)(con->window_rect.y + con->window_rect.height))
145 return tiling_resize_for_border(con, BORDER_BOTTOM, event, false);
146
147 return false;
148}
149
150static void allow_replay_pointer(xcb_timestamp_t time) {
151 xcb_allow_events(conn, XCB_ALLOW_REPLAY_POINTER, time);
152 xcb_flush(conn);
153 tree_render();
154}
155
156/*
157 * Being called by handle_button_press, this function calls the appropriate
158 * functions for resizing/dragging.
159 *
160 */
161static void route_click(Con *con, xcb_button_press_event_t *event, const bool mod_pressed, const click_destination_t dest) {
162 DLOG("--> click properties: mod = %d, destination = %d\n", mod_pressed, dest);
163 DLOG("--> OUTCOME = %p\n", con);
164 DLOG("type = %d, name = %s\n", con->type, con->name);
165
166 /* don’t handle dockarea cons, they must not be focused */
167 if (con->parent->type == CT_DOCKAREA) {
168 allow_replay_pointer(event->time);
169 return;
170 }
171
172 /* if the user has bound an action to this click, it should override the
173 * default behavior. */
174 Binding *bind = get_binding_from_xcb_event((xcb_generic_event_t *)event);
175 if (bind && ((dest == CLICK_DECORATION && !bind->exclude_titlebar) ||
176 (dest == CLICK_INSIDE && bind->whole_window) ||
177 (dest == CLICK_BORDER && bind->border))) {
178 CommandResult *result = run_binding(bind, con);
179
180 /* ASYNC_POINTER eats the event */
181 xcb_allow_events(conn, XCB_ALLOW_ASYNC_POINTER, event->time);
182 xcb_flush(conn);
183
184 command_result_free(result);
185 return;
186 }
187
188 /* There is no default behavior for button release events so we are done. */
189 if (event->response_type == XCB_BUTTON_RELEASE) {
190 allow_replay_pointer(event->time);
191 return;
192 }
193
194 /* Any click in a workspace should focus that workspace. If the
195 * workspace is on another output we need to do a workspace_show in
196 * order for i3bar (and others) to notice the change in workspace. */
197 Con *ws = con_get_workspace(con);
198 Con *focused_workspace = con_get_workspace(focused);
199
200 if (!ws) {
201 ws = TAILQ_FIRST(&(output_get_content(con_get_output(con))->focus_head));
202 if (!ws) {
203 allow_replay_pointer(event->time);
204 return;
205 }
206 }
207
208 /* get the floating con */
209 Con *floatingcon = con_inside_floating(con);
210 const bool proportional = (event->state & XCB_KEY_BUT_MASK_SHIFT) == XCB_KEY_BUT_MASK_SHIFT;
211 const bool in_stacked = (con->parent->layout == L_STACKED || con->parent->layout == L_TABBED);
212 const bool was_focused = focused == con;
213 const bool is_left_click = (event->detail == XCB_BUTTON_CLICK_LEFT);
214 const bool is_right_click = (event->detail == XCB_BUTTON_CLICK_RIGHT);
215 const bool is_left_or_right_click = (is_left_click || is_right_click);
216 const bool is_scroll = (event->detail == XCB_BUTTON_SCROLL_UP ||
217 event->detail == XCB_BUTTON_SCROLL_DOWN ||
218 event->detail == XCB_BUTTON_SCROLL_LEFT ||
219 event->detail == XCB_BUTTON_SCROLL_RIGHT);
220
221 /* 1: see if the user scrolled on the decoration of a stacked/tabbed con */
222 if (in_stacked && dest == CLICK_DECORATION && is_scroll) {
223 DLOG("Scrolling on a window decoration\n");
224 /* Correctly move workspace focus first, see: #5472 */
225 workspace_show(ws);
226
227 /* Use the focused child of the tabbed / stacked container, not the
228 * container the user scrolled on. */
229 Con *current = TAILQ_FIRST(&(con->parent->focus_head));
230 const position_t direction =
231 (event->detail == XCB_BUTTON_SCROLL_UP || event->detail == XCB_BUTTON_SCROLL_LEFT) ? BEFORE : AFTER;
232 Con *next = get_tree_next_sibling(current, direction);
233 con_activate(con_descend_focused(next ? next : current));
234
235 allow_replay_pointer(event->time);
236 return;
237 }
238
239 /* 2: floating modifier pressed, initiate a drag */
240 if (mod_pressed && is_left_click && !floatingcon &&
244 const bool use_threshold = !mod_pressed;
245 tiling_drag(con, event, use_threshold);
246 allow_replay_pointer(event->time);
247 return;
248 }
249
250 /* 3: focus this con or one of its children. */
251 Con *con_to_focus = con;
252 if (in_stacked && dest == CLICK_DECORATION) {
253 /* If the container is a tab/stacked container and the click happened
254 * on a tab, switch to the tab. If the tab contents were already
255 * focused, focus the tab container itself. If the tab container was
256 * already focused, cycle back to focusing the tab contents. */
257 if (was_focused || !con_has_parent(focused, con)) {
258 while (!TAILQ_EMPTY(&(con_to_focus->focus_head))) {
259 con_to_focus = TAILQ_FIRST(&(con_to_focus->focus_head));
260 }
261 }
262 }
263 if (ws != focused_workspace) {
264 workspace_show(ws);
265 }
266 con_activate(con_to_focus);
267
268 /* 4: For floating containers, we also want to raise them on click.
269 * We will skip handling events on floating cons in fullscreen mode */
271 if (floatingcon != NULL && fs != con) {
272 /* 5: floating_modifier plus left mouse button drags */
273 if (mod_pressed && is_left_click) {
274 floating_drag_window(floatingcon, event, false);
275 return;
276 }
277
278 /* 6: resize (floating) if this was a (left or right) click on the
279 * left/right/bottom border, or a right click on the decoration.
280 * also try resizing (tiling) if possible */
281 if (mod_pressed && is_right_click) {
282 DLOG("floating resize due to floatingmodifier\n");
283 floating_resize_window(floatingcon, proportional, event);
284 return;
285 }
286
287 if ((dest == CLICK_BORDER || dest == CLICK_DECORATION) &&
288 is_left_or_right_click) {
289 /* try tiling resize, but continue if it doesn’t work */
290 DLOG("tiling resize with fallback\n");
291 if (tiling_resize(con, event, dest, dest == CLICK_DECORATION && !was_focused)) {
292 allow_replay_pointer(event->time);
293 return;
294 }
295 }
296
297 if (dest == CLICK_DECORATION && is_right_click) {
298 DLOG("floating resize due to decoration right click\n");
299 floating_resize_window(floatingcon, proportional, event);
300 return;
301 }
302
303 if (dest == CLICK_BORDER && is_left_or_right_click) {
304 DLOG("floating resize due to border click\n");
305 floating_resize_window(floatingcon, proportional, event);
306 return;
307 }
308
309 /* 7: dragging, if this was a click on a decoration (which did not lead
310 * to a resize) */
311 if (dest == CLICK_DECORATION && is_left_click) {
312 floating_drag_window(floatingcon, event, !was_focused);
313 return;
314 }
315
316 allow_replay_pointer(event->time);
317 return;
318 }
319
320 /* 8: floating modifier pressed, or click in titlebar, initiate a drag */
321 if (is_left_click &&
324 (mod_pressed || dest == CLICK_DECORATION))) &&
326 allow_replay_pointer(event->time);
327 const bool use_threshold = !mod_pressed;
328 tiling_drag(con, event, use_threshold);
329 return;
330 }
331
332 /* 9: floating modifier pressed, initiate a resize */
333 if (dest == CLICK_INSIDE && mod_pressed && is_right_click) {
334 if (floating_mod_on_tiled_client(con, event)) {
335 return;
336 }
337 /* Avoid propagating events to clients, since the user expects
338 * $mod+click to be handled by i3. */
339 xcb_allow_events(conn, XCB_ALLOW_ASYNC_POINTER, event->time);
340 xcb_flush(conn);
341 return;
342 }
343 /* 10: otherwise, check for border/decoration clicks and resize */
344 if ((dest == CLICK_BORDER || dest == CLICK_DECORATION) &&
345 is_left_or_right_click) {
346 DLOG("Trying to resize (tiling)\n");
347 tiling_resize(con, event, dest, dest == CLICK_DECORATION && !was_focused);
348 }
349
350 allow_replay_pointer(event->time);
351}
352
353/*
354 * The button press X callback. This function determines whether the floating
355 * modifier is pressed and where the user clicked (decoration, border, inside
356 * the window).
357 *
358 * Then, route_click is called on the appropriate con.
359 *
360 */
361void handle_button_press(xcb_button_press_event_t *event) {
362 Con *con;
363 DLOG("Button %d (state %d) %s on window 0x%08x (child 0x%08x) at (%d, %d) (root %d, %d)\n",
364 event->detail, event->state, (event->response_type == XCB_BUTTON_PRESS ? "press" : "release"),
365 event->event, event->child, event->event_x, event->event_y, event->root_x,
366 event->root_y);
367
368 last_timestamp = event->time;
369
370 const uint32_t mod = (config.floating_modifier & 0xFFFF);
371 const bool mod_pressed = (mod != 0 && (event->state & mod) == mod);
372 DLOG("floating_mod = %d, detail = %d\n", mod_pressed, event->detail);
373 if ((con = con_by_window_id(event->event))) {
374 route_click(con, event, mod_pressed, CLICK_INSIDE);
375 return;
376 }
377
378 if (!(con = con_by_frame_id(event->event))) {
379 /* Run bindings on the root window as well, see #2097. We only run it
380 * if --whole-window was set as that's the equivalent for a normal
381 * window. */
382 if (event->event == root) {
383 Binding *bind = get_binding_from_xcb_event((xcb_generic_event_t *)event);
384 if (bind != NULL && bind->whole_window) {
385 CommandResult *result = run_binding(bind, NULL);
386 command_result_free(result);
387 }
388 }
389
390 /* If the root window is clicked, find the relevant output from the
391 * click coordinates and focus the output's active workspace. */
392 if (event->event == root && event->response_type == XCB_BUTTON_PRESS) {
393 Con *output, *ws;
394 TAILQ_FOREACH (output, &(croot->nodes_head), nodes) {
395 if (con_is_internal(output) ||
396 !rect_contains(output->rect, event->event_x, event->event_y))
397 continue;
398
399 ws = TAILQ_FIRST(&(output_get_content(output)->focus_head));
400 if (ws != con_get_workspace(focused)) {
401 workspace_show(ws);
402 tree_render();
403 }
404 return;
405 }
406 return;
407 }
408
409 ELOG("Clicked into unknown window?!\n");
410 xcb_allow_events(conn, XCB_ALLOW_REPLAY_POINTER, event->time);
411 xcb_flush(conn);
412 return;
413 }
414
415 /* Check if the click was on the decoration of a child */
416 if (con->window != NULL) {
417 if (rect_contains(con->deco_rect, event->event_x, event->event_y)) {
418 route_click(con, event, mod_pressed, CLICK_DECORATION);
419 return;
420 }
421 } else {
422 Con *child;
423 TAILQ_FOREACH_REVERSE (child, &(con->nodes_head), nodes_head, nodes) {
424 if (!rect_contains(child->deco_rect, event->event_x, event->event_y))
425 continue;
426
427 route_click(child, event, mod_pressed, CLICK_DECORATION);
428 return;
429 }
430 }
431
432 if (event->child != XCB_NONE) {
433 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");
434 route_click(con, event, mod_pressed, CLICK_INSIDE);
435 return;
436 }
437
438 route_click(con, event, mod_pressed, CLICK_BORDER);
439}
xcb_timestamp_t last_timestamp
The last timestamp we got from X11 (timestamps are included in some events and are used for some thin...
Definition main.c:64
xcb_connection_t * conn
XCB connection and root screen.
Definition main.c:54
xcb_window_t root
Definition main.c:67
void workspace_show(Con *workspace)
Switches to the given workspace.
Definition workspace.c:428
bool rect_contains(Rect rect, uint32_t x, uint32_t y)
Definition util.c:32
struct Con * focused
Definition tree.c:13
struct Con * croot
Definition tree.c:12
void tree_render(void)
Renders the tree, that is rendering all outputs using render_con() and pushing the changes to X11 usi...
Definition tree.c:451
Con * get_tree_next_sibling(Con *con, position_t direction)
Get the previous / next sibling.
Definition tree.c:633
void tiling_drag(Con *con, xcb_button_press_event_t *event, bool use_threshold)
Initiates a mouse drag operation on a tiled window.
bool has_drop_targets(void)
Returns whether there currently are any drop targets.
Definition tiling_drag.c:42
bool resize_find_tiling_participants(Con **current, Con **other, direction_t direction, bool both_sides)
Definition resize.c:70
void resize_graphical_handler(Con *first, Con *second, orientation_t orientation, const xcb_button_press_event_t *event, bool use_threshold)
Definition resize.c:171
Con * output_get_content(Con *output)
Returns the output container below the given output container.
Definition output.c:16
void floating_resize_window(Con *con, const bool proportional, const xcb_button_press_event_t *event)
Called when the user clicked on a floating window while holding the floating_modifier and the right m...
Definition floating.c:688
void floating_drag_window(Con *con, const xcb_button_press_event_t *event, bool use_threshold)
Called when the user clicked on the titlebar of a floating window.
Definition floating.c:590
Config config
Definition config.c:19
Con * con_get_workspace(Con *con)
Gets the workspace container this node is on.
Definition con.c:477
Con * con_by_window_id(xcb_window_t window)
Returns the container with the given client window ID or NULL if no such container exists.
Definition con.c:671
Rect con_border_style_rect(Con *con)
Returns a "relative" Rect which contains the amount of pixels that need to be added to the original R...
Definition con.c:1773
Con * con_get_fullscreen_covering_ws(Con *ws)
Returns the fullscreen node that covers the given workspace if it exists.
Definition con.c:573
Con * con_inside_floating(Con *con)
Checks if the given container is either floating or inside some floating container.
Definition con.c:620
bool con_is_internal(Con *con)
Returns true if the container is internal, such as __i3_scratch.
Definition con.c:588
Con * con_by_frame_id(xcb_window_t frame)
Returns the container with the given frame ID or NULL if no such container exists.
Definition con.c:711
void con_activate(Con *con)
Sets input focus to the given container and raises it to the top.
Definition con.c:287
bool con_has_parent(Con *con, Con *parent)
Checks if the container has the given parent as an actual parent.
Definition con.c:653
Con * con_get_output(Con *con)
Gets the output container (first container with CT_OUTPUT in hierarchy) this node is on.
Definition con.c:463
Con * con_descend_focused(Con *con)
Returns the focused con inside this client, descending the tree as far as possible.
Definition con.c:1590
void command_result_free(CommandResult *result)
Frees a CommandResult.
static bool floating_mod_on_tiled_client(Con *con, xcb_button_press_event_t *event)
Definition click.c:84
static bool tiling_resize_for_border(Con *con, border_t border, xcb_button_press_event_t *event, bool use_threshold)
Definition click.c:24
void handle_button_press(xcb_button_press_event_t *event)
The button press X callback.
Definition click.c:361
static void route_click(Con *con, xcb_button_press_event_t *event, const bool mod_pressed, const click_destination_t dest)
Definition click.c:161
static void allow_replay_pointer(xcb_timestamp_t time)
Definition click.c:150
static bool tiling_resize(Con *con, xcb_button_press_event_t *event, const click_destination_t dest, bool use_threshold)
Definition click.c:123
click_destination_t
Definition click.c:14
@ CLICK_INSIDE
Definition click.c:16
@ CLICK_BORDER
Definition click.c:14
@ CLICK_DECORATION
Definition click.c:15
Binding * get_binding_from_xcb_event(xcb_generic_event_t *event)
Returns a pointer to the Binding that matches the given xcb event or NULL if no such binding exists.
Definition bindings.c:304
CommandResult * run_binding(Binding *bind, Con *con)
Runs the given binding and handles parse errors.
Definition bindings.c:835
@ TILING_DRAG_MODIFIER_OR_TITLEBAR
Definition tiling_drag.h:21
@ TILING_DRAG_TITLEBAR
Definition tiling_drag.h:20
@ TILING_DRAG_MODIFIER
Definition tiling_drag.h:19
#define TAILQ_FOREACH(var, head, field)
Definition queue.h:347
#define TAILQ_FIRST(head)
Definition queue.h:336
#define TAILQ_FOREACH_REVERSE(var, head, headname, field)
Definition queue.h:352
#define TAILQ_EMPTY(head)
Definition queue.h:344
#define XCB_BUTTON_CLICK_LEFT
Mouse buttons.
Definition libi3.h:29
#define XCB_BUTTON_SCROLL_UP
Definition libi3.h:32
#define DLOG(fmt,...)
Definition libi3.h:105
#define ELOG(fmt,...)
Definition libi3.h:100
#define XCB_BUTTON_SCROLL_LEFT
Definition libi3.h:35
#define XCB_BUTTON_SCROLL_RIGHT
Definition libi3.h:36
#define XCB_BUTTON_CLICK_RIGHT
Definition libi3.h:31
#define XCB_BUTTON_SCROLL_DOWN
Definition libi3.h:33
border_t
On which border was the dragging initiated?
Definition floating.h:17
@ BORDER_BOTTOM
Definition floating.h:20
@ BORDER_TOP
Definition floating.h:19
@ BORDER_RIGHT
Definition floating.h:18
@ BORDER_LEFT
Definition floating.h:17
position_t
Definition data.h:63
@ AFTER
Definition data.h:64
@ BEFORE
Definition data.h:63
@ L_STACKED
Definition data.h:107
@ L_TABBED
Definition data.h:108
orientation_t
Definition data.h:60
@ VERT
Definition data.h:62
@ HORIZ
Definition data.h:61
direction_t
Definition data.h:56
@ D_RIGHT
Definition data.h:57
@ D_LEFT
Definition data.h:56
@ D_UP
Definition data.h:58
@ D_DOWN
Definition data.h:59
A struct that contains useful information about the result of a command as a whole (e....
tiling_drag_t tiling_drag
uint32_t floating_modifier
The modifier which needs to be pressed in combination with your mouse buttons to do things with float...
Stores a rectangle, for example the size of a window, the child window etc.
Definition data.h:189
uint32_t height
Definition data.h:193
uint32_t x
Definition data.h:190
uint32_t y
Definition data.h:191
uint32_t width
Definition data.h:192
Holds a keybinding, consisting of a keycode combined with modifiers and the command which is executed...
Definition data.h:310
bool whole_window
If this is true for a mouse binding, the binding should be executed when the button is pressed over a...
Definition data.h:336
bool border
If this is true for a mouse binding, the binding should be executed when the button is pressed over t...
Definition data.h:331
bool exclude_titlebar
If this is true for a mouse binding, the binding should only be executed if the button press was not ...
Definition data.h:340
A 'Con' represents everything from the X11 root window down to a single X11 window.
Definition data.h:647
struct Con * parent
Definition data.h:682
struct Rect deco_rect
Definition data.h:692
enum Con::@18 type
struct Rect rect
Definition data.h:686
layout_t layout
Definition data.h:759
struct Rect window_rect
Definition data.h:689
struct Window * window
Definition data.h:722
char * name
Definition data.h:696
fullscreen_mode_t fullscreen_mode
Definition data.h:738