Make default cursor respect output scale
Browse files at this commit8d443c2e841505dd9a09d50dbb24a2a5956ecbd7Parent: d39da3f4 changed files +118 -80
- src/cursor.rs +103 -0
- src/main.rs +1 -0
- src/niri.rs +11 -9
- src/utils.rs +3 -71
src/cursor.rsadded+103 -0
| @@ -0,0 +1,103 @@ | |||
| 1 | +use std::collections::HashMap; | ||
| 2 | +use std::fs::File; | ||
| 3 | +use std::io::Read; | ||
| 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}; | ||
| 9 | +use smithay::utils::{Physical, Point, Transform}; | ||
| 10 | +use xcursor::parser::{parse_xcursor, Image}; | ||
| 11 | +use xcursor::CursorTheme; | ||
| 12 | + | ||
| 13 | +const CURSOR_SIZE: i32 = 24; | ||
| 14 | +static FALLBACK_CURSOR_DATA: &[u8] = include_bytes!("../resources/cursor.rgba"); | ||
| 15 | + | ||
| 16 | +pub struct Cursor { | ||
| 17 | + images: Vec<Image>, | ||
| 18 | + cache: HashMap<i32, (TextureBuffer<GlesTexture>, Point<i32, Physical>)>, | ||
| 19 | +} | ||
| 20 | + | ||
| 21 | +impl Cursor { | ||
| 22 | + pub fn load() -> Self { | ||
| 23 | + let images = match load_xcursor() { | ||
| 24 | + Ok(images) => images, | ||
| 25 | + Err(err) => { | ||
| 26 | + warn!("error loading xcursor default cursor: {err:?}"); | ||
| 27 | + | ||
| 28 | + vec![Image { | ||
| 29 | + size: 32, | ||
| 30 | + width: 64, | ||
| 31 | + height: 64, | ||
| 32 | + xhot: 1, | ||
| 33 | + yhot: 1, | ||
| 34 | + delay: 1, | ||
| 35 | + pixels_rgba: Vec::from(FALLBACK_CURSOR_DATA), | ||
| 36 | + pixels_argb: vec![], | ||
| 37 | + }] | ||
| 38 | + } | ||
| 39 | + }; | ||
| 40 | + | ||
| 41 | + Self { | ||
| 42 | + images, | ||
| 43 | + cache: HashMap::new(), | ||
| 44 | + } | ||
| 45 | + } | ||
| 46 | + | ||
| 47 | + pub fn get( | ||
| 48 | + &mut self, | ||
| 49 | + renderer: &mut GlesRenderer, | ||
| 50 | + scale: i32, | ||
| 51 | + ) -> (TextureBuffer<GlesTexture>, Point<i32, Physical>) { | ||
| 52 | + self.cache | ||
| 53 | + .entry(scale) | ||
| 54 | + .or_insert_with_key(|scale| { | ||
| 55 | + let _span = tracy_client::span!("create cursor texture"); | ||
| 56 | + | ||
| 57 | + let size = CURSOR_SIZE * scale; | ||
| 58 | + | ||
| 59 | + let nearest_image = self | ||
| 60 | + .images | ||
| 61 | + .iter() | ||
| 62 | + .min_by_key(|image| (size - image.size as i32).abs()) | ||
| 63 | + .unwrap(); | ||
| 64 | + let frame = self | ||
| 65 | + .images | ||
| 66 | + .iter() | ||
| 67 | + .find(move |image| { | ||
| 68 | + image.width == nearest_image.width && image.height == nearest_image.height | ||
| 69 | + }) | ||
| 70 | + .unwrap(); | ||
| 71 | + | ||
| 72 | + let texture = TextureBuffer::from_memory( | ||
| 73 | + renderer, | ||
| 74 | + &frame.pixels_rgba, | ||
| 75 | + Fourcc::Abgr8888, | ||
| 76 | + (frame.width as i32, frame.height as i32), | ||
| 77 | + false, | ||
| 78 | + *scale, | ||
| 79 | + Transform::Normal, | ||
| 80 | + None, | ||
| 81 | + ) | ||
| 82 | + .unwrap(); | ||
| 83 | + (texture, (frame.xhot as i32, frame.yhot as i32).into()) | ||
| 84 | + }) | ||
| 85 | + .clone() | ||
| 86 | + } | ||
| 87 | +} | ||
| 88 | + | ||
| 89 | +fn load_xcursor() -> anyhow::Result<Vec<Image>> { | ||
| 90 | + let _span = tracy_client::span!(); | ||
| 91 | + | ||
| 92 | + let theme = CursorTheme::load("default"); | ||
| 93 | + let path = theme | ||
| 94 | + .load_icon("default") | ||
| 95 | + .ok_or_else(|| anyhow!("no default icon"))?; | ||
| 96 | + let mut file = File::open(path).context("error opening cursor icon file")?; | ||
| 97 | + let mut buf = vec![]; | ||
| 98 | + file.read_to_end(&mut buf) | ||
| 99 | + .context("error reading cursor icon file")?; | ||
| 100 | + let images = parse_xcursor(&buf).context("error parsing cursor icon file")?; | ||
| 101 | + | ||
| 102 | + Ok(images) | ||
| 103 | +} | ||
src/main.rsmodified+1 -0
| @@ -4,6 +4,7 @@ extern crate tracing; | |||
| 4 | mod animation; | 4 | mod animation; |
| 5 | mod backend; | 5 | mod backend; |
| 6 | mod config; | 6 | mod config; |
| 7 | +mod cursor; | ||
| 7 | mod dbus; | 8 | mod dbus; |
| 8 | mod frame_clock; | 9 | mod frame_clock; |
| 9 | mod handlers; | 10 | mod handlers; |
src/niri.rsmodified+11 -9
| @@ -15,7 +15,7 @@ use smithay::backend::allocator::Fourcc; | |||
| 15 | use smithay::backend::renderer::element::surface::{ | 15 | use smithay::backend::renderer::element::surface::{ |
| 16 | render_elements_from_surface_tree, WaylandSurfaceRenderElement, | 16 | render_elements_from_surface_tree, WaylandSurfaceRenderElement, |
| 17 | }; | 17 | }; |
| 18 | -use smithay::backend::renderer::element::texture::{TextureBuffer, TextureRenderElement}; | 18 | +use smithay::backend::renderer::element::texture::TextureRenderElement; |
| 19 | use smithay::backend::renderer::element::utils::{Relocate, RelocateRenderElement}; | 19 | use smithay::backend::renderer::element::utils::{Relocate, RelocateRenderElement}; |
| 20 | use smithay::backend::renderer::element::{ | 20 | use smithay::backend::renderer::element::{ |
| 21 | render_elements, AsRenderElements, Kind, RenderElement, RenderElementStates, | 21 | render_elements, AsRenderElements, Kind, RenderElement, RenderElementStates, |
| @@ -71,6 +71,7 @@ use zbus::fdo::RequestNameFlags; | |||
| 71 | 71 | ||
| 72 | use crate::backend::{Backend, Tty, Winit}; | 72 | use crate::backend::{Backend, Tty, Winit}; |
| 73 | use crate::config::Config; | 73 | use crate::config::Config; |
| 74 | +use crate::cursor::Cursor; | ||
| 74 | use crate::dbus::gnome_shell_screenshot::{self, NiriToScreenshot, ScreenshotToNiri}; | 75 | use crate::dbus::gnome_shell_screenshot::{self, NiriToScreenshot, ScreenshotToNiri}; |
| 75 | use crate::dbus::mutter_display_config::DisplayConfig; | 76 | use crate::dbus::mutter_display_config::DisplayConfig; |
| 76 | #[cfg(feature = "xdp-gnome-screencast")] | 77 | #[cfg(feature = "xdp-gnome-screencast")] |
| @@ -79,7 +80,7 @@ use crate::dbus::mutter_service_channel::ServiceChannel; | |||
| 79 | use crate::frame_clock::FrameClock; | 80 | use crate::frame_clock::FrameClock; |
| 80 | use crate::layout::{output_size, MonitorRenderElement, MonitorSet}; | 81 | use crate::layout::{output_size, MonitorRenderElement, MonitorSet}; |
| 81 | use crate::pw_utils::{Cast, PipeWire}; | 82 | use crate::pw_utils::{Cast, PipeWire}; |
| 82 | -use crate::utils::{center, get_monotonic_time, load_default_cursor, make_screenshot_path}; | 83 | +use crate::utils::{center, get_monotonic_time, make_screenshot_path}; |
| 83 | 84 | ||
| 84 | pub struct Niri { | 85 | pub struct Niri { |
| 85 | pub config: Rc<RefCell<Config>>, | 86 | pub config: Rc<RefCell<Config>>, |
| @@ -122,7 +123,7 @@ pub struct Niri { | |||
| 122 | 123 | ||
| 123 | pub seat: Seat<State>, | 124 | pub seat: Seat<State>, |
| 124 | 125 | ||
| 125 | - pub pointer_buffer: Option<(TextureBuffer<GlesTexture>, Point<i32, Physical>)>, | 126 | + pub default_cursor: Cursor, |
| 126 | pub cursor_image: CursorImageStatus, | 127 | pub cursor_image: CursorImageStatus, |
| 127 | pub dnd_icon: Option<WlSurface>, | 128 | pub dnd_icon: Option<WlSurface>, |
| 128 | 129 | ||
| @@ -328,6 +329,8 @@ impl Niri { | |||
| 328 | .unwrap(); | 329 | .unwrap(); |
| 329 | seat.add_pointer(); | 330 | seat.add_pointer(); |
| 330 | 331 | ||
| 332 | + let default_cursor = Cursor::load(); | ||
| 333 | + | ||
| 331 | let socket_source = ListeningSocketSource::new_auto().unwrap(); | 334 | let socket_source = ListeningSocketSource::new_auto().unwrap(); |
| 332 | let socket_name = socket_source.socket_name().to_os_string(); | 335 | let socket_name = socket_source.socket_name().to_os_string(); |
| 333 | event_loop | 336 | event_loop |
| @@ -675,7 +678,7 @@ impl Niri { | |||
| 675 | presentation_state, | 678 | presentation_state, |
| 676 | 679 | ||
| 677 | seat, | 680 | seat, |
| 678 | - pointer_buffer: None, | 681 | + default_cursor, |
| 679 | cursor_image: CursorImageStatus::Default, | 682 | cursor_image: CursorImageStatus::Default, |
| 680 | dnd_icon: None, | 683 | dnd_icon: None, |
| 681 | 684 | ||
| @@ -985,10 +988,9 @@ impl Niri { | |||
| 985 | let output_pos = self.global_space.output_geometry(output).unwrap().loc; | 988 | let output_pos = self.global_space.output_geometry(output).unwrap().loc; |
| 986 | let pointer_pos = self.seat.get_pointer().unwrap().current_location() - output_pos.to_f64(); | 989 | let pointer_pos = self.seat.get_pointer().unwrap().current_location() - output_pos.to_f64(); |
| 987 | 990 | ||
| 988 | - let (default_buffer, default_hotspot) = self | 991 | + let output_scale_int = output.current_scale().integer_scale(); |
| 989 | - .pointer_buffer | 992 | + let (default_buffer, default_hotspot) = self.default_cursor.get(renderer, output_scale_int); |
| 990 | - .get_or_insert_with(|| load_default_cursor(renderer)); | 993 | + let default_hotspot = default_hotspot.to_logical(output_scale_int); |
| 991 | - let default_hotspot = default_hotspot.to_logical(1); | ||
| 992 | 994 | ||
| 993 | let hotspot = if let CursorImageStatus::Surface(surface) = &mut self.cursor_image { | 995 | let hotspot = if let CursorImageStatus::Surface(surface) = &mut self.cursor_image { |
| 994 | if surface.alive() { | 996 | if surface.alive() { |
| @@ -1015,7 +1017,7 @@ impl Niri { | |||
| 1015 | CursorImageStatus::Default => vec![OutputRenderElements::DefaultPointer( | 1017 | CursorImageStatus::Default => vec![OutputRenderElements::DefaultPointer( |
| 1016 | TextureRenderElement::from_texture_buffer( | 1018 | TextureRenderElement::from_texture_buffer( |
| 1017 | pointer_pos.to_f64(), | 1019 | pointer_pos.to_f64(), |
| 1018 | - default_buffer, | 1020 | + &default_buffer, |
| 1019 | None, | 1021 | None, |
| 1020 | None, | 1022 | None, |
| 1021 | None, | 1023 | None, |
src/utils.rsmodified+3 -71
| @@ -1,24 +1,15 @@ | |||
| 1 | use std::ffi::OsStr; | 1 | use std::ffi::OsStr; |
| 2 | -use std::fs::File; | 2 | +use std::io; |
| 3 | -use std::io::{self, Read}; | ||
| 4 | use std::os::unix::process::CommandExt; | 3 | use std::os::unix::process::CommandExt; |
| 5 | use std::path::PathBuf; | 4 | use std::path::PathBuf; |
| 6 | use std::process::{Command, Stdio}; | 5 | use std::process::{Command, Stdio}; |
| 7 | use std::time::Duration; | 6 | use std::time::Duration; |
| 8 | 7 | ||
| 9 | -use anyhow::{anyhow, Context}; | 8 | +use anyhow::Context; |
| 10 | use directories::UserDirs; | 9 | use directories::UserDirs; |
| 11 | use nix::time::{clock_gettime, ClockId}; | 10 | use nix::time::{clock_gettime, ClockId}; |
| 12 | -use smithay::backend::allocator::Fourcc; | 11 | +use smithay::utils::{Logical, Point, Rectangle}; |
| 13 | -use smithay::backend::renderer::element::texture::TextureBuffer; | ||
| 14 | -use smithay::backend::renderer::gles::{GlesRenderer, GlesTexture}; | ||
| 15 | -use smithay::utils::{Logical, Physical, Point, Rectangle, Transform}; | ||
| 16 | use time::OffsetDateTime; | 12 | use time::OffsetDateTime; |
| 17 | -use xcursor::parser::parse_xcursor; | ||
| 18 | -use xcursor::CursorTheme; | ||
| 19 | - | ||
| 20 | -const CURSOR_SIZE: u32 = 24; | ||
| 21 | -static FALLBACK_CURSOR_DATA: &[u8] = include_bytes!("../resources/cursor.rgba"); | ||
| 22 | 13 | ||
| 23 | pub fn get_monotonic_time() -> Duration { | 14 | pub fn get_monotonic_time() -> Duration { |
| 24 | Duration::from(clock_gettime(ClockId::CLOCK_MONOTONIC).unwrap()) | 15 | Duration::from(clock_gettime(ClockId::CLOCK_MONOTONIC).unwrap()) |
| @@ -28,65 +19,6 @@ pub fn center(rect: Rectangle<i32, Logical>) -> Point<i32, Logical> { | |||
| 28 | rect.loc + rect.size.downscale(2).to_point() | 19 | rect.loc + rect.size.downscale(2).to_point() |
| 29 | } | 20 | } |
| 30 | 21 | ||
| 31 | -fn load_xcursor() -> anyhow::Result<xcursor::parser::Image> { | ||
| 32 | - let theme = CursorTheme::load("default"); | ||
| 33 | - let path = theme | ||
| 34 | - .load_icon("default") | ||
| 35 | - .ok_or_else(|| anyhow!("no default icon"))?; | ||
| 36 | - let mut file = File::open(path).context("error opening cursor icon file")?; | ||
| 37 | - let mut buf = vec![]; | ||
| 38 | - file.read_to_end(&mut buf) | ||
| 39 | - .context("error reading cursor icon file")?; | ||
| 40 | - let images = parse_xcursor(&buf).context("error parsing cursor icon file")?; | ||
| 41 | - | ||
| 42 | - let nearest_image = images | ||
| 43 | - .iter() | ||
| 44 | - .min_by_key(|image| (CURSOR_SIZE as i32 - image.size as i32).abs()) | ||
| 45 | - .unwrap(); | ||
| 46 | - let frame = images | ||
| 47 | - .iter() | ||
| 48 | - .find(move |image| { | ||
| 49 | - image.width == nearest_image.width && image.height == nearest_image.height | ||
| 50 | - }) | ||
| 51 | - .unwrap(); | ||
| 52 | - Ok(frame.clone()) | ||
| 53 | -} | ||
| 54 | - | ||
| 55 | -pub fn load_default_cursor( | ||
| 56 | - renderer: &mut GlesRenderer, | ||
| 57 | -) -> (TextureBuffer<GlesTexture>, Point<i32, Physical>) { | ||
| 58 | - let frame = match load_xcursor() { | ||
| 59 | - Ok(frame) => frame, | ||
| 60 | - Err(err) => { | ||
| 61 | - warn!("error loading xcursor default cursor: {err:?}"); | ||
| 62 | - | ||
| 63 | - xcursor::parser::Image { | ||
| 64 | - size: 32, | ||
| 65 | - width: 64, | ||
| 66 | - height: 64, | ||
| 67 | - xhot: 1, | ||
| 68 | - yhot: 1, | ||
| 69 | - delay: 1, | ||
| 70 | - pixels_rgba: Vec::from(FALLBACK_CURSOR_DATA), | ||
| 71 | - pixels_argb: vec![], | ||
| 72 | - } | ||
| 73 | - } | ||
| 74 | - }; | ||
| 75 | - | ||
| 76 | - let texture = TextureBuffer::from_memory( | ||
| 77 | - renderer, | ||
| 78 | - &frame.pixels_rgba, | ||
| 79 | - Fourcc::Abgr8888, | ||
| 80 | - (frame.width as i32, frame.height as i32), | ||
| 81 | - false, | ||
| 82 | - 1, | ||
| 83 | - Transform::Normal, | ||
| 84 | - None, | ||
| 85 | - ) | ||
| 86 | - .unwrap(); | ||
| 87 | - (texture, (frame.xhot as i32, frame.yhot as i32).into()) | ||
| 88 | -} | ||
| 89 | - | ||
| 90 | pub fn make_screenshot_path() -> anyhow::Result<PathBuf> { | 22 | pub fn make_screenshot_path() -> anyhow::Result<PathBuf> { |
| 91 | let dirs = UserDirs::new().context("error retrieving home directory")?; | 23 | let dirs = UserDirs::new().context("error retrieving home directory")?; |
| 92 | let mut path = dirs.picture_dir().map(|p| p.to_owned()).unwrap_or_else(|| { | 24 | let mut path = dirs.picture_dir().map(|p| p.to_owned()).unwrap_or_else(|| { |