From a3e54b6ef0ff4bfd8769cc27e7c1b83764befebd Mon Sep 17 00:00:00 2001 From: Jarkko Oranen Date: Mon, 5 Nov 2018 18:51:18 +0200 Subject: [PATCH] Implement a grace timer for swaylock --- include/swaylock/swaylock.h | 1 + swaylock/main.c | 21 ++++++++++++++++++++- swaylock/password.c | 4 ++++ swaylock/swaylock.1.scd | 3 +++ 4 files changed, 28 insertions(+), 1 deletion(-) diff --git a/include/swaylock/swaylock.h b/include/swaylock/swaylock.h index 18af7ab41..d5d939e86 100644 --- a/include/swaylock/swaylock.h +++ b/include/swaylock/swaylock.h @@ -70,6 +70,7 @@ struct swaylock_state { struct swaylock_xkb xkb; enum auth_state auth_state; bool run_display; + bool within_grace; struct zxdg_output_manager_v1 *zxdg_output_manager; }; diff --git a/swaylock/main.c b/swaylock/main.c index 9b74b6715..f27841322 100644 --- a/swaylock/main.c +++ b/swaylock/main.c @@ -430,6 +430,9 @@ enum line_mode { LM_RING, }; + +static int grace_time = 0; + static int parse_options(int argc, char **argv, struct swaylock_state *state, enum line_mode *line_mode, char **config_path) { enum long_option_codes { @@ -460,6 +463,7 @@ static int parse_options(int argc, char **argv, struct swaylock_state *state, static struct option long_options[] = { {"config", required_argument, NULL, 'C'}, {"color", required_argument, NULL, 'c'}, + {"grace", required_argument, NULL, 'g'}, {"ignore-empty-password", no_argument, NULL, 'e'}, {"daemonize", no_argument, NULL, 'f'}, {"help", no_argument, NULL, 'h'}, @@ -507,6 +511,8 @@ static int parse_options(int argc, char **argv, struct swaylock_state *state, "When an empty password is provided, do not validate it.\n" " -f, --daemonize " "Detach from the controlling terminal after locking.\n" + " -g --grace " + "Grace time in seconds before the display is actually locked" " -h, --help " "Show help message and quit.\n" " -i, --image [:] " @@ -577,7 +583,7 @@ static int parse_options(int argc, char **argv, struct swaylock_state *state, optind = 1; while (1) { int opt_idx = 0; - c = getopt_long(argc, argv, "c:efhi:nrs:tuvC:", long_options, &opt_idx); + c = getopt_long(argc, argv, "c:efg:hi:nrs:tuvC:", long_options, &opt_idx); if (c == -1) { break; } @@ -603,6 +609,9 @@ static int parse_options(int argc, char **argv, struct swaylock_state *state, state->args.daemonize = true; } break; + case 'g': + grace_time = atoi(optarg); + break; case 'i': if (state) { load_image(optarg, state); @@ -850,6 +859,10 @@ static void display_in(int fd, short mask, void *data) { } } +static void expire_grace(void *data) { + state.within_grace = false; +} + int main(int argc, char **argv) { wlr_log_init(WLR_DEBUG, NULL); initialize_pw_backend(); @@ -962,6 +975,12 @@ int main(int argc, char **argv) { } state.eventloop = loop_create(); + if (grace_time) { + state.within_grace = true; + loop_add_timer(state.eventloop, grace_time*1000, expire_grace, NULL); + } else { + state.within_grace = false; + } loop_add_fd(state.eventloop, wl_display_get_fd(state.display), POLL_IN, display_in, NULL); diff --git a/swaylock/password.c b/swaylock/password.c index 6138e1fef..5f5a294fb 100644 --- a/swaylock/password.c +++ b/swaylock/password.c @@ -80,6 +80,10 @@ static void handle_preverify_timeout(void *data) { void swaylock_handle_key(struct swaylock_state *state, xkb_keysym_t keysym, uint32_t codepoint) { + if (state->within_grace) { + state->run_display = false; + return; + } switch (keysym) { case XKB_KEY_KP_Enter: /* fallthrough */ case XKB_KEY_Return: diff --git a/swaylock/swaylock.1.scd b/swaylock/swaylock.1.scd index 8ddc7d3ae..3ab3b4095 100644 --- a/swaylock/swaylock.1.scd +++ b/swaylock/swaylock.1.scd @@ -29,6 +29,9 @@ Locks your Wayland session. Note: this is the default bahavior of i3lock. +*-g, --grace* + Set a grace time (in seconds) before the display is actually locked. + *-h, --help* Show help message and quit.