1use std::sync::Arc;
2
3use limes_common::{
4 Config, EventBus, FrontendMode, FrontendRunner, FrontendSpec, LimesError, LockAuthBackend,
5 PamLockAuth, Result,
6};
7use limes_proto::{AuthOutcome, AuthRequest, LockState};
8
9use crate::display::{DisplayBackend, WaylandSessionLockBackend};
10use crate::manager::LockManager;
11
12pub struct LockRuntime {
13 config: Config,
14 lock: LockManager,
15 frontends: FrontendRunner,
16 events: EventBus,
17}
18
19impl LockRuntime {
20 pub fn from_env() -> Result<Self> {
21 Self::from_config(Config::from_env()?)
22 }
23
24 pub fn from_config(config: Config) -> Result<Self> {
25 let events = EventBus::from_env();
26 let auth: Arc<dyn LockAuthBackend> =
27 Arc::new(PamLockAuth::with_events(Some(events.clone())));
28 let display: Arc<dyn DisplayBackend> = Arc::new(WaylandSessionLockBackend::default());
29 Ok(Self::with_parts(config, auth, display, events))
30 }
31
32 pub fn for_frontend_from_env() -> Result<Self> {
35 Self::for_frontend(Config::from_env()?)
36 }
37
38 pub fn for_frontend(config: Config) -> Result<Self> {
39 let events = EventBus::from_env();
40 let auth = Arc::new(PamLockAuth::with_events(Some(events.clone())));
41 Ok(Self {
42 config,
43 lock: LockManager::for_frontend(auth, events.clone()),
44 frontends: FrontendRunner::new(events.clone()),
45 events,
46 })
47 }
48
49 #[must_use]
50 pub fn with_parts(
51 config: Config,
52 auth: Arc<dyn LockAuthBackend>,
53 display: Arc<dyn DisplayBackend>,
54 events: EventBus,
55 ) -> Self {
56 let lock = LockManager::with_lock_auth(display, auth, events.clone());
57 let frontends = FrontendRunner::new(events.clone());
58
59 Self {
60 config,
61 lock,
62 frontends,
63 events,
64 }
65 }
66
67 #[must_use]
68 pub fn config(&self) -> &Config {
69 &self.config
70 }
71
72 #[must_use]
73 pub fn events(&self) -> EventBus {
74 self.events.clone()
75 }
76
77 pub fn lock_now(&self) -> Result<()> {
78 self.lock.lock_now()
79 }
80
81 pub fn unlock(&self, request: &AuthRequest) -> AuthOutcome {
82 self.lock.unlock(request)
83 }
84
85 pub fn authenticate_unlock(&self, request: &AuthRequest) -> AuthOutcome {
88 self.lock.authenticate_unlock(request)
89 }
90
91 pub fn state(&self) -> Result<LockState> {
93 self.lock.state()
94 }
95
96 pub fn launch_lock_frontend(&self) -> Result<i32> {
97 let spec = self.config.lock_frontend.as_ref().ok_or_else(|| {
98 LimesError::Config(
99 "no lock frontend configured; set LIMES_LOCK_FRONTEND or Config::lock_frontend"
100 .to_owned(),
101 )
102 })?;
103 self.launch_frontend(spec, FrontendMode::Lock)
104 }
105
106 fn launch_frontend(&self, spec: &FrontendSpec, mode: FrontendMode) -> Result<i32> {
107 let status = self.frontends.run(spec, mode)?;
108 Ok(status.code().unwrap_or(1))
109 }
110}