From 90d6e303cad98059c083f1b8a1f13ec6afaf96d6 Mon Sep 17 00:00:00 2001 From: Ivan Molodetskikh Date: Tue, 15 Aug 2023 17:17:42 +0400 Subject: [PATCH] Use xcursor default cursor, or fallback from Anvil --- Cargo.toml | 2 +- resources/cursor.rgba | Bin 0 -> 16384 bytes src/niri.rs | 34 +++++++++++++------- src/utils.rs | 71 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 94 insertions(+), 13 deletions(-) create mode 100644 resources/cursor.rgba diff --git a/Cargo.toml b/Cargo.toml index 8febe268..9d168dc4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,6 +16,7 @@ smithay-drm-extras = { version = "0.1.0", path = "../smithay/smithay-drm-extras" tracing = "0.1.37" tracing-subscriber = { version = "0.3.17", features = ["env-filter"] } tracy-client = { version = "0.15.2", default-features = false } +xcursor = "0.3.4" [dependencies.smithay] git = "https://github.com/Smithay/smithay" @@ -40,4 +41,3 @@ profile-with-tracy = ["profiling/profile-with-tracy", "tracy-client/default"] [profile.release] overflow-checks = true - diff --git a/resources/cursor.rgba b/resources/cursor.rgba new file mode 100644 index 0000000000000000000000000000000000000000..729c1cc466b85bb2b1e9923d4a0bb7db559df4be GIT binary patch literal 16384 zcmeI1Ur4i69LK-&*_^)WS~}En)(i2L1YYDIjKM2{387FI>nfDck?4pTqGcE6zmYO1 zya4ot~cdCX>kz0HCL*$3UN@ge=P+GRCr@P{<3w44|Yu2#3RNMNu+}qGW@?;Ku+S zba!``j0d4mi0jW}GK_K%4u?MjAUU1RlJJ1^GsZHLlankM43Y0hk&a8+Ge} zs()l;gzF~{>gwvq1BFb8Mx)aJEcNyEy7Zu!e)7O(v$5UXU9uo2%d!tZd2es8?mYNA z{p5ipNo;3lhu1c9*tyay}!hg|E9;~da@Sd1TrM|aVEH=6` zXfX%((eH3L*u=yH8y_F%Yiw+c&CbrUtE(%XgQKIPAE_2vT3U=+^Prf1>UCzbxsZ3G z(a3e?$uh=nY2|V`zUcd8GWner=YwMU2L=Y%lbt68{gn5M-P5Y0 zxd=do!C=r-FTAVY>-BQ&7Z(?2mzS5nUSD6cnwpw|2mSs1Oi>gTkH@LrtMlhS>7{o9 zb!iG!Ki&Itxg0w?JA2yR-tJypT%02_78Vu?`sqFpkH^{V?d@+fGcz4@C(@MIB9MN1 z|0AvGbov*I#qutIM*tq4pP$PoCnv1Axw+s$XJ;qRNIV|@!D_Wq?lt{Jl;3}Le0(go zwY9wu;AN^~05yYygYS`8pU=nj)44#sFc1jP*T1{n?zgm_fkYzVNu^Rh+wJyu05q#_ z;3kvFSXNf{QY;qxiNxCN_Cf}lnwqE&QeM^pRB8Eb#P9dN4&YUj$@D^gFYo5LdVYT1 zmCa@^x3;#p{*K2TH1qKqpkC2FL@X8y z`u+YdtXAu5YG$+|_%A%`+@jwe*le~pX|IWNI-PR4Twb%;{Fr*=opVr^l$V!N25Q&V z*1lX`Uj7<@6TmByB+nbGRL-, - pub pointer_buffer: SolidColorBuffer, + pub pointer_buffer: Option<(TextureBuffer, Point)>, pub cursor_image: CursorImageStatus, pub dnd_icon: Option, } @@ -152,8 +153,6 @@ impl Niri { }) .unwrap(); - let pointer_buffer = SolidColorBuffer::new((16, 16), [1., 0.8, 0., 1.]); - Self { start_time, event_loop, @@ -175,7 +174,7 @@ impl Niri { popups: PopupManager::default(), seat, - pointer_buffer, + pointer_buffer: None, cursor_image: CursorImageStatus::Default, dnd_icon: None, } @@ -334,6 +333,11 @@ impl Niri { let output_pos = self.global_space.output_geometry(output).unwrap().loc; let pointer_pos = self.seat.get_pointer().unwrap().current_location() - output_pos.to_f64(); + let (default_buffer, default_hotspot) = self + .pointer_buffer + .get_or_insert_with(|| load_default_cursor(renderer)); + let default_hotspot = default_hotspot.to_logical(1); + let hotspot = if let CursorImageStatus::Surface(surface) = &mut self.cursor_image { if surface.alive() { with_states(surface, |states| { @@ -347,17 +351,23 @@ impl Niri { }) } else { self.cursor_image = CursorImageStatus::Default; - (0, 0).into() + default_hotspot } } else { - (0, 0).into() + default_hotspot }; let pointer_pos = (pointer_pos - hotspot.to_f64()).to_physical_precise_round(1.); let mut pointer_elements = match &self.cursor_image { CursorImageStatus::Hidden => vec![], CursorImageStatus::Default => vec![OutputRenderElements::DefaultPointer( - SolidColorRenderElement::from_buffer(&self.pointer_buffer, pointer_pos, 1., 1.), + TextureRenderElement::from_texture_buffer( + pointer_pos.to_f64(), + default_buffer, + None, + None, + None, + ), )], CursorImageStatus::Surface(surface) => { render_elements_from_surface_tree(renderer, surface, pointer_pos, 1., 1.) @@ -469,7 +479,7 @@ render_elements! { pub OutputRenderElements where R: ImportAll; Monitor = MonitorRenderElement, Wayland = WaylandSurfaceRenderElement, - DefaultPointer = SolidColorRenderElement, + DefaultPointer = TextureRenderElement<::TextureId>, } #[derive(Default)] diff --git a/src/utils.rs b/src/utils.rs index 9a7d8092..7d895530 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -1,7 +1,78 @@ +use std::fs::File; +use std::io::Read; use std::time::Duration; +use anyhow::{anyhow, Context}; +use smithay::backend::allocator::Fourcc; +use smithay::backend::renderer::element::texture::TextureBuffer; +use smithay::backend::renderer::gles::{GlesRenderer, GlesTexture}; use smithay::reexports::nix::time::{clock_gettime, ClockId}; +use smithay::utils::{Physical, Point, Transform}; +use xcursor::parser::parse_xcursor; +use xcursor::CursorTheme; + +const CURSOR_SIZE: u32 = 24; +static FALLBACK_CURSOR_DATA: &[u8] = include_bytes!("../resources/cursor.rgba"); pub fn get_monotonic_time() -> Duration { Duration::from(clock_gettime(ClockId::CLOCK_MONOTONIC).unwrap()) } + +fn load_xcursor() -> anyhow::Result { + let theme = CursorTheme::load("default"); + let path = theme + .load_icon("default") + .ok_or_else(|| anyhow!("no default icon"))?; + let mut file = File::open(path).context("error opening cursor icon file")?; + let mut buf = vec![]; + file.read_to_end(&mut buf) + .context("error reading cursor icon file")?; + let images = parse_xcursor(&buf).context("error parsing cursor icon file")?; + + let nearest_image = images + .iter() + .min_by_key(|image| (CURSOR_SIZE as i32 - image.size as i32).abs()) + .unwrap(); + let frame = images + .iter() + .find(move |image| { + image.width == nearest_image.width && image.height == nearest_image.height + }) + .unwrap(); + Ok(frame.clone()) +} + +pub fn load_default_cursor( + renderer: &mut GlesRenderer, +) -> (TextureBuffer, Point) { + let frame = match load_xcursor() { + Ok(frame) => frame, + Err(err) => { + warn!("error loading xcursor default cursor: {err:?}"); + + xcursor::parser::Image { + size: 32, + width: 64, + height: 64, + xhot: 1, + yhot: 1, + delay: 1, + pixels_rgba: Vec::from(FALLBACK_CURSOR_DATA), + pixels_argb: vec![], + } + } + }; + + let texture = TextureBuffer::from_memory( + renderer, + &frame.pixels_rgba, + Fourcc::Abgr8888, + (frame.width as i32, frame.height as i32), + false, + 1, + Transform::Normal, + None, + ) + .unwrap(); + (texture, (frame.xhot as i32, frame.yhot as i32).into()) +}