diff options
Diffstat (limited to 'swaybar/bar.c')
-rw-r--r-- | swaybar/bar.c | 83 |
1 files changed, 56 insertions, 27 deletions
diff --git a/swaybar/bar.c b/swaybar/bar.c index 08c386a7..d36367fc 100644 --- a/swaybar/bar.c +++ b/swaybar/bar.c @@ -1,4 +1,4 @@ -#define _XOPEN_SOURCE 500 +#define _POSIX_C_SOURCE 200809L #include <assert.h> #include <errno.h> #include <fcntl.h> @@ -11,6 +11,7 @@ #include <wayland-client.h> #include <wayland-cursor.h> #include <wlr/util/log.h> +#include "config.h" #include "swaybar/bar.h" #include "swaybar/config.h" #include "swaybar/i3bar.h" @@ -18,6 +19,9 @@ #include "swaybar/ipc.h" #include "swaybar/status_line.h" #include "swaybar/render.h" +#if HAVE_TRAY +#include "swaybar/tray/tray.h" +#endif #include "ipc-client.h" #include "list.h" #include "log.h" @@ -31,6 +35,7 @@ void free_workspaces(struct wl_list *list) { wl_list_for_each_safe(ws, tmp, list, link) { wl_list_remove(&ws->link); free(ws->name); + free(ws->label); free(ws); } } @@ -54,6 +59,7 @@ static void swaybar_output_free(struct swaybar_output *output) { free_workspaces(&output->workspaces); wl_list_remove(&output->link); free(output->name); + free(output->identifier); free(output); } @@ -119,7 +125,7 @@ static void destroy_layer_surface(struct swaybar_output *output) { output->frame_scheduled = false; } -static void set_bar_dirty(struct swaybar *bar) { +void set_bar_dirty(struct swaybar *bar) { struct swaybar_output *output; wl_list_for_each(output, &bar->outputs, link) { set_output_dirty(output); @@ -161,13 +167,15 @@ bool determine_bar_visibility(struct swaybar *bar, bool moving_layer) { return visible; } -static bool bar_uses_output(struct swaybar *bar, const char *name) { - if (bar->config->all_outputs) { +static bool bar_uses_output(struct swaybar_output *output) { + if (output->bar->config->all_outputs) { return true; } + char *identifier = output->identifier; struct config_output *coutput; - wl_list_for_each(coutput, &bar->config->outputs, link) { - if (strcmp(coutput->name, name) == 0) { + wl_list_for_each(coutput, &output->bar->config->outputs, link) { + if (strcmp(coutput->name, output->name) == 0 || + (identifier && strcmp(coutput->name, identifier) == 0)) { return true; } } @@ -195,6 +203,10 @@ static void output_scale(void *data, struct wl_output *wl_output, int32_t factor) { struct swaybar_output *output = data; output->scale = factor; + if (output == output->bar->pointer.current) { + update_cursor(output->bar); + render_frame(output); + } } struct wl_output_listener output_listener = { @@ -206,12 +218,16 @@ struct wl_output_listener output_listener = { static void xdg_output_handle_logical_position(void *data, struct zxdg_output_v1 *xdg_output, int32_t x, int32_t y) { - // Who cares + struct swaybar_output *output = data; + output->output_x = x; + output->output_y = y; } static void xdg_output_handle_logical_size(void *data, struct zxdg_output_v1 *xdg_output, int32_t width, int32_t height) { - // Who cares + struct swaybar_output *output = data; + output->output_height = height; + output->output_width = width; } static void xdg_output_handle_done(void *data, @@ -220,7 +236,7 @@ static void xdg_output_handle_done(void *data, struct swaybar *bar = output->bar; assert(output->name != NULL); - if (!bar_uses_output(bar, output->name)) { + if (!bar_uses_output(output)) { swaybar_output_free(output); return; } @@ -245,7 +261,22 @@ static void xdg_output_handle_name(void *data, static void xdg_output_handle_description(void *data, struct zxdg_output_v1 *xdg_output, const char *description) { - // Who cares + // wlroots currently sets the description to `make model serial (name)` + // If this changes in the future, this will need to be modified. + struct swaybar_output *output = data; + free(output->identifier); + output->identifier = NULL; + char *paren = strrchr(description, '('); + if (paren) { + size_t length = paren - description; + output->identifier = malloc(length); + if (!output->identifier) { + wlr_log(WLR_ERROR, "Failed to allocate output identifier"); + return; + } + strncpy(output->identifier, description, length); + output->identifier[length - 1] = '\0'; + } } struct zxdg_output_v1_listener xdg_output_listener = { @@ -272,7 +303,7 @@ static void handle_global(void *data, struct wl_registry *registry, struct swaybar *bar = data; if (strcmp(interface, wl_compositor_interface.name) == 0) { bar->compositor = wl_registry_bind(registry, name, - &wl_compositor_interface, 3); + &wl_compositor_interface, 4); } else if (strcmp(interface, wl_seat_interface.name) == 0) { bar->seat = wl_registry_bind(registry, name, &wl_seat_interface, 3); @@ -354,25 +385,15 @@ bool bar_setup(struct swaybar *bar, const char *socket_path) { wl_display_roundtrip(bar->display); struct swaybar_pointer *pointer = &bar->pointer; - - int max_scale = 1; - struct swaybar_output *output; - wl_list_for_each(output, &bar->outputs, link) { - if (output->scale > max_scale) { - max_scale = output->scale; - } - } - - pointer->cursor_theme = - wl_cursor_theme_load(NULL, 24 * max_scale, bar->shm); - assert(pointer->cursor_theme); - struct wl_cursor *cursor; - cursor = wl_cursor_theme_get_cursor(pointer->cursor_theme, "left_ptr"); - assert(cursor); - pointer->cursor_image = cursor->images[0]; pointer->cursor_surface = wl_compositor_create_surface(bar->compositor); assert(pointer->cursor_surface); +#if HAVE_TRAY + if (!bar->config->tray_hidden) { + bar->tray = create_tray(bar); + } +#endif + if (bar->config->workspace_buttons) { ipc_get_workspaces(bar); } @@ -414,6 +435,11 @@ void bar_run(struct swaybar *bar) { loop_add_fd(bar->eventloop, bar->status->read_fd, POLLIN, status_in, bar); } +#if HAVE_TRAY + if (bar->tray) { + loop_add_fd(bar->eventloop, bar->tray->fd, POLLIN, tray_in, bar->tray->bus); + } +#endif while (1) { errno = 0; if (wl_display_flush(bar->display) == -1 && errno != EAGAIN) { @@ -431,6 +457,9 @@ static void free_outputs(struct wl_list *list) { } void bar_teardown(struct swaybar *bar) { +#if HAVE_TRAY + destroy_tray(bar->tray); +#endif free_outputs(&bar->outputs); if (bar->config) { free_config(bar->config); |