mirror of https://github.com/4yn/slidershim
persistency
parent
05ff89b923
commit
a44220fed1
|
@ -682,6 +682,15 @@ dependencies = [
|
||||||
"subtle",
|
"subtle",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "directories"
|
||||||
|
version = "4.0.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "f51c5d4ddabd36886dd3e1438cb358cdcb0d7c499cb99cb4ac2e38e18b5cb210"
|
||||||
|
dependencies = [
|
||||||
|
"dirs-sys",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "dirs"
|
name = "dirs"
|
||||||
version = "1.0.5"
|
version = "1.0.5"
|
||||||
|
@ -703,6 +712,17 @@ dependencies = [
|
||||||
"dirs-sys-next",
|
"dirs-sys-next",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "dirs-sys"
|
||||||
|
version = "0.3.6"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "03d86534ed367a67548dc68113a0f5db55432fdfbb6e6f9d77704397d95d5780"
|
||||||
|
dependencies = [
|
||||||
|
"libc",
|
||||||
|
"redox_users 0.4.0",
|
||||||
|
"winapi",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "dirs-sys-next"
|
name = "dirs-sys-next"
|
||||||
version = "0.1.2"
|
version = "0.1.2"
|
||||||
|
@ -2743,6 +2763,7 @@ checksum = "9def91fd1e018fe007022791f865d0ccc9b3a0d5001e01aabb8b40e46000afb5"
|
||||||
name = "slidershim"
|
name = "slidershim"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
|
"directories",
|
||||||
"palette",
|
"palette",
|
||||||
"rusb",
|
"rusb",
|
||||||
"serde",
|
"serde",
|
||||||
|
|
|
@ -21,6 +21,7 @@ tauri = { version = "1.0.0-beta.8", features = ["api-all", "system-tray"] }
|
||||||
rusb = "0.9.0"
|
rusb = "0.9.0"
|
||||||
palette = "0.6.0"
|
palette = "0.6.0"
|
||||||
winapi = "0.3.9"
|
winapi = "0.3.9"
|
||||||
|
directories = "4.0.1"
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
default = [ "custom-protocol" ]
|
default = [ "custom-protocol" ]
|
||||||
|
|
|
@ -2,7 +2,7 @@ extern crate slidershim;
|
||||||
|
|
||||||
use std::io;
|
use std::io;
|
||||||
|
|
||||||
use slidershim::slider_io::{config::Config, manager::Manager};
|
use slidershim::slider_io::{Config, Manager};
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let config = Config::from_str(
|
let config = Config::from_str(
|
||||||
|
@ -12,7 +12,8 @@ fn main() {
|
||||||
"ledMode": "reactive-8",
|
"ledMode": "reactive-8",
|
||||||
"keyboardSensitivity": 50
|
"keyboardSensitivity": 50
|
||||||
}"#,
|
}"#,
|
||||||
);
|
)
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
let manager = Manager::new(config);
|
let manager = Manager::new(config);
|
||||||
|
|
||||||
|
|
|
@ -7,6 +7,8 @@
|
||||||
|
|
||||||
mod slider_io;
|
mod slider_io;
|
||||||
|
|
||||||
|
use std::sync::{Arc, Mutex};
|
||||||
|
|
||||||
use tauri::{
|
use tauri::{
|
||||||
AppHandle, CustomMenuItem, Event, Manager, Runtime, SystemTray, SystemTrayEvent, SystemTrayMenu,
|
AppHandle, CustomMenuItem, Event, Manager, Runtime, SystemTray, SystemTrayEvent, SystemTrayMenu,
|
||||||
};
|
};
|
||||||
|
@ -24,6 +26,12 @@ fn quit_app() {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
|
let config = Arc::new(Mutex::new(Some(slider_io::Config::default())));
|
||||||
|
{
|
||||||
|
println!("Saving");
|
||||||
|
config.lock().unwrap().as_ref().unwrap().save();
|
||||||
|
}
|
||||||
|
|
||||||
tauri::Builder::default()
|
tauri::Builder::default()
|
||||||
.system_tray(
|
.system_tray(
|
||||||
SystemTray::new().with_menu(
|
SystemTray::new().with_menu(
|
||||||
|
@ -54,16 +62,34 @@ fn main() {
|
||||||
},
|
},
|
||||||
_ => {}
|
_ => {}
|
||||||
})
|
})
|
||||||
.setup(|app| {
|
.setup(move |app| {
|
||||||
app.listen_global("setConfig", |event| {
|
let app_handle = app.handle();
|
||||||
let payload = event.payload().unwrap();
|
let config_clone = Arc::clone(&config);
|
||||||
println!("Setting config to {}", payload);
|
app.listen_global("heartbeat", move |e| {
|
||||||
|
let config_handle = config_clone.lock().unwrap();
|
||||||
|
println!("Heartbeat {}", config_handle.as_ref().unwrap().raw.as_str());
|
||||||
|
app_handle
|
||||||
|
.emit_all(
|
||||||
|
"showConfig",
|
||||||
|
Some(config_handle.as_ref().unwrap().raw.as_str().to_string()),
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
});
|
});
|
||||||
|
|
||||||
let handle = app.handle();
|
let config_clone = Arc::clone(&config);
|
||||||
|
app.listen_global("setConfig", move |event| {
|
||||||
|
let payload = event.payload().unwrap();
|
||||||
|
println!("Setting config to {}", payload);
|
||||||
|
if let Some(new_config) = slider_io::Config::from_str(payload) {
|
||||||
|
let mut config_handle = config_clone.lock().unwrap();
|
||||||
|
config_handle.replace(new_config);
|
||||||
|
config_handle.as_ref().unwrap().save();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
let app_handle = app.handle();
|
||||||
app.listen_global("hide", move |_| {
|
app.listen_global("hide", move |_| {
|
||||||
hide_window(&handle);
|
hide_window(&app_handle);
|
||||||
});
|
});
|
||||||
|
|
||||||
app.listen_global("quit", |_| {
|
app.listen_global("quit", |_| {
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
|
use directories::ProjectDirs;
|
||||||
|
use std::{convert::TryFrom, fs, path::PathBuf};
|
||||||
|
|
||||||
use serde_json::Value;
|
use serde_json::Value;
|
||||||
use std::convert::TryFrom;
|
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub enum DeviceMode {
|
pub enum DeviceMode {
|
||||||
|
@ -59,12 +61,12 @@ pub struct Config {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Config {
|
impl Config {
|
||||||
pub fn from_str(s: &str) -> Config {
|
pub fn from_str(s: &str) -> Option<Config> {
|
||||||
let v: Value = serde_json::from_str(s).unwrap();
|
let v: Value = serde_json::from_str(s).ok()?;
|
||||||
|
|
||||||
Config {
|
Some(Config {
|
||||||
raw: s.to_string(),
|
raw: s.to_string(),
|
||||||
device_mode: match v["deviceMode"].as_str().unwrap() {
|
device_mode: match v["deviceMode"].as_str()? {
|
||||||
"none" => DeviceMode::None,
|
"none" => DeviceMode::None,
|
||||||
"tasoller-one" => DeviceMode::TasollerOne,
|
"tasoller-one" => DeviceMode::TasollerOne,
|
||||||
"tasoller-two" => DeviceMode::TasollerTwo,
|
"tasoller-two" => DeviceMode::TasollerTwo,
|
||||||
|
@ -77,34 +79,34 @@ impl Config {
|
||||||
"none" => OutputMode::None,
|
"none" => OutputMode::None,
|
||||||
"kb-32-tasoller" => OutputMode::Keyboard {
|
"kb-32-tasoller" => OutputMode::Keyboard {
|
||||||
layout: KeyboardLayout::Tasoller,
|
layout: KeyboardLayout::Tasoller,
|
||||||
sensitivity: u8::try_from(v["keyboardSensitivity"].as_i64().unwrap()).unwrap(),
|
sensitivity: u8::try_from(v["keyboardSensitivity"].as_i64()?).ok()?,
|
||||||
},
|
},
|
||||||
"kb-32-yuancon" => OutputMode::Keyboard {
|
"kb-32-yuancon" => OutputMode::Keyboard {
|
||||||
layout: KeyboardLayout::Yuancon,
|
layout: KeyboardLayout::Yuancon,
|
||||||
sensitivity: u8::try_from(v["keyboardSensitivity"].as_i64().unwrap()).unwrap(),
|
sensitivity: u8::try_from(v["keyboardSensitivity"].as_i64()?).ok()?,
|
||||||
},
|
},
|
||||||
"kb-6-deemo" => OutputMode::Keyboard {
|
"kb-6-deemo" => OutputMode::Keyboard {
|
||||||
layout: KeyboardLayout::Deemo,
|
layout: KeyboardLayout::Deemo,
|
||||||
sensitivity: u8::try_from(v["keyboardSensitivity"].as_i64().unwrap()).unwrap(),
|
sensitivity: u8::try_from(v["keyboardSensitivity"].as_i64()?).ok()?,
|
||||||
},
|
},
|
||||||
"websocket" => OutputMode::Websocket {
|
"websocket" => OutputMode::Websocket {
|
||||||
url: v["outputWebsocketUrl"].to_string(),
|
url: v["outputWebsocketUrl"].to_string(),
|
||||||
},
|
},
|
||||||
_ => panic!("Invalid output mode"),
|
_ => panic!("Invalid output mode"),
|
||||||
},
|
},
|
||||||
led_mode: match v["ledMode"].as_str().unwrap() {
|
led_mode: match v["ledMode"].as_str()? {
|
||||||
"none" => LedMode::None,
|
"none" => LedMode::None,
|
||||||
"reactive-4" => LedMode::Reactive {
|
"reactive-4" => LedMode::Reactive {
|
||||||
layout: ReactiveLayout::Four,
|
layout: ReactiveLayout::Four,
|
||||||
sensitivity: u8::try_from(v["ledSensitivity"].as_i64().unwrap()).unwrap(),
|
sensitivity: u8::try_from(v["ledSensitivity"].as_i64()?).ok()?,
|
||||||
},
|
},
|
||||||
"reactive-8" => LedMode::Reactive {
|
"reactive-8" => LedMode::Reactive {
|
||||||
layout: ReactiveLayout::Eight,
|
layout: ReactiveLayout::Eight,
|
||||||
sensitivity: u8::try_from(v["ledSensitivity"].as_i64().unwrap()).unwrap(),
|
sensitivity: u8::try_from(v["ledSensitivity"].as_i64()?).ok()?,
|
||||||
},
|
},
|
||||||
"reactive-16" => LedMode::Reactive {
|
"reactive-16" => LedMode::Reactive {
|
||||||
layout: ReactiveLayout::Sixteen,
|
layout: ReactiveLayout::Sixteen,
|
||||||
sensitivity: u8::try_from(v["ledSensitivity"].as_i64().unwrap()).unwrap(),
|
sensitivity: u8::try_from(v["ledSensitivity"].as_i64()?).ok()?,
|
||||||
},
|
},
|
||||||
"attract" => LedMode::Attract,
|
"attract" => LedMode::Attract,
|
||||||
"test" => LedMode::Test,
|
"test" => LedMode::Test,
|
||||||
|
@ -113,6 +115,56 @@ impl Config {
|
||||||
},
|
},
|
||||||
_ => panic!("Invalid led mode"),
|
_ => panic!("Invalid led mode"),
|
||||||
},
|
},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
}
|
}
|
||||||
|
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(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
pub mod config;
|
mod config;
|
||||||
|
|
||||||
mod controller_state;
|
mod controller_state;
|
||||||
mod worker;
|
mod worker;
|
||||||
|
@ -9,4 +9,7 @@ mod device;
|
||||||
mod led;
|
mod led;
|
||||||
mod output;
|
mod output;
|
||||||
|
|
||||||
pub mod manager;
|
mod manager;
|
||||||
|
|
||||||
|
pub use config::Config;
|
||||||
|
pub use manager::Manager;
|
||||||
|
|
|
@ -13,9 +13,14 @@
|
||||||
let ledSensitivity = 20;
|
let ledSensitivity = 20;
|
||||||
let ledWebsocketUrl = "http://localhost:3001";
|
let ledWebsocketUrl = "http://localhost:3001";
|
||||||
|
|
||||||
|
let debugstr = "";
|
||||||
|
|
||||||
onMount(async () => {
|
onMount(async () => {
|
||||||
|
// console.log(emit, listen);
|
||||||
await listen("showConfig", (event) => {
|
await listen("showConfig", (event) => {
|
||||||
const payload: any = event.payload;
|
console.log("heartbeat", event);
|
||||||
|
debugstr = event.payload;
|
||||||
|
const payload: any = JSON.parse(event.payload as any);
|
||||||
deviceMode = payload.deviceMode;
|
deviceMode = payload.deviceMode;
|
||||||
outputMode = payload.outputMode;
|
outputMode = payload.outputMode;
|
||||||
ledMode = payload.ledMode;
|
ledMode = payload.ledMode;
|
||||||
|
@ -24,6 +29,7 @@
|
||||||
ledSensitivity = payload.ledSensitivity;
|
ledSensitivity = payload.ledSensitivity;
|
||||||
ledWebsocketUrl = payload.ledWebsocketUrl;
|
ledWebsocketUrl = payload.ledWebsocketUrl;
|
||||||
});
|
});
|
||||||
|
await emit("heartbeat", "");
|
||||||
});
|
});
|
||||||
|
|
||||||
async function setConfig() {
|
async function setConfig() {
|
||||||
|
@ -59,6 +65,9 @@
|
||||||
slidershim
|
slidershim
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
<!-- <div>
|
||||||
|
{debugstr}
|
||||||
|
</div> -->
|
||||||
<div class="row">
|
<div class="row">
|
||||||
<Preview />
|
<Preview />
|
||||||
</div>
|
</div>
|
||||||
|
@ -145,7 +154,7 @@
|
||||||
min="1"
|
min="1"
|
||||||
max="255"
|
max="255"
|
||||||
step="1"
|
step="1"
|
||||||
bind:value={keyboardSensitivity}
|
bind:value={ledSensitivity}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
@ -157,7 +166,7 @@
|
||||||
min="1"
|
min="1"
|
||||||
max="255"
|
max="255"
|
||||||
step="1"
|
step="1"
|
||||||
bind:value={keyboardSensitivity}
|
bind:value={ledSensitivity}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
Loading…
Reference in New Issue