i3
click.c
Go to the documentation of this file.
1 #undef I3__FILE__
2 #define I3__FILE__ "click.c"
3 /*
4  * vim:ts=4:sw=4:expandtab
5  *
6  * i3 - an improved dynamic tiling window manager
7  * © 2009 Michael Stapelberg and contributors (see also: LICENSE)
8  *
9  * click.c: Button press (mouse click) events.
10  *
11  */
12 #include "all.h"
13 
14 #include <time.h>
15 #include <math.h>
16 
17 #include <xcb/xcb_icccm.h>
18 
19 #include <X11/XKBlib.h>
20 
21 typedef enum { CLICK_BORDER = 0,
24 
25 /*
26  * Finds the correct pair of first/second cons between the resize will take
27  * place according to the passed border position (top, left, right, bottom),
28  * then calls resize_graphical_handler().
29  *
30  */
31 static bool tiling_resize_for_border(Con *con, border_t border, xcb_button_press_event_t *event) {
32  DLOG("border = %d, con = %p\n", border, con);
33  Con *second = NULL;
34  Con *first = con;
35  direction_t search_direction;
36  switch (border) {
37  case BORDER_LEFT:
38  search_direction = D_LEFT;
39  break;
40  case BORDER_RIGHT:
41  search_direction = D_RIGHT;
42  break;
43  case BORDER_TOP:
44  search_direction = D_UP;
45  break;
46  case BORDER_BOTTOM:
47  search_direction = D_DOWN;
48  break;
49  default:
50  assert(false);
51  break;
52  }
53 
54  bool res = resize_find_tiling_participants(&first, &second, search_direction);
55  if (!res) {
56  LOG("No second container in this direction found.\n");
57  return false;
58  }
59 
60  assert(first != second);
61  assert(first->parent == second->parent);
62 
63  /* The first container should always be in front of the second container */
64  if (search_direction == D_UP || search_direction == D_LEFT) {
65  Con *tmp = first;
66  first = second;
67  second = tmp;
68  }
69 
70  const orientation_t orientation = ((border == BORDER_LEFT || border == BORDER_RIGHT) ? HORIZ : VERT);
71 
72  resize_graphical_handler(first, second, orientation, event);
73 
74  DLOG("After resize handler, rendering\n");
75  tree_render();
76  return true;
77 }
78 
79 /*
80  * Called when the user clicks using the floating_modifier, but the client is in
81  * tiling layout.
82  *
83  * Returns false if it does not do anything (that is, the click should be sent
84  * to the client).
85  *
86  */
87 static bool floating_mod_on_tiled_client(Con *con, xcb_button_press_event_t *event) {
88  /* The client is in tiling layout. We can still initiate a resize with the
89  * right mouse button, by chosing the border which is the most near one to
90  * the position of the mouse pointer */
91  int to_right = con->rect.width - event->event_x,
92  to_left = event->event_x,
93  to_top = event->event_y,
94  to_bottom = con->rect.height - event->event_y;
95 
96  DLOG("click was %d px to the right, %d px to the left, %d px to top, %d px to bottom\n",
97  to_right, to_left, to_top, to_bottom);
98 
99  if (to_right < to_left &&
100  to_right < to_top &&
101  to_right < to_bottom)
102  return tiling_resize_for_border(con, BORDER_RIGHT, event);
103 
104  if (to_left < to_right &&
105  to_left < to_top &&
106  to_left < to_bottom)
107  return tiling_resize_for_border(con, BORDER_LEFT, event);
108 
109  if (to_top < to_right &&
110  to_top < to_left &&
111  to_top < to_bottom)
112  return tiling_resize_for_border(con, BORDER_TOP, event);
113 
114  if (to_bottom < to_right &&
115  to_bottom < to_left &&
116  to_bottom < to_top)
117  return tiling_resize_for_border(con, BORDER_BOTTOM, event);
118 
119  return false;
120 }
121 
122 /*
123  * Finds out which border was clicked on and calls tiling_resize_for_border().
124  *
125  */
126 static bool tiling_resize(Con *con, xcb_button_press_event_t *event, const click_destination_t dest) {
127  /* check if this was a click on the window border (and on which one) */
128  Rect bsr = con_border_style_rect(con);
129  DLOG("BORDER x = %d, y = %d for con %p, window 0x%08x\n",
130  event->event_x, event->event_y, con, event->event);
131  DLOG("checks for right >= %d\n", con->window_rect.x + con->window_rect.width);
132  if (dest == CLICK_DECORATION) {
133  /* The user clicked on a window decoration. We ignore the following case:
134  * The container is a h-split, tabbed or stacked container with > 1
135  * window. Decorations will end up next to each other and the user
136  * expects to switch to a window by clicking on its decoration. */
137 
138  /* Since the container might either be the child *or* already a split
139  * container (in the case of a nested split container), we need to make
140  * sure that we are dealing with the split container here. */
141  Con *check_con = con;
142  if (con_is_leaf(check_con) && check_con->parent->type == CT_CON)
143  check_con = check_con->parent;
144 
145  if ((check_con->layout == L_STACKED ||
146  check_con->layout == L_TABBED ||
147  con_orientation(check_con) == HORIZ) &&
148  con_num_children(check_con) > 1) {
149  DLOG("Not handling this resize, this container has > 1 child.\n");
150  return false;
151  }
152  return tiling_resize_for_border(con, BORDER_TOP, event);
153  }
154 
155  if (event->event_x >= 0 && event->event_x <= (int32_t)bsr.x &&
156  event->event_y >= (int32_t)bsr.y && event->event_y <= (int32_t)(con->rect.height + bsr.height))
157  return tiling_resize_for_border(con, BORDER_LEFT, event);
158 
159  if (event->event_x >= (int32_t)(con->window_rect.x + con->window_rect.width) &&
160  event->event_y >= (int32_t)bsr.y && event->event_y <= (int32_t)(con->rect.height + bsr.height))
161  return tiling_resize_for_border(con, BORDER_RIGHT, event);
162 
163  if (event->event_y >= (int32_t)(con->window_rect.y + con->window_rect.height))
164  return tiling_resize_for_border(con, BORDER_BOTTOM, event);
165 
166  return false;
167 }
168 
169 /*
170  * Being called by handle_button_press, this function calls the appropriate
171  * functions for resizing/dragging.
172  *
173  */
174 static int route_click(Con *con, xcb_button_press_event_t *event, const bool mod_pressed, const click_destination_t dest) {
175  DLOG("--> click properties: mod = %d, destination = %d\n", mod_pressed, dest);
176  DLOG("--> OUTCOME = %p\n", con);
177  DLOG("type = %d, name = %s\n", con->type, con->name);
178 
179  /* don’t handle dockarea cons, they must not be focused */
180  if (con->parent->type == CT_DOCKAREA)
181  goto done;
182 
183  const bool is_left_or_right_click = (event->detail == XCB_BUTTON_INDEX_1 ||
184  event->detail == XCB_BUTTON_INDEX_3);
185 
186  /* if the user has bound an action to this click, it should override the
187  * default behavior. */
188  if (dest == CLICK_DECORATION || dest == CLICK_INSIDE || dest == CLICK_BORDER) {
189  Binding *bind = get_binding_from_xcb_event((xcb_generic_event_t *)event);
190  /* clicks over a window decoration will always trigger the binding and
191  * clicks on the inside of the window will only trigger a binding if
192  * the --whole-window flag was given for the binding. */
193  if (bind && ((dest == CLICK_DECORATION || bind->whole_window) ||
194  (dest == CLICK_BORDER && bind->border))) {
195  CommandResult *result = run_binding(bind, con);
196 
197  /* ASYNC_POINTER eats the event */
198  xcb_allow_events(conn, XCB_ALLOW_ASYNC_POINTER, event->time);
199  xcb_flush(conn);
200 
201  command_result_free(result);
202  return 0;
203  }
204  }
205 
206  /* There is no default behavior for button release events so we are done. */
207  if (event->response_type == XCB_BUTTON_RELEASE) {
208  goto done;
209  }
210 
211  /* Any click in a workspace should focus that workspace. If the
212  * workspace is on another output we need to do a workspace_show in
213  * order for i3bar (and others) to notice the change in workspace. */
214  Con *ws = con_get_workspace(con);
215  Con *focused_workspace = con_get_workspace(focused);
216 
217  if (!ws) {
218  ws = TAILQ_FIRST(&(output_get_content(con_get_output(con))->focus_head));
219  if (!ws)
220  goto done;
221  }
222 
223  if (ws != focused_workspace)
224  workspace_show(ws);
225 
226  /* get the floating con */
227  Con *floatingcon = con_inside_floating(con);
228  const bool proportional = (event->state & XCB_KEY_BUT_MASK_SHIFT) == XCB_KEY_BUT_MASK_SHIFT;
229  const bool in_stacked = (con->parent->layout == L_STACKED || con->parent->layout == L_TABBED);
230 
231  /* 1: see if the user scrolled on the decoration of a stacked/tabbed con */
232  if (in_stacked &&
233  dest == CLICK_DECORATION &&
234  (event->detail == XCB_BUTTON_INDEX_4 ||
235  event->detail == XCB_BUTTON_INDEX_5)) {
236  DLOG("Scrolling on a window decoration\n");
237  orientation_t orientation = (con->parent->layout == L_STACKED ? VERT : HORIZ);
238  /* Focus the currently focused container on the same level that the
239  * user scrolled on. e.g. the tabbed decoration contains
240  * "urxvt | i3: V[xterm geeqie] | firefox",
241  * focus is on the xterm, but the user scrolled on urxvt.
242  * The splitv container will be focused. */
243  Con *focused = con->parent;
244  focused = TAILQ_FIRST(&(focused->focus_head));
245  con_focus(focused);
246  /* To prevent scrolling from going outside the container (see ticket
247  * #557), we first check if scrolling is possible at all. */
248  bool scroll_prev_possible = (TAILQ_PREV(focused, nodes_head, nodes) != NULL);
249  bool scroll_next_possible = (TAILQ_NEXT(focused, nodes) != NULL);
250  if (event->detail == XCB_BUTTON_INDEX_4 && scroll_prev_possible)
251  tree_next('p', orientation);
252  else if (event->detail == XCB_BUTTON_INDEX_5 && scroll_next_possible)
253  tree_next('n', orientation);
254  goto done;
255  }
256 
257  /* 2: focus this con. */
258  con_focus(con);
259 
260  /* 3: For floating containers, we also want to raise them on click.
261  * We will skip handling events on floating cons in fullscreen mode */
262  Con *fs = (ws ? con_get_fullscreen_con(ws, CF_OUTPUT) : NULL);
263  if (floatingcon != NULL && fs != con) {
264  floating_raise_con(floatingcon);
265 
266  /* 4: floating_modifier plus left mouse button drags */
267  if (mod_pressed && event->detail == XCB_BUTTON_INDEX_1) {
268  floating_drag_window(floatingcon, event);
269  return 1;
270  }
271 
272  /* 5: resize (floating) if this was a (left or right) click on the
273  * left/right/bottom border, or a right click on the decoration.
274  * also try resizing (tiling) if it was a click on the top */
275  if (mod_pressed && event->detail == XCB_BUTTON_INDEX_3) {
276  DLOG("floating resize due to floatingmodifier\n");
277  floating_resize_window(floatingcon, proportional, event);
278  return 1;
279  }
280 
281  if (!in_stacked && dest == CLICK_DECORATION &&
282  is_left_or_right_click) {
283  /* try tiling resize, but continue if it doesn’t work */
284  DLOG("tiling resize with fallback\n");
285  if (tiling_resize(con, event, dest))
286  goto done;
287  }
288 
289  if (dest == CLICK_DECORATION && event->detail == XCB_BUTTON_INDEX_3) {
290  DLOG("floating resize due to decoration right click\n");
291  floating_resize_window(floatingcon, proportional, event);
292  return 1;
293  }
294 
295  if (dest == CLICK_BORDER && is_left_or_right_click) {
296  DLOG("floating resize due to border click\n");
297  floating_resize_window(floatingcon, proportional, event);
298  return 1;
299  }
300 
301  /* 6: dragging, if this was a click on a decoration (which did not lead
302  * to a resize) */
303  if (!in_stacked && dest == CLICK_DECORATION &&
304  (event->detail == XCB_BUTTON_INDEX_1)) {
305  floating_drag_window(floatingcon, event);
306  return 1;
307  }
308 
309  goto done;
310  }
311 
312  if (in_stacked) {
313  /* for stacked/tabbed cons, the resizing applies to the parent
314  * container */
315  con = con->parent;
316  }
317 
318  /* 7: floating modifier pressed, initiate a resize */
319  if (dest == CLICK_INSIDE && mod_pressed && event->detail == XCB_BUTTON_INDEX_3) {
320  if (floating_mod_on_tiled_client(con, event))
321  return 1;
322  }
323  /* 8: otherwise, check for border/decoration clicks and resize */
324  else if ((dest == CLICK_BORDER || dest == CLICK_DECORATION) &&
325  is_left_or_right_click) {
326  DLOG("Trying to resize (tiling)\n");
327  tiling_resize(con, event, dest);
328  }
329 
330 done:
331  xcb_allow_events(conn, XCB_ALLOW_REPLAY_POINTER, event->time);
332  xcb_flush(conn);
333  tree_render();
334 
335  return 0;
336 }
337 
338 /*
339  * The button press X callback. This function determines whether the floating
340  * modifier is pressed and where the user clicked (decoration, border, inside
341  * the window).
342  *
343  * Then, route_click is called on the appropriate con.
344  *
345  */
346 int handle_button_press(xcb_button_press_event_t *event) {
347  Con *con;
348  DLOG("Button %d (state %d) %s on window 0x%08x (child 0x%08x) at (%d, %d) (root %d, %d)\n",
349  event->detail, event->state, (event->response_type == XCB_BUTTON_PRESS ? "press" : "release"),
350  event->event, event->child, event->event_x, event->event_y, event->root_x,
351  event->root_y);
352 
353  last_timestamp = event->time;
354 
355  const uint32_t mod = (config.floating_modifier & 0xFFFF);
356  const bool mod_pressed = (mod != 0 && (event->state & mod) == mod);
357  DLOG("floating_mod = %d, detail = %d\n", mod_pressed, event->detail);
358  if ((con = con_by_window_id(event->event)))
359  return route_click(con, event, mod_pressed, CLICK_INSIDE);
360 
361  if (!(con = con_by_frame_id(event->event))) {
362  /* Run bindings on the root window as well, see #2097. We only run it
363  * if --whole-window was set as that's the equivalent for a normal
364  * window. */
365  if (event->event == root) {
366  Binding *bind = get_binding_from_xcb_event((xcb_generic_event_t *)event);
367  if (bind != NULL && bind->whole_window) {
368  CommandResult *result = run_binding(bind, NULL);
369  command_result_free(result);
370  }
371  }
372 
373  /* If the root window is clicked, find the relevant output from the
374  * click coordinates and focus the output's active workspace. */
375  if (event->event == root && event->response_type == XCB_BUTTON_PRESS) {
376  Con *output, *ws;
377  TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
378  if (con_is_internal(output) ||
379  !rect_contains(output->rect, event->event_x, event->event_y))
380  continue;
381 
382  ws = TAILQ_FIRST(&(output_get_content(output)->focus_head));
383  if (ws != con_get_workspace(focused)) {
384  workspace_show(ws);
385  tree_render();
386  }
387  return 1;
388  }
389  return 0;
390  }
391 
392  ELOG("Clicked into unknown window?!\n");
393  xcb_allow_events(conn, XCB_ALLOW_REPLAY_POINTER, event->time);
394  xcb_flush(conn);
395  return 0;
396  }
397 
398  /* Check if the click was on the decoration of a child */
399  Con *child;
400  TAILQ_FOREACH(child, &(con->nodes_head), nodes) {
401  if (!rect_contains(child->deco_rect, event->event_x, event->event_y))
402  continue;
403 
404  return route_click(child, event, mod_pressed, CLICK_DECORATION);
405  }
406 
407  if (event->child != XCB_NONE) {
408  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");
409  return route_click(con, event, mod_pressed, CLICK_INSIDE);
410  }
411 
412  return route_click(con, event, mod_pressed, CLICK_BORDER);
413 }
uint32_t height
Definition: data.h:145
struct Rect deco_rect
Definition: data.h:586
uint32_t x
Definition: data.h:142
Con * con_inside_floating(Con *con)
Checks if the given container is either floating or inside some floating container.
Definition: con.c:500
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:259
void tree_next(char way, orientation_t orientation)
Changes focus in the given way (next/previous) and given orientation (horizontal/vertical).
Definition: tree.c:674
int resize_graphical_handler(Con *first, Con *second, orientation_t orientation, const xcb_button_press_event_t *event)
Definition: resize.c:100
char * name
Definition: data.h:590
Definition: data.h:87
void floating_drag_window(Con *con, const xcb_button_press_event_t *event)
Called when the user clicked on the titlebar of a floating window.
Definition: floating.c:487
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:53
void con_focus(Con *con)
Sets input focus to the given container.
Definition: con.c:198
static int route_click(Con *con, xcb_button_press_event_t *event, const bool mod_pressed, const click_destination_t dest)
Definition: click.c:174
border_t
On which border was the dragging initiated?
Definition: floating.h:23
xcb_window_t root
Definition: main.c:56
Con * output_get_content(Con *output)
Returns the output container below the given output container.
Definition: output.c:18
bool con_is_internal(Con *con)
Returns true if the container is internal, such as __i3_scratch.
Definition: con.c:467
static bool tiling_resize(Con *con, xcb_button_press_event_t *event, const click_destination_t dest)
Definition: click.c:126
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:582
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:531
Definition: data.h:57
#define TAILQ_FOREACH(var, head, field)
Definition: queue.h:347
orientation_t
Definition: data.h:58
xcb_connection_t * conn
XCB connection and root screen.
Definition: main.c:43
#define TAILQ_NEXT(elm, field)
Definition: queue.h:338
Config config
Definition: config.c:17
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:492
Rect con_border_style_rect(Con *con)
Returns a &quot;relative&quot; Rect which contains the amount of pixels that need to be added to the original R...
Definition: con.c:1413
Con * con_get_output(Con *con)
Gets the output container (first container with CT_OUTPUT in hierarchy) this node is on...
Definition: con.c:359
uint32_t y
Definition: data.h:143
Definition: data.h:86
void floating_raise_con(Con *con)
Raises the given container in the list of floating containers.
Definition: floating.c:382
Definition: data.h:54
void workspace_show(Con *workspace)
Switches to the given workspace.
Definition: workspace.c:491
struct Con * croot
Definition: tree.c:14
#define ELOG(fmt,...)
Definition: libi3.h:93
uint32_t width
Definition: data.h:144
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:276
#define LOG(fmt,...)
Definition: libi3.h:88
struct Rect rect
Definition: data.h:580
click_destination_t
Definition: click.c:21
void command_result_free(CommandResult *result)
Frees a CommandResult.
bool rect_contains(Rect rect, uint32_t x, uint32_t y)
Definition: util.c:37
Stores a rectangle, for example the size of a window, the child window etc.
Definition: data.h:141
bool con_is_leaf(Con *con)
Returns true when this node is a leaf node (has no children)
Definition: con.c:257
Con * con_get_workspace(Con *con)
Gets the workspace container this node is on.
Definition: con.c:373
direction_t
Definition: data.h:54
orientation_t con_orientation(Con *con)
Returns the orientation of the given container (for stacked containers, vertical orientation is used ...
Definition: con.c:1166
Definition: data.h:55
bool resize_find_tiling_participants(Con **current, Con **other, direction_t direction)
Definition: resize.c:52
struct Rect window_rect
Definition: data.h:583
Con * con_get_fullscreen_con(Con *con, fullscreen_mode_t fullscreen_mode)
Returns the first fullscreen node below this node.
Definition: con.c:421
CommandResult * run_binding(Binding *bind, Con *con)
Runs the given binding and handles parse errors.
Definition: bindings.c:646
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:544
#define DLOG(fmt,...)
Definition: libi3.h:98
A &#39;Con&#39; represents everything from the X11 root window down to a single X11 window.
Definition: data.h:544
uint32_t floating_modifier
The modifier which needs to be pressed in combination with your mouse buttons to do things with float...
Definition: config.h:196
A struct that contains useful information about the result of a command as a whole (e...
#define TAILQ_FIRST(head)
Definition: queue.h:336
Definition: data.h:56
enum Con::@20 type
int handle_button_press(xcb_button_press_event_t *event)
The button press X callback.
Definition: click.c:346
static bool floating_mod_on_tiled_client(Con *con, xcb_button_press_event_t *event)
Definition: click.c:87
Definition: data.h:59
int con_num_children(Con *con)
Returns the number of children of this container.
Definition: con.c:720
layout_t layout
Definition: data.h:648
Definition: data.h:60
static bool tiling_resize_for_border(Con *con, border_t border, xcb_button_press_event_t *event)
Definition: click.c:31
Con * focused
Definition: tree.c:15
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:271
#define TAILQ_PREV(elm, headname, field)
Definition: queue.h:342
struct Con * parent
Definition: data.h:576
Holds a keybinding, consisting of a keycode combined with modifiers and the command which is executed...
Definition: data.h:250