Debian Codegraph @

src/desktop/game_mode.rs

raw
1 //! # Examples
2 //!
3 //! ```rust,no_run
4 //! use ashpd::desktop::game_mode::GameMode;
5 //!
6 //! async fn run() -> ashpd::Result<()> {
7 //! let proxy = GameMode::new().await?;
8 //!
9 //! println!("{:#?}", proxy.register(246612).await?);
10 //! println!("{:#?}", proxy.query_status(246612).await?);
11 //! println!("{:#?}", proxy.unregister(246612).await?);
12 //! println!("{:#?}", proxy.query_status(246612).await?);
13 //!
14 //! Ok(())
15 //! }
16 //! ```
17
18 use std::{fmt::Debug, os::fd::AsFd};
19
20 use serde_repr::Deserialize_repr;
21 use zbus::zvariant::{Fd, Type};
22
23 use crate::{Error, Pid, error::PortalError, proxy::Proxy};
24
25 #[cfg_attr(feature = "glib", derive(glib::Enum))]
26 #[cfg_attr(feature = "glib", enum_type(name = "AshpdGameModeStatus"))]
27 #[derive(Deserialize_repr, PartialEq, Eq, Debug, Clone, Copy, Type)]
28 #[repr(i32)]
29 /// The status of the game mode.
30 pub enum Status {
31 /// GameMode is inactive.
32 Inactive = 0,
33 /// GameMode is active.
34 Active = 1,
35 /// GameMode is active and `pid` is registered.
36 Registered = 2,
37 /// The query failed inside GameMode.
38 Rejected = -1,
39 }
40
41 #[derive(Deserialize_repr, PartialEq, Eq, Debug, Type)]
42 #[repr(i32)]
43 /// The status of a (un-)register game mode request.
44 enum RegisterStatus {
45 /// If the game was successfully (un-)registered.
46 Success = 0,
47 /// If the request was rejected by GameMode.
48 Rejected = -1,
49 }
50
51 /// The interface lets sandboxed applications access GameMode from within the
52 /// sandbox.
53 ///
54 /// It is analogous to the `com.feralinteractive.GameMode` interface and will
55 /// proxy request there, but with additional permission checking and pid
56 /// mapping. The latter is necessary in the case that sandbox has pid namespace
57 /// isolation enabled. See the man page for pid_namespaces(7) for more details,
58 /// but briefly, it means that the sandbox has its own process id namespace
59 /// which is separated from the one on the host. Thus there will be two separate
60 /// process ids (pids) within two different namespaces that both identify same
61 /// process. One id from the pid namespace inside the sandbox and one id from
62 /// the host pid namespace. Since GameMode expects pids from the host pid
63 /// namespace but programs inside the sandbox can only know pids from the
64 /// sandbox namespace, process ids need to be translated from the portal to the
65 /// host namespace. The portal will do that transparently for all calls where
66 /// this is necessary.
67 ///
68 /// Note: GameMode will monitor active clients, i.e. games and other programs
69 /// that have successfully called [`GameMode::register`]. In the event
70 /// that a client terminates without a call to the
71 /// [`GameMode::unregister`] method, GameMode will automatically
72 /// un-register the client. This might happen with a (small) delay.
73 ///
74 /// Wrapper of the DBus interface: [`org.freedesktop.portal.GameMode`](https://flatpak.github.io/xdg-desktop-portal/docs/doc-org.freedesktop.portal.GameMode.html).
75 #[derive(Debug)]
76 #[doc(alias = "org.freedesktop.portal.GameMode")]
77 pub struct GameMode(Proxy<'static>);
78
79 impl GameMode {
80 /// Create a new instance of [`GameMode`].
81 pub async fn new() -> Result<Self, Error> {
82 let proxy = Proxy::new_desktop("org.freedesktop.portal.GameMode").await?;
83 Ok(Self(proxy))
84 }
85
86 /// Create a new instance of [`GameMode`].
87 pub async fn with_connection(connection: zbus::Connection) -> Result<Self, Error> {
88 let proxy =
89 Proxy::new_desktop_with_connection(connection, "org.freedesktop.portal.GameMode")
90 .await?;
91 Ok(Self(proxy))
92 }
93
94 /// Returns the version of the portal interface.
95 pub fn version(&self) -> u32 {
96 self.0.version()
97 }
98
99 /// Query the GameMode status for a process.
100 /// If the caller is running inside a sandbox with pid namespace isolation,
101 /// the pid will be translated to the respective host pid.
102 ///
103 /// # Arguments
104 ///
105 /// * `pid` - Process id to query the GameMode status of.
106 ///
107 /// # Specifications
108 ///
109 /// See also [`QueryStatus`](https://flatpak.github.io/xdg-desktop-portal/docs/doc-org.freedesktop.portal.GameMode.html#org-freedesktop-portal-gamemode-querystatus).
110 #[doc(alias = "QueryStatus")]
111 pub async fn query_status(&self, pid: Pid) -> Result<Status, Error> {
112 self.0.call("QueryStatus", &(pid as i32)).await
113 }
114
115 /// Query the GameMode status for a process.
116 ///
117 /// # Arguments
118 ///
119 /// * `target` - Pid file descriptor to query the GameMode status of.
120 /// * `requester` - Pid file descriptor of the process requesting the
121 /// information.
122 ///
123 /// # Specifications
124 ///
125 /// See also [`QueryStatusByPIDFd`](https://flatpak.github.io/xdg-desktop-portal/docs/doc-org.freedesktop.portal.GameMode.html#org-freedesktop-portal-gamemode-querystatusbypidfd).
126 #[doc(alias = "QueryStatusByPIDFd")]
127 pub async fn query_status_by_pidfd(
128 &self,
129 target: &impl AsFd,
130 requester: &impl AsFd,
131 ) -> Result<Status, Error> {
132 self.0
133 .call(
134 "QueryStatusByPIDFd",
135 &(Fd::from(target), Fd::from(requester)),
136 )
137 .await
138 }
139
140 /// Query the GameMode status for a process.
141 ///
142 /// # Arguments
143 ///
144 /// * `target` - Process id to query the GameMode status of.
145 /// * `requester` - Process id of the process requesting the information.
146 ///
147 /// # Specifications
148 ///
149 /// See also [`QueryStatusByPid`](https://flatpak.github.io/xdg-desktop-portal/docs/doc-org.freedesktop.portal.GameMode.html#org-freedesktop-portal-gamemode-querystatusbypid).
150 #[doc(alias = "QueryStatusByPid")]
151 pub async fn query_status_by_pid(&self, target: Pid, requester: Pid) -> Result<Status, Error> {
152 self.0
153 .call("QueryStatusByPid", &(target as i32, requester as i32))
154 .await
155 }
156
157 /// Register a game with GameMode and thus request GameMode to be activated.
158 /// If the caller is running inside a sandbox with pid namespace isolation,
159 /// the pid will be translated to the respective host pid. See the general
160 /// introduction for details. If the GameMode has already been requested
161 /// for pid before, this call will fail.
162 ///
163 /// # Arguments
164 ///
165 /// * `pid` - Process id of the game to register.
166 ///
167 /// # Specifications
168 ///
169 /// See also [`RegisterGame`](https://flatpak.github.io/xdg-desktop-portal/docs/doc-org.freedesktop.portal.GameMode.html#org-freedesktop-portal-gamemode-registergame).
170 #[doc(alias = "RegisterGame")]
171 pub async fn register(&self, pid: Pid) -> Result<(), Error> {
172 let status = self.0.call("RegisterGame", &(pid as i32)).await?;
173 match status {
174 RegisterStatus::Success => Ok(()),
175 RegisterStatus::Rejected => Err(Error::Portal(PortalError::Failed(format!(
176 "Failed to register game for `{pid}`"
177 )))),
178 }
179 }
180
181 /// Register a game with GameMode.
182 ///
183 /// # Arguments
184 ///
185 /// * `target` - Process file descriptor of the game to register.
186 /// * `requester` - Process file descriptor of the process requesting the
187 /// registration.
188 ///
189 /// # Specifications
190 ///
191 /// See also [`RegisterGameByPIDFd`](https://flatpak.github.io/xdg-desktop-portal/docs/doc-org.freedesktop.portal.GameMode.html#org-freedesktop-portal-gamemode-registergamebypidfd).
192 #[doc(alias = "RegisterGameByPIDFd")]
193 pub async fn register_by_pidfd(
194 &self,
195 target: &impl AsFd,
196 requester: &impl AsFd,
197 ) -> Result<(), Error> {
198 let status = self
199 .0
200 .call(
201 "RegisterGameByPIDFd",
202 &(Fd::from(target), Fd::from(requester)),
203 )
204 .await?;
205 match status {
206 RegisterStatus::Success => Ok(()),
207 RegisterStatus::Rejected => Err(Error::Portal(PortalError::Failed(
208 "Failed to register by pidfd".to_string(),
209 ))),
210 }
211 }
212
213 /// Register a game with GameMode.
214 ///
215 /// # Arguments
216 ///
217 /// * `target` - Process id of the game to register.
218 /// * `requester` - Process id of the process requesting the registration.
219 ///
220 /// # Specifications
221 ///
222 /// See also [`RegisterGameByPid`](https://flatpak.github.io/xdg-desktop-portal/docs/doc-org.freedesktop.portal.GameMode.html#org-freedesktop-portal-gamemode-registergamebypid).
223 #[doc(alias = "RegisterGameByPid")]
224 pub async fn register_by_pid(&self, target: Pid, requester: Pid) -> Result<(), Error> {
225 let status = self
226 .0
227 .call("RegisterGameByPid", &(target as i32, requester as i32))
228 .await?;
229 match status {
230 RegisterStatus::Success => Ok(()),
231 RegisterStatus::Rejected => Err(Error::Portal(PortalError::Failed(format!(
232 "Failed to register by pid for target=`{target}` requester=`{requester}`"
233 )))),
234 }
235 }
236
237 /// Un-register a game from GameMode.
238 /// if the call is successful and there are no other games or clients
239 /// registered, GameMode will be deactivated. If the caller is running
240 /// inside a sandbox with pid namespace isolation, the pid will be
241 /// translated to the respective host pid.
242 ///
243 /// # Arguments
244 ///
245 /// * `pid` - Process id of the game to un-register.
246 ///
247 /// # Specifications
248 ///
249 /// See also [`UnregisterGame`](https://flatpak.github.io/xdg-desktop-portal/docs/doc-org.freedesktop.portal.GameMode.html#org-freedesktop-portal-gamemode-unregistergame).
250 #[doc(alias = "UnregisterGame")]
251 pub async fn unregister(&self, pid: Pid) -> Result<(), Error> {
252 let status = self.0.call("UnregisterGame", &(pid as i32)).await?;
253 match status {
254 RegisterStatus::Success => Ok(()),
255 RegisterStatus::Rejected => Err(Error::Portal(PortalError::Failed(format!(
256 "Failed to unregister for `{pid}`"
257 )))),
258 }
259 }
260
261 /// Un-register a game from GameMode.
262 ///
263 /// # Arguments
264 ///
265 /// * `target` - Pid file descriptor of the game to un-register.
266 /// * `requester` - Pid file descriptor of the process requesting the
267 /// un-registration.
268 ///
269 /// # Specifications
270 ///
271 /// See also [`UnregisterGameByPIDFd`](https://flatpak.github.io/xdg-desktop-portal/docs/doc-org.freedesktop.portal.GameMode.html#org-freedesktop-portal-gamemode-unregistergamebypidfd).
272 #[doc(alias = "UnregisterGameByPIDFd")]
273 pub async fn unregister_by_pidfd(
274 &self,
275 target: &impl AsFd,
276 requester: &impl AsFd,
277 ) -> Result<(), Error> {
278 let status = self
279 .0
280 .call(
281 "UnregisterGameByPIDFd",
282 &(Fd::from(target), Fd::from(requester)),
283 )
284 .await?;
285 match status {
286 RegisterStatus::Success => Ok(()),
287 RegisterStatus::Rejected => Err(Error::Portal(PortalError::Failed(
288 "Failed to unregister by pidfd`".to_string(),
289 ))),
290 }
291 }
292
293 /// Un-register a game from GameMode.
294 ///
295 /// # Arguments
296 ///
297 /// * `target` - Process id of the game to un-register.
298 /// * `requester` - Process id of the process requesting the
299 /// un-registration.
300 ///
301 /// # Specifications
302 ///
303 /// See also [`UnregisterGameByPid`](https://flatpak.github.io/xdg-desktop-portal/docs/doc-org.freedesktop.portal.GameMode.html#org-freedesktop-portal-gamemode-unregistergamebypid).
304 #[doc(alias = "UnregisterGameByPid")]
305 pub async fn unregister_by_pid(&self, target: Pid, requester: Pid) -> Result<(), Error> {
306 let status = self
307 .0
308 .call("UnregisterGameByPid", &(target as i32, requester as i32))
309 .await?;
310 match status {
311 RegisterStatus::Success => Ok(()),
312 RegisterStatus::Rejected => Err(Error::Portal(PortalError::Failed(format!(
313 "Failed to unregister by pid for target=`{target}` requester=`{requester}`"
314 )))),
315 }
316 }
317
318 /// Whether any pid on the system has enabled Game Mode.
319 ///
320 /// # Specifications
321 ///
322 /// See also [`Active`](https://flatpak.github.io/xdg-desktop-portal/docs/doc-org.freedesktop.portal.GameMode.html#org-freedesktop-portal-gamemode-active).
323 #[doc(alias = "Active")]
324 pub async fn is_active(&self) -> Result<bool, Error> {
325 let is_active = self.0.property("Active").await?;
326 Ok(is_active)
327 }
328 }
329
330 impl std::ops::Deref for GameMode {
331 type Target = zbus::Proxy<'static>;
332
333 fn deref(&self) -> &Self::Target {
334 &self.0
335 }
336 }