]> git.sur5r.net Git - i3/i3/blob - src/restore_layout.c
cmd_exit: Let i3_exit handle shutdown (#3600)
[i3/i3] / src / restore_layout.c
1 /*
2  * vim:ts=4:sw=4:expandtab
3  *
4  * i3 - an improved dynamic tiling window manager
5  * © 2009 Michael Stapelberg and contributors (see also: LICENSE)
6  *
7  * restore_layout.c: Everything for restored containers that is not pure state
8  *                   parsing (which can be found in load_layout.c).
9  *
10  *
11  */
12 #include "all.h"
13
14 #ifdef I3_ASAN_ENABLED
15 #include <sanitizer/lsan_interface.h>
16 #endif
17
18 #define TEXT_PADDING logical_px(2)
19
20 typedef struct placeholder_state {
21     /** The X11 placeholder window. */
22     xcb_window_t window;
23     /** The container to which this placeholder window belongs. */
24     Con *con;
25
26     /** Current size of the placeholder window (to detect size changes). */
27     Rect rect;
28
29     /** The drawable surface */
30     surface_t surface;
31
32     TAILQ_ENTRY(placeholder_state)
33     state;
34 } placeholder_state;
35
36 static TAILQ_HEAD(state_head, placeholder_state) state_head =
37     TAILQ_HEAD_INITIALIZER(state_head);
38
39 static xcb_connection_t *restore_conn;
40
41 static struct ev_io *xcb_watcher;
42 static struct ev_prepare *xcb_prepare;
43
44 static void restore_handle_event(int type, xcb_generic_event_t *event);
45
46 /* Documentation for these functions can be found in src/main.c, starting at xcb_got_event */
47 static void restore_xcb_got_event(EV_P_ struct ev_io *w, int revents) {
48 }
49
50 static void restore_xcb_prepare_cb(EV_P_ ev_prepare *w, int revents) {
51     xcb_generic_event_t *event;
52
53     if (xcb_connection_has_error(restore_conn)) {
54         DLOG("restore X11 connection has an error, reconnecting\n");
55         restore_connect();
56         return;
57     }
58
59     while ((event = xcb_poll_for_event(restore_conn)) != NULL) {
60         if (event->response_type == 0) {
61             xcb_generic_error_t *error = (xcb_generic_error_t *)event;
62             DLOG("X11 Error received (probably harmless)! sequence 0x%x, error_code = %d\n",
63                  error->sequence, error->error_code);
64             free(event);
65             continue;
66         }
67
68         /* Strip off the highest bit (set if the event is generated) */
69         int type = (event->response_type & 0x7F);
70
71         restore_handle_event(type, event);
72
73         free(event);
74     }
75
76     xcb_flush(restore_conn);
77 }
78
79 /*
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.
84  *
85  */
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_prepare_stop(main_loop, xcb_prepare);
92
93         placeholder_state *state;
94         while (!TAILQ_EMPTY(&state_head)) {
95             state = TAILQ_FIRST(&state_head);
96             TAILQ_REMOVE(&state_head, state, state);
97             free(state);
98         }
99
100         /* xcb_disconnect leaks memory in libxcb versions earlier than 1.11,
101          * but it’s the right function to call. See
102          * https://cgit.freedesktop.org/xcb/libxcb/commit/src/xcb_conn.c?id=4dcbfd77b
103          */
104         xcb_disconnect(restore_conn);
105         free(xcb_watcher);
106         free(xcb_prepare);
107     }
108
109     int screen;
110     restore_conn = xcb_connect(NULL, &screen);
111     if (restore_conn == NULL || xcb_connection_has_error(restore_conn)) {
112         if (restore_conn != NULL) {
113             xcb_disconnect(restore_conn);
114         }
115 #ifdef I3_ASAN_ENABLED
116         __lsan_do_leak_check();
117 #endif
118         errx(EXIT_FAILURE, "Cannot open display");
119     }
120
121     xcb_watcher = scalloc(1, sizeof(struct ev_io));
122     xcb_prepare = scalloc(1, sizeof(struct ev_prepare));
123
124     ev_io_init(xcb_watcher, restore_xcb_got_event, xcb_get_file_descriptor(restore_conn), EV_READ);
125     ev_io_start(main_loop, xcb_watcher);
126
127     ev_prepare_init(xcb_prepare, restore_xcb_prepare_cb);
128     ev_prepare_start(main_loop, xcb_prepare);
129 }
130
131 static void update_placeholder_contents(placeholder_state *state) {
132     const color_t foreground = config.client.placeholder.text;
133     const color_t background = config.client.placeholder.background;
134
135     draw_util_clear_surface(&(state->surface), background);
136
137     // TODO: make i3font functions per-connection, at least these two for now…?
138     xcb_flush(restore_conn);
139     xcb_aux_sync(restore_conn);
140
141     Match *swallows;
142     int n = 0;
143     TAILQ_FOREACH(swallows, &(state->con->swallow_head), matches) {
144         char *serialized = NULL;
145
146 #define APPEND_REGEX(re_name)                                                                                                                        \
147     do {                                                                                                                                             \
148         if (swallows->re_name != NULL) {                                                                                                             \
149             sasprintf(&serialized, "%s%s" #re_name "=\"%s\"", (serialized ? serialized : "["), (serialized ? " " : ""), swallows->re_name->pattern); \
150         }                                                                                                                                            \
151     } while (0)
152
153         APPEND_REGEX(class);
154         APPEND_REGEX(instance);
155         APPEND_REGEX(window_role);
156         APPEND_REGEX(title);
157
158         if (serialized == NULL) {
159             DLOG("This swallows specification is not serializable?!\n");
160             continue;
161         }
162
163         sasprintf(&serialized, "%s]", serialized);
164         DLOG("con %p (placeholder 0x%08x) line %d: %s\n", state->con, state->window, n, serialized);
165
166         i3String *str = i3string_from_utf8(serialized);
167         draw_util_text(str, &(state->surface), foreground, background,
168                        TEXT_PADDING,
169                        (n * (config.font.height + TEXT_PADDING)) + TEXT_PADDING,
170                        state->rect.width - 2 * TEXT_PADDING);
171         i3string_free(str);
172         n++;
173         free(serialized);
174     }
175
176     // TODO: render the watch symbol in a bigger font
177     i3String *line = i3string_from_utf8("⌚");
178     int text_width = predict_text_width(line);
179     int x = (state->rect.width / 2) - (text_width / 2);
180     int y = (state->rect.height / 2) - (config.font.height / 2);
181     draw_util_text(line, &(state->surface), foreground, background, x, y, text_width);
182     i3string_free(line);
183     xcb_flush(restore_conn);
184     xcb_aux_sync(restore_conn);
185 }
186
187 static void open_placeholder_window(Con *con) {
188     if (con_is_leaf(con) &&
189         (con->window == NULL || con->window->id == XCB_NONE) &&
190         !TAILQ_EMPTY(&(con->swallow_head)) &&
191         con->type == CT_CON) {
192         xcb_window_t placeholder = create_window(
193             restore_conn,
194             con->rect,
195             XCB_COPY_FROM_PARENT,
196             XCB_COPY_FROM_PARENT,
197             XCB_WINDOW_CLASS_INPUT_OUTPUT,
198             XCURSOR_CURSOR_POINTER,
199             true,
200             XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK,
201             (uint32_t[]){
202                 config.client.placeholder.background.colorpixel,
203                 XCB_EVENT_MASK_EXPOSURE | XCB_EVENT_MASK_STRUCTURE_NOTIFY,
204             });
205         /* Make i3 not focus this window. */
206         xcb_icccm_wm_hints_t hints;
207         xcb_icccm_wm_hints_set_none(&hints);
208         xcb_icccm_wm_hints_set_input(&hints, 0);
209         xcb_icccm_set_wm_hints(restore_conn, placeholder, &hints);
210         /* Set the same name as was stored in the layout file. While perhaps
211          * slightly confusing in the first instant, this brings additional
212          * clarity to which placeholder is waiting for which actual window. */
213         if (con->name != NULL)
214             xcb_change_property(restore_conn, XCB_PROP_MODE_REPLACE, placeholder,
215                                 A__NET_WM_NAME, A_UTF8_STRING, 8, strlen(con->name), con->name);
216         DLOG("Created placeholder window 0x%08x for leaf container %p / %s\n",
217              placeholder, con, con->name);
218
219         placeholder_state *state = scalloc(1, sizeof(placeholder_state));
220         state->window = placeholder;
221         state->con = con;
222         state->rect = con->rect;
223
224         draw_util_surface_init(restore_conn, &(state->surface), placeholder, get_visualtype(root_screen), state->rect.width, state->rect.height);
225         update_placeholder_contents(state);
226         TAILQ_INSERT_TAIL(&state_head, state, state);
227
228         /* create temporary id swallow to match the placeholder */
229         Match *temp_id = smalloc(sizeof(Match));
230         match_init(temp_id);
231         temp_id->dock = M_DONTCHECK;
232         temp_id->id = placeholder;
233         TAILQ_INSERT_HEAD(&(con->swallow_head), temp_id, matches);
234     }
235
236     Con *child;
237     TAILQ_FOREACH(child, &(con->nodes_head), nodes) {
238         open_placeholder_window(child);
239     }
240     TAILQ_FOREACH(child, &(con->floating_head), floating_windows) {
241         open_placeholder_window(child);
242     }
243 }
244
245 /*
246  * Open placeholder windows for all children of parent. The placeholder window
247  * will vanish as soon as a real window is swallowed by the container. Until
248  * then, it exposes the criteria that must be fulfilled for a window to be
249  * swallowed by this container.
250  *
251  */
252 void restore_open_placeholder_windows(Con *parent) {
253     Con *child;
254     TAILQ_FOREACH(child, &(parent->nodes_head), nodes) {
255         open_placeholder_window(child);
256     }
257     TAILQ_FOREACH(child, &(parent->floating_head), floating_windows) {
258         open_placeholder_window(child);
259     }
260
261     xcb_flush(restore_conn);
262 }
263
264 /*
265  * Kill the placeholder window, if placeholder refers to a placeholder window.
266  * This function is called when manage.c puts a window into an existing
267  * container. In order not to leak resources, we need to destroy the window and
268  * all associated X11 objects (pixmap/gc).
269  *
270  */
271 bool restore_kill_placeholder(xcb_window_t placeholder) {
272     placeholder_state *state;
273     TAILQ_FOREACH(state, &state_head, state) {
274         if (state->window != placeholder)
275             continue;
276
277         xcb_destroy_window(restore_conn, state->window);
278         draw_util_surface_free(restore_conn, &(state->surface));
279         TAILQ_REMOVE(&state_head, state, state);
280         free(state);
281         DLOG("placeholder window 0x%08x destroyed.\n", placeholder);
282         return true;
283     }
284
285     DLOG("0x%08x is not a placeholder window, ignoring.\n", placeholder);
286     return false;
287 }
288
289 static void expose_event(xcb_expose_event_t *event) {
290     placeholder_state *state;
291     TAILQ_FOREACH(state, &state_head, state) {
292         if (state->window != event->window)
293             continue;
294
295         DLOG("refreshing window 0x%08x contents (con %p)\n", state->window, state->con);
296
297         update_placeholder_contents(state);
298
299         return;
300     }
301
302     ELOG("Received ExposeEvent for unknown window 0x%08x\n", event->window);
303 }
304
305 /*
306  * Window size has changed. Update the width/height, then recreate the back
307  * buffer pixmap and the accompanying graphics context and force an immediate
308  * re-rendering.
309  *
310  */
311 static void configure_notify(xcb_configure_notify_event_t *event) {
312     placeholder_state *state;
313     TAILQ_FOREACH(state, &state_head, state) {
314         if (state->window != event->window)
315             continue;
316
317         DLOG("ConfigureNotify: window 0x%08x has now width=%d, height=%d (con %p)\n",
318              state->window, event->width, event->height, state->con);
319
320         state->rect.width = event->width;
321         state->rect.height = event->height;
322
323         draw_util_surface_set_size(&(state->surface), state->rect.width, state->rect.height);
324
325         update_placeholder_contents(state);
326
327         return;
328     }
329
330     ELOG("Received ConfigureNotify for unknown window 0x%08x\n", event->window);
331 }
332
333 static void restore_handle_event(int type, xcb_generic_event_t *event) {
334     switch (type) {
335         case XCB_EXPOSE:
336             if (((xcb_expose_event_t *)event)->count == 0) {
337                 expose_event((xcb_expose_event_t *)event);
338             }
339
340             break;
341         case XCB_CONFIGURE_NOTIFY:
342             configure_notify((xcb_configure_notify_event_t *)event);
343             break;
344         default:
345             DLOG("Received unhandled X11 event of type %d\n", type);
346             break;
347     }
348 }