mirror of
https://codeberg.org/Fl1tzi/microdeck.git
synced 2024-05-19 19:20:20 +00:00
better configuration internals
This commit is contained in:
parent
f45dc423d0
commit
0dfd2d5c39
|
@ -1,4 +1,4 @@
|
||||||
use crate::Config;
|
use serde::Deserialize;
|
||||||
use dirs::config_dir;
|
use dirs::config_dir;
|
||||||
use std::{
|
use std::{
|
||||||
env,
|
env,
|
||||||
|
@ -6,12 +6,47 @@ use std::{
|
||||||
fs,
|
fs,
|
||||||
io::ErrorKind,
|
io::ErrorKind,
|
||||||
path::PathBuf,
|
path::PathBuf,
|
||||||
|
collections::HashMap,
|
||||||
|
sync::Arc
|
||||||
};
|
};
|
||||||
use tracing::debug;
|
use tracing::debug;
|
||||||
|
|
||||||
/// The name of the folder which holds the config
|
/// The name of the folder which holds the config
|
||||||
pub const CONFIG_FOLDER_NAME: &'static str = "virtual-deck";
|
pub const CONFIG_FOLDER_NAME: &'static str = "virtual-deck";
|
||||||
|
|
||||||
|
#[derive(Deserialize, Debug)]
|
||||||
|
pub struct Config {
|
||||||
|
pub global: Option<GlobalConfig>,
|
||||||
|
pub devices: Vec<DeviceConfig>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[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<Arc<Button>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
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<HashMap<String, String>>,
|
||||||
|
/// allows to overwrite what it will do on a click (executes in /bin/sh)
|
||||||
|
pub on_click: Option<String>,
|
||||||
|
/// allows to overwrite what it will do on a release
|
||||||
|
pub on_release: Option<String>,
|
||||||
|
}
|
||||||
|
|
||||||
#[tracing::instrument]
|
#[tracing::instrument]
|
||||||
pub fn load_config() -> Result<Config, ConfigError> {
|
pub fn load_config() -> Result<Config, ConfigError> {
|
||||||
let config_file: PathBuf = match env::var_os("DACH_DECKER_CONFIG") {
|
let config_file: PathBuf = match env::var_os("DACH_DECKER_CONFIG") {
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
use crate::{
|
use crate::{
|
||||||
config::ConfigError,
|
config::{Button, ConfigError, DeviceConfig},
|
||||||
modules::{retrieve_module_from_name, start_module, HostEvent},
|
modules::{retrieve_module_from_name, start_module, HostEvent},
|
||||||
unwrap_or_error, Button, DeviceConfig,
|
unwrap_or_error,
|
||||||
};
|
};
|
||||||
use deck_driver as streamdeck;
|
use deck_driver as streamdeck;
|
||||||
use hidapi::HidApi;
|
use hidapi::HidApi;
|
||||||
|
@ -64,7 +64,7 @@ impl Device {
|
||||||
let button_count = kind.key_count();
|
let button_count = kind.key_count();
|
||||||
|
|
||||||
// CONFIG VALIDATING
|
// 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();
|
let _span_button = info_span!("button", index = button.index).entered();
|
||||||
// if the index of the button is higher than the button count
|
// if the index of the button is higher than the button count
|
||||||
if button_count < button.index {
|
if button_count < button.index {
|
||||||
|
@ -123,7 +123,7 @@ impl Device {
|
||||||
} else {
|
} else {
|
||||||
return Err(DeviceError::Config(ConfigError::ModuleDoesNotExist(
|
return Err(DeviceError::Config(ConfigError::ModuleDoesNotExist(
|
||||||
btn.index,
|
btn.index,
|
||||||
btn.module.clone(),
|
btn.module.to_owned(),
|
||||||
)));
|
)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
39
src/main.rs
39
src/main.rs
|
@ -1,8 +1,8 @@
|
||||||
use deck_driver as streamdeck;
|
use deck_driver as streamdeck;
|
||||||
|
use crate::config::{Config, DeviceConfig};
|
||||||
use device::Device;
|
use device::Device;
|
||||||
use hidapi::HidApi;
|
use hidapi::HidApi;
|
||||||
use serde::Deserialize;
|
use std::{collections::HashMap, process::exit, time::Duration};
|
||||||
use std::{collections::HashMap, process::exit, sync::Arc, time::Duration};
|
|
||||||
use tracing::{debug, error, info, warn};
|
use tracing::{debug, error, info, warn};
|
||||||
use tracing_subscriber::{
|
use tracing_subscriber::{
|
||||||
self, prelude::__tracing_subscriber_SubscriberExt, util::SubscriberInitExt, EnvFilter,
|
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<GlobalConfig>,
|
|
||||||
device: Vec<DeviceConfig>,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Deserialize, Debug)]
|
|
||||||
struct GlobalConfig;
|
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let fmt_layer = tracing_subscriber::fmt::layer().with_target(false);
|
let fmt_layer = tracing_subscriber::fmt::layer().with_target(false);
|
||||||
let filter_layer = EnvFilter::try_from_default_env()
|
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() {
|
if !ignore_devices.contains(&hw_device.1) && devices.get(&hw_device.1).is_none() {
|
||||||
debug!("New device detected: {}", &hw_device.1);
|
debug!("New device detected: {}", &hw_device.1);
|
||||||
if let Some(device_config) =
|
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
|
// start the device and its listener
|
||||||
if let Some(device) =
|
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<Arc<Button>>,
|
|
||||||
}
|
|
||||||
|
|
||||||
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<HashMap<String, String>>,
|
|
||||||
/// allows to overwrite what it will do on a click (executes in /bin/sh)
|
|
||||||
pub on_click: Option<String>,
|
|
||||||
/// allows to overwrite what it will do on a release; Same options as [on_click]
|
|
||||||
pub on_release: Option<String>,
|
|
||||||
}
|
|
||||||
|
|
|
@ -2,7 +2,7 @@ mod blank;
|
||||||
mod counter;
|
mod counter;
|
||||||
|
|
||||||
use self::counter::Counter;
|
use self::counter::Counter;
|
||||||
use crate::Button;
|
use crate::config::Button;
|
||||||
use async_trait::async_trait;
|
use async_trait::async_trait;
|
||||||
pub use deck_driver as streamdeck;
|
pub use deck_driver as streamdeck;
|
||||||
use futures_util::Future;
|
use futures_util::Future;
|
||||||
|
@ -38,8 +38,8 @@ pub enum HostEvent {
|
||||||
pub type ModuleFuture = Pin<Box<dyn Future<Output = Result<(), ReturnError>> + Send>>;
|
pub type ModuleFuture = Pin<Box<dyn Future<Output = Result<(), ReturnError>> + Send>>;
|
||||||
pub type ModuleFunction = fn(DeviceAccess, ChannelReceiver, Arc<Button>) -> ModuleFuture;
|
pub type ModuleFunction = fn(DeviceAccess, ChannelReceiver, Arc<Button>) -> ModuleFuture;
|
||||||
|
|
||||||
pub fn retrieve_module_from_name(name: &String) -> Option<ModuleFunction> {
|
pub fn retrieve_module_from_name(name: &str) -> Option<ModuleFunction> {
|
||||||
MODULE_MAP.get(name.as_str()).copied()
|
MODULE_MAP.get(name).copied()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// starts a module
|
/// starts a module
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
use crate::Button;
|
use crate::config::Button;
|
||||||
|
|
||||||
use super::Module;
|
use super::Module;
|
||||||
use super::{ChannelReceiver, DeviceAccess, HostEvent, ReturnError};
|
use super::{ChannelReceiver, DeviceAccess, HostEvent, ReturnError};
|
||||||
|
|
Loading…
Reference in a new issue