Extract start/stop timer code to reusable funcs
Patch status: merged
Patch by Vytautas Šaltenis
To apply this patch, use:
curl http://cr.i3wm.org/patch/461/raw.patch | git am
b/i3lock.c
| 15 |
@@ -34,6 +34,13 @@ |
| 16 |
#include "unlock_indicator.h" |
| 17 |
#include "xinerama.h" |
| 18 |
|
| 19 |
+#define START_TIMER(timer_obj, timeout, callback) \ |
| 20 |
+ timer_obj = start_timer(timer_obj, timeout, callback) |
| 21 |
+#define STOP_TIMER(timer_obj) \ |
| 22 |
+ timer_obj = stop_timer(timer_obj) |
| 23 |
+ |
| 24 |
+typedef void (*ev_callback_t)(EV_P_ ev_timer *w, int revents); |
| 25 |
+ |
| 26 |
/* We need this for libxkbfile */ |
| 27 |
static Display *display; |
| 28 |
char color[7] = "ffffff"; |
| 29 |
@@ -51,6 +58,7 @@ bool unlock_indicator = true; |
| 30 |
static bool dont_fork = false; |
| 31 |
struct ev_loop *main_loop; |
| 32 |
static struct ev_timer *clear_pam_wrong_timeout; |
| 33 |
+static struct ev_timer *clear_indicator_timeout; |
| 34 |
extern unlock_state_t unlock_state; |
| 35 |
extern pam_state_t pam_state; |
| 36 |
|
| 37 |
@@ -178,6 +186,30 @@ static void clear_password_memory(void) {
|
| 38 |
vpassword[c] = c + (int)beep; |
| 39 |
} |
| 40 |
|
| 41 |
+ev_timer* start_timer(ev_timer *timer_obj, ev_tstamp timeout, ev_callback_t callback) {
|
| 42 |
+ if (timer_obj) {
|
| 43 |
+ ev_timer_stop(main_loop, timer_obj); |
| 44 |
+ ev_timer_set(timer_obj, timeout, 0.); |
| 45 |
+ ev_timer_start(main_loop, timer_obj); |
| 46 |
+ } else {
|
| 47 |
+ /* When there is no memory, we just don’t have a timeout. We cannot |
| 48 |
+ * exit() here, since that would effectively unlock the screen. */ |
| 49 |
+ timer_obj = calloc(sizeof(struct ev_timer), 1); |
| 50 |
+ if (timer_obj) {
|
| 51 |
+ ev_timer_init(timer_obj, callback, timeout, 0.); |
| 52 |
+ ev_timer_start(main_loop, timer_obj); |
| 53 |
+ } |
| 54 |
+ } |
| 55 |
+ return timer_obj; |
| 56 |
+} |
| 57 |
+ |
| 58 |
+ev_timer* stop_timer(ev_timer *timer_obj) {
|
| 59 |
+ if (timer_obj) {
|
| 60 |
+ ev_timer_stop(main_loop, timer_obj); |
| 61 |
+ free(timer_obj); |
| 62 |
+ } |
| 63 |
+ return NULL; |
| 64 |
+} |
| 65 |
|
| 66 |
/* |
| 67 |
* Resets pam_state to STATE_PAM_IDLE 2 seconds after an unsuccesful |
| 68 |
@@ -196,6 +228,11 @@ static void clear_pam_wrong(EV_P_ ev_timer *w, int revents) {
|
| 69 |
clear_pam_wrong_timeout = NULL; |
| 70 |
} |
| 71 |
|
| 72 |
+static void clear_indicator_cb(EV_P_ ev_timer *w, int revents) {
|
| 73 |
+ clear_indicator(); |
| 74 |
+ STOP_TIMER(clear_indicator_timeout); |
| 75 |
+} |
| 76 |
+ |
| 77 |
static void clear_input(void) {
|
| 78 |
input_position = 0; |
| 79 |
clear_password_memory(); |
| 80 |
@@ -203,7 +240,7 @@ static void clear_input(void) {
|
| 81 |
|
| 82 |
/* Hide the unlock indicator after a bit if the password buffer is |
| 83 |
* empty. */ |
| 84 |
- start_clear_indicator_timeout(); |
| 85 |
+ START_TIMER(clear_indicator_timeout, 1.0, clear_indicator_cb); |
| 86 |
unlock_state = STATE_BACKSPACE_ACTIVE; |
| 87 |
redraw_screen(); |
| 88 |
unlock_state = STATE_KEY_PRESSED; |
| 89 |
@@ -245,7 +282,7 @@ static void input_done(void) {
|
| 90 |
|
| 91 |
/* Cancel the clear_indicator_timeout, it would hide the unlock indicator |
| 92 |
* too early. */ |
| 93 |
- stop_clear_indicator_timeout(); |
| 94 |
+ STOP_TIMER(clear_indicator_timeout); |
| 95 |
|
| 96 |
/* beep on authentication failure, if enabled */ |
| 97 |
if (beep) {
|
| 98 |
@@ -326,7 +363,7 @@ static void handle_key_press(xcb_key_press_event_t *event) {
|
| 99 |
|
| 100 |
/* Hide the unlock indicator after a bit if the password buffer is |
| 101 |
* empty. */ |
| 102 |
- start_clear_indicator_timeout(); |
| 103 |
+ START_TIMER(clear_indicator_timeout, 1.0, clear_indicator_cb); |
| 104 |
unlock_state = STATE_BACKSPACE_ACTIVE; |
| 105 |
redraw_screen(); |
| 106 |
unlock_state = STATE_KEY_PRESSED; |
| 107 |
@@ -365,7 +402,7 @@ static void handle_key_press(xcb_key_press_event_t *event) {
|
| 108 |
ev_timer_start(main_loop, timeout); |
| 109 |
} |
| 110 |
|
| 111 |
- stop_clear_indicator_timeout(); |
| 112 |
+ STOP_TIMER(clear_indicator_timeout); |
| 113 |
} |
| 114 |
|
| 115 |
/* |
b/unlock_indicator.c
| 120 |
@@ -31,9 +31,6 @@ |
| 121 |
* characters of the password have already been entered or not. */ |
| 122 |
int input_position; |
| 123 |
|
| 124 |
-/* The ev main loop. */ |
| 125 |
-struct ev_loop *main_loop; |
| 126 |
- |
| 127 |
/* The lock window. */ |
| 128 |
extern xcb_window_t win; |
| 129 |
|
| 130 |
@@ -55,8 +52,6 @@ extern char color[7]; |
| 131 |
* Local variables. |
| 132 |
******************************************************************************/ |
| 133 |
|
| 134 |
-static struct ev_timer *clear_indicator_timeout; |
| 135 |
- |
| 136 |
/* Cache the screen’s visual, necessary for creating a Cairo context. */ |
| 137 |
static xcb_visualtype_t *vistype; |
| 138 |
|
| 139 |
@@ -279,45 +274,9 @@ void redraw_screen(void) {
|
| 140 |
* password buffer. |
| 141 |
* |
| 142 |
*/ |
| 143 |
-static void clear_indicator(EV_P_ ev_timer *w, int revents) {
|
| 144 |
+void clear_indicator(void) {
|
| 145 |
if (input_position == 0) {
|
| 146 |
unlock_state = STATE_STARTED; |
| 147 |
} else unlock_state = STATE_KEY_PRESSED; |
| 148 |
redraw_screen(); |
| 149 |
- |
| 150 |
- ev_timer_stop(main_loop, clear_indicator_timeout); |
| 151 |
- free(clear_indicator_timeout); |
| 152 |
- clear_indicator_timeout = NULL; |
| 153 |
-} |
| 154 |
- |
| 155 |
-/* |
| 156 |
- * (Re-)starts the clear_indicator timeout. Called after pressing backspace or |
| 157 |
- * after an unsuccessful authentication attempt. |
| 158 |
- * |
| 159 |
- */ |
| 160 |
-void start_clear_indicator_timeout(void) {
|
| 161 |
- if (clear_indicator_timeout) {
|
| 162 |
- ev_timer_stop(main_loop, clear_indicator_timeout); |
| 163 |
- ev_timer_set(clear_indicator_timeout, 1.0, 0.); |
| 164 |
- ev_timer_start(main_loop, clear_indicator_timeout); |
| 165 |
- } else {
|
| 166 |
- /* When there is no memory, we just don’t have a timeout. We cannot |
| 167 |
- * exit() here, since that would effectively unlock the screen. */ |
| 168 |
- if (!(clear_indicator_timeout = calloc(sizeof(struct ev_timer), 1))) |
| 169 |
- return; |
| 170 |
- ev_timer_init(clear_indicator_timeout, clear_indicator, 1.0, 0.); |
| 171 |
- ev_timer_start(main_loop, clear_indicator_timeout); |
| 172 |
- } |
| 173 |
-} |
| 174 |
- |
| 175 |
-/* |
| 176 |
- * Stops the clear_indicator timeout. |
| 177 |
- * |
| 178 |
- */ |
| 179 |
-void stop_clear_indicator_timeout(void) {
|
| 180 |
- if (clear_indicator_timeout) {
|
| 181 |
- ev_timer_stop(main_loop, clear_indicator_timeout); |
| 182 |
- free(clear_indicator_timeout); |
| 183 |
- clear_indicator_timeout = NULL; |
| 184 |
- } |
| 185 |
} |
b/unlock_indicator.h
| 190 |
@@ -18,7 +18,6 @@ typedef enum {
|
| 191 |
|
| 192 |
xcb_pixmap_t draw_image(uint32_t* resolution); |
| 193 |
void redraw_screen(void); |
| 194 |
-void start_clear_indicator_timeout(void); |
| 195 |
-void stop_clear_indicator_timeout(void); |
| 196 |
+void clear_indicator(void); |
| 197 |
|
| 198 |
#endif |