use enumflags2::{bitflags, BitFlags};
use futures_util::{Stream, TryFutureExt};
use serde::Deserialize;
use serde_repr::{Deserialize_repr, Serialize_repr};
use zbus::zvariant::{DeserializeDict, ObjectPath, OwnedObjectPath, SerializeDict, Type};
use super::{HandleToken, Request, Session};
use crate::{proxy::Proxy, Error, WindowIdentifier};
#[derive(SerializeDict, Type, Debug, Default)]
#[zvariant(signature = "dict")]
struct CreateMonitorOptions {
handle_token: HandleToken,
session_handle_token: HandleToken,
}
#[derive(SerializeDict, Type, Debug, Default)]
#[zvariant(signature = "dict")]
struct InhibitOptions {
handle_token: HandleToken,
reason: Option<String>,
}
#[bitflags]
#[derive(Serialize_repr, PartialEq, Eq, Debug, Clone, Copy, Type)]
#[repr(u32)]
#[doc(alias = "XdpInhibitFlags")]
pub enum InhibitFlags {
#[doc(alias = "XDP_INHIBIT_FLAG_LOGOUT")]
Logout,
#[doc(alias = "XDP_INHIBIT_FLAG_USER_SWITCH")]
UserSwitch,
#[doc(alias = "XDP_INHIBIT_FLAG_SUSPEND")]
Suspend,
#[doc(alias = "XDP_INHIBIT_FLAG_IDLE")]
Idle,
}
#[derive(Debug, DeserializeDict, Type)]
#[zvariant(signature = "dict")]
struct CreateMonitor {
session_handle: String,
}
#[derive(Debug, DeserializeDict, Type)]
#[zvariant(signature = "dict")]
struct State {
#[zvariant(rename = "screensaver-active")]
screensaver_active: bool,
#[zvariant(rename = "session-state")]
session_state: SessionState,
}
#[derive(Debug, Deserialize, Type)]
pub struct InhibitState(OwnedObjectPath, State);
impl InhibitState {
pub fn session_handle(&self) -> ObjectPath<'_> {
self.0.as_ref()
}
pub fn screensaver_active(&self) -> bool {
self.1.screensaver_active
}
pub fn session_state(&self) -> SessionState {
self.1.session_state
}
}
#[cfg_attr(feature = "glib", derive(glib::Enum))]
#[cfg_attr(feature = "glib", enum_type(name = "AshpdSessionState"))]
#[derive(Serialize_repr, Deserialize_repr, PartialEq, Eq, Debug, Clone, Copy, Type)]
#[doc(alias = "XdpLoginSessionState")]
#[repr(u32)]
pub enum SessionState {
#[doc(alias = "XDP_LOGIN_SESSION_RUNNING")]
Running = 1,
#[doc(alias = "XDP_LOGIN_SESSION_QUERY_END")]
QueryEnd = 2,
#[doc(alias = "XDP_LOGIN_SESSION_ENDING")]
Ending = 3,
}
#[derive(Debug)]
#[doc(alias = "org.freedesktop.portal.Inhibit")]
pub struct InhibitProxy<'a>(Proxy<'a>);
impl<'a> InhibitProxy<'a> {
pub async fn new() -> Result<InhibitProxy<'a>, Error> {
let proxy = Proxy::new_desktop("org.freedesktop.portal.Inhibit").await?;
Ok(Self(proxy))
}
#[doc(alias = "CreateMonitor")]
#[doc(alias = "xdp_portal_session_monitor_start")]
pub async fn create_monitor(
&self,
identifier: &WindowIdentifier,
) -> Result<Session<'a>, Error> {
let options = CreateMonitorOptions::default();
let body = &(&identifier, &options);
let (monitor, proxy) = futures_util::try_join!(
self.0
.request::<CreateMonitor>(&options.handle_token, "CreateMonitor", body)
.into_future(),
Session::from_unique_name(&options.session_handle_token).into_future(),
)?;
assert_eq!(proxy.path().as_str(), &monitor.response()?.session_handle);
Ok(proxy)
}
#[doc(alias = "Inhibit")]
#[doc(alias = "xdp_portal_session_inhibit")]
pub async fn inhibit(
&self,
identifier: &WindowIdentifier,
flags: BitFlags<InhibitFlags>,
reason: &str,
) -> Result<Request<()>, Error> {
let options = InhibitOptions {
reason: Some(reason.to_owned()),
handle_token: Default::default(),
};
self.0
.empty_request(
&options.handle_token,
"Inhibit",
&(&identifier, flags, &options),
)
.await
}
#[doc(alias = "StateChanged")]
#[doc(alias = "XdpPortal::session-state-changed")]
pub async fn receive_state_changed(&self) -> Result<impl Stream<Item = InhibitState>, Error> {
self.0.signal("StateChanged").await
}
#[doc(alias = "QueryEndResponse")]
#[doc(alias = "xdp_portal_session_monitor_query_end_response")]
pub async fn query_end_response(&self, session: &Session<'_>) -> Result<(), Error> {
self.0.call("QueryEndResponse", &(session)).await
}
}
impl<'a> std::ops::Deref for InhibitProxy<'a> {
type Target = zbus::Proxy<'a>;
fn deref(&self) -> &Self::Target {
&self.0
}
}