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});
82 void manage_window(xcb_window_t window, xcb_get_window_attributes_cookie_t cookie,
83 bool needs_to_be_mapped) {
84 xcb_drawable_t d = {window};
85 xcb_get_geometry_cookie_t geomc;
86 xcb_get_geometry_reply_t *geom;
87 xcb_get_window_attributes_reply_t *attr = NULL;
89 xcb_get_property_cookie_t wm_type_cookie, strut_cookie, state_cookie,
90 utf8_title_cookie, title_cookie,
91 class_cookie, leader_cookie, transient_cookie,
92 role_cookie, startup_id_cookie, wm_hints_cookie,
93 wm_normal_hints_cookie, motif_wm_hints_cookie, wm_user_time_cookie, wm_desktop_cookie;
95 geomc = xcb_get_geometry(
conn, d);
99 if ((attr = xcb_get_window_attributes_reply(
conn, cookie, 0)) == NULL) {
100 DLOG(
"Could not get attributes\n");
101 xcb_discard_reply(
conn, geomc.sequence);
105 if (needs_to_be_mapped && attr->map_state != XCB_MAP_STATE_VIEWABLE) {
106 xcb_discard_reply(
conn, geomc.sequence);
111 if (attr->override_redirect) {
112 xcb_discard_reply(
conn, geomc.sequence);
119 xcb_discard_reply(
conn, geomc.sequence);
124 if ((geom = xcb_get_geometry_reply(
conn, geomc, 0)) == NULL) {
125 DLOG(
"could not get geometry\n");
140 values[0] = XCB_EVENT_MASK_PROPERTY_CHANGE |
141 XCB_EVENT_MASK_STRUCTURE_NOTIFY;
142 xcb_void_cookie_t event_mask_cookie =
143 xcb_change_window_attributes_checked(
conn, window, XCB_CW_EVENT_MASK, values);
144 if (xcb_request_check(
conn, event_mask_cookie) != NULL) {
145 LOG(
"Could not change event mask, the window probably already disappeared.\n");
149 #define GET_PROPERTY(atom, len) xcb_get_property(conn, false, window, atom, XCB_GET_PROPERTY_TYPE_ANY, 0, len)
151 wm_type_cookie =
GET_PROPERTY(A__NET_WM_WINDOW_TYPE, UINT32_MAX);
152 strut_cookie =
GET_PROPERTY(A__NET_WM_STRUT_PARTIAL, UINT32_MAX);
153 state_cookie =
GET_PROPERTY(A__NET_WM_STATE, UINT32_MAX);
155 leader_cookie =
GET_PROPERTY(A_WM_CLIENT_LEADER, UINT32_MAX);
160 startup_id_cookie =
GET_PROPERTY(A__NET_STARTUP_ID, 512);
163 motif_wm_hints_cookie =
GET_PROPERTY(A__MOTIF_WM_HINTS, 5 *
sizeof(uint64_t));
164 wm_user_time_cookie =
GET_PROPERTY(A__NET_WM_USER_TIME, UINT32_MAX);
165 wm_desktop_cookie =
GET_PROPERTY(A__NET_WM_DESKTOP, UINT32_MAX);
167 DLOG(
"Managing window 0x%08x\n", window);
170 cwindow->
id = window;
187 xcb_size_hints_t wm_size_hints;
189 memset(&wm_size_hints,
'\0',
sizeof(xcb_size_hints_t));
190 xcb_get_property_reply_t *type_reply = xcb_get_property_reply(
conn, wm_type_cookie, NULL);
191 xcb_get_property_reply_t *state_reply = xcb_get_property_reply(
conn, state_cookie, NULL);
193 xcb_get_property_reply_t *startup_id_reply;
194 startup_id_reply = xcb_get_property_reply(
conn, startup_id_cookie, NULL);
196 DLOG(
"startup workspace = %s\n", startup_ws);
199 xcb_get_property_reply_t *wm_desktop_reply;
200 wm_desktop_reply = xcb_get_property_reply(
conn, wm_desktop_cookie, NULL);
202 if (wm_desktop_reply != NULL && xcb_get_property_value_length(wm_desktop_reply) != 0) {
203 uint32_t *wm_desktops = xcb_get_property_value(wm_desktop_reply);
204 cwindow->
wm_desktop = (int32_t)wm_desktops[0];
206 FREE(wm_desktop_reply);
218 LOG(
"This window is of type dock\n");
220 if (output != NULL) {
221 DLOG(
"Starting search at output %s\n", output->
name);
222 search_at = output->
con;
227 DLOG(
"Top dock client\n");
228 cwindow->
dock = W_DOCK_TOP;
230 DLOG(
"Bottom dock client\n");
231 cwindow->
dock = W_DOCK_BOTTOM;
233 DLOG(
"Ignoring invalid reserved edges (_NET_WM_STRUT_PARTIAL), using position as fallback:\n");
234 if (geom->y < (int16_t)(search_at->
rect.
height / 2)) {
235 DLOG(
"geom->y = %d < rect.height / 2 = %d, it is a top dock client\n",
237 cwindow->
dock = W_DOCK_TOP;
239 DLOG(
"geom->y = %d >= rect.height / 2 = %d, it is a bottom dock client\n",
241 cwindow->
dock = W_DOCK_BOTTOM;
246 DLOG(
"Initial geometry: (%d, %d, %d, %d)\n", geom->x, geom->y, geom->width, geom->height);
256 const bool match_from_restart_mode = (match && match->
restart_mode);
258 Con *wm_desktop_ws = NULL;
262 DLOG(
"Assignment matches (%p)\n", match);
265 DLOG(
"focused on ws %s: %p / %s\n", assigned_ws->
name, nc, nc->
name);
266 if (nc->
type == CT_WORKSPACE)
281 DLOG(
"Using workspace %p / %s because _NET_WM_DESKTOP = %d.\n",
285 if (nc->
type == CT_WORKSPACE)
289 }
else if (startup_ws) {
291 DLOG(
"Using workspace on which this application was started (%s)\n", startup_ws);
293 DLOG(
"focused on ws %s: %p / %s\n", startup_ws, nc, nc->
name);
294 if (nc->
type == CT_WORKSPACE)
301 LOG(
"using current container, focused = %p, focused->name = %s\n",
319 DLOG(
"Removing match %p from container %p\n", match, nc);
326 DLOG(
"new container = %p\n", nc);
329 DLOG(
"Uh?! Container without a placeholder, but with a window, has swallowed this to-be-managed window?!\n");
349 sasprintf(&name,
"[i3 con] container around %p", cwindow);
367 bool set_focus =
false;
370 DLOG(
"Not in fullscreen mode, focusing\n");
371 if (!cwindow->
dock) {
379 if (!match_from_restart_mode) {
382 DLOG(
"not focusing, matched with restart_mode == true\n");
385 DLOG(
"workspace not visible, not focusing\n");
388 DLOG(
"dock, not focusing\n");
391 DLOG(
"fs = %p, ws = %p, not focusing\n", fs, ws);
406 bool want_floating =
false;
414 wm_size_hints.min_height == wm_size_hints.max_height &&
415 wm_size_hints.min_width == wm_size_hints.max_width)) {
416 LOG(
"This window is a dialog window, setting floating\n");
417 want_floating =
true;
424 DLOG(
"This window has _NET_WM_DESKTOP = 0xFFFFFFFF. Will float it and make it sticky.\n");
426 want_floating =
true;
433 (cwindow->
leader != XCB_NONE &&
436 LOG(
"This window is transient for another window, setting floating\n");
437 want_floating =
true;
441 LOG(
"There is a fullscreen window, leaving fullscreen mode\n");
447 while (transient_win != NULL &&
450 LOG(
"This floating window belongs to the fullscreen window (popup_during_fullscreen == smart)\n");
455 if (next_transient == NULL)
460 if (transient_win == next_transient->
window)
462 transient_win = next_transient->
window;
469 want_floating =
false;
472 if ((wm_size_hints.flags & XCB_ICCCM_SIZE_HINT_US_POSITION || wm_size_hints.flags & XCB_ICCCM_SIZE_HINT_P_POSITION) &&
473 (wm_size_hints.flags & XCB_ICCCM_SIZE_HINT_US_SIZE || wm_size_hints.flags & XCB_ICCCM_SIZE_HINT_P_SIZE)) {
474 DLOG(
"We are setting geometry according to wm_size_hints x=%d y=%d w=%d h=%d\n",
475 wm_size_hints.x, wm_size_hints.y, wm_size_hints.width, wm_size_hints.height);
476 geom->x = wm_size_hints.x;
477 geom->y = wm_size_hints.y;
478 geom->width = wm_size_hints.width;
479 geom->height = wm_size_hints.height;
488 nc->
geometry = (
Rect){geom->
x, geom->y, geom->width, geom->height};
491 DLOG(
"MOTIF_WM_HINTS specifies decorations (border_style = %d)\n", motif_border_style);
503 bool automatic_border = (motif_border_style ==
BS_NORMAL);
515 values[0] = XCB_NONE;
516 xcb_change_window_attributes(
conn, window, XCB_CW_EVENT_MASK, values);
518 xcb_void_cookie_t rcookie = xcb_reparent_window_checked(
conn, window, nc->
frame.
id, 0, 0);
519 if (xcb_request_check(
conn, rcookie) != NULL) {
520 LOG(
"Could not reparent the window, aborting\n");
525 xcb_change_window_attributes(
conn, window, XCB_CW_EVENT_MASK, values);
532 xcb_change_save_set(
conn, XCB_SET_MODE_INSERT, window);
571 DLOG(
"This is the first window on this workspace, ignoring no_focus.\n");
573 DLOG(
"no_focus was set for con = %p, not setting focus.\n", nc);
579 DLOG(
"Checking con = %p for _NET_WM_USER_TIME.\n", nc);
581 uint32_t *wm_user_time;
582 xcb_get_property_reply_t *wm_user_time_reply = xcb_get_property_reply(
conn, wm_user_time_cookie, NULL);
583 if (wm_user_time_reply != NULL && xcb_get_property_value_length(wm_user_time_reply) != 0 &&
584 (wm_user_time = xcb_get_property_value(wm_user_time_reply)) &&
585 wm_user_time[0] == 0) {
586 DLOG(
"_NET_WM_USER_TIME set to 0, not focusing con = %p.\n", nc);
590 FREE(wm_user_time_reply);
592 xcb_discard_reply(
conn, wm_user_time_cookie.sequence);
598 DLOG(
"Now setting focus.\n");
#define XCB_ICCCM_SIZE_HINT_P_MIN_SIZE
void window_update_motif_hints(i3Window *win, xcb_get_property_reply_t *prop, border_style_t *motif_border_style)
Updates the MOTIF_WM_HINTS.
void window_update_role(i3Window *win, xcb_get_property_reply_t *prop, bool before_mgmt)
Updates the WM_WINDOW_ROLE.
void restore_geometry(void)
Restores the geometry of each window by reparenting it to the root window at the position of its fram...
void floating_enable(Con *con, bool automatic)
Enables floating mode for the given container by detaching it from its parent, creating a new contain...
union Assignment::@19 dest
destination workspace/command, depending on the type
void window_update_strut_partial(i3Window *win, xcb_get_property_reply_t *prop)
Updates the _NET_WM_STRUT_PARTIAL (reserved pixels at the screen edges)
Con * con_for_window(Con *con, i3Window *window, Match **store_match)
Returns the first container below 'con' which wants to swallow this window TODO: priority.
void window_update_class(i3Window *win, xcb_get_property_reply_t *prop, bool before_mgmt)
Updates the WM_CLASS (consisting of the class and instance) for the given window. ...
void con_focus(Con *con)
Sets input focus to the given container.
void con_set_urgency(Con *con, bool urgent)
Set urgency flag to the container, all the parent containers and the workspace.
Con * tree_open_con(Con *con, i3Window *window)
Opens an empty container in the current container.
void con_toggle_fullscreen(Con *con, int fullscreen_mode)
Toggles fullscreen mode for the given container.
void * smalloc(size_t size)
Safe-wrapper around malloc which exits if malloc returns NULL (meaning that there is no more memory a...
bool xcb_reply_contains_atom(xcb_get_property_reply_t *prop, xcb_atom_t atom)
Returns true if the given reply contains the given data.
An Assignment makes specific windows go to a specific workspace/output or run a command for that wind...
void window_update_name(i3Window *win, xcb_get_property_reply_t *prop, bool before_mgmt)
Updates the name by using _NET_WM_NAME (encoded in UTF-8) for the given window.
#define TAILQ_EMPTY(head)
bool doesnt_accept_focus
Whether this window accepts focus.
void xcb_set_window_rect(xcb_connection_t *conn, xcb_window_t window, Rect r)
Configures the given window to have the size/position specified by given rect.
void output_push_sticky_windows(Con *to_focus)
Iterates over all outputs and pushes sticky windows to the currently visible workspace on that output...
Con * con_by_window_id(xcb_window_t window)
Returns the container with the given client window ID or NULL if no such container exists...
void xcb_grab_buttons(xcb_connection_t *conn, xcb_window_t window, bool bind_scrollwheel)
Grab the specified buttons on a window when managing it.
#define TAILQ_FOREACH(var, head, field)
#define xcb_icccm_get_wm_normal_hints
Con * workspace_get(const char *num, bool *created)
Returns a pointer to the workspace with the given number (starting at 0), creating the workspace if n...
void match_free(Match *match)
Frees the given match.
void * scalloc(size_t num, size_t size)
Safe-wrapper around calloc which exits if malloc returns NULL (meaning that there is no more memory a...
void window_free(i3Window *win)
Frees an i3Window and all its members.
int default_floating_border_width
xcb_connection_t * conn
XCB connection and root screen.
void run_assignments(i3Window *window)
Checks the list of assignments for the given window and runs all matching ones (unless they have alre...
Con * ewmh_get_workspace_by_index(uint32_t idx)
Returns the workspace container as enumerated by the EWMH desktop model.
#define NET_WM_DESKTOP_ALL
void tree_render(void)
Renders the tree, that is rendering all outputs using render_con() and pushing the changes to X11 usi...
Con * con_get_output(Con *con)
Gets the output container (first container with CT_OUTPUT in hierarchy) this node is on...
#define xcb_icccm_get_wm_hints
void manage_window(xcb_window_t window, xcb_get_window_attributes_cookie_t cookie, bool needs_to_be_mapped)
Do some sanity checks and then reparent the window.
#define XCB_ICCCM_SIZE_HINT_P_MAX_SIZE
#define TAILQ_INSERT_AFTER(head, listelm, elm, field)
bool bindings_should_grab_scrollwheel_buttons(void)
Returns true if the current config has any binding to a scroll wheel button (4 or 5) which is a whole...
void window_update_name_legacy(i3Window *win, xcb_get_property_reply_t *prop, bool before_mgmt)
Updates the name by using WM_NAME (encoded in COMPOUND_TEXT).
#define GET_PROPERTY(atom, len)
struct Rect geometry
the geometry this window requested when getting mapped
Con * con
Pointer to the Con which represents this output.
#define XCB_ATOM_WM_TRANSIENT_FOR
#define TAILQ_REMOVE(head, elm, field)
void x_set_name(Con *con, const char *name)
Sets the WM_NAME property (so, no UTF8, but used only for debugging anyways) of the given name...
enum Config::@7 popup_during_fullscreen
What should happen when a new popup is opened during fullscreen mode.
uint16_t depth
Depth of the window.
bool restore_kill_placeholder(xcb_window_t placeholder)
Kill the placeholder window, if placeholder refers to a placeholder window.
Assignment * assignment_for(i3Window *window, int type)
Returns the first matching assignment for the given window.
void ipc_send_window_event(const char *property, Con *con)
For the window events we send, along the usual "change" field, also the window container, in "container".
#define xcb_icccm_get_wm_size_hints_reply
Output * get_output_containing(unsigned int x, unsigned int y)
Returns the active (!) output which contains the coordinates x, y or NULL if there is no output which...
Con * con_get_workspace(Con *con)
Gets the workspace container this node is on.
void window_update_hints(i3Window *win, xcb_get_property_reply_t *prop, bool *urgency_hint)
Updates the WM_HINTS (we only care about the input focus handling part).
bool workspace_is_visible(Con *ws)
Returns true if the workspace is currently visible.
void window_update_transient_for(i3Window *win, xcb_get_property_reply_t *prop)
Updates the TRANSIENT_FOR (logical parent window).
A 'Window' is a type which contains an xcb_window_t and all the related information (hints like _NET_...
xcb_window_t leader
Holds the xcb_window_t (just an ID) for the leader window (logical parent for toolwindows and similar...
enum Match::@17 insert_where
void ewmh_update_wm_desktop(void)
Updates _NET_WM_DESKTOP for all windows.
Con * con_get_fullscreen_con(Con *con, fullscreen_mode_t fullscreen_mode)
Returns the first fullscreen node below this node.
char * startup_workspace_for_window(i3Window *cwindow, xcb_get_property_reply_t *startup_id_reply)
Checks if the given window belongs to a startup notification by checking if the _NET_STARTUP_ID prope...
void manage_existing_windows(xcb_window_t root)
Go through all existing windows (if the window manager is restarted) and manage them.
bool con_accepts_window(Con *con)
Returns true if this node accepts a window (if the node swallows windows, it might already have swall...
xcb_window_t transient_for
A 'Con' represents everything from the X11 root window down to a single X11 window.
#define XCB_ATOM_WM_CLASS
#define CHILD_EVENT_MASK
The XCB_CW_EVENT_MASK for the child (= real window)
#define TAILQ_FIRST(head)
Con * con_descend_tiling_focused(Con *con)
Returns the focused con inside this client, descending the tree as far as possible.
int sasprintf(char **strp, const char *fmt,...)
Safe-wrapper around asprintf which exits if it returns -1 (meaning that there is no more memory avail...
uint32_t wm_desktop
The _NET_WM_DESKTOP for this window.
uint16_t get_visual_depth(xcb_visualid_t visual_id)
Get depth of visual specified by visualid.
int con_num_children(Con *con)
Returns the number of children of this container.
void render_con(Con *con, bool render_fullscreen)
"Renders" the given container (and its children), meaning that all rects are updated correctly...
xcb_atom_t xcb_get_preferred_window_type(xcb_get_property_reply_t *reply)
Returns the first supported _NET_WM_WINDOW_TYPE atom.
void x_reinit(Con *con)
Re-initializes the associated X window state for this container.
bool needs_take_focus
Whether the application needs to receive WM_TAKE_FOCUS.
A "match" is a data structure which acts like a mask or expression to match certain windows or not...
bool window_supports_protocol(xcb_window_t window, xcb_atom_t atom)
Returns true if the client supports the given protocol atom (like WM_DELETE_WINDOW) ...
struct all_cons_head all_cons
enum Window::@13 dock
Whether the window says it is a dock window.
An Output is a physical output on your graphics driver.
void con_set_border_style(Con *con, int border_style, int border_width)
Sets the given border style on con, correctly keeping the position/size of a floating window...
char * name
Name of the output.
xcb_atom_t window_type
The _NET_WM_WINDOW_TYPE for this window.
void window_update_leader(i3Window *win, xcb_get_property_reply_t *prop)
Updates the CLIENT_LEADER (logical parent window).
#define NET_WM_DESKTOP_NONE
struct reservedpx reserved
Pixels the window reserves.