]> git.sur5r.net Git - i3/i3/blob - src/click.c
huge change: implement RandR instead of Xinerama
[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) != 0) {
269                 if (client == NULL) {
270                         DLOG("Not handling, floating_modifier was pressed and no client found\n");
271                         return 1;
272                 }
273                 if (client->fullscreen) {
274                         DLOG("Not handling, client is in fullscreen mode\n");
275                         return 1;
276                 }
277                 if (client_is_floating(client)) {
278                         DLOG("button %d pressed\n", event->detail);
279                         if (event->detail == 1) {
280                                 DLOG("left mouse button, dragging\n");
281                                 floating_drag_window(conn, client, event);
282                         } else if (event->detail == 3) {
283                                 DLOG("right mouse button\n");
284                                 floating_resize_window(conn, client, event);
285                         }
286                         return 1;
287                 }
288
289                 if (!floating_mod_on_tiled_client(conn, client, event)) {
290                         xcb_allow_events(conn, XCB_ALLOW_REPLAY_POINTER, event->time);
291                         xcb_flush(conn);
292                 }
293
294                 return 1;
295         }
296
297         if (client == NULL) {
298                 /* The client was neither on a client’s titlebar nor on a client itself, maybe on a stack_window? */
299                 if (button_press_stackwin(conn, event))
300                         return 1;
301
302                 /* Or on a bar? */
303                 if (button_press_bar(conn, event))
304                         return 1;
305
306                 DLOG("Could not handle this button press\n");
307                 return 1;
308         }
309
310         /* Set focus in any case */
311         set_focus(conn, client, true);
312
313         /* Let’s see if this was on the borders (= resize). If not, we’re done */
314         DLOG("press button on x=%d, y=%d\n", event->event_x, event->event_y);
315         resize_orientation_t orientation = O_VERTICAL;
316         Container *con = client->container;
317         int first, second;
318
319         if (client->dock) {
320                 DLOG("dock. done.\n");
321                 xcb_allow_events(conn, XCB_ALLOW_REPLAY_POINTER, event->time);
322                 xcb_flush(conn);
323                 return 1;
324         }
325
326         DLOG("event->event_x = %d, client->rect.width = %d\n", event->event_x, client->rect.width);
327
328         /* Some clients (xfontsel for example) seem to pass clicks on their
329          * window to the parent window, thus we receive an event here which in
330          * reality is a border_click. Check for the position and fix state. */
331         if (border_click &&
332             event->event_x >= client->child_rect.x &&
333             event->event_x <= (client->child_rect.x + client->child_rect.width) &&
334             event->event_y >= client->child_rect.y &&
335             event->event_y <= (client->child_rect.y + client->child_rect.height)) {
336                 DLOG("Fixing border_click = false because of click in child\n");
337                 border_click = false;
338         }
339
340         if (!border_click) {
341                 DLOG("client. done.\n");
342                 xcb_allow_events(conn, XCB_ALLOW_REPLAY_POINTER, event->time);
343                 /* Floating clients should be raised on click */
344                 if (client_is_floating(client))
345                         xcb_raise_window(conn, client->frame);
346                 xcb_flush(conn);
347                 return 1;
348         }
349
350         /* Don’t handle events inside the titlebar, only borders are interesting */
351         i3Font *font = load_font(conn, config.font);
352         if (event->event_y >= 2 && event->event_y <= (font->height + 2 + 2)) {
353                 DLOG("click on titlebar\n");
354
355                 /* Floating clients can be dragged by grabbing their titlebar */
356                 if (client_is_floating(client)) {
357                         /* Firstly, we raise it. Maybe the user just wanted to raise it without grabbing */
358                         xcb_raise_window(conn, client->frame);
359                         xcb_flush(conn);
360
361                         floating_drag_window(conn, client, event);
362                 }
363                 return 1;
364         }
365
366         if (client_is_floating(client))
367                 return floating_border_click(conn, client, event);
368
369         Workspace *ws = con->workspace;
370
371         if (event->event_y < 2) {
372                 /* This was a press on the top border */
373                 if (con->row == 0)
374                         return 1;
375                 first = con->row - 1;
376                 second = con->row;
377                 orientation = O_HORIZONTAL;
378         } else if (event->event_y >= (client->rect.height - 2)) {
379                 /* …bottom border */
380                 first = con->row + (con->rowspan - 1);
381                 if (!cell_exists(ws, con->col, first) ||
382                     (first == (ws->rows-1)))
383                         return 1;
384
385                 second = first + 1;
386                 orientation = O_HORIZONTAL;
387         } else if (event->event_x <= 2) {
388                 /* …left border */
389                 if (con->col == 0)
390                         return 1;
391
392                 first = con->col - 1;
393                 second = con->col;
394         } else if (event->event_x > 2) {
395                 /* …right border */
396                 first = con->col + (con->colspan - 1);
397                 DLOG("column %d\n", first);
398
399                 if (!cell_exists(ws, first, con->row) ||
400                     (first == (ws->cols-1)))
401                         return 1;
402
403                 second = first + 1;
404         }
405
406         return resize_graphical_handler(conn, ws, first, second, orientation, event);
407 }