rust_webvr/api/mock/
service.rs

1use {VRService, VRDisplayPtr, VREvent, VRGamepadPtr};
2use super::display::{MockVRDisplay, MockVRDisplayPtr};
3use super::{MockVRControlMsg, MockVRInit};
4use std::thread;
5use std::sync::mpsc::Receiver;
6
7pub struct MockVRService {
8    display: MockVRDisplayPtr,
9}
10
11unsafe impl Send for MockVRService {}
12
13impl VRService for MockVRService {
14    fn initialize(&mut self) -> Result<(), String> { 
15        Ok(())
16    }
17
18    fn fetch_displays(&mut self) -> Result<Vec<VRDisplayPtr>,String> {
19        Ok(vec![self.display.clone()])
20    }
21
22    fn fetch_gamepads(&mut self) -> Result<Vec<VRGamepadPtr>,String> {
23        Ok(Vec::new())
24    }
25
26    fn is_available(&self) -> bool {
27        true   
28    }
29
30    fn poll_events(&self) -> Vec<VREvent> {
31        self.display.borrow().poll_events()
32    }
33}
34
35impl MockVRService {
36    pub fn new(init: MockVRInit) -> MockVRService {
37        MockVRService {
38            display: MockVRDisplay::new(init),
39        }
40    }
41
42    pub fn new_with_receiver(rcv: Receiver<MockVRControlMsg>, init: MockVRInit) -> MockVRService {
43        let display = MockVRDisplay::new(init);
44        let state = display.borrow().state_handle();
45        thread::spawn(move || {
46            while let Ok(msg) = rcv.recv() {
47                // The only reason we need this is that the overall display API
48                // is somewhat unsound:
49                // https://github.com/servo/rust-webvr/issues/18 .
50                // Once that is fixed we should just have a handle to the display here
51                state.lock().unwrap().handle_msg(msg);
52            }
53        });
54        MockVRService {
55            display,
56        }
57    }
58}
59