]> git.sur5r.net Git - i3/i3/blob - src/floating.c
floating: enforce minimum size of 50x20 when resizing (Thanks Mirko)
[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
29 /* On which border was the dragging initiated? */
30 typedef enum { BORDER_LEFT, BORDER_RIGHT, BORDER_TOP, BORDER_BOTTOM} border_t;
31 /* Callback for dragging */
32 typedef void(*callback_t)(Rect*, uint32_t, uint32_t);
33
34 /* Forward definitions */
35 static void drag_pointer(xcb_connection_t *conn, Client *client, xcb_button_press_event_t *event,
36                          border_t border, callback_t callback);
37
38 /*
39  * Toggles floating mode for the given client.
40  * Correctly takes care of the position/size (separately stored for tiling/floating mode)
41  * and repositions/resizes/redecorates the client.
42  *
43  * If the automatic flag is set to true, this was an automatic update by a change of the
44  * window class from the application which can be overwritten by the user.
45  *
46  */
47 void toggle_floating_mode(xcb_connection_t *conn, Client *client, bool automatic) {
48         Container *con = client->container;
49         i3Font *font = load_font(conn, config.font);
50
51         if (con == NULL) {
52                 LOG("This client is already in floating (container == NULL), re-inserting\n");
53                 Client *next_tiling;
54                 SLIST_FOREACH(next_tiling, &(client->workspace->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(&(client->workspace->focus_stack)))
60                         con = client->workspace->table[0][0];
61                 else con = next_tiling->container;
62
63                 /* Remove the client from the list of floating clients */
64                 TAILQ_REMOVE(&(client->workspace->floating_clients), client, floating_clients);
65
66                 LOG("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                 LOG("Re-inserted the client into the matrix.\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         LOG("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                 LOG("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                 LOG("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                 LOG("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 /*
145  * Called whenever the user clicks on a border (not the titlebar!) of a floating window.
146  * Determines on which border the user clicked and launches the drag_pointer function
147  * with the resize_callback.
148  *
149  */
150 int floating_border_click(xcb_connection_t *conn, Client *client, xcb_button_press_event_t *event) {
151
152         LOG("floating border click\n");
153
154         border_t border;
155
156         void resize_callback(Rect *old_rect, uint32_t new_x, uint32_t new_y) {
157                 switch (border) {
158                         case BORDER_RIGHT: {
159                                 int new_width = old_rect->width + (new_x - event->root_x);
160                                 if ((new_width < 0) ||
161                                     (new_width < 50 && client->rect.width >= new_width))
162                                         return;
163                                 client->rect.width = new_width;
164                                 break;
165                         }
166
167                         case BORDER_BOTTOM: {
168                                 int new_height = old_rect->height + (new_y - event->root_y);
169                                 if ((new_height < 0) ||
170                                     (new_height < 20 && client->rect.height >= new_height))
171                                         return;
172                                 client->rect.height = old_rect->height + (new_y - event->root_y);
173                                 break;
174                         }
175
176                         case BORDER_TOP: {
177                                 int new_height = old_rect->height + (event->root_y - new_y);
178                                 if ((new_height < 0) ||
179                                     (new_height < 20 && client->rect.height >= new_height))
180                                         return;
181
182                                 client->rect.y = old_rect->y + (new_y - event->root_y);
183                                 client->rect.height = new_height;
184                                 break;
185                         }
186
187                         case BORDER_LEFT: {
188                                 int new_width = old_rect->width + (event->root_x - new_x);
189                                 if ((new_width < 0) ||
190                                     (new_width < 50 && client->rect.width >= new_width))
191                                         return;
192                                 client->rect.x = old_rect->x + (new_x - event->root_x);
193                                 client->rect.width = new_width;
194                                 break;
195                         }
196                 }
197
198                 /* Push the new position/size to X11 */
199                 reposition_client(conn, client);
200                 resize_client(conn, client);
201                 xcb_flush(conn);
202         }
203
204         if (event->event_y < 2)
205                 border = BORDER_TOP;
206         else if (event->event_y >= (client->rect.height - 2))
207                 border = BORDER_BOTTOM;
208         else if (event->event_x <= 2)
209                 border = BORDER_LEFT;
210         else if (event->event_x > 2)
211                 border = BORDER_RIGHT;
212         else {
213                 LOG("Not on any border, not doing anything.\n");
214                 return 1;
215         }
216
217         LOG("border = %d\n", border);
218
219         drag_pointer(conn, client, event, border, resize_callback);
220
221         return 1;
222 }
223
224
225 /*
226  * Called when the user clicked on the titlebar of a floating window.
227  * Calls the drag_pointer function with the drag_window callback
228  *
229  */
230 void floating_drag_window(xcb_connection_t *conn, Client *client, xcb_button_press_event_t *event) {
231         LOG("floating_drag_window\n");
232
233         void drag_window_callback(Rect *old_rect, uint32_t new_x, uint32_t new_y) {
234                 /* Reposition the client correctly while moving */
235                 client->rect.x = old_rect->x + (new_x - event->root_x);
236                 client->rect.y = old_rect->y + (new_y - event->root_y);
237                 reposition_client(conn, client);
238                 /* Because reposition_client does not send a faked configure event (only resize does),
239                  * we need to initiate that on our own */
240                 fake_absolute_configure_notify(conn, client);
241                 /* fake_absolute_configure_notify flushes */
242         }
243
244
245         drag_pointer(conn, client, event, BORDER_TOP /* irrelevant */, drag_window_callback);
246 }
247
248 /*
249  * This function grabs your pointer and lets you drag stuff around (borders).
250  * Every time you move your mouse, an XCB_MOTION_NOTIFY event will be received
251  * and the given callback will be called with the parameters specified (client,
252  * border on which the click originally was), the original rect of the client,
253  * the event and the new coordinates (x, y).
254  *
255  */
256 static void drag_pointer(xcb_connection_t *conn, Client *client, xcb_button_press_event_t *event,
257                          border_t border, callback_t callback) {
258         xcb_window_t root = xcb_setup_roots_iterator(xcb_get_setup(conn)).data->root;
259         uint32_t new_x, new_y;
260         Rect old_rect;
261         memcpy(&old_rect, &(client->rect), sizeof(Rect));
262
263         /* Grab the pointer */
264         /* TODO: returncode */
265         xcb_grab_pointer(conn, 
266                         false,               /* get all pointer events specified by the following mask */
267                         root,                /* grab the root window */
268                         XCB_EVENT_MASK_BUTTON_RELEASE | XCB_EVENT_MASK_POINTER_MOTION, /* which events to let through */
269                         XCB_GRAB_MODE_ASYNC, /* pointer events should continue as normal */
270                         XCB_GRAB_MODE_ASYNC, /* keyboard mode */
271                         XCB_NONE,            /* confine_to = in which window should the cursor stay */
272                         XCB_NONE,            /* don’t display a special cursor */
273                         XCB_CURRENT_TIME);
274
275         /* Go into our own event loop */
276         xcb_flush(conn);
277
278         xcb_generic_event_t *inside_event, *last_motion_notify = NULL;
279         /* I’ve always wanted to have my own eventhandler… */
280         while ((inside_event = xcb_wait_for_event(conn))) {
281                 /* We now handle all events we can get using xcb_poll_for_event */
282                 do {
283                         /* Same as get_event_handler in xcb */
284                         int nr = inside_event->response_type;
285                         if (nr == 0) {
286                                 /* An error occured */
287                                 handle_event(NULL, conn, inside_event);
288                                 free(inside_event);
289                                 continue;
290                         }
291                         assert(nr < 256);
292                         nr &= XCB_EVENT_RESPONSE_TYPE_MASK;
293                         assert(nr >= 2);
294
295                         switch (nr) {
296                                 case XCB_BUTTON_RELEASE:
297                                         goto done;
298
299                                 case XCB_MOTION_NOTIFY:
300                                         /* motion_notify events are saved for later */
301                                         FREE(last_motion_notify);
302                                         last_motion_notify = inside_event;
303
304                                         break;
305                                 default:
306                                         LOG("Passing to original handler\n");
307                                         /* Use original handler */
308                                         xcb_event_handle(&evenths, inside_event);
309                                         break;
310                         }
311                         if (last_motion_notify != inside_event)
312                                 free(inside_event);
313                 } while ((inside_event = xcb_poll_for_event(conn)) != NULL);
314
315                 if (last_motion_notify == NULL)
316                         continue;
317
318                 new_x = ((xcb_motion_notify_event_t*)last_motion_notify)->root_x;
319                 new_y = ((xcb_motion_notify_event_t*)last_motion_notify)->root_y;
320
321                 callback(&old_rect, new_x, new_y);
322                 FREE(last_motion_notify);
323         }
324 done:
325         xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
326         xcb_flush(conn);
327 }
328
329 /*
330  * Changes focus in the given direction for floating clients.
331  *
332  * Changing to the left/right means going to the previous/next floating client,
333  * changing to top/bottom means cycling through the Z-index.
334  *
335  */
336 void floating_focus_direction(xcb_connection_t *conn, Client *currently_focused, direction_t direction) {
337         LOG("floating focus\n");
338
339         if (direction == D_LEFT || direction == D_RIGHT) {
340                 /* Go to the next/previous floating client */
341                 Client *client;
342
343                 while ((client = (direction == D_LEFT ? TAILQ_PREV(currently_focused, floating_clients_head, floating_clients) :
344                                                         TAILQ_NEXT(currently_focused, floating_clients))) !=
345                        TAILQ_END(&(currently_focused->workspace->floating_clients))) {
346                         if (!client->floating)
347                                 continue;
348                         set_focus(conn, client, true);
349                         return;
350                 }
351         }
352 }
353
354 /*
355  * Moves the client 10px to the specified direction.
356  *
357  */
358 void floating_move(xcb_connection_t *conn, Client *currently_focused, direction_t direction) {
359         LOG("floating move\n");
360
361         switch (direction) {
362                 case D_LEFT:
363                         if (currently_focused->rect.x < 10)
364                                 return;
365                         currently_focused->rect.x -= 10;
366                         break;
367                 case D_RIGHT:
368                         currently_focused->rect.x += 10;
369                         break;
370                 case D_UP:
371                         if (currently_focused->rect.y < 10)
372                                 return;
373                         currently_focused->rect.y -= 10;
374                         break;
375                 case D_DOWN:
376                         currently_focused->rect.y += 10;
377                         break;
378                 /* to make static analyzers happy */
379                 default:
380                         break;
381         }
382
383         reposition_client(conn, currently_focused);
384
385         /* Because reposition_client does not send a faked configure event (only resize does),
386          * we need to initiate that on our own */
387         fake_absolute_configure_notify(conn, currently_focused);
388         /* fake_absolute_configure_notify flushes */
389 }
390
391 /*
392  * Hides all floating clients (or show them if they are currently hidden) on
393  * the specified workspace.
394  *
395  */
396 void floating_toggle_hide(xcb_connection_t *conn, Workspace *workspace) {
397         Client *client;
398
399         workspace->floating_hidden = !workspace->floating_hidden;
400         LOG("floating_hidden is now: %d\n", workspace->floating_hidden);
401         TAILQ_FOREACH(client, &(workspace->floating_clients), floating_clients) {
402                 if (workspace->floating_hidden)
403                         xcb_unmap_window(conn, client->frame);
404                 else xcb_map_window(conn, client->frame);
405         }
406
407         /* If we just unmapped all floating windows we should ensure that the focus
408          * is set correctly, that ist, to the first non-floating client in stack */
409         if (workspace->floating_hidden)
410                 SLIST_FOREACH(client, &(workspace->focus_stack), focus_clients) {
411                         if (client_is_floating(client))
412                                 continue;
413                         set_focus(conn, client, true);
414                         return;
415                 }
416
417         xcb_flush(conn);
418 }