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);
163 const bool in_stacked = (con->parent->layout == L_STACKED || con->parent->layout == L_TABBED);
165 /* 1: see if the user scrolled on the decoration of a stacked/tabbed con */
167 dest == CLICK_DECORATION &&
168 (event->detail == XCB_BUTTON_INDEX_4 ||
169 event->detail == XCB_BUTTON_INDEX_5)) {
170 DLOG("Scrolling on a window decoration\n");
171 orientation_t orientation = (con->parent->layout == L_STACKED ? VERT : HORIZ);
172 if (event->detail == XCB_BUTTON_INDEX_4)
173 tree_next('p', orientation);
174 else tree_next('n', orientation);
178 /* 2: focus this con */
181 /* 3: for floating containers, we also want to raise them on click */
182 if (floatingcon != NULL) {
183 floating_raise_con(floatingcon);
185 /* 4: floating_modifier plus left mouse button drags */
186 if (mod_pressed && event->detail == 1) {
187 floating_drag_window(floatingcon, event);
191 /* 5: resize (floating) if this was a click on the left/right/bottom
192 * border. also try resizing (tiling) if it was a click on the top
193 * border, but continue if that does not work */
194 if (mod_pressed && event->detail == 3) {
195 DLOG("floating resize due to floatingmodifier\n");
196 floating_resize_window(floatingcon, proportional, event);
200 if (!in_stacked && dest == CLICK_DECORATION) {
201 /* try tiling resize, but continue if it doesn’t work */
202 DLOG("tiling resize with fallback\n");
203 if (tiling_resize(con, event, dest))
207 if (dest == CLICK_BORDER) {
208 DLOG("floating resize due to border click\n");
209 floating_resize_window(floatingcon, proportional, event);
213 /* 6: dragging, if this was a click on a decoration (which did not lead
215 if (!in_stacked && dest == CLICK_DECORATION) {
216 floating_drag_window(floatingcon, event);
224 /* for stacked/tabbed cons, floating modifier + right click resizes the
225 * parent container */
226 if (mod_pressed && event->detail == 3)
227 if (floating_mod_on_tiled_client(con->parent, event))
232 /* 7: floating modifier pressed, initiate a resize */
233 if (mod_pressed && event->detail == 3) {
234 if (floating_mod_on_tiled_client(con, event))
237 /* 8: otherwise, check for border/decoration clicks and resize */
238 else if (dest == CLICK_BORDER || dest == CLICK_DECORATION) {
239 DLOG("Should trry resizing (tiling)\n");
240 tiling_resize(con, event, dest);
244 xcb_allow_events(conn, XCB_ALLOW_REPLAY_POINTER, event->time);
251 * The button press X callback. This function determines whether the floating
252 * modifier is pressed and where the user clicked (decoration, border, inside
255 * Then, route_click is called on the appropriate con.
258 int handle_button_press(xcb_button_press_event_t *event) {
260 DLOG("Button %d pressed on window 0x%08x\n", event->state, event->event);
262 const uint32_t mod = config.floating_modifier;
263 bool mod_pressed = (mod != 0 && (event->state & mod) == mod);
264 DLOG("floating_mod = %d, detail = %d\n", mod_pressed, event->detail);
265 if ((con = con_by_window_id(event->event)))
266 return route_click(con, event, mod_pressed, CLICK_INSIDE);
268 if (!(con = con_by_frame_id(event->event))) {
269 ELOG("Clicked into unknown window?!\n");
270 xcb_allow_events(conn, XCB_ALLOW_REPLAY_POINTER, event->time);
275 /* Check if the click was on the decoration of a child */
277 TAILQ_FOREACH(child, &(con->nodes_head), nodes) {
278 if (!rect_contains(child->deco_rect, event->event_x, event->event_y))
281 return route_click(child, event, mod_pressed, CLICK_DECORATION);
284 return route_click(con, event, mod_pressed, CLICK_BORDER);