summaryrefslogtreecommitdiffstats
path: root/swaybar/state.c
diff options
context:
space:
mode:
Diffstat (limited to 'swaybar/state.c')
-rw-r--r--swaybar/state.c69
1 files changed, 62 insertions, 7 deletions
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 @@
1#include <stdlib.h> 1#include <stdlib.h>
2#include <unistd.h>
3#include <sys/types.h>
4#include <sys/wait.h>
2 5
3#include "list.h" 6#include "list.h"
7#include "log.h"
4#include "config.h" 8#include "config.h"
5#include "status_line.h" 9#include "status_line.h"
6#include "state.h" 10#include "state.h"
@@ -18,13 +22,64 @@ struct swaybar_state *init_state() {
18 return state; 22 return state;
19} 23}
20 24
21void free_workspace(void *item) { 25void free_workspaces(list_t *workspaces) {
22 if (!item) { 26 int i;
23 return; 27 for (i = 0; i < workspaces->length; ++i) {
24 } 28 struct workspace *ws = workspaces->items[i];
25 struct workspace *ws = (struct workspace *)item;
26 if (ws->name) {
27 free(ws->name); 29 free(ws->name);
30 free(ws);
31 }
32 list_free(workspaces);
33}
34
35static void free_output(struct output *output) {
36 window_teardown(output->window);
37 if (output->registry) {
38 registry_teardown(output->registry);
39 }
40
41 free(output->name);
42
43 if (output->workspaces) {
44 free_workspaces(output->workspaces);
45 }
46
47 free(output);
48}
49
50static void terminate_status_command(pid_t pid) {
51 if (pid) {
52 // terminate status_command process
53 int ret = kill(pid, SIGTERM);
54 if (ret != 0) {
55 sway_log(L_ERROR, "Unable to terminate status_command [pid: %d]", pid);
56 } else {
57 int status;
58 waitpid(pid, &status, 0);
59 }
28 } 60 }
29 free(ws); 61}
62
63void free_state(struct swaybar_state *state) {
64 free_config(state->config);
65 free_output(state->output);
66 free_status_line(state->status);
67
68 /* close sockets/pipes */
69 if (state->status_read_fd) {
70 close(state->status_read_fd);
71 }
72
73 if (state->ipc_socketfd) {
74 close(state->ipc_socketfd);
75 }
76
77 if (state->ipc_event_socketfd) {
78 close(state->ipc_event_socketfd);
79 }
80
81 /* terminate status command process */
82 terminate_status_command(state->status_command_pid);
83
84 free(state);
30} 85}