openxr_sys/
lib.rs

1use std::{convert::TryFrom, fmt};
2
3#[macro_use]
4mod support;
5mod generated;
6pub mod loader;
7pub mod platform;
8
9#[cfg(feature = "mint")]
10mod mint_impls;
11
12pub trait Handle: Copy {
13    const NULL: Self;
14    fn into_raw(self) -> u64;
15    fn from_raw(handle: u64) -> Self;
16}
17
18// Hand-written bindings for cases which are too few or weird to bother automating
19
20wrapper! {
21    #[derive(Copy, Clone, Eq, PartialEq, Default)]
22    Bool32(u32)
23}
24pub const TRUE: Bool32 = Bool32(1);
25pub const FALSE: Bool32 = Bool32(0);
26impl fmt::Display for Bool32 {
27    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
28        (*self != FALSE).fmt(fmt)
29    }
30}
31
32impl fmt::Debug for Bool32 {
33    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
34        (*self != FALSE).fmt(fmt)
35    }
36}
37
38impl From<Bool32> for bool {
39    fn from(x: Bool32) -> Self {
40        x != FALSE
41    }
42}
43
44impl From<bool> for Bool32 {
45    fn from(x: bool) -> Self {
46        Self(x as _)
47    }
48}
49
50#[derive(Copy, Clone, Eq, PartialEq)]
51#[repr(transparent)]
52pub struct Time(i64);
53impl Time {
54    pub fn from_nanos(x: i64) -> Self {
55        Self(x)
56    }
57
58    pub fn as_nanos(self) -> i64 {
59        self.0
60    }
61}
62
63impl fmt::Debug for Time {
64    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
65        self.0.fmt(fmt)
66    }
67}
68
69impl std::ops::Sub<Time> for Time {
70    type Output = Duration;
71
72    fn sub(self, other: Time) -> Duration {
73        Duration(self.0.wrapping_sub(other.0))
74    }
75}
76
77#[derive(Copy, Clone, Default, Eq, PartialEq)]
78#[repr(transparent)]
79pub struct Duration(i64);
80impl Duration {
81    pub fn from_nanos(x: i64) -> Self {
82        Self(x)
83    }
84
85    pub fn as_nanos(self) -> i64 {
86        self.0
87    }
88}
89
90impl Duration {
91    pub const NONE: Self = Self(0);
92    pub const INFINITE: Self = Self(i64::MAX);
93    pub const MIN_HAPTIC: Self = Self(-1);
94}
95
96impl fmt::Debug for Duration {
97    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
98        std::time::Duration::from_nanos(self.0 as u64).fmt(fmt)
99    }
100}
101
102impl From<Duration> for std::time::Duration {
103    fn from(x: Duration) -> Self {
104        Self::from_nanos(x.0.unsigned_abs())
105    }
106}
107
108impl TryFrom<std::time::Duration> for Duration {
109    type Error = std::num::TryFromIntError;
110    fn try_from(x: std::time::Duration) -> std::result::Result<Self, Self::Error> {
111        Ok(Self::from_nanos(i64::try_from(x.as_nanos())?))
112    }
113}
114
115wrapper! {
116    #[derive(Debug, Copy, Clone, Default, Eq, PartialEq, Ord, PartialOrd, Hash)]
117    AsyncRequestIdFB(u64)
118}
119
120wrapper! {
121    #[derive(Debug, Copy, Clone, Default, Eq, PartialEq, Ord, PartialOrd, Hash)]
122    Path(u64)
123}
124
125impl Path {
126    pub const NULL: Path = Path(0);
127}
128
129wrapper! {
130    #[derive(Debug, Copy, Clone, Default, Eq, PartialEq, Ord, PartialOrd, Hash)]
131    SystemId(u64)
132}
133
134impl SystemId {
135    pub const NULL: SystemId = SystemId(0);
136}
137
138wrapper! {
139    #[derive(Debug, Copy, Clone, Default, Eq, PartialEq, Ord, PartialOrd, Hash)]
140    ControllerModelKeyMSFT(u64)
141}
142
143impl ControllerModelKeyMSFT {
144    pub const NULL: ControllerModelKeyMSFT = ControllerModelKeyMSFT(0);
145}
146
147wrapper! {
148    #[derive(Debug, Copy, Clone, Default, Eq, PartialEq, Ord, PartialOrd, Hash)]
149    RenderModelKeyFB(u64)
150}
151
152impl RenderModelKeyFB {
153    pub const NULL: RenderModelKeyFB = RenderModelKeyFB(0);
154}
155
156wrapper! {
157    #[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
158    MarkerML(u64)
159}
160
161wrapper! {
162    #[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
163    Version(u64)
164}
165
166impl Version {
167    #[inline]
168    pub const fn new(major: u16, minor: u16, patch: u32) -> Self {
169        Self((major as u64) << 48 | (minor as u64) << 32 | patch as u64)
170    }
171
172    #[inline]
173    pub const fn major(self) -> u16 {
174        (self.0 >> 48) as u16
175    }
176
177    #[inline]
178    pub const fn minor(self) -> u16 {
179        (self.0 >> 32) as u16
180    }
181
182    #[inline]
183    pub const fn patch(self) -> u32 {
184        self.0 as u32
185    }
186}
187
188impl From<(u16, u16, u32)> for Version {
189    fn from(other: (u16, u16, u32)) -> Self {
190        Self::new(other.0, other.1, other.2)
191    }
192}
193
194impl fmt::Display for Version {
195    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
196        write!(fmt, "{}.{}.{}", self.major(), self.minor(), self.patch())
197    }
198}
199
200impl fmt::Debug for Version {
201    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
202        write!(fmt, "{}.{}.{}", self.major(), self.minor(), self.patch())
203    }
204}
205
206pub const FREQUENCY_UNSPECIFIED: f32 = 0.0;
207
208impl Quaternionf {
209    pub const IDENTITY: Quaternionf = Quaternionf {
210        x: 0.0,
211        y: 0.0,
212        z: 0.0,
213        w: 1.0,
214    };
215}
216
217impl Posef {
218    pub const IDENTITY: Posef = Posef {
219        orientation: Quaternionf::IDENTITY,
220        position: Vector3f {
221            x: 0.0,
222            y: 0.0,
223            z: 0.0,
224        },
225    };
226}
227
228pub const HAND_JOINT_COUNT_EXT: u32 = 26;
229
230pub use generated::*;
231
232impl<T> std::ops::Index<HandJointEXT> for [T] {
233    type Output = T;
234    fn index(&self, joint: HandJointEXT) -> &T {
235        &self[joint.into_raw() as usize]
236    }
237}
238
239impl<T> std::ops::IndexMut<HandJointEXT> for [T] {
240    fn index_mut(&mut self, joint: HandJointEXT) -> &mut T {
241        &mut self[joint.into_raw() as usize]
242    }
243}
244
245// Magic Leap
246#[derive(Copy, Clone, Debug)]
247#[repr(C)]
248pub struct MLCoordinateFrameUID {
249    pub data: [u64; 2],
250}
251
252// Hacky constants originating from enums
253pub const EYE_POSITION_COUNT_FB: usize = 2;
254pub const MAX_VIRTUAL_KEYBOARD_COMMIT_TEXT_SIZE_META: usize = 3992;
255
256pub type SpaceUserIdFB = u64;
257pub type FutureEXT = u64;