Skip to content

Commit

Permalink
add screen lock functionality which is unlocked by pressing both forw…
Browse files Browse the repository at this point in the history
…ard and back buttons.
  • Loading branch information
xxxxxxxxxxxxx committed Dec 2, 2023
1 parent babe9a9 commit fdb6368
Show file tree
Hide file tree
Showing 4 changed files with 197 additions and 20 deletions.
2 changes: 2 additions & 0 deletions crates/core/src/view/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ pub fn toggle_main_menu(view: &mut dyn View, rect: Rectangle, enable: Option<boo
EntryKind::SubMenu("Rotate".to_string(), rotate),
EntryKind::Command("Take Screenshot".to_string(),
EntryId::TakeScreenshot),
EntryKind::Command("Lock Screen Input".to_string(),
EntryId::LockScreenInput),
EntryKind::Separator,
EntryKind::SubMenu("Applications".to_string(), apps),
EntryKind::Separator];
Expand Down
1 change: 1 addition & 0 deletions crates/core/src/view/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,7 @@ pub enum EntryId {
New,
Refresh,
TakeScreenshot,
LockScreenInput,
Reboot,
Quit,
}
Expand Down
85 changes: 85 additions & 0 deletions crates/emulator/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,10 @@ fn main() -> Result<(), Error> {

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

let (mut back_pressed, mut fwd_pressed) = (false, false);
let (mut swallow_back_release, mut swallow_fwd_release) = (false, false);
let mut is_screen_locked = false;

'outer: loop {
let mut event_pump = sdl_context.event_pump().unwrap();
while let Some(sdl_evt) = event_pump.poll_event() {
Expand Down Expand Up @@ -549,6 +553,10 @@ fn main() -> Result<(), Error> {
let notif = Notification::new(msg, &tx, &mut rq, &mut context);
view.children_mut().push(Box::new(notif) as Box<dyn View>);
},
Event::Select(EntryId::LockScreenInput) => {
is_screen_locked = true;
tx.send(Event::Toggle(ViewId::TopBottomBars)).ok();
}
Event::Notify(msg) => {
let notif = Notification::new(msg, &tx, &mut rq, &mut context);
view.children_mut().push(Box::new(notif) as Box<dyn View>);
Expand Down Expand Up @@ -577,13 +585,82 @@ fn main() -> Result<(), Error> {
}
}
},
Event::Device(DeviceEvent::Button { code, status: ButtonStatus::Pressed, .. }) => {
// IDK how we can catch the initial starting press.
// Seems impossible without delay.
if is_screen_locked {
match code {
ButtonCode::Backward => {
back_pressed = true;

// When both buttons are pressed during lock screen
// then unlock the screen.
if fwd_pressed {
is_screen_locked = false;
continue;
}
},
ButtonCode::Forward => {
fwd_pressed = true;

// When both buttons are pressed during lock screen
// then unlock the screen.
if back_pressed {
is_screen_locked = false;
continue;
}
},
_ => (),
}
}

handle_event(view.as_mut(), &evt, &tx, &mut bus, &mut rq, &mut context);
},
Event::Device(DeviceEvent::Button { code, status: ButtonStatus::Released, .. }) => {
match code {
ButtonCode::Backward => {
// User was holding both buttons, but released back first
// Now catch the foward release event and swallow it.
if fwd_pressed && back_pressed {
swallow_fwd_release = true;
}

back_pressed = false;

if swallow_back_release {
swallow_back_release = false;
continue;
}
},
ButtonCode::Forward => {
// Same but the user released forward first. We catch back release.
if fwd_pressed && back_pressed {
swallow_back_release = true;
}

fwd_pressed = false;

if swallow_fwd_release {
swallow_fwd_release = false;
continue
}
},
_ => (),
}

handle_event(view.as_mut(), &evt, &tx, &mut bus, &mut rq, &mut context);
},
Event::Device(DeviceEvent::RotateScreen(n)) => {
tx.send(Event::Select(EntryId::Rotate(n))).ok();
},
Event::Select(EntryId::Quit) => {
break 'outer;
},
_ => {
if is_screen_locked && is_gesture_event(&evt) {
// Skip this event
continue;
}
handle_event(view.as_mut(), &evt, &tx, &mut bus, &mut rq, &mut context);
},
}
Expand Down Expand Up @@ -615,3 +692,11 @@ fn main() -> Result<(), Error> {

Ok(())
}

fn is_gesture_event(evt: &Event) -> bool {
match evt {
Event::Gesture(_) => true,
_ => false,
}
}

129 changes: 109 additions & 20 deletions crates/plato/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,10 @@ pub fn run() -> Result<(), Error> {
BATTERY_REFRESH_INTERVAL, &tx, &mut tasks);
tx.send(Event::WakeUp).ok();

let (mut back_pressed, mut fwd_pressed) = (false, false);
let (mut swallow_back_release, mut swallow_fwd_release) = (false, false);
let mut is_screen_locked = false;

