diff options
Diffstat (limited to 'swaybar')
-rw-r--r-- | swaybar/bar.c | 161 | ||||
-rw-r--r-- | swaybar/config.c | 2 | ||||
-rw-r--r-- | swaybar/i3bar.c | 19 | ||||
-rw-r--r-- | swaybar/ipc.c | 19 | ||||
-rw-r--r-- | swaybar/main.c | 5 | ||||
-rw-r--r-- | swaybar/meson.build | 1 | ||||
-rw-r--r-- | swaybar/render.c | 146 | ||||
-rw-r--r-- | swaybar/status_line.c | 36 |
8 files changed, 251 insertions, 138 deletions
diff --git a/swaybar/bar.c b/swaybar/bar.c index 3ae730f7..388c24c4 100644 --- a/swaybar/bar.c +++ b/swaybar/bar.c @@ -16,12 +16,13 @@ #else #include <linux/input-event-codes.h> #endif -#include "swaybar/render.h" +#include "swaybar/bar.h" #include "swaybar/config.h" #include "swaybar/event_loop.h" -#include "swaybar/status_line.h" -#include "swaybar/bar.h" +#include "swaybar/i3bar.h" #include "swaybar/ipc.h" +#include "swaybar/status_line.h" +#include "swaybar/render.h" #include "ipc-client.h" #include "list.h" #include "log.h" @@ -48,8 +49,13 @@ static void swaybar_output_free(struct swaybar_output *output) { return; } wlr_log(WLR_DEBUG, "Removing output %s", output->name); - zwlr_layer_surface_v1_destroy(output->layer_surface); - wl_surface_destroy(output->surface); + if (output->layer_surface != NULL) { + zwlr_layer_surface_v1_destroy(output->layer_surface); + } + if (output->surface != NULL) { + wl_surface_destroy(output->surface); + } + zxdg_output_v1_destroy(output->xdg_output); wl_output_destroy(output->output); destroy_buffer(&output->buffers[0]); destroy_buffer(&output->buffers[1]); @@ -66,6 +72,16 @@ static void swaybar_output_free(struct swaybar_output *output) { free(output); } +static void set_output_dirty(struct swaybar_output *output) { + if (output->frame_scheduled) { + output->dirty = true; + return; + } + if (output->surface) { + render_frame(output); + } +} + static void layer_surface_configure(void *data, struct zwlr_layer_surface_v1 *surface, uint32_t serial, uint32_t width, uint32_t height) { @@ -73,7 +89,7 @@ static void layer_surface_configure(void *data, output->width = width; output->height = height; zwlr_layer_surface_v1_ack_configure(surface, serial); - render_frame(output->bar, output); + set_output_dirty(output); } static void layer_surface_closed(void *_output, @@ -283,28 +299,58 @@ const struct wl_seat_listener seat_listener = { .name = seat_handle_name, }; -static void output_geometry(void *data, struct wl_output *output, int32_t x, +static void add_layer_surface(struct swaybar_output *output) { + if (output->surface != NULL) { + return; + } + struct swaybar *bar = output->bar; + + output->surface = wl_compositor_create_surface(bar->compositor); + assert(output->surface); + output->layer_surface = zwlr_layer_shell_v1_get_layer_surface( + bar->layer_shell, output->surface, output->output, + ZWLR_LAYER_SHELL_V1_LAYER_BOTTOM, "panel"); + assert(output->layer_surface); + zwlr_layer_surface_v1_add_listener(output->layer_surface, + &layer_surface_listener, output); + zwlr_layer_surface_v1_set_anchor(output->layer_surface, + bar->config->position); +} + +static bool bar_uses_output(struct swaybar *bar, const char *name) { + if (bar->config->all_outputs) { + return true; + } + struct config_output *coutput; + wl_list_for_each(coutput, &bar->config->outputs, link) { + if (strcmp(coutput->name, name) == 0) { + return true; + } + } + return false; +} + +static void output_geometry(void *data, struct wl_output *wl_output, int32_t x, int32_t y, int32_t width_mm, int32_t height_mm, int32_t subpixel, const char *make, const char *model, int32_t transform) { - // Who cares + struct swaybar_output *output = data; + output->subpixel = subpixel; } -static void output_mode(void *data, struct wl_output *output, uint32_t flags, +static void output_mode(void *data, struct wl_output *wl_output, uint32_t flags, int32_t width, int32_t height, int32_t refresh) { // Who cares } -static void output_done(void *data, struct wl_output *output) { - // Who cares +static void output_done(void *data, struct wl_output *wl_output) { + struct swaybar_output *output = data; + set_output_dirty(output); } static void output_scale(void *data, struct wl_output *wl_output, int32_t factor) { struct swaybar_output *output = data; output->scale = factor; - if (output->surface) { - render_frame(output->bar, output); - } } struct wl_output_listener output_listener = { @@ -326,7 +372,22 @@ static void xdg_output_handle_logical_size(void *data, static void xdg_output_handle_done(void *data, struct zxdg_output_v1 *xdg_output) { - // Who cares + struct swaybar_output *output = data; + struct swaybar *bar = output->bar; + + assert(output->name != NULL); + if (!bar_uses_output(bar, output->name)) { + swaybar_output_free(output); + return; + } + + if (wl_list_empty(&output->link)) { + wl_list_remove(&output->link); + wl_list_insert(&bar->outputs, &output->link); + + add_layer_surface(output); + set_output_dirty(output); + } } static void xdg_output_handle_name(void *data, @@ -349,17 +410,15 @@ struct zxdg_output_v1_listener xdg_output_listener = { .description = xdg_output_handle_description, }; -static bool bar_uses_output(struct swaybar *bar, const char *name) { - if (bar->config->all_outputs) { - return true; - } - struct config_output *coutput; - wl_list_for_each(coutput, &bar->config->outputs, link) { - if (strcmp(coutput->name, name) == 0) { - return true; - } +static void add_xdg_output(struct swaybar_output *output) { + if (output->xdg_output != NULL) { + return; } - return false; + assert(output->bar->xdg_output_manager != NULL); + output->xdg_output = zxdg_output_manager_v1_get_xdg_output( + output->bar->xdg_output_manager, output->output); + zxdg_output_v1_add_listener(output->xdg_output, &xdg_output_listener, + output); } static void handle_global(void *data, struct wl_registry *registry, @@ -386,7 +445,10 @@ static void handle_global(void *data, struct wl_registry *registry, output->wl_name = name; wl_list_init(&output->workspaces); wl_list_init(&output->hotspots); - wl_list_insert(&bar->outputs, &output->link); + wl_list_init(&output->link); + if (bar->xdg_output_manager != NULL) { + add_xdg_output(output); + } } else if (strcmp(interface, zwlr_layer_shell_v1_interface.name) == 0) { bar->layer_shell = wl_registry_bind( registry, name, &zwlr_layer_shell_v1_interface, 1); @@ -413,21 +475,23 @@ static const struct wl_registry_listener registry_listener = { .global_remove = handle_global_remove, }; -static void render_all_frames(struct swaybar *bar) { +static void set_bar_dirty(struct swaybar *bar) { struct swaybar_output *output; wl_list_for_each(output, &bar->outputs, link) { - render_frame(bar, output); + set_output_dirty(output); } } -void bar_setup(struct swaybar *bar, +bool bar_setup(struct swaybar *bar, const char *socket_path, const char *bar_id) { bar_init(bar); init_event_loop(); bar->ipc_socketfd = ipc_open_socket(socket_path); bar->ipc_event_socketfd = ipc_open_socket(socket_path); - ipc_initialize(bar, bar_id); + if (!ipc_initialize(bar, bar_id)) { + return false; + } if (bar->config->status_command) { bar->status = status_line_init(bar->config->status_command); } @@ -443,23 +507,10 @@ void bar_setup(struct swaybar *bar, struct swaybar_output *output; wl_list_for_each(output, &bar->outputs, link) { - output->xdg_output = zxdg_output_manager_v1_get_xdg_output( - bar->xdg_output_manager, output->output); - zxdg_output_v1_add_listener(output->xdg_output, &xdg_output_listener, - output); + add_xdg_output(output); } wl_display_roundtrip(bar->display); - struct swaybar_output *output_tmp; - wl_list_for_each_safe(output, output_tmp, &bar->outputs, link) { - if (!bar_uses_output(bar, output->name)) { - zxdg_output_v1_destroy(output->xdg_output); - wl_output_destroy(output->output); - wl_list_remove(&output->link); - free(output); - } - } - struct swaybar_pointer *pointer = &bar->pointer; int max_scale = 1; @@ -479,20 +530,9 @@ void bar_setup(struct swaybar *bar, pointer->cursor_surface = wl_compositor_create_surface(bar->compositor); assert(pointer->cursor_surface); - wl_list_for_each(output, &bar->outputs, link) { - output->surface = wl_compositor_create_surface(bar->compositor); - assert(output->surface); - output->layer_surface = zwlr_layer_shell_v1_get_layer_surface( - bar->layer_shell, output->surface, output->output, - ZWLR_LAYER_SHELL_V1_LAYER_BOTTOM, "panel"); - assert(output->layer_surface); - zwlr_layer_surface_v1_add_listener(output->layer_surface, - &layer_surface_listener, output); - zwlr_layer_surface_v1_set_anchor(output->layer_surface, - bar->config->position); - } ipc_get_workspaces(bar); - render_all_frames(bar); + set_bar_dirty(bar); + return true; } static void display_in(int fd, short mask, void *data) { @@ -506,7 +546,7 @@ static void display_in(int fd, short mask, void *data) { static void ipc_in(int fd, short mask, void *data) { struct swaybar *bar = data; if (handle_ipc_readable(bar)) { - render_all_frames(bar); + set_bar_dirty(bar); } } @@ -514,10 +554,10 @@ static void status_in(int fd, short mask, void *data) { struct swaybar *bar = data; if (mask & (POLLHUP | POLLERR)) { status_error(bar->status, "[error reading from status command]"); - render_all_frames(bar); + set_bar_dirty(bar); remove_event(fd); } else if (status_handle_readable(bar->status)) { - render_all_frames(bar); + set_bar_dirty(bar); } } @@ -529,6 +569,7 @@ void bar_run(struct swaybar *bar) { } while (1) { event_loop_poll(); + wl_display_flush(bar->display); } } diff --git a/swaybar/config.c b/swaybar/config.c index db7b0db6..4e851cca 100644 --- a/swaybar/config.c +++ b/swaybar/config.c @@ -22,7 +22,7 @@ uint32_t parse_position(const char *position) { } } -struct swaybar_config *init_config() { +struct swaybar_config *init_config(void) { struct swaybar_config *config = calloc(1, sizeof(struct swaybar_config)); config->status_command = NULL; config->pango_markup = false; diff --git a/swaybar/i3bar.c b/swaybar/i3bar.c index 88404703..8e9b038b 100644 --- a/swaybar/i3bar.c +++ b/swaybar/i3bar.c @@ -6,7 +6,9 @@ #include <string.h> #include <unistd.h> #include <wlr/util/log.h> +#include "swaybar/bar.h" #include "swaybar/config.h" +#include "swaybar/i3bar.h" #include "swaybar/status_line.h" void i3bar_block_unref(struct i3bar_block *block) { @@ -117,7 +119,9 @@ bool i3bar_handle_readable(struct status_line *status) { memmove(status->buffer, &status->buffer[c], status->buffer_index); break; } else if (!isspace(status->buffer[c])) { - status_error(status, "[invalid json]"); + wlr_log(WLR_DEBUG, "Invalid i3bar json: expected '[' but encountered '%c'", + status->buffer[c]); + status_error(status, "[invalid i3bar json]"); return true; } } @@ -155,6 +159,8 @@ bool i3bar_handle_readable(struct status_line *status) { ++buffer_pos; break; } else if (!isspace(status->buffer[buffer_pos])) { + wlr_log(WLR_DEBUG, "Invalid i3bar json: expected ',' but encountered '%c'", + status->buffer[buffer_pos]); status_error(status, "[invalid i3bar json]"); return true; } @@ -166,7 +172,8 @@ bool i3bar_handle_readable(struct status_line *status) { } else { test_object = json_tokener_parse_ex(status->tokener, &status->buffer[buffer_pos], status->buffer_index - buffer_pos); - if (json_tokener_get_error(status->tokener) == json_tokener_success) { + enum json_tokener_error err = json_tokener_get_error(status->tokener); + if (err == json_tokener_success) { if (json_object_get_type(test_object) == json_type_array) { if (last_object) { json_object_put(last_object); @@ -198,12 +205,14 @@ bool i3bar_handle_readable(struct status_line *status) { continue; // look for comma without reading more input } buffer_pos = status->buffer_index = 0; - } else if (json_tokener_get_error(status->tokener) == json_tokener_continue) { + } else if (err == json_tokener_continue) { + json_tokener_reset(status->tokener); if (status->buffer_index < status->buffer_size) { // move the object to the start of the buffer status->buffer_index -= buffer_pos; memmove(status->buffer, &status->buffer[buffer_pos], status->buffer_index); + buffer_pos = 0; } else { // expand buffer status->buffer_size *= 2; @@ -217,6 +226,10 @@ bool i3bar_handle_readable(struct status_line *status) { } } } else { + char last_char = status->buffer[status->buffer_index - 1]; + status->buffer[status->buffer_index - 1] = '\0'; + wlr_log(WLR_DEBUG, "Failed to parse i3bar json - %s: '%s%c'", + json_tokener_error_desc(err), &status->buffer[buffer_pos], last_char); status_error(status, "[failed to parse i3bar json]"); return true; } diff --git a/swaybar/ipc.c b/swaybar/ipc.c index 0e60c10c..7c53a44f 100644 --- a/swaybar/ipc.c +++ b/swaybar/ipc.c @@ -141,9 +141,16 @@ static void ipc_parse_colors( } } -static void ipc_parse_config( +static bool ipc_parse_config( struct swaybar_config *config, const char *payload) { json_object *bar_config = json_tokener_parse(payload); + json_object *success; + if (json_object_object_get_ex(bar_config, "success", &success) + && !json_object_get_boolean(success)) { + wlr_log(WLR_ERROR, "No bar with that ID. Use 'swaymsg -t get_bar_config to get the available bar configs."); + json_object_put(bar_config); + return false; + } json_object *markup, *mode, *hidden_bar, *position, *status_command; json_object *font, *bar_height, *wrap_scroll, *workspace_buttons, *strip_workspace_numbers; json_object *binding_mode_indicator, *verbose, *colors, *sep_symbol, *outputs; @@ -226,10 +233,10 @@ static void ipc_parse_config( } json_object_put(bar_config); + return true; } void ipc_get_workspaces(struct swaybar *bar) { - bar->focused_output = NULL; struct swaybar_output *output; wl_list_for_each(output, &bar->outputs, link) { free_workspaces(&output->workspaces); @@ -312,11 +319,14 @@ static void ipc_get_outputs(struct swaybar *bar) { free(res); } -void ipc_initialize(struct swaybar *bar, const char *bar_id) { +bool ipc_initialize(struct swaybar *bar, const char *bar_id) { uint32_t len = strlen(bar_id); char *res = ipc_single_command(bar->ipc_socketfd, IPC_GET_BAR_CONFIG, bar_id, &len); - ipc_parse_config(bar->config, res); + if (!ipc_parse_config(bar->config, res)) { + free(res); + return false; + } free(res); ipc_get_outputs(bar); @@ -324,6 +334,7 @@ void ipc_initialize(struct swaybar *bar, const char *bar_id) { len = strlen(subscribe); free(ipc_single_command(bar->ipc_event_socketfd, IPC_SUBSCRIBE, subscribe, &len)); + return true; } bool handle_ipc_readable(struct swaybar *bar) { diff --git a/swaybar/main.c b/swaybar/main.c index 60e4b37c..d2c579db 100644 --- a/swaybar/main.c +++ b/swaybar/main.c @@ -96,7 +96,10 @@ int main(int argc, char **argv) { signal(SIGTERM, sig_handler); - bar_setup(&swaybar, socket_path, bar_id); + if (!bar_setup(&swaybar, socket_path, bar_id)) { + free(socket_path); + return 1; + } free(socket_path); free(bar_id); diff --git a/swaybar/meson.build b/swaybar/meson.build index d65edb11..7a02a33f 100644 --- a/swaybar/meson.build +++ b/swaybar/meson.build @@ -24,5 +24,6 @@ executable( wlroots, ], link_with: [lib_sway_common, lib_sway_client], + install_rpath : rpathdir, install: true ) diff --git a/swaybar/render.c b/swaybar/render.c index 97690338..dc31a5ea 100644 --- a/swaybar/render.c +++ b/swaybar/render.c @@ -1,4 +1,5 @@ #define _POSIX_C_SOURCE 200809L +#include <assert.h> #include <limits.h> #include <stdlib.h> #include <stdint.h> @@ -9,6 +10,7 @@ #include "pool-buffer.h" #include "swaybar/bar.h" #include "swaybar/config.h" +#include "swaybar/i3bar.h" #include "swaybar/ipc.h" #include "swaybar/render.h" #include "swaybar/status_line.h" @@ -19,47 +21,47 @@ static const double WS_VERTICAL_PADDING = 1.5; static const double BORDER_WIDTH = 1; static uint32_t render_status_line_error(cairo_t *cairo, - struct swaybar_output *output, struct swaybar_config *config, - const char *error, double *x, uint32_t surface_height) { + struct swaybar_output *output, double *x) { + const char *error = output->bar->status->text; if (!error) { return 0; } - uint32_t height = surface_height * output->scale; + uint32_t height = output->height * output->scale; cairo_set_source_u32(cairo, 0xFF0000FF); int margin = 3 * output->scale; int ws_vertical_padding = WS_VERTICAL_PADDING * output->scale; + char *font = output->bar->config->font; int text_width, text_height; - get_text_size(cairo, config->font, &text_width, &text_height, NULL, + get_text_size(cairo, font, &text_width, &text_height, NULL, output->scale, false, "%s", error); uint32_t ideal_height = text_height + ws_vertical_padding * 2; uint32_t ideal_surface_height = ideal_height / output->scale; - if (surface_height < ideal_surface_height) { + if (output->height < ideal_surface_height) { return ideal_surface_height; } *x -= text_width + margin; double text_y = height / 2.0 - text_height / 2.0; cairo_move_to(cairo, *x, (int)floor(text_y)); - pango_printf(cairo, config->font, output->scale, false, "%s", error); + pango_printf(cairo, font, output->scale, false, "%s", error); *x -= margin; - return surface_height; + return output->height; } static uint32_t render_status_line_text(cairo_t *cairo, - struct swaybar_output *output, struct swaybar_config *config, - const char *text, bool focused, double *x, uint32_t surface_height) { + struct swaybar_output *output, double *x) { + const char *text = output->bar->status->text; if (!text) { return 0; } - uint32_t height = surface_height * output->scale; - - cairo_set_source_u32(cairo, focused ? + struct swaybar_config *config = output->bar->config; + cairo_set_source_u32(cairo, output->focused ? config->colors.focused_statusline : config->colors.statusline); int text_width, text_height; @@ -71,17 +73,18 @@ static uint32_t render_status_line_text(cairo_t *cairo, uint32_t ideal_height = text_height + ws_vertical_padding * 2; uint32_t ideal_surface_height = ideal_height / output->scale; - if (surface_height < ideal_surface_height) { + if (output->height < ideal_surface_height) { return ideal_surface_height; } *x -= text_width + margin; + uint32_t height = output->height * output->scale; double text_y = height / 2.0 - text_height / 2.0; cairo_move_to(cairo, *x, (int)floor(text_y)); pango_printf(cairo, config->font, output->scale, config->pango_markup, "%s", text); *x -= margin; - return surface_height; + return output->height; } static void render_sharp_line(cairo_t *cairo, uint32_t color, @@ -121,12 +124,11 @@ static void i3bar_block_unref_callback(void *data) { static uint32_t render_status_block(cairo_t *cairo, struct swaybar_output *output, struct i3bar_block *block, double *x, - uint32_t surface_height, bool focused, bool edge) { + bool edge) { if (!block->full_text || !*block->full_text) { return 0; } - uint32_t height = surface_height * output->scale; struct swaybar_config *config = output->bar->config; int text_width, text_height; @@ -144,7 +146,7 @@ static uint32_t render_status_block(cairo_t *cairo, double block_width = width; uint32_t ideal_height = text_height + ws_vertical_padding * 2; uint32_t ideal_surface_height = ideal_height / output->scale; - if (surface_height < ideal_surface_height) { + if (output->height < ideal_surface_height) { return ideal_surface_height; } @@ -165,7 +167,7 @@ static uint32_t render_status_block(cairo_t *cairo, output->scale, false, "%s", config->sep_symbol); uint32_t _ideal_height = sep_height + ws_vertical_padding * 2; uint32_t _ideal_surface_height = _ideal_height / output->scale; - if (surface_height < _ideal_surface_height) { + if (output->height < _ideal_surface_height) { return _ideal_surface_height; } if (sep_width > block->separator_block_width) { @@ -177,6 +179,7 @@ static uint32_t render_status_block(cairo_t *cairo, *x -= margin; } + uint32_t height = output->height * output->scale; if (output->bar->status->click_events) { struct swaybar_hotspot *hotspot = calloc(1, sizeof(struct swaybar_hotspot)); hotspot->x = *x; @@ -240,7 +243,7 @@ static uint32_t render_status_block(cairo_t *cairo, } if (!edge && block->separator) { - if (focused) { + if (output->focused) { cairo_set_source_u32(cairo, config->colors.focused_separator); } else { cairo_set_source_u32(cairo, config->colors.separator); @@ -259,19 +262,16 @@ static uint32_t render_status_block(cairo_t *cairo, cairo_stroke(cairo); } } - return surface_height; + return output->height; } static uint32_t render_status_line_i3bar(cairo_t *cairo, - struct swaybar_config *config, struct swaybar_output *output, - struct status_line *status, bool focused, - double *x, uint32_t surface_height) { + struct swaybar_output *output, double *x) { uint32_t max_height = 0; bool edge = true; struct i3bar_block *block; - wl_list_for_each(block, &status->blocks, link) { - uint32_t h = render_status_block(cairo, output, - block, x, surface_height, focused, edge); + wl_list_for_each(block, &output->bar->status->blocks, link) { + uint32_t h = render_status_block(cairo, output, block, x, edge); max_height = h > max_height ? h : max_height; edge = false; } @@ -279,19 +279,15 @@ static uint32_t render_status_line_i3bar(cairo_t *cairo, } static uint32_t render_status_line(cairo_t *cairo, - struct swaybar_config *config, struct swaybar_output *output, - struct status_line *status, bool focused, - double *x, uint32_t surface_height) { + struct swaybar_output *output, double *x) { + struct status_line *status = output->bar->status; switch (status->protocol) { case PROTOCOL_ERROR: - return render_status_line_error(cairo, output, config, - status->text, x, surface_height); + return render_status_line_error(cairo, output, x); case PROTOCOL_TEXT: - return render_status_line_text(cairo, output, config, - status->text, focused, x, surface_height); + return render_status_line_text(cairo, output, x); case PROTOCOL_I3BAR: - return render_status_line_i3bar(cairo, config, output, - status, focused, x, surface_height); + return render_status_line_i3bar(cairo, output, x); case PROTOCOL_UNDEF: return 0; } @@ -299,10 +295,9 @@ static uint32_t render_status_line(cairo_t *cairo, } static uint32_t render_binding_mode_indicator(cairo_t *cairo, - struct swaybar_output *output, struct swaybar_config *config, - const char *mode, double x, uint32_t surface_height) { - uint32_t height = surface_height * output->scale; - + struct swaybar_output *output, double x) { + struct swaybar_config *config = output->bar->config; + const char *mode = config->mode; int text_width, text_height; get_text_size(cairo, config->font, &text_width, &text_height, NULL, output->scale, config->mode_pango_markup, @@ -315,11 +310,12 @@ static uint32_t render_binding_mode_indicator(cairo_t *cairo, uint32_t ideal_height = text_height + ws_vertical_padding * 2 + border_width * 2; uint32_t ideal_surface_height = ideal_height / output->scale; - if (surface_height < ideal_surface_height) { + if (output->height < ideal_surface_height) { return ideal_surface_height; } uint32_t width = text_width + ws_horizontal_padding * 2 + border_width * 2; + uint32_t height = output->height * output->scale; cairo_set_source_u32(cairo, config->colors.binding_mode.background); cairo_rectangle(cairo, x, 0, width, height); cairo_fill(cairo); @@ -339,7 +335,7 @@ static uint32_t render_binding_mode_indicator(cairo_t *cairo, cairo_move_to(cairo, x + width / 2 - text_width / 2, (int)floor(text_y)); pango_printf(cairo, config->font, output->scale, config->mode_pango_markup, "%s", mode); - return surface_height; + return output->height; } static const char *strip_workspace_number(const char *ws_name) { @@ -365,8 +361,9 @@ static enum hotspot_event_handling workspace_hotspot_callback(struct swaybar_out } static uint32_t render_workspace_button(cairo_t *cairo, - struct swaybar_output *output, struct swaybar_config *config, - struct swaybar_workspace *ws, double *x, uint32_t surface_height) { + struct swaybar_output *output, + struct swaybar_workspace *ws, double *x) { + struct swaybar_config *config = output->bar->config; const char *name = ws->name; if (config->strip_workspace_numbers) { name = strip_workspace_number(ws->name); @@ -383,7 +380,7 @@ static uint32_t render_workspace_button(cairo_t *cairo, box_colors = config->colors.inactive_workspace; } - uint32_t height = surface_height * output->scale; + uint32_t height = output->height * output->scale; int text_width, text_height; get_text_size(cairo, config->font, &text_width, &text_height, NULL, @@ -396,7 +393,7 @@ static uint32_t render_workspace_button(cairo_t *cairo, uint32_t ideal_height = ws_vertical_padding * 2 + text_height + border_width * 2; uint32_t ideal_surface_height = ideal_height / output->scale; - if (surface_height < ideal_surface_height) { + if (output->height < ideal_surface_height) { return ideal_surface_height; } @@ -433,11 +430,11 @@ static uint32_t render_workspace_button(cairo_t *cairo, wl_list_insert(&output->hotspots, &hotspot->link); *x += width; - return surface_height; + return output->height; } -static uint32_t render_to_cairo(cairo_t *cairo, - struct swaybar *bar, struct swaybar_output *output) { +static uint32_t render_to_cairo(cairo_t *cairo, struct swaybar_output *output) { + struct swaybar *bar = output->bar; struct swaybar_config *config = bar->config; cairo_set_operator(cairo, CAIRO_OPERATOR_SOURCE); if (output->focused) { @@ -457,29 +454,43 @@ static uint32_t render_to_cairo(cairo_t *cairo, */ double x = output->width * output->scale; if (bar->status) { - uint32_t h = render_status_line(cairo, config, output, - bar->status, output->focused, &x, output->height); + uint32_t h = render_status_line(cairo, output, &x); max_height = h > max_height ? h : max_height; } x = 0; if (config->workspace_buttons) { struct swaybar_workspace *ws; wl_list_for_each_reverse(ws, &output->workspaces, link) { - uint32_t h = render_workspace_button(cairo, - output, config, ws, &x, output->height); + uint32_t h = render_workspace_button(cairo, output, ws, &x); max_height = h > max_height ? h : max_height; } } if (config->binding_mode_indicator && config->mode) { - uint32_t h = render_binding_mode_indicator(cairo, - output, config, config->mode, x, output->height); + uint32_t h = render_binding_mode_indicator(cairo, output, x); max_height = h > max_height ? h : max_height; } return max_height > output->height ? max_height : output->height; } -void render_frame(struct swaybar *bar, struct swaybar_output *output) { +static void output_frame_handle_done(void *data, struct wl_callback *callback, + uint32_t time) { + wl_callback_destroy(callback); + struct swaybar_output *output = data; + output->frame_scheduled = false; + if (output->dirty) { + render_frame(output); + output->dirty = false; + } +} + +static const struct wl_callback_listener output_frame_listener = { + .done = output_frame_handle_done +}; + +void render_frame(struct swaybar_output *output) { + assert(output->surface != NULL); + struct swaybar_hotspot *hotspot, *tmp; wl_list_for_each_safe(hotspot, tmp, &output->hotspots, link) { if (hotspot->destroy) { @@ -492,13 +503,21 @@ void render_frame(struct swaybar *bar, struct swaybar_output *output) { cairo_surface_t *recorder = cairo_recording_surface_create( CAIRO_CONTENT_COLOR_ALPHA, NULL); cairo_t *cairo = cairo_create(recorder); + cairo_set_antialias(cairo, CAIRO_ANTIALIAS_BEST); + cairo_font_options_t *fo = cairo_font_options_create(); + cairo_font_options_set_hint_style(fo, CAIRO_HINT_STYLE_FULL); + cairo_font_options_set_antialias(fo, CAIRO_ANTIALIAS_SUBPIXEL); + cairo_font_options_set_subpixel_order(fo, to_cairo_subpixel_order(output->subpixel)); + cairo_set_font_options(cairo, fo); + cairo_font_options_destroy(fo); cairo_save(cairo); cairo_set_operator(cairo, CAIRO_OPERATOR_CLEAR); cairo_paint(cairo); cairo_restore(cairo); - uint32_t height = render_to_cairo(cairo, bar, output); - if (bar->config->height >= 0 && height < (uint32_t)bar->config->height) { - height = bar->config->height; + uint32_t height = render_to_cairo(cairo, output); + int config_height = output->bar->config->height; + if (config_height >= 0 && height < (uint32_t)config_height) { + height = config_height; } if (height != output->height) { // Reconfigure surface @@ -507,14 +526,15 @@ void render_frame(struct swaybar *bar, struct swaybar_output *output) { // TODO: this could infinite loop if the compositor assigns us a // different height than what we asked for wl_surface_commit(output->surface); - wl_display_roundtrip(bar->display); } else if (height > 0) { // Replay recording into shm and send it off - output->current_buffer = get_next_buffer(bar->shm, + output->current_buffer = get_next_buffer(output->bar->shm, output->buffers, output->width * output->scale, output->height * output->scale); if (!output->current_buffer) { + cairo_surface_destroy(recorder); + cairo_destroy(cairo); return; } cairo_t *shm = output->current_buffer->cairo; @@ -532,8 +552,12 @@ void render_frame(struct swaybar *bar, struct swaybar_output *output) { output->current_buffer->buffer, 0, 0); wl_surface_damage(output->surface, 0, 0, output->width, output->height); + + struct wl_callback *frame_callback = wl_surface_frame(output->surface); + wl_callback_add_listener(frame_callback, &output_frame_listener, output); + output->frame_scheduled = true; + wl_surface_commit(output->surface); - wl_display_roundtrip(bar->display); } cairo_surface_destroy(recorder); cairo_destroy(cairo); diff --git a/swaybar/status_line.c b/swaybar/status_line.c index 401bf6f6..ed6dc7c8 100644 --- a/swaybar/status_line.c +++ b/swaybar/status_line.c @@ -1,12 +1,15 @@ #define _POSIX_C_SOURCE 200809L #include <fcntl.h> +#include <sys/ioctl.h> #include <json-c/json.h> #include <stdlib.h> #include <string.h> #include <stdio.h> #include <unistd.h> #include <wlr/util/log.h> +#include "swaybar/bar.h" #include "swaybar/config.h" +#include "swaybar/i3bar.h" #include "swaybar/event_loop.h" #include "swaybar/status_line.h" #include "readline.h" @@ -34,18 +37,35 @@ bool status_handle_readable(struct status_line *status) { switch (status->protocol) { case PROTOCOL_UNDEF: errno = 0; - read_bytes = getline(&status->buffer, - &status->buffer_size, status->read); - if (errno == EAGAIN) { - clearerr(status->read); - } else if (errno) { + int available_bytes; + if (ioctl(status->read_fd, FIONREAD, &available_bytes) == -1) { + wlr_log(WLR_ERROR, "Unable to read status command output size"); status_error(status, "[error reading from status command]"); return true; } + if ((size_t)available_bytes + 1 > status->buffer_size) { + // need room for leading '\0' too + status->buffer_size = available_bytes + 1; + status->buffer = realloc(status->buffer, status->buffer_size); + } + if (status->buffer == NULL) { + wlr_log_errno(WLR_ERROR, "Unable to read status line"); + status_error(status, "[error reading from status command]"); + return true; + } + + read_bytes = read(status->read_fd, status->buffer, available_bytes); + if (read_bytes != available_bytes) { + status_error(status, "[error reading from status command]"); + return true; + } + status->buffer[available_bytes] = 0; + // the header must be sent completely the first time round + char *newline = strchr(status->buffer, '\n'); json_object *header, *version; - if (status->buffer[read_bytes - 1] == '\n' + if (newline != NULL && (header = json_tokener_parse(status->buffer)) && json_object_object_get_ex(header, "version", &version) && json_object_get_int(version) == 1) { @@ -67,8 +87,8 @@ bool status_handle_readable(struct status_line *status) { wl_list_init(&status->blocks); status->tokener = json_tokener_new(); - status->buffer_index = getdelim(&status->buffer, - &status->buffer_size, EOF, status->read); + status->buffer_index = strlen(newline + 1); + memmove(status->buffer, newline + 1, status->buffer_index + 1); return i3bar_handle_readable(status); } |