From da3cc9db3ee945aa32de774ef0b50e507728a895 Mon Sep 17 00:00:00 2001 From: James Magahern Date: Sun, 20 Jan 2019 00:53:03 -0800 Subject: Get default height/width from X11 --- src/main.c | 29 +++++++++++++++++++---------- src/x11_support.c | 6 ++++++ src/x11_support.h | 2 ++ 3 files changed, 27 insertions(+), 10 deletions(-) diff --git a/src/main.c b/src/main.c index fc00437..c869829 100644 --- a/src/main.c +++ b/src/main.c @@ -18,11 +18,15 @@ static const int kXSecureLockCharFD = 0; static const size_t kMaxPasswordLength = 128; +static const int kDefaultWidth = 800; +static const int kDefaultHeight = 600; + 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); /* @@ -53,7 +57,7 @@ static void handle_xsl_key_input(saver_state_t *state, const char c) // TODO: cursor movement break; case '\025': // Ctrl-U. - // TODO: clear line + clear_password(state); break; case 0: // Shouldn't happen. case '\033': // Escape. @@ -139,6 +143,11 @@ static int poll_events(saver_state_t *state) * Actions */ +static void clear_password(saver_state_t *state) +{ + state->password_buffer[0] = '\0'; +} + static void accept_password(saver_state_t *state) { state->cursor_animating = false; @@ -189,7 +198,7 @@ void callback_authentication_result(int result, void *context) state->is_authenticated = true; } else { // Try again - state->password_buffer[0] = '\0'; + clear_password(state); } } @@ -200,7 +209,7 @@ void callback_authentication_result(int result, void *context) static void update(saver_state_t *state) { if (state->cursor_animating) { - const double cursor_fade_speed = 0.007; + const double cursor_fade_speed = 0.01; if (state->cursor_fade_direction > 0) { state->cursor_opacity += cursor_fade_speed; if (state->cursor_opacity > 1.0) { @@ -251,8 +260,11 @@ static int runloop(cairo_surface_t *surface) state.input_allowed = false; state.password_prompt = ""; state.is_authenticated = false; - state.canvas_width = 800; - state.canvas_height = 600; + + x11_get_display_bounds(&state.canvas_width, &state.canvas_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, @@ -293,8 +305,8 @@ static int runloop(cairo_surface_t *surface) int main(int argc, char **argv) { - int default_width = 800; - int default_height = 600; + int default_width = kDefaultWidth; + int default_height = kDefaultHeight; cairo_surface_t *surface = x11_helper_acquire_cairo_surface(default_width, default_height); if (surface == NULL) { @@ -306,9 +318,6 @@ int main(int argc, char **argv) int flags = fcntl(kXSecureLockCharFD, F_GETFL, 0); fcntl(kXSecureLockCharFD, F_SETFL, flags | O_NONBLOCK); - // Docs say this must be called whenever the size of the window changes - cairo_xlib_surface_set_size(surface, default_width, default_height); - int result = runloop(surface); x11_helper_destroy_surface(surface); diff --git a/src/x11_support.c b/src/x11_support.c index 8ab05c1..3683b5a 100644 --- a/src/x11_support.c +++ b/src/x11_support.c @@ -39,6 +39,12 @@ static Window get_window_from_environment_or_make_one(Display *display, int widt return window; } +void x11_get_display_bounds(int *width, int *height) +{ + *width = DisplayWidth(__display, DefaultScreen(__display)); + *height = DisplayHeight(__display, DefaultScreen(__display)); +} + cairo_surface_t* x11_helper_acquire_cairo_surface(int width, int height) { __display = XOpenDisplay(NULL); diff --git a/src/x11_support.h b/src/x11_support.h index 2c24c98..6421f4b 100644 --- a/src/x11_support.h +++ b/src/x11_support.h @@ -12,6 +12,8 @@ #include #include +void x11_get_display_bounds(int *width, int *height); + // Sets up a window and returns a cairo_surface to draw onto cairo_surface_t* x11_helper_acquire_cairo_surface(int width, int height); -- cgit v1.2.3