]> git.sur5r.net Git - i3/i3/blob - src/floating.c
Bugfix: Don’t always focus floating windows (fixes focus stealing with assignments...
[i3/i3] / src / floating.c
1 /*
2  * vim:ts=4:sw=4: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
14
15 #include "all.h"
16
17 extern xcb_connection_t *conn;
18
19 void floating_enable(Con *con, bool automatic) {
20     bool set_focus = (con == focused);
21
22     if (con_is_floating(con)) {
23         LOG("Container is already in floating mode, not doing anything.\n");
24         return;
25     }
26
27     /* 1: If the container is a workspace container, we need to create a new
28      * split-container with the same orientation and make that one floating. We
29      * cannot touch the workspace container itself because floating containers
30      * are children of the workspace. */
31     if (con->type == CT_WORKSPACE) {
32         LOG("This is a workspace, creating new container around content\n");
33         if (con_num_children(con) == 0) {
34             LOG("Workspace is empty, aborting\n");
35             return;
36         }
37         /* TODO: refactor this with src/con.c:con_set_layout */
38         Con *new = con_new(NULL, NULL);
39         new->parent = con;
40         new->orientation = con->orientation;
41
42         /* since the new container will be set into floating mode directly
43          * afterwards, we need to copy the workspace rect. */
44         memcpy(&(new->rect), &(con->rect), sizeof(Rect));
45
46         Con *old_focused = TAILQ_FIRST(&(con->focus_head));
47         if (old_focused == TAILQ_END(&(con->focus_head)))
48             old_focused = NULL;
49
50         /* 4: move the existing cons of this workspace below the new con */
51         DLOG("Moving cons\n");
52         Con *child;
53         while (!TAILQ_EMPTY(&(con->nodes_head))) {
54             child = TAILQ_FIRST(&(con->nodes_head));
55             con_detach(child);
56             con_attach(child, new, true);
57         }
58
59         /* 4: attach the new split container to the workspace */
60         DLOG("Attaching new split to ws\n");
61         con_attach(new, con, false);
62
63         if (old_focused)
64             con_focus(old_focused);
65
66         con = new;
67         set_focus = false;
68     }
69
70     /* 1: detach the container from its parent */
71     /* TODO: refactor this with tree_close() */
72     TAILQ_REMOVE(&(con->parent->nodes_head), con, nodes);
73     TAILQ_REMOVE(&(con->parent->focus_head), con, focused);
74
75     con_fix_percent(con->parent);
76
77     /* 2: create a new container to render the decoration on, add
78      * it as a floating window to the workspace */
79     Con *nc = con_new(NULL, NULL);
80     /* we need to set the parent afterwards instead of passing it as an
81      * argument to con_new() because nc would be inserted into the tiling layer
82      * otherwise. */
83     Con *ws = con_get_workspace(con);
84     nc->parent = ws;
85
86     /* check if the parent container is empty and close it if so */
87     if ((con->parent->type == CT_CON || con->parent->type == CT_FLOATING_CON) && con_num_children(con->parent) == 0) {
88         DLOG("Old container empty after setting this child to floating, closing\n");
89         tree_close(con->parent, DONT_KILL_WINDOW, false);
90     }
91
92     char *name;
93     asprintf(&name, "[i3 con] floatingcon around %p", con);
94     x_set_name(nc, name);
95     free(name);
96
97     /* find the height for the decorations */
98     int deco_height = config.font.height + 5;
99
100     DLOG("Original rect: (%d, %d) with %d x %d\n", con->rect.x, con->rect.y, con->rect.width, con->rect.height);
101     Rect zero = { 0, 0, 0, 0 };
102     nc->rect = con->geometry;
103     /* If the geometry was not set (split containers), we need to determine a
104      * sensible one by combining the geometry of all children */
105     if (memcmp(&(nc->rect), &zero, sizeof(Rect)) == 0) {
106         DLOG("Geometry not set, combining children\n");
107         Con *child;
108         TAILQ_FOREACH(child, &(con->nodes_head), nodes) {
109             DLOG("child geometry: %d x %d\n", child->geometry.width, child->geometry.height);
110             nc->rect.width += child->geometry.width;
111             nc->rect.height = max(nc->rect.height, child->geometry.height);
112         }
113     }
114     /* Raise the width/height to at least 75x50 (minimum size for windows) */
115     nc->rect.width = max(nc->rect.width, 75);
116     nc->rect.height = max(nc->rect.height, 50);
117     /* add pixels for the decoration */
118     /* TODO: don’t add them when the user automatically puts new windows into
119      * 1pixel/borderless mode */
120     nc->rect.height += deco_height + 4;
121     nc->rect.width += 4;
122
123     /* Some clients (like GIMP’s color picker window) get mapped
124      * to (0, 0), so we push them to a reasonable position
125      * (centered over their leader) */
126     if (nc->rect.x == 0 && nc->rect.y == 0) {
127         Con *leader;
128         if (con->window && con->window->leader != XCB_NONE &&
129             (leader = con_by_window_id(con->window->leader)) != NULL) {
130             DLOG("Centering above leader\n");
131             nc->rect.x = leader->rect.x + (leader->rect.width / 2) - (nc->rect.width / 2);
132             nc->rect.y = leader->rect.y + (leader->rect.height / 2) - (nc->rect.height / 2);
133         } else {
134             /* center the window on workspace as fallback */
135             nc->rect.x = ws->rect.x + (ws->rect.width / 2) - (nc->rect.width / 2);
136             nc->rect.y = ws->rect.y + (ws->rect.height / 2) - (nc->rect.height / 2);
137         }
138     }
139
140     /* Sanity check: Are the coordinates on the appropriate output? If not, we
141      * need to change them */
142     Output *current_output = get_output_containing(nc->rect.x, nc->rect.y);
143     Con *correct_output = con_get_output(ws);
144     if (!current_output || current_output->con != correct_output) {
145         DLOG("This floating window is on the wrong output, fixing coordinates (currently (%d, %d))\n",
146              nc->rect.x, nc->rect.y);
147         /* Take the relative coordinates of the current output, then add them
148          * to the coordinate space of the correct output */
149         uint32_t rel_x = (nc->rect.x - (current_output ? current_output->con->rect.x : 0));
150         uint32_t rel_y = (nc->rect.y - (current_output ? current_output->con->rect.y : 0));
151         nc->rect.x = correct_output->rect.x + rel_x;
152         nc->rect.y = correct_output->rect.y + rel_y;
153     }
154
155     DLOG("Floating rect: (%d, %d) with %d x %d\n", nc->rect.x, nc->rect.y, nc->rect.width, nc->rect.height);
156     nc->orientation = NO_ORIENTATION;
157     nc->type = CT_FLOATING_CON;
158     TAILQ_INSERT_TAIL(&(ws->floating_head), nc, floating_windows);
159     TAILQ_INSERT_TAIL(&(ws->focus_head), nc, focused);
160
161     /* 3: attach the child to the new parent container */
162     con->parent = nc;
163     con->percent = 1.0;
164     con->floating = FLOATING_USER_ON;
165
166     TAILQ_INSERT_TAIL(&(nc->nodes_head), con, nodes);
167     TAILQ_INSERT_TAIL(&(nc->focus_head), con, focused);
168
169     /* render the cons to get initial window_rect correct */
170     render_con(nc, false);
171     render_con(con, false);
172
173     if (set_focus)
174         con_focus(con);
175
176     /* Check if we need to re-assign it to a different workspace because of its
177      * coordinates and exit if that was done successfully. */
178     if (floating_maybe_reassign_ws(nc))
179         return;
180
181     /* Sanitize coordinates: Check if they are on any output */
182     if (get_output_containing(nc->rect.x, nc->rect.y) != NULL)
183         return;
184
185     ELOG("No output found at destination coordinates, centering floating window on current ws\n");
186     nc->rect.x = ws->rect.x + (ws->rect.width / 2) - (nc->rect.width / 2);
187     nc->rect.y = ws->rect.y + (ws->rect.height / 2) - (nc->rect.height / 2);
188 }
189
190 void floating_disable(Con *con, bool automatic) {
191     if (!con_is_floating(con)) {
192         LOG("Container isn't floating, not doing anything.\n");
193         return;
194     }
195
196     Con *ws = con_get_workspace(con);
197
198     /* 1: detach from parent container */
199     TAILQ_REMOVE(&(con->parent->nodes_head), con, nodes);
200     TAILQ_REMOVE(&(con->parent->focus_head), con, focused);
201
202     /* 2: kill parent container */
203     TAILQ_REMOVE(&(con->parent->parent->floating_head), con->parent, floating_windows);
204     TAILQ_REMOVE(&(con->parent->parent->focus_head), con->parent, focused);
205     tree_close(con->parent, DONT_KILL_WINDOW, false);
206
207     /* 3: re-attach to the parent of the currently focused con on the workspace
208      * this floating con was on */
209     Con *focused = con_descend_tiling_focused(ws);
210
211     /* if there is no other container on this workspace, focused will be the
212      * workspace itself */
213     if (focused->type == CT_WORKSPACE)
214         con->parent = focused;
215     else con->parent = focused->parent;
216
217     /* con_fix_percent will adjust the percent value */
218     con->percent = 0.0;
219
220     TAILQ_INSERT_TAIL(&(con->parent->nodes_head), con, nodes);
221     TAILQ_INSERT_TAIL(&(con->parent->focus_head), con, focused);
222
223     con->floating = FLOATING_USER_OFF;
224
225     con_fix_percent(con->parent);
226     // TODO: don’t influence focus handling when Con was not focused before.
227     con_focus(con);
228 }
229
230 /*
231  * Toggles floating mode for the given container.
232  *
233  * If the automatic flag is set to true, this was an automatic update by a change of the
234  * window class from the application which can be overwritten by the user.
235  *
236  */
237 void toggle_floating_mode(Con *con, bool automatic) {
238     /* see if the client is already floating */
239     if (con_is_floating(con)) {
240         LOG("already floating, re-setting to tiling\n");
241
242         floating_disable(con, automatic);
243         return;
244     }
245
246     floating_enable(con, automatic);
247 }
248
249 /*
250  * Raises the given container in the list of floating containers
251  *
252  */
253 void floating_raise_con(Con *con) {
254     DLOG("Raising floating con %p / %s\n", con, con->name);
255     TAILQ_REMOVE(&(con->parent->floating_head), con, floating_windows);
256     TAILQ_INSERT_TAIL(&(con->parent->floating_head), con, floating_windows);
257 }
258
259 /*
260  * Checks if con’s coordinates are within its workspace and re-assigns it to
261  * the actual workspace if not.
262  *
263  */
264 bool floating_maybe_reassign_ws(Con *con) {
265     Output *output = get_output_containing(
266         con->rect.x + (con->rect.width / 2),
267         con->rect.y + (con->rect.height / 2));
268
269     if (!output) {
270         ELOG("No output found at destination coordinates?\n");
271         return false;
272     }
273
274     if (con_get_output(con) == output->con) {
275         DLOG("still the same ws\n");
276         return false;
277     }
278
279     DLOG("Need to re-assign!\n");
280
281     Con *content = output_get_content(output->con);
282     Con *ws = TAILQ_FIRST(&(content->focus_head));
283     DLOG("Moving con %p / %s to workspace %p / %s\n", con, con->name, ws, ws->name);
284     con_move_to_workspace(con, ws, true);
285     con_focus(con_descend_focused(con));
286     return true;
287 }
288
289 DRAGGING_CB(drag_window_callback) {
290     struct xcb_button_press_event_t *event = extra;
291
292     /* Reposition the client correctly while moving */
293     con->rect.x = old_rect->x + (new_x - event->root_x);
294     con->rect.y = old_rect->y + (new_y - event->root_y);
295
296     render_con(con, false);
297     x_push_node(con);
298     xcb_flush(conn);
299
300     /* Check if we cross workspace boundaries while moving */
301     if (!floating_maybe_reassign_ws(con))
302         return;
303     tree_render();
304 }
305
306 /*
307  * Called when the user clicked on the titlebar of a floating window.
308  * Calls the drag_pointer function with the drag_window callback
309  *
310  */
311 void floating_drag_window(Con *con, xcb_button_press_event_t *event) {
312     DLOG("floating_drag_window\n");
313
314     /* Push changes before dragging, so that the window gets raised now and not
315      * after the user releases the mouse button */
316     tree_render();
317
318     /* Drag the window */
319     drag_pointer(con, event, XCB_NONE, BORDER_TOP /* irrelevant */, drag_window_callback, event);
320     tree_render();
321 }
322
323 /*
324  * This is an ugly data structure which we need because there is no standard
325  * way of having nested functions (only available as a gcc extension at the
326  * moment, clang doesn’t support it) or blocks (only available as a clang
327  * extension and only on Mac OS X systems at the moment).
328  *
329  */
330 struct resize_window_callback_params {
331     border_t corner;
332     bool proportional;
333     xcb_button_press_event_t *event;
334 };
335
336 DRAGGING_CB(resize_window_callback) {
337     struct resize_window_callback_params *params = extra;
338     xcb_button_press_event_t *event = params->event;
339     border_t corner = params->corner;
340
341     int32_t dest_x = con->rect.x;
342     int32_t dest_y = con->rect.y;
343     uint32_t dest_width;
344     uint32_t dest_height;
345
346     double ratio = (double) old_rect->width / old_rect->height;
347
348     /* First guess: We resize by exactly the amount the mouse moved,
349      * taking into account in which corner the client was grabbed */
350     if (corner & BORDER_LEFT)
351         dest_width = old_rect->width - (new_x - event->root_x);
352     else dest_width = old_rect->width + (new_x - event->root_x);
353
354     if (corner & BORDER_TOP)
355         dest_height = old_rect->height - (new_y - event->root_y);
356     else dest_height = old_rect->height + (new_y - event->root_y);
357
358     /* Obey minimum window size */
359     Rect minimum = con_minimum_size(con);
360     dest_width = max(dest_width, minimum.width);
361     dest_height = max(dest_height, minimum.height);
362
363     /* User wants to keep proportions, so we may have to adjust our values */
364     if (params->proportional) {
365         dest_width = max(dest_width, (int) (dest_height * ratio));
366         dest_height = max(dest_height, (int) (dest_width / ratio));
367     }
368
369     /* If not the lower right corner is grabbed, we must also reposition
370      * the client by exactly the amount we resized it */
371     if (corner & BORDER_LEFT)
372         dest_x = old_rect->x + (old_rect->width - dest_width);
373
374     if (corner & BORDER_TOP)
375         dest_y = old_rect->y + (old_rect->height - dest_height);
376
377     con->rect = (Rect) { dest_x, dest_y, dest_width, dest_height };
378
379     /* TODO: don’t re-render the whole tree just because we change
380      * coordinates of a floating window */
381     tree_render();
382     x_push_changes(croot);
383 }
384
385 /*
386  * Called when the user clicked on a floating window while holding the
387  * floating_modifier and the right mouse button.
388  * Calls the drag_pointer function with the resize_window callback
389  *
390  */
391 void floating_resize_window(Con *con, bool proportional,
392                             xcb_button_press_event_t *event) {
393     DLOG("floating_resize_window\n");
394
395     /* corner saves the nearest corner to the original click. It contains
396      * a bitmask of the nearest borders (BORDER_LEFT, BORDER_RIGHT, …) */
397     border_t corner = 0;
398
399     if (event->event_x <= (con->rect.width / 2))
400         corner |= BORDER_LEFT;
401     else corner |= BORDER_RIGHT;
402
403     if (event->event_y <= (con->rect.height / 2))
404         corner |= BORDER_TOP;
405     else corner |= BORDER_BOTTOM;
406
407     struct resize_window_callback_params params = { corner, proportional, event };
408
409     drag_pointer(con, event, XCB_NONE, BORDER_TOP /* irrelevant */, resize_window_callback, &params);
410 }
411
412 /*
413  * This function grabs your pointer and lets you drag stuff around (borders).
414  * Every time you move your mouse, an XCB_MOTION_NOTIFY event will be received
415  * and the given callback will be called with the parameters specified (client,
416  * border on which the click originally was), the original rect of the client,
417  * the event and the new coordinates (x, y).
418  *
419  */
420 void drag_pointer(Con *con, xcb_button_press_event_t *event, xcb_window_t
421                 confine_to, border_t border, callback_t callback, void *extra)
422 {
423     uint32_t new_x, new_y;
424     Rect old_rect;
425     if (con != NULL)
426         memcpy(&old_rect, &(con->rect), sizeof(Rect));
427
428     /* Grab the pointer */
429     xcb_grab_pointer_cookie_t cookie;
430     xcb_grab_pointer_reply_t *reply;
431     cookie = xcb_grab_pointer(conn,
432         false,               /* get all pointer events specified by the following mask */
433         root,                /* grab the root window */
434         XCB_EVENT_MASK_BUTTON_RELEASE | XCB_EVENT_MASK_POINTER_MOTION, /* which events to let through */
435         XCB_GRAB_MODE_ASYNC, /* pointer events should continue as normal */
436         XCB_GRAB_MODE_ASYNC, /* keyboard mode */
437         confine_to,          /* confine_to = in which window should the cursor stay */
438         XCB_NONE,            /* don’t display a special cursor */
439         XCB_CURRENT_TIME);
440
441     if ((reply = xcb_grab_pointer_reply(conn, cookie, NULL)) == NULL) {
442         ELOG("Could not grab pointer\n");
443         return;
444     }
445
446     free(reply);
447
448     /* Go into our own event loop */
449     xcb_flush(conn);
450
451     xcb_generic_event_t *inside_event, *last_motion_notify = NULL;
452     bool loop_done = false;
453     /* I’ve always wanted to have my own eventhandler… */
454     while (!loop_done && (inside_event = xcb_wait_for_event(conn))) {
455         /* We now handle all events we can get using xcb_poll_for_event */
456         do {
457             /* skip x11 errors */
458             if (inside_event->response_type == 0) {
459                 free(inside_event);
460                 continue;
461             }
462             /* Strip off the highest bit (set if the event is generated) */
463             int type = (inside_event->response_type & 0x7F);
464
465             switch (type) {
466                 case XCB_BUTTON_RELEASE:
467                     loop_done = true;
468                     break;
469
470                 case XCB_MOTION_NOTIFY:
471                     /* motion_notify events are saved for later */
472                     FREE(last_motion_notify);
473                     last_motion_notify = inside_event;
474                     break;
475
476                 case XCB_UNMAP_NOTIFY:
477                 case XCB_KEY_PRESS:
478                 case XCB_KEY_RELEASE:
479                     DLOG("Unmap-notify, aborting\n");
480                     handle_event(type, inside_event);
481                     loop_done = true;
482                     break;
483
484                 default:
485                     DLOG("Passing to original handler\n");
486                     /* Use original handler */
487                     handle_event(type, inside_event);
488                     break;
489             }
490             if (last_motion_notify != inside_event)
491                 free(inside_event);
492         } while ((inside_event = xcb_poll_for_event(conn)) != NULL);
493
494         if (last_motion_notify == NULL)
495             continue;
496
497         new_x = ((xcb_motion_notify_event_t*)last_motion_notify)->root_x;
498         new_y = ((xcb_motion_notify_event_t*)last_motion_notify)->root_y;
499
500         callback(con, &old_rect, new_x, new_y, extra);
501         FREE(last_motion_notify);
502     }
503
504     xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
505     xcb_flush(conn);
506 }
507
508 #if 0
509 /*
510  * Changes focus in the given direction for floating clients.
511  *
512  * Changing to the left/right means going to the previous/next floating client,
513  * changing to top/bottom means cycling through the Z-index.
514  *
515  */
516 void floating_focus_direction(xcb_connection_t *conn, Client *currently_focused, direction_t direction) {
517         DLOG("floating focus\n");
518
519         if (direction == D_LEFT || direction == D_RIGHT) {
520                 /* Go to the next/previous floating client */
521                 Client *client;
522
523                 while ((client = (direction == D_LEFT ? TAILQ_PREV(currently_focused, floating_clients_head, floating_clients) :
524                                                         TAILQ_NEXT(currently_focused, floating_clients))) !=
525                        TAILQ_END(&(currently_focused->workspace->floating_clients))) {
526                         if (!client->floating)
527                                 continue;
528                         set_focus(conn, client, true);
529                         return;
530                 }
531         }
532 }
533
534 /*
535  * Moves the client 10px to the specified direction.
536  *
537  */
538 void floating_move(xcb_connection_t *conn, Client *currently_focused, direction_t direction) {
539         DLOG("floating move\n");
540
541         Rect destination = currently_focused->rect;
542         Rect *screen = &(currently_focused->workspace->output->rect);
543
544         switch (direction) {
545                 case D_LEFT:
546                         destination.x -= 10;
547                         break;
548                 case D_RIGHT:
549                         destination.x += 10;
550                         break;
551                 case D_UP:
552                         destination.y -= 10;
553                         break;
554                 case D_DOWN:
555                         destination.y += 10;
556                         break;
557                 /* to make static analyzers happy */
558                 default:
559                         break;
560         }
561
562         /* Prevent windows from vanishing completely */
563         if ((int32_t)(destination.x + destination.width - 5) <= (int32_t)screen->x ||
564             (int32_t)(destination.x + 5) >= (int32_t)(screen->x + screen->width) ||
565             (int32_t)(destination.y + destination.height - 5) <= (int32_t)screen->y ||
566             (int32_t)(destination.y + 5) >= (int32_t)(screen->y + screen->height)) {
567                 DLOG("boundary check failed, not moving\n");
568                 return;
569         }
570
571         currently_focused->rect = destination;
572         reposition_client(conn, currently_focused);
573
574         /* Because reposition_client does not send a faked configure event (only resize does),
575          * we need to initiate that on our own */
576         fake_absolute_configure_notify(conn, currently_focused);
577         /* fake_absolute_configure_notify flushes */
578 }
579
580 /*
581  * Hides all floating clients (or show them if they are currently hidden) on
582  * the specified workspace.
583  *
584  */
585 void floating_toggle_hide(xcb_connection_t *conn, Workspace *workspace) {
586         Client *client;
587
588         workspace->floating_hidden = !workspace->floating_hidden;
589         DLOG("floating_hidden is now: %d\n", workspace->floating_hidden);
590         TAILQ_FOREACH(client, &(workspace->floating_clients), floating_clients) {
591                 if (workspace->floating_hidden)
592                         client_unmap(conn, client);
593                 else client_map(conn, client);
594         }
595
596         /* If we just unmapped all floating windows we should ensure that the focus
597          * is set correctly, that ist, to the first non-floating client in stack */
598         if (workspace->floating_hidden)
599                 SLIST_FOREACH(client, &(workspace->focus_stack), focus_clients) {
600                         if (client_is_floating(client))
601                                 continue;
602                         set_focus(conn, client, true);
603                         return;
604                 }
605
606         xcb_flush(conn);
607 }
608 #endif