swaybar: Move swaybar_teardown to free_state

This commit is contained in:
Mikkel Oscar Lyderik 2016-01-24 01:03:08 +01:00
parent fcc47cb3bd
commit 6140f9c42c
6 changed files with 87 additions and 49 deletions

View File

@ -152,8 +152,7 @@ static void ipc_parse_config(struct swaybar_config *config, const char *payload)
static void ipc_update_workspaces(struct swaybar_state *state) {
if (state->output->workspaces) {
list_foreach(state->output->workspaces, free_workspace);
list_free(state->output->workspaces);
free_workspaces(state->output->workspaces);
}
state->output->workspaces = create_list();

View File

@ -27,47 +27,13 @@
struct swaybar_state *state;
void swaybar_teardown() {
window_teardown(state->output->window);
if (state->output->registry) {
registry_teardown(state->output->registry);
}
if (state->status_read_fd) {
close(state->status_read_fd);
}
if (state->status_command_pid) {
// terminate status_command process
int ret = kill(state->status_command_pid, SIGTERM);
if (ret != 0) {
sway_log(L_ERROR, "Unable to terminate status_command [pid: %d]", state->status_command_pid);
} else {
int status;
waitpid(state->status_command_pid, &status, 0);
}
}
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);
}
}
void sway_terminate(void) {
swaybar_teardown();
free_state(state);
exit(EXIT_FAILURE);
}
void sig_handler(int signal) {
swaybar_teardown();
free_state(state);
exit(0);
}
@ -244,7 +210,7 @@ int main(int argc, char **argv) {
poll_for_update();
// gracefully shutdown swaybar and status_command
swaybar_teardown();
free_state(state);
return 0;
}

View File

@ -1,6 +1,10 @@
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#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);
}
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);
}
}
}
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);
}

View File

@ -3,6 +3,7 @@
#include "client/registry.h"
#include "client/window.h"
#include "list.h"
struct swaybar_state {
struct swaybar_config *config;
@ -37,8 +38,13 @@ struct workspace {
struct swaybar_state *init_state();
/**
* free workspace struct.
* free workspace list.
*/
void free_workspace(void *item);
void free_workspaces(list_t *workspaces);
/**
* Free state struct.
*/
void free_state(struct swaybar_state *state);
#endif /* _SWAYBAR_STATE_H */

View File

@ -433,3 +433,10 @@ struct status_line *init_status_line() {
return line;
}
void free_status_line(struct status_line *line) {
if (line->block_line) {
list_foreach(line->block_line, free_status_block);
list_free(line->block_line);
}
}

View File

@ -11,7 +11,7 @@ typedef enum {UNDEF, TEXT, I3BAR} command_protocol;
struct status_line {
list_t *block_line;
char *text_line;
const char *text_line;
command_protocol protocol;
};
@ -42,4 +42,9 @@ struct status_line *init_status_line();
*/
bool handle_status_line(struct swaybar_state *st);
/**
* Free status line struct.
*/
void free_status_line(struct status_line *line);
#endif /* _SWAYBAR_STATUS_LINE_H */