use enumflags2::{bitflags, BitFlags};
use futures::TryFutureExt;
use serde::{Deserialize, Serialize};
use serde_repr::{Deserialize_repr, Serialize_repr};
use zbus::zvariant::{DeserializeDict, OwnedObjectPath, SerializeDict, Type};
use super::{HandleToken, SessionProxy, DESTINATION, PATH};
use crate::{
helpers::{call_basic_response_method, call_method, call_request_method, receive_signal},
Error, WindowIdentifier,
};
#[derive(SerializeDict, DeserializeDict, Type, Debug, Default)]
#[zvariant(signature = "dict")]
struct CreateMonitorOptions {
handle_token: HandleToken,
session_handle_token: HandleToken,
}
#[derive(SerializeDict, DeserializeDict, Type, Debug, Default)]
#[zvariant(signature = "dict")]
struct InhibitOptions {
handle_token: HandleToken,
reason: Option<String>,
}
impl InhibitOptions {
pub fn reason(mut self, reason: &str) -> Self {
self.reason = Some(reason.to_string());
self
}
}
#[bitflags]
#[derive(Serialize_repr, Deserialize_repr, PartialEq, 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, SerializeDict, DeserializeDict, Type)]
#[zvariant(signature = "dict")]
struct CreateMonitor {
session_handle: String,
}
#[derive(Debug, SerializeDict, DeserializeDict, Type)]
#[zvariant(signature = "dict")]
struct State {
#[zvariant(rename = "screensaver-active")]
screensaver_active: bool,
#[zvariant(rename = "session-state")]
session_state: SessionState,
}
#[derive(Debug, Serialize, Deserialize, Type)]
pub struct InhibitState(OwnedObjectPath, State);
impl InhibitState {
pub fn screensaver_active(&self) -> bool {
self.1.screensaver_active
}
pub fn session_state(&self) -> SessionState {
self.1.session_state
}
}
#[derive(Serialize_repr, Deserialize_repr, PartialEq, Debug, Clone, Copy, Type)]
#[doc(alias = "XdpLoginSessionState")]
#[repr(u8)]
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>(zbus::Proxy<'a>);
impl<'a> InhibitProxy<'a> {
pub async fn new(connection: &zbus::Connection) -> Result<InhibitProxy<'a>, Error> {
let proxy = zbus::ProxyBuilder::new_bare(connection)
.interface("org.freedesktop.portal.Inhibit")?
.path(PATH)?
.destination(DESTINATION)?
.build()
.await?;
Ok(Self(proxy))
}
pub fn inner(&self) -> &zbus::Proxy<'_> {
&self.0
}
#[doc(alias = "CreateMonitor")]
#[doc(alias = "xdp_portal_session_monitor_start")]
pub async fn create_monitor(
&self,
identifier: &WindowIdentifier,
) -> Result<SessionProxy<'a>, Error> {
let options = CreateMonitorOptions::default();
let body = &(&identifier, &options);
let (monitor, proxy): (CreateMonitor, SessionProxy) = futures::try_join!(
call_request_method(self.inner(), &options.handle_token, "CreateMonitor", body)
.into_future(),
SessionProxy::from_unique_name(
self.inner().connection(),
&options.session_handle_token
)
.into_future(),
)?;
assert_eq!(proxy.inner().path().as_str(), &monitor.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<(), Error> {
let options = InhibitOptions::default().reason(reason);
call_basic_response_method(
self.inner(),
&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<InhibitState, Error> {
receive_signal(self.inner(), "StateChanged").await
}
#[doc(alias = "QueryEndResponse")]
#[doc(alias = "xdp_portal_session_monitor_query_end_response")]
pub async fn query_end_response(&self, session: &SessionProxy<'_>) -> Result<(), Error> {
call_method(self.inner(), "QueryEndResponse", &(session)).await
}
}