From 6140f9c42c4f09142d647c96236cc030689e6f34 Mon Sep 17 00:00:00 2001 From: Mikkel Oscar Lyderik Date: Sun, 24 Jan 2016 01:03:08 +0100 Subject: swaybar: Move swaybar_teardown to free_state --- swaybar/state.c | 69 +++++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 62 insertions(+), 7 deletions(-) (limited to 'swaybar/state.c') diff --git a/swaybar/state.c b/swaybar/state.c index 77427555..26cdcafe 100644 --- a/swaybar/state.c +++ b/swaybar/state.c @@ -1,6 +1,10 @@ #include +#include +#include +#include #include "list.h" +#include "log.h" #include "config.h" #include "status_line.h" #include "state.h" @@ -18,13 +22,64 @@ struct swaybar_state *init_state() { return state; } -void free_workspace(void *item) { - if (!item) { - return; - } - struct workspace *ws = (struct workspace *)item; - if (ws->name) { +void free_workspaces(list_t *workspaces) { + int i; + for (i = 0; i < workspaces->length; ++i) { + struct workspace *ws = workspaces->items[i]; free(ws->name); + free(ws); + } + list_free(workspaces); +} + +static void free_output(struct output *output) { + window_teardown(output->window); + if (output->registry) { + registry_teardown(output->registry); + } + + free(output->name); + + if (output->workspaces) { + free_workspaces(output->workspaces); + } + + free(output); +} + +static void terminate_status_command(pid_t pid) { + if (pid) { + // terminate status_command process + int ret = kill(pid, SIGTERM); + if (ret != 0) { + sway_log(L_ERROR, "Unable to terminate status_command [pid: %d]", pid); + } else { + int status; + waitpid(pid, &status, 0); + } } - free(ws); +} + +void free_state(struct swaybar_state *state) { + free_config(state->config); + free_output(state->output); + free_status_line(state->status); + + /* close sockets/pipes */ + if (state->status_read_fd) { + close(state->status_read_fd); + } + + if (state->ipc_socketfd) { + close(state->ipc_socketfd); + } + + if (state->ipc_event_socketfd) { + close(state->ipc_event_socketfd); + } + + /* terminate status command process */ + terminate_status_command(state->status_command_pid); + + free(state); } -- cgit v1.2.3