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