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) {
225 if (!con_is_leaf(con) || (con->type != CT_CON && con->type != CT_FLOATING_CON))
228 /* 1: find out which colors to use */
229 struct Colortriple *color;
231 color = &config.client.urgent;
232 else if (con == focused)
233 color = &config.client.focused;
234 else if (con == TAILQ_FIRST(&(con->parent->focus_head)))
235 color = &config.client.focused_inactive;
237 color = &config.client.unfocused;
239 Con *parent = con->parent;
241 /* 2: draw a rectangle in border color around the client */
242 if (con->border_style != BS_NONE) {
243 xcb_change_gc_single(conn, con->gc, XCB_GC_FOREGROUND, color->background);
244 xcb_rectangle_t rect = { 0, 0, con->rect.width, con->rect.height };
245 xcb_poly_fill_rectangle(conn, con->frame, con->gc, 1, &rect);
247 if (con->border_style != BS_NORMAL)
250 /* 3: paint the bar */
251 xcb_change_gc_single(conn, parent->gc, XCB_GC_FOREGROUND, color->background);
252 xcb_rectangle_t drect = { con->deco_rect.x, con->deco_rect.y, con->deco_rect.width, con->deco_rect.height };
253 xcb_poly_fill_rectangle(conn, parent->frame, parent->gc, 1, &drect);
255 /* 4: draw the two lines in border color */
256 xcb_draw_line(conn, parent->frame, parent->gc, color->border,
257 con->deco_rect.x, /* x */
258 con->deco_rect.y, /* y */
259 con->deco_rect.x + con->deco_rect.width, /* to_x */
260 con->deco_rect.y); /* to_y */
261 xcb_draw_line(conn, parent->frame, parent->gc, color->border,
262 con->deco_rect.x, /* x */
263 con->deco_rect.y + con->deco_rect.height - 1, /* y */
264 con->deco_rect.x + con->deco_rect.width, /* to_x */
265 con->deco_rect.y + con->deco_rect.height - 1); /* to_y */
267 /* 5: draw the title */
268 struct Window *win = con->window;
269 if (win == NULL || win ->name_x == NULL)
271 xcb_change_gc_single(conn, parent->gc, XCB_GC_BACKGROUND, color->background);
272 xcb_change_gc_single(conn, parent->gc, XCB_GC_FOREGROUND, color->text);
273 if (win->uses_net_wm_name)
280 con->deco_rect.y + 14, /* TODO: hardcoded */
281 (xcb_char2b_t*)win->name_x
290 con->deco_rect.y + 14, /* TODO: hardcoded */
296 * This function pushes the properties of each node of the layout tree to
297 * X11 if they have changed (like the map state, position of the window, …).
298 * It recursively traverses all children of the given node.
301 static void x_push_node(Con *con) {
305 LOG("Pushing changes for node %p / %s\n", con, con->name);
306 state = state_for_frame(con->frame);
308 /* reparent the child window (when the window was moved due to a sticky
310 if (state->need_reparent && con->window != NULL) {
311 LOG("Reparenting child window\n");
313 /* Temporarily set the event masks to XCB_NONE so that we won’t get
314 * UnmapNotify events (otherwise the handler would close the container).
315 * These events are generated automatically when reparenting. */
316 uint32_t values[] = { XCB_NONE };
317 xcb_change_window_attributes(conn, state->old_frame, XCB_CW_EVENT_MASK, values);
318 xcb_change_window_attributes(conn, con->window->id, XCB_CW_EVENT_MASK, values);
320 xcb_reparent_window(conn, con->window->id, con->frame, 0, 0);
322 values[0] = FRAME_EVENT_MASK;
323 xcb_change_window_attributes(conn, state->old_frame, XCB_CW_EVENT_MASK, values);
324 values[0] = CHILD_EVENT_MASK;
325 xcb_change_window_attributes(conn, con->window->id, XCB_CW_EVENT_MASK, values);
327 state->old_frame = XCB_NONE;
328 state->need_reparent = false;
331 /* map/unmap if map state changed, also ensure that the child window
332 * is changed if we are mapped *and* in initial state (meaning the
333 * container was empty before, but now got a child) */
334 if (state->mapped != con->mapped || (con->mapped && state->initial)) {
336 xcb_void_cookie_t cookie;
337 cookie = xcb_unmap_window(conn, con->frame);
338 LOG("unmapping container (serial %d)\n", cookie.sequence);
339 /* Ignore enter_notifies which are generated when unmapping */
340 add_ignore_event(cookie.sequence);
342 xcb_void_cookie_t cookie;
343 if (state->initial && con->window != NULL) {
344 cookie = xcb_map_window(conn, con->window->id);
345 LOG("mapping child window (serial %d)\n", cookie.sequence);
346 /* Ignore enter_notifies which are generated when mapping */
347 add_ignore_event(cookie.sequence);
349 cookie = xcb_map_window(conn, con->frame);
350 LOG("mapping container (serial %d)\n", cookie.sequence);
351 /* Ignore enter_notifies which are generated when mapping */
352 add_ignore_event(cookie.sequence);
354 state->mapped = con->mapped;
357 bool fake_notify = false;
358 /* set new position if rect changed */
359 if (memcmp(&(state->rect), &(con->rect), sizeof(Rect)) != 0) {
360 LOG("setting rect (%d, %d, %d, %d)\n", con->rect.x, con->rect.y, con->rect.width, con->rect.height);
361 xcb_set_window_rect(conn, con->frame, con->rect);
362 memcpy(&(state->rect), &(con->rect), sizeof(Rect));
366 /* dito, but for child windows */
367 if (con->window != NULL &&
368 memcmp(&(state->window_rect), &(con->window_rect), sizeof(Rect)) != 0) {
369 LOG("setting window rect (%d, %d, %d, %d)\n",
370 con->window_rect.x, con->window_rect.y, con->window_rect.width, con->window_rect.height);
371 xcb_set_window_rect(conn, con->window->id, con->window_rect);
372 memcpy(&(state->rect), &(con->rect), sizeof(Rect));
377 LOG("Sending fake configure notify\n");
378 fake_absolute_configure_notify(con);
381 /* handle all children and floating windows of this node */
382 TAILQ_FOREACH(current, &(con->nodes_head), nodes)
383 x_push_node(current);
385 TAILQ_FOREACH(current, &(con->floating_head), floating_windows)
386 x_push_node(current);
388 if (con->type != CT_ROOT && con->type != CT_OUTPUT)
389 x_draw_decoration(con);
393 * Pushes all changes (state of each node, see x_push_node() and the window
397 void x_push_changes(Con *con) {
400 LOG("\n\n PUSHING CHANGES\n\n");
403 LOG("-- PUSHING WINDOW STACK --\n");
404 bool order_changed = false;
405 /* X11 correctly represents the stack if we push it from bottom to top */
406 CIRCLEQ_FOREACH_REVERSE(state, &state_head, state) {
407 LOG("stack: 0x%08x\n", state->id);
408 con_state *prev = CIRCLEQ_PREV(state, state);
409 con_state *old_prev = CIRCLEQ_PREV(state, old_state);
410 if (prev != old_prev)
411 order_changed = true;
412 if ((state->initial || order_changed) && prev != CIRCLEQ_END(&state_head)) {
413 state->initial = false;
414 LOG("Stacking 0x%08x above 0x%08x\n", prev->id, state->id);
416 mask |= XCB_CONFIG_WINDOW_SIBLING;
417 mask |= XCB_CONFIG_WINDOW_STACK_MODE;
418 uint32_t values[] = {state->id, XCB_STACK_MODE_ABOVE};
420 xcb_configure_window(conn, prev->id, mask, values);
424 xcb_window_t to_focus = focused->frame;
425 if (focused->window != NULL)
426 to_focus = focused->window->id;
428 if (focused_id != to_focus) {
429 LOG("Updating focus (focused: %p / %s)\n", focused, focused->name);
430 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_POINTER_ROOT, to_focus, XCB_CURRENT_TIME);
434 LOG("\n\n ENDING CHANGES\n\n");
436 /* save the current stack as old stack */
437 CIRCLEQ_FOREACH(state, &state_head, state) {
438 CIRCLEQ_REMOVE(&old_state_head, state, old_state);
439 CIRCLEQ_INSERT_TAIL(&old_state_head, state, old_state);
441 CIRCLEQ_FOREACH(state, &old_state_head, old_state) {
442 LOG("old stack: 0x%08x\n", state->id);
447 * Raises the specified container in the internal stack of X windows. The
448 * next call to x_push_changes() will make the change visible in X11.
451 void x_raise_con(Con *con) {
453 LOG("raising in new stack: %p / %s\n", con, con->name);
454 state = state_for_frame(con->frame);
456 LOG("found state entry, moving to top\n");
457 CIRCLEQ_REMOVE(&state_head, state, state);
458 CIRCLEQ_INSERT_HEAD(&state_head, state, state);