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