diff options
Diffstat (limited to 'swaylock/password.c')
-rw-r--r-- | swaylock/password.c | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/swaylock/password.c b/swaylock/password.c new file mode 100644 index 00000000..da67205d --- /dev/null +++ b/swaylock/password.c | |||
@@ -0,0 +1,57 @@ | |||
1 | #include <assert.h> | ||
2 | #include <stdlib.h> | ||
3 | #include <unistd.h> | ||
4 | #include <wlr/util/log.h> | ||
5 | #include <xkbcommon/xkbcommon.h> | ||
6 | #include "swaylock/swaylock.h" | ||
7 | #include "swaylock/seat.h" | ||
8 | #include "unicode.h" | ||
9 | |||
10 | static void backspace(struct swaylock_password *pw) { | ||
11 | if (pw->len != 0) { | ||
12 | pw->buffer[--pw->len] = 0; | ||
13 | } | ||
14 | } | ||
15 | |||
16 | static void append_ch(struct swaylock_password *pw, uint32_t codepoint) { | ||
17 | if (!pw->buffer) { | ||
18 | pw->size = 8; | ||
19 | if (!(pw->buffer = malloc(pw->size))) { | ||
20 | // TODO: Display error | ||
21 | return; | ||
22 | } | ||
23 | pw->buffer[0] = 0; | ||
24 | } | ||
25 | size_t utf8_size = utf8_chsize(codepoint); | ||
26 | if (pw->len + utf8_size + 1 >= pw->size) { | ||
27 | size_t size = pw->size * 2; | ||
28 | char *buffer = realloc(pw->buffer, size); | ||
29 | if (!buffer) { | ||
30 | // TODO: Display error | ||
31 | return; | ||
32 | } | ||
33 | pw->size = size; | ||
34 | pw->buffer = buffer; | ||
35 | } | ||
36 | utf8_encode(&pw->buffer[pw->len], codepoint); | ||
37 | pw->buffer[pw->len + utf8_size] = 0; | ||
38 | pw->len += utf8_size; | ||
39 | } | ||
40 | |||
41 | void swaylock_handle_key(struct swaylock_state *state, | ||
42 | xkb_keysym_t keysym, uint32_t codepoint) { | ||
43 | switch (keysym) { | ||
44 | case XKB_KEY_KP_Enter: /* fallthrough */ | ||
45 | case XKB_KEY_Return: | ||
46 | // TODO: Attempt password | ||
47 | break; | ||
48 | case XKB_KEY_BackSpace: | ||
49 | backspace(&state->password); | ||
50 | break; | ||
51 | default: | ||
52 | if (codepoint) { | ||
53 | append_ch(&state->password, codepoint); | ||
54 | } | ||
55 | break; | ||
56 | } | ||
57 | } | ||