move config to .config/unffettered-keyboard/unffetered.yaml

This commit is contained in:
Your Name 2025-11-06 21:10:40 +01:00
parent a0d8bbd1f4
commit 5f2aa46ea9

View file

@ -3,15 +3,15 @@
* Copyright (c) 2024, Richard Acayan. All rights reserved.
*/
use std::convert::TryInto;
use std::fs::File;
use rgb::alt::BGR;
use rgb::alt::BGRA;
use rgb::RGB;
use std::convert::TryInto;
use std::fs::File;
use yaml_rust2::yaml::Hash;
use yaml_rust2::yaml::LoadError;
use yaml_rust2::yaml::YamlDecoder;
use yaml_rust2::yaml::Yaml;
use yaml_rust2::yaml::YamlDecoder;
#[derive(Debug, Clone)]
pub struct KeyboardColors {
@ -25,8 +25,7 @@ pub struct KeyboardColors {
}
impl Default for KeyboardColors {
fn default() -> Self
{
fn default() -> Self {
Self {
background: RGB::new(0, 0, 0).into(),
keycap: RGB::new(0, 0, 0).into(),
@ -39,8 +38,7 @@ impl Default for KeyboardColors {
}
}
fn parse_color(yaml: &Yaml) -> Option<RGB<u8>>
{
fn parse_color(yaml: &Yaml) -> Option<RGB<u8>> {
let mut color_bytes = yaml
.as_vec()?
.iter()
@ -50,7 +48,7 @@ fn parse_color(yaml: &Yaml) -> Option<RGB<u8>>
Some(RGB::new(
color_bytes.next()?,
color_bytes.next()?,
color_bytes.next()?
color_bytes.next()?,
))
}
@ -71,17 +69,19 @@ pub struct Configuration {
}
impl Configuration {
fn get_configuration_file() -> Result<File, Box<dyn std::error::Error>>
{
let xdg_dirs = xdg::BaseDirectories::new()?;
let config_file = xdg_dirs.find_config_file("unfettered-keyboard.yaml")
.ok_or(std::io::Error::new(std::io::ErrorKind::NotFound, "Config file not found"))?;
fn get_configuration_file() -> Result<File, Box<dyn std::error::Error>> {
let xdg_dirs = xdg::BaseDirectories::with_prefix("unfettered-keyboard")?;
let config_file = xdg_dirs
.find_config_file("unfettered-keyboard.yaml")
.ok_or(std::io::Error::new(
std::io::ErrorKind::NotFound,
"Config file not found",
))?;
println!("test{:?}", config_file);
Ok(File::open(config_file)?)
}
fn load_evfb(&mut self, yaml: &Hash)
{
fn load_evfb(&mut self, yaml: &Hash) {
let height = yaml.get(&Yaml::String(String::from("height")));
if let Some(height) = height {
let height = match height.as_f64() {
@ -93,8 +93,7 @@ impl Configuration {
}
}
fn load_wayland(&mut self, yaml: &Hash)
{
fn load_wayland(&mut self, yaml: &Hash) {
let height = yaml.get(&Yaml::String(String::from("height")));
if let Some(height) = height {
let height = match height.as_i64() {
@ -113,8 +112,7 @@ impl Configuration {
}
}
fn load_colors(&mut self, yaml: &Hash)
{
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")
@ -152,8 +150,7 @@ impl Configuration {
}
}
pub fn load() -> Result<Self, LoadError>
{
pub fn load() -> Result<Self, LoadError> {
let mut cfg = Configuration {
longpress: 600,
repeat: 25,
@ -171,7 +168,9 @@ impl Configuration {
if let Ok(file) = Self::get_configuration_file() {
let yaml = YamlDecoder::read(file).decode()?;
let yaml = yaml[0].as_hash().expect("Top-level configuration should be a YAML mapping");
let yaml = yaml[0]
.as_hash()
.expect("Top-level configuration should be a YAML mapping");
let longpress = yaml.get(&Yaml::String(String::from("longpress_ms")));
if let Some(longpress) = longpress {
@ -179,7 +178,8 @@ impl Configuration {
Some(l) => l.try_into().ok(),
None => None,
};
let longpress = longpress.expect("Longpress time should be a 64-bit unsigned integer");
let longpress =
longpress.expect("Longpress time should be a 64-bit unsigned integer");
cfg.longpress = longpress;
}
@ -202,19 +202,26 @@ impl Configuration {
let keys = yaml.get(&Yaml::String(String::from("extra_keys")));
if let Some(keys) = keys {
let keys = keys.as_vec().expect("Extra keys should be a list");
cfg.extra_keys = keys.iter().map(|y| String::from(y.as_str().unwrap())).collect();
cfg.extra_keys = keys
.iter()
.map(|y| String::from(y.as_str().unwrap()))
.collect();
cfg.extra_keys.sort_unstable();
}
let wl = yaml.get(&Yaml::String(String::from("wayland")));
if let Some(wl) = wl {
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);
}
let evfb = yaml.get(&Yaml::String(String::from("evfb")));
if let Some(evfb) = evfb {
let evfb = evfb.as_hash().expect("Linux evdev-fbdev configuration should be a YAML mapping");
let evfb = evfb
.as_hash()
.expect("Linux evdev-fbdev configuration should be a YAML mapping");
cfg.load_evfb(evfb);
}
@ -224,7 +231,8 @@ impl Configuration {
Some(l) => l.try_into().ok(),
None => None,
};
let key_padding = key_padding.expect("Key padding should be a 64-bit unsigned integer");
let key_padding =
key_padding.expect("Key padding should be a 64-bit unsigned integer");
cfg.key_padding = key_padding;
}
@ -234,14 +242,16 @@ impl Configuration {
Some(l) => l.try_into().ok(),
None => None,
};
let key_corner_radius = key_corner_radius.expect("Key corner radius should be a 64-bit unsigned integer");
let key_corner_radius = key_corner_radius
.expect("Key corner radius should be a 64-bit unsigned integer");
cfg.key_corner_radius = key_corner_radius;
}
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");
let colors = colors
.as_hash()
.expect("Color configuration should be a YAML mapping");
cfg.load_colors(colors);
}
}
@ -250,63 +260,52 @@ impl Configuration {
}
#[inline(always)]
pub fn longpress_ms(&self) -> u64
{
pub fn longpress_ms(&self) -> u64 {
self.longpress
}
#[inline(always)]
pub fn repeat_ms(&self) -> u64
{
pub fn repeat_ms(&self) -> u64 {
self.repeat
}
#[inline(always)]
pub fn layout(&self) -> &str
{
pub fn layout(&self) -> &str {
&self.layout
}
#[inline(always)]
pub fn extra_keys(&self) -> &Vec<String>
{
pub fn extra_keys(&self) -> &Vec<String> {
&self.extra_keys
}
#[inline(always)]
pub fn wayland_height(&self) -> i32
{
pub fn wayland_height(&self) -> i32 {
self.wayland_height
}
#[inline(always)]
pub fn wayland_im_enable(&self) -> bool
{
pub fn wayland_im_enable(&self) -> bool {
self.wayland_im_enable
}
#[inline(always)]
pub fn evfb_height(&self) -> f64
{
pub fn evfb_height(&self) -> f64 {
self.evfb_height
}
#[inline(always)]
pub fn colors(&self) -> &KeyboardColors
{
pub fn colors(&self) -> &KeyboardColors {
&self.colors
}
#[inline(always)]
pub fn key_padding(&self) -> u64
{
pub fn key_padding(&self) -> u64 {
self.key_padding
}
#[inline(always)]
pub fn key_corner_radius(&self) -> u64
{
pub fn key_corner_radius(&self) -> u64 {
self.key_corner_radius
}
}