From 066143adef7adc6e76e43e1990db2f75fe984b42 Mon Sep 17 00:00:00 2001 From: Drew DeVault Date: Tue, 3 Apr 2018 14:31:30 -0400 Subject: Add password buffer, refactor rendering/surfaces --- swaylock/render.c | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 swaylock/render.c (limited to 'swaylock/render.c') diff --git a/swaylock/render.c b/swaylock/render.c new file mode 100644 index 00000000..8fc47281 --- /dev/null +++ b/swaylock/render.c @@ -0,0 +1,21 @@ +#include +#include "cairo.h" +#include "background-image.h" +#include "swaylock/swaylock.h" + +void render_frame(struct swaylock_surface *surface) { + struct swaylock_state *state = surface->state; + surface->current_buffer = get_next_buffer(state->shm, + surface->buffers, surface->width, surface->height); + cairo_t *cairo = surface->current_buffer->cairo; + if (state->args.mode == BACKGROUND_MODE_SOLID_COLOR) { + cairo_set_source_u32(cairo, state->args.color); + cairo_paint(cairo); + } else { + render_background_image(cairo, surface->image, + state->args.mode, surface->width, surface->height); + } + wl_surface_attach(surface->surface, surface->current_buffer->buffer, 0, 0); + wl_surface_damage(surface->surface, 0, 0, surface->width, surface->height); + wl_surface_commit(surface->surface); +} -- cgit v1.2.3 From d053acbed6fea0f73eb79ac800c1342f8afadeb8 Mon Sep 17 00:00:00 2001 From: Drew DeVault Date: Tue, 3 Apr 2018 15:04:31 -0400 Subject: R E N D E R I N G --- common/background-image.c | 3 +- include/swaylock/swaylock.h | 10 ++++ swaylock/main.c | 14 +++-- swaylock/password.c | 20 ++++++-- swaylock/render.c | 121 +++++++++++++++++++++++++++++++++++++++++++- 5 files changed, 159 insertions(+), 9 deletions(-) (limited to 'swaylock/render.c') diff --git a/common/background-image.c b/common/background-image.c index 1a6c0df0..e5fb4433 100644 --- a/common/background-image.c +++ b/common/background-image.c @@ -28,7 +28,8 @@ cairo_surface_t *load_background_image(const char *path) { GError *err = NULL; GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file(path, &err); if (!pixbuf) { - wlr_log(L_ERROR, "Failed to load background image."); + wlr_log(L_ERROR, "Failed to load background image (%s).", + err->message); return false; } image = gdk_cairo_image_surface_create_from_pixbuf(pixbuf); diff --git a/include/swaylock/swaylock.h b/include/swaylock/swaylock.h index f3b0b58b..ddca633d 100644 --- a/include/swaylock/swaylock.h +++ b/include/swaylock/swaylock.h @@ -9,6 +9,14 @@ #include "swaylock/seat.h" #include "wlr-layer-shell-unstable-v1-client-protocol.h" +enum auth_state { + AUTH_STATE_IDLE, + AUTH_STATE_INPUT, + AUTH_STATE_BACKSPACE, + AUTH_STATE_VALIDATING, + AUTH_STATE_INVALID, +}; + struct swaylock_args { uint32_t color; enum background_mode mode; @@ -30,6 +38,7 @@ struct swaylock_state { struct swaylock_args args; struct swaylock_password password; struct swaylock_xkb xkb; + enum auth_state auth_state; bool run_display; }; @@ -48,5 +57,6 @@ struct swaylock_surface { void swaylock_handle_key(struct swaylock_state *state, xkb_keysym_t keysym, uint32_t codepoint); void render_frame(struct swaylock_surface *surface); +void render_frames(struct swaylock_state *state); #endif diff --git a/swaylock/main.c b/swaylock/main.c index c8fdc2f4..ce337e24 100644 --- a/swaylock/main.c +++ b/swaylock/main.c @@ -133,6 +133,7 @@ int main(int argc, char **argv) { .color = 0xFFFFFFFF, .show_indicator = true, }; + cairo_surface_t *background_image = NULL; state.args = args; wlr_log_init(L_DEBUG, NULL); @@ -150,8 +151,13 @@ int main(int argc, char **argv) { break; } case 'i': - // TODO - return 1; + // TODO: Multiple background images (bleh) + background_image = load_background_image(optarg); + if (!background_image) { + return 1; + } + state.args.mode = BACKGROUND_MODE_FILL; + break; case 's': state.args.mode = parse_background_mode(optarg); if (state.args.mode == BACKGROUND_MODE_INVALID) { @@ -159,7 +165,7 @@ int main(int argc, char **argv) { } break; case 't': - // TODO + state.args.mode = BACKGROUND_MODE_TILE; break; case 'v': #if defined SWAY_GIT_VERSION && defined SWAY_GIT_BRANCH && defined SWAY_VERSION_DATE @@ -197,6 +203,8 @@ int main(int argc, char **argv) { struct swaylock_surface *surface; wl_list_for_each(surface, &state.surfaces, link) { + surface->image = background_image; + assert(surface->surface = wl_compositor_create_surface(state.compositor)); diff --git a/swaylock/password.c b/swaylock/password.c index 9af7fe16..2bdf151f 100644 --- a/swaylock/password.c +++ b/swaylock/password.c @@ -50,21 +50,23 @@ static bool attempt_password(struct swaylock_password *pw) { wlr_log(L_ERROR, "pam_end failed"); goto fail; } - // PAM freed this + // PAM frees this pw->buffer = NULL; pw->len = pw->size = 0; return true; fail: - // PAM freed this + // PAM frees this pw->buffer = NULL; pw->len = pw->size = 0; return false; } -static void backspace(struct swaylock_password *pw) { +static bool backspace(struct swaylock_password *pw) { if (pw->len != 0) { pw->buffer[--pw->len] = 0; + return true; } + return false; } static void append_ch(struct swaylock_password *pw, uint32_t codepoint) { @@ -97,17 +99,27 @@ void swaylock_handle_key(struct swaylock_state *state, switch (keysym) { case XKB_KEY_KP_Enter: /* fallthrough */ case XKB_KEY_Return: + state->auth_state = AUTH_STATE_VALIDATING; + render_frames(state); if (attempt_password(&state->password)) { exit(0); } + state->auth_state = AUTH_STATE_INVALID; + render_frames(state); break; case XKB_KEY_BackSpace: - backspace(&state->password); + if (backspace(&state->password)) { + state->auth_state = AUTH_STATE_BACKSPACE; + render_frames(state); + } break; default: if (codepoint) { append_ch(&state->password, codepoint); + state->auth_state = AUTH_STATE_INPUT; + render_frames(state); } break; } + // TODO: Expire state in a few seconds } diff --git a/swaylock/render.c b/swaylock/render.c index 8fc47281..90db71e3 100644 --- a/swaylock/render.c +++ b/swaylock/render.c @@ -1,21 +1,140 @@ #include +#include #include "cairo.h" #include "background-image.h" #include "swaylock/swaylock.h" +#define M_PI 3.14159265358979323846 + void render_frame(struct swaylock_surface *surface) { struct swaylock_state *state = surface->state; surface->current_buffer = get_next_buffer(state->shm, surface->buffers, surface->width, surface->height); cairo_t *cairo = surface->current_buffer->cairo; + cairo_identity_matrix(cairo); if (state->args.mode == BACKGROUND_MODE_SOLID_COLOR) { cairo_set_source_u32(cairo, state->args.color); cairo_paint(cairo); } else { + // TODO: hidpi render_background_image(cairo, surface->image, - state->args.mode, surface->width, surface->height); + state->args.mode, surface->width, surface->height, 1); + } + cairo_identity_matrix(cairo); + + const int ARC_RADIUS = 50; + const int ARC_THICKNESS = 10; + const float TYPE_INDICATOR_RANGE = M_PI / 3.0f; + const float TYPE_INDICATOR_BORDER_THICKNESS = M_PI / 128.0f; + if (state->args.show_indicator && state->auth_state != AUTH_STATE_IDLE) { + // Draw circle + cairo_set_line_width(cairo, ARC_THICKNESS); + cairo_arc(cairo, surface->width / 2, surface->height / 2, + ARC_RADIUS, 0, 2 * M_PI); + switch (state->auth_state) { + case AUTH_STATE_INPUT: + case AUTH_STATE_BACKSPACE: { + cairo_set_source_rgba(cairo, 0, 0, 0, 0.75); + cairo_fill_preserve(cairo); + cairo_set_source_rgb(cairo, 51.0 / 255, 125.0 / 255, 0); + cairo_stroke(cairo); + } break; + case AUTH_STATE_VALIDATING: { + cairo_set_source_rgba(cairo, 0, 114.0 / 255, 255.0 / 255, 0.75); + cairo_fill_preserve(cairo); + cairo_set_source_rgb(cairo, 51.0 / 255, 0, 250.0 / 255); + cairo_stroke(cairo); + } break; + case AUTH_STATE_INVALID: { + cairo_set_source_rgba(cairo, 250.0 / 255, 0, 0, 0.75); + cairo_fill_preserve(cairo); + cairo_set_source_rgb(cairo, 125.0 / 255, 51.0 / 255, 0); + cairo_stroke(cairo); + } break; + default: break; + } + + // Draw a message + char *text = NULL; + cairo_set_source_rgb(cairo, 0, 0, 0); + cairo_select_font_face(cairo, "sans-serif", + CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL); + cairo_set_font_size(cairo, ARC_RADIUS / 3.0f); + switch (state->auth_state) { + case AUTH_STATE_VALIDATING: + text = "verifying"; + break; + case AUTH_STATE_INVALID: + text = "wrong"; + break; + default: break; + } + + if (text) { + cairo_text_extents_t extents; + double x, y; + cairo_text_extents(cairo, text, &extents); + x = (surface->width / 2) - + (extents.width / 2 + extents.x_bearing); + y = (surface->height / 2) - + (extents.height / 2 + extents.y_bearing); + + cairo_move_to(cairo, x, y); + cairo_show_text(cairo, text); + cairo_close_path(cairo); + cairo_new_sub_path(cairo); + } + + // Typing indicator: Highlight random part on keypress + if (state->auth_state == AUTH_STATE_INPUT + || state->auth_state == AUTH_STATE_BACKSPACE) { + static double highlight_start = 0; + highlight_start += + (rand() % (int)(M_PI * 100)) / 100.0 + M_PI * 0.5; + cairo_arc(cairo, surface->width / 2, surface->height / 2, + ARC_RADIUS, highlight_start, + highlight_start + TYPE_INDICATOR_RANGE); + if (state->auth_state == AUTH_STATE_INPUT) { + cairo_set_source_rgb(cairo, 51.0 / 255, 219.0 / 255, 0); + } else { + cairo_set_source_rgb(cairo, 219.0 / 255, 51.0 / 255, 0); + } + cairo_stroke(cairo); + + // Draw borders + cairo_set_source_rgb(cairo, 0, 0, 0); + cairo_arc(cairo, surface->width / 2, surface->height / 2, + ARC_RADIUS, highlight_start, + highlight_start + TYPE_INDICATOR_BORDER_THICKNESS); + cairo_stroke(cairo); + + cairo_arc(cairo, surface->width / 2, surface->height / 2, + ARC_RADIUS, highlight_start + TYPE_INDICATOR_RANGE, + highlight_start + TYPE_INDICATOR_RANGE + + TYPE_INDICATOR_BORDER_THICKNESS); + cairo_stroke(cairo); + } + + // Draw inner + outer border of the circle + cairo_set_source_rgb(cairo, 0, 0, 0); + cairo_set_line_width(cairo, 2.0); + cairo_arc(cairo, surface->width / 2, surface->height / 2, + ARC_RADIUS - ARC_THICKNESS / 2, 0, 2 * M_PI); + cairo_stroke(cairo); + cairo_arc(cairo, surface->width / 2, surface->height / 2, + ARC_RADIUS + ARC_THICKNESS / 2, 0, 2 * M_PI); + cairo_stroke(cairo); } + wl_surface_attach(surface->surface, surface->current_buffer->buffer, 0, 0); wl_surface_damage(surface->surface, 0, 0, surface->width, surface->height); wl_surface_commit(surface->surface); + wl_display_roundtrip(state->display); +} + +void render_frames(struct swaylock_state *state) { + struct swaylock_surface *surface; + wl_list_for_each(surface, &state->surfaces, link) { + render_frame(surface); + } } -- cgit v1.2.3 From 46b388995d7d50a39d13fce9417e2ad0d2cf749f Mon Sep 17 00:00:00 2001 From: Drew DeVault Date: Tue, 3 Apr 2018 19:15:14 -0400 Subject: Add hidpi support to swaylock --- include/swaylock/swaylock.h | 1 + swaylock/main.c | 35 +++++++++++++++++++++++++++++++++-- swaylock/render.c | 41 +++++++++++++++++++++++------------------ 3 files changed, 57 insertions(+), 20 deletions(-) (limited to 'swaylock/render.c') diff --git a/include/swaylock/swaylock.h b/include/swaylock/swaylock.h index 06c94ead..173e8b12 100644 --- a/include/swaylock/swaylock.h +++ b/include/swaylock/swaylock.h @@ -52,6 +52,7 @@ struct swaylock_surface { struct pool_buffer buffers[2]; struct pool_buffer *current_buffer; uint32_t width, height; + int32_t scale; struct wl_list link; }; diff --git a/swaylock/main.c b/swaylock/main.c index 6cd4e41d..1eda3afc 100644 --- a/swaylock/main.c +++ b/swaylock/main.c @@ -56,12 +56,42 @@ static const struct zwlr_layer_surface_v1_listener layer_surface_listener = { .closed = layer_surface_closed, }; +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 *output, int32_t factor) { + struct swaylock_surface *surface = data; + surface->scale = factor; + if (surface->state->run_display) { + render_frames(surface->state); + } +} + +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 swaylock_state *state = data; if (strcmp(interface, wl_compositor_interface.name) == 0) { state->compositor = wl_registry_bind(registry, name, - &wl_compositor_interface, 1); + &wl_compositor_interface, 3); } else if (strcmp(interface, wl_shm_interface.name) == 0) { state->shm = wl_registry_bind(registry, name, &wl_shm_interface, 1); @@ -80,7 +110,8 @@ static void handle_global(void *data, struct wl_registry *registry, calloc(1, sizeof(struct swaylock_surface)); surface->state = state; surface->output = wl_registry_bind(registry, name, - &wl_output_interface, 1); + &wl_output_interface, 3); + wl_output_add_listener(surface->output, &output_listener, surface); wl_list_insert(&state->surfaces, &surface->link); } } diff --git a/swaylock/render.c b/swaylock/render.c index 90db71e3..2469e60b 100644 --- a/swaylock/render.c +++ b/swaylock/render.c @@ -9,28 +9,32 @@ void render_frame(struct swaylock_surface *surface) { struct swaylock_state *state = surface->state; surface->current_buffer = get_next_buffer(state->shm, - surface->buffers, surface->width, surface->height); + surface->buffers, + surface->width * surface->scale, + surface->height * surface->scale); cairo_t *cairo = surface->current_buffer->cairo; cairo_identity_matrix(cairo); + + int buffer_width = surface->width * surface->scale; + int buffer_height = surface->height * surface->scale; if (state->args.mode == BACKGROUND_MODE_SOLID_COLOR) { cairo_set_source_u32(cairo, state->args.color); cairo_paint(cairo); } else { - // TODO: hidpi render_background_image(cairo, surface->image, - state->args.mode, surface->width, surface->height, 1); + state->args.mode, buffer_width, buffer_height); } cairo_identity_matrix(cairo); - const int ARC_RADIUS = 50; - const int ARC_THICKNESS = 10; - const float TYPE_INDICATOR_RANGE = M_PI / 3.0f; - const float TYPE_INDICATOR_BORDER_THICKNESS = M_PI / 128.0f; + int ARC_RADIUS = 50 * surface->scale; + int ARC_THICKNESS = 10 * surface->scale; + float TYPE_INDICATOR_RANGE = M_PI / 3.0f; + float TYPE_INDICATOR_BORDER_THICKNESS = M_PI / 128.0f * surface->scale; + if (state->args.show_indicator && state->auth_state != AUTH_STATE_IDLE) { // Draw circle cairo_set_line_width(cairo, ARC_THICKNESS); - cairo_arc(cairo, surface->width / 2, surface->height / 2, - ARC_RADIUS, 0, 2 * M_PI); + cairo_arc(cairo, buffer_width / 2, buffer_height / 2, ARC_RADIUS, 0, 2 * M_PI); switch (state->auth_state) { case AUTH_STATE_INPUT: case AUTH_STATE_BACKSPACE: { @@ -74,9 +78,9 @@ void render_frame(struct swaylock_surface *surface) { cairo_text_extents_t extents; double x, y; cairo_text_extents(cairo, text, &extents); - x = (surface->width / 2) - + x = (buffer_width / 2) - (extents.width / 2 + extents.x_bearing); - y = (surface->height / 2) - + y = (buffer_height / 2) - (extents.height / 2 + extents.y_bearing); cairo_move_to(cairo, x, y); @@ -91,7 +95,7 @@ void render_frame(struct swaylock_surface *surface) { static double highlight_start = 0; highlight_start += (rand() % (int)(M_PI * 100)) / 100.0 + M_PI * 0.5; - cairo_arc(cairo, surface->width / 2, surface->height / 2, + cairo_arc(cairo, buffer_width / 2, buffer_height / 2, ARC_RADIUS, highlight_start, highlight_start + TYPE_INDICATOR_RANGE); if (state->auth_state == AUTH_STATE_INPUT) { @@ -103,12 +107,12 @@ void render_frame(struct swaylock_surface *surface) { // Draw borders cairo_set_source_rgb(cairo, 0, 0, 0); - cairo_arc(cairo, surface->width / 2, surface->height / 2, + cairo_arc(cairo, buffer_width / 2, buffer_height / 2, ARC_RADIUS, highlight_start, highlight_start + TYPE_INDICATOR_BORDER_THICKNESS); cairo_stroke(cairo); - cairo_arc(cairo, surface->width / 2, surface->height / 2, + cairo_arc(cairo, buffer_width / 2, buffer_height / 2, ARC_RADIUS, highlight_start + TYPE_INDICATOR_RANGE, highlight_start + TYPE_INDICATOR_RANGE + TYPE_INDICATOR_BORDER_THICKNESS); @@ -117,17 +121,18 @@ void render_frame(struct swaylock_surface *surface) { // Draw inner + outer border of the circle cairo_set_source_rgb(cairo, 0, 0, 0); - cairo_set_line_width(cairo, 2.0); - cairo_arc(cairo, surface->width / 2, surface->height / 2, + cairo_set_line_width(cairo, 2.0 * surface->scale); + cairo_arc(cairo, buffer_width / 2, buffer_height / 2, ARC_RADIUS - ARC_THICKNESS / 2, 0, 2 * M_PI); cairo_stroke(cairo); - cairo_arc(cairo, surface->width / 2, surface->height / 2, + cairo_arc(cairo, buffer_width / 2, buffer_height / 2, ARC_RADIUS + ARC_THICKNESS / 2, 0, 2 * M_PI); cairo_stroke(cairo); } + wl_surface_set_buffer_scale(surface->surface, surface->scale); wl_surface_attach(surface->surface, surface->current_buffer->buffer, 0, 0); - wl_surface_damage(surface->surface, 0, 0, surface->width, surface->height); + wl_surface_damage(surface->surface, 0, 0, buffer_width, buffer_height); wl_surface_commit(surface->surface); wl_display_roundtrip(state->display); } -- cgit v1.2.3 From 0138f79b4aae563e1223737856ab44c8634c76b0 Mon Sep 17 00:00:00 2001 From: Drew DeVault Date: Tue, 3 Apr 2018 22:00:29 -0400 Subject: Move extra roundtrip into password.c --- swaylock/password.c | 1 + swaylock/render.c | 1 - 2 files changed, 1 insertion(+), 1 deletion(-) (limited to 'swaylock/render.c') diff --git a/swaylock/password.c b/swaylock/password.c index 2927a9a9..06c1180c 100644 --- a/swaylock/password.c +++ b/swaylock/password.c @@ -101,6 +101,7 @@ void swaylock_handle_key(struct swaylock_state *state, case XKB_KEY_Return: state->auth_state = AUTH_STATE_VALIDATING; render_frames(state); + wl_display_roundtrip(state->display); if (attempt_password(&state->password)) { exit(0); } diff --git a/swaylock/render.c b/swaylock/render.c index 2469e60b..79609e96 100644 --- a/swaylock/render.c +++ b/swaylock/render.c @@ -134,7 +134,6 @@ void render_frame(struct swaylock_surface *surface) { wl_surface_attach(surface->surface, surface->current_buffer->buffer, 0, 0); wl_surface_damage(surface->surface, 0, 0, buffer_width, buffer_height); wl_surface_commit(surface->surface); - wl_display_roundtrip(state->display); } void render_frames(struct swaylock_state *state) { -- cgit v1.2.3 From 218a3787d26296103225b7680bacdfd5fc6d2955 Mon Sep 17 00:00:00 2001 From: Drew DeVault Date: Tue, 3 Apr 2018 22:01:29 -0400 Subject: Import stdlib.h and define POSIX macro for rand() --- swaylock/render.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'swaylock/render.c') diff --git a/swaylock/render.c b/swaylock/render.c index 79609e96..cb3ed276 100644 --- a/swaylock/render.c +++ b/swaylock/render.c @@ -1,5 +1,7 @@ -#include +#define _POSIX_C_SOURCE 199506L #include +#include +#include #include "cairo.h" #include "background-image.h" #include "swaylock/swaylock.h" -- cgit v1.2.3 From 5d444b34f6af17894e2808c9d25948db625dabde Mon Sep 17 00:00:00 2001 From: Drew DeVault Date: Wed, 4 Apr 2018 18:52:44 -0400 Subject: Address review feedback from @emersion --- swaylock/main.c | 7 ++++--- swaylock/password.c | 3 ++- swaylock/render.c | 44 ++++++++++++++++++++++++-------------------- 3 files changed, 30 insertions(+), 24 deletions(-) (limited to 'swaylock/render.c') diff --git a/swaylock/main.c b/swaylock/main.c index 1eda3afc..1d522184 100644 --- a/swaylock/main.c +++ b/swaylock/main.c @@ -216,7 +216,8 @@ int main(int argc, char **argv) { wl_list_init(&state.surfaces); state.xkb.context = xkb_context_new(XKB_CONTEXT_NO_FLAGS); - assert(state.display = wl_display_connect(NULL)); + state.display = wl_display_connect(NULL); + assert(state.display); struct wl_registry *registry = wl_display_get_registry(state.display); wl_registry_add_listener(registry, ®istry_listener, &state); @@ -236,8 +237,8 @@ int main(int argc, char **argv) { wl_list_for_each(surface, &state.surfaces, link) { surface->image = background_image; - assert(surface->surface = - wl_compositor_create_surface(state.compositor)); + surface->surface = wl_compositor_create_surface(state.compositor); + assert(surface->surface); surface->layer_surface = zwlr_layer_shell_v1_get_layer_surface( state.layer_shell, surface->surface, surface->output, diff --git a/swaylock/password.c b/swaylock/password.c index 06c1180c..1839f991 100644 --- a/swaylock/password.c +++ b/swaylock/password.c @@ -103,7 +103,8 @@ void swaylock_handle_key(struct swaylock_state *state, render_frames(state); wl_display_roundtrip(state->display); if (attempt_password(&state->password)) { - exit(0); + state->run_display = false; + break; } state->auth_state = AUTH_STATE_INVALID; render_frames(state); diff --git a/swaylock/render.c b/swaylock/render.c index cb3ed276..cd387be5 100644 --- a/swaylock/render.c +++ b/swaylock/render.c @@ -7,18 +7,22 @@ #include "swaylock/swaylock.h" #define M_PI 3.14159265358979323846 +const int ARC_RADIUS = 50; +const int ARC_THICKNESS = 10; +const float TYPE_INDICATOR_RANGE = M_PI / 3.0f; +const float TYPE_INDICATOR_BORDER_THICKNESS = M_PI / 128.0f; void render_frame(struct swaylock_surface *surface) { struct swaylock_state *state = surface->state; + + int buffer_width = surface->width * surface->scale; + int buffer_height = surface->height * surface->scale; + surface->current_buffer = get_next_buffer(state->shm, - surface->buffers, - surface->width * surface->scale, - surface->height * surface->scale); + surface->buffers, buffer_width, buffer_height); cairo_t *cairo = surface->current_buffer->cairo; cairo_identity_matrix(cairo); - int buffer_width = surface->width * surface->scale; - int buffer_height = surface->height * surface->scale; if (state->args.mode == BACKGROUND_MODE_SOLID_COLOR) { cairo_set_source_u32(cairo, state->args.color); cairo_paint(cairo); @@ -28,15 +32,15 @@ void render_frame(struct swaylock_surface *surface) { } cairo_identity_matrix(cairo); - int ARC_RADIUS = 50 * surface->scale; - int ARC_THICKNESS = 10 * surface->scale; - float TYPE_INDICATOR_RANGE = M_PI / 3.0f; - float TYPE_INDICATOR_BORDER_THICKNESS = M_PI / 128.0f * surface->scale; + int arc_radius = ARC_RADIUS * surface->scale; + int arc_thickness = ARC_THICKNESS * surface->scale; + float type_indicator_border_thickness = + TYPE_INDICATOR_BORDER_THICKNESS * surface->scale; if (state->args.show_indicator && state->auth_state != AUTH_STATE_IDLE) { // Draw circle - cairo_set_line_width(cairo, ARC_THICKNESS); - cairo_arc(cairo, buffer_width / 2, buffer_height / 2, ARC_RADIUS, 0, 2 * M_PI); + cairo_set_line_width(cairo, arc_thickness); + cairo_arc(cairo, buffer_width / 2, buffer_height / 2, arc_radius, 0, 2 * M_PI); switch (state->auth_state) { case AUTH_STATE_INPUT: case AUTH_STATE_BACKSPACE: { @@ -65,7 +69,7 @@ void render_frame(struct swaylock_surface *surface) { cairo_set_source_rgb(cairo, 0, 0, 0); cairo_select_font_face(cairo, "sans-serif", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL); - cairo_set_font_size(cairo, ARC_RADIUS / 3.0f); + cairo_set_font_size(cairo, arc_radius / 3.0f); switch (state->auth_state) { case AUTH_STATE_VALIDATING: text = "verifying"; @@ -98,7 +102,7 @@ void render_frame(struct swaylock_surface *surface) { highlight_start += (rand() % (int)(M_PI * 100)) / 100.0 + M_PI * 0.5; cairo_arc(cairo, buffer_width / 2, buffer_height / 2, - ARC_RADIUS, highlight_start, + arc_radius, highlight_start, highlight_start + TYPE_INDICATOR_RANGE); if (state->auth_state == AUTH_STATE_INPUT) { cairo_set_source_rgb(cairo, 51.0 / 255, 219.0 / 255, 0); @@ -110,14 +114,14 @@ void render_frame(struct swaylock_surface *surface) { // Draw borders cairo_set_source_rgb(cairo, 0, 0, 0); cairo_arc(cairo, buffer_width / 2, buffer_height / 2, - ARC_RADIUS, highlight_start, - highlight_start + TYPE_INDICATOR_BORDER_THICKNESS); + arc_radius, highlight_start, + highlight_start + type_indicator_border_thickness); cairo_stroke(cairo); cairo_arc(cairo, buffer_width / 2, buffer_height / 2, - ARC_RADIUS, highlight_start + TYPE_INDICATOR_RANGE, + arc_radius, highlight_start + TYPE_INDICATOR_RANGE, highlight_start + TYPE_INDICATOR_RANGE + - TYPE_INDICATOR_BORDER_THICKNESS); + type_indicator_border_thickness); cairo_stroke(cairo); } @@ -125,16 +129,16 @@ void render_frame(struct swaylock_surface *surface) { cairo_set_source_rgb(cairo, 0, 0, 0); cairo_set_line_width(cairo, 2.0 * surface->scale); cairo_arc(cairo, buffer_width / 2, buffer_height / 2, - ARC_RADIUS - ARC_THICKNESS / 2, 0, 2 * M_PI); + arc_radius - arc_thickness / 2, 0, 2 * M_PI); cairo_stroke(cairo); cairo_arc(cairo, buffer_width / 2, buffer_height / 2, - ARC_RADIUS + ARC_THICKNESS / 2, 0, 2 * M_PI); + arc_radius + arc_thickness / 2, 0, 2 * M_PI); cairo_stroke(cairo); } wl_surface_set_buffer_scale(surface->surface, surface->scale); wl_surface_attach(surface->surface, surface->current_buffer->buffer, 0, 0); - wl_surface_damage(surface->surface, 0, 0, buffer_width, buffer_height); + wl_surface_damage(surface->surface, 0, 0, surface->width, surface->height); wl_surface_commit(surface->surface); } -- cgit v1.2.3