use crate::event::Cursor;
use crate::wire::{Arg, Interface, Shared, Wire};
use std::os::fd::AsRawFd;
use std::path::PathBuf;
const MAGIC: &[u8; 4] = b"Xcur";
const PICTURE: u32 = 0xfffd_0002;
const HEAD: usize = 36;
#[derive(Clone, Debug, PartialEq)]
pub(crate) struct Picture {
pub(crate) size: [i32; 2],
pub(crate) hot: [i32; 2],
pub(crate) pixels: Vec<u32>,
}
fn names(cursor: Cursor) -> &'static [&'static str] {
match cursor {
Cursor::Arrow => &["default", "left_ptr"],
Cursor::Hand => &["pointer", "hand2", "hand1"],
Cursor::Text => &["text", "xterm"],
Cursor::ResizeX => &["ew-resize", "col-resize", "sb_h_double_arrow"],
Cursor::ResizeY => &["ns-resize", "row-resize", "sb_v_double_arrow"],
Cursor::ResizeDown => &["nwse-resize", "bd_double_arrow", "size_fdiag"],
Cursor::ResizeUp => &["nesw-resize", "fd_double_arrow", "size_bdiag"],
Cursor::Move => &["move", "fleur", "all-scroll"],
Cursor::No => &["not-allowed", "crossed_circle", "forbidden"],
}
}
pub(crate) fn pick(file: &[u8], size: u32) -> Option<Picture> {
let word = |at: usize| file.get(at..at + 4).and_then(|word| word.first_chunk()).map(|word| u32::from_le_bytes(*word));
if file.get(..4) != Some(MAGIC) {
return None;
}
let (head, count) = (word(4)? as usize, word(12)? as usize);
let listed = (0..count.min(4096)).filter_map(|nth| Some((word(head + nth * 12)?, word(head + nth * 12 + 4)?, word(head + nth * 12 + 8)? as usize)));
let (_, _, at) = listed.filter(|(kind, ..)| *kind == PICTURE).min_by_key(|(_, nominal, _)| nominal.abs_diff(size))?;
let [wide, high, x, y] = [at + 16, at + 20, at + 24, at + 28].map(|at| word(at).unwrap_or(0));
if word(at)? as usize != HEAD || wide == 0 || high == 0 || wide > 1024 || high > 1024 {
return None;
}
let pixels: Option<Vec<u32>> = (0..(wide * high) as usize).map(|nth| word(at + HEAD + nth * 4)).collect();
Some(Picture { size: [wide as i32, high as i32], hot: [x as i32, y as i32], pixels: pixels? })
}
fn places() -> Vec<PathBuf> {
let home = std::env::var_os("HOME").map(PathBuf::from).unwrap_or_default();
let data = std::env::var_os("XDG_DATA_HOME").map_or(home.join(".local/share"), PathBuf::from);
match std::env::var_os("XCURSOR_PATH") {
Some(said) => std::env::split_paths(&said).collect(),
None => vec![data.join("icons"), home.join(".icons"), "/usr/share/icons".into(), "/usr/share/pixmaps".into()],
}
}
pub(crate) fn file_of(theme: &str, cursor: Cursor, places: &[PathBuf]) -> Option<PathBuf> {
let (mut themes, mut at) = (vec![theme.to_string()], 0);
while let Some(theme) = themes.get(at).cloned() {
at += 1;
let folders = places.iter().map(|place| place.join(&theme).join("cursors"));
let found = folders.flat_map(|folder| names(cursor).iter().map(move |name| folder.join(name))).find(|file| file.is_file());
if found.is_some() {
return found;
}
let said = places.iter().find_map(|place| std::fs::read_to_string(place.join(&theme).join("index.theme")).ok()).unwrap_or_default();
let builds_on = said.lines().find_map(|line| line.trim().strip_prefix("Inherits")?.trim_start().strip_prefix('=')).unwrap_or_default();
for other in builds_on.split([',', ';']).map(str::trim).filter(|other| !other.is_empty()) {
if !themes.iter().any(|known| known == other) && themes.len() < 16 {
themes.push(other.into());
}
}
}
None
}
pub(crate) struct Pointers {
surface: u32,
theme: String,
size: u32,
places: Vec<PathBuf>,
read: Vec<(Cursor, i32, Option<Picture>)>,
buffers: Vec<(u32, u32, Shared)>,
}
impl Pointers {
pub(crate) fn new(wire: &Wire, compositor: u32) -> Self {
let surface = wire.make(Interface::Surface);
wire.send(compositor, 0, &[Arg::Uint(surface)]);
let theme = std::env::var("XCURSOR_THEME").ok().filter(|theme| !theme.is_empty()).unwrap_or_else(|| "default".into());
let size = std::env::var("XCURSOR_SIZE").ok().and_then(|size| size.parse().ok()).filter(|size| *size > 0).unwrap_or(24);
Pointers { surface, theme, size, places: places(), read: Vec::new(), buffers: Vec::new() }
}
fn picture(&mut self, cursor: Cursor, scale: i32) -> Option<Picture> {
if let Some((.., kept)) = self.read.iter().find(|(shape, at, _)| (*shape, *at) == (cursor, scale)) {
return kept.clone();
}
let file = file_of(&self.theme, cursor, &self.places).and_then(|file| std::fs::read(file).ok());
let picked = file.and_then(|file| pick(&file, self.size * scale as u32));
self.read.push((cursor, scale, picked.clone()));
picked
}
pub(crate) fn show(&mut self, wire: &Wire, shm: u32, pointer: u32, serial: u32, cursor: Cursor, scale: f32) {
let whole = (scale.ceil() as i32).max(1);
let Some(picture) = self.picture(cursor, whole).or_else(|| self.picture(Cursor::Arrow, whole)) else { return };
let len = picture.pixels.len() * 4;
let Some(mut memory) = Shared::new(len) else { return };
memory.pixels().copy_from_slice(&picture.pixels);
let (pool, buffer) = (wire.make(Interface::Pool), wire.make(Interface::Buffer));
wire.send(shm, 0, &[Arg::Uint(pool), Arg::File(memory.file.as_raw_fd()), Arg::Int(len as i32)]);
let [wide, high] = picture.size;
wire.send(pool, 0, &[Arg::Uint(buffer), Arg::Int(0), Arg::Int(wide), Arg::Int(high), Arg::Int(wide * 4), Arg::Uint(0)]);
wire.send(self.surface, 1, &[Arg::Object(buffer), Arg::Int(0), Arg::Int(0)]);
wire.send(self.surface, 8, &[Arg::Int(whole)]);
wire.send(self.surface, 2, &[Arg::Int(0), Arg::Int(0), Arg::Int(wide), Arg::Int(high)]);
wire.send(self.surface, 6, &[]);
let [x, y] = picture.hot.map(|at| at / whole);
wire.send(pointer, 0, &[Arg::Uint(serial), Arg::Object(self.surface), Arg::Int(x), Arg::Int(y)]);
self.buffers.push((buffer, pool, memory));
}
pub(crate) fn released(&mut self, wire: &Wire, id: u32) {
let last = self.buffers.last().map(|(buffer, ..)| *buffer);
if let Some(at) = self.buffers.iter().position(|(buffer, ..)| *buffer == id && Some(id) != last) {
let (buffer, pool, _) = self.buffers.remove(at);
wire.send(buffer, 0, &[]);
wire.send(pool, 1, &[]);
}
}
}
#[cfg(test)]
mod tests {
use super::*;
fn file(sizes: &[(u32, u32)]) -> Vec<u8> {
let mut file = Vec::new();
let words = |file: &mut Vec<u8>, words: &[u32]| words.iter().for_each(|word| file.extend_from_slice(&word.to_le_bytes()));
words(&mut file, &[u32::from_le_bytes(*MAGIC), 16, 0x1_0000, sizes.len() as u32 + 1]);
let mut at = 16 + 12 * (sizes.len() + 1);
words(&mut file, &[0xfffe_0001, 1, 0]); for (size, _) in sizes {
words(&mut file, &[PICTURE, *size, at as u32]);
at += HEAD + (size * size * 4) as usize;
}
for (size, fill) in sizes {
words(&mut file, &[HEAD as u32, PICTURE, *size, 1, *size, *size, size / 2, size / 2, 0]);
words(&mut file, &vec![*fill; (size * size) as usize]);
}
file
}
#[test]
fn the_picture_nearest_the_size_asked_for_is_taken() {
let file = file(&[(24, 0xff00_0000), (32, 0xffff_ffff), (48, 0x8080_8080)]);
let picked = pick(&file, 30).unwrap();
assert_eq!((picked.size, picked.hot, picked.pixels[0], picked.pixels.len()), ([32, 32], [16, 16], 0xffff_ffff, 32 * 32));
assert_eq!(pick(&file, 48).unwrap().pixels[5], 0x8080_8080);
assert_eq!(pick(&file, 1).unwrap().size, [24, 24]);
assert!(pick(&file[..file.len() - 4], 48).is_none(), "a picture cut short is none");
assert!(pick(b"not a pointer", 24).is_none());
}
#[test]
fn a_shape_is_found_in_the_theme_or_the_ones_it_builds_on() {
let folder = std::env::temp_dir().join(format!("taut-pointers-{}", std::process::id()));
let (own, base) = (folder.join("own"), folder.join("base"));
std::fs::create_dir_all(own.join("cursors")).unwrap();
std::fs::create_dir_all(base.join("cursors")).unwrap();
std::fs::write(own.join("index.theme"), "[Icon Theme]\nName=Own\nInherits = missing, base\n").unwrap();
std::fs::write(own.join("cursors/left_ptr"), file(&[(24, 1)])).unwrap();
std::fs::write(base.join("cursors/xterm"), file(&[(24, 2)])).unwrap();
let places = [folder.clone()];
assert_eq!(file_of("own", Cursor::Arrow, &places), Some(own.join("cursors/left_ptr")), "by its older name");
assert_eq!(file_of("own", Cursor::Text, &places), Some(base.join("cursors/xterm")), "from what the theme builds on");
assert_eq!(file_of("own", Cursor::Hand, &places), None);
std::fs::remove_dir_all(folder).unwrap();
}
#[test]
fn the_desktops_own_theme_is_read() {
let found = ["Adwaita", "breeze_cursors", "default"].into_iter().find_map(|theme| file_of(theme, Cursor::Arrow, &places()));
let Some(found) = found else { return eprintln!("no theme of pointers here: not tested") };
let picture = pick(&std::fs::read(&found).unwrap(), 24).expect("the theme's arrow is read");
assert!(picture.size[0] >= 16 && picture.pixels.iter().any(|pixel| pixel >> 24 == 0xff), "{found:?}: a picture with something solid in it");
}
}