summaryrefslogtreecommitdiffstats
path: root/swaybar
diff options
context:
space:
mode:
authorLibravatar Mikkel Oscar Lyderik <mikkeloscar@gmail.com>2016-01-24 01:03:08 +0100
committerLibravatar Mikkel Oscar Lyderik <mikkeloscar@gmail.com>2016-01-24 14:22:19 +0100
commit6140f9c42c4f09142d647c96236cc030689e6f34 (patch)
tree2958848d61b10e20dfa9d4f5cd9188d384be0029 /swaybar
parentswaybar: move ipc stuff to ipc.{h,c} (diff)
downloadsway-6140f9c42c4f09142d647c96236cc030689e6f34.tar.gz
sway-6140f9c42c4f09142d647c96236cc030689e6f34.tar.zst
sway-6140f9c42c4f09142d647c96236cc030689e6f34.zip
swaybar: Move swaybar_teardown to free_state
Diffstat (limited to 'swaybar')
-rw-r--r--swaybar/ipc.c3
-rw-r--r--swaybar/main.c40
-rw-r--r--swaybar/state.c69
-rw-r--r--swaybar/state.h10
-rw-r--r--swaybar/status_line.c7
-rw-r--r--swaybar/status_line.h7
6 files changed, 87 insertions, 49 deletions
diff --git a/swaybar/ipc.c b/swaybar/ipc.c
index cb0b81aa..884d02ff 100644
--- a/swaybar/ipc.c
+++ b/swaybar/ipc.c
@@ -152,8 +152,7 @@ static void ipc_parse_config(struct swaybar_config *config, const char *payload)
152 152
153static void ipc_update_workspaces(struct swaybar_state *state) { 153static void ipc_update_workspaces(struct swaybar_state *state) {
154 if (state->output->workspaces) { 154 if (state->output->workspaces) {
155 list_foreach(state->output->workspaces, free_workspace); 155 free_workspaces(state->output->workspaces);
156 list_free(state->output->workspaces);
157 } 156 }
158 state->output->workspaces = create_list(); 157 state->output->workspaces = create_list();
159 158
diff --git a/swaybar/main.c b/swaybar/main.c
index a521fa79..976fcea0 100644
--- a/swaybar/main.c
+++ b/swaybar/main.c
@@ -27,47 +27,13 @@
27 27
28struct swaybar_state *state; 28struct swaybar_state *state;
29 29
30void swaybar_teardown() {
31 window_teardown(state->output->window);
32 if (state->output->registry) {
33 registry_teardown(state->output->registry);
34 }
35
36 if (state->status_read_fd) {
37 close(state->status_read_fd);
38 }
39
40 if (state->status_command_pid) {
41 // terminate status_command process
42 int ret = kill(state->status_command_pid, SIGTERM);
43 if (ret != 0) {
44 sway_log(L_ERROR, "Unable to terminate status_command [pid: %d]", state->status_command_pid);
45 } else {
46 int status;
47 waitpid(state->status_command_pid, &status, 0);
48 }
49 }
50
51 if (state->status_read_fd) {
52 close(state->status_read_fd);
53 }
54
55 if (state->ipc_socketfd) {
56 close(state->ipc_socketfd);
57 }
58
59 if (state->ipc_event_socketfd) {
60 close(state->ipc_event_socketfd);
61 }
62}
63
64void sway_terminate(void) { 30void sway_terminate(void) {
65 swaybar_teardown(); 31 free_state(state);
66 exit(EXIT_FAILURE); 32 exit(EXIT_FAILURE);
67} 33}
68 34
69void sig_handler(int signal) { 35void sig_handler(int signal) {
70 swaybar_teardown(); 36 free_state(state);
71 exit(0); 37 exit(0);
72} 38}
73 39
@@ -244,7 +210,7 @@ int main(int argc, char **argv) {
244 poll_for_update(); 210 poll_for_update();
245 211
246 // gracefully shutdown swaybar and status_command 212 // gracefully shutdown swaybar and status_command
247 swaybar_teardown(); 213 free_state(state);
248 214
249 return 0; 215 return 0;
250} 216}
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}
diff --git a/swaybar/state.h b/swaybar/state.h
index f95e03bc..e09807d0 100644
--- a/swaybar/state.h
+++ b/swaybar/state.h
@@ -3,6 +3,7 @@
3 3
4#include "client/registry.h" 4#include "client/registry.h"
5#include "client/window.h" 5#include "client/window.h"
6#include "list.h"
6 7
7struct swaybar_state { 8struct swaybar_state {
8 struct swaybar_config *config; 9 struct swaybar_config *config;
@@ -37,8 +38,13 @@ struct workspace {
37struct swaybar_state *init_state(); 38struct swaybar_state *init_state();
38 39
39/** 40/**
40 * free workspace struct. 41 * free workspace list.
41 */ 42 */
42void free_workspace(void *item); 43void free_workspaces(list_t *workspaces);
44
45/**
46 * Free state struct.
47 */
48void free_state(struct swaybar_state *state);
43 49
44#endif /* _SWAYBAR_STATE_H */ 50#endif /* _SWAYBAR_STATE_H */
diff --git a/swaybar/status_line.c b/swaybar/status_line.c
index ee740c6b..a072673b 100644
--- a/swaybar/status_line.c
+++ b/swaybar/status_line.c
@@ -433,3 +433,10 @@ struct status_line *init_status_line() {
433 433
434 return line; 434 return line;
435} 435}
436
437void free_status_line(struct status_line *line) {
438 if (line->block_line) {
439 list_foreach(line->block_line, free_status_block);
440 list_free(line->block_line);
441 }
442}
diff --git a/swaybar/status_line.h b/swaybar/status_line.h
index 36020aeb..1d73dd57 100644
--- a/swaybar/status_line.h
+++ b/swaybar/status_line.h
@@ -11,7 +11,7 @@ typedef enum {UNDEF, TEXT, I3BAR} command_protocol;
11 11
12struct status_line { 12struct status_line {
13 list_t *block_line; 13 list_t *block_line;
14 char *text_line; 14 const char *text_line;
15 command_protocol protocol; 15 command_protocol protocol;
16}; 16};
17 17
@@ -42,4 +42,9 @@ struct status_line *init_status_line();
42 */ 42 */
43bool handle_status_line(struct swaybar_state *st); 43bool handle_status_line(struct swaybar_state *st);
44 44
45/**
46 * Free status line struct.
47 */
48void free_status_line(struct status_line *line);
49
45#endif /* _SWAYBAR_STATUS_LINE_H */ 50#endif /* _SWAYBAR_STATUS_LINE_H */