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 {
26 CIRCLEQ_ENTRY(con_state) state;
27 CIRCLEQ_ENTRY(con_state) old_state;
30 CIRCLEQ_HEAD(state_head, con_state) state_head =
31 CIRCLEQ_HEAD_INITIALIZER(state_head);
33 CIRCLEQ_HEAD(old_state_head, con_state) old_state_head =
34 CIRCLEQ_HEAD_INITIALIZER(old_state_head);
37 * Returns the container state for the given frame. This function always
38 * returns a container state (otherwise, there is a bug in the code and the
39 * container state of a container for which x_con_init() was not called was
43 static con_state *state_for_frame(xcb_window_t window) {
45 CIRCLEQ_FOREACH(state, &state_head, state)
46 if (state->id == window)
49 /* TODO: better error handling? */
50 ELOG("No state found\n");
56 * Initializes the X11 part for the given container. Called exactly once for
57 * every container from con_new().
60 void x_con_init(Con *con) {
61 /* TODO: maybe create the window when rendering first? we could then even
62 * get the initial geometry right */
67 /* our own frames should not be managed */
68 mask |= XCB_CW_OVERRIDE_REDIRECT;
71 /* We want to know when… */
72 mask |= XCB_CW_EVENT_MASK;
73 values[1] = FRAME_EVENT_MASK;
75 Rect dims = { -15, -15, 10, 10 };
76 con->frame = create_window(conn, dims, XCB_WINDOW_CLASS_INPUT_OUTPUT, -1, false, mask, values);
77 con->gc = xcb_generate_id(conn);
78 xcb_create_gc(conn, con->gc, con->frame, 0, 0);
80 struct con_state *state = scalloc(sizeof(struct con_state));
81 state->id = con->frame;
82 state->mapped = false;
83 state->initial = true;
84 CIRCLEQ_INSERT_HEAD(&state_head, state, state);
85 CIRCLEQ_INSERT_HEAD(&old_state_head, state, old_state);
86 LOG("adding new state for window id 0x%08x\n", state->id);
90 * Re-initializes the associated X window state for this container. You have
91 * to call this when you assign a client to an empty container to ensure that
92 * its state gets updated correctly.
95 void x_reinit(Con *con) {
96 struct con_state *state;
98 if ((state = state_for_frame(con->frame)) == NULL) {
99 ELOG("window state not found\n");
103 LOG("resetting state %p to initial\n", state);
104 state->initial = true;
105 memset(&(state->window_rect), 0, sizeof(Rect));
108 void x_con_kill(Con *con) {
111 xcb_destroy_window(conn, con->frame);
112 state = state_for_frame(con->frame);
113 CIRCLEQ_REMOVE(&state_head, state, state);
114 CIRCLEQ_REMOVE(&old_state_head, state, old_state);
118 * Returns true if the client supports the given protocol atom (like WM_DELETE_WINDOW)
121 static bool window_supports_protocol(xcb_window_t window, xcb_atom_t atom) {
122 xcb_get_property_cookie_t cookie;
123 xcb_get_wm_protocols_reply_t protocols;
126 cookie = xcb_get_wm_protocols_unchecked(conn, window, atoms[WM_PROTOCOLS]);
127 if (xcb_get_wm_protocols_reply(conn, cookie, &protocols, NULL) != 1)
130 /* Check if the client’s protocols have the requested atom set */
131 for (uint32_t i = 0; i < protocols.atoms_len; i++)
132 if (protocols.atoms[i] == atom)
135 xcb_get_wm_protocols_reply_wipe(&protocols);
140 void x_window_kill(xcb_window_t window) {
141 /* if this window does not support WM_DELETE_WINDOW, we kill it the hard way */
142 if (!window_supports_protocol(window, atoms[WM_DELETE_WINDOW])) {
143 LOG("Killing window the hard way\n");
144 xcb_kill_client(conn, window);
148 xcb_client_message_event_t ev;
150 memset(&ev, 0, sizeof(xcb_client_message_event_t));
152 ev.response_type = XCB_CLIENT_MESSAGE;
154 ev.type = atoms[WM_PROTOCOLS];
156 ev.data.data32[0] = atoms[WM_DELETE_WINDOW];
157 ev.data.data32[1] = XCB_CURRENT_TIME;
159 LOG("Sending WM_DELETE to the client\n");
160 xcb_send_event(conn, false, window, XCB_EVENT_MASK_NO_EVENT, (char*)&ev);
164 void x_draw_decoration(Con *con) {
167 parent = con->parent;
170 xcb_change_gc_single(conn, parent->gc, XCB_GC_FOREGROUND, get_colorpixel("#FF0000"));
171 else xcb_change_gc_single(conn, parent->gc, XCB_GC_FOREGROUND, get_colorpixel("#0C0C0C"));
172 xcb_rectangle_t drect = { con->deco_rect.x, con->deco_rect.y, con->deco_rect.width, con->deco_rect.height };
173 xcb_poly_fill_rectangle(conn, parent->frame, parent->gc, 1, &drect);
175 if (con->window == NULL)
178 i3Window *win = con->window;
180 if (win->name_x == NULL) {
181 LOG("not rendering decoration, not yet known\n");
186 LOG("should render text %s onto %p / %s\n", win->name_json, parent, parent->name);
188 xcb_change_gc_single(conn, parent->gc, XCB_GC_FOREGROUND, get_colorpixel("#FFFFFF"));
189 if (win->uses_net_wm_name)
196 con->deco_rect.y + 14, /* TODO: hardcoded */
197 (xcb_char2b_t*)win->name_x
206 con->deco_rect.y + 14, /* TODO: hardcoded */
212 * This function pushes the properties of each node of the layout tree to
213 * X11 if they have changed (like the map state, position of the window, …).
214 * It recursively traverses all children of the given node.
217 static void x_push_node(Con *con) {
221 LOG("Pushing changes for node %p / %s\n", con, con->name);
222 state = state_for_frame(con->frame);
224 /* map/unmap if map state changed, also ensure that the child window
225 * is changed if we are mapped *and* in initial state (meaning the
226 * container was empty before, but now got a child) */
227 if (state->mapped != con->mapped || (con->mapped && state->initial)) {
229 LOG("unmapping container\n");
230 xcb_unmap_window(conn, con->frame);
232 if (state->initial && con->window != NULL) {
233 LOG("mapping child window\n");
234 xcb_map_window(conn, con->window->id);
236 LOG("mapping container\n");
237 xcb_map_window(conn, con->frame);
239 state->mapped = con->mapped;
242 /* set new position if rect changed */
243 if (memcmp(&(state->rect), &(con->rect), sizeof(Rect)) != 0) {
244 LOG("setting rect (%d, %d, %d, %d)\n", con->rect.x, con->rect.y, con->rect.width, con->rect.height);
245 xcb_set_window_rect(conn, con->frame, con->rect);
246 memcpy(&(state->rect), &(con->rect), sizeof(Rect));
249 /* dito, but for child windows */
250 if (memcmp(&(state->window_rect), &(con->window_rect), sizeof(Rect)) != 0) {
251 LOG("setting window rect (%d, %d, %d, %d)\n",
252 con->window_rect.x, con->window_rect.y, con->window_rect.width, con->window_rect.height);
253 xcb_set_window_rect(conn, con->window->id, con->window_rect);
254 memcpy(&(state->rect), &(con->rect), sizeof(Rect));
257 /* handle all children and floating windows of this node */
258 TAILQ_FOREACH(current, &(con->nodes_head), nodes)
259 x_push_node(current);
261 TAILQ_FOREACH(current, &(con->floating_head), floating_windows)
262 x_push_node(current);
264 if (con->type != CT_ROOT && con->type != CT_OUTPUT)
265 x_draw_decoration(con);
269 * Pushes all changes (state of each node, see x_push_node() and the window
273 void x_push_changes(Con *con) {
276 LOG("\n\n PUSHING CHANGES\n\n");
279 LOG("-- PUSHING WINDOW STACK --\n");
280 /* X11 correctly represents the stack if we push it from bottom to top */
281 CIRCLEQ_FOREACH_REVERSE(state, &state_head, state) {
282 LOG("stack: 0x%08x\n", state->id);
283 con_state *prev = CIRCLEQ_PREV(state, state);
284 con_state *old_prev = CIRCLEQ_PREV(state, old_state);
285 if ((state->initial || prev != old_prev) && prev != CIRCLEQ_END(&state_head)) {
286 state->initial = false;
287 LOG("Stacking 0x%08x above 0x%08x\n", prev->id, state->id);
289 mask |= XCB_CONFIG_WINDOW_SIBLING;
290 mask |= XCB_CONFIG_WINDOW_STACK_MODE;
291 uint32_t values[] = {state->id, XCB_STACK_MODE_ABOVE};
293 xcb_configure_window(conn, prev->id, mask, values);
297 xcb_window_t to_focus = focused->frame;
298 if (focused->window != NULL)
299 to_focus = focused->window->id;
301 if (focused_id != to_focus) {
302 LOG("Updating focus (focused: %p / %s)\n", focused, focused->name);
303 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_POINTER_ROOT, to_focus, XCB_CURRENT_TIME);
307 LOG("\n\n ENDING CHANGES\n\n");
309 /* save the current stack as old stack */
310 CIRCLEQ_FOREACH(state, &state_head, state) {
311 CIRCLEQ_REMOVE(&old_state_head, state, old_state);
312 CIRCLEQ_INSERT_TAIL(&old_state_head, state, old_state);
314 CIRCLEQ_FOREACH(state, &old_state_head, old_state) {
315 LOG("old stack: 0x%08x\n", state->id);
320 * Raises the specified container in the internal stack of X windows. The
321 * next call to x_push_changes() will make the change visible in X11.
324 void x_raise_con(Con *con) {
326 LOG("raising in new stack: %p / %s\n", con, con->name);
327 state = state_for_frame(con->frame);
329 LOG("found state entry, moving to top\n");
330 CIRCLEQ_REMOVE(&state_head, state, state);
331 CIRCLEQ_INSERT_HEAD(&state_head, state, state);