core: config: Add configuration options for keyboard colors

[richard: prefix commit message with code modified and sign]
This commit is contained in:
Frieder Hannenheim 2025-05-11 15:53:59 +02:00 committed by Richard Acayan
parent 30b6b0e636
commit f6405cad69
No known key found for this signature in database
GPG key ID: 0346F4894879DB73
3 changed files with 109 additions and 0 deletions

View file

@ -122,6 +122,15 @@ Configuration example:
height: 185 height: 185
input-method: true 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 # License
Unfettered Keyboard is free software; you can redistribute it and/or modify Unfettered Keyboard is free software; you can redistribute it and/or modify

View file

@ -5,11 +5,55 @@
use std::convert::TryInto; use std::convert::TryInto;
use std::fs::File; use std::fs::File;
use rgb::alt::BGR;
use rgb::alt::BGRA;
use rgb::RGB;
use yaml_rust2::yaml::Hash; use yaml_rust2::yaml::Hash;
use yaml_rust2::yaml::LoadError; use yaml_rust2::yaml::LoadError;
use yaml_rust2::yaml::YamlDecoder; use yaml_rust2::yaml::YamlDecoder;
use yaml_rust2::yaml::Yaml; use yaml_rust2::yaml::Yaml;
#[derive(Debug, Clone)]
pub struct KeyboardColors {
pub background: BGRA<u8>,
pub keycap: BGRA<u8>,
pub keycap_pressed: BGRA<u8>,
pub label: BGR<u8>,
pub label_locked: BGR<u8>,
pub label_pressed: BGR<u8>,
pub sublabel: BGR<u8>,
}
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<RGB<u8>>
{
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)] #[derive(Debug)]
pub struct Configuration { pub struct Configuration {
longpress: u64, longpress: u64,
@ -18,6 +62,8 @@ pub struct Configuration {
extra_keys: Vec<String>, extra_keys: Vec<String>,
wayland_height: i32, wayland_height: i32,
wayland_im_enable: bool, wayland_im_enable: bool,
colors: KeyboardColors,
} }
impl Configuration { 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<Self, LoadError> pub fn load() -> Result<Self, LoadError>
{ {
let mut cfg = Configuration { let mut cfg = Configuration {
@ -50,6 +135,8 @@ impl Configuration {
extra_keys: vec![ String::from("alt"), String::from("meta") ], extra_keys: vec![ String::from("alt"), String::from("meta") ],
wayland_height: 185, wayland_height: 185,
wayland_im_enable: true, wayland_im_enable: true,
colors: KeyboardColors::default(),
}; };
if let Ok(file) = File::open("/etc/unfettered-keyboard.yaml") { 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"); let wl = wl.as_hash().expect("Wayland configuration should be a YAML mapping");
cfg.load_wayland(wl); 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) Ok(cfg)
@ -134,4 +227,10 @@ impl Configuration {
{ {
self.wayland_im_enable self.wayland_im_enable
} }
#[inline(always)]
pub fn colors(&self) -> &KeyboardColors
{
&self.colors
}
} }

View file

@ -13,6 +13,7 @@ pub use self::button::Button;
pub use self::button::Keyboard; pub use self::button::Keyboard;
pub use self::button::ModState; pub use self::button::ModState;
pub use self::config::Configuration; pub use self::config::Configuration;
pub use self::config::KeyboardColors;
pub use self::graphics::Display; pub use self::graphics::Display;
pub use self::graphics::Graphics; pub use self::graphics::Graphics;
pub use self::layout::Layout; pub use self::layout::Layout;