ctru/services/
apt.rs

1//! Applet service.
2//!
3//! The APT service handles integration with other applications,
4//! including high-level OS features such as Sleep mode, the Home Menu and application switching.
5//!
6//! It also handles running applets, small programs made available by the OS to streamline specific functionality.
7//! Those are implemented in the [`applets`](crate::applets) module.
8
9use crate::error::ResultCode;
10
11/// Handle to the Applet service.
12pub struct Apt(());
13
14impl Apt {
15    /// Initialize a new service handle.
16    ///
17    /// # Example
18    ///
19    /// ```
20    /// # let _runner = test_runner::GdbRunner::default();
21    /// # use std::error::Error;
22    /// # fn main() -> Result<(), Box<dyn Error>> {
23    /// #
24    /// use ctru::services::apt::Apt;
25    ///
26    /// let apt = Apt::new()?;
27    /// #
28    /// # Ok(())
29    /// # }
30    /// ```
31    #[doc(alias = "aptInit")]
32    pub fn new() -> crate::Result<Apt> {
33        unsafe {
34            ResultCode(ctru_sys::aptInit())?;
35            Ok(Apt(()))
36        }
37    }
38
39    /// Returns `true` if the application is running in the foreground as normal.
40    ///
41    /// # Notes
42    ///
43    /// This function is called as such since it automatically handles all checks for Home Menu switching, Sleep mode and other events that could take away control from the application.
44    /// For this reason, its main use is as the condition of a while loop that controls the main logic for your program.
45    ///
46    /// # Example
47    ///
48    /// ```
49    /// # let _runner = test_runner::GdbRunner::default();
50    /// use std::error::Error;
51    /// use ctru::services::apt::Apt;
52    ///
53    /// // In a simple `main` function, the structure should be the following.
54    /// fn main() -> Result<(), Box<dyn Error>> {
55    ///
56    /// let apt = Apt::new()?;
57    ///
58    /// while apt.main_loop() {
59    ///     // Main program logic should be written here.
60    /// }
61    ///
62    /// // Optional clean-ups after running the application should be written after the main loop.
63    /// #
64    /// # Ok(())
65    /// # }
66    /// ```
67    #[doc(alias = "aptMainLoop")]
68    pub fn main_loop(&self) -> bool {
69        unsafe { ctru_sys::aptMainLoop() }
70    }
71
72    /// Set (in percentage) the amount of time to lend to the application thread spawned on the syscore (core #1).
73    ///
74    /// # Notes
75    ///
76    /// It is necessary to set a time limit before spawning threads on the syscore.
77    /// The percentage value must be withing 5% and 89%, though it is suggested to use lower values (around 30-45%) to avoid slowing down the OS processes.
78    #[doc(alias = "APT_SetAppCpuTimeLimit")]
79    pub fn set_app_cpu_time_limit(&mut self, percent: u32) -> crate::Result<()> {
80        unsafe {
81            ResultCode(ctru_sys::APT_SetAppCpuTimeLimit(percent))?;
82            Ok(())
83        }
84    }
85
86    /// Set if the console is allowed to enter sleep mode.
87    ///
88    /// You can check whether the console is allowed to sleep with [Apt::is_sleep_allowed].
89    #[doc(alias = "aptSetSleepAllowed")]
90    pub fn set_sleep_allowed(&mut self, allowed: bool) {
91        unsafe {
92            ctru_sys::aptSetSleepAllowed(allowed);
93        }
94    }
95
96    /// Check if the console is allowed to enter sleep mode.
97    ///
98    /// You can set whether the console is allowed to sleep with [Apt::set_sleep_allowed].
99    #[doc(alias = "aptIsSleepAllowed")]
100    pub fn is_sleep_allowed(&self) -> bool {
101        unsafe { ctru_sys::aptIsSleepAllowed() }
102    }
103
104    /// Set if the console is allowed to enter the home menu.
105    ///
106    /// You can check whether the console is allowed to enter the home menu with [Apt::is_home_allowed].
107    #[doc(alias = "aptSetHomeAllowed")]
108    pub fn set_home_allowed(&mut self, allowed: bool) {
109        unsafe {
110            ctru_sys::aptSetHomeAllowed(allowed);
111        }
112    }
113
114    /// Check if the console is allowed to enter the home menu.
115    ///
116    /// You can set whether the console is allowed to enter the home menu with [Apt::set_home_allowed].
117    #[doc(alias = "aptIsHomeAllowed")]
118    pub fn is_home_allowed(&self) -> bool {
119        unsafe { ctru_sys::aptIsHomeAllowed() }
120    }
121
122    /// Immediately jumps to the home menu.
123    #[doc(alias = "aptJumpToHomeMenu")]
124    pub fn jump_to_home_menu(&mut self) {
125        unsafe { ctru_sys::aptJumpToHomeMenu() }
126    }
127}
128
129impl Drop for Apt {
130    #[doc(alias = "aptExit")]
131    fn drop(&mut self) {
132        unsafe { ctru_sys::aptExit() };
133    }
134}
135
136/// Can launch other applications when the current one exits.
137pub struct Chainloader<'a> {
138    _apt: &'a Apt,
139}
140
141impl<'a> Chainloader<'a> {
142    /// Gets a handle to the chainloader
143    pub fn new(apt: &'a Apt) -> Self {
144        Self { _apt: apt }
145    }
146
147    /// Checks if the chainloader is set
148    #[doc(alias = "aptIsChainload")]
149    pub fn is_set(&self) -> bool {
150        // static funtion not exported
151        unsafe {
152            (ctru_sys::envGetSystemRunFlags() & u32::from(ctru_sys::RUNFLAG_APTCHAINLOAD)) != 0
153        }
154    }
155
156    /// Clears the chainloader state.
157    #[doc(alias = "aptClearChainloader")]
158    pub fn clear(&mut self) {
159        unsafe { ctru_sys::aptClearChainloader() }
160    }
161
162    /// Configures the chainloader to launch a specific application.
163    ///
164    /// See also [`Title`](crate::services::am::Title]
165    #[doc(alias = "aptSetChainloader")]
166    pub fn set(&mut self, title: &super::am::Title<'_>) {
167        unsafe { ctru_sys::aptSetChainloader(title.id(), title.media_type() as u8) }
168    }
169
170    /// Configures the chainloader to launch the previous application.
171    #[doc(alias = "aptSetChainloaderToCaller")]
172    pub fn set_to_caller(&mut self) {
173        unsafe { ctru_sys::aptSetChainloaderToCaller() }
174    }
175
176    /// Configures the chainloader to relaunch the current application (i.e. soft-reset)
177    #[doc(alias = "aptSetChainloaderToSelf")]
178    pub fn set_to_self(&mut self) {
179        unsafe { ctru_sys::aptSetChainloaderToSelf() }
180    }
181}