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