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