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