]> 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
40 static struct Stack_Window *get_stack_window(xcb_window_t window_id) {
41         struct Stack_Window *current;
42
43         SLIST_FOREACH(current, &stack_wins, stack_windows) {
44                 if (current->window != window_id)
45                         continue;
46
47                 return current;
48         }
49
50         return NULL;
51 }
52
53 /*
54  * Checks if the button press was on a stack window, handles focus setting and returns true
55  * if so, or false otherwise.
56  *
57  */
58 static bool button_press_stackwin(xcb_connection_t *conn, xcb_button_press_event_t *event) {
59         struct Stack_Window *stack_win;
60
61         /* If we find a corresponding stack window, we can handle the event */
62         if ((stack_win = get_stack_window(event->event)) == NULL)
63                 return false;
64
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);
70                 return true;
71         }
72
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),
79             c = 0,
80             num_clients = 0;
81         Client *client;
82         Container *container = stack_win->container;
83
84         CIRCLEQ_FOREACH(client, &(container->clients), clients)
85                 num_clients++;
86
87         /* If we don’t have any clients in this container, we cannot do
88          * anything useful anyways. */
89         if (num_clients == 0)
90                 return true;
91
92         if (container->mode == MODE_TABBED)
93                 destination = (event->event_x / (container->width / num_clients));
94         else if (container->mode == MODE_STACK &&
95                  container->stack_limit != STACK_LIMIT_NONE) {
96                 if (container->stack_limit == STACK_LIMIT_COLS) {
97                         int wrap = ceil((float)num_clients / container->stack_limit_value);
98                         int clicked_column = (event->event_x / (stack_win->rect.width / container->stack_limit_value));
99                         int clicked_row = (event->event_y / decoration_height);
100                         LOG("clicked on column %d, row %d\n", clicked_column, clicked_row);
101                         destination = (wrap * clicked_column) + clicked_row;
102                 } else {
103                         int width = (stack_win->rect.width / ceil((float)num_clients / container->stack_limit_value));
104                         int clicked_column = (event->event_x / width);
105                         int clicked_row = (event->event_y / decoration_height);
106                         LOG("clicked on column %d, row %d\n", clicked_column, clicked_row);
107                         destination = (container->stack_limit_value * clicked_column) + clicked_row;
108                 }
109         }
110
111         LOG("Click on stack_win for client %d\n", destination);
112         CIRCLEQ_FOREACH(client, &(stack_win->container->clients), clients)
113                 if (c++ == destination) {
114                         set_focus(conn, client, true);
115                         return true;
116                 }
117
118         return true;
119 }
120
121 /*
122  * Checks if the button press was on a bar, switches to the workspace and returns true
123  * if so, or false otherwise.
124  *
125  */
126 static bool button_press_bar(xcb_connection_t *conn, xcb_button_press_event_t *event) {
127         i3Screen *screen;
128         TAILQ_FOREACH(screen, virtual_screens, screens) {
129                 if (screen->bar != event->event)
130                         continue;
131
132                 LOG("Click on a bar\n");
133
134                 /* Check if the button was one of button4 or button5 (scroll up / scroll down) */
135                 if (event->detail == XCB_BUTTON_INDEX_4 || event->detail == XCB_BUTTON_INDEX_5) {
136                         Workspace *ws = c_ws;
137                         if (event->detail == XCB_BUTTON_INDEX_5) {
138                                 while ((ws = TAILQ_NEXT(ws, workspaces)) != TAILQ_END(workspaces_head)) {
139                                         if (ws->screen == screen) {
140                                                 workspace_show(conn, ws->num + 1);
141                                                 return true;
142                                         }
143                                 }
144                         } else {
145                                 while ((ws = TAILQ_PREV(ws, workspaces_head, workspaces)) != TAILQ_END(workspaces)) {
146                                         if (ws->screen == screen) {
147                                                 workspace_show(conn, ws->num + 1);
148                                                 return true;
149                                         }
150                                 }
151                         }
152                         return true;
153                 }
154                 int drawn = 0;
155                 /* Because workspaces can be on different screens, we need to loop
156                    through all of them and decide to count it based on its ->screen */
157                 Workspace *ws;
158                 TAILQ_FOREACH(ws, workspaces, workspaces) {
159                         if (ws->screen != screen)
160                                 continue;
161                         LOG("Checking if click was on workspace %d with drawn = %d, tw = %d\n",
162                                         ws->num, drawn, ws->text_width);
163                         if (event->event_x > (drawn + 1) &&
164                             event->event_x <= (drawn + 1 + ws->text_width + 5 + 5)) {
165                                 workspace_show(conn, ws->num + 1);
166                                 return true;
167                         }
168
169                         drawn += ws->text_width + 5 + 5 + 2;
170                 }
171                 return true;
172         }
173
174         return false;
175 }
176
177 /*
178  * Called when the user clicks using the floating_modifier, but the client is in
179  * tiling layout.
180  *
181  * Returns false if it does not do anything (that is, the click should be sent
182  * to the client).
183  *
184  */
185 static bool floating_mod_on_tiled_client(xcb_connection_t *conn, Client *client,
186                                          xcb_button_press_event_t *event) {
187         /* Only the right mouse button is interesting for us at the moment */
188         if (event->detail != 3)
189                 return false;
190
191         /* The client is in tiling layout. We can still
192          * initiate a resize with the right mouse button,
193          * by chosing the border which is the most near one
194          * to the position of the mouse pointer */
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;
203
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);
206
207         if (to_right < to_left &&
208             to_right < to_top &&
209             to_right < to_bottom) {
210                 /* …right border */
211                 first = con->col + (con->colspan - 1);
212                 LOG("column %d\n", first);
213
214                 if (!cell_exists(ws, first, con->row) ||
215                     (first == (ws->cols-1)))
216                         return false;
217
218                 second = first + 1;
219         } else if (to_left < to_right &&
220                    to_left < to_top &&
221                    to_left < to_bottom) {
222                 /* …left border */
223                 if (con->col == 0)
224                         return false;
225
226                 first = con->col - 1;
227                 second = con->col;
228         } else if (to_top < to_right &&
229                    to_top < to_left &&
230                    to_top < to_bottom) {
231                 /* This was a press on the top border */
232                 if (con->row == 0)
233                         return false;
234                 first = con->row - 1;
235                 second = con->row;
236                 orientation = O_HORIZONTAL;
237         } else if (to_bottom < to_right &&
238                    to_bottom < to_left &&
239                    to_bottom < to_top) {
240                 /* …bottom border */
241                 first = con->row + (con->rowspan - 1);
242                 if (!cell_exists(ws, con->col, first) ||
243                     (first == (ws->rows-1)))
244                         return false;
245
246                 second = first + 1;
247                 orientation = O_HORIZONTAL;
248         }
249
250        return resize_graphical_handler(conn, ws, first, second, orientation, event);
251 }
252
253 int handle_button_press(void *ignored, xcb_connection_t *conn, xcb_button_press_event_t *event) {
254         LOG("Button %d pressed\n", event->state);
255         /* This was either a focus for a client’s parent (= titlebar)… */
256         Client *client = table_get(&by_child, event->event);
257         bool border_click = false;
258         if (client == NULL) {
259                 client = table_get(&by_parent, event->event);
260                 border_click = true;
261         }
262         /* See if this was a click with the configured modifier. If so, we need
263          * to move around the client if it was floating. if not, we just process
264          * as usual. */
265         if (config.floating_modifier != 0 &&
266             (event->state & config.floating_modifier) != 0) {
267                 if (client == NULL) {
268                         LOG("Not handling, floating_modifier was pressed and no client found\n");
269                         return 1;
270                 }
271                 if (client->fullscreen) {
272                         LOG("Not handling, client is in fullscreen mode\n");
273                         return 1;
274                 }
275                 if (client_is_floating(client)) {
276                         LOG("button %d pressed\n", event->detail);
277                         if (event->detail == 1) {
278                                 LOG("left mouse button, dragging\n");
279                                 floating_drag_window(conn, client, event);
280                         } else if (event->detail == 3) {
281                                 LOG("right mouse button\n");
282                                 floating_resize_window(conn, client, event);
283                         }
284                         return 1;
285                 }
286
287                 if (!floating_mod_on_tiled_client(conn, client, event)) {
288                         xcb_allow_events(conn, XCB_ALLOW_REPLAY_POINTER, event->time);
289                         xcb_flush(conn);
290                 }
291
292                 return 1;
293         }
294
295         if (client == NULL) {
296                 /* The client was neither on a client’s titlebar nor on a client itself, maybe on a stack_window? */
297                 if (button_press_stackwin(conn, event))
298                         return 1;
299
300                 /* Or on a bar? */
301                 if (button_press_bar(conn, event))
302                         return 1;
303
304                 LOG("Could not handle this button press\n");
305                 return 1;
306         }
307
308         /* Set focus in any case */
309         set_focus(conn, client, true);
310
311         /* Let’s see if this was on the borders (= resize). If not, we’re done */
312         LOG("press button on x=%d, y=%d\n", event->event_x, event->event_y);
313         resize_orientation_t orientation = O_VERTICAL;
314         Container *con = client->container;
315         int first, second;
316
317         if (client->dock) {
318                 LOG("dock. done.\n");
319                 xcb_allow_events(conn, XCB_ALLOW_REPLAY_POINTER, event->time);
320                 xcb_flush(conn);
321                 return 1;
322         }
323
324         LOG("event->event_x = %d, client->rect.width = %d\n", event->event_x, client->rect.width);
325
326         /* Some clients (xfontsel for example) seem to pass clicks on their
327          * window to the parent window, thus we receive an event here which in
328          * reality is a border_click. Check for the position and fix state. */
329         if (border_click &&
330             event->event_x >= client->child_rect.x &&
331             event->event_x <= (client->child_rect.x + client->child_rect.width) &&
332             event->event_y >= client->child_rect.y &&
333             event->event_y <= (client->child_rect.y + client->child_rect.height)) {
334                 LOG("Fixing border_click = false because of click in child\n");
335                 border_click = false;
336         }
337
338         if (!border_click) {
339                 LOG("client. done.\n");
340                 xcb_allow_events(conn, XCB_ALLOW_REPLAY_POINTER, event->time);
341                 /* Floating clients should be raised on click */
342                 if (client_is_floating(client))
343                         xcb_raise_window(conn, client->frame);
344                 xcb_flush(conn);
345                 return 1;
346         }
347
348         /* Don’t handle events inside the titlebar, only borders are interesting */
349         i3Font *font = load_font(conn, config.font);
350         if (event->event_y >= 2 && event->event_y <= (font->height + 2 + 2)) {
351                 LOG("click on titlebar\n");
352
353                 /* Floating clients can be dragged by grabbing their titlebar */
354                 if (client_is_floating(client)) {
355                         /* Firstly, we raise it. Maybe the user just wanted to raise it without grabbing */
356                         xcb_raise_window(conn, client->frame);
357                         xcb_flush(conn);
358
359                         floating_drag_window(conn, client, event);
360                 }
361                 return 1;
362         }
363
364         if (client_is_floating(client))
365                 return floating_border_click(conn, client, event);
366
367         Workspace *ws = con->workspace;
368
369         if (event->event_y < 2) {
370                 /* This was a press on the top border */
371                 if (con->row == 0)
372                         return 1;
373                 first = con->row - 1;
374                 second = con->row;
375                 orientation = O_HORIZONTAL;
376         } else if (event->event_y >= (client->rect.height - 2)) {
377                 /* …bottom border */
378                 first = con->row + (con->rowspan - 1);
379                 if (!cell_exists(ws, con->col, first) ||
380                     (first == (ws->rows-1)))
381                         return 1;
382
383                 second = first + 1;
384                 orientation = O_HORIZONTAL;
385         } else if (event->event_x <= 2) {
386                 /* …left border */
387                 if (con->col == 0)
388                         return 1;
389
390                 first = con->col - 1;
391                 second = con->col;
392         } else if (event->event_x > 2) {
393                 /* …right border */
394                 first = con->col + (con->colspan - 1);
395                 LOG("column %d\n", first);
396
397                 if (!cell_exists(ws, first, con->row) ||
398                     (first == (ws->cols-1)))
399                         return 1;
400
401                 second = first + 1;
402         }
403
404         return resize_graphical_handler(conn, ws, first, second, orientation, event);
405 }