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 
14 typedef 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  */
24 static 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  }
43 
44  bool res = resize_find_tiling_participants(&first, &second, search_direction, false);
45  if (!res) {
46  DLOG("No second container in this direction found.\n");
47  return false;
48  }
49  if (first->fullscreen_mode != second->fullscreen_mode) {
50  DLOG("Avoiding resize between containers with different fullscreen modes, %d != %d\n", first->fullscreen_mode, second->fullscreen_mode);
51  return false;
52  }
53 
54  assert(first != second);
55  assert(first->parent == second->parent);
56 
57  /* The first container should always be in front of the second container */
58  if (search_direction == D_UP || search_direction == D_LEFT) {
59  Con *tmp = first;
60  first = second;
61  second = tmp;
62  }
63 
64  const orientation_t orientation = ((border == BORDER_LEFT || border == BORDER_RIGHT) ? HORIZ : VERT);
65 
66  resize_graphical_handler(first, second, orientation, event, use_threshold);
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, false);
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, false);
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, false);
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, false);
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, const click_destination_t dest, bool use_threshold) {
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, use_threshold);
128  }
129 
130  if (event->event_x >= 0 && event->event_x <= (int32_t)bsr.x &&
131  event->event_y >= (int32_t)bsr.y && event->event_y <= (int32_t)(con->rect.height + bsr.height))
132  return tiling_resize_for_border(con, BORDER_LEFT, event, false);
133 
134  if (event->event_x >= (int32_t)(con->window_rect.x + con->window_rect.width) &&
135  event->event_y >= (int32_t)bsr.y && event->event_y <= (int32_t)(con->rect.height + bsr.height))
136  return tiling_resize_for_border(con, BORDER_RIGHT, event, false);
137 
138  if (event->event_y >= (int32_t)(con->window_rect.y + con->window_rect.height))
139  return tiling_resize_for_border(con, BORDER_BOTTOM, event, false);
140 
141  return false;
142 }
143 
144 /*
145  * Being called by handle_button_press, this function calls the appropriate
146  * functions for resizing/dragging.
147  *
148  */
149 static void route_click(Con *con, xcb_button_press_event_t *event, const bool mod_pressed, const click_destination_t dest) {
150  DLOG("--> click properties: mod = %d, destination = %d\n", mod_pressed, dest);
151  DLOG("--> OUTCOME = %p\n", con);
152  DLOG("type = %d, name = %s\n", con->type, con->name);
153 
154  /* don’t handle dockarea cons, they must not be focused */
155  if (con->parent->type == CT_DOCKAREA)
156  goto done;
157 
158  /* if the user has bound an action to this click, it should override the
159  * default behavior. */
160  Binding *bind = get_binding_from_xcb_event((xcb_generic_event_t *)event);
161  if (bind && ((dest == CLICK_DECORATION && !bind->exclude_titlebar) ||
162  (dest == CLICK_INSIDE && bind->whole_window) ||
163  (dest == CLICK_BORDER && bind->border))) {
164  CommandResult *result = run_binding(bind, con);
165 
166  /* ASYNC_POINTER eats the event */
167  xcb_allow_events(conn, XCB_ALLOW_ASYNC_POINTER, event->time);
168  xcb_flush(conn);
169 
170  command_result_free(result);
171  return;
172  }
173 
174  /* There is no default behavior for button release events so we are done. */
175  if (event->response_type == XCB_BUTTON_RELEASE) {
176  goto done;
177  }
178 
179  /* Any click in a workspace should focus that workspace. If the
180  * workspace is on another output we need to do a workspace_show in
181  * order for i3bar (and others) to notice the change in workspace. */
182  Con *ws = con_get_workspace(con);
183  Con *focused_workspace = con_get_workspace(focused);
184 
185  if (!ws) {
186  ws = TAILQ_FIRST(&(output_get_content(con_get_output(con))->focus_head));
187  if (!ws)
188  goto done;
189  }
190 
191  if (ws != focused_workspace)
192  workspace_show(ws);
193 
194  /* get the floating con */
195  Con *floatingcon = con_inside_floating(con);
196  const bool proportional = (event->state & XCB_KEY_BUT_MASK_SHIFT) == XCB_KEY_BUT_MASK_SHIFT;
197  const bool in_stacked = (con->parent->layout == L_STACKED || con->parent->layout == L_TABBED);
198  const bool was_focused = focused == con;
199  const bool is_left_click = (event->detail == XCB_BUTTON_CLICK_LEFT);
200  const bool is_right_click = (event->detail == XCB_BUTTON_CLICK_RIGHT);
201  const bool is_left_or_right_click = (is_left_click || is_right_click);
202  const bool is_scroll = (event->detail == XCB_BUTTON_SCROLL_UP ||
203  event->detail == XCB_BUTTON_SCROLL_DOWN ||
204  event->detail == XCB_BUTTON_SCROLL_LEFT ||
205  event->detail == XCB_BUTTON_SCROLL_RIGHT);
206 
207  /* 1: see if the user scrolled on the decoration of a stacked/tabbed con */
208  if (in_stacked && dest == CLICK_DECORATION && is_scroll) {
209  DLOG("Scrolling on a window decoration\n");
210  /* Use the focused child of the tabbed / stacked container, not the
211  * container the user scrolled on. */
212  Con *current = TAILQ_FIRST(&(con->parent->focus_head));
213  const position_t direction =
214  (event->detail == XCB_BUTTON_SCROLL_UP || event->detail == XCB_BUTTON_SCROLL_LEFT) ? BEFORE : AFTER;
215  Con *next = get_tree_next_sibling(current, direction);
216  con_activate(con_descend_focused(next ? next : current));
217 
218  goto done;
219  }
220 
221  /* 2: focus this con. */
222  con_activate(con);
223 
224  /* 3: For floating containers, we also want to raise them on click.
225  * We will skip handling events on floating cons in fullscreen mode */
227  if (floatingcon != NULL && fs != con) {
228  /* 4: floating_modifier plus left mouse button drags */
229  if (mod_pressed && is_left_click) {
230  floating_drag_window(floatingcon, event, false);
231  return;
232  }
233 
234  /* 5: resize (floating) if this was a (left or right) click on the
235  * left/right/bottom border, or a right click on the decoration.
236  * also try resizing (tiling) if possible */
237  if (mod_pressed && is_right_click) {
238  DLOG("floating resize due to floatingmodifier\n");
239  floating_resize_window(floatingcon, proportional, event);
240  return;
241  }
242 
243  if ((dest == CLICK_BORDER || dest == CLICK_DECORATION) &&
244  is_left_or_right_click) {
245  /* try tiling resize, but continue if it doesn’t work */
246  DLOG("tiling resize with fallback\n");
247  if (tiling_resize(con, event, dest, dest == CLICK_DECORATION && !was_focused))
248  goto done;
249  }
250 
251  if (dest == CLICK_DECORATION && is_right_click) {
252  DLOG("floating resize due to decoration right click\n");
253  floating_resize_window(floatingcon, proportional, event);
254  return;
255  }
256 
257  if (dest == CLICK_BORDER && is_left_or_right_click) {
258  DLOG("floating resize due to border click\n");
259  floating_resize_window(floatingcon, proportional, event);
260  return;
261  }
262 
263  /* 6: dragging, if this was a click on a decoration (which did not lead
264  * to a resize) */
265  if (dest == CLICK_DECORATION && is_left_click) {
266  floating_drag_window(floatingcon, event, !was_focused);
267  return;
268  }
269 
270  goto done;
271  }
272 
273  /* 7: floating modifier pressed, initiate a resize */
274  if (dest == CLICK_INSIDE && mod_pressed && is_right_click) {
275  if (floating_mod_on_tiled_client(con, event)) {
276  return;
277  }
278  /* Avoid propagating events to clients, since the user expects
279  * $mod+click to be handled by i3. */
280  xcb_allow_events(conn, XCB_ALLOW_ASYNC_POINTER, event->time);
281  xcb_flush(conn);
282  return;
283  }
284  /* 8: otherwise, check for border/decoration clicks and resize */
285  if ((dest == CLICK_BORDER || dest == CLICK_DECORATION) &&
286  is_left_or_right_click) {
287  DLOG("Trying to resize (tiling)\n");
288  tiling_resize(con, event, dest, dest == CLICK_DECORATION && !was_focused);
289  }
290 
291 done:
292  xcb_allow_events(conn, XCB_ALLOW_REPLAY_POINTER, event->time);
293  xcb_flush(conn);
294  tree_render();
295 }
296 
297 /*
298  * The button press X callback. This function determines whether the floating
299  * modifier is pressed and where the user clicked (decoration, border, inside
300  * the window).
301  *
302  * Then, route_click is called on the appropriate con.
303  *
304  */
305 void handle_button_press(xcb_button_press_event_t *event) {
306  Con *con;
307  DLOG("Button %d (state %d) %s on window 0x%08x (child 0x%08x) at (%d, %d) (root %d, %d)\n",
308  event->detail, event->state, (event->response_type == XCB_BUTTON_PRESS ? "press" : "release"),
309  event->event, event->child, event->event_x, event->event_y, event->root_x,
310  event->root_y);
311 
312  last_timestamp = event->time;
313 
314  const uint32_t mod = (config.floating_modifier & 0xFFFF);
315  const bool mod_pressed = (mod != 0 && (event->state & mod) == mod);
316  DLOG("floating_mod = %d, detail = %d\n", mod_pressed, event->detail);
317  if ((con = con_by_window_id(event->event))) {
318  route_click(con, event, mod_pressed, CLICK_INSIDE);
319  return;
320  }
321 
322  if (!(con = con_by_frame_id(event->event))) {
323  /* Run bindings on the root window as well, see #2097. We only run it
324  * if --whole-window was set as that's the equivalent for a normal
325  * window. */
326  if (event->event == root) {
327  Binding *bind = get_binding_from_xcb_event((xcb_generic_event_t *)event);
328  if (bind != NULL && bind->whole_window) {
329  CommandResult *result = run_binding(bind, NULL);
330  command_result_free(result);
331  }
332  }
333 
334  /* If the root window is clicked, find the relevant output from the
335  * click coordinates and focus the output's active workspace. */
336  if (event->event == root && event->response_type == XCB_BUTTON_PRESS) {
337  Con *output, *ws;
338  TAILQ_FOREACH (output, &(croot->nodes_head), nodes) {
339  if (con_is_internal(output) ||
340  !rect_contains(output->rect, event->event_x, event->event_y))
341  continue;
342 
343  ws = TAILQ_FIRST(&(output_get_content(output)->focus_head));
344  if (ws != con_get_workspace(focused)) {
345  workspace_show(ws);
346  tree_render();
347  }
348  return;
349  }
350  return;
351  }
352 
353  ELOG("Clicked into unknown window?!\n");
354  xcb_allow_events(conn, XCB_ALLOW_REPLAY_POINTER, event->time);
355  xcb_flush(conn);
356  return;
357  }
358 
359  /* Check if the click was on the decoration of a child */
360  Con *child;
361  TAILQ_FOREACH_REVERSE (child, &(con->nodes_head), nodes_head, nodes) {
362  if (!rect_contains(child->deco_rect, event->event_x, event->event_y))
363  continue;
364 
365  route_click(child, event, mod_pressed, CLICK_DECORATION);
366  return;
367  }
368 
369  if (event->child != XCB_NONE) {
370  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");
371  route_click(con, event, mod_pressed, CLICK_INSIDE);
372  return;
373  }
374 
375  route_click(con, event, mod_pressed, CLICK_BORDER);
376 }
CommandResult * run_binding(Binding *bind, Con *con)
Runs the given binding and handles parse errors.
Definition: bindings.c:818
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
static bool floating_mod_on_tiled_client(Con *con, xcb_button_press_event_t *event)
Definition: click.c:81
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:305
static void route_click(Con *con, xcb_button_press_event_t *event, const bool mod_pressed, const click_destination_t dest)
Definition: click.c:149
static bool tiling_resize(Con *con, xcb_button_press_event_t *event, const click_destination_t dest, bool use_threshold)
Definition: click.c:120
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
void command_result_free(CommandResult *result)
Frees a CommandResult.
Con * con_get_fullscreen_covering_ws(Con *ws)
Returns the fullscreen node that covers the given workspace if it exists.
Definition: con.c:572
Con * con_get_workspace(Con *con)
Gets the workspace container this node is on.
Definition: con.c:476
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:667
Con * con_descend_focused(Con *con)
Returns the focused con inside this client, descending the tree as far as possible.
Definition: con.c:1549
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:707
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:1656
bool con_is_internal(Con *con)
Returns true if the container is internal, such as __i3_scratch.
Definition: con.c:587
Con * con_inside_floating(Con *con)
Checks if the given container is either floating or inside some floating container.
Definition: con.c:619
Con * con_get_output(Con *con)
Gets the output container (first container with CT_OUTPUT in hierarchy) this node is on.
Definition: con.c:462
void con_activate(Con *con)
Sets input focus to the given container and raises it to the top.
Definition: con.c:286
Config config
Definition: config.c:17
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:697
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:599
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:61
xcb_connection_t * conn
XCB connection and root screen.
Definition: main.c:51
xcb_window_t root
Definition: main.c:64
Con * output_get_content(Con *output)
Returns the output container below the given output container.
Definition: output.c:16
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
struct Con * focused
Definition: tree.c:13
struct Con * croot
Definition: tree.c:12
Con * get_tree_next_sibling(Con *con, position_t direction)
Get the previous / next sibling.
Definition: tree.c:629
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
bool rect_contains(Rect rect, uint32_t x, uint32_t y)
Definition: util.c:32
void workspace_show(Con *workspace)
Switches to the given workspace.
Definition: workspace.c:417
position_t
Definition: data.h:59
@ AFTER
Definition: data.h:60
@ BEFORE
Definition: data.h:59
@ L_STACKED
Definition: data.h:92
@ L_TABBED
Definition: data.h:93
orientation_t
Definition: data.h:56
@ VERT
Definition: data.h:58
@ HORIZ
Definition: data.h:57
direction_t
Definition: data.h:52
@ D_RIGHT
Definition: data.h:53
@ D_LEFT
Definition: data.h:52
@ D_UP
Definition: data.h:54
@ D_DOWN
Definition: data.h:55
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
#define XCB_BUTTON_CLICK_LEFT
Mouse buttons.
Definition: libi3.h:28
#define XCB_BUTTON_SCROLL_UP
Definition: libi3.h:31
#define DLOG(fmt,...)
Definition: libi3.h:104
#define ELOG(fmt,...)
Definition: libi3.h:99
#define XCB_BUTTON_SCROLL_LEFT
Definition: libi3.h:34
#define XCB_BUTTON_SCROLL_RIGHT
Definition: libi3.h:35
#define XCB_BUTTON_CLICK_RIGHT
Definition: libi3.h:30
#define XCB_BUTTON_SCROLL_DOWN
Definition: libi3.h:32
#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
A struct that contains useful information about the result of a command as a whole (e....
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:155
uint32_t height
Definition: data.h:159
uint32_t x
Definition: data.h:156
uint32_t y
Definition: data.h:157
uint32_t width
Definition: data.h:158
Holds a keybinding, consisting of a keycode combined with modifiers and the command which is executed...
Definition: data.h:275
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:301
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:296
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:305
A 'Con' represents everything from the X11 root window down to a single X11 window.
Definition: data.h:604
struct Con * parent
Definition: data.h:636
struct Rect deco_rect
Definition: data.h:646
struct Rect rect
Definition: data.h:640
enum Con::@20 type
layout_t layout
Definition: data.h:708
struct Rect window_rect
Definition: data.h:643
char * name
Definition: data.h:650
fullscreen_mode_t fullscreen_mode
Definition: data.h:687