aboutsummaryrefslogtreecommitdiffstats
path: root/swaybar/state.c
diff options
context:
space:
mode:
Diffstat (limited to 'swaybar/state.c')
-rw-r--r--swaybar/state.c185
1 files changed, 0 insertions, 185 deletions
diff --git a/swaybar/state.c b/swaybar/state.c
deleted file mode 100644
index 535efbc4..00000000
--- a/swaybar/state.c
+++ /dev/null
@@ -1,185 +0,0 @@
1#include <stdlib.h>
2#include <unistd.h>
3#include <fcntl.h>
4#include <errno.h>
5#include <sys/types.h>
6#include <sys/wait.h>
7
8#include "ipc-client.h"
9#include "list.h"
10#include "log.h"
11#include "ipc.h"
12#include "render.h"
13#include "config.h"
14#include "status_line.h"
15#include "state.h"
16
17static void state_init(struct swaybar_state *state) {
18 state->config = init_config();
19 state->status = init_status_line();
20 state->output = malloc(sizeof(struct output));
21 state->output->window = NULL;
22 state->output->registry = NULL;
23 state->output->workspaces = create_list();
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 }
45
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 }
125}
126
127void free_workspaces(list_t *workspaces) {
128 int i;
129 for (i = 0; i < workspaces->length; ++i) {
130 struct workspace *ws = workspaces->items[i];
131 free(ws->name);
132 free(ws);
133 }
134 list_free(workspaces);
135}
136
137static void free_output(struct output *output) {
138 window_teardown(output->window);
139 if (output->registry) {
140 registry_teardown(output->registry);
141 }
142
143 free(output->name);
144
145 if (output->workspaces) {
146 free_workspaces(output->workspaces);
147 }
148
149 free(output);
150}
151
152static void terminate_status_command(pid_t pid) {
153 if (pid) {
154 // terminate status_command process
155 int ret = kill(pid, SIGTERM);
156 if (ret != 0) {
157 sway_log(L_ERROR, "Unable to terminate status_command [pid: %d]", pid);
158 } else {
159 int status;
160 waitpid(pid, &status, 0);
161 }
162 }
163}
164
165void state_teardown(struct swaybar_state *state) {
166 free_config(state->config);
167 free_output(state->output);
168 free_status_line(state->status);
169
170 /* close sockets/pipes */
171 if (state->status_read_fd) {
172 close(state->status_read_fd);
173 }
174
175 if (state->ipc_socketfd) {
176 close(state->ipc_socketfd);
177 }
178
179 if (state->ipc_event_socketfd) {
180 close(state->ipc_event_socketfd);
181 }
182
183 /* terminate status command process */
184 terminate_status_command(state->status_command_pid);
185}