summaryrefslogtreecommitdiff
path: root/i3lock.c
diff options
context:
space:
mode:
authorVytautas Šaltenis <vytas@rtfb.lt>2014-03-06 22:22:02 +0200
committerMichael Stapelberg <michael@stapelberg.de>2014-03-11 09:11:20 +0100
commit2509c987d1d01a1890521a0c9d087dedbc7103dc (patch)
tree30038523f989d8e2d1dee443c8e97fc1f4d8ca9d /i3lock.c
parentd91fdc368e4e119f4673e23741dbf2718624de45 (diff)
Extract start/stop timer code to reusable funcs
Diffstat (limited to 'i3lock.c')
-rw-r--r--i3lock.c45
1 files changed, 41 insertions, 4 deletions
diff --git a/i3lock.c b/i3lock.c
index 28a93f0..1599282 100644
--- a/i3lock.c
+++ b/i3lock.c
@@ -34,6 +34,13 @@
#include "unlock_indicator.h"
#include "xinerama.h"
+#define START_TIMER(timer_obj, timeout, callback) \
+ timer_obj = start_timer(timer_obj, timeout, callback)
+#define STOP_TIMER(timer_obj) \
+ timer_obj = stop_timer(timer_obj)
+
+typedef void (*ev_callback_t)(EV_P_ ev_timer *w, int revents);
+
/* We need this for libxkbfile */
static Display *display;
char color[7] = "ffffff";
@@ -51,6 +58,7 @@ bool unlock_indicator = true;
static bool dont_fork = false;
struct ev_loop *main_loop;
static struct ev_timer *clear_pam_wrong_timeout;
+static struct ev_timer *clear_indicator_timeout;
extern unlock_state_t unlock_state;
extern pam_state_t pam_state;
@@ -178,6 +186,30 @@ static void clear_password_memory(void) {
vpassword[c] = c + (int)beep;
}
+ev_timer* start_timer(ev_timer *timer_obj, ev_tstamp timeout, ev_callback_t callback) {
+ if (timer_obj) {
+ ev_timer_stop(main_loop, timer_obj);
+ ev_timer_set(timer_obj, timeout, 0.);
+ ev_timer_start(main_loop, timer_obj);
+ } else {
+ /* When there is no memory, we just don’t have a timeout. We cannot
+ * exit() here, since that would effectively unlock the screen. */
+ timer_obj = calloc(sizeof(struct ev_timer), 1);
+ if (timer_obj) {
+ ev_timer_init(timer_obj, callback, timeout, 0.);
+ ev_timer_start(main_loop, timer_obj);
+ }
+ }
+ return timer_obj;
+}
+
+ev_timer* stop_timer(ev_timer *timer_obj) {
+ if (timer_obj) {
+ ev_timer_stop(main_loop, timer_obj);
+ free(timer_obj);
+ }
+ return NULL;
+}
/*
* Resets pam_state to STATE_PAM_IDLE 2 seconds after an unsuccesful
@@ -196,6 +228,11 @@ static void clear_pam_wrong(EV_P_ ev_timer *w, int revents) {
clear_pam_wrong_timeout = NULL;
}
+static void clear_indicator_cb(EV_P_ ev_timer *w, int revents) {
+ clear_indicator();
+ STOP_TIMER(clear_indicator_timeout);
+}
+
static void clear_input(void) {
input_position = 0;
clear_password_memory();
@@ -203,7 +240,7 @@ static void clear_input(void) {
/* Hide the unlock indicator after a bit if the password buffer is
* empty. */
- start_clear_indicator_timeout();
+ START_TIMER(clear_indicator_timeout, 1.0, clear_indicator_cb);
unlock_state = STATE_BACKSPACE_ACTIVE;
redraw_screen();
unlock_state = STATE_KEY_PRESSED;
@@ -245,7 +282,7 @@ static void input_done(void) {
/* Cancel the clear_indicator_timeout, it would hide the unlock indicator
* too early. */
- stop_clear_indicator_timeout();
+ STOP_TIMER(clear_indicator_timeout);
/* beep on authentication failure, if enabled */
if (beep) {
@@ -326,7 +363,7 @@ static void handle_key_press(xcb_key_press_event_t *event) {
/* Hide the unlock indicator after a bit if the password buffer is
* empty. */
- start_clear_indicator_timeout();
+ START_TIMER(clear_indicator_timeout, 1.0, clear_indicator_cb);
unlock_state = STATE_BACKSPACE_ACTIVE;
redraw_screen();
unlock_state = STATE_KEY_PRESSED;
@@ -365,7 +402,7 @@ static void handle_key_press(xcb_key_press_event_t *event) {
ev_timer_start(main_loop, timeout);
}
- stop_clear_indicator_timeout();
+ STOP_TIMER(clear_indicator_timeout);
}
/*