X-Git-Url: https://git.sur5r.net/?a=blobdiff_plain;f=src%2Frestore_layout.c;h=cdf15517e10a68e65b97680caa34f31b6796ddb2;hb=cd0cd0c3d22d7acba240ca10d8594621cc67ee34;hp=f868cf3e5839adebb94cf6ab3313040b65a3c2b9;hpb=35b70ca423790d071d503ce76d8074e9b30e2dc5;p=i3%2Fi3 diff --git a/src/restore_layout.c b/src/restore_layout.c index f868cf3e..cdf15517 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,8 +53,8 @@ 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; } @@ -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); @@ -226,6 +250,32 @@ void restore_open_placeholder_windows(Con *parent) { 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) { @@ -285,7 +335,6 @@ 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: