-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for multiple devices in config (#8)
- Loading branch information
Showing
15 changed files
with
403 additions
and
293 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,9 @@ | ||
{ | ||
"device": { | ||
"productId": 2116, | ||
"usage": 97, | ||
"usagePage": 65376 | ||
}, | ||
"layouts": ["en"], | ||
"reconnectDelay": 5000 | ||
"devices": [ | ||
{ | ||
"name": "stront", | ||
"productId": "0x0844" | ||
} | ||
], | ||
"layouts": ["en"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,77 @@ | ||
use std::path::PathBuf; | ||
use std::{path::PathBuf, sync::OnceLock}; | ||
|
||
#[derive(serde::Deserialize, serde::Serialize)] | ||
#[serde(rename_all = "camelCase")] | ||
pub struct Config { | ||
pub device: Device, | ||
pub devices: Vec<Device>, | ||
pub layouts: Vec<String>, | ||
pub reconnect_delay: u64, | ||
#[serde(skip_serializing_if = "Option::is_none")] | ||
pub reconnect_delay: Option<u64>, | ||
} | ||
|
||
#[derive(serde::Deserialize, serde::Serialize)] | ||
#[serde(rename_all = "camelCase")] | ||
pub struct Device { | ||
#[serde(skip_serializing_if = "Option::is_none")] | ||
pub name: Option<String>, | ||
#[serde(serialize_with = "hex_to_string", deserialize_with = "string_to_hex")] | ||
pub product_id: u16, | ||
pub usage: u16, | ||
pub usage_page: u16, | ||
#[serde(skip_serializing_if = "Option::is_none")] | ||
pub usage: Option<u16>, | ||
#[serde(skip_serializing_if = "Option::is_none")] | ||
pub usage_page: Option<u16>, | ||
} | ||
|
||
pub fn get_config(maybe_path: Option<PathBuf>) -> Config { | ||
static CONFIG: OnceLock<Config> = OnceLock::new(); | ||
|
||
pub fn get_config() -> &'static Config { | ||
CONFIG.get().unwrap() | ||
} | ||
|
||
pub fn load_config(path: PathBuf) -> &'static Config { | ||
if let Some(config) = CONFIG.get() { | ||
return config; | ||
} | ||
|
||
let default_config = Config { | ||
device: Device { | ||
devices: vec![Device { | ||
name: None, | ||
product_id: 0x0844, | ||
usage: 0x61, | ||
usage_page: 0xff60, | ||
}, | ||
layouts: vec!["pl".to_string()], | ||
reconnect_delay: 5000, | ||
usage: None, | ||
usage_page: None, | ||
}], | ||
layouts: vec!["en".to_string()], | ||
reconnect_delay: None, | ||
}; | ||
|
||
let path = maybe_path.unwrap_or("./qmk-hid-host.json".into()); | ||
|
||
if let Ok(file) = std::fs::read_to_string(&path) { | ||
if let Ok(file_config) = serde_json::from_str::<Config>(&file) { | ||
tracing::info!("Read config from file {:?}", path); | ||
return file_config; | ||
} | ||
|
||
tracing::error!("Error while reading config from file {:?}", path); | ||
let config = serde_json::from_str::<Config>(&file) | ||
.map_err(|e| tracing::error!("Incorrect config file: {}", e)) | ||
.unwrap(); | ||
return CONFIG.get_or_init(|| config); | ||
} | ||
|
||
let file_content = serde_json::to_string_pretty(&default_config).unwrap(); | ||
std::fs::write(&path, &file_content).unwrap(); | ||
std::fs::write(&path, &file_content) | ||
.map_err(|e| tracing::error!("Error while saving config file to {:?}: {}", path, e)) | ||
.unwrap(); | ||
tracing::info!("New config file created at {:?}", path); | ||
|
||
return default_config; | ||
CONFIG.get_or_init(|| default_config) | ||
} | ||
|
||
fn string_to_hex<'de, D>(deserializer: D) -> Result<u16, D::Error> | ||
where | ||
D: serde::Deserializer<'de>, | ||
{ | ||
let value: &str = serde::Deserialize::deserialize(deserializer)?; | ||
let hex = value.trim_start_matches("0x"); | ||
return u16::from_str_radix(hex, 16).map_err(serde::de::Error::custom); | ||
} | ||
|
||
fn hex_to_string<S>(value: &u16, serializer: S) -> Result<S::Ok, S::Error> | ||
where | ||
S: serde::Serializer, | ||
{ | ||
serializer.serialize_str(&format!("0x{:04x}", value)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.