From 260595076977729bcaf9aadcfbbc8c5f269c6387 Mon Sep 17 00:00:00 2001 From: Drew DeVault Date: Tue, 3 Apr 2018 21:06:28 -0400 Subject: Add hidpi support to swaybar --- swaybar/bar.c | 73 +++++++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 63 insertions(+), 10 deletions(-) (limited to 'swaybar/bar.c') diff --git a/swaybar/bar.c b/swaybar/bar.c index fb417095..cf812a60 100644 --- a/swaybar/bar.c +++ b/swaybar/bar.c @@ -69,11 +69,19 @@ static void wl_pointer_enter(void *data, struct wl_pointer *wl_pointer, break; } } + 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; + } + } + wl_surface_set_buffer_scale(pointer->cursor_surface, max_scale); wl_surface_attach(pointer->cursor_surface, wl_cursor_image_get_buffer(pointer->cursor_image), 0, 0); wl_pointer_set_cursor(wl_pointer, serial, pointer->cursor_surface, - pointer->cursor_image->hotspot_x, - pointer->cursor_image->hotspot_y); + pointer->cursor_image->hotspot_x / max_scale, + pointer->cursor_image->hotspot_y / max_scale); wl_surface_commit(pointer->cursor_surface); } @@ -103,10 +111,12 @@ static void wl_pointer_button(void *data, struct wl_pointer *wl_pointer, } struct swaybar_hotspot *hotspot; wl_list_for_each(hotspot, &output->hotspots, link) { - if (pointer->x >= hotspot->x - && pointer->y >= hotspot->y - && pointer->x < hotspot->x + hotspot->width - && pointer->y < hotspot->y + hotspot->height) { + double x = pointer->x * output->scale; + double y = pointer->y * output->scale; + if (x >= hotspot->x + && y >= hotspot->y + && x < hotspot->x + hotspot->width + && y < hotspot->y + hotspot->height) { hotspot->callback(output, pointer->x, pointer->y, button, hotspot->data); } @@ -197,12 +207,43 @@ const struct wl_seat_listener seat_listener = { .name = seat_handle_name, }; +static void output_geometry(void *data, struct wl_output *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 +} + +static void output_mode(void *data, struct wl_output *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_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 = { + .geometry = output_geometry, + .mode = output_mode, + .done = output_done, + .scale = output_scale, +}; + static void handle_global(void *data, struct wl_registry *registry, uint32_t name, const char *interface, uint32_t version) { struct swaybar *bar = data; if (strcmp(interface, wl_compositor_interface.name) == 0) { bar->compositor = wl_registry_bind(registry, name, - &wl_compositor_interface, 1); + &wl_compositor_interface, 3); } else if (strcmp(interface, wl_seat_interface.name) == 0) { bar->seat = wl_registry_bind(registry, name, &wl_seat_interface, 1); @@ -216,7 +257,9 @@ static void handle_global(void *data, struct wl_registry *registry, calloc(1, sizeof(struct swaybar_output)); output->bar = bar; output->output = wl_registry_bind(registry, name, - &wl_output_interface, 1); + &wl_output_interface, 3); + wl_output_add_listener(output->output, &output_listener, output); + output->scale = 1; output->index = index++; wl_list_init(&output->workspaces); wl_list_init(&output->hotspots); @@ -262,9 +305,20 @@ void bar_setup(struct swaybar *bar, wl_registry_add_listener(registry, ®istry_listener, bar); wl_display_roundtrip(bar->display); assert(bar->compositor && bar->layer_shell && bar->shm); + wl_display_roundtrip(bar->display); + struct swaybar_pointer *pointer = &bar->pointer; - assert(pointer->cursor_theme = wl_cursor_theme_load(NULL, 16, bar->shm)); + 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; + } + } + + assert(pointer->cursor_theme = wl_cursor_theme_load( + NULL, 16 * (max_scale * 2), bar->shm)); struct wl_cursor *cursor; assert(cursor = wl_cursor_theme_get_cursor( pointer->cursor_theme, "left_ptr")); @@ -273,7 +327,6 @@ void bar_setup(struct swaybar *bar, wl_compositor_create_surface(bar->compositor)); // TODO: we might not necessarily be meant to do all of the outputs - struct swaybar_output *output; wl_list_for_each(output, &bar->outputs, link) { struct config_output *coutput; wl_list_for_each(coutput, &bar->config->outputs, link) { -- cgit v1.2.3 From d48e7036aa04f8503737de678519cc389058b259 Mon Sep 17 00:00:00 2001 From: Drew DeVault Date: Tue, 3 Apr 2018 21:29:43 -0400 Subject: Don't use asserts with side-effects --- swaybar/bar.c | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) (limited to 'swaybar/bar.c') diff --git a/swaybar/bar.c b/swaybar/bar.c index cf812a60..b617f9ab 100644 --- a/swaybar/bar.c +++ b/swaybar/bar.c @@ -299,7 +299,8 @@ void bar_setup(struct swaybar *bar, bar->status = status_line_init(bar->config->status_command); } - assert(bar->display = wl_display_connect(NULL)); + bar->display = wl_display_connect(NULL); + assert(bar->display); struct wl_registry *registry = wl_display_get_registry(bar->display); wl_registry_add_listener(registry, ®istry_listener, bar); @@ -317,14 +318,15 @@ void bar_setup(struct swaybar *bar, } } - assert(pointer->cursor_theme = wl_cursor_theme_load( - NULL, 16 * (max_scale * 2), bar->shm)); + pointer->cursor_theme = wl_cursor_theme_load( + NULL, 16 * (max_scale * 2), bar->shm); + assert(pointer->cursor_theme); struct wl_cursor *cursor; - assert(cursor = wl_cursor_theme_get_cursor( - pointer->cursor_theme, "left_ptr")); + cursor = wl_cursor_theme_get_cursor(pointer->cursor_theme, "left_ptr"); + assert(cursor); pointer->cursor_image = cursor->images[0]; - assert(pointer->cursor_surface = - wl_compositor_create_surface(bar->compositor)); + pointer->cursor_surface = wl_compositor_create_surface(bar->compositor); + assert(pointer->cursor_surface); // TODO: we might not necessarily be meant to do all of the outputs wl_list_for_each(output, &bar->outputs, link) { @@ -334,8 +336,8 @@ void bar_setup(struct swaybar *bar, continue; } output->name = strdup(coutput->name); - assert(output->surface = wl_compositor_create_surface( - bar->compositor)); + 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"); -- cgit v1.2.3 From 38bdd4bdebc4938f25af90bc4f79e278f61808e0 Mon Sep 17 00:00:00 2001 From: Drew DeVault Date: Tue, 3 Apr 2018 22:52:40 -0400 Subject: Address review feedback --- swaybar/bar.c | 2 +- swaybar/render.c | 23 +++++++++++------------ 2 files changed, 12 insertions(+), 13 deletions(-) (limited to 'swaybar/bar.c') diff --git a/swaybar/bar.c b/swaybar/bar.c index b617f9ab..ea0141cc 100644 --- a/swaybar/bar.c +++ b/swaybar/bar.c @@ -319,7 +319,7 @@ void bar_setup(struct swaybar *bar, } pointer->cursor_theme = wl_cursor_theme_load( - NULL, 16 * (max_scale * 2), bar->shm); + 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"); diff --git a/swaybar/render.c b/swaybar/render.c index 1f84fdba..be58301d 100644 --- a/swaybar/render.c +++ b/swaybar/render.c @@ -20,7 +20,7 @@ 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 width, uint32_t surface_height) { + const char *error, double *x, uint32_t surface_height) { if (!error) { return 0; } @@ -51,8 +51,7 @@ static uint32_t render_status_line_error(cairo_t *cairo, 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 width, uint32_t surface_height) { + const char *text, bool focused, double *x, uint32_t surface_height) { if (!text) { return 0; } @@ -159,7 +158,7 @@ static uint32_t render_status_block(cairo_t *cairo, get_text_size(cairo, config->font, &sep_width, &_height, output->scale, false, "%s", config->sep_symbol); uint32_t _ideal_height = _height + ws_vertical_padding * 2; - if (height < _ideal_height / output->scale) { + if ((uint32_t)_height < _ideal_height / output->scale) { return _height / output->scale; } if (sep_width > block->separator_block_width) { @@ -256,7 +255,7 @@ static uint32_t render_status_block(cairo_t *cairo, 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 width, uint32_t surface_height) { + double *x, uint32_t surface_height) { uint32_t max_height = 0; bool edge = true; struct i3bar_block *block; @@ -272,17 +271,17 @@ 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 width, uint32_t surface_height) { + double *x, uint32_t surface_height) { switch (status->protocol) { case PROTOCOL_ERROR: return render_status_line_error(cairo, output, config, - status->text, x, width, surface_height); + status->text, x, surface_height); case PROTOCOL_TEXT: return render_status_line_text(cairo, output, config, - status->text, focused, x, width, surface_height); + status->text, focused, x, surface_height); case PROTOCOL_I3BAR: return render_status_line_i3bar(cairo, config, output, - status, focused, x, width, surface_height); + status, focused, x, surface_height); case PROTOCOL_UNDEF: return 0; } @@ -368,7 +367,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 = surface_height * output->scale; int text_width, text_height; get_text_size(cairo, config->font, &text_width, &text_height, @@ -442,8 +441,8 @@ 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->width, output->height); + uint32_t h = render_status_line(cairo, config, output, + bar->status, output->focused, &x, output->height); max_height = h > max_height ? h : max_height; } x = 0; -- cgit v1.2.3