From 2492b9fc738621d2779e9a78f4cbff805e3319e2 Mon Sep 17 00:00:00 2001 From: 0xFFF42 Date: Wed, 24 Jan 2024 02:39:20 +0530 Subject: [PATCH 1/2] Added hotkeys for start and stop recording --- src-tauri/Cargo.toml | 3 +- src-tauri/src/main.rs | 65 ++++++++++++++++++++++++++++++--------- src-tauri/tauri.conf.json | 3 ++ 3 files changed, 54 insertions(+), 17 deletions(-) diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.toml index 68beb69..687a1ff 100644 --- a/src-tauri/Cargo.toml +++ b/src-tauri/Cargo.toml @@ -15,7 +15,7 @@ tauri-build = { version = "1.5.0", features = [] } [dependencies] serde_json = "1.0" serde = { version = "1.0", features = ["derive"] } -tauri = { version = "1.5.1", features = [ "shell-open", "system-tray", "devtools"] } +tauri = { version = "1.5.1", features = [ "global-shortcut-all", "shell-open", "system-tray", "devtools"] } window-shadows = "0.2.1" tauri-plugin-positioner = { version = "1.0.4", features = ["system-tray"] } win_desktop_duplication = { path = "../crates/win_desktop_duplication" } @@ -31,7 +31,6 @@ tauri-plugin-log = { git = "https://github.com/tauri-apps/plugins-workspace", br log = "^0.4" anyhow = "1.0.75" ndarray = { version = "0.15" } - [dependencies.windows] version = "0.39.0" features = ["Win32_System_Com"] diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index 4ae1c95..3e2a68a 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -24,6 +24,7 @@ use video_rs::{Encoder, EncoderSettings, Locator, Options, PixelFormat, RawFrame use anyhow::Result; use ndarray::Array3; use std::collections::HashMap; +use std::sync::{Arc, Mutex}; use tauri_plugin_store::StoreBuilder; use tauri_plugin_log::{LogTarget}; @@ -54,7 +55,7 @@ fn encode_frames(output_directory: &str, frames: Vec>, width: u32, heigh Ok(()) } -async fn capture_frames(dupl: &mut DesktopDuplicationApi, x: u32, y: u32, width: u32, height: u32, fps: u32) -> Result>> { +async fn capture_frames(dupl: &mut DesktopDuplicationApi, x: u32, y: u32, width: u32, height: u32, fps: u32, recording: Arc>) -> Result>> { let (device, ctx) = dupl.get_device_and_ctx(); let mut texture_reader = TextureReader::new(device, ctx); let mut frames: Vec> = vec![]; @@ -62,7 +63,7 @@ async fn capture_frames(dupl: &mut DesktopDuplicationApi, x: u32, y: u32, width: let mut last_display_time = Duration::new(0, 0); let start_time = Instant::now(); let mut next_frame_time = start_time + frame_interval; - while Instant::now() - start_time < Duration::from_secs(10) { + while !*recording.lock().unwrap() { let tex = dupl.acquire_next_vsync_frame().await; if Instant::now() >= next_frame_time && tex.is_ok() { let mut pic_data: Vec = vec![0; (width * height * 4) as usize]; @@ -74,7 +75,7 @@ async fn capture_frames(dupl: &mut DesktopDuplicationApi, x: u32, y: u32, width: Ok(frames) } -async fn capture_video(x: u32, y: u32, width: u32, height: u32, fps_store: u32, crf_store: u32, output_store: &str, app: &AppHandle) { +async fn capture_video(x: u32, y: u32, width: u32, height: u32, fps_store: u32, crf_store: u32, output_store: &str, app: &AppHandle, state: Arc>) { let width = if width % 2 != 0 { width - 1 } else { width }; let height = if height % 2 != 0 { height - 1 } else { height }; set_process_dpi_awareness(); @@ -83,7 +84,7 @@ async fn capture_video(x: u32, y: u32, width: u32, height: u32, fps_store: u32, let output = adapter.get_display_by_idx(0).unwrap(); let display_mode = output.get_current_display_mode().unwrap(); let mut dupl = DesktopDuplicationApi::new(adapter, output).unwrap(); - match capture_frames(&mut dupl, x, y, width, height, fps_store).await { + match capture_frames(&mut dupl, x, y, width, height, fps_store, state).await { Ok(frames) => { match encode_frames(output_store, frames, width, height, fps_store, crf_store) { Ok(()) => { @@ -99,13 +100,13 @@ async fn capture_video(x: u32, y: u32, width: u32, height: u32, fps_store: u32, } #[tauri::command] -fn video_capture(x: u32, y: u32, width: u32, height: u32, fps: u32, crf: u32, output: &str, app_handle: tauri::AppHandle) { +fn video_capture(x: u32, y: u32, width: u32, height: u32, fps: u32, crf: u32, output: &str, app_handle: tauri::AppHandle, state: tauri::State) { let output_store_cloned = output.to_string(); - println!("X {}, Y {}, WIDTH {}, HEIGHT {}, FPS {}, CRF {}, OUTPUT PATH {}", x,y,width,height,fps,crf,&output_store_cloned); + let recording = state.recording.clone(); tauri::async_runtime::spawn(async move { - capture_video(x, y, width, height, fps, crf, &output_store_cloned, &app_handle).await; + capture_video(x, y, width, height, fps, crf, &output_store_cloned, &app_handle, recording).await; }); } @@ -155,7 +156,7 @@ fn region_quick_capture(app: &AppHandle) { window.emit("quickshot-data", base64_string).unwrap(); } -fn region_video_capture(app: &AppHandle) { +fn region_video_capture(app: &AppHandle) { let (pos_x, pos_y) = mouse_location(); println!("{:?},{:?} ", pos_x, pos_y); let display_info = DisplayInfo::from_point(pos_x as i32, pos_y as i32).unwrap(); @@ -210,7 +211,13 @@ fn tray_events(app: &AppHandle, event: SystemTrayEvent) { } } +#[derive(Clone)] +struct AppState { + recording: Arc>, +} + fn main() { + let sub_menu_capture = { let mut menu = SystemTrayMenu::new(); menu = menu @@ -228,7 +235,15 @@ fn main() { let tray = SystemTray::new().with_menu(tray_menu); + // Create a Mutex of hashmaps for containg hotkeys and their respective states + // Spawn a thread with Hotkeys crate and make changes to hotkeys + // Find a way to access the hashmap from the tuari thread + + let app_state = AppState { + recording: Arc::new(Mutex::new(false)) + }; tauri::Builder::default() + .manage(app_state.clone()) .setup(|app| { let default_settings = HashMap::from([ @@ -240,8 +255,9 @@ fn main() { .defaults(default_settings) .build(); - let app_handle = app.handle(); + let app_handle = app.handle(); + std::thread::spawn(move || { app_handle.plugin(tauri_plugin_store::Builder::default() .stores([settings]) @@ -266,6 +282,7 @@ fn main() { .disable_file_drop_handler() .build() .unwrap(); + region_select_window.open_devtools(); let excalidraw_window = tauri::WindowBuilder::new( app, @@ -283,8 +300,7 @@ fn main() { .disable_file_drop_handler() .build() .unwrap(); - - + Ok(()) }) .invoke_handler(tauri::generate_handler![video_capture]) @@ -306,19 +322,38 @@ fn main() { }) .build(tauri::generate_context!()) .expect("error while running tauri application") - .run(|app_handle, event| match event { + .run(move |app_handle, event|{ + match event { RunEvent::Ready => { let app_handle = app_handle.clone(); + let app_handle2 = app_handle.clone(); + let app_statei = app_state.clone(); app_handle .global_shortcut_manager() - .register("PrintScreen", move || { + .register("CommandOrControl+Shift+D", move || { region_selection_window(&app_handle); - }) // Handle hotkey register error, else it will panic + println!("FIRED"); + }); // Handle hotkey register error, else it will panic + app_handle2 + .global_shortcut_manager() + .register("CommandOrControl+Shift+R", move || { + let mut a=app_statei.recording.lock().unwrap(); + if *a { + *a = !*a; + } else { + region_video_capture(&app_handle2); + *a=true; + } + }) .unwrap(); + print!("Ready and registered"); } tauri::RunEvent::ExitRequested { api, .. } => { api.prevent_exit(); } _ => {} + } }); -} \ No newline at end of file + + print!("This is text"); +} diff --git a/src-tauri/tauri.conf.json b/src-tauri/tauri.conf.json index ed96bb5..e4a9a30 100644 --- a/src-tauri/tauri.conf.json +++ b/src-tauri/tauri.conf.json @@ -21,6 +21,9 @@ "shell": { "all": false, "open": true + }, + "globalShortcut": { + "all": true } }, "bundle": { From 31224aa7f255d70cd5cd5b5ef35ee41512ee73df Mon Sep 17 00:00:00 2001 From: manthan Date: Thu, 25 Jan 2024 00:18:37 +0530 Subject: [PATCH 2/2] cleanup --- src-tauri/src/main.rs | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index 435c107..2b5e155 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -237,10 +237,6 @@ fn main() { let tray = SystemTray::new().with_menu(tray_menu); - // Create a Mutex of hashmaps for containg hotkeys and their respective states - // Spawn a thread with Hotkeys crate and make changes to hotkeys - // Find a way to access the hashmap from the tuari thread - let app_state = AppState { recording: Arc::new(Mutex::new(false)) };