2 #define I3__FILE__ "manage.c"
15 #include <yajl/yajl_gen.h>
22 xcb_query_tree_reply_t *reply;
24 xcb_window_t *children;
25 xcb_get_window_attributes_cookie_t *cookies;
28 if ((reply = xcb_query_tree_reply(
conn, xcb_query_tree(
conn, root), 0)) == NULL)
31 len = xcb_query_tree_children_length(reply);
32 cookies =
smalloc(len *
sizeof(*cookies));
35 children = xcb_query_tree_children(reply);
36 for (i = 0; i < len; ++i)
37 cookies[i] = xcb_get_window_attributes(
conn, children[i]);
40 for (i = 0; i < len; ++i)
56 DLOG(
"Restoring geometry\n");
72 xcb_change_window_attributes(
conn,
root, XCB_CW_EVENT_MASK, (uint32_t[]){ XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT });
85 setlocale(LC_NUMERIC,
"C");
98 const unsigned char *payload;
100 y(get_buf, &payload, &length);
102 ipc_send_event(
"window", I3_IPC_EVENT_WINDOW, (
const char *)payload);
104 setlocale(LC_NUMERIC,
"");
111 void manage_window(xcb_window_t window, xcb_get_window_attributes_cookie_t cookie,
112 bool needs_to_be_mapped) {
113 xcb_drawable_t d = { window };
114 xcb_get_geometry_cookie_t geomc;
115 xcb_get_geometry_reply_t *geom;
116 xcb_get_window_attributes_reply_t *attr = NULL;
118 xcb_get_property_cookie_t wm_type_cookie, strut_cookie, state_cookie,
119 utf8_title_cookie, title_cookie,
120 class_cookie, leader_cookie, transient_cookie,
121 role_cookie, startup_id_cookie, wm_hints_cookie;
124 geomc = xcb_get_geometry(
conn, d);
125 #define FREE_GEOMETRY() do { \
126 if ((geom = xcb_get_geometry_reply(conn, geomc, 0)) != NULL) \
132 if ((attr = xcb_get_window_attributes_reply(
conn, cookie, 0)) == NULL) {
133 DLOG(
"Could not get attributes\n");
138 if (needs_to_be_mapped && attr->map_state != XCB_MAP_STATE_VIEWABLE) {
144 if (attr->override_redirect) {
157 if ((geom = xcb_get_geometry_reply(
conn, geomc, 0)) == NULL) {
158 DLOG(
"could not get geometry\n");
173 values[0] = XCB_EVENT_MASK_PROPERTY_CHANGE |
174 XCB_EVENT_MASK_STRUCTURE_NOTIFY;
175 xcb_void_cookie_t event_mask_cookie =
176 xcb_change_window_attributes_checked(
conn, window, XCB_CW_EVENT_MASK, values);
177 if (xcb_request_check(
conn, event_mask_cookie) != NULL) {
178 LOG(
"Could not change event mask, the window probably already disappeared.\n");
182 #define GET_PROPERTY(atom, len) xcb_get_property(conn, false, window, atom, XCB_GET_PROPERTY_TYPE_ANY, 0, len)
184 wm_type_cookie =
GET_PROPERTY(A__NET_WM_WINDOW_TYPE, UINT32_MAX);
185 strut_cookie =
GET_PROPERTY(A__NET_WM_STRUT_PARTIAL, UINT32_MAX);
186 state_cookie =
GET_PROPERTY(A__NET_WM_STATE, UINT32_MAX);
188 leader_cookie =
GET_PROPERTY(A_WM_CLIENT_LEADER, UINT32_MAX);
193 startup_id_cookie =
GET_PROPERTY(A__NET_STARTUP_ID, 512);
197 DLOG(
"Managing window 0x%08x\n", window);
200 cwindow->
id = window;
204 xcb_grab_button(
conn,
false, window, XCB_EVENT_MASK_BUTTON_PRESS,
205 XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_ASYNC,
root, XCB_NONE,
207 XCB_BUTTON_MASK_ANY );
209 xcb_grab_button(
conn,
false, window, XCB_EVENT_MASK_BUTTON_PRESS,
210 XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_ASYNC,
root, XCB_NONE,
212 XCB_BUTTON_MASK_ANY );
214 xcb_grab_button(
conn,
false, window, XCB_EVENT_MASK_BUTTON_PRESS,
215 XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_ASYNC,
root, XCB_NONE,
217 XCB_BUTTON_MASK_ANY );
229 xcb_get_property_reply_t *startup_id_reply;
230 startup_id_reply = xcb_get_property_reply(
conn, startup_id_cookie, NULL);
232 DLOG(
"startup workspace = %s\n", startup_ws);
240 xcb_get_property_reply_t *reply = xcb_get_property_reply(
conn, wm_type_cookie, NULL);
242 LOG(
"This window is of type dock\n");
244 if (output != NULL) {
245 DLOG(
"Starting search at output %s\n", output->
name);
246 search_at = output->
con;
251 DLOG(
"Top dock client\n");
252 cwindow->
dock = W_DOCK_TOP;
254 DLOG(
"Bottom dock client\n");
255 cwindow->
dock = W_DOCK_BOTTOM;
257 DLOG(
"Ignoring invalid reserved edges (_NET_WM_STRUT_PARTIAL), using position as fallback:\n");
259 DLOG(
"geom->y = %d < rect.height / 2 = %d, it is a top dock client\n",
261 cwindow->
dock = W_DOCK_TOP;
263 DLOG(
"geom->y = %d >= rect.height / 2 = %d, it is a bottom dock client\n",
265 cwindow->
dock = W_DOCK_BOTTOM;
270 DLOG(
"Initial geometry: (%d, %d, %d, %d)\n", geom->x, geom->y, geom->width, geom->height);
282 if ((assignment =
assignment_for(cwindow, A_TO_WORKSPACE | A_TO_OUTPUT))) {
283 DLOG(
"Assignment matches (%p)\n", match);
284 if (assignment->
type == A_TO_WORKSPACE) {
287 if (nc->
type == CT_WORKSPACE)
292 }
else if (startup_ws) {
295 DLOG(
"Using workspace on which this application was started (%s)\n", startup_ws);
297 DLOG(
"focused on ws %s: %p / %s\n", startup_ws, nc, nc->
name);
298 if (nc->
type == CT_WORKSPACE)
304 LOG(
"using current container, focused = %p, focused->name = %s\n",
317 DLOG(
"new container = %p\n", nc);
324 sasprintf(&name,
"[i3 con] container around %p", cwindow);
334 DLOG(
"Not in fullscreen mode, focusing\n");
335 if (!cwindow->
dock) {
345 }
else DLOG(
"not focusing, matched with restart_mode == true\n");
346 }
else DLOG(
"workspace not visible, not focusing\n");
347 }
else DLOG(
"dock, not focusing\n");
349 DLOG(
"fs = %p, ws = %p, not focusing\n", fs, ws);
364 bool want_floating =
false;
369 LOG(
"This window is a dialog window, setting floating\n");
370 want_floating =
true;
376 (cwindow->
leader != XCB_NONE &&
379 LOG(
"This window is transient for another window, setting floating\n");
380 want_floating =
true;
384 LOG(
"There is a fullscreen window, leaving fullscreen mode\n");
390 while (transient_win != NULL &&
393 LOG(
"This floating window belongs to the fullscreen window (popup_during_fullscreen == smart)\n");
398 if (next_transient == NULL)
400 transient_win = next_transient->
window;
407 want_floating =
false;
414 nc->
geometry = (
Rect){ geom->
x, geom->y, geom->width, geom->height };
423 values[0] = XCB_NONE;
424 xcb_change_window_attributes(
conn, window, XCB_CW_EVENT_MASK, values);
426 xcb_void_cookie_t rcookie = xcb_reparent_window_checked(
conn, window, nc->
frame, 0, 0);
427 if (xcb_request_check(
conn, rcookie) != NULL) {
428 LOG(
"Could not reparent the window, aborting\n");
433 xcb_change_window_attributes(
conn, window, XCB_CW_EVENT_MASK, values);
436 reply = xcb_get_property_reply(
conn, state_cookie, NULL);
446 xcb_change_save_set(
conn, XCB_SET_MODE_INSERT, window);