Skip to content

Commit fdb6368

Browse files
committed
add screen lock functionality which is unlocked by pressing both forward and back buttons.
1 parent babe9a9 commit fdb6368

File tree

4 files changed

+197
-20
lines changed

4 files changed

+197
-20
lines changed

crates/core/src/view/common.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,8 @@ pub fn toggle_main_menu(view: &mut dyn View, rect: Rectangle, enable: Option<boo
105105
EntryKind::SubMenu("Rotate".to_string(), rotate),
106106
EntryKind::Command("Take Screenshot".to_string(),
107107
EntryId::TakeScreenshot),
108+
EntryKind::Command("Lock Screen Input".to_string(),
109+
EntryId::LockScreenInput),
108110
EntryKind::Separator,
109111
EntryKind::SubMenu("Applications".to_string(), apps),
110112
EntryKind::Separator];

crates/core/src/view/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -574,6 +574,7 @@ pub enum EntryId {
574574
New,
575575
Refresh,
576576
TakeScreenshot,
577+
LockScreenInput,
577578
Reboot,
578579
Quit,
579580
}

crates/emulator/src/main.rs

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,10 @@ fn main() -> Result<(), Error> {
281281

282282
let mut bus = VecDeque::with_capacity(4);
283283

284+
let (mut back_pressed, mut fwd_pressed) = (false, false);
285+
let (mut swallow_back_release, mut swallow_fwd_release) = (false, false);
286+
let mut is_screen_locked = false;
287+
284288
'outer: loop {
285289
let mut event_pump = sdl_context.event_pump().unwrap();
286290
while let Some(sdl_evt) = event_pump.poll_event() {
@@ -549,6 +553,10 @@ fn main() -> Result<(), Error> {
549553
let notif = Notification::new(msg, &tx, &mut rq, &mut context);
550554
view.children_mut().push(Box::new(notif) as Box<dyn View>);
551555
},
556+
Event::Select(EntryId::LockScreenInput) => {
557+
is_screen_locked = true;
558+
tx.send(Event::Toggle(ViewId::TopBottomBars)).ok();
559+
}
552560
Event::Notify(msg) => {
553561
let notif = Notification::new(msg, &tx, &mut rq, &mut context);
554562
view.children_mut().push(Box::new(notif) as Box<dyn View>);
@@ -577,13 +585,82 @@ fn main() -> Result<(), Error> {
577585
}
578586
}
579587
},
588+
Event::Device(DeviceEvent::Button { code, status: ButtonStatus::Pressed, .. }) => {
589+
// IDK how we can catch the initial starting press.
590+
// Seems impossible without delay.
591+
if is_screen_locked {
592+
match code {
593+
ButtonCode::Backward => {
594+
back_pressed = true;
595+
596+
// When both buttons are pressed during lock screen
597+
// then unlock the screen.
598+
if fwd_pressed {
599+
is_screen_locked = false;
600+
continue;
601+
}
602+
},
603+
ButtonCode::Forward => {
604+
fwd_pressed = true;
605+
606+
// When both buttons are pressed during lock screen
607+
// then unlock the screen.
608+
if back_pressed {
609+
is_screen_locked = false;
610+
continue;
611+
}
612+
},
613+
_ => (),
614+
}
615+
}
616+
617+
handle_event(view.as_mut(), &evt, &tx, &mut bus, &mut rq, &mut context);
618+
},
619+
Event::Device(DeviceEvent::Button { code, status: ButtonStatus::Released, .. }) => {
620+
match code {
621+
ButtonCode::Backward => {
622+
// User was holding both buttons, but released back first
623+
// Now catch the foward release event and swallow it.
624+
if fwd_pressed && back_pressed {
625+
swallow_fwd_release = true;
626+
}
627+
628+
back_pressed = false;
629+
630+
if swallow_back_release {
631+
swallow_back_release = false;
632+
continue;
633+
}
634+
},
635+
ButtonCode::Forward => {
636+
// Same but the user released forward first. We catch back release.
637+
if fwd_pressed && back_pressed {
638+
swallow_back_release = true;
639+
}
640+
641+
fwd_pressed = false;
642+
643+
if swallow_fwd_release {
644+
swallow_fwd_release = false;
645+
continue
646+
}
647+
},
648+
_ => (),
649+
}
650+
651+
handle_event(view.as_mut(), &evt, &tx, &mut bus, &mut rq, &mut context);
652+
},
580653
Event::Device(DeviceEvent::RotateScreen(n)) => {
581654
tx.send(Event::Select(EntryId::Rotate(n))).ok();
582655
},
583656
Event::Select(EntryId::Quit) => {
584657
break 'outer;
585658
},
586659
_ => {
660+
if is_screen_locked && is_gesture_event(&evt) {
661+
// Skip this event
662+
continue;
663+
}
587664
handle_event(view.as_mut(), &evt, &tx, &mut bus, &mut rq, &mut context);
588665
},
589666
}
@@ -615,3 +692,11 @@ fn main() -> Result<(), Error> {
615692

616693
Ok(())
617694
}
695+
696+
fn is_gesture_event(evt: &Event) -> bool {
697+
match evt {
698+
Event::Gesture(_) => true,
699+
_ => false,
700+
}
701+
}
702+

