aboutsummaryrefslogtreecommitdiff
path: root/src/main.c
blob: cc690126bc76535cd9a943a527607ee8e28547a0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
/*
 * buzzsaver.c
 * 
 * Created 2019-01-16 by James Magahern <james@magahern.com>
 */

#include "auth.h"
#include "render.h"
#include "x11_support.h"

#include <fcntl.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>

static const int kXSecureLockCharFD = 0;

static const char *kDefaultFont = "Input Mono 22";

static inline saver_state_t* saver_state(void *c)
{
    return (saver_state_t *)c;
}

static void clear_password(saver_state_t *state);
static void accept_password(saver_state_t *state);

static int poll_events(saver_state_t *state);

static void handle_key_event(saver_state_t *state, XKeyEvent *event);
static void handle_xsl_key_input(saver_state_t *state, const char c);
static void window_changed_size(saver_state_t *state, XConfigureEvent *event);

static void ending_animation_completed(struct animation_t *animation, void *context);
static void authentication_accepted(saver_state_t *state);
static void authentication_rejected(saver_state_t *state);

static void update(saver_state_t *state);
static void draw(saver_state_t *state);
static int runloop(cairo_surface_t *surface);

void callback_show_info(const char *info_msg, void *context);
void callback_show_error(const char *error_msg, void *context);
void callback_prompt_user(const char *prompt, void *context);
void callback_authentication_result(int result, void *context);

/*
 * Event handling
 */

static void window_changed_size(saver_state_t *state, XConfigureEvent *event)
{
    state->canvas_width = event->width;
    state->canvas_height = event->height;

    cairo_xlib_surface_set_size(state->surface, event->width, event->height);
}

// The following two methods are separate methods for processing input:
// The first one `handle_xsl_key_input` is for handling input via the XSecureLock
// file descriptor, which basically gives us TTY keycodes. The second handles
// input via X11, which is really only used for testing (when the locker is being
// run inside a window during development).
static void handle_xsl_key_input(saver_state_t *state, const char c)
{
    if (!state->input_allowed) return;

    char *password_buf = state->password_buffer;
    size_t pw_len = strlen(password_buf);
    switch (c) {
        case '\b':      // Backspace.
            if (pw_len > 0) {
                password_buf[pw_len - 1] = '\0';
            }
            break;
        case '\177':  // Delete
            break;
        case '\001':  // Ctrl-A.
            // TODO: cursor movement
            break;
        case '\025':  // Ctrl-U.
            clear_password(state);
            break;
        case 0:       // Shouldn't happen.
        case '\033':  // Escape.
            break;
        case '\r':  // Return.
        case '\n':  // Return.
            accept_password(state);
            break;
        default:
            if (pw_len + 1 < kMaxPasswordLength) {
                password_buf[pw_len] = c;
                password_buf[pw_len + 1] = '\0';
            }
            break;
    }
}

// This input handler is only when the locker is being run in "X11 mode" for development
// (See comment above for why this is separate)
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);

    char *password_buf = state->password_buffer;
    size_t length = strlen(password_buf);
    if (XK_BackSpace == key) {
        // delete char
        if (length > 0) {
            password_buf[length - 1] = '\0';
        }
    } else if (XK_Return == key) {
        accept_password(state);
    } else if (strlen(keybuf) > 0) {
        if (length + 1 < kMaxPasswordLength) {
            password_buf[length] = keybuf[0];
            password_buf[length + 1] = '\0';
        }
    }

    set_layer_needs_draw(state, LAYER_PASSWORD, true);
}

static int poll_events(saver_state_t *state)
{
    XEvent e;
    const bool block_for_next_event = false;

    // Via xsecurelock, take this route
    char buf;
    ssize_t read_res = read(kXSecureLockCharFD, &buf, 1);
    if (read_res > 0) {
        handle_xsl_key_input(state, buf);
    }

    // Temp: this should be handled by x11_support
    Display *display = cairo_xlib_surface_get_display(state->surface);
    for (;;) {
        if (block_for_next_event || XPending(display)) {
            // XNextEvent blocks the caller until an event arrives
            XNextEvent(display, &e);
        } else {
            return 0;
        }

        switch (e.type) {
            case ConfigureNotify:
                window_changed_size(state, (XConfigureEvent *)&e);
                return 1;
            case ButtonPress:
                return -e.xbutton.button;
            case KeyPress:
                handle_key_event(state, (XKeyEvent *)&e);
                return 1;
            default:
                fprintf(stderr, "Dropping unhandled XEevent.type = %d.\n", e.type);
        }
    }

    return 0;
}

/*
 * Actions
 */

static void clear_password(saver_state_t *state)
{
    state->password_buffer[0] = '\0';
}

static void accept_password(saver_state_t *state)
{
    auth_prompt_response_t response;
    strncpy(response.response_buffer, state->password_buffer, MAX_RESPONSE_SIZE);
    response.response_code = 0;
    auth_attempt_authentication(state->auth_handle, response);

    // Block input until we hear back from the auth thread
    state->is_processing = true;
    state->input_allowed = false;

    // Spinner animation
    if (state->spinner_anim_key == ANIM_KEY_NOEXIST) {
        state->spinner_anim_key = schedule_animation(state, (animation_t) {
            .type = ASpinnerAnimation,
            .anim.spinner_anim = { 0 }
        });
    }

    // Update prompt
    set_password_prompt(state, "Authenticating...");
}

