diff options
author | Drew DeVault <sir@cmpwn.com> | 2016-01-24 09:46:28 -0500 |
---|---|---|
committer | Drew DeVault <sir@cmpwn.com> | 2016-01-24 09:46:28 -0500 |
commit | 4b48a8399a33f3bb5f5d55f87213bb4692a5f1f9 (patch) | |
tree | 1d820cd07f6ba693a89d43caf01b3c8b01390609 /swaybar/bar.c | |
parent | numlen(0) == 1 (diff) | |
parent | swaybar: cleanup CmakeLists.txt (diff) | |
download | sway-4b48a8399a33f3bb5f5d55f87213bb4692a5f1f9.tar.gz sway-4b48a8399a33f3bb5f5d55f87213bb4692a5f1f9.tar.zst sway-4b48a8399a33f3bb5f5d55f87213bb4692a5f1f9.zip |
Merge pull request #467 from mikkeloscar/swaybar-refactor
[WIP] Swaybar refactor
Diffstat (limited to 'swaybar/bar.c')
-rw-r--r-- | swaybar/bar.c | 185 |
1 files changed, 185 insertions, 0 deletions
diff --git a/swaybar/bar.c b/swaybar/bar.c new file mode 100644 index 00000000..fc01863b --- /dev/null +++ b/swaybar/bar.c | |||
@@ -0,0 +1,185 @@ | |||
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 "bar.h" | ||
16 | |||
17 | static void bar_init(struct bar *bar) { | ||
18 | bar->config = init_config(); | ||
19 | bar->status = init_status_line(); | ||
20 | bar->output = malloc(sizeof(struct output)); | ||
21 | bar->output->window = NULL; | ||
22 | bar->output->registry = NULL; | ||
23 | bar->output->workspaces = create_list(); | ||
24 | bar->output->name = NULL; | ||
25 | } | ||
26 | |||
27 | static void spawn_status_cmd_proc(struct bar *bar) { | ||
28 | if (bar->config->status_command) { | ||
29 | int pipefd[2]; | ||
30 | pipe(pipefd); | ||
31 | bar->status_command_pid = fork(); | ||
32 | if (bar->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 | bar->config->status_command, | ||
40 | NULL, | ||
41 | }; | ||
42 | execvp(cmd[0], cmd); | ||
43 | return; | ||
44 | } | ||
45 | |||
46 | close(pipefd[1]); | ||
47 | bar->status_read_fd = pipefd[0]; | ||
48 | fcntl(bar->status_read_fd, F_SETFL, O_NONBLOCK); | ||
49 | } | ||
50 | } | ||
51 | |||
52 | |||
53 | void bar_setup(struct bar *bar, const char *socket_path, const char *bar_id, int desired_output) { | ||
54 | /* initialize bar with default values */ | ||
55 | bar_init(bar); | ||
56 | |||
57 | bar->output->registry = registry_poll(); | ||
58 | |||
59 | if (!bar->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 | bar->ipc_socketfd = ipc_open_socket(socket_path); | ||
65 | bar->ipc_event_socketfd = ipc_open_socket(socket_path); | ||
66 | |||
67 | ipc_bar_init(bar, desired_output, bar_id); | ||
68 | |||
69 | struct output_state *output = bar->output->registry->outputs->items[desired_output]; | ||
70 | |||
71 | bar->output->window = window_setup(bar->output->registry, output->width, 30, false); | ||
72 | if (!bar->output->window) { | ||
73 | sway_abort("Failed to create window."); | ||
74 | } | ||
75 | desktop_shell_set_panel(bar->output->registry->desktop_shell, output->output, bar->output->window->surface); | ||
76 | desktop_shell_set_panel_position(bar->output->registry->desktop_shell, bar->config->position); | ||
77 | |||
78 | /* set font */ | ||
79 | bar->output->window->font = bar->config->font; | ||
80 | |||
81 | /* set window height */ | ||
82 | set_window_height(bar->output->window, bar->config->height); | ||
83 | |||
84 | /* spawn status command */ | ||
85 | spawn_status_cmd_proc(bar); | ||
86 | } | ||
87 | |||
88 | void bar_run(struct bar *bar) { | ||
89 | fd_set readfds; | ||
90 | int activity; | ||
91 | bool dirty = true; | ||
92 | |||
93 | while (1) { | ||
94 | if (dirty) { | ||
95 | struct output *output = bar->output; | ||
96 | if (window_prerender(output->window) && output->window->cairo) { | ||
97 | render(output, bar->config, bar->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(bar->ipc_event_socketfd, &readfds); | ||
108 | FD_SET(bar->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(bar->ipc_event_socketfd, &readfds)) { | ||
116 | sway_log(L_DEBUG, "Got IPC event."); | ||
117 | dirty = handle_ipc_event(bar); | ||
118 | } | ||
119 | |||
120 | if (bar->config->status_command && FD_ISSET(bar->status_read_fd, &readfds)) { | ||
121 | sway_log(L_DEBUG, "Got update from status command."); | ||
122 | dirty = handle_status_line(bar); | ||
123 | } | ||
124 | } | ||
125 | } | ||
126 | |||
127 | void 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 | |||
137 | static 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 | |||
152 | static 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 | |||
165 | void bar_teardown(struct bar *bar) { | ||
166 | free_config(bar->config); | ||
167 | free_output(bar->output); | ||
168 | free_status_line(bar->status); | ||
169 | |||
170 | /* close sockets/pipes */ | ||
171 | if (bar->status_read_fd) { | ||
172 | close(bar->status_read_fd); | ||
173 | } | ||
174 | |||
175 | if (bar->ipc_socketfd) { | ||
176 | close(bar->ipc_socketfd); | ||
177 | } | ||
178 | |||
179 | if (bar->ipc_event_socketfd) { | ||
180 | close(bar->ipc_event_socketfd); | ||
181 | } | ||
182 | |||
183 | /* terminate status command process */ | ||
184 | terminate_status_command(bar->status_command_pid); | ||
185 | } | ||