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
use crate::browser::Browser;
use crate::embedder::EmbedderCallbacks;
use crate::events_loop::{EventsLoop, ServoEvent};
use crate::window_trait::WindowPortsMethods;
use crate::{headed_window, headless_window};
use winit::window::WindowId;
use winit::event_loop::EventLoopWindowTarget;
use servo::compositing::windowing::WindowEvent;
use servo::config::opts::{self, parse_url_or_filename};
use servo::servo_config::pref;
use servo::servo_url::ServoUrl;
use servo::Servo;
use std::cell::{Cell, RefCell};
use std::collections::HashMap;
use std::env;
use std::rc::Rc;
use webxr::glwindow::GlWindowDiscovery;
pub struct App {
servo: Option<Servo<dyn WindowPortsMethods>>,
browser: RefCell<Browser<dyn WindowPortsMethods>>,
event_queue: RefCell<Vec<WindowEvent>>,
suspended: Cell<bool>,
windows: HashMap<WindowId, Rc<dyn WindowPortsMethods>>,
}
impl App {
pub fn run(
no_native_titlebar: bool,
device_pixels_per_px: Option<f32>,
user_agent: Option<String>,
) {
let events_loop = EventsLoop::new(opts::get().headless, opts::get().output_file.is_some());
let window = if opts::get().headless {
headless_window::Window::new(opts::get().initial_window_size, device_pixels_per_px)
} else {
Rc::new(headed_window::Window::new(
opts::get().initial_window_size,
&events_loop,
no_native_titlebar,
device_pixels_per_px,
))
};
let browser = Browser::new(window.clone());
let mut app = App {
event_queue: RefCell::new(vec![]),
browser: RefCell::new(browser),
servo: None,
suspended: Cell::new(false),
windows: HashMap::new(),
};
let ev_waker = events_loop.create_event_loop_waker();
events_loop.run_forever(move |e, w, control_flow| {
if let winit::event::Event::NewEvents(winit::event::StartCause::Init) = e {
let surfman = window.webrender_surfman();
let xr_discovery = if pref!(dom.webxr.glwindow.enabled) && ! opts::get().headless {
let window = window.clone();
let w = unsafe {
std::mem::transmute::<
&EventLoopWindowTarget<ServoEvent>,
&'static EventLoopWindowTarget<ServoEvent>
>(w.unwrap())
};
let factory = Box::new(move || Ok(window.new_glwindow(w)));
Some(GlWindowDiscovery::new(
surfman.connection(),
surfman.adapter(),
surfman.context_attributes(),
factory,
))
} else {
None
};
let window = window.clone();
let embedder = Box::new(EmbedderCallbacks::new(
ev_waker.clone(),
xr_discovery,
));
let servo_data = Servo::new(embedder, window.clone(), user_agent.clone());
let mut servo = servo_data.servo;
servo.handle_events(vec![WindowEvent::NewBrowser(get_default_url(), servo_data.browser_id)]);
servo.setup_logging();
app.windows.insert(window.id(), window.clone());
app.servo = Some(servo);
}
if app.servo.is_none() {
return;
}
app.winit_event_to_servo_event(e);
let animating = app.is_animating();
if !animating || app.suspended.get() {
*control_flow = winit::event_loop::ControlFlow::Wait;
} else {
*control_flow = winit::event_loop::ControlFlow::Poll;
}
let stop = app.handle_events();
if stop {
*control_flow = winit::event_loop::ControlFlow::Exit;
app.servo.take().unwrap().deinit();
}
});
}
fn is_animating(&self) -> bool {
self.windows.iter().any(|(_, window)| window.is_animating())
}
fn get_events(&self) -> Vec<WindowEvent> {
std::mem::take(&mut *self.event_queue.borrow_mut())
}
fn winit_event_to_servo_event(&self, event: winit::event::Event<'_, ServoEvent>) {
match event {
winit::event::Event::Suspended => {
self.suspended.set(true);
},
winit::event::Event::Resumed => {
self.suspended.set(false);
self.event_queue.borrow_mut().push(WindowEvent::Idle);
},
winit::event::Event::UserEvent(_) => {
self.event_queue.borrow_mut().push(WindowEvent::Idle);
},
winit::event::Event::DeviceEvent { .. } => {},
winit::event::Event::RedrawRequested(_) => {
self.event_queue.borrow_mut().push(WindowEvent::Idle);
},
winit::event::Event::WindowEvent {
window_id, event, ..
} => {
match self.windows.get(&window_id) {
None => {
warn!("Got an event from unknown window");
},
Some(window) => {
window.winit_event_to_servo_event(event);
},
}
},
winit::event::Event::LoopDestroyed |
winit::event::Event::NewEvents(..) |
winit::event::Event::MainEventsCleared |
winit::event::Event::RedrawEventsCleared => {},
}
}
fn handle_events(&mut self) -> bool {
let mut browser = self.browser.borrow_mut();
let mut app_events = self.get_events();
for (_win_id, window) in &self.windows {
app_events.extend(window.get_events());
}
browser.handle_window_events(app_events);
let mut servo_events = self.servo.as_mut().unwrap().get_events();
let mut need_resize = false;
loop {
browser.handle_servo_events(servo_events);
need_resize |= self.servo.as_mut().unwrap().handle_events(browser.get_events());
if browser.shutdown_requested() {
return true;
}
servo_events = self.servo.as_mut().unwrap().get_events();
if servo_events.is_empty() {
break;
}
}
if need_resize {
self.servo.as_mut().unwrap().repaint_synchronously();
}
false
}
}
fn get_default_url() -> ServoUrl {
let cwd = env::current_dir().unwrap();
let cmdline_url = opts::get().url.clone();
let pref_url = {
let homepage_url = pref!(shell.homepage);
parse_url_or_filename(&cwd, &homepage_url).ok()
};
let blank_url = ServoUrl::parse("about:blank").ok();
cmdline_url.or(pref_url).or(blank_url).unwrap()
}