]> git.sur5r.net Git - i3/i3/blob - src/restore_layout.c
set original window title on placeholder windows
[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-2013 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 void restore_handle_event(int type, xcb_generic_event_t *event);
39
40 /* Documentation for these functions can be found in src/main.c, starting at xcb_got_event */
41 static void restore_xcb_got_event(EV_P_ struct ev_io *w, int revents) {
42 }
43
44 static void restore_xcb_prepare_cb(EV_P_ ev_prepare *w, int revents) {
45     xcb_flush(restore_conn);
46 }
47
48 static void restore_xcb_check_cb(EV_P_ ev_check *w, int revents) {
49     xcb_generic_event_t *event;
50
51     if (xcb_connection_has_error(restore_conn)) {
52         // TODO: figure out how to reconnect with libev
53         ELOG("connection has an error\n");
54         return;
55     }
56
57     while ((event = xcb_poll_for_event(restore_conn)) != NULL) {
58         if (event->response_type == 0) {
59             xcb_generic_error_t *error = (xcb_generic_error_t*)event;
60             DLOG("X11 Error received (probably harmless)! sequence 0x%x, error_code = %d\n",
61                  error->sequence, error->error_code);
62             free(event);
63             continue;
64         }
65
66         /* Strip off the highest bit (set if the event is generated) */
67         int type = (event->response_type & 0x7F);
68
69         restore_handle_event(type, event);
70
71         free(event);
72     }
73 }
74
75 /*
76  * Opens a separate connection to X11 for placeholder windows when restoring
77  * layouts. This is done as a safety measure (users can xkill a placeholder
78  * window without killing their window manager) and for better isolation, both
79  * on the wire to X11 and thus also in the code.
80  *
81  */
82 void restore_connect(void) {
83     int screen;
84     restore_conn = xcb_connect(NULL, &screen);
85     if (restore_conn == NULL || xcb_connection_has_error(restore_conn))
86         errx(EXIT_FAILURE, "Cannot open display\n");
87
88     struct ev_io *xcb_watcher = scalloc(sizeof(struct ev_io));
89     struct ev_check *xcb_check = scalloc(sizeof(struct ev_check));
90     struct ev_prepare *xcb_prepare = scalloc(sizeof(struct ev_prepare));
91
92     ev_io_init(xcb_watcher, restore_xcb_got_event, xcb_get_file_descriptor(restore_conn), EV_READ);
93     ev_io_start(main_loop, xcb_watcher);
94
95     ev_check_init(xcb_check, restore_xcb_check_cb);
96     ev_check_start(main_loop, xcb_check);
97
98     ev_prepare_init(xcb_prepare, restore_xcb_prepare_cb);
99     ev_prepare_start(main_loop, xcb_prepare);
100 }
101
102 static void update_placeholder_contents(placeholder_state *state) {
103     // TODO: introduce color configuration for placeholder windows.
104     xcb_change_gc(restore_conn, state->gc, XCB_GC_FOREGROUND, (uint32_t[]) { config.client.background });
105     xcb_poly_fill_rectangle(restore_conn, state->pixmap, state->gc, 1,
106             (xcb_rectangle_t[]) { { 0, 0, state->rect.width, state->rect.height } });
107
108     // TODO: make i3font functions per-connection, at least these two for now…?
109     xcb_flush(restore_conn);
110     xcb_aux_sync(restore_conn);
111
112     set_font_colors(state->gc, config.client.focused.background, 0);
113
114     Match *swallows;
115     int n = 0;
116     TAILQ_FOREACH(swallows, &(state->con->swallow_head), matches) {
117         char *serialized = NULL;
118
119 #define APPEND_REGEX(re_name) do { \
120     if (swallows->re_name != NULL) { \
121         sasprintf(&serialized, "%s%s" #re_name "=\"%s\"", \
122                   (serialized ? serialized : "["), \
123                   (serialized ? " " : ""), \
124                   swallows->re_name->pattern); \
125     } \
126 } while (0)
127
128         APPEND_REGEX(class);
129         APPEND_REGEX(instance);
130         APPEND_REGEX(window_role);
131         APPEND_REGEX(title);
132
133         if (serialized == NULL) {
134             DLOG("This swallows specification is not serializable?!\n");
135             continue;
136         }
137
138         sasprintf(&serialized, "%s]", serialized);
139         DLOG("con %p (placeholder 0x%08x) line %d: %s\n", state->con, state->window, n, serialized);
140
141         i3String *str = i3string_from_utf8(serialized);
142         draw_text(str, state->pixmap, state->gc, 2, (n * (config.font.height + 2)) + 2, state->rect.width - 2);
143         i3string_free(str);
144         n++;
145         free(serialized);
146     }
147
148     // TODO: render the watch symbol in a bigger font
149     i3String *line = i3string_from_utf8("⌚");
150     int text_width = predict_text_width(line);
151     int x = (state->rect.width / 2) - (text_width / 2);
152     int y = (state->rect.height / 2) - (config.font.height / 2);
153     draw_text(line, state->pixmap, state->gc, x, y, text_width);
154     i3string_free(line);
155     xcb_flush(conn);
156     xcb_aux_sync(conn);
157 }
158
159 static void open_placeholder_window(Con *con) {
160     if (con_is_leaf(con)) {
161         xcb_window_t placeholder = create_window(
162                 restore_conn,
163                 con->rect,
164                 XCB_COPY_FROM_PARENT,
165                 XCB_COPY_FROM_PARENT,
166                 XCB_WINDOW_CLASS_INPUT_OUTPUT,
167                 XCURSOR_CURSOR_POINTER,
168                 true,
169                 XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK,
170                 (uint32_t[]){
171                     // TODO: use the background color as background pixel to avoid flickering
172                     root_screen->white_pixel,
173                     XCB_EVENT_MASK_EXPOSURE | XCB_EVENT_MASK_STRUCTURE_NOTIFY,
174                 });
175         /* Set the same name as was stored in the layout file. While perhaps
176          * slightly confusing in the first instant, this brings additional
177          * clarity to which placeholder is waiting for which actual window. */
178         xcb_change_property(restore_conn, XCB_PROP_MODE_REPLACE, placeholder,
179                             A__NET_WM_NAME, A_UTF8_STRING, 8, strlen(con->name), con->name);
180         DLOG("Created placeholder window 0x%08x for leaf container %p / %s\n",
181              placeholder, con, con->name);
182
183         placeholder_state *state = scalloc(sizeof(placeholder_state));
184         state->window = placeholder;
185         state->con = con;
186         state->rect = con->rect;
187         state->pixmap = xcb_generate_id(restore_conn);
188         xcb_create_pixmap(restore_conn, root_depth, state->pixmap,
189                           state->window, state->rect.width, state->rect.height);
190         state->gc = xcb_generate_id(restore_conn);
191         xcb_create_gc(restore_conn, state->gc, state->pixmap, XCB_GC_GRAPHICS_EXPOSURES, (uint32_t[]){ 0 });
192         update_placeholder_contents(state);
193         TAILQ_INSERT_TAIL(&state_head, state, state);
194
195         /* create temporary id swallow to match the placeholder */
196         Match *temp_id = smalloc(sizeof(Match));
197         match_init(temp_id);
198         temp_id->id = placeholder;
199         TAILQ_INSERT_TAIL(&(con->swallow_head), temp_id, matches);
200     }
201
202     Con *child;
203     TAILQ_FOREACH(child, &(con->nodes_head), nodes) {
204         open_placeholder_window(child);
205     }
206     TAILQ_FOREACH(child, &(con->floating_head), floating_windows) {
207         open_placeholder_window(child);
208     }
209 }
210
211 /*
212  * Open placeholder windows for all children of parent. The placeholder window
213  * will vanish as soon as a real window is swallowed by the container. Until
214  * then, it exposes the criteria that must be fulfilled for a window to be
215  * swallowed by this container.
216  *
217  */
218 void restore_open_placeholder_windows(Con *parent) {
219     Con *child;
220     TAILQ_FOREACH(child, &(parent->nodes_head), nodes) {
221         open_placeholder_window(child);
222     }
223     TAILQ_FOREACH(child, &(parent->floating_head), floating_windows) {
224         open_placeholder_window(child);
225     }
226
227     xcb_flush(restore_conn);
228 }
229
230 static void expose_event(xcb_expose_event_t *event) {
231     placeholder_state *state;
232     TAILQ_FOREACH(state, &state_head, state) {
233         if (state->window != event->window)
234             continue;
235
236         DLOG("refreshing window 0x%08x contents (con %p)\n", state->window, state->con);
237
238         /* Since we render to our pixmap on every change anyways, expose events
239          * only tell us that the X server lost (parts of) the window contents. We
240          * can handle that by copying the appropriate part from our pixmap to the
241          * window. */
242         xcb_copy_area(restore_conn, state->pixmap, state->window, state->gc,
243                       event->x, event->y, event->x, event->y,
244                       event->width, event->height);
245         xcb_flush(restore_conn);
246         return;
247     }
248
249     ELOG("Received ExposeEvent for unknown window 0x%08x\n", event->window);
250 }
251
252 /*
253  * Window size has changed. Update the width/height, then recreate the back
254  * buffer pixmap and the accompanying graphics context and force an immediate
255  * re-rendering.
256  *
257  */
258 static void configure_notify(xcb_configure_notify_event_t *event) {
259     placeholder_state *state;
260     TAILQ_FOREACH(state, &state_head, state) {
261         if (state->window != event->window)
262             continue;
263
264         DLOG("ConfigureNotify: window 0x%08x has now width=%d, height=%d (con %p)\n",
265              state->window, event->width, event->height, state->con);
266
267         state->rect.width = event->width;
268         state->rect.height = event->height;
269
270         xcb_free_pixmap(restore_conn, state->pixmap);
271         xcb_free_gc(restore_conn, state->gc);
272
273         state->pixmap = xcb_generate_id(restore_conn);
274         xcb_create_pixmap(restore_conn, root_depth, state->pixmap,
275                           state->window, state->rect.width, state->rect.height);
276         state->gc = xcb_generate_id(restore_conn);
277         xcb_create_gc(restore_conn, state->gc, state->pixmap, XCB_GC_GRAPHICS_EXPOSURES, (uint32_t[]){ 0 });
278
279         update_placeholder_contents(state);
280         xcb_copy_area(restore_conn, state->pixmap, state->window, state->gc,
281                       0, 0, 0, 0, state->rect.width, state->rect.height);
282         xcb_flush(restore_conn);
283         return;
284     }
285
286     ELOG("Received ConfigureNotify for unknown window 0x%08x\n", event->window);
287 }
288
289 // TODO: this event loop is not taken care of in the floating event handler
290 static void restore_handle_event(int type, xcb_generic_event_t *event) {
291     switch (type) {
292         case XCB_EXPOSE:
293             expose_event((xcb_expose_event_t*)event);
294             break;
295         case XCB_CONFIGURE_NOTIFY:
296             configure_notify((xcb_configure_notify_event_t*)event);
297             break;
298         default:
299             DLOG("Received unhandled X11 event of type %d\n", type);
300             break;
301     }
302 }