diff options
author | 2018-04-12 20:19:54 -0400 | |
---|---|---|
committer | 2018-04-12 20:19:54 -0400 | |
commit | cd1b32453a9296c18b28bff71607aeb22987b5cd (patch) | |
tree | c653c6d525b471914c01a9d7ae543f521b6138ed /swaybar/bar.c | |
parent | Merge pull request #1634 from aleksander/master (diff) | |
parent | Fix separator height calculation (diff) | |
download | sway-cd1b32453a9296c18b28bff71607aeb22987b5cd.tar.gz sway-cd1b32453a9296c18b28bff71607aeb22987b5cd.tar.zst sway-cd1b32453a9296c18b28bff71607aeb22987b5cd.zip |
Merge branch 'wlroots'
Diffstat (limited to 'swaybar/bar.c')
-rw-r--r-- | swaybar/bar.c | 644 |
1 files changed, 355 insertions, 289 deletions
diff --git a/swaybar/bar.c b/swaybar/bar.c index f12923a8..d51c4ec7 100644 --- a/swaybar/bar.c +++ b/swaybar/bar.c | |||
@@ -1,390 +1,456 @@ | |||
1 | #define _XOPEN_SOURCE 500 | 1 | #define _XOPEN_SOURCE 500 |
2 | #include <assert.h> | ||
3 | #include <errno.h> | ||
4 | #include <fcntl.h> | ||
5 | #include <poll.h> | ||
6 | #include <signal.h> | ||
2 | #include <stdlib.h> | 7 | #include <stdlib.h> |
3 | #include <unistd.h> | ||
4 | #include <string.h> | 8 | #include <string.h> |
5 | #include <fcntl.h> | ||
6 | #include <errno.h> | ||
7 | #include <sys/wait.h> | 9 | #include <sys/wait.h> |
8 | #include <signal.h> | 10 | #include <unistd.h> |
9 | #include <poll.h> | 11 | #include <wayland-client.h> |
12 | #include <wayland-cursor.h> | ||
13 | #include <wlr/util/log.h> | ||
10 | #ifdef __FreeBSD__ | 14 | #ifdef __FreeBSD__ |
11 | #include <dev/evdev/input-event-codes.h> | 15 | #include <dev/evdev/input-event-codes.h> |
12 | #else | 16 | #else |
13 | #include <linux/input-event-codes.h> | 17 | #include <linux/input-event-codes.h> |
14 | #endif | 18 | #endif |
15 | #ifdef ENABLE_TRAY | ||
16 | #include <dbus/dbus.h> | ||
17 | #include "swaybar/tray/sni_watcher.h" | ||
18 | #include "swaybar/tray/tray.h" | ||
19 | #include "swaybar/tray/sni.h" | ||
20 | #endif | ||
21 | #include "swaybar/ipc.h" | ||
22 | #include "swaybar/render.h" | 19 | #include "swaybar/render.h" |
23 | #include "swaybar/config.h" | 20 | #include "swaybar/config.h" |
24 | #include "swaybar/status_line.h" | ||
25 | #include "swaybar/event_loop.h" | 21 | #include "swaybar/event_loop.h" |
22 | #include "swaybar/status_line.h" | ||
26 | #include "swaybar/bar.h" | 23 | #include "swaybar/bar.h" |
24 | #include "swaybar/ipc.h" | ||
27 | #include "ipc-client.h" | 25 | #include "ipc-client.h" |
28 | #include "list.h" | 26 | #include "list.h" |
29 | #include "log.h" | 27 | #include "log.h" |
28 | #include "pango.h" | ||
29 | #include "pool-buffer.h" | ||
30 | #include "wlr-layer-shell-unstable-v1-client-protocol.h" | ||
30 | 31 | ||
31 | static void bar_init(struct bar *bar) { | 32 | static void bar_init(struct swaybar *bar) { |
32 | bar->config = init_config(); | 33 | bar->config = init_config(); |
33 | bar->status = init_status_line(); | 34 | wl_list_init(&bar->outputs); |
34 | bar->outputs = create_list(); | ||
35 | } | 35 | } |
36 | 36 | ||
37 | static void spawn_status_cmd_proc(struct bar *bar) { | 37 | static void swaybar_output_free(struct swaybar_output *output) { |
38 | if (bar->config->status_command) { | 38 | if (!output) { |
39 | int pipe_read_fd[2]; | 39 | return; |
40 | int pipe_write_fd[2]; | 40 | } |
41 | 41 | wlr_log(L_DEBUG, "Removing output %s", output->name); | |
42 | if (pipe(pipe_read_fd) != 0) { | 42 | zwlr_layer_surface_v1_destroy(output->layer_surface); |
43 | sway_log(L_ERROR, "Unable to create pipes for status_command fork"); | 43 | wl_surface_destroy(output->surface); |
44 | return; | 44 | wl_output_destroy(output->output); |
45 | } | 45 | destroy_buffer(&output->buffers[0]); |
46 | if (pipe(pipe_write_fd) != 0) { | 46 | destroy_buffer(&output->buffers[1]); |
47 | sway_log(L_ERROR, "Unable to create pipe for status_command fork (write)"); | 47 | struct swaybar_workspace *ws, *ws_tmp; |
48 | close(pipe_read_fd[0]); | 48 | wl_list_for_each_safe(ws, ws_tmp, &output->workspaces, link) { |
49 | close(pipe_read_fd[1]); | 49 | wl_list_remove(&ws->link); |
50 | return; | 50 | free(ws->name); |
51 | } | 51 | free(ws); |
52 | 52 | } | |
53 | bar->status_command_pid = fork(); | 53 | struct swaybar_hotspot *hotspot, *hotspot_tmp; |
54 | if (bar->status_command_pid == 0) { | 54 | wl_list_for_each_safe(hotspot, hotspot_tmp, &output->hotspots, link) { |
55 | close(pipe_read_fd[0]); | 55 | if (hotspot->destroy) { |
56 | dup2(pipe_read_fd[1], STDOUT_FILENO); | 56 | hotspot->destroy(hotspot->data); |
57 | close(pipe_read_fd[1]); | ||
58 | |||
59 | dup2(pipe_write_fd[0], STDIN_FILENO); | ||
60 | close(pipe_write_fd[0]); | ||
61 | close(pipe_write_fd[1]); | ||
62 | |||
63 | char *const cmd[] = { | ||
64 | "sh", | ||
65 | "-c", | ||
66 | bar->config->status_command, | ||
67 | NULL, | ||
68 | }; | ||
69 | execvp(cmd[0], cmd); | ||
70 | return; | ||
71 | } | 57 | } |
72 | 58 | free(hotspot); | |
73 | close(pipe_read_fd[1]); | ||
74 | bar->status_read_fd = pipe_read_fd[0]; | ||
75 | fcntl(bar->status_read_fd, F_SETFL, O_NONBLOCK); | ||
76 | |||
77 | close(pipe_write_fd[0]); | ||
78 | bar->status_write_fd = pipe_write_fd[1]; | ||
79 | fcntl(bar->status_write_fd, F_SETFL, O_NONBLOCK); | ||
80 | } | 59 | } |
60 | wl_list_remove(&output->link); | ||
61 | free(output->name); | ||
62 | free(output); | ||
81 | } | 63 | } |
82 | 64 | ||
83 | struct output *new_output(const char *name) { | 65 | static void layer_surface_configure(void *data, |
84 | struct output *output = malloc(sizeof(struct output)); | 66 | struct zwlr_layer_surface_v1 *surface, |
85 | output->name = strdup(name); | 67 | uint32_t serial, uint32_t width, uint32_t height) { |
86 | output->window = NULL; | 68 | struct swaybar_output *output = data; |
87 | output->registry = NULL; | 69 | output->width = width; |
88 | output->workspaces = create_list(); | 70 | output->height = height; |
89 | #ifdef ENABLE_TRAY | 71 | zwlr_layer_surface_v1_ack_configure(surface, serial); |
90 | output->items = create_list(); | 72 | render_frame(output->bar, output); |
91 | #endif | ||
92 | return output; | ||
93 | } | 73 | } |
94 | 74 | ||
95 | static void mouse_button_notify(struct window *window, int x, int y, | 75 | static void layer_surface_closed(void *_output, |
96 | uint32_t button, uint32_t state_w) { | 76 | struct zwlr_layer_surface_v1 *surface) { |
97 | sway_log(L_DEBUG, "Mouse button %d clicked at %d %d %d", button, x, y, state_w); | 77 | struct swaybar_output *output = _output; |
98 | if (!state_w) { | 78 | swaybar_output_free(output); |
99 | return; | 79 | } |
100 | } | ||
101 | 80 | ||
102 | struct output *clicked_output = NULL; | 81 | struct zwlr_layer_surface_v1_listener layer_surface_listener = { |
103 | for (int i = 0; i < swaybar.outputs->length; i++) { | 82 | .configure = layer_surface_configure, |
104 | struct output *output = swaybar.outputs->items[i]; | 83 | .closed = layer_surface_closed, |
105 | if (window == output->window) { | 84 | }; |
106 | clicked_output = output; | 85 | |
86 | static void wl_pointer_enter(void *data, struct wl_pointer *wl_pointer, | ||
87 | uint32_t serial, struct wl_surface *surface, | ||
88 | wl_fixed_t surface_x, wl_fixed_t surface_y) { | ||
89 | struct swaybar *bar = data; | ||
90 | struct swaybar_pointer *pointer = &bar->pointer; | ||
91 | struct swaybar_output *output; | ||
92 | wl_list_for_each(output, &bar->outputs, link) { | ||
93 | if (output->surface == surface) { | ||
94 | pointer->current = output; | ||
107 | break; | 95 | break; |
108 | } | 96 | } |
109 | } | 97 | } |
110 | 98 | int max_scale = 1; | |
111 | if (!sway_assert(clicked_output != NULL, "Got pointer event for non-existing output")) { | 99 | struct swaybar_output *_output; |
112 | return; | 100 | wl_list_for_each(_output, &bar->outputs, link) { |
113 | } | 101 | if (_output->scale > max_scale) { |
114 | 102 | max_scale = _output->scale; | |
115 | double button_x = 0.5; | ||
116 | for (int i = 0; i < clicked_output->workspaces->length; i++) { | ||
117 | struct workspace *workspace = clicked_output->workspaces->items[i]; | ||
118 | int button_width, button_height; | ||
119 | |||
120 | workspace_button_size(window, workspace->name, &button_width, &button_height); | ||
121 | |||
122 | button_x += button_width; | ||
123 | if (x <= button_x) { | ||
124 | ipc_send_workspace_command(workspace->name); | ||
125 | break; | ||
126 | } | 103 | } |
127 | } | 104 | } |
105 | wl_surface_set_buffer_scale(pointer->cursor_surface, max_scale); | ||
106 | wl_surface_attach(pointer->cursor_surface, | ||
107 | wl_cursor_image_get_buffer(pointer->cursor_image), 0, 0); | ||
108 | wl_pointer_set_cursor(wl_pointer, serial, pointer->cursor_surface, | ||
109 | pointer->cursor_image->hotspot_x / max_scale, | ||
110 | pointer->cursor_image->hotspot_y / max_scale); | ||
111 | wl_surface_commit(pointer->cursor_surface); | ||
112 | } | ||
128 | 113 | ||
129 | switch (button) { | 114 | static void wl_pointer_leave(void *data, struct wl_pointer *wl_pointer, |
130 | case BTN_LEFT: | 115 | uint32_t serial, struct wl_surface *surface) { |
131 | status_line_mouse_event(&swaybar, x, y, 1); | 116 | struct swaybar *bar = data; |
132 | break; | 117 | bar->pointer.current = NULL; |
133 | case BTN_MIDDLE: | 118 | } |
134 | status_line_mouse_event(&swaybar, x, y, 2); | ||
135 | break; | ||
136 | case BTN_RIGHT: | ||
137 | status_line_mouse_event(&swaybar, x, y, 3); | ||
138 | break; | ||
139 | } | ||
140 | |||
141 | #ifdef ENABLE_TRAY | ||
142 | tray_mouse_event(clicked_output, x, y, button, state_w); | ||
143 | #endif | ||
144 | 119 | ||
120 | static void wl_pointer_motion(void *data, struct wl_pointer *wl_pointer, | ||
121 | uint32_t time, wl_fixed_t surface_x, wl_fixed_t surface_y) { | ||
122 | struct swaybar *bar = data; | ||
123 | bar->pointer.x = wl_fixed_to_int(surface_x); | ||
124 | bar->pointer.y = wl_fixed_to_int(surface_y); | ||
145 | } | 125 | } |
146 | 126 | ||
147 | static void mouse_scroll_notify(struct window *window, enum scroll_direction direction) { | 127 | static void wl_pointer_button(void *data, struct wl_pointer *wl_pointer, |
148 | sway_log(L_DEBUG, "Mouse wheel scrolled %s", direction == SCROLL_UP ? "up" : "down"); | 128 | uint32_t serial, uint32_t time, uint32_t button, uint32_t state) { |
149 | 129 | struct swaybar *bar = data; | |
150 | // If there are status blocks and click_events are enabled | 130 | struct swaybar_pointer *pointer = &bar->pointer; |
151 | // check if the position is within the status area and if so | 131 | struct swaybar_output *output = pointer->current; |
152 | // tell the status line to output the event and skip workspace | 132 | if (!sway_assert(output, "button with no active output")) { |
153 | // switching below. | 133 | return; |
154 | int num_blocks = swaybar.status->block_line->length; | 134 | } |
155 | if (swaybar.status->click_events && num_blocks > 0) { | 135 | if (state != WL_POINTER_BUTTON_STATE_PRESSED) { |
156 | struct status_block *first_block = swaybar.status->block_line->items[0]; | 136 | return; |
157 | int x = window->pointer_input.last_x; | 137 | } |
158 | int y = window->pointer_input.last_y; | 138 | struct swaybar_hotspot *hotspot; |
159 | if (x > first_block->x) { | 139 | wl_list_for_each(hotspot, &output->hotspots, link) { |
160 | if (direction == SCROLL_UP) { | 140 | double x = pointer->x * output->scale; |
161 | status_line_mouse_event(&swaybar, x, y, 4); | 141 | double y = pointer->y * output->scale; |
162 | } else { | 142 | if (x >= hotspot->x |
163 | status_line_mouse_event(&swaybar, x, y, 5); | 143 | && y >= hotspot->y |
164 | } | 144 | && x < hotspot->x + hotspot->width |
165 | return; | 145 | && y < hotspot->y + hotspot->height) { |
146 | hotspot->callback(output, pointer->x, pointer->y, | ||
147 | button, hotspot->data); | ||
166 | } | 148 | } |
167 | } | 149 | } |
150 | } | ||
168 | 151 | ||
169 | if (!swaybar.config->wrap_scroll) { | 152 | static void wl_pointer_axis(void *data, struct wl_pointer *wl_pointer, |
170 | // Find output this window lives on | 153 | uint32_t time, uint32_t axis, wl_fixed_t value) { |
171 | int i; | 154 | struct swaybar *bar = data; |
172 | struct output *output = NULL; | 155 | struct swaybar_output *output = bar->pointer.current; |
173 | for (i = 0; i < swaybar.outputs->length; ++i) { | 156 | if (!sway_assert(output, "axis with no active output")) { |
174 | output = swaybar.outputs->items[i]; | 157 | return; |
175 | if (output->window == window) { | 158 | } |
176 | break; | 159 | double amt = wl_fixed_to_double(value); |
177 | } | 160 | if (!bar->config->wrap_scroll) { |
178 | } | 161 | int i = 0; |
179 | if (!sway_assert(i != swaybar.outputs->length, "Unknown window in scroll event")) { | 162 | struct swaybar_workspace *ws = NULL; |
180 | return; | 163 | wl_list_for_each(ws, &output->workspaces, link) { |
181 | } | ||
182 | int focused = -1; | ||
183 | for (i = 0; i < output->workspaces->length; ++i) { | ||
184 | struct workspace *ws = output->workspaces->items[i]; | ||
185 | if (ws->focused) { | 164 | if (ws->focused) { |
186 | focused = i; | ||
187 | break; | 165 | break; |
188 | } | 166 | } |
167 | ++i; | ||
189 | } | 168 | } |
190 | if (!sway_assert(focused != -1, "Scroll wheel event received on inactive output")) { | 169 | int len = wl_list_length(&output->workspaces); |
170 | if (!sway_assert(i != len, "axis with null workspace")) { | ||
191 | return; | 171 | return; |
192 | } | 172 | } |
193 | if ((focused == 0 && direction == SCROLL_UP) || | 173 | if (i == 0 && amt > 0) { |
194 | (focused == output->workspaces->length - 1 && direction == SCROLL_DOWN)) { | 174 | return; // Do not wrap |
195 | // Do not wrap | 175 | } |
196 | return; | 176 | if (i == len - 1 && amt < 0) { |
177 | return; // Do not wrap | ||
197 | } | 178 | } |
198 | } | 179 | } |
199 | 180 | ||
200 | const char *workspace_name = direction == SCROLL_UP ? "prev_on_output" : "next_on_output"; | 181 | const char *workspace_name = |
201 | ipc_send_workspace_command(workspace_name); | 182 | amt < 0 ? "prev_on_output" : "next_on_output"; |
183 | ipc_send_workspace_command(bar, workspace_name); | ||
202 | } | 184 | } |
203 | 185 | ||
204 | void bar_setup(struct bar *bar, const char *socket_path, const char *bar_id) { | 186 | static void wl_pointer_frame(void *data, struct wl_pointer *wl_pointer) { |
205 | /* initialize bar with default values */ | 187 | // Who cares |
206 | bar_init(bar); | 188 | } |
207 | |||
208 | /* Initialize event loop lists */ | ||
209 | init_event_loop(); | ||
210 | |||
211 | /* connect to sway ipc */ | ||
212 | bar->ipc_socketfd = ipc_open_socket(socket_path); | ||
213 | bar->ipc_event_socketfd = ipc_open_socket(socket_path); | ||
214 | |||
215 | ipc_bar_init(bar, bar_id); | ||
216 | |||
217 | int i; | ||
218 | for (i = 0; i < bar->outputs->length; ++i) { | ||
219 | struct output *bar_output = bar->outputs->items[i]; | ||
220 | |||
221 | bar_output->registry = registry_poll(); | ||
222 | |||
223 | if (!bar_output->registry->desktop_shell) { | ||
224 | sway_abort("swaybar requires the compositor to support the desktop-shell extension."); | ||
225 | } | ||
226 | |||
227 | struct output_state *output = bar_output->registry->outputs->items[bar_output->idx]; | ||
228 | |||
229 | bar_output->window = window_setup(bar_output->registry, | ||
230 | output->width / output->scale, 30, output->scale, false); | ||
231 | if (!bar_output->window) { | ||
232 | sway_abort("Failed to create window."); | ||
233 | } | ||
234 | desktop_shell_set_panel(bar_output->registry->desktop_shell, | ||
235 | output->output, bar_output->window->surface); | ||
236 | desktop_shell_set_panel_position(bar_output->registry->desktop_shell, | ||
237 | bar->config->position); | ||
238 | 189 | ||
239 | window_make_shell(bar_output->window); | 190 | static void wl_pointer_axis_source(void *data, struct wl_pointer *wl_pointer, |
191 | uint32_t axis_source) { | ||
192 | // Who cares | ||
193 | } | ||
240 | 194 | ||
241 | /* set font */ | 195 | static void wl_pointer_axis_stop(void *data, struct wl_pointer *wl_pointer, |
242 | bar_output->window->font = bar->config->font; | 196 | uint32_t time, uint32_t axis) { |
197 | // Who cares | ||
198 | } | ||
243 | 199 | ||
244 | /* set mouse event callbacks */ | 200 | static void wl_pointer_axis_discrete(void *data, struct wl_pointer *wl_pointer, |
245 | bar_output->window->pointer_input.notify_button = mouse_button_notify; | 201 | uint32_t axis, int32_t discrete) { |
246 | bar_output->window->pointer_input.notify_scroll = mouse_scroll_notify; | 202 | // Who cares |
203 | } | ||
247 | 204 | ||
248 | /* set window height */ | 205 | struct wl_pointer_listener pointer_listener = { |
249 | set_window_height(bar_output->window, bar->config->height); | 206 | .enter = wl_pointer_enter, |
207 | .leave = wl_pointer_leave, | ||
208 | .motion = wl_pointer_motion, | ||
209 | .button = wl_pointer_button, | ||
210 | .axis = wl_pointer_axis, | ||
211 | .frame = wl_pointer_frame, | ||
212 | .axis_source = wl_pointer_axis_source, | ||
213 | .axis_stop = wl_pointer_axis_stop, | ||
214 | .axis_discrete = wl_pointer_axis_discrete, | ||
215 | }; | ||
216 | |||
217 | static void seat_handle_capabilities(void *data, struct wl_seat *wl_seat, | ||
218 | enum wl_seat_capability caps) { | ||
219 | struct swaybar *bar = data; | ||
220 | if ((caps & WL_SEAT_CAPABILITY_POINTER)) { | ||
221 | bar->pointer.pointer = wl_seat_get_pointer(wl_seat); | ||
222 | wl_pointer_add_listener(bar->pointer.pointer, &pointer_listener, bar); | ||
250 | } | 223 | } |
251 | /* spawn status command */ | 224 | } |
252 | spawn_status_cmd_proc(bar); | ||
253 | 225 | ||
254 | #ifdef ENABLE_TRAY | 226 | static void seat_handle_name(void *data, struct wl_seat *wl_seat, |
255 | init_tray(bar); | 227 | const char *name) { |
256 | #endif | 228 | // Who cares |
257 | } | 229 | } |
258 | 230 | ||
259 | bool dirty = true; | 231 | const struct wl_seat_listener seat_listener = { |
232 | .capabilities = seat_handle_capabilities, | ||
233 | .name = seat_handle_name, | ||
234 | }; | ||
260 | 235 | ||
261 | static void respond_ipc(int fd, short mask, void *_bar) { | 236 | static void output_geometry(void *data, struct wl_output *output, int32_t x, |
262 | struct bar *bar = (struct bar *)_bar; | 237 | int32_t y, int32_t width_mm, int32_t height_mm, int32_t subpixel, |
263 | sway_log(L_DEBUG, "Got IPC event."); | 238 | const char *make, const char *model, int32_t transform) { |
264 | dirty = handle_ipc_event(bar); | 239 | // Who cares |
265 | } | 240 | } |
266 | 241 | ||
267 | static void respond_command(int fd, short mask, void *_bar) { | 242 | static void output_mode(void *data, struct wl_output *output, uint32_t flags, |
268 | struct bar *bar = (struct bar *)_bar; | 243 | int32_t width, int32_t height, int32_t refresh) { |
269 | dirty = handle_status_line(bar); | 244 | // Who cares |
270 | } | 245 | } |
271 | 246 | ||
272 | static void respond_output(int fd, short mask, void *_output) { | 247 | static void output_done(void *data, struct wl_output *output) { |
273 | struct output *output = (struct output *)_output; | 248 | // Who cares |
274 | if (wl_display_dispatch(output->registry->display) == -1) { | 249 | } |
275 | sway_log(L_ERROR, "failed to dispatch wl: %d", errno); | 250 | |
251 | static void output_scale(void *data, struct wl_output *wl_output, | ||
252 | int32_t factor) { | ||
253 | struct swaybar_output *output = data; | ||
254 | output->scale = factor; | ||
255 | if (output->surface) { | ||
256 | render_frame(output->bar, output); | ||
276 | } | 257 | } |
277 | } | 258 | } |
278 | 259 | ||
279 | void bar_run(struct bar *bar) { | 260 | struct wl_output_listener output_listener = { |
280 | add_event(bar->ipc_event_socketfd, POLLIN, respond_ipc, bar); | 261 | .geometry = output_geometry, |
281 | add_event(bar->status_read_fd, POLLIN, respond_command, bar); | 262 | .mode = output_mode, |
263 | .done = output_done, | ||
264 | .scale = output_scale, | ||
265 | }; | ||
282 | 266 | ||
283 | int i; | 267 | static bool bar_uses_output(struct swaybar *bar, size_t output_index) { |
284 | for (i = 0; i < bar->outputs->length; ++i) { | 268 | if (bar->config->all_outputs) { |
285 | struct output *output = bar->outputs->items[i]; | 269 | return true; |
286 | add_event(wl_display_get_fd(output->registry->display), | ||
287 | POLLIN, respond_output, output); | ||
288 | } | 270 | } |
289 | 271 | struct config_output *coutput; | |
290 | while (1) { | 272 | wl_list_for_each(coutput, &bar->config->outputs, link) { |
291 | if (dirty) { | 273 | if (coutput->index == output_index) { |
292 | int i; | 274 | return true; |
293 | for (i = 0; i < bar->outputs->length; ++i) { | ||
294 | struct output *output = bar->outputs->items[i]; | ||
295 | if (window_prerender(output->window) && output->window->cairo) { | ||
296 | render(output, bar->config, bar->status); | ||
297 | window_render(output->window); | ||
298 | wl_display_flush(output->registry->display); | ||
299 | } | ||
300 | } | ||
301 | } | 275 | } |
276 | } | ||
277 | return false; | ||
278 | } | ||
302 | 279 | ||
303 | dirty = false; | 280 | static void handle_global(void *data, struct wl_registry *registry, |
304 | 281 | uint32_t name, const char *interface, uint32_t version) { | |
305 | event_loop_poll(); | 282 | struct swaybar *bar = data; |
306 | #ifdef ENABLE_TRAY | 283 | if (strcmp(interface, wl_compositor_interface.name) == 0) { |
307 | dispatch_dbus(); | 284 | bar->compositor = wl_registry_bind(registry, name, |
308 | #endif | 285 | &wl_compositor_interface, 3); |
286 | } else if (strcmp(interface, wl_seat_interface.name) == 0) { | ||
287 | bar->seat = wl_registry_bind(registry, name, | ||
288 | &wl_seat_interface, 1); | ||
289 | wl_seat_add_listener(bar->seat, &seat_listener, bar); | ||
290 | } else if (strcmp(interface, wl_shm_interface.name) == 0) { | ||
291 | bar->shm = wl_registry_bind(registry, name, | ||
292 | &wl_shm_interface, 1); | ||
293 | } else if (strcmp(interface, wl_output_interface.name) == 0) { | ||
294 | static size_t output_index = 0; | ||
295 | if (bar_uses_output(bar, output_index)) { | ||
296 | struct swaybar_output *output = | ||
297 | calloc(1, sizeof(struct swaybar_output)); | ||
298 | output->bar = bar; | ||
299 | output->output = wl_registry_bind(registry, name, | ||
300 | &wl_output_interface, 3); | ||
301 | wl_output_add_listener(output->output, &output_listener, output); | ||
302 | output->scale = 1; | ||
303 | output->index = output_index; | ||
304 | output->wl_name = name; | ||
305 | wl_list_init(&output->workspaces); | ||
306 | wl_list_init(&output->hotspots); | ||
307 | wl_list_insert(&bar->outputs, &output->link); | ||
308 | } | ||
309 | ++output_index; | ||
310 | } else if (strcmp(interface, zwlr_layer_shell_v1_interface.name) == 0) { | ||
311 | bar->layer_shell = wl_registry_bind( | ||
312 | registry, name, &zwlr_layer_shell_v1_interface, 1); | ||
309 | } | 313 | } |
310 | } | 314 | } |
311 | 315 | ||
312 | void free_workspaces(list_t *workspaces) { | 316 | static void handle_global_remove(void *data, struct wl_registry *registry, |
313 | int i; | 317 | uint32_t name) { |
314 | for (i = 0; i < workspaces->length; ++i) { | 318 | struct swaybar *bar = data; |
315 | struct workspace *ws = workspaces->items[i]; | 319 | struct swaybar_output *output, *tmp; |
316 | free(ws->name); | 320 | wl_list_for_each_safe(output, tmp, &bar->outputs, link) { |
317 | free(ws); | 321 | if (output->wl_name == name) { |
322 | swaybar_output_free(output); | ||
323 | break; | ||
324 | } | ||
318 | } | 325 | } |
319 | list_free(workspaces); | ||
320 | } | 326 | } |
321 | 327 | ||
322 | static void free_output(struct output *output) { | 328 | static const struct wl_registry_listener registry_listener = { |
323 | window_teardown(output->window); | 329 | .global = handle_global, |
324 | if (output->registry) { | 330 | .global_remove = handle_global_remove, |
325 | registry_teardown(output->registry); | 331 | }; |
332 | |||
333 | static void render_all_frames(struct swaybar *bar) { | ||
334 | struct swaybar_output *output; | ||
335 | wl_list_for_each(output, &bar->outputs, link) { | ||
336 | render_frame(bar, output); | ||
326 | } | 337 | } |
338 | } | ||
327 | 339 | ||
328 | free(output->name); | 340 | void bar_setup(struct swaybar *bar, |
341 | const char *socket_path, const char *bar_id) { | ||
342 | bar_init(bar); | ||
343 | init_event_loop(); | ||
329 | 344 | ||
330 | if (output->workspaces) { | 345 | bar->ipc_socketfd = ipc_open_socket(socket_path); |
331 | free_workspaces(output->workspaces); | 346 | bar->ipc_event_socketfd = ipc_open_socket(socket_path); |
347 | ipc_initialize(bar, bar_id); | ||
348 | if (bar->config->status_command) { | ||
349 | bar->status = status_line_init(bar->config->status_command); | ||
332 | } | 350 | } |
333 | 351 | ||
334 | free(output); | 352 | bar->display = wl_display_connect(NULL); |
335 | } | 353 | assert(bar->display); |
336 | 354 | ||
337 | static void free_outputs(list_t *outputs) { | 355 | struct wl_registry *registry = wl_display_get_registry(bar->display); |
338 | int i; | 356 | wl_registry_add_listener(registry, ®istry_listener, bar); |
339 | for (i = 0; i < outputs->length; ++i) { | 357 | wl_display_roundtrip(bar->display); |
340 | free_output(outputs->items[i]); | 358 | assert(bar->compositor && bar->layer_shell && bar->shm); |
359 | wl_display_roundtrip(bar->display); | ||
360 | |||
361 | struct swaybar_pointer *pointer = &bar->pointer; | ||
362 | |||
363 | int max_scale = 1; | ||
364 | struct swaybar_output *output; | ||
365 | wl_list_for_each(output, &bar->outputs, link) { | ||
366 | if (output->scale > max_scale) { | ||
367 | max_scale = output->scale; | ||
368 | } | ||
341 | } | 369 | } |
342 | list_free(outputs); | ||
343 | } | ||
344 | 370 | ||
345 | static void terminate_status_command(pid_t pid) { | 371 | pointer->cursor_theme = wl_cursor_theme_load( |
346 | if (pid) { | 372 | NULL, 24 * max_scale, bar->shm); |
347 | // terminate status_command process | 373 | assert(pointer->cursor_theme); |
348 | int ret = kill(pid, SIGTERM); | 374 | struct wl_cursor *cursor; |
349 | if (ret != 0) { | 375 | cursor = wl_cursor_theme_get_cursor(pointer->cursor_theme, "left_ptr"); |
350 | sway_log(L_ERROR, "Unable to terminate status_command [pid: %d]", pid); | 376 | assert(cursor); |
351 | } else { | 377 | pointer->cursor_image = cursor->images[0]; |
352 | int status; | 378 | pointer->cursor_surface = wl_compositor_create_surface(bar->compositor); |
353 | waitpid(pid, &status, 0); | 379 | assert(pointer->cursor_surface); |
380 | |||
381 | wl_list_for_each(output, &bar->outputs, link) { | ||
382 | struct config_output *coutput; | ||
383 | wl_list_for_each(coutput, &bar->config->outputs, link) { | ||
384 | if (coutput->index == output->index) { | ||
385 | output->name = strdup(coutput->name); | ||
386 | break; | ||
387 | } | ||
354 | } | 388 | } |
389 | output->surface = wl_compositor_create_surface(bar->compositor); | ||
390 | assert(output->surface); | ||
391 | output->layer_surface = zwlr_layer_shell_v1_get_layer_surface( | ||
392 | bar->layer_shell, output->surface, output->output, | ||
393 | ZWLR_LAYER_SHELL_V1_LAYER_BOTTOM, "panel"); | ||
394 | assert(output->layer_surface); | ||
395 | zwlr_layer_surface_v1_add_listener(output->layer_surface, | ||
396 | &layer_surface_listener, output); | ||
397 | zwlr_layer_surface_v1_set_anchor(output->layer_surface, | ||
398 | bar->config->position); | ||
355 | } | 399 | } |
400 | ipc_get_workspaces(bar); | ||
401 | render_all_frames(bar); | ||
356 | } | 402 | } |
357 | 403 | ||
358 | void bar_teardown(struct bar *bar) { | 404 | static void display_in(int fd, short mask, void *_bar) { |
359 | if (bar->config) { | 405 | struct swaybar *bar = (struct swaybar *)_bar; |
360 | free_config(bar->config); | 406 | if (wl_display_dispatch(bar->display) == -1) { |
407 | bar_teardown(bar); | ||
408 | exit(0); | ||
361 | } | 409 | } |
410 | } | ||
362 | 411 | ||
363 | if (bar->outputs) { | 412 | static void ipc_in(int fd, short mask, void *_bar) { |
364 | free_outputs(bar->outputs); | 413 | struct swaybar *bar = (struct swaybar *)_bar; |
414 | if (handle_ipc_readable(bar)) { | ||
415 | render_all_frames(bar); | ||
365 | } | 416 | } |
417 | } | ||
366 | 418 | ||
367 | if (bar->status) { | 419 | static void status_in(int fd, short mask, void *_bar) { |
368 | free_status_line(bar->status); | 420 | struct swaybar *bar = (struct swaybar *)_bar; |
421 | if (status_handle_readable(bar->status)) { | ||
422 | render_all_frames(bar); | ||
369 | } | 423 | } |
424 | } | ||
370 | 425 | ||
371 | /* close sockets/pipes */ | 426 | void bar_run(struct swaybar *bar) { |
372 | if (bar->status_read_fd) { | 427 | add_event(wl_display_get_fd(bar->display), POLLIN, display_in, bar); |
373 | close(bar->status_read_fd); | 428 | add_event(bar->ipc_event_socketfd, POLLIN, ipc_in, bar); |
429 | if (bar->status) { | ||
430 | add_event(bar->status->read_fd, POLLIN, status_in, bar); | ||
374 | } | 431 | } |
375 | 432 | while (1) { | |
376 | if (bar->status_write_fd) { | 433 | event_loop_poll(); |
377 | close(bar->status_write_fd); | ||
378 | } | 434 | } |
435 | } | ||
379 | 436 | ||
380 | if (bar->ipc_socketfd) { | 437 | static void free_outputs(struct wl_list *list) { |
381 | close(bar->ipc_socketfd); | 438 | struct swaybar_output *output, *tmp; |
439 | wl_list_for_each_safe(output, tmp, list, link) { | ||
440 | wl_list_remove(&output->link); | ||
441 | free(output->name); | ||
442 | free(output); | ||
382 | } | 443 | } |
444 | } | ||
383 | 445 | ||
384 | if (bar->ipc_event_socketfd) { | 446 | void bar_teardown(struct swaybar *bar) { |
385 | close(bar->ipc_event_socketfd); | 447 | free_outputs(&bar->outputs); |
448 | if (bar->config) { | ||
449 | free_config(bar->config); | ||
450 | } | ||
451 | close(bar->ipc_event_socketfd); | ||
452 | close(bar->ipc_socketfd); | ||
453 | if (bar->status) { | ||
454 | status_line_free(bar->status); | ||
386 | } | 455 | } |
387 | |||
388 | /* terminate status command process */ | ||
389 | terminate_status_command(bar->status_command_pid); | ||
390 | } | 456 | } |