Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

desktop/game_mode: Use i32 for pid #234

Merged
merged 4 commits into from
Sep 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/desktop/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use serde::{Deserialize, Serialize};
use zbus::zvariant::{SerializeDict, Type};

use super::{HandleToken, Request};
use crate::{proxy::Proxy, Error};
use crate::{proxy::Proxy, Error, Pid};

#[derive(SerializeDict, Type, Debug, Default)]
/// Specified options for a [`DeviceProxy::access_device`] request.
Expand Down Expand Up @@ -124,7 +124,7 @@ impl<'a> DeviceProxy<'a> {
///
/// See also [`AccessDevice`](https://flatpak.github.io/xdg-desktop-portal/docs/doc-org.freedesktop.portal.Device.html#org-freedesktop-portal-device-accessdevice).
#[doc(alias = "AccessDevice")]
pub async fn access_device(&self, pid: u32, devices: &[Device]) -> Result<Request<()>, Error> {
pub async fn access_device(&self, pid: Pid, devices: &[Device]) -> Result<Request<()>, Error> {
let options = AccessDeviceOptions::default();
self.0
.empty_request(
Expand Down
28 changes: 15 additions & 13 deletions src/desktop/game_mode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use std::{fmt::Debug, os::fd::BorrowedFd};
use serde_repr::Deserialize_repr;
use zbus::zvariant::{Fd, Type};

use crate::{error::PortalError, proxy::Proxy, Error};
use crate::{error::PortalError, proxy::Proxy, Error, Pid};

#[cfg_attr(feature = "glib", derive(glib::Enum))]
#[cfg_attr(feature = "glib", enum_type(name = "AshpdGameModeStatus"))]
Expand Down Expand Up @@ -95,8 +95,8 @@ impl<'a> GameMode<'a> {
///
/// See also [`QueryStatus`](https://flatpak.github.io/xdg-desktop-portal/docs/doc-org.freedesktop.portal.GameMode.html#org-freedesktop-portal-gamemode-querystatus).
#[doc(alias = "QueryStatus")]
pub async fn query_status(&self, pid: u32) -> Result<Status, Error> {
self.0.call("QueryStatus", &(pid)).await
pub async fn query_status(&self, pid: Pid) -> Result<Status, Error> {
self.0.call("QueryStatus", &(pid as i32)).await
}

/// Query the GameMode status for a process.
Expand Down Expand Up @@ -135,8 +135,10 @@ impl<'a> GameMode<'a> {
///
/// See also [`QueryStatusByPid`](https://flatpak.github.io/xdg-desktop-portal/docs/doc-org.freedesktop.portal.GameMode.html#org-freedesktop-portal-gamemode-querystatusbypid).
#[doc(alias = "QueryStatusByPid")]
pub async fn query_status_by_pid(&self, target: u32, requester: u32) -> Result<Status, Error> {
self.0.call("QueryStatusByPid", &(target, requester)).await
pub async fn query_status_by_pid(&self, target: Pid, requester: Pid) -> Result<Status, Error> {
self.0
.call("QueryStatusByPid", &(target as i32, requester as i32))
.await
}

/// Register a game with GameMode and thus request GameMode to be activated.
Expand All @@ -153,8 +155,8 @@ impl<'a> GameMode<'a> {
///
/// See also [`RegisterGame`](https://flatpak.github.io/xdg-desktop-portal/docs/doc-org.freedesktop.portal.GameMode.html#org-freedesktop-portal-gamemode-registergame).
#[doc(alias = "RegisterGame")]
pub async fn register(&self, pid: u32) -> Result<(), Error> {
let status = self.0.call("RegisterGame", &(pid)).await?;
pub async fn register(&self, pid: Pid) -> Result<(), Error> {
let status = self.0.call("RegisterGame", &(pid as i32)).await?;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not replace the function signature? is this in order to have a backportable variant?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Originally, I had two reasons not to change the signature:

  1. Backward compatibility, in order not to break code that already uses u32. (Now writing this I realized that probably no one uses it as it doesn't work...)
  2. Better interoperability with Rust, as Rust uses u32 for pids: https://doc.rust-lang.org/std/process/fn.id.html, https://doc.rust-lang.org/std/process/struct.Child.html#method.id

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, in such case I would add

// Matches the type used in std
pub type Pid = u32;

and pass that to the functions instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure thing, added the Pid type alias and replaced most pids in API. There are instances where u64 is used though, but I haven't touched them.

Let me know if that's what you meant.

match status {
RegisterStatus::Success => Ok(()),
RegisterStatus::Rejected => Err(Error::Portal(PortalError::Failed(format!(
Expand Down Expand Up @@ -206,10 +208,10 @@ impl<'a> GameMode<'a> {
///
/// See also [`RegisterGameByPid`](https://flatpak.github.io/xdg-desktop-portal/docs/doc-org.freedesktop.portal.GameMode.html#org-freedesktop-portal-gamemode-registergamebypid).
#[doc(alias = "RegisterGameByPid")]
pub async fn register_by_pid(&self, target: u32, requester: u32) -> Result<(), Error> {
pub async fn register_by_pid(&self, target: Pid, requester: Pid) -> Result<(), Error> {
let status = self
.0
.call("RegisterGameByPid", &(target, requester))
.call("RegisterGameByPid", &(target as i32, requester as i32))
.await?;
match status {
RegisterStatus::Success => Ok(()),
Expand All @@ -233,8 +235,8 @@ impl<'a> GameMode<'a> {
///
/// See also [`UnregisterGame`](https://flatpak.github.io/xdg-desktop-portal/docs/doc-org.freedesktop.portal.GameMode.html#org-freedesktop-portal-gamemode-unregistergame).
#[doc(alias = "UnregisterGame")]
pub async fn unregister(&self, pid: u32) -> Result<(), Error> {
let status = self.0.call("UnregisterGame", &(pid)).await?;
pub async fn unregister(&self, pid: Pid) -> Result<(), Error> {
let status = self.0.call("UnregisterGame", &(pid as i32)).await?;
match status {
RegisterStatus::Success => Ok(()),
RegisterStatus::Rejected => Err(Error::Portal(PortalError::Failed(format!(
Expand Down Expand Up @@ -287,10 +289,10 @@ impl<'a> GameMode<'a> {
///
/// See also [`UnregisterGameByPid`](https://flatpak.github.io/xdg-desktop-portal/docs/doc-org.freedesktop.portal.GameMode.html#org-freedesktop-portal-gamemode-unregistergamebypid).
#[doc(alias = "UnregisterGameByPid")]
pub async fn unregister_by_pid(&self, target: u32, requester: u32) -> Result<(), Error> {
pub async fn unregister_by_pid(&self, target: Pid, requester: Pid) -> Result<(), Error> {
let status = self
.0
.call("UnregisterGameByPid", &(target, requester))
.call("UnregisterGameByPid", &(target as i32, requester as i32))
.await?;
match status {
RegisterStatus::Success => Ok(()),
Expand Down
4 changes: 2 additions & 2 deletions src/flatpak/development.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use futures_util::Stream;
use serde_repr::{Deserialize_repr, Serialize_repr};
use zbus::zvariant::{Fd, Type};

use crate::{proxy::Proxy, Error, FilePath};
use crate::{proxy::Proxy, Error, FilePath, Pid};

#[bitflags]
#[derive(Serialize_repr, Deserialize_repr, PartialEq, Eq, Copy, Clone, Debug, Type)]
Expand Down Expand Up @@ -106,7 +106,7 @@ impl<'a> Development<'a> {
#[doc(alias = "xdp_portal_spawn_signal")]
pub async fn host_command_signal(
&self,
pid: u32,
pid: Pid,
signal: u32,
to_process_group: bool,
) -> Result<(), Error> {
Expand Down
4 changes: 2 additions & 2 deletions src/flatpak/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ use serde::Serialize;
use serde_repr::{Deserialize_repr, Serialize_repr};
use zbus::zvariant::{self, Fd, OwnedObjectPath, SerializeDict, Type};

use crate::{proxy::Proxy, Error, FilePath};
use crate::{proxy::Proxy, Error, FilePath, Pid};

#[bitflags]
#[derive(Serialize_repr, Deserialize_repr, PartialEq, Eq, Copy, Clone, Debug, Type)]
Expand Down Expand Up @@ -357,7 +357,7 @@ impl<'a> Flatpak<'a> {
#[doc(alias = "xdp_portal_spawn_signal")]
pub async fn spawn_signal(
&self,
pid: u32,
pid: Pid,
signal: u32,
to_process_group: bool,
) -> Result<(), Error> {
Expand Down
5 changes: 5 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,8 @@ mod sealed {
}

pub(crate) use sealed::Sealed;

/// Process ID.
///
/// Matches the type used in std.
pub type Pid = u32;
Loading