Use xcursor default cursor, or fallback from Anvil

This commit is contained in:
Ivan Molodetskikh
2023-08-15 17:17:42 +04:00
parent 162c74c259
commit 90d6e303ca
4 changed files with 94 additions and 13 deletions
+1 -1
View File
@@ -16,6 +16,7 @@ smithay-drm-extras = { version = "0.1.0", path = "../smithay/smithay-drm-extras"
tracing = "0.1.37" tracing = "0.1.37"
tracing-subscriber = { version = "0.3.17", features = ["env-filter"] } tracing-subscriber = { version = "0.3.17", features = ["env-filter"] }
tracy-client = { version = "0.15.2", default-features = false } tracy-client = { version = "0.15.2", default-features = false }
xcursor = "0.3.4"
[dependencies.smithay] [dependencies.smithay]
git = "https://github.com/Smithay/smithay" git = "https://github.com/Smithay/smithay"
@@ -40,4 +41,3 @@ profile-with-tracy = ["profiling/profile-with-tracy", "tracy-client/default"]
[profile.release] [profile.release]
overflow-checks = true overflow-checks = true
Binary file not shown.
+22 -12
View File
@@ -3,13 +3,13 @@ use std::os::unix::io::AsRawFd;
use std::sync::{Arc, Mutex}; use std::sync::{Arc, Mutex};
use std::time::Duration; use std::time::Duration;
use smithay::backend::renderer::element::solid::{SolidColorBuffer, SolidColorRenderElement};
use smithay::backend::renderer::element::surface::{ use smithay::backend::renderer::element::surface::{
render_elements_from_surface_tree, WaylandSurfaceRenderElement, render_elements_from_surface_tree, WaylandSurfaceRenderElement,
}; };
use smithay::backend::renderer::element::texture::{TextureBuffer, TextureRenderElement};
use smithay::backend::renderer::element::{render_elements, AsRenderElements}; use smithay::backend::renderer::element::{render_elements, AsRenderElements};
use smithay::backend::renderer::gles::GlesRenderer; use smithay::backend::renderer::gles::{GlesRenderer, GlesTexture};
use smithay::backend::renderer::ImportAll; use smithay::backend::renderer::{ImportAll, Renderer};
use smithay::desktop::{ use smithay::desktop::{
layer_map_for_output, LayerSurface, PopupManager, Space, Window, WindowSurfaceType, layer_map_for_output, LayerSurface, PopupManager, Space, Window, WindowSurfaceType,
}; };
@@ -25,7 +25,7 @@ use smithay::reexports::wayland_server::backend::{
}; };
use smithay::reexports::wayland_server::protocol::wl_surface::WlSurface; use smithay::reexports::wayland_server::protocol::wl_surface::WlSurface;
use smithay::reexports::wayland_server::{Display, DisplayHandle}; use smithay::reexports::wayland_server::{Display, DisplayHandle};
use smithay::utils::{IsAlive, Logical, Point, Scale, SERIAL_COUNTER}; use smithay::utils::{IsAlive, Logical, Physical, Point, Scale, SERIAL_COUNTER};
use smithay::wayland::compositor::{with_states, CompositorClientState, CompositorState}; use smithay::wayland::compositor::{with_states, CompositorClientState, CompositorState};
use smithay::wayland::data_device::DataDeviceState; use smithay::wayland::data_device::DataDeviceState;
use smithay::wayland::output::OutputManagerState; use smithay::wayland::output::OutputManagerState;
@@ -37,6 +37,7 @@ use smithay::wayland::socket::ListeningSocketSource;
use crate::backend::Backend; use crate::backend::Backend;
use crate::frame_clock::FrameClock; use crate::frame_clock::FrameClock;
use crate::layout::{MonitorRenderElement, MonitorSet}; use crate::layout::{MonitorRenderElement, MonitorSet};
use crate::utils::load_default_cursor;
use crate::LoopData; use crate::LoopData;
pub struct Niri { pub struct Niri {
@@ -70,7 +71,7 @@ pub struct Niri {
pub seat: Seat<Self>, pub seat: Seat<Self>,
pub pointer_buffer: SolidColorBuffer, pub pointer_buffer: Option<(TextureBuffer<GlesTexture>, Point<i32, Physical>)>,
pub cursor_image: CursorImageStatus, pub cursor_image: CursorImageStatus,
pub dnd_icon: Option<WlSurface>, pub dnd_icon: Option<WlSurface>,
} }
@@ -152,8 +153,6 @@ impl Niri {
}) })
.unwrap(); .unwrap();
let pointer_buffer = SolidColorBuffer::new((16, 16), [1., 0.8, 0., 1.]);
Self { Self {
start_time, start_time,
event_loop, event_loop,
@@ -175,7 +174,7 @@ impl Niri {
popups: PopupManager::default(), popups: PopupManager::default(),
seat, seat,
pointer_buffer, pointer_buffer: None,
cursor_image: CursorImageStatus::Default, cursor_image: CursorImageStatus::Default,
dnd_icon: None, dnd_icon: None,
} }
@@ -334,6 +333,11 @@ impl Niri {
let output_pos = self.global_space.output_geometry(output).unwrap().loc; 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 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 { let hotspot = if let CursorImageStatus::Surface(surface) = &mut self.cursor_image {
if surface.alive() { if surface.alive() {
with_states(surface, |states| { with_states(surface, |states| {
@@ -347,17 +351,23 @@ impl Niri {
}) })
} else { } else {
self.cursor_image = CursorImageStatus::Default; self.cursor_image = CursorImageStatus::Default;
(0, 0).into() default_hotspot
} }
} else { } else {
(0, 0).into() default_hotspot
}; };
let pointer_pos = (pointer_pos - hotspot.to_f64()).to_physical_precise_round(1.); let pointer_pos = (pointer_pos - hotspot.to_f64()).to_physical_precise_round(1.);
let mut pointer_elements = match &self.cursor_image { let mut pointer_elements = match &self.cursor_image {
CursorImageStatus::Hidden => vec![], CursorImageStatus::Hidden => vec![],
CursorImageStatus::Default => vec![OutputRenderElements::DefaultPointer( 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) => { CursorImageStatus::Surface(surface) => {
render_elements_from_surface_tree(renderer, surface, pointer_pos, 1., 1.) render_elements_from_surface_tree(renderer, surface, pointer_pos, 1., 1.)
@@ -469,7 +479,7 @@ render_elements! {
pub OutputRenderElements<R> where R: ImportAll; pub OutputRenderElements<R> where R: ImportAll;
Monitor = MonitorRenderElement<R>, Monitor = MonitorRenderElement<R>,
Wayland = WaylandSurfaceRenderElement<R>, Wayland = WaylandSurfaceRenderElement<R>,
DefaultPointer = SolidColorRenderElement, DefaultPointer = TextureRenderElement<<R as Renderer>::TextureId>,
} }
#[derive(Default)] #[derive(Default)]
+71
View File
@@ -1,7 +1,78 @@
use std::fs::File;
use std::io::Read;
use std::time::Duration; 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::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 { pub fn get_monotonic_time() -> Duration {
Duration::from(clock_gettime(ClockId::CLOCK_MONOTONIC).unwrap()) Duration::from(clock_gettime(ClockId::CLOCK_MONOTONIC).unwrap())
} }
fn load_xcursor() -> anyhow::Result<xcursor::parser::Image> {
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<GlesTexture>, Point<i32, Physical>) {
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())
}