2 #define I3__FILE__ "restore_layout.c"
36 static xcb_connection_t *restore_conn;
38 static struct ev_io *xcb_watcher;
40 static struct ev_prepare *xcb_prepare;
45 static
void restore_xcb_got_event(EV_P_ struct ev_io *w,
int revents) {
49 xcb_flush(restore_conn);
53 xcb_generic_event_t *event;
55 if (xcb_connection_has_error(restore_conn)) {
56 DLOG(
"restore X11 connection has an error, reconnecting\n");
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);
71 int type = (
event->response_type & 0x7F);
87 if (restore_conn != NULL) {
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");
112 xcb_watcher =
scalloc(
sizeof(
struct ev_io));
114 xcb_prepare =
scalloc(
sizeof(
struct ev_prepare));
116 ev_io_init(xcb_watcher, restore_xcb_got_event, xcb_get_file_descriptor(restore_conn), EV_READ);
123 ev_prepare_start(
main_loop, xcb_prepare);
127 xcb_change_gc(restore_conn, state->
gc, XCB_GC_FOREGROUND,
129 xcb_poly_fill_rectangle(restore_conn,
state->pixmap,
state->gc, 1,
130 (xcb_rectangle_t[]) {{0, 0,
state->rect.width,
state->rect.height}});
133 xcb_flush(restore_conn);
134 xcb_aux_sync(restore_conn);
141 char *serialized = NULL;
143 #define APPEND_REGEX(re_name) \
145 if (swallows->re_name != NULL) { \
146 sasprintf(&serialized, "%s%s" #re_name "=\"%s\"", (serialized ? serialized : "["), (serialized ? " " : ""), swallows->re_name->pattern); \
155 if (serialized == NULL) {
156 DLOG(
"This swallows specification is not serializable?!\n");
160 sasprintf(&serialized,
"%s]", serialized);
161 DLOG(
"con %p (placeholder 0x%08x) line %d: %s\n",
state->con,
state->window, n, serialized);
173 int x = (
state->rect.width / 2) - (text_width / 2);
187 XCB_COPY_FROM_PARENT,
188 XCB_COPY_FROM_PARENT,
189 XCB_WINDOW_CLASS_INPUT_OUTPUT,
192 XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK,
195 XCB_EVENT_MASK_EXPOSURE | XCB_EVENT_MASK_STRUCTURE_NOTIFY,
200 xcb_change_property(restore_conn, XCB_PROP_MODE_REPLACE, placeholder,
201 A__NET_WM_NAME, A_UTF8_STRING, 8, strlen(con->
name), con->
name);
202 DLOG(
"Created placeholder window 0x%08x for leaf container %p / %s\n",
203 placeholder, con, con->
name);
206 state->
window = placeholder;
209 state->
pixmap = xcb_generate_id(restore_conn);
212 state->
gc = xcb_generate_id(restore_conn);
213 xcb_create_gc(restore_conn, state->
gc, state->
pixmap, XCB_GC_GRAPHICS_EXPOSURES, (uint32_t[]) {0});
220 temp_id->
id = placeholder;
228 TAILQ_FOREACH (child, &(con->floating_head), floating_windows) {
245 TAILQ_FOREACH (child, &(parent->floating_head), floating_windows) {
249 xcb_flush(restore_conn);
262 if (state->
window != placeholder)
265 xcb_destroy_window(restore_conn, state->
window);
266 xcb_free_pixmap(restore_conn, state->
pixmap);
267 xcb_free_gc(restore_conn, state->
gc);
270 DLOG(
"placeholder window 0x%08x destroyed.\n", placeholder);
274 DLOG(
"0x%08x is not a placeholder window, ignoring.\n", placeholder);
281 if (state->
window != event->window)
284 DLOG(
"refreshing window 0x%08x contents (con %p)\n", state->
window, state->
con);
290 xcb_copy_area(restore_conn, state->
pixmap, state->
window, state->
gc,
291 event->x, event->y, event->x, event->y,
292 event->width, event->height);
293 xcb_flush(restore_conn);
297 ELOG(
"Received ExposeEvent for unknown window 0x%08x\n", event->window);
309 if (state->
window != event->window)
312 DLOG(
"ConfigureNotify: window 0x%08x has now width=%d, height=%d (con %p)\n",
313 state->
window, event->width, event->height, state->
con);
318 xcb_free_pixmap(restore_conn, state->
pixmap);
319 xcb_free_gc(restore_conn, state->
gc);
321 state->
pixmap = xcb_generate_id(restore_conn);
324 state->
gc = xcb_generate_id(restore_conn);
325 xcb_create_gc(restore_conn, state->
gc, state->
pixmap, XCB_GC_GRAPHICS_EXPOSURES, (uint32_t[]) {0});
328 xcb_copy_area(restore_conn, state->
pixmap, state->
window, state->
gc,
330 xcb_flush(restore_conn);
334 ELOG(
"Received ConfigureNotify for unknown window 0x%08x\n", event->window);
342 case XCB_CONFIGURE_NOTIFY:
346 DLOG(
"Received unhandled X11 event of type %d\n", type);
static TAILQ_HEAD(state_head, placeholder_state)
int predict_text_width(i3String *text)
Predict the text width in pixels for the given text.
static void open_placeholder_window(Con *con)
#define TAILQ_REMOVE(head, elm, field)
Stores a rectangle, for example the size of a window, the child window etc.
struct ev_loop * main_loop
#define TAILQ_FIRST(head)
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.
xcb_pixmap_t pixmap
The pixmap to render on (back buffer).
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...
static void restore_xcb_check_cb(EV_P_ ev_check *w, int revents)
struct _i3String i3String
Opaque data structure for storing strings.
xcb_window_t window
The X11 placeholder window.
bool con_is_leaf(Con *con)
Returns true when this node is a leaf node (has no children)
int height
The height of the font, built from font_ascent + font_descent.
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)
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)
A "match" is a data structure which acts like a mask or expression to match certain windows or not...
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...
A 'Con' represents everything from the X11 root window down to a single X11 window.
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)
#define TAILQ_HEAD_INITIALIZER(head)
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)
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
void match_init(Match *match)
struct Colortriple placeholder
Con * con
The container to which this placeholder window belongs.