aboutsummaryrefslogtreecommitdiffstats
path: root/swaylock/render.c
diff options
context:
space:
mode:
Diffstat (limited to 'swaylock/render.c')
-rw-r--r--swaylock/render.c189
1 files changed, 0 insertions, 189 deletions
diff --git a/swaylock/render.c b/swaylock/render.c
deleted file mode 100644
index 5aedaad5..00000000
--- a/swaylock/render.c
+++ /dev/null
@@ -1,189 +0,0 @@
1#include <math.h>
2#include <stdlib.h>
3#include <wayland-client.h>
4#include "cairo.h"
5#include "background-image.h"
6#include "swaylock/swaylock.h"
7
8#define M_PI 3.14159265358979323846
9const float TYPE_INDICATOR_RANGE = M_PI / 3.0f;
10const float TYPE_INDICATOR_BORDER_THICKNESS = M_PI / 128.0f;
11
12static void set_color_for_state(cairo_t *cairo, struct swaylock_state *state,
13 struct swaylock_colorset *colorset) {
14 if (state->auth_state == AUTH_STATE_VALIDATING) {
15 cairo_set_source_u32(cairo, colorset->verifying);
16 } else if (state->auth_state == AUTH_STATE_INVALID) {
17 cairo_set_source_u32(cairo, colorset->wrong);
18 } else if (state->auth_state == AUTH_STATE_CLEAR) {
19 cairo_set_source_u32(cairo, colorset->cleared);
20 } else {
21 if (state->xkb.caps_lock && state->args.show_caps_lock_indicator) {
22 cairo_set_source_u32(cairo, colorset->caps_lock);
23 } else if (state->xkb.caps_lock && !state->args.show_caps_lock_indicator &&
24 state->args.show_caps_lock_text) {
25 uint32_t inputtextcolor = state->args.colors.text.input;
26 state->args.colors.text.input = state->args.colors.text.caps_lock;
27 cairo_set_source_u32(cairo, colorset->input);
28 state->args.colors.text.input = inputtextcolor;
29 } else {
30 cairo_set_source_u32(cairo, colorset->input);
31 }
32 }
33}
34
35void render_frame(struct swaylock_surface *surface) {
36 struct swaylock_state *state = surface->state;
37
38 int buffer_width = surface->width * surface->scale;
39 int buffer_height = surface->height * surface->scale;
40 if (buffer_width == 0 || buffer_height == 0) {
41 return; // not yet configured
42 }
43
44 surface->current_buffer = get_next_buffer(state->shm,
45 surface->buffers, buffer_width, buffer_height);
46 if (surface->current_buffer == NULL) {
47 return;
48 }
49
50 cairo_t *cairo = surface->current_buffer->cairo;
51 cairo_set_antialias(cairo, CAIRO_ANTIALIAS_BEST);
52 cairo_font_options_t *fo = cairo_font_options_create();
53 cairo_font_options_set_hint_style(fo, CAIRO_HINT_STYLE_FULL);
54 cairo_font_options_set_antialias(fo, CAIRO_ANTIALIAS_SUBPIXEL);
55 cairo_font_options_set_subpixel_order(fo, to_cairo_subpixel_order(surface->subpixel));
56 cairo_set_font_options(cairo, fo);
57 cairo_font_options_destroy(fo);
58 cairo_identity_matrix(cairo);
59
60 cairo_save(cairo);
61 cairo_set_operator(cairo, CAIRO_OPERATOR_SOURCE);
62 if (state->args.mode == BACKGROUND_MODE_SOLID_COLOR || !surface->image) {
63 cairo_set_source_u32(cairo, state->args.colors.background);
64 cairo_paint(cairo);
65 } else {
66 render_background_image(cairo, surface->image,
67 state->args.mode, buffer_width, buffer_height);
68 }
69 cairo_restore(cairo);
70 cairo_identity_matrix(cairo);
71
72 int arc_radius = state->args.radius * surface->scale;
73 int arc_thickness = state->args.thickness * surface->scale;
74 float type_indicator_border_thickness =
75 TYPE_INDICATOR_BORDER_THICKNESS * surface->scale;
76
77 if (state->args.show_indicator && state->auth_state != AUTH_STATE_IDLE) {
78 // Draw circle
79 cairo_set_line_width(cairo, arc_thickness);
80 cairo_arc(cairo, buffer_width / 2, buffer_height / 2, arc_radius,
81 0, 2 * M_PI);
82 set_color_for_state(cairo, state, &state->args.colors.inside);
83 cairo_fill_preserve(cairo);
84 set_color_for_state(cairo, state, &state->args.colors.ring);
85 cairo_stroke(cairo);
86
87 // Draw a message
88 char *text = NULL;
89 set_color_for_state(cairo, state, &state->args.colors.text);
90 cairo_select_font_face(cairo, state->args.font,
91 CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL);
92 cairo_set_font_size(cairo, arc_radius / 3.0f);
93 switch (state->auth_state) {
94 case AUTH_STATE_VALIDATING:
95 text = "verifying";
96 break;
97 case AUTH_STATE_INVALID:
98 text = "wrong";
99 break;
100 case AUTH_STATE_CLEAR:
101 text = "cleared";
102 break;
103 case AUTH_STATE_INPUT:
104 case AUTH_STATE_INPUT_NOP:
105 case AUTH_STATE_BACKSPACE:
106 if (state->xkb.caps_lock && state->args.show_caps_lock_text) {
107 text = "Caps Lock";
108 }
109 break;
110 default:
111 break;
112 }
113
114 if (text) {
115 cairo_text_extents_t extents;
116 double x, y;
117 cairo_text_extents(cairo, text, &extents);
118 x = (buffer_width / 2) -
119 (extents.width / 2 + extents.x_bearing);
120 y = (buffer_height / 2) -
121 (extents.height / 2 + extents.y_bearing);
122
123 cairo_move_to(cairo, x, y);
124 cairo_show_text(cairo, text);
125 cairo_close_path(cairo);
126 cairo_new_sub_path(cairo);
127 }
128
129 // Typing indicator: Highlight random part on keypress
130 if (state->auth_state == AUTH_STATE_INPUT
131 || state->auth_state == AUTH_STATE_BACKSPACE) {
132 static double highlight_start = 0;
133 highlight_start +=
134 (rand() % (int)(M_PI * 100)) / 100.0 + M_PI * 0.5;
135 cairo_arc(cairo, buffer_width / 2, buffer_height / 2,
136 arc_radius, highlight_start,
137 highlight_start + TYPE_INDICATOR_RANGE);
138 if (state->auth_state == AUTH_STATE_INPUT) {
139 if (state->xkb.caps_lock && state->args.show_caps_lock_indicator) {
140 cairo_set_source_u32(cairo, state->args.colors.caps_lock_key_highlight);
141 } else {
142 cairo_set_source_u32(cairo, state->args.colors.key_highlight);
143 }
144 } else {
145 if (state->xkb.caps_lock && state->args.show_caps_lock_indicator) {
146 cairo_set_source_u32(cairo, state->args.colors.caps_lock_bs_highlight);
147 } else {
148 cairo_set_source_u32(cairo, state->args.colors.bs_highlight);
149 }
150 }
151 cairo_stroke(cairo);
152
153 // Draw borders
154 cairo_set_source_u32(cairo, state->args.colors.separator);
155 cairo_arc(cairo, buffer_width / 2, buffer_height / 2,
156 arc_radius, highlight_start,
157 highlight_start + type_indicator_border_thickness);
158 cairo_stroke(cairo);
159
160 cairo_arc(cairo, buffer_width / 2, buffer_height / 2,
161 arc_radius, highlight_start + TYPE_INDICATOR_RANGE,
162 highlight_start + TYPE_INDICATOR_RANGE +
163 type_indicator_border_thickness);
164 cairo_stroke(cairo);
165 }
166
167 // Draw inner + outer border of the circle
168 set_color_for_state(cairo, state, &state->args.colors.line);
169 cairo_set_line_width(cairo, 2.0 * surface->scale);
170 cairo_arc(cairo, buffer_width / 2, buffer_height / 2,
171 arc_radius - arc_thickness / 2, 0, 2 * M_PI);
172 cairo_stroke(cairo);
173 cairo_arc(cairo, buffer_width / 2, buffer_height / 2,
174 arc_radius + arc_thickness / 2, 0, 2 * M_PI);
175 cairo_stroke(cairo);
176 }
177
178 wl_surface_set_buffer_scale(surface->surface, surface->scale);
179 wl_surface_attach(surface->surface, surface->current_buffer->buffer, 0, 0);
180 wl_surface_damage(surface->surface, 0, 0, surface->width, surface->height);
181 wl_surface_commit(surface->surface);
182}
183
184void render_frames(struct swaylock_state *state) {
185 struct swaylock_surface *surface;
186 wl_list_for_each(surface, &state->surfaces, link) {
187 render_frame(surface);
188 }
189}