ashpd 0.1.1 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +66 -0
- data/Cargo.lock +1 -0
- data/ext/ashpd/Cargo.toml +9 -2
- data/ext/ashpd/src/devices.rs +152 -0
- data/ext/ashpd/src/files.rs +211 -0
- data/ext/ashpd/src/lib.rs +124 -6
- data/ext/ashpd/src/screencast.rs +387 -0
- data/ext/ashpd/src/sessions.rs +545 -0
- data/ext/ashpd/src/shell.rs +278 -0
- data/ext/ashpd/src/system.rs +220 -0
- data/lib/ashpd/version.rb +1 -1
- metadata +8 -1
|
@@ -0,0 +1,387 @@
|
|
|
1
|
+
//! org.freedesktop.portal.ScreenCast -- casting a screen to a PipeWire stream.
|
|
2
|
+
//!
|
|
3
|
+
//! This portal is unlike the others bound here, in two ways that shape the API.
|
|
4
|
+
//!
|
|
5
|
+
//! It is stateful. A cast is a *session*: created, then told what to record,
|
|
6
|
+
//! then started, and it lives until it is closed or the D-Bus connection that
|
|
7
|
+
//! made it goes away. So this is a class with methods rather than a module
|
|
8
|
+
//! function, and the object has to be kept for as long as the stream is wanted.
|
|
9
|
+
//! Let it go and the compositor stops casting.
|
|
10
|
+
//!
|
|
11
|
+
//! It hands back a file descriptor. The streams the portal publishes are not on
|
|
12
|
+
//! the PipeWire the rest of the session can see; they are on a remote of the
|
|
13
|
+
//! portal's own, and the only way in is a descriptor passed over D-Bus. A node
|
|
14
|
+
//! id alone is useless - a consumer that connects to the ordinary PipeWire and
|
|
15
|
+
//! asks for that node gets "target not found" - so {Session#open_pipe_wire_remote}
|
|
16
|
+
//! is what makes the stream readable, and the pair of numbers it leaves is what
|
|
17
|
+
//! a consumer needs:
|
|
18
|
+
//!
|
|
19
|
+
//! ```ruby
|
|
20
|
+
//! session = Ashpd::ScreenCast::Session.new
|
|
21
|
+
//! session.select_sources(sources: [:monitor], cursor_mode: :embedded)
|
|
22
|
+
//! stream = session.start.first
|
|
23
|
+
//! fd = session.open_pipe_wire_remote
|
|
24
|
+
//!
|
|
25
|
+
//! system("gst-launch-1.0", "pipewiresrc", "fd=#{fd}", "path=#{stream['node_id']}",
|
|
26
|
+
//! "!", "videoconvert", "!", "autovideosink", fd => fd)
|
|
27
|
+
//! ```
|
|
28
|
+
//!
|
|
29
|
+
//! # Provenance
|
|
30
|
+
//!
|
|
31
|
+
//! - <https://flatpak.github.io/xdg-desktop-portal/docs/doc-org.freedesktop.portal.ScreenCast.html>
|
|
32
|
+
//! - ashpd's own `screen_cast_gstreamer.rs` example, for the fd and node id pair.
|
|
33
|
+
|
|
34
|
+
use std::os::fd::IntoRawFd;
|
|
35
|
+
use std::sync::Mutex;
|
|
36
|
+
|
|
37
|
+
use magnus::{function, method, prelude::*, Error, RArray, RHash, RModule, Ruby, TryConvert};
|
|
38
|
+
|
|
39
|
+
use ashpd_core::desktop::screencast::{
|
|
40
|
+
CursorMode, OpenPipeWireRemoteOptions, Screencast, SelectSourcesOptions, SourceType,
|
|
41
|
+
StartCastOptions, Stream, Streams,
|
|
42
|
+
};
|
|
43
|
+
use ashpd_core::desktop::{PersistMode, Session};
|
|
44
|
+
use ashpd_core::enumflags2::BitFlags;
|
|
45
|
+
|
|
46
|
+
use crate::{block, option_bool, option_name, rt_err, runtime};
|
|
47
|
+
|
|
48
|
+
// --- names -----------------------------------------------------------------
|
|
49
|
+
|
|
50
|
+
/// The interfaces number their enumerations, and a number tells a reader
|
|
51
|
+
/// nothing. Each is taken and given as a symbol instead, with the name the
|
|
52
|
+
/// specification uses.
|
|
53
|
+
fn source_type_from(name: &str) -> Result<SourceType, Error> {
|
|
54
|
+
match name {
|
|
55
|
+
"monitor" => Ok(SourceType::Monitor),
|
|
56
|
+
"window" => Ok(SourceType::Window),
|
|
57
|
+
"virtual" => Ok(SourceType::Virtual),
|
|
58
|
+
other => Err(rt_err(format!(
|
|
59
|
+
"unknown source type {other:?}, wanted :monitor, :window or :virtual"
|
|
60
|
+
))),
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
fn source_type_name(source_type: SourceType) -> &'static str {
|
|
65
|
+
match source_type {
|
|
66
|
+
SourceType::Monitor => "monitor",
|
|
67
|
+
SourceType::Window => "window",
|
|
68
|
+
SourceType::Virtual => "virtual",
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
fn cursor_mode_from(name: &str) -> Result<CursorMode, Error> {
|
|
73
|
+
match name {
|
|
74
|
+
"hidden" => Ok(CursorMode::Hidden),
|
|
75
|
+
"embedded" => Ok(CursorMode::Embedded),
|
|
76
|
+
"metadata" => Ok(CursorMode::Metadata),
|
|
77
|
+
other => Err(rt_err(format!(
|
|
78
|
+
"unknown cursor mode {other:?}, wanted :hidden, :embedded or :metadata"
|
|
79
|
+
))),
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
fn cursor_mode_name(cursor_mode: CursorMode) -> &'static str {
|
|
84
|
+
match cursor_mode {
|
|
85
|
+
CursorMode::Hidden => "hidden",
|
|
86
|
+
CursorMode::Embedded => "embedded",
|
|
87
|
+
CursorMode::Metadata => "metadata",
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/// How long the permission outlives the session: not at all, until the
|
|
92
|
+
/// application exits, or until the user revokes it. Only the last is any use to
|
|
93
|
+
/// a program that wants to cast again tomorrow without asking.
|
|
94
|
+
fn persist_mode_from(name: &str) -> Result<PersistMode, Error> {
|
|
95
|
+
match name {
|
|
96
|
+
"do_not" => Ok(PersistMode::DoNot),
|
|
97
|
+
"application" => Ok(PersistMode::Application),
|
|
98
|
+
"explicitly_revoked" => Ok(PersistMode::ExplicitlyRevoked),
|
|
99
|
+
other => Err(rt_err(format!(
|
|
100
|
+
"unknown persist mode {other:?}, wanted :do_not, :application or :explicitly_revoked"
|
|
101
|
+
))),
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// --- the session -----------------------------------------------------------
|
|
106
|
+
|
|
107
|
+
/// The proxy and the session together, because neither is any use alone and
|
|
108
|
+
/// both have to be let go at the same moment.
|
|
109
|
+
///
|
|
110
|
+
/// They are kept behind an Option so that closing can take them: a closed
|
|
111
|
+
/// session is a real state, and a method called on one should say so rather
|
|
112
|
+
/// than talk to a portal that has already finished with us.
|
|
113
|
+
struct Parts {
|
|
114
|
+
proxy: Screencast,
|
|
115
|
+
session: Session<Screencast>,
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/// A screen cast session: what is being recorded, and the way in to it.
|
|
119
|
+
#[magnus::wrap(class = "Ashpd::ScreenCast::Session", free_immediately, size)]
|
|
120
|
+
struct ScreenCastSession {
|
|
121
|
+
parts: Mutex<Option<Parts>>,
|
|
122
|
+
/// What Start answered, kept so the streams and the restore token can be
|
|
123
|
+
/// asked for separately afterwards.
|
|
124
|
+
started: Mutex<Option<Streams>>,
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
impl ScreenCastSession {
|
|
128
|
+
fn new() -> Result<Self, Error> {
|
|
129
|
+
// One block_on for both, so that every temporary the proxy and the
|
|
130
|
+
// session create along the way is dropped inside the runtime.
|
|
131
|
+
let parts = block(async {
|
|
132
|
+
let proxy = Screencast::new().await?;
|
|
133
|
+
let session = proxy.create_session(Default::default()).await?;
|
|
134
|
+
|
|
135
|
+
Ok::<_, ashpd_core::Error>(Parts { proxy, session })
|
|
136
|
+
})?
|
|
137
|
+
.map_err(rt_err)?;
|
|
138
|
+
|
|
139
|
+
Ok(Self {
|
|
140
|
+
parts: Mutex::new(Some(parts)),
|
|
141
|
+
started: Mutex::new(None),
|
|
142
|
+
})
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/// Say what to record. This must come before {start}, and the portal allows
|
|
146
|
+
/// it only once per session.
|
|
147
|
+
///
|
|
148
|
+
/// `restore_token` is a token a previous session handed back. Given one the
|
|
149
|
+
/// portal may restore that permission and start without asking the user
|
|
150
|
+
/// again; a token it will not accept is ignored rather than fatal, and the
|
|
151
|
+
/// user is asked as if none had been given.
|
|
152
|
+
fn select_sources(ruby: &Ruby, rb_self: &Self, options: RHash) -> Result<(), Error> {
|
|
153
|
+
let mut selection = SelectSourcesOptions::default();
|
|
154
|
+
|
|
155
|
+
if let Some(sources) = options.get(ruby.to_symbol("sources")) {
|
|
156
|
+
let sources = RArray::try_convert(sources)?;
|
|
157
|
+
let mut types = BitFlags::<SourceType>::empty();
|
|
158
|
+
|
|
159
|
+
for source in sources.into_iter() {
|
|
160
|
+
types |= source_type_from(&source.to_r_string()?.to_string()?)?;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
selection = selection.set_sources(types);
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
if let Some(name) = option_name(ruby, options, "cursor_mode")? {
|
|
167
|
+
selection = selection.set_cursor_mode(cursor_mode_from(&name)?);
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
if let Some(multiple) = option_bool(ruby, options, "multiple") {
|
|
171
|
+
selection = selection.set_multiple(multiple);
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
if let Some(name) = option_name(ruby, options, "persist_mode")? {
|
|
175
|
+
selection = selection.set_persist_mode(persist_mode_from(&name)?);
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
let token = option_name(ruby, options, "restore_token")?;
|
|
179
|
+
let selection = selection.set_restore_token(token.as_deref());
|
|
180
|
+
|
|
181
|
+
let guard = rb_self.locked()?;
|
|
182
|
+
let parts = Self::open(&guard)?;
|
|
183
|
+
|
|
184
|
+
// The request is awaited and read inside the block, so that it is
|
|
185
|
+
// dropped in there too. A zbus request holds tokio resources, and
|
|
186
|
+
// dropping one with no runtime entered panics with "there is no
|
|
187
|
+
// reactor running" -- which across the FFI boundary takes the VM with
|
|
188
|
+
// it rather than raising.
|
|
189
|
+
block(async {
|
|
190
|
+
parts
|
|
191
|
+
.proxy
|
|
192
|
+
.select_sources(&parts.session, selection)
|
|
193
|
+
.await?
|
|
194
|
+
.response()
|
|
195
|
+
})?
|
|
196
|
+
.map_err(rt_err)?;
|
|
197
|
+
|
|
198
|
+
Ok(())
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/// Start casting, and return the streams the portal published.
|
|
202
|
+
///
|
|
203
|
+
/// This is where the user is asked, unless a restore token spared them, so
|
|
204
|
+
/// it does not return until they have answered.
|
|
205
|
+
fn start(ruby: &Ruby, rb_self: &Self) -> Result<RArray, Error> {
|
|
206
|
+
let streams = {
|
|
207
|
+
let guard = rb_self.locked()?;
|
|
208
|
+
let parts = Self::open(&guard)?;
|
|
209
|
+
|
|
210
|
+
block(async {
|
|
211
|
+
parts
|
|
212
|
+
.proxy
|
|
213
|
+
.start(&parts.session, None, StartCastOptions::default())
|
|
214
|
+
.await?
|
|
215
|
+
.response()
|
|
216
|
+
})?
|
|
217
|
+
.map_err(rt_err)?
|
|
218
|
+
};
|
|
219
|
+
|
|
220
|
+
let array = ruby.ary_new_capa(streams.streams().len());
|
|
221
|
+
for stream in streams.streams() {
|
|
222
|
+
let _ = array.push(stream_to(ruby, stream)?);
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
*rb_self.started.lock().map_err(rt_err)? = Some(streams);
|
|
226
|
+
|
|
227
|
+
Ok(array)
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
/// The token to keep for next time, if the portal issued one.
|
|
231
|
+
///
|
|
232
|
+
/// A fresh token comes back from every start, and using one does not keep
|
|
233
|
+
/// it alive, so the one to save is always the newest.
|
|
234
|
+
fn restore_token(&self) -> Result<Option<String>, Error> {
|
|
235
|
+
Ok(self
|
|
236
|
+
.started
|
|
237
|
+
.lock()
|
|
238
|
+
.map_err(rt_err)?
|
|
239
|
+
.as_ref()
|
|
240
|
+
.and_then(|streams| streams.restore_token().map(ToOwned::to_owned)))
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
/// A descriptor for the PipeWire remote the streams are on.
|
|
244
|
+
///
|
|
245
|
+
/// The descriptor is handed to Ruby and Ruby owns it: nothing here closes
|
|
246
|
+
/// it, and whoever asked should, once the stream is finished with. It only
|
|
247
|
+
/// works while this session is alive.
|
|
248
|
+
fn open_pipe_wire_remote(&self) -> Result<i32, Error> {
|
|
249
|
+
let guard = self.locked()?;
|
|
250
|
+
let parts = Self::open(&guard)?;
|
|
251
|
+
|
|
252
|
+
let fd = block(async {
|
|
253
|
+
parts
|
|
254
|
+
.proxy
|
|
255
|
+
.open_pipe_wire_remote(&parts.session, OpenPipeWireRemoteOptions::default())
|
|
256
|
+
.await
|
|
257
|
+
})?
|
|
258
|
+
.map_err(rt_err)?;
|
|
259
|
+
|
|
260
|
+
Ok(fd.into_raw_fd())
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
/// Stop casting, and let the portal go.
|
|
264
|
+
///
|
|
265
|
+
/// Closing twice is not an error: the second time there is nothing left to
|
|
266
|
+
/// close, which is the state the caller asked for.
|
|
267
|
+
fn close(&self) -> Result<(), Error> {
|
|
268
|
+
let Some(parts) = self.locked()?.take() else {
|
|
269
|
+
return Ok(());
|
|
270
|
+
};
|
|
271
|
+
|
|
272
|
+
block(async {
|
|
273
|
+
let result = parts.session.close().await;
|
|
274
|
+
drop(parts);
|
|
275
|
+
result
|
|
276
|
+
})?
|
|
277
|
+
.map_err(rt_err)
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
fn locked(&self) -> Result<std::sync::MutexGuard<'_, Option<Parts>>, Error> {
|
|
281
|
+
self.parts.lock().map_err(rt_err)
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
fn open<'a>(guard: &'a std::sync::MutexGuard<'_, Option<Parts>>) -> Result<&'a Parts, Error> {
|
|
285
|
+
guard
|
|
286
|
+
.as_ref()
|
|
287
|
+
.ok_or_else(|| rt_err("the screen cast session is closed"))
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
impl Drop for ScreenCastSession {
|
|
292
|
+
/// Ruby frees this whenever the garbage collector gets to it, on whatever
|
|
293
|
+
/// it was doing at the time. The parts have to be dropped inside the
|
|
294
|
+
/// runtime for the same reason the requests above do, so the drop is done
|
|
295
|
+
/// in a block rather than left to the fields.
|
|
296
|
+
fn drop(&mut self) {
|
|
297
|
+
let Ok(mut guard) = self.parts.lock() else {
|
|
298
|
+
return;
|
|
299
|
+
};
|
|
300
|
+
let Some(parts) = guard.take() else {
|
|
301
|
+
return;
|
|
302
|
+
};
|
|
303
|
+
let Ok(runtime) = runtime() else {
|
|
304
|
+
return;
|
|
305
|
+
};
|
|
306
|
+
|
|
307
|
+
runtime.block_on(async move {
|
|
308
|
+
drop(parts);
|
|
309
|
+
});
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
/// One stream, as `{"node_id" =>, "position" =>, "size" =>, "source_type" =>, "id" =>}`.
|
|
314
|
+
///
|
|
315
|
+
/// node_id is the only part a consumer must have; the rest the portal may omit,
|
|
316
|
+
/// and then it is nil.
|
|
317
|
+
fn stream_to(ruby: &Ruby, stream: &Stream) -> Result<RHash, Error> {
|
|
318
|
+
let hash = ruby.hash_new();
|
|
319
|
+
|
|
320
|
+
hash.aset("node_id", stream.pipe_wire_node_id())?;
|
|
321
|
+
hash.aset("position", stream.position().map(|(x, y)| vec![x, y]))?;
|
|
322
|
+
hash.aset("size", stream.size().map(|(width, height)| vec![width, height]))?;
|
|
323
|
+
hash.aset(
|
|
324
|
+
"source_type",
|
|
325
|
+
stream.source_type().map(|kind| ruby.to_symbol(source_type_name(kind))),
|
|
326
|
+
)?;
|
|
327
|
+
hash.aset("id", stream.id())?;
|
|
328
|
+
hash.aset("mapping_id", stream.mapping_id())?;
|
|
329
|
+
|
|
330
|
+
Ok(hash)
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
// --- the portal ------------------------------------------------------------
|
|
334
|
+
|
|
335
|
+
/// Which kinds of source this compositor will cast, as symbols.
|
|
336
|
+
fn available_source_types(ruby: &Ruby) -> Result<RArray, Error> {
|
|
337
|
+
let proxy = block(Screencast::new())?.map_err(rt_err)?;
|
|
338
|
+
let types = block(proxy.available_source_types())?.map_err(rt_err)?;
|
|
339
|
+
|
|
340
|
+
let array = ruby.ary_new();
|
|
341
|
+
for source_type in types.iter() {
|
|
342
|
+
let _ = array.push(ruby.to_symbol(source_type_name(source_type)));
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
Ok(array)
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
/// Which cursor modes this compositor offers, as symbols.
|
|
349
|
+
fn available_cursor_modes(ruby: &Ruby) -> Result<RArray, Error> {
|
|
350
|
+
let proxy = block(Screencast::new())?.map_err(rt_err)?;
|
|
351
|
+
let modes = block(proxy.available_cursor_modes())?.map_err(rt_err)?;
|
|
352
|
+
|
|
353
|
+
let array = ruby.ary_new();
|
|
354
|
+
for mode in modes.iter() {
|
|
355
|
+
let _ = array.push(ruby.to_symbol(cursor_mode_name(mode)));
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
Ok(array)
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
/// The version of the interface the portal implements.
|
|
362
|
+
fn version() -> Result<u32, Error> {
|
|
363
|
+
let proxy = block(Screencast::new())?.map_err(rt_err)?;
|
|
364
|
+
|
|
365
|
+
Ok(proxy.version())
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
pub fn init(ruby: &Ruby, namespace: RModule) -> Result<(), Error> {
|
|
369
|
+
let module = namespace.define_module("ScreenCast")?;
|
|
370
|
+
|
|
371
|
+
module.define_singleton_method("available_source_types", function!(available_source_types, 0))?;
|
|
372
|
+
module.define_singleton_method("available_cursor_modes", function!(available_cursor_modes, 0))?;
|
|
373
|
+
module.define_singleton_method("version", function!(version, 0))?;
|
|
374
|
+
|
|
375
|
+
let session = module.define_class("Session", ruby.class_object())?;
|
|
376
|
+
session.define_singleton_method("new", function!(ScreenCastSession::new, 0))?;
|
|
377
|
+
session.define_method("select_sources", method!(ScreenCastSession::select_sources, 1))?;
|
|
378
|
+
session.define_method("start", method!(ScreenCastSession::start, 0))?;
|
|
379
|
+
session.define_method("restore_token", method!(ScreenCastSession::restore_token, 0))?;
|
|
380
|
+
session.define_method(
|
|
381
|
+
"open_pipe_wire_remote",
|
|
382
|
+
method!(ScreenCastSession::open_pipe_wire_remote, 0),
|
|
383
|
+
)?;
|
|
384
|
+
session.define_method("close", method!(ScreenCastSession::close, 0))?;
|
|
385
|
+
|
|
386
|
+
Ok(())
|
|
387
|
+
}
|