@@ -0,0 +1,163 @@ |
| 1 | +//! Watchdog process for auto-reverting display changes. |
| 2 | +//! |
| 3 | +//! This module spawns a separate process that will revert display changes |
| 4 | +//! if not canceled within a timeout. This is more robust than relying on the |
| 5 | +//! main process's event loop, which may crash or lose its X connection when |
| 6 | +//! display settings change. |
| 7 | + |
| 8 | +use std::fs; |
| 9 | +use std::io::Write; |
| 10 | +use std::path::PathBuf; |
| 11 | +use std::process::{Child, Command, Stdio}; |
| 12 | + |
| 13 | +use crate::config::MonitorConfig; |
| 14 | + |
| 15 | +/// Timeout in seconds for auto-revert. |
| 16 | +const REVERT_TIMEOUT_SECS: u32 = 15; |
| 17 | + |
| 18 | +/// Get the path to the revert config file. |
| 19 | +fn revert_config_path() -> PathBuf { |
| 20 | + let runtime_dir = std::env::var("XDG_RUNTIME_DIR").unwrap_or_else(|_| "/tmp".to_string()); |
| 21 | + PathBuf::from(runtime_dir).join("gardisplay-revert.json") |
| 22 | +} |
| 23 | + |
| 24 | +/// Start the watchdog process that will revert display settings after timeout. |
| 25 | +/// Returns the child process handle if successful. |
| 26 | +pub fn start_watchdog(configs: &[MonitorConfig]) -> std::io::Result<Child> { |
| 27 | + let config_path = revert_config_path(); |
| 28 | + |
| 29 | + // Serialize the revert config to JSON |
| 30 | + let json = serde_json::to_string(configs).map_err(|e| { |
| 31 | + std::io::Error::new(std::io::ErrorKind::InvalidData, e.to_string()) |
| 32 | + })?; |
| 33 | + |
| 34 | + // Write to temp file |
| 35 | + let mut file = fs::File::create(&config_path)?; |
| 36 | + file.write_all(json.as_bytes())?; |
| 37 | + file.sync_all()?; |
| 38 | + |
| 39 | + tracing::info!( |
| 40 | + "watchdog: wrote revert config to {:?} ({} monitors)", |
| 41 | + config_path, |
| 42 | + configs.len() |
| 43 | + ); |
| 44 | + |
| 45 | + // Get the DISPLAY environment variable |
| 46 | + let display = std::env::var("DISPLAY").unwrap_or_else(|_| ":0".to_string()); |
| 47 | + |
| 48 | + // Spawn the watchdog script as a detached process |
| 49 | + // We use bash to create a simple watchdog that: |
| 50 | + // 1. Sleeps for the timeout |
| 51 | + // 2. Checks if the config file still exists |
| 52 | + // 3. If so, applies xrandr commands to revert |
| 53 | + // 4. Deletes the config file |
| 54 | + let script = format!( |
| 55 | + r#" |
| 56 | + sleep {timeout} |
| 57 | + if [ -f "{config_path}" ]; then |
| 58 | + echo "gardisplay watchdog: reverting display settings..." |
| 59 | + {xrandr_commands} |
| 60 | + rm -f "{config_path}" |
| 61 | + echo "gardisplay watchdog: revert complete" |
| 62 | + fi |
| 63 | + "#, |
| 64 | + timeout = REVERT_TIMEOUT_SECS, |
| 65 | + config_path = config_path.display(), |
| 66 | + xrandr_commands = generate_xrandr_commands(configs), |
| 67 | + ); |
| 68 | + |
| 69 | + tracing::debug!("watchdog script:\n{}", script); |
| 70 | + |
| 71 | + let child = Command::new("bash") |
| 72 | + .arg("-c") |
| 73 | + .arg(&script) |
| 74 | + .env("DISPLAY", display) |
| 75 | + .stdin(Stdio::null()) |
| 76 | + .stdout(Stdio::null()) |
| 77 | + .stderr(Stdio::null()) |
| 78 | + .spawn()?; |
| 79 | + |
| 80 | + tracing::info!("watchdog: started with PID {}", child.id()); |
| 81 | + Ok(child) |
| 82 | +} |
| 83 | + |
| 84 | +/// Cancel the watchdog by deleting the config file. |
| 85 | +/// The watchdog process will check for this and exit without reverting. |
| 86 | +pub fn cancel_watchdog() { |
| 87 | + let config_path = revert_config_path(); |
| 88 | + if config_path.exists() { |
| 89 | + if let Err(e) = fs::remove_file(&config_path) { |
| 90 | + tracing::warn!("watchdog: failed to remove config file: {}", e); |
| 91 | + } else { |
| 92 | + tracing::info!("watchdog: canceled (removed config file)"); |
| 93 | + } |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +/// Generate xrandr commands to restore the given configurations. |
| 98 | +fn generate_xrandr_commands(configs: &[MonitorConfig]) -> String { |
| 99 | + let mut commands = Vec::new(); |
| 100 | + |
| 101 | + for config in configs { |
| 102 | + if !config.enabled { |
| 103 | + commands.push(format!("xrandr --output {} --off", config.name)); |
| 104 | + continue; |
| 105 | + } |
| 106 | + |
| 107 | + let rotation = match config.rotation { |
| 108 | + 90 => "left", |
| 109 | + 180 => "inverted", |
| 110 | + 270 => "right", |
| 111 | + _ => "normal", |
| 112 | + }; |
| 113 | + |
| 114 | + commands.push(format!( |
| 115 | + "xrandr --output {} --mode {}x{} --pos {}x{} --rotate {}", |
| 116 | + config.name, |
| 117 | + config.width, |
| 118 | + config.height, |
| 119 | + config.x, |
| 120 | + config.y, |
| 121 | + rotation |
| 122 | + )); |
| 123 | + } |
| 124 | + |
| 125 | + commands.join("\n ") |
| 126 | +} |
| 127 | + |
| 128 | +#[cfg(test)] |
| 129 | +mod tests { |
| 130 | + use super::*; |
| 131 | + |
| 132 | + #[test] |
| 133 | + fn test_generate_xrandr_commands() { |
| 134 | + let configs = vec![ |
| 135 | + MonitorConfig { |
| 136 | + name: "eDP-1".to_string(), |
| 137 | + enabled: true, |
| 138 | + x: 0, |
| 139 | + y: 0, |
| 140 | + width: 2880, |
| 141 | + height: 1800, |
| 142 | + refresh: 60.0, |
| 143 | + scale: 1.0, |
| 144 | + rotation: 0, |
| 145 | + }, |
| 146 | + MonitorConfig { |
| 147 | + name: "HDMI-1".to_string(), |
| 148 | + enabled: true, |
| 149 | + x: 2880, |
| 150 | + y: 0, |
| 151 | + width: 1920, |
| 152 | + height: 1080, |
| 153 | + refresh: 60.0, |
| 154 | + scale: 1.0, |
| 155 | + rotation: 90, |
| 156 | + }, |
| 157 | + ]; |
| 158 | + |
| 159 | + let commands = generate_xrandr_commands(&configs); |
| 160 | + assert!(commands.contains("xrandr --output eDP-1 --mode 2880x1800 --pos 0x0 --rotate normal")); |
| 161 | + assert!(commands.contains("xrandr --output HDMI-1 --mode 1920x1080 --pos 2880x0 --rotate left")); |
| 162 | + } |
| 163 | +} |