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