You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

100 lines
3.0 KiB

use dotenv::dotenv;
use std::env;
use rosc::{encoder, OscMessage, OscPacket, OscType};
use std::{net::SocketAddrV4, str::FromStr};
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};
use tauri::{AppHandle, Manager};
#[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::<Vec<u16>>();
webview
.with_webview(move |webview| unsafe {
let core = webview.controller().CoreWebView2().unwrap();
let core = Interface::cast::<ICoreWebView2_13>(&core).unwrap();
let profile = core.Profile().unwrap();
let profile = Interface::cast::<ICoreWebView2Profile4>(&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()
.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");
}