@@ -40,6 +40,12 @@ impl RandrManager { |
| 40 | 40 | #[allow(dead_code)] // Used for mode selection UI |
| 41 | 41 | pub fn get_outputs(&self) -> Result<Vec<OutputInfo>> { |
| 42 | 42 | let resources = self.get_resources()?; |
| 43 | + tracing::debug!( |
| 44 | + "get_outputs: resources has {} modes available, {} outputs", |
| 45 | + resources.modes.len(), |
| 46 | + resources.outputs.len() |
| 47 | + ); |
| 48 | + |
| 43 | 49 | let mut outputs = Vec::new(); |
| 44 | 50 | |
| 45 | 51 | for &output in &resources.outputs { |
@@ -52,13 +58,27 @@ impl RandrManager { |
| 52 | 58 | let name = String::from_utf8_lossy(&info.name).to_string(); |
| 53 | 59 | let connected = info.connection == randr::Connection::CONNECTED; |
| 54 | 60 | |
| 61 | + tracing::debug!( |
| 62 | + " output '{}': {} mode IDs in info.modes", |
| 63 | + name, |
| 64 | + info.modes.len() |
| 65 | + ); |
| 66 | + |
| 55 | 67 | // Get available modes |
| 56 | 68 | let modes: Vec<ModeInfo> = info |
| 57 | 69 | .modes |
| 58 | 70 | .iter() |
| 59 | | - .filter_map(|&mode_id| self.get_mode_info(&resources, mode_id)) |
| 71 | + .filter_map(|&mode_id| { |
| 72 | + let result = self.get_mode_info(&resources, mode_id); |
| 73 | + if result.is_none() { |
| 74 | + tracing::debug!(" mode_id {} not found in resources.modes", mode_id); |
| 75 | + } |
| 76 | + result |
| 77 | + }) |
| 60 | 78 | .collect(); |
| 61 | 79 | |
| 80 | + tracing::debug!(" resolved {} modes for '{}'", modes.len(), name); |
| 81 | + |
| 62 | 82 | // Get current mode and position if CRTC is set |
| 63 | 83 | let (crtc, current_mode, position) = if info.crtc != 0 { |
| 64 | 84 | let crtc_info = self |
@@ -167,6 +187,156 @@ impl RandrManager { |
| 167 | 187 | Err(RandrError::OutputNotFound(name.to_string())) |
| 168 | 188 | } |
| 169 | 189 | |
| 190 | + /// Ensure the virtual screen is large enough to contain the given bounds. |
| 191 | + /// This must be called before applying configurations that might exceed the current screen size. |
| 192 | + pub fn ensure_screen_size(&self, required_width: u32, required_height: u32) -> Result<()> { |
| 193 | + let _resources = self.get_resources()?; |
| 194 | + |
| 195 | + // Get current screen info |
| 196 | + let screen_info = self |
| 197 | + .conn |
| 198 | + .inner() |
| 199 | + .randr_get_screen_info(self.root)? |
| 200 | + .reply()?; |
| 201 | + |
| 202 | + let current_size = screen_info |
| 203 | + .sizes |
| 204 | + .get(screen_info.size_id as usize) |
| 205 | + .map(|s| (s.width as u32, s.height as u32)) |
| 206 | + .unwrap_or((0, 0)); |
| 207 | + |
| 208 | + tracing::debug!( |
| 209 | + "ensure_screen_size: current={}x{}, required={}x{}", |
| 210 | + current_size.0, |
| 211 | + current_size.1, |
| 212 | + required_width, |
| 213 | + required_height |
| 214 | + ); |
| 215 | + |
| 216 | + // Only resize if needed |
| 217 | + if current_size.0 >= required_width && current_size.1 >= required_height { |
| 218 | + return Ok(()); |
| 219 | + } |
| 220 | + |
| 221 | + let new_width = required_width.max(current_size.0); |
| 222 | + let new_height = required_height.max(current_size.1); |
| 223 | + |
| 224 | + // Calculate physical size in mm (approximate based on 96 DPI) |
| 225 | + let mm_width = (new_width as f64 * 25.4 / 96.0) as u32; |
| 226 | + let mm_height = (new_height as f64 * 25.4 / 96.0) as u32; |
| 227 | + |
| 228 | + tracing::info!( |
| 229 | + "resizing virtual screen to {}x{} ({}x{}mm)", |
| 230 | + new_width, |
| 231 | + new_height, |
| 232 | + mm_width, |
| 233 | + mm_height |
| 234 | + ); |
| 235 | + |
| 236 | + self.conn.inner().randr_set_screen_size( |
| 237 | + self.root, |
| 238 | + new_width as u16, |
| 239 | + new_height as u16, |
| 240 | + mm_width, |
| 241 | + mm_height, |
| 242 | + )?; |
| 243 | + |
| 244 | + self.conn.inner().flush()?; |
| 245 | + Ok(()) |
| 246 | + } |
| 247 | + |
| 248 | + /// Calculate the effective dimensions after rotation. |
| 249 | + fn rotated_dimensions(width: u32, height: u32, rotation: u32) -> (u32, u32) { |
| 250 | + match rotation { |
| 251 | + 90 | 270 => (height, width), // Swap dimensions for 90/270 rotation |
| 252 | + _ => (width, height), // 0 or 180 keeps same dimensions |
| 253 | + } |
| 254 | + } |
| 255 | + |
| 256 | + /// Calculate the required screen size to contain all given monitor configurations. |
| 257 | + pub fn calculate_required_screen_size(configs: &[MonitorConfig]) -> (u32, u32) { |
| 258 | + let mut max_x = 0u32; |
| 259 | + let mut max_y = 0u32; |
| 260 | + |
| 261 | + for config in configs { |
| 262 | + if !config.enabled { |
| 263 | + continue; |
| 264 | + } |
| 265 | + |
| 266 | + let (eff_width, eff_height) = |
| 267 | + Self::rotated_dimensions(config.width, config.height, config.rotation); |
| 268 | + |
| 269 | + let right = config.x.max(0) as u32 + eff_width; |
| 270 | + let bottom = config.y.max(0) as u32 + eff_height; |
| 271 | + |
| 272 | + max_x = max_x.max(right); |
| 273 | + max_y = max_y.max(bottom); |
| 274 | + } |
| 275 | + |
| 276 | + // Ensure minimum screen size |
| 277 | + (max_x.max(320), max_y.max(200)) |
| 278 | + } |
| 279 | + |
| 280 | + /// Prepare the screen for a set of monitor configurations. |
| 281 | + /// This should be called before applying any configurations to ensure the screen is large enough. |
| 282 | + pub fn prepare_screen_for_configs(&self, configs: &[MonitorConfig]) -> Result<()> { |
| 283 | + let (required_width, required_height) = Self::calculate_required_screen_size(configs); |
| 284 | + tracing::info!( |
| 285 | + "preparing screen for {} monitors: required size {}x{}", |
| 286 | + configs.iter().filter(|c| c.enabled).count(), |
| 287 | + required_width, |
| 288 | + required_height |
| 289 | + ); |
| 290 | + self.ensure_screen_size(required_width, required_height) |
| 291 | + } |
| 292 | + |
| 293 | + /// Shrink the screen to the minimum size required for the current outputs. |
| 294 | + /// Call this after applying all configurations to clean up excess virtual screen space. |
| 295 | + pub fn shrink_screen_to_fit(&self) -> Result<()> { |
| 296 | + let outputs = self.get_outputs()?; |
| 297 | + |
| 298 | + let mut max_x = 0u32; |
| 299 | + let mut max_y = 0u32; |
| 300 | + |
| 301 | + for output in &outputs { |
| 302 | + if !output.connected { |
| 303 | + continue; |
| 304 | + } |
| 305 | + if let (Some(mode), Some(pos)) = (&output.current_mode, output.position) { |
| 306 | + let right = pos.0.max(0) as u32 + mode.width as u32; |
| 307 | + let bottom = pos.1.max(0) as u32 + mode.height as u32; |
| 308 | + max_x = max_x.max(right); |
| 309 | + max_y = max_y.max(bottom); |
| 310 | + } |
| 311 | + } |
| 312 | + |
| 313 | + // Ensure minimum size |
| 314 | + let target_width = max_x.max(320); |
| 315 | + let target_height = max_y.max(200); |
| 316 | + |
| 317 | + let mm_width = (target_width as f64 * 25.4 / 96.0) as u32; |
| 318 | + let mm_height = (target_height as f64 * 25.4 / 96.0) as u32; |
| 319 | + |
| 320 | + tracing::info!( |
| 321 | + "shrinking virtual screen to {}x{} ({}x{}mm)", |
| 322 | + target_width, |
| 323 | + target_height, |
| 324 | + mm_width, |
| 325 | + mm_height |
| 326 | + ); |
| 327 | + |
| 328 | + self.conn.inner().randr_set_screen_size( |
| 329 | + self.root, |
| 330 | + target_width as u16, |
| 331 | + target_height as u16, |
| 332 | + mm_width, |
| 333 | + mm_height, |
| 334 | + )?; |
| 335 | + |
| 336 | + self.conn.inner().flush()?; |
| 337 | + Ok(()) |
| 338 | + } |
| 339 | + |
| 170 | 340 | /// Apply a monitor configuration. |
| 171 | 341 | pub fn apply_monitor(&self, config: &MonitorConfig) -> Result<()> { |
| 172 | 342 | if !config.enabled { |
@@ -188,6 +358,32 @@ impl RandrManager { |
| 188 | 358 | // Find available CRTC |
| 189 | 359 | let crtc = self.find_available_crtc(&resources, &output_info, output)?; |
| 190 | 360 | |
| 361 | + // Calculate effective dimensions after rotation |
| 362 | + let (eff_width, eff_height) = |
| 363 | + Self::rotated_dimensions(config.width, config.height, config.rotation); |
| 364 | + |
| 365 | + // Calculate required screen size to contain this monitor |
| 366 | + let required_width = config.x as u32 + eff_width; |
| 367 | + let required_height = config.y as u32 + eff_height; |
| 368 | + |
| 369 | + tracing::debug!( |
| 370 | + "apply_monitor {}: {}x{} rot={} -> effective {}x{} at ({}, {})", |
| 371 | + config.name, |
| 372 | + config.width, |
| 373 | + config.height, |
| 374 | + config.rotation, |
| 375 | + eff_width, |
| 376 | + eff_height, |
| 377 | + config.x, |
| 378 | + config.y |
| 379 | + ); |
| 380 | + |
| 381 | + // Ensure screen is large enough BEFORE applying the CRTC config |
| 382 | + self.ensure_screen_size(required_width, required_height)?; |
| 383 | + |
| 384 | + // Re-fetch resources after screen resize (timestamps may have changed) |
| 385 | + let resources = self.get_resources()?; |
| 386 | + |
| 191 | 387 | // Convert rotation |
| 192 | 388 | let rotation = match config.rotation { |
| 193 | 389 | 90 => randr::Rotation::ROTATE90, |
@@ -220,10 +416,11 @@ impl RandrManager { |
| 220 | 416 | } |
| 221 | 417 | |
| 222 | 418 | tracing::info!( |
| 223 | | - "applied config for {}: {}x{} at ({}, {})", |
| 419 | + "applied config for {}: {}x{} rot={} at ({}, {})", |
| 224 | 420 | config.name, |
| 225 | 421 | config.width, |
| 226 | 422 | config.height, |
| 423 | + config.rotation, |
| 227 | 424 | config.x, |
| 228 | 425 | config.y |
| 229 | 426 | ); |