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