]> git.sur5r.net Git - i3/i3/blob - src/floating.c
181fbe34ab389686884a92867ad9c0597083dbb0
[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 "data.h"
22 #include "util.h"
23 #include "xcb.h"
24 #include "debug.h"
25 #include "layout.h"
26 #include "client.h"
27
28 /* On which border was the dragging initiated? */
29 typedef enum { BORDER_LEFT, BORDER_RIGHT, BORDER_TOP, BORDER_BOTTOM} border_t;
30 /* Callback for dragging */
31 typedef void(*callback_t)(xcb_connection_t*, Client*, border_t, Rect*, xcb_button_press_event_t*, uint32_t, uint32_t);
32
33 /* Forward definitions */
34 static void drag_pointer(xcb_connection_t *conn, Client *client, xcb_button_press_event_t *event,
35                          border_t border, callback_t callback);
36
37 /*
38  * Toggles floating mode for the given client.
39  * Correctly takes care of the position/size (separately stored for tiling/floating mode)
40  * and repositions/resizes/redecorates the client.
41  *
42  * If the automatic flag is set to true, this was an automatic update by a change of the
43  * window class from the application which can be overwritten by the user.
44  *
45  */
46 void toggle_floating_mode(xcb_connection_t *conn, Client *client, bool automatic) {
47         Container *con = client->container;
48
49         if (con == NULL) {
50                 LOG("This client is already in floating (container == NULL), re-inserting\n");
51                 Client *next_tiling;
52                 SLIST_FOREACH(next_tiling, &(client->workspace->focus_stack), focus_clients)
53                         if (next_tiling->floating <= FLOATING_USER_OFF)
54                                 break;
55                 /* If there are no tiling clients on this workspace, there can only be one
56                  * container: the first one */
57                 if (next_tiling == SLIST_END(&(client->workspace->focus_stack)))
58                         con = client->workspace->table[0][0];
59                 else con = next_tiling->container;
60
61                 LOG("destination container = %p\n", con);
62                 Client *old_focused = con->currently_focused;
63                 /* Preserve position/size */
64                 memcpy(&(client->floating_rect), &(client->rect), sizeof(Rect));
65
66                 client->floating = FLOATING_USER_OFF;
67                 client->container = con;
68
69                 if (old_focused != NULL && !old_focused->dock)
70                         CIRCLEQ_INSERT_AFTER(&(con->clients), old_focused, client, clients);
71                 else CIRCLEQ_INSERT_TAIL(&(con->clients), client, clients);
72
73                 LOG("Re-inserted the client into the matrix.\n");
74                 con->currently_focused = client;
75
76                 render_container(conn, con);
77                 xcb_flush(conn);
78
79                 return;
80         }
81
82         LOG("Entering floating for client %08x\n", client->child);
83
84         /* Remove the client of its container */
85         client_remove_from_container(conn, client, con, false);
86         client->container = NULL;
87
88         if (con->currently_focused == client) {
89                 LOG("Need to re-adjust currently_focused\n");
90                 /* Get the next client in the focus stack for this particular container */
91                 con->currently_focused = get_last_focused_client(conn, con, NULL);
92         }
93
94         if (automatic)
95                 client->floating = FLOATING_AUTO_ON;
96         else client->floating = FLOATING_USER_ON;
97
98         /* Initialize the floating position from the position in tiling mode, if this
99          * client never was floating (x == -1) */
100         if (client->floating_rect.x == -1) {
101                 /* Copy over the position */
102                 client->floating_rect.x = client->rect.x;
103                 client->floating_rect.y = client->rect.y;
104
105                 /* Copy the size the other direction */
106                 client->rect.width = client->floating_rect.width;
107                 client->rect.height = client->floating_rect.height;
108
109                 LOG("copying size from tiling (%d, %d) size (%d, %d)\n", client->floating_rect.x, client->floating_rect.y,
110                                 client->floating_rect.width, client->floating_rect.height);
111         } else {
112                 /* If the client was already in floating before we restore the old position / size */
113                 LOG("using: (%d, %d) size (%d, %d)\n", client->floating_rect.x, client->floating_rect.y,
114                         client->floating_rect.width, client->floating_rect.height);
115                 memcpy(&(client->rect), &(client->floating_rect), sizeof(Rect));
116         }
117
118         /* Raise the client */
119         xcb_raise_window(conn, client->frame);
120         reposition_client(conn, client);
121         resize_client(conn, client);
122         /* redecorate_window flushes */
123         redecorate_window(conn, client);
124
125         /* Re-render the tiling layout of this container */
126         render_container(conn, con);
127         xcb_flush(conn);
128 }
129
130 /*
131  * Callback for resizing windows
132  *
133  */
134 static void resize_callback(xcb_connection_t *conn, Client *client, border_t border, Rect *old_rect,
135                             xcb_button_press_event_t *event, uint32_t new_x, uint32_t new_y) {
136         switch (border) {
137                 case BORDER_RIGHT:
138                         client->rect.width = old_rect->width + (new_x - event->root_x);
139                         break;
140
141                 case BORDER_BOTTOM:
142                         client->rect.height = old_rect->height + (new_y - event->root_y);
143                         break;
144
145                 case BORDER_TOP:
146                         client->rect.y = old_rect->y + (new_y - event->root_y);
147                         client->rect.height = old_rect->height + (event->root_y - new_y);
148                         break;
149
150                 case BORDER_LEFT:
151                         client->rect.x = old_rect->x + (new_x - event->root_x);
152                         client->rect.width = old_rect->width + (event->root_x - new_x);
153                         break;
154         }
155
156         /* Push the new position/size to X11 */
157         reposition_client(conn, client);
158         resize_client(conn, client);
159         xcb_flush(conn);
160 }
161
162 /*
163  * Called whenever the user clicks on a border (not the titlebar!) of a floating window.
164  * Determines on which border the user clicked and launches the drag_pointer function
165  * with the resize_callback.
166  *
167  */
168 int floating_border_click(xcb_connection_t *conn, Client *client, xcb_button_press_event_t *event) {
169
170         LOG("floating border click\n");
171
172         border_t border;
173         if (event->event_y < 2)
174                 border = BORDER_TOP;
175         else if (event->event_y >= (client->rect.height - 2))
176                 border = BORDER_BOTTOM;
177         else if (event->event_x <= 2)
178                 border = BORDER_LEFT;
179         else if (event->event_x > 2)
180                 border = BORDER_RIGHT;
181         else {
182                 LOG("Not on any border, not doing anything.\n");
183                 return 1;
184         }
185
186         LOG("border = %d\n", border);
187
188         drag_pointer(conn, client, event, border, resize_callback);
189
190         return 1;
191 }
192
193 static void drag_window_callback(xcb_connection_t *conn, Client *client, border_t border, Rect *old_rect,
194                             xcb_button_press_event_t *event, uint32_t new_x, uint32_t new_y) {
195         /* Reposition the client correctly while moving */
196         client->rect.x = old_rect->x + (new_x - event->root_x);
197         client->rect.y = old_rect->y + (new_y - event->root_y);
198         reposition_client(conn, client);
199         /* Because reposition_client does not send a faked configure event (only resize does),
200          * we need to initiate that on our own */
201         fake_absolute_configure_notify(conn, client);
202         /* fake_absolute_configure_notify flushes */
203 }
204
205 /*
206  * Called when the user clicked on the titlebar of a floating window.
207  * Calls the drag_pointer function with the drag_window callback
208  *
209  */
210 void floating_drag_window(xcb_connection_t *conn, Client *client, xcb_button_press_event_t *event) {
211         LOG("floating_drag_window\n");
212
213         drag_pointer(conn, client, event, BORDER_TOP /* irrelevant */, drag_window_callback);
214 }
215
216 /*
217  * This function grabs your pointer and lets you drag stuff around (borders).
218  * Every time you move your mouse, an XCB_MOTION_NOTIFY event will be received
219  * and the given callback will be called with the parameters specified (client,
220  * border on which the click originally was), the original rect of the client,
221  * the event and the new coordinates (x, y).
222  *
223  */
224 static void drag_pointer(xcb_connection_t *conn, Client *client, xcb_button_press_event_t *event,
225                          border_t border, callback_t callback) {
226         xcb_window_t root = xcb_setup_roots_iterator(xcb_get_setup(conn)).data->root;
227         uint32_t new_x, new_y;
228         Rect old_rect;
229         memcpy(&old_rect, &(client->rect), sizeof(Rect));
230
231         /* Grab the pointer */
232         /* TODO: returncode */
233         xcb_grab_pointer(conn, 
234                         false,               /* get all pointer events specified by the following mask */
235                         root,                /* grab the root window */
236                         XCB_EVENT_MASK_BUTTON_RELEASE | XCB_EVENT_MASK_POINTER_MOTION, /* which events to let through */
237                         XCB_GRAB_MODE_ASYNC, /* pointer events should continue as normal */
238                         XCB_GRAB_MODE_ASYNC, /* keyboard mode */
239                         XCB_NONE,            /* confine_to = in which window should the cursor stay */
240                         XCB_NONE,            /* don’t display a special cursor */
241                         XCB_CURRENT_TIME);
242
243         /* Go into our own event loop */
244         xcb_flush(conn);
245
246         xcb_generic_event_t *inside_event;
247         /* I’ve always wanted to have my own eventhandler… */
248         while ((inside_event = xcb_wait_for_event(conn))) {
249                 /* Same as get_event_handler in xcb */
250                 int nr = inside_event->response_type;
251                 if (nr == 0) {
252                         /* An error occured */
253                         handle_event(NULL, conn, inside_event);
254                         free(inside_event);
255                         continue;
256                 }
257                 assert(nr < 256);
258                 nr &= XCB_EVENT_RESPONSE_TYPE_MASK;
259                 assert(nr >= 2);
260
261                 /* Check if we need to escape this loop */
262                 if (nr == XCB_BUTTON_RELEASE)
263                         break;
264
265                 switch (nr) {
266                         case XCB_MOTION_NOTIFY:
267                                 new_x = ((xcb_motion_notify_event_t*)inside_event)->root_x;
268                                 new_y = ((xcb_motion_notify_event_t*)inside_event)->root_y;
269
270                                 callback(conn, client, border, &old_rect, event, new_x, new_y);
271
272                                 break;
273                         default:
274                                 LOG("Passing to original handler\n");
275                                 /* Use original handler */
276                                 xcb_event_handle(&evenths, inside_event);
277                                 break;
278                 }
279                 free(inside_event);
280         }
281
282         xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
283         xcb_flush(conn);
284 }
285