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) {
241 DLOG("Button %d pressed on window 0x%08x\n", event->state, event->event);
243 con = con_by_window_id(event->event);
244 bool border_click = false;
246 con = con_by_frame_id(event->event);
249 DLOG("border_click = %d\n", border_click);
250 //if (con && con->type == CT_FLOATING_CON)
251 //con = TAILQ_FIRST(&(con->nodes_head));
253 /* See if this was a click with the configured modifier. If so, we need
254 * to move around the client if it was floating. if not, we just process
256 DLOG("state = %d, floating_modifier = %d\n", event->state, config.floating_modifier);
258 (config.floating_modifier != 0 &&
259 (event->state & config.floating_modifier) == config.floating_modifier)) {
261 LOG("Not handling, floating_modifier was pressed and no client found\n");
266 if (con->fullscreen) {
267 LOG("Not handling, client is in fullscreen mode\n");
271 if ((border_click && con->type == CT_FLOATING_CON) ||
272 (!border_click && con_is_floating(con))) {
273 /* floating operations are always on the container around
274 * the "payload container", so make sure we use the right one */
275 Con *floatingcon = (border_click ? con : con->parent);
276 LOG("button %d pressed\n", event->detail);
277 if (event->detail == 1) {
278 LOG("left mouse button, dragging\n");
279 floating_drag_window(floatingcon, event);
281 else if (event->detail == 3) {
282 bool proportional = (event->state & BIND_SHIFT);
283 DLOG("right mouse button\n");
284 floating_resize_window(floatingcon, proportional, event);
293 Con *clicked_into = NULL;
296 TAILQ_FOREACH(child, &(con->nodes_head), nodes) {
297 if (!rect_contains(child->deco_rect, event->event_x, event->event_y))
300 clicked_into = child;
307 /* check if this was a click on the window border (and on which one) */
308 Rect bsr = con_border_style_rect(con);
309 DLOG("BORDER x = %d, y = %d for con %p, window 0x%08x, border_click = %d, clicked_into = %p\n",
310 event->event_x, event->event_y, con, event->event, border_click, clicked_into);
311 DLOG("checks for right >= %d\n", con->window_rect.x + con->window_rect.width);
312 /* TODO: das problem ist, dass TAILQ_PREV etc. nicht die orientation beachtet. */
313 Con *first = NULL, *second = NULL;
315 DLOG("BORDER top\n");
316 second = clicked_into;
317 if ((first = con_get_next(clicked_into, 'p', VERT)) != NULL)
318 resize_graphical_handler(first, second, VERT, event);
319 } else if (event->event_x >= 0 && event->event_x <= bsr.x &&
320 event->event_y >= bsr.y && event->event_y <= con->rect.height + bsr.height) {
321 DLOG("BORDER left\n");
323 if ((first = con_get_next(con, 'p', HORIZ)) != NULL)
324 resize_graphical_handler(first, second, HORIZ, event);
325 } else if (event->event_x >= (con->window_rect.x + con->window_rect.width) &&
326 event->event_y >= bsr.y && event->event_y <= con->rect.height + bsr.height) {
327 DLOG("BORDER right\n");
329 if ((second = con_get_next(con, 'n', HORIZ)) != NULL)
330 resize_graphical_handler(first, second, HORIZ, event);
331 } else if (event->event_y >= (con->window_rect.y + con->window_rect.height)) {
332 DLOG("BORDER bottom\n");
335 if ((second = con_get_next(con, 'n', VERT)) != NULL)
336 resize_graphical_handler(first, second, VERT, event);
338 /* Set first and second to NULL to trigger a replay of the event */
339 first = second = NULL;
342 if (first == NULL || second == NULL) {
343 DLOG("Replaying click\n");
344 /* No border click, replay the click */
345 xcb_allow_events(conn, XCB_ALLOW_REPLAY_POINTER, event->time);
350 DLOG("After resize handler, rendering\n");
355 if (client == NULL) {
356 /* The client was neither on a client’s titlebar nor on a client itself, maybe on a stack_window? */
357 if (button_press_stackwin(conn, event))
361 if (button_press_bar(conn, event))
364 DLOG("Could not handle this button press\n");
368 /* Set focus in any case */
369 set_focus(conn, client, true);
371 /* Let’s see if this was on the borders (= resize). If not, we’re done */
372 DLOG("press button on x=%d, y=%d\n", event->event_x, event->event_y);
373 resize_orientation_t orientation = O_VERTICAL;
374 Container *con = client->container;
378 DLOG("dock. done.\n");
379 xcb_allow_events(conn, XCB_ALLOW_REPLAY_POINTER, event->time);
384 DLOG("event->event_x = %d, client->rect.width = %d\n", event->event_x, client->rect.width);
386 /* Some clients (xfontsel for example) seem to pass clicks on their
387 * window to the parent window, thus we receive an event here which in
388 * reality is a border_click. Check for the position and fix state. */
390 event->event_x >= client->child_rect.x &&
391 event->event_x <= (client->child_rect.x + client->child_rect.width) &&
392 event->event_y >= client->child_rect.y &&
393 event->event_y <= (client->child_rect.y + client->child_rect.height)) {
394 DLOG("Fixing border_click = false because of click in child\n");
395 border_click = false;
399 DLOG("client. done.\n");
400 xcb_allow_events(conn, XCB_ALLOW_REPLAY_POINTER, event->time);
401 /* Floating clients should be raised on click */
402 if (client_is_floating(client))
403 xcb_raise_window(conn, client->frame);
408 /* Don’t handle events inside the titlebar, only borders are interesting */
409 i3Font *font = load_font(conn, config.font);
410 if (event->event_y >= 2 && event->event_y <= (font->height + 2 + 2)) {
411 DLOG("click on titlebar\n");
413 /* Floating clients can be dragged by grabbing their titlebar */
414 if (client_is_floating(client)) {
415 /* Firstly, we raise it. Maybe the user just wanted to raise it without grabbing */
416 xcb_raise_window(conn, client->frame);
419 floating_drag_window(conn, client, event);
424 if (client_is_floating(client))
425 return floating_border_click(conn, client, event);
427 Workspace *ws = con->workspace;
429 if (event->event_y < 2) {
430 /* This was a press on the top border */
433 first = con->row - 1;
435 orientation = O_HORIZONTAL;
436 } else if (event->event_y >= (client->rect.height - 2)) {
438 first = con->row + (con->rowspan - 1);
439 if (!cell_exists(ws, con->col, first) ||
440 (first == (ws->rows-1)))
444 orientation = O_HORIZONTAL;
445 } else if (event->event_x <= 2) {
450 first = con->col - 1;
452 } else if (event->event_x > 2) {
454 first = con->col + (con->colspan - 1);
455 DLOG("column %d\n", first);
457 if (!cell_exists(ws, first, con->row) ||
458 (first == (ws->cols-1)))
464 return resize_graphical_handler(conn, ws, first, second, orientation, event);