]> git.sur5r.net Git - i3/i3/blob - src/handlers.c
Bugfix: Missing flush after render_container()
[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
180         if (con == NULL) {
181                 LOG("dock. done.\n");
182                 xcb_allow_events(conn, XCB_ALLOW_REPLAY_POINTER, event->time);
183                 xcb_flush(conn);
184                 return 1;
185         }
186
187         LOG("event->event_x = %d, client->rect.width = %d\n", event->event_x, client->rect.width);
188
189         if (!border_click) {
190                 LOG("client. done.\n");
191                 xcb_allow_events(conn, XCB_ALLOW_REPLAY_POINTER, event->time);
192                 xcb_flush(conn);
193                 return 1;
194         }
195
196         if (event->event_y < 2) {
197                 /* This was a press on the top border */
198                 if (con->row == 0)
199                         return 1;
200                 first = con->workspace->table[con->col][con->row-1];
201                 second = con;
202                 orientation = O_HORIZONTAL;
203         } else if (event->event_y >= (client->rect.height - 2)) {
204                 /* …bottom border */
205                 if (con->row == (con->workspace->rows-1))
206                         return 1;
207                 first = con;
208                 second = con->workspace->table[con->col][con->row+1];
209                 orientation = O_HORIZONTAL;
210         } else if (event->event_x <= 2) {
211                 /* …left border */
212                 if (con->col == 0)
213                         return 1;
214                 first = con->workspace->table[con->col-1][con->row];
215                 second = con;
216         } else if (event->event_x > 2) {
217                 /* …right border */
218                 if (con->col == (con->workspace->cols-1))
219                         return 1;
220                 first = con;
221                 second = con->workspace->table[con->col+1][con->row];
222         }
223
224         /* Open a new window, the resizebar. Grab the pointer and move the window around
225            as the user moves the pointer. */
226         Rect grabrect = {0, 0, root_screen->width_in_pixels, root_screen->height_in_pixels};
227         xcb_window_t grabwin = create_window(conn, grabrect, XCB_WINDOW_CLASS_INPUT_ONLY, -1, 0, NULL);
228
229         Rect helprect;
230         if (orientation == O_VERTICAL) {
231                 helprect.x = event->root_x;
232                 helprect.y = 0;
233                 helprect.width = 2;
234                 helprect.height = root_screen->height_in_pixels; /* this has to be the cell’s height */
235         } else {
236                 helprect.x = 0;
237                 helprect.y = event->root_y;
238                 helprect.width = root_screen->width_in_pixels; /* this has to be the cell’s width */
239                 helprect.height = 2;
240         }
241         xcb_window_t helpwin = create_window(conn, helprect, XCB_WINDOW_CLASS_INPUT_OUTPUT,
242                                              (orientation == O_VERTICAL ?
243                                               XCB_CURSOR_SB_V_DOUBLE_ARROW :
244                                               XCB_CURSOR_SB_H_DOUBLE_ARROW), 0, NULL);
245
246         uint32_t values[1] = {get_colorpixel(conn, "#4c7899")};
247         xcb_void_cookie_t cookie = xcb_change_window_attributes_checked(conn, helpwin, XCB_CW_BACK_PIXEL, values);
248         check_error(conn, cookie, "Could not change window attributes (background color)");
249
250         xcb_circulate_window(conn, XCB_CIRCULATE_RAISE_LOWEST, helpwin);
251
252         xcb_grab_pointer(conn, false, root, XCB_EVENT_MASK_BUTTON_RELEASE | XCB_EVENT_MASK_POINTER_MOTION,
253                         XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC, grabwin, XCB_NONE, XCB_CURRENT_TIME);
254
255         xcb_flush(conn);
256
257         xcb_generic_event_t *inside_event;
258         /* I’ve always wanted to have my own eventhandler… */
259         while ((inside_event = xcb_wait_for_event(conn))) {
260                 /* Same as get_event_handler in xcb */
261                 int nr = inside_event->response_type;
262                 if (nr == 0) {
263                         /* An error occured */
264                         handle_event(NULL, conn, inside_event);
265                         free(inside_event);
266                         continue;
267                 }
268                 assert(nr < 256);
269                 nr &= XCB_EVENT_RESPONSE_TYPE_MASK;
270                 assert(nr >= 2);
271
272                 /* Check if we need to escape this loop */
273                 if (nr == XCB_BUTTON_RELEASE)
274                         break;
275
276                 switch (nr) {
277                         case XCB_MOTION_NOTIFY:
278                                 if (orientation == O_VERTICAL) {
279                                         values[0] = ((xcb_motion_notify_event_t*)inside_event)->root_x;
280                                         xcb_configure_window(conn, helpwin, XCB_CONFIG_WINDOW_X, values);
281                                 } else {
282                                         values[0] = ((xcb_motion_notify_event_t*)inside_event)->root_y;
283                                         xcb_configure_window(conn, helpwin, XCB_CONFIG_WINDOW_Y, values);
284                                 }
285
286                                 xcb_flush(conn);
287                                 break;
288                         case XCB_EXPOSE:
289                                 /* Use original handler */
290                                 xcb_event_handle(&evenths, inside_event);
291                                 break;
292                         default:
293                                 LOG("Ignoring event of type %d\n", nr);
294                                 break;
295                 }
296                 free(inside_event);
297         }
298
299         xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
300         xcb_destroy_window(conn, helpwin);
301         xcb_destroy_window(conn, grabwin);
302         xcb_flush(conn);
303
304         Workspace *ws = con->workspace;
305         if (orientation == O_VERTICAL) {
306                 LOG("Resize was from X = %d to X = %d\n", event->root_x, values[0]);
307
308                 /* Convert 0 (for default width_factor) to actual numbers */
309                 if (first->width_factor == 0)
310                         first->width_factor = ((float)ws->rect.width / ws->cols) / ws->rect.width;
311                 if (second->width_factor == 0)
312                         second->width_factor = ((float)ws->rect.width / ws->cols) / ws->rect.width;
313
314                 first->width_factor *= (float)(first->width + (values[0] - event->root_x)) / first->width;
315                 second->width_factor *= (float)(second->width - (values[0] - event->root_x)) / second->width;
316         } else {
317                 LOG("Resize was from Y = %d to Y = %d\n", event->root_y, values[0]);
318
319                 /* Convert 0 (for default height_factor) to actual numbers */
320                 if (first->height_factor == 0)
321                         first->height_factor = ((float)ws->rect.height / ws->rows) / ws->rect.height;
322                 if (second->height_factor == 0)
323                         second->height_factor = ((float)ws->rect.height / ws->rows) / ws->rect.height;
324
325                 first->height_factor *= (float)(first->height + (values[0] - event->root_y)) / first->height;
326                 second->height_factor *= (float)(second->height - (values[0] - event->root_y)) / second->height;
327         }
328
329         render_layout(conn);
330
331         return 1;
332 }
333
334 /*
335  * A new window appeared on the screen (=was mapped), so let’s manage it.
336  *
337  */
338 int handle_map_notify_event(void *prophs, xcb_connection_t *conn, xcb_map_notify_event_t *event) {
339         window_attributes_t wa = { TAG_VALUE };
340         wa.u.override_redirect = event->override_redirect;
341         LOG("MapNotify for 0x%08x, serial is %d.\n", event->window, event->sequence);
342         LOG("setting ignore_notify_event = %d\n", event->sequence);
343         ignore_notify_event = event->sequence;
344         manage_window(prophs, conn, event->window, wa);
345         return 1;
346 }
347
348 /*
349  * Configuration notifies are only handled because we need to set up ignore for the following
350  * enter notify events
351  *
352  */
353 int handle_configure_event(void *prophs, xcb_connection_t *conn, xcb_configure_notify_event_t *event) {
354         xcb_window_t root = xcb_setup_roots_iterator(xcb_get_setup(conn)).data->root;
355
356         LOG("handle_configure_event\n");
357         LOG("event->type = %d, \n", event->response_type);
358         LOG("event->x = %d, ->y = %d, ->width = %d, ->height = %d\n", event->x, event->y, event->width, event->height);
359         LOG("sequence = %d\n", event->sequence);
360
361         ignore_notify_event = event->sequence;
362
363         if (event->event == root) {
364                 LOG("reconfigure of the root window, need to xinerama\n");
365                 /* FIXME: Somehow, this is occuring too often. Therefore, we check for 0/0,
366                    but is there a better way? */
367                 if (event->x == 0 && event->y == 0)
368                         xinerama_requery_screens(conn);
369                 return 1;
370         }
371
372         Client *client = table_get(byChild, event->window);
373         if (client == NULL) {
374                 LOG("client not managed, ignoring\n");
375                 return 1;
376         }
377
378         if (client->fullscreen) {
379                 LOG("client in fullscreen, not touching\n");
380                 return 1;
381         }
382
383         /* Let’s see if the application has changed size/position on its own *sigh*… */
384         if ((event->x != client->child_rect.x) ||
385             (event->y != client->child_rect.y) ||
386             (event->width != client->child_rect.width) ||
387             (event->height != client->child_rect.height)) {
388                 /* Who is your window manager? Who’s that, huh? I AM YOUR WINDOW MANAGER! */
389                 LOG("Application wanted to resize itself. Fixed that.\n");
390                 client->force_reconfigure = true;
391                 render_container(conn, client->container);
392                 xcb_flush(conn);
393         }
394
395         return 1;
396 }
397
398 /*
399  * Our window decorations were unmapped. That means, the window will be killed now,
400  * so we better clean up before.
401  *
402  */
403 int handle_unmap_notify_event(void *data, xcb_connection_t *conn, xcb_unmap_notify_event_t *event) {
404         xcb_window_t root = xcb_setup_roots_iterator(xcb_get_setup(conn)).data->root;
405
406         LOG("setting ignore_notify_event = %d\n", event->sequence);
407
408         ignore_notify_event = event->sequence;
409
410         Client *client = table_get(byChild, event->window);
411         /* First, we need to check if the client is awaiting an unmap-request which
412            was generated by us reparenting the window. In that case, we just ignore it. */
413         if (client != NULL && client->awaiting_useless_unmap) {
414                 LOG("Dropping this unmap request, it was generated by reparenting\n");
415                 client->awaiting_useless_unmap = false;
416                 return 1;
417         }
418         client = table_remove(byChild, event->window);
419
420         LOG("UnmapNotify for 0x%08x (received from 0x%08x): ", event->window, event->event);
421         if (client == NULL) {
422                 LOG("not a managed window. Ignoring.\n");
423                 return 0;
424         }
425
426         if (client->name != NULL)
427                 free(client->name);
428
429         if (client->container != NULL) {
430                 Container *con = client->container;
431
432                 /* If this was the fullscreen client, we need to unset it */
433                 if (client->fullscreen)
434                         con->workspace->fullscreen_client = NULL;
435
436                 /* If the container will be empty now and is in stacking mode, we need to
437                    correctly resize the stack_win */
438                 if (CIRCLEQ_EMPTY(&(con->clients)) && con->mode == MODE_STACK) {
439                         struct Stack_Window *stack_win = &(con->stack_win);
440                         stack_win->rect.height = 0;
441                         xcb_unmap_window(conn, stack_win->window);
442                 }
443
444                 /* Remove the client from the list of clients */
445                 CIRCLEQ_REMOVE(&(con->clients), client, clients);
446
447                 /* Remove from the focus stack */
448                 LOG("Removing from focus stack\n");
449                 SLIST_REMOVE(&(con->workspace->focus_stack), client, Client, focus_clients);
450
451                 /* Set currently_focused to the next client which will get focus in this
452                    particular container. This does not necessarily correspond with the client
453                    that will be focused next */
454                 con->currently_focused = NULL;
455                 Client *focus_client;
456                 SLIST_FOREACH(focus_client, &(con->workspace->focus_stack), focus_clients)
457                         if (focus_client->container == con) {
458                                 con->currently_focused = focus_client;
459                                 break;
460                         }
461
462                 /* Actually set focus, if there is a window which should get it */
463                 if (!SLIST_EMPTY(&(con->workspace->focus_stack)))
464                         set_focus(conn, SLIST_FIRST(&(con->workspace->focus_stack)));
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         Client *client = table_get(byChild, window);
490         if (client == NULL)
491                 return 1;
492
493         if (client->name != NULL)
494                 free(client->name);
495
496         client->name_len = xcb_get_property_value_length(prop);
497         client->name = smalloc(client->name_len);
498         strncpy(client->name, xcb_get_property_value(prop), client->name_len);
499         LOG("rename to \"%.*s\".\n", client->name_len, client->name);
500
501         if (client->container->mode == MODE_STACK)
502                 render_container(conn, client->container);
503         else decorate_window(conn, client, client->frame, client->titlegc, 0);
504         xcb_flush(conn);
505
506         return 1;
507 }
508
509 /*
510  * Expose event means we should redraw our windows (= title bar)
511  *
512  */
513 int handle_expose_event(void *data, xcb_connection_t *conn, xcb_expose_event_t *event) {
514         /* event->count is the number of minimum remaining expose events for this window, so we
515            skip all events but the last one */
516         if (event->count != 0)
517                 return 1;
518
519         Client *client = table_get(byParent, event->window);
520         if (client == NULL) {
521                 /* There was no client in the table, so this is probably an expose event for
522                    one of our stack_windows. */
523                 struct Stack_Window *stack_win;
524                 SLIST_FOREACH(stack_win, &stack_wins, stack_windows)
525                         if (stack_win->window == event->window) {
526                                 render_container(conn, stack_win->container);
527                                 return 1;
528                         }
529                 return 1;
530         }
531
532         LOG("handle_expose_event()\n");
533         if (client->container->mode != MODE_STACK)
534                 decorate_window(conn, client, client->frame, client->titlegc, 0);
535         else {
536                 uint32_t background_color;
537                 /* Distinguish if the window is currently focused… */
538                 if (CUR_CELL->currently_focused == client)
539                         background_color = get_colorpixel(conn, "#285577");
540                 /* …or if it is the focused window in a not focused container */
541                 else background_color = get_colorpixel(conn, "#555555");
542
543                 /* Set foreground color to current focused color, line width to 2 */
544                 uint32_t values[] = {background_color, 2};
545                 xcb_change_gc(conn, client->titlegc, XCB_GC_FOREGROUND | XCB_GC_LINE_WIDTH, values);
546
547                 /* Draw the border, the ±1 is for line width = 2 */
548                 xcb_point_t points[] = {{1, 0},                                           /* left upper edge */
549                                         {1, client->rect.height-1},                       /* left bottom edge */
550                                         {client->rect.width-1, client->rect.height-1},    /* right bottom edge */
551                                         {client->rect.width-1, 0}};                       /* right upper edge */
552                 xcb_poly_line(conn, XCB_COORD_MODE_ORIGIN, client->frame, client->titlegc, 4, points);
553
554                 /* Draw a black background */
555                 xcb_change_gc_single(conn, client->titlegc, XCB_GC_FOREGROUND, get_colorpixel(conn, "#000000"));
556                 xcb_rectangle_t crect = {2, 0, client->rect.width - (2 + 2), client->rect.height - 2};
557                 xcb_poly_fill_rectangle(conn, client->frame, client->titlegc, 1, &crect);
558
559                 xcb_flush(conn);
560         }
561         return 1;
562 }
563
564 /*
565  * Handle client messages (EWMH)
566  *
567  */
568 int handle_client_message(void *data, xcb_connection_t *conn, xcb_client_message_event_t *event) {
569         LOG("client_message\n");
570
571         if (event->type == atoms[_NET_WM_STATE]) {
572                 if (event->format != 32 || event->data.data32[1] != atoms[_NET_WM_STATE_FULLSCREEN])
573                         return 0;
574
575                 LOG("fullscreen\n");
576
577                 Client *client = table_get(byChild, event->window);
578                 if (client == NULL)
579                         return 0;
580
581                 /* Check if the fullscreen state should be toggled */
582                 if ((client->fullscreen &&
583                      (event->data.data32[0] == _NET_WM_STATE_REMOVE ||
584                       event->data.data32[0] == _NET_WM_STATE_TOGGLE)) ||
585                     (!client->fullscreen &&
586                      (event->data.data32[0] == _NET_WM_STATE_ADD ||
587                       event->data.data32[0] == _NET_WM_STATE_TOGGLE)))
588                         toggle_fullscreen(conn, client);
589         } else {
590                 LOG("unhandled clientmessage\n");
591                 return 0;
592         }
593
594         return 1;
595 }
596
597 int window_type_handler(void *data, xcb_connection_t *conn, uint8_t state, xcb_window_t window,
598                         xcb_atom_t atom, xcb_get_property_reply_t *property) {
599         /* TODO: Implement this one. To do this, implement a little test program which sleep(1)s
600          before changing this property. */
601         LOG("_NET_WM_WINDOW_TYPE changed, this is not yet implemented.\n");
602         return 0;
603 }
604
605 /*
606  * Handles the size hints set by a window, but currently only the part necessary for displaying
607  * clients proportionally inside their frames (mplayer for example)
608  *
609  * See ICCCM 4.1.2.3 for more details
610  *
611  */
612 int handle_normal_hints(void *data, xcb_connection_t *conn, uint8_t state, xcb_window_t window,
613                         xcb_atom_t name, xcb_get_property_reply_t *reply) {
614         LOG("handle_normal_hints\n");
615         Client *client = table_get(byChild, window);
616         if (client == NULL) {
617                 LOG("No such client\n");
618                 return;
619         }
620         xcb_size_hints_t size_hints;
621
622         /* If the hints were already in this event, use them, if not, request them */
623         if (reply != NULL)
624                 xcb_get_wm_size_hints_from_reply(&size_hints, reply);
625         else
626                 xcb_get_wm_normal_hints_reply(conn, xcb_get_wm_normal_hints_unchecked(conn, client->child), &size_hints, NULL);
627
628         /* If no aspect ratio was set or if it was invalid, we ignore the hints */
629         if (!(size_hints.flags & XCB_SIZE_HINT_P_ASPECT) ||
630             (size_hints.min_aspect_num <= 0) ||
631             (size_hints.min_aspect_den <= 0)) {
632                 LOG("No aspect ratio set, ignoring\n");
633                 return;
634         }
635
636         LOG("window is %08x / %s\n", client->child, client->name);
637
638         int base_width = 0, base_height = 0,
639             min_width = 0, min_height = 0;
640
641         /* base_width/height are the desired size of the window.
642            We check if either the program-specified size or the program-specified
643            min-size is available */
644         if (size_hints.flags & XCB_SIZE_HINT_P_SIZE) {
645                 base_width = size_hints.base_width;
646                 base_height = size_hints.base_height;
647         } else if (size_hints.flags & XCB_SIZE_HINT_P_MIN_SIZE) {
648                 base_width = size_hints.min_width;
649                 base_height = size_hints.min_height;
650         }
651
652         if (size_hints.flags & XCB_SIZE_HINT_P_MIN_SIZE) {
653                 min_width = size_hints.min_width;
654                 min_height = size_hints.min_height;
655         } else if (size_hints.flags & XCB_SIZE_HINT_P_SIZE) {
656                 min_width = size_hints.base_width;
657                 min_height = size_hints.base_height;
658         }
659
660         double width = client->rect.width - base_width;
661         double height = client->rect.height - base_height;
662         /* Convert numerator/denominator to a double */
663         double min_aspect = (double)size_hints.min_aspect_num / size_hints.min_aspect_den;
664         double max_aspect = (double)size_hints.max_aspect_num / size_hints.min_aspect_den;
665
666         LOG("min_aspect = %f, max_aspect = %f\n", min_aspect, max_aspect);
667         LOG("width = %f, height = %f\n", width, height);
668
669         /* Sanity checks, this is user-input, in a way */
670         if (max_aspect <= 0 || min_aspect <= 0 || height == 0 || (width / height) <= 0)
671                 return;
672
673         /* Check if we need to set proportional_* variables using the correct ratio */
674         if ((width / height) < min_aspect) {
675                 client->proportional_width = width;
676                 client->proportional_height = width / min_aspect;
677         } else if ((width / height) > max_aspect) {
678                 client->proportional_width = width;
679                 client->proportional_height = width / max_aspect;
680         } else return;
681
682         client->force_reconfigure = true;
683
684         if (client->container != NULL) {
685                 render_container(conn, client->container);
686                 xcb_flush(conn);
687         }
688 }