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