forge
Explore
Log in

Don't you dare go Hollow

config/input: add cursor section

Browse files at this commit

d8a511bbac8d487c537dc325d105c1b07ff9c0cfKirill ChibisovParent: 8d443c2
This should allow users to configure theme and size for the cursor,
as well as automatically set `XCURSOR_THEME` and `XCURSOR_SIZE` env
variables.
resources/default-config.kdlmodified+7 -0
@@ -67,6 +67,13 @@ focus-ring {
6767 inactive-color 0.3 0.3 0.3 1.0
6868 }
6969
70+cursor {
71+ // Change the theme and size of the cursor as well as set the
72+ // `XCURSOR_THEME` and `XCURSOR_SIZE` env variables.
73+ // xcursor-theme "default"
74+ // xcursor-size 24
75+}
76+
7077 // Uncomment this line to ask the clients to omit their client-side decorations if possible.
7178 // If the client will specifically ask for CSD, the request will be honored.
7279 // prefer-no-csd
src/config.rsmodified+28 -0
@@ -21,6 +21,8 @@ pub struct Config {
2121 #[knuffel(child, default)]
2222 pub prefer_no_csd: bool,
2323 #[knuffel(child, default)]
24+ pub cursor: Cursor,
25+ #[knuffel(child, default)]
2426 pub binds: Binds,
2527 #[knuffel(child, default)]
2628 pub debug: DebugConfig,
@@ -152,6 +154,23 @@ impl From<Color> for [f32; 4] {
152154 }
153155 }
154156
157+#[derive(knuffel::Decode, Debug, PartialEq)]
158+pub struct Cursor {
159+ #[knuffel(child, unwrap(argument), default = String::from("default"))]
160+ pub xcursor_theme: String,
161+ #[knuffel(child, unwrap(argument), default = 24)]
162+ pub xcursor_size: u8,
163+}
164+
165+impl Default for Cursor {
166+ fn default() -> Self {
167+ Self {
168+ xcursor_theme: String::from("default"),
169+ xcursor_size: 24,
170+ }
171+ }
172+}
173+
155174 #[derive(knuffel::Decode, Debug, Default, PartialEq, Eq)]
156175 pub struct Binds(#[knuffel(children)] pub Vec<Bind>);
157176
@@ -366,6 +385,11 @@ mod tests {
366385
367386 prefer-no-csd
368387
388+ cursor {
389+ xcursor-theme "breeze_cursors"
390+ xcursor-size 16
391+ }
392+
369393 binds {
370394 Mod+T { spawn "alacritty"; }
371395 Mod+Q { close-window; }
@@ -421,6 +445,10 @@ mod tests {
421445 },
422446 },
423447 prefer_no_csd: true,
448+ cursor: Cursor {
449+ xcursor_theme: String::from("breeze_cursors"),
450+ xcursor_size: 16,
451+ },
424452 binds: Binds(vec![
425453 Bind {
426454 key: Key {
src/cursor.rsmodified+13 -6
@@ -1,4 +1,5 @@
11 use std::collections::HashMap;
2+use std::env;
23 use std::fs::File;
34 use std::io::Read;
45
@@ -10,17 +11,22 @@ use smithay::utils::{Physical, Point, Transform};
1011 use xcursor::parser::{parse_xcursor, Image};
1112 use xcursor::CursorTheme;
1213
13-const CURSOR_SIZE: i32 = 24;
1414 static FALLBACK_CURSOR_DATA: &[u8] = include_bytes!("../resources/cursor.rgba");
1515
1616 pub struct Cursor {
1717 images: Vec<Image>,
18+ size: i32,
1819 cache: HashMap<i32, (TextureBuffer<GlesTexture>, Point<i32, Physical>)>,
1920 }
2021
2122 impl Cursor {
22- pub fn load() -> Self {
23- let images = match load_xcursor() {
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) {
2430 Ok(images) => images,
2531 Err(err) => {
2632 warn!("error loading xcursor default cursor: {err:?}");
@@ -40,6 +46,7 @@ impl Cursor {
4046
4147 Self {
4248 images,
49+ size: size as i32,
4350 cache: HashMap::new(),
4451 }
4552 }
@@ -54,7 +61,7 @@ impl Cursor {
5461 .or_insert_with_key(|scale| {
5562 let _span = tracy_client::span!("create cursor texture");
5663
57- let size = CURSOR_SIZE * scale;
64+ let size = self.size * scale;
5865
5966 let nearest_image = self
6067 .images
@@ -86,10 +93,10 @@ impl Cursor {
8693 }
8794 }
8895
89-fn load_xcursor() -> anyhow::Result<Vec<Image>> {
96+fn load_xcursor(theme: &str) -> anyhow::Result<Vec<Image>> {
9097 let _span = tracy_client::span!();
9198
92- let theme = CursorTheme::load("default");
99+ let theme = CursorTheme::load(theme);
93100 let path = theme
94101 .load_icon("default")
95102 .ok_or_else(|| anyhow!("no default icon"))?;
src/niri.rsmodified+14 -2
@@ -262,7 +262,18 @@ impl State {
262262 }
263263 };
264264
265- *self.niri.config.borrow_mut() = config;
265+ let mut old_config = self.niri.config.borrow_mut();
266+
267+ if config.cursor != old_config.cursor {
268+ self.niri.default_cursor =
269+ Cursor::load(&config.cursor.xcursor_theme, config.cursor.xcursor_size);
270+ }
271+
272+ *old_config = config;
273+
274+ // Release the borrow.
275+ drop(old_config);
276+
266277 self.niri.queue_redraw_all();
267278 // FIXME: apply output scale and whatnot.
268279 // FIXME: apply libinput device settings.
@@ -329,7 +340,8 @@ impl Niri {
329340 .unwrap();
330341 seat.add_pointer();
331342
332- let default_cursor = Cursor::load();
343+ let default_cursor =
344+ Cursor::load(&config_.cursor.xcursor_theme, config_.cursor.xcursor_size);
333345
334346 let socket_source = ListeningSocketSource::new_auto().unwrap();
335347 let socket_name = socket_source.socket_name().to_os_string();