diff --git a/common/loop.c b/common/loop.c index bfbfd5a6..da3c2142 100644 --- a/common/loop.c +++ b/common/loop.c @@ -12,6 +12,7 @@ struct loop_event { void (*callback)(int fd, short mask, void *data); void *data; + bool is_timer; }; struct loop { @@ -52,6 +53,11 @@ void loop_poll(struct loop *loop) { if (pfd.revents & events) { event->callback(pfd.fd, pfd.revents, event->data); + + if (event->is_timer) { + loop_remove_event(loop, event); + --i; + } } } } @@ -82,11 +88,14 @@ struct loop_event *loop_add_timer(struct loop *loop, int ms, struct itimerspec its; its.it_interval.tv_sec = 0; its.it_interval.tv_nsec = 0; - its.it_value.tv_sec = ms / 1000000000; - its.it_value.tv_nsec = (ms * 1000000) % 1000000000; + its.it_value.tv_sec = ms / 1000; + its.it_value.tv_nsec = (ms % 1000) * 1000000; timerfd_settime(fd, 0, &its, NULL); - return loop_add_fd(loop, fd, POLLIN, callback, data); + struct loop_event *event = loop_add_fd(loop, fd, POLLIN, callback, data); + event->is_timer = true; + + return event; } bool loop_remove_event(struct loop *loop, struct loop_event *event) { @@ -94,6 +103,10 @@ bool loop_remove_event(struct loop *loop, struct loop_event *event) { if (loop->events->items[i] == event) { list_del(loop->events, i); + if (event->is_timer) { + close(loop->fds[i].fd); + } + loop->fd_length--; memmove(&loop->fds[i], &loop->fds[i + 1], sizeof(void*) * (loop->fd_length - i)); diff --git a/include/swaylock/swaylock.h b/include/swaylock/swaylock.h index c8732cb7..792d34cf 100644 --- a/include/swaylock/swaylock.h +++ b/include/swaylock/swaylock.h @@ -55,6 +55,7 @@ struct swaylock_password { struct swaylock_state { struct loop *eventloop; + struct loop_event *clear_indicator_timer; // clears the indicator struct wl_display *display; struct wl_compositor *compositor; struct zwlr_layer_shell_v1 *layer_shell; diff --git a/swaylock/password.c b/swaylock/password.c index 50b81f6b..fb610bf2 100644 --- a/swaylock/password.c +++ b/swaylock/password.c @@ -8,6 +8,7 @@ #include #include "swaylock/swaylock.h" #include "swaylock/seat.h" +#include "loop.h" #include "unicode.h" void clear_password_buffer(struct swaylock_password *pw) { @@ -39,6 +40,21 @@ static void append_ch(struct swaylock_password *pw, uint32_t codepoint) { pw->len += utf8_size; } +static void clear_indicator(int fd, short mask, void *data) { + struct swaylock_state *state = data; + state->clear_indicator_timer = NULL; + state->auth_state = AUTH_STATE_IDLE; + damage_state(state); +} + +static void schedule_indicator_clear(struct swaylock_state *state) { + if (state->clear_indicator_timer) { + loop_remove_event(state->eventloop, state->clear_indicator_timer); + } + state->clear_indicator_timer = loop_add_timer( + state->eventloop, 3000, clear_indicator, state); +} + void swaylock_handle_key(struct swaylock_state *state, xkb_keysym_t keysym, uint32_t codepoint) { switch (keysym) { @@ -79,11 +95,13 @@ void swaylock_handle_key(struct swaylock_state *state, state->auth_state = AUTH_STATE_CLEAR; } damage_state(state); + schedule_indicator_clear(state); break; case XKB_KEY_Escape: clear_password_buffer(&state->password); state->auth_state = AUTH_STATE_CLEAR; damage_state(state); + schedule_indicator_clear(state); break; case XKB_KEY_Caps_Lock: /* The state is getting active after this @@ -91,6 +109,7 @@ void swaylock_handle_key(struct swaylock_state *state, state->xkb.caps_lock = !state->xkb.caps_lock; state->auth_state = AUTH_STATE_INPUT_NOP; damage_state(state); + schedule_indicator_clear(state); break; case XKB_KEY_Shift_L: case XKB_KEY_Shift_R: @@ -104,12 +123,14 @@ void swaylock_handle_key(struct swaylock_state *state, case XKB_KEY_Super_R: state->auth_state = AUTH_STATE_INPUT_NOP; damage_state(state); + schedule_indicator_clear(state); break; case XKB_KEY_u: if (state->xkb.control) { clear_password_buffer(&state->password); state->auth_state = AUTH_STATE_CLEAR; damage_state(state); + schedule_indicator_clear(state); break; } // fallthrough @@ -118,6 +139,7 @@ void swaylock_handle_key(struct swaylock_state *state, append_ch(&state->password, codepoint); state->auth_state = AUTH_STATE_INPUT; damage_state(state); + schedule_indicator_clear(state); } break; }