2022-01-29 04:23:16 +08:00
|
|
|
use directories::ProjectDirs;
|
|
|
|
use std::{convert::TryFrom, fs, path::PathBuf};
|
|
|
|
|
2022-01-28 20:17:34 +08:00
|
|
|
use serde_json::Value;
|
|
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub enum DeviceMode {
|
|
|
|
None,
|
2022-01-29 01:10:12 +08:00
|
|
|
TasollerOne,
|
|
|
|
TasollerTwo,
|
2022-01-28 20:17:34 +08:00
|
|
|
Yuancon,
|
|
|
|
Brokenithm { ground_only: bool },
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, Copy)]
|
|
|
|
pub enum KeyboardLayout {
|
|
|
|
Tasoller,
|
|
|
|
Yuancon,
|
|
|
|
Deemo,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub enum OutputMode {
|
|
|
|
None,
|
|
|
|
Keyboard {
|
|
|
|
layout: KeyboardLayout,
|
|
|
|
sensitivity: u8,
|
|
|
|
},
|
|
|
|
Websocket {
|
|
|
|
url: String,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, Copy)]
|
|
|
|
pub enum ReactiveLayout {
|
|
|
|
Four,
|
|
|
|
Eight,
|
|
|
|
Sixteen,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub enum LedMode {
|
|
|
|
None,
|
2022-01-29 02:51:09 +08:00
|
|
|
Reactive {
|
|
|
|
layout: ReactiveLayout,
|
|
|
|
sensitivity: u8,
|
|
|
|
},
|
2022-01-28 20:17:34 +08:00
|
|
|
Attract,
|
|
|
|
Test,
|
2022-01-29 02:51:09 +08:00
|
|
|
Websocket {
|
|
|
|
url: String,
|
|
|
|
},
|
2022-01-28 20:17:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub struct Config {
|
|
|
|
pub raw: String,
|
|
|
|
pub device_mode: DeviceMode,
|
|
|
|
pub output_mode: OutputMode,
|
|
|
|
pub led_mode: LedMode,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Config {
|
2022-01-29 04:23:16 +08:00
|
|
|
pub fn from_str(s: &str) -> Option<Config> {
|
|
|
|
let v: Value = serde_json::from_str(s).ok()?;
|
2022-01-28 20:17:34 +08:00
|
|
|
|
2022-01-29 04:23:16 +08:00
|
|
|
Some(Config {
|
2022-01-28 20:17:34 +08:00
|
|
|
raw: s.to_string(),
|
2022-01-29 04:23:16 +08:00
|
|
|
device_mode: match v["deviceMode"].as_str()? {
|
2022-01-28 20:17:34 +08:00
|
|
|
"none" => DeviceMode::None,
|
2022-01-29 01:10:12 +08:00
|
|
|
"tasoller-one" => DeviceMode::TasollerOne,
|
|
|
|
"tasoller-two" => DeviceMode::TasollerTwo,
|
2022-01-28 20:17:34 +08:00
|
|
|
"yuancon" => DeviceMode::Yuancon,
|
|
|
|
"brokenithm" => DeviceMode::Brokenithm { ground_only: false },
|
|
|
|
"brokenithm-ground" => DeviceMode::Brokenithm { ground_only: true },
|
|
|
|
_ => panic!("Invalid device mode"),
|
|
|
|
},
|
|
|
|
output_mode: match v["outputMode"].as_str().unwrap() {
|
|
|
|
"none" => OutputMode::None,
|
|
|
|
"kb-32-tasoller" => OutputMode::Keyboard {
|
|
|
|
layout: KeyboardLayout::Tasoller,
|
2022-01-29 04:23:16 +08:00
|
|
|
sensitivity: u8::try_from(v["keyboardSensitivity"].as_i64()?).ok()?,
|
2022-01-28 20:17:34 +08:00
|
|
|
},
|
|
|
|
"kb-32-yuancon" => OutputMode::Keyboard {
|
|
|
|
layout: KeyboardLayout::Yuancon,
|
2022-01-29 04:23:16 +08:00
|
|
|
sensitivity: u8::try_from(v["keyboardSensitivity"].as_i64()?).ok()?,
|
2022-01-28 20:17:34 +08:00
|
|
|
},
|
|
|
|
"kb-6-deemo" => OutputMode::Keyboard {
|
|
|
|
layout: KeyboardLayout::Deemo,
|
2022-01-29 04:23:16 +08:00
|
|
|
sensitivity: u8::try_from(v["keyboardSensitivity"].as_i64()?).ok()?,
|
2022-01-28 20:17:34 +08:00
|
|
|
},
|
|
|
|
"websocket" => OutputMode::Websocket {
|
|
|
|
url: v["outputWebsocketUrl"].to_string(),
|
|
|
|
},
|
|
|
|
_ => panic!("Invalid output mode"),
|
|
|
|
},
|
2022-01-29 04:23:16 +08:00
|
|
|
led_mode: match v["ledMode"].as_str()? {
|
2022-01-28 20:17:34 +08:00
|
|
|
"none" => LedMode::None,
|
|
|
|
"reactive-4" => LedMode::Reactive {
|
|
|
|
layout: ReactiveLayout::Four,
|
2022-01-29 04:23:16 +08:00
|
|
|
sensitivity: u8::try_from(v["ledSensitivity"].as_i64()?).ok()?,
|
2022-01-28 20:17:34 +08:00
|
|
|
},
|
|
|
|
"reactive-8" => LedMode::Reactive {
|
|
|
|
layout: ReactiveLayout::Eight,
|
2022-01-29 04:23:16 +08:00
|
|
|
sensitivity: u8::try_from(v["ledSensitivity"].as_i64()?).ok()?,
|
2022-01-28 20:17:34 +08:00
|
|
|
},
|
|
|
|
"reactive-16" => LedMode::Reactive {
|
|
|
|
layout: ReactiveLayout::Sixteen,
|
2022-01-29 04:23:16 +08:00
|
|
|
sensitivity: u8::try_from(v["ledSensitivity"].as_i64()?).ok()?,
|
2022-01-28 20:17:34 +08:00
|
|
|
},
|
|
|
|
"attract" => LedMode::Attract,
|
|
|
|
"test" => LedMode::Test,
|
|
|
|
"websocket" => LedMode::Websocket {
|
|
|
|
url: v["ledWebsocketUrl"].to_string(),
|
|
|
|
},
|
|
|
|
_ => panic!("Invalid led mode"),
|
|
|
|
},
|
2022-01-29 04:23:16 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fn factory() -> Self {
|
|
|
|
Self::from_str(
|
|
|
|
r#"{
|
|
|
|
"deviceMode": "none",
|
|
|
|
"outputMode": "none",
|
|
|
|
"ledMode": "none",
|
|
|
|
"keyboardSensitivity": 20,
|
|
|
|
"outputWebsocketUrl": "localhost:3000",
|
|
|
|
"ledSensitivity": 20,
|
|
|
|
"ledWebsocketUrl": "localhost:3001"
|
|
|
|
}"#,
|
|
|
|
)
|
|
|
|
.unwrap()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_saved_path() -> Option<Box<PathBuf>> {
|
|
|
|
let project_dir = ProjectDirs::from("me", "imp.ress", "slidershim").unwrap();
|
|
|
|
let config_dir = project_dir.config_dir();
|
|
|
|
fs::create_dir_all(config_dir);
|
|
|
|
|
|
|
|
let config_path = config_dir.join("config.json");
|
|
|
|
|
|
|
|
return Some(Box::new(config_path));
|
|
|
|
}
|
|
|
|
|
|
|
|
fn load_saved() -> Option<Self> {
|
|
|
|
let config_path = Self::get_saved_path()?;
|
|
|
|
if !config_path.exists() {
|
|
|
|
return None;
|
2022-01-28 20:17:34 +08:00
|
|
|
}
|
2022-01-29 04:23:16 +08:00
|
|
|
println!("Found saved");
|
|
|
|
let mut saved_data = fs::read_to_string(config_path.as_path()).ok()?;
|
|
|
|
println!("Loaded saved {}", saved_data);
|
|
|
|
return Self::from_str(saved_data.as_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn default() -> Self {
|
|
|
|
Self::load_saved()
|
|
|
|
.or_else(|| Some(Self::factory()))
|
|
|
|
.unwrap()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn save(&self) -> Option<()> {
|
|
|
|
let config_path = Self::get_saved_path()?;
|
|
|
|
println!("Saving to {:?}", config_path);
|
|
|
|
fs::write(config_path.as_path(), self.raw.as_str()).unwrap();
|
|
|
|
|
|
|
|
Some(())
|
2022-01-28 20:17:34 +08:00
|
|
|
}
|
|
|
|
}
|