use std::{
collections::HashMap,
fmt::Debug,
os::unix::prelude::{IntoRawFd, RawFd},
};
use enumflags2::{bitflags, BitFlags};
use futures::TryFutureExt;
use serde::{Deserialize, Serialize};
use serde_repr::{Deserialize_repr, Serialize_repr};
use zbus::zvariant::{DeserializeDict, OwnedFd, SerializeDict, Type, Value};
use super::{HandleToken, SessionProxy, DESTINATION, PATH};
use crate::{
helpers::{call_basic_response_method, call_method, call_request_method},
Error, WindowIdentifier,
};
#[bitflags]
#[derive(Serialize_repr, Deserialize_repr, PartialEq, Copy, Clone, Debug, Type)]
#[repr(u32)]
#[doc(alias = "XdpOutputType")]
pub enum SourceType {
#[doc(alias = "XDP_OUTPUT_MONITOR")]
Monitor,
#[doc(alias = "XDP_OUTPUT_WINDOW")]
Window,
#[doc(alias = "XDP_OUTPUT_VIRTUAL")]
Virtual,
}
#[bitflags]
#[derive(Serialize_repr, Deserialize_repr, PartialEq, Debug, Copy, Clone, Type)]
#[repr(u32)]
#[doc(alias = "XdpCursorMode")]
pub enum CursorMode {
#[doc(alias = "XDP_CURSOR_MODE_HIDDEN")]
Hidden,
#[doc(alias = "XDP_CURSOR_MODE_EMBEDDED")]
Embedded,
#[doc(alias = "XDP_CURSOR_MODE_METADATA")]
Metadata,
}
#[derive(Serialize_repr, Deserialize_repr, PartialEq, Debug, Copy, Clone, Type)]
#[doc(alias = "XdpPersistMode")]
#[repr(u8)]
pub enum PersistMode {
#[doc(alias = "XDP_PERSIST_MODE_NONE")]
DoNot = 0,
#[doc(alias = "XDP_PERSIST_MODE_TRANSIENT")]
Application = 1,
#[doc(alias = "XDP_PERSIST_MODE_PERSISTENT")]
ExplicitlyRevoked = 2,
}
impl Default for PersistMode {
fn default() -> Self {
Self::DoNot
}
}
#[derive(SerializeDict, DeserializeDict, Type, Debug, Default)]
#[zvariant(signature = "dict")]
struct CreateSessionOptions {
handle_token: HandleToken,
session_handle_token: HandleToken,
}
#[derive(SerializeDict, DeserializeDict, Type, Debug, Default)]
#[zvariant(signature = "dict")]
struct SelectSourcesOptions {
handle_token: HandleToken,
types: Option<BitFlags<SourceType>>,
multiple: Option<bool>,
cursor_mode: Option<BitFlags<CursorMode>>,
restore_token: Option<String>,
persist_mode: Option<PersistMode>,
}
impl SelectSourcesOptions {
#[must_use]
pub fn multiple(mut self, multiple: bool) -> Self {
self.multiple = Some(multiple);
self
}
#[must_use]
pub fn cursor_mode(mut self, cursor_mode: BitFlags<CursorMode>) -> Self {
self.cursor_mode = Some(cursor_mode);
self
}
#[must_use]
pub fn types(mut self, types: BitFlags<SourceType>) -> Self {
self.types = Some(types);
self
}
#[must_use]
pub fn persist_mode(mut self, persist_mode: PersistMode) -> Self {
self.persist_mode = Some(persist_mode);
self
}
pub fn set_restore_token(&mut self, token: &str) {
self.restore_token = Some(token.to_string());
}
}
#[derive(SerializeDict, DeserializeDict, Type, Debug, Default)]
#[zvariant(signature = "dict")]
struct StartCastOptions {
handle_token: HandleToken,
}
#[derive(SerializeDict, DeserializeDict, Type, Debug)]
#[zvariant(signature = "dict")]
struct CreateSession {
session_handle: String,
}
#[derive(SerializeDict, DeserializeDict, Type)]
#[zvariant(signature = "dict")]
struct Streams {
streams: Vec<Stream>,
restore_token: Option<String>,
}
impl Debug for Streams {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_list().entries(self.streams.iter()).finish()
}
}
#[derive(Serialize, Deserialize, Type, Clone)]
pub struct Stream(u32, StreamProperties);
impl Stream {
pub fn pipe_wire_node_id(&self) -> u32 {
self.0
}
pub fn position(&self) -> Option<(i32, i32)> {
self.1.position
}
pub fn size(&self) -> Option<(i32, i32)> {
self.1.size
}
pub fn source_type(&self) -> SourceType {
self.1.source_type
}
pub fn id(&self) -> Option<&str> {
self.1.id.as_deref()
}
}
impl Debug for Stream {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Stream")
.field("pipewire_node_id", &self.pipe_wire_node_id())
.field("position", &self.position())
.field("size", &self.size())
.field("source_type", &self.source_type())
.field("id", &self.id())
.finish()
}
}
#[derive(SerializeDict, DeserializeDict, Type, Debug, Clone)]
#[zvariant(signature = "dict")]
struct StreamProperties {
id: Option<String>,
position: Option<(i32, i32)>,
size: Option<(i32, i32)>,
source_type: SourceType,
}
#[derive(Debug)]
#[doc(alias = "org.freedesktop.portal.ScreenCast")]
pub struct ScreenCastProxy<'a>(zbus::Proxy<'a>);
impl<'a> ScreenCastProxy<'a> {
pub async fn new(connection: &zbus::Connection) -> Result<ScreenCastProxy<'a>, Error> {
let proxy = zbus::ProxyBuilder::new_bare(connection)
.interface("org.freedesktop.portal.ScreenCast")?
.path(PATH)?
.destination(DESTINATION)?
.build()
.await?;
Ok(Self(proxy))
}
pub fn inner(&self) -> &zbus::Proxy<'_> {
&self.0
}
#[doc(alias = "CreateSession")]
#[doc(alias = "xdp_portal_create_screencast_session")]
pub async fn create_session(&self) -> Result<SessionProxy<'a>, Error> {
let options = CreateSessionOptions::default();
let (session, proxy) = futures::try_join!(
call_request_method::<CreateSession, _>(
self.inner(),
&options.handle_token,
"CreateSession",
&options
)
.into_future(),
SessionProxy::from_unique_name(
self.inner().connection(),
&options.session_handle_token
)
.into_future(),
)?;
assert_eq!(proxy.inner().path().as_str(), &session.session_handle);
Ok(proxy)
}
#[doc(alias = "OpenPipeWireRemote")]
pub async fn open_pipe_wire_remote(&self, session: &SessionProxy<'_>) -> Result<RawFd, Error> {
let options: HashMap<&str, Value<'_>> = HashMap::new();
let fd: OwnedFd =
call_method(self.inner(), "OpenPipeWireRemote", &(session, options)).await?;
Ok(fd.into_raw_fd())
}
#[doc(alias = "SelectSources")]
pub async fn select_sources(
&self,
session: &SessionProxy<'_>,
cursor_mode: BitFlags<CursorMode>,
types: BitFlags<SourceType>,
multiple: bool,
restore_token: Option<&str>,
persist_mode: PersistMode,
) -> Result<(), Error> {
let mut options = SelectSourcesOptions::default()
.cursor_mode(cursor_mode)
.multiple(multiple)
.types(types)
.persist_mode(persist_mode);
if let Some(token) = restore_token {
options.set_restore_token(token);
}
call_basic_response_method(
self.inner(),
&options.handle_token,
"SelectSources",
&(session, &options),
)
.await
}
#[doc(alias = "Start")]
pub async fn start(
&self,
session: &SessionProxy<'_>,
identifier: &WindowIdentifier,
) -> Result<(Vec<Stream>, Option<String>), Error> {
let options = StartCastOptions::default();
let streams: Streams = call_request_method(
self.inner(),
&options.handle_token,
"Start",
&(session, &identifier, &options),
)
.await?;
Ok((streams.streams.to_vec(), streams.restore_token))
}
#[doc(alias = "AvailableCursorModes")]
pub async fn available_cursor_modes(&self) -> Result<BitFlags<CursorMode>, Error> {
self.inner()
.get_property::<BitFlags<CursorMode>>("AvailableCursorModes")
.await
.map_err(From::from)
}
#[doc(alias = "AvailableSourceTypes")]
pub async fn available_source_types(&self) -> Result<BitFlags<SourceType>, Error> {
self.inner()
.get_property::<BitFlags<SourceType>>("AvailableSourceTypes")
.await
.map_err(From::from)
}
}