summaryrefslogtreecommitdiffstats
path: root/swaybar
diff options
context:
space:
mode:
authorLibravatar Mikkel Oscar Lyderik <mikkeloscar@gmail.com>2016-01-24 02:19:08 +0100
committerLibravatar Mikkel Oscar Lyderik <mikkeloscar@gmail.com>2016-01-24 14:22:19 +0100
commitc6fc0033e1bf8aa1deb2c44284049041766361f8 (patch)
tree9512609ef9f48b42bf48fb82e7bdddaf2e647ae4 /swaybar
parentswaybar: Move swaybar_teardown to free_state (diff)
downloadsway-c6fc0033e1bf8aa1deb2c44284049041766361f8.tar.gz
sway-c6fc0033e1bf8aa1deb2c44284049041766361f8.tar.zst
sway-c6fc0033e1bf8aa1deb2c44284049041766361f8.zip
swaybar: move core functionality to state.c
Diffstat (limited to 'swaybar')
-rw-r--r--swaybar/main.c118
-rw-r--r--swaybar/state.c112
-rw-r--r--swaybar/state.h13
3 files changed, 124 insertions, 119 deletions
diff --git a/swaybar/main.c b/swaybar/main.c
index 976fcea0..51846bca 100644
--- a/swaybar/main.c
+++ b/swaybar/main.c
@@ -1,86 +1,28 @@
1#include <fcntl.h>
2#include <stdio.h> 1#include <stdio.h>
3#include <stdlib.h> 2#include <stdlib.h>
4#include <string.h> 3#include <string.h>
5#include <stdint.h>
6#include <stdbool.h> 4#include <stdbool.h>
7#include <unistd.h>
8#include <sys/select.h>
9#include <sys/wait.h>
10#include <errno.h>
11#include <json-c/json.h>
12#include <sys/un.h>
13#include <sys/socket.h>
14#include <sys/ioctl.h>
15#include <getopt.h> 5#include <getopt.h>
16#include "ipc-client.h" 6#include "ipc-client.h"
17#include "client/registry.h"
18#include "client/window.h"
19#include "client/pango.h"
20#include "stringop.h"
21#include "log.h" 7#include "log.h"
22#include "state.h" 8#include "state.h"
23#include "config.h"
24#include "render.h"
25#include "status_line.h"
26#include "ipc.h"
27 9
28struct swaybar_state *state; 10struct swaybar_state state;
29 11
30void sway_terminate(void) { 12void sway_terminate(void) {
31 free_state(state); 13 state_teardown(&state);
32 exit(EXIT_FAILURE); 14 exit(EXIT_FAILURE);
33} 15}
34 16
35void sig_handler(int signal) { 17void sig_handler(int signal) {
36 free_state(state); 18 state_teardown(&state);
37 exit(0); 19 exit(0);
38} 20}
39 21
40void poll_for_update() {
41 fd_set readfds;
42 int activity;
43 bool dirty = true;
44
45 while (1) {
46 if (dirty) {
47 struct output *output = state->output;
48 if (window_prerender(output->window) && output->window->cairo) {
49 render(output, state->config, state->status);
50 window_render(output->window);
51 if (wl_display_dispatch(output->registry->display) == -1) {
52 break;
53 }
54 }
55 }
56
57 dirty = false;
58 FD_ZERO(&readfds);
59 FD_SET(state->ipc_event_socketfd, &readfds);
60 FD_SET(state->status_read_fd, &readfds);
61
62 activity = select(FD_SETSIZE, &readfds, NULL, NULL, NULL);
63 if (activity < 0) {
64 sway_log(L_ERROR, "polling failed: %d", errno);
65 }
66
67 if (FD_ISSET(state->ipc_event_socketfd, &readfds)) {
68 sway_log(L_DEBUG, "Got IPC event.");
69 dirty = handle_ipc_event(state);
70 }
71
72 if (state->config->status_command && FD_ISSET(state->status_read_fd, &readfds)) {
73 sway_log(L_DEBUG, "Got update from status command.");
74 dirty = handle_status_line(state);
75 }
76 }
77}
78
79int main(int argc, char **argv) { 22int main(int argc, char **argv) {
80 char *socket_path = NULL; 23 char *socket_path = NULL;
81 char *bar_id = NULL; 24 char *bar_id = NULL;
82 bool debug = false; 25 bool debug = false;
83 state = init_state();
84 26
85 static struct option long_options[] = { 27 static struct option long_options[] = {
86 {"help", no_argument, NULL, 'h'}, 28 {"help", no_argument, NULL, 'h'},
@@ -145,72 +87,30 @@ int main(int argc, char **argv) {
145 init_log(L_ERROR); 87 init_log(L_ERROR);
146 } 88 }
147 89
148 state->output->registry = registry_poll();
149
150 if (!state->output->registry->desktop_shell) {
151 sway_abort("swaybar requires the compositor to support the desktop-shell extension.");
152 }
153
154 if (!socket_path) { 90 if (!socket_path) {
155 socket_path = get_socketpath(); 91 socket_path = get_socketpath();
156 if (!socket_path) { 92 if (!socket_path) {
157 sway_abort("Unable to retrieve socket path"); 93 sway_abort("Unable to retrieve socket path");
158 } 94 }
159 } 95 }
160 state->ipc_socketfd = ipc_open_socket(socket_path);
161 state->ipc_event_socketfd = ipc_open_socket(socket_path);
162 96
163 if (argc == optind) { 97 if (argc == optind) {
164 sway_abort("No output index provided"); 98 sway_abort("No output index provided");
165 } 99 }
166 100
167 int desired_output = atoi(argv[optind]); 101 int desired_output = atoi(argv[optind]);
168 ipc_bar_init(state, desired_output, bar_id);
169
170 struct output_state *output = state->output->registry->outputs->items[desired_output];
171 102
172 state->output->window = window_setup(state->output->registry, output->width, 30, false); 103 signal(SIGTERM, sig_handler);
173 if (!state->output->window) {
174 sway_abort("Failed to create window.");
175 }
176 desktop_shell_set_panel(state->output->registry->desktop_shell, output->output, state->output->window->surface);
177 desktop_shell_set_panel_position(state->output->registry->desktop_shell, state->config->position);
178
179 /* set font */
180 state->output->window->font = state->config->font;
181
182 /* set window height */
183 set_window_height(state->output->window, state->config->height);
184
185 if (state->config->status_command) {
186 int pipefd[2];
187 pipe(pipefd);
188 state->status_command_pid = fork();
189 if (state->status_command_pid == 0) {
190 close(pipefd[0]);
191 dup2(pipefd[1], STDOUT_FILENO);
192 close(pipefd[1]);
193 char *const cmd[] = {
194 "sh",
195 "-c",
196 state->config->status_command,
197 NULL,
198 };
199 execvp(cmd[0], cmd);
200 return 0;
201 }
202 104
203 close(pipefd[1]); 105 state_setup(&state, socket_path, bar_id, desired_output);
204 state->status_read_fd = pipefd[0];
205 fcntl(state->status_read_fd, F_SETFL, O_NONBLOCK);
206 }
207 106
208 signal(SIGTERM, sig_handler); 107 free(socket_path);
108 free(bar_id);
209 109
210 poll_for_update(); 110 state_run(&state);
211 111
212 // gracefully shutdown swaybar and status_command 112 // gracefully shutdown swaybar and status_command
213 free_state(state); 113 state_teardown(&state);
214 114
215 return 0; 115 return 0;
216} 116}
diff --git a/swaybar/state.c b/swaybar/state.c
index 26cdcafe..535efbc4 100644
--- a/swaybar/state.c
+++ b/swaybar/state.c
@@ -1,16 +1,20 @@
1#include <stdlib.h> 1#include <stdlib.h>
2#include <unistd.h> 2#include <unistd.h>
3#include <fcntl.h>
4#include <errno.h>
3#include <sys/types.h> 5#include <sys/types.h>
4#include <sys/wait.h> 6#include <sys/wait.h>
5 7
8#include "ipc-client.h"
6#include "list.h" 9#include "list.h"
7#include "log.h" 10#include "log.h"
11#include "ipc.h"
12#include "render.h"
8#include "config.h" 13#include "config.h"
9#include "status_line.h" 14#include "status_line.h"
10#include "state.h" 15#include "state.h"
11 16
12struct swaybar_state *init_state() { 17static void state_init(struct swaybar_state *state) {
13 struct swaybar_state *state = calloc(1, sizeof(struct swaybar_state));
14 state->config = init_config(); 18 state->config = init_config();
15 state->status = init_status_line(); 19 state->status = init_status_line();
16 state->output = malloc(sizeof(struct output)); 20 state->output = malloc(sizeof(struct output));
@@ -18,8 +22,106 @@ struct swaybar_state *init_state() {
18 state->output->registry = NULL; 22 state->output->registry = NULL;
19 state->output->workspaces = create_list(); 23 state->output->workspaces = create_list();
20 state->output->name = NULL; 24 state->output->name = NULL;
25}
26
27static void spawn_status_cmd_proc(struct swaybar_state *state) {
28 if (state->config->status_command) {
29 int pipefd[2];
30 pipe(pipefd);
31 state->status_command_pid = fork();
32 if (state->status_command_pid == 0) {
33 close(pipefd[0]);
34 dup2(pipefd[1], STDOUT_FILENO);
35 close(pipefd[1]);
36 char *const cmd[] = {
37 "sh",
38 "-c",
39 state->config->status_command,
40 NULL,
41 };
42 execvp(cmd[0], cmd);
43 return;
44 }
21 45
22 return state; 46 close(pipefd[1]);
47 state->status_read_fd = pipefd[0];
48 fcntl(state->status_read_fd, F_SETFL, O_NONBLOCK);
49 }
50}
51
52
53void state_setup(struct swaybar_state *state, const char *socket_path, const char *bar_id, int desired_output) {
54 /* initialize state with default values */
55 state_init(state);
56
57 state->output->registry = registry_poll();
58
59 if (!state->output->registry->desktop_shell) {
60 sway_abort("swaybar requires the compositor to support the desktop-shell extension.");
61 }
62
63 /* connect to sway ipc */
64 state->ipc_socketfd = ipc_open_socket(socket_path);
65 state->ipc_event_socketfd = ipc_open_socket(socket_path);
66
67 ipc_bar_init(state, desired_output, bar_id);
68
69 struct output_state *output = state->output->registry->outputs->items[desired_output];
70
71 state->output->window = window_setup(state->output->registry, output->width, 30, false);
72 if (!state->output->window) {
73 sway_abort("Failed to create window.");
74 }
75 desktop_shell_set_panel(state->output->registry->desktop_shell, output->output, state->output->window->surface);
76 desktop_shell_set_panel_position(state->output->registry->desktop_shell, state->config->position);
77
78 /* set font */
79 state->output->window->font = state->config->font;
80
81 /* set window height */
82 set_window_height(state->output->window, state->config->height);
83
84 /* spawn status command */
85 spawn_status_cmd_proc(state);
86}
87
88void state_run(struct swaybar_state *state) {
89 fd_set readfds;
90 int activity;
91 bool dirty = true;
92
93 while (1) {
94 if (dirty) {
95 struct output *output = state->output;
96 if (window_prerender(output->window) && output->window->cairo) {
97 render(output, state->config, state->status);
98 window_render(output->window);
99 if (wl_display_dispatch(output->registry->display) == -1) {
100 break;
101 }
102 }
103 }
104
105 dirty = false;
106 FD_ZERO(&readfds);
107 FD_SET(state->ipc_event_socketfd, &readfds);
108 FD_SET(state->status_read_fd, &readfds);
109
110 activity = select(FD_SETSIZE, &readfds, NULL, NULL, NULL);
111 if (activity < 0) {
112 sway_log(L_ERROR, "polling failed: %d", errno);
113 }
114
115 if (FD_ISSET(state->ipc_event_socketfd, &readfds)) {
116 sway_log(L_DEBUG, "Got IPC event.");
117 dirty = handle_ipc_event(state);
118 }
119
120 if (state->config->status_command && FD_ISSET(state->status_read_fd, &readfds)) {
121 sway_log(L_DEBUG, "Got update from status command.");
122 dirty = handle_status_line(state);
123 }
124 }
23} 125}
24 126
25void free_workspaces(list_t *workspaces) { 127void free_workspaces(list_t *workspaces) {
@@ -60,7 +162,7 @@ static void terminate_status_command(pid_t pid) {
60 } 162 }
61} 163}
62 164
63void free_state(struct swaybar_state *state) { 165void state_teardown(struct swaybar_state *state) {
64 free_config(state->config); 166 free_config(state->config);
65 free_output(state->output); 167 free_output(state->output);
66 free_status_line(state->status); 168 free_status_line(state->status);
@@ -80,6 +182,4 @@ void free_state(struct swaybar_state *state) {
80 182
81 /* terminate status command process */ 183 /* terminate status command process */
82 terminate_status_command(state->status_command_pid); 184 terminate_status_command(state->status_command_pid);
83
84 free(state);
85} 185}
diff --git a/swaybar/state.h b/swaybar/state.h
index e09807d0..985002f8 100644
--- a/swaybar/state.h
+++ b/swaybar/state.h
@@ -33,9 +33,14 @@ struct workspace {
33}; 33};
34 34
35/** 35/**
36 * Initialize state. 36 * Setup state.
37 */ 37 */
38struct swaybar_state *init_state(); 38void state_setup(struct swaybar_state *state, const char *socket_path, const char *bar_id, int desired_output);
39
40/**
41 * State mainloop.
42 */
43void state_run(struct swaybar_state *state);
39 44
40/** 45/**
41 * free workspace list. 46 * free workspace list.
@@ -43,8 +48,8 @@ struct swaybar_state *init_state();
43void free_workspaces(list_t *workspaces); 48void free_workspaces(list_t *workspaces);
44 49
45/** 50/**
46 * Free state struct. 51 * Teardown state.
47 */ 52 */
48void free_state(struct swaybar_state *state); 53void state_teardown(struct swaybar_state *state);
49 54
50#endif /* _SWAYBAR_STATE_H */ 55#endif /* _SWAYBAR_STATE_H */