cathode/src-tauri/src/main.rs

231 lines
7.0 KiB
Rust
Raw Normal View History

2022-08-26 11:08:23 -04:00
#![cfg_attr(
all(not(debug_assertions), target_os = "windows"),
windows_subsystem = "windows"
)]
2022-09-03 14:21:41 -04:00
use std::{
2022-10-12 15:18:49 -04:00
path::{Path, PathBuf},
2022-09-03 14:21:41 -04:00
sync::{Arc, Mutex},
thread::sleep,
2022-09-03 14:21:41 -04:00
time::Duration,
};
2022-09-03 04:07:13 -04:00
use audio::monitor;
2022-10-12 15:18:49 -04:00
use fs::WebRay;
2022-10-05 14:10:18 -04:00
use log::{debug, error, trace, warn};
2022-09-17 12:45:26 -04:00
use serde_json::Value;
2022-10-05 14:10:18 -04:00
use tauri::{
api::path::{cache_dir, config_dir},
Manager,
State, //, WindowEvent,
2022-10-05 14:10:18 -04:00
};
use crate::config::Config;
2022-09-03 04:07:13 -04:00
mod audio;
2022-10-05 14:10:18 -04:00
mod config;
2022-09-04 04:12:10 -04:00
mod fs;
2022-09-03 04:07:13 -04:00
const MIC_THRESHOLD: f32 = 0.5f32;
struct MicThreshold(Arc<Mutex<f32>>);
2022-09-06 00:57:23 -04:00
struct AudioLevel(Arc<Mutex<f32>>);
struct BlinkInterval(Arc<Mutex<u64>>);
2022-10-05 14:10:18 -04:00
struct CurrentConfig(Arc<Mutex<Config>>);
2022-10-12 15:18:49 -04:00
struct RayToLoad(Arc<Mutex<Option<PathBuf>>>);
struct MicSense(Arc<Mutex<f32>>);
2022-09-25 19:11:32 -04:00
2022-08-26 11:08:23 -04:00
fn main() {
2022-09-03 14:21:41 -04:00
env_logger::init();
2022-09-03 04:07:13 -04:00
let threshold = Arc::new(Mutex::new(MIC_THRESHOLD));
2022-09-06 00:57:23 -04:00
let level = Arc::new(Mutex::new(0.));
let blink_interval = Arc::new(Mutex::new(1500));
2022-10-12 15:18:49 -04:00
let ray = Arc::new(Mutex::new(None));
let sens = Arc::new(Mutex::new(1.0));
2022-09-25 19:11:32 -04:00
if let Some(d) = cache_dir() {
use std::fs;
2022-10-12 10:34:19 -04:00
if let Ok(s) = fs::read_to_string(d.join("cathode").join("last_selected")) {
2022-09-25 19:11:32 -04:00
debug!("Found selected ray in cache");
2022-10-12 15:18:49 -04:00
*ray.lock().unwrap() = Some(Path::new(&s).to_path_buf());
2022-09-25 19:11:32 -04:00
}
}
2022-09-03 04:07:13 -04:00
2022-10-05 14:10:18 -04:00
// Create the config directory if needed
let config = config::load_config();
2022-10-12 10:34:19 -04:00
{
*blink_interval.lock().unwrap() = config.blink_interval;
*sens.lock().unwrap() = config.mic_sens;
2022-10-12 10:34:19 -04:00
}
2022-10-05 14:10:18 -04:00
let current_conf = Arc::new(Mutex::new(config));
let config = current_conf.clone();
// let c2 = current_conf.clone();
2022-08-26 11:08:23 -04:00
tauri::Builder::default()
2022-09-03 04:07:13 -04:00
.manage(MicThreshold(threshold.clone()))
2022-09-06 00:57:23 -04:00
.manage(AudioLevel(level.clone()))
.manage(BlinkInterval(blink_interval.clone()))
2022-10-05 14:10:18 -04:00
.manage(CurrentConfig(current_conf.clone()))
2022-10-12 15:18:49 -04:00
.manage(RayToLoad(ray.clone()))
.manage(MicSense(sens.clone()))
// .on_window_event(move |event| match event.event() {
// WindowEvent::CloseRequested { .. } => {
// debug!("close requested");
// if let Err(e) = config::save_config(&*c2.lock().unwrap()) {
// error!("Error writing config file: {}", e);
// }
// }
// _ => {}
// })
2022-10-12 15:18:49 -04:00
.setup(move |app| {
2022-09-03 04:07:13 -04:00
let window = app.get_window("main").unwrap();
tauri::async_runtime::spawn(async move {
monitor(window, threshold, level, sens).await;
2022-09-03 04:07:13 -04:00
});
2022-09-03 14:21:41 -04:00
let window = app.get_window("main").unwrap();
tauri::async_runtime::spawn(async move {
loop {
if rand::random() {
trace!("Blinking");
2022-10-12 15:18:49 -04:00
if let Some(e) = window.emit_all("blink", "").err() {
2022-09-03 14:21:41 -04:00
warn!("Failed to emit blink event: {}", e);
}
}
let blink = blink_interval.lock().unwrap();
sleep(Duration::from_millis(*blink));
2022-09-03 14:21:41 -04:00
}
});
2022-10-05 14:10:18 -04:00
let window = app.get_window("main").unwrap();
tauri::async_runtime::spawn(async move {
use notify::{event::AccessKind, Event, EventKind, RecursiveMode, Result, Watcher};
if let Some(path) = config_dir() {
let mut watcher =
notify::recommended_watcher(move |res: Result<Event>| match res {
Ok(event) => {
if let EventKind::Access(AccessKind::Close(_)) = event.kind {
*config.lock().unwrap() = config::load_config();
2022-10-12 15:18:49 -04:00
window.emit_all("reload-config", "").unwrap();
2022-10-05 14:10:18 -04:00
}
}
Err(e) => error!("error watching filesystem {:?}", e),
})
.unwrap();
watcher
.watch(
&path.join("cathode").join("config.toml"),
RecursiveMode::NonRecursive,
)
.unwrap();
loop {
sleep(Duration::from_millis(5000));
}
}
});
2022-10-12 15:18:49 -04:00
match app.get_cli_matches() {
Ok(matches) => {
trace!("matches OK");
if let Some(arg) = matches.args.get("file") {
trace!("CLI arg value: {:?}", arg);
if let Value::String(path) = arg.value.clone() {
*ray.lock().unwrap() = Some(PathBuf::from(path));
2022-09-25 19:11:32 -04:00
}
2022-10-12 15:18:49 -04:00
}
2022-09-25 19:11:32 -04:00
}
2022-10-12 15:18:49 -04:00
Err(e) => eprintln!("{}", e),
2022-09-17 12:45:26 -04:00
}
2022-09-03 04:07:13 -04:00
Ok(())
})
.invoke_handler(tauri::generate_handler![
2022-09-13 01:13:51 -04:00
log,
2022-09-03 04:07:13 -04:00
set_mic_threshold,
2022-09-04 04:12:10 -04:00
get_mic_threshold,
set_mic_sens,
get_mic_sens,
2022-09-06 00:57:23 -04:00
get_audio_level,
2022-10-12 10:34:19 -04:00
get_blink_interval,
set_blink_interval,
2022-10-05 14:10:18 -04:00
get_config,
set_config,
save_current_config,
2022-10-12 15:18:49 -04:00
get_ray_to_load,
2022-09-04 04:12:10 -04:00
fs::open_image,
2022-09-15 03:22:43 -04:00
fs::save_ray,
fs::open_ray,
audio::get_devices,
2022-09-03 04:07:13 -04:00
])
2022-08-26 11:08:23 -04:00
.run(tauri::generate_context!())
.expect("error while running tauri application");
}
2022-09-03 04:07:13 -04:00
2022-10-12 15:18:49 -04:00
#[tauri::command]
fn get_ray_to_load(ray: State<'_, RayToLoad>) -> Option<WebRay> {
let ray = { (*ray.0.lock().unwrap()).clone() };
if let Some(r) = ray {
let ray = fs::load_ray(r);
ray
} else {
None
}
}
2022-09-13 01:13:51 -04:00
#[tauri::command]
fn log(msg: String) {
2022-10-12 15:18:49 -04:00
debug!("frontend: {}", msg);
2022-09-13 01:13:51 -04:00
}
2022-09-03 04:07:13 -04:00
#[tauri::command]
fn set_mic_threshold(threshold: f32, current: State<'_, MicThreshold>) {
*current.0.lock().unwrap() = threshold;
}
#[tauri::command]
fn get_mic_threshold(current: State<'_, MicThreshold>) -> f32 {
*current.0.lock().unwrap()
}
2022-09-06 00:57:23 -04:00
#[tauri::command]
fn set_mic_sens(sens: f32, current: State<'_, MicSense>) {
*current.0.lock().unwrap() = sens;
}
#[tauri::command]
fn get_mic_sens(current: State<'_, MicSense>) -> f32 {
*current.0.lock().unwrap()
}
2022-09-06 00:57:23 -04:00
#[tauri::command]
fn get_audio_level(level: State<'_, AudioLevel>) -> f32 {
*level.0.lock().unwrap()
}
2022-10-05 14:10:18 -04:00
2022-10-12 10:34:19 -04:00
#[tauri::command]
fn get_blink_interval(current: State<'_, BlinkInterval>) -> u64 {
*current.0.lock().unwrap()
}
#[tauri::command]
fn set_blink_interval(value: u64, current: State<'_, BlinkInterval>) {
*current.0.lock().unwrap() = value;
}
2022-10-05 14:10:18 -04:00
#[tauri::command]
fn get_config(current: State<'_, CurrentConfig>) -> Config {
current.0.lock().unwrap().clone()
}
#[tauri::command]
fn set_config(config: Config, current: State<'_, CurrentConfig>) {
*current.0.lock().unwrap() = config
}
#[tauri::command]
fn save_current_config(current: State<'_, CurrentConfig>) -> Result<(), String> {
config::save_config(&*current.0.lock().unwrap()).map_err(|e| format!("{}", e))
}