Fix lock sequence to actually work

This commit is contained in:
Mattias Eriksson 2018-04-28 09:38:11 +02:00
parent 72054eba22
commit 20c113952a

View file

@ -57,51 +57,58 @@ bool have_lock() {
return false; return false;
} }
static int fd = -1; static int lock_fd = -1;
static int ongoing_fd = -1;
static int inhibit_cnt=0; static int inhibit_cnt=0;
void cleanup_inhibit() { void release_lock() {
if (fd >= 0) wlr_log(L_DEBUG, "Release lock %d", ongoing_fd);
close(fd); //Release lock if (ongoing_fd >= 0)
fd = -1; close(ongoing_fd);
ongoing_fd = -1;
inhibit_cnt=0; inhibit_cnt=0;
wlr_log(L_DEBUG, "Cleanup inhibit");
return; return;
} }
static int check_for_lock(void *data) { static int check_for_lockscreen(void *data) {
struct sway_server *server = data; struct sway_server *server = data;
if(have_lock()) { //If we for some reason already have a lockscreen if(have_lock()) { //If we for some reason already have a lockscreen
wlr_log(L_INFO, "Got lock, will release inhibit lock"); wlr_log(L_INFO, "Got lock, will release inhibit lock");
cleanup_inhibit(); release_lock();
return 0; return 0;
} }
if(inhibit_cnt > 4) { if(inhibit_cnt > 4) {
wlr_log(L_INFO, "Reached inhibit timeout, releasing lock"); wlr_log(L_INFO, "Reached inhibit timeout, releasing lock");
cleanup_inhibit(); release_lock();
return 0; return 0;
} }
inhibit_cnt++; inhibit_cnt++;
struct wl_event_source *source = wl_event_loop_add_timer(server->wl_event_loop, check_for_lock, server); struct wl_event_source *source = wl_event_loop_add_timer(server->wl_event_loop, check_for_lockscreen, server);
wl_event_source_timer_update(source, 100); wl_event_source_timer_update(source, 100);
return 0; return 0;
} }
static void prepare_for_sleep(struct wlr_session *session, void *data) { static void prepare_for_sleep(struct wlr_session *session, bool going_down, void *data) {
struct sway_server *server = data; struct sway_server *server = data;
wlr_log(L_INFO, "PrepareForSleep signal received"); wlr_log(L_INFO, "PrepareForSleep signal received %d", going_down);
fd = wlr_session_inhibit_sleep(session); if (!going_down) {
if(have_lock()) { //If we for some reason already have a lockscreen lock_fd = wlr_session_aquire_sleep_lock(session);
wlr_log(L_INFO, "Already have lock, no inhibit"); wlr_log(L_INFO, "Got new lock %d", lock_fd);
cleanup_inhibit();
return; return;
} }
ongoing_fd = lock_fd;
if(have_lock()) { //If we for some reason already have a lockscreen
wlr_log(L_INFO, "Already have lock, no action");
release_lock();
return;
}
wlr_log(L_INFO, "Starting lockscreen, holding lock %d", ongoing_fd);
invoke_swaylock(); invoke_swaylock();
if (fd>=0) { if (ongoing_fd>=0) {
struct wl_event_source *source = wl_event_loop_add_timer(server->wl_event_loop, check_for_lock, server); struct wl_event_source *source = wl_event_loop_add_timer(server->wl_event_loop, check_for_lockscreen, server);
wl_event_source_timer_update(source, 100); wl_event_source_timer_update(source, 100);
} }
return; return;
@ -119,6 +126,8 @@ void setup_sleep_listener(struct sway_server *server) {
} }
wlr_session_prepare_for_sleep_listen(session, prepare_for_sleep, server); wlr_session_prepare_for_sleep_listen(session, prepare_for_sleep, server);
lock_fd = wlr_session_aquire_sleep_lock(session);
wlr_log(L_INFO, "Got initial lock %d", lock_fd);
} }
static int handle_idle(void* data) { static int handle_idle(void* data) {