2 * vim:ts=4:sw=4:expandtab
4 * i3 - an improved dynamic tiling window manager
5 * © 2009 Michael Stapelberg and contributors (see also: LICENSE)
7 * restore_layout.c: Everything for restored containers that is not pure state
8 * parsing (which can be found in load_layout.c).
14 #ifdef I3_ASAN_ENABLED
15 #include <sanitizer/lsan_interface.h>
18 typedef struct placeholder_state {
19 /** The X11 placeholder window. */
21 /** The container to which this placeholder window belongs. */
24 /** Current size of the placeholder window (to detect size changes). */
27 /** The pixmap to render on (back buffer). */
29 /** The graphics context for “pixmap”. */
32 TAILQ_ENTRY(placeholder_state)
36 static TAILQ_HEAD(state_head, placeholder_state) state_head =
37 TAILQ_HEAD_INITIALIZER(state_head);
39 static xcb_connection_t *restore_conn;
41 static struct ev_io *xcb_watcher;
42 static struct ev_check *xcb_check;
43 static struct ev_prepare *xcb_prepare;
45 static void restore_handle_event(int type, xcb_generic_event_t *event);
47 /* Documentation for these functions can be found in src/main.c, starting at xcb_got_event */
48 static void restore_xcb_got_event(EV_P_ struct ev_io *w, int revents) {
51 static void restore_xcb_prepare_cb(EV_P_ ev_prepare *w, int revents) {
52 xcb_flush(restore_conn);
55 static void restore_xcb_check_cb(EV_P_ ev_check *w, int revents) {
56 xcb_generic_event_t *event;
58 if (xcb_connection_has_error(restore_conn)) {
59 DLOG("restore X11 connection has an error, reconnecting\n");
64 while ((event = xcb_poll_for_event(restore_conn)) != NULL) {
65 if (event->response_type == 0) {
66 xcb_generic_error_t *error = (xcb_generic_error_t *)event;
67 DLOG("X11 Error received (probably harmless)! sequence 0x%x, error_code = %d\n",
68 error->sequence, error->error_code);
73 /* Strip off the highest bit (set if the event is generated) */
74 int type = (event->response_type & 0x7F);
76 restore_handle_event(type, event);
83 * Opens a separate connection to X11 for placeholder windows when restoring
84 * layouts. This is done as a safety measure (users can xkill a placeholder
85 * window without killing their window manager) and for better isolation, both
86 * on the wire to X11 and thus also in the code.
89 void restore_connect(void) {
90 if (restore_conn != NULL) {
91 /* This is not the initial connect, but a reconnect, most likely
92 * because our X11 connection was killed (e.g. by a user with xkill. */
93 ev_io_stop(main_loop, xcb_watcher);
94 ev_check_stop(main_loop, xcb_check);
95 ev_prepare_stop(main_loop, xcb_prepare);
97 placeholder_state *state;
98 while (!TAILQ_EMPTY(&state_head)) {
99 state = TAILQ_FIRST(&state_head);
100 TAILQ_REMOVE(&state_head, state, state);
104 /* xcb_disconnect leaks memory in libxcb versions earlier than 1.11,
105 * but it’s the right function to call. See
106 * http://cgit.freedesktop.org/xcb/libxcb/commit/src/xcb_conn.c?id=4dcbfd77b
108 xcb_disconnect(restore_conn);
115 restore_conn = xcb_connect(NULL, &screen);
116 if (restore_conn == NULL || xcb_connection_has_error(restore_conn)) {
117 if (restore_conn != NULL) {
118 xcb_disconnect(restore_conn);
120 #ifdef I3_ASAN_ENABLED
121 __lsan_do_leak_check();
123 errx(EXIT_FAILURE, "Cannot open display\n");
126 xcb_watcher = scalloc(1, sizeof(struct ev_io));
127 xcb_check = scalloc(1, sizeof(struct ev_check));
128 xcb_prepare = scalloc(1, sizeof(struct ev_prepare));
130 ev_io_init(xcb_watcher, restore_xcb_got_event, xcb_get_file_descriptor(restore_conn), EV_READ);
131 ev_io_start(main_loop, xcb_watcher);
133 ev_check_init(xcb_check, restore_xcb_check_cb);
134 ev_check_start(main_loop, xcb_check);
136 ev_prepare_init(xcb_prepare, restore_xcb_prepare_cb);
137 ev_prepare_start(main_loop, xcb_prepare);
140 static void update_placeholder_contents(placeholder_state *state) {
141 xcb_change_gc(restore_conn, state->gc, XCB_GC_FOREGROUND,
142 (uint32_t[]){config.client.placeholder.background.colorpixel});
143 xcb_poly_fill_rectangle(restore_conn, state->pixmap, state->gc, 1,
144 (xcb_rectangle_t[]){{0, 0, state->rect.width, state->rect.height}});
146 // TODO: make i3font functions per-connection, at least these two for now…?
147 xcb_flush(restore_conn);
148 xcb_aux_sync(restore_conn);
150 set_font_colors(state->gc, config.client.placeholder.text, config.client.placeholder.background);
154 TAILQ_FOREACH(swallows, &(state->con->swallow_head), matches) {
155 char *serialized = NULL;
157 #define APPEND_REGEX(re_name) \
159 if (swallows->re_name != NULL) { \
160 sasprintf(&serialized, "%s%s" #re_name "=\"%s\"", (serialized ? serialized : "["), (serialized ? " " : ""), swallows->re_name->pattern); \
165 APPEND_REGEX(instance);
166 APPEND_REGEX(window_role);
169 if (serialized == NULL) {
170 DLOG("This swallows specification is not serializable?!\n");
174 sasprintf(&serialized, "%s]", serialized);
175 DLOG("con %p (placeholder 0x%08x) line %d: %s\n", state->con, state->window, n, serialized);
177 i3String *str = i3string_from_utf8(serialized);
178 draw_text(str, state->pixmap, state->gc, NULL, 2, (n * (config.font.height + 2)) + 2, state->rect.width - 2);
184 // TODO: render the watch symbol in a bigger font
185 i3String *line = i3string_from_utf8("⌚");
186 int text_width = predict_text_width(line);
187 int x = (state->rect.width / 2) - (text_width / 2);
188 int y = (state->rect.height / 2) - (config.font.height / 2);
189 draw_text(line, state->pixmap, state->gc, NULL, x, y, text_width);
195 static void open_placeholder_window(Con *con) {
196 if (con_is_leaf(con) &&
197 (con->window == NULL || con->window->id == XCB_NONE) &&
198 !TAILQ_EMPTY(&(con->swallow_head)) &&
199 con->type == CT_CON) {
200 xcb_window_t placeholder = create_window(
203 XCB_COPY_FROM_PARENT,
204 XCB_COPY_FROM_PARENT,
205 XCB_WINDOW_CLASS_INPUT_OUTPUT,
206 XCURSOR_CURSOR_POINTER,
208 XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK,
210 config.client.placeholder.background.colorpixel,
211 XCB_EVENT_MASK_EXPOSURE | XCB_EVENT_MASK_STRUCTURE_NOTIFY,
213 /* Make i3 not focus this window. */
214 xcb_icccm_wm_hints_t hints;
215 xcb_icccm_wm_hints_set_none(&hints);
216 xcb_icccm_wm_hints_set_input(&hints, 0);
217 xcb_icccm_set_wm_hints(restore_conn, placeholder, &hints);
218 /* Set the same name as was stored in the layout file. While perhaps
219 * slightly confusing in the first instant, this brings additional
220 * clarity to which placeholder is waiting for which actual window. */
221 if (con->name != NULL)
222 xcb_change_property(restore_conn, XCB_PROP_MODE_REPLACE, placeholder,
223 A__NET_WM_NAME, A_UTF8_STRING, 8, strlen(con->name), con->name);
224 DLOG("Created placeholder window 0x%08x for leaf container %p / %s\n",
225 placeholder, con, con->name);
227 placeholder_state *state = scalloc(1, sizeof(placeholder_state));
228 state->window = placeholder;
230 state->rect = con->rect;
231 state->pixmap = xcb_generate_id(restore_conn);
232 xcb_create_pixmap(restore_conn, root_depth, state->pixmap,
233 state->window, state->rect.width, state->rect.height);
234 state->gc = xcb_generate_id(restore_conn);
235 xcb_create_gc(restore_conn, state->gc, state->pixmap, XCB_GC_GRAPHICS_EXPOSURES, (uint32_t[]){0});
236 update_placeholder_contents(state);
237 TAILQ_INSERT_TAIL(&state_head, state, state);
239 /* create temporary id swallow to match the placeholder */
240 Match *temp_id = smalloc(sizeof(Match));
242 temp_id->dock = M_DONTCHECK;
243 temp_id->id = placeholder;
244 TAILQ_INSERT_HEAD(&(con->swallow_head), temp_id, matches);
248 TAILQ_FOREACH(child, &(con->nodes_head), nodes) {
249 open_placeholder_window(child);
251 TAILQ_FOREACH(child, &(con->floating_head), floating_windows) {
252 open_placeholder_window(child);
257 * Open placeholder windows for all children of parent. The placeholder window
258 * will vanish as soon as a real window is swallowed by the container. Until
259 * then, it exposes the criteria that must be fulfilled for a window to be
260 * swallowed by this container.
263 void restore_open_placeholder_windows(Con *parent) {
265 TAILQ_FOREACH(child, &(parent->nodes_head), nodes) {
266 open_placeholder_window(child);
268 TAILQ_FOREACH(child, &(parent->floating_head), floating_windows) {
269 open_placeholder_window(child);
272 xcb_flush(restore_conn);
276 * Kill the placeholder window, if placeholder refers to a placeholder window.
277 * This function is called when manage.c puts a window into an existing
278 * container. In order not to leak resources, we need to destroy the window and
279 * all associated X11 objects (pixmap/gc).
282 bool restore_kill_placeholder(xcb_window_t placeholder) {
283 placeholder_state *state;
284 TAILQ_FOREACH(state, &state_head, state) {
285 if (state->window != placeholder)
288 xcb_destroy_window(restore_conn, state->window);
289 xcb_free_pixmap(restore_conn, state->pixmap);
290 xcb_free_gc(restore_conn, state->gc);
291 TAILQ_REMOVE(&state_head, state, state);
293 DLOG("placeholder window 0x%08x destroyed.\n", placeholder);
297 DLOG("0x%08x is not a placeholder window, ignoring.\n", placeholder);
301 static void expose_event(xcb_expose_event_t *event) {
302 placeholder_state *state;
303 TAILQ_FOREACH(state, &state_head, state) {
304 if (state->window != event->window)
307 DLOG("refreshing window 0x%08x contents (con %p)\n", state->window, state->con);
309 /* Since we render to our pixmap on every change anyways, expose events
310 * only tell us that the X server lost (parts of) the window contents. We
311 * can handle that by copying the appropriate part from our pixmap to the
313 xcb_copy_area(restore_conn, state->pixmap, state->window, state->gc,
314 event->x, event->y, event->x, event->y,
315 event->width, event->height);
316 xcb_flush(restore_conn);
320 ELOG("Received ExposeEvent for unknown window 0x%08x\n", event->window);
324 * Window size has changed. Update the width/height, then recreate the back
325 * buffer pixmap and the accompanying graphics context and force an immediate
329 static void configure_notify(xcb_configure_notify_event_t *event) {
330 placeholder_state *state;
331 TAILQ_FOREACH(state, &state_head, state) {
332 if (state->window != event->window)
335 DLOG("ConfigureNotify: window 0x%08x has now width=%d, height=%d (con %p)\n",
336 state->window, event->width, event->height, state->con);
338 state->rect.width = event->width;
339 state->rect.height = event->height;
341 xcb_free_pixmap(restore_conn, state->pixmap);
342 xcb_free_gc(restore_conn, state->gc);
344 state->pixmap = xcb_generate_id(restore_conn);
345 xcb_create_pixmap(restore_conn, root_depth, state->pixmap,
346 state->window, state->rect.width, state->rect.height);
347 state->gc = xcb_generate_id(restore_conn);
348 xcb_create_gc(restore_conn, state->gc, state->pixmap, XCB_GC_GRAPHICS_EXPOSURES, (uint32_t[]){0});
350 update_placeholder_contents(state);
351 xcb_copy_area(restore_conn, state->pixmap, state->window, state->gc,
352 0, 0, 0, 0, state->rect.width, state->rect.height);
353 xcb_flush(restore_conn);
357 ELOG("Received ConfigureNotify for unknown window 0x%08x\n", event->window);
360 static void restore_handle_event(int type, xcb_generic_event_t *event) {
363 expose_event((xcb_expose_event_t *)event);
365 case XCB_CONFIGURE_NOTIFY:
366 configure_notify((xcb_configure_notify_event_t *)event);
369 DLOG("Received unhandled X11 event of type %d\n", type);