Skip to content
This repository was archived by the owner on Feb 16, 2025. It is now read-only.

Commit f6eed01

Browse files
author
Alan Jeffrey
committed
Add a main thread waker to the registry
1 parent 7229f13 commit f6eed01

2 files changed

Lines changed: 55 additions & 1 deletion

File tree

webxr-api/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ pub use mock::MockInputInit;
4040
pub use mock::MockInputMsg;
4141

4242
pub use registry::MainThreadRegistry;
43+
pub use registry::MainThreadWaker;
4344
pub use registry::Registry;
4445
pub use registry::{MockDeviceCallback, SessionRequestCallback, SessionSupportCallback};
4546

webxr-api/registry.rs

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ use serde::{Deserialize, Serialize};
2121
#[cfg_attr(feature = "ipc", derive(Serialize, Deserialize))]
2222
pub struct Registry {
2323
sender: Sender<RegistryMsg>,
24+
waker: MainThreadWakerImpl,
2425
}
2526

2627
pub struct MainThreadRegistry {
@@ -29,6 +30,52 @@ pub struct MainThreadRegistry {
2930
mocks: Vec<Box<dyn MockDiscovery>>,
3031
sender: Sender<RegistryMsg>,
3132
receiver: Receiver<RegistryMsg>,
33+
waker: MainThreadWakerImpl,
34+
}
35+
36+
pub trait MainThreadWaker: 'static + Send {
37+
fn clone_box(&self) -> Box<dyn MainThreadWaker>;
38+
fn wake(&self);
39+
}
40+
41+
impl Clone for Box<dyn MainThreadWaker> {
42+
fn clone(&self) -> Self {
43+
self.clone_box()
44+
}
45+
}
46+
47+
#[derive(Clone)]
48+
#[cfg_attr(feature = "ipc", derive(Serialize, Deserialize))]
49+
struct MainThreadWakerImpl {
50+
#[cfg(feature = "ipc")]
51+
sender: Sender<()>,
52+
#[cfg(not(feature = "ipc"))]
53+
waker: Box<dyn MainThreadWaker>,
54+
}
55+
56+
#[cfg(feature = "ipc")]
57+
impl MainThreadWakerImpl {
58+
fn new(waker: Box<dyn MainThreadWaker>) -> Result<MainThreadWakerImpl, Error> {
59+
let (sender, receiver) = crate::channel().or(Err(Error::CommunicationError))?;
60+
ipc_channel::router::ROUTER
61+
.add_route(receiver.to_opaque(), Box::new(move |_| waker.wake()));
62+
Ok(MainThreadWakerImpl { sender })
63+
}
64+
65+
fn wake(&self) {
66+
let _ = self.sender.send(());
67+
}
68+
}
69+
70+
#[cfg(not(feature = "ipc"))]
71+
impl MainThreadWakerImpl {
72+
fn new(waker: Box<dyn MainThreadWaker>) -> Result<MainThreadWakerImpl, Error> {
73+
Ok(MainThreadWakerImpl { waker })
74+
}
75+
76+
pub fn wake(&self) {
77+
self.waker.wake()
78+
}
3279
}
3380

3481
#[cfg_attr(feature = "ipc", typetag::serde)]
@@ -54,6 +101,7 @@ impl Registry {
54101
let _ = self
55102
.sender
56103
.send(RegistryMsg::SupportsSession(mode, Box::new(callback)));
104+
self.waker.wake();
57105
}
58106

59107
pub fn request_session<C>(&mut self, mode: SessionMode, callback: C)
@@ -63,6 +111,7 @@ impl Registry {
63111
let _ = self
64112
.sender
65113
.send(RegistryMsg::RequestSession(mode, Box::new(callback)));
114+
self.waker.wake();
66115
}
67116

68117
pub fn simulate_device_connection<C>(&mut self, init: MockDeviceInit, callback: C)
@@ -73,27 +122,31 @@ impl Registry {
73122
init,
74123
Box::new(callback),
75124
));
125+
self.waker.wake();
76126
}
77127
}
78128

79129
impl MainThreadRegistry {
80-
pub fn new() -> Result<MainThreadRegistry, Error> {
130+
pub fn new(waker: Box<dyn MainThreadWaker>) -> Result<MainThreadRegistry, Error> {
81131
let (sender, receiver) = crate::channel().or(Err(Error::CommunicationError))?;
82132
let discoveries = Vec::new();
83133
let sessions = Vec::new();
84134
let mocks = Vec::new();
135+
let waker = MainThreadWakerImpl::new(waker)?;
85136
Ok(MainThreadRegistry {
86137
discoveries,
87138
sessions,
88139
mocks,
89140
sender,
90141
receiver,
142+
waker,
91143
})
92144
}
93145

94146
pub fn registry(&self) -> Registry {
95147
Registry {
96148
sender: self.sender.clone(),
149+
waker: self.waker.clone(),
97150
}
98151
}
99152

0 commit comments

Comments
 (0)