diff --git a/Cargo.lock b/Cargo.lock index 11fb9a2..33c9331 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -167,7 +167,7 @@ dependencies = [ "fnv", "ident_case", "proc-macro2", - "quote 1.0.33", + "quote 1.0.35", "strsim", "syn 1.0.109", ] @@ -179,7 +179,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a4aab4dbc9f7611d8b55048a3a16d2d010c2c8334e46304b40ac1cc14bf3b48e" dependencies = [ "darling_core", - "quote 1.0.33", + "quote 1.0.35", "syn 1.0.109", ] @@ -228,7 +228,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4e40a16955681d469ab3da85aaa6b42ff656b3c67b52e1d8d3dd36afe97fd462" dependencies = [ "proc-macro2", - "quote 1.0.33", + "quote 1.0.35", "syn 1.0.109", ] @@ -261,7 +261,7 @@ checksum = "55a5e644a80e6d96b2b4910fa7993301d7b7926c045b475b62202b20a36ce69e" dependencies = [ "darling", "proc-macro2", - "quote 1.0.33", + "quote 1.0.35", "syn 1.0.109", ] @@ -320,8 +320,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "89ca545a94061b6365f2c7355b4b32bd20df3ff95f02da9329b34ccc3bd6ee72" dependencies = [ "proc-macro2", - "quote 1.0.33", - "syn 2.0.29", + "quote 1.0.35", + "syn 2.0.52", ] [[package]] @@ -391,6 +391,19 @@ dependencies = [ "hashbrown", ] +[[package]] +name = "int-enum" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b2e6a45c18bc4873fef226acaabbc8cfe1e28c6778d6de4ea58c7cf903339db0" +dependencies = [ + "proc-macro2", + "proc-macro2-diagnostics", + "quote 1.0.35", + "syn 2.0.52", + "version_check", +] + [[package]] name = "itertools" version = "0.11.0" @@ -519,7 +532,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "876a53fff98e03a936a674b29568b0e605f06b29372c2489ff4de23f1949743d" dependencies = [ "proc-macro2", - "quote 1.0.33", + "quote 1.0.35", "syn 1.0.109", ] @@ -553,6 +566,7 @@ version = "0.1.0" dependencies = [ "crossbeam-channel", "delay_map", + "int-enum", "itertools", "libpulse-binding", "midir", @@ -581,13 +595,26 @@ checksum = "26072860ba924cbfa98ea39c8c19b4dd6a4a25423dbdf219c1eca91aa0cf6964" [[package]] name = "proc-macro2" -version = "1.0.66" +version = "1.0.78" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "18fb31db3f9bddb2ea821cde30a9f70117e3f119938b5ee630b7403aa6e2ead9" +checksum = "e2422ad645d89c99f8f3e6b88a9fdeca7fabeac836b1002371c4367c8f984aae" dependencies = [ "unicode-ident", ] +[[package]] +name = "proc-macro2-diagnostics" +version = "0.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "af066a9c399a26e020ada66a034357a868728e72cd426f3adcd35f80d88d88c8" +dependencies = [ + "proc-macro2", + "quote 1.0.35", + "syn 2.0.52", + "version_check", + "yansi", +] + [[package]] name = "quote" version = "0.3.15" @@ -596,9 +623,9 @@ checksum = "7a6e920b65c65f10b2ae65c831a81a073a89edd28c7cce89475bff467ab4167a" [[package]] name = "quote" -version = "1.0.33" +version = "1.0.35" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5267fca4496028628a95160fc423a33e8b2e6af8a5302579e322e4b520293cae" +checksum = "291ec9ab5efd934aaf503a6466c5d5251535d108ee747472c3977cc5acc868ef" dependencies = [ "proc-macro2", ] @@ -625,8 +652,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4eca7ac642d82aa35b60049a6eccb4be6be75e599bd2e9adb5f875a737654af2" dependencies = [ "proc-macro2", - "quote 1.0.33", - "syn 2.0.29", + "quote 1.0.35", + "syn 2.0.52", ] [[package]] @@ -671,18 +698,18 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237" dependencies = [ "proc-macro2", - "quote 1.0.33", + "quote 1.0.35", "unicode-ident", ] [[package]] name = "syn" -version = "2.0.29" +version = "2.0.52" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c324c494eba9d92503e6f1ef2e6df781e78f6a7705a0202d9801b198807d518a" +checksum = "b699d15b36d1f02c3e7c69f8ffef53de37aefae075d8488d4ba1a7788d574a07" dependencies = [ "proc-macro2", - "quote 1.0.33", + "quote 1.0.35", "unicode-ident", ] @@ -711,8 +738,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6bb623b56e39ab7dcd4b1b98bb6c8f8d907ed255b18de254088016b27a8ee19b" dependencies = [ "proc-macro2", - "quote 1.0.33", - "syn 2.0.29", + "quote 1.0.35", + "syn 2.0.52", ] [[package]] @@ -791,6 +818,12 @@ version = "0.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8c1f860d7d29cf02cb2f3f359fd35991af3d30bac52c57d265a3c461074cb4dc" +[[package]] +name = "version_check" +version = "0.9.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" + [[package]] name = "wasm-bindgen" version = "0.2.87" @@ -811,8 +844,8 @@ dependencies = [ "log", "once_cell", "proc-macro2", - "quote 1.0.33", - "syn 2.0.29", + "quote 1.0.35", + "syn 2.0.52", "wasm-bindgen-shared", ] @@ -822,7 +855,7 @@ version = "0.2.87" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dee495e55982a3bd48105a7b947fd2a9b4a8ae3010041b9e0faab3f9cd028f1d" dependencies = [ - "quote 1.0.33", + "quote 1.0.35", "wasm-bindgen-macro-support", ] @@ -833,8 +866,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "54681b18a46765f095758388f2d0cf16eb8d4169b639ab575a8f5693af210c7b" dependencies = [ "proc-macro2", - "quote 1.0.33", - "syn 2.0.29", + "quote 1.0.35", + "syn 2.0.52", "wasm-bindgen-backend", "wasm-bindgen-shared", ] @@ -942,3 +975,9 @@ checksum = "7c2e3184b9c4e92ad5167ca73039d0c42476302ab603e2fec4487511f38ccefc" dependencies = [ "memchr", ] + +[[package]] +name = "yansi" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6c2861d76f58ec8fc95708b9b1e417f7b12fd72ad33c01fa6886707092dea0d3" diff --git a/Cargo.toml b/Cargo.toml index bcb3aa2..1553a5c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,6 +6,7 @@ edition = "2021" [dependencies] crossbeam-channel = "0.5.8" delay_map = "0.3.0" +int-enum = "1.0.1" itertools = "0.11.0" libpulse-binding = "2.28.1" midir = "0.9.1" diff --git a/src/config.rs b/src/config.rs index 6200ca9..8b19b60 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,65 +1,128 @@ +use int_enum::IntEnum; use serde::{Deserialize, Serialize}; -use std::{collections::BTreeMap, fmt::Debug}; +use std::path::Path; +use std::process; +use std::{collections::BTreeMap, fmt::Debug, fs}; use toml::ser::Error; -pub fn default() -> Result { - let mut applications = BTreeMap::new(); - applications.insert("Feishin".to_string(), "Feishin".to_string()); - applications.insert("Librewolf".to_string(), "Librewolf".to_string()); +pub fn init(path: &str) -> Config { + if !Path::new(path).exists() { + write_default(path); + } + load(path) +} + +fn serialize_default() -> Result { + let mut mpris = BTreeMap::new(); let mut keymap = BTreeMap::new(); keymap.insert( NanoKeys::Param4Mute, - "program-mute/Feishin,Librewolf".to_string(), + vec![ + KeyMapVariant::Mpris { + ids: vec!["Feishin".to_string()], + action: MprisAction::PlayPause, + }, + KeyMapVariant::PipeWire { + ids: vec![ + "alsa_output.usb-0d8c_Generic_USB_Audio_Device-00.iec958-stereo".to_string(), + ], + }, + ], ); - let mut audioinputs = BTreeMap::new(); - audioinputs.insert( - "headphones".to_string(), - "alsa_output.usb-Burr-Brown_from_TI_USB_Audio_CODEC-00.pro-output-0".to_string(), + keymap.insert( + NanoKeys::Play, + vec![KeyMapVariant::Exec { + command: "echo hello".to_string(), + }], ); - audioinputs.insert( - "speakers".to_string(), - "alsa_output.pci-0000_0c_00.4.analog-stereo".to_string(), - ); - let audiooutputs = BTreeMap::new(); + mpris.insert("music".to_string(), "Feishin".to_string()); let conf: Config = Config { general: General { - nano_kontrol_pattern: "nanoKONTROL2".to_string(), + midi_device: "nanoKONTROL2".to_string(), led_mode: LedMode::Toggle, }, - applications, keymap, - audioinputs, - audiooutputs, }; + toml::to_string(&conf) } +fn deserialize(path: &str) -> Result { + let cfg_str = fs::read_to_string(path).unwrap(); + toml::from_str::(&cfg_str) +} + +pub fn load(path: &str) -> Config { + let cfg = deserialize(path); + + match cfg { + Ok(expr) => expr, + Err(expr) => { + eprintln!("Failed loading config at {path}: {expr}"); + process::exit(1); + } + } +} + +pub fn write_default(path: &str) -> () { + let cfg_toml = serialize_default(); + match cfg_toml { + Ok(cfg_toml_text) => { + let _ = fs::write(path, &cfg_toml_text); + } + Err(expr) => { + eprintln!("Failed serializing default config: {expr}"); + process::exit(1); + } + } +} + +#[derive(Debug, Serialize, Deserialize)] +pub enum MprisAction { + Play, + Pause, + PlayPause, + Stop, + Next, + Previous, + VOLUME, +} + +#[derive(Debug, Serialize, Deserialize)] +#[serde(tag = "type")] +pub enum KeyMapVariant { + Mpris { + ids: Vec, + action: MprisAction, + }, + PipeWire { + ids: Vec, + }, + Exec { + command: String, + }, +} + #[derive(Debug, Serialize, Deserialize)] pub struct Config { pub general: General, - pub audioinputs: BTreeMap, - pub audiooutputs: BTreeMap, - pub applications: BTreeMap, - pub keymap: BTreeMap, + pub keymap: BTreeMap>, } #[derive(Debug, Serialize, Deserialize)] pub struct General { - #[serde(rename = "nanoKONTROLPattern")] - pub nano_kontrol_pattern: String, - #[serde(rename = "ledMode")] + pub midi_device: String, pub led_mode: LedMode, } #[derive(Debug, Serialize, Deserialize)] -#[serde(rename_all = "camelCase")] pub enum LedMode { Default, Toggle, } -#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord)] -#[serde(rename_all = "SCREAMING_SNAKE_CASE")] +#[repr(u8)] +#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord, IntEnum)] pub enum NanoKeys { TrackPrev = 58, TrackNext = 59,