Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add wayland specific code to examples for windows to appear #780

Closed
Closed
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 @@ 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" }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is the smithay-client-toolkit dependency changed to refer to the git version instead of the crates.io version? I'm pretty sure will prevent us from pushing this update to crates.io.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This requires changes from SCTK that are not yet released, though given how things are coming, next version of SCTK will be a breaking change (upgrade to wayland-client 0.22).

I plan to make the upgrade as part of the event loop 2.0 refactor, so I'd say this PR should wait until the eventloop 2.0 story is done.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh wait, that was... written in original PR comment. Whoops. I guess I should read everything before making these sorts of comments 😅.

x11-dl = "2.18.3"
parking_lot = "0.7"
percent-encoding = "1.0"
6 changes: 6 additions & 0 deletions examples/cursor.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
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();

// 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];
Expand Down
5 changes: 5 additions & 0 deletions examples/cursor_grab.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
extern crate winit;

mod helpers;

fn main() {
let mut events_loop = winit::EventsLoop::new();

Expand All @@ -8,6 +10,9 @@ fn main() {
.build(&events_loop)
.unwrap();

// Wayland requires the commiting of a surface to display a window
helpers::init_wayland(&window);

events_loop.run_forever(|event| {
if let winit::Event::WindowEvent { event, .. } = event {
use winit::WindowEvent::*;
Expand Down
5 changes: 5 additions & 0 deletions examples/fullscreen.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
extern crate winit;

mod helpers;

use std::io::{self, Write};
use winit::{ControlFlow, Event, WindowEvent};

Expand Down Expand Up @@ -46,6 +48,9 @@ fn main() {
.build(&events_loop)
.unwrap();

// Wayland requires the commiting of a surface to display a window
helpers::init_wayland(&window);

events_loop.run_forever(|event| {
println!("{:?}", event);

Expand Down
7 changes: 6 additions & 1 deletion examples/handling_close.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
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();

// Wayland requires the commiting of a surface to display a window
helpers::init_wayland(&window);

let mut close_requested = false;

events_loop.run_forever(|event| {
Expand Down
45 changes: 45 additions & 0 deletions examples/helpers/mod.rs
Original file line number Diff line number Diff line change
@@ -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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is emitting unused_variables warnings on non-linux platforms. Could you add a let _ = window; line somewhere to suppress that warning?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For clarity, I'd actually rather move the #[cfg(...)] in the call site in the examples, so that people can clearly see that this code is for some platform-specific stuff.

#[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();
}
}
}
}
}
5 changes: 5 additions & 0 deletions examples/min_max_size.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
extern crate winit;

mod helpers;

use winit::dpi::LogicalSize;

fn main() {
Expand All @@ -9,6 +11,9 @@ fn main() {
.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)));
window.set_max_dimensions(Some(LogicalSize::new(800.0, 400.0)));

Expand Down
4 changes: 4 additions & 0 deletions examples/monitor_list.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
extern crate winit;

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());
}
4 changes: 4 additions & 0 deletions examples/multiwindow.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
extern crate winit;

mod helpers;

use std::collections::HashMap;

fn main() {
Expand All @@ -8,6 +10,8 @@ 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);
}

Expand Down
7 changes: 6 additions & 1 deletion examples/proxy.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
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();

// Wayland requires the commiting of a surface to display a window
helpers::init_wayland(&window);

let proxy = events_loop.create_proxy();

std::thread::spawn(move || {
Expand Down
5 changes: 5 additions & 0 deletions examples/resizable.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
extern crate winit;

mod helpers;

fn main() {
let mut events_loop = winit::EventsLoop::new();

Expand All @@ -12,6 +14,9 @@ fn main() {
.build(&events_loop)
.unwrap();

// Wayland requires the commiting of a surface to display a window
helpers::init_wayland(&window);

events_loop.run_forever(|event| {
match event {
winit::Event::WindowEvent { event, .. } => match event {
Expand Down
5 changes: 5 additions & 0 deletions examples/transparent.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
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();

// Wayland requires the commiting of a surface to display a window
helpers::init_wayland(&window);

window.set_title("A fantastic window!");

events_loop.run_forever(|event| {
Expand Down
7 changes: 6 additions & 1 deletion examples/window.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
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();

// Wayland requires the commiting of a surface to display a window
helpers::init_wayland(&window);

events_loop.run_forever(|event| {
println!("{:?}", event);

Expand Down
5 changes: 5 additions & 0 deletions examples/window_icon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -30,6 +32,9 @@ fn main() {
.build(&events_loop)
.unwrap();

// Wayland requires the commiting of a surface to display a window
helpers::init_wayland(&window);

events_loop.run_forever(|event| {
if let winit::Event::WindowEvent { event, .. } = event {
use winit::WindowEvent::*;
Expand Down