]> git.sur5r.net Git - i3/i3/blob - src/floating.c
floating: nested functions make the callbacks a lot more easier & beautiful
[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 (next_tiling->floating <= FLOATING_USER_OFF)
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                 render_container(conn, con);
82                 xcb_flush(conn);
83
84                 return;
85         }
86
87         LOG("Entering floating for client %08x\n", client->child);
88
89         /* Remove the client of its container */
90         client_remove_from_container(conn, client, con, false);
91         client->container = NULL;
92
93         /* Add the client to the list of floating clients for its workspace */
94         TAILQ_INSERT_TAIL(&(client->workspace->floating_clients), client, floating_clients);
95
96         if (con->currently_focused == client) {
97                 LOG("Need to re-adjust currently_focused\n");
98                 /* Get the next client in the focus stack for this particular container */
99                 con->currently_focused = get_last_focused_client(conn, con, NULL);
100         }
101
102         if (automatic)
103                 client->floating = FLOATING_AUTO_ON;
104         else client->floating = FLOATING_USER_ON;
105
106         /* Initialize the floating position from the position in tiling mode, if this
107          * client never was floating (x == -1) */
108         if (client->floating_rect.x == -1) {
109                 /* Copy over the position */
110                 client->floating_rect.x = client->rect.x;
111                 client->floating_rect.y = client->rect.y;
112
113                 /* Copy size the other direction */
114                 client->child_rect.width = client->floating_rect.width;
115                 client->child_rect.height = client->floating_rect.height;
116
117                 client->rect.width = client->child_rect.width + 2 + 2;
118                 client->rect.height = client->child_rect.height + (font->height + 2 + 2) + 2;
119
120                 LOG("copying size from tiling (%d, %d) size (%d, %d)\n", client->floating_rect.x, client->floating_rect.y,
121                                 client->floating_rect.width, client->floating_rect.height);
122         } else {
123                 /* If the client was already in floating before we restore the old position / size */
124                 LOG("using: (%d, %d) size (%d, %d)\n", client->floating_rect.x, client->floating_rect.y,
125                         client->floating_rect.width, client->floating_rect.height);
126                 memcpy(&(client->rect), &(client->floating_rect), sizeof(Rect));
127         }
128
129         /* Raise the client */
130         xcb_raise_window(conn, client->frame);
131         reposition_client(conn, client);
132         resize_client(conn, client);
133         /* redecorate_window flushes */
134         redecorate_window(conn, client);
135
136         /* Re-render the tiling layout of this container */
137         render_container(conn, con);
138         xcb_flush(conn);
139 }
140
141
142 /*
143  * Called whenever the user clicks on a border (not the titlebar!) of a floating window.
144  * Determines on which border the user clicked and launches the drag_pointer function
145  * with the resize_callback.
146  *
147  */
148 int floating_border_click(xcb_connection_t *conn, Client *client, xcb_button_press_event_t *event) {
149
150         LOG("floating border click\n");
151
152         border_t border;
153
154         void resize_callback(Rect *old_rect, uint32_t new_x, uint32_t new_y) {
155                 switch (border) {
156                         case BORDER_RIGHT:
157                                 client->rect.width = old_rect->width + (new_x - event->root_x);
158                                 break;
159
160                         case BORDER_BOTTOM:
161                                 client->rect.height = old_rect->height + (new_y - event->root_y);
162                                 break;
163
164                         case BORDER_TOP:
165                                 client->rect.y = old_rect->y + (new_y - event->root_y);
166                                 client->rect.height = old_rect->height + (event->root_y - new_y);
167                                 break;
168
169                         case BORDER_LEFT:
170                                 client->rect.x = old_rect->x + (new_x - event->root_x);
171                                 client->rect.width = old_rect->width + (event->root_x - new_x);
172                                 break;
173                 }
174
175                 /* Push the new position/size to X11 */
176                 reposition_client(conn, client);
177                 resize_client(conn, client);
178                 xcb_flush(conn);
179         }
180
181         if (event->event_y < 2)
182                 border = BORDER_TOP;
183         else if (event->event_y >= (client->rect.height - 2))
184                 border = BORDER_BOTTOM;
185         else if (event->event_x <= 2)
186                 border = BORDER_LEFT;
187         else if (event->event_x > 2)
188                 border = BORDER_RIGHT;
189         else {
190                 LOG("Not on any border, not doing anything.\n");
191                 return 1;
192         }
193
194         LOG("border = %d\n", border);
195
196         drag_pointer(conn, client, event, border, resize_callback);
197
198         return 1;
199 }
200
201
202 /*
203  * Called when the user clicked on the titlebar of a floating window.
204  * Calls the drag_pointer function with the drag_window callback
205  *
206  */
207 void floating_drag_window(xcb_connection_t *conn, Client *client, xcb_button_press_event_t *event) {
208         LOG("floating_drag_window\n");
209
210         void drag_window_callback(Rect *old_rect, uint32_t new_x, uint32_t new_y) {
211                 /* Reposition the client correctly while moving */
212                 client->rect.x = old_rect->x + (new_x - event->root_x);
213                 client->rect.y = old_rect->y + (new_y - event->root_y);
214                 reposition_client(conn, client);
215                 /* Because reposition_client does not send a faked configure event (only resize does),
216                  * we need to initiate that on our own */
217                 fake_absolute_configure_notify(conn, client);
218                 /* fake_absolute_configure_notify flushes */
219         }
220
221
222         drag_pointer(conn, client, event, BORDER_TOP /* irrelevant */, drag_window_callback);
223 }
224
225 /*
226  * This function grabs your pointer and lets you drag stuff around (borders).
227  * Every time you move your mouse, an XCB_MOTION_NOTIFY event will be received
228  * and the given callback will be called with the parameters specified (client,
229  * border on which the click originally was), the original rect of the client,
230  * the event and the new coordinates (x, y).
231  *
232  */
233 static void drag_pointer(xcb_connection_t *conn, Client *client, xcb_button_press_event_t *event,
234                          border_t border, callback_t callback) {
235         xcb_window_t root = xcb_setup_roots_iterator(xcb_get_setup(conn)).data->root;
236         uint32_t new_x, new_y;
237         Rect old_rect;
238         memcpy(&old_rect, &(client->rect), sizeof(Rect));
239
240         /* Grab the pointer */
241         /* TODO: returncode */
242         xcb_grab_pointer(conn, 
243                         false,               /* get all pointer events specified by the following mask */
244                         root,                /* grab the root window */
245                         XCB_EVENT_MASK_BUTTON_RELEASE | XCB_EVENT_MASK_POINTER_MOTION, /* which events to let through */
246                         XCB_GRAB_MODE_ASYNC, /* pointer events should continue as normal */
247                         XCB_GRAB_MODE_ASYNC, /* keyboard mode */
248                         XCB_NONE,            /* confine_to = in which window should the cursor stay */
249                         XCB_NONE,            /* don’t display a special cursor */
250                         XCB_CURRENT_TIME);
251
252         /* Go into our own event loop */
253         xcb_flush(conn);
254
255         xcb_generic_event_t *inside_event;
256         /* I’ve always wanted to have my own eventhandler… */
257         while ((inside_event = xcb_wait_for_event(conn))) {
258                 /* Same as get_event_handler in xcb */
259                 int nr = inside_event->response_type;
260                 if (nr == 0) {
261                         /* An error occured */
262                         handle_event(NULL, conn, inside_event);
263                         free(inside_event);
264                         continue;
265                 }
266                 assert(nr < 256);
267                 nr &= XCB_EVENT_RESPONSE_TYPE_MASK;
268                 assert(nr >= 2);
269
270                 /* Check if we need to escape this loop */
271                 if (nr == XCB_BUTTON_RELEASE)
272                         break;
273
274                 switch (nr) {
275                         case XCB_MOTION_NOTIFY:
276                                 new_x = ((xcb_motion_notify_event_t*)inside_event)->root_x;
277                                 new_y = ((xcb_motion_notify_event_t*)inside_event)->root_y;
278
279                                 callback(&old_rect, new_x, new_y);
280
281                                 break;
282                         default:
283                                 LOG("Passing to original handler\n");
284                                 /* Use original handler */
285                                 xcb_event_handle(&evenths, inside_event);
286                                 break;
287                 }
288                 free(inside_event);
289         }
290
291         xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
292         xcb_flush(conn);
293 }
294
295 /*
296  * Changes focus in the given direction for floating clients.
297  *
298  * Changing to the left/right means going to the previous/next floating client,
299  * changing to top/bottom means cycling through the Z-index.
300  *
301  */
302 void floating_focus_direction(xcb_connection_t *conn, Client *currently_focused, direction_t direction) {
303         LOG("floating focus\n");
304
305         if (direction == D_LEFT || direction == D_RIGHT) {
306                 /* Go to the next/previous floating client */
307                 Client *client;
308
309                 while ((client = (direction == D_LEFT ? TAILQ_PREV(currently_focused, floating_clients_head, floating_clients) :
310                                                         TAILQ_NEXT(currently_focused, floating_clients))) !=
311                        TAILQ_END(&(currently_focused->workspace->floating_clients))) {
312                         if (!client->floating)
313                                 continue;
314                         set_focus(conn, client, true);
315                         return;
316                 }
317         }
318 }
319
320 /*
321  * Moves the client 10px to the specified direction.
322  *
323  */
324 void floating_move(xcb_connection_t *conn, Client *currently_focused, direction_t direction) {
325         LOG("floating move\n");
326
327         switch (direction) {
328                 case D_LEFT:
329                         if (currently_focused->rect.x < 10)
330                                 return;
331                         currently_focused->rect.x -= 10;
332                         break;
333                 case D_RIGHT:
334                         currently_focused->rect.x += 10;
335                         break;
336                 case D_UP:
337                         if (currently_focused->rect.y < 10)
338                                 return;
339                         currently_focused->rect.y -= 10;
340                         break;
341                 case D_DOWN:
342                         currently_focused->rect.y += 10;
343                         break;
344                 /* to make static analyzers happy */
345                 default:
346                         break;
347         }
348
349         reposition_client(conn, currently_focused);
350
351         /* Because reposition_client does not send a faked configure event (only resize does),
352          * we need to initiate that on our own */
353         fake_absolute_configure_notify(conn, currently_focused);
354         /* fake_absolute_configure_notify flushes */
355 }
356
357 /*
358  * Hides all floating clients (or show them if they are currently hidden) on
359  * the specified workspace.
360  *
361  */
362 void floating_toggle_hide(xcb_connection_t *conn, Workspace *workspace) {
363         Client *client;
364
365         workspace->floating_hidden = !workspace->floating_hidden;
366         LOG("floating_hidden is now: %d\n", workspace->floating_hidden);
367         TAILQ_FOREACH(client, &(workspace->floating_clients), floating_clients) {
368                 if (workspace->floating_hidden)
369                         xcb_unmap_window(conn, client->frame);
370                 else xcb_map_window(conn, client->frame);
371         }
372
373         /* If we just unmapped all floating windows we should ensure that the focus
374          * is set correctly, that ist, to the first non-floating client in stack */
375         if (workspace->floating_hidden)
376                 SLIST_FOREACH(client, &(workspace->focus_stack), focus_clients)
377                         if (client->floating <= FLOATING_USER_OFF) {
378                                 set_focus(conn, client, true);
379                                 return;
380                         }
381
382         xcb_flush(conn);
383 }