| 1 |
use std::sync::{Arc, Mutex}; |
| 2 |
|
| 3 |
use async_trait::async_trait; |
| 4 |
use enumflags2::BitFlags; |
| 5 |
use serde::Serialize; |
| 6 |
|
| 7 |
use crate::{ |
| 8 |
MaybeAppID, PortalError, WindowIdentifierType, |
| 9 |
backend::{ |
| 10 |
Result, |
| 11 |
request::{Request, RequestImpl}, |
| 12 |
session::{CreateSessionResponse, Session, SessionImpl, SessionManager}, |
| 13 |
}, |
| 14 |
desktop::{ |
| 15 |
CreateSessionOptions, HandleToken, |
| 16 |
request::Response, |
| 17 |
screencast::{CursorMode, SelectSourcesOptions, SourceType, StartCastOptions, Streams}, |
| 18 |
}, |
| 19 |
zvariant::{Optional, OwnedObjectPath, Type}, |
| 20 |
}; |
| 21 |
|
| 22 |
#[derive(Serialize, Type, Debug, Default)] |
| 23 |
#[zvariant(signature = "dict")] |
| 24 |
pub struct SelectSourcesResponse {} |
| 25 |
|
| 26 |
#[async_trait] |
| 27 |
pub trait ScreencastImpl: RequestImpl + SessionImpl { |
| 28 |
#[doc(alias = "AvailableSourceTypes")] |
| 29 |
fn available_source_types(&self) -> BitFlags<SourceType>; |
| 30 |
|
| 31 |
#[doc(alias = "AvailableCursorModes")] |
| 32 |
fn available_cursor_mode(&self) -> BitFlags<CursorMode>; |
| 33 |
|
| 34 |
#[doc(alias = "CreateSession")] |
| 35 |
async fn create_session( |
| 36 |
&self, |
| 37 |
token: HandleToken, |
| 38 |
session_token: HandleToken, |
| 39 |
app_id: Option<MaybeAppID>, |
| 40 |
options: CreateSessionOptions, |
| 41 |
) -> Result<CreateSessionResponse>; |
| 42 |
|
| 43 |
#[doc(alias = "SelectSources")] |
| 44 |
async fn select_sources( |
| 45 |
&self, |
| 46 |
session_token: HandleToken, |
| 47 |
app_id: Option<MaybeAppID>, |
| 48 |
options: SelectSourcesOptions, |
| 49 |
) -> Result<SelectSourcesResponse>; |
| 50 |
|
| 51 |
#[doc(alias = "Start")] |
| 52 |
async fn start_cast( |
| 53 |
&self, |
| 54 |
session_token: HandleToken, |
| 55 |
app_id: Option<MaybeAppID>, |
| 56 |
window_identifier: Option<WindowIdentifierType>, |
| 57 |
options: StartCastOptions, |
| 58 |
) -> Result<Streams>; |
| 59 |
} |
| 60 |
|
| 61 |
pub(crate) struct ScreencastInterface { |
| 62 |
imp: Arc<dyn ScreencastImpl>, |
| 63 |
spawn: Arc<dyn futures_util::task::Spawn + Send + Sync>, |
| 64 |
cnx: zbus::Connection, |
| 65 |
sessions: Arc<Mutex<SessionManager>>, |
| 66 |
} |
| 67 |
|
| 68 |
impl ScreencastInterface { |
| 69 |
pub fn new( |
| 70 |
imp: Arc<dyn ScreencastImpl>, |
| 71 |
cnx: zbus::Connection, |
| 72 |
spawn: Arc<dyn futures_util::task::Spawn + Send + Sync>, |
| 73 |
sessions: Arc<Mutex<SessionManager>>, |
| 74 |
) -> Self { |
| 75 |
Self { |
| 76 |
imp, |
| 77 |
cnx, |
| 78 |
spawn, |
| 79 |
sessions, |
| 80 |
} |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
#[zbus::interface(name = "org.freedesktop.impl.portal.ScreenCast")] |
| 85 |
impl ScreencastInterface { |
| 86 |
#[zbus( |
| 87 |
property(emits_changed_signal = "const"), |
| 88 |
name = "AvailableSourceTypes" |
| 89 |
)] |
| 90 |
fn available_source_types(&self) -> u32 { |
| 91 |
let imp = Arc::clone(&self.imp); |
| 92 |
imp.available_source_types().bits() |
| 93 |
} |
| 94 |
|
| 95 |
#[zbus( |
| 96 |
property(emits_changed_signal = "const"), |
| 97 |
name = "AvailableCursorModes" |
| 98 |
)] |
| 99 |
fn available_cursor_mode(&self) -> u32 { |
| 100 |
let imp = Arc::clone(&self.imp); |
| 101 |
imp.available_cursor_mode().bits() |
| 102 |
} |
| 103 |
|
| 104 |
#[zbus(property(emits_changed_signal = "const"), name = "version")] |
| 105 |
fn version(&self) -> u32 { |
| 106 |
5 |
| 107 |
} |
| 108 |
|
| 109 |
#[zbus(name = "CreateSession")] |
| 110 |
#[zbus(out_args("response", "results"))] |
| 111 |
async fn create_session( |
| 112 |
&self, |
| 113 |
handle: OwnedObjectPath, |
| 114 |
session_handle: OwnedObjectPath, |
| 115 |
app_id: Optional<MaybeAppID>, |
| 116 |
options: CreateSessionOptions, |
| 117 |
) -> Result<Response<CreateSessionResponse>> { |
| 118 |
let session_token = HandleToken::try_from(&session_handle).unwrap(); |
| 119 |
{ |
| 120 |
let sessions = self.sessions.lock().unwrap(); |
| 121 |
if sessions.contains(&session_token) { |
| 122 |
let errormsg = format!("A session with handle `{session_token}` already exists"); |
| 123 |
#[cfg(feature = "tracing")] |
| 124 |
tracing::error!("ScreencastInterface::create_session: {}", errormsg); |
| 125 |
return Err(PortalError::Exist(errormsg)); |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
let imp = Arc::clone(&self.imp); |
| 130 |
let token = session_token.clone(); |
| 131 |
let result = Request::spawn( |
| 132 |
"ScreenCast::CreateSession", |
| 133 |
&self.cnx, |
| 134 |
handle.clone(), |
| 135 |
Arc::clone(&self.imp), |
| 136 |
Arc::clone(&self.spawn), |
| 137 |
async move { |
| 138 |
imp.create_session( |
| 139 |
HandleToken::try_from(&handle).unwrap(), |
| 140 |
token, |
| 141 |
app_id.into(), |
| 142 |
options, |
| 143 |
) |
| 144 |
.await |
| 145 |
}, |
| 146 |
) |
| 147 |
.await; |
| 148 |
|
| 149 |
if result.is_ok() { |
| 150 |
#[cfg(feature = "tracing")] |
| 151 |
tracing::debug!( |
| 152 |
"ScreencastInterface::create_session: session with handle `{session_token}` created" |
| 153 |
); |
| 154 |
|
| 155 |
let session = Session::new( |
| 156 |
session_handle, |
| 157 |
Arc::clone(&self.sessions), |
| 158 |
Some(Arc::clone(&self.imp) as Arc<dyn SessionImpl>), |
| 159 |
); |
| 160 |
session.serve(self.cnx.clone()).await?; |
| 161 |
{ |
| 162 |
let mut sessions = self.sessions.lock().unwrap(); |
| 163 |
sessions.add(session); |
| 164 |
} |
| 165 |
} else { |
| 166 |
#[cfg(feature = "tracing")] |
| 167 |
tracing::error!( |
| 168 |
"ScreencastInterface::create_session: failed to create a session with handle `{session_token}`" |
| 169 |
); |
| 170 |
} |
| 171 |
|
| 172 |
result |
| 173 |
} |
| 174 |
|
| 175 |
#[zbus(name = "SelectSources")] |
| 176 |
#[zbus(out_args("response", "results"))] |
| 177 |
async fn select_sources( |
| 178 |
&self, |
| 179 |
handle: OwnedObjectPath, |
| 180 |
session_handle: OwnedObjectPath, |
| 181 |
app_id: Optional<MaybeAppID>, |
| 182 |
options: SelectSourcesOptions, |
| 183 |
) -> Result<Response<SelectSourcesResponse>> { |
| 184 |
let session_token = HandleToken::try_from(&session_handle).unwrap(); |
| 185 |
{ |
| 186 |
let sessions = self.sessions.lock().unwrap(); |
| 187 |
sessions.try_contains(&session_token)?; |
| 188 |
} |
| 189 |
|
| 190 |
let imp = Arc::clone(&self.imp); |
| 191 |
Request::spawn( |
| 192 |
"ScreenCast::SelectSources", |
| 193 |
&self.cnx, |
| 194 |
handle.clone(), |
| 195 |
Arc::clone(&self.imp), |
| 196 |
Arc::clone(&self.spawn), |
| 197 |
async move { |
| 198 |
imp.select_sources(session_token, app_id.into(), options) |
| 199 |
.await |
| 200 |
}, |
| 201 |
) |
| 202 |
.await |
| 203 |
} |
| 204 |
|
| 205 |
#[zbus(name = "Start")] |
| 206 |
#[zbus(out_args("response", "results"))] |
| 207 |
async fn start( |
| 208 |
&self, |
| 209 |
handle: OwnedObjectPath, |
| 210 |
session_handle: OwnedObjectPath, |
| 211 |
app_id: Optional<MaybeAppID>, |
| 212 |
window_identifier: Optional<WindowIdentifierType>, |
| 213 |
options: StartCastOptions, |
| 214 |
) -> Result<Response<Streams>> { |
| 215 |
let session_token = HandleToken::try_from(&session_handle).unwrap(); |
| 216 |
{ |
| 217 |
let sessions = self.sessions.lock().unwrap(); |
| 218 |
sessions.try_contains(&session_token)?; |
| 219 |
} |
| 220 |
|
| 221 |
let imp = Arc::clone(&self.imp); |
| 222 |
Request::spawn( |
| 223 |
"ScreenCast::Start", |
| 224 |
&self.cnx, |
| 225 |
handle.clone(), |
| 226 |
Arc::clone(&self.imp), |
| 227 |
Arc::clone(&self.spawn), |
| 228 |
async move { |
| 229 |
imp.start_cast( |
| 230 |
session_token, |
| 231 |
app_id.into(), |
| 232 |
window_identifier.into(), |
| 233 |
options, |
| 234 |
) |
| 235 |
.await |
| 236 |
}, |
| 237 |
) |
| 238 |
.await |
| 239 |
} |
| 240 |
} |