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