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