summaryrefslogtreecommitdiff
path: root/swaylock/password.c
diff options
context:
space:
mode:
authorDrew DeVault <[email protected]>2018-04-03 14:31:30 -0400
committerDrew DeVault <[email protected]>2018-04-04 18:47:48 -0400
commit066143adef7adc6e76e43e1990db2f75fe984b42 (patch)
treef9509c14f04399bf02d2cc31ff62869a07691543 /swaylock/password.c
parent1fe3cb8965e70f8f05f28512e66d76c49453a196 (diff)
Add password buffer, refactor rendering/surfaces
Diffstat (limited to 'swaylock/password.c')
-rw-r--r--swaylock/password.c57
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 @@
+#include <assert.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <wlr/util/log.h>
+#include <xkbcommon/xkbcommon.h>
+#include "swaylock/swaylock.h"
+#include "swaylock/seat.h"
+#include "unicode.h"
+
+static void backspace(struct swaylock_password *pw) {
+ if (pw->len != 0) {
+ pw->buffer[--pw->len] = 0;
+ }
+}
+
+static void append_ch(struct swaylock_password *pw, uint32_t codepoint) {
+ if (!pw->buffer) {
+ pw->size = 8;
+ if (!(pw->buffer = malloc(pw->size))) {
+ // TODO: Display error
+ return;
+ }
+ pw->buffer[0] = 0;
+ }
+ size_t utf8_size = utf8_chsize(codepoint);
+ if (pw->len + utf8_size + 1 >= pw->size) {
+ size_t size = pw->size * 2;
+ char *buffer = realloc(pw->buffer, size);
+ if (!buffer) {
+ // TODO: Display error
+ return;
+ }
+ pw->size = size;
+ pw->buffer = buffer;
+ }
+ utf8_encode(&pw->buffer[pw->len], codepoint);
+ pw->buffer[pw->len + utf8_size] = 0;
+ pw->len += utf8_size;
+}
+
+void swaylock_handle_key(struct swaylock_state *state,
+ xkb_keysym_t keysym, uint32_t codepoint) {
+ switch (keysym) {
+ case XKB_KEY_KP_Enter: /* fallthrough */
+ case XKB_KEY_Return:
+ // TODO: Attempt password
+ break;
+ case XKB_KEY_BackSpace:
+ backspace(&state->password);
+ break;
+ default:
+ if (codepoint) {
+ append_ch(&state->password, codepoint);
+ }
+ break;
+ }
+}