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