while let Ok(evt) = rx.recv() {
match evt {
Event::Device(de) => {
Expand All @@ -357,9 +361,74 @@ pub fn run() -> Result<(), Error> {
view.children_mut().push(Box::new(interm) as Box<dyn View>);
}
},
DeviceEvent::Button { code: ButtonCode::Light, status: ButtonStatus::Pressed, .. } => {
DeviceEvent::Button { code: ButtonCode::Light, status, .. } => {
tx.send(Event::ToggleFrontlight).ok();
},
DeviceEvent::Button { code, status: ButtonStatus::Pressed, .. } => {
// IDK how we can catch the initial starting press.
// Seems impossible without delay.
if is_screen_locked {
match code {
ButtonCode::Backward => {
back_pressed = true;

// When both buttons are pressed during lock screen
// then unlock the screen.
if fwd_pressed {
is_screen_locked = false;
continue;
}
},
ButtonCode::Forward => {
fwd_pressed = true;

// When both buttons are pressed during lock screen
// then unlock the screen.
if back_pressed {
is_screen_locked = false;
continue;
}
},
_ => (),
}
}

handle_event(view.as_mut(), &evt, &tx, &mut bus, &mut rq, &mut context);
},
DeviceEvent::Button { code, status: ButtonStatus::Released, .. } => {
match code {
ButtonCode::Backward => {
// User was holding both buttons, but released back first
// Now catch the foward release event and swallow it.
if fwd_pressed && back_pressed {
swallow_fwd_release = true;
}

back_pressed = false;

if swallow_back_release {
swallow_back_release = false;
continue;
}
},
ButtonCode::Forward => {
// Same but the user released forward first. We catch back release.
if fwd_pressed && back_pressed {
swallow_back_release = true;
}

fwd_pressed = false;

if swallow_fwd_release {
swallow_fwd_release = false;
continue
}
},
_ => (),
}

handle_event(view.as_mut(), &evt, &tx, &mut bus, &mut rq, &mut context);
},
DeviceEvent::CoverOn => {
if context.covered {
continue;
Expand Down Expand Up @@ -699,28 +768,32 @@ pub fn run() -> Result<(), Error> {
break;
},
GestureEvent::MultiTap(mut points) => {
if points[0].x > points[1].x {
points.swap(0, 1);
}
let rect = context.fb.rect();
let r1 = Region::from_point(points[0], rect,
context.settings.reader.strip_width,
context.settings.reader.corner_width);
let r2 = Region::from_point(points[1], rect,
context.settings.reader.strip_width,
context.settings.reader.corner_width);
match (r1, r2) {
(Region::Corner(DiagDir::SouthWest), Region::Corner(DiagDir::NorthEast)) => {
rq.add(RenderData::new(view.id(), context.fb.rect(), UpdateMode::Full));
},
(Region::Corner(DiagDir::NorthWest), Region::Corner(DiagDir::SouthEast)) => {
tx.send(Event::Select(EntryId::TakeScreenshot)).ok();
},
_ => (),
if !is_screen_locked {
if points[0].x > points[1].x {
points.swap(0, 1);
}
let rect = context.fb.rect();
let r1 = Region::from_point(points[0], rect,
context.settings.reader.strip_width,
context.settings.reader.corner_width);
let r2 = Region::from_point(points[1], rect,
context.settings.reader.strip_width,
context.settings.reader.corner_width);
match (r1, r2) {
(Region::Corner(DiagDir::SouthWest), Region::Corner(DiagDir::NorthEast)) => {
rq.add(RenderData::new(view.id(), context.fb.rect(), UpdateMode::Full));
},
(Region::Corner(DiagDir::NorthWest), Region::Corner(DiagDir::SouthEast)) => {
tx.send(Event::Select(EntryId::TakeScreenshot)).ok();
},
_ => (),
}
}
},
_ => {
handle_event(view.as_mut(), &evt, &tx, &mut bus, &mut rq, &mut context);
if !is_screen_locked {
handle_event(view.as_mut(), &evt, &tx, &mut bus, &mut rq, &mut context);
}
},
}
},
Expand Down Expand Up @@ -951,6 +1024,10 @@ pub fn run() -> Result<(), Error> {
let notif = Notification::new(msg, &tx, &mut rq, &mut context);
view.children_mut().push(Box::new(notif) as Box<dyn View>);
},
Event::Select(EntryId::LockScreenInput) => {
is_screen_locked = true;
tx.send(Event::Toggle(ViewId::TopBottomBars)).ok();
}
Event::CheckFetcher(..) |
Event::FetcherAddDocument(..) |
Event::FetcherRemoveDocument(..) |
Expand Down Expand Up @@ -988,6 +1065,10 @@ pub fn run() -> Result<(), Error> {
}
},
_ => {
if is_screen_locked && is_gesture_event(&evt) {
// Skip this event
continue;
}
handle_event(view.as_mut(), &evt, &tx, &mut bus, &mut rq, &mut context);
},
}
Expand Down Expand Up @@ -1026,3 +1107,11 @@ pub fn run() -> Result<(), Error> {

Ok(())
}

fn is_gesture_event(evt: &Event) -> bool {
match evt {
Event::Gesture(_) => true,
_ => false,
}
}

0 comments on commit fdb6368

Please sign in to comment.