Skip to content

Commit

Permalink
Add HiDPI support
Browse files Browse the repository at this point in the history
Fixes #3
  • Loading branch information
emersion committed Jun 1, 2018
1 parent eb8d6e4 commit d1c3b2e
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 6 deletions.
1 change: 1 addition & 0 deletions include/slurp.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ struct slurp_output {
struct wl_list link; // slurp_state::outputs

struct slurp_box geometry;
int32_t scale;

struct wl_surface *surface;
struct zwlr_layer_surface_v1 *layer_surface;
Expand Down
16 changes: 14 additions & 2 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -150,11 +150,18 @@ static void output_handle_geometry(void *data, struct wl_output *wl_output,
output->geometry.y = y;
}

static void output_handle_scale(void *data, struct wl_output *wl_output,
int32_t scale) {
struct slurp_output *output = data;

output->scale = scale;
}

static const struct wl_output_listener output_listener = {
.geometry = output_handle_geometry,
.mode = noop,
.done = noop,
.scale = noop,
.scale = output_handle_scale,
};

static void create_output(struct slurp_state *state,
Expand All @@ -166,6 +173,7 @@ static void create_output(struct slurp_state *state,
}
output->wl_output = wl_output;
output->state = state;
output->scale = 1;
wl_list_insert(&state->outputs, &output->link);

wl_output_add_listener(wl_output, &output_listener, output);
Expand All @@ -192,8 +200,11 @@ static void send_frame(struct slurp_output *output) {
return;
}

int32_t buffer_width = output->width * output->scale;
int32_t buffer_height = output->height * output->scale;

output->current_buffer = get_next_buffer(state->shm, output->buffers,
output->width, output->height);
buffer_width, buffer_height);
if (output->current_buffer == NULL) {
return;
}
Expand All @@ -207,6 +218,7 @@ static void send_frame(struct slurp_output *output) {

wl_surface_attach(output->surface, output->current_buffer->buffer, 0, 0);
wl_surface_damage(output->surface, 0, 0, output->width, output->height);
wl_surface_set_buffer_scale(output->surface, output->scale);
wl_surface_commit(output->surface);
output->dirty = false;
}
Expand Down
12 changes: 8 additions & 4 deletions render.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ void render(struct slurp_output *output) {
struct slurp_state *state = output->state;
struct pool_buffer *buffer = output->current_buffer;
cairo_t *cairo = buffer->cairo;
int32_t scale = output->scale;

uint32_t border_color = 0x000000FF;
int border_size = 2;

// Clear
cairo_save(cairo);
Expand All @@ -37,10 +41,10 @@ void render(struct slurp_output *output) {
pointer_get_box(pointer, &x, &y, &width, &height);

// Draw border
int border_size = 2;
set_source_u32(cairo, 0x000000FF);
cairo_set_line_width(cairo, border_size);
cairo_rectangle(cairo, x, y, width, height);
set_source_u32(cairo, border_color);
cairo_set_line_width(cairo, border_size * scale);
cairo_rectangle(cairo, x * scale, y * scale,
width * scale, height * scale);
cairo_stroke(cairo);
}
}

0 comments on commit d1c3b2e

Please sign in to comment.