]> git.sur5r.net Git - i3/i3/blob - src/handlers.c
Use default cursor (XC_left_ptr) for all windows
[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, NULL, helpwin, "#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         ignore_notify_event = event->sequence;
342         manage_window(prophs, conn, event->window, wa);
343         return 1;
344 }
345
346 /*
347  * Configuration notifies are only handled because we need to set up ignore for the following
348  * enter notify events
349  *
350  */
351 int handle_configure_event(void *prophs, xcb_connection_t *conn, xcb_configure_notify_event_t *event) {
352         xcb_window_t root = xcb_setup_roots_iterator(xcb_get_setup(conn)).data->root;
353
354         printf("handle_configure_event\n");
355         printf("event->x = %d, ->y = %d, ->width = %d, ->height = %d\n", event->x, event->y, event->width, event->height);
356         if (event->event == root) {
357                 printf("reconfigure of the root window, need to xinerama\n");
358                 xinerama_requery_screens(conn);
359                 return 1;
360         }
361
362         ignore_notify_event = event->sequence;
363
364         Client *client = table_get(byChild, event->window);
365         if (client == NULL) {
366                 printf("client not managed, ignoring\n");
367                 return 1;
368         }
369
370         if (client->fullscreen) {
371                 printf("client in fullscreen, not touching\n");
372                 return 1;
373         }
374
375         /* Let’s see if the application has changed size/position on its own *sigh*… */
376         if ((event->x != client->child_rect.x) ||
377             (event->y != client->child_rect.y) ||
378             (event->width != client->child_rect.width) ||
379             (event->height != client->child_rect.height)) {
380                 /* Who is your window manager? Who’s that, huh? I AM YOUR WINDOW MANAGER! */
381                 printf("Application wanted to resize itself. Fixed that.\n");
382                 client->force_reconfigure = true;
383                 render_container(conn, client->container);
384                 xcb_flush(conn);
385         }
386
387         return 1;
388 }
389
390 /*
391  * Our window decorations were unmapped. That means, the window will be killed now,
392  * so we better clean up before.
393  *
394  */
395 int handle_unmap_notify_event(void *data, xcb_connection_t *c, xcb_unmap_notify_event_t *e) {
396         xcb_window_t root = xcb_setup_roots_iterator(xcb_get_setup(c)).data->root;
397
398         ignore_notify_event = e->sequence;
399
400         Client *client = table_get(byChild, e->window);
401         /* First, we need to check if the client is awaiting an unmap-request which
402            was generated by us reparenting the window. In that case, we just ignore it. */
403         if (client != NULL && client->awaiting_useless_unmap) {
404                 printf("Dropping this unmap request, it was generated by reparenting\n");
405                 client->awaiting_useless_unmap = false;
406                 return 1;
407         }
408         client = table_remove(byChild, e->window);
409
410         printf("UnmapNotify for 0x%08x (received from 0x%08x): ", e->window, e->event);
411         if (client == NULL) {
412                 printf("not a managed window. Ignoring.\n");
413                 return 0;
414         }
415
416         if (client->container != NULL) {
417                 Client *to_focus = NULL;
418                 Container *con = client->container;
419
420                 /* If the client which is being unmapped was the currently active, we need to find
421                    the next possible window to focus in this container */
422                 if (con->currently_focused == client) {
423                         to_focus = CIRCLEQ_NEXT_OR_NULL(&(con->clients), client, clients);
424                         if (to_focus == NULL)
425                                 to_focus = CIRCLEQ_PREV_OR_NULL(&(con->clients), client, clients);
426
427                         /* Set focus in data structure to the next/previous window, if any (else NULL) */
428                         con->currently_focused = to_focus;
429                 }
430
431                 /* If this was the fullscreen client, we need to unset it */
432                 if (client->fullscreen)
433                         con->workspace->fullscreen_client = NULL;
434
435                 /* If the container will be empty now and is in stacking mode, we need to
436                    correctly resize the stack_win */
437                 if (con->currently_focused == NULL && con->mode == MODE_STACK) {
438                         struct Stack_Window *stack_win = &(con->stack_win);
439                         stack_win->height = 0;
440                         xcb_unmap_window(c, stack_win->window);
441                 }
442
443                 /* Remove the client from the list of clients */
444                 CIRCLEQ_REMOVE(&(con->clients), client, clients);
445
446                 /* Actually set focus, if there is a window which should get it */
447                 if (to_focus != NULL)
448                         set_focus(c, to_focus);
449         }
450
451         printf("child of 0x%08x.\n", client->frame);
452         xcb_reparent_window(c, client->child, root, 0, 0);
453         xcb_destroy_window(c, client->frame);
454         xcb_flush(c);
455         table_remove(byParent, client->frame);
456
457         cleanup_table(c, client->container->workspace);
458
459         free(client);
460
461         render_layout(c);
462
463         return 1;
464 }
465
466 /*
467  * Called when a window changes its title
468  *
469  */
470 int handle_windowname_change(void *data, xcb_connection_t *conn, uint8_t state,
471                                 xcb_window_t window, xcb_atom_t atom, xcb_get_property_reply_t *prop) {
472         printf("window's name changed.\n");
473         Client *client = table_get(byChild, window);
474         if (client == NULL)
475                 return 1;
476
477         client->name_len = xcb_get_property_value_length(prop);
478         client->name = malloc(client->name_len);
479         strncpy(client->name, xcb_get_property_value(prop), client->name_len);
480         printf("rename to \"%.*s\".\n", client->name_len, client->name);
481
482         if (client->container->mode == MODE_STACK)
483                 render_container(conn, client->container);
484         else decorate_window(conn, client, client->frame, client->titlegc, 0);
485         xcb_flush(conn);
486
487         return 1;
488 }
489
490 /*
491  * Expose event means we should redraw our windows (= title bar)
492  *
493  */
494 int handle_expose_event(void *data, xcb_connection_t *conn, xcb_expose_event_t *e) {
495         printf("got expose_event\n");
496         /* e->count is the number of minimum remaining expose events for this window, so we
497            skip all events but the last one */
498         if (e->count != 0)
499                 return 1;
500
501         Client *client = table_get(byParent, e->window);
502         if (client == NULL) {
503                 /* There was no client in the table, so this is probably an expose event for
504                    one of our stack_windows. */
505                 struct Stack_Window *stack_win;
506                 SLIST_FOREACH(stack_win, &stack_wins, stack_windows)
507                         if (stack_win->window == e->window) {
508                                 render_container(conn, stack_win->container);
509                                 return 1;
510                         }
511                 return 1;
512         }
513
514         printf("handle_expose_event()\n");
515         if (client->container->mode != MODE_STACK)
516                 decorate_window(conn, client, client->frame, client->titlegc, 0);
517         else {
518                 xcb_change_gc_single(conn, client->titlegc, XCB_GC_FOREGROUND,
519                         get_colorpixel(conn, client, client->frame, "#285577"));
520
521                 xcb_rectangle_t rect = {0, 0, client->rect.width, client->rect.height};
522                 xcb_poly_fill_rectangle(conn, client->frame, client->titlegc, 1, &rect);
523
524                 xcb_flush(conn);
525         }
526         return 1;
527 }
528
529 /*
530  * Handle client messages (EWMH)
531  *
532  */
533 int handle_client_message(void *data, xcb_connection_t *conn, xcb_client_message_event_t *event) {
534         printf("client_message\n");
535
536         if (event->type == atoms[_NET_WM_STATE]) {
537                 if (event->format != 32 || event->data.data32[1] != atoms[_NET_WM_STATE_FULLSCREEN])
538                         return 0;
539
540                 printf("fullscreen\n");
541
542                 Client *client = table_get(byChild, event->window);
543                 if (client == NULL)
544                         return 0;
545
546                 /* Check if the fullscreen state should be toggled */
547                 if ((client->fullscreen &&
548                      (event->data.data32[0] == _NET_WM_STATE_REMOVE ||
549                       event->data.data32[0] == _NET_WM_STATE_TOGGLE)) ||
550                     (!client->fullscreen &&
551                      (event->data.data32[0] == _NET_WM_STATE_ADD ||
552                       event->data.data32[0] == _NET_WM_STATE_TOGGLE)))
553                         toggle_fullscreen(conn, client);
554         } else {
555                 printf("unhandled clientmessage\n");
556                 return 0;
557         }
558
559         return 1;
560 }
561
562 int window_type_handler(void *data, xcb_connection_t *conn, uint8_t state, xcb_window_t window,
563                         xcb_atom_t atom, xcb_get_property_reply_t *property) {
564         /* TODO: Implement this one. To do this, implement a little test program which sleep(1)s
565          before changing this property. */
566         printf("_NET_WM_WINDOW_TYPE changed, this is not yet implemented.\n");
567         return 0;
568 }