Skip to main content

keep_active/
lib.rs

1//! Keep your computer awake (and active).
2//!
3//! # Examples
4//!
5//! ```
6//! # fn try_main() -> keep_active::Result<()> {
7//! let _awake = keep_active::Builder::default()
8//!     .display(true)
9//!     .reason("Video playback")
10//!     .app_name("My prog")
11//!     .app_reverse_domain("io.github.myprog")
12//!     .create()?;
13//! # Ok(())
14//! # }
15//! # try_main();
16//! ```
17//!
18//! ```
19//! # fn try_main() -> keep_active::Result<()> {
20//! let _awake = keep_active::Builder::default()
21//!     .display(true)
22//!     .idle(true)
23//!     .sleep(true)
24//!     .create()?;
25//! # Ok(())
26//! # }
27//! # try_main();
28//! ```
29
30use derive_builder::Builder;
31use thiserror::Error;
32
33mod sys;
34
35#[cfg(feature = "activity")]
36pub mod activity;
37
38#[cfg(feature = "activity")]
39pub use activity::{ActivityError, ActivityMethod, ActivitySimulator};
40
41/// A system error whose actual type varies by target.
42pub use sys::Error as SystemError;
43
44/// Error type.
45#[derive(Error, Debug)]
46pub enum Error {
47    #[error("builder: {0}")]
48    Builder(#[from] BuilderError),
49
50    #[error("system: {0}")]
51    System(#[from] SystemError),
52
53    #[cfg(feature = "activity")]
54    #[error("activity: {0}")]
55    Activity(#[from] ActivityError),
56}
57
58/// A specialized [`Result`](std::result::Result) type for this crate.
59pub type Result<T, E = Error> = std::result::Result<T, E>;
60
61#[derive(Builder, Debug)]
62#[builder(public, name = "Builder", build_fn(private))]
63#[allow(dead_code)] // Some fields are unused on some platforms
64struct Options {
65    /// Prevent the display from turning off.
66    #[builder(default)]
67    display: bool,
68
69    /// Prevent the system from sleeping due to idleness.
70    #[builder(default)]
71    idle: bool,
72
73    /// Prevent the system from explicitly sleeping. Only works under certain, OS dependant, conditions.
74    #[builder(default)]
75    sleep: bool,
76
77    // TODO Reconsider this defaults. They are really meant for the CLI.
78    /// Reason the consumer is keeping the system awake. Defaults to `"User requested"`. (Used on Linux & macOS)
79    #[builder(setter(into), default = "\"User requested\".to_string()")]
80    reason: String,
81
82    /// Name of the program keeping the system awake. Defaults to `"keep-active"`. (Used on Linux)
83    #[builder(setter(into), default = "\"keep-active\".to_string()")]
84    app_name: String,
85
86    /// Reverse domain name of the program keeping the system awake. Defaults to `"io.github.omerbustun.keep-active"`. (Used on Linux)
87    #[builder(
88        setter(into),
89        default = "\"io.github.omerbustun.keep-active\".to_string()"
90    )]
91    app_reverse_domain: String,
92}
93
94impl Builder {
95    /// Create the [`KeepActive`].
96    pub fn create(&self) -> Result<KeepActive> {
97        Ok(KeepActive {
98            _imp: sys::KeepActive::new(self.build()?)?,
99        })
100    }
101}
102
103/// Keeps the machine or display awake (as configured), until dropped. Create using [struct@Builder].
104pub struct KeepActive {
105    _imp: sys::KeepActive,
106}