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