]> git.sur5r.net Git - i3/i3/blob - src/floating.c
Merge branch 'master' into next
[i3/i3] / src / floating.c
1 /*
2  * vim:ts=4:sw=4:expandtab
3  *
4  * i3 - an improved dynamic tiling window manager
5  *
6  * © 2009-2010 Michael Stapelberg and contributors
7  *
8  * See file LICENSE for license information.
9  *
10  * src/floating.c: contains all functions for handling floating clients
11  *
12  */
13
14
15 #include "all.h"
16
17 extern xcb_connection_t *conn;
18
19 void floating_enable(Con *con, bool automatic) {
20     bool set_focus = (con == focused);
21
22     if (con->parent && con->parent->type == CT_DOCKAREA) {
23         LOG("Container is a dock window, not enabling floating mode.\n");
24         return;
25     }
26
27     if (con_is_floating(con)) {
28         LOG("Container is already in floating mode, not doing anything.\n");
29         return;
30     }
31
32     /* 1: If the container is a workspace container, we need to create a new
33      * split-container with the same orientation and make that one floating. We
34      * cannot touch the workspace container itself because floating containers
35      * are children of the workspace. */
36     if (con->type == CT_WORKSPACE) {
37         LOG("This is a workspace, creating new container around content\n");
38         if (con_num_children(con) == 0) {
39             LOG("Workspace is empty, aborting\n");
40             return;
41         }
42         /* TODO: refactor this with src/con.c:con_set_layout */
43         Con *new = con_new(NULL, NULL);
44         new->parent = con;
45         new->orientation = con->orientation;
46
47         /* since the new container will be set into floating mode directly
48          * afterwards, we need to copy the workspace rect. */
49         memcpy(&(new->rect), &(con->rect), sizeof(Rect));
50
51         Con *old_focused = TAILQ_FIRST(&(con->focus_head));
52         if (old_focused == TAILQ_END(&(con->focus_head)))
53             old_focused = NULL;
54
55         /* 4: move the existing cons of this workspace below the new con */
56         DLOG("Moving cons\n");
57         Con *child;
58         while (!TAILQ_EMPTY(&(con->nodes_head))) {
59             child = TAILQ_FIRST(&(con->nodes_head));
60             con_detach(child);
61             con_attach(child, new, true);
62         }
63
64         /* 4: attach the new split container to the workspace */
65         DLOG("Attaching new split to ws\n");
66         con_attach(new, con, false);
67
68         if (old_focused)
69             con_focus(old_focused);
70
71         con = new;
72         set_focus = false;
73     }
74
75     /* 1: detach the container from its parent */
76     /* TODO: refactor this with tree_close() */
77     TAILQ_REMOVE(&(con->parent->nodes_head), con, nodes);
78     TAILQ_REMOVE(&(con->parent->focus_head), con, focused);
79
80     con_fix_percent(con->parent);
81
82     /* 2: create a new container to render the decoration on, add
83      * it as a floating window to the workspace */
84     Con *nc = con_new(NULL, NULL);
85     /* we need to set the parent afterwards instead of passing it as an
86      * argument to con_new() because nc would be inserted into the tiling layer
87      * otherwise. */
88     Con *ws = con_get_workspace(con);
89     nc->parent = ws;
90
91     /* check if the parent container is empty and close it if so */
92     if ((con->parent->type == CT_CON || con->parent->type == CT_FLOATING_CON) && con_num_children(con->parent) == 0) {
93         DLOG("Old container empty after setting this child to floating, closing\n");
94         tree_close(con->parent, DONT_KILL_WINDOW, false);
95     }
96
97     char *name;
98     asprintf(&name, "[i3 con] floatingcon around %p", con);
99     x_set_name(nc, name);
100     free(name);
101
102     /* find the height for the decorations */
103     int deco_height = config.font.height + 5;
104
105     DLOG("Original rect: (%d, %d) with %d x %d\n", con->rect.x, con->rect.y, con->rect.width, con->rect.height);
106     Rect zero = { 0, 0, 0, 0 };
107     nc->rect = con->geometry;
108     /* If the geometry was not set (split containers), we need to determine a
109      * sensible one by combining the geometry of all children */
110     if (memcmp(&(nc->rect), &zero, sizeof(Rect)) == 0) {
111         DLOG("Geometry not set, combining children\n");
112         Con *child;
113         TAILQ_FOREACH(child, &(con->nodes_head), nodes) {
114             DLOG("child geometry: %d x %d\n", child->geometry.width, child->geometry.height);
115             nc->rect.width += child->geometry.width;
116             nc->rect.height = max(nc->rect.height, child->geometry.height);
117         }
118     }
119     /* Raise the width/height to at least 75x50 (minimum size for windows) */
120     nc->rect.width = max(nc->rect.width, 75);
121     nc->rect.height = max(nc->rect.height, 50);
122     /* add pixels for the decoration */
123     /* TODO: don’t add them when the user automatically puts new windows into
124      * 1pixel/borderless mode */
125     nc->rect.height += deco_height + 4;
126     nc->rect.width += 4;
127
128     /* Some clients (like GIMP’s color picker window) get mapped
129      * to (0, 0), so we push them to a reasonable position
130      * (centered over their leader) */
131     if (nc->rect.x == 0 && nc->rect.y == 0) {
132         Con *leader;
133         if (con->window && con->window->leader != XCB_NONE &&
134             (leader = con_by_window_id(con->window->leader)) != NULL) {
135             DLOG("Centering above leader\n");
136             nc->rect.x = leader->rect.x + (leader->rect.width / 2) - (nc->rect.width / 2);
137             nc->rect.y = leader->rect.y + (leader->rect.height / 2) - (nc->rect.height / 2);
138         } else {
139             /* center the window on workspace as fallback */
140             nc->rect.x = ws->rect.x + (ws->rect.width / 2) - (nc->rect.width / 2);
141             nc->rect.y = ws->rect.y + (ws->rect.height / 2) - (nc->rect.height / 2);
142         }
143     }
144
145     /* Sanity check: Are the coordinates on the appropriate output? If not, we
146      * need to change them */
147     Output *current_output = get_output_containing(nc->rect.x, nc->rect.y);
148     Con *correct_output = con_get_output(ws);
149     if (!current_output || current_output->con != correct_output) {
150         DLOG("This floating window is on the wrong output, fixing coordinates (currently (%d, %d))\n",
151              nc->rect.x, nc->rect.y);
152         /* Take the relative coordinates of the current output, then add them
153          * to the coordinate space of the correct output */
154         uint32_t rel_x = (nc->rect.x - (current_output ? current_output->con->rect.x : 0));
155         uint32_t rel_y = (nc->rect.y - (current_output ? current_output->con->rect.y : 0));
156         nc->rect.x = correct_output->rect.x + rel_x;
157         nc->rect.y = correct_output->rect.y + rel_y;
158     }
159
160     DLOG("Floating rect: (%d, %d) with %d x %d\n", nc->rect.x, nc->rect.y, nc->rect.width, nc->rect.height);
161     nc->orientation = NO_ORIENTATION;
162     nc->type = CT_FLOATING_CON;
163     TAILQ_INSERT_TAIL(&(ws->floating_head), nc, floating_windows);
164     TAILQ_INSERT_TAIL(&(ws->focus_head), nc, focused);
165
166     /* 3: attach the child to the new parent container */
167     con->parent = nc;
168     con->percent = 1.0;
169     con->floating = FLOATING_USER_ON;
170
171     /* 4: set the border style as specified with new_float */
172     if (automatic)
173         con->border_style = config.default_floating_border;
174
175     TAILQ_INSERT_TAIL(&(nc->nodes_head), con, nodes);
176     TAILQ_INSERT_TAIL(&(nc->focus_head), con, focused);
177
178     /* render the cons to get initial window_rect correct */
179     render_con(nc, false);
180     render_con(con, false);
181
182     if (set_focus)
183         con_focus(con);
184
185     /* Check if we need to re-assign it to a different workspace because of its
186      * coordinates and exit if that was done successfully. */
187     if (floating_maybe_reassign_ws(nc))
188         return;
189
190     /* Sanitize coordinates: Check if they are on any output */
191     if (get_output_containing(nc->rect.x, nc->rect.y) != NULL)
192         return;
193
194     ELOG("No output found at destination coordinates, centering floating window on current ws\n");
195     nc->rect.x = ws->rect.x + (ws->rect.width / 2) - (nc->rect.width / 2);
196     nc->rect.y = ws->rect.y + (ws->rect.height / 2) - (nc->rect.height / 2);
197 }
198
199 void floating_disable(Con *con, bool automatic) {
200     if (!con_is_floating(con)) {
201         LOG("Container isn't floating, not doing anything.\n");
202         return;
203     }
204
205     Con *ws = con_get_workspace(con);
206
207     /* 1: detach from parent container */
208     TAILQ_REMOVE(&(con->parent->nodes_head), con, nodes);
209     TAILQ_REMOVE(&(con->parent->focus_head), con, focused);
210
211     /* 2: kill parent container */
212     TAILQ_REMOVE(&(con->parent->parent->floating_head), con->parent, floating_windows);
213     TAILQ_REMOVE(&(con->parent->parent->focus_head), con->parent, focused);
214     tree_close(con->parent, DONT_KILL_WINDOW, false);
215
216     /* 3: re-attach to the parent of the currently focused con on the workspace
217      * this floating con was on */
218     Con *focused = con_descend_tiling_focused(ws);
219
220     /* if there is no other container on this workspace, focused will be the
221      * workspace itself */
222     if (focused->type == CT_WORKSPACE)
223         con->parent = focused;
224     else con->parent = focused->parent;
225
226     /* con_fix_percent will adjust the percent value */
227     con->percent = 0.0;
228
229     TAILQ_INSERT_TAIL(&(con->parent->nodes_head), con, nodes);
230     TAILQ_INSERT_TAIL(&(con->parent->focus_head), con, focused);
231
232     con->floating = FLOATING_USER_OFF;
233
234     con_fix_percent(con->parent);
235     // TODO: don’t influence focus handling when Con was not focused before.
236     con_focus(con);
237 }
238
239 /*
240  * Toggles floating mode for the given container.
241  *
242  * If the automatic flag is set to true, this was an automatic update by a change of the
243  * window class from the application which can be overwritten by the user.
244  *
245  */
246 void toggle_floating_mode(Con *con, bool automatic) {
247     /* see if the client is already floating */
248     if (con_is_floating(con)) {
249         LOG("already floating, re-setting to tiling\n");
250
251         floating_disable(con, automatic);
252         return;
253     }
254
255     floating_enable(con, automatic);
256 }
257
258 /*
259  * Raises the given container in the list of floating containers
260  *
261  */
262 void floating_raise_con(Con *con) {
263     DLOG("Raising floating con %p / %s\n", con, con->name);
264     TAILQ_REMOVE(&(con->parent->floating_head), con, floating_windows);
265     TAILQ_INSERT_TAIL(&(con->parent->floating_head), con, floating_windows);
266 }
267
268 /*
269  * Checks if con’s coordinates are within its workspace and re-assigns it to
270  * the actual workspace if not.
271  *
272  */
273 bool floating_maybe_reassign_ws(Con *con) {
274     Output *output = get_output_containing(
275         con->rect.x + (con->rect.width / 2),
276         con->rect.y + (con->rect.height / 2));
277
278     if (!output) {
279         ELOG("No output found at destination coordinates?\n");
280         return false;
281     }
282
283     if (con_get_output(con) == output->con) {
284         DLOG("still the same ws\n");
285         return false;
286     }
287
288     DLOG("Need to re-assign!\n");
289
290     Con *content = output_get_content(output->con);
291     Con *ws = TAILQ_FIRST(&(content->focus_head));
292     DLOG("Moving con %p / %s to workspace %p / %s\n", con, con->name, ws, ws->name);
293     con_move_to_workspace(con, ws, true);
294     con_focus(con_descend_focused(con));
295     return true;
296 }
297
298 DRAGGING_CB(drag_window_callback) {
299     struct xcb_button_press_event_t *event = extra;
300
301     /* Reposition the client correctly while moving */
302     con->rect.x = old_rect->x + (new_x - event->root_x);
303     con->rect.y = old_rect->y + (new_y - event->root_y);
304
305     render_con(con, false);
306     x_push_node(con);
307     xcb_flush(conn);
308
309     /* Check if we cross workspace boundaries while moving */
310     if (!floating_maybe_reassign_ws(con))
311         return;
312     tree_render();
313 }
314
315 /*
316  * Called when the user clicked on the titlebar of a floating window.
317  * Calls the drag_pointer function with the drag_window callback
318  *
319  */
320 void floating_drag_window(Con *con, xcb_button_press_event_t *event) {
321     DLOG("floating_drag_window\n");
322
323     /* Push changes before dragging, so that the window gets raised now and not
324      * after the user releases the mouse button */
325     tree_render();
326
327     /* Drag the window */
328     drag_pointer(con, event, XCB_NONE, BORDER_TOP /* irrelevant */, drag_window_callback, event);
329     tree_render();
330 }
331
332 /*
333  * This is an ugly data structure which we need because there is no standard
334  * way of having nested functions (only available as a gcc extension at the
335  * moment, clang doesn’t support it) or blocks (only available as a clang
336  * extension and only on Mac OS X systems at the moment).
337  *
338  */
339 struct resize_window_callback_params {
340     border_t corner;
341     bool proportional;
342     xcb_button_press_event_t *event;
343 };
344
345 DRAGGING_CB(resize_window_callback) {
346     struct resize_window_callback_params *params = extra;
347     xcb_button_press_event_t *event = params->event;
348     border_t corner = params->corner;
349
350     int32_t dest_x = con->rect.x;
351     int32_t dest_y = con->rect.y;
352     uint32_t dest_width;
353     uint32_t dest_height;
354
355     double ratio = (double) old_rect->width / old_rect->height;
356
357     /* First guess: We resize by exactly the amount the mouse moved,
358      * taking into account in which corner the client was grabbed */
359     if (corner & BORDER_LEFT)
360         dest_width = old_rect->width - (new_x - event->root_x);
361     else dest_width = old_rect->width + (new_x - event->root_x);
362
363     if (corner & BORDER_TOP)
364         dest_height = old_rect->height - (new_y - event->root_y);
365     else dest_height = old_rect->height + (new_y - event->root_y);
366
367     /* Obey minimum window size */
368     Rect minimum = con_minimum_size(con);
369     dest_width = max(dest_width, minimum.width);
370     dest_height = max(dest_height, minimum.height);
371
372     /* User wants to keep proportions, so we may have to adjust our values */
373     if (params->proportional) {
374         dest_width = max(dest_width, (int) (dest_height * ratio));
375         dest_height = max(dest_height, (int) (dest_width / ratio));
376     }
377
378     /* If not the lower right corner is grabbed, we must also reposition
379      * the client by exactly the amount we resized it */
380     if (corner & BORDER_LEFT)
381         dest_x = old_rect->x + (old_rect->width - dest_width);
382
383     if (corner & BORDER_TOP)
384         dest_y = old_rect->y + (old_rect->height - dest_height);
385
386     con->rect = (Rect) { dest_x, dest_y, dest_width, dest_height };
387
388     /* TODO: don’t re-render the whole tree just because we change
389      * coordinates of a floating window */
390     tree_render();
391     x_push_changes(croot);
392 }
393
394 /*
395  * Called when the user clicked on a floating window while holding the
396  * floating_modifier and the right mouse button.
397  * Calls the drag_pointer function with the resize_window callback
398  *
399  */
400 void floating_resize_window(Con *con, bool proportional,
401                             xcb_button_press_event_t *event) {
402     DLOG("floating_resize_window\n");
403
404     /* corner saves the nearest corner to the original click. It contains
405      * a bitmask of the nearest borders (BORDER_LEFT, BORDER_RIGHT, …) */
406     border_t corner = 0;
407
408     if (event->event_x <= (con->rect.width / 2))
409         corner |= BORDER_LEFT;
410     else corner |= BORDER_RIGHT;
411
412     if (event->event_y <= (con->rect.height / 2))
413         corner |= BORDER_TOP;
414     else corner |= BORDER_BOTTOM;
415
416     struct resize_window_callback_params params = { corner, proportional, event };
417
418     drag_pointer(con, event, XCB_NONE, BORDER_TOP /* irrelevant */, resize_window_callback, &params);
419 }
420
421 /*
422  * This function grabs your pointer and lets you drag stuff around (borders).
423  * Every time you move your mouse, an XCB_MOTION_NOTIFY event will be received
424  * and the given callback will be called with the parameters specified (client,
425  * border on which the click originally was), the original rect of the client,
426  * the event and the new coordinates (x, y).
427  *
428  */
429 void drag_pointer(Con *con, xcb_button_press_event_t *event, xcb_window_t
430                 confine_to, border_t border, callback_t callback, void *extra)
431 {
432     uint32_t new_x, new_y;
433     Rect old_rect;
434     if (con != NULL)
435         memcpy(&old_rect, &(con->rect), sizeof(Rect));
436
437     /* Grab the pointer */
438     xcb_grab_pointer_cookie_t cookie;
439     xcb_grab_pointer_reply_t *reply;
440     cookie = xcb_grab_pointer(conn,
441         false,               /* get all pointer events specified by the following mask */
442         root,                /* grab the root window */
443         XCB_EVENT_MASK_BUTTON_RELEASE | XCB_EVENT_MASK_POINTER_MOTION, /* which events to let through */
444         XCB_GRAB_MODE_ASYNC, /* pointer events should continue as normal */
445         XCB_GRAB_MODE_ASYNC, /* keyboard mode */
446         confine_to,          /* confine_to = in which window should the cursor stay */
447         XCB_NONE,            /* don’t display a special cursor */
448         XCB_CURRENT_TIME);
449
450     if ((reply = xcb_grab_pointer_reply(conn, cookie, NULL)) == NULL) {
451         ELOG("Could not grab pointer\n");
452         return;
453     }
454
455     free(reply);
456
457     /* Go into our own event loop */
458     xcb_flush(conn);
459
460     xcb_generic_event_t *inside_event, *last_motion_notify = NULL;
461     bool loop_done = false;
462     /* I’ve always wanted to have my own eventhandler… */
463     while (!loop_done && (inside_event = xcb_wait_for_event(conn))) {
464         /* We now handle all events we can get using xcb_poll_for_event */
465         do {
466             /* skip x11 errors */
467             if (inside_event->response_type == 0) {
468                 free(inside_event);
469                 continue;
470             }
471             /* Strip off the highest bit (set if the event is generated) */
472             int type = (inside_event->response_type & 0x7F);
473
474             switch (type) {
475                 case XCB_BUTTON_RELEASE:
476                     loop_done = true;
477                     break;
478
479                 case XCB_MOTION_NOTIFY:
480                     /* motion_notify events are saved for later */
481                     FREE(last_motion_notify);
482                     last_motion_notify = inside_event;
483                     break;
484
485                 case XCB_UNMAP_NOTIFY:
486                 case XCB_KEY_PRESS:
487                 case XCB_KEY_RELEASE:
488                     DLOG("Unmap-notify, aborting\n");
489                     handle_event(type, inside_event);
490                     loop_done = true;
491                     break;
492
493                 default:
494                     DLOG("Passing to original handler\n");
495                     /* Use original handler */
496                     handle_event(type, inside_event);
497                     break;
498             }
499             if (last_motion_notify != inside_event)
500                 free(inside_event);
501         } while ((inside_event = xcb_poll_for_event(conn)) != NULL);
502
503         if (last_motion_notify == NULL)
504             continue;
505
506         new_x = ((xcb_motion_notify_event_t*)last_motion_notify)->root_x;
507         new_y = ((xcb_motion_notify_event_t*)last_motion_notify)->root_y;
508
509         callback(con, &old_rect, new_x, new_y, extra);
510         FREE(last_motion_notify);
511     }
512
513     xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
514     xcb_flush(conn);
515 }
516
517 #if 0
518 /*
519  * Changes focus in the given direction for floating clients.
520  *
521  * Changing to the left/right means going to the previous/next floating client,
522  * changing to top/bottom means cycling through the Z-index.
523  *
524  */
525 void floating_focus_direction(xcb_connection_t *conn, Client *currently_focused, direction_t direction) {
526         DLOG("floating focus\n");
527
528         if (direction == D_LEFT || direction == D_RIGHT) {
529                 /* Go to the next/previous floating client */
530                 Client *client;
531
532                 while ((client = (direction == D_LEFT ? TAILQ_PREV(currently_focused, floating_clients_head, floating_clients) :
533                                                         TAILQ_NEXT(currently_focused, floating_clients))) !=
534                        TAILQ_END(&(currently_focused->workspace->floating_clients))) {
535                         if (!client->floating)
536                                 continue;
537                         set_focus(conn, client, true);
538                         return;
539                 }
540         }
541 }
542
543 /*
544  * Moves the client 10px to the specified direction.
545  *
546  */
547 void floating_move(xcb_connection_t *conn, Client *currently_focused, direction_t direction) {
548         DLOG("floating move\n");
549
550         Rect destination = currently_focused->rect;
551         Rect *screen = &(currently_focused->workspace->output->rect);
552
553         switch (direction) {
554                 case D_LEFT:
555                         destination.x -= 10;
556                         break;
557                 case D_RIGHT:
558                         destination.x += 10;
559                         break;
560                 case D_UP:
561                         destination.y -= 10;
562                         break;
563                 case D_DOWN:
564                         destination.y += 10;
565                         break;
566                 /* to make static analyzers happy */
567                 default:
568                         break;
569         }
570
571         /* Prevent windows from vanishing completely */
572         if ((int32_t)(destination.x + destination.width - 5) <= (int32_t)screen->x ||
573             (int32_t)(destination.x + 5) >= (int32_t)(screen->x + screen->width) ||
574             (int32_t)(destination.y + destination.height - 5) <= (int32_t)screen->y ||
575             (int32_t)(destination.y + 5) >= (int32_t)(screen->y + screen->height)) {
576                 DLOG("boundary check failed, not moving\n");
577                 return;
578         }
579
580         currently_focused->rect = destination;
581         reposition_client(conn, currently_focused);
582
583         /* Because reposition_client does not send a faked configure event (only resize does),
584          * we need to initiate that on our own */
585         fake_absolute_configure_notify(conn, currently_focused);
586         /* fake_absolute_configure_notify flushes */
587 }
588
589 /*
590  * Hides all floating clients (or show them if they are currently hidden) on
591  * the specified workspace.
592  *
593  */
594 void floating_toggle_hide(xcb_connection_t *conn, Workspace *workspace) {
595         Client *client;
596
597         workspace->floating_hidden = !workspace->floating_hidden;
598         DLOG("floating_hidden is now: %d\n", workspace->floating_hidden);
599         TAILQ_FOREACH(client, &(workspace->floating_clients), floating_clients) {
600                 if (workspace->floating_hidden)
601                         client_unmap(conn, client);
602                 else client_map(conn, client);
603         }
604
605         /* If we just unmapped all floating windows we should ensure that the focus
606          * is set correctly, that ist, to the first non-floating client in stack */
607         if (workspace->floating_hidden)
608                 SLIST_FOREACH(client, &(workspace->focus_stack), focus_clients) {
609                         if (client_is_floating(client))
610                                 continue;
611                         set_focus(conn, client, true);
612                         return;
613                 }
614
615         xcb_flush(conn);
616 }
617 #endif