]> git.sur5r.net Git - i3/i3/blob - src/click.c
Bugfix: Containers could lose their snap state (Thanks Atsutane)
[i3/i3] / src / click.c
1 /*
2  * vim:ts=8:expandtab
3  *
4  * i3 - an improved dynamic tiling window manager
5  *
6  * © 2009 Michael Stapelberg and contributors
7  *
8  * See file LICENSE for license information.
9  *
10  * src/click.c: Contains the handlers for button press (mouse click) events
11  *              because they are quite large.
12  *
13  */
14 #include <stdio.h>
15 #include <assert.h>
16 #include <string.h>
17 #include <stdlib.h>
18 #include <time.h>
19 #include <stdbool.h>
20 #include <math.h>
21
22 #include <xcb/xcb.h>
23 #include <xcb/xcb_atom.h>
24 #include <xcb/xcb_icccm.h>
25
26 #include <X11/XKBlib.h>
27
28 #include "i3.h"
29 #include "queue.h"
30 #include "table.h"
31 #include "config.h"
32 #include "util.h"
33 #include "xcb.h"
34 #include "client.h"
35 #include "workspace.h"
36 #include "commands.h"
37 #include "floating.h"
38 #include "resize.h"
39 #include "log.h"
40
41 static struct Stack_Window *get_stack_window(xcb_window_t window_id) {
42         struct Stack_Window *current;
43
44         SLIST_FOREACH(current, &stack_wins, stack_windows) {
45                 if (current->window != window_id)
46                         continue;
47
48                 return current;
49         }
50
51         return NULL;
52 }
53
54 /*
55  * Checks if the button press was on a stack window, handles focus setting and returns true
56  * if so, or false otherwise.
57  *
58  */
59 static bool button_press_stackwin(xcb_connection_t *conn, xcb_button_press_event_t *event) {
60         struct Stack_Window *stack_win;
61
62         /* If we find a corresponding stack window, we can handle the event */
63         if ((stack_win = get_stack_window(event->event)) == NULL)
64                 return false;
65
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);
71                 return true;
72         }
73
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),
80             c = 0,
81             num_clients = 0;
82         Client *client;
83         Container *container = stack_win->container;
84
85         CIRCLEQ_FOREACH(client, &(container->clients), clients)
86                 num_clients++;
87
88         /* If we don’t have any clients in this container, we cannot do
89          * anything useful anyways. */
90         if (num_clients == 0)
91                 return true;
92
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;
103                 } else {
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;
109                 }
110         }
111
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);
116                         return true;
117                 }
118
119         return true;
120 }
121
122 /*
123  * Checks if the button press was on a bar, switches to the workspace and returns true
124  * if so, or false otherwise.
125  *
126  */
127 static bool button_press_bar(xcb_connection_t *conn, xcb_button_press_event_t *event) {
128         i3Screen *screen;
129         TAILQ_FOREACH(screen, virtual_screens, screens) {
130                 if (screen->bar != event->event)
131                         continue;
132
133                 DLOG("Click on a bar\n");
134
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);
142                                                 return true;
143                                         }
144                                 }
145                         } else {
146                                 while ((ws = TAILQ_PREV(ws, workspaces_head, workspaces)) != TAILQ_END(workspaces)) {
147                                         if (ws->screen == screen) {
148                                                 workspace_show(conn, ws->num + 1);
149                                                 return true;
150                                         }
151                                 }
152                         }
153                         return true;
154                 }
155                 int drawn = 0;
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 */
158                 Workspace *ws;
159                 TAILQ_FOREACH(ws, workspaces, workspaces) {
160                         if (ws->screen != screen)
161                                 continue;
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);
167                                 return true;
168                         }
169
170                         drawn += ws->text_width + 5 + 5 + 2;
171                 }
172                 return true;
173         }
174
175         return false;
176 }
177
178 /*
179  * Called when the user clicks using the floating_modifier, but the client is in
180  * tiling layout.
181  *
182  * Returns false if it does not do anything (that is, the click should be sent
183  * to the client).
184  *
185  */
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)
190                 return false;
191
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;
204
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);
207
208         if (to_right < to_left &&
209             to_right < to_top &&
210             to_right < to_bottom) {
211                 /* …right border */
212                 first = con->col + (con->colspan - 1);
213                 DLOG("column %d\n", first);
214
215                 if (!cell_exists(ws, first, con->row) ||
216                     (first == (ws->cols-1)))
217                         return false;
218
219                 second = first + 1;
220         } else if (to_left < to_right &&
221                    to_left < to_top &&
222                    to_left < to_bottom) {
223                 /* …left border */
224                 if (con->col == 0)
225                         return false;
226
227                 first = con->col - 1;
228                 second = con->col;
229         } else if (to_top < to_right &&
230                    to_top < to_left &&
231                    to_top < to_bottom) {
232                 /* This was a press on the top border */
233                 if (con->row == 0)
234                         return false;
235                 first = con->row - 1;
236                 second = con->row;
237                 orientation = O_HORIZONTAL;
238         } else if (to_bottom < to_right &&
239                    to_bottom < to_left &&
240                    to_bottom < to_top) {
241                 /* …bottom border */
242                 first = con->row + (con->rowspan - 1);
243                 if (!cell_exists(ws, con->col, first) ||
244                     (first == (ws->rows-1)))
245                         return false;
246
247                 second = first + 1;
248                 orientation = O_HORIZONTAL;
249         }
250
251        return resize_graphical_handler(conn, ws, first, second, orientation, event);
252 }
253
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);
261                 border_click = true;
262         }
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
265          * as usual. */
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");
270                         return 1;
271                 }
272                 if (client->fullscreen) {
273                         DLOG("Not handling, client is in fullscreen mode\n");
274                         return 1;
275                 }
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);
284                         }
285                         return 1;
286                 }
287
288                 if (!floating_mod_on_tiled_client(conn, client, event)) {
289                         xcb_allow_events(conn, XCB_ALLOW_REPLAY_POINTER, event->time);
290                         xcb_flush(conn);
291                 }
292
293                 return 1;
294         }
295
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))
299                         return 1;
300
301                 /* Or on a bar? */
302                 if (button_press_bar(conn, event))
303                         return 1;
304
305                 DLOG("Could not handle this button press\n");
306                 return 1;
307         }
308
309         /* Set focus in any case */
310         set_focus(conn, client, true);
311
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;
316         int first, second;
317
318         if (client->dock) {
319                 DLOG("dock. done.\n");
320                 xcb_allow_events(conn, XCB_ALLOW_REPLAY_POINTER, event->time);
321                 xcb_flush(conn);
322                 return 1;
323         }
324
325         DLOG("event->event_x = %d, client->rect.width = %d\n", event->event_x, client->rect.width);
326
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. */
330         if (border_click &&
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;
337         }
338
339         if (!border_click) {
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);
345                 xcb_flush(conn);
346                 return 1;
347         }
348
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");
353
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);
358                         xcb_flush(conn);
359
360                         floating_drag_window(conn, client, event);
361                 }
362                 return 1;
363         }
364
365         if (client_is_floating(client))
366                 return floating_border_click(conn, client, event);
367
368         Workspace *ws = con->workspace;
369
370         if (event->event_y < 2) {
371                 /* This was a press on the top border */
372                 if (con->row == 0)
373                         return 1;
374                 first = con->row - 1;
375                 second = con->row;
376                 orientation = O_HORIZONTAL;
377         } else if (event->event_y >= (client->rect.height - 2)) {
378                 /* …bottom border */
379                 first = con->row + (con->rowspan - 1);
380                 if (!cell_exists(ws, con->col, first) ||
381                     (first == (ws->rows-1)))
382                         return 1;
383
384                 second = first + 1;
385                 orientation = O_HORIZONTAL;
386         } else if (event->event_x <= 2) {
387                 /* …left border */
388                 if (con->col == 0)
389                         return 1;
390
391                 first = con->col - 1;
392                 second = con->col;
393         } else if (event->event_x > 2) {
394                 /* …right border */
395                 first = con->col + (con->colspan - 1);
396                 DLOG("column %d\n", first);
397
398                 if (!cell_exists(ws, first, con->row) ||
399                     (first == (ws->cols-1)))
400                         return 1;
401
402                 second = first + 1;
403         }
404
405         return resize_graphical_handler(conn, ws, first, second, orientation, event);
406 }