]> git.sur5r.net Git - i3/i3/blob - src/manage.c
Focus new window only if it is on a visible workspace on the current focused output.
[i3/i3] / src / manage.c
1 /*
2  * vim:ts=4:sw=4:expandtab
3  *
4  * i3 - an improved dynamic tiling window manager
5  * © 2009-2011 Michael Stapelberg and contributors (see also: LICENSE)
6  *
7  * manage.c: Contains all functions for initially managing new windows
8  *           (or existing ones on restart).
9  *
10  */
11
12 #include "all.h"
13
14 /*
15  * Go through all existing windows (if the window manager is restarted) and manage them
16  *
17  */
18 void manage_existing_windows(xcb_window_t root) {
19     xcb_query_tree_reply_t *reply;
20     int i, len;
21     xcb_window_t *children;
22     xcb_get_window_attributes_cookie_t *cookies;
23
24     /* Get the tree of windows whose parent is the root window (= all) */
25     if ((reply = xcb_query_tree_reply(conn, xcb_query_tree(conn, root), 0)) == NULL)
26         return;
27
28     len = xcb_query_tree_children_length(reply);
29     cookies = smalloc(len * sizeof(*cookies));
30
31     /* Request the window attributes for every window */
32     children = xcb_query_tree_children(reply);
33     for (i = 0; i < len; ++i)
34         cookies[i] = xcb_get_window_attributes(conn, children[i]);
35
36     /* Call manage_window with the attributes for every window */
37     for (i = 0; i < len; ++i)
38         manage_window(children[i], cookies[i], true);
39
40     free(reply);
41     free(cookies);
42 }
43
44 /*
45  * Restores the geometry of each window by reparenting it to the root window
46  * at the position of its frame.
47  *
48  * This is to be called *only* before exiting/restarting i3 because of evil
49  * side-effects which are to be expected when continuing to run i3.
50  *
51  */
52 void restore_geometry() {
53     DLOG("Restoring geometry\n");
54
55     Con *con;
56     TAILQ_FOREACH(con, &all_cons, all_cons)
57         if (con->window) {
58             DLOG("Re-adding X11 border of %d px\n", con->border_width);
59             con->window_rect.width += (2 * con->border_width);
60             con->window_rect.height += (2 * con->border_width);
61             xcb_set_window_rect(conn, con->window->id, con->window_rect);
62             DLOG("placing window %08x at %d %d\n", con->window->id, con->rect.x, con->rect.y);
63             xcb_reparent_window(conn, con->window->id, root,
64                                 con->rect.x, con->rect.y);
65         }
66
67     /* Make sure our changes reach the X server, we restart/exit now */
68     xcb_flush(conn);
69 }
70
71 /*
72  * Do some sanity checks and then reparent the window.
73  *
74  */
75 void manage_window(xcb_window_t window, xcb_get_window_attributes_cookie_t cookie,
76                    bool needs_to_be_mapped) {
77     xcb_drawable_t d = { window };
78     xcb_get_geometry_cookie_t geomc;
79     xcb_get_geometry_reply_t *geom;
80     xcb_get_window_attributes_reply_t *attr = NULL;
81
82     DLOG("---> looking at window 0x%08x\n", window);
83
84     xcb_get_property_cookie_t wm_type_cookie, strut_cookie, state_cookie,
85                               utf8_title_cookie, title_cookie,
86                               class_cookie, leader_cookie, transient_cookie,
87                               role_cookie, startup_id_cookie;
88
89
90     geomc = xcb_get_geometry(conn, d);
91 #define FREE_GEOMETRY() do { \
92     if ((geom = xcb_get_geometry_reply(conn, geomc, 0)) != NULL) \
93         free(geom); \
94 } while (0)
95
96     /* Check if the window is mapped (it could be not mapped when intializing and
97        calling manage_window() for every window) */
98     if ((attr = xcb_get_window_attributes_reply(conn, cookie, 0)) == NULL) {
99         DLOG("Could not get attributes\n");
100         FREE_GEOMETRY();
101         return;
102     }
103
104     if (needs_to_be_mapped && attr->map_state != XCB_MAP_STATE_VIEWABLE) {
105         DLOG("map_state unviewable\n");
106         FREE_GEOMETRY();
107         goto out;
108     }
109
110     /* Don’t manage clients with the override_redirect flag */
111     DLOG("override_redirect is %d\n", attr->override_redirect);
112     if (attr->override_redirect) {
113         FREE_GEOMETRY();
114         goto out;
115     }
116
117     /* Check if the window is already managed */
118     if (con_by_window_id(window) != NULL) {
119         DLOG("already managed (by con %p)\n", con_by_window_id(window));
120         FREE_GEOMETRY();
121         goto out;
122     }
123
124     /* Get the initial geometry (position, size, …) */
125     if ((geom = xcb_get_geometry_reply(conn, geomc, 0)) == NULL) {
126         DLOG("could not get geometry\n");
127         goto out;
128     }
129
130     uint32_t values[1];
131
132     /* Set a temporary event mask for the new window, consisting only of
133      * PropertyChange. We need to be notified of PropertyChanges because the
134      * client can change its properties *after* we requested them but *before*
135      * we actually reparented it and have set our final event mask. */
136     values[0] = XCB_EVENT_MASK_PROPERTY_CHANGE;
137     xcb_change_window_attributes(conn, window, XCB_CW_EVENT_MASK, values);
138
139 #define GET_PROPERTY(atom, len) xcb_get_property(conn, false, window, atom, XCB_GET_PROPERTY_TYPE_ANY, 0, len)
140
141     wm_type_cookie = GET_PROPERTY(A__NET_WM_WINDOW_TYPE, UINT32_MAX);
142     strut_cookie = GET_PROPERTY(A__NET_WM_STRUT_PARTIAL, UINT32_MAX);
143     state_cookie = GET_PROPERTY(A__NET_WM_STATE, UINT32_MAX);
144     utf8_title_cookie = GET_PROPERTY(A__NET_WM_NAME, 128);
145     leader_cookie = GET_PROPERTY(A_WM_CLIENT_LEADER, UINT32_MAX);
146     transient_cookie = GET_PROPERTY(XCB_ATOM_WM_TRANSIENT_FOR, UINT32_MAX);
147     title_cookie = GET_PROPERTY(XCB_ATOM_WM_NAME, 128);
148     class_cookie = GET_PROPERTY(XCB_ATOM_WM_CLASS, 128);
149     role_cookie = GET_PROPERTY(A_WM_WINDOW_ROLE, 128);
150     startup_id_cookie = GET_PROPERTY(A__NET_STARTUP_ID, 512);
151     /* TODO: also get wm_normal_hints here. implement after we got rid of xcb-event */
152
153     DLOG("reparenting!\n");
154
155     i3Window *cwindow = scalloc(sizeof(i3Window));
156     cwindow->id = window;
157
158     /* We need to grab the mouse buttons for click to focus */
159     xcb_grab_button(conn, false, window, XCB_EVENT_MASK_BUTTON_PRESS,
160                     XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_ASYNC, root, XCB_NONE,
161                     1 /* left mouse button */,
162                     XCB_BUTTON_MASK_ANY /* don’t filter for any modifiers */);
163
164     xcb_grab_button(conn, false, window, XCB_EVENT_MASK_BUTTON_PRESS,
165                     XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_ASYNC, root, XCB_NONE,
166                     3 /* right mouse button */,
167                     XCB_BUTTON_MASK_ANY /* don’t filter for any modifiers */);
168
169
170     /* update as much information as possible so far (some replies may be NULL) */
171     window_update_class(cwindow, xcb_get_property_reply(conn, class_cookie, NULL), true);
172     window_update_name_legacy(cwindow, xcb_get_property_reply(conn, title_cookie, NULL), true);
173     window_update_name(cwindow, xcb_get_property_reply(conn, utf8_title_cookie, NULL), true);
174     window_update_leader(cwindow, xcb_get_property_reply(conn, leader_cookie, NULL));
175     window_update_transient_for(cwindow, xcb_get_property_reply(conn, transient_cookie, NULL));
176     window_update_strut_partial(cwindow, xcb_get_property_reply(conn, strut_cookie, NULL));
177     window_update_role(cwindow, xcb_get_property_reply(conn, role_cookie, NULL), true);
178
179     xcb_get_property_reply_t *startup_id_reply;
180     startup_id_reply = xcb_get_property_reply(conn, startup_id_cookie, NULL);
181     char *startup_ws = startup_workspace_for_window(cwindow, startup_id_reply);
182     DLOG("startup workspace = %s\n", startup_ws);
183
184     /* check if the window needs WM_TAKE_FOCUS */
185     cwindow->needs_take_focus = window_supports_protocol(cwindow->id, A_WM_TAKE_FOCUS);
186
187     /* Where to start searching for a container that swallows the new one? */
188     Con *search_at = croot;
189
190     xcb_get_property_reply_t *reply = xcb_get_property_reply(conn, wm_type_cookie, NULL);
191     if (xcb_reply_contains_atom(reply, A__NET_WM_WINDOW_TYPE_DOCK)) {
192         LOG("This window is of type dock\n");
193         Output *output = get_output_containing(geom->x, geom->y);
194         if (output != NULL) {
195             DLOG("Starting search at output %s\n", output->name);
196             search_at = output->con;
197         }
198
199         /* find out the desired position of this dock window */
200         if (cwindow->reserved.top > 0 && cwindow->reserved.bottom == 0) {
201             DLOG("Top dock client\n");
202             cwindow->dock = W_DOCK_TOP;
203         } else if (cwindow->reserved.top == 0 && cwindow->reserved.bottom > 0) {
204             DLOG("Bottom dock client\n");
205             cwindow->dock = W_DOCK_BOTTOM;
206         } else {
207             DLOG("Ignoring invalid reserved edges (_NET_WM_STRUT_PARTIAL), using position as fallback:\n");
208             if (geom->y < (search_at->rect.height / 2)) {
209                 DLOG("geom->y = %d < rect.height / 2 = %d, it is a top dock client\n",
210                      geom->y, (search_at->rect.height / 2));
211                 cwindow->dock = W_DOCK_TOP;
212             } else {
213                 DLOG("geom->y = %d >= rect.height / 2 = %d, it is a bottom dock client\n",
214                      geom->y, (search_at->rect.height / 2));
215                 cwindow->dock = W_DOCK_BOTTOM;
216             }
217         }
218     }
219
220     DLOG("Initial geometry: (%d, %d, %d, %d)\n", geom->x, geom->y, geom->width, geom->height);
221
222     Con *nc = NULL;
223     Match *match;
224     Assignment *assignment;
225
226     /* TODO: two matches for one container */
227
228     /* See if any container swallows this new window */
229     nc = con_for_window(search_at, cwindow, &match);
230     if (nc == NULL) {
231         /* If not, check if it is assigned to a specific workspace / output */
232         if ((assignment = assignment_for(cwindow, A_TO_WORKSPACE | A_TO_OUTPUT))) {
233             DLOG("Assignment matches (%p)\n", match);
234             if (assignment->type == A_TO_WORKSPACE) {
235                 nc = con_descend_tiling_focused(workspace_get(assignment->dest.workspace, NULL));
236                 DLOG("focused on ws %s: %p / %s\n", assignment->dest.workspace, nc, nc->name);
237                 if (nc->type == CT_WORKSPACE)
238                     nc = tree_open_con(nc, cwindow);
239                 else nc = tree_open_con(nc->parent, cwindow);
240             }
241         /* TODO: handle assignments with type == A_TO_OUTPUT */
242         } else if (startup_ws) {
243             /* If it’s not assigned, but was started on a specific workspace,
244              * we want to open it there */
245             DLOG("Using workspace on which this application was started (%s)\n", startup_ws);
246             nc = con_descend_tiling_focused(workspace_get(startup_ws, NULL));
247             DLOG("focused on ws %s: %p / %s\n", startup_ws, nc, nc->name);
248             if (nc->type == CT_WORKSPACE)
249                 nc = tree_open_con(nc, cwindow);
250             else nc = tree_open_con(nc->parent, cwindow);
251         } else {
252             /* If not, insert it at the currently focused position */
253             if (focused->type == CT_CON && con_accepts_window(focused)) {
254                 LOG("using current container, focused = %p, focused->name = %s\n",
255                                 focused, focused->name);
256                 nc = focused;
257             } else nc = tree_open_con(NULL, cwindow);
258         }
259     } else {
260         /* M_BELOW inserts the new window as a child of the one which was
261          * matched (e.g. dock areas) */
262         if (match != NULL && match->insert_where == M_BELOW) {
263             nc = tree_open_con(nc, cwindow);
264         }
265     }
266
267     DLOG("new container = %p\n", nc);
268     nc->window = cwindow;
269     x_reinit(nc);
270
271     nc->border_width = geom->border_width;
272
273     char *name;
274     asprintf(&name, "[i3 con] container around %p", cwindow);
275     x_set_name(nc, name);
276     free(name);
277
278     Con *ws = con_get_workspace(nc);
279     Con *fs = (ws ? con_get_fullscreen_con(ws, CF_OUTPUT) : NULL);
280     if (fs == NULL)
281         fs = con_get_fullscreen_con(croot, CF_GLOBAL);
282
283     if (fs == NULL) {
284         DLOG("Not in fullscreen mode, focusing\n");
285         if (!cwindow->dock) {
286             /* Check that the workspace is visible and on the same output as
287              * the current focused container. If the window was assigned to an
288              * invisible workspace, we should not steal focus. */
289             Con *current_output = con_get_output(focused);
290             Con *target_output = con_get_output(ws);
291
292             if (workspace_is_visible(ws) && current_output == target_output) {
293                 con_focus(nc);
294             } else DLOG("workspace not visible, not focusing\n");
295         } else DLOG("dock, not focusing\n");
296     } else {
297         DLOG("fs = %p, ws = %p, not focusing\n", fs, ws);
298         /* Insert the new container in focus stack *after* the currently
299          * focused (fullscreen) con. This way, the new container will be
300          * focused after we return from fullscreen mode */
301         Con *first = TAILQ_FIRST(&(nc->parent->focus_head));
302         if (first != nc) {
303             /* We only modify the focus stack if the container is not already
304              * the first one. This can happen when existing containers swallow
305              * new windows, for example when restarting. */
306             TAILQ_REMOVE(&(nc->parent->focus_head), nc, focused);
307             TAILQ_INSERT_AFTER(&(nc->parent->focus_head), first, nc, focused);
308         }
309     }
310
311     /* set floating if necessary */
312     bool want_floating = false;
313     if (xcb_reply_contains_atom(reply, A__NET_WM_WINDOW_TYPE_DIALOG) ||
314         xcb_reply_contains_atom(reply, A__NET_WM_WINDOW_TYPE_UTILITY) ||
315         xcb_reply_contains_atom(reply, A__NET_WM_WINDOW_TYPE_TOOLBAR) ||
316         xcb_reply_contains_atom(reply, A__NET_WM_WINDOW_TYPE_SPLASH)) {
317         LOG("This window is a dialog window, setting floating\n");
318         want_floating = true;
319     }
320
321     FREE(reply);
322
323     if (cwindow->transient_for != XCB_NONE ||
324         (cwindow->leader != XCB_NONE &&
325          cwindow->leader != cwindow->id &&
326          con_by_window_id(cwindow->leader) != NULL)) {
327         LOG("This window is transiert for another window, setting floating\n");
328         want_floating = true;
329
330         if (config.popup_during_fullscreen == PDF_LEAVE_FULLSCREEN &&
331             fs != NULL) {
332             LOG("There is a fullscreen window, leaving fullscreen mode\n");
333             con_toggle_fullscreen(fs, CF_OUTPUT);
334         }
335     }
336
337     /* dock clients cannot be floating, that makes no sense */
338     if (cwindow->dock)
339         want_floating = false;
340
341     /* Store the requested geometry. The width/height gets raised to at least
342      * 75x50 when entering floating mode, which is the minimum size for a
343      * window to be useful (smaller windows are usually overlays/toolbars/…
344      * which are not managed by the wm anyways). We store the original geometry
345      * here because it’s used for dock clients. */
346     nc->geometry = (Rect){ geom->x, geom->y, geom->width, geom->height };
347
348     if (want_floating) {
349         DLOG("geometry = %d x %d\n", nc->geometry.width, nc->geometry.height);
350         floating_enable(nc, true);
351     }
352
353     /* to avoid getting an UnmapNotify event due to reparenting, we temporarily
354      * declare no interest in any state change event of this window */
355     values[0] = XCB_NONE;
356     xcb_change_window_attributes(conn, window, XCB_CW_EVENT_MASK, values);
357
358     xcb_void_cookie_t rcookie = xcb_reparent_window_checked(conn, window, nc->frame, 0, 0);
359     if (xcb_request_check(conn, rcookie) != NULL) {
360         LOG("Could not reparent the window, aborting\n");
361         goto geom_out;
362     }
363
364     values[0] = CHILD_EVENT_MASK & ~XCB_EVENT_MASK_ENTER_WINDOW;
365     xcb_change_window_attributes(conn, window, XCB_CW_EVENT_MASK, values);
366     xcb_flush(conn);
367
368     reply = xcb_get_property_reply(conn, state_cookie, NULL);
369     if (xcb_reply_contains_atom(reply, A__NET_WM_STATE_FULLSCREEN))
370         con_toggle_fullscreen(nc, CF_OUTPUT);
371
372     FREE(reply);
373
374     /* Put the client inside the save set. Upon termination (whether killed or
375      * normal exit does not matter) of the window manager, these clients will
376      * be correctly reparented to their most closest living ancestor (=
377      * cleanup) */
378     xcb_change_save_set(conn, XCB_SET_MODE_INSERT, window);
379
380     /* Check if any assignments match */
381     run_assignments(cwindow);
382
383     tree_render();
384
385 geom_out:
386     free(geom);
387 out:
388     free(attr);
389     return;
390 }