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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
//! Idiomatic Rust bindings for the Oculus SDK. Provides access to headset
//! metadata and tracking information, plus helpers for attaching the headset
//! to an OpenGL rendering context.

#[macro_use] extern crate bitflags;
extern crate libc;
#[cfg(windows)] extern crate winapi;
#[cfg(windows)] extern crate kernel32;

#[cfg(feature = "glutin")]
extern crate glutin;

use std::rc::Rc;
use std::fmt;

mod ffi;
mod shim;

pub use shim::HmdDisplayId;
pub use shim::HmdDisplay;

pub mod render;
pub mod target;

/// Error produced while interacting with a wrapped Oculus device.
#[derive(Clone, Debug)]
pub enum OculusError {
    /// Error while attempting to find the Oculus runtime. This probably means a supported version
    /// of the runtime is not installed.
    OculusRuntimeError(String),

    /// Error while interacting directly with the Oculus SDK. The SDK doesn't provide more detailed
    /// error information, but the included string provides some basic context about what was
    /// happening at the time of failure.
    SdkError(&'static str),

    /// Only one `Context` can be active at a time per process. This error occurs when attempting to
    /// create a second `Context` while a `Context` is already active.
    DuplicateContext
}

impl fmt::Display for OculusError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            &OculusError::OculusRuntimeError(ref description) => f.write_str(description),
            &OculusError::SdkError(ref description) => f.write_str(description),
            &OculusError::DuplicateContext => f.write_str(
                "Context creation failed because another Context is already active in this process")
        }
    }
}

#[derive(Copy, Clone)]
pub enum Eye {
    Left,
    Right
}

/// Oculus SDK context. Ensures the Oculus SDK has been initialized properly, and serves as a
/// factory for builders that give access to the HMD.
pub struct Context {
    shim_context: Rc<shim::Context>
}

impl Context {
    /// Create a new Oculus SDK context.
    ///
    /// # Failure
    ///
    /// Only one `Context` can be active per process. If a `Context` is already active, this will
    /// fail with `Err(OculsuError::DuplicateContext)`. Note that `Hmd`s hold an internal reference
    /// to their associated context.
    pub fn new() -> Result<Context, OculusError> {
        let shim_context = Rc::new(try!(shim::Context::new()));
        Ok(Context {
            shim_context: shim_context
        })
    }

    /// Create a builder for an HMD.
    pub fn build_hmd(&self) -> HmdBuilder {
        HmdBuilder::new(self.shim_context.clone())
    }
}

/// Options for specifying the enabled tracking capabilities of a headset.
pub struct TrackingOptions {
    track_caps: ffi::ovrTrackingCaps
}

impl TrackingOptions {
    /// `TrackingOptions` with no tracking options enabled.
    pub fn new() -> TrackingOptions {
        TrackingOptions {
            track_caps: ffi::ovrTrackingCaps::empty()
        }
    }

    /// `TrackingOptions` with all supported tracking options enabled.
    pub fn with_all() -> TrackingOptions {
        TrackingOptions {
            track_caps: ffi::ovrTrackingCap_Orientation |
                ffi::ovrTrackingCap_MagYawCorrection |
                ffi::ovrTrackingCap_Position
        }
    }

    /// Enable tracking of head position.
    pub fn position<'f>(&'f mut self) -> &'f mut TrackingOptions {
        self.track_caps.insert(ffi::ovrTrackingCap_Position);
        self
    }

    /// Enable tracking of head orientation.
    pub fn orientation<'f>(&'f mut self) -> &'f mut TrackingOptions {
        self.track_caps.insert(ffi::ovrTrackingCap_Orientation);
        self
    }

    /// Enable yaw drift correction.
    pub fn mag_yaw_correct<'f>(&'f mut self) -> &'f mut TrackingOptions {
        self.track_caps.insert(ffi::ovrTrackingCap_MagYawCorrection);
        self
    }
    
}

/// Builder to construct an HMD. Allows the configuration of HMD settings and tracking
/// capabilities.
pub struct HmdBuilder {
    caps: ffi::ovrHmdCaps,
    track_caps: ffi::ovrTrackingCaps,
    allow_debug: bool,
    owning_context: Rc<shim::Context> 
}

