Use xcursor default cursor, or fallback from Anvil
Browse files at this commit90d6e303cad98059c083f1b8a1f13ec6afaf96d6Parent: 162c74c4 changed files +94 -13
- Cargo.toml +1 -1
- resources/cursor.rgba +0 -0
- src/niri.rs +22 -12
- src/utils.rs +71 -0
Cargo.tomlmodified+1 -1
| @@ -16,6 +16,7 @@ smithay-drm-extras = { version = "0.1.0", path = "../smithay/smithay-drm-extras" | ||
| 16 | 16 | tracing = "0.1.37" |
| 17 | 17 | tracing-subscriber = { version = "0.3.17", features = ["env-filter"] } |
| 18 | 18 | tracy-client = { version = "0.15.2", default-features = false } |
| 19 | +xcursor = "0.3.4" | |
| 19 | 20 | |
| 20 | 21 | [dependencies.smithay] |
| 21 | 22 | git = "https://github.com/Smithay/smithay" |
| @@ -40,4 +41,3 @@ profile-with-tracy = ["profiling/profile-with-tracy", "tracy-client/default"] | ||
| 40 | 41 | |
| 41 | 42 | [profile.release] |
| 42 | 43 | overflow-checks = true |
| 43 | - |
resources/cursor.rgbaadded+0 -0
Binary file changed
src/niri.rsmodified+22 -12
| @@ -3,13 +3,13 @@ use std::os::unix::io::AsRawFd; | ||
| 3 | 3 | use std::sync::{Arc, Mutex}; |
| 4 | 4 | use std::time::Duration; |
| 5 | 5 | |
| 6 | -use smithay::backend::renderer::element::solid::{SolidColorBuffer, SolidColorRenderElement}; | |
| 7 | 6 | use smithay::backend::renderer::element::surface::{ |
| 8 | 7 | render_elements_from_surface_tree, WaylandSurfaceRenderElement, |
| 9 | 8 | }; |
| 9 | +use smithay::backend::renderer::element::texture::{TextureBuffer, TextureRenderElement}; | |
| 10 | 10 | use smithay::backend::renderer::element::{render_elements, AsRenderElements}; |
| 11 | -use smithay::backend::renderer::gles::GlesRenderer; | |
| 12 | -use smithay::backend::renderer::ImportAll; | |
| 11 | +use smithay::backend::renderer::gles::{GlesRenderer, GlesTexture}; | |
| 12 | +use smithay::backend::renderer::{ImportAll, Renderer}; | |
| 13 | 13 | use smithay::desktop::{ |
| 14 | 14 | layer_map_for_output, LayerSurface, PopupManager, Space, Window, WindowSurfaceType, |
| 15 | 15 | }; |
| @@ -25,7 +25,7 @@ use smithay::reexports::wayland_server::backend::{ | ||
| 25 | 25 | }; |
| 26 | 26 | use smithay::reexports::wayland_server::protocol::wl_surface::WlSurface; |
| 27 | 27 | use smithay::reexports::wayland_server::{Display, DisplayHandle}; |
| 28 | -use smithay::utils::{IsAlive, Logical, Point, Scale, SERIAL_COUNTER}; | |
| 28 | +use smithay::utils::{IsAlive, Logical, Physical, Point, Scale, SERIAL_COUNTER}; | |
| 29 | 29 | use smithay::wayland::compositor::{with_states, CompositorClientState, CompositorState}; |
| 30 | 30 | use smithay::wayland::data_device::DataDeviceState; |
| 31 | 31 | use smithay::wayland::output::OutputManagerState; |
| @@ -37,6 +37,7 @@ use smithay::wayland::socket::ListeningSocketSource; | ||
| 37 | 37 | use crate::backend::Backend; |
| 38 | 38 | use crate::frame_clock::FrameClock; |
| 39 | 39 | use crate::layout::{MonitorRenderElement, MonitorSet}; |
| 40 | +use crate::utils::load_default_cursor; | |
| 40 | 41 | use crate::LoopData; |
| 41 | 42 | |
| 42 | 43 | pub struct Niri { |
| @@ -70,7 +71,7 @@ pub struct Niri { | ||
| 70 | 71 | |
| 71 | 72 | pub seat: Seat<Self>, |
| 72 | 73 | |
| 73 | - pub pointer_buffer: SolidColorBuffer, | |
| 74 | + pub pointer_buffer: Option<(TextureBuffer<GlesTexture>, Point<i32, Physical>)>, | |
| 74 | 75 | pub cursor_image: CursorImageStatus, |
| 75 | 76 | pub dnd_icon: Option<WlSurface>, |
| 76 | 77 | } |
| @@ -152,8 +153,6 @@ impl Niri { | ||
| 152 | 153 | }) |
| 153 | 154 | .unwrap(); |
| 154 | 155 | |
| 155 | - let pointer_buffer = SolidColorBuffer::new((16, 16), [1., 0.8, 0., 1.]); | |
| 156 | - | |
| 157 | 156 | Self { |
| 158 | 157 | start_time, |
| 159 | 158 | event_loop, |
| @@ -175,7 +174,7 @@ impl Niri { | ||
| 175 | 174 | popups: PopupManager::default(), |
| 176 | 175 | |
| 177 | 176 | seat, |
| 178 | - pointer_buffer, | |
| 177 | + pointer_buffer: None, | |
| 179 | 178 | cursor_image: CursorImageStatus::Default, |
| 180 | 179 | dnd_icon: None, |
| 181 | 180 | } |
| @@ -334,6 +333,11 @@ impl Niri { | ||
| 334 | 333 | let output_pos = self.global_space.output_geometry(output).unwrap().loc; |
| 335 | 334 | let pointer_pos = self.seat.get_pointer().unwrap().current_location() - output_pos.to_f64(); |
| 336 | 335 | |
| 336 | + let (default_buffer, default_hotspot) = self | |
| 337 | + .pointer_buffer | |
| 338 | + .get_or_insert_with(|| load_default_cursor(renderer)); | |
| 339 | + let default_hotspot = default_hotspot.to_logical(1); | |
| 340 | + | |
| 337 | 341 | let hotspot = if let CursorImageStatus::Surface(surface) = &mut self.cursor_image { |
| 338 | 342 | if surface.alive() { |
| 339 | 343 | with_states(surface, |states| { |
| @@ -347,17 +351,23 @@ impl Niri { | ||
| 347 | 351 | }) |
| 348 | 352 | } else { |
| 349 | 353 | self.cursor_image = CursorImageStatus::Default; |
| 350 | - (0, 0).into() | |
| 354 | + default_hotspot | |
| 351 | 355 | } |
| 352 | 356 | } else { |
| 353 | - (0, 0).into() | |
| 357 | + default_hotspot | |
| 354 | 358 | }; |
| 355 | 359 | let pointer_pos = (pointer_pos - hotspot.to_f64()).to_physical_precise_round(1.); |
| 356 | 360 | |
| 357 | 361 | let mut pointer_elements = match &self.cursor_image { |
| 358 | 362 | CursorImageStatus::Hidden => vec![], |
| 359 | 363 | CursorImageStatus::Default => vec![OutputRenderElements::DefaultPointer( |
| 360 | - SolidColorRenderElement::from_buffer(&self.pointer_buffer, pointer_pos, 1., 1.), | |
| 364 | + TextureRenderElement::from_texture_buffer( | |
| 365 | + pointer_pos.to_f64(), | |
| 366 | + default_buffer, | |
| 367 | + None, | |
| 368 | + None, | |
| 369 | + None, | |
| 370 | + ), | |
| 361 | 371 | )], |
| 362 | 372 | CursorImageStatus::Surface(surface) => { |
| 363 | 373 | render_elements_from_surface_tree(renderer, surface, pointer_pos, 1., 1.) |
| @@ -469,7 +479,7 @@ render_elements! { | ||
| 469 | 479 | pub OutputRenderElements<R> where R: ImportAll; |
| 470 | 480 | Monitor = MonitorRenderElement<R>, |
| 471 | 481 | Wayland = WaylandSurfaceRenderElement<R>, |
| 472 | - DefaultPointer = SolidColorRenderElement, | |
| 482 | + DefaultPointer = TextureRenderElement<<R as Renderer>::TextureId>, | |
| 473 | 483 | } |
| 474 | 484 | |
| 475 | 485 | #[derive(Default)] |
src/utils.rsmodified+71 -0
| @@ -1,7 +1,78 @@ | ||
| 1 | +use std::fs::File; | |
| 2 | +use std::io::Read; | |
| 1 | 3 | use std::time::Duration; |
| 2 | 4 | |
| 5 | +use anyhow::{anyhow, Context}; | |
| 6 | +use smithay::backend::allocator::Fourcc; | |
| 7 | +use smithay::backend::renderer::element::texture::TextureBuffer; | |
| 8 | +use smithay::backend::renderer::gles::{GlesRenderer, GlesTexture}; | |
| 3 | 9 | use smithay::reexports::nix::time::{clock_gettime, ClockId}; |
| 10 | +use smithay::utils::{Physical, Point, Transform}; | |
| 11 | +use xcursor::parser::parse_xcursor; | |
| 12 | +use xcursor::CursorTheme; | |
| 13 | + | |
| 14 | +const CURSOR_SIZE: u32 = 24; | |
| 15 | +static FALLBACK_CURSOR_DATA: &[u8] = include_bytes!("../resources/cursor.rgba"); | |
| 4 | 16 | |
| 5 | 17 | pub fn get_monotonic_time() -> Duration { |
| 6 | 18 | Duration::from(clock_gettime(ClockId::CLOCK_MONOTONIC).unwrap()) |
| 7 | 19 | } |
| 20 | + | |
| 21 | +fn load_xcursor() -> anyhow::Result<xcursor::parser::Image> { | |
| 22 | + let theme = CursorTheme::load("default"); | |
| 23 | + let path = theme | |
| 24 | + .load_icon("default") | |
| 25 | + .ok_or_else(|| anyhow!("no default icon"))?; | |
| 26 | + let mut file = File::open(path).context("error opening cursor icon file")?; | |
| 27 | + let mut buf = vec![]; | |
| 28 | + file.read_to_end(&mut buf) | |
| 29 | + .context("error reading cursor icon file")?; | |
| 30 | + let images = parse_xcursor(&buf).context("error parsing cursor icon file")?; | |
| 31 | + | |
| 32 | + let nearest_image = images | |
| 33 | + .iter() | |
| 34 | + .min_by_key(|image| (CURSOR_SIZE as i32 - image.size as i32).abs()) | |
| 35 | + .unwrap(); | |
| 36 | + let frame = images | |
| 37 | + .iter() | |
| 38 | + .find(move |image| { | |
| 39 | + image.width == nearest_image.width && image.height == nearest_image.height | |
| 40 | + }) | |
| 41 | + .unwrap(); | |
| 42 | + Ok(frame.clone()) | |
| 43 | +} | |
| 44 | + | |
| 45 | +pub fn load_default_cursor( | |
| 46 | + renderer: &mut GlesRenderer, | |
| 47 | +) -> (TextureBuffer<GlesTexture>, Point<i32, Physical>) { | |
| 48 | + let frame = match load_xcursor() { | |
| 49 | + Ok(frame) => frame, | |
| 50 | + Err(err) => { | |
| 51 | + warn!("error loading xcursor default cursor: {err:?}"); | |
| 52 | + | |
| 53 | + xcursor::parser::Image { | |
| 54 | + size: 32, | |
| 55 | + width: 64, | |
| 56 | + height: 64, | |
| 57 | + xhot: 1, | |
| 58 | + yhot: 1, | |
| 59 | + delay: 1, | |
| 60 | + pixels_rgba: Vec::from(FALLBACK_CURSOR_DATA), | |
| 61 | + pixels_argb: vec![], | |
| 62 | + } | |
| 63 | + } | |
| 64 | + }; | |
| 65 | + | |
| 66 | + let texture = TextureBuffer::from_memory( | |
| 67 | + renderer, | |
| 68 | + &frame.pixels_rgba, | |
| 69 | + Fourcc::Abgr8888, | |
| 70 | + (frame.width as i32, frame.height as i32), | |
| 71 | + false, | |
| 72 | + 1, | |
| 73 | + Transform::Normal, | |
| 74 | + None, | |
| 75 | + ) | |
| 76 | + .unwrap(); | |
| 77 | + (texture, (frame.xhot as i32, frame.yhot as i32).into()) | |
| 78 | +} |