Skip to content

Commit e7a027f

Browse files
committed
WIP cursor capture
1 parent 29e780d commit e7a027f

File tree

2 files changed

+49
-11
lines changed

2 files changed

+49
-11
lines changed

src/wayland/cursor_stream.rs

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,56 @@
1-
use cosmic_client_toolkit::screencopy::CaptureSession;
1+
use cosmic_client_toolkit::screencopy::{CaptureSession, FailureReason, Frame};
2+
use futures::channel::oneshot;
23
use std::{
34
pin::Pin,
5+
sync::Mutex,
46
task::{Context, Poll},
57
};
8+
use wayland_client::{QueueHandle, WEnum};
9+
10+
use super::{AppData, CursorCaptureSessionData, FrameData};
11+
12+
enum State {
13+
WaitingForFormats,
14+
Capturing(oneshot::Receiver<Result<Frame, WEnum<FailureReason>>>),
15+
}
616

717
// TODO wake stream when we get formats?
818
pub struct CursorStream {
919
// TODO formats
10-
pub(super) capture_session: CaptureSession,
20+
capture_session: CaptureSession,
21+
qh: QueueHandle<AppData>,
22+
}
23+
24+
impl CursorStream {
25+
pub(super) fn new(capture_session: &CaptureSession, qh: &QueueHandle<AppData>) -> Self {
26+
Self {
27+
capture_session: capture_session.clone(),
28+
qh: qh.clone(),
29+
}
30+
}
1131
}
1232

1333
impl futures::stream::Stream for CursorStream {
1434
type Item = image::RgbaImage;
1535

1636
fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<image::RgbaImage>> {
37+
let (sender, receiver) = oneshot::channel();
38+
let data = self
39+
.capture_session
40+
.data::<CursorCaptureSessionData>()
41+
.unwrap();
42+
*data.waker.lock().unwrap() = Some(cx.waker().clone());
43+
let formats = data.formats.lock().unwrap().clone();
44+
// WIP damage
45+
self.capture_session.capture(
46+
todo!(),
47+
&[],
48+
&self.qh,
49+
FrameData {
50+
frame_data: Default::default(),
51+
sender: Mutex::new(Some(sender)),
52+
},
53+
);
1754
todo!()
1855
}
1956
}

src/wayland/mod.rs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,6 @@ struct SessionInner {
180180
wayland_helper: WaylandHelper,
181181
capture_session: CaptureSession,
182182
capture_cursor_session: Option<(CaptureCursorSession, CaptureSession)>,
183-
capture_cursor_formats: Mutex<Option<Formats>>,
184183
condvar: Condvar,
185184
state: Mutex<SessionState>,
186185
}
@@ -244,14 +243,15 @@ impl Session {
244243
receiver.await.unwrap()
245244
}
246245

247-
// Should only be called once
246+
// XXX Should only be called once
248247
fn cursor_stream(&self) -> Option<cursor_stream::CursorStream> {
249248
let Some((_, capture_session)) = &self.0.capture_cursor_session else {
250249
return None;
251250
};
252-
Some(cursor_stream::CursorStream {
253-
capture_session: capture_session.clone(),
254-
})
251+
Some(cursor_stream::CursorStream::new(
252+
capture_session,
253+
&self.0.wayland_helper.inner.qh,
254+
))
255255
}
256256
}
257257

@@ -414,6 +414,8 @@ impl WaylandHelper {
414414
CursorCaptureSessionData {
415415
session: weak_session.clone(),
416416
session_data: ScreencopySessionData::default(),
417+
waker: Mutex::new(None),
418+
formats: Mutex::new(None),
417419
},
418420
)
419421
.unwrap();
@@ -427,7 +429,6 @@ impl WaylandHelper {
427429
wayland_helper: self.clone(),
428430
capture_session,
429431
capture_cursor_session,
430-
capture_cursor_formats: Mutex::new(None),
431432
condvar: Condvar::new(),
432433
state: Default::default(),
433434
}
@@ -658,9 +659,7 @@ impl ScreencopyHandler for AppData {
658659
data.formats = Some(formats.clone());
659660
});
660661
} else if let Some(data) = session.data::<CursorCaptureSessionData>() {
661-
if let Some(session_inner) = data.session.upgrade() {
662-
*session_inner.capture_cursor_formats.lock().unwrap() = Some(formats.clone());
663-
}
662+
*data.formats.lock().unwrap() = Some(formats.clone());
664663
println!("Cursor session formats: {:?}", formats);
665664
}
666665
}
@@ -860,6 +859,8 @@ impl ScreencopySessionDataExt for SessionData {
860859
struct CursorCaptureSessionData {
861860
session: Weak<SessionInner>,
862861
session_data: ScreencopySessionData,
862+
waker: Mutex<Option<std::task::Waker>>,
863+
formats: Mutex<Option<Formats>>,
863864
}
864865

865866
impl ScreencopySessionDataExt for CursorCaptureSessionData {

0 commit comments

Comments
 (0)