diff --git a/include/ipc.h b/include/ipc.h index 25d2fc61..606c47ba 100644 --- a/include/ipc.h +++ b/include/ipc.h @@ -12,6 +12,7 @@ enum ipc_command_type { IPC_GET_VERSION = 7, }; -void init_ipc(void); +void ipc_init(void); +void ipc_shutdown(void); #endif diff --git a/sway/commands.c b/sway/commands.c index 803d9a21..38557b62 100644 --- a/sway/commands.c +++ b/sway/commands.c @@ -186,7 +186,7 @@ static bool cmd_exit(struct sway_config *config, int argc, char **argv) { } // Close all views container_map(&root_container, kill_views, NULL); - exit(0); + wlc_terminate(); return true; } diff --git a/sway/ipc.c b/sway/ipc.c index ba01a679..a6c4eb1a 100644 --- a/sway/ipc.c +++ b/sway/ipc.c @@ -16,6 +16,11 @@ #include "commands.h" static int ipc_socket = -1; +static struct wlc_event_source *ipc_event_source = NULL; +static struct sockaddr_un ipc_sockaddr = { + .sun_family = AF_UNIX, + .sun_path = "/tmp/sway-ipc.sock" +}; static const char ipc_magic[] = {'i', '3', '-', 'i', 'p', 'c'}; @@ -32,17 +37,12 @@ void ipc_client_disconnect(struct ipc_client *client); void ipc_client_handle_command(struct ipc_client *client); bool ipc_send_reply(struct ipc_client *client, const char *payload, uint32_t payload_length); -void init_ipc() { +void ipc_init(void) { ipc_socket = socket(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC, 0); if (ipc_socket == -1) { sway_abort("Unable to create IPC socket"); } - struct sockaddr_un ipc_sockaddr = { - .sun_family = AF_UNIX, - .sun_path = "/tmp/sway-ipc.sock" - }; - if (getenv("SWAYSOCK") != NULL) { strncpy(ipc_sockaddr.sun_path, getenv("SWAYSOCK"), sizeof(ipc_sockaddr.sun_path)); } @@ -56,10 +56,19 @@ void init_ipc() { sway_abort("Unable to listen on IPC socket"); } - wlc_event_loop_add_fd(ipc_socket, WLC_EVENT_READABLE, ipc_handle_connection, NULL); + ipc_event_source = wlc_event_loop_add_fd(ipc_socket, WLC_EVENT_READABLE, ipc_handle_connection, NULL); +} + +void ipc_shutdown(void) { + if (ipc_event_source) { + wlc_event_source_remove(ipc_event_source); + } + close(ipc_socket); + unlink(ipc_sockaddr.sun_path); } int ipc_handle_connection(int fd, uint32_t mask, void *data) { + (void) fd; (void) data; sway_log(L_DEBUG, "Event on IPC listening socket"); assert(mask == WLC_EVENT_READABLE); diff --git a/sway/main.c b/sway/main.c index 1af1278d..a42fbcb7 100644 --- a/sway/main.c +++ b/sway/main.c @@ -100,13 +100,15 @@ int main(int argc, char **argv) { free(config_path); } - init_ipc(); + ipc_init(); wlc_run(); if (devnull) { fclose(devnull); } + ipc_shutdown(); + return 0; }