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