]> git.sur5r.net Git - i3/i3/blob - src/floating.c
6a2d8218f017b7efbc879cc708817ed5b24c03aa
[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)(xcb_connection_t*, Client*, border_t, Rect*, xcb_button_press_event_t*, 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 == SLIST_END(&(client->workspace->focus_stack)))
60                         con = client->workspace->table[0][0];
61                 else con = next_tiling->container;
62
63                 LOG("destination container = %p\n", con);
64                 Client *old_focused = con->currently_focused;
65                 /* Preserve position/size */
66                 memcpy(&(client->floating_rect), &(client->rect), sizeof(Rect));
67
68                 client->floating = FLOATING_USER_OFF;
69                 client->container = con;
70
71                 if (old_focused != NULL && !old_focused->dock)
72                         CIRCLEQ_INSERT_AFTER(&(con->clients), old_focused, client, clients);
73                 else CIRCLEQ_INSERT_TAIL(&(con->clients), client, clients);
74
75                 LOG("Re-inserted the client into the matrix.\n");
76                 con->currently_focused = client;
77
78                 render_container(conn, con);
79                 xcb_flush(conn);
80
81                 return;
82         }
83
84         LOG("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         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 the 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  * Callback for resizing windows
137  *
138  */
139 static void resize_callback(xcb_connection_t *conn, Client *client, border_t border, Rect *old_rect,
140                             xcb_button_press_event_t *event, uint32_t new_x, uint32_t new_y) {
141         switch (border) {
142                 case BORDER_RIGHT:
143                         client->rect.width = old_rect->width + (new_x - event->root_x);
144                         break;
145
146                 case BORDER_BOTTOM:
147                         client->rect.height = old_rect->height + (new_y - event->root_y);
148                         break;
149
150                 case BORDER_TOP:
151                         client->rect.y = old_rect->y + (new_y - event->root_y);
152                         client->rect.height = old_rect->height + (event->root_y - new_y);
153                         break;
154
155                 case BORDER_LEFT:
156                         client->rect.x = old_rect->x + (new_x - event->root_x);
157                         client->rect.width = old_rect->width + (event->root_x - new_x);
158                         break;
159         }
160
161         /* Push the new position/size to X11 */
162         reposition_client(conn, client);
163         resize_client(conn, client);
164         xcb_flush(conn);
165 }
166
167 /*
168  * Called whenever the user clicks on a border (not the titlebar!) of a floating window.
169  * Determines on which border the user clicked and launches the drag_pointer function
170  * with the resize_callback.
171  *
172  */
173 int floating_border_click(xcb_connection_t *conn, Client *client, xcb_button_press_event_t *event) {
174
175         LOG("floating border click\n");
176
177         border_t border;
178         if (event->event_y < 2)
179                 border = BORDER_TOP;
180         else if (event->event_y >= (client->rect.height - 2))
181                 border = BORDER_BOTTOM;
182         else if (event->event_x <= 2)
183                 border = BORDER_LEFT;
184         else if (event->event_x > 2)
185                 border = BORDER_RIGHT;
186         else {
187                 LOG("Not on any border, not doing anything.\n");
188                 return 1;
189         }
190
191         LOG("border = %d\n", border);
192
193         drag_pointer(conn, client, event, border, resize_callback);
194
195         return 1;
196 }
197
198 static void drag_window_callback(xcb_connection_t *conn, Client *client, border_t border, Rect *old_rect,
199                             xcb_button_press_event_t *event, uint32_t new_x, uint32_t new_y) {
200         /* Reposition the client correctly while moving */
201         client->rect.x = old_rect->x + (new_x - event->root_x);
202         client->rect.y = old_rect->y + (new_y - event->root_y);
203         reposition_client(conn, client);
204         /* Because reposition_client does not send a faked configure event (only resize does),
205          * we need to initiate that on our own */
206         fake_absolute_configure_notify(conn, client);
207         /* fake_absolute_configure_notify flushes */
208 }
209
210 /*
211  * Called when the user clicked on the titlebar of a floating window.
212  * Calls the drag_pointer function with the drag_window callback
213  *
214  */
215 void floating_drag_window(xcb_connection_t *conn, Client *client, xcb_button_press_event_t *event) {
216         LOG("floating_drag_window\n");
217
218         drag_pointer(conn, client, event, BORDER_TOP /* irrelevant */, drag_window_callback);
219 }
220
221 /*
222  * This function grabs your pointer and lets you drag stuff around (borders).
223  * Every time you move your mouse, an XCB_MOTION_NOTIFY event will be received
224  * and the given callback will be called with the parameters specified (client,
225  * border on which the click originally was), the original rect of the client,
226  * the event and the new coordinates (x, y).
227  *
228  */
229 static void drag_pointer(xcb_connection_t *conn, Client *client, xcb_button_press_event_t *event,
230                          border_t border, callback_t callback) {
231         xcb_window_t root = xcb_setup_roots_iterator(xcb_get_setup(conn)).data->root;
232         uint32_t new_x, new_y;
233         Rect old_rect;
234         memcpy(&old_rect, &(client->rect), sizeof(Rect));
235
236         /* Grab the pointer */
237         /* TODO: returncode */
238         xcb_grab_pointer(conn, 
239                         false,               /* get all pointer events specified by the following mask */
240                         root,                /* grab the root window */
241                         XCB_EVENT_MASK_BUTTON_RELEASE | XCB_EVENT_MASK_POINTER_MOTION, /* which events to let through */
242                         XCB_GRAB_MODE_ASYNC, /* pointer events should continue as normal */
243                         XCB_GRAB_MODE_ASYNC, /* keyboard mode */
244                         XCB_NONE,            /* confine_to = in which window should the cursor stay */
245                         XCB_NONE,            /* don’t display a special cursor */
246                         XCB_CURRENT_TIME);
247
248         /* Go into our own event loop */
249         xcb_flush(conn);
250
251         xcb_generic_event_t *inside_event;
252         /* I’ve always wanted to have my own eventhandler… */
253         while ((inside_event = xcb_wait_for_event(conn))) {
254                 /* Same as get_event_handler in xcb */
255                 int nr = inside_event->response_type;
256                 if (nr == 0) {
257                         /* An error occured */
258                         handle_event(NULL, conn, inside_event);
259                         free(inside_event);
260                         continue;
261                 }
262                 assert(nr < 256);
263                 nr &= XCB_EVENT_RESPONSE_TYPE_MASK;
264                 assert(nr >= 2);
265
266                 /* Check if we need to escape this loop */
267                 if (nr == XCB_BUTTON_RELEASE)
268                         break;
269
270                 switch (nr) {
271                         case XCB_MOTION_NOTIFY:
272                                 new_x = ((xcb_motion_notify_event_t*)inside_event)->root_x;
273                                 new_y = ((xcb_motion_notify_event_t*)inside_event)->root_y;
274
275                                 callback(conn, client, border, &old_rect, event, new_x, new_y);
276
277                                 break;
278                         default:
279                                 LOG("Passing to original handler\n");
280                                 /* Use original handler */
281                                 xcb_event_handle(&evenths, inside_event);
282                                 break;
283                 }
284                 free(inside_event);
285         }
286
287         xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
288         xcb_flush(conn);
289 }
290