Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ wayland-protocols-misc = { version = "0.3.8", features = ["server"], optional =
wayland-server = { version = "0.31.9", optional = true }
wayland-sys = { version = "0.31.6", optional = true }
wayland-backend = { version = "0.3.10", optional = true }
winit = { version = "0.30.0", default-features = false, features = ["wayland", "wayland-dlopen", "x11", "rwh_06"], optional = true }
winit = { version = "0.31.0-beta.2", default-features = false, features = ["wayland", "wayland-dlopen", "x11"], optional = true }
x11rb = { version = "0.13.0", optional = true, features = ["res"]}
xkbcommon = { version = "0.9.0", features = ["wayland"]}
encoding_rs = { version = "0.8.33", optional = true }
Expand Down
2 changes: 1 addition & 1 deletion anvil/src/winit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ use smithay::{
calloop::EventLoop,
wayland_protocols::wp::presentation_time::server::wp_presentation_feedback,
wayland_server::{protocol::wl_surface, Display},
winit::platform::pump_events::PumpStatus,
winit::event_loop::pump_events::PumpStatus,
},
utils::{IsAlive, Scale, Transform},
wayland::{
Expand Down
2 changes: 1 addition & 1 deletion src/backend/egl/native.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ impl<A: AsFd + Send + 'static> EGLNativeDisplay for GbmDevice<A> {
}

#[cfg(feature = "backend_winit")]
impl EGLNativeDisplay for Arc<WinitWindow> {
impl EGLNativeDisplay for Arc<dyn WinitWindow> {
fn supported_platforms(&self) -> Vec<EGLPlatform<'_>> {
use winit::raw_window_handle::{self, HasDisplayHandle};

Expand Down
17 changes: 13 additions & 4 deletions src/backend/winit/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,13 +190,22 @@ impl PointerButtonEvent<WinitInput> for WinitMouseInputEvent {
WinitMouseButton::Middle => 0x112,
WinitMouseButton::Forward => 0x115,
WinitMouseButton::Back => 0x116,
WinitMouseButton::Other(b) => {
// XXX?
_ => {
if self.is_x11 {
input::xorg_mouse_to_libinput(b as u32)
input::xorg_mouse_to_libinput(self.button as u32 + 1)
} else {
b as u32
self.button as u32 + 1
}
}
} /*
WinitMouseButton::Other(b) => {
if self.is_x11 {
input::xorg_mouse_to_libinput(b as u32)
} else {
b as u32
}
}
*/
}
}

Expand Down
133 changes: 86 additions & 47 deletions src/backend/winit/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,16 @@ use calloop::generic::Generic;
use calloop::{EventSource, Interest, PostAction, Readiness, Token};
use tracing::{debug, info, info_span, instrument, trace, warn};
use wayland_egl as wegl;
use winit::platform::pump_events::PumpStatus;
use winit::platform::scancode::PhysicalKeyExtScancode;
use winit::raw_window_handle::{HasWindowHandle, RawWindowHandle};
use winit::{
application::ApplicationHandler,
dpi::LogicalSize,
event::{ElementState, Touch, TouchPhase, WindowEvent},
event_loop::{ActiveEventLoop, EventLoop},
platform::pump_events::EventLoopExtPumpEvents,
event::{ButtonSource, ElementState, WindowEvent},
event_loop::{
pump_events::{EventLoopExtPumpEvents, PumpStatus},
ActiveEventLoop, EventLoop,
},
window::{Window as WinitWindow, WindowAttributes, WindowId},
};

Expand Down Expand Up @@ -66,8 +67,8 @@ where
crate::backend::SwapBuffersError: From<R::Error>,
{
init_from_attributes(
WinitWindow::default_attributes()
.with_inner_size(LogicalSize::new(1280.0, 800.0))
WindowAttributes::default()
.with_surface_size(LogicalSize::new(1280.0, 800.0))
.with_title("Smithay")
.with_visible(true),
)
Expand Down Expand Up @@ -110,17 +111,13 @@ where
let _guard = span.enter();
info!("Initializing a winit backend");

let event_loop = EventLoop::builder().build().map_err(Error::EventLoopCreation)?;
let mut event_loop = EventLoop::builder().build().map_err(Error::EventLoopCreation)?;

// TODO: Create window in `resumed`?
#[allow(deprecated)]
let window = Arc::new(
event_loop
.create_window(attributes)
.map_err(Error::WindowCreation)?,
let window = Arc::<dyn WinitWindow>::from(
InitApp::init(&mut event_loop, attributes).map_err(Error::WindowCreation)?,
);

span.record("window", Into::<u64>::into(window.id()));
span.record("window", window.id().into_raw());
debug!("Window created");

let (display, context, surface, is_x11) = {
Expand All @@ -135,7 +132,7 @@ where
let (surface, is_x11) = match window.window_handle().map(|handle| handle.as_raw()) {
Ok(RawWindowHandle::Wayland(handle)) => {
debug!("Winit backend: Wayland");
let size = window.inner_size();
let size = window.surface_size();
let surface = unsafe {
wegl::WlEglSurface::new_from_raw(
handle.surface.as_ptr() as *mut _,
Expand Down Expand Up @@ -221,7 +218,7 @@ pub enum Error {
EventLoopCreation(#[from] winit::error::EventLoopError),
/// Failed to initialize a window.
#[error("Failed to initialize a window")]
WindowCreation(#[from] winit::error::OsError),
WindowCreation(#[from] winit::error::RequestError),
#[error("Failed to create a surface for the window")]
/// Surface creation error.
Surface(Box<dyn std::error::Error>),
Expand All @@ -243,7 +240,7 @@ pub struct WinitGraphicsBackend<R> {
// The display isn't used past this point but must be kept alive.
_display: EGLDisplay,
egl_surface: EGLSurface,
window: Arc<WinitWindow>,
window: Arc<dyn WinitWindow>,
damage_tracking: bool,
bind_size: Option<Size<i32, Physical>>,
span: tracing::Span,
Expand All @@ -256,7 +253,7 @@ where
{
/// Window size of the underlying window
pub fn window_size(&self) -> Size<i32, Physical> {
let (w, h): (i32, i32) = self.window.inner_size().into();
let (w, h): (i32, i32) = self.window.surface_size().into();
(w, h).into()
}

Expand All @@ -266,8 +263,8 @@ where
}

/// Reference to the underlying window
pub fn window(&self) -> &WinitWindow {
&self.window
pub fn window(&self) -> &dyn WinitWindow {
&*self.window
}

/// Access the underlying renderer
Expand Down Expand Up @@ -355,7 +352,7 @@ where

#[derive(Debug)]
struct WinitEventLoopInner {
window: Arc<WinitWindow>,
window: Arc<dyn WinitWindow>,
clock: Clock<Monotonic>,
key_counter: u32,
is_x11: bool,
Expand All @@ -372,7 +369,7 @@ pub struct WinitEventLoop {
inner: WinitEventLoopInner,
fake_token: Option<Token>,
pending_events: Vec<WinitEvent>,
event_loop: Generic<EventLoop<()>>,
event_loop: Generic<EventLoop>,
span: tracing::Span,
}

Expand Down Expand Up @@ -419,15 +416,17 @@ impl<F: FnMut(WinitEvent)> WinitEventLoopApp<'_, F> {
}

impl<F: FnMut(WinitEvent)> ApplicationHandler for WinitEventLoopApp<'_, F> {
fn resumed(&mut self, _event_loop: &ActiveEventLoop) {
fn can_create_surfaces(&mut self, _event_loop: &dyn ActiveEventLoop) {}

fn resumed(&mut self, _event_loop: &dyn ActiveEventLoop) {
(self.callback)(WinitEvent::Input(InputEvent::DeviceAdded {
device: WinitVirtualDevice,
}));
}

fn window_event(&mut self, _event_loop: &ActiveEventLoop, _window_id: WindowId, event: WindowEvent) {
fn window_event(&mut self, _event_loop: &dyn ActiveEventLoop, _window_id: WindowId, event: WindowEvent) {
match event {
WindowEvent::Resized(size) => {
WindowEvent::SurfaceResized(size) => {
trace!("Resizing window to {size:?}");
let (w, h): (i32, i32) = size.into();

Expand All @@ -442,7 +441,7 @@ impl<F: FnMut(WinitEvent)> ApplicationHandler for WinitEventLoopApp<'_, F> {
} => {
trace!("Scale factor changed to {new_scale_factor}");
self.inner.scale_factor = new_scale_factor;
let (w, h): (i32, i32) = self.inner.window.inner_size().into();
let (w, h): (i32, i32) = self.inner.window.surface_size().into();
(self.callback)(WinitEvent::Resized {
size: (w, h).into(),
scale_factor: self.inner.scale_factor,
Expand Down Expand Up @@ -478,8 +477,8 @@ impl<F: FnMut(WinitEvent)> ApplicationHandler for WinitEventLoopApp<'_, F> {
};
(self.callback)(WinitEvent::Input(event));
}
WindowEvent::CursorMoved { position, .. } => {
let size = self.inner.window.inner_size();
WindowEvent::PointerMoved { position, .. } => {
let size = self.inner.window.surface_size();
let x = position.x / size.width as f64;
let y = position.y / size.height as f64;
let event = InputEvent::PointerMotionAbsolute {
Expand All @@ -500,24 +499,31 @@ impl<F: FnMut(WinitEvent)> ApplicationHandler for WinitEventLoopApp<'_, F> {
};
(self.callback)(WinitEvent::Input(event));
}
WindowEvent::MouseInput { state, button, .. } => {
let event = InputEvent::PointerButton {
event: WinitMouseInputEvent {
time: self.timestamp(),
button,
state,
is_x11: self.inner.is_x11,
},
};
(self.callback)(WinitEvent::Input(event));
WindowEvent::PointerButton { state, button, .. } => {
match button {
ButtonSource::Mouse(button) => {
let event = InputEvent::PointerButton {
event: WinitMouseInputEvent {
time: self.timestamp(),
button,
state,
is_x11: self.inner.is_x11,
},
};
(self.callback)(WinitEvent::Input(event));
}
// TODO: touch
_ => {}
}
}
/*
WindowEvent::Touch(Touch {
phase: TouchPhase::Started,
location,
id,
..
}) => {
let size = self.inner.window.inner_size();
let size = self.inner.window.surface_size();
let x = location.x / size.width as f64;
let y = location.y / size.width as f64;
let event = InputEvent::TouchDown {
Expand All @@ -537,7 +543,7 @@ impl<F: FnMut(WinitEvent)> ApplicationHandler for WinitEventLoopApp<'_, F> {
id,
..
}) => {
let size = self.inner.window.inner_size();
let size = self.inner.window.surface_size();
let x = location.x / size.width as f64;
let y = location.y / size.width as f64;
let event = InputEvent::TouchMotion {
Expand All @@ -558,7 +564,7 @@ impl<F: FnMut(WinitEvent)> ApplicationHandler for WinitEventLoopApp<'_, F> {
id,
..
}) => {
let size = self.inner.window.inner_size();
let size = self.inner.window.surface_size();
let x = location.x / size.width as f64;
let y = location.y / size.width as f64;
let event = InputEvent::TouchMotion {
Expand Down Expand Up @@ -594,15 +600,16 @@ impl<F: FnMut(WinitEvent)> ApplicationHandler for WinitEventLoopApp<'_, F> {
};
(self.callback)(WinitEvent::Input(event));
}
WindowEvent::DroppedFile(_)
*/
WindowEvent::DragDropped { .. }
| WindowEvent::Destroyed
| WindowEvent::CursorEntered { .. }
| WindowEvent::AxisMotion { .. }
| WindowEvent::CursorLeft { .. }
| WindowEvent::PointerEntered { .. }
| WindowEvent::PointerLeft { .. }
| WindowEvent::ModifiersChanged(_)
| WindowEvent::KeyboardInput { .. }
| WindowEvent::HoveredFile(_)
| WindowEvent::HoveredFileCancelled
| WindowEvent::DragEntered { .. }
| WindowEvent::DragLeft { .. }
| WindowEvent::DragMoved { .. }
| WindowEvent::Ime(_)
| WindowEvent::Moved(_)
| WindowEvent::Occluded(_)
Expand All @@ -617,6 +624,38 @@ impl<F: FnMut(WinitEvent)> ApplicationHandler for WinitEventLoopApp<'_, F> {
}
}

struct InitApp {
attributes: WindowAttributes,
window: Option<Result<Box<dyn WinitWindow>, winit::error::RequestError>>,
}

impl InitApp {
fn init(
event_loop: &mut EventLoop,
attributes: WindowAttributes,
) -> Result<Box<dyn WinitWindow>, winit::error::RequestError> {
let mut app = Self {
attributes,
window: None,
};
while app.window.is_none() {
event_loop.pump_app_events(None, &mut app);
}
app.window.unwrap()
}
}

impl ApplicationHandler for InitApp {
fn can_create_surfaces(&mut self, event_loop: &dyn ActiveEventLoop) {
self.window = Some(event_loop.create_window(self.attributes.clone()));
}

fn window_event(&mut self, _event_loop: &dyn ActiveEventLoop, _window_id: WindowId, _event: WindowEvent) {
// TODO is event here needed, and not handled by main ApplicationHandler?
//unreachable!()
}
}

impl EventSource for WinitEventLoop {
type Event = WinitEvent;
type Metadata = ();
Expand Down
Loading