webxr_api/
lib.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
5//! This crate defines the Rust API for WebXR. It is implemented by the `webxr` crate.
6
7mod device;
8mod error;
9mod events;
10mod frame;
11mod hand;
12mod hittest;
13mod input;
14mod layer;
15mod mock;
16mod registry;
17mod session;
18mod space;
19pub mod util;
20mod view;
21
22#[cfg(not(feature = "ipc"))]
23pub use std::sync::mpsc::{RecvTimeoutError, WebXrReceiver, WebXrSender};
24#[cfg(feature = "ipc")]
25use std::thread;
26use std::time::Duration;
27
28pub use device::{DeviceAPI, DiscoveryAPI};
29pub use error::Error;
30pub use events::{Event, EventBuffer, Visibility};
31pub use frame::{Frame, FrameUpdateEvent, ViewerPose};
32pub use hand::{Finger, FingerJoint, Hand, HandSpace, Joint, JointFrame};
33pub use hittest::{
34    EntityType, EntityTypes, HitTestId, HitTestResult, HitTestSource, HitTestSpace, Ray, Triangle,
35};
36pub use input::{
37    Handedness, InputFrame, InputId, InputSource, SelectEvent, SelectKind, TargetRayMode,
38};
39#[cfg(feature = "ipc")]
40pub use ipc_channel::ipc::IpcReceiver as WebXrReceiver;
41#[cfg(feature = "ipc")]
42pub use ipc_channel::ipc::IpcSender as WebXrSender;
43#[cfg(feature = "ipc")]
44pub use ipc_channel::ipc::channel as webxr_channel;
45pub use layer::{
46    ContextId, GLContexts, GLTypes, LayerGrandManager, LayerGrandManagerAPI, LayerId, LayerInit,
47    LayerLayout, LayerManager, LayerManagerAPI, LayerManagerFactory, SubImage, SubImages,
48};
49pub use mock::{
50    MockButton, MockButtonType, MockDeviceInit, MockDeviceMsg, MockDiscoveryAPI, MockInputInit,
51    MockInputMsg, MockRegion, MockViewInit, MockViewsInit, MockWorld,
52};
53pub use registry::{MainThreadRegistry, Registry};
54pub use session::{
55    EnvironmentBlendMode, MainThreadSession, Quitter, Session, SessionBuilder, SessionId,
56    SessionInit, SessionMode, SessionThread,
57};
58pub use space::{ApiSpace, BaseSpace, Space};
59pub use view::{
60    CUBE_BACK, CUBE_BOTTOM, CUBE_LEFT, CUBE_RIGHT, CUBE_TOP, Capture, CubeBack, CubeBottom,
61    CubeLeft, CubeRight, CubeTop, Display, Floor, Input, LEFT_EYE, LeftEye, Native, RIGHT_EYE,
62    RightEye, SomeEye, VIEWER, View, Viewer, Viewport, Viewports, Views,
63};
64
65#[cfg(not(feature = "ipc"))]
66pub fn webxr_channel<T>() -> Result<(WebXrWebXrSender<T>, WebXrWebXrReceiver<T>), ()> {
67    Ok(std::sync::mpsc::channel())
68}
69
70#[cfg(not(feature = "ipc"))]
71pub fn recv_timeout<T>(
72    receiver: &WebXrReceiver<T>,
73    timeout: Duration,
74) -> Result<T, RecvTimeoutError> {
75    receiver.recv_timeout(timeout)
76}
77
78#[cfg(feature = "ipc")]
79pub fn recv_timeout<T>(
80    receiver: &WebXrReceiver<T>,
81    timeout: Duration,
82) -> Result<T, ipc_channel::ipc::TryRecvError>
83where
84    T: serde::Serialize + for<'a> serde::Deserialize<'a>,
85{
86    // Sigh, polling, sigh.
87    let mut delay = timeout / 1000;
88    while delay < timeout {
89        if let Ok(msg) = receiver.try_recv() {
90            return Ok(msg);
91        }
92        thread::sleep(delay);
93        delay *= 2;
94    }
95    receiver.try_recv()
96}