/// <summary> /// Returns *any* attached device to this computer. /// In case there's no device attached to yet, waits for the specified /// amount of time (or idenfinately, in case null is specified) and /// either returns a reference to the HMD, or null in case none /// was attached within the specified time. /// </summary> /// <param name="waitTime"></param> /// <returns></returns> public IHMD WaitForAttachedDevice(TimeSpan?waitTime) { // Warning to my dear reader: This method is ugly as f**k // // At first, we try to find a device which is attached. // If we succeed, then we can return that and be happy. // Otherwise, we subscribe to the DeviceAttached event // temporarily, and wait in this thread until the event // is fired, or until the specified time has elapsed. // IHMD hmd = AttachedDevice; if (hmd != null) { return(hmd); } using (var handle = new AutoResetEvent(false)) { Action <IHMD> fn = attachedHMD => { // Invoked in case a device was indeed attached. hmd = attachedHMD; try { handle.Set(); } catch (ObjectDisposedException) { } }; DeviceAttached += fn; try { if (waitTime == null) { handle.WaitOne(); } else { handle.WaitOne(waitTime.Value); } } finally { DeviceAttached -= fn; } } return(hmd); }
public void Initialize() { m_hmdManager = new HMDManager(); m_hmd = m_hmdManager.AttachedDevice ?? m_hmdManager.WaitForAttachedDevice(null); HorizontalResolution = m_hmd.Info.HResolution; VerticalResolution = m_hmd.Info.VResolution; VScreenSize = m_hmd.Info.VScreenSize; EyeToScreenDistance = m_hmd.Info.EyeToScreenDistance; HScreenSize = m_hmd.Info.HScreenSize; LensSeparationDistance = m_hmd.Info.LensSeparationDistance; InterpupillaryDistance = m_hmd.Info.InterpupillaryDistance; DistortionK = m_hmd.Info.DistortionK; m_hmd.Reset(); }
public void TestDeviceAdded() { // // This test uses the dummy implementation and sends a message from another // thread that a device has been added. // using (var mgr = new HMDManager(new Dummy.Factory())) { var debug = (Dummy.DeviceManager)mgr.Manager; mgr.Devices.Count.Should().Be(0); IHMD hmdAdded = null; Action <IHMD> fn = tmp => { hmdAdded = tmp; }; mgr.DeviceAttached += fn; var sw = new Stopwatch(); sw.Start(); debug.SimulateAddOneDevice(TimeSpan.FromMilliseconds(100), false); var hmd = mgr.WaitForAttachedDevice(null); sw.Elapsed.Should().BeGreaterOrEqualTo(TimeSpan.FromMilliseconds(100)); //< Because of the way, we can be certain // The manager must have waited for the device to be attached. hmd.Should().NotBeNull(); mgr.Devices.Count.Should().Be(1); mgr.Devices[0].Should().BeSameAs(hmd); // The manager must have emitted the signal *and* passed the correct // hmd hmdAdded.Should().NotBeNull(); hmdAdded.Should().BeSameAs(hmd); // The HMD must recognize that it is currently attached hmd.IsAttached.Should().BeTrue(); // The manager must return this device, as it is currently attached mgr.AttachedDevice.Should().BeSameAs(hmd); } }