impl HmdBuilder {
    fn new(owning_context: Rc<shim::Context>) -> HmdBuilder {
        let default_caps = ffi::ovrHmdCap_LowPersistence | ffi::ovrHmdCap_DynamicPrediction;
        HmdBuilder { 
            caps: default_caps, 
            track_caps: ffi::ovrTrackingCaps::empty(), 
            allow_debug: false,
            owning_context: owning_context
        }
    }

    /// Disables mirroring of HMD output to the attached window. This may improve
    /// rendering performance slightly.
    pub fn no_mirror<'f>(&'f mut self) -> &'f mut HmdBuilder {
        self.caps.insert(ffi::ovrHmdCap_NoMirrorToWindow);
        self
    }

    /// Turns off HMD screen and output (only if the HMD is not in Direct display
    /// mode).
    pub fn no_display<'f>(&'f mut self) -> &'f mut HmdBuilder {
        self.caps.insert(ffi::ovrHmdCap_DisplayOff);
        self
    }

    /// Disable low persistence.
    pub fn no_low_persistence<'f>(&'f mut self) -> &'f mut HmdBuilder {
        self.caps.remove(ffi::ovrHmdCap_LowPersistence);
        self
    }

    /// Disable dynamic adjustment of tracking prediction based on internally
    /// measured latency.
    pub fn no_dynamic_prediction<'f>(&'f mut self) -> &'f mut HmdBuilder {
        self.caps.remove(ffi::ovrHmdCap_DynamicPrediction);
        self
    }
    
    /// Disable VSync.
    pub fn no_vsync<'f>(&'f mut self) -> &'f mut HmdBuilder {
        self.caps.insert(ffi::ovrHmdCap_NoVSync);
        self
    }

    /// Enable tracking with the specified tracking options.
    pub fn track<'f>(&'f mut self, tracking_options: &TrackingOptions) -> &'f mut HmdBuilder {
        self.track_caps = tracking_options.track_caps;
        self
    }

    /// Allow creation of a dummy "debug" HMD if no other HMD is found.
    pub fn allow_debug<'f>(&'f mut self) -> &'f mut HmdBuilder {
        self.allow_debug = true;
        self
    }

    /// Build the HMD instance. This will begin tracking if tracking is enabled.
    pub fn build(&self) -> Result<Hmd, OculusError> {
        Hmd::new(self.caps, self.track_caps, self.allow_debug, self.owning_context.clone())
    }
}

/// A target window to bind headset rendering to.
pub trait RenderTarget {
    /// Number of samples used for MSAA.
    fn get_multisample(&self) -> u32;

    /// The native window handle for this window. This can return null for all platforms except
    /// Windows. The returned handle must be valid with an effective lifetime greater than or equal 
    /// to the lifetime of self.
    unsafe fn get_native_window(&self) -> *const libc::c_void;
}

/// An initialized HMD.
pub struct Hmd {
    shim_hmd: shim::Hmd
}

impl Hmd {
    fn new(caps: ffi::ovrHmdCaps, 
           track_caps: ffi::ovrTrackingCaps,
           allow_debug: bool,
           owning_context: Rc<shim::Context>) -> Result<Hmd, OculusError> {
        let mut shim_hmd = try!(shim::Hmd::new(allow_debug, owning_context));
        shim_hmd.set_caps(caps);
        if !track_caps.is_empty() {
            try!(shim_hmd.configure_tracking(track_caps));
        }
        Ok(Hmd{ shim_hmd: shim_hmd })
    }

    /// Create a `RenderContext` for this headset.
    pub fn render_to<'a>(&'a self,
                         target: &'a RenderTarget) -> Result<render::RenderContext, OculusError> {
        use shim::CreateRenderContext;
        render::RenderContext::new(&self.shim_hmd, target)
    }

    /// Returns a `(width, height)` pair representing the native resolution of the HMD.
    pub fn resolution(&self) -> (u32, u32) {
        self.shim_hmd.resolution()
    }

    /// Return details about the display representing this headset.
    pub fn get_display(&self) -> HmdDisplay {
        self.shim_hmd.get_display()
    }
}