2 #define I3__FILE__ "restore_layout.c"
4 * vim:ts=4:sw=4:expandtab
6 * i3 - an improved dynamic tiling window manager
7 * © 2009-2013 Michael Stapelberg and contributors (see also: LICENSE)
9 * restore_layout.c: Everything for restored containers that is not pure state
10 * parsing (which can be found in load_layout.c).
16 typedef struct placeholder_state {
17 /** The X11 placeholder window. */
19 /** The container to which this placeholder window belongs. */
22 /** Current size of the placeholder window (to detect size changes). */
25 /** The pixmap to render on (back buffer). */
27 /** The graphics context for “pixmap”. */
30 TAILQ_ENTRY(placeholder_state) state;
33 static TAILQ_HEAD(state_head, placeholder_state) state_head =
34 TAILQ_HEAD_INITIALIZER(state_head);
36 static xcb_connection_t *restore_conn;
38 static struct ev_io *xcb_watcher;
39 static struct ev_check *xcb_check;
40 static struct ev_prepare *xcb_prepare;
42 static void restore_handle_event(int type, xcb_generic_event_t *event);
44 /* Documentation for these functions can be found in src/main.c, starting at xcb_got_event */
45 static void restore_xcb_got_event(EV_P_ struct ev_io *w, int revents) {
48 static void restore_xcb_prepare_cb(EV_P_ ev_prepare *w, int revents) {
49 xcb_flush(restore_conn);
52 static void restore_xcb_check_cb(EV_P_ ev_check *w, int revents) {
53 xcb_generic_event_t *event;
55 if (xcb_connection_has_error(restore_conn)) {
56 DLOG("restore X11 connection has an error, reconnecting\n");
61 while ((event = xcb_poll_for_event(restore_conn)) != NULL) {
62 if (event->response_type == 0) {
63 xcb_generic_error_t *error = (xcb_generic_error_t *)event;
64 DLOG("X11 Error received (probably harmless)! sequence 0x%x, error_code = %d\n",
65 error->sequence, error->error_code);
70 /* Strip off the highest bit (set if the event is generated) */
71 int type = (event->response_type & 0x7F);
73 restore_handle_event(type, event);
80 * Opens a separate connection to X11 for placeholder windows when restoring
81 * layouts. This is done as a safety measure (users can xkill a placeholder
82 * window without killing their window manager) and for better isolation, both
83 * on the wire to X11 and thus also in the code.
86 void restore_connect(void) {
87 if (restore_conn != NULL) {
88 /* This is not the initial connect, but a reconnect, most likely
89 * because our X11 connection was killed (e.g. by a user with xkill. */
90 ev_io_stop(main_loop, xcb_watcher);
91 ev_check_stop(main_loop, xcb_check);
92 ev_prepare_stop(main_loop, xcb_prepare);
94 placeholder_state *state;
95 while (!TAILQ_EMPTY(&state_head)) {
96 state = TAILQ_FIRST(&state_head);
97 TAILQ_REMOVE(&state_head, state, state);
108 restore_conn = xcb_connect(NULL, &screen);
109 if (restore_conn == NULL || xcb_connection_has_error(restore_conn))
110 errx(EXIT_FAILURE, "Cannot open display\n");
112 xcb_watcher = scalloc(sizeof(struct ev_io));
113 xcb_check = scalloc(sizeof(struct ev_check));
114 xcb_prepare = scalloc(sizeof(struct ev_prepare));
116 ev_io_init(xcb_watcher, restore_xcb_got_event, xcb_get_file_descriptor(restore_conn), EV_READ);
117 ev_io_start(main_loop, xcb_watcher);
119 ev_check_init(xcb_check, restore_xcb_check_cb);
120 ev_check_start(main_loop, xcb_check);
122 ev_prepare_init(xcb_prepare, restore_xcb_prepare_cb);
123 ev_prepare_start(main_loop, xcb_prepare);
126 static void update_placeholder_contents(placeholder_state *state) {
127 xcb_change_gc(restore_conn, state->gc, XCB_GC_FOREGROUND,
128 (uint32_t[]){config.client.placeholder.background});
129 xcb_poly_fill_rectangle(restore_conn, state->pixmap, state->gc, 1,
130 (xcb_rectangle_t[]){{0, 0, state->rect.width, state->rect.height}});
132 // TODO: make i3font functions per-connection, at least these two for now…?
133 xcb_flush(restore_conn);
134 xcb_aux_sync(restore_conn);
136 set_font_colors(state->gc, config.client.placeholder.text, config.client.placeholder.background);
140 TAILQ_FOREACH(swallows, &(state->con->swallow_head), matches) {
141 char *serialized = NULL;
143 #define APPEND_REGEX(re_name) \
145 if (swallows->re_name != NULL) { \
146 sasprintf(&serialized, "%s%s" #re_name "=\"%s\"", (serialized ? serialized : "["), (serialized ? " " : ""), swallows->re_name->pattern); \
151 APPEND_REGEX(instance);
152 APPEND_REGEX(window_role);
155 if (serialized == NULL) {
156 DLOG("This swallows specification is not serializable?!\n");
160 sasprintf(&serialized, "%s]", serialized);
161 DLOG("con %p (placeholder 0x%08x) line %d: %s\n", state->con, state->window, n, serialized);
163 i3String *str = i3string_from_utf8(serialized);
164 draw_text(str, state->pixmap, state->gc, 2, (n * (config.font.height + 2)) + 2, state->rect.width - 2);
170 // TODO: render the watch symbol in a bigger font
171 i3String *line = i3string_from_utf8("⌚");
172 int text_width = predict_text_width(line);
173 int x = (state->rect.width / 2) - (text_width / 2);
174 int y = (state->rect.height / 2) - (config.font.height / 2);
175 draw_text(line, state->pixmap, state->gc, x, y, text_width);
181 static void open_placeholder_window(Con *con) {
182 if (con_is_leaf(con) &&
183 (con->window == NULL || con->window->id == XCB_NONE)) {
184 xcb_window_t placeholder = create_window(
187 XCB_COPY_FROM_PARENT,
188 XCB_COPY_FROM_PARENT,
189 XCB_WINDOW_CLASS_INPUT_OUTPUT,
190 XCURSOR_CURSOR_POINTER,
192 XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK,
194 config.client.placeholder.background,
195 XCB_EVENT_MASK_EXPOSURE | XCB_EVENT_MASK_STRUCTURE_NOTIFY,
197 /* Set the same name as was stored in the layout file. While perhaps
198 * slightly confusing in the first instant, this brings additional
199 * clarity to which placeholder is waiting for which actual window. */
200 if (con->name != NULL)
201 xcb_change_property(restore_conn, XCB_PROP_MODE_REPLACE, placeholder,
202 A__NET_WM_NAME, A_UTF8_STRING, 8, strlen(con->name), con->name);
203 DLOG("Created placeholder window 0x%08x for leaf container %p / %s\n",
204 placeholder, con, con->name);
206 placeholder_state *state = scalloc(sizeof(placeholder_state));
207 state->window = placeholder;
209 state->rect = con->rect;
210 state->pixmap = xcb_generate_id(restore_conn);
211 xcb_create_pixmap(restore_conn, root_depth, state->pixmap,
212 state->window, state->rect.width, state->rect.height);
213 state->gc = xcb_generate_id(restore_conn);
214 xcb_create_gc(restore_conn, state->gc, state->pixmap, XCB_GC_GRAPHICS_EXPOSURES, (uint32_t[]){0});
215 update_placeholder_contents(state);
216 TAILQ_INSERT_TAIL(&state_head, state, state);
218 /* create temporary id swallow to match the placeholder */
219 Match *temp_id = smalloc(sizeof(Match));
221 temp_id->id = placeholder;
222 TAILQ_INSERT_HEAD(&(con->swallow_head), temp_id, matches);
226 TAILQ_FOREACH(child, &(con->nodes_head), nodes) {
227 open_placeholder_window(child);
229 TAILQ_FOREACH(child, &(con->floating_head), floating_windows) {
230 open_placeholder_window(child);
235 * Open placeholder windows for all children of parent. The placeholder window
236 * will vanish as soon as a real window is swallowed by the container. Until
237 * then, it exposes the criteria that must be fulfilled for a window to be
238 * swallowed by this container.
241 void restore_open_placeholder_windows(Con *parent) {
243 TAILQ_FOREACH(child, &(parent->nodes_head), nodes) {
244 open_placeholder_window(child);
246 TAILQ_FOREACH(child, &(parent->floating_head), floating_windows) {
247 open_placeholder_window(child);
250 xcb_flush(restore_conn);
254 * Kill the placeholder window, if placeholder refers to a placeholder window.
255 * This function is called when manage.c puts a window into an existing
256 * container. In order not to leak resources, we need to destroy the window and
257 * all associated X11 objects (pixmap/gc).
260 bool restore_kill_placeholder(xcb_window_t placeholder) {
261 placeholder_state *state;
262 TAILQ_FOREACH(state, &state_head, state) {
263 if (state->window != placeholder)
266 xcb_destroy_window(restore_conn, state->window);
267 xcb_free_pixmap(restore_conn, state->pixmap);
268 xcb_free_gc(restore_conn, state->gc);
269 TAILQ_REMOVE(&state_head, state, state);
271 DLOG("placeholder window 0x%08x destroyed.\n", placeholder);
275 DLOG("0x%08x is not a placeholder window, ignoring.\n", placeholder);
279 static void expose_event(xcb_expose_event_t *event) {
280 placeholder_state *state;
281 TAILQ_FOREACH(state, &state_head, state) {
282 if (state->window != event->window)
285 DLOG("refreshing window 0x%08x contents (con %p)\n", state->window, state->con);
287 /* Since we render to our pixmap on every change anyways, expose events
288 * only tell us that the X server lost (parts of) the window contents. We
289 * can handle that by copying the appropriate part from our pixmap to the
291 xcb_copy_area(restore_conn, state->pixmap, state->window, state->gc,
292 event->x, event->y, event->x, event->y,
293 event->width, event->height);
294 xcb_flush(restore_conn);
298 ELOG("Received ExposeEvent for unknown window 0x%08x\n", event->window);
302 * Window size has changed. Update the width/height, then recreate the back
303 * buffer pixmap and the accompanying graphics context and force an immediate
307 static void configure_notify(xcb_configure_notify_event_t *event) {
308 placeholder_state *state;
309 TAILQ_FOREACH(state, &state_head, state) {
310 if (state->window != event->window)
313 DLOG("ConfigureNotify: window 0x%08x has now width=%d, height=%d (con %p)\n",
314 state->window, event->width, event->height, state->con);
316 state->rect.width = event->width;
317 state->rect.height = event->height;
319 xcb_free_pixmap(restore_conn, state->pixmap);
320 xcb_free_gc(restore_conn, state->gc);
322 state->pixmap = xcb_generate_id(restore_conn);
323 xcb_create_pixmap(restore_conn, root_depth, state->pixmap,
324 state->window, state->rect.width, state->rect.height);
325 state->gc = xcb_generate_id(restore_conn);
326 xcb_create_gc(restore_conn, state->gc, state->pixmap, XCB_GC_GRAPHICS_EXPOSURES, (uint32_t[]){0});
328 update_placeholder_contents(state);
329 xcb_copy_area(restore_conn, state->pixmap, state->window, state->gc,
330 0, 0, 0, 0, state->rect.width, state->rect.height);
331 xcb_flush(restore_conn);
335 ELOG("Received ConfigureNotify for unknown window 0x%08x\n", event->window);
338 static void restore_handle_event(int type, xcb_generic_event_t *event) {
341 expose_event((xcb_expose_event_t *)event);
343 case XCB_CONFIGURE_NOTIFY:
344 configure_notify((xcb_configure_notify_event_t *)event);
347 DLOG("Received unhandled X11 event of type %d\n", type);