scythe/src/bin/vrec-ui.rs

61 lines
2.6 KiB
Rust

use vrec::ipc::Command;
use std::os::unix::net::UnixStream;
use std::io::Write;
use std::env;
use global_hotkey::{GlobalHotKeyManager, hotkey::{HotKey, Modifiers, Code}, GlobalHotKeyEvent};
fn send_command(cmd: Command) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
let socket_path = format!("{}/vrec.sock", env::var("XDG_RUNTIME_DIR").unwrap_or_else(|_| "/tmp".to_string()));
let mut stream = UnixStream::connect(&socket_path)?;
let payload = serde_json::to_vec(&cmd)?;
let len_buf = (payload.len() as u32).to_le_bytes();
stream.write_all(&len_buf)?;
stream.write_all(&payload)?;
Ok(())
}
fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
println!("Starting vrec UI/Hotkey process...");
// Check session type
let session_type = env::var("XDG_SESSION_TYPE").unwrap_or_else(|_| "x11".to_string());
let desktop = env::var("XDG_CURRENT_DESKTOP").unwrap_or_else(|_| "".to_string()).to_lowercase();
let is_gnome = desktop.contains("gnome") || desktop.contains("ubuntu");
let is_kde = desktop.contains("kde");
if session_type.to_lowercase() == "wayland" {
if is_gnome || is_kde {
println!("WARNING: wlr-layer-shell unsupported on GNOME/KDE Wayland. Degrading to hotkey-only mode.");
println!("Note: Global hotkeys may also fail to register on some Wayland compositors.");
} else {
println!("wlroots-based compositor detected. (wlr-layer-shell overlay would be spawned here).");
}
} else {
println!("X11 session detected. (X11 override-redirect overlay would be spawned here).");
}
let manager = GlobalHotKeyManager::new()?;
let hotkey = HotKey::new(Some(Modifiers::CONTROL | Modifiers::SHIFT), Code::KeyR);
if let Err(e) = manager.register(hotkey) {
eprintln!("Failed to register global hotkey! Wayland compositor might be blocking it: {}", e);
eprintln!("Fallback: running without global hotkey. Send commands via socket manually.");
} else {
println!("Registered global hotkey Ctrl+Shift+R to trigger replay save.");
}
let receiver = GlobalHotKeyEvent::receiver();
loop {
if let Ok(event) = receiver.recv() {
if event.id == hotkey.id() {
if event.state == global_hotkey::HotKeyState::Pressed {
println!("Hotkey triggered! Sending SaveReplay command to daemon...");
if let Err(e) = send_command(Command::SaveReplay) {
eprintln!("Failed to communicate with daemon: {}", e);
}
}
}
}
}
}