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