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.
23 #include <xcb/xcb_atom.h>
24 #include <xcb/xcb_icccm.h>
26 #include <X11/XKBlib.h>
35 #include "workspace.h"
42 static struct Stack_Window *get_stack_window(xcb_window_t window_id) {
43 struct Stack_Window *current;
45 SLIST_FOREACH(current, &stack_wins, stack_windows) {
46 if (current->window != window_id)
56 * Checks if the button press was on a stack window, handles focus setting and returns true
57 * if so, or false otherwise.
60 static bool button_press_stackwin(xcb_connection_t *conn, xcb_button_press_event_t *event) {
61 struct Stack_Window *stack_win;
63 /* If we find a corresponding stack window, we can handle the event */
64 if ((stack_win = get_stack_window(event->event)) == NULL)
67 /* A stack window was clicked, we check if it was button4 or button5
68 which are scroll up / scroll down. */
69 if (event->detail == XCB_BUTTON_INDEX_4 || event->detail == XCB_BUTTON_INDEX_5) {
70 direction_t direction = (event->detail == XCB_BUTTON_INDEX_4 ? D_UP : D_DOWN);
71 focus_window_in_container(conn, CUR_CELL, direction);
75 /* It was no scrolling, so we calculate the destination client by
76 dividing the Y position of the event through the height of a window
77 decoration and then set the focus to this client. */
78 i3Font *font = load_font(conn, config.font);
79 int decoration_height = (font->height + 2 + 2);
80 int destination = (event->event_y / decoration_height),
84 Container *container = stack_win->container;
86 CIRCLEQ_FOREACH(client, &(container->clients), clients)
89 /* If we don’t have any clients in this container, we cannot do
90 * anything useful anyways. */
94 if (container->mode == MODE_TABBED)
95 destination = (event->event_x / (container->width / num_clients));
96 else if (container->mode == MODE_STACK &&
97 container->stack_limit != STACK_LIMIT_NONE) {
98 if (container->stack_limit == STACK_LIMIT_COLS) {
99 int wrap = ceil((float)num_clients / container->stack_limit_value);
100 int clicked_column = (event->event_x / (stack_win->rect.width / container->stack_limit_value));
101 int clicked_row = (event->event_y / decoration_height);
102 DLOG("clicked on column %d, row %d\n", clicked_column, clicked_row);
103 destination = (wrap * clicked_column) + clicked_row;
105 int width = (stack_win->rect.width / ceil((float)num_clients / container->stack_limit_value));
106 int clicked_column = (event->event_x / width);
107 int clicked_row = (event->event_y / decoration_height);
108 DLOG("clicked on column %d, row %d\n", clicked_column, clicked_row);
109 destination = (container->stack_limit_value * clicked_column) + clicked_row;
113 DLOG("Click on stack_win for client %d\n", destination);
114 CIRCLEQ_FOREACH(client, &(stack_win->container->clients), clients)
115 if (c++ == destination) {
116 set_focus(conn, client, true);
124 * Checks if the button press was on a bar, switches to the workspace and returns true
125 * if so, or false otherwise.
128 static bool button_press_bar(xcb_connection_t *conn, xcb_button_press_event_t *event) {
130 TAILQ_FOREACH(output, &outputs, outputs) {
131 if (output->bar != event->event)
134 DLOG("Click on a bar\n");
136 /* Check if the button was one of button4 or button5 (scroll up / scroll down) */
137 if (event->detail == XCB_BUTTON_INDEX_4 || event->detail == XCB_BUTTON_INDEX_5) {
138 Workspace *ws = c_ws;
139 if (event->detail == XCB_BUTTON_INDEX_5) {
140 while ((ws = TAILQ_NEXT(ws, workspaces)) != TAILQ_END(workspaces_head)) {
141 if (ws->output == output) {
142 workspace_show(conn, ws->num + 1);
147 while ((ws = TAILQ_PREV(ws, workspaces_head, workspaces)) != TAILQ_END(workspaces)) {
148 if (ws->output == output) {
149 workspace_show(conn, ws->num + 1);
157 /* Because workspaces can be on different outputs, we need to loop
158 through all of them and decide to count it based on its ->output */
160 TAILQ_FOREACH(ws, workspaces, workspaces) {
161 if (ws->output != output)
163 DLOG("Checking if click was on workspace %d with drawn = %d, tw = %d\n",
164 ws->num, drawn, ws->text_width);
165 if (event->event_x > (drawn + 1) &&
166 event->event_x <= (drawn + 1 + ws->text_width + 5 + 5)) {
167 workspace_show(conn, ws->num + 1);
171 drawn += ws->text_width + 5 + 5 + 2;
180 * Called when the user clicks using the floating_modifier, but the client is in
183 * Returns false if it does not do anything (that is, the click should be sent
187 static bool floating_mod_on_tiled_client(xcb_connection_t *conn, Client *client,
188 xcb_button_press_event_t *event) {
189 /* Only the right mouse button is interesting for us at the moment */
190 if (event->detail != 3)
193 /* The client is in tiling layout. We can still
194 * initiate a resize with the right mouse button,
195 * by chosing the border which is the most near one
196 * to the position of the mouse pointer */
197 int to_right = client->rect.width - event->event_x,
198 to_left = event->event_x,
199 to_top = event->event_y,
200 to_bottom = client->rect.height - event->event_y;
201 resize_orientation_t orientation = O_VERTICAL;
202 Container *con = client->container;
203 Workspace *ws = con->workspace;
204 int first = 0, second = 0;
206 DLOG("click was %d px to the right, %d px to the left, %d px to top, %d px to bottom\n",
207 to_right, to_left, to_top, to_bottom);
209 if (to_right < to_left &&
211 to_right < to_bottom) {
213 first = con->col + (con->colspan - 1);
214 DLOG("column %d\n", first);
216 if (!cell_exists(ws, first, con->row) ||
217 (first == (ws->cols-1)))
221 } else if (to_left < to_right &&
223 to_left < to_bottom) {
228 first = con->col - 1;
230 } else if (to_top < to_right &&
232 to_top < to_bottom) {
233 /* This was a press on the top border */
236 first = con->row - 1;
238 orientation = O_HORIZONTAL;
239 } else if (to_bottom < to_right &&
240 to_bottom < to_left &&
241 to_bottom < to_top) {
243 first = con->row + (con->rowspan - 1);
244 if (!cell_exists(ws, con->col, first) ||
245 (first == (ws->rows-1)))
249 orientation = O_HORIZONTAL;
252 return resize_graphical_handler(conn, ws, first, second, orientation, event);
255 int handle_button_press(void *ignored, xcb_connection_t *conn, xcb_button_press_event_t *event) {
256 DLOG("Button %d pressed\n", event->state);
257 /* This was either a focus for a client’s parent (= titlebar)… */
258 Client *client = table_get(&by_child, event->event);
259 bool border_click = false;
260 if (client == NULL) {
261 client = table_get(&by_parent, event->event);
264 /* See if this was a click with the configured modifier. If so, we need
265 * to move around the client if it was floating. if not, we just process
267 if (config.floating_modifier != 0 &&
268 (event->state & config.floating_modifier) == config.floating_modifier) {
269 if (client == NULL) {
270 DLOG("Not handling, floating_modifier was pressed and no client found\n");
271 xcb_allow_events(conn, XCB_ALLOW_REPLAY_POINTER, event->time);
275 if (client->fullscreen) {
276 DLOG("Not handling, client is in fullscreen mode\n");
277 xcb_allow_events(conn, XCB_ALLOW_REPLAY_POINTER, event->time);
281 if (client_is_floating(client)) {
282 DLOG("button %d pressed\n", event->detail);
283 if (event->detail == 1) {
284 DLOG("left mouse button, dragging\n");
285 floating_drag_window(conn, client, event);
286 } else if (event->detail == 3) {
287 bool proportional = (event->state & BIND_SHIFT);
288 DLOG("right mouse button\n");
289 floating_resize_window(conn, client, proportional, event);
294 if (!floating_mod_on_tiled_client(conn, client, event)) {
295 xcb_allow_events(conn, XCB_ALLOW_REPLAY_POINTER, event->time);
302 if (client == NULL) {
303 /* The client was neither on a client’s titlebar nor on a client itself, maybe on a stack_window? */
304 if (button_press_stackwin(conn, event))
308 if (button_press_bar(conn, event))
311 DLOG("Could not handle this button press\n");
315 /* Set focus in any case */
316 set_focus(conn, client, true);
318 /* Let’s see if this was on the borders (= resize). If not, we’re done */
319 DLOG("press button on x=%d, y=%d\n", event->event_x, event->event_y);
320 resize_orientation_t orientation = O_VERTICAL;
321 Container *con = client->container;
325 DLOG("dock. done.\n");
326 xcb_allow_events(conn, XCB_ALLOW_REPLAY_POINTER, event->time);
331 DLOG("event->event_x = %d, client->rect.width = %d\n", event->event_x, client->rect.width);
333 /* Some clients (xfontsel for example) seem to pass clicks on their
334 * window to the parent window, thus we receive an event here which in
335 * reality is a border_click. Check for the position and fix state. */
337 event->event_x >= client->child_rect.x &&
338 event->event_x <= (client->child_rect.x + client->child_rect.width) &&
339 event->event_y >= client->child_rect.y &&
340 event->event_y <= (client->child_rect.y + client->child_rect.height)) {
341 DLOG("Fixing border_click = false because of click in child\n");
342 border_click = false;
346 DLOG("client. done.\n");
347 xcb_allow_events(conn, XCB_ALLOW_REPLAY_POINTER, event->time);
348 /* Floating clients should be raised on click */
349 if (client_is_floating(client))
350 xcb_raise_window(conn, client->frame);
355 /* Don’t handle events inside the titlebar, only borders are interesting */
356 i3Font *font = load_font(conn, config.font);
357 if (event->event_y >= 2 && event->event_y <= (font->height + 2 + 2)) {
358 DLOG("click on titlebar\n");
360 /* Floating clients can be dragged by grabbing their titlebar */
361 if (client_is_floating(client)) {
362 /* Firstly, we raise it. Maybe the user just wanted to raise it without grabbing */
363 xcb_raise_window(conn, client->frame);
366 floating_drag_window(conn, client, event);
371 if (client_is_floating(client))
372 return floating_border_click(conn, client, event);
374 Workspace *ws = con->workspace;
376 if (event->event_y < 2) {
377 /* This was a press on the top border */
380 first = con->row - 1;
382 orientation = O_HORIZONTAL;
383 } else if (event->event_y >= (client->rect.height - 2)) {
385 first = con->row + (con->rowspan - 1);
386 if (!cell_exists(ws, con->col, first) ||
387 (first == (ws->rows-1)))
391 orientation = O_HORIZONTAL;
392 } else if (event->event_x <= 2) {
397 first = con->col - 1;
399 } else if (event->event_x > 2) {
401 first = con->col + (con->colspan - 1);
402 DLOG("column %d\n", first);
404 if (!cell_exists(ws, first, con->row) ||
405 (first == (ws->cols-1)))
411 return resize_graphical_handler(conn, ws, first, second, orientation, event);