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 #include <math.h>
14 
15 #include <xcb/xcb_icccm.h>
16 
17 #include <X11/XKBlib.h>
18 
19 typedef enum { CLICK_BORDER = 0,
22 
23 /*
24  * Finds the correct pair of first/second cons between the resize will take
25  * place according to the passed border position (top, left, right, bottom),
26  * then calls resize_graphical_handler().
27  *
28  */
29 static bool tiling_resize_for_border(Con *con, border_t border, xcb_button_press_event_t *event, bool use_threshold) {
30  DLOG("border = %d, con = %p\n", border, con);
31  Con *second = NULL;
32  Con *first = con;
33  direction_t search_direction;
34  switch (border) {
35  case BORDER_LEFT:
36  search_direction = D_LEFT;
37  break;
38  case BORDER_RIGHT:
39  search_direction = D_RIGHT;
40  break;
41  case BORDER_TOP:
42  search_direction = D_UP;
43  break;
44  case BORDER_BOTTOM:
45  search_direction = D_DOWN;
46  break;
47  }
48 
49  bool res = resize_find_tiling_participants(&first, &second, search_direction, false);
50  if (!res) {
51  LOG("No second container in this direction found.\n");
52  return false;
53  }
54 
55  assert(first != second);
56  assert(first->parent == second->parent);
57 
58  /* The first container should always be in front of the second container */
59  if (search_direction == D_UP || search_direction == D_LEFT) {
60  Con *tmp = first;
61  first = second;
62  second = tmp;
63  }
64 
65  const orientation_t orientation = ((border == BORDER_LEFT || border == BORDER_RIGHT) ? HORIZ : VERT);
66 
67  resize_graphical_handler(first, second, orientation, event, use_threshold);
68 
69  DLOG("After resize handler, rendering\n");
70  tree_render();
71  return true;
72 }
73 
74 /*
75  * Called when the user clicks using the floating_modifier, but the client is in
76  * tiling layout.
77  *
78  * Returns false if it does not do anything (that is, the click should be sent
79  * to the client).
80  *
81  */
82 static bool floating_mod_on_tiled_client(Con *con, xcb_button_press_event_t *event) {
83  /* The client is in tiling layout. We can still initiate a resize with the
84  * right mouse button, by chosing the border which is the most near one to
85  * the position of the mouse pointer */
86  int to_right = con->rect.width - event->event_x,
87  to_left = event->event_x,
88  to_top = event->event_y,
89  to_bottom = con->rect.height - event->event_y;
90 
91  DLOG("click was %d px to the right, %d px to the left, %d px to top, %d px to bottom\n",
92  to_right, to_left, to_top, to_bottom);
93 
94  if (to_right < to_left &&
95  to_right < to_top &&
96  to_right < to_bottom)
97  return tiling_resize_for_border(con, BORDER_RIGHT, event, false);
98 
99  if (to_left < to_right &&
100  to_left < to_top &&
101  to_left < to_bottom)
102  return tiling_resize_for_border(con, BORDER_LEFT, event, false);
103 
104  if (to_top < to_right &&
105  to_top < to_left &&
106  to_top < to_bottom)
107  return tiling_resize_for_border(con, BORDER_TOP, event, false);
108 
109  if (to_bottom < to_right &&
110  to_bottom < to_left &&
111  to_bottom < to_top)
112  return tiling_resize_for_border(con, BORDER_BOTTOM, event, false);
113 
114  return false;
115 }
116 
117 /*
118  * Finds out which border was clicked on and calls tiling_resize_for_border().
119  *
120  */
121 static bool tiling_resize(Con *con, xcb_button_press_event_t *event, const click_destination_t dest, bool use_threshold) {
122  /* check if this was a click on the window border (and on which one) */
123  Rect bsr = con_border_style_rect(con);
124  DLOG("BORDER x = %d, y = %d for con %p, window 0x%08x\n",
125  event->event_x, event->event_y, con, event->event);
126  DLOG("checks for right >= %d\n", con->window_rect.x + con->window_rect.width);
127  if (dest == CLICK_DECORATION) {
128  return tiling_resize_for_border(con, BORDER_TOP, event, use_threshold);
129  }
130 
131  if (event->event_x >= 0 && event->event_x <= (int32_t)bsr.x &&
132  event->event_y >= (int32_t)bsr.y && event->event_y <= (int32_t)(con->rect.height + bsr.height))
133  return tiling_resize_for_border(con, BORDER_LEFT, event, false);
134 
135  if (event->event_x >= (int32_t)(con->window_rect.x + con->window_rect.width) &&
136  event->event_y >= (int32_t)bsr.y && event->event_y <= (int32_t)(con->rect.height + bsr.height))
137  return tiling_resize_for_border(con, BORDER_RIGHT, event, false);
138 
139  if (event->event_y >= (int32_t)(con->window_rect.y + con->window_rect.height))
140  return tiling_resize_for_border(con, BORDER_BOTTOM, event, false);
141 
142  return false;
143 }
144 
145 /*
146  * Being called by handle_button_press, this function calls the appropriate
147  * functions for resizing/dragging.
148  *
149  */
150 static int route_click(Con *con, xcb_button_press_event_t *event, const bool mod_pressed, const click_destination_t dest) {
151  DLOG("--> click properties: mod = %d, destination = %d\n", mod_pressed, dest);
152  DLOG("--> OUTCOME = %p\n", con);
153  DLOG("type = %d, name = %s\n", con->type, con->name);
154 
155  /* don’t handle dockarea cons, they must not be focused */
156  if (con->parent->type == CT_DOCKAREA)
157  goto done;
158 
159  const bool is_left_or_right_click = (event->detail == XCB_BUTTON_CLICK_LEFT ||
160  event->detail == XCB_BUTTON_CLICK_RIGHT);
161 
162  /* if the user has bound an action to this click, it should override the
163  * default behavior. */
164  if (dest == CLICK_DECORATION || dest == CLICK_INSIDE || dest == CLICK_BORDER) {
165  Binding *bind = get_binding_from_xcb_event((xcb_generic_event_t *)event);
166 
167  if (bind != NULL && ((dest == CLICK_DECORATION && !bind->exclude_titlebar) ||
168  (dest == CLICK_INSIDE && bind->whole_window) ||
169  (dest == CLICK_BORDER && bind->border))) {
170  CommandResult *result = run_binding(bind, con);
171 
172  /* ASYNC_POINTER eats the event */
173  xcb_allow_events(conn, XCB_ALLOW_ASYNC_POINTER, event->time);
174  xcb_flush(conn);
175 
176  command_result_free(result);
177  return 0;
178  }
179  }
180 
181  /* There is no default behavior for button release events so we are done. */
182  if (event->response_type == XCB_BUTTON_RELEASE) {
183  goto done;
184  }
185 
186  /* Any click in a workspace should focus that workspace. If the
187  * workspace is on another output we need to do a workspace_show in
188  * order for i3bar (and others) to notice the change in workspace. */
189  Con *ws = con_get_workspace(con);
190  Con *focused_workspace = con_get_workspace(focused);
191 
192  if (!ws) {
193  ws = TAILQ_FIRST(&(output_get_content(con_get_output(con))->focus_head));
194  if (!ws)
195  goto done;
196  }
197 
198  if (ws != focused_workspace)
199  workspace_show(ws);
200 
201  /* get the floating con */
202  Con *floatingcon = con_inside_floating(con);
203  const bool proportional = (event->state & XCB_KEY_BUT_MASK_SHIFT) == XCB_KEY_BUT_MASK_SHIFT;
204  const bool in_stacked = (con->parent->layout == L_STACKED || con->parent->layout == L_TABBED);
205  const bool was_focused = focused == con;
206 
207  /* 1: see if the user scrolled on the decoration of a stacked/tabbed con */
208  if (in_stacked &&
209  dest == CLICK_DECORATION &&
210  (event->detail == XCB_BUTTON_SCROLL_UP ||
211  event->detail == XCB_BUTTON_SCROLL_DOWN ||
212  event->detail == XCB_BUTTON_SCROLL_LEFT ||
213  event->detail == XCB_BUTTON_SCROLL_RIGHT)) {
214  DLOG("Scrolling on a window decoration\n");
215  /* Use the focused child of the tabbed / stacked container, not the
216  * container the user scrolled on. */
217  Con *current = TAILQ_FIRST(&(con->parent->focus_head));
218  const position_t direction =
219  (event->detail == XCB_BUTTON_SCROLL_UP || event->detail == XCB_BUTTON_SCROLL_LEFT) ? BEFORE : AFTER;
220  Con *next = get_tree_next_sibling(current, direction);
221  con_activate(con_descend_focused(next ? next : current));
222 
223  goto done;
224  }
225 
226  /* 2: focus this con. */
227  con_activate(con);
228 
229  /* 3: For floating containers, we also want to raise them on click.
230  * We will skip handling events on floating cons in fullscreen mode */
232  if (floatingcon != NULL && fs != con) {
233  /* 4: floating_modifier plus left mouse button drags */
234  if (mod_pressed && event->detail == XCB_BUTTON_CLICK_LEFT) {
235  floating_drag_window(floatingcon, event, false);
236  return 1;
237  }
238 
239  /* 5: resize (floating) if this was a (left or right) click on the
240  * left/right/bottom border, or a right click on the decoration.
241  * also try resizing (tiling) if it was a click on the top */
242  if (mod_pressed && event->detail == XCB_BUTTON_CLICK_RIGHT) {
243  DLOG("floating resize due to floatingmodifier\n");
244  floating_resize_window(floatingcon, proportional, event);
245  return 1;
246  }
247 
248  if (!in_stacked && dest == CLICK_DECORATION &&
249  is_left_or_right_click) {
250  /* try tiling resize, but continue if it doesn’t work */
251  DLOG("tiling resize with fallback\n");
252  if (tiling_resize(con, event, dest, !was_focused))
253  goto done;
254  }
255 
256  if (dest == CLICK_DECORATION && event->detail == XCB_BUTTON_CLICK_RIGHT) {
257  DLOG("floating resize due to decoration right click\n");
258  floating_resize_window(floatingcon, proportional, event);
259  return 1;
260  }
261 
262  if (dest == CLICK_BORDER && is_left_or_right_click) {
263  DLOG("floating resize due to border click\n");
264  floating_resize_window(floatingcon, proportional, event);
265  return 1;
266  }
267 
268  /* 6: dragging, if this was a click on a decoration (which did not lead
269  * to a resize) */
270  if (dest == CLICK_DECORATION && event->detail == XCB_BUTTON_CLICK_LEFT) {
271  floating_drag_window(floatingcon, event, !was_focused);
272  return 1;
273  }
274 
275  goto done;
276  }
277 
278  /* 7: floating modifier pressed, initiate a resize */
279  if (dest == CLICK_INSIDE && mod_pressed && event->detail == XCB_BUTTON_CLICK_RIGHT) {
280  if (floating_mod_on_tiled_client(con, event))
281  return 1;
282  }
283  /* 8: otherwise, check for border/decoration clicks and resize */
284  else if ((dest == CLICK_BORDER || dest == CLICK_DECORATION) &&
285  is_left_or_right_click) {
286  DLOG("Trying to resize (tiling)\n");
287  /* Since we updated the tree (con_activate() above), we need to
288  * re-render the tree before returning to the event loop (drag_pointer()
289  * inside tiling_resize() runs its own event-loop). */
290  tree_render();
291  tiling_resize(con, event, dest, dest == CLICK_DECORATION && !was_focused);
292  }
293 
294 done:
295  xcb_allow_events(conn, XCB_ALLOW_REPLAY_POINTER, event->time);
296  xcb_flush(conn);
297  tree_render();
298 
299  return 0;
300 }
301 
302 /*
303  * The button press X callback. This function determines whether the floating
304  * modifier is pressed and where the user clicked (decoration, border, inside
305  * the window).
306  *
307  * Then, route_click is called on the appropriate con.
308  *
309  */
310 int handle_button_press(xcb_button_press_event_t *event) {
311  Con *con;
312  DLOG("Button %d (state %d) %s on window 0x%08x (child 0x%08x) at (%d, %d) (root %d, %d)\n",
313  event->detail, event->state, (event->response_type == XCB_BUTTON_PRESS ? "press" : "release"),
314  event->event, event->child, event->event_x, event->event_y, event->root_x,
315  event->root_y);
316 
317  last_timestamp = event->time;
318 
319  const uint32_t mod = (config.floating_modifier & 0xFFFF);
320  const bool mod_pressed = (mod != 0 && (event->state & mod) == mod);
321  DLOG("floating_mod = %d, detail = %d\n", mod_pressed, event->detail);
322  if ((con = con_by_window_id(event->event)))
323  return route_click(con, event, mod_pressed, CLICK_INSIDE);
324 
325  if (!(con = con_by_frame_id(event->event))) {
326  /* Run bindings on the root window as well, see #2097. We only run it
327  * if --whole-window was set as that's the equivalent for a normal
328  * window. */
329  if (event->event == root) {
330  Binding *bind = get_binding_from_xcb_event((xcb_generic_event_t *)event);
331  if (bind != NULL && bind->whole_window) {
332  CommandResult *result = run_binding(bind, NULL);
333  command_result_free(result);
334  }
335  }
336 
337  /* If the root window is clicked, find the relevant output from the
338  * click coordinates and focus the output's active workspace. */
339  if (event->event == root && event->response_type == XCB_BUTTON_PRESS) {
340  Con *output, *ws;
341  TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
342  if (con_is_internal(output) ||
343  !rect_contains(output->rect, event->event_x, event->event_y))
344  continue;
345 
346  ws = TAILQ_FIRST(&(output_get_content(output)->focus_head));
347  if (ws != con_get_workspace(focused)) {
348  workspace_show(ws);
349  tree_render();
350  }
351  return 1;
352  }
353  return 0;
354  }
355 
356  ELOG("Clicked into unknown window?!\n");
357  xcb_allow_events(conn, XCB_ALLOW_REPLAY_POINTER, event->time);
358  xcb_flush(conn);
359  return 0;
360  }
361 
362  /* Check if the click was on the decoration of a child */
363  Con *child;
364  TAILQ_FOREACH(child, &(con->nodes_head), nodes) {
365  if (!rect_contains(child->deco_rect, event->event_x, event->event_y))
366  continue;
367 
368  return route_click(child, event, mod_pressed, CLICK_DECORATION);
369  }
370 
371  if (event->child != XCB_NONE) {
372  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");
373  return route_click(con, event, mod_pressed, CLICK_INSIDE);
374  }
375 
376  return route_click(con, event, mod_pressed, CLICK_BORDER);
377 }
Con::parent
struct Con * parent
Definition: data.h:672
workspace_show
void workspace_show(Con *workspace)
Switches to the given workspace.
Definition: workspace.c:446
con_by_window_id
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:647
tree_render
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:449
LOG
#define LOG(fmt,...)
Definition: libi3.h:94
VERT
@ VERT
Definition: data.h:62
Rect::y
uint32_t y
Definition: data.h:178
BORDER_BOTTOM
@ BORDER_BOTTOM
Definition: floating.h:20
AFTER
@ AFTER
Definition: data.h:64
orientation_t
orientation_t
Definition: data.h:60
XCB_BUTTON_CLICK_LEFT
#define XCB_BUTTON_CLICK_LEFT
Mouse buttons.
Definition: libi3.h:28
CommandResult
A struct that contains useful information about the result of a command as a whole (e....
Definition: commands_parser.h:46
Binding::border
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:325
Con::rect
struct Rect rect
Definition: data.h:676
D_LEFT
@ D_LEFT
Definition: data.h:58
XCB_BUTTON_CLICK_RIGHT
#define XCB_BUTTON_CLICK_RIGHT
Definition: libi3.h:30
tiling_resize_for_border
static bool tiling_resize_for_border(Con *con, border_t border, xcb_button_press_event_t *event, bool use_threshold)
Definition: click.c:29
click_destination_t
click_destination_t
Definition: click.c:19
CLICK_BORDER
@ CLICK_BORDER
Definition: click.c:19
XCB_BUTTON_SCROLL_UP
#define XCB_BUTTON_SCROLL_UP
Definition: libi3.h:31
get_binding_from_xcb_event
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:302
Con::layout
layout_t layout
Definition: data.h:750
Con::window_rect
struct Rect window_rect
Definition: data.h:679
CLICK_DECORATION
@ CLICK_DECORATION
Definition: click.c:20
Config::floating_modifier
uint32_t floating_modifier
The modifier which needs to be pressed in combination with your mouse buttons to do things with float...
Definition: configuration.h:219
tiling_resize
static bool tiling_resize(Con *con, xcb_button_press_event_t *event, const click_destination_t dest, bool use_threshold)
Definition: click.c:121
all.h
L_TABBED
@ L_TABBED
Definition: data.h:106
rect_contains
bool rect_contains(Rect rect, uint32_t x, uint32_t y)
Definition: util.c:35
DLOG
#define DLOG(fmt,...)
Definition: libi3.h:104
ELOG
#define ELOG(fmt,...)
Definition: libi3.h:99
con_activate
void con_activate(Con *con)
Sets input focus to the given container and raises it to the top.
Definition: con.c:263
resize_graphical_handler
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::focus_head
focus_head
Definition: data.h:724
TAILQ_FIRST
#define TAILQ_FIRST(head)
Definition: queue.h:336
D_RIGHT
@ D_RIGHT
Definition: data.h:59
resize_find_tiling_participants
bool resize_find_tiling_participants(Con **current, Con **other, direction_t direction, bool both_sides)
Definition: resize.c:70
D_DOWN
@ D_DOWN
Definition: data.h:61
floating_mod_on_tiled_client
static bool floating_mod_on_tiled_client(Con *con, xcb_button_press_event_t *event)
Definition: click.c:82
Con::deco_rect
struct Rect deco_rect
Definition: data.h:682
con_border_style_rect
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:1630
position_t
position_t
Definition: data.h:63
con_get_output
Con * con_get_output(Con *con)
Gets the output container (first container with CT_OUTPUT in hierarchy) this node is on.
Definition: con.c:439
con_is_internal
bool con_is_internal(Con *con)
Returns true if the container is internal, such as __i3_scratch.
Definition: con.c:567
BORDER_RIGHT
@ BORDER_RIGHT
Definition: floating.h:18
Con::nodes_head
nodes_head
Definition: data.h:721
Rect::width
uint32_t width
Definition: data.h:179
L_STACKED
@ L_STACKED
Definition: data.h:105
BEFORE
@ BEFORE
Definition: data.h:63
Binding::whole_window
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:330
Con::type
enum Con::@20 type
focused
struct Con * focused
Definition: tree.c:13
con_by_frame_id
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:685
con_inside_floating
Con * con_inside_floating(Con *con)
Checks if the given container is either floating or inside some floating container.
Definition: con.c:599
XCB_BUTTON_SCROLL_DOWN
#define XCB_BUTTON_SCROLL_DOWN
Definition: libi3.h:32
Binding
Holds a keybinding, consisting of a keycode combined with modifiers and the command which is executed...
Definition: data.h:302
config
Config config
Definition: config.c:17
TAILQ_FOREACH
#define TAILQ_FOREACH(var, head, field)
Definition: queue.h:347
floating_resize_window
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:694
command_result_free
void command_result_free(CommandResult *result)
Frees a CommandResult.
Definition: commands_parser.c:460
Binding::exclude_titlebar
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:334
get_tree_next_sibling
Con * get_tree_next_sibling(Con *con, position_t direction)
Get the previous / next sibling.
Definition: tree.c:627
run_binding
CommandResult * run_binding(Binding *bind, Con *con)
Runs the given binding and handles parse errors.
Definition: bindings.c:815
con_descend_focused
Con * con_descend_focused(Con *con)
Returns the focused con inside this client, descending the tree as far as possible.
Definition: con.c:1516
Con
A 'Con' represents everything from the X11 root window down to a single X11 window.
Definition: data.h:637
route_click
static int route_click(Con *con, xcb_button_press_event_t *event, const bool mod_pressed, const click_destination_t dest)
Definition: click.c:150
BORDER_LEFT
@ BORDER_LEFT
Definition: floating.h:17
border_t
border_t
On which border was the dragging initiated?
Definition: floating.h:17
XCB_BUTTON_SCROLL_RIGHT
#define XCB_BUTTON_SCROLL_RIGHT
Definition: libi3.h:35
CLICK_INSIDE
@ CLICK_INSIDE
Definition: click.c:21
Rect::height
uint32_t height
Definition: data.h:180
con_get_fullscreen_covering_ws
Con * con_get_fullscreen_covering_ws(Con *ws)
Returns the fullscreen node that covers the given workspace if it exists.
Definition: con.c:552
handle_button_press
int handle_button_press(xcb_button_press_event_t *event)
The button press X callback.
Definition: click.c:310
conn
xcb_connection_t * conn
XCB connection and root screen.
Definition: main.c:44
BORDER_TOP
@ BORDER_TOP
Definition: floating.h:19
croot
struct Con * croot
Definition: tree.c:12
Con::name
char * name
Definition: data.h:686
last_timestamp
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:54
HORIZ
@ HORIZ
Definition: data.h:61
root
xcb_window_t root
Definition: main.c:57
Rect::x
uint32_t x
Definition: data.h:177
XCB_BUTTON_SCROLL_LEFT
#define XCB_BUTTON_SCROLL_LEFT
Definition: libi3.h:34
floating_drag_window
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:596
D_UP
@ D_UP
Definition: data.h:60
con_get_workspace
Con * con_get_workspace(Con *con)
Gets the workspace container this node is on.
Definition: con.c:453
output_get_content
Con * output_get_content(Con *output)
Returns the output container below the given output container.
Definition: output.c:16
direction_t
direction_t
Definition: data.h:56
Rect
Stores a rectangle, for example the size of a window, the child window etc.
Definition: data.h:176