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-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 {
18  xcb_window_t window;
20  Con *con;
21 
24 
26  xcb_pixmap_t pixmap;
28  xcb_gcontext_t gc;
29 
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");
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 
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  free(restore_conn);
102  free(xcb_watcher);
103  free(xcb_check);
104  free(xcb_prepare);
105  }
106 
107  int screen;
108  restore_conn = xcb_connect(NULL, &screen);
109  if (restore_conn == NULL || xcb_connection_has_error(restore_conn))
110  errx(EXIT_FAILURE, "Cannot open display\n");
111 
112  xcb_watcher = scalloc(sizeof(struct ev_io));
113  xcb_check = scalloc(sizeof(struct ev_check));
114  xcb_prepare = scalloc(sizeof(struct ev_prepare));
115 
116  ev_io_init(xcb_watcher, restore_xcb_got_event, xcb_get_file_descriptor(restore_conn), EV_READ);
117  ev_io_start(main_loop, xcb_watcher);
118 
119  ev_check_init(xcb_check, restore_xcb_check_cb);
120  ev_check_start(main_loop, xcb_check);
121 
122  ev_prepare_init(xcb_prepare, restore_xcb_prepare_cb);
123  ev_prepare_start(main_loop, xcb_prepare);
124 }
125 
127  xcb_change_gc(restore_conn, state->gc, XCB_GC_FOREGROUND,
128  (uint32_t[]){config.client.placeholder.background});
129  xcb_poly_fill_rectangle(restore_conn, state->pixmap, state->gc, 1,
130  (xcb_rectangle_t[]){{0, 0, state->rect.width, state->rect.height}});
131 
132  // TODO: make i3font functions per-connection, at least these two for now…?
133  xcb_flush(restore_conn);
134  xcb_aux_sync(restore_conn);
135 
137 
138  Match *swallows;
139  int n = 0;
140  TAILQ_FOREACH(swallows, &(state->con->swallow_head), matches) {
141  char *serialized = NULL;
142 
143 #define APPEND_REGEX(re_name) \
144  do { \
145  if (swallows->re_name != NULL) { \
146  sasprintf(&serialized, "%s%s" #re_name "=\"%s\"", (serialized ? serialized : "["), (serialized ? " " : ""), swallows->re_name->pattern); \
147  } \
148  } while (0)
149 
150  APPEND_REGEX(class);
151  APPEND_REGEX(instance);
152  APPEND_REGEX(window_role);
153  APPEND_REGEX(title);
154 
155  if (serialized == NULL) {
156  DLOG("This swallows specification is not serializable?!\n");
157  continue;
158  }
159 
160  sasprintf(&serialized, "%s]", serialized);
161  DLOG("con %p (placeholder 0x%08x) line %d: %s\n", state->con, state->window, n, serialized);
162 
163  i3String *str = i3string_from_utf8(serialized);
164  draw_text(str, state->pixmap, state->gc, 2, (n * (config.font.height + 2)) + 2, state->rect.width - 2);
165  i3string_free(str);
166  n++;
167  free(serialized);
168  }
169 
170  // TODO: render the watch symbol in a bigger font
171  i3String *line = i3string_from_utf8("⌚");
172  int text_width = predict_text_width(line);
173  int x = (state->rect.width / 2) - (text_width / 2);
174  int y = (state->rect.height / 2) - (config.font.height / 2);
175  draw_text(line, state->pixmap, state->gc, x, y, text_width);
176  i3string_free(line);
177  xcb_flush(conn);
178  xcb_aux_sync(conn);
179 }
180 
181 static void open_placeholder_window(Con *con) {
182  if (con_is_leaf(con) &&
183  (con->window == NULL || con->window->id == XCB_NONE) &&
184  !TAILQ_EMPTY(&(con->swallow_head)) &&
185  con->type == CT_CON) {
186  xcb_window_t placeholder = create_window(
187  restore_conn,
188  con->rect,
189  XCB_COPY_FROM_PARENT,
190  XCB_COPY_FROM_PARENT,
191  XCB_WINDOW_CLASS_INPUT_OUTPUT,
193  true,
194  XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK,
195  (uint32_t[]){
196  config.client.placeholder.background,
197  XCB_EVENT_MASK_EXPOSURE | XCB_EVENT_MASK_STRUCTURE_NOTIFY,
198  });
199  /* Make i3 not focus this window. */
200  xcb_icccm_wm_hints_t hints;
201  xcb_icccm_wm_hints_set_none(&hints);
202  xcb_icccm_wm_hints_set_input(&hints, 0);
203  xcb_icccm_set_wm_hints(restore_conn, placeholder, &hints);
204  /* Set the same name as was stored in the layout file. While perhaps
205  * slightly confusing in the first instant, this brings additional
206  * clarity to which placeholder is waiting for which actual window. */
207  if (con->name != NULL)
208  xcb_change_property(restore_conn, XCB_PROP_MODE_REPLACE, placeholder,
209  A__NET_WM_NAME, A_UTF8_STRING, 8, strlen(con->name), con->name);
210  DLOG("Created placeholder window 0x%08x for leaf container %p / %s\n",
211  placeholder, con, con->name);
212 
214  state->window = placeholder;
215  state->con = con;
216  state->rect = con->rect;
217  state->pixmap = xcb_generate_id(restore_conn);
218  xcb_create_pixmap(restore_conn, root_depth, state->pixmap,
219  state->window, state->rect.width, state->rect.height);
220  state->gc = xcb_generate_id(restore_conn);
221  xcb_create_gc(restore_conn, state->gc, state->pixmap, XCB_GC_GRAPHICS_EXPOSURES, (uint32_t[]){0});
223  TAILQ_INSERT_TAIL(&state_head, state, state);
224 
225  /* create temporary id swallow to match the placeholder */
226  Match *temp_id = smalloc(sizeof(Match));
227  match_init(temp_id);
228  temp_id->id = placeholder;
229  TAILQ_INSERT_HEAD(&(con->swallow_head), temp_id, matches);
230  }
231 
232  Con *child;
233  TAILQ_FOREACH(child, &(con->nodes_head), nodes) {
235  }
236  TAILQ_FOREACH(child, &(con->floating_head), floating_windows) {
238  }
239 }
240 
241 /*
242  * Open placeholder windows for all children of parent. The placeholder window
243  * will vanish as soon as a real window is swallowed by the container. Until
244  * then, it exposes the criteria that must be fulfilled for a window to be
245  * swallowed by this container.
246  *
247  */
249  Con *child;
250  TAILQ_FOREACH(child, &(parent->nodes_head), nodes) {
252  }
253  TAILQ_FOREACH(child, &(parent->floating_head), floating_windows) {
255  }
256 
257  xcb_flush(restore_conn);
258 }
259 
260 /*
261  * Kill the placeholder window, if placeholder refers to a placeholder window.
262  * This function is called when manage.c puts a window into an existing
263  * container. In order not to leak resources, we need to destroy the window and
264  * all associated X11 objects (pixmap/gc).
265  *
266  */
267 bool restore_kill_placeholder(xcb_window_t placeholder) {
269  TAILQ_FOREACH(state, &state_head, state) {
270  if (state->window != placeholder)
271  continue;
272 
273  xcb_destroy_window(restore_conn, state->window);
274  xcb_free_pixmap(restore_conn, state->pixmap);
275  xcb_free_gc(restore_conn, state->gc);
276  TAILQ_REMOVE(&state_head, state, state);
277  free(state);
278  DLOG("placeholder window 0x%08x destroyed.\n", placeholder);
279  return true;
280  }
281 
282  DLOG("0x%08x is not a placeholder window, ignoring.\n", placeholder);
283  return false;
284 }
285 
286 static void expose_event(xcb_expose_event_t *event) {
288  TAILQ_FOREACH(state, &state_head, state) {
289  if (state->window != event->window)
290  continue;
291 
292  DLOG("refreshing window 0x%08x contents (con %p)\n", state->window, state->con);
293 
294  /* Since we render to our pixmap on every change anyways, expose events
295  * only tell us that the X server lost (parts of) the window contents. We
296  * can handle that by copying the appropriate part from our pixmap to the
297  * window. */
298  xcb_copy_area(restore_conn, state->pixmap, state->window, state->gc,
299  event->x, event->y, event->x, event->y,
300  event->width, event->height);
301  xcb_flush(restore_conn);
302  return;
303  }
304 
305  ELOG("Received ExposeEvent for unknown window 0x%08x\n", event->window);
306 }
307 
308 /*
309  * Window size has changed. Update the width/height, then recreate the back
310  * buffer pixmap and the accompanying graphics context and force an immediate
311  * re-rendering.
312  *
313  */
314 static void configure_notify(xcb_configure_notify_event_t *event) {
316  TAILQ_FOREACH(state, &state_head, state) {
317  if (state->window != event->window)
318  continue;
319 
320  DLOG("ConfigureNotify: window 0x%08x has now width=%d, height=%d (con %p)\n",
321  state->window, event->width, event->height, state->con);
322 
323  state->rect.width = event->width;
324  state->rect.height = event->height;
325 
326  xcb_free_pixmap(restore_conn, state->pixmap);
327  xcb_free_gc(restore_conn, state->gc);
328 
329  state->pixmap = xcb_generate_id(restore_conn);
330  xcb_create_pixmap(restore_conn, root_depth, state->pixmap,
331  state->window, state->rect.width, state->rect.height);
332  state->gc = xcb_generate_id(restore_conn);
333  xcb_create_gc(restore_conn, state->gc, state->pixmap, XCB_GC_GRAPHICS_EXPOSURES, (uint32_t[]){0});
334 
336  xcb_copy_area(restore_conn, state->pixmap, state->window, state->gc,
337  0, 0, 0, 0, state->rect.width, state->rect.height);
338  xcb_flush(restore_conn);
339  return;
340  }
341 
342  ELOG("Received ConfigureNotify for unknown window 0x%08x\n", event->window);
343 }
344 
345 static void restore_handle_event(int type, xcb_generic_event_t *event) {
346  switch (type) {
347  case XCB_EXPOSE:
348  expose_event((xcb_expose_event_t *)event);
349  break;
350  case XCB_CONFIGURE_NOTIFY:
351  configure_notify((xcb_configure_notify_event_t *)event);
352  break;
353  default:
354  DLOG("Received unhandled X11 event of type %d\n", type);
355  break;
356  }
357 }
static TAILQ_HEAD(state_head, placeholder_state)
int predict_text_width(i3String *text)
Predict the text width in pixels for the given text.
uint32_t text
Definition: config.h:55
static void open_placeholder_window(Con *con)
#define TAILQ_REMOVE(head, elm, field)
Definition: queue.h:402
Config config
Definition: config.c:17
xcb_connection_t * conn
Definition: main.c:43
i3Font font
Definition: config.h:92
uint32_t background
Definition: config.h:54
Stores a rectangle, for example the size of a window, the child window etc.
Definition: data.h:130
struct Rect rect
Definition: data.h:531
struct ev_loop * main_loop
Definition: main.c:65
#define TAILQ_FIRST(head)
Definition: queue.h:336
struct Window * window
Definition: data.h:564
static void restore_xcb_prepare_cb(EV_P_ ev_prepare *w, int revents)
struct Config::config_client client
static void update_placeholder_contents(placeholder_state *state)
bool restore_kill_placeholder(xcb_window_t placeholder)
Kill the placeholder window, if placeholder refers to a placeholder window.
#define APPEND_REGEX(re_name)
xcb_gcontext_t gc
The graphics context for “pixmap”.
i3String * i3string_from_utf8(const char *from_utf8)
Build an i3String from an UTF-8 encoded string.
enum Con::@18 type
#define DLOG(fmt,...)
Definition: libi3.h:86
xcb_pixmap_t pixmap
The pixmap to render on (back buffer).
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...
xcb_window_t id
Definition: data.h:423
static void restore_xcb_check_cb(EV_P_ ev_check *w, int revents)
struct _i3String i3String
Opaque data structure for storing strings.
Definition: libi3.h:28
xcb_window_t window
The X11 placeholder window.
uint8_t root_depth
Definition: main.c:61
bool con_is_leaf(Con *con)
Returns true when this node is a leaf node (has no children)
Definition: con.c:222
uint32_t height
Definition: data.h:134
#define TAILQ_INSERT_HEAD(head, elm, field)
Definition: queue.h:366
char * name
Definition: data.h:537
int height
The height of the font, built from font_ascent + font_descent.
Definition: libi3.h:47
#define xcb_icccm_wm_hints_t
Definition: xcb_compat.h:31
void draw_text(i3String *text, xcb_drawable_t drawable, xcb_gcontext_t gc, int x, int y, int max_width)
Draws text onto the specified X drawable (normally a pixmap) at the specified coordinates (from the t...
static void configure_notify(xcb_configure_notify_event_t *event)
#define TAILQ_EMPTY(head)
Definition: queue.h:344
Rect rect
Current size of the placeholder window (to detect size changes).
void i3string_free(i3String *str)
Free an i3String.
void restore_open_placeholder_windows(Con *parent)
Open placeholder windows for all children of parent.
#define TAILQ_ENTRY(type)
Definition: queue.h:327
#define ELOG(fmt,...)
Definition: libi3.h:81
A "match" is a data structure which acts like a mask or expression to match certain windows or not...
Definition: data.h:404
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
A 'Con' represents everything from the X11 root window down to a single X11 window.
Definition: data.h:496
void * scalloc(size_t size)
Safe-wrapper around calloc which exits if malloc returns NULL (meaning that there is no more memory a...
static void restore_handle_event(int type, xcb_generic_event_t *event)
void set_font_colors(xcb_gcontext_t gc, uint32_t foreground, uint32_t background)
Defines the colors to be used for the forthcoming draw_text calls.
#define TAILQ_INSERT_TAIL(head, elm, field)
Definition: queue.h:376
#define TAILQ_HEAD_INITIALIZER(head)
Definition: queue.h:324
uint32_t y
Definition: data.h:31
xcb_window_t id
Definition: data.h:345
void * smalloc(size_t size)
Safe-wrapper around malloc which exits if malloc returns NULL (meaning that there is no more memory a...
#define TAILQ_FOREACH(var, head, field)
Definition: queue.h:347
void restore_connect(void)
Opens a separate connection to X11 for placeholder windows when restoring layouts.
static void expose_event(xcb_expose_event_t *event)
static struct ev_check * xcb_check
Definition: main.c:37
uint32_t x
Definition: data.h:30
void match_init(Match *match)
Definition: match.c:28
uint32_t width
Definition: data.h:133
struct Colortriple placeholder
Definition: config.h:193
Con * con
The container to which this placeholder window belongs.