2018-04-03 21:01:29 -05:00
|
|
|
#define _POSIX_C_SOURCE 199506L
|
2018-04-03 14:04:31 -05:00
|
|
|
#include <math.h>
|
2018-04-03 21:01:29 -05:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <wayland-client.h>
|
2018-04-03 13:31:30 -05:00
|
|
|
#include "cairo.h"
|
|
|
|
#include "background-image.h"
|
|
|
|
#include "swaylock/swaylock.h"
|
|
|
|
|
2018-04-03 14:04:31 -05:00
|
|
|
#define M_PI 3.14159265358979323846
|
2018-04-04 17:52:44 -05:00
|
|
|
const float TYPE_INDICATOR_RANGE = M_PI / 3.0f;
|
|
|
|
const float TYPE_INDICATOR_BORDER_THICKNESS = M_PI / 128.0f;
|
2018-04-03 14:04:31 -05:00
|
|
|
|
2018-07-10 20:29:15 -05:00
|
|
|
static void set_color_for_state(cairo_t *cairo, struct swaylock_state *state,
|
|
|
|
struct swaylock_colorset *colorset) {
|
|
|
|
if (state->auth_state == AUTH_STATE_VALIDATING) {
|
|
|
|
cairo_set_source_u32(cairo, colorset->verifying);
|
|
|
|
} else if (state->auth_state == AUTH_STATE_INVALID) {
|
|
|
|
cairo_set_source_u32(cairo, colorset->wrong);
|
|
|
|
} else if (state->auth_state == AUTH_STATE_CLEAR) {
|
|
|
|
cairo_set_source_u32(cairo, colorset->cleared);
|
|
|
|
} else {
|
|
|
|
cairo_set_source_u32(cairo, colorset->input);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-03 13:31:30 -05:00
|
|
|
void render_frame(struct swaylock_surface *surface) {
|
|
|
|
struct swaylock_state *state = surface->state;
|
2018-04-04 17:52:44 -05:00
|
|
|
|
|
|
|
int buffer_width = surface->width * surface->scale;
|
|
|
|
int buffer_height = surface->height * surface->scale;
|
2018-05-23 16:55:57 -05:00
|
|
|
if (buffer_width == 0 || buffer_height == 0) {
|
|
|
|
return; // not yet configured
|
|
|
|
}
|
2018-04-04 17:52:44 -05:00
|
|
|
|
2018-04-03 13:31:30 -05:00
|
|
|
surface->current_buffer = get_next_buffer(state->shm,
|
2018-04-04 17:52:44 -05:00
|
|
|
surface->buffers, buffer_width, buffer_height);
|
2018-05-25 13:34:36 -05:00
|
|
|
if (surface->current_buffer == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-04-03 13:31:30 -05:00
|
|
|
cairo_t *cairo = surface->current_buffer->cairo;
|
2018-04-03 14:04:31 -05:00
|
|
|
cairo_identity_matrix(cairo);
|
2018-04-03 18:15:14 -05:00
|
|
|
|
2018-07-04 05:09:05 -05:00
|
|
|
cairo_save(cairo);
|
|
|
|
cairo_set_operator(cairo, CAIRO_OPERATOR_SOURCE);
|
2018-05-18 17:47:47 -05:00
|
|
|
if (state->args.mode == BACKGROUND_MODE_SOLID_COLOR || !surface->image) {
|
2018-07-10 20:29:15 -05:00
|
|
|
cairo_set_source_u32(cairo, state->args.colors.background);
|
2018-04-03 13:31:30 -05:00
|
|
|
cairo_paint(cairo);
|
|
|
|
} else {
|
|
|
|
render_background_image(cairo, surface->image,
|
2018-04-03 18:15:14 -05:00
|
|
|
state->args.mode, buffer_width, buffer_height);
|
2018-04-03 14:04:31 -05:00
|
|
|
}
|
2018-07-04 05:09:05 -05:00
|
|
|
cairo_restore(cairo);
|
2018-04-03 14:04:31 -05:00
|
|
|
cairo_identity_matrix(cairo);
|
|
|
|
|
2018-07-10 20:29:15 -05:00
|
|
|
int arc_radius = state->args.radius * surface->scale;
|
|
|
|
int arc_thickness = state->args.thickness * surface->scale;
|
2018-04-04 17:52:44 -05:00
|
|
|
float type_indicator_border_thickness =
|
|
|
|
TYPE_INDICATOR_BORDER_THICKNESS * surface->scale;
|
2018-04-03 18:15:14 -05:00
|
|
|
|
2018-04-03 14:04:31 -05:00
|
|
|
if (state->args.show_indicator && state->auth_state != AUTH_STATE_IDLE) {
|
|
|
|
// Draw circle
|
2018-04-04 17:52:44 -05:00
|
|
|
cairo_set_line_width(cairo, arc_thickness);
|
2018-07-10 20:29:15 -05:00
|
|
|
cairo_arc(cairo, buffer_width / 2, buffer_height / 2, arc_radius,
|
|
|
|
0, 2 * M_PI);
|
|
|
|
set_color_for_state(cairo, state, &state->args.colors.inside);
|
|
|
|
cairo_fill_preserve(cairo);
|
|
|
|
set_color_for_state(cairo, state, &state->args.colors.ring);
|
|
|
|
cairo_stroke(cairo);
|
2018-04-03 14:04:31 -05:00
|
|
|
|
|
|
|
// Draw a message
|
|
|
|
char *text = NULL;
|
2018-07-10 20:29:15 -05:00
|
|
|
set_color_for_state(cairo, state, &state->args.colors.text);
|
|
|
|
cairo_select_font_face(cairo, state->args.font,
|
2018-04-03 14:04:31 -05:00
|
|
|
CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL);
|
2018-04-04 17:52:44 -05:00
|
|
|
cairo_set_font_size(cairo, arc_radius / 3.0f);
|
2018-04-03 14:04:31 -05:00
|
|
|
switch (state->auth_state) {
|
|
|
|
case AUTH_STATE_VALIDATING:
|
|
|
|
text = "verifying";
|
|
|
|
break;
|
|
|
|
case AUTH_STATE_INVALID:
|
|
|
|
text = "wrong";
|
|
|
|
break;
|
2018-04-20 07:46:30 -05:00
|
|
|
case AUTH_STATE_CLEAR:
|
|
|
|
text = "cleared";
|
|
|
|
break;
|
|
|
|
case AUTH_STATE_INPUT:
|
|
|
|
case AUTH_STATE_INPUT_NOP:
|
|
|
|
if (state->xkb.caps_lock) {
|
|
|
|
text = "Caps Lock";
|
|
|
|
}
|
2018-07-10 20:29:15 -05:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
2018-04-03 14:04:31 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if (text) {
|
|
|
|
cairo_text_extents_t extents;
|
|
|
|
double x, y;
|
|
|
|
cairo_text_extents(cairo, text, &extents);
|
2018-04-03 18:15:14 -05:00
|
|
|
x = (buffer_width / 2) -
|
2018-04-03 14:04:31 -05:00
|
|
|
(extents.width / 2 + extents.x_bearing);
|
2018-04-03 18:15:14 -05:00
|
|
|
y = (buffer_height / 2) -
|
2018-04-03 14:04:31 -05:00
|
|
|
(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;
|
2018-04-03 18:15:14 -05:00
|
|
|
cairo_arc(cairo, buffer_width / 2, buffer_height / 2,
|
2018-04-04 17:52:44 -05:00
|
|
|
arc_radius, highlight_start,
|
2018-04-03 14:04:31 -05:00
|
|
|
highlight_start + TYPE_INDICATOR_RANGE);
|
|
|
|
if (state->auth_state == AUTH_STATE_INPUT) {
|
2018-07-10 20:29:15 -05:00
|
|
|
cairo_set_source_u32(cairo, state->args.colors.key_highlight);
|
2018-04-03 14:04:31 -05:00
|
|
|
} else {
|
2018-07-10 20:29:15 -05:00
|
|
|
cairo_set_source_u32(cairo, state->args.colors.bs_highlight);
|
2018-04-03 14:04:31 -05:00
|
|
|
}
|
|
|
|
cairo_stroke(cairo);
|
|
|
|
|
|
|
|
// Draw borders
|
2018-07-10 20:29:15 -05:00
|
|
|
cairo_set_source_u32(cairo, state->args.colors.separator);
|
2018-04-03 18:15:14 -05:00
|
|
|
cairo_arc(cairo, buffer_width / 2, buffer_height / 2,
|
2018-04-04 17:52:44 -05:00
|
|
|
arc_radius, highlight_start,
|
|
|
|
highlight_start + type_indicator_border_thickness);
|
2018-04-03 14:04:31 -05:00
|
|
|
cairo_stroke(cairo);
|
|
|
|
|
2018-04-03 18:15:14 -05:00
|
|
|
cairo_arc(cairo, buffer_width / 2, buffer_height / 2,
|
2018-04-04 17:52:44 -05:00
|
|
|
arc_radius, highlight_start + TYPE_INDICATOR_RANGE,
|
2018-04-03 14:04:31 -05:00
|
|
|
highlight_start + TYPE_INDICATOR_RANGE +
|
2018-04-04 17:52:44 -05:00
|
|
|
type_indicator_border_thickness);
|
2018-04-03 14:04:31 -05:00
|
|
|
cairo_stroke(cairo);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Draw inner + outer border of the circle
|
2018-07-10 20:29:15 -05:00
|
|
|
set_color_for_state(cairo, state, &state->args.colors.line);
|
2018-04-03 18:15:14 -05:00
|
|
|
cairo_set_line_width(cairo, 2.0 * surface->scale);
|
|
|
|
cairo_arc(cairo, buffer_width / 2, buffer_height / 2,
|
2018-04-04 17:52:44 -05:00
|
|
|
arc_radius - arc_thickness / 2, 0, 2 * M_PI);
|
2018-04-03 14:04:31 -05:00
|
|
|
cairo_stroke(cairo);
|
2018-04-03 18:15:14 -05:00
|
|
|
cairo_arc(cairo, buffer_width / 2, buffer_height / 2,
|
2018-04-04 17:52:44 -05:00
|
|
|
arc_radius + arc_thickness / 2, 0, 2 * M_PI);
|
2018-04-03 14:04:31 -05:00
|
|
|
cairo_stroke(cairo);
|
2018-04-03 13:31:30 -05:00
|
|
|
}
|
2018-04-03 14:04:31 -05:00
|
|
|
|
2018-04-03 18:15:14 -05:00
|
|
|
wl_surface_set_buffer_scale(surface->surface, surface->scale);
|
2018-04-03 13:31:30 -05:00
|
|
|
wl_surface_attach(surface->surface, surface->current_buffer->buffer, 0, 0);
|
2018-04-04 17:52:44 -05:00
|
|
|
wl_surface_damage(surface->surface, 0, 0, surface->width, surface->height);
|
2018-04-03 13:31:30 -05:00
|
|
|
wl_surface_commit(surface->surface);
|
2018-04-03 14:04:31 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void render_frames(struct swaylock_state *state) {
|
|
|
|
struct swaylock_surface *surface;
|
|
|
|
wl_list_for_each(surface, &state->surfaces, link) {
|
|
|
|
render_frame(surface);
|
|
|
|
}
|
2018-04-03 13:31:30 -05:00
|
|
|
}
|