diff options
author | Drew DeVault <sir@cmpwn.com> | 2018-10-14 16:34:22 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-10-14 16:34:22 +0200 |
commit | 53d90dd6a82205a20c3e97a8a396048588e1b5ef (patch) | |
tree | 10f5596fd0d90711af66281c38780c5a1d4d724f /swaylock | |
parent | Update README.MD (and README.*.md) (diff) | |
parent | Event loop: Fix memmove and remove extraneous declaration (diff) | |
download | sway-53d90dd6a82205a20c3e97a8a396048588e1b5ef.tar.gz sway-53d90dd6a82205a20c3e97a8a396048588e1b5ef.tar.zst sway-53d90dd6a82205a20c3e97a8a396048588e1b5ef.zip |
Merge pull request #2826 from RyanDwyer/common-eventloop
Implement common event loop for swaybar and swaylock
Diffstat (limited to 'swaylock')
-rw-r--r-- | swaylock/main.c | 14 | ||||
-rw-r--r-- | swaylock/password.c | 62 |
2 files changed, 73 insertions, 3 deletions
diff --git a/swaylock/main.c b/swaylock/main.c index d1384c6f..27bcfe32 100644 --- a/swaylock/main.c +++ b/swaylock/main.c | |||
@@ -21,6 +21,7 @@ | |||
21 | #include "pool-buffer.h" | 21 | #include "pool-buffer.h" |
22 | #include "cairo.h" | 22 | #include "cairo.h" |
23 | #include "log.h" | 23 | #include "log.h" |
24 | #include "loop.h" | ||
24 | #include "readline.h" | 25 | #include "readline.h" |
25 | #include "stringop.h" | 26 | #include "stringop.h" |
26 | #include "util.h" | 27 | #include "util.h" |
@@ -844,6 +845,10 @@ static int load_config(char *path, struct swaylock_state *state, | |||
844 | 845 | ||
845 | static struct swaylock_state state; | 846 | static struct swaylock_state state; |
846 | 847 | ||
848 | static void display_in(int fd, short mask, void *data) { | ||
849 | wl_display_dispatch(state.display); | ||
850 | } | ||
851 | |||
847 | int main(int argc, char **argv) { | 852 | int main(int argc, char **argv) { |
848 | wlr_log_init(WLR_DEBUG, NULL); | 853 | wlr_log_init(WLR_DEBUG, NULL); |
849 | initialize_pw_backend(); | 854 | initialize_pw_backend(); |
@@ -946,9 +951,14 @@ int main(int argc, char **argv) { | |||
946 | daemonize(); | 951 | daemonize(); |
947 | } | 952 | } |
948 | 953 | ||
954 | state.eventloop = loop_create(); | ||
955 | loop_add_fd(state.eventloop, wl_display_get_fd(state.display), POLL_IN, | ||
956 | display_in, NULL); | ||
957 | |||
949 | state.run_display = true; | 958 | state.run_display = true; |
950 | while (wl_display_dispatch(state.display) != -1 && state.run_display) { | 959 | while (state.run_display) { |
951 | // This space intentionally left blank | 960 | wl_display_flush(state.display); |
961 | loop_poll(state.eventloop); | ||
952 | } | 962 | } |
953 | 963 | ||
954 | free(state.args.font); | 964 | free(state.args.font); |
diff --git a/swaylock/password.c b/swaylock/password.c index 50b81f6b..fecaecbf 100644 --- a/swaylock/password.c +++ b/swaylock/password.c | |||
@@ -8,6 +8,7 @@ | |||
8 | #include <xkbcommon/xkbcommon.h> | 8 | #include <xkbcommon/xkbcommon.h> |
9 | #include "swaylock/swaylock.h" | 9 | #include "swaylock/swaylock.h" |
10 | #include "swaylock/seat.h" | 10 | #include "swaylock/seat.h" |
11 | #include "loop.h" | ||
11 | #include "unicode.h" | 12 | #include "unicode.h" |
12 | 13 | ||
13 | void clear_password_buffer(struct swaylock_password *pw) { | 14 | void clear_password_buffer(struct swaylock_password *pw) { |
@@ -39,6 +40,43 @@ static void append_ch(struct swaylock_password *pw, uint32_t codepoint) { | |||
39 | pw->len += utf8_size; | 40 | pw->len += utf8_size; |
40 | } | 41 | } |
41 | 42 | ||
43 | static void clear_indicator(void *data) { | ||
44 | struct swaylock_state *state = data; | ||
45 | state->clear_indicator_timer = NULL; | ||
46 | state->auth_state = AUTH_STATE_IDLE; | ||
47 | damage_state(state); | ||
48 | } | ||
49 | |||
50 | static void schedule_indicator_clear(struct swaylock_state *state) { | ||
51 | if (state->clear_indicator_timer) { | ||
52 | loop_remove_timer(state->eventloop, state->clear_indicator_timer); | ||
53 | } | ||
54 | state->clear_indicator_timer = loop_add_timer( | ||
55 | state->eventloop, 3000, clear_indicator, state); | ||
56 | } | ||
57 | |||
58 | static void clear_password(void *data) { | ||
59 | struct swaylock_state *state = data; | ||
60 | state->clear_password_timer = NULL; | ||
61 | state->auth_state = AUTH_STATE_CLEAR; | ||
62 | clear_password_buffer(&state->password); | ||
63 | damage_state(state); | ||
64 | schedule_indicator_clear(state); | ||
65 | } | ||
66 | |||
67 | static void schedule_password_clear(struct swaylock_state *state) { | ||
68 | if (state->clear_password_timer) { | ||
69 | loop_remove_timer(state->eventloop, state->clear_password_timer); | ||
70 | } | ||
71 | state->clear_password_timer = loop_add_timer( | ||
72 | state->eventloop, 10000, clear_password, state); | ||
73 | } | ||
74 | |||
75 | static void handle_preverify_timeout(void *data) { | ||
76 | struct swaylock_state *state = data; | ||
77 | state->verify_password_timer = NULL; | ||
78 | } | ||
79 | |||
42 | void swaylock_handle_key(struct swaylock_state *state, | 80 | void swaylock_handle_key(struct swaylock_state *state, |
43 | xkb_keysym_t keysym, uint32_t codepoint) { | 81 | xkb_keysym_t keysym, uint32_t codepoint) { |
44 | switch (keysym) { | 82 | switch (keysym) { |
@@ -50,7 +88,18 @@ void swaylock_handle_key(struct swaylock_state *state, | |||
50 | 88 | ||
51 | state->auth_state = AUTH_STATE_VALIDATING; | 89 | state->auth_state = AUTH_STATE_VALIDATING; |
52 | damage_state(state); | 90 | damage_state(state); |
53 | while (wl_display_dispatch(state->display) != -1 && state->run_display) { | 91 | |
92 | // We generally want to wait until all surfaces are showing the | ||
93 | // "verifying" state before we go and verify the password, because | ||
94 | // verifying it is a blocking operation. However, if the surface is on | ||
95 | // an output with DPMS off then it won't update, so we set a timer. | ||
96 | state->verify_password_timer = loop_add_timer( | ||
97 | state->eventloop, 50, handle_preverify_timeout, state); | ||
98 | |||
99 | while (state->run_display && state->verify_password_timer) { | ||
100 | wl_display_flush(state->display); | ||
101 | loop_poll(state->eventloop); | ||
102 | |||
54 | bool ok = 1; | 103 | bool ok = 1; |
55 | struct swaylock_surface *surface; | 104 | struct swaylock_surface *surface; |
56 | wl_list_for_each(surface, &state->surfaces, link) { | 105 | wl_list_for_each(surface, &state->surfaces, link) { |
@@ -70,6 +119,7 @@ void swaylock_handle_key(struct swaylock_state *state, | |||
70 | } | 119 | } |
71 | state->auth_state = AUTH_STATE_INVALID; | 120 | state->auth_state = AUTH_STATE_INVALID; |
72 | damage_state(state); | 121 | damage_state(state); |
122 | schedule_indicator_clear(state); | ||
73 | break; | 123 | break; |
74 | case XKB_KEY_Delete: | 124 | case XKB_KEY_Delete: |
75 | case XKB_KEY_BackSpace: | 125 | case XKB_KEY_BackSpace: |
@@ -79,11 +129,14 @@ void swaylock_handle_key(struct swaylock_state *state, | |||
79 | state->auth_state = AUTH_STATE_CLEAR; | 129 | state->auth_state = AUTH_STATE_CLEAR; |
80 | } | 130 | } |
81 | damage_state(state); | 131 | damage_state(state); |
132 | schedule_indicator_clear(state); | ||
133 | schedule_password_clear(state); | ||
82 | break; | 134 | break; |
83 | case XKB_KEY_Escape: | 135 | case XKB_KEY_Escape: |
84 | clear_password_buffer(&state->password); | 136 | clear_password_buffer(&state->password); |
85 | state->auth_state = AUTH_STATE_CLEAR; | 137 | state->auth_state = AUTH_STATE_CLEAR; |
86 | damage_state(state); | 138 | damage_state(state); |
139 | schedule_indicator_clear(state); | ||
87 | break; | 140 | break; |
88 | case XKB_KEY_Caps_Lock: | 141 | case XKB_KEY_Caps_Lock: |
89 | /* The state is getting active after this | 142 | /* The state is getting active after this |
@@ -91,6 +144,8 @@ void swaylock_handle_key(struct swaylock_state *state, | |||
91 | state->xkb.caps_lock = !state->xkb.caps_lock; | 144 | state->xkb.caps_lock = !state->xkb.caps_lock; |
92 | state->auth_state = AUTH_STATE_INPUT_NOP; | 145 | state->auth_state = AUTH_STATE_INPUT_NOP; |
93 | damage_state(state); | 146 | damage_state(state); |
147 | schedule_indicator_clear(state); | ||
148 | schedule_password_clear(state); | ||
94 | break; | 149 | break; |
95 | case XKB_KEY_Shift_L: | 150 | case XKB_KEY_Shift_L: |
96 | case XKB_KEY_Shift_R: | 151 | case XKB_KEY_Shift_R: |
@@ -104,12 +159,15 @@ void swaylock_handle_key(struct swaylock_state *state, | |||
104 | case XKB_KEY_Super_R: | 159 | case XKB_KEY_Super_R: |
105 | state->auth_state = AUTH_STATE_INPUT_NOP; | 160 | state->auth_state = AUTH_STATE_INPUT_NOP; |
106 | damage_state(state); | 161 | damage_state(state); |
162 | schedule_indicator_clear(state); | ||
163 | schedule_password_clear(state); | ||
107 | break; | 164 | break; |
108 | case XKB_KEY_u: | 165 | case XKB_KEY_u: |
109 | if (state->xkb.control) { | 166 | if (state->xkb.control) { |
110 | clear_password_buffer(&state->password); | 167 | clear_password_buffer(&state->password); |
111 | state->auth_state = AUTH_STATE_CLEAR; | 168 | state->auth_state = AUTH_STATE_CLEAR; |
112 | damage_state(state); | 169 | damage_state(state); |
170 | schedule_indicator_clear(state); | ||
113 | break; | 171 | break; |
114 | } | 172 | } |
115 | // fallthrough | 173 | // fallthrough |
@@ -118,6 +176,8 @@ void swaylock_handle_key(struct swaylock_state *state, | |||
118 | append_ch(&state->password, codepoint); | 176 | append_ch(&state->password, codepoint); |
119 | state->auth_state = AUTH_STATE_INPUT; | 177 | state->auth_state = AUTH_STATE_INPUT; |
120 | damage_state(state); | 178 | damage_state(state); |
179 | schedule_indicator_clear(state); | ||
180 | schedule_password_clear(state); | ||
121 | } | 181 | } |
122 | break; | 182 | break; |
123 | } | 183 | } |