aboutsummaryrefslogtreecommitdiffstats
path: root/swaylock/main.c
diff options
context:
space:
mode:
Diffstat (limited to 'swaylock/main.c')
-rw-r--r--swaylock/main.c135
1 files changed, 68 insertions, 67 deletions
diff --git a/swaylock/main.c b/swaylock/main.c
index 47592409..c2615951 100644
--- a/swaylock/main.c
+++ b/swaylock/main.c
@@ -81,6 +81,9 @@ struct lock_config *init_config() {
81 config->colors.invalid.inner_ring = 0xFA0000BF; 81 config->colors.invalid.inner_ring = 0xFA0000BF;
82 config->colors.invalid.outer_ring = 0x7D3300FF; 82 config->colors.invalid.outer_ring = 0x7D3300FF;
83 83
84 config->radius = 50;
85 config->thickness = 10;
86
84 return config; 87 return config;
85} 88}
86 89
@@ -149,10 +152,11 @@ bool verify_password() {
149void notify_key(enum wl_keyboard_key_state state, xkb_keysym_t sym, uint32_t code, uint32_t codepoint) { 152void notify_key(enum wl_keyboard_key_state state, xkb_keysym_t sym, uint32_t code, uint32_t codepoint) {
150 int redraw_screen = 0; 153 int redraw_screen = 0;
151 char *password_realloc; 154 char *password_realloc;
155 int i;
152 156
153 if (state == WL_KEYBOARD_KEY_STATE_PRESSED) { 157 if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
154 switch (sym) { 158 switch (sym) {
155 case XKB_KEY_KP_Enter: // fallthrough 159 case XKB_KEY_KP_Enter:
156 case XKB_KEY_Return: 160 case XKB_KEY_Return:
157 render_data.auth_state = AUTH_STATE_VALIDATING; 161 render_data.auth_state = AUTH_STATE_VALIDATING;
158 162
@@ -163,6 +167,7 @@ void notify_key(enum wl_keyboard_key_state state, xkb_keysym_t sym, uint32_t cod
163 if (verify_password()) { 167 if (verify_password()) {
164 exit(0); 168 exit(0);
165 } 169 }
170
166 render_data.auth_state = AUTH_STATE_INVALID; 171 render_data.auth_state = AUTH_STATE_INVALID;
167 redraw_screen = 1; 172 redraw_screen = 1;
168 173
@@ -171,73 +176,65 @@ void notify_key(enum wl_keyboard_key_state state, xkb_keysym_t sym, uint32_t cod
171 password[0] = '\0'; 176 password[0] = '\0';
172 break; 177 break;
173 case XKB_KEY_BackSpace: 178 case XKB_KEY_BackSpace:
174 { 179 i = strlen(password);
175 int i = strlen(password); 180 if (i > 0) {
176 if (i > 0) { 181 password[i - 1] = '\0';
177 password[i - 1] = '\0'; 182 render_data.auth_state = AUTH_STATE_BACKSPACE;
178 render_data.auth_state = AUTH_STATE_BACKSPACE; 183 redraw_screen = 1;
179 redraw_screen = 1;
180 }
181 break;
182 } 184 }
183 case XKB_KEY_Control_L: // fallthrough 185 break;
184 case XKB_KEY_Control_R: // fallthrough 186 case XKB_KEY_Control_L:
185 case XKB_KEY_Shift_L: // fallthrough 187 case XKB_KEY_Control_R:
186 case XKB_KEY_Shift_R: // fallthrough 188 case XKB_KEY_Shift_L:
187 case XKB_KEY_Caps_Lock: // fallthrough 189 case XKB_KEY_Shift_R:
188 case XKB_KEY_Shift_Lock: // fallthrough 190 case XKB_KEY_Caps_Lock:
189 case XKB_KEY_Meta_L: // fallthrough 191 case XKB_KEY_Shift_Lock:
190 case XKB_KEY_Meta_R: // fallthrough 192 case XKB_KEY_Meta_L:
191 case XKB_KEY_Alt_L: // fallthrough 193 case XKB_KEY_Meta_R:
192 case XKB_KEY_Alt_R: // fallthrough 194 case XKB_KEY_Alt_L:
193 case XKB_KEY_Super_L: // fallthrough 195 case XKB_KEY_Alt_R:
194 case XKB_KEY_Super_R: // fallthrough 196 case XKB_KEY_Super_L:
195 case XKB_KEY_Hyper_L: // fallthrough 197 case XKB_KEY_Super_R:
198 case XKB_KEY_Hyper_L:
196 case XKB_KEY_Hyper_R: 199 case XKB_KEY_Hyper_R:
197 { 200 break; // don't draw screen on modifier keys
198 // don't draw screen on modifier keys 201 case XKB_KEY_Escape:
199 break; 202 case XKB_KEY_u:
200 }
201 case XKB_KEY_Escape: // fallthrough
202 case XKB_KEY_u: // fallthrough
203 case XKB_KEY_U: 203 case XKB_KEY_U:
204 { 204 // clear password buffer on ctrl-u (or escape for i3lock compatibility)
205 // clear password buffer on ctrl-u (or escape for i3lock compatibility) 205 if (sym == XKB_KEY_Escape || xkb_state_mod_name_is_active(registry->input->xkb.state,
206 if (sym == XKB_KEY_Escape || xkb_state_mod_name_is_active(registry->input->xkb.state, 206 XKB_MOD_NAME_CTRL, XKB_STATE_MODS_EFFECTIVE) > 0) {
207 XKB_MOD_NAME_CTRL, XKB_STATE_MODS_EFFECTIVE) > 0) { 207 render_data.auth_state = AUTH_STATE_BACKSPACE;
208 render_data.auth_state = AUTH_STATE_BACKSPACE; 208 redraw_screen = 1;
209 redraw_screen = 1;
210 209
210 password_size = 1024;
211 free(password);
212 password = malloc(password_size);
213 password[0] = '\0';
214 break;
215 }
216 /* fallthrough */
217 default:
218 render_data.auth_state = AUTH_STATE_INPUT;
219 redraw_screen = 1;
220 i = strlen(password);
221 if (i + 1 == password_size) {
222 password_size += 1024;
223 password_realloc = realloc(password, password_size);
224 // reset password if realloc fails.
225 if (password_realloc == NULL) {
211 password_size = 1024; 226 password_size = 1024;
212 free(password); 227 free(password);
213 password = malloc(password_size); 228 password = malloc(password_size);
214 password[0] = '\0'; 229 password[0] = '\0';
215 break; 230 break;
231 } else {
232 password = password_realloc;
216 } 233 }
217 } 234 }
218 default: 235 password[i] = (char)codepoint;
219 { 236 password[i + 1] = '\0';
220 render_data.auth_state = AUTH_STATE_INPUT; 237 break;
221 redraw_screen = 1;
222 int i = strlen(password);
223 if (i + 1 == password_size) {
224 password_size += 1024;
225 password_realloc = realloc(password, password_size);
226 // reset password if realloc fails.
227 if (password_realloc == NULL) {
228 password_size = 1024;
229 free(password);
230 password = malloc(password_size);
231 password[0] = '\0';
232 break;
233 } else {
234 password = password_realloc;
235 }
236 }
237 password[i] = (char)codepoint;
238 password[i + 1] = '\0';
239 break;
240 }
241 } 238 }
242 if (redraw_screen) { 239 if (redraw_screen) {
243 render(&render_data, config); 240 render(&render_data, config);
@@ -383,6 +380,8 @@ int main(int argc, char **argv) {
383 {"separatorcolor", required_argument, NULL, 0}, 380 {"separatorcolor", required_argument, NULL, 0},
384 {"keyhlcolor", required_argument, NULL, 0}, 381 {"keyhlcolor", required_argument, NULL, 0},
385 {"bshlcolor", required_argument, NULL, 0}, 382 {"bshlcolor", required_argument, NULL, 0},
383 {"indicator-radius", required_argument, NULL, 0},
384 {"indicator-thickness", required_argument, NULL, 0},
386 {0, 0, 0, 0} 385 {0, 0, 0, 0}
387 }; 386 };
388 387
@@ -509,6 +508,10 @@ int main(int argc, char **argv) {
509 config->colors.input_cursor = parse_color(optarg); 508 config->colors.input_cursor = parse_color(optarg);
510 } else if (strcmp(long_options[option_index].name, "bshlcolor") == 0) { 509 } else if (strcmp(long_options[option_index].name, "bshlcolor") == 0) {
511 config->colors.backspace_cursor = parse_color(optarg); 510 config->colors.backspace_cursor = parse_color(optarg);
511 } else if (strcmp(long_options[option_index].name, "indicator-radius") == 0) {
512 config->radius = atoi(optarg);
513 } else if (strcmp(long_options[option_index].name, "indicator-thickness") == 0) {
514 config->thickness = atoi(optarg);
512 } 515 }
513 break; 516 break;
514 default: 517 default:
@@ -678,16 +681,14 @@ void render(struct render_data *render_data, struct lock_config *config) {
678 cairo_identity_matrix(window->cairo); 681 cairo_identity_matrix(window->cairo);
679 682
680 // Draw specific values (copied from i3) 683 // Draw specific values (copied from i3)
681 const int ARC_RADIUS = 50;
682 const int ARC_THICKNESS = 10;
683 const float TYPE_INDICATOR_RANGE = M_PI / 3.0f; 684 const float TYPE_INDICATOR_RANGE = M_PI / 3.0f;
684 const float TYPE_INDICATOR_BORDER_THICKNESS = M_PI / 128.0f; 685 const float TYPE_INDICATOR_BORDER_THICKNESS = M_PI / 128.0f;
685 686
686 // Add visual indicator 687 // Add visual indicator
687 if (show_indicator && render_data->auth_state != AUTH_STATE_IDLE) { 688 if (show_indicator && render_data->auth_state != AUTH_STATE_IDLE) {
688 // Draw circle 689 // Draw circle
689 cairo_set_line_width(window->cairo, ARC_THICKNESS); 690 cairo_set_line_width(window->cairo, config->thickness);
690 cairo_arc(window->cairo, wwidth/2, wheight/2, ARC_RADIUS, 0, 2 * M_PI); 691 cairo_arc(window->cairo, wwidth/2, wheight/2, config->radius, 0, 2 * M_PI);
691 switch (render_data->auth_state) { 692 switch (render_data->auth_state) {
692 case AUTH_STATE_INPUT: 693 case AUTH_STATE_INPUT:
693 case AUTH_STATE_BACKSPACE: { 694 case AUTH_STATE_BACKSPACE: {
@@ -715,7 +716,7 @@ void render(struct render_data *render_data, struct lock_config *config) {
715 char *text = NULL; 716 char *text = NULL;
716 cairo_set_source_u32(window->cairo, config->colors.text); 717 cairo_set_source_u32(window->cairo, config->colors.text);
717 cairo_select_font_face(window->cairo, config->font, CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL); 718 cairo_select_font_face(window->cairo, config->font, CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL);
718 cairo_set_font_size(window->cairo, ARC_RADIUS/3.0f); 719 cairo_set_font_size(window->cairo, config->radius/3.0f);
719 switch (render_data->auth_state) { 720 switch (render_data->auth_state) {
720 case AUTH_STATE_VALIDATING: 721 case AUTH_STATE_VALIDATING:
721 text = "verifying"; 722 text = "verifying";
@@ -744,7 +745,7 @@ void render(struct render_data *render_data, struct lock_config *config) {
744 if (render_data->auth_state == AUTH_STATE_INPUT || render_data->auth_state == AUTH_STATE_BACKSPACE) { 745 if (render_data->auth_state == AUTH_STATE_INPUT || render_data->auth_state == AUTH_STATE_BACKSPACE) {
745 static double highlight_start = 0; 746 static double highlight_start = 0;
746 highlight_start += (rand() % (int)(M_PI * 100)) / 100.0 + M_PI * 0.5; 747 highlight_start += (rand() % (int)(M_PI * 100)) / 100.0 + M_PI * 0.5;
747 cairo_arc(window->cairo, wwidth/2, wheight/2, ARC_RADIUS, highlight_start, highlight_start + TYPE_INDICATOR_RANGE); 748 cairo_arc(window->cairo, wwidth/2, wheight/2, config->radius, highlight_start, highlight_start + TYPE_INDICATOR_RANGE);
748 if (render_data->auth_state == AUTH_STATE_INPUT) { 749 if (render_data->auth_state == AUTH_STATE_INPUT) {
749 cairo_set_source_u32(window->cairo, config->colors.input_cursor); 750 cairo_set_source_u32(window->cairo, config->colors.input_cursor);
750 } else { 751 } else {
@@ -754,10 +755,10 @@ void render(struct render_data *render_data, struct lock_config *config) {
754 755
755 // Draw borders 756 // Draw borders
756 cairo_set_source_u32(window->cairo, config->colors.separator); 757 cairo_set_source_u32(window->cairo, config->colors.separator);
757 cairo_arc(window->cairo, wwidth/2, wheight/2, ARC_RADIUS, highlight_start, highlight_start + TYPE_INDICATOR_BORDER_THICKNESS); 758 cairo_arc(window->cairo, wwidth/2, wheight/2, config->radius, highlight_start, highlight_start + TYPE_INDICATOR_BORDER_THICKNESS);
758 cairo_stroke(window->cairo); 759 cairo_stroke(window->cairo);
759 760
760 cairo_arc(window->cairo, wwidth/2, wheight/2, ARC_RADIUS, highlight_start + TYPE_INDICATOR_RANGE, (highlight_start + TYPE_INDICATOR_RANGE) + TYPE_INDICATOR_BORDER_THICKNESS); 761 cairo_arc(window->cairo, wwidth/2, wheight/2, config->radius, highlight_start + TYPE_INDICATOR_RANGE, (highlight_start + TYPE_INDICATOR_RANGE) + TYPE_INDICATOR_BORDER_THICKNESS);
761 cairo_stroke(window->cairo); 762 cairo_stroke(window->cairo);
762 } 763 }
763 764
@@ -793,9 +794,9 @@ void render(struct render_data *render_data, struct lock_config *config) {
793 } 794 }
794 // Draw inner + outer border of the circle 795 // Draw inner + outer border of the circle
795 cairo_set_line_width(window->cairo, 2.0); 796 cairo_set_line_width(window->cairo, 2.0);
796 cairo_arc(window->cairo, wwidth/2, wheight/2, ARC_RADIUS - ARC_THICKNESS/2, 0, 2*M_PI); 797 cairo_arc(window->cairo, wwidth/2, wheight/2, config->radius - config->thickness/2, 0, 2*M_PI);
797 cairo_stroke(window->cairo); 798 cairo_stroke(window->cairo);
798 cairo_arc(window->cairo, wwidth/2, wheight/2, ARC_RADIUS + ARC_THICKNESS/2, 0, 2*M_PI); 799 cairo_arc(window->cairo, wwidth/2, wheight/2, config->radius + config->thickness/2, 0, 2*M_PI);
799 cairo_stroke(window->cairo); 800 cairo_stroke(window->cairo);
800 } 801 }
801 window_render(window); 802 window_render(window);