Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add hidpi support to swaybar #1722

Merged
merged 4 commits into from
Apr 4, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions include/swaybar/bar.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ struct swaybar_output {
bool focused;

uint32_t width, height;
int32_t scale;
struct pool_buffer buffers[2];
struct pool_buffer *current_buffer;
};
Expand Down
73 changes: 63 additions & 10 deletions swaybar/bar.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -262,9 +305,20 @@ void bar_setup(struct swaybar *bar,
wl_registry_add_listener(registry, &registry_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(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You shouldn't put a side effect in an assertion.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do this all the time, it's all over sway et al. This is fine.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nevermind duh I see why

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pushed fix to this file but will file a follow-up ticket to go fix my fuck-ups elsewhere

NULL, 16 * (max_scale * 2), bar->shm));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

* 2?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

x cursor themes are weird, I dunno

struct wl_cursor *cursor;
assert(cursor = wl_cursor_theme_get_cursor(
pointer->cursor_theme, "left_ptr"));
Expand All @@ -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) {
Expand Down
Loading