use dotenv::dotenv; use rosc::{encoder, OscMessage, OscPacket, OscType}; use std::env; use std::{net::SocketAddrV4, str::FromStr}; use tauri::{AppHandle, Manager}; use tokio::net::UdpSocket; use webview2_com::Microsoft::Web::WebView2::Win32::{ ICoreWebView2Profile4, ICoreWebView2_13, COREWEBVIEW2_PERMISSION_KIND_MICROPHONE, COREWEBVIEW2_PERMISSION_STATE_DEFAULT, }; use windows::core::{Interface, PCWSTR}; #[tauri::command] fn get_env(name: &str) -> String { println!("Getting environment variable: {}", name); match env::var(name) { Ok(value) => { // println!("Found environment variable {}: {}", name, value); value } Err(e) => { println!("Error getting environment variable {}: {}", name, e); String::new() } } } #[tauri::command] async fn send_osc_message( key: &str, message: &str, host: &str, target: &str, ) -> Result<(), String> { // print println!("Sending OSC message: {}", message); let sock = UdpSocket::bind(host).await.unwrap(); let remote = SocketAddrV4::from_str(target).unwrap(); let msg_buf = encoder::encode(&OscPacket::Message(OscMessage { addr: key.to_string(), args: vec![OscType::String(message.parse().unwrap())], })) .unwrap(); sock.send_to(&msg_buf, remote).await.unwrap(); Ok(()) } #[tauri::command] fn reset_permission(origin: &str, app: AppHandle) { let webview = app.get_webview_window("main").unwrap(); let mut origin = origin.to_string(); origin.push('\0'); let origin = origin.encode_utf16().collect::>(); webview .with_webview(move |webview| unsafe { let core = webview.controller().CoreWebView2().unwrap(); let core = Interface::cast::(&core).unwrap(); let profile = core.Profile().unwrap(); let profile = Interface::cast::(&profile).unwrap(); let origin = PCWSTR::from_raw(origin.as_ptr()); profile .SetPermissionState( COREWEBVIEW2_PERMISSION_KIND_MICROPHONE, origin, COREWEBVIEW2_PERMISSION_STATE_DEFAULT, None, ) .unwrap(); }) .unwrap(); } #[cfg_attr(mobile, tauri::mobile_entry_point)] pub fn run() { dotenv().ok(); tauri::Builder::default() .plugin(tauri_plugin_opener::init()) .plugin(tauri_plugin_fs::init()) .invoke_handler(tauri::generate_handler![ get_env, send_osc_message, reset_permission ]) .plugin(tauri_plugin_http::init()) .setup(|app| { if cfg!(debug_assertions) { app.handle().plugin( tauri_plugin_log::Builder::default() .level(log::LevelFilter::Info) .build(), )?; } Ok(()) }) .run(tauri::generate_context!()) .expect("error while running tauri application"); }