use std::{collections::HashMap, fmt::Debug, os::fd::OwnedFd};
use enumflags2::{BitFlags, bitflags};
use serde::{Deserialize, Serialize};
use serde_repr::{Deserialize_repr, Serialize_repr};
use zbus::zvariant::{self, DeserializeDict, SerializeDict, Type, Value};
use super::{HandleToken, PersistMode, Request, Session, session::SessionPortal};
use crate::{
Error, WindowIdentifier, desktop::session::CreateSessionResponse, proxy::Proxy,
window_identifier::MaybeWindowIdentifierExt,
};
#[bitflags]
#[derive(Serialize_repr, Deserialize_repr, PartialEq, Eq, 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, Eq, 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(SerializeDict, Type, Debug, Default)]
#[zvariant(signature = "dict")]
struct CreateSessionOptions {
handle_token: HandleToken,
session_handle_token: HandleToken,
}
#[derive(SerializeDict, Type, Debug, Default)]
#[zvariant(signature = "dict")]
struct SelectSourcesOptions {
handle_token: HandleToken,
types: Option<BitFlags<SourceType>>,
multiple: Option<bool>,
cursor_mode: Option<CursorMode>,
restore_token: Option<String>,
persist_mode: Option<PersistMode>,
}
impl SelectSourcesOptions {
#[must_use]
pub fn multiple(mut self, multiple: impl Into<Option<bool>>) -> Self {
self.multiple = multiple.into();
self
}
#[must_use]
pub fn cursor_mode(mut self, cursor_mode: impl Into<Option<CursorMode>>) -> Self {
self.cursor_mode = cursor_mode.into();
self
}
#[must_use]
pub fn types(mut self, types: impl Into<Option<BitFlags<SourceType>>>) -> Self {
self.types = types.into();
self
}
#[must_use]
pub fn persist_mode(mut self, persist_mode: impl Into<Option<PersistMode>>) -> Self {
self.persist_mode = persist_mode.into();
self
}
#[must_use]
pub fn restore_token<'a>(mut self, token: impl Into<Option<&'a str>>) -> Self {
self.restore_token = token.into().map(ToOwned::to_owned);
self
}
}
#[derive(SerializeDict, Type, Debug, Default)]
#[zvariant(signature = "dict")]
struct StartCastOptions {
handle_token: HandleToken,
}
#[derive(Default, SerializeDict, DeserializeDict, Type)]
#[zvariant(signature = "dict")]
pub struct Streams {
streams: Vec<Stream>,
restore_token: Option<String>,
}
impl Streams {
pub fn restore_token(&self) -> Option<&str> {
self.restore_token.as_deref()
}
pub fn streams(&self) -> &[Stream] {
&self.streams
}
}
impl Debug for Streams {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_tuple("Streams")
.field(&self.restore_token)
.field(&self.streams)
.finish()
}
}
#[cfg(feature = "backend_screencast")]
#[cfg_attr(docsrs, doc(cfg(feature = "backend_screencast")))]
pub struct StreamsBuilder {
streams: Streams,
}
#[cfg(feature = "backend_screencast")]
#[cfg_attr(docsrs, doc(cfg(feature = "backend_screencast")))]
impl StreamsBuilder {
pub fn new(streams: Vec<Stream>) -> Self {
Self {
streams: Streams {
streams,
restore_token: None,
},
}
}
pub fn restore_token(mut self, restore_token: impl Into<Option<String>>) -> Self {
self.streams.restore_token = restore_token.into();
self
}
pub fn build(self) -> Streams {
self.streams
}
}
#[derive(Clone, Serialize, Deserialize, Type)]
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) -> Option<SourceType> {
self.1.source_type
}
pub fn id(&self) -> Option<&str> {
self.1.id.as_deref()
}
pub fn mapping_id(&self) -> Option<&str> {
self.1.mapping_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(Clone, SerializeDict, DeserializeDict, Type, Debug)]
#[zvariant(signature = "dict")]
struct StreamProperties {
id: Option<String>,
position: Option<(i32, i32)>,
size: Option<(i32, i32)>,
source_type: Option<SourceType>,
mapping_id: Option<String>,
}
#[cfg(feature = "backend_screencast")]
#[cfg_attr(docsrs, doc(cfg(feature = "backend_screencast")))]
pub struct StreamBuilder {
stream: Stream,
}
#[cfg(feature = "backend_screencast")]
#[cfg_attr(docsrs, doc(cfg(feature = "backend_screencast")))]
impl StreamBuilder {
pub fn new(pipe_wire_node_id: u32) -> Self {
Self {
stream: Stream(
pipe_wire_node_id,
StreamProperties {
id: None,
position: None,
size: None,
source_type: None,
mapping_id: None,
},
),
}
}
pub fn id(mut self, id: impl Into<Option<String>>) -> Self {
self.stream.1.id = id.into();
self
}
pub fn position(mut self, position: impl Into<Option<(i32, i32)>>) -> Self {
self.stream.1.position = position.into();
self
}
pub fn size(mut self, size: impl Into<Option<(i32, i32)>>) -> Self {
self.stream.1.size = size.into();
self
}
pub fn source_type(mut self, source_type: impl Into<Option<SourceType>>) -> Self {
self.stream.1.source_type = source_type.into();
self
}
pub fn mapping_id(mut self, mapping_id: impl Into<Option<String>>) -> Self {
self.stream.1.mapping_id = mapping_id.into();
self
}
pub fn build(self) -> Stream {
self.stream
}
}
#[derive(Debug)]
#[doc(alias = "org.freedesktop.portal.ScreenCast")]
pub struct Screencast(Proxy<'static>);
impl Screencast {
pub async fn new() -> Result<Self, Error> {
let proxy = Proxy::new_desktop("org.freedesktop.portal.ScreenCast").await?;
Ok(Self(proxy))
}
pub async fn with_connection(connection: zbus::Connection) -> Result<Self, Error> {
let proxy =
Proxy::new_desktop_with_connection(connection, "org.freedesktop.portal.ScreenCast")
.await?;
Ok(Self(proxy))
}
#[doc(alias = "CreateSession")]
#[doc(alias = "xdp_portal_create_screencast_session")]
pub async fn create_session(&self) -> Result<Session<Self>, Error> {
let options = CreateSessionOptions::default();
let (request, proxy) = futures_util::try_join!(
self.0.request::<CreateSessionResponse>(
&options.handle_token,
"CreateSession",
&options
),
Session::from_unique_name(self.0.connection().clone(), &options.session_handle_token),
)?;
assert_eq!(proxy.path(), &request.response()?.session_handle.as_ref());
Ok(proxy)
}
#[doc(alias = "OpenPipeWireRemote")]
pub async fn open_pipe_wire_remote(
&self,
session: &Session<impl HasScreencastSession>,
) -> Result<OwnedFd, Error> {
let options: HashMap<&str, Value<'_>> = HashMap::new();
let fd = self
.0
.call::<zvariant::OwnedFd>("OpenPipeWireRemote", &(session, options))
.await?;
Ok(fd.into())
}
#[doc(alias = "SelectSources")]
pub async fn select_sources(
&self,
session: &Session<impl HasScreencastSession>,
cursor_mode: CursorMode,
types: BitFlags<SourceType>,
multiple: bool,
restore_token: Option<&str>,
persist_mode: PersistMode,
) -> Result<Request<()>, Error> {
let options = SelectSourcesOptions::default()
.cursor_mode(cursor_mode)
.multiple(multiple)
.types(types)
.persist_mode(persist_mode)
.restore_token(restore_token);
self.0
.empty_request(&options.handle_token, "SelectSources", &(session, &options))
.await
}
#[doc(alias = "Start")]
pub async fn start(
&self,
session: &Session<impl HasScreencastSession>,
identifier: Option<&WindowIdentifier>,
) -> Result<Request<Streams>, Error> {
let options = StartCastOptions::default();
let identifier = identifier.to_string_or_empty();
self.0
.request(
&options.handle_token,
"Start",
&(session, &identifier, &options),
)
.await
}
#[doc(alias = "AvailableCursorModes")]
pub async fn available_cursor_modes(&self) -> Result<BitFlags<CursorMode>, Error> {
self.0.property_versioned("AvailableCursorModes", 2).await
}
#[doc(alias = "AvailableSourceTypes")]
pub async fn available_source_types(&self) -> Result<BitFlags<SourceType>, Error> {
self.0.property("AvailableSourceTypes").await
}
}
impl std::ops::Deref for Screencast {
type Target = zbus::Proxy<'static>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl crate::Sealed for Screencast {}
impl SessionPortal for Screencast {}
pub trait HasScreencastSession: SessionPortal {}
impl HasScreencastSession for Screencast {}
#[cfg(feature = "remote_desktop")]
impl HasScreencastSession for super::remote_desktop::RemoteDesktop {}