xidlehook_core/modules/
mod.rs1use crate::{Error, Result, TimerInfo};
5
6use log::warn;
7
8#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
11pub enum Progress {
12 Continue,
14 Abort,
17 Reset,
23 Stop,
25}
26
27pub trait Module {
30 fn pre_timer(&mut self, _timer: TimerInfo) -> Result<Progress> {
32 Ok(Progress::Continue)
33 }
34
35 fn post_timer(&mut self, _timer: TimerInfo) -> Result<Progress> {
37 Ok(Progress::Continue)
38 }
39
40 fn warning(&mut self, _error: &Error) -> Result<()> {
43 Ok(())
44 }
45
46 fn reset(&mut self) -> Result<()> {
49 Ok(())
50 }
51}
52
53impl Module for () {
55 fn warning(&mut self, error: &Error) -> Result<()> {
56 warn!("{} (Debug: {:?})", error, error);
57 Ok(())
58 }
59}
60
61impl Module for Box<dyn Module> {
62 fn pre_timer(&mut self, timer: TimerInfo) -> Result<Progress> {
63 (&mut **self).pre_timer(timer)
64 }
65 fn post_timer(&mut self, timer: TimerInfo) -> Result<Progress> {
66 (&mut **self).post_timer(timer)
67 }
68 fn warning(&mut self, error: &Error) -> Result<()> {
69 (&mut **self).warning(error)
70 }
71 fn reset(&mut self) -> Result<()> {
72 (&mut **self).reset()
73 }
74}
75
76impl<A, B> Module for (A, B)
79where
80 A: Module,
81 B: Module,
82{
83 fn pre_timer(&mut self, timer: TimerInfo) -> Result<Progress> {
84 let status = self.0.pre_timer(timer)?;
85 if status != Progress::Continue {
86 return Ok(status);
87 }
88 self.1.pre_timer(timer)
89 }
90 fn post_timer(&mut self, timer: TimerInfo) -> Result<Progress> {
91 let status = self.0.post_timer(timer)?;
92 if status != Progress::Continue {
93 return Ok(status);
94 }
95 self.1.post_timer(timer)
96 }
97 fn warning(&mut self, error: &Error) -> Result<()> {
98 self.0.warning(error)?;
99 self.1.warning(error)
100 }
101 fn reset(&mut self) -> Result<()> {
102 self.0.reset()?;
103 self.1.reset()
104 }
105}
106
107impl<M: Module> Module for Vec<M> {
109 fn pre_timer(&mut self, timer: TimerInfo) -> Result<Progress> {
110 for module in self {
111 let status = module.pre_timer(timer)?;
112 if status != Progress::Continue {
113 return Ok(status);
114 }
115 }
116 Ok(Progress::Continue)
117 }
118 fn post_timer(&mut self, timer: TimerInfo) -> Result<Progress> {
119 for module in self {
120 let status = module.post_timer(timer)?;
121 if status != Progress::Continue {
122 return Ok(status);
123 }
124 }
125 Ok(Progress::Continue)
126 }
127 fn warning(&mut self, error: &Error) -> Result<()> {
128 for module in self {
129 module.warning(error)?;
130 }
131 Ok(())
132 }
133 fn reset(&mut self) -> Result<()> {
134 for module in self {
135 module.reset()?;
136 }
137 Ok(())
138 }
139}
140
141#[cfg(feature = "pulse")]
142pub mod pulse;
143pub mod stop_at;
144pub mod xcb;
145
146#[cfg(feature = "pulse")]
147pub use self::pulse::NotWhenAudio;
148pub use self::{stop_at::StopAt, xcb::Xcb};