2 * vim:ts=4:sw=4:expandtab
4 * i3 - an improved dynamic tiling window manager
5 * © 2009-2011 Michael Stapelberg and contributors (see also: LICENSE)
7 * click.c: Button press (mouse click) events.
15 #include <xcb/xcb_atom.h>
16 #include <xcb/xcb_icccm.h>
18 #include <X11/XKBlib.h>
20 typedef enum { CLICK_BORDER = 0, CLICK_DECORATION = 1, CLICK_INSIDE = 2 } click_destination_t;
23 * Finds the correct pair of first/second cons between the resize will take
24 * place according to the passed border position (top, left, right, bottom),
25 * then calls resize_graphical_handler().
28 static bool tiling_resize_for_border(Con *con, border_t border, const xcb_button_press_event_t *event) {
29 DLOG("border = %d, con = %p\n", border, con);
30 char way = (border == BORDER_TOP || border == BORDER_LEFT ? 'p' : 'n');
31 orientation_t orientation = (border == BORDER_TOP || border == BORDER_BOTTOM ? VERT : HORIZ);
33 /* look for a parent container with the right orientation */
34 Con *first = NULL, *second = NULL;
35 Con *resize_con = con;
36 while (resize_con->type != CT_WORKSPACE &&
37 resize_con->type != CT_FLOATING_CON &&
38 resize_con->parent->orientation != orientation)
39 resize_con = resize_con->parent;
41 DLOG("resize_con = %p\n", resize_con);
42 if (resize_con->type != CT_WORKSPACE &&
43 resize_con->type != CT_FLOATING_CON &&
44 resize_con->parent->orientation == orientation) {
46 second = (way == 'n') ? TAILQ_NEXT(first, nodes) : TAILQ_PREV(first, nodes_head, nodes);
47 if (second == TAILQ_END(&(first->nodes_head))) {
50 else if (way == 'p') {
55 DLOG("first = %p, second = %p, resize_con = %p\n",
56 first, second, resize_con);
59 if (first == NULL || second == NULL) {
60 DLOG("Resize not possible\n");
64 assert(first != second);
65 assert(first->parent == second->parent);
66 resize_graphical_handler(first, second, orientation, event);
69 DLOG("After resize handler, rendering\n");
75 * Called when the user clicks using the floating_modifier, but the client is in
78 * Returns false if it does not do anything (that is, the click should be sent
82 static bool floating_mod_on_tiled_client(Con *con, const 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;
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);
94 if (to_right < to_left &&
97 return tiling_resize_for_border(con, BORDER_RIGHT, event);
99 if (to_left < to_right &&
102 return tiling_resize_for_border(con, BORDER_LEFT, event);
104 if (to_top < to_right &&
107 return tiling_resize_for_border(con, BORDER_TOP, event);
109 if (to_bottom < to_right &&
110 to_bottom < to_left &&
112 return tiling_resize_for_border(con, BORDER_BOTTOM, event);
118 * Finds out which border was clicked on and calls tiling_resize_for_border().
121 static bool tiling_resize(Con *con, const xcb_button_press_event_t *event, const click_destination_t dest) {
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 /* The user clicked on a window decoration. We ignore the following case:
129 * The container is a h-split, tabbed or stacked container with > 1
130 * window. Decorations will end up next to each other and the user
131 * expects to switch to a window by clicking on its decoration. */
133 /* Since the container might either be the child *or* already a split
134 * container (in the case of a nested split container), we need to make
135 * sure that we are dealing with the split container here. */
136 Con *check_con = con;
137 if (con_is_leaf(check_con) && check_con->parent->type == CT_CON)
138 check_con = check_con->parent;
140 if ((check_con->layout == L_STACKED ||
141 check_con->layout == L_TABBED ||
142 check_con->orientation == HORIZ) &&
143 con_num_children(check_con) > 1) {
144 DLOG("Not handling this resize, this container has > 1 child.\n");
147 return tiling_resize_for_border(con, BORDER_TOP, event);
150 if (event->event_x >= 0 && event->event_x <= bsr.x &&
151 event->event_y >= bsr.y && event->event_y <= con->rect.height + bsr.height)
152 return tiling_resize_for_border(con, BORDER_LEFT, event);
154 if (event->event_x >= (con->window_rect.x + con->window_rect.width) &&
155 event->event_y >= bsr.y && event->event_y <= con->rect.height + bsr.height)
156 return tiling_resize_for_border(con, BORDER_RIGHT, event);
158 if (event->event_y >= (con->window_rect.y + con->window_rect.height))
159 return tiling_resize_for_border(con, BORDER_BOTTOM, event);
165 * Being called by handle_button_press, this function calls the appropriate
166 * functions for resizing/dragging.
169 static int route_click(Con *con, const xcb_button_press_event_t *event, const bool mod_pressed, const click_destination_t dest) {
170 DLOG("--> click properties: mod = %d, destination = %d\n", mod_pressed, dest);
171 DLOG("--> OUTCOME = %p\n", con);
172 DLOG("type = %d, name = %s\n", con->type, con->name);
174 /* don’t handle dockarea cons, they must not be focused */
175 if (con->parent->type == CT_DOCKAREA)
178 /* get the floating con */
179 Con *floatingcon = con_inside_floating(con);
180 const bool proportional = (event->state & BIND_SHIFT);
181 const bool in_stacked = (con->parent->layout == L_STACKED || con->parent->layout == L_TABBED);
183 /* 1: see if the user scrolled on the decoration of a stacked/tabbed con */
185 dest == CLICK_DECORATION &&
186 (event->detail == XCB_BUTTON_INDEX_4 ||
187 event->detail == XCB_BUTTON_INDEX_5)) {
188 DLOG("Scrolling on a window decoration\n");
189 orientation_t orientation = (con->parent->layout == L_STACKED ? VERT : HORIZ);
190 if (event->detail == XCB_BUTTON_INDEX_4)
191 tree_next('p', orientation);
192 else tree_next('n', orientation);
196 /* 2: focus this con */
199 /* 3: For floating containers, we also want to raise them on click.
200 * We will skip handling events on floating cons in fullscreen mode */
201 Con *ws = con_get_workspace(con);
202 Con *fs = (ws ? con_get_fullscreen_con(ws, CF_OUTPUT) : NULL);
203 if (floatingcon != NULL && fs == NULL) {
204 floating_raise_con(floatingcon);
206 /* 4: floating_modifier plus left mouse button drags */
207 if (mod_pressed && event->detail == 1) {
208 floating_drag_window(floatingcon, event);
212 /* 5: resize (floating) if this was a click on the left/right/bottom
213 * border. also try resizing (tiling) if it was a click on the top
214 * border, but continue if that does not work */
215 if (mod_pressed && event->detail == 3) {
216 DLOG("floating resize due to floatingmodifier\n");
217 floating_resize_window(floatingcon, proportional, event);
221 if (!in_stacked && dest == CLICK_DECORATION) {
222 /* try tiling resize, but continue if it doesn’t work */
223 DLOG("tiling resize with fallback\n");
224 if (tiling_resize(con, event, dest))
228 if (dest == CLICK_BORDER) {
229 DLOG("floating resize due to border click\n");
230 floating_resize_window(floatingcon, proportional, event);
234 /* 6: dragging, if this was a click on a decoration (which did not lead
236 if (!in_stacked && dest == CLICK_DECORATION) {
237 floating_drag_window(floatingcon, event);
245 /* for stacked/tabbed cons, the resizing applies to the parent
250 /* 7: floating modifier pressed, initiate a resize */
251 if (mod_pressed && event->detail == 3) {
252 if (floating_mod_on_tiled_client(con, event))
255 /* 8: otherwise, check for border/decoration clicks and resize */
256 else if (dest == CLICK_BORDER || dest == CLICK_DECORATION) {
257 DLOG("Trying to resize (tiling)\n");
258 tiling_resize(con, event, dest);
262 xcb_allow_events(conn, XCB_ALLOW_REPLAY_POINTER, event->time);
269 * The button press X callback. This function determines whether the floating
270 * modifier is pressed and where the user clicked (decoration, border, inside
273 * Then, route_click is called on the appropriate con.
276 int handle_button_press(xcb_button_press_event_t *event) {
278 DLOG("Button %d pressed on window 0x%08x\n", event->state, event->event);
280 last_timestamp = event->time;
282 const uint32_t mod = config.floating_modifier;
283 const bool mod_pressed = (mod != 0 && (event->state & mod) == mod);
284 DLOG("floating_mod = %d, detail = %d\n", mod_pressed, event->detail);
285 if ((con = con_by_window_id(event->event)))
286 return route_click(con, event, mod_pressed, CLICK_INSIDE);
288 if (!(con = con_by_frame_id(event->event))) {
289 ELOG("Clicked into unknown window?!\n");
290 xcb_allow_events(conn, XCB_ALLOW_REPLAY_POINTER, event->time);
295 /* Check if the click was on the decoration of a child */
297 TAILQ_FOREACH(child, &(con->nodes_head), nodes) {
298 if (!rect_contains(child->deco_rect, event->event_x, event->event_y))
301 return route_click(child, event, mod_pressed, CLICK_DECORATION);
304 return route_click(con, event, mod_pressed, CLICK_BORDER);