i3
restore_layout.c
Go to the documentation of this file.
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 typedef struct placeholder_state {
20  xcb_window_t window;
22  Con *con;
23 
26 
28  xcb_pixmap_t pixmap;
30  xcb_gcontext_t gc;
31 
34 
35 static TAILQ_HEAD(state_head, placeholder_state) state_head =
36  TAILQ_HEAD_INITIALIZER(state_head);
37 
38 static xcb_connection_t *restore_conn;
39 
40 static struct ev_io *xcb_watcher;
41 static struct ev_check *xcb_check;
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_flush(restore_conn);
52 }
53 
54 static void restore_xcb_check_cb(EV_P_ ev_check *w, int revents) {
55  xcb_generic_event_t *event;
56 
57  if (xcb_connection_has_error(restore_conn)) {
58  DLOG("restore X11 connection has an error, reconnecting\n");
60  return;
61  }
62 
63  while ((event = xcb_poll_for_event(restore_conn)) != NULL) {
64  if (event->response_type == 0) {
65  xcb_generic_error_t *error = (xcb_generic_error_t *)event;
66  DLOG("X11 Error received (probably harmless)! sequence 0x%x, error_code = %d\n",
67  error->sequence, error->error_code);
68  free(event);
69  continue;
70  }
71 
72  /* Strip off the highest bit (set if the event is generated) */
73  int type = (event->response_type & 0x7F);
74 
75  restore_handle_event(type, event);
76 
77  free(event);
78  }
79 }
80 
81 /*
82  * Opens a separate connection to X11 for placeholder windows when restoring
83  * layouts. This is done as a safety measure (users can xkill a placeholder
84  * window without killing their window manager) and for better isolation, both
85  * on the wire to X11 and thus also in the code.
86  *
87  */
88 void restore_connect(void) {
89  if (restore_conn != NULL) {
90  /* This is not the initial connect, but a reconnect, most likely
91  * because our X11 connection was killed (e.g. by a user with xkill. */
92  ev_io_stop(main_loop, xcb_watcher);
93  ev_check_stop(main_loop, xcb_check);
94  ev_prepare_stop(main_loop, xcb_prepare);
95 
97  while (!TAILQ_EMPTY(&state_head)) {
98  state = TAILQ_FIRST(&state_head);
99  TAILQ_REMOVE(&state_head, state, state);
100  free(state);
101  }
102 
103  /* xcb_disconnect leaks memory in libxcb versions earlier than 1.11,
104  * but it’s the right function to call. See
105  * http://cgit.freedesktop.org/xcb/libxcb/commit/src/xcb_conn.c?id=4dcbfd77b
106  */
107  xcb_disconnect(restore_conn);
108  free(xcb_watcher);
109  free(xcb_check);
110  free(xcb_prepare);
111  }
112 
113  int screen;
114  restore_conn = xcb_connect(NULL, &screen);
115  if (restore_conn == NULL || xcb_connection_has_error(restore_conn)) {
116  if (restore_conn != NULL) {
117  xcb_disconnect(restore_conn);
118  }
119 #ifdef I3_ASAN_ENABLED
120  __lsan_do_leak_check();
121 #endif
122  errx(EXIT_FAILURE, "Cannot open display\n");
123  }
124 
125  xcb_watcher = scalloc(1, sizeof(struct ev_io));
126  xcb_check = scalloc(1, sizeof(struct ev_check));
127  xcb_prepare = scalloc(1, sizeof(struct ev_prepare));
128 
129  ev_io_init(xcb_watcher, restore_xcb_got_event, xcb_get_file_descriptor(restore_conn), EV_READ);
130  ev_io_start(main_loop, xcb_watcher);
131 
132  ev_check_init(xcb_check, restore_xcb_check_cb);
133  ev_check_start(main_loop, xcb_check);
134 
135  ev_prepare_init(xcb_prepare, restore_xcb_prepare_cb);
136  ev_prepare_start(main_loop, xcb_prepare);
137 }
138 
140  xcb_change_gc(restore_conn, state->gc, XCB_GC_FOREGROUND,
141  (uint32_t[]){config.client.placeholder.background.colorpixel});
142  xcb_poly_fill_rectangle(restore_conn, state->pixmap, state->gc, 1,
143  (xcb_rectangle_t[]){{0, 0, state->rect.width, state->rect.height}});
144 
145  // TODO: make i3font functions per-connection, at least these two for now…?
146  xcb_flush(restore_conn);
147  xcb_aux_sync(restore_conn);
148 
150 
151  Match *swallows;
152  int n = 0;
153  TAILQ_FOREACH(swallows, &(state->con->swallow_head), matches) {
154  char *serialized = NULL;
155 
156 #define APPEND_REGEX(re_name) \
157  do { \
158  if (swallows->re_name != NULL) { \
159  sasprintf(&serialized, "%s%s" #re_name "=\"%s\"", (serialized ? serialized : "["), (serialized ? " " : ""), swallows->re_name->pattern); \
160  } \
161  } while (0)
162 
163  APPEND_REGEX(class);
164  APPEND_REGEX(instance);
165  APPEND_REGEX(window_role);
166  APPEND_REGEX(title);
167 
168  if (serialized == NULL) {
169  DLOG("This swallows specification is not serializable?!\n");
170  continue;
171  }
172 
173  sasprintf(&serialized, "%s]", serialized);
174  DLOG("con %p (placeholder 0x%08x) line %d: %s\n", state->con, state->window, n, serialized);
175 
176  i3String *str = i3string_from_utf8(serialized);
177  draw_text(str, state->pixmap, state->gc, NULL, 2, (n * (config.font.height + 2)) + 2, state->rect.width - 2);
178  i3string_free(str);
179  n++;
180  free(serialized);
181  }
182 
183  // TODO: render the watch symbol in a bigger font
184  i3String *line = i3string_from_utf8("⌚");
185  int text_width = predict_text_width(line);
186  int x = (state->rect.width / 2) - (text_width / 2);
187  int y = (state->rect.height / 2) - (config.font.height / 2);
188  draw_text(line, state->pixmap, state->gc, NULL, x, y, text_width);
189  i3string_free(line);
190  xcb_flush(conn);
191  xcb_aux_sync(conn);
192 }
193 
195  if (con_is_leaf(con) &&
196  (con->window == NULL || con->window->id == XCB_NONE) &&
197  !TAILQ_EMPTY(&(con->swallow_head)) &&
198  con->type == CT_CON) {
199  xcb_window_t placeholder = create_window(
200  restore_conn,
201  con->rect,
202  XCB_COPY_FROM_PARENT,
203  XCB_COPY_FROM_PARENT,
204  XCB_WINDOW_CLASS_INPUT_OUTPUT,
206  true,
207  XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK,
208  (uint32_t[]){
209  config.client.placeholder.background.colorpixel,
210  XCB_EVENT_MASK_EXPOSURE | XCB_EVENT_MASK_STRUCTURE_NOTIFY,
211  });
212  /* Make i3 not focus this window. */
213  xcb_icccm_wm_hints_t hints;
214  xcb_icccm_wm_hints_set_none(&hints);
215  xcb_icccm_wm_hints_set_input(&hints, 0);
216  xcb_icccm_set_wm_hints(restore_conn, placeholder, &hints);
217  /* Set the same name as was stored in the layout file. While perhaps
218  * slightly confusing in the first instant, this brings additional
219  * clarity to which placeholder is waiting for which actual window. */
220  if (con->name != NULL)
221  xcb_change_property(restore_conn, XCB_PROP_MODE_REPLACE, placeholder,
222  A__NET_WM_NAME, A_UTF8_STRING, 8, strlen(con->name), con->name);
223  DLOG("Created placeholder window 0x%08x for leaf container %p / %s\n",
224  placeholder, con, con->name);
225 
227  state->window = placeholder;
228  state->con = con;
229  state->rect = con->rect;
230  state->pixmap = xcb_generate_id(restore_conn);
231  xcb_create_pixmap(restore_conn, root_depth, state->pixmap,
232  state->window, state->rect.width, state->rect.height);
233  state->gc = xcb_generate_id(restore_conn);
234  xcb_create_gc(restore_conn, state->gc, state->pixmap, XCB_GC_GRAPHICS_EXPOSURES, (uint32_t[]){0});
236  TAILQ_INSERT_TAIL(&state_head, state, state);
237 
238  /* create temporary id swallow to match the placeholder */
239  Match *temp_id = smalloc(sizeof(Match));
240  match_init(temp_id);
241  temp_id->dock = M_DONTCHECK;
242  temp_id->id = placeholder;
243  TAILQ_INSERT_HEAD(&(con->swallow_head), temp_id, matches);
244  }
245 
246  Con *child;
247  TAILQ_FOREACH(child, &(con->nodes_head), nodes) {
249  }
250  TAILQ_FOREACH(child, &(con->floating_head), floating_windows) {
252  }
253 }
254 
255 /*
256  * Open placeholder windows for all children of parent. The placeholder window
257  * will vanish as soon as a real window is swallowed by the container. Until
258  * then, it exposes the criteria that must be fulfilled for a window to be
259  * swallowed by this container.
260  *
261  */
263  Con *child;
264  TAILQ_FOREACH(child, &(parent->nodes_head), nodes) {
266  }
267  TAILQ_FOREACH(child, &(parent->floating_head), floating_windows) {
269  }
270 
271  xcb_flush(restore_conn);
272 }
273 
274 /*
275  * Kill the placeholder window, if placeholder refers to a placeholder window.
276  * This function is called when manage.c puts a window into an existing
277  * container. In order not to leak resources, we need to destroy the window and
278  * all associated X11 objects (pixmap/gc).
279  *
280  */
281 bool restore_kill_placeholder(xcb_window_t placeholder) {
283  TAILQ_FOREACH(state, &state_head, state) {
284  if (state->window != placeholder)
285  continue;
286 
287  xcb_destroy_window(restore_conn, state->window);
288  xcb_free_pixmap(restore_conn, state->pixmap);
289  xcb_free_gc(restore_conn, state->gc);
290  TAILQ_REMOVE(&state_head, state, state);
291  free(state);
292  DLOG("placeholder window 0x%08x destroyed.\n", placeholder);
293  return true;
294  }
295 
296  DLOG("0x%08x is not a placeholder window, ignoring.\n", placeholder);
297  return false;
298 }
299 
300 static void expose_event(xcb_expose_event_t *event) {
302  TAILQ_FOREACH(state, &state_head, state) {
303  if (state->window != event->window)
304  continue;
305 
306  DLOG("refreshing window 0x%08x contents (con %p)\n", state->window, state->con);
307 
308  /* Since we render to our pixmap on every change anyways, expose events
309  * only tell us that the X server lost (parts of) the window contents. We
310  * can handle that by copying the appropriate part from our pixmap to the
311  * window. */
312  xcb_copy_area(restore_conn, state->pixmap, state->window, state->gc,
313  event->x, event->y, event->x, event->y,
314  event->width, event->height);
315  xcb_flush(restore_conn);
316  return;
317  }
318 
319  ELOG("Received ExposeEvent for unknown window 0x%08x\n", event->window);
320 }
321 
322 /*
323  * Window size has changed. Update the width/height, then recreate the back
324  * buffer pixmap and the accompanying graphics context and force an immediate
325  * re-rendering.
326  *
327  */
328 static void configure_notify(xcb_configure_notify_event_t *event) {
330  TAILQ_FOREACH(state, &state_head, state) {
331  if (state->window != event->window)
332  continue;
333 
334  DLOG("ConfigureNotify: window 0x%08x has now width=%d, height=%d (con %p)\n",
335  state->window, event->width, event->height, state->con);
336 
337  state->rect.width = event->width;
338  state->rect.height = event->height;
339 
340  xcb_free_pixmap(restore_conn, state->pixmap);
341  xcb_free_gc(restore_conn, state->gc);
342 
343  state->pixmap = xcb_generate_id(restore_conn);
344  xcb_create_pixmap(restore_conn, root_depth, state->pixmap,
345  state->window, state->rect.width, state->rect.height);
346  state->gc = xcb_generate_id(restore_conn);
347  xcb_create_gc(restore_conn, state->gc, state->pixmap, XCB_GC_GRAPHICS_EXPOSURES, (uint32_t[]){0});
348 
350  xcb_copy_area(restore_conn, state->pixmap, state->window, state->gc,
351  0, 0, 0, 0, state->rect.width, state->rect.height);
352  xcb_flush(restore_conn);
353  return;
354  }
355 
356  ELOG("Received ConfigureNotify for unknown window 0x%08x\n", event->window);
357 }
358 
359 static void restore_handle_event(int type, xcb_generic_event_t *event) {
360  switch (type) {
361  case XCB_EXPOSE:
362  expose_event((xcb_expose_event_t *)event);
363  break;
364  case XCB_CONFIGURE_NOTIFY:
365  configure_notify((xcb_configure_notify_event_t *)event);
366  break;
367  default:
368  DLOG("Received unhandled X11 event of type %d\n", type);
369  break;
370  }
371 }
void draw_text(i3String *text, xcb_drawable_t drawable, xcb_gcontext_t gc, xcb_visualtype_t *visual, int x, int y, int max_width)
Draws text onto the specified X drawable (normally a pixmap) at the specified coordinates (from the t...
void i3string_free(i3String *str)
Free an i3String.
xcb_window_t id
Definition: data.h:477
#define ELOG(fmt,...)
Definition: libi3.h:89
color_t text
Definition: configuration.h:56
xcb_window_t window
The X11 placeholder window.
i3Font font
Definition: configuration.h:95
static void restore_xcb_prepare_cb(EV_P_ ev_prepare *w, int revents)
uint32_t height
Definition: data.h:152
#define TAILQ_ENTRY(type)
Definition: queue.h:327
static void configure_notify(xcb_configure_notify_event_t *event)
static struct ev_check * xcb_check
Definition: main.c:36
Rect rect
Current size of the placeholder window (to detect size changes).
void set_font_colors(xcb_gcontext_t gc, color_t foreground, color_t background)
Defines the colors to be used for the forthcoming draw_text calls.
int height
The height of the font, built from font_ascent + font_descent.
Definition: libi3.h:57
xcb_gcontext_t gc
The graphics context for “pixmap”.
struct Config::config_client client
static void update_placeholder_contents(placeholder_state *state)
void restore_connect(void)
Opens a separate connection to X11 for placeholder windows when restoring layouts.
static void open_placeholder_window(Con *con)
bool con_is_leaf(Con *con)
Returns true when this node is a leaf node (has no children)
Definition: con.c:256
void restore_open_placeholder_windows(Con *parent)
Open placeholder windows for all children of parent.
static void restore_xcb_check_cb(EV_P_ ev_check *w, int revents)
static void expose_event(xcb_expose_event_t *event)
struct Rect rect
Definition: data.h:594
i3String * i3string_from_utf8(const char *from_utf8)
Build an i3String from an UTF-8 encoded string.
Stores a rectangle, for example the size of a window, the child window etc.
Definition: data.h:148
#define TAILQ_INSERT_TAIL(head, elm, field)
Definition: queue.h:376
static TAILQ_HEAD(state_head, placeholder_state)
xcb_connection_t * conn
XCB connection and root screen.
Definition: main.c:42
#define TAILQ_HEAD_INITIALIZER(head)
Definition: queue.h:324
#define TAILQ_FOREACH(var, head, field)
Definition: queue.h:347
static cmdp_state state
int sasprintf(char **strp, const char *fmt,...)
Safe-wrapper around asprintf which exits if it returns -1 (meaning that there is no more memory avail...
#define TAILQ_FIRST(head)
Definition: queue.h:336
void * scalloc(size_t num, size_t size)
Safe-wrapper around calloc which exits if malloc returns NULL (meaning that there is no more memory a...
A "match" is a data structure which acts like a mask or expression to match certain windows or not...
Definition: data.h:453
char * name
Definition: data.h:604
#define TAILQ_REMOVE(head, elm, field)
Definition: queue.h:402
color_t background
Definition: configuration.h:55
void match_init(Match *match)
Definition: match.c:26
struct Colortriple placeholder
struct Window * window
Definition: data.h:625
xcb_window_t id
Definition: data.h:376
int predict_text_width(i3String *text)
Predict the text width in pixels for the given text.
void * smalloc(size_t size)
Safe-wrapper around malloc which exits if malloc returns NULL (meaning that there is no more memory a...
A &#39;Con&#39; represents everything from the X11 root window down to a single X11 window.
Definition: data.h:558
uint32_t width
Definition: data.h:151
#define DLOG(fmt,...)
Definition: libi3.h:94
uint8_t root_depth
Definition: main.c:60
Con * con
The container to which this placeholder window belongs.
struct _i3String i3String
Opaque data structure for storing strings.
Definition: libi3.h:38
#define TAILQ_EMPTY(head)
Definition: queue.h:344
Config config
Definition: config.c:16
#define APPEND_REGEX(re_name)
uint32_t y
Definition: data.h:128
static void restore_handle_event(int type, xcb_generic_event_t *event)
enum Match::@15 dock
struct placeholder_state placeholder_state
enum Con::@20 type
bool restore_kill_placeholder(xcb_window_t placeholder)
Kill the placeholder window, if placeholder refers to a placeholder window.
xcb_window_t create_window(xcb_connection_t *conn, Rect dims, uint16_t depth, xcb_visualid_t visual, uint16_t window_class, enum xcursor_cursor_t cursor, bool map, uint32_t mask, uint32_t *values)
Convenience wrapper around xcb_create_window which takes care of depth, generating an ID and checking...
Definition: xcb.c:19
#define TAILQ_INSERT_HEAD(head, elm, field)
Definition: queue.h:366
uint32_t x
Definition: data.h:127
xcb_pixmap_t pixmap
The pixmap to render on (back buffer).
struct ev_loop * main_loop
Definition: main.c:64