summaryrefslogtreecommitdiffstats
path: root/swaylock/render.c
diff options
context:
space:
mode:
Diffstat (limited to 'swaylock/render.c')
-rw-r--r--swaylock/render.c121
1 files changed, 120 insertions, 1 deletions
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 @@
1#include <wayland-client.h> 1#include <wayland-client.h>
2#include <math.h>
2#include "cairo.h" 3#include "cairo.h"
3#include "background-image.h" 4#include "background-image.h"
4#include "swaylock/swaylock.h" 5#include "swaylock/swaylock.h"
5 6
7#define M_PI 3.14159265358979323846
8
6void render_frame(struct swaylock_surface *surface) { 9void render_frame(struct swaylock_surface *surface) {
7 struct swaylock_state *state = surface->state; 10 struct swaylock_state *state = surface->state;
8 surface->current_buffer = get_next_buffer(state->shm, 11 surface->current_buffer = get_next_buffer(state->shm,
9 surface->buffers, surface->width, surface->height); 12 surface->buffers, surface->width, surface->height);
10 cairo_t *cairo = surface->current_buffer->cairo; 13 cairo_t *cairo = surface->current_buffer->cairo;
14 cairo_identity_matrix(cairo);
11 if (state->args.mode == BACKGROUND_MODE_SOLID_COLOR) { 15 if (state->args.mode == BACKGROUND_MODE_SOLID_COLOR) {
12 cairo_set_source_u32(cairo, state->args.color); 16 cairo_set_source_u32(cairo, state->args.color);
13 cairo_paint(cairo); 17 cairo_paint(cairo);
14 } else { 18 } else {
19 // TODO: hidpi
15 render_background_image(cairo, surface->image, 20 render_background_image(cairo, surface->image,
16 state->args.mode, surface->width, surface->height); 21 state->args.mode, surface->width, surface->height, 1);
22 }
23 cairo_identity_matrix(cairo);
24
25 const int ARC_RADIUS = 50;
26 const int ARC_THICKNESS = 10;
27 const float TYPE_INDICATOR_RANGE = M_PI / 3.0f;
28 const float TYPE_INDICATOR_BORDER_THICKNESS = M_PI / 128.0f;
29 if (state->args.show_indicator && state->auth_state != AUTH_STATE_IDLE) {
30 // Draw circle
31 cairo_set_line_width(cairo, ARC_THICKNESS);
32 cairo_arc(cairo, surface->width / 2, surface->height / 2,
33 ARC_RADIUS, 0, 2 * M_PI);
34 switch (state->auth_state) {
35 case AUTH_STATE_INPUT:
36 case AUTH_STATE_BACKSPACE: {
37 cairo_set_source_rgba(cairo, 0, 0, 0, 0.75);
38 cairo_fill_preserve(cairo);
39 cairo_set_source_rgb(cairo, 51.0 / 255, 125.0 / 255, 0);
40 cairo_stroke(cairo);
41 } break;
42 case AUTH_STATE_VALIDATING: {
43 cairo_set_source_rgba(cairo, 0, 114.0 / 255, 255.0 / 255, 0.75);
44 cairo_fill_preserve(cairo);
45 cairo_set_source_rgb(cairo, 51.0 / 255, 0, 250.0 / 255);
46 cairo_stroke(cairo);
47 } break;
48 case AUTH_STATE_INVALID: {
49 cairo_set_source_rgba(cairo, 250.0 / 255, 0, 0, 0.75);
50 cairo_fill_preserve(cairo);
51 cairo_set_source_rgb(cairo, 125.0 / 255, 51.0 / 255, 0);
52 cairo_stroke(cairo);
53 } break;
54 default: break;
55 }
56
57 // Draw a message
58 char *text = NULL;
59 cairo_set_source_rgb(cairo, 0, 0, 0);
60 cairo_select_font_face(cairo, "sans-serif",
61 CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL);
62 cairo_set_font_size(cairo, ARC_RADIUS / 3.0f);
63 switch (state->auth_state) {
64 case AUTH_STATE_VALIDATING:
65 text = "verifying";
66 break;
67 case AUTH_STATE_INVALID:
68 text = "wrong";
69 break;
70 default: break;
71 }
72
73 if (text) {
74 cairo_text_extents_t extents;
75 double x, y;
76 cairo_text_extents(cairo, text, &extents);
77 x = (surface->width / 2) -
78 (extents.width / 2 + extents.x_bearing);
79 y = (surface->height / 2) -
80 (extents.height / 2 + extents.y_bearing);
81
82 cairo_move_to(cairo, x, y);
83 cairo_show_text(cairo, text);
84 cairo_close_path(cairo);
85 cairo_new_sub_path(cairo);
86 }
87
88 // Typing indicator: Highlight random part on keypress
89 if (state->auth_state == AUTH_STATE_INPUT
90 || state->auth_state == AUTH_STATE_BACKSPACE) {
91 static double highlight_start = 0;
92 highlight_start +=
93 (rand() % (int)(M_PI * 100)) / 100.0 + M_PI * 0.5;
94 cairo_arc(cairo, surface->width / 2, surface->height / 2,
95 ARC_RADIUS, highlight_start,
96 highlight_start + TYPE_INDICATOR_RANGE);
97 if (state->auth_state == AUTH_STATE_INPUT) {
98 cairo_set_source_rgb(cairo, 51.0 / 255, 219.0 / 255, 0);
99 } else {
100 cairo_set_source_rgb(cairo, 219.0 / 255, 51.0 / 255, 0);
101 }
102 cairo_stroke(cairo);
103
104 // Draw borders
105 cairo_set_source_rgb(cairo, 0, 0, 0);
106 cairo_arc(cairo, surface->width / 2, surface->height / 2,
107 ARC_RADIUS, highlight_start,
108 highlight_start + TYPE_INDICATOR_BORDER_THICKNESS);
109 cairo_stroke(cairo);
110
111 cairo_arc(cairo, surface->width / 2, surface->height / 2,
112 ARC_RADIUS, highlight_start + TYPE_INDICATOR_RANGE,
113 highlight_start + TYPE_INDICATOR_RANGE +
114 TYPE_INDICATOR_BORDER_THICKNESS);
115 cairo_stroke(cairo);
116 }
117
118 // Draw inner + outer border of the circle
119 cairo_set_source_rgb(cairo, 0, 0, 0);
120 cairo_set_line_width(cairo, 2.0);
121 cairo_arc(cairo, surface->width / 2, surface->height / 2,
122 ARC_RADIUS - ARC_THICKNESS / 2, 0, 2 * M_PI);
123 cairo_stroke(cairo);
124 cairo_arc(cairo, surface->width / 2, surface->height / 2,
125 ARC_RADIUS + ARC_THICKNESS / 2, 0, 2 * M_PI);
126 cairo_stroke(cairo);
17 } 127 }
128
18 wl_surface_attach(surface->surface, surface->current_buffer->buffer, 0, 0); 129 wl_surface_attach(surface->surface, surface->current_buffer->buffer, 0, 0);
19 wl_surface_damage(surface->surface, 0, 0, surface->width, surface->height); 130 wl_surface_damage(surface->surface, 0, 0, surface->width, surface->height);
20 wl_surface_commit(surface->surface); 131 wl_surface_commit(surface->surface);
132 wl_display_roundtrip(state->display);
133}
134
135void render_frames(struct swaylock_state *state) {
136 struct swaylock_surface *surface;
137 wl_list_for_each(surface, &state->surfaces, link) {
138 render_frame(surface);
139 }
21} 140}