]> git.sur5r.net Git - i3/i3/blob - src/handlers.c
Merge font.c into xcb.c
[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 "xcb.h"
27 #include "util.h"
28 #include "xinerama.h"
29 #include "config.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                 /* The client was neither on a client’s titlebar nor on a client itself, maybe on a stack_window? */
140                 struct Stack_Window *stack_win;
141                 SLIST_FOREACH(stack_win, &stack_wins, stack_windows)
142                         if (stack_win->window == event->event) {
143                                 /* A stack window was clicked. We calculate the destination client by
144                                    dividing the Y position of the event through the height of a window
145                                    decoration and then set the focus to this client. */
146                                 i3Font *font = load_font(conn, config.font);
147                                 int decoration_height = (font->height + 2 + 2);
148                                 int destination = (event->event_y / decoration_height),
149                                     c = 0;
150                                 Client *client;
151
152                                 printf("Click on stack_win for client %d\n", destination);
153                                 CIRCLEQ_FOREACH(client, &(stack_win->container->clients), clients)
154                                         if (c++ == destination) {
155                                                 set_focus(conn, client);
156                                                 return 1;
157                                         }
158
159                                 return 1;
160                         }
161
162                 return 1;
163         }
164
165         xcb_window_t root = xcb_setup_roots_iterator(xcb_get_setup(conn)).data->root;
166         xcb_screen_t *root_screen = xcb_setup_roots_iterator(xcb_get_setup(conn)).data;
167
168         /* Set focus in any case */
169         set_focus(conn, client);
170
171         /* Let’s see if this was on the borders (= resize). If not, we’re done */
172         printf("press button on x=%d, y=%d\n", event->event_x, event->event_y);
173
174         Container *con = client->container,
175                   *first = NULL,
176                   *second = NULL;
177         enum { O_HORIZONTAL, O_VERTICAL } orientation = O_VERTICAL;
178
179         if (con == NULL) {
180                 printf("dock. done.\n");
181                 xcb_allow_events(conn, XCB_ALLOW_REPLAY_POINTER, event->time);
182                 xcb_flush(conn);
183                 return 1;
184         }
185
186         printf("event->event_x = %d, client->rect.width = %d\n", event->event_x, client->rect.width);
187
188         if (!border_click) {
189                 printf("client. done.\n");
190                 xcb_allow_events(conn, XCB_ALLOW_REPLAY_POINTER, event->time);
191                 xcb_flush(conn);
192                 return 1;
193         }
194
195         if (event->event_y < 2) {
196                 /* This was a press on the top border */
197                 if (con->row == 0)
198                         return 1;
199                 first = con->workspace->table[con->col][con->row-1];
200                 second = con;
201                 orientation = O_HORIZONTAL;
202         } else if (event->event_y >= (client->rect.height - 2)) {
203                 /* …bottom border */
204                 if (con->row == (con->workspace->rows-1))
205                         return 1;
206                 first = con;
207                 second = con->workspace->table[con->col][con->row+1];
208                 orientation = O_HORIZONTAL;
209         } else if (event->event_x < 2) {
210                 /* …left border */
211                 if (con->col == 0)
212                         return 1;
213                 first = con->workspace->table[con->col-1][con->row];
214                 second = con;
215         } else if (event->event_x > 2) {
216                 /* …right border */
217                 if (con->col == (con->workspace->cols-1))
218                         return 1;
219                 first = con;
220                 second = con->workspace->table[con->col+1][con->row];
221         }
222
223         /* Open a new window, the resizebar. Grab the pointer and move the window around
224            as the user moves the pointer. */
225         Rect grabrect = {0, 0, root_screen->width_in_pixels, root_screen->height_in_pixels};
226         xcb_window_t grabwin = create_window(conn, grabrect, XCB_WINDOW_CLASS_INPUT_ONLY, 0, NULL);
227
228         Rect helprect;
229         if (orientation == O_VERTICAL) {
230                 helprect.x = event->root_x;
231                 helprect.y = 0;
232                 helprect.width = 2;
233                 helprect.height = root_screen->height_in_pixels; /* this has to be the cell’s height */
234         } else {
235                 helprect.x = 0;
236                 helprect.y = event->root_y;
237                 helprect.width = root_screen->width_in_pixels; /* this has to be the cell’s width*/
238                 helprect.height = 2;
239         }
240         xcb_window_t helpwin = create_window(conn, helprect, XCB_WINDOW_CLASS_INPUT_OUTPUT, 0, NULL);
241
242         uint32_t values[1] = {get_colorpixel(conn, NULL, helpwin, "#4c7899")};
243         xcb_void_cookie_t cookie = xcb_change_window_attributes_checked(conn, helpwin, XCB_CW_BACK_PIXEL, values);
244         check_error(conn, cookie, "Could not change window attributes (background color)");
245
246         xcb_circulate_window(conn, XCB_CIRCULATE_RAISE_LOWEST, helpwin);
247
248         xcb_grab_pointer(conn, false, root, XCB_EVENT_MASK_BUTTON_RELEASE | XCB_EVENT_MASK_POINTER_MOTION,
249                         XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC, grabwin, XCB_NONE, XCB_CURRENT_TIME);
250
251         xcb_flush(conn);
252
253         xcb_generic_event_t *inside_event;
254         /* I’ve always wanted to have my own eventhandler… */
255         while ((inside_event = xcb_wait_for_event(conn))) {
256                 /* Same as get_event_handler in xcb */
257                 int nr = inside_event->response_type;
258                 if (nr == 0) {
259                         /* An error occured */
260                         handle_event(NULL, conn, inside_event);
261                         free(inside_event);
262                         continue;
263                 }
264                 assert(nr < 256);
265                 nr &= XCB_EVENT_RESPONSE_TYPE_MASK;
266                 assert(nr >= 2);
267
268                 /* Check if we need to escape this loop */
269                 if (nr == XCB_BUTTON_RELEASE)
270                         break;
271
272                 switch (nr) {
273                         case XCB_MOTION_NOTIFY:
274                                 if (orientation == O_VERTICAL) {
275                                         values[0] = ((xcb_motion_notify_event_t*)inside_event)->root_x;
276                                         xcb_configure_window(conn, helpwin, XCB_CONFIG_WINDOW_X, values);
277                                 } else {
278                                         values[0] = ((xcb_motion_notify_event_t*)inside_event)->root_y;
279                                         xcb_configure_window(conn, helpwin, XCB_CONFIG_WINDOW_Y, values);
280                                 }
281
282                                 xcb_flush(conn);
283                                 break;
284                         case XCB_EXPOSE:
285                                 /* Use original handler */
286                                 xcb_event_handle(&evenths, inside_event);
287                                 break;
288                         default:
289                                 printf("Ignoring event of type %d\n", nr);
290                                 break;
291                 }
292                 free(inside_event);
293         }
294
295         xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
296         xcb_destroy_window(conn, helpwin);
297         xcb_destroy_window(conn, grabwin);
298         xcb_flush(conn);
299
300         Workspace *ws = con->workspace;
301         if (orientation == O_VERTICAL) {
302                 printf("Resize was from X = %d to X = %d\n", event->root_x, values[0]);
303
304                 /* Convert 0 (for default width_factor) to actual numbers */
305                 if (first->width_factor == 0)
306                         first->width_factor = ((float)ws->rect.width / ws->cols) / ws->rect.width;
307                 if (second->width_factor == 0)
308                         second->width_factor = ((float)ws->rect.width / ws->cols) / ws->rect.width;
309
310                 first->width_factor *= (float)(first->width + (values[0] - event->root_x)) / first->width;
311                 second->width_factor *= (float)(second->width - (values[0] - event->root_x)) / second->width;
312         } else {
313                 printf("Resize was from Y = %d to Y = %d\n", event->root_y, values[0]);
314
315                 /* Convert 0 (for default height_factor) to actual numbers */
316                 if (first->height_factor == 0)
317                         first->height_factor = ((float)ws->rect.height / ws->rows) / ws->rect.height;
318                 if (second->height_factor == 0)
319                         second->height_factor = ((float)ws->rect.height / ws->rows) / ws->rect.height;
320
321                 first->height_factor *= (float)(first->height + (values[0] - event->root_y)) / first->height;
322                 second->height_factor *= (float)(second->height - (values[0] - event->root_y)) / second->height;
323         }
324
325         render_layout(conn);
326
327         return 1;
328 }
329
330 /*
331  * A new window appeared on the screen (=was mapped), so let’s manage it.
332  *
333  */
334 int handle_map_notify_event(void *prophs, xcb_connection_t *conn, xcb_map_notify_event_t *event) {
335         window_attributes_t wa = { TAG_VALUE };
336         wa.u.override_redirect = event->override_redirect;
337         printf("MapNotify for 0x%08x, serial is %d.\n", event->window, event->sequence);
338         ignore_notify_event = event->sequence;
339         manage_window(prophs, conn, event->window, wa);
340         return 1;
341 }
342
343 /*
344  * Configuration notifies are only handled because we need to set up ignore for the following
345  * enter notify events
346  *
347  */
348 int handle_configure_event(void *prophs, xcb_connection_t *conn, xcb_configure_notify_event_t *event) {
349         xcb_window_t root = xcb_setup_roots_iterator(xcb_get_setup(conn)).data->root;
350
351         printf("handle_configure_event\n");
352         printf("event->x = %d, ->y = %d, ->width = %d, ->height = %d\n", event->x, event->y, event->width, event->height);
353         if (event->event == root) {
354                 printf("reconfigure of the root window, need to xinerama\n");
355                 xinerama_requery_screens(conn);
356                 return 1;
357         }
358
359         ignore_notify_event = event->sequence;
360
361         Client *client = table_get(byChild, event->window);
362         if (client == NULL) {
363                 printf("client not managed, ignoring\n");
364                 return 1;
365         }
366
367         if (client->fullscreen) {
368                 printf("client in fullscreen, not touching\n");
369                 return 1;
370         }
371
372         /* Let’s see if the application has changed size/position on its own *sigh*… */
373         if ((event->x != client->child_rect.x) ||
374             (event->y != client->child_rect.y) ||
375             (event->width != client->child_rect.width) ||
376             (event->height != client->child_rect.height)) {
377                 /* Who is your window manager? Who’s that, huh? I AM YOUR WINDOW MANAGER! */
378                 printf("Application wanted to resize itself. Fixed that.\n");
379                 client->force_reconfigure = true;
380                 render_container(conn, client->container);
381                 xcb_flush(conn);
382         }
383
384         return 1;
385 }
386
387 /*
388  * Our window decorations were unmapped. That means, the window will be killed now,
389  * so we better clean up before.
390  *
391  */
392 int handle_unmap_notify_event(void *data, xcb_connection_t *c, xcb_unmap_notify_event_t *e) {
393         xcb_window_t root = xcb_setup_roots_iterator(xcb_get_setup(c)).data->root;
394
395         ignore_notify_event = e->sequence;
396
397         Client *client = table_get(byChild, e->window);
398         /* First, we need to check if the client is awaiting an unmap-request which
399            was generated by us reparenting the window. In that case, we just ignore it. */
400         if (client != NULL && client->awaiting_useless_unmap) {
401                 printf("Dropping this unmap request, it was generated by reparenting\n");
402                 client->awaiting_useless_unmap = false;
403                 return 1;
404         }
405         client = table_remove(byChild, e->window);
406
407         printf("UnmapNotify for 0x%08x (received from 0x%08x): ", e->window, e->event);
408         if (client == NULL) {
409                 printf("not a managed window. Ignoring.\n");
410                 return 0;
411         }
412
413         if (client->container != NULL) {
414                 Client *to_focus = NULL;
415                 Container *con = client->container;
416
417                 /* If the client which is being unmapped was the currently active, we need to find
418                    the next possible window to focus in this container */
419                 if (con->currently_focused == client) {
420                         to_focus = CIRCLEQ_NEXT_OR_NULL(&(con->clients), client, clients);
421                         if (to_focus == NULL)
422                                 to_focus = CIRCLEQ_PREV_OR_NULL(&(con->clients), client, clients);
423
424                         /* Set focus in data structure to the next/previous window, if any (else NULL) */
425                         con->currently_focused = to_focus;
426                 }
427
428                 /* If this was the fullscreen client, we need to unset it */
429                 if (client->fullscreen)
430                         con->workspace->fullscreen_client = NULL;
431
432                 /* If the container will be empty now and is in stacking mode, we need to
433                    correctly resize the stack_win */
434                 if (con->currently_focused == NULL && con->mode == MODE_STACK) {
435                         struct Stack_Window *stack_win = &(con->stack_win);
436                         stack_win->height = 0;
437                         xcb_unmap_window(c, stack_win->window);
438                 }
439
440                 /* Remove the client from the list of clients */
441                 CIRCLEQ_REMOVE(&(con->clients), client, clients);
442
443                 /* Actually set focus, if there is a window which should get it */
444                 if (to_focus != NULL)
445                         set_focus(c, to_focus);
446         }
447
448         printf("child of 0x%08x.\n", client->frame);
449         xcb_reparent_window(c, client->child, root, 0, 0);
450         xcb_destroy_window(c, client->frame);
451         xcb_flush(c);
452         table_remove(byParent, client->frame);
453
454         cleanup_table(c, client->container->workspace);
455
456         free(client);
457
458         render_layout(c);
459
460         return 1;
461 }
462
463 /*
464  * Called when a window changes its title
465  *
466  */
467 int handle_windowname_change(void *data, xcb_connection_t *conn, uint8_t state,
468                                 xcb_window_t window, xcb_atom_t atom, xcb_get_property_reply_t *prop) {
469         printf("window's name changed.\n");
470         Client *client = table_get(byChild, window);
471         if (client == NULL)
472                 return 1;
473
474         client->name_len = xcb_get_property_value_length(prop);
475         client->name = malloc(client->name_len);
476         strncpy(client->name, xcb_get_property_value(prop), client->name_len);
477         printf("rename to \"%.*s\".\n", client->name_len, client->name);
478
479         if (client->container->mode == MODE_STACK)
480                 render_container(conn, client->container);
481         else decorate_window(conn, client, client->frame, client->titlegc, 0);
482         xcb_flush(conn);
483
484         return 1;
485 }
486
487 /*
488  * Expose event means we should redraw our windows (= title bar)
489  *
490  */
491 int handle_expose_event(void *data, xcb_connection_t *conn, xcb_expose_event_t *e) {
492         printf("got expose_event\n");
493         /* e->count is the number of minimum remaining expose events for this window, so we
494            skip all events but the last one */
495         if (e->count != 0)
496                 return 1;
497
498         Client *client = table_get(byParent, e->window);
499         if (client == NULL) {
500                 /* There was no client in the table, so this is probably an expose event for
501                    one of our stack_windows. */
502                 struct Stack_Window *stack_win;
503                 SLIST_FOREACH(stack_win, &stack_wins, stack_windows)
504                         if (stack_win->window == e->window) {
505                                 render_container(conn, stack_win->container);
506                                 return 1;
507                         }
508                 return 1;
509         }
510
511         printf("handle_expose_event()\n");
512         if (client->container->mode != MODE_STACK)
513                 decorate_window(conn, client, client->frame, client->titlegc, 0);
514         else {
515                 xcb_change_gc_single(conn, client->titlegc, XCB_GC_FOREGROUND,
516                         get_colorpixel(conn, client, client->frame, "#285577"));
517
518                 xcb_rectangle_t rect = {0, 0, client->rect.width, client->rect.height};
519                 xcb_poly_fill_rectangle(conn, client->frame, client->titlegc, 1, &rect);
520
521                 xcb_flush(conn);
522         }
523         return 1;
524 }
525
526 /*
527  * Handle client messages (EWMH)
528  *
529  */
530 int handle_client_message(void *data, xcb_connection_t *conn, xcb_client_message_event_t *event) {
531         printf("client_message\n");
532
533         if (event->type == atoms[_NET_WM_STATE]) {
534                 if (event->format != 32 || event->data.data32[1] != atoms[_NET_WM_STATE_FULLSCREEN])
535                         return 0;
536
537                 printf("fullscreen\n");
538
539                 Client *client = table_get(byChild, event->window);
540                 if (client == NULL)
541                         return 0;
542
543                 /* Check if the fullscreen state should be toggled */
544                 if ((client->fullscreen &&
545                      (event->data.data32[0] == _NET_WM_STATE_REMOVE ||
546                       event->data.data32[0] == _NET_WM_STATE_TOGGLE)) ||
547                     (!client->fullscreen &&
548                      (event->data.data32[0] == _NET_WM_STATE_ADD ||
549                       event->data.data32[0] == _NET_WM_STATE_TOGGLE)))
550                         toggle_fullscreen(conn, client);
551         } else {
552                 printf("unhandled clientmessage\n");
553                 return 0;
554         }
555
556         return 1;
557 }
558
559 int window_type_handler(void *data, xcb_connection_t *conn, uint8_t state, xcb_window_t window,
560                         xcb_atom_t atom, xcb_get_property_reply_t *property) {
561         /* TODO: Implement this one. To do this, implement a little test program which sleep(1)s
562          before changing this property. */
563         printf("_NET_WM_WINDOW_TYPE changed, this is not yet implemented.\n");
564         return 0;
565 }