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-2012 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 
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) {
30  DLOG("border = %d, con = %p\n", border, con);
31  char way = (border == BORDER_TOP || border == BORDER_LEFT ? 'p' : 'n');
32  orientation_t orientation = (border == BORDER_TOP || border == BORDER_BOTTOM ? VERT : HORIZ);
33 
34  /* look for a parent container with the right orientation */
35  Con *first = NULL, *second = NULL;
36  Con *resize_con = con;
37  while (resize_con->type != CT_WORKSPACE &&
38  resize_con->type != CT_FLOATING_CON &&
39  con_orientation(resize_con->parent) != orientation)
40  resize_con = resize_con->parent;
41 
42  DLOG("resize_con = %p\n", resize_con);
43  if (resize_con->type != CT_WORKSPACE &&
44  resize_con->type != CT_FLOATING_CON &&
45  con_orientation(resize_con->parent) == orientation) {
46  first = resize_con;
47  second = (way == 'n') ? TAILQ_NEXT(first, nodes) : TAILQ_PREV(first, nodes_head, nodes);
48  if (second == TAILQ_END(&(first->nodes_head))) {
49  second = NULL;
50  }
51  else if (way == 'p') {
52  Con *tmp = first;
53  first = second;
54  second = tmp;
55  }
56  DLOG("first = %p, second = %p, resize_con = %p\n",
57  first, second, resize_con);
58  }
59 
60  if (first == NULL || second == NULL) {
61  DLOG("Resize not possible\n");
62  return false;
63  }
64 
65  assert(first != second);
66  assert(first->parent == second->parent);
67 
68  /* We modify the X/Y position in the event so that the divider line is at
69  * the actual position of the border, not at the position of the click. */
70  if (orientation == HORIZ)
71  event->root_x = second->rect.x;
72  else event->root_y = second->rect.y;
73 
74  resize_graphical_handler(first, second, orientation, event);
75 
76  DLOG("After resize handler, rendering\n");
77  tree_render();
78  return true;
79 }
80 
81 /*
82  * Called when the user clicks using the floating_modifier, but the client is in
83  * tiling layout.
84  *
85  * Returns false if it does not do anything (that is, the click should be sent
86  * to the client).
87  *
88  */
89 static bool floating_mod_on_tiled_client(Con *con, xcb_button_press_event_t *event) {
90  /* The client is in tiling layout. We can still initiate a resize with the
91  * right mouse button, by chosing the border which is the most near one to
92  * the position of the mouse pointer */
93  int to_right = con->rect.width - event->event_x,
94  to_left = event->event_x,
95  to_top = event->event_y,
96  to_bottom = con->rect.height - event->event_y;
97 
98  DLOG("click was %d px to the right, %d px to the left, %d px to top, %d px to bottom\n",
99  to_right, to_left, to_top, to_bottom);
100 
101  if (to_right < to_left &&
102  to_right < to_top &&
103  to_right < to_bottom)
104  return tiling_resize_for_border(con, BORDER_RIGHT, event);
105 
106  if (to_left < to_right &&
107  to_left < to_top &&
108  to_left < to_bottom)
109  return tiling_resize_for_border(con, BORDER_LEFT, event);
110 
111  if (to_top < to_right &&
112  to_top < to_left &&
113  to_top < to_bottom)
114  return tiling_resize_for_border(con, BORDER_TOP, event);
115 
116  if (to_bottom < to_right &&
117  to_bottom < to_left &&
118  to_bottom < to_top)
119  return tiling_resize_for_border(con, BORDER_BOTTOM, event);
120 
121  return false;
122 }
123 
124 /*
125  * Finds out which border was clicked on and calls tiling_resize_for_border().
126  *
127  */
128 static bool tiling_resize(Con *con, xcb_button_press_event_t *event, const click_destination_t dest) {
129  /* check if this was a click on the window border (and on which one) */
130  Rect bsr = con_border_style_rect(con);
131  DLOG("BORDER x = %d, y = %d for con %p, window 0x%08x\n",
132  event->event_x, event->event_y, con, event->event);
133  DLOG("checks for right >= %d\n", con->window_rect.x + con->window_rect.width);
134  if (dest == CLICK_DECORATION) {
135  /* The user clicked on a window decoration. We ignore the following case:
136  * The container is a h-split, tabbed or stacked container with > 1
137  * window. Decorations will end up next to each other and the user
138  * expects to switch to a window by clicking on its decoration. */
139 
140  /* Since the container might either be the child *or* already a split
141  * container (in the case of a nested split container), we need to make
142  * sure that we are dealing with the split container here. */
143  Con *check_con = con;
144  if (con_is_leaf(check_con) && check_con->parent->type == CT_CON)
145  check_con = check_con->parent;
146 
147  if ((check_con->layout == L_STACKED ||
148  check_con->layout == L_TABBED ||
149  con_orientation(check_con) == HORIZ) &&
150  con_num_children(check_con) > 1) {
151  DLOG("Not handling this resize, this container has > 1 child.\n");
152  return false;
153  }
154  return tiling_resize_for_border(con, BORDER_TOP, event);
155  }
156 
157  if (event->event_x >= 0 && event->event_x <= bsr.x &&
158  event->event_y >= bsr.y && event->event_y <= con->rect.height + bsr.height)
159  return tiling_resize_for_border(con, BORDER_LEFT, event);
160 
161  if (event->event_x >= (con->window_rect.x + con->window_rect.width) &&
162  event->event_y >= bsr.y && event->event_y <= con->rect.height + bsr.height)
163  return tiling_resize_for_border(con, BORDER_RIGHT, event);
164 
165  if (event->event_y >= (con->window_rect.y + con->window_rect.height))
166  return tiling_resize_for_border(con, BORDER_BOTTOM, event);
167 
168  return false;
169 }
170 
171 /*
172  * Being called by handle_button_press, this function calls the appropriate
173  * functions for resizing/dragging.
174  *
175  */
176 static int route_click(Con *con, xcb_button_press_event_t *event, const bool mod_pressed, const click_destination_t dest) {
177  DLOG("--> click properties: mod = %d, destination = %d\n", mod_pressed, dest);
178  DLOG("--> OUTCOME = %p\n", con);
179  DLOG("type = %d, name = %s\n", con->type, con->name);
180 
181  /* Any click in a workspace should focus that workspace. If the
182  * workspace is on another output we need to do a workspace_show in
183  * order for i3bar (and others) to notice the change in workspace. */
184  Con *ws = con_get_workspace(con);
185  Con *focused_workspace = con_get_workspace(focused);
186 
187  if (!ws) {
188  ws = TAILQ_FIRST(&(output_get_content(con_get_output(con))->focus_head));
189  if (!ws)
190  goto done;
191  }
192 
193  if (ws != focused_workspace)
194  workspace_show(ws);
195  focused_id = XCB_NONE;
196 
197  /* don’t handle dockarea cons, they must not be focused */
198  if (con->parent->type == CT_DOCKAREA)
199  goto done;
200 
201  /* get the floating con */
202  Con *floatingcon = con_inside_floating(con);
203  const bool proportional = (event->state & BIND_SHIFT);
204  const bool in_stacked = (con->parent->layout == L_STACKED || con->parent->layout == L_TABBED);
205 
206  /* 1: see if the user scrolled on the decoration of a stacked/tabbed con */
207  if (in_stacked &&
208  dest == CLICK_DECORATION &&
209  (event->detail == XCB_BUTTON_INDEX_4 ||
210  event->detail == XCB_BUTTON_INDEX_5)) {
211  DLOG("Scrolling on a window decoration\n");
212  orientation_t orientation = (con->parent->layout == L_STACKED ? VERT : HORIZ);
213  /* Focus the currently focused container on the same level that the
214  * user scrolled on. e.g. the tabbed decoration contains
215  * "urxvt | i3: V[xterm geeqie] | firefox",
216  * focus is on the xterm, but the user scrolled on urxvt.
217  * The splitv container will be focused. */
218  Con *focused = con->parent;
219  focused = TAILQ_FIRST(&(focused->focus_head));
220  con_focus(focused);
221  /* To prevent scrolling from going outside the container (see ticket
222  * #557), we first check if scrolling is possible at all. */
223  bool scroll_prev_possible = (TAILQ_PREV(focused, nodes_head, nodes) != NULL);
224  bool scroll_next_possible = (TAILQ_NEXT(focused, nodes) != NULL);
225  if (event->detail == XCB_BUTTON_INDEX_4 && scroll_prev_possible)
226  tree_next('p', orientation);
227  else if (event->detail == XCB_BUTTON_INDEX_5 && scroll_next_possible)
228  tree_next('n', orientation);
229  goto done;
230  }
231 
232  /* 2: focus this con. */
233  con_focus(con);
234 
235  /* 3: For floating containers, we also want to raise them on click.
236  * We will skip handling events on floating cons in fullscreen mode */
237  Con *fs = (ws ? con_get_fullscreen_con(ws, CF_OUTPUT) : NULL);
238  if (floatingcon != NULL && fs != con) {
239  floating_raise_con(floatingcon);
240 
241  /* 4: floating_modifier plus left mouse button drags */
242  if (mod_pressed && event->detail == XCB_BUTTON_INDEX_1) {
243  floating_drag_window(floatingcon, event);
244  return 1;
245  }
246 
247  /* 5: resize (floating) if this was a click on the left/right/bottom
248  * border. also try resizing (tiling) if it was a click on the top
249  * border, but continue if that does not work */
250  if (mod_pressed && event->detail == 3) {
251  DLOG("floating resize due to floatingmodifier\n");
252  floating_resize_window(floatingcon, proportional, event);
253  return 1;
254  }
255 
256  if (!in_stacked && dest == CLICK_DECORATION) {
257  /* try tiling resize, but continue if it doesn’t work */
258  DLOG("tiling resize with fallback\n");
259  if (tiling_resize(con, event, dest))
260  goto done;
261  }
262 
263  if (dest == CLICK_BORDER) {
264  DLOG("floating resize due to border click\n");
265  floating_resize_window(floatingcon, proportional, event);
266  return 1;
267  }
268 
269  /* 6: dragging, if this was a click on a decoration (which did not lead
270  * to a resize) */
271  if (!in_stacked && dest == CLICK_DECORATION) {
272  floating_drag_window(floatingcon, event);
273  return 1;
274  }
275 
276  goto done;
277  }
278 
279  if (in_stacked) {
280  /* for stacked/tabbed cons, the resizing applies to the parent
281  * container */
282  con = con->parent;
283  }
284 
285  /* 7: floating modifier pressed, initiate a resize */
286  if (dest == CLICK_INSIDE && mod_pressed && event->detail == 3) {
287  if (floating_mod_on_tiled_client(con, event))
288  return 1;
289  }
290  /* 8: otherwise, check for border/decoration clicks and resize */
291  else if ((dest == CLICK_BORDER || dest == CLICK_DECORATION) &&
292  (event->detail == XCB_BUTTON_INDEX_1 ||
293  event->detail == XCB_BUTTON_INDEX_3)) {
294  DLOG("Trying to resize (tiling)\n");
295  tiling_resize(con, event, dest);
296  }
297 
298 done:
299  xcb_allow_events(conn, XCB_ALLOW_REPLAY_POINTER, event->time);
300  xcb_flush(conn);
301  tree_render();
302  return 0;
303 }
304 
305 /*
306  * The button press X callback. This function determines whether the floating
307  * modifier is pressed and where the user clicked (decoration, border, inside
308  * the window).
309  *
310  * Then, route_click is called on the appropriate con.
311  *
312  */
313 int handle_button_press(xcb_button_press_event_t *event) {
314  Con *con;
315  DLOG("Button %d pressed on window 0x%08x\n", event->state, event->event);
316 
317  last_timestamp = event->time;
318 
319  const uint32_t mod = config.floating_modifier;
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  /* If the root window is clicked, find the relevant output from the
327  * click coordinates and focus the output's active workspace. */
328  if (event->event == root) {
329  Con *output, *ws;
330  TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
331  if (con_is_internal(output) ||
332  !rect_contains(output->rect, event->event_x, event->event_y))
333  continue;
334 
335  ws = TAILQ_FIRST(&(output_get_content(output)->focus_head));
336  if (ws != con_get_workspace(focused)) {
337  workspace_show(ws);
338  tree_render();
339  }
340  return 1;
341  }
342  return 0;
343  }
344 
345  ELOG("Clicked into unknown window?!\n");
346  xcb_allow_events(conn, XCB_ALLOW_REPLAY_POINTER, event->time);
347  xcb_flush(conn);
348  return 0;
349  }
350 
351  /* Check if the click was on the decoration of a child */
352  Con *child;
353  TAILQ_FOREACH(child, &(con->nodes_head), nodes) {
354  if (!rect_contains(child->deco_rect, event->event_x, event->event_y))
355  continue;
356 
357  return route_click(child, event, mod_pressed, CLICK_DECORATION);
358  }
359 
360  return route_click(con, event, mod_pressed, CLICK_BORDER);
361 }