diff --git a/src/config.rs b/src/config.rs index 28e56d5..4136aa3 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,4 +1,4 @@ -use crate::Config; +use serde::Deserialize; use dirs::config_dir; use std::{ env, @@ -6,12 +6,47 @@ use std::{ fs, io::ErrorKind, path::PathBuf, + collections::HashMap, + sync::Arc }; use tracing::debug; /// The name of the folder which holds the config pub const CONFIG_FOLDER_NAME: &'static str = "virtual-deck"; +#[derive(Deserialize, Debug)] +pub struct Config { + pub global: Option, + pub devices: Vec, +} + +#[derive(Deserialize, Debug)] +pub struct GlobalConfig; + +#[derive(Deserialize, Debug, Clone)] +pub struct DeviceConfig { + pub serial: String, + #[serde(default = "default_brightness")] + pub brightness: u8, + pub buttons: Vec>, +} + +fn default_brightness() -> u8 { + 100 +} + +#[derive(Deserialize, Debug, Clone)] +pub struct Button { + pub index: u8, + pub module: String, + /// options which get passed to the module + pub options: Option>, + /// allows to overwrite what it will do on a click (executes in /bin/sh) + pub on_click: Option, + /// allows to overwrite what it will do on a release + pub on_release: Option, +} + #[tracing::instrument] pub fn load_config() -> Result { let config_file: PathBuf = match env::var_os("DACH_DECKER_CONFIG") { diff --git a/src/device.rs b/src/device.rs index b1a617c..6064fcc 100644 --- a/src/device.rs +++ b/src/device.rs @@ -1,7 +1,7 @@ use crate::{ - config::ConfigError, + config::{Button, ConfigError, DeviceConfig}, modules::{retrieve_module_from_name, start_module, HostEvent}, - unwrap_or_error, Button, DeviceConfig, + unwrap_or_error, }; use deck_driver as streamdeck; use hidapi::HidApi; @@ -64,7 +64,7 @@ impl Device { let button_count = kind.key_count(); // CONFIG VALIDATING - for button in device_conf.buttons.clone().into_iter() { + for button in device_conf.buttons.as_slice().into_iter() { let _span_button = info_span!("button", index = button.index).entered(); // if the index of the button is higher than the button count if button_count < button.index { @@ -123,7 +123,7 @@ impl Device { } else { return Err(DeviceError::Config(ConfigError::ModuleDoesNotExist( btn.index, - btn.module.clone(), + btn.module.to_owned(), ))); } } diff --git a/src/main.rs b/src/main.rs index 7fd86fb..0e46896 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,8 +1,8 @@ use deck_driver as streamdeck; +use crate::config::{Config, DeviceConfig}; use device::Device; use hidapi::HidApi; -use serde::Deserialize; -use std::{collections::HashMap, process::exit, sync::Arc, time::Duration}; +use std::{collections::HashMap, process::exit, time::Duration}; use tracing::{debug, error, info, warn}; use tracing_subscriber::{ self, prelude::__tracing_subscriber_SubscriberExt, util::SubscriberInitExt, EnvFilter, @@ -34,16 +34,6 @@ macro_rules! unwrap_or_error { }; } -/// The config structure -#[derive(Deserialize, Debug)] -pub struct Config { - global: Option, - device: Vec, -} - -#[derive(Deserialize, Debug)] -struct GlobalConfig; - fn main() { let fmt_layer = tracing_subscriber::fmt::layer().with_target(false); let filter_layer = EnvFilter::try_from_default_env() @@ -101,7 +91,7 @@ pub async fn start(config: Config, mut hid: HidApi) { if !ignore_devices.contains(&hw_device.1) && devices.get(&hw_device.1).is_none() { debug!("New device detected: {}", &hw_device.1); if let Some(device_config) = - config.device.iter().find(|d| d.serial == hw_device.1) + config.devices.iter().find(|d| d.serial == hw_device.1) { // start the device and its listener if let Some(device) = @@ -151,26 +141,3 @@ pub async fn start_device( } } -#[derive(Deserialize, Debug, Clone)] -pub struct DeviceConfig { - pub serial: String, - #[serde(default = "default_brightness")] - pub brightness: u8, - pub buttons: Vec>, -} - -fn default_brightness() -> u8 { - 100 -} - -#[derive(Deserialize, Debug, Clone)] -pub struct Button { - index: u8, - module: String, - /// options which get passed to the module - options: Option>, - /// allows to overwrite what it will do on a click (executes in /bin/sh) - pub on_click: Option, - /// allows to overwrite what it will do on a release; Same options as [on_click] - pub on_release: Option, -} diff --git a/src/modules.rs b/src/modules.rs index cff88c4..6bccf9c 100644 --- a/src/modules.rs +++ b/src/modules.rs @@ -2,7 +2,7 @@ mod blank; mod counter; use self::counter::Counter; -use crate::Button; +use crate::config::Button; use async_trait::async_trait; pub use deck_driver as streamdeck; use futures_util::Future; @@ -38,8 +38,8 @@ pub enum HostEvent { pub type ModuleFuture = Pin> + Send>>; pub type ModuleFunction = fn(DeviceAccess, ChannelReceiver, Arc