]> git.sur5r.net Git - i3/i3/blob - src/handlers.c
Make colorpixels independent from clients
[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, -1, 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,
241                                              (orientation == O_VERTICAL ?
242                                               XCB_CURSOR_SB_V_DOUBLE_ARROW :
243                                               XCB_CURSOR_SB_H_DOUBLE_ARROW), 0, NULL);
244
245         uint32_t values[1] = {get_colorpixel(conn, "#4c7899")};
246         xcb_void_cookie_t cookie = xcb_change_window_attributes_checked(conn, helpwin, XCB_CW_BACK_PIXEL, values);
247         check_error(conn, cookie, "Could not change window attributes (background color)");
248
249         xcb_circulate_window(conn, XCB_CIRCULATE_RAISE_LOWEST, helpwin);
250
251         xcb_grab_pointer(conn, false, root, XCB_EVENT_MASK_BUTTON_RELEASE | XCB_EVENT_MASK_POINTER_MOTION,
252                         XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC, grabwin, XCB_NONE, XCB_CURRENT_TIME);
253
254         xcb_flush(conn);
255
256         xcb_generic_event_t *inside_event;
257         /* I’ve always wanted to have my own eventhandler… */
258         while ((inside_event = xcb_wait_for_event(conn))) {
259                 /* Same as get_event_handler in xcb */
260                 int nr = inside_event->response_type;
261                 if (nr == 0) {
262                         /* An error occured */
263                         handle_event(NULL, conn, inside_event);
264                         free(inside_event);
265                         continue;
266                 }
267                 assert(nr < 256);
268                 nr &= XCB_EVENT_RESPONSE_TYPE_MASK;
269                 assert(nr >= 2);
270
271                 /* Check if we need to escape this loop */
272                 if (nr == XCB_BUTTON_RELEASE)
273                         break;
274
275                 switch (nr) {
276                         case XCB_MOTION_NOTIFY:
277                                 if (orientation == O_VERTICAL) {
278                                         values[0] = ((xcb_motion_notify_event_t*)inside_event)->root_x;
279                                         xcb_configure_window(conn, helpwin, XCB_CONFIG_WINDOW_X, values);
280                                 } else {
281                                         values[0] = ((xcb_motion_notify_event_t*)inside_event)->root_y;
282                                         xcb_configure_window(conn, helpwin, XCB_CONFIG_WINDOW_Y, values);
283                                 }
284
285                                 xcb_flush(conn);
286                                 break;
287                         case XCB_EXPOSE:
288                                 /* Use original handler */
289                                 xcb_event_handle(&evenths, inside_event);
290                                 break;
291                         default:
292                                 printf("Ignoring event of type %d\n", nr);
293                                 break;
294                 }
295                 free(inside_event);
296         }
297
298         xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
299         xcb_destroy_window(conn, helpwin);
300         xcb_destroy_window(conn, grabwin);
301         xcb_flush(conn);
302
303         Workspace *ws = con->workspace;
304         if (orientation == O_VERTICAL) {
305                 printf("Resize was from X = %d to X = %d\n", event->root_x, values[0]);
306
307                 /* Convert 0 (for default width_factor) to actual numbers */
308                 if (first->width_factor == 0)
309                         first->width_factor = ((float)ws->rect.width / ws->cols) / ws->rect.width;
310                 if (second->width_factor == 0)
311                         second->width_factor = ((float)ws->rect.width / ws->cols) / ws->rect.width;
312
313                 first->width_factor *= (float)(first->width + (values[0] - event->root_x)) / first->width;
314                 second->width_factor *= (float)(second->width - (values[0] - event->root_x)) / second->width;
315         } else {
316                 printf("Resize was from Y = %d to Y = %d\n", event->root_y, values[0]);
317
318                 /* Convert 0 (for default height_factor) to actual numbers */
319                 if (first->height_factor == 0)
320                         first->height_factor = ((float)ws->rect.height / ws->rows) / ws->rect.height;
321                 if (second->height_factor == 0)
322                         second->height_factor = ((float)ws->rect.height / ws->rows) / ws->rect.height;
323
324                 first->height_factor *= (float)(first->height + (values[0] - event->root_y)) / first->height;
325                 second->height_factor *= (float)(second->height - (values[0] - event->root_y)) / second->height;
326         }
327
328         render_layout(conn);
329
330         return 1;
331 }
332
333 /*
334  * A new window appeared on the screen (=was mapped), so let’s manage it.
335  *
336  */
337 int handle_map_notify_event(void *prophs, xcb_connection_t *conn, xcb_map_notify_event_t *event) {
338         window_attributes_t wa = { TAG_VALUE };
339         wa.u.override_redirect = event->override_redirect;
340         printf("MapNotify for 0x%08x, serial is %d.\n", event->window, event->sequence);
341         printf("setting ignore_notify_event = %d\n", event->sequence);
342         ignore_notify_event = event->sequence;
343         manage_window(prophs, conn, event->window, wa);
344         return 1;
345 }
346
347 /*
348  * Configuration notifies are only handled because we need to set up ignore for the following
349  * enter notify events
350  *
351  */
352 int handle_configure_event(void *prophs, xcb_connection_t *conn, xcb_configure_notify_event_t *event) {
353         xcb_window_t root = xcb_setup_roots_iterator(xcb_get_setup(conn)).data->root;
354
355         printf("handle_configure_event\n");
356         printf("event->x = %d, ->y = %d, ->width = %d, ->height = %d\n", event->x, event->y, event->width, event->height);
357         printf("sequence = %d\n", event->sequence);
358
359         ignore_notify_event = event->sequence;
360
361         if (event->event == root) {
362                 printf("reconfigure of the root window, need to xinerama\n");
363                 /* FIXME: Somehow, this is occuring too often. Therefore, we check for 0/0,
364                    but is there a better way? */
365                 if (event->x == 0 && event->y == 0)
366                         xinerama_requery_screens(conn);
367                 return 1;
368         }
369
370         Client *client = table_get(byChild, event->window);
371         if (client == NULL) {
372                 printf("client not managed, ignoring\n");
373                 return 1;
374         }
375
376         if (client->fullscreen) {
377                 printf("client in fullscreen, not touching\n");
378                 return 1;
379         }
380
381         /* Let’s see if the application has changed size/position on its own *sigh*… */
382         if ((event->x != client->child_rect.x) ||
383             (event->y != client->child_rect.y) ||
384             (event->width != client->child_rect.width) ||
385             (event->height != client->child_rect.height)) {
386                 /* Who is your window manager? Who’s that, huh? I AM YOUR WINDOW MANAGER! */
387                 printf("Application wanted to resize itself. Fixed that.\n");
388                 client->force_reconfigure = true;
389                 render_container(conn, client->container);
390                 xcb_flush(conn);
391         }
392
393         return 1;
394 }
395
396 /*
397  * Our window decorations were unmapped. That means, the window will be killed now,
398  * so we better clean up before.
399  *
400  */
401 int handle_unmap_notify_event(void *data, xcb_connection_t *conn, xcb_unmap_notify_event_t *event) {
402         xcb_window_t root = xcb_setup_roots_iterator(xcb_get_setup(conn)).data->root;
403
404         printf("setting ignore_notify_event = %d\n", event->sequence);
405
406         ignore_notify_event = event->sequence;
407
408         Client *client = table_get(byChild, event->window);
409         /* First, we need to check if the client is awaiting an unmap-request which
410            was generated by us reparenting the window. In that case, we just ignore it. */
411         if (client != NULL && client->awaiting_useless_unmap) {
412                 printf("Dropping this unmap request, it was generated by reparenting\n");
413                 client->awaiting_useless_unmap = false;
414                 return 1;
415         }
416         client = table_remove(byChild, event->window);
417
418         printf("UnmapNotify for 0x%08x (received from 0x%08x): ", event->window, event->event);
419         if (client == NULL) {
420                 printf("not a managed window. Ignoring.\n");
421                 return 0;
422         }
423
424         if (client->name != NULL)
425                 free(client->name);
426
427         if (client->container != NULL) {
428                 Container *con = client->container;
429
430                 /* If this was the fullscreen client, we need to unset it */
431                 if (client->fullscreen)
432                         con->workspace->fullscreen_client = NULL;
433
434                 /* If the container will be empty now and is in stacking mode, we need to
435                    correctly resize the stack_win */
436                 if (CIRCLEQ_EMPTY(&(con->clients)) && con->mode == MODE_STACK) {
437                         struct Stack_Window *stack_win = &(con->stack_win);
438                         stack_win->rect.height = 0;
439                         xcb_unmap_window(conn, stack_win->window);
440                 }
441
442                 /* Remove the client from the list of clients */
443                 CIRCLEQ_REMOVE(&(con->clients), client, clients);
444
445                 /* Remove from the focus stack */
446                 printf("Removing from focus stack\n");
447                 SLIST_REMOVE(&(con->workspace->focus_stack), client, Client, focus_clients);
448
449                 /* Remove from currently_focused */
450                 con->currently_focused = NULL;
451
452                 /* Actually set focus, if there is a window which should get it */
453                 if (!SLIST_EMPTY(&(con->workspace->focus_stack)))
454                         set_focus(conn, SLIST_FIRST(&(con->workspace->focus_stack)));
455         }
456
457         printf("child of 0x%08x.\n", client->frame);
458         xcb_reparent_window(conn, client->child, root, 0, 0);
459         xcb_destroy_window(conn, client->frame);
460         xcb_flush(conn);
461         table_remove(byParent, client->frame);
462
463         cleanup_table(conn, client->container->workspace);
464
465         free(client);
466
467         render_layout(conn);
468
469         return 1;
470 }
471
472 /*
473  * Called when a window changes its title
474  *
475  */
476 int handle_windowname_change(void *data, xcb_connection_t *conn, uint8_t state,
477                                 xcb_window_t window, xcb_atom_t atom, xcb_get_property_reply_t *prop) {
478         printf("window's name changed.\n");
479         Client *client = table_get(byChild, window);
480         if (client == NULL)
481                 return 1;
482
483         if (client->name != NULL)
484                 free(client->name);
485
486         client->name_len = xcb_get_property_value_length(prop);
487         client->name = smalloc(client->name_len);
488         strncpy(client->name, xcb_get_property_value(prop), client->name_len);
489         printf("rename to \"%.*s\".\n", client->name_len, client->name);
490
491         if (client->container->mode == MODE_STACK)
492                 render_container(conn, client->container);
493         else decorate_window(conn, client, client->frame, client->titlegc, 0);
494         xcb_flush(conn);
495
496         return 1;
497 }
498
499 /*
500  * Expose event means we should redraw our windows (= title bar)
501  *
502  */
503 int handle_expose_event(void *data, xcb_connection_t *conn, xcb_expose_event_t *event) {
504         printf("got expose_event\n");
505         /* event->count is the number of minimum remaining expose events for this window, so we
506            skip all events but the last one */
507         if (event->count != 0)
508                 return 1;
509
510         Client *client = table_get(byParent, event->window);
511         if (client == NULL) {
512                 /* There was no client in the table, so this is probably an expose event for
513                    one of our stack_windows. */
514                 struct Stack_Window *stack_win;
515                 SLIST_FOREACH(stack_win, &stack_wins, stack_windows)
516                         if (stack_win->window == event->window) {
517                                 render_container(conn, stack_win->container);
518                                 return 1;
519                         }
520                 return 1;
521         }
522
523         printf("handle_expose_event()\n");
524         if (client->container->mode != MODE_STACK)
525                 decorate_window(conn, client, client->frame, client->titlegc, 0);
526         else {
527                 xcb_change_gc_single(conn, client->titlegc, XCB_GC_FOREGROUND,
528                         get_colorpixel(conn, "#285577"));
529
530                 xcb_rectangle_t rect = {0, 0, client->rect.width, client->rect.height};
531                 xcb_poly_fill_rectangle(conn, client->frame, client->titlegc, 1, &rect);
532
533                 xcb_flush(conn);
534         }
535         return 1;
536 }
537
538 /*
539  * Handle client messages (EWMH)
540  *
541  */
542 int handle_client_message(void *data, xcb_connection_t *conn, xcb_client_message_event_t *event) {
543         printf("client_message\n");
544
545         if (event->type == atoms[_NET_WM_STATE]) {
546                 if (event->format != 32 || event->data.data32[1] != atoms[_NET_WM_STATE_FULLSCREEN])
547                         return 0;
548
549                 printf("fullscreen\n");
550
551                 Client *client = table_get(byChild, event->window);
552                 if (client == NULL)
553                         return 0;
554
555                 /* Check if the fullscreen state should be toggled */
556                 if ((client->fullscreen &&
557                      (event->data.data32[0] == _NET_WM_STATE_REMOVE ||
558                       event->data.data32[0] == _NET_WM_STATE_TOGGLE)) ||
559                     (!client->fullscreen &&
560                      (event->data.data32[0] == _NET_WM_STATE_ADD ||
561                       event->data.data32[0] == _NET_WM_STATE_TOGGLE)))
562                         toggle_fullscreen(conn, client);
563         } else {
564                 printf("unhandled clientmessage\n");
565                 return 0;
566         }
567
568         return 1;
569 }
570
571 int window_type_handler(void *data, xcb_connection_t *conn, uint8_t state, xcb_window_t window,
572                         xcb_atom_t atom, xcb_get_property_reply_t *property) {
573         /* TODO: Implement this one. To do this, implement a little test program which sleep(1)s
574          before changing this property. */
575         printf("_NET_WM_WINDOW_TYPE changed, this is not yet implemented.\n");
576         return 0;
577 }