2 * vim:ts=4:sw=4:expandtab
4 * i3 - an improved dynamic tiling window manager
6 * © 2009-2010 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 static struct Stack_Window *get_stack_window(xcb_window_t window_id) {
26 struct Stack_Window *current;
28 SLIST_FOREACH(current, &stack_wins, stack_windows) {
29 if (current->window != window_id)
39 * Checks if the button press was on a stack window, handles focus setting and returns true
40 * if so, or false otherwise.
43 static bool button_press_stackwin(xcb_connection_t *conn, xcb_button_press_event_t *event) {
44 struct Stack_Window *stack_win;
46 /* If we find a corresponding stack window, we can handle the event */
47 if ((stack_win = get_stack_window(event->event)) == NULL)
50 /* A stack window was clicked, we check if it was button4 or button5
51 which are scroll up / scroll down. */
52 if (event->detail == XCB_BUTTON_INDEX_4 || event->detail == XCB_BUTTON_INDEX_5) {
53 direction_t direction = (event->detail == XCB_BUTTON_INDEX_4 ? D_UP : D_DOWN);
54 focus_window_in_container(conn, CUR_CELL, direction);
58 /* It was no scrolling, so we calculate the destination client by
59 dividing the Y position of the event through the height of a window
60 decoration and then set the focus to this client. */
61 i3Font *font = load_font(conn, config.font);
62 int decoration_height = (font->height + 2 + 2);
63 int destination = (event->event_y / decoration_height),
67 Container *container = stack_win->container;
69 CIRCLEQ_FOREACH(client, &(container->clients), clients)
72 /* If we don’t have any clients in this container, we cannot do
73 * anything useful anyways. */
77 if (container->mode == MODE_TABBED)
78 destination = (event->event_x / (container->width / num_clients));
79 else if (container->mode == MODE_STACK &&
80 container->stack_limit != STACK_LIMIT_NONE) {
81 if (container->stack_limit == STACK_LIMIT_COLS) {
82 int wrap = ceil((float)num_clients / container->stack_limit_value);
83 int clicked_column = (event->event_x / (stack_win->rect.width / container->stack_limit_value));
84 int clicked_row = (event->event_y / decoration_height);
85 DLOG("clicked on column %d, row %d\n", clicked_column, clicked_row);
86 destination = (wrap * clicked_column) + clicked_row;
88 int width = (stack_win->rect.width / ceil((float)num_clients / container->stack_limit_value));
89 int clicked_column = (event->event_x / width);
90 int clicked_row = (event->event_y / decoration_height);
91 DLOG("clicked on column %d, row %d\n", clicked_column, clicked_row);
92 destination = (container->stack_limit_value * clicked_column) + clicked_row;
96 DLOG("Click on stack_win for client %d\n", destination);
97 CIRCLEQ_FOREACH(client, &(stack_win->container->clients), clients)
98 if (c++ == destination) {
99 set_focus(conn, client, true);
107 * Checks if the button press was on a bar, switches to the workspace and returns true
108 * if so, or false otherwise.
111 static bool button_press_bar(xcb_connection_t *conn, xcb_button_press_event_t *event) {
113 TAILQ_FOREACH(output, &outputs, outputs) {
114 if (output->bar != event->event)
117 DLOG("Click on a bar\n");
119 /* Check if the button was one of button4 or button5 (scroll up / scroll down) */
120 if (event->detail == XCB_BUTTON_INDEX_4 || event->detail == XCB_BUTTON_INDEX_5) {
121 Workspace *ws = c_ws;
122 if (event->detail == XCB_BUTTON_INDEX_5) {
123 while ((ws = TAILQ_NEXT(ws, workspaces)) != TAILQ_END(workspaces_head)) {
124 if (ws->output == output) {
125 workspace_show(conn, ws->num + 1);
130 while ((ws = TAILQ_PREV(ws, workspaces_head, workspaces)) != TAILQ_END(workspaces)) {
131 if (ws->output == output) {
132 workspace_show(conn, ws->num + 1);
140 /* Because workspaces can be on different outputs, we need to loop
141 through all of them and decide to count it based on its ->output */
143 TAILQ_FOREACH(ws, workspaces, workspaces) {
144 if (ws->output != output)
146 DLOG("Checking if click was on workspace %d with drawn = %d, tw = %d\n",
147 ws->num, drawn, ws->text_width);
148 if (event->event_x > (drawn + 1) &&
149 event->event_x <= (drawn + 1 + ws->text_width + 5 + 5)) {
150 workspace_show(conn, ws->num + 1);
154 drawn += ws->text_width + 5 + 5 + 2;
163 * Called when the user clicks using the floating_modifier, but the client is in
166 * Returns false if it does not do anything (that is, the click should be sent
170 static bool floating_mod_on_tiled_client(xcb_connection_t *conn, Client *client,
171 xcb_button_press_event_t *event) {
172 /* Only the right mouse button is interesting for us at the moment */
173 if (event->detail != 3)
176 /* The client is in tiling layout. We can still
177 * initiate a resize with the right mouse button,
178 * by chosing the border which is the most near one
179 * to the position of the mouse pointer */
180 int to_right = client->rect.width - event->event_x,
181 to_left = event->event_x,
182 to_top = event->event_y,
183 to_bottom = client->rect.height - event->event_y;
184 resize_orientation_t orientation = O_VERTICAL;
185 Container *con = client->container;
186 Workspace *ws = con->workspace;
187 int first = 0, second = 0;
189 DLOG("click was %d px to the right, %d px to the left, %d px to top, %d px to bottom\n",
190 to_right, to_left, to_top, to_bottom);
192 if (to_right < to_left &&
194 to_right < to_bottom) {
196 first = con->col + (con->colspan - 1);
197 DLOG("column %d\n", first);
199 if (!cell_exists(ws, first, con->row) ||
200 (first == (ws->cols-1)))
204 } else if (to_left < to_right &&
206 to_left < to_bottom) {
211 first = con->col - 1;
213 } else if (to_top < to_right &&
215 to_top < to_bottom) {
216 /* This was a press on the top border */
219 first = con->row - 1;
221 orientation = O_HORIZONTAL;
222 } else if (to_bottom < to_right &&
223 to_bottom < to_left &&
224 to_bottom < to_top) {
226 first = con->row + (con->rowspan - 1);
227 if (!cell_exists(ws, con->col, first) ||
228 (first == (ws->rows-1)))
232 orientation = O_HORIZONTAL;
235 return resize_graphical_handler(conn, ws, first, second, orientation, event);
239 int handle_button_press(void *ignored, xcb_connection_t *conn, xcb_button_press_event_t *event) {
240 /* TODO: dragging floating windows by grabbing their decoration does not
241 * work right now. We need to somehow recognize that special case: either
242 * we check if the con with the clicked decoration is the only con inside
243 * its parent (so that you can only drag single floating windows, not
244 * floating containers with multiple windows) *or* we somehow find out
245 * which decoration(s) are at the top and enable grabbing for them while
246 * resizing for the others. Maybe we could process the resizing parameters
247 * first and check if resizing is possible: if yes, resize, if not, drag.
249 * Also raise on click on decoration is not working. */
251 DLOG("Button %d pressed on window 0x%08x\n", event->state, event->event);
253 con = con_by_window_id(event->event);
254 bool border_click = (con == NULL);
255 const uint32_t mod = config.floating_modifier;
256 bool mod_pressed = (mod != 0 && (event->state & mod) == mod);
259 con = con_by_frame_id(event->event);
261 DLOG("border_click = %d, mod_pressed = %d\n", border_click, mod_pressed);
263 Con *clicked_into = NULL;
266 TAILQ_FOREACH(child, &(con->nodes_head), nodes) {
267 if (!rect_contains(child->deco_rect, event->event_x, event->event_y))
270 clicked_into = child;
274 DLOG("clicked_into = %p\n", clicked_into);
276 /* See if this was a click with the configured modifier. If so, we need
277 * to move around the client if it was floating. if not, we just process
279 DLOG("state = %d, floating_modifier = %d\n", event->state, config.floating_modifier);
280 if (border_click || mod_pressed) {
282 LOG("Not handling, floating_modifier was pressed and no client found\n");
287 if (con->fullscreen) {
288 LOG("Not handling, client is in fullscreen mode\n");
292 Con *floatingcon = con;
293 if ((border_click && con->type == CT_FLOATING_CON) ||
294 ((floatingcon = con_inside_floating(con)) != NULL &&
295 clicked_into == NULL)) {
296 LOG("button %d pressed\n", event->detail);
297 if (event->detail == 1) {
298 LOG("left mouse button, dragging\n");
299 floating_drag_window(floatingcon, event);
301 else if (event->detail == 3) {
302 bool proportional = (event->state & BIND_SHIFT);
303 DLOG("right mouse button\n");
304 floating_resize_window(floatingcon, proportional, event);
310 /* click to focus, either on the clicked window or its child if thas was a
311 * click into a child decoration */
312 con_focus((clicked_into ? clicked_into : con));
314 /* for floating containers, we also want to raise them on click */
315 Con *floatingcon = con_inside_floating(con);
316 if (floatingcon != NULL)
317 floating_raise_con(floatingcon);
321 /* if we clicked into a child decoration on a stacked/tabbed container, we
322 * are done and don’t want to resize */
324 (con->layout == L_STACKED || con->layout == L_TABBED))
327 /* check if this was a click on the window border (and on which one) */
328 Rect bsr = con_border_style_rect(con);
329 DLOG("BORDER x = %d, y = %d for con %p, window 0x%08x, border_click = %d, clicked_into = %p\n",
330 event->event_x, event->event_y, con, event->event, border_click, clicked_into);
331 DLOG("checks for right >= %d\n", con->window_rect.x + con->window_rect.width);
332 Con *first = NULL, *second = NULL;
334 DLOG("BORDER top\n");
335 if ((first = con_get_next(clicked_into, 'p', VERT)) != NULL) {
336 /* instead of setting second = clicked_into we get the container
337 * below the one which con_get_next returned. This way, if
338 * clicked_into is inside another split-con, we get the correct
339 * parent to work with. */
340 second = TAILQ_NEXT(first, nodes);
341 resize_graphical_handler(first, second, VERT, event);
343 } else if (event->event_x >= 0 && event->event_x <= bsr.x &&
344 event->event_y >= bsr.y && event->event_y <= con->rect.height + bsr.height) {
345 DLOG("BORDER left\n");
347 if ((first = con_get_next(con, 'p', HORIZ)) != NULL)
348 resize_graphical_handler(first, second, HORIZ, event);
349 } else if (event->event_x >= (con->window_rect.x + con->window_rect.width) &&
350 event->event_y >= bsr.y && event->event_y <= con->rect.height + bsr.height) {
351 DLOG("BORDER right\n");
353 if ((second = con_get_next(con, 'n', HORIZ)) != NULL)
354 resize_graphical_handler(first, second, HORIZ, event);
355 } else if (event->event_y >= (con->window_rect.y + con->window_rect.height)) {
356 DLOG("BORDER bottom\n");
359 if ((second = con_get_next(con, 'n', VERT)) != NULL)
360 resize_graphical_handler(first, second, VERT, event);
362 /* Set first and second to NULL to trigger a replay of the event */
363 first = second = NULL;
366 if (first == NULL || second == NULL) {
367 DLOG("Replaying click\n");
368 /* No border click, replay the click */
369 xcb_allow_events(conn, XCB_ALLOW_REPLAY_POINTER, event->time);
374 DLOG("After resize handler, rendering\n");
379 if (client == NULL) {
380 /* The client was neither on a client’s titlebar nor on a client itself, maybe on a stack_window? */
381 if (button_press_stackwin(conn, event))
385 if (button_press_bar(conn, event))
388 DLOG("Could not handle this button press\n");
392 /* Set focus in any case */
393 set_focus(conn, client, true);
395 /* Let’s see if this was on the borders (= resize). If not, we’re done */
396 DLOG("press button on x=%d, y=%d\n", event->event_x, event->event_y);
397 resize_orientation_t orientation = O_VERTICAL;
398 Container *con = client->container;
402 DLOG("dock. done.\n");
403 xcb_allow_events(conn, XCB_ALLOW_REPLAY_POINTER, event->time);
408 DLOG("event->event_x = %d, client->rect.width = %d\n", event->event_x, client->rect.width);
410 /* Some clients (xfontsel for example) seem to pass clicks on their
411 * window to the parent window, thus we receive an event here which in
412 * reality is a border_click. Check for the position and fix state. */
414 event->event_x >= client->child_rect.x &&
415 event->event_x <= (client->child_rect.x + client->child_rect.width) &&
416 event->event_y >= client->child_rect.y &&
417 event->event_y <= (client->child_rect.y + client->child_rect.height)) {
418 DLOG("Fixing border_click = false because of click in child\n");
419 border_click = false;
423 DLOG("client. done.\n");
424 xcb_allow_events(conn, XCB_ALLOW_REPLAY_POINTER, event->time);
425 /* Floating clients should be raised on click */
426 if (client_is_floating(client))
427 xcb_raise_window(conn, client->frame);
432 /* Don’t handle events inside the titlebar, only borders are interesting */
433 i3Font *font = load_font(conn, config.font);
434 if (event->event_y >= 2 && event->event_y <= (font->height + 2 + 2)) {
435 DLOG("click on titlebar\n");
437 /* Floating clients can be dragged by grabbing their titlebar */
438 if (client_is_floating(client)) {
439 /* Firstly, we raise it. Maybe the user just wanted to raise it without grabbing */
440 xcb_raise_window(conn, client->frame);
443 floating_drag_window(conn, client, event);
448 if (client_is_floating(client))
449 return floating_border_click(conn, client, event);
451 Workspace *ws = con->workspace;
453 if (event->event_y < 2) {
454 /* This was a press on the top border */
457 first = con->row - 1;
459 orientation = O_HORIZONTAL;
460 } else if (event->event_y >= (client->rect.height - 2)) {
462 first = con->row + (con->rowspan - 1);
463 if (!cell_exists(ws, con->col, first) ||
464 (first == (ws->rows-1)))
468 orientation = O_HORIZONTAL;
469 } else if (event->event_x <= 2) {
474 first = con->col - 1;
476 } else if (event->event_x > 2) {
478 first = con->col + (con->colspan - 1);
479 DLOG("column %d\n", first);
481 if (!cell_exists(ws, first, con->row) ||
482 (first == (ws->cols-1)))
488 return resize_graphical_handler(conn, ws, first, second, orientation, event);