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

Wayland #1498

Closed
wants to merge 3 commits into from
Closed

Wayland #1498

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
12 changes: 12 additions & 0 deletions druid-shell/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ gtk = ["gio", "gdk", "gdk-sys", "glib", "glib-sys", "gtk-sys", "gtk-rs", "gdk-pi
x11 = ["x11rb", "nix", "cairo-sys-rs"]
# Implement HasRawWindowHandle for WindowHandle
raw-win-handle = ["raw-window-handle"]
# **WARNING** not ready for the prime time. Many things don't work yet.
wayland = ["wayland-client", "wayland-protocols/client", "wayland-protocols/unstable_protocols",
"nix", "cairo-sys-rs", "rand", "xkbcommon-sys", "calloop", "wayland-cursor", "log", "im"]

# passing on all the image features. AVIF is not supported because it does not
# support decoding, and that's all we use `Image` for.
Expand Down Expand Up @@ -89,6 +92,15 @@ glib-sys = { version = "0.10.0", optional = true }
gtk-sys = { version = "0.10.0", optional = true }
nix = { version = "0.18.0", optional = true }
x11rb = { version = "0.8.0", features = ["allow-unsafe-code", "present", "randr", "xfixes"], optional = true }
# todo use dlopen eventually to gracefully fallback to X11
wayland-client = { version = "0.28.2", optional = true, features = ["dlopen"] }
wayland-protocols = { version = "0.28.2", optional = true }
wayland-cursor = { version = "0.28.3", optional = true }
rand = { version = "0.8.0", optional = true }
xkbcommon-sys = { version = "0.7.4", optional = true }
calloop = { version = "0.7.1", optional = true }
log = { version = "0.4.14", optional = true }
im = { version = "15.0.0", optional = true }

[target.'cfg(target_arch="wasm32")'.dependencies]
wasm-bindgen = "0.2.67"
Expand Down
136 changes: 136 additions & 0 deletions druid-shell/examples/empty_window.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
// Copyright 2018 The Druid Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

/// An example that is as simple as possible (just throw up an empty window).
use std::any::Any;

use druid_shell::kurbo::{Point, Rect, Size};
use druid_shell::piet::{Color, FixedLinearGradient, GradientStop, RenderContext};

use druid_shell::{
Application, Cursor, FileDialogToken, FileInfo, KeyEvent, MouseEvent, Region, TimerToken,
WinHandler, WindowBuilder, WindowHandle,
};

#[derive(Default)]
struct HelloState {
size: Size,
handle: Option<WindowHandle>,
}

impl WinHandler for HelloState {
fn connect(&mut self, handle: &WindowHandle) {
self.handle = Some(handle.clone());
}

fn prepare_paint(&mut self) {
self.handle.as_mut().unwrap().invalidate();
}

fn paint(&mut self, piet: &mut piet_common::Piet, _: &Region) {
// draw a gradient so we can see what's going on.
let brush = piet
.gradient(FixedLinearGradient {
start: Point::ZERO,
end: self.size.to_vec2().to_point(),
stops: vec![
GradientStop {
pos: 0.0,
color: Color::RED,
},
GradientStop {
pos: 1.0,
color: Color::BLUE,
},
],
})
.unwrap();
piet.fill(Rect::ZERO.with_size(self.size), &brush);
}

fn command(&mut self, id: u32) {
println!("command id {}", id);
}

fn open_file(&mut self, _token: FileDialogToken, file_info: Option<FileInfo>) {
println!("open file result: {:?}", file_info);
}

fn key_down(&mut self, event: KeyEvent) -> bool {
println!("keydown: {:?}", event);
false
}

fn key_up(&mut self, event: KeyEvent) {
println!("keyup: {:?}", event);
}

fn wheel(&mut self, event: &MouseEvent) {
println!("mouse_wheel {:?}", event);
}

fn mouse_move(&mut self, event: &MouseEvent) {
self.handle.as_mut().unwrap().set_cursor(&Cursor::Arrow);
println!("mouse_move {:?}", event);
}

fn mouse_down(&mut self, event: &MouseEvent) {
println!("mouse_down {:?}", event);
}

fn mouse_up(&mut self, event: &MouseEvent) {
println!("mouse_up {:?}", event);
}

fn timer(&mut self, id: TimerToken) {
println!("timer fired: {:?}", id);
}

fn size(&mut self, size: Size) {
self.size = size;
}

fn got_focus(&mut self) {
println!("Got focus");
}

fn lost_focus(&mut self) {
println!("Lost focus");
}

fn request_close(&mut self) {
self.handle.as_ref().unwrap().close();
}

fn destroy(&mut self) {
Application::global().quit()
}

fn as_any(&mut self) -> &mut dyn Any {
self
}
}

fn main() {
simple_logger::SimpleLogger::new().init().unwrap();
let app = Application::new().unwrap();
let mut builder = WindowBuilder::new(app.clone());
builder.set_handler(Box::new(HelloState::default()));
builder.set_title("Hello example");

let window = builder.build().unwrap();
window.show();

app.run(None);
}
5 changes: 4 additions & 1 deletion druid-shell/src/common_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ const MULTI_CLICK_MAX_DISTANCE: f64 = 5.0;
/// Strip the access keys from the menu string.
///
/// Changes "E&xit" to "Exit". Actual ampersands are escaped as "&&".
#[cfg(any(target_os = "macos", all(target_os = "linux", feature = "gtk")))]
#[cfg(any(
target_os = "macos",
all(target_os = "linux", any(feature = "gtk", feature = "wayland"))
))]
pub fn strip_access_key(raw_menu_text: &str) -> String {
let mut saw_ampersand = false;
let mut result = String::new();
Expand Down
8 changes: 8 additions & 0 deletions druid-shell/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ use crate::platform::error as platform;
pub enum Error {
/// The Application instance has already been created.
ApplicationAlreadyExists,
/// Tried to use the application after it had been dropped.
ApplicationDropped,
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The behavior ok the wayland backend is to only hold weak handles to the application in the windows, so that the application might be dropped and then the window used. Not sure if this is correct.

/// The window has already been destroyed.
WindowDropped,
/// Platform specific error.
Expand All @@ -38,6 +40,12 @@ impl fmt::Display for Error {
Error::ApplicationAlreadyExists => {
write!(f, "An application instance has already been created.")
}
Error::ApplicationDropped => {
write!(
f,
"The application this operation requires has been dropped."
)
}
Error::Platform(err) => fmt::Display::fmt(err, f),
Error::WindowDropped => write!(f, "The window has already been destroyed."),
Error::Other(s) => write!(f, "{}", s),
Expand Down
13 changes: 10 additions & 3 deletions druid-shell/src/platform/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,18 @@ pub use x11::*;
#[cfg(all(feature = "x11", target_os = "linux"))]
pub(crate) mod shared;

#[cfg(all(not(feature = "x11"), target_os = "linux"))]
#[cfg(all(feature = "wayland", target_os = "linux"))]
mod wayland;
#[cfg(all(feature = "wayland", target_os = "linux"))]
pub use wayland::*;
#[cfg(all(feature = "wayland", target_os = "linux"))]
pub(crate) mod shared;

#[cfg(all(not(feature = "x11"), not(feature = "wayland"), target_os = "linux"))]
mod gtk;
#[cfg(all(not(feature = "x11"), target_os = "linux"))]
#[cfg(all(not(feature = "x11"), not(feature = "wayland"), target_os = "linux"))]
pub use self::gtk::*;
#[cfg(all(not(feature = "x11"), target_os = "linux"))]
#[cfg(all(not(feature = "x11"), not(feature = "wayland"), target_os = "linux"))]
pub(crate) mod shared;

#[cfg(target_arch = "wasm32")]
Expand Down
5 changes: 4 additions & 1 deletion druid-shell/src/platform/shared/keyboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@
#[allow(unused)]
use keyboard_types::{Code, Location};

#[cfg(any(all(feature = "x11", target_os = "linux"), target_os = "macos"))]
#[cfg(any(
all(any(feature = "x11", feature = "wayland"), target_os = "linux"),
target_os = "macos"
))]
/// Map key code to location.
///
/// The logic for this is adapted from InitKeyEvent in TextInputHandler (in the Mozilla
Expand Down
6 changes: 6 additions & 0 deletions druid-shell/src/platform/shared/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,9 @@ cfg_if::cfg_if! {
pub use keyboard::*;
}
}
cfg_if::cfg_if! {
if #[cfg(any(feature = "x11", feature = "wayland"))] {
mod timer;
pub use timer::*;
}
}
44 changes: 44 additions & 0 deletions druid-shell/src/platform/shared/timer.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
use crate::TimerToken;
use std::{cmp::Ordering, time::Instant};

/// A timer is a deadline (`std::Time::Instant`) and a `TimerToken`.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) struct Timer<T> {
deadline: Instant,
token: TimerToken,
pub data: T,
}

impl<T> Timer<T> {
pub(crate) fn new(deadline: Instant, data: T) -> Self {
let token = TimerToken::next();
Self {
deadline,
token,
data,
}
}

pub(crate) fn deadline(&self) -> Instant {
self.deadline
}

pub(crate) fn token(&self) -> TimerToken {
self.token
}
}

impl<T: Eq + PartialEq> Ord for Timer<T> {
/// Ordering is so that earliest deadline sorts first
// "Earliest deadline first" that a std::collections::BinaryHeap will have the earliest timer
// at its head, which is just what is needed for timer management.
fn cmp(&self, other: &Self) -> Ordering {
self.deadline.cmp(&other.deadline).reverse()
}
}

impl<T: Eq + PartialEq> PartialOrd for Timer<T> {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
Loading