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"
40 static struct Stack_Window *get_stack_window(xcb_window_t window_id) {
41 struct Stack_Window *current;
43 SLIST_FOREACH(current, &stack_wins, stack_windows) {
44 if (current->window != window_id)
54 * Checks if the button press was on a stack window, handles focus setting and returns true
55 * if so, or false otherwise.
58 static bool button_press_stackwin(xcb_connection_t *conn, xcb_button_press_event_t *event) {
59 struct Stack_Window *stack_win;
61 /* If we find a corresponding stack window, we can handle the event */
62 if ((stack_win = get_stack_window(event->event)) == NULL)
65 /* A stack window was clicked, we check if it was button4 or button5
66 which are scroll up / scroll down. */
67 if (event->detail == XCB_BUTTON_INDEX_4 || event->detail == XCB_BUTTON_INDEX_5) {
68 direction_t direction = (event->detail == XCB_BUTTON_INDEX_4 ? D_UP : D_DOWN);
69 focus_window_in_container(conn, CUR_CELL, direction);
73 /* It was no scrolling, so we calculate the destination client by
74 dividing the Y position of the event through the height of a window
75 decoration and then set the focus to this client. */
76 i3Font *font = load_font(conn, config.font);
77 int decoration_height = (font->height + 2 + 2);
78 int destination = (event->event_y / decoration_height),
82 Container *container = stack_win->container;
84 CIRCLEQ_FOREACH(client, &(container->clients), clients)
87 if (container->mode == MODE_TABBED)
88 destination = (event->event_x / (container->width / num_clients));
89 else if (container->mode == MODE_STACK &&
90 container->stack_limit != STACK_LIMIT_NONE) {
91 if (container->stack_limit == STACK_LIMIT_COLS) {
92 int wrap = ceil((float)num_clients / container->stack_limit_value);
93 int clicked_column = (event->event_x / (stack_win->rect.width / container->stack_limit_value));
94 int clicked_row = (event->event_y / decoration_height);
95 LOG("clicked on column %d, row %d\n", clicked_column, clicked_row);
96 destination = (wrap * clicked_column) + clicked_row;
98 int width = (stack_win->rect.width / ceil((float)num_clients / container->stack_limit_value));
99 int clicked_column = (event->event_x / width);
100 int clicked_row = (event->event_y / decoration_height);
101 LOG("clicked on column %d, row %d\n", clicked_column, clicked_row);
102 destination = (container->stack_limit_value * clicked_column) + clicked_row;
106 LOG("Click on stack_win for client %d\n", destination);
107 CIRCLEQ_FOREACH(client, &(stack_win->container->clients), clients)
108 if (c++ == destination) {
109 set_focus(conn, client, true);
117 * Checks if the button press was on a bar, switches to the workspace and returns true
118 * if so, or false otherwise.
121 static bool button_press_bar(xcb_connection_t *conn, xcb_button_press_event_t *event) {
123 TAILQ_FOREACH(screen, virtual_screens, screens) {
124 if (screen->bar != event->event)
127 LOG("Click on a bar\n");
129 /* Check if the button was one of button4 or button5 (scroll up / scroll down) */
130 if (event->detail == XCB_BUTTON_INDEX_4 || event->detail == XCB_BUTTON_INDEX_5) {
131 int add = (event->detail == XCB_BUTTON_INDEX_4 ? -1 : 1);
132 for (int i = c_ws->num + add; (i >= 0) && (i < num_workspaces); i += add)
133 if (workspaces[i].screen == screen) {
134 workspace_show(conn, i+1);
140 /* Because workspaces can be on different screens, we need to loop
141 through all of them and decide to count it based on its ->screen */
142 for (int i = 0; i < num_workspaces; i++) {
143 if (workspaces[i].screen != screen)
145 LOG("Checking if click was on workspace %d with drawn = %d, tw = %d\n",
146 i, drawn, workspaces[i].text_width);
147 if (event->event_x > (drawn + 1) &&
148 event->event_x <= (drawn + 1 + workspaces[i].text_width + 5 + 5)) {
149 workspace_show(conn, i+1);
153 drawn += workspaces[i].text_width + 5 + 5 + 2;
161 int handle_button_press(void *ignored, xcb_connection_t *conn, xcb_button_press_event_t *event) {
162 LOG("Button %d pressed\n", event->state);
163 /* This was either a focus for a client’s parent (= titlebar)… */
164 Client *client = table_get(&by_child, event->event);
165 bool border_click = false;
166 if (client == NULL) {
167 client = table_get(&by_parent, event->event);
170 /* See if this was a click with the configured modifier. If so, we need
171 * to move around the client if it was floating. if not, we just process
173 if (config.floating_modifier != 0 &&
174 (event->state & config.floating_modifier) != 0) {
175 if (client == NULL) {
176 LOG("Not handling, floating_modifier was pressed and no client found\n");
179 if (client_is_floating(client)) {
180 LOG("button %d pressed\n", event->detail);
181 if (event->detail == 1) {
182 LOG("left mouse button, dragging\n");
183 floating_drag_window(conn, client, event);
184 } else if (event->detail == 3) {
185 LOG("right mouse button\n");
186 floating_resize_window(conn, client, event);
190 /* The client is in tiling layout. We can still
191 * initiate a resize with the right mouse button,
192 * by chosing the border which is the most near one
193 * to the position of the mouse pointer */
194 if (event->detail == 3) {
195 int to_right = client->rect.width - event->event_x,
196 to_left = event->event_x,
197 to_top = event->event_y,
198 to_bottom = client->rect.height - event->event_y;
199 resize_orientation_t orientation = O_VERTICAL;
200 Container *con = client->container;
201 Workspace *ws = con->workspace;
202 int first = 0, second = 0;
204 LOG("click was %d px to the right, %d px to the left, %d px to top, %d px to bottom\n",
205 to_right, to_left, to_top, to_bottom);
207 if (to_right < to_left &&
209 to_right < to_bottom) {
211 first = con->col + (con->colspan - 1);
212 LOG("column %d\n", first);
214 if (!cell_exists(first, con->row) ||
215 (first == (ws->cols-1)))
219 } else if (to_left < to_right &&
221 to_left < to_bottom) {
226 first = con->col - 1;
228 } else if (to_top < to_right &&
230 to_top < to_bottom) {
231 /* This was a press on the top border */
234 first = con->row - 1;
236 orientation = O_HORIZONTAL;
237 } else if (to_bottom < to_right &&
238 to_bottom < to_left &&
239 to_bottom < to_top) {
241 first = con->row + (con->rowspan - 1);
242 if (!cell_exists(con->col, first) ||
243 (first == (ws->rows-1)))
247 orientation = O_HORIZONTAL;
250 return resize_graphical_handler(conn, ws, first, second, orientation, event);
255 if (client == NULL) {
256 /* The client was neither on a client’s titlebar nor on a client itself, maybe on a stack_window? */
257 if (button_press_stackwin(conn, event))
261 if (button_press_bar(conn, event))
264 LOG("Could not handle this button press\n");
268 /* Set focus in any case */
269 set_focus(conn, client, true);
271 /* Let’s see if this was on the borders (= resize). If not, we’re done */
272 LOG("press button on x=%d, y=%d\n", event->event_x, event->event_y);
273 resize_orientation_t orientation = O_VERTICAL;
274 Container *con = client->container;
278 LOG("dock. done.\n");
279 xcb_allow_events(conn, XCB_ALLOW_REPLAY_POINTER, event->time);
284 LOG("event->event_x = %d, client->rect.width = %d\n", event->event_x, client->rect.width);
286 /* Some clients (xfontsel for example) seem to pass clicks on their
287 * window to the parent window, thus we receive an event here which in
288 * reality is a border_click. Check for the position and fix state. */
290 event->event_x >= client->child_rect.x &&
291 event->event_x <= (client->child_rect.x + client->child_rect.width) &&
292 event->event_y >= client->child_rect.y &&
293 event->event_y <= (client->child_rect.y + client->child_rect.height)) {
294 LOG("Fixing border_click = false because of click in child\n");
295 border_click = false;
299 LOG("client. done.\n");
300 xcb_allow_events(conn, XCB_ALLOW_REPLAY_POINTER, event->time);
301 /* Floating clients should be raised on click */
302 if (client_is_floating(client))
303 xcb_raise_window(conn, client->frame);
308 /* Don’t handle events inside the titlebar, only borders are interesting */
309 i3Font *font = load_font(conn, config.font);
310 if (event->event_y >= 2 && event->event_y <= (font->height + 2 + 2)) {
311 LOG("click on titlebar\n");
313 /* Floating clients can be dragged by grabbing their titlebar */
314 if (client_is_floating(client)) {
315 /* Firstly, we raise it. Maybe the user just wanted to raise it without grabbing */
316 xcb_raise_window(conn, client->frame);
319 floating_drag_window(conn, client, event);
324 if (client_is_floating(client))
325 return floating_border_click(conn, client, event);
327 Workspace *ws = con->workspace;
329 if (event->event_y < 2) {
330 /* This was a press on the top border */
333 first = con->row - 1;
335 orientation = O_HORIZONTAL;
336 } else if (event->event_y >= (client->rect.height - 2)) {
338 first = con->row + (con->rowspan - 1);
339 if (!cell_exists(con->col, first) ||
340 (first == (ws->rows-1)))
344 orientation = O_HORIZONTAL;
345 } else if (event->event_x <= 2) {
350 first = con->col - 1;
352 } else if (event->event_x > 2) {
354 first = con->col + (con->colspan - 1);
355 LOG("column %d\n", first);
357 if (!cell_exists(first, con->row) ||
358 (first == (ws->cols-1)))
364 return resize_graphical_handler(conn, ws, first, second, orientation, event);