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 /* this code needs to run for:
227 * • non-leaf containers which are in a stacking container
229 if (!con_is_leaf(con) && con->parent->layout != L_STACKED)
231 DLOG("decoration should be rendered for con %p\n", con);
233 /* 1: find out which colors to use */
234 struct Colortriple *color;
236 color = &config.client.urgent;
237 else if (con == focused)
238 color = &config.client.focused;
239 else if (con == TAILQ_FIRST(&(con->parent->focus_head)))
240 color = &config.client.focused_inactive;
242 color = &config.client.unfocused;
244 Con *parent = con->parent;
245 int border_style = con_border_style(con);
247 /* 2: draw a rectangle in border color around the client */
248 if (border_style != BS_NONE && con_is_leaf(con)) {
249 Rect br = con_border_style_rect(con);
250 Rect *r = &(con->rect);
252 DLOG("con->rect spans %d x %d\n", con->rect.width, con->rect.height);
253 DLOG("border_rect spans (%d, %d) with %d x %d\n", border_rect.x, border_rect.y, border_rect.width, border_rect.height);
254 DLOG("window_rect spans (%d, %d) with %d x %d\n", con->window_rect.x, con->window_rect.y, con->window_rect.width, con->window_rect.height);
257 /* This polygon represents the border around the child window (left,
258 * bottom and right part). We don’t just fill the whole rectangle
259 * because some childs are not freely resizable and we want their
260 * background color to "shine through". */
261 xcb_change_gc_single(conn, con->gc, XCB_GC_FOREGROUND, color->background);
262 xcb_point_t points[] = {
265 { r->width, r->height },
267 { r->width + br.width + br.x, 0 },
268 { r->width + br.width + br.x, r->height + br.height + br.y },
269 { br.x, r->height + br.height },
272 xcb_fill_poly(conn, con->frame, con->gc, XCB_POLY_SHAPE_COMPLEX, XCB_COORD_MODE_ORIGIN, 8, points);
274 /* 1pixel border needs an additional line at the top */
275 if (border_style == BS_1PIXEL) {
276 xcb_rectangle_t topline = { br.x, 0, con->rect.width + br.width + br.x, br.y };
277 xcb_poly_fill_rectangle(conn, con->frame, con->gc, 1, &topline);
281 /* if this is a borderless/1pixel window, we don’t * need to render the
283 if (border_style != BS_NORMAL) {
284 DLOG("border style not BS_NORMAL, aborting rendering of decoration\n");
288 /* 3: paint the bar */
289 xcb_change_gc_single(conn, parent->gc, XCB_GC_FOREGROUND, color->background);
290 xcb_rectangle_t drect = { con->deco_rect.x, con->deco_rect.y, con->deco_rect.width, con->deco_rect.height };
291 xcb_poly_fill_rectangle(conn, parent->frame, parent->gc, 1, &drect);
293 /* 4: draw the two lines in border color */
294 xcb_draw_line(conn, parent->frame, parent->gc, color->border,
295 con->deco_rect.x, /* x */
296 con->deco_rect.y, /* y */
297 con->deco_rect.x + con->deco_rect.width, /* to_x */
298 con->deco_rect.y); /* to_y */
299 xcb_draw_line(conn, parent->frame, parent->gc, color->border,
300 con->deco_rect.x, /* x */
301 con->deco_rect.y + con->deco_rect.height - 1, /* y */
302 con->deco_rect.x + con->deco_rect.width, /* to_x */
303 con->deco_rect.y + con->deco_rect.height - 1); /* to_y */
305 /* 5: draw the title */
306 xcb_change_gc_single(conn, parent->gc, XCB_GC_BACKGROUND, color->background);
307 xcb_change_gc_single(conn, parent->gc, XCB_GC_FOREGROUND, color->text);
309 struct Window *win = con->window;
310 if (win == NULL || win->name_x == NULL) {
311 /* this is a non-leaf container, we need to make up a good description */
312 // TODO: use a good description instead of just "another container"
315 strlen("another container"),
318 con->deco_rect.x + 2,
319 con->deco_rect.y + 14, /* TODO: hardcoded */
325 int indent_level = 0,
327 Con *il_parent = con->parent;
328 if (il_parent->type != L_STACKED) {
330 DLOG("il_parent = %p, layout = %d\n", il_parent, il_parent->layout);
331 if (il_parent->layout == L_STACKED)
333 if (il_parent->type == CT_WORKSPACE)
335 il_parent = il_parent->parent;
339 DLOG("indent_level = %d, indent_mult = %d\n", indent_level, indent_mult);
340 int indent_px = (indent_level * 5) * indent_mult;
342 if (win->uses_net_wm_name)
348 con->deco_rect.x + 2 + indent_px,
349 con->deco_rect.y + 14, /* TODO: hardcoded */
350 (xcb_char2b_t*)win->name_x
358 con->deco_rect.x + 2 + indent_px,
359 con->deco_rect.y + 14, /* TODO: hardcoded */
365 * This function pushes the properties of each node of the layout tree to
366 * X11 if they have changed (like the map state, position of the window, …).
367 * It recursively traverses all children of the given node.
370 static void x_push_node(Con *con) {
374 LOG("Pushing changes for node %p / %s\n", con, con->name);
375 state = state_for_frame(con->frame);
377 /* reparent the child window (when the window was moved due to a sticky
379 if (state->need_reparent && con->window != NULL) {
380 LOG("Reparenting child window\n");
382 /* Temporarily set the event masks to XCB_NONE so that we won’t get
383 * UnmapNotify events (otherwise the handler would close the container).
384 * These events are generated automatically when reparenting. */
385 uint32_t values[] = { XCB_NONE };
386 xcb_change_window_attributes(conn, state->old_frame, XCB_CW_EVENT_MASK, values);
387 xcb_change_window_attributes(conn, con->window->id, XCB_CW_EVENT_MASK, values);
389 xcb_reparent_window(conn, con->window->id, con->frame, 0, 0);
391 values[0] = FRAME_EVENT_MASK;
392 xcb_change_window_attributes(conn, state->old_frame, XCB_CW_EVENT_MASK, values);
393 values[0] = CHILD_EVENT_MASK;
394 xcb_change_window_attributes(conn, con->window->id, XCB_CW_EVENT_MASK, values);
396 state->old_frame = XCB_NONE;
397 state->need_reparent = false;
400 /* map/unmap if map state changed, also ensure that the child window
401 * is changed if we are mapped *and* in initial state (meaning the
402 * container was empty before, but now got a child) */
403 if (state->mapped != con->mapped || (con->mapped && state->initial)) {
405 xcb_void_cookie_t cookie;
406 cookie = xcb_unmap_window(conn, con->frame);
407 LOG("unmapping container (serial %d)\n", cookie.sequence);
408 /* Ignore enter_notifies which are generated when unmapping */
409 add_ignore_event(cookie.sequence);
411 xcb_void_cookie_t cookie;
412 if (state->initial && con->window != NULL) {
413 cookie = xcb_map_window(conn, con->window->id);
414 LOG("mapping child window (serial %d)\n", cookie.sequence);
415 /* Ignore enter_notifies which are generated when mapping */
416 add_ignore_event(cookie.sequence);
418 cookie = xcb_map_window(conn, con->frame);
419 LOG("mapping container (serial %d)\n", cookie.sequence);
420 /* Ignore enter_notifies which are generated when mapping */
421 add_ignore_event(cookie.sequence);
423 state->mapped = con->mapped;
426 bool fake_notify = false;
427 /* set new position if rect changed */
428 if (memcmp(&(state->rect), &(con->rect), sizeof(Rect)) != 0) {
429 LOG("setting rect (%d, %d, %d, %d)\n", con->rect.x, con->rect.y, con->rect.width, con->rect.height);
430 xcb_set_window_rect(conn, con->frame, con->rect);
431 memcpy(&(state->rect), &(con->rect), sizeof(Rect));
435 /* dito, but for child windows */
436 if (con->window != NULL &&
437 memcmp(&(state->window_rect), &(con->window_rect), sizeof(Rect)) != 0) {
438 LOG("setting window rect (%d, %d, %d, %d)\n",
439 con->window_rect.x, con->window_rect.y, con->window_rect.width, con->window_rect.height);
440 xcb_set_window_rect(conn, con->window->id, con->window_rect);
441 memcpy(&(state->rect), &(con->rect), sizeof(Rect));
446 LOG("Sending fake configure notify\n");
447 fake_absolute_configure_notify(con);
450 /* handle all children and floating windows of this node */
451 TAILQ_FOREACH(current, &(con->nodes_head), nodes)
452 x_push_node(current);
454 TAILQ_FOREACH(current, &(con->floating_head), floating_windows)
455 x_push_node(current);
457 if (con->type != CT_ROOT && con->type != CT_OUTPUT)
458 x_draw_decoration(con);
462 * Pushes all changes (state of each node, see x_push_node() and the window
466 void x_push_changes(Con *con) {
469 LOG("\n\n PUSHING CHANGES\n\n");
472 LOG("-- PUSHING WINDOW STACK --\n");
473 bool order_changed = false;
474 /* X11 correctly represents the stack if we push it from bottom to top */
475 CIRCLEQ_FOREACH_REVERSE(state, &state_head, state) {
476 LOG("stack: 0x%08x\n", state->id);
477 con_state *prev = CIRCLEQ_PREV(state, state);
478 con_state *old_prev = CIRCLEQ_PREV(state, old_state);
479 if (prev != old_prev)
480 order_changed = true;
481 if ((state->initial || order_changed) && prev != CIRCLEQ_END(&state_head)) {
482 state->initial = false;
483 LOG("Stacking 0x%08x above 0x%08x\n", prev->id, state->id);
485 mask |= XCB_CONFIG_WINDOW_SIBLING;
486 mask |= XCB_CONFIG_WINDOW_STACK_MODE;
487 uint32_t values[] = {state->id, XCB_STACK_MODE_ABOVE};
489 xcb_configure_window(conn, prev->id, mask, values);
493 xcb_window_t to_focus = focused->frame;
494 if (focused->window != NULL)
495 to_focus = focused->window->id;
497 if (focused_id != to_focus) {
498 LOG("Updating focus (focused: %p / %s)\n", focused, focused->name);
499 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_POINTER_ROOT, to_focus, XCB_CURRENT_TIME);
503 LOG("\n\n ENDING CHANGES\n\n");
505 /* save the current stack as old stack */
506 CIRCLEQ_FOREACH(state, &state_head, state) {
507 CIRCLEQ_REMOVE(&old_state_head, state, old_state);
508 CIRCLEQ_INSERT_TAIL(&old_state_head, state, old_state);
510 CIRCLEQ_FOREACH(state, &old_state_head, old_state) {
511 LOG("old stack: 0x%08x\n", state->id);
516 * Raises the specified container in the internal stack of X windows. The
517 * next call to x_push_changes() will make the change visible in X11.
520 void x_raise_con(Con *con) {
522 LOG("raising in new stack: %p / %s\n", con, con->name);
523 state = state_for_frame(con->frame);
525 LOG("found state entry, moving to top\n");
526 CIRCLEQ_REMOVE(&state_head, state, state);
527 CIRCLEQ_INSERT_HEAD(&state_head, state, state);