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