forge
Explore
Log in

Don't you dare go Hollow

niri

Clone
★ 0

0 watching0 forksRustGPL-3.012.1 MiB0411fd8Ivan Molodetskikh3 years ago263 commits

cursor.rs114 lines, 3.5 KiBHistoryRawDownload
1use std::collections::HashMap;
2use std::env;
3use std::fs::File;
4use std::io::Read;
5
6use anyhow::{anyhow, Context};
7use smithay::backend::allocator::Fourcc;
8use smithay::backend::renderer::element::texture::TextureBuffer;
9use smithay::backend::renderer::gles::{GlesRenderer, GlesTexture};
10use smithay::utils::{Physical, Point, Transform};
11use xcursor::parser::{parse_xcursor, Image};
12use xcursor::CursorTheme;
13
14static FALLBACK_CURSOR_DATA: &[u8] = include_bytes!("../resources/cursor.rgba");
15
16pub struct Cursor {
17 images: Vec<Image>,
18 size: i32,
19 cache: HashMap<i32, (TextureBuffer<GlesTexture>, Point<i32, Physical>)>,
20}
21
22impl Cursor {
23 /// Load the said theme as well as set the `XCURSOR_THEME` and `XCURSOR_SIZE`
24 /// env variables.
25 pub fn load(theme: &str, size: u8) -> Self {
26 env::set_var("XCURSOR_THEME", theme);
27 env::set_var("XCURSOR_SIZE", size.to_string());
28
29 let images = match load_xcursor(theme) {
30 Ok(images) => images,
31 Err(err) => {
32 warn!("error loading xcursor default cursor: {err:?}");
33
34 vec![Image {
35 size: 32,
36 width: 64,
37 height: 64,
38 xhot: 1,
39 yhot: 1,
40 delay: 1,
41 pixels_rgba: Vec::from(FALLBACK_CURSOR_DATA),
42 pixels_argb: vec![],
43 }]
44 }
45 };
46
47 Self {
48 images,
49 size: size as i32,
50 cache: HashMap::new(),
51 }
52 }
53
54 pub fn get(
55 &mut self,
56 renderer: &mut GlesRenderer,
57 scale: i32,
58 ) -> (TextureBuffer<GlesTexture>, Point<i32, Physical>) {
59 self.cache
60 .entry(scale)
61 .or_insert_with_key(|scale| {
62 let _span = tracy_client::span!("create cursor texture");
63
64 let size = self.size * scale;
65
66 let nearest_image = self
67 .images
68 .iter()
69 .min_by_key(|image| (size - image.size as i32).abs())
70 .unwrap();
71 let frame = self
72 .images
73 .iter()
74 .find(move |image| {
75 image.width == nearest_image.width && image.height == nearest_image.height
76 })
77 .unwrap();
78
79 let texture = TextureBuffer::from_memory(
80 renderer,
81 &frame.pixels_rgba,
82 Fourcc::Abgr8888,
83 (frame.width as i32, frame.height as i32),
84 false,
85 *scale,
86 Transform::Normal,
87 None,
88 )
89 .unwrap();
90 (texture, (frame.xhot as i32, frame.yhot as i32).into())
91 })
92 .clone()
93 }
94
95 pub fn get_cached_hotspot(&self, scale: i32) -> Option<Point<i32, Physical>> {
96 self.cache.get(&scale).map(|(_, hotspot)| *hotspot)
97 }
98}
99
100fn load_xcursor(theme: &str) -> anyhow::Result<Vec<Image>> {
101 let _span = tracy_client::span!();
102
103 let theme = CursorTheme::load(theme);
104 let path = theme
105 .load_icon("default")
106 .ok_or_else(|| anyhow!("no default icon"))?;
107 let mut file = File::open(path).context("error opening cursor icon file")?;
108 let mut buf = vec![];
109 file.read_to_end(&mut buf)
110 .context("error reading cursor icon file")?;
111 let images = parse_xcursor(&buf).context("error parsing cursor icon file")?;
112
113 Ok(images)
114}