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  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);
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  /* We modify the X/Y position in the event so that the divider line is at
66  * the actual position of the border, not at the position of the click. */
67  const orientation_t orientation = ((border == BORDER_LEFT || border == BORDER_RIGHT) ? HORIZ : VERT);
68  if (orientation == HORIZ)
69  event->root_x = second->rect.x;
70  else event->root_y = second->rect.y;
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 <= bsr.x &&
156  event->event_y >= bsr.y && event->event_y <= con->rect.height + bsr.height)
157  return tiling_resize_for_border(con, BORDER_LEFT, event);
158 
159  if (event->event_x >= (con->window_rect.x + con->window_rect.width) &&
160  event->event_y >= bsr.y && event->event_y <= con->rect.height + bsr.height)
161  return tiling_resize_for_border(con, BORDER_RIGHT, event);
162 
163  if (event->event_y >= (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  /* 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  focused_id = XCB_NONE;
194 
195  /* don’t handle dockarea cons, they must not be focused */
196  if (con->parent->type == CT_DOCKAREA)
197  goto done;
198 
199  /* get the floating con */
200  Con *floatingcon = con_inside_floating(con);
201  const bool proportional = (event->state & BIND_SHIFT);
202  const bool in_stacked = (con->parent->layout == L_STACKED || con->parent->layout == L_TABBED);
203 
204  /* 1: see if the user scrolled on the decoration of a stacked/tabbed con */
205  if (in_stacked &&
206  dest == CLICK_DECORATION &&
207  (event->detail == XCB_BUTTON_INDEX_4 ||
208  event->detail == XCB_BUTTON_INDEX_5)) {
209  DLOG("Scrolling on a window decoration\n");
210  orientation_t orientation = (con->parent->layout == L_STACKED ? VERT : HORIZ);
211  /* Focus the currently focused container on the same level that the
212  * user scrolled on. e.g. the tabbed decoration contains
213  * "urxvt | i3: V[xterm geeqie] | firefox",
214  * focus is on the xterm, but the user scrolled on urxvt.
215  * The splitv container will be focused. */
216  Con *focused = con->parent;
217  focused = TAILQ_FIRST(&(focused->focus_head));
218  con_focus(focused);
219  /* To prevent scrolling from going outside the container (see ticket
220  * #557), we first check if scrolling is possible at all. */
221  bool scroll_prev_possible = (TAILQ_PREV(focused, nodes_head, nodes) != NULL);
222  bool scroll_next_possible = (TAILQ_NEXT(focused, nodes) != NULL);
223  if (event->detail == XCB_BUTTON_INDEX_4 && scroll_prev_possible)
224  tree_next('p', orientation);
225  else if (event->detail == XCB_BUTTON_INDEX_5 && scroll_next_possible)
226  tree_next('n', orientation);
227  goto done;
228  }
229 
230  /* 2: focus this con. */
231  con_focus(con);
232 
233  /* 3: For floating containers, we also want to raise them on click.
234  * We will skip handling events on floating cons in fullscreen mode */
235  Con *fs = (ws ? con_get_fullscreen_con(ws, CF_OUTPUT) : NULL);
236  if (floatingcon != NULL && fs != con) {
237  floating_raise_con(floatingcon);
238 
239  /* 4: floating_modifier plus left mouse button drags */
240  if (mod_pressed && event->detail == XCB_BUTTON_INDEX_1) {
241  floating_drag_window(floatingcon, event);
242  return 1;
243  }
244 
245  /* 5: resize (floating) if this was a click on the left/right/bottom
246  * border. also try resizing (tiling) if it was a click on the top
247  * border, but continue if that does not work */
248  if (mod_pressed && event->detail == 3) {
249  DLOG("floating resize due to floatingmodifier\n");
250  floating_resize_window(floatingcon, proportional, event);
251  return 1;
252  }
253 
254  if (!in_stacked && dest == CLICK_DECORATION) {
255  /* try tiling resize, but continue if it doesn’t work */
256  DLOG("tiling resize with fallback\n");
257  if (tiling_resize(con, event, dest))
258  goto done;
259  }
260 
261  if (dest == CLICK_BORDER) {
262  DLOG("floating resize due to border click\n");
263  floating_resize_window(floatingcon, proportional, event);
264  return 1;
265  }
266 
267  /* 6: dragging, if this was a click on a decoration (which did not lead
268  * to a resize) */
269  if (!in_stacked && dest == CLICK_DECORATION) {
270  floating_drag_window(floatingcon, event);
271  return 1;
272  }
273 
274  goto done;
275  }
276 
277  if (in_stacked) {
278  /* for stacked/tabbed cons, the resizing applies to the parent
279  * container */
280  con = con->parent;
281  }
282 
283  /* 7: floating modifier pressed, initiate a resize */
284  if (dest == CLICK_INSIDE && mod_pressed && event->detail == 3) {
285  if (floating_mod_on_tiled_client(con, event))
286  return 1;
287  }
288  /* 8: otherwise, check for border/decoration clicks and resize */
289  else if ((dest == CLICK_BORDER || dest == CLICK_DECORATION) &&
290  (event->detail == XCB_BUTTON_INDEX_1 ||
291  event->detail == XCB_BUTTON_INDEX_3)) {
292  DLOG("Trying to resize (tiling)\n");
293  tiling_resize(con, event, dest);
294  }
295 
296 done:
297  xcb_allow_events(conn, XCB_ALLOW_REPLAY_POINTER, event->time);
298  xcb_flush(conn);
299  tree_render();
300  return 0;
301 }
302 
303 /*
304  * The button press X callback. This function determines whether the floating
305  * modifier is pressed and where the user clicked (decoration, border, inside
306  * the window).
307  *
308  * Then, route_click is called on the appropriate con.
309  *
310  */
311 int handle_button_press(xcb_button_press_event_t *event) {
312  Con *con;
313  DLOG("Button %d pressed on window 0x%08x\n", event->state, event->event);
314 
315  last_timestamp = event->time;
316 
317  const uint32_t mod = config.floating_modifier;
318  const bool mod_pressed = (mod != 0 && (event->state & mod) == mod);
319  DLOG("floating_mod = %d, detail = %d\n", mod_pressed, event->detail);
320  if ((con = con_by_window_id(event->event)))
321  return route_click(con, event, mod_pressed, CLICK_INSIDE);
322 
323  if (!(con = con_by_frame_id(event->event))) {
324  /* If the root window is clicked, find the relevant output from the
325  * click coordinates and focus the output's active workspace. */
326  if (event->event == root) {
327  Con *output, *ws;
328  TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
329  if (con_is_internal(output) ||
330  !rect_contains(output->rect, event->event_x, event->event_y))
331  continue;
332 
333  ws = TAILQ_FIRST(&(output_get_content(output)->focus_head));
334  if (ws != con_get_workspace(focused)) {
335  workspace_show(ws);
336  tree_render();
337  }
338  return 1;
339  }
340  return 0;
341  }
342 
343  ELOG("Clicked into unknown window?!\n");
344  xcb_allow_events(conn, XCB_ALLOW_REPLAY_POINTER, event->time);
345  xcb_flush(conn);
346  return 0;
347  }
348 
349  /* Check if the click was on the decoration of a child */
350  Con *child;
351  TAILQ_FOREACH(child, &(con->nodes_head), nodes) {
352  if (!rect_contains(child->deco_rect, event->event_x, event->event_y))
353  continue;
354 
355  return route_click(child, event, mod_pressed, CLICK_DECORATION);
356  }
357 
358  return route_click(con, event, mod_pressed, CLICK_BORDER);
359 }