2 #define I3__FILE__ "click.c"
4 * vim:ts=4:sw=4:expandtab
6 * i3 - an improved dynamic tiling window manager
7 * © 2009 Michael Stapelberg and contributors (see also: LICENSE)
9 * click.c: Button press (mouse click) events.
17 #include <xcb/xcb_icccm.h>
19 #include <X11/XKBlib.h>
21 typedef enum { CLICK_BORDER = 0,
23 CLICK_INSIDE = 2 } click_destination_t;
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().
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);
35 direction_t search_direction;
38 search_direction = D_LEFT;
41 search_direction = D_RIGHT;
44 search_direction = D_UP;
47 search_direction = D_DOWN;
54 bool res = resize_find_tiling_participants(&first, &second, search_direction);
56 LOG("No second container in this direction found.\n");
60 assert(first != second);
61 assert(first->parent == second->parent);
63 /* The first container should always be in front of the second container */
64 if (search_direction == D_UP || search_direction == D_LEFT) {
70 const orientation_t orientation = ((border == BORDER_LEFT || border == BORDER_RIGHT) ? HORIZ : VERT);
72 resize_graphical_handler(first, second, orientation, event);
74 DLOG("After resize handler, rendering\n");
80 * Called when the user clicks using the floating_modifier, but the client is in
83 * Returns false if it does not do anything (that is, the click should be sent
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;
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);
99 if (to_right < to_left &&
101 to_right < to_bottom)
102 return tiling_resize_for_border(con, BORDER_RIGHT, event);
104 if (to_left < to_right &&
107 return tiling_resize_for_border(con, BORDER_LEFT, event);
109 if (to_top < to_right &&
112 return tiling_resize_for_border(con, BORDER_TOP, event);
114 if (to_bottom < to_right &&
115 to_bottom < to_left &&
117 return tiling_resize_for_border(con, BORDER_BOTTOM, event);
123 * Finds out which border was clicked on and calls tiling_resize_for_border().
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. */
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;
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");
152 return tiling_resize_for_border(con, BORDER_TOP, event);
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);
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);
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);
170 * Being called by handle_button_press, this function calls the appropriate
171 * functions for resizing/dragging.
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);
179 /* don’t handle dockarea cons, they must not be focused */
180 if (con->parent->type == CT_DOCKAREA)
183 const bool is_left_or_right_click = (event->detail == XCB_BUTTON_INDEX_1 ||
184 event->detail == XCB_BUTTON_INDEX_3);
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);
197 /* ASYNC_POINTER eats the event */
198 xcb_allow_events(conn, XCB_ALLOW_ASYNC_POINTER, event->time);
201 if (result->needs_tree_render)
204 command_result_free(result);
210 /* There is no default behavior for button release events so we are done. */
211 if (event->response_type == XCB_BUTTON_RELEASE) {
215 /* Any click in a workspace should focus that workspace. If the
216 * workspace is on another output we need to do a workspace_show in
217 * order for i3bar (and others) to notice the change in workspace. */
218 Con *ws = con_get_workspace(con);
219 Con *focused_workspace = con_get_workspace(focused);
222 ws = TAILQ_FIRST(&(output_get_content(con_get_output(con))->focus_head));
227 if (ws != focused_workspace)
230 /* get the floating con */
231 Con *floatingcon = con_inside_floating(con);
232 const bool proportional = (event->state & XCB_KEY_BUT_MASK_SHIFT) == XCB_KEY_BUT_MASK_SHIFT;
233 const bool in_stacked = (con->parent->layout == L_STACKED || con->parent->layout == L_TABBED);
235 /* 1: see if the user scrolled on the decoration of a stacked/tabbed con */
237 dest == CLICK_DECORATION &&
238 (event->detail == XCB_BUTTON_INDEX_4 ||
239 event->detail == XCB_BUTTON_INDEX_5)) {
240 DLOG("Scrolling on a window decoration\n");
241 orientation_t orientation = (con->parent->layout == L_STACKED ? VERT : HORIZ);
242 /* Focus the currently focused container on the same level that the
243 * user scrolled on. e.g. the tabbed decoration contains
244 * "urxvt | i3: V[xterm geeqie] | firefox",
245 * focus is on the xterm, but the user scrolled on urxvt.
246 * The splitv container will be focused. */
247 Con *focused = con->parent;
248 focused = TAILQ_FIRST(&(focused->focus_head));
250 /* To prevent scrolling from going outside the container (see ticket
251 * #557), we first check if scrolling is possible at all. */
252 bool scroll_prev_possible = (TAILQ_PREV(focused, nodes_head, nodes) != NULL);
253 bool scroll_next_possible = (TAILQ_NEXT(focused, nodes) != NULL);
254 if (event->detail == XCB_BUTTON_INDEX_4 && scroll_prev_possible)
255 tree_next('p', orientation);
256 else if (event->detail == XCB_BUTTON_INDEX_5 && scroll_next_possible)
257 tree_next('n', orientation);
261 /* 2: focus this con. */
264 /* 3: For floating containers, we also want to raise them on click.
265 * We will skip handling events on floating cons in fullscreen mode */
266 Con *fs = (ws ? con_get_fullscreen_con(ws, CF_OUTPUT) : NULL);
267 if (floatingcon != NULL && fs != con) {
268 floating_raise_con(floatingcon);
270 /* 4: floating_modifier plus left mouse button drags */
271 if (mod_pressed && event->detail == XCB_BUTTON_INDEX_1) {
272 floating_drag_window(floatingcon, event);
276 /* 5: resize (floating) if this was a (left or right) click on the
277 * left/right/bottom border, or a right click on the decoration.
278 * also try resizing (tiling) if it was a click on the top */
279 if (mod_pressed && event->detail == XCB_BUTTON_INDEX_3) {
280 DLOG("floating resize due to floatingmodifier\n");
281 floating_resize_window(floatingcon, proportional, event);
285 if (!in_stacked && dest == CLICK_DECORATION &&
286 is_left_or_right_click) {
287 /* try tiling resize, but continue if it doesn’t work */
288 DLOG("tiling resize with fallback\n");
289 if (tiling_resize(con, event, dest))
293 if (dest == CLICK_DECORATION && event->detail == XCB_BUTTON_INDEX_3) {
294 DLOG("floating resize due to decoration right click\n");
295 floating_resize_window(floatingcon, proportional, event);
299 if (dest == CLICK_BORDER && is_left_or_right_click) {
300 DLOG("floating resize due to border click\n");
301 floating_resize_window(floatingcon, proportional, event);
305 /* 6: dragging, if this was a click on a decoration (which did not lead
307 if (!in_stacked && dest == CLICK_DECORATION &&
308 (event->detail == XCB_BUTTON_INDEX_1)) {
309 floating_drag_window(floatingcon, event);
317 /* for stacked/tabbed cons, the resizing applies to the parent
322 /* 7: floating modifier pressed, initiate a resize */
323 if (dest == CLICK_INSIDE && mod_pressed && event->detail == XCB_BUTTON_INDEX_3) {
324 if (floating_mod_on_tiled_client(con, event))
327 /* 8: otherwise, check for border/decoration clicks and resize */
328 else if ((dest == CLICK_BORDER || dest == CLICK_DECORATION) &&
329 is_left_or_right_click) {
330 DLOG("Trying to resize (tiling)\n");
331 tiling_resize(con, event, dest);
335 xcb_allow_events(conn, XCB_ALLOW_REPLAY_POINTER, event->time);
343 * The button press X callback. This function determines whether the floating
344 * modifier is pressed and where the user clicked (decoration, border, inside
347 * Then, route_click is called on the appropriate con.
350 int handle_button_press(xcb_button_press_event_t *event) {
352 DLOG("Button %d (state %d) %s on window 0x%08x (child 0x%08x) at (%d, %d) (root %d, %d)\n",
353 event->detail, event->state, (event->response_type == XCB_BUTTON_PRESS ? "press" : "release"),
354 event->event, event->child, event->event_x, event->event_y, event->root_x,
357 last_timestamp = event->time;
359 const uint32_t mod = config.floating_modifier;
360 const bool mod_pressed = (mod != 0 && (event->state & mod) == mod);
361 DLOG("floating_mod = %d, detail = %d\n", mod_pressed, event->detail);
362 if ((con = con_by_window_id(event->event)))
363 return route_click(con, event, mod_pressed, CLICK_INSIDE);
365 if (!(con = con_by_frame_id(event->event))) {
366 /* If the root window is clicked, find the relevant output from the
367 * click coordinates and focus the output's active workspace. */
368 if (event->event == root && event->response_type == XCB_BUTTON_PRESS) {
370 TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
371 if (con_is_internal(output) ||
372 !rect_contains(output->rect, event->event_x, event->event_y))
375 ws = TAILQ_FIRST(&(output_get_content(output)->focus_head));
376 if (ws != con_get_workspace(focused)) {
385 ELOG("Clicked into unknown window?!\n");
386 xcb_allow_events(conn, XCB_ALLOW_REPLAY_POINTER, event->time);
391 /* Check if the click was on the decoration of a child */
393 TAILQ_FOREACH(child, &(con->nodes_head), nodes) {
394 if (!rect_contains(child->deco_rect, event->event_x, event->event_y))
397 return route_click(child, event, mod_pressed, CLICK_DECORATION);
400 if (event->child != XCB_NONE) {
401 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");
402 return route_click(con, event, mod_pressed, CLICK_INSIDE);
405 return route_click(con, event, mod_pressed, CLICK_BORDER);