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