]> git.sur5r.net Git - i3/i3/blob - src/floating.c
4dd44f574d98699cf896294643543d3761623c5b
[i3/i3] / src / floating.c
1 #undef I3__FILE__
2 #define I3__FILE__ "floating.c"
3 /*
4  * vim:ts=4:sw=4:expandtab
5  *
6  * i3 - an improved dynamic tiling window manager
7  * © 2009-2011 Michael Stapelberg and contributors (see also: LICENSE)
8  *
9  * floating.c: Floating windows.
10  *
11  */
12 #include "all.h"
13
14 extern xcb_connection_t *conn;
15
16 /*
17  * Calculates sum of heights and sum of widths of all currently active outputs
18  *
19  */
20 static Rect total_outputs_dimensions(void) {
21     Output *output;
22     /* Use Rect to encapsulate dimensions, ignoring x/y */
23     Rect outputs_dimensions = {0, 0, 0, 0};
24     TAILQ_FOREACH(output, &outputs, outputs) {
25         outputs_dimensions.height += output->rect.height;
26         outputs_dimensions.width += output->rect.width;
27     }
28     return outputs_dimensions;
29 }
30
31 /**
32  * Called when a floating window is created or resized.
33  * This function resizes the window if its size is higher or lower than the
34  * configured maximum/minimum size, respectively.
35  *
36  */
37 void floating_check_size(Con *floating_con) {
38     /* Define reasonable minimal and maximal sizes for floating windows */
39     const int floating_sane_min_height = 50;
40     const int floating_sane_min_width = 75;
41     Rect floating_sane_max_dimensions;
42     Con *focused_con = con_descend_focused(floating_con);
43
44     /* obey size increments */
45     if (focused_con->height_increment || focused_con->width_increment) {
46         Rect border_rect = con_border_style_rect(focused_con);
47
48         /* We have to do the opposite calculations that render_con() do
49          * to get the exact size we want. */
50         border_rect.width = -border_rect.width;
51         border_rect.width += 2 * focused_con->border_width;
52         border_rect.height = -border_rect.height;
53         border_rect.height += 2 * focused_con->border_width;
54         if (con_border_style(focused_con) == BS_NORMAL)
55             border_rect.height += render_deco_height();
56
57         if (focused_con->height_increment &&
58             floating_con->rect.height >= focused_con->base_height + border_rect.height) {
59             floating_con->rect.height -= focused_con->base_height + border_rect.height;
60             floating_con->rect.height -= floating_con->rect.height % focused_con->height_increment;
61             floating_con->rect.height += focused_con->base_height + border_rect.height;
62         }
63
64         if (focused_con->width_increment &&
65             floating_con->rect.width >= focused_con->base_width + border_rect.width) {
66             floating_con->rect.width -= focused_con->base_width + border_rect.width;
67             floating_con->rect.width -= floating_con->rect.width % focused_con->width_increment;
68             floating_con->rect.width += focused_con->base_width + border_rect.width;
69         }
70     }
71
72     /* Unless user requests otherwise (-1), ensure width/height do not exceed
73      * configured maxima or, if unconfigured, limit to combined width of all
74      * outputs */
75     if (config.floating_minimum_height != -1) {
76         if (config.floating_minimum_height == 0)
77             floating_con->rect.height = max(floating_con->rect.height, floating_sane_min_height);
78         else
79             floating_con->rect.height = max(floating_con->rect.height, config.floating_minimum_height);
80     }
81     if (config.floating_minimum_width != -1) {
82         if (config.floating_minimum_width == 0)
83             floating_con->rect.width = max(floating_con->rect.width, floating_sane_min_width);
84         else
85             floating_con->rect.width = max(floating_con->rect.width, config.floating_minimum_width);
86     }
87
88     /* Unless user requests otherwise (-1), raise the width/height to
89      * reasonable minimum dimensions */
90     floating_sane_max_dimensions = total_outputs_dimensions();
91     if (config.floating_maximum_height != -1) {
92         if (config.floating_maximum_height == 0)
93             floating_con->rect.height = min(floating_con->rect.height, floating_sane_max_dimensions.height);
94         else
95             floating_con->rect.height = min(floating_con->rect.height, config.floating_maximum_height);
96     }
97     if (config.floating_maximum_width != -1) {
98         if (config.floating_maximum_width == 0)
99             floating_con->rect.width = min(floating_con->rect.width, floating_sane_max_dimensions.width);
100         else
101             floating_con->rect.width = min(floating_con->rect.width, config.floating_maximum_width);
102     }
103 }
104
105 void floating_enable(Con *con, bool automatic) {
106     bool set_focus = (con == focused);
107
108     if (con->parent && con->parent->type == CT_DOCKAREA) {
109         LOG("Container is a dock window, not enabling floating mode.\n");
110         return;
111     }
112
113     if (con_is_floating(con)) {
114         LOG("Container is already in floating mode, not doing anything.\n");
115         return;
116     }
117
118     /* 1: If the container is a workspace container, we need to create a new
119      * split-container with the same layout and make that one floating. We
120      * cannot touch the workspace container itself because floating containers
121      * are children of the workspace. */
122     if (con->type == CT_WORKSPACE) {
123         LOG("This is a workspace, creating new container around content\n");
124         if (con_num_children(con) == 0) {
125             LOG("Workspace is empty, aborting\n");
126             return;
127         }
128         /* TODO: refactor this with src/con.c:con_set_layout */
129         Con *new = con_new(NULL, NULL);
130         new->parent = con;
131         new->layout = con->layout;
132
133         /* since the new container will be set into floating mode directly
134          * afterwards, we need to copy the workspace rect. */
135         memcpy(&(new->rect), &(con->rect), sizeof(Rect));
136
137         Con *old_focused = TAILQ_FIRST(&(con->focus_head));
138         if (old_focused == TAILQ_END(&(con->focus_head)))
139             old_focused = NULL;
140
141         /* 4: move the existing cons of this workspace below the new con */
142         DLOG("Moving cons\n");
143         Con *child;
144         while (!TAILQ_EMPTY(&(con->nodes_head))) {
145             child = TAILQ_FIRST(&(con->nodes_head));
146             con_detach(child);
147             con_attach(child, new, true);
148         }
149
150         /* 4: attach the new split container to the workspace */
151         DLOG("Attaching new split to ws\n");
152         con_attach(new, con, false);
153
154         if (old_focused)
155             con_focus(old_focused);
156
157         con = new;
158         set_focus = false;
159     }
160
161     /* 1: detach the container from its parent */
162     /* TODO: refactor this with tree_close() */
163     TAILQ_REMOVE(&(con->parent->nodes_head), con, nodes);
164     TAILQ_REMOVE(&(con->parent->focus_head), con, focused);
165
166     con_fix_percent(con->parent);
167
168     /* 2: create a new container to render the decoration on, add
169      * it as a floating window to the workspace */
170     Con *nc = con_new(NULL, NULL);
171     /* we need to set the parent afterwards instead of passing it as an
172      * argument to con_new() because nc would be inserted into the tiling layer
173      * otherwise. */
174     Con *ws = con_get_workspace(con);
175     nc->parent = ws;
176     nc->type = CT_FLOATING_CON;
177     nc->layout = L_SPLITH;
178     /* We insert nc already, even though its rect is not yet calculated. This
179      * is necessary because otherwise the workspace might be empty (and get
180      * closed in tree_close()) even though it’s not. */
181     TAILQ_INSERT_TAIL(&(ws->floating_head), nc, floating_windows);
182     TAILQ_INSERT_TAIL(&(ws->focus_head), nc, focused);
183
184     /* check if the parent container is empty and close it if so */
185     if ((con->parent->type == CT_CON || con->parent->type == CT_FLOATING_CON) &&
186         con_num_children(con->parent) == 0) {
187         DLOG("Old container empty after setting this child to floating, closing\n");
188         tree_close(con->parent, DONT_KILL_WINDOW, false, false);
189     }
190
191     char *name;
192     sasprintf(&name, "[i3 con] floatingcon around %p", con);
193     x_set_name(nc, name);
194     free(name);
195
196     /* find the height for the decorations */
197     int deco_height = render_deco_height();
198
199     DLOG("Original rect: (%d, %d) with %d x %d\n", con->rect.x, con->rect.y, con->rect.width, con->rect.height);
200     DLOG("Geometry = (%d, %d) with %d x %d\n", con->geometry.x, con->geometry.y, con->geometry.width, con->geometry.height);
201     Rect zero = { 0, 0, 0, 0 };
202     nc->rect = con->geometry;
203     /* If the geometry was not set (split containers), we need to determine a
204      * sensible one by combining the geometry of all children */
205     if (memcmp(&(nc->rect), &zero, sizeof(Rect)) == 0) {
206         DLOG("Geometry not set, combining children\n");
207         Con *child;
208         TAILQ_FOREACH(child, &(con->nodes_head), nodes) {
209             DLOG("child geometry: %d x %d\n", child->geometry.width, child->geometry.height);
210             nc->rect.width += child->geometry.width;
211             nc->rect.height = max(nc->rect.height, child->geometry.height);
212         }
213     }
214
215     floating_check_size(nc);
216
217     /* 3: attach the child to the new parent container. We need to do this
218      * because con_border_style_rect() needs to access con->parent. */
219     con->parent = nc;
220     con->percent = 1.0;
221     con->floating = FLOATING_USER_ON;
222
223     /* 4: set the border style as specified with new_float */
224     if (automatic)
225         con->border_style = config.default_floating_border;
226
227     /* Add pixels for the decoration. */
228     Rect border_style_rect = con_border_style_rect(con);
229
230     nc->rect.height -= border_style_rect.height;
231     nc->rect.width -= border_style_rect.width;
232
233     /* Add some more pixels for the title bar */
234     if(con_border_style(con) == BS_NORMAL)
235         nc->rect.height += deco_height;
236
237     /* Honor the X11 border */
238     nc->rect.height += con->border_width * 2;
239     nc->rect.width += con->border_width * 2;
240
241     /* Some clients (like GIMP’s color picker window) get mapped
242      * to (0, 0), so we push them to a reasonable position
243      * (centered over their leader) */
244     if (nc->rect.x == 0 && nc->rect.y == 0) {
245         Con *leader;
246         if (con->window && con->window->leader != XCB_NONE &&
247             (leader = con_by_window_id(con->window->leader)) != NULL) {
248             DLOG("Centering above leader\n");
249             nc->rect.x = leader->rect.x + (leader->rect.width / 2) - (nc->rect.width / 2);
250             nc->rect.y = leader->rect.y + (leader->rect.height / 2) - (nc->rect.height / 2);
251         } else {
252             /* center the window on workspace as fallback */
253             nc->rect.x = ws->rect.x + (ws->rect.width / 2) - (nc->rect.width / 2);
254             nc->rect.y = ws->rect.y + (ws->rect.height / 2) - (nc->rect.height / 2);
255         }
256     }
257
258     /* Sanity check: Are the coordinates on the appropriate output? If not, we
259      * need to change them */
260     Output *current_output = get_output_containing(nc->rect.x +
261         (nc->rect.width / 2), nc->rect.y + (nc->rect.height / 2));
262
263     Con *correct_output = con_get_output(ws);
264     if (!current_output || current_output->con != correct_output) {
265         DLOG("This floating window is on the wrong output, fixing coordinates (currently (%d, %d))\n",
266              nc->rect.x, nc->rect.y);
267
268         /* If moving from one output to another, keep the relative position
269          * consistent (e.g. a centered dialog will remain centered). */
270         if (current_output)
271             floating_fix_coordinates(nc, &current_output->con->rect, &correct_output->rect);
272         else {
273             nc->rect.x = correct_output->rect.x;
274             nc->rect.y = correct_output->rect.y;
275         }
276     }
277
278     DLOG("Floating rect: (%d, %d) with %d x %d\n", nc->rect.x, nc->rect.y, nc->rect.width, nc->rect.height);
279
280     /* 5: Subtract the deco_height in order to make the floating window appear
281      * at precisely the position it specified in its original geometry (which
282      * is what applications might remember). */
283     deco_height = (con->border_style == BS_NORMAL ? render_deco_height() : 0);
284     nc->rect.y -= deco_height;
285
286     DLOG("Corrected y = %d (deco_height = %d)\n", nc->rect.y, deco_height);
287
288     TAILQ_INSERT_TAIL(&(nc->nodes_head), con, nodes);
289     TAILQ_INSERT_TAIL(&(nc->focus_head), con, focused);
290
291     /* render the cons to get initial window_rect correct */
292     render_con(nc, false);
293     render_con(con, false);
294
295     if (set_focus)
296         con_focus(con);
297
298     /* Check if we need to re-assign it to a different workspace because of its
299      * coordinates and exit if that was done successfully. */
300     if (floating_maybe_reassign_ws(nc))
301         return;
302
303     /* Sanitize coordinates: Check if they are on any output */
304     if (get_output_containing(nc->rect.x, nc->rect.y) != NULL)
305         return;
306
307     ELOG("No output found at destination coordinates, centering floating window on current ws\n");
308     nc->rect.x = ws->rect.x + (ws->rect.width / 2) - (nc->rect.width / 2);
309     nc->rect.y = ws->rect.y + (ws->rect.height / 2) - (nc->rect.height / 2);
310 }
311
312 void floating_disable(Con *con, bool automatic) {
313     if (!con_is_floating(con)) {
314         LOG("Container isn't floating, not doing anything.\n");
315         return;
316     }
317
318     Con *ws = con_get_workspace(con);
319
320     /* 1: detach from parent container */
321     TAILQ_REMOVE(&(con->parent->nodes_head), con, nodes);
322     TAILQ_REMOVE(&(con->parent->focus_head), con, focused);
323
324     /* 2: kill parent container */
325     TAILQ_REMOVE(&(con->parent->parent->floating_head), con->parent, floating_windows);
326     TAILQ_REMOVE(&(con->parent->parent->focus_head), con->parent, focused);
327     tree_close(con->parent, DONT_KILL_WINDOW, true, false);
328
329     /* 3: re-attach to the parent of the currently focused con on the workspace
330      * this floating con was on */
331     Con *focused = con_descend_tiling_focused(ws);
332
333     /* if there is no other container on this workspace, focused will be the
334      * workspace itself */
335     if (focused->type == CT_WORKSPACE)
336         con->parent = focused;
337     else con->parent = focused->parent;
338
339     /* con_fix_percent will adjust the percent value */
340     con->percent = 0.0;
341
342     con->floating = FLOATING_USER_OFF;
343
344     con_attach(con, con->parent, false);
345
346     con_fix_percent(con->parent);
347     // TODO: don’t influence focus handling when Con was not focused before.
348     con_focus(con);
349 }
350
351 /*
352  * Toggles floating mode for the given container.
353  *
354  * If the automatic flag is set to true, this was an automatic update by a change of the
355  * window class from the application which can be overwritten by the user.
356  *
357  */
358 void toggle_floating_mode(Con *con, bool automatic) {
359     /* see if the client is already floating */
360     if (con_is_floating(con)) {
361         LOG("already floating, re-setting to tiling\n");
362
363         floating_disable(con, automatic);
364         return;
365     }
366
367     floating_enable(con, automatic);
368 }
369
370 /*
371  * Raises the given container in the list of floating containers
372  *
373  */
374 void floating_raise_con(Con *con) {
375     DLOG("Raising floating con %p / %s\n", con, con->name);
376     TAILQ_REMOVE(&(con->parent->floating_head), con, floating_windows);
377     TAILQ_INSERT_TAIL(&(con->parent->floating_head), con, floating_windows);
378 }
379
380 /*
381  * Checks if con’s coordinates are within its workspace and re-assigns it to
382  * the actual workspace if not.
383  *
384  */
385 bool floating_maybe_reassign_ws(Con *con) {
386     Output *output = get_output_containing(
387         con->rect.x + (con->rect.width / 2),
388         con->rect.y + (con->rect.height / 2));
389
390     if (!output) {
391         ELOG("No output found at destination coordinates?\n");
392         return false;
393     }
394
395     if (con_get_output(con) == output->con) {
396         DLOG("still the same ws\n");
397         return false;
398     }
399
400     DLOG("Need to re-assign!\n");
401
402     Con *content = output_get_content(output->con);
403     Con *ws = TAILQ_FIRST(&(content->focus_head));
404     DLOG("Moving con %p / %s to workspace %p / %s\n", con, con->name, ws, ws->name);
405     con_move_to_workspace(con, ws, false, true);
406     con_focus(con_descend_focused(con));
407     return true;
408 }
409
410 DRAGGING_CB(drag_window_callback) {
411     const struct xcb_button_press_event_t *event = extra;
412
413     /* Reposition the client correctly while moving */
414     con->rect.x = old_rect->x + (new_x - event->root_x);
415     con->rect.y = old_rect->y + (new_y - event->root_y);
416
417     render_con(con, false);
418     x_push_node(con);
419     xcb_flush(conn);
420
421     /* Check if we cross workspace boundaries while moving */
422     if (!floating_maybe_reassign_ws(con))
423         return;
424     tree_render();
425 }
426
427 /*
428  * Called when the user clicked on the titlebar of a floating window.
429  * Calls the drag_pointer function with the drag_window callback
430  *
431  */
432 void floating_drag_window(Con *con, const xcb_button_press_event_t *event) {
433     DLOG("floating_drag_window\n");
434
435     /* Push changes before dragging, so that the window gets raised now and not
436      * after the user releases the mouse button */
437     tree_render();
438
439     /* Drag the window */
440     drag_pointer(con, event, XCB_NONE, BORDER_TOP /* irrelevant */, XCURSOR_CURSOR_MOVE, drag_window_callback, event);
441
442     /* If this is a scratchpad window, don't auto center it from now on. */
443     if (con->scratchpad_state == SCRATCHPAD_FRESH)
444         con->scratchpad_state = SCRATCHPAD_CHANGED;
445
446     tree_render();
447 }
448
449 /*
450  * This is an ugly data structure which we need because there is no standard
451  * way of having nested functions (only available as a gcc extension at the
452  * moment, clang doesn’t support it) or blocks (only available as a clang
453  * extension and only on Mac OS X systems at the moment).
454  *
455  */
456 struct resize_window_callback_params {
457     const border_t corner;
458     const bool proportional;
459     const xcb_button_press_event_t *event;
460 };
461
462 DRAGGING_CB(resize_window_callback) {
463     const struct resize_window_callback_params *params = extra;
464     const xcb_button_press_event_t *event = params->event;
465     border_t corner = params->corner;
466
467     int32_t dest_x = con->rect.x;
468     int32_t dest_y = con->rect.y;
469     uint32_t dest_width;
470     uint32_t dest_height;
471
472     double ratio = (double) old_rect->width / old_rect->height;
473
474     /* First guess: We resize by exactly the amount the mouse moved,
475      * taking into account in which corner the client was grabbed */
476     if (corner & BORDER_LEFT)
477         dest_width = old_rect->width - (new_x - event->root_x);
478     else dest_width = old_rect->width + (new_x - event->root_x);
479
480     if (corner & BORDER_TOP)
481         dest_height = old_rect->height - (new_y - event->root_y);
482     else dest_height = old_rect->height + (new_y - event->root_y);
483
484     /* User wants to keep proportions, so we may have to adjust our values */
485     if (params->proportional) {
486         dest_width = max(dest_width, (int) (dest_height * ratio));
487         dest_height = max(dest_height, (int) (dest_width / ratio));
488     }
489
490     con->rect = (Rect) { dest_x, dest_y, dest_width, dest_height };
491
492     /* Obey window size */
493     floating_check_size(con);
494
495     /* If not the lower right corner is grabbed, we must also reposition
496      * the client by exactly the amount we resized it */
497     if (corner & BORDER_LEFT)
498         dest_x = old_rect->x + (old_rect->width - con->rect.width);
499
500     if (corner & BORDER_TOP)
501         dest_y = old_rect->y + (old_rect->height - con->rect.height);
502
503     con->rect.x = dest_x;
504     con->rect.y = dest_y;
505
506     /* TODO: don’t re-render the whole tree just because we change
507      * coordinates of a floating window */
508     tree_render();
509     x_push_changes(croot);
510 }
511
512 /*
513  * Called when the user clicked on a floating window while holding the
514  * floating_modifier and the right mouse button.
515  * Calls the drag_pointer function with the resize_window callback
516  *
517  */
518 void floating_resize_window(Con *con, const bool proportional,
519                             const xcb_button_press_event_t *event) {
520     DLOG("floating_resize_window\n");
521
522     /* corner saves the nearest corner to the original click. It contains
523      * a bitmask of the nearest borders (BORDER_LEFT, BORDER_RIGHT, …) */
524     border_t corner = 0;
525
526     if (event->event_x <= (con->rect.width / 2))
527         corner |= BORDER_LEFT;
528     else corner |= BORDER_RIGHT;
529
530     int cursor = 0;
531     if (event->event_y <= (con->rect.height / 2)) {
532         corner |= BORDER_TOP;
533         cursor = (corner & BORDER_LEFT) ?
534             XCURSOR_CURSOR_TOP_LEFT_CORNER : XCURSOR_CURSOR_TOP_RIGHT_CORNER;
535     }
536     else {
537         corner |= BORDER_BOTTOM;
538         cursor = (corner & BORDER_LEFT) ?
539             XCURSOR_CURSOR_BOTTOM_LEFT_CORNER : XCURSOR_CURSOR_BOTTOM_RIGHT_CORNER;
540     }
541
542     struct resize_window_callback_params params = { corner, proportional, event };
543
544     drag_pointer(con, event, XCB_NONE, BORDER_TOP /* irrelevant */, cursor, resize_window_callback, &params);
545
546     /* If this is a scratchpad window, don't auto center it from now on. */
547     if (con->scratchpad_state == SCRATCHPAD_FRESH)
548         con->scratchpad_state = SCRATCHPAD_CHANGED;
549 }
550
551 /*
552  * This function grabs your pointer and lets you drag stuff around (borders).
553  * Every time you move your mouse, an XCB_MOTION_NOTIFY event will be received
554  * and the given callback will be called with the parameters specified (client,
555  * border on which the click originally was), the original rect of the client,
556  * the event and the new coordinates (x, y).
557  *
558  */
559 void drag_pointer(Con *con, const xcb_button_press_event_t *event, xcb_window_t
560                 confine_to, border_t border, int cursor, callback_t callback, const void *extra)
561 {
562     uint32_t new_x, new_y;
563     Rect old_rect = { 0, 0, 0, 0 };
564     if (con != NULL)
565         memcpy(&old_rect, &(con->rect), sizeof(Rect));
566
567     Cursor xcursor = (cursor && xcursor_supported) ?
568         xcursor_get_cursor(cursor) : XCB_NONE;
569
570     /* Grab the pointer */
571     xcb_grab_pointer_cookie_t cookie;
572     xcb_grab_pointer_reply_t *reply;
573     cookie = xcb_grab_pointer(conn,
574         false,               /* get all pointer events specified by the following mask */
575         root,                /* grab the root window */
576         XCB_EVENT_MASK_BUTTON_RELEASE | XCB_EVENT_MASK_POINTER_MOTION, /* which events to let through */
577         XCB_GRAB_MODE_ASYNC, /* pointer events should continue as normal */
578         XCB_GRAB_MODE_ASYNC, /* keyboard mode */
579         confine_to,          /* confine_to = in which window should the cursor stay */
580         xcursor,             /* possibly display a special cursor */
581         XCB_CURRENT_TIME);
582
583     if ((reply = xcb_grab_pointer_reply(conn, cookie, NULL)) == NULL) {
584         ELOG("Could not grab pointer\n");
585         return;
586     }
587
588     free(reply);
589
590     /* Go into our own event loop */
591     xcb_flush(conn);
592
593     xcb_generic_event_t *inside_event, *last_motion_notify = NULL;
594     bool loop_done = false;
595     /* I’ve always wanted to have my own eventhandler… */
596     while (!loop_done && (inside_event = xcb_wait_for_event(conn))) {
597         /* We now handle all events we can get using xcb_poll_for_event */
598         do {
599             /* skip x11 errors */
600             if (inside_event->response_type == 0) {
601                 free(inside_event);
602                 continue;
603             }
604             /* Strip off the highest bit (set if the event is generated) */
605             int type = (inside_event->response_type & 0x7F);
606
607             switch (type) {
608                 case XCB_BUTTON_RELEASE:
609                     loop_done = true;
610                     break;
611
612                 case XCB_MOTION_NOTIFY:
613                     /* motion_notify events are saved for later */
614                     FREE(last_motion_notify);
615                     last_motion_notify = inside_event;
616                     break;
617
618                 case XCB_UNMAP_NOTIFY:
619                 case XCB_KEY_PRESS:
620                 case XCB_KEY_RELEASE:
621                     DLOG("Unmap-notify, aborting\n");
622                     handle_event(type, inside_event);
623                     loop_done = true;
624                     break;
625
626                 default:
627                     DLOG("Passing to original handler\n");
628                     /* Use original handler */
629                     handle_event(type, inside_event);
630                     break;
631             }
632             if (last_motion_notify != inside_event)
633                 free(inside_event);
634         } while ((inside_event = xcb_poll_for_event(conn)) != NULL);
635
636         if (last_motion_notify == NULL || loop_done)
637             continue;
638
639         new_x = ((xcb_motion_notify_event_t*)last_motion_notify)->root_x;
640         new_y = ((xcb_motion_notify_event_t*)last_motion_notify)->root_y;
641
642         callback(con, &old_rect, new_x, new_y, extra);
643         FREE(last_motion_notify);
644     }
645
646     xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
647     xcb_flush(conn);
648 }
649
650 /*
651  * Repositions the CT_FLOATING_CON to have the coordinates specified by
652  * newrect, but only if the coordinates are not out-of-bounds. Also reassigns
653  * the floating con to a different workspace if this move was across different
654  * outputs.
655  *
656  */
657 void floating_reposition(Con *con, Rect newrect) {
658     /* Sanity check: Are the new coordinates on any output? If not, we
659      * ignore that request. */
660     Output *output = get_output_containing(
661         newrect.x + (newrect.width / 2),
662         newrect.y + (newrect.height / 2));
663
664     if (!output) {
665         ELOG("No output found at destination coordinates. Not repositioning.\n");
666         return;
667     }
668
669     con->rect = newrect;
670
671     floating_maybe_reassign_ws(con);
672
673     /* If this is a scratchpad window, don't auto center it from now on. */
674     if (con->scratchpad_state == SCRATCHPAD_FRESH)
675         con->scratchpad_state = SCRATCHPAD_CHANGED;
676
677     tree_render();
678 }
679
680 /*
681  * Fixes the coordinates of the floating window whenever the window gets
682  * reassigned to a different output (or when the output’s rect changes).
683  *
684  */
685 void floating_fix_coordinates(Con *con, Rect *old_rect, Rect *new_rect) {
686     DLOG("Fixing coordinates of floating window %p (rect (%d, %d), %d x %d)\n",
687          con, con->rect.x, con->rect.y, con->rect.width, con->rect.height);
688     DLOG("old_rect = (%d, %d), %d x %d\n",
689          old_rect->x, old_rect->y, old_rect->width, old_rect->height);
690     DLOG("new_rect = (%d, %d), %d x %d\n",
691          new_rect->x, new_rect->y, new_rect->width, new_rect->height);
692     /* First we get the x/y coordinates relative to the x/y coordinates
693      * of the output on which the window is on */
694     int32_t rel_x = con->rect.x - old_rect->x + (int32_t)(con->rect.width  / 2);
695     int32_t rel_y = con->rect.y - old_rect->y + (int32_t)(con->rect.height / 2);
696     /* Then we calculate a fraction, for example 0.63 for a window
697      * which is at y = 1212 of a 1920 px high output */
698     DLOG("rel_x = %d, rel_y = %d, fraction_x = %f, fraction_y = %f, output->w = %d, output->h = %d\n",
699           rel_x, rel_y, (double)rel_x / old_rect->width, (double)rel_y / old_rect->height,
700           old_rect->width, old_rect->height);
701     /* Here we have to multiply at first. Or we will lose precision when not compiled with -msse2 */
702     con->rect.x = (int32_t)new_rect->x + (double)(rel_x * (int32_t)new_rect->width)
703         / (int32_t)old_rect->width - (int32_t)(con->rect.width / 2);
704     con->rect.y = (int32_t)new_rect->y + (double)(rel_y * (int32_t)new_rect->height)
705         / (int32_t)old_rect->height - (int32_t)(con->rect.height / 2);
706     DLOG("Resulting coordinates: x = %d, y = %d\n", con->rect.x, con->rect.y);
707 }
708
709 #if 0
710 /*
711  * Moves the client 10px to the specified direction.
712  *
713  */
714 void floating_move(xcb_connection_t *conn, Client *currently_focused, direction_t direction) {
715         DLOG("floating move\n");
716
717         Rect destination = currently_focused->rect;
718         Rect *screen = &(currently_focused->workspace->output->rect);
719
720         switch (direction) {
721                 case D_LEFT:
722                         destination.x -= 10;
723                         break;
724                 case D_RIGHT:
725                         destination.x += 10;
726                         break;
727                 case D_UP:
728                         destination.y -= 10;
729                         break;
730                 case D_DOWN:
731                         destination.y += 10;
732                         break;
733                 /* to make static analyzers happy */
734                 default:
735                         break;
736         }
737
738         /* Prevent windows from vanishing completely */
739         if ((int32_t)(destination.x + destination.width - 5) <= (int32_t)screen->x ||
740             (int32_t)(destination.x + 5) >= (int32_t)(screen->x + screen->width) ||
741             (int32_t)(destination.y + destination.height - 5) <= (int32_t)screen->y ||
742             (int32_t)(destination.y + 5) >= (int32_t)(screen->y + screen->height)) {
743                 DLOG("boundary check failed, not moving\n");
744                 return;
745         }
746
747         currently_focused->rect = destination;
748         reposition_client(conn, currently_focused);
749
750         /* Because reposition_client does not send a faked configure event (only resize does),
751          * we need to initiate that on our own */
752         fake_absolute_configure_notify(conn, currently_focused);
753         /* fake_absolute_configure_notify flushes */
754 }
755
756 /*
757  * Hides all floating clients (or show them if they are currently hidden) on
758  * the specified workspace.
759  *
760  */
761 void floating_toggle_hide(xcb_connection_t *conn, Workspace *workspace) {
762         Client *client;
763
764         workspace->floating_hidden = !workspace->floating_hidden;
765         DLOG("floating_hidden is now: %d\n", workspace->floating_hidden);
766         TAILQ_FOREACH(client, &(workspace->floating_clients), floating_clients) {
767                 if (workspace->floating_hidden)
768                         client_unmap(conn, client);
769                 else client_map(conn, client);
770         }
771
772         /* If we just unmapped all floating windows we should ensure that the focus
773          * is set correctly, that ist, to the first non-floating client in stack */
774         if (workspace->floating_hidden)
775                 SLIST_FOREACH(client, &(workspace->focus_stack), focus_clients) {
776                         if (client_is_floating(client))
777                                 continue;
778                         set_focus(conn, client, true);
779                         return;
780                 }
781
782         xcb_flush(conn);
783 }
784 #endif