From 4c6fca876b20656b7ebc3206ed1c63978e50d981 Mon Sep 17 00:00:00 2001 From: Lucas Timmins Date: Sun, 27 Jan 2019 22:33:25 +0800 Subject: [PATCH 1/2] Add wayland specific code to examples for windows to appear --- Cargo.toml | 2 +- examples/cursor.rs | 3 +++ examples/cursor_grab.rs | 3 +++ examples/fullscreen.rs | 3 +++ examples/handling_close.rs | 5 ++++- examples/helpers/mod.rs | 45 ++++++++++++++++++++++++++++++++++++++ examples/min_max_size.rs | 3 +++ examples/monitor_list.rs | 3 +++ examples/multiwindow.rs | 3 +++ examples/proxy.rs | 5 ++++- examples/resizable.rs | 3 +++ examples/transparent.rs | 3 +++ examples/window.rs | 5 ++++- examples/window_icon.rs | 3 +++ 14 files changed, 85 insertions(+), 4 deletions(-) create mode 100644 examples/helpers/mod.rs diff --git a/Cargo.toml b/Cargo.toml index a5a882eb57..73ef8fdadb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -63,7 +63,7 @@ features = [ [target.'cfg(any(target_os = "linux", target_os = "dragonfly", target_os = "freebsd", target_os = "openbsd", target_os = "netbsd"))'.dependencies] wayland-client = { version = "0.21", features = [ "dlopen", "egl", "cursor"] } -smithay-client-toolkit = "0.4.3" +smithay-client-toolkit = { git = "https://github.com/smithay/client-toolkit" } x11-dl = "2.18.3" parking_lot = "0.7" percent-encoding = "1.0" diff --git a/examples/cursor.rs b/examples/cursor.rs index d2df71a7af..9866899b92 100644 --- a/examples/cursor.rs +++ b/examples/cursor.rs @@ -1,11 +1,14 @@ extern crate winit; +mod helpers; + use winit::{Event, ElementState, MouseCursor, WindowEvent, KeyboardInput, ControlFlow}; fn main() { let mut events_loop = winit::EventsLoop::new(); let window = winit::WindowBuilder::new().build(&events_loop).unwrap(); + helpers::init_wayland(&window); window.set_title("A fantastic window!"); let cursors = [MouseCursor::Default, MouseCursor::Crosshair, MouseCursor::Hand, MouseCursor::Arrow, MouseCursor::Move, MouseCursor::Text, MouseCursor::Wait, MouseCursor::Help, MouseCursor::Progress, MouseCursor::NotAllowed, MouseCursor::ContextMenu, MouseCursor::Cell, MouseCursor::VerticalText, MouseCursor::Alias, MouseCursor::Copy, MouseCursor::NoDrop, MouseCursor::Grab, MouseCursor::Grabbing, MouseCursor::AllScroll, MouseCursor::ZoomIn, MouseCursor::ZoomOut, MouseCursor::EResize, MouseCursor::NResize, MouseCursor::NeResize, MouseCursor::NwResize, MouseCursor::SResize, MouseCursor::SeResize, MouseCursor::SwResize, MouseCursor::WResize, MouseCursor::EwResize, MouseCursor::NsResize, MouseCursor::NeswResize, MouseCursor::NwseResize, MouseCursor::ColResize, MouseCursor::RowResize]; diff --git a/examples/cursor_grab.rs b/examples/cursor_grab.rs index 641a324ded..e97576a649 100644 --- a/examples/cursor_grab.rs +++ b/examples/cursor_grab.rs @@ -1,5 +1,7 @@ extern crate winit; +mod helpers; + fn main() { let mut events_loop = winit::EventsLoop::new(); @@ -7,6 +9,7 @@ fn main() { .with_title("Super Cursor Grab'n'Hide Simulator 9000") .build(&events_loop) .unwrap(); + helpers::init_wayland(&window); events_loop.run_forever(|event| { if let winit::Event::WindowEvent { event, .. } = event { diff --git a/examples/fullscreen.rs b/examples/fullscreen.rs index c29013c41a..b5c5cd6b80 100644 --- a/examples/fullscreen.rs +++ b/examples/fullscreen.rs @@ -1,5 +1,7 @@ extern crate winit; +mod helpers; + use std::io::{self, Write}; use winit::{ControlFlow, Event, WindowEvent}; @@ -45,6 +47,7 @@ fn main() { .with_fullscreen(monitor) .build(&events_loop) .unwrap(); + helpers::init_wayland(&window); events_loop.run_forever(|event| { println!("{:?}", event); diff --git a/examples/handling_close.rs b/examples/handling_close.rs index 101f45cf32..cc7f8f0bde 100644 --- a/examples/handling_close.rs +++ b/examples/handling_close.rs @@ -1,12 +1,15 @@ extern crate winit; +mod helpers; + fn main() { let mut events_loop = winit::EventsLoop::new(); - let _window = winit::WindowBuilder::new() + let window = winit::WindowBuilder::new() .with_title("Your faithful window") .build(&events_loop) .unwrap(); + helpers::init_wayland(&window); let mut close_requested = false; diff --git a/examples/helpers/mod.rs b/examples/helpers/mod.rs new file mode 100644 index 0000000000..32684b35f9 --- /dev/null +++ b/examples/helpers/mod.rs @@ -0,0 +1,45 @@ +#[cfg(any(target_os = "linux", target_os = "dragonfly", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd"))] +extern crate smithay_client_toolkit as sctk; + +#[cfg(any(target_os = "linux", target_os = "dragonfly", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd"))] +use winit::os::unix::WindowExt; + +// Wayland requires the commiting of a surface to display a window +pub fn init_wayland(window: &winit::Window) { + #[cfg(any(target_os = "linux", target_os = "dragonfly", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd"))] + { + if let Some(winit_display) = window.get_wayland_display() { + if let Some(surface) = window.get_wayland_surface() { + use self::sctk::reexports::client::protocol::wl_shm; + use self::sctk::reexports::client::protocol::wl_surface::RequestsTrait as SurfaceRequests; + use self::sctk::reexports::client::{Display, Proxy}; + use self::sctk::utils::DoubleMemPool; + use self::sctk::wayland_client::sys::client::wl_display; + use self::sctk::Environment; + + let (width, height): (u32, u32) = window.get_inner_size().unwrap().into(); + let (display, mut event_queue) = + unsafe { Display::from_external_display(winit_display as *mut wl_display) }; + let env = Environment::from_display(&*display, &mut event_queue).unwrap(); + let mut pools = + DoubleMemPool::new(&env.shm, || {}).expect("Failed to create a memory pool !"); + let surface = unsafe { Proxy::from_c_ptr(surface as *mut _) }; + + if let Some(pool) = pools.pool() { + pool.resize(4 * (width * height) as usize) + .expect("Failed to resize the memory pool."); + let new_buffer = pool.buffer( + 0, + width as i32, + height as i32, + 4 * width as i32, + wl_shm::Format::Argb8888, + ); + surface.attach(Some(&new_buffer), 0, 0); + surface.commit(); + event_queue.sync_roundtrip().unwrap(); + } + } + } + } +} diff --git a/examples/min_max_size.rs b/examples/min_max_size.rs index 06f4aa18d2..37237360d3 100644 --- a/examples/min_max_size.rs +++ b/examples/min_max_size.rs @@ -1,5 +1,7 @@ extern crate winit; +mod helpers; + use winit::dpi::LogicalSize; fn main() { @@ -8,6 +10,7 @@ fn main() { let window = winit::WindowBuilder::new() .build(&events_loop) .unwrap(); + helpers::init_wayland(&window); window.set_min_dimensions(Some(LogicalSize::new(400.0, 200.0))); window.set_max_dimensions(Some(LogicalSize::new(800.0, 400.0))); diff --git a/examples/monitor_list.rs b/examples/monitor_list.rs index e40ac30918..c093c752b1 100644 --- a/examples/monitor_list.rs +++ b/examples/monitor_list.rs @@ -1,7 +1,10 @@ extern crate winit; +mod helpers; + fn main() { let event_loop = winit::EventsLoop::new(); let window = winit::WindowBuilder::new().build(&event_loop).unwrap(); + helpers::init_wayland(&window); println!("{:#?}\nPrimary: {:#?}", window.get_available_monitors(), window.get_primary_monitor()); } diff --git a/examples/multiwindow.rs b/examples/multiwindow.rs index b558aa231a..4e1687c442 100644 --- a/examples/multiwindow.rs +++ b/examples/multiwindow.rs @@ -1,5 +1,7 @@ extern crate winit; +mod helpers; + use std::collections::HashMap; fn main() { @@ -8,6 +10,7 @@ fn main() { let mut windows = HashMap::new(); for _ in 0..3 { let window = winit::Window::new(&events_loop).unwrap(); + helpers::init_wayland(&window); windows.insert(window.id(), window); } diff --git a/examples/proxy.rs b/examples/proxy.rs index a975181503..8f9754ed67 100644 --- a/examples/proxy.rs +++ b/examples/proxy.rs @@ -1,12 +1,15 @@ extern crate winit; +mod helpers; + fn main() { let mut events_loop = winit::EventsLoop::new(); - let _window = winit::WindowBuilder::new() + let window = winit::WindowBuilder::new() .with_title("A fantastic window!") .build(&events_loop) .unwrap(); + helpers::init_wayland(&window); let proxy = events_loop.create_proxy(); diff --git a/examples/resizable.rs b/examples/resizable.rs index 749e852121..725ad4d03a 100644 --- a/examples/resizable.rs +++ b/examples/resizable.rs @@ -1,5 +1,7 @@ extern crate winit; +mod helpers; + fn main() { let mut events_loop = winit::EventsLoop::new(); @@ -11,6 +13,7 @@ fn main() { .with_resizable(resizable) .build(&events_loop) .unwrap(); + helpers::init_wayland(&window); events_loop.run_forever(|event| { match event { diff --git a/examples/transparent.rs b/examples/transparent.rs index a558350f3e..e59c5717e8 100644 --- a/examples/transparent.rs +++ b/examples/transparent.rs @@ -1,11 +1,14 @@ extern crate winit; +mod helpers; + fn main() { let mut events_loop = winit::EventsLoop::new(); let window = winit::WindowBuilder::new().with_decorations(false) .with_transparency(true) .build(&events_loop).unwrap(); + helpers::init_wayland(&window); window.set_title("A fantastic window!"); diff --git a/examples/window.rs b/examples/window.rs index fcc7d9ea33..f42258a9e8 100644 --- a/examples/window.rs +++ b/examples/window.rs @@ -1,12 +1,15 @@ extern crate winit; +mod helpers; + fn main() { let mut events_loop = winit::EventsLoop::new(); - let _window = winit::WindowBuilder::new() + let window = winit::WindowBuilder::new() .with_title("A fantastic window!") .build(&events_loop) .unwrap(); + helpers::init_wayland(&window); events_loop.run_forever(|event| { println!("{:?}", event); diff --git a/examples/window_icon.rs b/examples/window_icon.rs index 5be1433e01..96c9a298e2 100644 --- a/examples/window_icon.rs +++ b/examples/window_icon.rs @@ -6,6 +6,8 @@ extern crate winit; #[cfg(feature = "icon_loading")] extern crate image; +mod helpers; + #[cfg(feature = "icon_loading")] fn main() { use winit::Icon; @@ -29,6 +31,7 @@ fn main() { .with_window_icon(Some(icon)) .build(&events_loop) .unwrap(); + helpers::init_wayland(&window); events_loop.run_forever(|event| { if let winit::Event::WindowEvent { event, .. } = event { From 636b91a4749b24e999183ab4fcbd07b69109a885 Mon Sep 17 00:00:00 2001 From: Lucas Timmins Date: Sun, 27 Jan 2019 22:38:49 +0800 Subject: [PATCH 2/2] Add comments --- examples/cursor.rs | 3 +++ examples/cursor_grab.rs | 2 ++ examples/fullscreen.rs | 2 ++ examples/handling_close.rs | 2 ++ examples/min_max_size.rs | 2 ++ examples/monitor_list.rs | 1 + examples/multiwindow.rs | 1 + examples/proxy.rs | 2 ++ examples/resizable.rs | 2 ++ examples/transparent.rs | 2 ++ examples/window.rs | 2 ++ examples/window_icon.rs | 2 ++ 12 files changed, 23 insertions(+) diff --git a/examples/cursor.rs b/examples/cursor.rs index 9866899b92..f7fc38b6b4 100644 --- a/examples/cursor.rs +++ b/examples/cursor.rs @@ -8,7 +8,10 @@ fn main() { let mut events_loop = winit::EventsLoop::new(); let window = winit::WindowBuilder::new().build(&events_loop).unwrap(); + + // Wayland requires the commiting of a surface to display a window helpers::init_wayland(&window); + window.set_title("A fantastic window!"); let cursors = [MouseCursor::Default, MouseCursor::Crosshair, MouseCursor::Hand, MouseCursor::Arrow, MouseCursor::Move, MouseCursor::Text, MouseCursor::Wait, MouseCursor::Help, MouseCursor::Progress, MouseCursor::NotAllowed, MouseCursor::ContextMenu, MouseCursor::Cell, MouseCursor::VerticalText, MouseCursor::Alias, MouseCursor::Copy, MouseCursor::NoDrop, MouseCursor::Grab, MouseCursor::Grabbing, MouseCursor::AllScroll, MouseCursor::ZoomIn, MouseCursor::ZoomOut, MouseCursor::EResize, MouseCursor::NResize, MouseCursor::NeResize, MouseCursor::NwResize, MouseCursor::SResize, MouseCursor::SeResize, MouseCursor::SwResize, MouseCursor::WResize, MouseCursor::EwResize, MouseCursor::NsResize, MouseCursor::NeswResize, MouseCursor::NwseResize, MouseCursor::ColResize, MouseCursor::RowResize]; diff --git a/examples/cursor_grab.rs b/examples/cursor_grab.rs index e97576a649..eb384d073f 100644 --- a/examples/cursor_grab.rs +++ b/examples/cursor_grab.rs @@ -9,6 +9,8 @@ fn main() { .with_title("Super Cursor Grab'n'Hide Simulator 9000") .build(&events_loop) .unwrap(); + + // Wayland requires the commiting of a surface to display a window helpers::init_wayland(&window); events_loop.run_forever(|event| { diff --git a/examples/fullscreen.rs b/examples/fullscreen.rs index b5c5cd6b80..5086ae232a 100644 --- a/examples/fullscreen.rs +++ b/examples/fullscreen.rs @@ -47,6 +47,8 @@ fn main() { .with_fullscreen(monitor) .build(&events_loop) .unwrap(); + + // Wayland requires the commiting of a surface to display a window helpers::init_wayland(&window); events_loop.run_forever(|event| { diff --git a/examples/handling_close.rs b/examples/handling_close.rs index cc7f8f0bde..75952346f2 100644 --- a/examples/handling_close.rs +++ b/examples/handling_close.rs @@ -9,6 +9,8 @@ fn main() { .with_title("Your faithful window") .build(&events_loop) .unwrap(); + + // Wayland requires the commiting of a surface to display a window helpers::init_wayland(&window); let mut close_requested = false; diff --git a/examples/min_max_size.rs b/examples/min_max_size.rs index 37237360d3..f056925c33 100644 --- a/examples/min_max_size.rs +++ b/examples/min_max_size.rs @@ -10,6 +10,8 @@ fn main() { let window = winit::WindowBuilder::new() .build(&events_loop) .unwrap(); + + // Wayland requires the commiting of a surface to display a window helpers::init_wayland(&window); window.set_min_dimensions(Some(LogicalSize::new(400.0, 200.0))); diff --git a/examples/monitor_list.rs b/examples/monitor_list.rs index c093c752b1..802663816b 100644 --- a/examples/monitor_list.rs +++ b/examples/monitor_list.rs @@ -5,6 +5,7 @@ mod helpers; fn main() { let event_loop = winit::EventsLoop::new(); let window = winit::WindowBuilder::new().build(&event_loop).unwrap(); + // Wayland requires the commiting of a surface to display a window helpers::init_wayland(&window); println!("{:#?}\nPrimary: {:#?}", window.get_available_monitors(), window.get_primary_monitor()); } diff --git a/examples/multiwindow.rs b/examples/multiwindow.rs index 4e1687c442..f506cc8470 100644 --- a/examples/multiwindow.rs +++ b/examples/multiwindow.rs @@ -10,6 +10,7 @@ fn main() { let mut windows = HashMap::new(); for _ in 0..3 { let window = winit::Window::new(&events_loop).unwrap(); + // Wayland requires the commiting of a surface to display a window helpers::init_wayland(&window); windows.insert(window.id(), window); } diff --git a/examples/proxy.rs b/examples/proxy.rs index 8f9754ed67..a795c6f8bd 100644 --- a/examples/proxy.rs +++ b/examples/proxy.rs @@ -9,6 +9,8 @@ fn main() { .with_title("A fantastic window!") .build(&events_loop) .unwrap(); + + // Wayland requires the commiting of a surface to display a window helpers::init_wayland(&window); let proxy = events_loop.create_proxy(); diff --git a/examples/resizable.rs b/examples/resizable.rs index 725ad4d03a..b60d133400 100644 --- a/examples/resizable.rs +++ b/examples/resizable.rs @@ -13,6 +13,8 @@ fn main() { .with_resizable(resizable) .build(&events_loop) .unwrap(); + + // Wayland requires the commiting of a surface to display a window helpers::init_wayland(&window); events_loop.run_forever(|event| { diff --git a/examples/transparent.rs b/examples/transparent.rs index e59c5717e8..65b9eb1e68 100644 --- a/examples/transparent.rs +++ b/examples/transparent.rs @@ -8,6 +8,8 @@ fn main() { let window = winit::WindowBuilder::new().with_decorations(false) .with_transparency(true) .build(&events_loop).unwrap(); + + // Wayland requires the commiting of a surface to display a window helpers::init_wayland(&window); window.set_title("A fantastic window!"); diff --git a/examples/window.rs b/examples/window.rs index f42258a9e8..f67292043a 100644 --- a/examples/window.rs +++ b/examples/window.rs @@ -9,6 +9,8 @@ fn main() { .with_title("A fantastic window!") .build(&events_loop) .unwrap(); + + // Wayland requires the commiting of a surface to display a window helpers::init_wayland(&window); events_loop.run_forever(|event| { diff --git a/examples/window_icon.rs b/examples/window_icon.rs index 96c9a298e2..5a838248de 100644 --- a/examples/window_icon.rs +++ b/examples/window_icon.rs @@ -31,6 +31,8 @@ fn main() { .with_window_icon(Some(icon)) .build(&events_loop) .unwrap(); + + // Wayland requires the commiting of a surface to display a window helpers::init_wayland(&window); events_loop.run_forever(|event| {