Skip to content
This repository has been archived by the owner on Jul 15, 2024. It is now read-only.

Make a new wayland backend using smithay-client-toolkit #107

Merged
merged 15 commits into from
Jul 20, 2023
Merged
3 changes: 1 addition & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,7 @@ jobs:
uses: Swatinem/rust-cache@v2

- name: cargo clippy glazier
# run: cargo clippy --all-targets --features=wayland --no-default-features -- -D warnings
run: echo "Temporarily skipped"
run: cargo clippy --all-targets --features=wayland --no-default-features -- -D warnings

- name: cargo test glazier
run: cargo test --features wayland --no-default-features
Expand Down
31 changes: 15 additions & 16 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,15 @@ rustdoc-args = ["--cfg", "docsrs"]
default-target = "x86_64-pc-windows-msvc"

[features]
default = ["x11", "wayland"]
default = ["x11"]
x11 = ["ashpd", "bindgen", "futures", "nix", "pkg-config", "x11rb"]
wayland = [
"wayland-client",
"wayland-protocols/client",
"wayland-protocols/unstable_protocols",
"nix",
"rand",
"calloop",
"wayland-cursor",
"log",
"im",
"bindgen",
# Required for XKBCommon
"pkg-config",
"bindgen",
"nix",
"smithay-client-toolkit",
"wayland-backend",
]

# passing on all the image features. AVIF is not supported because it does not
Expand Down Expand Up @@ -119,6 +114,7 @@ ashpd = { version = "0.4", optional = true }
futures = { version = "0.3.24", optional = true, features = ["executor"] }

nix = { version = "0.25.0", optional = true }

