X-Git-Url: https://git.sur5r.net/?a=blobdiff_plain;f=src%2Frestore_layout.c;h=ba82f76f26275dc35357c68f5be9873ace39211c;hb=92000942039fa99d7334ca5099b467b0d3d17792;hp=b19f7f77cb725e9fe870a4f873aee29d3dde31bb;hpb=076636a8350683861f9555c3339004a0ba1777a8;p=i3%2Fi3 diff --git a/src/restore_layout.c b/src/restore_layout.c index b19f7f77..ba82f76f 100644 --- a/src/restore_layout.c +++ b/src/restore_layout.c @@ -35,6 +35,10 @@ static TAILQ_HEAD(state_head, placeholder_state) state_head = static xcb_connection_t *restore_conn; +static struct ev_io *xcb_watcher; +static struct ev_check *xcb_check; +static struct ev_prepare *xcb_prepare; + static void restore_handle_event(int type, xcb_generic_event_t *event); /* Documentation for these functions can be found in src/main.c, starting at xcb_got_event */ @@ -49,14 +53,14 @@ static void restore_xcb_check_cb(EV_P_ ev_check *w, int revents) { xcb_generic_event_t *event; if (xcb_connection_has_error(restore_conn)) { - // TODO: figure out how to reconnect with libev - ELOG("connection has an error\n"); + DLOG("restore X11 connection has an error, reconnecting\n"); + restore_connect(); return; } while ((event = xcb_poll_for_event(restore_conn)) != NULL) { if (event->response_type == 0) { - xcb_generic_error_t *error = (xcb_generic_error_t*)event; + xcb_generic_error_t *error = (xcb_generic_error_t *)event; DLOG("X11 Error received (probably harmless)! sequence 0x%x, error_code = %d\n", error->sequence, error->error_code); free(event); @@ -80,14 +84,34 @@ static void restore_xcb_check_cb(EV_P_ ev_check *w, int revents) { * */ void restore_connect(void) { + if (restore_conn != NULL) { + /* This is not the initial connect, but a reconnect, most likely + * because our X11 connection was killed (e.g. by a user with xkill. */ + ev_io_stop(main_loop, xcb_watcher); + ev_check_stop(main_loop, xcb_check); + ev_prepare_stop(main_loop, xcb_prepare); + + placeholder_state *state; + while (!TAILQ_EMPTY(&state_head)) { + state = TAILQ_FIRST(&state_head); + TAILQ_REMOVE(&state_head, state, state); + free(state); + } + + free(restore_conn); + free(xcb_watcher); + free(xcb_check); + free(xcb_prepare); + } + int screen; restore_conn = xcb_connect(NULL, &screen); if (restore_conn == NULL || xcb_connection_has_error(restore_conn)) errx(EXIT_FAILURE, "Cannot open display\n"); - struct ev_io *xcb_watcher = scalloc(sizeof(struct ev_io)); - struct ev_check *xcb_check = scalloc(sizeof(struct ev_check)); - struct ev_prepare *xcb_prepare = scalloc(sizeof(struct ev_prepare)); + xcb_watcher = scalloc(sizeof(struct ev_io)); + xcb_check = scalloc(sizeof(struct ev_check)); + xcb_prepare = scalloc(sizeof(struct ev_prepare)); ev_io_init(xcb_watcher, restore_xcb_got_event, xcb_get_file_descriptor(restore_conn), EV_READ); ev_io_start(main_loop, xcb_watcher); @@ -100,30 +124,28 @@ void restore_connect(void) { } static void update_placeholder_contents(placeholder_state *state) { - // TODO: introduce color configuration for placeholder windows. - xcb_change_gc(restore_conn, state->gc, XCB_GC_FOREGROUND, (uint32_t[]) { config.client.background }); + xcb_change_gc(restore_conn, state->gc, XCB_GC_FOREGROUND, + (uint32_t[]) {config.client.placeholder.background}); xcb_poly_fill_rectangle(restore_conn, state->pixmap, state->gc, 1, - (xcb_rectangle_t[]) { { 0, 0, state->rect.width, state->rect.height } }); + (xcb_rectangle_t[]) {{0, 0, state->rect.width, state->rect.height}}); // TODO: make i3font functions per-connection, at least these two for now…? xcb_flush(restore_conn); xcb_aux_sync(restore_conn); - set_font_colors(state->gc, config.client.focused.background, 0); + set_font_colors(state->gc, config.client.placeholder.text, config.client.placeholder.background); Match *swallows; int n = 0; - TAILQ_FOREACH(swallows, &(state->con->swallow_head), matches) { + TAILQ_FOREACH (swallows, &(state->con->swallow_head), matches) { char *serialized = NULL; -#define APPEND_REGEX(re_name) do { \ - if (swallows->re_name != NULL) { \ - sasprintf(&serialized, "%s%s" #re_name "=\"%s\"", \ - (serialized ? serialized : "["), \ - (serialized ? " " : ""), \ - swallows->re_name->pattern); \ - } \ -} while (0) +#define APPEND_REGEX(re_name) \ + do { \ + if (swallows->re_name != NULL) { \ + sasprintf(&serialized, "%s%s" #re_name "=\"%s\"", (serialized ? serialized : "["), (serialized ? " " : ""), swallows->re_name->pattern); \ + } \ + } while (0) APPEND_REGEX(class); APPEND_REGEX(instance); @@ -157,22 +179,26 @@ static void update_placeholder_contents(placeholder_state *state) { } static void open_placeholder_window(Con *con) { - if (con_is_leaf(con)) { + if (con_is_leaf(con) && + (con->window == NULL || con->window->id == XCB_NONE)) { xcb_window_t placeholder = create_window( - restore_conn, - con->rect, - XCB_COPY_FROM_PARENT, - XCB_COPY_FROM_PARENT, - XCB_WINDOW_CLASS_INPUT_OUTPUT, - XCURSOR_CURSOR_POINTER, - true, - XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK, - (uint32_t[]){ - // TODO: use the background color as background pixel to avoid flickering - root_screen->white_pixel, - XCB_EVENT_MASK_EXPOSURE | XCB_EVENT_MASK_STRUCTURE_NOTIFY, - }); - // TODO: set window title from con->name + restore_conn, + con->rect, + XCB_COPY_FROM_PARENT, + XCB_COPY_FROM_PARENT, + XCB_WINDOW_CLASS_INPUT_OUTPUT, + XCURSOR_CURSOR_POINTER, + true, + XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK, + (uint32_t[]) { + config.client.placeholder.background, + XCB_EVENT_MASK_EXPOSURE | XCB_EVENT_MASK_STRUCTURE_NOTIFY, + }); + /* Set the same name as was stored in the layout file. While perhaps + * slightly confusing in the first instant, this brings additional + * clarity to which placeholder is waiting for which actual window. */ + xcb_change_property(restore_conn, XCB_PROP_MODE_REPLACE, placeholder, + A__NET_WM_NAME, A_UTF8_STRING, 8, strlen(con->name), con->name); DLOG("Created placeholder window 0x%08x for leaf container %p / %s\n", placeholder, con, con->name); @@ -181,11 +207,10 @@ static void open_placeholder_window(Con *con) { state->con = con; state->rect = con->rect; state->pixmap = xcb_generate_id(restore_conn); - // TODO: get rid of hardcoded 24 - xcb_create_pixmap(restore_conn, 24, state->pixmap, + xcb_create_pixmap(restore_conn, root_depth, state->pixmap, state->window, state->rect.width, state->rect.height); state->gc = xcb_generate_id(restore_conn); - xcb_create_gc(restore_conn, state->gc, state->pixmap, XCB_GC_GRAPHICS_EXPOSURES, (uint32_t[]){ 0 }); + xcb_create_gc(restore_conn, state->gc, state->pixmap, XCB_GC_GRAPHICS_EXPOSURES, (uint32_t[]) {0}); update_placeholder_contents(state); TAILQ_INSERT_TAIL(&state_head, state, state); @@ -197,10 +222,10 @@ static void open_placeholder_window(Con *con) { } Con *child; - TAILQ_FOREACH(child, &(con->nodes_head), nodes) { + TAILQ_FOREACH (child, &(con->nodes_head), nodes) { open_placeholder_window(child); } - TAILQ_FOREACH(child, &(con->floating_head), floating_windows) { + TAILQ_FOREACH (child, &(con->floating_head), floating_windows) { open_placeholder_window(child); } } @@ -214,19 +239,45 @@ static void open_placeholder_window(Con *con) { */ void restore_open_placeholder_windows(Con *parent) { Con *child; - TAILQ_FOREACH(child, &(parent->nodes_head), nodes) { + TAILQ_FOREACH (child, &(parent->nodes_head), nodes) { open_placeholder_window(child); } - TAILQ_FOREACH(child, &(parent->floating_head), floating_windows) { + TAILQ_FOREACH (child, &(parent->floating_head), floating_windows) { open_placeholder_window(child); } xcb_flush(restore_conn); } +/* + * Kill the placeholder window, if placeholder refers to a placeholder window. + * This function is called when manage.c puts a window into an existing + * container. In order not to leak resources, we need to destroy the window and + * all associated X11 objects (pixmap/gc). + * + */ +bool restore_kill_placeholder(xcb_window_t placeholder) { + placeholder_state *state; + TAILQ_FOREACH (state, &state_head, state) { + if (state->window != placeholder) + continue; + + xcb_destroy_window(restore_conn, state->window); + xcb_free_pixmap(restore_conn, state->pixmap); + xcb_free_gc(restore_conn, state->gc); + TAILQ_REMOVE(&state_head, state, state); + free(state); + DLOG("placeholder window 0x%08x destroyed.\n", placeholder); + return true; + } + + DLOG("0x%08x is not a placeholder window, ignoring.\n", placeholder); + return false; +} + static void expose_event(xcb_expose_event_t *event) { placeholder_state *state; - TAILQ_FOREACH(state, &state_head, state) { + TAILQ_FOREACH (state, &state_head, state) { if (state->window != event->window) continue; @@ -254,7 +305,7 @@ static void expose_event(xcb_expose_event_t *event) { */ static void configure_notify(xcb_configure_notify_event_t *event) { placeholder_state *state; - TAILQ_FOREACH(state, &state_head, state) { + TAILQ_FOREACH (state, &state_head, state) { if (state->window != event->window) continue; @@ -268,11 +319,10 @@ static void configure_notify(xcb_configure_notify_event_t *event) { xcb_free_gc(restore_conn, state->gc); state->pixmap = xcb_generate_id(restore_conn); - // TODO: get rid of hardcoded 24 - xcb_create_pixmap(restore_conn, 24, state->pixmap, + xcb_create_pixmap(restore_conn, root_depth, state->pixmap, state->window, state->rect.width, state->rect.height); state->gc = xcb_generate_id(restore_conn); - xcb_create_gc(restore_conn, state->gc, state->pixmap, XCB_GC_GRAPHICS_EXPOSURES, (uint32_t[]){ 0 }); + xcb_create_gc(restore_conn, state->gc, state->pixmap, XCB_GC_GRAPHICS_EXPOSURES, (uint32_t[]) {0}); update_placeholder_contents(state); xcb_copy_area(restore_conn, state->pixmap, state->window, state->gc, @@ -284,14 +334,13 @@ static void configure_notify(xcb_configure_notify_event_t *event) { ELOG("Received ConfigureNotify for unknown window 0x%08x\n", event->window); } -// TODO: this event loop is not taken care of in the floating event handler static void restore_handle_event(int type, xcb_generic_event_t *event) { switch (type) { case XCB_EXPOSE: - expose_event((xcb_expose_event_t*)event); + expose_event((xcb_expose_event_t *)event); break; case XCB_CONFIGURE_NOTIFY: - configure_notify((xcb_configure_notify_event_t*)event); + configure_notify((xcb_configure_notify_event_t *)event); break; default: DLOG("Received unhandled X11 event of type %d\n", type);