scythe/scratch/daemon.patch

82 lines
2.9 KiB
Diff

--- src/bin/vrec-daemon.rs
+++ src/bin/vrec-daemon.rs
@@ -1,13 +1,6 @@
-pub mod capture;
-pub mod encoder;
-pub mod ring;
-pub mod muxer;
-pub mod hotkey;
-pub mod debug;
-
-use capture::{Frame, FrameSource};
-use encoder::VaapiEncoder;
-use ring::Packet;
-use muxer::Muxer;
+use vrec::capture::{Frame, FrameSource};
+use vrec::encoder::VaapiEncoder;
+use vrec::ring::Packet;
+use vrec::muxer::Muxer;
+use vrec::ipc::Command;
use ringbuf::HeapRb;
use ringbuf::traits::{RingBuffer, Consumer};
use crossbeam_channel::bounded;
@@ -15,6 +8,9 @@
use std::env;
+use std::os::unix::net::UnixListener;
+use std::io::Read;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let session_type = env::var("XDG_SESSION_TYPE").unwrap_or_else(|_| "x11".to_string());
@@ -23,10 +19,10 @@
let mut source: Box<dyn FrameSource> = if session_type.to_lowercase() == "wayland" {
println!("Initializing Wayland capture...");
- Box::new(capture::wayland::WaylandCapture::new().await?)
+ Box::new(vrec::capture::wayland::WaylandCapture::new().await?)
} else {
println!("Initializing X11 capture...");
- Box::new(capture::x11::X11Capture::new()?)
+ Box::new(vrec::capture::x11::X11Capture::new()?)
};
@@ -118,10 +114,35 @@
});
- // 5. Hotkey listener
- println!("Starting hotkey listener...");
- if let Err(e) = hotkey::run_hotkey_listener(trigger_tx) {
- eprintln!("Failed to start hotkey listener: {}", e);
+ // 5. IPC Unix Socket Listener
+ let socket_path = format!("{}/vrec.sock", env::var("XDG_RUNTIME_DIR").unwrap_or_else(|_| "/tmp".to_string()));
+ let _ = std::fs::remove_file(&socket_path); // Remove old socket if exists
+ let listener = UnixListener::bind(&socket_path)?;
+ println!("Daemon listening on IPC socket: {}", socket_path);
+
+ for stream in listener.incoming() {
+ match stream {
+ Ok(mut stream) => {
+ let mut len_buf = [0u8; 4];
+ if stream.read_exact(&mut len_buf).is_ok() {
+ let len = u32::from_le_bytes(len_buf) as usize;
+ let mut payload = vec![0u8; len];
+ if stream.read_exact(&mut payload).is_ok() {
+ if let Ok(cmd) = serde_json::from_slice::<Command>(&payload) {
+ match cmd {
+ Command::SaveReplay => {
+ let _ = trigger_tx.try_send(());
+ },
+ Command::StopDaemon => {
+ break;
+ },
+ _ => {}
+ }
+ }
+ }
+ }
+ }
+ Err(err) => {
+ eprintln!("Socket error: {}", err);
+ }
+ }
}