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