2 * vim:ts=4:sw=4:expandtab
4 * i3 - an improved dynamic tiling window manager
6 * © 2009-2011 Michael Stapelberg and contributors
8 * See file LICENSE for license information.
10 * src/click.c: Contains the handlers for button press (mouse click) events
11 * because they are quite large.
17 #include <xcb/xcb_atom.h>
18 #include <xcb/xcb_icccm.h>
20 #include <X11/XKBlib.h>
25 typedef enum { CLICK_BORDER = 0, CLICK_DECORATION = 1, CLICK_INSIDE = 2 } click_destination_t;
28 * Finds the correct pair of first/second cons between the resize will take
29 * place according to the passed border position (top, left, right, bottom),
30 * then calls resize_graphical_handler().
33 static bool tiling_resize_for_border(Con *con, border_t border, xcb_button_press_event_t *event) {
34 DLOG("border = %d\n", border);
35 char way = (border == BORDER_TOP || border == BORDER_LEFT ? 'p' : 'n');
36 orientation_t orientation = (border == BORDER_TOP || border == BORDER_BOTTOM ? VERT : HORIZ);
38 /* look for a parent container with the right orientation */
39 Con *first = NULL, *second = NULL;
40 Con *resize_con = con;
41 while (resize_con->type != CT_WORKSPACE &&
42 resize_con->type != CT_FLOATING_CON &&
43 resize_con->parent->orientation != orientation)
44 resize_con = resize_con->parent;
46 if (resize_con->type != CT_WORKSPACE &&
47 resize_con->type != CT_FLOATING_CON &&
48 resize_con->parent->orientation == orientation) {
50 second = (way == 'n') ? TAILQ_NEXT(first, nodes) : TAILQ_PREV(first, nodes_head, nodes);
51 if (second == TAILQ_END(&(first->nodes_head))) {
54 else if (way == 'p') {
61 if (first == NULL || second == NULL) {
62 DLOG("Resize not possible\n");
66 assert(first != second);
67 assert(first->parent == second->parent);
68 resize_graphical_handler(first, second, orientation, event);
71 DLOG("After resize handler, rendering\n");
77 * Called when the user clicks using the floating_modifier, but the client is in
80 * Returns false if it does not do anything (that is, the click should be sent
84 static bool floating_mod_on_tiled_client(Con *con, xcb_button_press_event_t *event) {
85 /* The client is in tiling layout. We can still initiate a resize with the
86 * right mouse button, by chosing the border which is the most near one to
87 * the position of the mouse pointer */
88 int to_right = con->rect.width - event->event_x,
89 to_left = event->event_x,
90 to_top = event->event_y,
91 to_bottom = con->rect.height - event->event_y;
93 DLOG("click was %d px to the right, %d px to the left, %d px to top, %d px to bottom\n",
94 to_right, to_left, to_top, to_bottom);
96 if (to_right < to_left &&
99 return tiling_resize_for_border(con, BORDER_RIGHT, event);
101 if (to_left < to_right &&
104 return tiling_resize_for_border(con, BORDER_LEFT, event);
106 if (to_top < to_right &&
109 return tiling_resize_for_border(con, BORDER_TOP, event);
111 if (to_bottom < to_right &&
112 to_bottom < to_left &&
114 return tiling_resize_for_border(con, BORDER_BOTTOM, event);
120 * Finds out which border was clicked on and calls tiling_resize_for_border().
123 static bool tiling_resize(Con *con, xcb_button_press_event_t *event, click_destination_t dest) {
124 /* check if this was a click on the window border (and on which one) */
125 Rect bsr = con_border_style_rect(con);
126 DLOG("BORDER x = %d, y = %d for con %p, window 0x%08x\n",
127 event->event_x, event->event_y, con, event->event);
128 DLOG("checks for right >= %d\n", con->window_rect.x + con->window_rect.width);
129 if (dest == CLICK_DECORATION)
130 return tiling_resize_for_border(con, BORDER_TOP, event);
132 if (event->event_x >= 0 && event->event_x <= bsr.x &&
133 event->event_y >= bsr.y && event->event_y <= con->rect.height + bsr.height)
134 return tiling_resize_for_border(con, BORDER_LEFT, event);
136 if (event->event_x >= (con->window_rect.x + con->window_rect.width) &&
137 event->event_y >= bsr.y && event->event_y <= con->rect.height + bsr.height)
138 return tiling_resize_for_border(con, BORDER_RIGHT, event);
140 if (event->event_y >= (con->window_rect.y + con->window_rect.height))
141 return tiling_resize_for_border(con, BORDER_BOTTOM, event);
147 * Being called by handle_button_press, this function calls the appropriate
148 * functions for resizing/dragging.
151 static int route_click(Con *con, xcb_button_press_event_t *event, bool mod_pressed, click_destination_t dest) {
152 DLOG("--> click properties: mod = %d, destination = %d\n", mod_pressed, dest);
153 DLOG("--> OUTCOME = %p\n", con);
154 DLOG("type = %d, name = %s\n", con->type, con->name);
156 /* don’t handle dockarea cons, they must not be focused */
157 if (con->parent->type == CT_DOCKAREA)
160 /* get the floating con */
161 Con *floatingcon = con_inside_floating(con);
162 const bool proportional = (event->state & BIND_SHIFT);
164 /* 1: focus this con */
167 const bool in_stacked = (con->parent->layout == L_STACKED || con->parent->layout == L_TABBED);
169 /* 2: for floating containers, we also want to raise them on click */
170 if (floatingcon != NULL) {
171 floating_raise_con(floatingcon);
173 /* 3: floating_modifier plus left mouse button drags */
174 if (mod_pressed && event->detail == 1) {
175 floating_drag_window(floatingcon, event);
179 /* 3: resize (floating) if this was a click on the left/right/bottom
180 * border. also try resizing (tiling) if it was a click on the top
181 * border, but continue if that does not work */
182 if (mod_pressed && event->detail == 3) {
183 DLOG("floating resize due to floatingmodifier\n");
184 floating_resize_window(floatingcon, proportional, event);
188 if (!in_stacked && dest == CLICK_DECORATION) {
189 /* try tiling resize, but continue if it doesn’t work */
190 DLOG("tiling resize with fallback\n");
191 if (tiling_resize(con, event, dest))
195 if (dest == CLICK_BORDER) {
196 DLOG("floating resize due to border click\n");
197 floating_resize_window(floatingcon, proportional, event);
201 /* 4: dragging, if this was a click on a decoration (which did not lead
203 if (!in_stacked && dest == CLICK_DECORATION) {
204 floating_drag_window(floatingcon, event);
212 /* for stacked/tabbed cons, floating modifier + right click resizes the
213 * parent container */
214 if (mod_pressed && event->detail == 3)
215 if (floating_mod_on_tiled_client(con->parent, event))
220 /* 3: floating modifier pressed, initiate a resize */
221 if (mod_pressed && event->detail == 3) {
222 if (floating_mod_on_tiled_client(con, event))
225 /* 4: otherwise, check for border/decoration clicks and resize */
226 else if (dest == CLICK_BORDER || dest == CLICK_DECORATION) {
227 DLOG("Should trry resizing (tiling)\n");
228 tiling_resize(con, event, dest);
232 xcb_allow_events(conn, XCB_ALLOW_REPLAY_POINTER, event->time);
239 * The button press X callback. This function determines whether the floating
240 * modifier is pressed and where the user clicked (decoration, border, inside
243 * Then, route_click is called on the appropriate con.
246 int handle_button_press(xcb_button_press_event_t *event) {
248 DLOG("Button %d pressed on window 0x%08x\n", event->state, event->event);
250 const uint32_t mod = config.floating_modifier;
251 bool mod_pressed = (mod != 0 && (event->state & mod) == mod);
252 DLOG("floating_mod = %d, detail = %d\n", mod_pressed, event->detail);
253 if ((con = con_by_window_id(event->event)))
254 return route_click(con, event, mod_pressed, CLICK_INSIDE);
256 if (!(con = con_by_frame_id(event->event))) {
257 ELOG("Clicked into unknown window?!\n");
258 xcb_allow_events(conn, XCB_ALLOW_REPLAY_POINTER, event->time);
263 /* Check if the click was on the decoration of a child */
265 TAILQ_FOREACH(child, &(con->nodes_head), nodes) {
266 if (!rect_contains(child->deco_rect, event->event_x, event->event_y))
269 return route_click(child, event, mod_pressed, CLICK_DECORATION);
272 return route_click(con, event, mod_pressed, CLICK_BORDER);