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