Add client support for HiDPI

This adds HiDPI support to swaybar, swaybg, and swaylock.
This commit is contained in:
Drew DeVault 2016-09-05 11:36:48 -04:00
parent 61184e3208
commit b2226ac655
9 changed files with 96 additions and 61 deletions

View file

@ -50,8 +50,10 @@ static const struct wl_buffer_listener buffer_listener = {
};
static struct buffer *create_buffer(struct window *window, struct buffer *buf,
int32_t width, int32_t height, uint32_t format) {
int32_t width, int32_t height, int32_t scale, uint32_t format) {
width *= scale;
height *= scale;
uint32_t stride = width * 4;
uint32_t size = stride * height;
@ -63,7 +65,8 @@ static struct buffer *create_buffer(struct window *window, struct buffer *buf,
}
void *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
struct wl_shm_pool *pool = wl_shm_create_pool(window->registry->shm, fd, size);
buf->buffer = wl_shm_pool_create_buffer(pool, 0, width, height, stride, format);
buf->buffer = wl_shm_pool_create_buffer(pool, 0,
width, height, stride, format);
wl_shm_pool_destroy(pool);
close(fd);
unlink(name);
@ -72,10 +75,10 @@ static struct buffer *create_buffer(struct window *window, struct buffer *buf,
buf->width = width;
buf->height = height;
buf->surface = cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32, width, height, stride);
buf->surface = cairo_image_surface_create_for_data(data,
CAIRO_FORMAT_ARGB32, width, height, stride);
buf->cairo = cairo_create(buf->surface);
buf->pango = pango_cairo_create_context(buf->cairo);
pango_cairo_context_set_resolution(buf->pango, 96 * 2);
wl_buffer_add_listener(buf->buffer, &buffer_listener, buf);
return buf;
@ -114,7 +117,9 @@ struct buffer *get_next_buffer(struct window *window) {
}
if (!buffer->buffer) {
if (!create_buffer(window, buffer, window->width, window->height, WL_SHM_FORMAT_ARGB8888)) {
if (!create_buffer(window, buffer,
window->width, window->height, window->scale,
WL_SHM_FORMAT_ARGB8888)) {
return NULL;
}
}

View file

@ -9,6 +9,8 @@
PangoLayout *get_pango_layout(cairo_t *cairo, const char *font, const char *text, bool markup) {
PangoLayout *layout = pango_cairo_create_layout(cairo);
PangoAttrList *attrs = pango_attr_list_new();
pango_attr_list_insert(attrs, pango_attr_scale_new(2));
if (markup) {
pango_layout_set_markup(layout, text, -1);
} else {
@ -17,6 +19,8 @@ PangoLayout *get_pango_layout(cairo_t *cairo, const char *font, const char *text
PangoFontDescription *desc = pango_font_description_from_string(font);
pango_layout_set_font_description(layout, desc);
pango_layout_set_single_paragraph_mode(layout, 1);
pango_layout_set_attributes(layout, attrs);
pango_attr_list_unref(attrs);
pango_font_description_free(desc);
return layout;
}

View file

@ -18,6 +18,8 @@ static void display_handle_mode(void *data, struct wl_output *wl_output,
state->flags = flags;
state->width = width;
state->height = height;
sway_log(L_DEBUG, "Got mode %dx%x:0x%X for output %p",
width, height, flags, data);
}
}
@ -34,6 +36,7 @@ static void display_handle_done(void *data, struct wl_output *wl_output) {
static void display_handle_scale(void *data, struct wl_output *wl_output, int32_t factor) {
struct output_state *state = data;
state->scale = factor;
sway_log(L_DEBUG, "Got scale factor %d for output %p", factor, data);
}
static const struct wl_output_listener output_listener = {

View file

@ -93,11 +93,13 @@ void window_make_shell(struct window *window) {
wl_shell_surface_set_toplevel(window->shell_surface);
}
struct window *window_setup(struct registry *registry, uint32_t width, uint32_t height, bool shell_surface) {
struct window *window_setup(struct registry *registry, uint32_t width, uint32_t height,
int32_t scale, bool shell_surface) {
struct window *window = malloc(sizeof(struct window));
memset(window, 0, sizeof(struct window));
window->width = width;
window->height = height;
window->scale = scale;
window->registry = registry;
window->font = "monospace 10";
@ -121,15 +123,18 @@ struct window *window_setup(struct registry *registry, uint32_t width, uint32_t
cursor_size = "16";
}
sway_log(L_DEBUG, "Cursor scale: %d", scale);
window->cursor.cursor_theme = wl_cursor_theme_load(cursor_theme,
atoi(cursor_size), registry->shm);
atoi(cursor_size) * scale, registry->shm);
window->cursor.cursor = wl_cursor_theme_get_cursor(window->cursor.cursor_theme, "left_ptr");
window->cursor.surface = wl_compositor_create_surface(registry->compositor);
struct wl_cursor_image *image = window->cursor.cursor->images[0];
struct wl_buffer *cursor_buf = wl_cursor_image_get_buffer(image);
wl_surface_attach(window->cursor.surface, cursor_buf, 0, 0);
wl_surface_damage(window->cursor.surface, 0, 0, image->width, image->height);
wl_surface_set_buffer_scale(window->cursor.surface, scale);
wl_surface_damage(window->cursor.surface, 0, 0,
image->width, image->height);
wl_surface_commit(window->cursor.surface);
}
@ -159,8 +164,9 @@ int window_render(struct window *window) {
window->frame_cb = wl_surface_frame(window->surface);
wl_callback_add_listener(window->frame_cb, &listener, window);
wl_surface_damage(window->surface, 0, 0, window->buffer->width, window->buffer->height);
wl_surface_attach(window->surface, window->buffer->buffer, 0, 0);
wl_surface_set_buffer_scale(window->surface, window->scale);
wl_surface_damage(window->surface, 0, 0, window->width, window->height);
wl_surface_commit(window->surface);
return 1;