x11rb = { version = "0.11.1", features = [
"allow-unsafe-code",
"present",
Expand All @@ -130,16 +126,19 @@ x11rb = { version = "0.11.1", features = [
"cursor",
"xinput",
], optional = true }
wayland-client = { version = "0.29.5", optional = true, features = [
"use_system_lib",
] }
wayland-protocols = { version = "0.29.5", optional = true }
wayland-cursor = { version = "0.29.5", optional = true }

rand = { version = "0.8.0", optional = true }
calloop = { version = "0.7.1", optional = true }
log = { version = "0.4.14", optional = true }
im = { version = "15.0.0", optional = true }

# Wayland dependencies
smithay-client-toolkit = { version = "0.17.0", optional = true }
# Needed for supporting RawWindowHandle
wayland-backend = { version = "0.1.0", default_features = false, features = [
"client_system",
], optional = true }

[target.'cfg(target_arch="wasm32")'.dependencies]
wasm-bindgen = "0.2.67"
js-sys = "0.3.44"
Expand Down
31 changes: 31 additions & 0 deletions examples/edit_text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,37 @@ impl WindowState {
None,
&rect,
);
if let Some(composition) = &doc.composition {
let composition_start = parley::layout::Cursor::from_position(
&doc.layout,
composition.start,
true,
)
.offset() as f64
+ TEXT_X;
let composition_end =
parley::layout::Cursor::from_position(&doc.layout, composition.end, true).offset()
as f64
+ TEXT_X;
let rect = Rect::from_points(
Point::new(
composition_start.min(composition_end - 1.0),
TEXT_Y + FONT_SIZE as f64 + 2.0,
),
Point::new(
composition_start.max(composition_end + 1.0),
TEXT_Y + FONT_SIZE as f64,
),
);
sb.fill(
Fill::NonZero,
Affine::IDENTITY,
&Brush::Solid(Color::rgba8(0, 0, 255, 100)),
None,
&rect,
);
}

sb.pop_layer();
}
}
Expand Down
4 changes: 4 additions & 0 deletions examples/shello.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use glazier::{
};
use parley::{FontContext, Layout};
use std::any::Any;
use tracing_subscriber::EnvFilter;
use vello::util::{RenderContext, RenderSurface};
use vello::Renderer;
use vello::{
Expand All @@ -21,6 +22,9 @@ const WIDTH: usize = 2048;
const HEIGHT: usize = 1536;

fn main() {
tracing_subscriber::fmt()
.with_env_filter(EnvFilter::from_default_env())
.init();
let app = Application::new().unwrap();
let window = glazier::WindowBuilder::new(app.clone())
.size((WIDTH as f64 / 2., HEIGHT as f64 / 2.).into())
Expand Down
6 changes: 4 additions & 2 deletions src/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ impl Application {
let backend_app = backend::Application::new()?;
let state = Rc::new(RefCell::new(State { running: false }));
let app = Application { backend_app, state };
GLOBAL_APP.with(|global_app: &RefCell<Option<Application>>| {
GLOBAL_APP.with(|global_app| {
*global_app.borrow_mut() = Some(app.clone());
});
Ok(app)
Expand Down Expand Up @@ -201,7 +201,7 @@ impl AppHandle {
where
F: FnOnce(Option<&mut dyn AppHandler>) + Send + 'static,
{
self.0.run_on_main(callback)
self.0.run_on_main(callback);
}
}

Expand All @@ -213,4 +213,6 @@ mod test {

sa::assert_not_impl_any!(Application: Send, Sync);
sa::assert_impl_all!(AppHandle: Send);
// TODO: sa::assert_not_impl_all!(AppHandle: Sync);
// and same for IdleHandle
}
2 changes: 1 addition & 1 deletion src/backend/linux/error.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::fmt;

#[derive(Debug, Clone)]
#[derive(Debug)]
pub enum Error {
#[cfg(feature = "wayland")]
Wayland(crate::backend::wayland::error::Error),
Expand Down
17 changes: 8 additions & 9 deletions src/backend/linux/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,15 +258,14 @@ impl From<x11::window::WindowHandle> for crate::WindowHandle {
}

impl WindowHandle {
#[cfg(feature = "wayland")]
/// Assume that this WindowHandle is from Wayland
pub(crate) fn unwrap_wayland(&self) -> &wayland::window::WindowHandle {
match self {
WindowHandle::Wayland(it) => it,
_ => unreachable!("Must use a wayland window handle"),
}
}

// #[cfg(feature = "wayland")]
// /// Assume that this WindowHandle is from Wayland
// pub(crate) fn unwrap_wayland(&self) -> &wayland::window::WindowHandle {
// match self {
// WindowHandle::Wayland(it) => it,
// _ => unreachable!("Must use a wayland window handle"),
// }
// }
jaredoconnell marked this conversation as resolved.
Show resolved Hide resolved
#[cfg(feature = "x11")]
/// Assume that this WindowHandle is from X11
pub(crate) fn unwrap_x11(&self) -> &x11::window::WindowHandle {
Expand Down
9 changes: 7 additions & 2 deletions src/backend/shared/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,14 @@ cfg_if::cfg_if! {
}
cfg_if::cfg_if! {
if #[cfg(all(any(target_os = "freebsd", target_os = "linux"), any(feature = "x11", feature = "wayland")))] {
mod timer;
pub(crate) use timer::*;
pub(crate) mod xkb;
pub(crate) mod linux;
}
}
cfg_if::cfg_if! {
if #[cfg(all(any(target_os = "freebsd", target_os = "linux"), any(feature = "x11")))] {
// TODO: This might also be used in Wayland, but we don't implement timers there yet
mod timer;
pub(crate) use timer::*;
}
}
7 changes: 7 additions & 0 deletions src/backend/shared/xkb/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,13 @@ impl Drop for Context {

pub struct Keymap(*mut xkb_keymap);

impl Keymap {
#[cfg(feature = "wayland")]
pub fn repeats(&mut self, key: u32) -> bool {
unsafe { xkb_keymap_key_repeats(self.0, key) == 1 }
}
}

impl Clone for Keymap {
fn clone(&self) -> Self {
Self(unsafe { xkb_keymap_ref(self.0) })
Expand Down
Loading