]> git.sur5r.net Git - i3/i3/blob - src/handlers.c
Implement clicking on titlebars in stack windows to focus
[i3/i3] / src / handlers.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  */
11 #include <stdio.h>
12 #include <assert.h>
13 #include <string.h>
14 #include <stdlib.h>
15 #include <xcb/xcb.h>
16
17 #include <xcb/xcb_wm.h>
18 #include <X11/XKBlib.h>
19
20 #include "i3.h"
21 #include "debug.h"
22 #include "table.h"
23 #include "layout.h"
24 #include "commands.h"
25 #include "data.h"
26 #include "font.h"
27 #include "xcb.h"
28 #include "util.h"
29 #include "xinerama.h"
30 #include "config.h"
31
32 /* After mapping/unmapping windows, a notify event is generated. However, we don’t want it,
33    since it’d trigger an infinite loop of switching between the different windows when
34    changing workspaces */
35 int ignore_notify_event = -1;
36
37 /*
38  * Due to bindings like Mode_switch + <a>, we need to bind some keys in XCB_GRAB_MODE_SYNC.
39  * Therefore, we just replay all key presses.
40  *
41  */
42 int handle_key_release(void *ignored, xcb_connection_t *conn, xcb_key_release_event_t *event) {
43         printf("got key release, just passing\n");
44         xcb_allow_events(conn, XCB_ALLOW_REPLAY_KEYBOARD, event->time);
45         xcb_flush(conn);
46         return 1;
47 }
48
49 /*
50  * There was a key press. We compare this key code with our bindings table and pass
51  * the bound action to parse_command().
52  *
53  */
54 int handle_key_press(void *ignored, xcb_connection_t *conn, xcb_key_press_event_t *event) {
55         printf("Keypress %d\n", event->detail);
56
57         /* We need to get the keysym group (There are group 1 to group 4, each holding
58            two keysyms (without shift and with shift) using Xkb because X fails to
59            provide them reliably (it works in Xephyr, it does not in real X) */
60         XkbStateRec state;
61         if (XkbGetState(xkbdpy, XkbUseCoreKbd, &state) == Success && (state.group+1) == 2)
62                 event->state |= 0x2;
63
64         printf("state %d\n", event->state);
65
66         /* Remove the numlock bit, all other bits are modifiers we can bind to */
67         uint16_t state_filtered = event->state & ~XCB_MOD_MASK_LOCK;
68
69         /* Find the binding */
70         Binding *bind;
71         TAILQ_FOREACH(bind, &bindings, bindings)
72                 if (bind->keycode == event->detail && bind->mods == state_filtered)
73                         break;
74
75         /* No match? Then it was an actively grabbed key, that is with Mode_switch, and
76            the user did not press Mode_switch, so just pass it… */
77         if (bind == TAILQ_END(&bindings)) {
78                 xcb_allow_events(conn, ReplayKeyboard, event->time);
79                 xcb_flush(conn);
80                 return 1;
81         }
82
83         parse_command(conn, bind->command);
84         if (event->state & 0x2) {
85                 printf("Mode_switch -> allow_events(SyncKeyboard)\n");
86                 xcb_allow_events(conn, SyncKeyboard, event->time);
87                 xcb_flush(conn);
88         }
89         return 1;
90 }
91
92
93 /*
94  * When the user moves the mouse pointer onto a window, this callback gets called.
95  *
96  */
97 int handle_enter_notify(void *ignored, xcb_connection_t *conn, xcb_enter_notify_event_t *event) {
98         printf("enter_notify for %08x, serial %d\n", event->event, event->sequence);
99         /* Some events are not interesting, because they were not generated actively by the
100            user, but be reconfiguration of windows */
101         if (event->sequence == ignore_notify_event) {
102                 printf("Ignoring, because of previous map\n");
103                 return 1;
104         }
105
106         /* This was either a focus for a client’s parent (= titlebar)… */
107         Client *client = table_get(byParent, event->event);
108         /* …or the client itself */
109         if (client == NULL)
110                 client = table_get(byChild, event->event);
111
112         /* If not, then the user moved his cursor to the root window. In that case, we adjust c_ws */
113         if (client == NULL) {
114                 printf("Getting screen at %d x %d\n", event->root_x, event->root_y);
115                 i3Screen *screen = get_screen_containing(event->root_x, event->root_y);
116                 if (screen == NULL) {
117                         printf("ERROR: No such screen\n");
118                         return 0;
119                 }
120                 c_ws = &workspaces[screen->current_workspace];
121                 printf("We're now on virtual screen number %d\n", screen->num);
122                 return 1;
123         }
124
125         set_focus(conn, client);
126
127         return 1;
128 }
129
130 int handle_button_press(void *ignored, xcb_connection_t *conn, xcb_button_press_event_t *event) {
131         printf("button press!\n");
132         /* This was either a focus for a client’s parent (= titlebar)… */
133         Client *client = table_get(byChild, event->event);
134         bool border_click = false;
135         if (client == NULL) {
136                 client = table_get(byParent, event->event);
137                 border_click = true;
138         }
139         if (client == NULL) {
140                 /* The client was neither on a client’s titlebar nor on a client itself, maybe on a stack_window? */
141                 struct Stack_Window *stack_win;
142                 SLIST_FOREACH(stack_win, &stack_wins, stack_windows)
143                         if (stack_win->window == event->event) {
144                                 /* A stack window was clicked. We calculate the destination client by
145                                    dividing the Y position of the event through the height of a window
146                                    decoration and then set the focus to this client. */
147                                 i3Font *font = load_font(conn, config.font);
148                                 int decoration_height = (font->height + 2 + 2);
149                                 int destination = (event->event_y / decoration_height),
150                                     c = 0;
151                                 Client *client;
152
153                                 printf("Click on stack_win for client %d\n", destination);
154                                 CIRCLEQ_FOREACH(client, &(stack_win->container->clients), clients)
155                                         if (c++ == destination) {
156                                                 set_focus(conn, client);
157                                                 return 1;
158                                         }
159
160                                 return 1;
161                         }
162
163                 return 1;
164         }
165
166         xcb_window_t root = xcb_setup_roots_iterator(xcb_get_setup(conn)).data->root;
167         xcb_screen_t *root_screen = xcb_setup_roots_iterator(xcb_get_setup(conn)).data;
168
169         /* Set focus in any case */
170         set_focus(conn, client);
171
172         /* Let’s see if this was on the borders (= resize). If not, we’re done */
173         printf("press button on x=%d, y=%d\n", event->event_x, event->event_y);
174
175         Container *con = client->container,
176                   *first = NULL,
177                   *second = NULL;
178         enum { O_HORIZONTAL, O_VERTICAL } orientation = O_VERTICAL;
179
180         if (con == NULL) {
181                 printf("dock. done.\n");
182                 xcb_allow_events(conn, XCB_ALLOW_REPLAY_POINTER, event->time);
183                 xcb_flush(conn);
184                 return 1;
185         }
186
187         printf("event->event_x = %d, client->rect.width = %d\n", event->event_x, client->rect.width);
188
189         if (!border_click) {
190                 printf("client. done.\n");
191                 xcb_allow_events(conn, XCB_ALLOW_REPLAY_POINTER, event->time);
192                 xcb_flush(conn);
193                 return 1;
194         }
195
196         if (event->event_y < 2) {
197                 /* This was a press on the top border */
198                 if (con->row == 0)
199                         return 1;
200                 first = con->workspace->table[con->col][con->row-1];
201                 second = con;
202                 orientation = O_HORIZONTAL;
203         } else if (event->event_y >= (client->rect.height - 2)) {
204                 /* …bottom border */
205                 if (con->row == (con->workspace->rows-1))
206                         return 1;
207                 first = con;
208                 second = con->workspace->table[con->col][con->row+1];
209                 orientation = O_HORIZONTAL;
210         } else if (event->event_x < 2) {
211                 /* …left border */
212                 if (con->col == 0)
213                         return 1;
214                 first = con->workspace->table[con->col-1][con->row];
215                 second = con;
216         } else if (event->event_x > 2) {
217                 /* …right border */
218                 if (con->col == (con->workspace->cols-1))
219                         return 1;
220                 first = con;
221                 second = con->workspace->table[con->col+1][con->row];
222         }
223
224         /* Open a new window, the resizebar. Grab the pointer and move the window around
225            as the user moves the pointer. */
226         Rect grabrect = {0, 0, root_screen->width_in_pixels, root_screen->height_in_pixels};
227         xcb_window_t grabwin = create_window(conn, grabrect, XCB_WINDOW_CLASS_INPUT_ONLY, 0, NULL);
228
229         Rect helprect;
230         if (orientation == O_VERTICAL) {
231                 helprect.x = event->root_x;
232                 helprect.y = 0;
233                 helprect.width = 2;
234                 helprect.height = root_screen->height_in_pixels; /* this has to be the cell’s height */
235         } else {
236                 helprect.x = 0;
237                 helprect.y = event->root_y;
238                 helprect.width = root_screen->width_in_pixels; /* this has to be the cell’s width*/
239                 helprect.height = 2;
240         }
241         xcb_window_t helpwin = create_window(conn, helprect, XCB_WINDOW_CLASS_INPUT_OUTPUT, 0, NULL);
242
243         uint32_t values[1] = {get_colorpixel(conn, NULL, helpwin, "#4c7899")};
244         xcb_void_cookie_t cookie = xcb_change_window_attributes_checked(conn, helpwin, XCB_CW_BACK_PIXEL, values);
245         check_error(conn, cookie, "Could not change window attributes (background color)");
246
247         xcb_circulate_window(conn, XCB_CIRCULATE_RAISE_LOWEST, helpwin);
248
249         xcb_grab_pointer(conn, false, root, XCB_EVENT_MASK_BUTTON_RELEASE | XCB_EVENT_MASK_POINTER_MOTION,
250                         XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC, grabwin, XCB_NONE, XCB_CURRENT_TIME);
251
252         xcb_flush(conn);
253
254         xcb_generic_event_t *inside_event;
255         /* I’ve always wanted to have my own eventhandler… */
256         while ((inside_event = xcb_wait_for_event(conn))) {
257                 /* Same as get_event_handler in xcb */
258                 int nr = inside_event->response_type;
259                 if (nr == 0) {
260                         /* An error occured */
261                         handle_event(NULL, conn, inside_event);
262                         free(inside_event);
263                         continue;
264                 }
265                 assert(nr < 256);
266                 nr &= XCB_EVENT_RESPONSE_TYPE_MASK;
267                 assert(nr >= 2);
268
269                 /* Check if we need to escape this loop */
270                 if (nr == XCB_BUTTON_RELEASE)
271                         break;
272
273                 switch (nr) {
274                         case XCB_MOTION_NOTIFY:
275                                 if (orientation == O_VERTICAL) {
276                                         values[0] = ((xcb_motion_notify_event_t*)inside_event)->root_x;
277                                         xcb_configure_window(conn, helpwin, XCB_CONFIG_WINDOW_X, values);
278                                 } else {
279                                         values[0] = ((xcb_motion_notify_event_t*)inside_event)->root_y;
280                                         xcb_configure_window(conn, helpwin, XCB_CONFIG_WINDOW_Y, values);
281                                 }
282
283                                 xcb_flush(conn);
284                                 break;
285                         case XCB_EXPOSE:
286                                 /* Use original handler */
287                                 xcb_event_handle(&evenths, inside_event);
288                                 break;
289                         default:
290                                 printf("Ignoring event of type %d\n", nr);
291                                 break;
292                 }
293                 free(inside_event);
294         }
295
296         xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
297         xcb_destroy_window(conn, helpwin);
298         xcb_destroy_window(conn, grabwin);
299         xcb_flush(conn);
300
301         Workspace *ws = con->workspace;
302         if (orientation == O_VERTICAL) {
303                 printf("Resize was from X = %d to X = %d\n", event->root_x, values[0]);
304
305                 /* Convert 0 (for default width_factor) to actual numbers */
306                 if (first->width_factor == 0)
307                         first->width_factor = ((float)ws->rect.width / ws->cols) / ws->rect.width;
308                 if (second->width_factor == 0)
309                         second->width_factor = ((float)ws->rect.width / ws->cols) / ws->rect.width;
310
311                 first->width_factor *= (float)(first->width + (values[0] - event->root_x)) / first->width;
312                 second->width_factor *= (float)(second->width - (values[0] - event->root_x)) / second->width;
313         } else {
314                 printf("Resize was from Y = %d to Y = %d\n", event->root_y, values[0]);
315
316                 /* Convert 0 (for default height_factor) to actual numbers */
317                 if (first->height_factor == 0)
318                         first->height_factor = ((float)ws->rect.height / ws->rows) / ws->rect.height;
319                 if (second->height_factor == 0)
320                         second->height_factor = ((float)ws->rect.height / ws->rows) / ws->rect.height;
321
322                 first->height_factor *= (float)(first->height + (values[0] - event->root_y)) / first->height;
323                 second->height_factor *= (float)(second->height - (values[0] - event->root_y)) / second->height;
324         }
325
326         render_layout(conn);
327
328         return 1;
329 }
330
331 /*
332  * A new window appeared on the screen (=was mapped), so let’s manage it.
333  *
334  */
335 int handle_map_notify_event(void *prophs, xcb_connection_t *conn, xcb_map_notify_event_t *event) {
336         window_attributes_t wa = { TAG_VALUE };
337         wa.u.override_redirect = event->override_redirect;
338         printf("MapNotify for 0x%08x, serial is %d.\n", event->window, event->sequence);
339         ignore_notify_event = event->sequence;
340         manage_window(prophs, conn, event->window, wa);
341         return 1;
342 }
343
344 /*
345  * Configuration notifies are only handled because we need to set up ignore for the following
346  * enter notify events
347  *
348  */
349 int handle_configure_event(void *prophs, xcb_connection_t *conn, xcb_configure_notify_event_t *event) {
350         xcb_window_t root = xcb_setup_roots_iterator(xcb_get_setup(conn)).data->root;
351
352         printf("handle_configure_event\n");
353         printf("event->x = %d, ->y = %d, ->width = %d, ->height = %d\n", event->x, event->y, event->width, event->height);
354         if (event->event == root) {
355                 printf("reconfigure of the root window, need to xinerama\n");
356                 xinerama_requery_screens(conn);
357                 return 1;
358         }
359
360         ignore_notify_event = event->sequence;
361
362         Client *client = table_get(byChild, event->window);
363         if (client == NULL) {
364                 printf("client not managed, ignoring\n");
365                 return 1;
366         }
367
368         if (client->fullscreen) {
369                 printf("client in fullscreen, not touching\n");
370                 return 1;
371         }
372
373         /* Let’s see if the application has changed size/position on its own *sigh*… */
374         if ((event->x != client->child_rect.x) ||
375             (event->y != client->child_rect.y) ||
376             (event->width != client->child_rect.width) ||
377             (event->height != client->child_rect.height)) {
378                 /* Who is your window manager? Who’s that, huh? I AM YOUR WINDOW MANAGER! */
379                 printf("Application wanted to resize itself. Fixed that.\n");
380                 client->force_reconfigure = true;
381                 render_container(conn, client->container);
382                 xcb_flush(conn);
383         }
384
385         return 1;
386 }
387
388 /*
389  * Our window decorations were unmapped. That means, the window will be killed now,
390  * so we better clean up before.
391  *
392  */
393 int handle_unmap_notify_event(void *data, xcb_connection_t *c, xcb_unmap_notify_event_t *e) {
394         xcb_window_t root = xcb_setup_roots_iterator(xcb_get_setup(c)).data->root;
395
396         ignore_notify_event = e->sequence;
397
398         Client *client = table_get(byChild, e->window);
399         /* First, we need to check if the client is awaiting an unmap-request which
400            was generated by us reparenting the window. In that case, we just ignore it. */
401         if (client != NULL && client->awaiting_useless_unmap) {
402                 printf("Dropping this unmap request, it was generated by reparenting\n");
403                 client->awaiting_useless_unmap = false;
404                 return 1;
405         }
406         client = table_remove(byChild, e->window);
407
408         printf("UnmapNotify for 0x%08x (received from 0x%08x): ", e->window, e->event);
409         if (client == NULL) {
410                 printf("not a managed window. Ignoring.\n");
411                 return 0;
412         }
413
414         if (client->container != NULL) {
415                 Client *to_focus = NULL;
416                 Container *con = client->container;
417
418                 /* If the client which is being unmapped was the currently active, we need to find
419                    the next possible window to focus in this container */
420                 if (con->currently_focused == client) {
421                         to_focus = CIRCLEQ_NEXT_OR_NULL(&(con->clients), client, clients);
422                         if (to_focus == NULL)
423                                 to_focus = CIRCLEQ_PREV_OR_NULL(&(con->clients), client, clients);
424
425                         /* Set focus in data structure to the next/previous window, if any (else NULL) */
426                         con->currently_focused = to_focus;
427                 }
428
429                 /* If this was the fullscreen client, we need to unset it */
430                 if (client->fullscreen)
431                         con->workspace->fullscreen_client = NULL;
432
433                 /* If the container will be empty now and is in stacking mode, we need to
434                    correctly resize the stack_win */
435                 if (con->currently_focused == NULL && con->mode == MODE_STACK) {
436                         struct Stack_Window *stack_win = &(con->stack_win);
437                         stack_win->height = 0;
438                         xcb_unmap_window(c, stack_win->window);
439                 }
440
441                 /* Remove the client from the list of clients */
442                 CIRCLEQ_REMOVE(&(con->clients), client, clients);
443
444                 /* Actually set focus, if there is a window which should get it */
445                 if (to_focus != NULL)
446                         set_focus(c, to_focus);
447         }
448
449         printf("child of 0x%08x.\n", client->frame);
450         xcb_reparent_window(c, client->child, root, 0, 0);
451         xcb_destroy_window(c, client->frame);
452         xcb_flush(c);
453         table_remove(byParent, client->frame);
454
455         cleanup_table(c, client->container->workspace);
456
457         free(client);
458
459         render_layout(c);
460
461         return 1;
462 }
463
464 /*
465  * Called when a window changes its title
466  *
467  */
468 int handle_windowname_change(void *data, xcb_connection_t *conn, uint8_t state,
469                                 xcb_window_t window, xcb_atom_t atom, xcb_get_property_reply_t *prop) {
470         printf("window's name changed.\n");
471         Client *client = table_get(byChild, window);
472         if (client == NULL)
473                 return 1;
474
475         client->name_len = xcb_get_property_value_length(prop);
476         client->name = malloc(client->name_len);
477         strncpy(client->name, xcb_get_property_value(prop), client->name_len);
478         printf("rename to \"%.*s\".\n", client->name_len, client->name);
479
480         if (client->container->mode == MODE_STACK)
481                 render_container(conn, client->container);
482         else decorate_window(conn, client, client->frame, client->titlegc, 0);
483         xcb_flush(conn);
484
485         return 1;
486 }
487
488 /*
489  * Expose event means we should redraw our windows (= title bar)
490  *
491  */
492 int handle_expose_event(void *data, xcb_connection_t *conn, xcb_expose_event_t *e) {
493         printf("got expose_event\n");
494         /* e->count is the number of minimum remaining expose events for this window, so we
495            skip all events but the last one */
496         if (e->count != 0)
497                 return 1;
498
499         Client *client = table_get(byParent, e->window);
500         if (client == NULL) {
501                 /* There was no client in the table, so this is probably an expose event for
502                    one of our stack_windows. */
503                 struct Stack_Window *stack_win;
504                 SLIST_FOREACH(stack_win, &stack_wins, stack_windows)
505                         if (stack_win->window == e->window) {
506                                 render_container(conn, stack_win->container);
507                                 return 1;
508                         }
509                 return 1;
510         }
511
512         printf("handle_expose_event()\n");
513         if (client->container->mode != MODE_STACK)
514                 decorate_window(conn, client, client->frame, client->titlegc, 0);
515         else {
516                 xcb_change_gc_single(conn, client->titlegc, XCB_GC_FOREGROUND,
517                         get_colorpixel(conn, client, client->frame, "#285577"));
518
519                 xcb_rectangle_t rect = {0, 0, client->rect.width, client->rect.height};
520                 xcb_poly_fill_rectangle(conn, client->frame, client->titlegc, 1, &rect);
521
522                 xcb_flush(conn);
523         }
524         return 1;
525 }
526
527 /*
528  * Handle client messages (EWMH)
529  *
530  */
531 int handle_client_message(void *data, xcb_connection_t *conn, xcb_client_message_event_t *event) {
532         printf("client_message\n");
533
534         if (event->type == atoms[_NET_WM_STATE]) {
535                 if (event->format != 32 || event->data.data32[1] != atoms[_NET_WM_STATE_FULLSCREEN])
536                         return 0;
537
538                 printf("fullscreen\n");
539
540                 Client *client = table_get(byChild, event->window);
541                 if (client == NULL)
542                         return 0;
543
544                 /* Check if the fullscreen state should be toggled */
545                 if ((client->fullscreen &&
546                      (event->data.data32[0] == _NET_WM_STATE_REMOVE ||
547                       event->data.data32[0] == _NET_WM_STATE_TOGGLE)) ||
548                     (!client->fullscreen &&
549                      (event->data.data32[0] == _NET_WM_STATE_ADD ||
550                       event->data.data32[0] == _NET_WM_STATE_TOGGLE)))
551                         toggle_fullscreen(conn, client);
552         } else {
553                 printf("unhandled clientmessage\n");
554                 return 0;
555         }
556
557         return 1;
558 }
559
560 int window_type_handler(void *data, xcb_connection_t *conn, uint8_t state, xcb_window_t window,
561                         xcb_atom_t atom, xcb_get_property_reply_t *property) {
562         /* TODO: Implement this one. To do this, implement a little test program which sleep(1)s
563          before changing this property. */
564         printf("_NET_WM_WINDOW_TYPE changed, this is not yet implemented.\n");
565         return 0;
566 }