]> git.sur5r.net Git - i3/i3/blob - src/floating.c
b1e23785a67c274614b98856fd93e56895458a31
[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     if (con_is_floating(con)) {
21         LOG("Container is already in floating mode, not doing anything.\n");
22         return;
23     }
24
25     /* 1: detach the container from its parent */
26     /* TODO: refactor this with tree_close() */
27     TAILQ_REMOVE(&(con->parent->nodes_head), con, nodes);
28     TAILQ_REMOVE(&(con->parent->focus_head), con, focused);
29
30     con_fix_percent(con->parent, WINDOW_REMOVE);
31
32     /* 2: create a new container to render the decoration on, add
33      * it as a floating window to the workspace */
34     Con *nc = con_new(NULL);
35     /* we need to set the parent afterwards instead of passing it as an
36      * argument to con_new() because nc would be inserted into the tiling layer
37      * otherwise. */
38     nc->parent = con_get_workspace(con);
39
40     char *name;
41     asprintf(&name, "[i3 con] floatingcon around %p", con);
42     x_set_name(nc, name);
43     free(name);
44
45     /* find the height for the decorations */
46     i3Font *font = load_font(conn, config.font);
47     int deco_height = font->height + 5;
48
49     nc->rect = con->rect;
50     /* add pixels for the decoration */
51     /* TODO: don’t add them when the user automatically puts new windows into
52      * 1pixel/borderless mode */
53     nc->rect.height += deco_height + 4;
54     nc->rect.width += 4;
55     nc->orientation = NO_ORIENTATION;
56     nc->type = CT_FLOATING_CON;
57     TAILQ_INSERT_TAIL(&(nc->parent->floating_head), nc, floating_windows);
58     TAILQ_INSERT_TAIL(&(nc->parent->focus_head), nc, focused);
59
60     /* 3: attach the child to the new parent container */
61     con->old_parent = con->parent;
62     con->parent = nc;
63     con->floating = FLOATING_USER_ON;
64
65     /* Some clients (like GIMP’s color picker window) get mapped
66      * to (0, 0), so we push them to a reasonable position
67      * (centered over their leader) */
68     if (nc->rect.x == 0 && nc->rect.y == 0) {
69         Con *leader;
70         if (con->window && con->window->leader != XCB_NONE &&
71             (leader = con_by_window_id(con->window->leader)) != NULL) {
72             DLOG("Centering above leader\n");
73             nc->rect.x = leader->rect.x + (leader->rect.width / 2) - (nc->rect.width / 2);
74             nc->rect.y = leader->rect.y + (leader->rect.height / 2) - (nc->rect.height / 2);
75         } else {
76             /* center the window on workspace as fallback */
77             Con *ws = nc->parent;
78             nc->rect.x = ws->rect.x + (ws->rect.width / 2) - (nc->rect.width / 2);
79             nc->rect.y = ws->rect.y + (ws->rect.height / 2) - (nc->rect.height / 2);
80         }
81     }
82
83     TAILQ_INSERT_TAIL(&(nc->nodes_head), con, nodes);
84     TAILQ_INSERT_TAIL(&(nc->focus_head), con, focused);
85     // TODO: don’t influence focus handling when Con was not focused before.
86     con_focus(con);
87 }
88
89 void floating_disable(Con *con, bool automatic) {
90     if (!con_is_floating(con)) {
91         LOG("Container isn't floating, not doing anything.\n");
92         return;
93     }
94
95     assert(con->old_parent != NULL);
96
97     /* 1: detach from parent container */
98     TAILQ_REMOVE(&(con->parent->nodes_head), con, nodes);
99     TAILQ_REMOVE(&(con->parent->focus_head), con, focused);
100
101     /* 2: kill parent container */
102     TAILQ_REMOVE(&(con->parent->parent->floating_head), con->parent, floating_windows);
103     TAILQ_REMOVE(&(con->parent->parent->focus_head), con->parent, focused);
104     tree_close(con->parent, false, false);
105
106     /* 3: re-attach to previous parent */
107     con->parent = con->old_parent;
108     TAILQ_INSERT_TAIL(&(con->parent->nodes_head), con, nodes);
109     TAILQ_INSERT_TAIL(&(con->parent->focus_head), con, focused);
110
111     con->floating = FLOATING_USER_OFF;
112
113     con_fix_percent(con->parent, WINDOW_ADD);
114     // TODO: don’t influence focus handling when Con was not focused before.
115     con_focus(con);
116 }
117
118
119 /*
120  * Toggles floating mode for the given container.
121  *
122  * If the automatic flag is set to true, this was an automatic update by a change of the
123  * window class from the application which can be overwritten by the user.
124  *
125  */
126 void toggle_floating_mode(Con *con, bool automatic) {
127         //i3Font *font = load_font(conn, config.font);
128
129         /* see if the client is already floating */
130         if (con_is_floating(con)) {
131                 LOG("already floating, re-setting to tiling\n");
132
133                 floating_disable(con, automatic);
134                 return;
135         }
136
137         floating_enable(con, automatic);
138
139
140 #if 0
141         if (client->dock) {
142                 DLOG("Not putting dock client into floating mode\n");
143                 return;
144         }
145
146         if (con == NULL) {
147                 DLOG("This client is already in floating (container == NULL), re-inserting\n");
148                 Client *next_tiling;
149                 Workspace *ws = client->workspace;
150                 SLIST_FOREACH(next_tiling, &(ws->focus_stack), focus_clients)
151                         if (!client_is_floating(next_tiling))
152                                 break;
153                 /* If there are no tiling clients on this workspace, there can only be one
154                  * container: the first one */
155                 if (next_tiling == TAILQ_END(&(ws->focus_stack)))
156                         con = ws->table[0][0];
157                 else con = next_tiling->container;
158
159                 /* Remove the client from the list of floating clients */
160                 TAILQ_REMOVE(&(ws->floating_clients), client, floating_clients);
161
162                 DLOG("destination container = %p\n", con);
163                 Client *old_focused = con->currently_focused;
164                 /* Preserve position/size */
165                 memcpy(&(client->floating_rect), &(client->rect), sizeof(Rect));
166
167                 client->floating = FLOATING_USER_OFF;
168                 client->container = con;
169
170                 if (old_focused != NULL && !old_focused->dock)
171                         CIRCLEQ_INSERT_AFTER(&(con->clients), old_focused, client, clients);
172                 else CIRCLEQ_INSERT_TAIL(&(con->clients), client, clients);
173
174                 DLOG("Re-inserted the window.\n");
175                 con->currently_focused = client;
176
177                 client_set_below_floating(conn, client);
178
179                 render_container(conn, con);
180                 xcb_flush(conn);
181
182                 return;
183         }
184
185         DLOG("Entering floating for client %08x\n", client->child);
186
187         /* Remove the client of its container */
188         client_remove_from_container(conn, client, con, false);
189         client->container = NULL;
190
191         /* Add the client to the list of floating clients for its workspace */
192         TAILQ_INSERT_TAIL(&(client->workspace->floating_clients), client, floating_clients);
193
194         if (con->currently_focused == client) {
195                 DLOG("Need to re-adjust currently_focused\n");
196                 /* Get the next client in the focus stack for this particular container */
197                 con->currently_focused = get_last_focused_client(conn, con, NULL);
198         }
199
200         if (automatic)
201                 client->floating = FLOATING_AUTO_ON;
202         else client->floating = FLOATING_USER_ON;
203
204         /* Initialize the floating position from the position in tiling mode, if this
205          * client never was floating (x == -1) */
206         if (client->floating_rect.x == -1) {
207                 /* Copy over the position */
208                 client->floating_rect.x = client->rect.x;
209                 client->floating_rect.y = client->rect.y;
210
211                 /* Copy size the other direction */
212                 client->child_rect.width = client->floating_rect.width;
213                 client->child_rect.height = client->floating_rect.height;
214
215                 client->rect.width = client->child_rect.width + 2 + 2;
216                 client->rect.height = client->child_rect.height + (font->height + 2 + 2) + 2;
217
218                 DLOG("copying size from tiling (%d, %d) size (%d, %d)\n", client->floating_rect.x, client->floating_rect.y,
219                                 client->floating_rect.width, client->floating_rect.height);
220         } else {
221                 /* If the client was already in floating before we restore the old position / size */
222                 DLOG("using: (%d, %d) size (%d, %d)\n", client->floating_rect.x, client->floating_rect.y,
223                         client->floating_rect.width, client->floating_rect.height);
224                 memcpy(&(client->rect), &(client->floating_rect), sizeof(Rect));
225         }
226
227         /* Raise the client */
228         xcb_raise_window(conn, client->frame);
229         reposition_client(conn, client);
230         resize_client(conn, client);
231         /* redecorate_window flushes */
232         redecorate_window(conn, client);
233
234         /* Re-render the tiling layout of this container */
235         render_container(conn, con);
236         xcb_flush(conn);
237 #endif
238 }
239
240 #if 0
241 /*
242  * Removes the floating client from its workspace and attaches it to the new workspace.
243  * This is centralized here because it may happen if you move it via keyboard and
244  * if you move it using your mouse.
245  *
246  */
247 void floating_assign_to_workspace(Client *client, Workspace *new_workspace) {
248         /* Remove from focus stack and list of floating clients */
249         SLIST_REMOVE(&(client->workspace->focus_stack), client, Client, focus_clients);
250         TAILQ_REMOVE(&(client->workspace->floating_clients), client, floating_clients);
251
252         if (client->workspace->fullscreen_client == client)
253                 client->workspace->fullscreen_client = NULL;
254
255         /* Insert into destination focus stack and list of floating clients */
256         client->workspace = new_workspace;
257         SLIST_INSERT_HEAD(&(client->workspace->focus_stack), client, focus_clients);
258         TAILQ_INSERT_TAIL(&(client->workspace->floating_clients), client, floating_clients);
259         if (client->fullscreen)
260                 client->workspace->fullscreen_client = client;
261 }
262
263 /*
264  * This is an ugly data structure which we need because there is no standard
265  * way of having nested functions (only available as a gcc extension at the
266  * moment, clang doesn’t support it) or blocks (only available as a clang
267  * extension and only on Mac OS X systems at the moment).
268  *
269  */
270 struct resize_callback_params {
271         border_t border;
272         xcb_button_press_event_t *event;
273 };
274
275 DRAGGING_CB(resize_callback) {
276         struct resize_callback_params *params = extra;
277         xcb_button_press_event_t *event = params->event;
278         switch (params->border) {
279                 case BORDER_RIGHT: {
280                         int new_width = old_rect->width + (new_x - event->root_x);
281                         if ((new_width < 0) ||
282                             (new_width < client_min_width(client) && client->rect.width >= new_width))
283                                 return;
284                         client->rect.width = new_width;
285                         break;
286                 }
287
288                 case BORDER_BOTTOM: {
289                         int new_height = old_rect->height + (new_y - event->root_y);
290                         if ((new_height < 0) ||
291                             (new_height < client_min_height(client) && client->rect.height >= new_height))
292                                 return;
293                         client->rect.height = old_rect->height + (new_y - event->root_y);
294                         break;
295                 }
296
297                 case BORDER_TOP: {
298                         int new_height = old_rect->height + (event->root_y - new_y);
299                         if ((new_height < 0) ||
300                             (new_height < client_min_height(client) && client->rect.height >= new_height))
301                                 return;
302
303                         client->rect.y = old_rect->y + (new_y - event->root_y);
304                         client->rect.height = new_height;
305                         break;
306                 }
307
308                 case BORDER_LEFT: {
309                         int new_width = old_rect->width + (event->root_x - new_x);
310                         if ((new_width < 0) ||
311                             (new_width < client_min_width(client) && client->rect.width >= new_width))
312                                 return;
313                         client->rect.x = old_rect->x + (new_x - event->root_x);
314                         client->rect.width = new_width;
315                         break;
316                 }
317         }
318
319         /* Push the new position/size to X11 */
320         reposition_client(conn, client);
321         resize_client(conn, client);
322         xcb_flush(conn);
323 }
324
325
326 /*
327  * Called whenever the user clicks on a border (not the titlebar!) of a floating window.
328  * Determines on which border the user clicked and launches the drag_pointer function
329  * with the resize_callback.
330  *
331  */
332 int floating_border_click(xcb_connection_t *conn, Client *client, xcb_button_press_event_t *event) {
333         DLOG("floating border click\n");
334
335         border_t border;
336
337         if (event->event_y < 2)
338                 border = BORDER_TOP;
339         else if (event->event_y >= (client->rect.height - 2))
340                 border = BORDER_BOTTOM;
341         else if (event->event_x <= 2)
342                 border = BORDER_LEFT;
343         else if (event->event_x >= (client->rect.width - 2))
344                 border = BORDER_RIGHT;
345         else {
346                 DLOG("Not on any border, not doing anything.\n");
347                 return 1;
348         }
349
350         DLOG("border = %d\n", border);
351
352         struct resize_callback_params params = { border, event };
353
354         drag_pointer(conn, client, event, XCB_NONE, border, resize_callback, &params);
355
356         return 1;
357 }
358
359 #endif
360 DRAGGING_CB(drag_window_callback) {
361         struct xcb_button_press_event_t *event = extra;
362
363         /* Reposition the client correctly while moving */
364         con->rect.x = old_rect->x + (new_x - event->root_x);
365         con->rect.y = old_rect->y + (new_y - event->root_y);
366         /* TODO: don’t re-render the whole tree just because we change
367          * coordinates of a floating window */
368         tree_render();
369         x_push_changes(croot);
370 }
371
372 /*
373  * Called when the user clicked on the titlebar of a floating window.
374  * Calls the drag_pointer function with the drag_window callback
375  *
376  */
377 void floating_drag_window(Con *con, xcb_button_press_event_t *event) {
378         DLOG("floating_drag_window\n");
379
380         drag_pointer(con, event, XCB_NONE, BORDER_TOP /* irrelevant */, drag_window_callback, event);
381         tree_render();
382 }
383
384 /*
385  * This is an ugly data structure which we need because there is no standard
386  * way of having nested functions (only available as a gcc extension at the
387  * moment, clang doesn’t support it) or blocks (only available as a clang
388  * extension and only on Mac OS X systems at the moment).
389  *
390  */
391 struct resize_window_callback_params {
392     border_t corner;
393     bool proportional;
394     xcb_button_press_event_t *event;
395 };
396
397 DRAGGING_CB(resize_window_callback) {
398     struct resize_window_callback_params *params = extra;
399     xcb_button_press_event_t *event = params->event;
400     border_t corner = params->corner;
401
402     int32_t dest_x = con->rect.x;
403     int32_t dest_y = con->rect.y;
404     uint32_t dest_width;
405     uint32_t dest_height;
406
407     double ratio = (double) old_rect->width / old_rect->height;
408
409     /* First guess: We resize by exactly the amount the mouse moved,
410      * taking into account in which corner the client was grabbed */
411     if (corner & BORDER_LEFT)
412             dest_width = old_rect->width - (new_x - event->root_x);
413     else dest_width = old_rect->width + (new_x - event->root_x);
414
415     if (corner & BORDER_TOP)
416             dest_height = old_rect->height - (new_y - event->root_y);
417     else dest_height = old_rect->height + (new_y - event->root_y);
418
419     /* TODO: minimum window size */
420 #if 0
421     /* Obey minimum window size */
422     dest_width = max(dest_width, client_min_width(client));
423     dest_height = max(dest_height, client_min_height(client));
424 #endif
425
426     /* User wants to keep proportions, so we may have to adjust our values */
427     if (params->proportional) {
428             dest_width = max(dest_width, (int) (dest_height * ratio));
429             dest_height = max(dest_height, (int) (dest_width / ratio));
430     }
431
432     /* If not the lower right corner is grabbed, we must also reposition
433      * the client by exactly the amount we resized it */
434     if (corner & BORDER_LEFT)
435             dest_x = old_rect->x + (old_rect->width - dest_width);
436
437     if (corner & BORDER_TOP)
438             dest_y = old_rect->y + (old_rect->height - dest_height);
439
440     con->rect = (Rect) { dest_x, dest_y, dest_width, dest_height };
441
442     /* TODO: don’t re-render the whole tree just because we change
443      * coordinates of a floating window */
444     tree_render();
445     x_push_changes(croot);
446 }
447
448 /*
449  * Called when the user clicked on a floating window while holding the
450  * floating_modifier and the right mouse button.
451  * Calls the drag_pointer function with the resize_window callback
452  *
453  */
454 void floating_resize_window(Con *con, bool proportional,
455                             xcb_button_press_event_t *event) {
456     DLOG("floating_resize_window\n");
457
458     /* corner saves the nearest corner to the original click. It contains
459      * a bitmask of the nearest borders (BORDER_LEFT, BORDER_RIGHT, …) */
460     border_t corner = 0;
461
462     if (event->event_x <= (con->rect.width / 2))
463             corner |= BORDER_LEFT;
464     else corner |= BORDER_RIGHT;
465
466     if (event->event_y <= (con->rect.height / 2))
467             corner |= BORDER_TOP;
468     else corner |= BORDER_RIGHT;
469
470     struct resize_window_callback_params params = { corner, proportional, event };
471
472     drag_pointer(con, event, XCB_NONE, BORDER_TOP /* irrelevant */, resize_window_callback, &params);
473 }
474
475 /*
476  * This function grabs your pointer and lets you drag stuff around (borders).
477  * Every time you move your mouse, an XCB_MOTION_NOTIFY event will be received
478  * and the given callback will be called with the parameters specified (client,
479  * border on which the click originally was), the original rect of the client,
480  * the event and the new coordinates (x, y).
481  *
482  */
483 void drag_pointer(Con *con, xcb_button_press_event_t *event, xcb_window_t
484                 confine_to, border_t border, callback_t callback, void *extra)
485 {
486         xcb_window_t root = xcb_setup_roots_iterator(xcb_get_setup(conn)).data->root;
487         uint32_t new_x, new_y;
488         Rect old_rect;
489         if (con != NULL)
490                 memcpy(&old_rect, &(con->rect), sizeof(Rect));
491
492         /* Grab the pointer */
493         /* TODO: returncode */
494         xcb_grab_pointer(conn, 
495                         false,               /* get all pointer events specified by the following mask */
496                         root,                /* grab the root window */
497                         XCB_EVENT_MASK_BUTTON_RELEASE | XCB_EVENT_MASK_POINTER_MOTION, /* which events to let through */
498                         XCB_GRAB_MODE_ASYNC, /* pointer events should continue as normal */
499                         XCB_GRAB_MODE_ASYNC, /* keyboard mode */
500                         confine_to,          /* confine_to = in which window should the cursor stay */
501                         XCB_NONE,            /* don’t display a special cursor */
502                         XCB_CURRENT_TIME);
503
504         /* Go into our own event loop */
505         xcb_flush(conn);
506
507         xcb_generic_event_t *inside_event, *last_motion_notify = NULL;
508         /* I’ve always wanted to have my own eventhandler… */
509         while ((inside_event = xcb_wait_for_event(conn))) {
510                 /* We now handle all events we can get using xcb_poll_for_event */
511                 do {
512                         /* Same as get_event_handler in xcb */
513                         int nr = inside_event->response_type;
514                         if (nr == 0) {
515                                 /* An error occured */
516                                 //handle_event(NULL, conn, inside_event);
517                                 free(inside_event);
518                                 continue;
519                         }
520                         assert(nr < 256);
521                         nr &= XCB_EVENT_RESPONSE_TYPE_MASK;
522                         assert(nr >= 2);
523
524                         switch (nr) {
525                                 case XCB_BUTTON_RELEASE:
526                                         goto done;
527
528                                 case XCB_MOTION_NOTIFY:
529                                         /* motion_notify events are saved for later */
530                                         FREE(last_motion_notify);
531                                         last_motion_notify = inside_event;
532                                         break;
533
534                                 case XCB_UNMAP_NOTIFY:
535                                         DLOG("Unmap-notify, aborting\n");
536                                         xcb_event_handle(&evenths, inside_event);
537                                         goto done;
538
539                                 default:
540                                         DLOG("Passing to original handler\n");
541                                         /* Use original handler */
542                                         xcb_event_handle(&evenths, inside_event);
543                                         break;
544                         }
545                         if (last_motion_notify != inside_event)
546                                 free(inside_event);
547                 } while ((inside_event = xcb_poll_for_event(conn)) != NULL);
548
549                 if (last_motion_notify == NULL)
550                         continue;
551
552                 new_x = ((xcb_motion_notify_event_t*)last_motion_notify)->root_x;
553                 new_y = ((xcb_motion_notify_event_t*)last_motion_notify)->root_y;
554
555                 callback(con, &old_rect, new_x, new_y, extra);
556                 FREE(last_motion_notify);
557         }
558 done:
559         xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
560         xcb_flush(conn);
561 }
562
563 #if 0
564 /*
565  * Changes focus in the given direction for floating clients.
566  *
567  * Changing to the left/right means going to the previous/next floating client,
568  * changing to top/bottom means cycling through the Z-index.
569  *
570  */
571 void floating_focus_direction(xcb_connection_t *conn, Client *currently_focused, direction_t direction) {
572         DLOG("floating focus\n");
573
574         if (direction == D_LEFT || direction == D_RIGHT) {
575                 /* Go to the next/previous floating client */
576                 Client *client;
577
578                 while ((client = (direction == D_LEFT ? TAILQ_PREV(currently_focused, floating_clients_head, floating_clients) :
579                                                         TAILQ_NEXT(currently_focused, floating_clients))) !=
580                        TAILQ_END(&(currently_focused->workspace->floating_clients))) {
581                         if (!client->floating)
582                                 continue;
583                         set_focus(conn, client, true);
584                         return;
585                 }
586         }
587 }
588
589 /*
590  * Moves the client 10px to the specified direction.
591  *
592  */
593 void floating_move(xcb_connection_t *conn, Client *currently_focused, direction_t direction) {
594         DLOG("floating move\n");
595
596         Rect destination = currently_focused->rect;
597         Rect *screen = &(currently_focused->workspace->output->rect);
598
599         switch (direction) {
600                 case D_LEFT:
601                         destination.x -= 10;
602                         break;
603                 case D_RIGHT:
604                         destination.x += 10;
605                         break;
606                 case D_UP:
607                         destination.y -= 10;
608                         break;
609                 case D_DOWN:
610                         destination.y += 10;
611                         break;
612                 /* to make static analyzers happy */
613                 default:
614                         break;
615         }
616
617         /* Prevent windows from vanishing completely */
618         if ((int32_t)(destination.x + destination.width - 5) <= (int32_t)screen->x ||
619             (int32_t)(destination.x + 5) >= (int32_t)(screen->x + screen->width) ||
620             (int32_t)(destination.y + destination.height - 5) <= (int32_t)screen->y ||
621             (int32_t)(destination.y + 5) >= (int32_t)(screen->y + screen->height)) {
622                 DLOG("boundary check failed, not moving\n");
623                 return;
624         }
625
626         currently_focused->rect = destination;
627         reposition_client(conn, currently_focused);
628
629         /* Because reposition_client does not send a faked configure event (only resize does),
630          * we need to initiate that on our own */
631         fake_absolute_configure_notify(conn, currently_focused);
632         /* fake_absolute_configure_notify flushes */
633 }
634
635 /*
636  * Hides all floating clients (or show them if they are currently hidden) on
637  * the specified workspace.
638  *
639  */
640 void floating_toggle_hide(xcb_connection_t *conn, Workspace *workspace) {
641         Client *client;
642
643         workspace->floating_hidden = !workspace->floating_hidden;
644         DLOG("floating_hidden is now: %d\n", workspace->floating_hidden);
645         TAILQ_FOREACH(client, &(workspace->floating_clients), floating_clients) {
646                 if (workspace->floating_hidden)
647                         client_unmap(conn, client);
648                 else client_map(conn, client);
649         }
650
651         /* If we just unmapped all floating windows we should ensure that the focus
652          * is set correctly, that ist, to the first non-floating client in stack */
653         if (workspace->floating_hidden)
654                 SLIST_FOREACH(client, &(workspace->focus_stack), focus_clients) {
655                         if (client_is_floating(client))
656                                 continue;
657                         set_focus(conn, client, true);
658                         return;
659                 }
660
661         xcb_flush(conn);
662 }
663 #endif