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
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */

//! An event loop implementation that works in headless mode.


use glutin;
use servo::embedder_traits::EventLoopWaker;
use std::sync::Arc;
use std::rc::Rc;
use std::cell::RefCell;
use std::thread;
use std::time;

pub struct EventsLoop(Option<glutin::EventsLoop>);

impl EventsLoop {
    // Ideally, we could use the winit event loop in both modes,
    // but on Linux, the event loop requires a X11 server.
    #[cfg(not(target_os = "linux"))]
    pub fn new(_headless: bool) -> Rc<RefCell<EventsLoop>> {
        Rc::new(RefCell::new(EventsLoop(Some(glutin::EventsLoop::new()))))
    }
    #[cfg(target_os = "linux")]
    pub fn new(headless: bool) -> Rc<RefCell<EventsLoop>> {
        let events_loop = if headless {
            None
        } else {
            Some(glutin::EventsLoop::new())
        };
        Rc::new(RefCell::new(EventsLoop(events_loop)))
    }
}

impl EventsLoop {
    pub fn create_event_loop_waker(&self) -> Box<dyn EventLoopWaker> {
        if let Some(ref events_loop) = self.0 {
            Box::new(HeadedEventLoopWaker::new(&events_loop))
        } else {
            Box::new(HeadlessEventLoopWaker)
        }
    }
    pub fn as_winit(&self) -> &glutin::EventsLoop {
        &self.0.as_ref().expect("Can't access winit event loop while using the fake headless event loop")
    }
    pub fn take(&mut self) -> Option<glutin::EventsLoop> {
        self.0.take()
    }
    pub fn poll_events<F>(&mut self, callback: F) where F: FnMut(glutin::Event) {
        if let Some(ref mut events_loop) = self.0 {
            events_loop.poll_events(callback);
        } else {
            self.sleep();
        }
    }
    pub fn run_forever<F>(&mut self, mut callback: F) where F: FnMut(glutin::Event) -> glutin::ControlFlow {
        if let Some(ref mut events_loop) = self.0 {
            events_loop.run_forever(callback);
        } else {
            loop {
                self.sleep();
                if callback(glutin::Event::Awakened) == glutin::ControlFlow::Break {
                    break;
                }
            }
        }
    }
    fn sleep(&self) {
        thread::sleep(time::Duration::from_millis(5));
    }
}

struct HeadedEventLoopWaker {
    proxy: Arc<glutin::EventsLoopProxy>,
}
impl HeadedEventLoopWaker {
    fn new(events_loop: &glutin::EventsLoop) -> HeadedEventLoopWaker {
        let proxy = Arc::new(events_loop.create_proxy());
        HeadedEventLoopWaker { proxy }
    }
}
impl EventLoopWaker for HeadedEventLoopWaker {
    fn wake(&self) {
        // kick the OS event loop awake.
        if let Err(err) = self.proxy.wakeup() {
            warn!("Failed to wake up event loop ({}).", err);
        }
    }
    fn clone(&self) -> Box<dyn EventLoopWaker + Send> {
        Box::new(HeadedEventLoopWaker {
            proxy: self.proxy.clone(),
        })
    }
}

struct HeadlessEventLoopWaker;
impl EventLoopWaker for HeadlessEventLoopWaker {
    fn wake(&self) {}
    fn clone(&self) -> Box<dyn EventLoopWaker + Send> { Box::new(HeadlessEventLoopWaker) }
}