summaryrefslogtreecommitdiffstats
path: root/swaybar/state.c
diff options
context:
space:
mode:
Diffstat (limited to 'swaybar/state.c')
-rw-r--r--swaybar/state.c112
1 files changed, 106 insertions, 6 deletions
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}