2 * vim:ts=4:sw=4:expandtab
4 * i3 - an improved dynamic tiling window manager
5 * © 2009 Michael Stapelberg and contributors (see also: LICENSE)
7 * click.c: Button press (mouse click) events.
15 #include <xcb/xcb_icccm.h>
17 #include <X11/XKBlib.h>
19 typedef enum { CLICK_BORDER = 0,
21 CLICK_INSIDE = 2 } click_destination_t;
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().
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);
33 direction_t search_direction;
36 search_direction = D_LEFT;
39 search_direction = D_RIGHT;
42 search_direction = D_UP;
45 search_direction = D_DOWN;
52 bool res = resize_find_tiling_participants(&first, &second, search_direction, false);
54 LOG("No second container in this direction found.\n");
58 assert(first != second);
59 assert(first->parent == second->parent);
61 /* The first container should always be in front of the second container */
62 if (search_direction == D_UP || search_direction == D_LEFT) {
68 const orientation_t orientation = ((border == BORDER_LEFT || border == BORDER_RIGHT) ? HORIZ : VERT);
70 resize_graphical_handler(first, second, orientation, event);
72 DLOG("After resize handler, rendering\n");
78 * Called when the user clicks using the floating_modifier, but the client is in
81 * Returns false if it does not do anything (that is, the click should be sent
85 static bool floating_mod_on_tiled_client(Con *con, xcb_button_press_event_t *event) {
86 /* The client is in tiling layout. We can still initiate a resize with the
87 * right mouse button, by chosing the border which is the most near one to
88 * the position of the mouse pointer */
89 int to_right = con->rect.width - event->event_x,
90 to_left = event->event_x,
91 to_top = event->event_y,
92 to_bottom = con->rect.height - event->event_y;
94 DLOG("click was %d px to the right, %d px to the left, %d px to top, %d px to bottom\n",
95 to_right, to_left, to_top, to_bottom);
97 if (to_right < to_left &&
100 return tiling_resize_for_border(con, BORDER_RIGHT, event);
102 if (to_left < to_right &&
105 return tiling_resize_for_border(con, BORDER_LEFT, event);
107 if (to_top < to_right &&
110 return tiling_resize_for_border(con, BORDER_TOP, event);
112 if (to_bottom < to_right &&
113 to_bottom < to_left &&
115 return tiling_resize_for_border(con, BORDER_BOTTOM, event);
121 * Finds out which border was clicked on and calls tiling_resize_for_border().
124 static bool tiling_resize(Con *con, xcb_button_press_event_t *event, const click_destination_t dest) {
125 /* check if this was a click on the window border (and on which one) */
126 Rect bsr = con_border_style_rect(con);
127 DLOG("BORDER x = %d, y = %d for con %p, window 0x%08x\n",
128 event->event_x, event->event_y, con, event->event);
129 DLOG("checks for right >= %d\n", con->window_rect.x + con->window_rect.width);
130 if (dest == CLICK_DECORATION) {
131 /* The user clicked on a window decoration. We ignore the following case:
132 * The container is a h-split, tabbed or stacked container with > 1
133 * window. Decorations will end up next to each other and the user
134 * expects to switch to a window by clicking on its decoration. */
136 /* Since the container might either be the child *or* already a split
137 * container (in the case of a nested split container), we need to make
138 * sure that we are dealing with the split container here. */
139 Con *check_con = con;
140 if (con_is_leaf(check_con) && check_con->parent->type == CT_CON)
141 check_con = check_con->parent;
143 if ((check_con->layout == L_STACKED ||
144 check_con->layout == L_TABBED ||
145 con_orientation(check_con) == HORIZ) &&
146 con_num_children(check_con) > 1) {
147 DLOG("Not handling this resize, this container has > 1 child.\n");
150 return tiling_resize_for_border(con, BORDER_TOP, event);
153 if (event->event_x >= 0 && event->event_x <= (int32_t)bsr.x &&
154 event->event_y >= (int32_t)bsr.y && event->event_y <= (int32_t)(con->rect.height + bsr.height))
155 return tiling_resize_for_border(con, BORDER_LEFT, event);
157 if (event->event_x >= (int32_t)(con->window_rect.x + con->window_rect.width) &&
158 event->event_y >= (int32_t)bsr.y && event->event_y <= (int32_t)(con->rect.height + bsr.height))
159 return tiling_resize_for_border(con, BORDER_RIGHT, event);
161 if (event->event_y >= (int32_t)(con->window_rect.y + con->window_rect.height))
162 return tiling_resize_for_border(con, BORDER_BOTTOM, event);
168 * Being called by handle_button_press, this function calls the appropriate
169 * functions for resizing/dragging.
172 static int route_click(Con *con, xcb_button_press_event_t *event, const bool mod_pressed, const click_destination_t dest) {
173 DLOG("--> click properties: mod = %d, destination = %d\n", mod_pressed, dest);
174 DLOG("--> OUTCOME = %p\n", con);
175 DLOG("type = %d, name = %s\n", con->type, con->name);
177 /* don’t handle dockarea cons, they must not be focused */
178 if (con->parent->type == CT_DOCKAREA)
181 const bool is_left_or_right_click = (event->detail == XCB_BUTTON_CLICK_LEFT ||
182 event->detail == XCB_BUTTON_CLICK_RIGHT);
184 /* if the user has bound an action to this click, it should override the
185 * default behavior. */
186 if (dest == CLICK_DECORATION || dest == CLICK_INSIDE || dest == CLICK_BORDER) {
187 Binding *bind = get_binding_from_xcb_event((xcb_generic_event_t *)event);
189 if (bind != NULL && ((dest == CLICK_DECORATION && !bind->exclude_titlebar) ||
190 (dest == CLICK_INSIDE && bind->whole_window) ||
191 (dest == CLICK_BORDER && bind->border))) {
192 CommandResult *result = run_binding(bind, con);
194 /* ASYNC_POINTER eats the event */
195 xcb_allow_events(conn, XCB_ALLOW_ASYNC_POINTER, event->time);
198 command_result_free(result);
203 /* There is no default behavior for button release events so we are done. */
204 if (event->response_type == XCB_BUTTON_RELEASE) {
208 /* Any click in a workspace should focus that workspace. If the
209 * workspace is on another output we need to do a workspace_show in
210 * order for i3bar (and others) to notice the change in workspace. */
211 Con *ws = con_get_workspace(con);
212 Con *focused_workspace = con_get_workspace(focused);
215 ws = TAILQ_FIRST(&(output_get_content(con_get_output(con))->focus_head));
220 if (ws != focused_workspace)
223 /* get the floating con */
224 Con *floatingcon = con_inside_floating(con);
225 const bool proportional = (event->state & XCB_KEY_BUT_MASK_SHIFT) == XCB_KEY_BUT_MASK_SHIFT;
226 const bool in_stacked = (con->parent->layout == L_STACKED || con->parent->layout == L_TABBED);
228 /* 1: see if the user scrolled on the decoration of a stacked/tabbed con */
230 dest == CLICK_DECORATION &&
231 (event->detail == XCB_BUTTON_SCROLL_UP ||
232 event->detail == XCB_BUTTON_SCROLL_DOWN ||
233 event->detail == XCB_BUTTON_SCROLL_LEFT ||
234 event->detail == XCB_BUTTON_SCROLL_RIGHT)) {
235 DLOG("Scrolling on a window decoration\n");
236 orientation_t orientation = (con->parent->layout == L_STACKED ? VERT : HORIZ);
237 /* Focus the currently focused container on the same level that the
238 * user scrolled on. e.g. the tabbed decoration contains
239 * "urxvt | i3: V[xterm geeqie] | firefox",
240 * focus is on the xterm, but the user scrolled on urxvt.
241 * The splitv container will be focused. */
242 Con *focused = con->parent;
243 focused = TAILQ_FIRST(&(focused->focus_head));
244 con_activate(focused);
245 /* To prevent scrolling from going outside the container (see ticket
246 * #557), we first check if scrolling is possible at all. */
247 bool scroll_prev_possible = (TAILQ_PREV(focused, nodes_head, nodes) != NULL);
248 bool scroll_next_possible = (TAILQ_NEXT(focused, nodes) != NULL);
249 if ((event->detail == XCB_BUTTON_SCROLL_UP || event->detail == XCB_BUTTON_SCROLL_LEFT) && scroll_prev_possible) {
250 tree_next('p', orientation);
251 } else if ((event->detail == XCB_BUTTON_SCROLL_DOWN || event->detail == XCB_BUTTON_SCROLL_RIGHT) && scroll_next_possible) {
252 tree_next('n', orientation);
258 /* 2: focus this con. */
261 /* 3: For floating containers, we also want to raise them on click.
262 * We will skip handling events on floating cons in fullscreen mode */
263 Con *fs = (ws ? con_get_fullscreen_con(ws, CF_OUTPUT) : NULL);
264 if (floatingcon != NULL && fs != con) {
265 /* 4: floating_modifier plus left mouse button drags */
266 if (mod_pressed && event->detail == XCB_BUTTON_CLICK_LEFT) {
267 floating_drag_window(floatingcon, event);
271 /* 5: resize (floating) if this was a (left or right) click on the
272 * left/right/bottom border, or a right click on the decoration.
273 * also try resizing (tiling) if it was a click on the top */
274 if (mod_pressed && event->detail == XCB_BUTTON_CLICK_RIGHT) {
275 DLOG("floating resize due to floatingmodifier\n");
276 floating_resize_window(floatingcon, proportional, event);
280 if (!in_stacked && dest == CLICK_DECORATION &&
281 is_left_or_right_click) {
282 /* try tiling resize, but continue if it doesn’t work */
283 DLOG("tiling resize with fallback\n");
284 if (tiling_resize(con, event, dest))
288 if (dest == CLICK_DECORATION && event->detail == XCB_BUTTON_CLICK_RIGHT) {
289 DLOG("floating resize due to decoration right click\n");
290 floating_resize_window(floatingcon, proportional, event);
294 if (dest == CLICK_BORDER && is_left_or_right_click) {
295 DLOG("floating resize due to border click\n");
296 floating_resize_window(floatingcon, proportional, event);
300 /* 6: dragging, if this was a click on a decoration (which did not lead
302 if (!in_stacked && dest == CLICK_DECORATION &&
303 (event->detail == XCB_BUTTON_CLICK_LEFT)) {
304 floating_drag_window(floatingcon, event);
312 /* for stacked/tabbed cons, the resizing applies to the parent
317 /* 7: floating modifier pressed, initiate a resize */
318 if (dest == CLICK_INSIDE && mod_pressed && event->detail == XCB_BUTTON_CLICK_RIGHT) {
319 if (floating_mod_on_tiled_client(con, event))
322 /* 8: otherwise, check for border/decoration clicks and resize */
323 else if ((dest == CLICK_BORDER || dest == CLICK_DECORATION) &&
324 is_left_or_right_click) {
325 DLOG("Trying to resize (tiling)\n");
326 tiling_resize(con, event, dest);
330 xcb_allow_events(conn, XCB_ALLOW_REPLAY_POINTER, event->time);
338 * The button press X callback. This function determines whether the floating
339 * modifier is pressed and where the user clicked (decoration, border, inside
342 * Then, route_click is called on the appropriate con.
345 int handle_button_press(xcb_button_press_event_t *event) {
347 DLOG("Button %d (state %d) %s on window 0x%08x (child 0x%08x) at (%d, %d) (root %d, %d)\n",
348 event->detail, event->state, (event->response_type == XCB_BUTTON_PRESS ? "press" : "release"),
349 event->event, event->child, event->event_x, event->event_y, event->root_x,
352 last_timestamp = event->time;
354 const uint32_t mod = (config.floating_modifier & 0xFFFF);
355 const bool mod_pressed = (mod != 0 && (event->state & mod) == mod);
356 DLOG("floating_mod = %d, detail = %d\n", mod_pressed, event->detail);
357 if ((con = con_by_window_id(event->event)))
358 return route_click(con, event, mod_pressed, CLICK_INSIDE);
360 if (!(con = con_by_frame_id(event->event))) {
361 /* Run bindings on the root window as well, see #2097. We only run it
362 * if --whole-window was set as that's the equivalent for a normal
364 if (event->event == root) {
365 Binding *bind = get_binding_from_xcb_event((xcb_generic_event_t *)event);
366 if (bind != NULL && bind->whole_window) {
367 CommandResult *result = run_binding(bind, NULL);
368 command_result_free(result);
372 /* If the root window is clicked, find the relevant output from the
373 * click coordinates and focus the output's active workspace. */
374 if (event->event == root && event->response_type == XCB_BUTTON_PRESS) {
376 TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
377 if (con_is_internal(output) ||
378 !rect_contains(output->rect, event->event_x, event->event_y))
381 ws = TAILQ_FIRST(&(output_get_content(output)->focus_head));
382 if (ws != con_get_workspace(focused)) {
391 ELOG("Clicked into unknown window?!\n");
392 xcb_allow_events(conn, XCB_ALLOW_REPLAY_POINTER, event->time);
397 /* Check if the click was on the decoration of a child */
399 TAILQ_FOREACH(child, &(con->nodes_head), nodes) {
400 if (!rect_contains(child->deco_rect, event->event_x, event->event_y))
403 return route_click(child, event, mod_pressed, CLICK_DECORATION);
406 if (event->child != XCB_NONE) {
407 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");
408 return route_click(con, event, mod_pressed, CLICK_INSIDE);
411 return route_click(con, event, mod_pressed, CLICK_BORDER);