Skip to content

Commit

Permalink
Add support for automatic screen locking
Browse files Browse the repository at this point in the history
  • Loading branch information
Kilobyte22 committed Mar 21, 2016
1 parent 42ac1cb commit be52554
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 7 deletions.
16 changes: 16 additions & 0 deletions src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,22 @@ pub fn actor_api(core: Sender<CoreMessage>) {
core.send(CoreMessage::Exit).unwrap();
Ok(vec![m.method_return()])
})
).add_m(
f.method("AutoLock", |m, _, _| {
core.send(CoreMessage::AutoLock).unwrap();
Ok(vec![m.method_return()])
})
).add_m(
f.method("SetAutoLock", |m, _, _| {
core.send(CoreMessage::SetAutoLock(m.get1().unwrap())).unwrap();
Ok(vec![m.method_return()])
}).inarg::<bool, _>("value")
).add_m(
f.method("GetAutoLock", |m, _, _| {
let (tx, rx) = mpsc::channel::<bool>();
core.send(CoreMessage::QueryFlag(CoreFlag::AutoLock, tx)).unwrap();
Ok(vec![m.method_return().append1(rx.recv().unwrap())])
}).outarg::<bool, _>("value")
)
)
);
Expand Down
16 changes: 16 additions & 0 deletions src/control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,22 @@ fn exec() -> Result<(), Error> {
println!("ignore")
}
},
"autolock" => if args.len() > 2 {
let b: &str = &args[2];
match b {
"on" => try!(basic_call(&c, method("SetAutoLock").append1(true))),
"off" => try!(basic_call(&c, method("SetAutoLock").append1(false))),
_ => usage()
}
} else {
let r = try!(call(&c, method("GetAutoLock")));
if r.get1().unwrap() {
println!("on")
} else {
println!("off")
}
},
"perform_autolock" => try!(basic_call(&c, method("AutoLock"))),
_ => usage()
};
} else {
Expand Down
19 changes: 15 additions & 4 deletions src/daemon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ struct State {
locked: bool,
inhibit_lid: bool,
locking: bool,
should_exit: bool
should_exit: bool,
autolock: bool
}

fn main() {
Expand Down Expand Up @@ -64,7 +65,8 @@ fn actor_main(handles: ActorMainHandles) {
locked: false,
inhibit_lid: false,
locking: false,
should_exit: false
should_exit: false,
autolock: true
};
handles.inhibitors.send(InhibitMessage::CreateDelay).unwrap();
for message in handles.inbox {
Expand Down Expand Up @@ -125,10 +127,19 @@ fn actor_main(handles: ActorMainHandles) {
},
CoreMessage::QueryFlag(flag, channel) => {
channel.send(match flag {
CoreFlag::SuspendOnLid => !state.inhibit_lid
CoreFlag::SuspendOnLid => !state.inhibit_lid,
//CoreFlag::Locking => state.locking,
//CoreFlag::Locked => state.locked
//CoreFlag::Locked => state.locked,
CoreFlag::AutoLock => state.autolock
}).unwrap();
},
CoreMessage::AutoLock => {
if !state.locked && !state.locking && state.autolock {
handles.lockscreen.send(LockMessage::Lock).unwrap();
}
},
CoreMessage::SetAutoLock(value) => {
state.autolock = value;
}
}
}
Expand Down
9 changes: 6 additions & 3 deletions src/msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,15 @@ pub enum CoreMessage {
SuspendOnLid(bool),
Suspending,
Suspended,
QueryFlag(CoreFlag, Sender<bool>)
QueryFlag(CoreFlag, Sender<bool>),
AutoLock,
SetAutoLock(bool)
}

#[derive(Debug)]
pub enum CoreFlag {
SuspendOnLid
SuspendOnLid,
//Locking, TODO: Add dbus api
//Locked TODO: Add dbus api
//Locked,
AutoLock
}

0 comments on commit be52554

Please sign in to comment.