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