]> git.sur5r.net Git - i3/i3/blob - src/click.c
Bugfix: Replay unhandled pointer events (Thanks Marcus)
[i3/i3] / src / click.c
1 /*
2  * vim:ts=8:expandtab
3  *
4  * i3 - an improved dynamic tiling window manager
5  *
6  * © 2009-2010 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 #include "randr.h"
41
42 static struct Stack_Window *get_stack_window(xcb_window_t window_id) {
43         struct Stack_Window *current;
44
45         SLIST_FOREACH(current, &stack_wins, stack_windows) {
46                 if (current->window != window_id)
47                         continue;
48
49                 return current;
50         }
51
52         return NULL;
53 }
54
55 /*
56  * Checks if the button press was on a stack window, handles focus setting and returns true
57  * if so, or false otherwise.
58  *
59  */
60 static bool button_press_stackwin(xcb_connection_t *conn, xcb_button_press_event_t *event) {
61         struct Stack_Window *stack_win;
62
63         /* If we find a corresponding stack window, we can handle the event */
64         if ((stack_win = get_stack_window(event->event)) == NULL)
65                 return false;
66
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);
72                 return true;
73         }
74
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),
81             c = 0,
82             num_clients = 0;
83         Client *client;
84         Container *container = stack_win->container;
85
86         CIRCLEQ_FOREACH(client, &(container->clients), clients)
87                 num_clients++;
88
89         /* If we don’t have any clients in this container, we cannot do
90          * anything useful anyways. */
91         if (num_clients == 0)
92                 return true;
93
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;
104                 } else {
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;
110                 }
111         }
112
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);
117                         return true;
118                 }
119
120         return true;
121 }
122
123 /*
124  * Checks if the button press was on a bar, switches to the workspace and returns true
125  * if so, or false otherwise.
126  *
127  */
128 static bool button_press_bar(xcb_connection_t *conn, xcb_button_press_event_t *event) {
129         Output *output;
130         TAILQ_FOREACH(output, &outputs, outputs) {
131                 if (output->bar != event->event)
132                         continue;
133
134                 DLOG("Click on a bar\n");
135
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);
143                                                 return true;
144                                         }
145                                 }
146                         } else {
147                                 while ((ws = TAILQ_PREV(ws, workspaces_head, workspaces)) != TAILQ_END(workspaces)) {
148                                         if (ws->output == output) {
149                                                 workspace_show(conn, ws->num + 1);
150                                                 return true;
151                                         }
152                                 }
153                         }
154                         return true;
155                 }
156                 int drawn = 0;
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 */
159                 Workspace *ws;
160                 TAILQ_FOREACH(ws, workspaces, workspaces) {
161                         if (ws->output != output)
162                                 continue;
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);
168                                 return true;
169                         }
170
171                         drawn += ws->text_width + 5 + 5 + 2;
172                 }
173                 return true;
174         }
175
176         return false;
177 }
178
179 /*
180  * Called when the user clicks using the floating_modifier, but the client is in
181  * tiling layout.
182  *
183  * Returns false if it does not do anything (that is, the click should be sent
184  * to the client).
185  *
186  */
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)
191                 return false;
192
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;
205
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);
208
209         if (to_right < to_left &&
210             to_right < to_top &&
211             to_right < to_bottom) {
212                 /* …right border */
213                 first = con->col + (con->colspan - 1);
214                 DLOG("column %d\n", first);
215
216                 if (!cell_exists(ws, first, con->row) ||
217                     (first == (ws->cols-1)))
218                         return false;
219
220                 second = first + 1;
221         } else if (to_left < to_right &&
222                    to_left < to_top &&
223                    to_left < to_bottom) {
224                 /* …left border */
225                 if (con->col == 0)
226                         return false;
227
228                 first = con->col - 1;
229                 second = con->col;
230         } else if (to_top < to_right &&
231                    to_top < to_left &&
232                    to_top < to_bottom) {
233                 /* This was a press on the top border */
234                 if (con->row == 0)
235                         return false;
236                 first = con->row - 1;
237                 second = con->row;
238                 orientation = O_HORIZONTAL;
239         } else if (to_bottom < to_right &&
240                    to_bottom < to_left &&
241                    to_bottom < to_top) {
242                 /* …bottom border */
243                 first = con->row + (con->rowspan - 1);
244                 if (!cell_exists(ws, con->col, first) ||
245                     (first == (ws->rows-1)))
246                         return false;
247
248                 second = first + 1;
249                 orientation = O_HORIZONTAL;
250         }
251
252        return resize_graphical_handler(conn, ws, first, second, orientation, event);
253 }
254
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);
262                 border_click = true;
263         }
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
266          * as usual. */
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);
272                         xcb_flush(conn);
273                         return 1;
274                 }
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);
278                         xcb_flush(conn);
279                         return 1;
280                 }
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);
290                         }
291                         return 1;
292                 }
293
294                 if (!floating_mod_on_tiled_client(conn, client, event)) {
295                         xcb_allow_events(conn, XCB_ALLOW_REPLAY_POINTER, event->time);
296                         xcb_flush(conn);
297                 }
298
299                 return 1;
300         }
301
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))
305                         return 1;
306
307                 /* Or on a bar? */
308                 if (button_press_bar(conn, event))
309                         return 1;
310
311                 DLOG("Could not handle this button press\n");
312                 xcb_allow_events(conn, XCB_ALLOW_REPLAY_POINTER, event->time);
313                 xcb_flush(conn);
314                 return 1;
315         }
316
317         /* Set focus in any case */
318         set_focus(conn, client, true);
319
320         /* Let’s see if this was on the borders (= resize). If not, we’re done */
321         DLOG("press button on x=%d, y=%d\n", event->event_x, event->event_y);
322         resize_orientation_t orientation = O_VERTICAL;
323         Container *con = client->container;
324         int first, second;
325
326         if (client->dock) {
327                 DLOG("dock. done.\n");
328                 xcb_allow_events(conn, XCB_ALLOW_REPLAY_POINTER, event->time);
329                 xcb_flush(conn);
330                 return 1;
331         }
332
333         DLOG("event->event_x = %d, client->rect.width = %d\n", event->event_x, client->rect.width);
334
335         /* Some clients (xfontsel for example) seem to pass clicks on their
336          * window to the parent window, thus we receive an event here which in
337          * reality is a border_click. Check for the position and fix state. */
338         if (border_click &&
339             event->event_x >= client->child_rect.x &&
340             event->event_x <= (client->child_rect.x + client->child_rect.width) &&
341             event->event_y >= client->child_rect.y &&
342             event->event_y <= (client->child_rect.y + client->child_rect.height)) {
343                 DLOG("Fixing border_click = false because of click in child\n");
344                 border_click = false;
345         }
346
347         if (!border_click) {
348                 DLOG("client. done.\n");
349                 xcb_allow_events(conn, XCB_ALLOW_REPLAY_POINTER, event->time);
350                 /* Floating clients should be raised on click */
351                 if (client_is_floating(client))
352                         xcb_raise_window(conn, client->frame);
353                 xcb_flush(conn);
354                 return 1;
355         }
356
357         /* Don’t handle events inside the titlebar, only borders are interesting */
358         i3Font *font = load_font(conn, config.font);
359         if (event->event_y >= 2 && event->event_y <= (font->height + 2 + 2)) {
360                 DLOG("click on titlebar\n");
361
362                 /* Floating clients can be dragged by grabbing their titlebar */
363                 if (client_is_floating(client)) {
364                         /* Firstly, we raise it. Maybe the user just wanted to raise it without grabbing */
365                         xcb_raise_window(conn, client->frame);
366                         xcb_flush(conn);
367
368                         floating_drag_window(conn, client, event);
369                 }
370                 return 1;
371         }
372
373         if (client_is_floating(client))
374                 return floating_border_click(conn, client, event);
375
376         Workspace *ws = con->workspace;
377
378         if (event->event_y < 2) {
379                 /* This was a press on the top border */
380                 if (con->row == 0)
381                         return 1;
382                 first = con->row - 1;
383                 second = con->row;
384                 orientation = O_HORIZONTAL;
385         } else if (event->event_y >= (client->rect.height - 2)) {
386                 /* …bottom border */
387                 first = con->row + (con->rowspan - 1);
388                 if (!cell_exists(ws, con->col, first) ||
389                     (first == (ws->rows-1)))
390                         return 1;
391
392                 second = first + 1;
393                 orientation = O_HORIZONTAL;
394         } else if (event->event_x <= 2) {
395                 /* …left border */
396                 if (con->col == 0)
397                         return 1;
398
399                 first = con->col - 1;
400                 second = con->col;
401         } else if (event->event_x > 2) {
402                 /* …right border */
403                 first = con->col + (con->colspan - 1);
404                 DLOG("column %d\n", first);
405
406                 if (!cell_exists(ws, first, con->row) ||
407                     (first == (ws->cols-1)))
408                         return 1;
409
410                 second = first + 1;
411         }
412
413         return resize_graphical_handler(conn, ws, first, second, orientation, event);
414 }