diff options
Diffstat (limited to 'swaybar/main.c')
-rw-r--r-- | swaybar/main.c | 118 |
1 files changed, 9 insertions, 109 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 | ||
28 | struct swaybar_state *state; | 10 | struct swaybar_state state; |
29 | 11 | ||
30 | void sway_terminate(void) { | 12 | void sway_terminate(void) { |
31 | free_state(state); | 13 | state_teardown(&state); |
32 | exit(EXIT_FAILURE); | 14 | exit(EXIT_FAILURE); |
33 | } | 15 | } |
34 | 16 | ||
35 | void sig_handler(int signal) { | 17 | void sig_handler(int signal) { |
36 | free_state(state); | 18 | state_teardown(&state); |
37 | exit(0); | 19 | exit(0); |
38 | } | 20 | } |
39 | 21 | ||
40 | void 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 | |||
79 | int main(int argc, char **argv) { | 22 | int 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 | } |