]> git.sur5r.net Git - i3/i3/blob - src/floating.c
Make i3 compatible with the very latest xcb
[i3/i3] / src / floating.c
1 /*
2  * vim:ts=8:expandtab
3  *
4  * i3 - an improved dynamic tiling window manager
5  *
6  * © 2009-2010 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
19 #include "i3.h"
20 #include "config.h"
21 #include "data.h"
22 #include "util.h"
23 #include "xcb.h"
24 #include "layout.h"
25 #include "client.h"
26 #include "floating.h"
27 #include "workspace.h"
28 #include "log.h"
29 #include "handlers.h"
30
31 /*
32  * Toggles floating mode for the given client.
33  * Correctly takes care of the position/size (separately stored for tiling/floating mode)
34  * and repositions/resizes/redecorates the client.
35  *
36  * If the automatic flag is set to true, this was an automatic update by a change of the
37  * window class from the application which can be overwritten by the user.
38  *
39  */
40 void toggle_floating_mode(xcb_connection_t *conn, Client *client, bool automatic) {
41         Container *con = client->container;
42         i3Font *font = load_font(conn, config.font);
43
44         if (client->dock) {
45                 DLOG("Not putting dock client into floating mode\n");
46                 return;
47         }
48
49         if (con == NULL) {
50                 DLOG("This client is already in floating (container == NULL), re-inserting\n");
51                 Client *next_tiling;
52                 Workspace *ws = client->workspace;
53                 SLIST_FOREACH(next_tiling, &(ws->focus_stack), focus_clients)
54                         if (!client_is_floating(next_tiling))
55                                 break;
56                 /* If there are no tiling clients on this workspace, there can only be one
57                  * container: the first one */
58                 if (next_tiling == TAILQ_END(&(ws->focus_stack)))
59                         con = ws->table[0][0];
60                 else con = next_tiling->container;
61
62                 /* Remove the client from the list of floating clients */
63                 TAILQ_REMOVE(&(ws->floating_clients), client, floating_clients);
64
65                 DLOG("destination container = %p\n", con);
66                 Client *old_focused = con->currently_focused;
67                 /* Preserve position/size */
68                 memcpy(&(client->floating_rect), &(client->rect), sizeof(Rect));
69
70                 client->floating = FLOATING_USER_OFF;
71                 client->container = con;
72
73                 if (old_focused != NULL && !old_focused->dock)
74                         CIRCLEQ_INSERT_AFTER(&(con->clients), old_focused, client, clients);
75                 else CIRCLEQ_INSERT_TAIL(&(con->clients), client, clients);
76
77                 DLOG("Re-inserted the window.\n");
78                 con->currently_focused = client;
79
80                 client_set_below_floating(conn, client);
81
82                 render_container(conn, con);
83                 xcb_flush(conn);
84
85                 return;
86         }
87
88         DLOG("Entering floating for client %08x\n", client->child);
89
90         /* Remove the client of its container */
91         client_remove_from_container(conn, client, con, false);
92         client->container = NULL;
93
94         /* Add the client to the list of floating clients for its workspace */
95         TAILQ_INSERT_TAIL(&(client->workspace->floating_clients), client, floating_clients);
96
97         if (con->currently_focused == client) {
98                 DLOG("Need to re-adjust currently_focused\n");
99                 /* Get the next client in the focus stack for this particular container */
100                 con->currently_focused = get_last_focused_client(conn, con, NULL);
101         }
102
103         if (automatic)
104                 client->floating = FLOATING_AUTO_ON;
105         else client->floating = FLOATING_USER_ON;
106
107         /* Initialize the floating position from the position in tiling mode, if this
108          * client never was floating (x == -1) */
109         if (client->floating_rect.x == -1) {
110                 /* Copy over the position */
111                 client->floating_rect.x = client->rect.x;
112                 client->floating_rect.y = client->rect.y;
113
114                 /* Copy size the other direction */
115                 client->child_rect.width = client->floating_rect.width;
116                 client->child_rect.height = client->floating_rect.height;
117
118                 client->rect.width = client->child_rect.width + 2 + 2;
119                 client->rect.height = client->child_rect.height + (font->height + 2 + 2) + 2;
120
121                 DLOG("copying size from tiling (%d, %d) size (%d, %d)\n", client->floating_rect.x, client->floating_rect.y,
122                                 client->floating_rect.width, client->floating_rect.height);
123         } else {
124                 /* If the client was already in floating before we restore the old position / size */
125                 DLOG("using: (%d, %d) size (%d, %d)\n", client->floating_rect.x, client->floating_rect.y,
126                         client->floating_rect.width, client->floating_rect.height);
127                 memcpy(&(client->rect), &(client->floating_rect), sizeof(Rect));
128         }
129
130         /* Raise the client */
131         xcb_raise_window(conn, client->frame);
132         reposition_client(conn, client);
133         resize_client(conn, client);
134         /* redecorate_window flushes */
135         redecorate_window(conn, client);
136
137         /* Re-render the tiling layout of this container */
138         render_container(conn, con);
139         xcb_flush(conn);
140 }
141
142 /*
143  * Removes the floating client from its workspace and attaches it to the new workspace.
144  * This is centralized here because it may happen if you move it via keyboard and
145  * if you move it using your mouse.
146  *
147  */
148 void floating_assign_to_workspace(Client *client, Workspace *new_workspace) {
149         /* Remove from focus stack and list of floating clients */
150         SLIST_REMOVE(&(client->workspace->focus_stack), client, Client, focus_clients);
151         TAILQ_REMOVE(&(client->workspace->floating_clients), client, floating_clients);
152
153         if (client->workspace->fullscreen_client == client)
154                 client->workspace->fullscreen_client = NULL;
155
156         /* Insert into destination focus stack and list of floating clients */
157         client->workspace = new_workspace;
158         SLIST_INSERT_HEAD(&(client->workspace->focus_stack), client, focus_clients);
159         TAILQ_INSERT_TAIL(&(client->workspace->floating_clients), client, floating_clients);
160         if (client->fullscreen)
161                 client->workspace->fullscreen_client = client;
162 }
163
164 /*
165  * This is an ugly data structure which we need because there is no standard
166  * way of having nested functions (only available as a gcc extension at the
167  * moment, clang doesn’t support it) or blocks (only available as a clang
168  * extension and only on Mac OS X systems at the moment).
169  *
170  */
171 struct resize_callback_params {
172         border_t border;
173         xcb_button_press_event_t *event;
174 };
175
176 DRAGGING_CB(resize_callback) {
177         struct resize_callback_params *params = extra;
178         xcb_button_press_event_t *event = params->event;
179         switch (params->border) {
180                 case BORDER_RIGHT: {
181                         int new_width = old_rect->width + (new_x - event->root_x);
182                         if ((new_width < 0) ||
183                             (new_width < client_min_width(client) && client->rect.width >= new_width))
184                                 return;
185                         client->rect.width = new_width;
186                         break;
187                 }
188
189                 case BORDER_BOTTOM: {
190                         int new_height = old_rect->height + (new_y - event->root_y);
191                         if ((new_height < 0) ||
192                             (new_height < client_min_height(client) && client->rect.height >= new_height))
193                                 return;
194                         client->rect.height = old_rect->height + (new_y - event->root_y);
195                         break;
196                 }
197
198                 case BORDER_TOP: {
199                         int new_height = old_rect->height + (event->root_y - new_y);
200                         if ((new_height < 0) ||
201                             (new_height < client_min_height(client) && client->rect.height >= new_height))
202                                 return;
203
204                         client->rect.y = old_rect->y + (new_y - event->root_y);
205                         client->rect.height = new_height;
206                         break;
207                 }
208
209                 case BORDER_LEFT: {
210                         int new_width = old_rect->width + (event->root_x - new_x);
211                         if ((new_width < 0) ||
212                             (new_width < client_min_width(client) && client->rect.width >= new_width))
213                                 return;
214                         client->rect.x = old_rect->x + (new_x - event->root_x);
215                         client->rect.width = new_width;
216                         break;
217                 }
218         }
219
220         /* Push the new position/size to X11 */
221         reposition_client(conn, client);
222         resize_client(conn, client);
223         xcb_flush(conn);
224 }
225
226
227 /*
228  * Called whenever the user clicks on a border (not the titlebar!) of a floating window.
229  * Determines on which border the user clicked and launches the drag_pointer function
230  * with the resize_callback.
231  *
232  */
233 int floating_border_click(xcb_connection_t *conn, Client *client, xcb_button_press_event_t *event) {
234         DLOG("floating border click\n");
235
236         border_t border;
237
238         if (event->event_y < 2)
239                 border = BORDER_TOP;
240         else if (event->event_y >= (client->rect.height - 2))
241                 border = BORDER_BOTTOM;
242         else if (event->event_x <= 2)
243                 border = BORDER_LEFT;
244         else if (event->event_x >= (client->rect.width - 2))
245                 border = BORDER_RIGHT;
246         else {
247                 DLOG("Not on any border, not doing anything.\n");
248                 return 1;
249         }
250
251         DLOG("border = %d\n", border);
252
253         struct resize_callback_params params = { border, event };
254
255         drag_pointer(conn, client, event, XCB_NONE, border, resize_callback, &params);
256
257         return 1;
258 }
259
260 DRAGGING_CB(drag_window_callback) {
261         struct xcb_button_press_event_t *event = extra;
262
263         /* Reposition the client correctly while moving */
264         client->rect.x = old_rect->x + (new_x - event->root_x);
265         client->rect.y = old_rect->y + (new_y - event->root_y);
266         reposition_client(conn, client);
267         /* Because reposition_client does not send a faked configure event (only resize does),
268          * we need to initiate that on our own */
269         fake_absolute_configure_notify(conn, client);
270         /* fake_absolute_configure_notify flushes */
271 }
272
273 /*
274  * Called when the user clicked on the titlebar of a floating window.
275  * Calls the drag_pointer function with the drag_window callback
276  *
277  */
278 void floating_drag_window(xcb_connection_t *conn, Client *client, xcb_button_press_event_t *event) {
279         DLOG("floating_drag_window\n");
280
281         drag_pointer(conn, client, event, XCB_NONE, BORDER_TOP /* irrelevant */, drag_window_callback, event);
282 }
283
284 /*
285  * This is an ugly data structure which we need because there is no standard
286  * way of having nested functions (only available as a gcc extension at the
287  * moment, clang doesn’t support it) or blocks (only available as a clang
288  * extension and only on Mac OS X systems at the moment).
289  *
290  */
291 struct resize_window_callback_params {
292         border_t corner;
293         bool proportional;
294         xcb_button_press_event_t *event;
295 };
296
297 DRAGGING_CB(resize_window_callback) {
298         struct resize_window_callback_params *params = extra;
299         xcb_button_press_event_t *event = params->event;
300         border_t corner = params->corner;
301
302         int32_t dest_x = client->rect.x;
303         int32_t dest_y = client->rect.y;
304         uint32_t dest_width;
305         uint32_t dest_height;
306
307         double ratio = (double) old_rect->width / old_rect->height;
308
309         /* First guess: We resize by exactly the amount the mouse moved,
310          * taking into account in which corner the client was grabbed */
311         if (corner & BORDER_LEFT)
312                 dest_width = old_rect->width - (new_x - event->root_x);
313         else dest_width = old_rect->width + (new_x - event->root_x);
314
315         if (corner & BORDER_TOP)
316                 dest_height = old_rect->height - (new_y - event->root_y);
317         else dest_height = old_rect->height + (new_y - event->root_y);
318
319         /* Obey minimum window size */
320         dest_width = max(dest_width, client_min_width(client));
321         dest_height = max(dest_height, client_min_height(client));
322
323         /* User wants to keep proportions, so we may have to adjust our values */
324         if (params->proportional) {
325                 dest_width = max(dest_width, (int) (dest_height * ratio));
326                 dest_height = max(dest_height, (int) (dest_width / ratio));
327         }
328
329         /* If not the lower right corner is grabbed, we must also reposition
330          * the client by exactly the amount we resized it */
331         if (corner & BORDER_LEFT)
332                 dest_x = old_rect->x + (old_rect->width - dest_width);
333
334         if (corner & BORDER_TOP)
335                 dest_y = old_rect->y + (old_rect->height - dest_height);
336
337         client->rect = (Rect) { dest_x, dest_y, dest_width, dest_height };
338
339         /* resize_client flushes */
340         resize_client(conn, client);
341 }
342
343 /*
344  * Called when the user clicked on a floating window while holding the
345  * floating_modifier and the right mouse button.
346  * Calls the drag_pointer function with the resize_window callback
347  *
348  */
349 void floating_resize_window(xcb_connection_t *conn, Client *client,
350                             bool proportional, xcb_button_press_event_t *event) {
351         DLOG("floating_resize_window\n");
352
353         /* corner saves the nearest corner to the original click. It contains
354          * a bitmask of the nearest borders (BORDER_LEFT, BORDER_RIGHT, …) */
355         border_t corner = 0;
356
357         if (event->event_x <= (client->rect.width / 2))
358                 corner |= BORDER_LEFT;
359         else corner |= BORDER_RIGHT;
360
361         if (event->event_y <= (client->rect.height / 2))
362                 corner |= BORDER_TOP;
363         else corner |= BORDER_RIGHT;
364
365         struct resize_window_callback_params params = { corner, proportional, event };
366
367         drag_pointer(conn, client, event, XCB_NONE, BORDER_TOP /* irrelevant */, resize_window_callback, &params);
368 }
369
370
371 /*
372  * This function grabs your pointer and lets you drag stuff around (borders).
373  * Every time you move your mouse, an XCB_MOTION_NOTIFY event will be received
374  * and the given callback will be called with the parameters specified (client,
375  * border on which the click originally was), the original rect of the client,
376  * the event and the new coordinates (x, y).
377  *
378  */
379 void drag_pointer(xcb_connection_t *conn, Client *client, xcb_button_press_event_t *event,
380                   xcb_window_t confine_to, border_t border, callback_t callback, void *extra) {
381         uint32_t new_x, new_y;
382         Rect old_rect;
383         if (client != NULL)
384                 memcpy(&old_rect, &(client->rect), sizeof(Rect));
385
386         /* Grab the pointer */
387         /* TODO: returncode */
388         xcb_grab_pointer(conn, 
389                         false,               /* get all pointer events specified by the following mask */
390                         root,                /* grab the root window */
391                         XCB_EVENT_MASK_BUTTON_RELEASE | XCB_EVENT_MASK_POINTER_MOTION, /* which events to let through */
392                         XCB_GRAB_MODE_ASYNC, /* pointer events should continue as normal */
393                         XCB_GRAB_MODE_ASYNC, /* keyboard mode */
394                         confine_to,          /* confine_to = in which window should the cursor stay */
395                         XCB_NONE,            /* don’t display a special cursor */
396                         XCB_CURRENT_TIME);
397
398         /* Go into our own event loop */
399         xcb_flush(conn);
400
401         xcb_generic_event_t *inside_event, *last_motion_notify = NULL;
402         /* I’ve always wanted to have my own eventhandler… */
403         while ((inside_event = xcb_wait_for_event(conn))) {
404                 /* We now handle all events we can get using xcb_poll_for_event */
405                 do {
406                         /* skip x11 errors */
407                         if (inside_event->response_type == 0) {
408                                 free(inside_event);
409                                 continue;
410                         }
411                         /* Strip off the highest bit (set if the event is generated) */
412                         int type = (inside_event->response_type & 0x7F);
413
414                         switch (type) {
415                                 case XCB_BUTTON_RELEASE:
416                                         goto done;
417
418                                 case XCB_MOTION_NOTIFY:
419                                         /* motion_notify events are saved for later */
420                                         FREE(last_motion_notify);
421                                         last_motion_notify = inside_event;
422                                         break;
423
424                                 case XCB_UNMAP_NOTIFY:
425                                         DLOG("Unmap-notify, aborting\n");
426                                         handle_event(type, inside_event);
427                                         goto done;
428
429                                 default:
430                                         DLOG("Passing to original handler\n");
431                                         /* Use original handler */
432                                         handle_event(type, inside_event);
433                                         break;
434                         }
435                         if (last_motion_notify != inside_event)
436                                 free(inside_event);
437                 } while ((inside_event = xcb_poll_for_event(conn)) != NULL);
438
439                 if (last_motion_notify == NULL)
440                         continue;
441
442                 new_x = ((xcb_motion_notify_event_t*)last_motion_notify)->root_x;
443                 new_y = ((xcb_motion_notify_event_t*)last_motion_notify)->root_y;
444
445                 callback(conn, client, &old_rect, new_x, new_y, extra);
446                 FREE(last_motion_notify);
447         }
448 done:
449         xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
450         xcb_flush(conn);
451 }
452
453 /*
454  * Changes focus in the given direction for floating clients.
455  *
456  * Changing to the left/right means going to the previous/next floating client,
457  * changing to top/bottom means cycling through the Z-index.
458  *
459  */
460 void floating_focus_direction(xcb_connection_t *conn, Client *currently_focused, direction_t direction) {
461         DLOG("floating focus\n");
462
463         if (direction == D_LEFT || direction == D_RIGHT) {
464                 /* Go to the next/previous floating client */
465                 Client *client;
466
467                 while ((client = (direction == D_LEFT ? TAILQ_PREV(currently_focused, floating_clients_head, floating_clients) :
468                                                         TAILQ_NEXT(currently_focused, floating_clients))) !=
469                        TAILQ_END(&(currently_focused->workspace->floating_clients))) {
470                         if (!client->floating)
471                                 continue;
472                         set_focus(conn, client, true);
473                         return;
474                 }
475         }
476 }
477
478 /*
479  * Moves the client 10px to the specified direction.
480  *
481  */
482 void floating_move(xcb_connection_t *conn, Client *currently_focused, direction_t direction) {
483         DLOG("floating move\n");
484
485         if (currently_focused->fullscreen) {
486                 DLOG("Cannot move fullscreen windows\n");
487                 return;
488         }
489
490         Rect destination = currently_focused->rect;
491         Rect *screen = &(currently_focused->workspace->output->rect);
492
493         switch (direction) {
494                 case D_LEFT:
495                         destination.x -= 10;
496                         break;
497                 case D_RIGHT:
498                         destination.x += 10;
499                         break;
500                 case D_UP:
501                         destination.y -= 10;
502                         break;
503                 case D_DOWN:
504                         destination.y += 10;
505                         break;
506                 /* to make static analyzers happy */
507                 default:
508                         break;
509         }
510
511         /* Prevent windows from vanishing completely */
512         if ((int32_t)(destination.x + destination.width - 5) <= (int32_t)screen->x ||
513             (int32_t)(destination.x + 5) >= (int32_t)(screen->x + screen->width) ||
514             (int32_t)(destination.y + destination.height - 5) <= (int32_t)screen->y ||
515             (int32_t)(destination.y + 5) >= (int32_t)(screen->y + screen->height)) {
516                 DLOG("boundary check failed, not moving\n");
517                 return;
518         }
519
520         currently_focused->rect = destination;
521         reposition_client(conn, currently_focused);
522
523         /* Because reposition_client does not send a faked configure event (only resize does),
524          * we need to initiate that on our own */
525         fake_absolute_configure_notify(conn, currently_focused);
526         /* fake_absolute_configure_notify flushes */
527 }
528
529 /*
530  * Hides all floating clients (or show them if they are currently hidden) on
531  * the specified workspace.
532  *
533  */
534 void floating_toggle_hide(xcb_connection_t *conn, Workspace *workspace) {
535         Client *client;
536
537         workspace->floating_hidden = !workspace->floating_hidden;
538         DLOG("floating_hidden is now: %d\n", workspace->floating_hidden);
539         TAILQ_FOREACH(client, &(workspace->floating_clients), floating_clients) {
540                 if (workspace->floating_hidden)
541                         client_unmap(conn, client);
542                 else client_map(conn, client);
543         }
544
545         /* If we just unmapped all floating windows we should ensure that the focus
546          * is set correctly, that ist, to the first non-floating client in stack */
547         if (workspace->floating_hidden)
548                 SLIST_FOREACH(client, &(workspace->focus_stack), focus_clients) {
549                         if (client_is_floating(client))
550                                 continue;
551                         set_focus(conn, client, true);
552                         return;
553                 }
554
555         xcb_flush(conn);
556 }