diff --git a/Cargo.lock b/Cargo.lock index c22cbf50586..e453b1eafa9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5646,7 +5646,7 @@ dependencies = [ [[package]] name = "webxr" version = "0.0.1" -source = "git+https://github.com/servo/webxr#876424c9adbe4592518f76888452c0cdbb6adbed" +source = "git+https://github.com/servo/webxr#72d85b8254f8ce5d10f91d59ed35cd5969d15ed8" dependencies = [ "bindgen 0.51.1 (registry+https://github.com/rust-lang/crates.io-index)", "euclid 0.20.1 (registry+https://github.com/rust-lang/crates.io-index)", @@ -5664,7 +5664,7 @@ dependencies = [ [[package]] name = "webxr-api" version = "0.0.1" -source = "git+https://github.com/servo/webxr#876424c9adbe4592518f76888452c0cdbb6adbed" +source = "git+https://github.com/servo/webxr#72d85b8254f8ce5d10f91d59ed35cd5969d15ed8" dependencies = [ "euclid 0.20.1 (registry+https://github.com/rust-lang/crates.io-index)", "gleam 0.6.18 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/ports/glutin/app.rs b/ports/glutin/app.rs index 991662808f2..77bd3eb41dc 100644 --- a/ports/glutin/app.rs +++ b/ports/glutin/app.rs @@ -9,19 +9,24 @@ use crate::embedder::EmbedderCallbacks; use crate::window_trait::WindowPortsMethods; use crate::events_loop::EventsLoop; use crate::{headed_window, headless_window}; +use glutin::WindowId; 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::{BrowserId, Servo}; use std::cell::{Cell, RefCell}; +use std::collections::HashMap; use std::env; use std::mem; use std::rc::Rc; +thread_local! { + pub static WINDOWS: RefCell>> = RefCell::new(HashMap::new()); +} + pub struct App { events_loop: Rc>, - window: Rc, servo: RefCell>, browser: RefCell>, event_queue: RefCell>, @@ -54,10 +59,11 @@ impl App { servo.handle_events(vec![WindowEvent::NewBrowser(get_default_url(), browser_id)]); servo.setup_logging(); + register_window(window); + let app = App { event_queue: RefCell::new(vec![]), events_loop, - window: window, browser: RefCell::new(browser), servo: RefCell::new(servo), suspended: Cell::new(false), @@ -89,18 +95,24 @@ impl App { glutin::Event::WindowEvent { window_id, event, .. } => { - if Some(window_id) != self.window.id() { - warn!("Got an event from unknown window"); - } else { - // Resize events need to be handled during run_forever - let cont = if let glutin::WindowEvent::Resized(_) = event { - glutin::ControlFlow::Continue - } else { - glutin::ControlFlow::Break - }; - self.window.winit_event_to_servo_event(event); - return cont; - } + return WINDOWS.with(|windows| { + match windows.borrow().get(&window_id) { + None => { + warn!("Got an event from unknown window"); + glutin::ControlFlow::Break + }, + Some(window) => { + // Resize events need to be handled during run_forever + let cont = if let glutin::WindowEvent::Resized(_) = event { + glutin::ControlFlow::Continue + } else { + glutin::ControlFlow::Break + }; + window.winit_event_to_servo_event(event); + return cont; + } + } + }); }, } glutin::ControlFlow::Break @@ -108,7 +120,10 @@ impl App { fn run_loop(self) { loop { - if !self.window.is_animating() || self.suspended.get() { + let animating = WINDOWS.with(|windows| { + windows.borrow().iter().any(|(_, window)| window.is_animating()) + }); + if !animating || self.suspended.get() { // If there's no animations running then we block on the window event loop. self.events_loop.borrow_mut().run_forever(|e| { let cont = self.winit_event_to_servo_event(e); @@ -141,18 +156,27 @@ impl App { let mut browser = self.browser.borrow_mut(); let mut servo = self.servo.borrow_mut(); - let win_events = self.window.get_events(); + // FIXME: + // As of now, we support only one browser (self.browser) + // but have multiple windows (dom.webxr.glwindow). We forward + // the events of all the windows combined to that single + // browser instance. Pressing the "a" key on the glwindow + // will send a key event to the servo window. + + let mut app_events = self.get_events(); + WINDOWS.with(|windows| { + for (_win_id, window) in &*windows.borrow() { + app_events.extend(window.get_events()); + } + }); // FIXME: this could be handled by Servo. We don't need // a repaint_synchronously function exposed. - let need_resize = win_events.iter().any(|e| match *e { + let need_resize = app_events.iter().any(|e| match *e { WindowEvent::Resize => true, _ => false, }); - let mut app_events = self.get_events(); - app_events.extend(win_events); - browser.handle_window_events(app_events); let mut servo_events = servo.get_events(); @@ -189,6 +213,12 @@ fn get_default_url() -> ServoUrl { cmdline_url.or(pref_url).or(blank_url).unwrap() } +pub fn register_window(window: Rc) { + WINDOWS.with(|w| { + w.borrow_mut().insert(window.id(), window); + }); +} + pub fn gl_version() -> glutin::GlRequest { if opts::get().angle { glutin::GlRequest::Specific(glutin::Api::OpenGlEs, (3, 0)) diff --git a/ports/glutin/headed_window.rs b/ports/glutin/headed_window.rs index 294f9c2973c..034cbe3a945 100644 --- a/ports/glutin/headed_window.rs +++ b/ports/glutin/headed_window.rs @@ -9,7 +9,7 @@ use crate::context::GlContext; use crate::events_loop::EventsLoop; use crate::keyutils::keyboard_event_from_winit; use crate::window_trait::{WindowPortsMethods, LINE_HEIGHT}; -use euclid::{default::Size2D as UntypedSize2D, Point2D, Scale, Size2D, Vector2D}; +use euclid::{Angle, default::Size2D as UntypedSize2D, Point2D, Rotation3D, Scale, Size2D, UnknownUnit, Vector2D}; use gleam::gl; use glutin::dpi::{LogicalPosition, LogicalSize, PhysicalSize}; #[cfg(target_os = "macos")] @@ -17,7 +17,7 @@ use glutin::os::macos::{ActivationPolicy, WindowBuilderExt}; use glutin::Api; #[cfg(any(target_os = "linux", target_os = "windows"))] use glutin::Icon; -use glutin::{ElementState, KeyboardInput, MouseButton, MouseScrollDelta, TouchPhase}; +use glutin::{ElementState, KeyboardInput, MouseButton, MouseScrollDelta, TouchPhase, VirtualKeyCode}; #[cfg(any(target_os = "linux", target_os = "windows"))] use image; use keyboard_types::{Key, KeyState, KeyboardEvent}; @@ -68,6 +68,7 @@ pub struct Window { animation_state: Cell, fullscreen: Cell, gl: Rc, + xr_rotation: Cell>, } #[cfg(not(target_os = "windows"))] @@ -194,6 +195,7 @@ impl Window { inner_size: Cell::new(inner_size), primary_monitor, screen_size, + xr_rotation: Cell::new(Rotation3D::identity()), }; window.present(); @@ -235,12 +237,36 @@ impl Window { self.last_pressed.set(Some(event)); } else if event.key != Key::Unidentified { self.last_pressed.set(None); + self.handle_xr_rotation(&input); self.event_queue .borrow_mut() .push(WindowEvent::Keyboard(event)); } } + fn handle_xr_rotation(&self, input: &KeyboardInput) { + if input.state != glutin::ElementState::Pressed { + return; + } + let mut x = 0.0; + let mut y = 0.0; + match input.virtual_keycode { + Some(VirtualKeyCode::Up) => x = 1.0, + Some(VirtualKeyCode::Down) => x = -1.0, + Some(VirtualKeyCode::Left) => y = 1.0, + Some(VirtualKeyCode::Right) => y = -1.0, + _ => return, + }; + if input.modifiers.shift { + x = 10.0 * x; + y = 10.0 * y; + } + let x: Rotation3D<_, UnknownUnit, UnknownUnit> = Rotation3D::around_x(Angle::degrees(x)); + let y: Rotation3D<_, UnknownUnit, UnknownUnit> = Rotation3D::around_y(Angle::degrees(y)); + let rotation = self.xr_rotation.get().post_rotate(&x).post_rotate(&y); + self.xr_rotation.set(rotation); + } + /// Helper function to handle a click fn handle_mouse( &self, @@ -394,8 +420,8 @@ impl WindowPortsMethods for Window { self.animation_state.get() == AnimationState::Animating } - fn id(&self) -> Option { - Some(self.gl_context.borrow().window().id()) + fn id(&self) -> glutin::WindowId { + self.gl_context.borrow().window().id() } fn winit_event_to_servo_event(&self, event: glutin::WindowEvent) { @@ -486,15 +512,15 @@ impl WindowPortsMethods for Window { } impl webxr::glwindow::GlWindow for Window { - fn make_current(&mut self) { + fn make_current(&self) { debug!("Making window {:?} current", self.gl_context.borrow().window().id()); - self.gl_context.get_mut().make_current(); + self.gl_context.borrow_mut().make_current(); } - fn swap_buffers(&mut self) { + fn swap_buffers(&self) { debug!("Swapping buffers on window {:?}", self.gl_context.borrow().window().id()); - self.gl_context.get_mut().swap_buffers(); - self.gl_context.get_mut().make_not_current(); + self.gl_context.borrow().swap_buffers(); + self.gl_context.borrow_mut().make_not_current(); } fn size(&self) -> UntypedSize2D { @@ -508,12 +534,18 @@ impl webxr::glwindow::GlWindow for Window { Size2D::new(width * dpr, height *dpr).to_i32() } - fn new_window(&self) -> Result, ()> { - Ok(Box::new(Window::new( + fn get_rotation(&self) -> Rotation3D { + self.xr_rotation.get().clone() + } + + fn new_window(&self) -> Result, ()> { + let window = Rc::new(Window::new( self.inner_size.get(), Some(self), self.events_loop.clone(), - ))) + )); + app::register_window(window.clone()); + Ok(window) } } diff --git a/ports/glutin/headless_window.rs b/ports/glutin/headless_window.rs index f195f75b779..4b87915bb17 100644 --- a/ports/glutin/headless_window.rs +++ b/ports/glutin/headless_window.rs @@ -6,7 +6,7 @@ use crate::window_trait::WindowPortsMethods; use glutin; -use euclid::{default::Size2D as UntypedSize2D, Point2D, Scale, Size2D}; +use euclid::{default::Size2D as UntypedSize2D, Point2D, Rotation3D, Scale, Size2D, UnknownUnit}; use gleam::gl; use servo::compositing::windowing::{AnimationState, WindowEvent}; use servo::compositing::windowing::{EmbedderCoordinates, WindowMethods}; @@ -132,8 +132,10 @@ impl WindowPortsMethods for Window { false } - fn id(&self) -> Option { - None + fn id(&self) -> glutin::WindowId { + unsafe { + glutin::WindowId::dummy() + } } fn page_height(&self) -> f32 { @@ -217,23 +219,26 @@ impl WindowMethods for Window { } impl webxr::glwindow::GlWindow for Window { - fn make_current(&mut self) {} - fn swap_buffers(&mut self) {} + fn make_current(&self) {} + fn swap_buffers(&self) {} fn size(&self) -> UntypedSize2D { let dpr = self.servo_hidpi_factor().get(); Size2D::new((self.context.width as f32 * dpr) as gl::GLsizei, (self.context.height as f32 * dpr) as gl::GLsizei) } - fn new_window(&self) -> Result, ()> { + fn new_window(&self) -> Result, ()> { let width = self.context.width; let height = self.context.height; let share = Some(&self.context); let context = HeadlessContext::new(width, height, share); let gl = self.gl.clone(); - Ok(Box::new(Window { + Ok(Rc::new(Window { context, gl, animation_state: Cell::new(AnimationState::Idle), fullscreen: Cell::new(false), })) } + fn get_rotation(&self) -> Rotation3D { + Rotation3D::identity() + } } diff --git a/ports/glutin/window_trait.rs b/ports/glutin/window_trait.rs index eab5b86bf0f..6a1fd57f631 100644 --- a/ports/glutin/window_trait.rs +++ b/ports/glutin/window_trait.rs @@ -15,7 +15,7 @@ pub const LINE_HEIGHT: f32 = 38.0; pub trait WindowPortsMethods: WindowMethods + webxr::glwindow::GlWindow { fn get_events(&self) -> Vec; - fn id(&self) -> Option; + fn id(&self) -> glutin::WindowId; fn has_events(&self) -> bool; fn page_height(&self) -> f32; fn get_fullscreen(&self) -> bool;