2 * vim:ts=4:sw=4:expandtab
7 /* Stores the X11 window ID of the currently focused window */
8 static xcb_window_t focused_id = XCB_NONE;
11 * Describes the X11 state we may modify (map state, position, window stack).
12 * There is one entry per container. The state represents the current situation
13 * as X11 sees it (with the exception of the order in the state_head CIRCLEQ,
14 * which represents the order that will be pushed to X11, while old_state_head
15 * represents the current order). It will be updated in x_push_changes().
18 typedef struct con_state {
22 /* For reparenting, we have a flag (need_reparent) and the X ID of the old
23 * frame this window was in. The latter is necessary because we need to
24 * ignore UnmapNotify events (by changing the window event mask). */
26 xcb_window_t old_frame;
33 CIRCLEQ_ENTRY(con_state) state;
34 CIRCLEQ_ENTRY(con_state) old_state;
37 CIRCLEQ_HEAD(state_head, con_state) state_head =
38 CIRCLEQ_HEAD_INITIALIZER(state_head);
40 CIRCLEQ_HEAD(old_state_head, con_state) old_state_head =
41 CIRCLEQ_HEAD_INITIALIZER(old_state_head);
44 * Returns the container state for the given frame. This function always
45 * returns a container state (otherwise, there is a bug in the code and the
46 * container state of a container for which x_con_init() was not called was
50 static con_state *state_for_frame(xcb_window_t window) {
52 CIRCLEQ_FOREACH(state, &state_head, state)
53 if (state->id == window)
56 /* TODO: better error handling? */
57 ELOG("No state found\n");
63 * Initializes the X11 part for the given container. Called exactly once for
64 * every container from con_new().
67 void x_con_init(Con *con) {
68 /* TODO: maybe create the window when rendering first? we could then even
69 * get the initial geometry right */
74 /* our own frames should not be managed */
75 mask |= XCB_CW_OVERRIDE_REDIRECT;
78 /* We want to know when… */
79 mask |= XCB_CW_EVENT_MASK;
80 values[1] = FRAME_EVENT_MASK;
82 Rect dims = { -15, -15, 10, 10 };
83 con->frame = create_window(conn, dims, XCB_WINDOW_CLASS_INPUT_OUTPUT, -1, false, mask, values);
84 con->gc = xcb_generate_id(conn);
85 xcb_create_gc(conn, con->gc, con->frame, 0, 0);
87 struct con_state *state = scalloc(sizeof(struct con_state));
88 state->id = con->frame;
89 state->mapped = false;
90 state->initial = true;
91 CIRCLEQ_INSERT_HEAD(&state_head, state, state);
92 CIRCLEQ_INSERT_HEAD(&old_state_head, state, old_state);
93 LOG("adding new state for window id 0x%08x\n", state->id);
97 * Re-initializes the associated X window state for this container. You have
98 * to call this when you assign a client to an empty container to ensure that
99 * its state gets updated correctly.
102 void x_reinit(Con *con) {
103 struct con_state *state;
105 if ((state = state_for_frame(con->frame)) == NULL) {
106 ELOG("window state not found\n");
110 LOG("resetting state %p to initial\n", state);
111 state->initial = true;
112 memset(&(state->window_rect), 0, sizeof(Rect));
116 * Reparents the child window of the given container (necessary for sticky
117 * containers). The reparenting happens in the next call of x_push_changes().
120 void x_reparent_child(Con *con, Con *old) {
121 struct con_state *state;
122 if ((state = state_for_frame(con->frame)) == NULL) {
123 ELOG("window state for con not found\n");
127 state->need_reparent = true;
128 state->old_frame = old->frame;
132 * Moves a child window from Container src to Container dest.
135 void x_move_win(Con *src, Con *dest) {
136 struct con_state *state_src, *state_dest;
138 if ((state_src = state_for_frame(src->frame)) == NULL) {
139 ELOG("window state for src not found\n");
143 if ((state_dest = state_for_frame(dest->frame)) == NULL) {
144 ELOG("window state for dest not found\n");
149 memset(&zero, 0, sizeof(Rect));
150 if (memcmp(&(state_dest->window_rect), &(zero), sizeof(Rect)) == 0) {
151 memcpy(&(state_dest->window_rect), &(state_src->window_rect), sizeof(Rect));
152 LOG("COPYING RECT\n");
157 * Kills the window decoration associated with the given container.
160 void x_con_kill(Con *con) {
163 xcb_destroy_window(conn, con->frame);
164 state = state_for_frame(con->frame);
165 CIRCLEQ_REMOVE(&state_head, state, state);
166 CIRCLEQ_REMOVE(&old_state_head, state, old_state);
170 * Returns true if the client supports the given protocol atom (like WM_DELETE_WINDOW)
173 static bool window_supports_protocol(xcb_window_t window, xcb_atom_t atom) {
174 xcb_get_property_cookie_t cookie;
175 xcb_get_wm_protocols_reply_t protocols;
178 cookie = xcb_get_wm_protocols_unchecked(conn, window, atoms[WM_PROTOCOLS]);
179 if (xcb_get_wm_protocols_reply(conn, cookie, &protocols, NULL) != 1)
182 /* Check if the client’s protocols have the requested atom set */
183 for (uint32_t i = 0; i < protocols.atoms_len; i++)
184 if (protocols.atoms[i] == atom)
187 xcb_get_wm_protocols_reply_wipe(&protocols);
193 * Kills the given X11 window using WM_DELETE_WINDOW (if supported).
196 void x_window_kill(xcb_window_t window) {
197 /* if this window does not support WM_DELETE_WINDOW, we kill it the hard way */
198 if (!window_supports_protocol(window, atoms[WM_DELETE_WINDOW])) {
199 LOG("Killing window the hard way\n");
200 xcb_kill_client(conn, window);
204 xcb_client_message_event_t ev;
206 memset(&ev, 0, sizeof(xcb_client_message_event_t));
208 ev.response_type = XCB_CLIENT_MESSAGE;
210 ev.type = atoms[WM_PROTOCOLS];
212 ev.data.data32[0] = atoms[WM_DELETE_WINDOW];
213 ev.data.data32[1] = XCB_CURRENT_TIME;
215 LOG("Sending WM_DELETE to the client\n");
216 xcb_send_event(conn, false, window, XCB_EVENT_MASK_NO_EVENT, (char*)&ev);
221 * Draws the decoration of the given container onto its parent.
224 void x_draw_decoration(Con *con) {
227 parent = con->parent;
230 xcb_change_gc_single(conn, parent->gc, XCB_GC_FOREGROUND, get_colorpixel("#FF0000"));
231 else xcb_change_gc_single(conn, parent->gc, XCB_GC_FOREGROUND, get_colorpixel("#0C0C0C"));
232 xcb_rectangle_t drect = { con->deco_rect.x, con->deco_rect.y, con->deco_rect.width, con->deco_rect.height };
233 xcb_poly_fill_rectangle(conn, parent->frame, parent->gc, 1, &drect);
235 if (con->window == NULL)
238 i3Window *win = con->window;
240 if (win->name_x == NULL) {
241 LOG("not rendering decoration, not yet known\n");
246 LOG("should render text %s onto %p / %s\n", win->name_json, parent, parent->name);
248 xcb_change_gc_single(conn, parent->gc, XCB_GC_FOREGROUND, get_colorpixel("#FFFFFF"));
249 if (win->uses_net_wm_name)
256 con->deco_rect.y + 14, /* TODO: hardcoded */
257 (xcb_char2b_t*)win->name_x
266 con->deco_rect.y + 14, /* TODO: hardcoded */
272 * This function pushes the properties of each node of the layout tree to
273 * X11 if they have changed (like the map state, position of the window, …).
274 * It recursively traverses all children of the given node.
277 static void x_push_node(Con *con) {
281 LOG("Pushing changes for node %p / %s\n", con, con->name);
282 state = state_for_frame(con->frame);
284 /* reparent the child window (when the window was moved due to a sticky
286 if (state->need_reparent && con->window != NULL) {
287 LOG("Reparenting child window\n");
289 /* Temporarily set the event masks to XCB_NONE so that we won’t get
290 * UnmapNotify events (otherwise the handler would close the container).
291 * These events are generated automatically when reparenting. */
292 uint32_t values[] = { XCB_NONE };
293 xcb_change_window_attributes(conn, state->old_frame, XCB_CW_EVENT_MASK, values);
294 xcb_change_window_attributes(conn, con->window->id, XCB_CW_EVENT_MASK, values);
296 xcb_reparent_window(conn, con->window->id, con->frame, 0, 0);
298 values[0] = FRAME_EVENT_MASK;
299 xcb_change_window_attributes(conn, state->old_frame, XCB_CW_EVENT_MASK, values);
300 values[0] = CHILD_EVENT_MASK;
301 xcb_change_window_attributes(conn, con->window->id, XCB_CW_EVENT_MASK, values);
303 state->old_frame = XCB_NONE;
304 state->need_reparent = false;
307 /* map/unmap if map state changed, also ensure that the child window
308 * is changed if we are mapped *and* in initial state (meaning the
309 * container was empty before, but now got a child) */
310 if (state->mapped != con->mapped || (con->mapped && state->initial)) {
312 xcb_void_cookie_t cookie;
313 cookie = xcb_unmap_window(conn, con->frame);
314 LOG("unmapping container (serial %d)\n", cookie.sequence);
315 /* Ignore enter_notifies which are generated when unmapping */
316 add_ignore_event(cookie.sequence);
318 xcb_void_cookie_t cookie;
319 if (state->initial && con->window != NULL) {
320 cookie = xcb_map_window(conn, con->window->id);
321 LOG("mapping child window (serial %d)\n", cookie.sequence);
322 /* Ignore enter_notifies which are generated when mapping */
323 add_ignore_event(cookie.sequence);
325 cookie = xcb_map_window(conn, con->frame);
326 LOG("mapping container (serial %d)\n", cookie.sequence);
327 /* Ignore enter_notifies which are generated when mapping */
328 add_ignore_event(cookie.sequence);
330 state->mapped = con->mapped;
333 bool fake_notify = false;
334 /* set new position if rect changed */
335 if (memcmp(&(state->rect), &(con->rect), sizeof(Rect)) != 0) {
336 LOG("setting rect (%d, %d, %d, %d)\n", con->rect.x, con->rect.y, con->rect.width, con->rect.height);
337 xcb_set_window_rect(conn, con->frame, con->rect);
338 memcpy(&(state->rect), &(con->rect), sizeof(Rect));
342 /* dito, but for child windows */
343 if (con->window != NULL &&
344 memcmp(&(state->window_rect), &(con->window_rect), sizeof(Rect)) != 0) {
345 LOG("setting window rect (%d, %d, %d, %d)\n",
346 con->window_rect.x, con->window_rect.y, con->window_rect.width, con->window_rect.height);
347 xcb_set_window_rect(conn, con->window->id, con->window_rect);
348 memcpy(&(state->rect), &(con->rect), sizeof(Rect));
353 LOG("Sending fake configure notify\n");
354 fake_absolute_configure_notify(con);
357 /* handle all children and floating windows of this node */
358 TAILQ_FOREACH(current, &(con->nodes_head), nodes)
359 x_push_node(current);
361 TAILQ_FOREACH(current, &(con->floating_head), floating_windows)
362 x_push_node(current);
364 if (con->type != CT_ROOT && con->type != CT_OUTPUT)
365 x_draw_decoration(con);
369 * Pushes all changes (state of each node, see x_push_node() and the window
373 void x_push_changes(Con *con) {
376 LOG("\n\n PUSHING CHANGES\n\n");
379 LOG("-- PUSHING WINDOW STACK --\n");
380 bool order_changed = false;
381 /* X11 correctly represents the stack if we push it from bottom to top */
382 CIRCLEQ_FOREACH_REVERSE(state, &state_head, state) {
383 LOG("stack: 0x%08x\n", state->id);
384 con_state *prev = CIRCLEQ_PREV(state, state);
385 con_state *old_prev = CIRCLEQ_PREV(state, old_state);
386 if (prev != old_prev)
387 order_changed = true;
388 if ((state->initial || order_changed) && prev != CIRCLEQ_END(&state_head)) {
389 state->initial = false;
390 LOG("Stacking 0x%08x above 0x%08x\n", prev->id, state->id);
392 mask |= XCB_CONFIG_WINDOW_SIBLING;
393 mask |= XCB_CONFIG_WINDOW_STACK_MODE;
394 uint32_t values[] = {state->id, XCB_STACK_MODE_ABOVE};
396 xcb_configure_window(conn, prev->id, mask, values);
400 xcb_window_t to_focus = focused->frame;
401 if (focused->window != NULL)
402 to_focus = focused->window->id;
404 if (focused_id != to_focus) {
405 LOG("Updating focus (focused: %p / %s)\n", focused, focused->name);
406 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_POINTER_ROOT, to_focus, XCB_CURRENT_TIME);
410 LOG("\n\n ENDING CHANGES\n\n");
412 /* save the current stack as old stack */
413 CIRCLEQ_FOREACH(state, &state_head, state) {
414 CIRCLEQ_REMOVE(&old_state_head, state, old_state);
415 CIRCLEQ_INSERT_TAIL(&old_state_head, state, old_state);
417 CIRCLEQ_FOREACH(state, &old_state_head, old_state) {
418 LOG("old stack: 0x%08x\n", state->id);
423 * Raises the specified container in the internal stack of X windows. The
424 * next call to x_push_changes() will make the change visible in X11.
427 void x_raise_con(Con *con) {
429 LOG("raising in new stack: %p / %s\n", con, con->name);
430 state = state_for_frame(con->frame);
432 LOG("found state entry, moving to top\n");
433 CIRCLEQ_REMOVE(&state_head, state, state);
434 CIRCLEQ_INSERT_HEAD(&state_head, state, state);