4 * i3 - an improved dynamic tiling window manager
6 * © 2009 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.
23 #include <xcb/xcb_atom.h>
24 #include <xcb/xcb_icccm.h>
26 #include <X11/XKBlib.h>
35 #include "workspace.h"
41 static struct Stack_Window *get_stack_window(xcb_window_t window_id) {
42 struct Stack_Window *current;
44 SLIST_FOREACH(current, &stack_wins, stack_windows) {
45 if (current->window != window_id)
55 * Checks if the button press was on a stack window, handles focus setting and returns true
56 * if so, or false otherwise.
59 static bool button_press_stackwin(xcb_connection_t *conn, xcb_button_press_event_t *event) {
60 struct Stack_Window *stack_win;
62 /* If we find a corresponding stack window, we can handle the event */
63 if ((stack_win = get_stack_window(event->event)) == NULL)
66 /* A stack window was clicked, we check if it was button4 or button5
67 which are scroll up / scroll down. */
68 if (event->detail == XCB_BUTTON_INDEX_4 || event->detail == XCB_BUTTON_INDEX_5) {
69 direction_t direction = (event->detail == XCB_BUTTON_INDEX_4 ? D_UP : D_DOWN);
70 focus_window_in_container(conn, CUR_CELL, direction);
74 /* It was no scrolling, so we calculate the destination client by
75 dividing the Y position of the event through the height of a window
76 decoration and then set the focus to this client. */
77 i3Font *font = load_font(conn, config.font);
78 int decoration_height = (font->height + 2 + 2);
79 int destination = (event->event_y / decoration_height),
83 Container *container = stack_win->container;
85 CIRCLEQ_FOREACH(client, &(container->clients), clients)
88 /* If we don’t have any clients in this container, we cannot do
89 * anything useful anyways. */
93 if (container->mode == MODE_TABBED)
94 destination = (event->event_x / (container->width / num_clients));
95 else if (container->mode == MODE_STACK &&
96 container->stack_limit != STACK_LIMIT_NONE) {
97 if (container->stack_limit == STACK_LIMIT_COLS) {
98 int wrap = ceil((float)num_clients / container->stack_limit_value);
99 int clicked_column = (event->event_x / (stack_win->rect.width / container->stack_limit_value));
100 int clicked_row = (event->event_y / decoration_height);
101 DLOG("clicked on column %d, row %d\n", clicked_column, clicked_row);
102 destination = (wrap * clicked_column) + clicked_row;
104 int width = (stack_win->rect.width / ceil((float)num_clients / container->stack_limit_value));
105 int clicked_column = (event->event_x / width);
106 int clicked_row = (event->event_y / decoration_height);
107 DLOG("clicked on column %d, row %d\n", clicked_column, clicked_row);
108 destination = (container->stack_limit_value * clicked_column) + clicked_row;
112 DLOG("Click on stack_win for client %d\n", destination);
113 CIRCLEQ_FOREACH(client, &(stack_win->container->clients), clients)
114 if (c++ == destination) {
115 set_focus(conn, client, true);
123 * Checks if the button press was on a bar, switches to the workspace and returns true
124 * if so, or false otherwise.
127 static bool button_press_bar(xcb_connection_t *conn, xcb_button_press_event_t *event) {
129 TAILQ_FOREACH(screen, virtual_screens, screens) {
130 if (screen->bar != event->event)
133 DLOG("Click on a bar\n");
135 /* Check if the button was one of button4 or button5 (scroll up / scroll down) */
136 if (event->detail == XCB_BUTTON_INDEX_4 || event->detail == XCB_BUTTON_INDEX_5) {
137 Workspace *ws = c_ws;
138 if (event->detail == XCB_BUTTON_INDEX_5) {
139 while ((ws = TAILQ_NEXT(ws, workspaces)) != TAILQ_END(workspaces_head)) {
140 if (ws->screen == screen) {
141 workspace_show(conn, ws->num + 1);
146 while ((ws = TAILQ_PREV(ws, workspaces_head, workspaces)) != TAILQ_END(workspaces)) {
147 if (ws->screen == screen) {
148 workspace_show(conn, ws->num + 1);
156 /* Because workspaces can be on different screens, we need to loop
157 through all of them and decide to count it based on its ->screen */
159 TAILQ_FOREACH(ws, workspaces, workspaces) {
160 if (ws->screen != screen)
162 DLOG("Checking if click was on workspace %d with drawn = %d, tw = %d\n",
163 ws->num, drawn, ws->text_width);
164 if (event->event_x > (drawn + 1) &&
165 event->event_x <= (drawn + 1 + ws->text_width + 5 + 5)) {
166 workspace_show(conn, ws->num + 1);
170 drawn += ws->text_width + 5 + 5 + 2;
179 * Called when the user clicks using the floating_modifier, but the client is in
182 * Returns false if it does not do anything (that is, the click should be sent
186 static bool floating_mod_on_tiled_client(xcb_connection_t *conn, Client *client,
187 xcb_button_press_event_t *event) {
188 /* Only the right mouse button is interesting for us at the moment */
189 if (event->detail != 3)
192 /* The client is in tiling layout. We can still
193 * initiate a resize with the right mouse button,
194 * by chosing the border which is the most near one
195 * to the position of the mouse pointer */
196 int to_right = client->rect.width - event->event_x,
197 to_left = event->event_x,
198 to_top = event->event_y,
199 to_bottom = client->rect.height - event->event_y;
200 resize_orientation_t orientation = O_VERTICAL;
201 Container *con = client->container;
202 Workspace *ws = con->workspace;
203 int first = 0, second = 0;
205 DLOG("click was %d px to the right, %d px to the left, %d px to top, %d px to bottom\n",
206 to_right, to_left, to_top, to_bottom);
208 if (to_right < to_left &&
210 to_right < to_bottom) {
212 first = con->col + (con->colspan - 1);
213 DLOG("column %d\n", first);
215 if (!cell_exists(first, con->row) ||
216 (first == (ws->cols-1)))
220 } else if (to_left < to_right &&
222 to_left < to_bottom) {
227 first = con->col - 1;
229 } else if (to_top < to_right &&
231 to_top < to_bottom) {
232 /* This was a press on the top border */
235 first = con->row - 1;
237 orientation = O_HORIZONTAL;
238 } else if (to_bottom < to_right &&
239 to_bottom < to_left &&
240 to_bottom < to_top) {
242 first = con->row + (con->rowspan - 1);
243 if (!cell_exists(con->col, first) ||
244 (first == (ws->rows-1)))
248 orientation = O_HORIZONTAL;
251 return resize_graphical_handler(conn, ws, first, second, orientation, event);
254 int handle_button_press(void *ignored, xcb_connection_t *conn, xcb_button_press_event_t *event) {
255 DLOG("Button %d pressed\n", event->state);
256 /* This was either a focus for a client’s parent (= titlebar)… */
257 Client *client = table_get(&by_child, event->event);
258 bool border_click = false;
259 if (client == NULL) {
260 client = table_get(&by_parent, event->event);
263 /* See if this was a click with the configured modifier. If so, we need
264 * to move around the client if it was floating. if not, we just process
266 if (config.floating_modifier != 0 &&
267 (event->state & config.floating_modifier) != 0) {
268 if (client == NULL) {
269 DLOG("Not handling, floating_modifier was pressed and no client found\n");
272 if (client->fullscreen) {
273 DLOG("Not handling, client is in fullscreen mode\n");
276 if (client_is_floating(client)) {
277 DLOG("button %d pressed\n", event->detail);
278 if (event->detail == 1) {
279 DLOG("left mouse button, dragging\n");
280 floating_drag_window(conn, client, event);
281 } else if (event->detail == 3) {
282 DLOG("right mouse button\n");
283 floating_resize_window(conn, client, event);
288 if (!floating_mod_on_tiled_client(conn, client, event)) {
289 xcb_allow_events(conn, XCB_ALLOW_REPLAY_POINTER, event->time);
296 if (client == NULL) {
297 /* The client was neither on a client’s titlebar nor on a client itself, maybe on a stack_window? */
298 if (button_press_stackwin(conn, event))
302 if (button_press_bar(conn, event))
305 DLOG("Could not handle this button press\n");
309 /* Set focus in any case */
310 set_focus(conn, client, true);
312 /* Let’s see if this was on the borders (= resize). If not, we’re done */
313 DLOG("press button on x=%d, y=%d\n", event->event_x, event->event_y);
314 resize_orientation_t orientation = O_VERTICAL;
315 Container *con = client->container;
319 DLOG("dock. done.\n");
320 xcb_allow_events(conn, XCB_ALLOW_REPLAY_POINTER, event->time);
325 DLOG("event->event_x = %d, client->rect.width = %d\n", event->event_x, client->rect.width);
327 /* Some clients (xfontsel for example) seem to pass clicks on their
328 * window to the parent window, thus we receive an event here which in
329 * reality is a border_click. Check for the position and fix state. */
331 event->event_x >= client->child_rect.x &&
332 event->event_x <= (client->child_rect.x + client->child_rect.width) &&
333 event->event_y >= client->child_rect.y &&
334 event->event_y <= (client->child_rect.y + client->child_rect.height)) {
335 DLOG("Fixing border_click = false because of click in child\n");
336 border_click = false;
340 DLOG("client. done.\n");
341 xcb_allow_events(conn, XCB_ALLOW_REPLAY_POINTER, event->time);
342 /* Floating clients should be raised on click */
343 if (client_is_floating(client))
344 xcb_raise_window(conn, client->frame);
349 /* Don’t handle events inside the titlebar, only borders are interesting */
350 i3Font *font = load_font(conn, config.font);
351 if (event->event_y >= 2 && event->event_y <= (font->height + 2 + 2)) {
352 DLOG("click on titlebar\n");
354 /* Floating clients can be dragged by grabbing their titlebar */
355 if (client_is_floating(client)) {
356 /* Firstly, we raise it. Maybe the user just wanted to raise it without grabbing */
357 xcb_raise_window(conn, client->frame);
360 floating_drag_window(conn, client, event);
365 if (client_is_floating(client))
366 return floating_border_click(conn, client, event);
368 Workspace *ws = con->workspace;
370 if (event->event_y < 2) {
371 /* This was a press on the top border */
374 first = con->row - 1;
376 orientation = O_HORIZONTAL;
377 } else if (event->event_y >= (client->rect.height - 2)) {
379 first = con->row + (con->rowspan - 1);
380 if (!cell_exists(con->col, first) ||
381 (first == (ws->rows-1)))
385 orientation = O_HORIZONTAL;
386 } else if (event->event_x <= 2) {
391 first = con->col - 1;
393 } else if (event->event_x > 2) {
395 first = con->col + (con->colspan - 1);
396 DLOG("column %d\n", first);
398 if (!cell_exists(first, con->row) ||
399 (first == (ws->cols-1)))
405 return resize_graphical_handler(conn, ws, first, second, orientation, event);