]> git.sur5r.net Git - i3/i3/blob - src/handlers.c
Bugfix: Use memcmp() instead of strcmp(), use new_len * 2 to check all bytes
[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 <xcb/xcb_icccm.h>
19 #include <X11/XKBlib.h>
20
21 #include "i3.h"
22 #include "debug.h"
23 #include "table.h"
24 #include "layout.h"
25 #include "commands.h"
26 #include "data.h"
27 #include "xcb.h"
28 #include "util.h"
29 #include "xinerama.h"
30 #include "config.h"
31
32 /* After mapping/unmapping windows, a notify event is generated. However, we don’t want it,
33    since it’d trigger an infinite loop of switching between the different windows when
34    changing workspaces */
35 int ignore_notify_event = -1;
36
37 /*
38  * Due to bindings like Mode_switch + <a>, we need to bind some keys in XCB_GRAB_MODE_SYNC.
39  * Therefore, we just replay all key presses.
40  *
41  */
42 int handle_key_release(void *ignored, xcb_connection_t *conn, xcb_key_release_event_t *event) {
43         LOG("got key release, just passing\n");
44         xcb_allow_events(conn, XCB_ALLOW_REPLAY_KEYBOARD, event->time);
45         xcb_flush(conn);
46         return 1;
47 }
48
49 /*
50  * There was a key press. We compare this key code with our bindings table and pass
51  * the bound action to parse_command().
52  *
53  */
54 int handle_key_press(void *ignored, xcb_connection_t *conn, xcb_key_press_event_t *event) {
55         LOG("Keypress %d\n", event->detail);
56
57         /* We need to get the keysym group (There are group 1 to group 4, each holding
58            two keysyms (without shift and with shift) using Xkb because X fails to
59            provide them reliably (it works in Xephyr, it does not in real X) */
60         XkbStateRec state;
61         if (XkbGetState(xkbdpy, XkbUseCoreKbd, &state) == Success && (state.group+1) == 2)
62                 event->state |= 0x2;
63
64         LOG("state %d\n", event->state);
65
66         /* Remove the numlock bit, all other bits are modifiers we can bind to */
67         uint16_t state_filtered = event->state & ~XCB_MOD_MASK_LOCK;
68
69         /* Find the binding */
70         Binding *bind;
71         TAILQ_FOREACH(bind, &bindings, bindings)
72                 if (bind->keycode == event->detail && bind->mods == state_filtered)
73                         break;
74
75         /* No match? Then it was an actively grabbed key, that is with Mode_switch, and
76            the user did not press Mode_switch, so just pass it… */
77         if (bind == TAILQ_END(&bindings)) {
78                 xcb_allow_events(conn, ReplayKeyboard, event->time);
79                 xcb_flush(conn);
80                 return 1;
81         }
82
83         parse_command(conn, bind->command);
84         if (event->state & 0x2) {
85                 LOG("Mode_switch -> allow_events(SyncKeyboard)\n");
86                 xcb_allow_events(conn, SyncKeyboard, event->time);
87                 xcb_flush(conn);
88         }
89         return 1;
90 }
91
92
93 /*
94  * When the user moves the mouse pointer onto a window, this callback gets called.
95  *
96  */
97 int handle_enter_notify(void *ignored, xcb_connection_t *conn, xcb_enter_notify_event_t *event) {
98         LOG("enter_notify for %08x, serial %d\n", event->event, event->sequence);
99         /* Some events are not interesting, because they were not generated actively by the
100            user, but be reconfiguration of windows */
101         if (event->sequence == ignore_notify_event) {
102                 LOG("Ignoring, because of previous map\n");
103                 return 1;
104         }
105
106         /* This was either a focus for a client’s parent (= titlebar)… */
107         Client *client = table_get(byParent, event->event);
108         /* …or the client itself */
109         if (client == NULL)
110                 client = table_get(byChild, event->event);
111
112         /* If not, then the user moved his cursor to the root window. In that case, we adjust c_ws */
113         if (client == NULL) {
114                 LOG("Getting screen at %d x %d\n", event->root_x, event->root_y);
115                 i3Screen *screen = get_screen_containing(event->root_x, event->root_y);
116                 if (screen == NULL) {
117                         LOG("ERROR: No such screen\n");
118                         return 0;
119                 }
120                 c_ws = &workspaces[screen->current_workspace];
121                 LOG("We're now on virtual screen number %d\n", screen->num);
122                 return 1;
123         }
124
125         set_focus(conn, client);
126
127         return 1;
128 }
129
130 int handle_button_press(void *ignored, xcb_connection_t *conn, xcb_button_press_event_t *event) {
131         LOG("button press!\n");
132         /* This was either a focus for a client’s parent (= titlebar)… */
133         Client *client = table_get(byChild, event->event);
134         bool border_click = false;
135         if (client == NULL) {
136                 client = table_get(byParent, event->event);
137                 border_click = true;
138         }
139         if (client == NULL) {
140                 /* The client was neither on a client’s titlebar nor on a client itself, maybe on a stack_window? */
141                 struct Stack_Window *stack_win;
142                 SLIST_FOREACH(stack_win, &stack_wins, stack_windows)
143                         if (stack_win->window == event->event) {
144                                 /* A stack window was clicked. We calculate the destination client by
145                                    dividing the Y position of the event through the height of a window
146                                    decoration and then set the focus to this client. */
147                                 i3Font *font = load_font(conn, config.font);
148                                 int decoration_height = (font->height + 2 + 2);
149                                 int destination = (event->event_y / decoration_height),
150                                     c = 0;
151                                 Client *client;
152
153                                 LOG("Click on stack_win for client %d\n", destination);
154                                 CIRCLEQ_FOREACH(client, &(stack_win->container->clients), clients)
155                                         if (c++ == destination) {
156                                                 set_focus(conn, client);
157                                                 return 1;
158                                         }
159
160                                 return 1;
161                         }
162
163                 return 1;
164         }
165
166         xcb_window_t root = xcb_setup_roots_iterator(xcb_get_setup(conn)).data->root;
167         xcb_screen_t *root_screen = xcb_setup_roots_iterator(xcb_get_setup(conn)).data;
168
169         /* Set focus in any case */
170         set_focus(conn, client);
171
172         /* Let’s see if this was on the borders (= resize). If not, we’re done */
173         LOG("press button on x=%d, y=%d\n", event->event_x, event->event_y);
174
175         Container *con = client->container,
176                   *first = NULL,
177                   *second = NULL;
178         enum { O_HORIZONTAL, O_VERTICAL } orientation = O_VERTICAL;
179         int new_position;
180
181         if (con == NULL) {
182                 LOG("dock. done.\n");
183                 xcb_allow_events(conn, XCB_ALLOW_REPLAY_POINTER, event->time);
184                 xcb_flush(conn);
185                 return 1;
186         }
187
188         LOG("event->event_x = %d, client->rect.width = %d\n", event->event_x, client->rect.width);
189
190         if (!border_click) {
191                 LOG("client. done.\n");
192                 xcb_allow_events(conn, XCB_ALLOW_REPLAY_POINTER, event->time);
193                 xcb_flush(conn);
194                 return 1;
195         }
196
197         if (event->event_y < 2) {
198                 /* This was a press on the top border */
199                 if (con->row == 0)
200                         return 1;
201                 first = con->workspace->table[con->col][con->row-1];
202                 second = con;
203                 orientation = O_HORIZONTAL;
204         } else if (event->event_y >= (client->rect.height - 2)) {
205                 /* …bottom border */
206                 if (con->row == (con->workspace->rows-1))
207                         return 1;
208                 first = con;
209                 second = con->workspace->table[con->col][con->row+1];
210                 orientation = O_HORIZONTAL;
211         } else if (event->event_x <= 2) {
212                 /* …left border */
213                 if (con->col == 0)
214                         return 1;
215                 first = con->workspace->table[con->col-1][con->row];
216                 second = con;
217         } else if (event->event_x > 2) {
218                 /* …right border */
219                 if (con->col == (con->workspace->cols-1))
220                         return 1;
221                 first = con;
222                 second = con->workspace->table[con->col+1][con->row];
223         }
224
225         /* Open a new window, the resizebar. Grab the pointer and move the window around
226            as the user moves the pointer. */
227         Rect grabrect = {0, 0, root_screen->width_in_pixels, root_screen->height_in_pixels};
228         xcb_window_t grabwin = create_window(conn, grabrect, XCB_WINDOW_CLASS_INPUT_ONLY, -1, 0, NULL);
229
230         Rect helprect;
231         if (orientation == O_VERTICAL) {
232                 helprect.x = event->root_x;
233                 helprect.y = 0;
234                 helprect.width = 2;
235                 helprect.height = root_screen->height_in_pixels; /* this has to be the cell’s height */
236                 new_position = event->root_x;
237         } else {
238                 helprect.x = 0;
239                 helprect.y = event->root_y;
240                 helprect.width = root_screen->width_in_pixels; /* this has to be the cell’s width */
241                 helprect.height = 2;
242                 new_position = event->root_y;
243         }
244         xcb_window_t helpwin = create_window(conn, helprect, XCB_WINDOW_CLASS_INPUT_OUTPUT,
245                                              (orientation == O_VERTICAL ?
246                                               XCB_CURSOR_SB_V_DOUBLE_ARROW :
247                                               XCB_CURSOR_SB_H_DOUBLE_ARROW), 0, NULL);
248
249         uint32_t values[1] = {get_colorpixel(conn, "#4c7899")};
250         xcb_void_cookie_t cookie = xcb_change_window_attributes_checked(conn, helpwin, XCB_CW_BACK_PIXEL, values);
251         check_error(conn, cookie, "Could not change window attributes (background color)");
252
253         xcb_circulate_window(conn, XCB_CIRCULATE_RAISE_LOWEST, helpwin);
254
255         xcb_grab_pointer(conn, false, root, XCB_EVENT_MASK_BUTTON_RELEASE | XCB_EVENT_MASK_POINTER_MOTION,
256                         XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC, grabwin, XCB_NONE, XCB_CURRENT_TIME);
257
258         xcb_flush(conn);
259
260         xcb_generic_event_t *inside_event;
261         /* I’ve always wanted to have my own eventhandler… */
262         while ((inside_event = xcb_wait_for_event(conn))) {
263                 /* Same as get_event_handler in xcb */
264                 int nr = inside_event->response_type;
265                 if (nr == 0) {
266                         /* An error occured */
267                         handle_event(NULL, conn, inside_event);
268                         free(inside_event);
269                         continue;
270                 }
271                 assert(nr < 256);
272                 nr &= XCB_EVENT_RESPONSE_TYPE_MASK;
273                 assert(nr >= 2);
274
275                 /* Check if we need to escape this loop */
276                 if (nr == XCB_BUTTON_RELEASE)
277                         break;
278
279                 switch (nr) {
280                         case XCB_MOTION_NOTIFY:
281                                 if (orientation == O_VERTICAL) {
282                                         values[0] = new_position = ((xcb_motion_notify_event_t*)inside_event)->root_x;
283                                         xcb_configure_window(conn, helpwin, XCB_CONFIG_WINDOW_X, values);
284                                 } else {
285                                         values[0] = new_position = ((xcb_motion_notify_event_t*)inside_event)->root_y;
286                                         xcb_configure_window(conn, helpwin, XCB_CONFIG_WINDOW_Y, values);
287                                 }
288
289                                 xcb_flush(conn);
290                                 break;
291                         case XCB_EXPOSE:
292                                 /* Use original handler */
293                                 xcb_event_handle(&evenths, inside_event);
294                                 break;
295                         default:
296                                 LOG("Ignoring event of type %d\n", nr);
297                                 break;
298                 }
299                 free(inside_event);
300         }
301
302         xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
303         xcb_destroy_window(conn, helpwin);
304         xcb_destroy_window(conn, grabwin);
305         xcb_flush(conn);
306
307         Workspace *ws = con->workspace;
308         if (orientation == O_VERTICAL) {
309                 LOG("Resize was from X = %d to X = %d\n", event->root_x, new_position);
310                 if (event->root_x == new_position) {
311                         LOG("Nothing changed, not updating anything\n");
312                         return 1;
313                 }
314
315                 /* Convert 0 (for default width_factor) to actual numbers */
316                 if (first->width_factor == 0)
317                         first->width_factor = ((float)ws->rect.width / ws->cols) / ws->rect.width;
318                 if (second->width_factor == 0)
319                         second->width_factor = ((float)ws->rect.width / ws->cols) / ws->rect.width;
320
321                 first->width_factor *= (float)(first->width + (new_position - event->root_x)) / first->width;
322                 second->width_factor *= (float)(second->width - (new_position - event->root_x)) / second->width;
323         } else {
324                 LOG("Resize was from Y = %d to Y = %d\n", event->root_y, new_position);
325                 if (event->root_y == new_position) {
326                         LOG("Nothing changed, not updating anything\n");
327                         return 1;
328                 }
329
330                 /* Convert 0 (for default height_factor) to actual numbers */
331                 if (first->height_factor == 0)
332                         first->height_factor = ((float)ws->rect.height / ws->rows) / ws->rect.height;
333                 if (second->height_factor == 0)
334                         second->height_factor = ((float)ws->rect.height / ws->rows) / ws->rect.height;
335
336                 first->height_factor *= (float)(first->height + (new_position - event->root_y)) / first->height;
337                 second->height_factor *= (float)(second->height - (new_position - event->root_y)) / second->height;
338         }
339
340         render_layout(conn);
341
342         return 1;
343 }
344
345 /*
346  * A new window appeared on the screen (=was mapped), so let’s manage it.
347  *
348  */
349 int handle_map_notify_event(void *prophs, xcb_connection_t *conn, xcb_map_notify_event_t *event) {
350         window_attributes_t wa = { TAG_VALUE };
351         wa.u.override_redirect = event->override_redirect;
352         LOG("MapNotify for 0x%08x, serial is %d.\n", event->window, event->sequence);
353         LOG("setting ignore_notify_event = %d\n", event->sequence);
354         ignore_notify_event = event->sequence;
355         manage_window(prophs, conn, event->window, wa);
356         return 1;
357 }
358
359 /*
360  * Configuration notifies are only handled because we need to set up ignore for the following
361  * enter notify events
362  *
363  */
364 int handle_configure_event(void *prophs, xcb_connection_t *conn, xcb_configure_notify_event_t *event) {
365         xcb_window_t root = xcb_setup_roots_iterator(xcb_get_setup(conn)).data->root;
366
367         LOG("handle_configure_event\n");
368         LOG("event->type = %d, \n", event->response_type);
369         LOG("event->x = %d, ->y = %d, ->width = %d, ->height = %d\n", event->x, event->y, event->width, event->height);
370         LOG("sequence = %d\n", event->sequence);
371
372         ignore_notify_event = event->sequence;
373
374         if (event->event == root) {
375                 LOG("reconfigure of the root window, need to xinerama\n");
376                 /* FIXME: Somehow, this is occuring too often. Therefore, we check for 0/0,
377                    but is there a better way? */
378                 if (event->x == 0 && event->y == 0)
379                         xinerama_requery_screens(conn);
380                 return 1;
381         }
382
383         Client *client = table_get(byChild, event->window);
384         if (client == NULL) {
385                 LOG("client not managed, ignoring\n");
386                 return 1;
387         }
388
389         if (client->fullscreen) {
390                 LOG("client in fullscreen, not touching\n");
391                 return 1;
392         }
393
394         /* Let’s see if the application has changed size/position on its own *sigh*… */
395         if ((event->x != client->child_rect.x) ||
396             (event->y != client->child_rect.y) ||
397             (event->width != client->child_rect.width) ||
398             (event->height != client->child_rect.height)) {
399                 /* Who is your window manager? Who’s that, huh? I AM YOUR WINDOW MANAGER! */
400                 LOG("Application wanted to resize itself. Fixed that.\n");
401                 client->force_reconfigure = true;
402                 render_container(conn, client->container);
403                 xcb_flush(conn);
404         }
405
406         return 1;
407 }
408
409 /*
410  * Our window decorations were unmapped. That means, the window will be killed now,
411  * so we better clean up before.
412  *
413  */
414 int handle_unmap_notify_event(void *data, xcb_connection_t *conn, xcb_unmap_notify_event_t *event) {
415         xcb_window_t root = xcb_setup_roots_iterator(xcb_get_setup(conn)).data->root;
416
417         LOG("setting ignore_notify_event = %d\n", event->sequence);
418
419         ignore_notify_event = event->sequence;
420
421         Client *client = table_get(byChild, event->window);
422         /* First, we need to check if the client is awaiting an unmap-request which
423            was generated by us reparenting the window. In that case, we just ignore it. */
424         if (client != NULL && client->awaiting_useless_unmap) {
425                 LOG("Dropping this unmap request, it was generated by reparenting\n");
426                 client->awaiting_useless_unmap = false;
427                 return 1;
428         }
429         client = table_remove(byChild, event->window);
430
431         LOG("UnmapNotify for 0x%08x (received from 0x%08x): ", event->window, event->event);
432         if (client == NULL) {
433                 LOG("not a managed window. Ignoring.\n");
434                 return 0;
435         }
436
437         if (client->name != NULL)
438                 free(client->name);
439
440         if (client->container != NULL) {
441                 Container *con = client->container;
442
443                 /* If this was the fullscreen client, we need to unset it */
444                 if (client->fullscreen)
445                         con->workspace->fullscreen_client = NULL;
446
447                 /* Remove the client from the list of clients */
448                 remove_client_from_container(conn, client, con);
449
450                 /* Remove from the focus stack */
451                 LOG("Removing from focus stack\n");
452                 SLIST_REMOVE(&(con->workspace->focus_stack), client, Client, focus_clients);
453
454                 /* Set currently_focused to the next client which will get focus in this
455                    particular container. This does not necessarily correspond with the client
456                    that will be focused next */
457                 con->currently_focused = NULL;
458                 Client *focus_client;
459                 SLIST_FOREACH(focus_client, &(con->workspace->focus_stack), focus_clients)
460                         if (focus_client->container == con) {
461                                 con->currently_focused = focus_client;
462                                 set_focus(conn, focus_client);
463                                 break;
464                         }
465         }
466
467         LOG("child of 0x%08x.\n", client->frame);
468         xcb_reparent_window(conn, client->child, root, 0, 0);
469         xcb_destroy_window(conn, client->frame);
470         xcb_flush(conn);
471         table_remove(byParent, client->frame);
472
473         cleanup_table(conn, client->container->workspace);
474
475         free(client);
476
477         render_layout(conn);
478
479         return 1;
480 }
481
482 /*
483  * Called when a window changes its title
484  *
485  */
486 int handle_windowname_change(void *data, xcb_connection_t *conn, uint8_t state,
487                                 xcb_window_t window, xcb_atom_t atom, xcb_get_property_reply_t *prop) {
488         LOG("window's name changed.\n");
489         if (prop == NULL) {
490                 LOG("prop == NULL\n");
491                 return 1;
492         }
493         Client *client = table_get(byChild, window);
494         if (client == NULL)
495                 return 1;
496
497         /* Save the old pointer to make the update atomic */
498         char *new_name;
499         int new_len;
500         asprintf(&new_name, "%.*s", xcb_get_property_value_length(prop), (char*)xcb_get_property_value(prop));
501         /* Convert it to UCS-2 here for not having to convert it later every time we want to pass it to X */
502         char *ucs2_name = convert_utf8_to_ucs2(new_name, &new_len);
503         free(new_name);
504
505         /* Check if they are the same and don’t update if so.
506            Note the use of new_len * 2 to check all bytes as each glyph takes 2 bytes.
507            Also note the use of memcmp() instead of strncmp() because the latter stops on nullbytes,
508            but UCS-2 uses nullbytes to fill up glyphs which only use one byte. */
509         if ((new_len == client->name_len) &&
510             (client->name != NULL) &&
511             (memcmp(client->name, ucs2_name, new_len * 2) == 0)) {
512                 LOG("Name did not change, not updating\n");
513                 free(ucs2_name);
514                 return;
515         }
516
517         char *old_name = client->name;
518         client->name = ucs2_name;
519         client->name_len = new_len;
520
521         if (old_name != NULL)
522                 free(old_name);
523         LOG("rename to \"%s\".\n", client->name);
524
525         if (client->container->mode == MODE_STACK)
526                 render_container(conn, client->container);
527         else decorate_window(conn, client, client->frame, client->titlegc, 0);
528         xcb_flush(conn);
529
530         return 1;
531 }
532
533 /*
534  * Expose event means we should redraw our windows (= title bar)
535  *
536  */
537 int handle_expose_event(void *data, xcb_connection_t *conn, xcb_expose_event_t *event) {
538         /* event->count is the number of minimum remaining expose events for this window, so we
539            skip all events but the last one */
540         if (event->count != 0)
541                 return 1;
542         LOG("window = %08x\n", event->window);
543
544         Client *client = table_get(byParent, event->window);
545         if (client == NULL) {
546                 /* There was no client in the table, so this is probably an expose event for
547                    one of our stack_windows. */
548                 struct Stack_Window *stack_win;
549                 SLIST_FOREACH(stack_win, &stack_wins, stack_windows)
550                         if (stack_win->window == event->window) {
551                                 render_container(conn, stack_win->container);
552                                 return 1;
553                         }
554                 return 1;
555         }
556
557         LOG("got client %s\n", client->name);
558         if (client->container->mode != MODE_STACK)
559                 decorate_window(conn, client, client->frame, client->titlegc, 0);
560         else {
561                 uint32_t background_color;
562                 /* Distinguish if the window is currently focused… */
563                 if (CUR_CELL->currently_focused == client)
564                         background_color = get_colorpixel(conn, "#285577");
565                 /* …or if it is the focused window in a not focused container */
566                 else background_color = get_colorpixel(conn, "#555555");
567
568                 /* Set foreground color to current focused color, line width to 2 */
569                 uint32_t values[] = {background_color, 2};
570                 xcb_change_gc(conn, client->titlegc, XCB_GC_FOREGROUND | XCB_GC_LINE_WIDTH, values);
571
572                 /* Draw the border, the ±1 is for line width = 2 */
573                 xcb_point_t points[] = {{1, 0},                                           /* left upper edge */
574                                         {1, client->rect.height-1},                       /* left bottom edge */
575                                         {client->rect.width-1, client->rect.height-1},    /* right bottom edge */
576                                         {client->rect.width-1, 0}};                       /* right upper edge */
577                 xcb_poly_line(conn, XCB_COORD_MODE_ORIGIN, client->frame, client->titlegc, 4, points);
578
579                 /* Draw a black background */
580                 xcb_change_gc_single(conn, client->titlegc, XCB_GC_FOREGROUND, get_colorpixel(conn, "#000000"));
581                 xcb_rectangle_t crect = {2, 0, client->rect.width - (2 + 2), client->rect.height - 2};
582                 xcb_poly_fill_rectangle(conn, client->frame, client->titlegc, 1, &crect);
583         }
584         xcb_flush(conn);
585         return 1;
586 }
587
588 /*
589  * Handle client messages (EWMH)
590  *
591  */
592 int handle_client_message(void *data, xcb_connection_t *conn, xcb_client_message_event_t *event) {
593         LOG("client_message\n");
594
595         if (event->type == atoms[_NET_WM_STATE]) {
596                 if (event->format != 32 || event->data.data32[1] != atoms[_NET_WM_STATE_FULLSCREEN])
597                         return 0;
598
599                 LOG("fullscreen\n");
600
601                 Client *client = table_get(byChild, event->window);
602                 if (client == NULL)
603                         return 0;
604
605                 /* Check if the fullscreen state should be toggled */
606                 if ((client->fullscreen &&
607                      (event->data.data32[0] == _NET_WM_STATE_REMOVE ||
608                       event->data.data32[0] == _NET_WM_STATE_TOGGLE)) ||
609                     (!client->fullscreen &&
610                      (event->data.data32[0] == _NET_WM_STATE_ADD ||
611                       event->data.data32[0] == _NET_WM_STATE_TOGGLE)))
612                         toggle_fullscreen(conn, client);
613         } else {
614                 LOG("unhandled clientmessage\n");
615                 return 0;
616         }
617
618         return 1;
619 }
620
621 int window_type_handler(void *data, xcb_connection_t *conn, uint8_t state, xcb_window_t window,
622                         xcb_atom_t atom, xcb_get_property_reply_t *property) {
623         /* TODO: Implement this one. To do this, implement a little test program which sleep(1)s
624          before changing this property. */
625         LOG("_NET_WM_WINDOW_TYPE changed, this is not yet implemented.\n");
626         return 0;
627 }
628
629 /*
630  * Handles the size hints set by a window, but currently only the part necessary for displaying
631  * clients proportionally inside their frames (mplayer for example)
632  *
633  * See ICCCM 4.1.2.3 for more details
634  *
635  */
636 int handle_normal_hints(void *data, xcb_connection_t *conn, uint8_t state, xcb_window_t window,
637                         xcb_atom_t name, xcb_get_property_reply_t *reply) {
638         LOG("handle_normal_hints\n");
639         Client *client = table_get(byChild, window);
640         if (client == NULL) {
641                 LOG("No such client\n");
642                 return 1;
643         }
644         xcb_size_hints_t size_hints;
645
646         /* If the hints were already in this event, use them, if not, request them */
647         if (reply != NULL)
648                 xcb_get_wm_size_hints_from_reply(&size_hints, reply);
649         else
650                 xcb_get_wm_normal_hints_reply(conn, xcb_get_wm_normal_hints_unchecked(conn, client->child), &size_hints, NULL);
651
652         /* If no aspect ratio was set or if it was invalid, we ignore the hints */
653         if (!(size_hints.flags & XCB_SIZE_HINT_P_ASPECT) ||
654             (size_hints.min_aspect_num <= 0) ||
655             (size_hints.min_aspect_den <= 0)) {
656                 LOG("No aspect ratio set, ignoring\n");
657                 return 1;
658         }
659
660         LOG("window is %08x / %s\n", client->child, client->name);
661
662         int base_width = 0, base_height = 0,
663             min_width = 0, min_height = 0;
664
665         /* base_width/height are the desired size of the window.
666            We check if either the program-specified size or the program-specified
667            min-size is available */
668         if (size_hints.flags & XCB_SIZE_HINT_P_SIZE) {
669                 base_width = size_hints.base_width;
670                 base_height = size_hints.base_height;
671         } else if (size_hints.flags & XCB_SIZE_HINT_P_MIN_SIZE) {
672                 base_width = size_hints.min_width;
673                 base_height = size_hints.min_height;
674         }
675
676         if (size_hints.flags & XCB_SIZE_HINT_P_MIN_SIZE) {
677                 min_width = size_hints.min_width;
678                 min_height = size_hints.min_height;
679         } else if (size_hints.flags & XCB_SIZE_HINT_P_SIZE) {
680                 min_width = size_hints.base_width;
681                 min_height = size_hints.base_height;
682         }
683
684         double width = client->rect.width - base_width;
685         double height = client->rect.height - base_height;
686         /* Convert numerator/denominator to a double */
687         double min_aspect = (double)size_hints.min_aspect_num / size_hints.min_aspect_den;
688         double max_aspect = (double)size_hints.max_aspect_num / size_hints.min_aspect_den;
689
690         LOG("min_aspect = %f, max_aspect = %f\n", min_aspect, max_aspect);
691         LOG("width = %f, height = %f\n", width, height);
692
693         /* Sanity checks, this is user-input, in a way */
694         if (max_aspect <= 0 || min_aspect <= 0 || height == 0 || (width / height) <= 0)
695                 return 1;
696
697         /* Check if we need to set proportional_* variables using the correct ratio */
698         if ((width / height) < min_aspect) {
699                 client->proportional_width = width;
700                 client->proportional_height = width / min_aspect;
701         } else if ((width / height) > max_aspect) {
702                 client->proportional_width = width;
703                 client->proportional_height = width / max_aspect;
704         } else return 1;
705
706         client->force_reconfigure = true;
707
708         if (client->container != NULL) {
709                 render_container(conn, client->container);
710                 xcb_flush(conn);
711         }
712
713         return 1;
714 }