forge
Explore
Log in

Don't you dare go Hollow

niri

Clone
★ 0

0 watching0 forksRustGPL-3.012.1 MiB085cd14Ivan Molodetskikh3 years ago78 commits

utils.rs78 lines, 2.4 KiBHistoryRawDownload
1use std::fs::File;
2use std::io::Read;
3use std::time::Duration;
4
5use anyhow::{anyhow, Context};
6use smithay::backend::allocator::Fourcc;
7use smithay::backend::renderer::element::texture::TextureBuffer;
8use smithay::backend::renderer::gles::{GlesRenderer, GlesTexture};
9use smithay::reexports::nix::time::{clock_gettime, ClockId};
10use smithay::utils::{Physical, Point, Transform};
11use xcursor::parser::parse_xcursor;
12use xcursor::CursorTheme;
13
14const CURSOR_SIZE: u32 = 24;
15static FALLBACK_CURSOR_DATA: &[u8] = include_bytes!("../resources/cursor.rgba");
16
17pub fn get_monotonic_time() -> Duration {
18 Duration::from(clock_gettime(ClockId::CLOCK_MONOTONIC).unwrap())
19}
20
21fn 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
45pub 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}