1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */

//! This crate defines the Rust API for WebXR. It is implemented by the `webxr` crate.

mod device;
mod error;
mod events;
mod frame;
mod hand;
mod hittest;
mod input;
mod layer;
mod mock;
mod registry;
mod session;
mod space;
pub mod util;
mod view;

pub use device::DeviceAPI;
pub use device::DiscoveryAPI;

pub use error::Error;

pub use events::Event;
pub use events::EventBuffer;
pub use events::Visibility;

pub use frame::Frame;
pub use frame::FrameUpdateEvent;
pub use frame::ViewerPose;

pub use hand::Finger;
pub use hand::FingerJoint;
pub use hand::Hand;
pub use hand::HandSpace;
pub use hand::Joint;
pub use hand::JointFrame;

pub use hittest::EntityType;
pub use hittest::EntityTypes;
pub use hittest::HitTestId;
pub use hittest::HitTestResult;
pub use hittest::HitTestSource;
pub use hittest::HitTestSpace;
pub use hittest::Ray;
pub use hittest::Triangle;

pub use input::Handedness;
pub use input::InputFrame;
pub use input::InputId;
pub use input::InputSource;
pub use input::SelectEvent;
pub use input::SelectKind;
pub use input::TargetRayMode;

pub use layer::ContextId;
pub use layer::GLContexts;
pub use layer::GLTypes;
pub use layer::LayerGrandManager;
pub use layer::LayerGrandManagerAPI;
pub use layer::LayerId;
pub use layer::LayerInit;
pub use layer::LayerLayout;
pub use layer::LayerManager;
pub use layer::LayerManagerAPI;
pub use layer::LayerManagerFactory;
pub use layer::SubImage;
pub use layer::SubImages;

pub use mock::MockDeviceInit;
pub use mock::MockDeviceMsg;
pub use mock::MockDiscoveryAPI;
pub use mock::MockInputInit;
pub use mock::MockInputMsg;
pub use mock::MockRegion;
pub use mock::MockViewInit;
pub use mock::MockViewsInit;
pub use mock::MockWorld;

pub use registry::MainThreadRegistry;
pub use registry::MainThreadWaker;
pub use registry::Registry;

pub use session::EnvironmentBlendMode;
pub use session::MainThreadSession;
pub use session::Quitter;
pub use session::Session;
pub use session::SessionBuilder;
pub use session::SessionId;
pub use session::SessionInit;
pub use session::SessionMode;
pub use session::SessionThread;

pub use space::ApiSpace;
pub use space::BaseSpace;
pub use space::Space;

pub use view::Capture;
pub use view::CubeBack;
pub use view::CubeBottom;
pub use view::CubeLeft;
pub use view::CubeRight;
pub use view::CubeTop;
pub use view::Display;
pub use view::Floor;
pub use view::Input;
pub use view::LeftEye;
pub use view::Native;
pub use view::RightEye;
pub use view::SomeEye;
pub use view::View;
pub use view::Viewer;
pub use view::Viewport;
pub use view::Viewports;
pub use view::Views;
pub use view::CUBE_BACK;
pub use view::CUBE_BOTTOM;
pub use view::CUBE_LEFT;
pub use view::CUBE_RIGHT;
pub use view::CUBE_TOP;
pub use view::LEFT_EYE;
pub use view::RIGHT_EYE;
pub use view::VIEWER;

#[cfg(feature = "ipc")]
use std::thread;

use std::time::Duration;

#[cfg(feature = "ipc")]
pub use ipc_channel::ipc::IpcSender as Sender;

#[cfg(feature = "ipc")]
pub use ipc_channel::ipc::IpcReceiver as Receiver;

#[cfg(feature = "ipc")]
pub use ipc_channel::ipc::channel;

#[cfg(not(feature = "ipc"))]
pub use std::sync::mpsc::{Receiver, RecvTimeoutError, Sender};

#[cfg(not(feature = "ipc"))]
pub fn channel<T>() -> Result<(Sender<T>, Receiver<T>), ()> {
    Ok(std::sync::mpsc::channel())
}

#[cfg(not(feature = "ipc"))]
pub fn recv_timeout<T>(receiver: &Receiver<T>, timeout: Duration) -> Result<T, RecvTimeoutError> {
    receiver.recv_timeout(timeout)
}

#[cfg(feature = "ipc")]
pub fn recv_timeout<T>(
    receiver: &Receiver<T>,
    timeout: Duration,
) -> Result<T, ipc_channel::ipc::TryRecvError>
where
    T: serde::Serialize + for<'a> serde::Deserialize<'a>,
{
    // Sigh, polling, sigh.
    let mut delay = timeout / 1000;
    while delay < timeout {
        if let Ok(msg) = receiver.try_recv() {
            return Ok(msg);
        }
        thread::sleep(delay);
        delay = delay * 2;
    }
    receiver.try_recv()
}