From f6405cad6954a67699049825087a2b4cde82f707 Mon Sep 17 00:00:00 2001 From: Frieder Hannenheim Date: Sun, 11 May 2025 15:53:59 +0200 Subject: [PATCH] core: config: Add configuration options for keyboard colors [richard: prefix commit message with code modified and sign] --- README.md | 9 +++++ src/core/config.rs | 99 ++++++++++++++++++++++++++++++++++++++++++++++ src/core/mod.rs | 1 + 3 files changed, 109 insertions(+) diff --git a/README.md b/README.md index bfba0ee..c3c2924 100644 --- a/README.md +++ b/README.md @@ -122,6 +122,15 @@ Configuration example: height: 185 input-method: true + colors: + background: [0, 0, 0] + keycap: [0, 0, 0] + keycap_pressed: [51,51,51] + label: [255, 255, 255] + label_locked: [25, 255, 0] + label_pressed: [255, 153, 0] + sublabel: [178, 178, 178] + # License Unfettered Keyboard is free software; you can redistribute it and/or modify diff --git a/src/core/config.rs b/src/core/config.rs index 4fd5755..f7f4a40 100644 --- a/src/core/config.rs +++ b/src/core/config.rs @@ -5,11 +5,55 @@ use std::convert::TryInto; use std::fs::File; +use rgb::alt::BGR; +use rgb::alt::BGRA; +use rgb::RGB; use yaml_rust2::yaml::Hash; use yaml_rust2::yaml::LoadError; use yaml_rust2::yaml::YamlDecoder; use yaml_rust2::yaml::Yaml; +#[derive(Debug, Clone)] +pub struct KeyboardColors { + pub background: BGRA, + pub keycap: BGRA, + pub keycap_pressed: BGRA, + pub label: BGR, + pub label_locked: BGR, + pub label_pressed: BGR, + pub sublabel: BGR, +} + +impl Default for KeyboardColors { + fn default() -> Self + { + Self { + background: RGB::new(0, 0, 0).into(), + keycap: RGB::new(0, 0, 0).into(), + keycap_pressed: RGB::new(51,51,51).into(), + label: RGB::new(255, 255, 255).into(), + label_locked: RGB::new(25, 255, 0).into(), + label_pressed: RGB::new(255, 153, 0).into(), + sublabel: RGB::new(178, 178, 178).into(), + } + } +} + +fn parse_color(yaml: &Yaml) -> Option> +{ + let mut color_bytes = yaml + .as_vec()? + .iter() + .map_while(|b| b.as_i64()) + .map_while(|b| b.try_into().ok()); + + Some(RGB::new( + color_bytes.next()?, + color_bytes.next()?, + color_bytes.next()? + )) +} + #[derive(Debug)] pub struct Configuration { longpress: u64, @@ -18,6 +62,8 @@ pub struct Configuration { extra_keys: Vec, wayland_height: i32, wayland_im_enable: bool, + + colors: KeyboardColors, } impl Configuration { @@ -41,6 +87,45 @@ impl Configuration { } } + fn load_colors(&mut self, yaml: &Hash) + { + if let Some(keycap) = yaml.get(&Yaml::String(String::from("keycap"))) { + self.colors.keycap = parse_color(keycap) + .expect("Keycap color needs to be a list of 3 8-bit unsigned integers") + .into(); + } + if let Some(keycap_pressed) = yaml.get(&Yaml::String(String::from("keycap_pressed"))) { + self.colors.keycap_pressed = parse_color(keycap_pressed) + .expect("Keycap pressed color needs to be a list of 3 8-bit unsigned integers") + .into(); + } + if let Some(label) = yaml.get(&Yaml::String(String::from("label"))) { + self.colors.label = parse_color(label) + .expect("Label color needs to be a list of 3 8-bit unsigned integers") + .into(); + } + if let Some(label_pressed) = yaml.get(&Yaml::String(String::from("label_pressed"))) { + self.colors.label_pressed = parse_color(label_pressed) + .expect("Label pressed color needs to be a list of 3 8-bit unsigned integers") + .into(); + } + if let Some(label_locked) = yaml.get(&Yaml::String(String::from("label_locked"))) { + self.colors.label_locked = parse_color(label_locked) + .expect("Label locked color needs to be a list of 3 8-bit unsigned integers") + .into(); + } + if let Some(sublabel) = yaml.get(&Yaml::String(String::from("sublabel"))) { + self.colors.sublabel = parse_color(sublabel) + .expect("Sublabel color needs to be a list of 3 8-bit unsigned integers") + .into(); + } + if let Some(background) = yaml.get(&Yaml::String(String::from("background"))) { + self.colors.background = parse_color(background) + .expect("Background color needs to be a list of 3 8-bit unsigned integers") + .into(); + } + } + pub fn load() -> Result { let mut cfg = Configuration { @@ -50,6 +135,8 @@ impl Configuration { extra_keys: vec![ String::from("alt"), String::from("meta") ], wayland_height: 185, wayland_im_enable: true, + + colors: KeyboardColors::default(), }; if let Ok(file) = File::open("/etc/unfettered-keyboard.yaml") { @@ -94,6 +181,12 @@ impl Configuration { let wl = wl.as_hash().expect("Wayland configuration should be a YAML mapping"); cfg.load_wayland(wl); } + + let colors = yaml.get(&Yaml::String(String::from("colors"))); + if let Some(colors) = colors { + let colors = colors.as_hash().expect("Color configuration should be a YAML mapping"); + cfg.load_colors(colors); + } } Ok(cfg) @@ -134,4 +227,10 @@ impl Configuration { { self.wayland_im_enable } + + #[inline(always)] + pub fn colors(&self) -> &KeyboardColors + { + &self.colors + } } diff --git a/src/core/mod.rs b/src/core/mod.rs index 2dd41e9..0af1d19 100644 --- a/src/core/mod.rs +++ b/src/core/mod.rs @@ -13,6 +13,7 @@ pub use self::button::Button; pub use self::button::Keyboard; pub use self::button::ModState; pub use self::config::Configuration; +pub use self::config::KeyboardColors; pub use self::graphics::Display; pub use self::graphics::Graphics; pub use self::layout::Layout;