diff options
Diffstat (limited to 'src/main.c')
-rw-r--r-- | src/main.c | 78 |
1 files changed, 76 insertions, 2 deletions
@@ -4,6 +4,7 @@ * Created 2019-01-16 by James Magahern <james@magahern.com> */ +#include "auth.h" #include "render.h" #include "x11_support.h" @@ -15,6 +16,11 @@ static const size_t kMaxPasswordLength = 128; +static inline saver_state_t* saver_state(void *c) +{ + return (saver_state_t *)c; +} + static void accept_password(saver_state_t *state); /* @@ -31,6 +37,8 @@ static void window_changed_size(saver_state_t *state, XConfigureEvent *event) static void handle_key_event(saver_state_t *state, XKeyEvent *event) { + if (!state->input_allowed) return; + KeySym key; char keybuf[8]; XLookupString(event, keybuf, sizeof(keybuf), &key, NULL); @@ -47,7 +55,7 @@ static void handle_key_event(saver_state_t *state, XKeyEvent *event) } else if (strlen(keybuf) > 0) { size_t add_len = strlen(keybuf); if ( (length + add_len) < state->password_buffer_len - 1 ) { - strncpy(password_buf + length, keybuf, add_len); + strncpy(password_buf + length, keybuf, add_len + 1); } } } @@ -91,6 +99,55 @@ static int poll_events(saver_state_t *state) static void accept_password(saver_state_t *state) { state->cursor_animating = false; + + size_t pw_length = strlen(state->password_buffer); + char *password_buf = malloc(pw_length); + strncpy(password_buf, state->password_buffer, pw_length + 1); + + auth_prompt_response_t response; + response.response_buffer = password_buf; + response.response_code = 0; + auth_attempt_authentication(state->auth_handle, response); + + // Block input until we hear back from the auth thread + state->input_allowed = false; +} + +/* + * Auth callbacks + */ + +void callback_show_info(const char *info_msg, void *context) +{ + saver_state(context)->password_prompt = info_msg; +} + +void callback_show_error(const char *error_msg, void *context) +{ + saver_state(context)->password_prompt = error_msg; +} + +void callback_prompt_user(const char *prompt, void *context) +{ + size_t prompt_len = strlen(prompt); + char *new_prompt = malloc(prompt_len); + strncpy(new_prompt, prompt, prompt_len + 1); + + saver_state_t *state = saver_state(context); + state->password_prompt = new_prompt; + state->cursor_animating = true; + state->input_allowed = true; +} + +void callback_authentication_result(int result, void *context) +{ + saver_state_t *state = saver_state(context); + if (result == 0) { + state->is_authenticated = true; + } else { + // Try again + state->password_buffer[0] = '\0'; + } } /* @@ -147,7 +204,19 @@ static int runloop(cairo_surface_t *surface) state.status_font = status_font; state.password_buffer = calloc(1, kMaxPasswordLength); state.password_buffer_len = kMaxPasswordLength; - state.cursor_animating = true; + state.cursor_animating = false; + state.input_allowed = false; + state.password_prompt = ""; + state.is_authenticated = false; + + auth_callbacks_t callbacks = { + .info_handler = callback_show_info, + .error_handler = callback_show_error, + .prompt_handler = callback_prompt_user, + .result_handler = callback_authentication_result + }; + + state.auth_handle = auth_begin_authentication(callbacks, &state); // Main run loop struct timespec sleep_time = { 0, 5000000 }; @@ -164,6 +233,11 @@ static int runloop(cairo_surface_t *surface) cairo_surface_flush(surface); nanosleep(&sleep_time, NULL); + + if (state.is_authenticated) { + // We're done here + break; + } } // Cleanup |