]> git.sur5r.net Git - i3/i3/blob - src/floating.c
Use drag_pointer from floating.c for the resize handler
[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 > 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
259         drag_pointer(conn, client, event, XCB_NONE, BORDER_TOP /* irrelevant */, drag_window_callback);
260 }
261
262 /*
263  * This function grabs your pointer and lets you drag stuff around (borders).
264  * Every time you move your mouse, an XCB_MOTION_NOTIFY event will be received
265  * and the given callback will be called with the parameters specified (client,
266  * border on which the click originally was), the original rect of the client,
267  * the event and the new coordinates (x, y).
268  *
269  */
270 void drag_pointer(xcb_connection_t *conn, Client *client, xcb_button_press_event_t *event,
271                   xcb_window_t confine_to, border_t border, callback_t callback) {
272         xcb_window_t root = xcb_setup_roots_iterator(xcb_get_setup(conn)).data->root;
273         uint32_t new_x, new_y;
274         Rect old_rect;
275         if (client != NULL)
276                 memcpy(&old_rect, &(client->rect), sizeof(Rect));
277
278         /* Grab the pointer */
279         /* TODO: returncode */
280         xcb_grab_pointer(conn, 
281                         false,               /* get all pointer events specified by the following mask */
282                         root,                /* grab the root window */
283                         XCB_EVENT_MASK_BUTTON_RELEASE | XCB_EVENT_MASK_POINTER_MOTION, /* which events to let through */
284                         XCB_GRAB_MODE_ASYNC, /* pointer events should continue as normal */
285                         XCB_GRAB_MODE_ASYNC, /* keyboard mode */
286                         confine_to,          /* confine_to = in which window should the cursor stay */
287                         XCB_NONE,            /* don’t display a special cursor */
288                         XCB_CURRENT_TIME);
289
290         /* Go into our own event loop */
291         xcb_flush(conn);
292
293         xcb_generic_event_t *inside_event, *last_motion_notify = NULL;
294         /* I’ve always wanted to have my own eventhandler… */
295         while ((inside_event = xcb_wait_for_event(conn))) {
296                 /* We now handle all events we can get using xcb_poll_for_event */
297                 do {
298                         /* Same as get_event_handler in xcb */
299                         int nr = inside_event->response_type;
300                         if (nr == 0) {
301                                 /* An error occured */
302                                 handle_event(NULL, conn, inside_event);
303                                 free(inside_event);
304                                 continue;
305                         }
306                         assert(nr < 256);
307                         nr &= XCB_EVENT_RESPONSE_TYPE_MASK;
308                         assert(nr >= 2);
309
310                         switch (nr) {
311                                 case XCB_BUTTON_RELEASE:
312                                         goto done;
313
314                                 case XCB_MOTION_NOTIFY:
315                                         /* motion_notify events are saved for later */
316                                         FREE(last_motion_notify);
317                                         last_motion_notify = inside_event;
318
319                                         break;
320                                 default:
321                                         LOG("Passing to original handler\n");
322                                         /* Use original handler */
323                                         xcb_event_handle(&evenths, inside_event);
324                                         break;
325                         }
326                         if (last_motion_notify != inside_event)
327                                 free(inside_event);
328                 } while ((inside_event = xcb_poll_for_event(conn)) != NULL);
329
330                 if (last_motion_notify == NULL)
331                         continue;
332
333                 new_x = ((xcb_motion_notify_event_t*)last_motion_notify)->root_x;
334                 new_y = ((xcb_motion_notify_event_t*)last_motion_notify)->root_y;
335
336                 callback(&old_rect, new_x, new_y);
337                 FREE(last_motion_notify);
338         }
339 done:
340         xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
341         xcb_flush(conn);
342 }
343
344 /*
345  * Changes focus in the given direction for floating clients.
346  *
347  * Changing to the left/right means going to the previous/next floating client,
348  * changing to top/bottom means cycling through the Z-index.
349  *
350  */
351 void floating_focus_direction(xcb_connection_t *conn, Client *currently_focused, direction_t direction) {
352         LOG("floating focus\n");
353
354         if (direction == D_LEFT || direction == D_RIGHT) {
355                 /* Go to the next/previous floating client */
356                 Client *client;
357
358                 while ((client = (direction == D_LEFT ? TAILQ_PREV(currently_focused, floating_clients_head, floating_clients) :
359                                                         TAILQ_NEXT(currently_focused, floating_clients))) !=
360                        TAILQ_END(&(currently_focused->workspace->floating_clients))) {
361                         if (!client->floating)
362                                 continue;
363                         set_focus(conn, client, true);
364                         return;
365                 }
366         }
367 }
368
369 /*
370  * Moves the client 10px to the specified direction.
371  *
372  */
373 void floating_move(xcb_connection_t *conn, Client *currently_focused, direction_t direction) {
374         LOG("floating move\n");
375
376         switch (direction) {
377                 case D_LEFT:
378                         if (currently_focused->rect.x < 10)
379                                 return;
380                         currently_focused->rect.x -= 10;
381                         break;
382                 case D_RIGHT:
383                         currently_focused->rect.x += 10;
384                         break;
385                 case D_UP:
386                         if (currently_focused->rect.y < 10)
387                                 return;
388                         currently_focused->rect.y -= 10;
389                         break;
390                 case D_DOWN:
391                         currently_focused->rect.y += 10;
392                         break;
393                 /* to make static analyzers happy */
394                 default:
395                         break;
396         }
397
398         reposition_client(conn, currently_focused);
399
400         /* Because reposition_client does not send a faked configure event (only resize does),
401          * we need to initiate that on our own */
402         fake_absolute_configure_notify(conn, currently_focused);
403         /* fake_absolute_configure_notify flushes */
404 }
405
406 /*
407  * Hides all floating clients (or show them if they are currently hidden) on
408  * the specified workspace.
409  *
410  */
411 void floating_toggle_hide(xcb_connection_t *conn, Workspace *workspace) {
412         Client *client;
413
414         workspace->floating_hidden = !workspace->floating_hidden;
415         LOG("floating_hidden is now: %d\n", workspace->floating_hidden);
416         TAILQ_FOREACH(client, &(workspace->floating_clients), floating_clients) {
417                 if (workspace->floating_hidden)
418                         xcb_unmap_window(conn, client->frame);
419                 else xcb_map_window(conn, client->frame);
420         }
421
422         /* If we just unmapped all floating windows we should ensure that the focus
423          * is set correctly, that ist, to the first non-floating client in stack */
424         if (workspace->floating_hidden)
425                 SLIST_FOREACH(client, &(workspace->focus_stack), focus_clients) {
426                         if (client_is_floating(client))
427                                 continue;
428                         set_focus(conn, client, true);
429                         return;
430                 }
431
432         xcb_flush(conn);
433 }