static void ending_animation_completed(struct animation_t *animation, void *context)
{
    saver_state_t *state = saver_state(context);
    state->is_authenticated = true;
}

static void authentication_accepted(saver_state_t *state)
{
    state->is_processing = false;
    set_password_prompt(state, "Welcome");
    clear_password(state);

    // Stop cursor animation
    animation_t *cursor_anim = get_animation_for_key(state, state->cursor_anim_key);
    if (cursor_anim) {
        cursor_anim->anim.cursor_anim.cursor_animating = false;
        state->cursor_opacity = 0.0;
    }

    animation_t out_animation = {
        .type = ALogoAnimation,
        .completion_func = ending_animation_completed,
        .completion_func_context = state,
        .direction = OUT,
    };
    schedule_animation(state, out_animation);
}

static void authentication_rejected(saver_state_t *state)
{
    animation_t flash_animation = {
        .type = ARedFlashAnimation,
        .direction = IN,
    };
    schedule_animation(state, flash_animation);

    clear_password(state);
}

/*
 * Auth callbacks
 */

void callback_show_info(const char *info_msg, void *context)
{
    set_password_prompt(saver_state(context), info_msg);
}

void callback_show_error(const char *error_msg, void *context)
{
    set_password_prompt(saver_state(context), error_msg);
}

void callback_prompt_user(const char *prompt, void *context)
{
    saver_state_t *state = saver_state(context);
    set_password_prompt(state, prompt);
    state->input_allowed = true;
    state->is_processing = false;
    set_layer_needs_draw(state, LAYER_PROMPT, true);
}

void callback_authentication_result(int result, void *context)
{
    saver_state_t *state = saver_state(context);
    if (result == 0) {
        authentication_accepted(state);
    } else {
        // Try again
        authentication_rejected(state);
    }
}

/*
 * Main drawing/update routines
 */

static void update(saver_state_t *state)
{
    update_animations(state);
    poll_events(state);
}

static void draw(saver_state_t *state)
{
    if (layer_needs_draw(state, LAYER_BACKGROUND)) {
        draw_background(state, 0, 0, state->canvas_width, state->canvas_height);
    }

    if (layer_needs_draw(state, LAYER_LOGO)) {
        draw_logo(state);
    }

    draw_password_field(state);

    // Automatically reset this after every draw call
    set_layer_needs_draw(state, LAYER_BACKGROUND, false);
}

static int runloop(cairo_surface_t *surface)
{
    cairo_t *cr = cairo_create(surface);

    // Initialize pango context
    PangoLayout *pango_layout = pango_cairo_create_layout(cr);
    PangoFontDescription *status_font = pango_font_description_from_string(kDefaultFont);

    saver_state_t state = { 0 };
    state.ctx = cr;
    state.surface = surface;
    state.cursor_opacity = 1.0;
    state.pango_layout = pango_layout;
    state.status_font = status_font;
    state.input_allowed = false;
    state.is_authenticated = false;
    state.is_processing = false;
    state.spinner_anim_key = ANIM_KEY_NOEXIST;

    // Add initial animations
    // Cursor animation -- repeats indefinitely
    animation_t cursor_animation = {
        .type = ACursorAnimation,
        .direction = OUT,
        .anim.cursor_anim = {
            .cursor_animating = true
        }
    };
    state.cursor_anim_key = schedule_animation(&state, cursor_animation);

    // Logo incoming animation
    animation_t logo_animation = {
        .type = ALogoAnimation,
        .direction = IN,
    };
    schedule_animation(&state, logo_animation);

    x11_display_bounds_t bounds;
    x11_get_display_bounds(get_preferred_monitor_num(), &bounds);
    state.canvas_width = bounds.width;
    state.canvas_height = bounds.height;

    // Docs say this must be called whenever the size of the window changes
    cairo_xlib_surface_set_size(surface, state.canvas_width, state.canvas_height);

    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
    const int frames_per_sec = 60;
    const long sleep_nsec = (1.0 / frames_per_sec) * 1000000000;
    struct timespec sleep_time = { 0, sleep_nsec };
    while (!state.is_authenticated) {
        update(&state);

        cairo_push_group(cr);
        
        draw(&state);
        
        cairo_pop_group_to_source(cr);

        cairo_paint(cr);
        cairo_surface_flush(surface);

        nanosleep(&sleep_time, NULL);
    }

    // Cleanup
    cairo_destroy(cr);

    return EXIT_SUCCESS;
}

int main(int argc, char **argv)
{
    cairo_surface_t *surface = x11_helper_acquire_cairo_surface();
    if (surface == NULL) {
        fprintf(stderr, "Error creating cairo surface\n");
        exit(1);
    }

    // Make it so reading from the xsecurelock file descriptor doesn't block
    int flags = fcntl(kXSecureLockCharFD, F_GETFL, 0);
    fcntl(kXSecureLockCharFD, F_SETFL, flags | O_NONBLOCK);

    int result = runloop(surface);

    x11_helper_destroy_surface(surface);
    return result;
}