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 LOG("Button %d pressed\n", event->state);
243 con = con_by_window_id(event->event);
244 bool border_click = false;
246 con = con_by_frame_id(event->event);
249 //if (con && con->type == CT_FLOATING_CON)
250 //con = TAILQ_FIRST(&(con->nodes_head));
252 /* See if this was a click with the configured modifier. If so, we need
253 * to move around the client if it was floating. if not, we just process
255 //if (config.floating_modifier != 0 &&
256 //(event->state & config.floating_modifier) == config.floating_modifier) {
258 LOG("Not handling, floating_modifier was pressed and no client found\n");
262 if (con->fullscreen) {
263 LOG("Not handling, client is in fullscreen mode\n");
267 if (con->type == CT_FLOATING_CON) {
268 LOG("button %d pressed\n", event->detail);
269 if (event->detail == 1) {
270 LOG("left mouse button, dragging\n");
271 floating_drag_window(con, event);
274 else if (event->detail == 3) {
275 bool proportional = (event->state & BIND_SHIFT);
276 DLOG("right mouse button\n");
277 floating_resize_window(conn, client, proportional, event);
284 if (!floating_mod_on_tiled_client(conn, client, event)) {
285 xcb_allow_events(conn, XCB_ALLOW_REPLAY_POINTER, event->time);
294 if (client == NULL) {
295 /* The client was neither on a client’s titlebar nor on a client itself, maybe on a stack_window? */
296 if (button_press_stackwin(conn, event))
300 if (button_press_bar(conn, event))
303 DLOG("Could not handle this button press\n");
307 /* Set focus in any case */
308 set_focus(conn, client, true);
310 /* Let’s see if this was on the borders (= resize). If not, we’re done */
311 DLOG("press button on x=%d, y=%d\n", event->event_x, event->event_y);
312 resize_orientation_t orientation = O_VERTICAL;
313 Container *con = client->container;
317 DLOG("dock. done.\n");
318 xcb_allow_events(conn, XCB_ALLOW_REPLAY_POINTER, event->time);
323 DLOG("event->event_x = %d, client->rect.width = %d\n", event->event_x, client->rect.width);
325 /* Some clients (xfontsel for example) seem to pass clicks on their
326 * window to the parent window, thus we receive an event here which in
327 * reality is a border_click. Check for the position and fix state. */
329 event->event_x >= client->child_rect.x &&
330 event->event_x <= (client->child_rect.x + client->child_rect.width) &&
331 event->event_y >= client->child_rect.y &&
332 event->event_y <= (client->child_rect.y + client->child_rect.height)) {
333 DLOG("Fixing border_click = false because of click in child\n");
334 border_click = false;
338 DLOG("client. done.\n");
339 xcb_allow_events(conn, XCB_ALLOW_REPLAY_POINTER, event->time);
340 /* Floating clients should be raised on click */
341 if (client_is_floating(client))
342 xcb_raise_window(conn, client->frame);
347 /* Don’t handle events inside the titlebar, only borders are interesting */
348 i3Font *font = load_font(conn, config.font);
349 if (event->event_y >= 2 && event->event_y <= (font->height + 2 + 2)) {
350 DLOG("click on titlebar\n");
352 /* Floating clients can be dragged by grabbing their titlebar */
353 if (client_is_floating(client)) {
354 /* Firstly, we raise it. Maybe the user just wanted to raise it without grabbing */
355 xcb_raise_window(conn, client->frame);
358 floating_drag_window(conn, client, event);
363 if (client_is_floating(client))
364 return floating_border_click(conn, client, event);
366 Workspace *ws = con->workspace;
368 if (event->event_y < 2) {
369 /* This was a press on the top border */
372 first = con->row - 1;
374 orientation = O_HORIZONTAL;
375 } else if (event->event_y >= (client->rect.height - 2)) {
377 first = con->row + (con->rowspan - 1);
378 if (!cell_exists(ws, con->col, first) ||
379 (first == (ws->rows-1)))
383 orientation = O_HORIZONTAL;
384 } else if (event->event_x <= 2) {
389 first = con->col - 1;
391 } else if (event->event_x > 2) {
393 first = con->col + (con->colspan - 1);
394 DLOG("column %d\n", first);
396 if (!cell_exists(ws, first, con->row) ||
397 (first == (ws->cols-1)))
403 return resize_graphical_handler(conn, ws, first, second, orientation, event);