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 {
67 inactive-color 0.3 0.3 0.3 1.067 inactive-color 0.3 0.3 0.3 1.0
68 }68 }
69 69
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+
70 // Uncomment this line to ask the clients to omit their client-side decorations if possible.77 // Uncomment this line to ask the clients to omit their client-side decorations if possible.
71 // If the client will specifically ask for CSD, the request will be honored.78 // If the client will specifically ask for CSD, the request will be honored.
72 // prefer-no-csd79 // prefer-no-csd
src/config.rsmodified+28 -0
@@ -21,6 +21,8 @@ pub struct Config {
21 #[knuffel(child, default)]21 #[knuffel(child, default)]
22 pub prefer_no_csd: bool,22 pub prefer_no_csd: bool,
23 #[knuffel(child, default)]23 #[knuffel(child, default)]
24+ pub cursor: Cursor,
25+ #[knuffel(child, default)]
24 pub binds: Binds,26 pub binds: Binds,
25 #[knuffel(child, default)]27 #[knuffel(child, default)]
26 pub debug: DebugConfig,28 pub debug: DebugConfig,
@@ -152,6 +154,23 @@ impl From<Color> for [f32; 4] {
152 }154 }
153 }155 }
154 156
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+
155 #[derive(knuffel::Decode, Debug, Default, PartialEq, Eq)]174 #[derive(knuffel::Decode, Debug, Default, PartialEq, Eq)]
156 pub struct Binds(#[knuffel(children)] pub Vec<Bind>);175 pub struct Binds(#[knuffel(children)] pub Vec<Bind>);
157 176
@@ -366,6 +385,11 @@ mod tests {
366 385
367 prefer-no-csd386 prefer-no-csd
368 387
388+ cursor {
389+ xcursor-theme "breeze_cursors"
390+ xcursor-size 16
391+ }
392+
369 binds {393 binds {
370 Mod+T { spawn "alacritty"; }394 Mod+T { spawn "alacritty"; }
371 Mod+Q { close-window; }395 Mod+Q { close-window; }
@@ -421,6 +445,10 @@ mod tests {
421 },445 },
422 },446 },
423 prefer_no_csd: true,447 prefer_no_csd: true,
448+ cursor: Cursor {
449+ xcursor_theme: String::from("breeze_cursors"),
450+ xcursor_size: 16,
451+ },
424 binds: Binds(vec![452 binds: Binds(vec![
425 Bind {453 Bind {
426 key: Key {454 key: Key {
src/cursor.rsmodified+13 -6
@@ -1,4 +1,5 @@
1 use std::collections::HashMap;1 use std::collections::HashMap;
2+use std::env;
2 use std::fs::File;3 use std::fs::File;
3 use std::io::Read;4 use std::io::Read;
4 5
@@ -10,17 +11,22 @@ use smithay::utils::{Physical, Point, Transform};
10 use xcursor::parser::{parse_xcursor, Image};11 use xcursor::parser::{parse_xcursor, Image};
11 use xcursor::CursorTheme;12 use xcursor::CursorTheme;
12 13
13-const CURSOR_SIZE: i32 = 24;
14 static FALLBACK_CURSOR_DATA: &[u8] = include_bytes!("../resources/cursor.rgba");14 static FALLBACK_CURSOR_DATA: &[u8] = include_bytes!("../resources/cursor.rgba");
15 15
16 pub struct Cursor {16 pub struct Cursor {
17 images: Vec<Image>,17 images: Vec<Image>,
18+ size: i32,
18 cache: HashMap<i32, (TextureBuffer<GlesTexture>, Point<i32, Physical>)>,19 cache: HashMap<i32, (TextureBuffer<GlesTexture>, Point<i32, Physical>)>,
19 }20 }
20 21
21 impl Cursor {22 impl Cursor {
22- pub fn load() -> Self {23+ /// Load the said theme as well as set the `XCURSOR_THEME` and `XCURSOR_SIZE`
23- let images = match load_xcursor() {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) {
24 Ok(images) => images,30 Ok(images) => images,
25 Err(err) => {31 Err(err) => {
26 warn!("error loading xcursor default cursor: {err:?}");32 warn!("error loading xcursor default cursor: {err:?}");
@@ -40,6 +46,7 @@ impl Cursor {
40 46
41 Self {47 Self {
42 images,48 images,
49+ size: size as i32,
43 cache: HashMap::new(),50 cache: HashMap::new(),
44 }51 }
45 }52 }
@@ -54,7 +61,7 @@ impl Cursor {
54 .or_insert_with_key(|scale| {61 .or_insert_with_key(|scale| {
55 let _span = tracy_client::span!("create cursor texture");62 let _span = tracy_client::span!("create cursor texture");
56 63
57- let size = CURSOR_SIZE * scale;64+ let size = self.size * scale;
58 65
59 let nearest_image = self66 let nearest_image = self
60 .images67 .images
@@ -86,10 +93,10 @@ impl Cursor {
86 }93 }
87 }94 }
88 95
89-fn load_xcursor() -> anyhow::Result<Vec<Image>> {96+fn load_xcursor(theme: &str) -> anyhow::Result<Vec<Image>> {
90 let _span = tracy_client::span!();97 let _span = tracy_client::span!();
91 98
92- let theme = CursorTheme::load("default");99+ let theme = CursorTheme::load(theme);
93 let path = theme100 let path = theme
94 .load_icon("default")101 .load_icon("default")
95 .ok_or_else(|| anyhow!("no default icon"))?;102 .ok_or_else(|| anyhow!("no default icon"))?;
src/niri.rsmodified+14 -2
@@ -262,7 +262,18 @@ impl State {
262 }262 }
263 };263 };
264 264
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+
266 self.niri.queue_redraw_all();277 self.niri.queue_redraw_all();
267 // FIXME: apply output scale and whatnot.278 // FIXME: apply output scale and whatnot.
268 // FIXME: apply libinput device settings.279 // FIXME: apply libinput device settings.
@@ -329,7 +340,8 @@ impl Niri {
329 .unwrap();340 .unwrap();
330 seat.add_pointer();341 seat.add_pointer();
331 342
332- let default_cursor = Cursor::load();343+ let default_cursor =
344+ Cursor::load(&config_.cursor.xcursor_theme, config_.cursor.xcursor_size);
333 345
334 let socket_source = ListeningSocketSource::new_auto().unwrap();346 let socket_source = ListeningSocketSource::new_auto().unwrap();
335 let socket_name = socket_source.socket_name().to_os_string();347 let socket_name = socket_source.socket_name().to_os_string();