crates/plato/src/app.rs

Lines changed: 109 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,10 @@ pub fn run() -> Result<(), Error> {
335335
BATTERY_REFRESH_INTERVAL, &tx, &mut tasks);
336336
tx.send(Event::WakeUp).ok();
337337

338+
let (mut back_pressed, mut fwd_pressed) = (false, false);
339+
let (mut swallow_back_release, mut swallow_fwd_release) = (false, false);
340+
let mut is_screen_locked = false;
341+
338342
while let Ok(evt) = rx.recv() {
339343
match evt {
340344
Event::Device(de) => {
@@ -357,9 +361,74 @@ pub fn run() -> Result<(), Error> {
357361
view.children_mut().push(Box::new(interm) as Box<dyn View>);
358362
}
359363
},
360-
DeviceEvent::Button { code: ButtonCode::Light, status: ButtonStatus::Pressed, .. } => {
364+
DeviceEvent::Button { code: ButtonCode::Light, status, .. } => {
361365
tx.send(Event::ToggleFrontlight).ok();
362366
},
367+
DeviceEvent::Button { code, status: ButtonStatus::Pressed, .. } => {
368+
// IDK how we can catch the initial starting press.
369+
// Seems impossible without delay.
370+
if is_screen_locked {
371+
match code {
372+
ButtonCode::Backward => {
373+
back_pressed = true;
374+
375+
// When both buttons are pressed during lock screen
376+
// then unlock the screen.
377+
if fwd_pressed {
378+
is_screen_locked = false;
379+
continue;
380+
}
381+
},
382+
ButtonCode::Forward => {
383+
fwd_pressed = true;
384+
385+
// When both buttons are pressed during lock screen
386+
// then unlock the screen.
387+
if back_pressed {
388+
is_screen_locked = false;
389+
continue;
390+
}
391+
},
392+
_ => (),
393+
}
394+
}
395+
396+
handle_event(view.as_mut(), &evt, &tx, &mut bus, &mut rq, &mut context);
397+
},
398+
DeviceEvent::Button { code, status: ButtonStatus::Released, .. } => {
399+
match code {
400+
ButtonCode::Backward => {
401+
// User was holding both buttons, but released back first
402+
// Now catch the foward release event and swallow it.
403+
if fwd_pressed && back_pressed {
404+
swallow_fwd_release = true;
405+
}
406+
407+
back_pressed = false;
408+
409+
if swallow_back_release {
410+
swallow_back_release = false;
411+
continue;
412+
}
413+
},
414+
ButtonCode::Forward => {
415+
// Same but the user released forward first. We catch back release.
416+
if fwd_pressed && back_pressed {
417+
swallow_back_release = true;
418+
}
419+
420+
fwd_pressed = false;
421+
422+
if swallow_fwd_release {
423+
swallow_fwd_release = false;
424+
continue
425+
}
426+
},
427+
_ => (),
428+
}
429+
430+
handle_event(view.as_mut(), &evt, &tx, &mut bus, &mut rq, &mut context);
431+
},
363432
DeviceEvent::CoverOn => {
364433
if context.covered {
365434
continue;
@@ -699,28 +768,32 @@ pub fn run() -> Result<(), Error> {
699768
break;
700769
},
701770
GestureEvent::MultiTap(mut points) => {
702-
if points[0].x > points[1].x {
703-
points.swap(0, 1);
704-
}
705-
let rect = context.fb.rect();
706-
let r1 = Region::from_point(points[0], rect,
707-
context.settings.reader.strip_width,
708-
context.settings.reader.corner_width);
709-
let r2 = Region::from_point(points[1], rect,
710-
context.settings.reader.strip_width,
711-
context.settings.reader.corner_width);
712-
match (r1, r2) {
713-
(Region::Corner(DiagDir::SouthWest), Region::Corner(DiagDir::NorthEast)) => {
714-
rq.add(RenderData::new(view.id(), context.fb.rect(), UpdateMode::Full));
715-
},
716-
(Region::Corner(DiagDir::NorthWest), Region::Corner(DiagDir::SouthEast)) => {
717-
tx.send(Event::Select(EntryId::TakeScreenshot)).ok();
718-
},
719-
_ => (),
771+
if !is_screen_locked {
772+
if points[0].x > points[1].x {
773+
points.swap(0, 1);
774+
}
775+
let rect = context.fb.rect();
776+
let r1 = Region::from_point(points[0], rect,
777+
context.settings.reader.strip_width,
778+
context.settings.reader.corner_width);
779+
let r2 = Region::from_point(points[1], rect,
780+
context.settings.reader.strip_width,
781+
context.settings.reader.corner_width);
782+
match (r1, r2) {
783+
(Region::Corner(DiagDir::SouthWest), Region::Corner(DiagDir::NorthEast)) => {
784+
rq.add(RenderData::new(view.id(), context.fb.rect(), UpdateMode::Full));
785+
},
786+
(Region::Corner(DiagDir::NorthWest), Region::Corner(DiagDir::SouthEast)) => {
787+
tx.send(Event::Select(EntryId::TakeScreenshot)).ok();
788+
},
789+
_ => (),
790+
}
720791
}
721792
},
722793
_ => {
723-
handle_event(view.as_mut(), &evt, &tx, &mut bus, &mut rq, &mut context);
794+
if !is_screen_locked {
795+
handle_event(view.as_mut(), &evt, &tx, &mut bus, &mut rq, &mut context);
796+
}
724797
},
725798
}
726799
},
@@ -951,6 +1024,10 @@ pub fn run() -> Result<(), Error> {
9511024
let notif = Notification::new(msg, &tx, &mut rq, &mut context);
9521025
view.children_mut().push(Box::new(notif) as Box<dyn View>);
9531026
},
1027+
Event::Select(EntryId::LockScreenInput) => {
1028+
is_screen_locked = true;
1029+
tx.send(Event::Toggle(ViewId::TopBottomBars)).ok();
1030+
}
9541031
Event::CheckFetcher(..) |
9551032
Event::FetcherAddDocument(..) |
9561033
Event::FetcherRemoveDocument(..) |
@@ -988,6 +1065,10 @@ pub fn run() -> Result<(), Error> {
9881065
}
9891066
},
9901067
_ => {
1068+
if is_screen_locked && is_gesture_event(&evt) {
1069+
// Skip this event
1070+
continue;
1071+
}
9911072
handle_event(view.as_mut(), &evt, &tx, &mut bus, &mut rq, &mut context);
9921073
},
9931074
}
@@ -1026,3 +1107,11 @@ pub fn run() -> Result<(), Error> {
10261107

10271108
Ok(())
10281109
}
1110+
1111+
fn is_gesture_event(evt: &Event) -> bool {
1112+
match evt {
1113+
Event::Gesture(_) => true,
1114+
_ => false,
1115+
}
1116+
}
1117+

0 commit comments

Comments
 (0)