Make output modes configurable
Browse files at this commitbb3fbe2e83e5173b7e0df40908280aec02dc1a4bParent: ef76b1d3 changed files +157 -11
- resources/default-config.kdl +8 -0
- src/backend/tty.rs +72 -11
- src/config.rs +77 -0
resources/default-config.kdlmodified+8 -0
| @@ -34,6 +34,14 @@ input { | |||
| 34 | // Scale is a floating-point number, but at the moment only integer values work. | 34 | // Scale is a floating-point number, but at the moment only integer values work. |
| 35 | scale 2.0 | 35 | scale 2.0 |
| 36 | 36 | ||
| 37 | + // Resolution and, optionally, refresh rate of the output. | ||
| 38 | + // The format is "<width>x<height>" or "<width>x<height>@<refresh rate>". | ||
| 39 | + // If the refresh rate is omitted, niri will pick the highest refresh rate | ||
| 40 | + // for the resolution. | ||
| 41 | + // If the mode is omitted altogether or is invalid, niri will pick one automatically. | ||
| 42 | + // All valid modes are listed in niri's debug output when an output is connected. | ||
| 43 | + mode "1920x1080@144" | ||
| 44 | + | ||
| 37 | // Position of the output in the global coordinate space. | 45 | // Position of the output in the global coordinate space. |
| 38 | // This affects directional monitor actions like "focus-monitor-left", and cursor movement. | 46 | // This affects directional monitor actions like "focus-monitor-left", and cursor movement. |
| 39 | // The cursor can only move between directly adjacent outputs. | 47 | // The cursor can only move between directly adjacent outputs. |
src/backend/tty.rsmodified+72 -11
| @@ -434,20 +434,81 @@ impl Tty { | |||
| 434 | .as_mut() | 434 | .as_mut() |
| 435 | .context("missing output device")?; | 435 | .context("missing output device")?; |
| 436 | 436 | ||
| 437 | - let mut mode = connector.modes().get(0); | 437 | + // FIXME: print modes here until we have a better way to list all modes. |
| 438 | - connector.modes().iter().for_each(|m| { | 438 | + for m in connector.modes() { |
| 439 | - trace!("mode: {m:?}"); | 439 | + let wl_mode = Mode::from(*m); |
| 440 | - | 440 | + debug!( |
| 441 | - if m.mode_type().contains(ModeTypeFlags::PREFERRED) { | 441 | + "mode: {}x{}@{:.3}", |
| 442 | - // Pick the highest refresh rate. | 442 | + m.size().0, |
| 443 | - if mode | 443 | + m.size().1, |
| 444 | - .map(|curr| curr.vrefresh() < m.vrefresh()) | 444 | + wl_mode.refresh as f64 / 1000., |
| 445 | - .unwrap_or(true) | 445 | + ); |
| 446 | - { | 446 | + |
| 447 | + trace!("{m:?}"); | ||
| 448 | + } | ||
| 449 | + | ||
| 450 | + let mut mode = None; | ||
| 451 | + | ||
| 452 | + if let Some(target) = &config.mode { | ||
| 453 | + let refresh = target.refresh.map(|r| (r * 1000.).round() as i32); | ||
| 454 | + | ||
| 455 | + for m in connector.modes() { | ||
| 456 | + if m.size() != (target.width, target.height) { | ||
| 457 | + continue; | ||
| 458 | + } | ||
| 459 | + | ||
| 460 | + if let Some(refresh) = refresh { | ||
| 461 | + // If refresh is set, only pick modes with matching refresh. | ||
| 462 | + let wl_mode = Mode::from(*m); | ||
| 463 | + if wl_mode.refresh == refresh { | ||
| 464 | + mode = Some(m); | ||
| 465 | + } | ||
| 466 | + } else if let Some(curr) = mode { | ||
| 467 | + // If refresh isn't set, pick the mode with the highest refresh. | ||
| 468 | + if curr.vrefresh() < m.vrefresh() { | ||
| 469 | + mode = Some(m); | ||
| 470 | + } | ||
| 471 | + } else { | ||
| 447 | mode = Some(m); | 472 | mode = Some(m); |
| 448 | } | 473 | } |
| 449 | } | 474 | } |
| 450 | - }); | 475 | + |
| 476 | + if mode.is_none() { | ||
| 477 | + warn!( | ||
| 478 | + "configured mode {}x{}{} could not be found, falling back to preferred", | ||
| 479 | + target.width, | ||
| 480 | + target.height, | ||
| 481 | + if let Some(refresh) = target.refresh { | ||
| 482 | + format!("@{refresh}") | ||
| 483 | + } else { | ||
| 484 | + String::new() | ||
| 485 | + }, | ||
| 486 | + ); | ||
| 487 | + } | ||
| 488 | + } | ||
| 489 | + | ||
| 490 | + if mode.is_none() { | ||
| 491 | + // Pick a preferred mode. | ||
| 492 | + for m in connector.modes() { | ||
| 493 | + if !m.mode_type().contains(ModeTypeFlags::PREFERRED) { | ||
| 494 | + continue; | ||
| 495 | + } | ||
| 496 | + | ||
| 497 | + if let Some(curr) = mode { | ||
| 498 | + if curr.vrefresh() < m.vrefresh() { | ||
| 499 | + mode = Some(m); | ||
| 500 | + } | ||
| 501 | + } else { | ||
| 502 | + mode = Some(m); | ||
| 503 | + } | ||
| 504 | + } | ||
| 505 | + } | ||
| 506 | + | ||
| 507 | + if mode.is_none() { | ||
| 508 | + // Last attempt. | ||
| 509 | + mode = connector.modes().get(0); | ||
| 510 | + } | ||
| 511 | + | ||
| 451 | let mode = mode.ok_or_else(|| anyhow!("no mode"))?; | 512 | let mode = mode.ok_or_else(|| anyhow!("no mode"))?; |
| 452 | debug!("picking mode: {mode:?}"); | 513 | debug!("picking mode: {mode:?}"); |
| 453 | 514 | ||
src/config.rsmodified+77 -0
| @@ -81,6 +81,8 @@ pub struct Output { | |||
| 81 | pub scale: f64, | 81 | pub scale: f64, |
| 82 | #[knuffel(child)] | 82 | #[knuffel(child)] |
| 83 | pub position: Option<Position>, | 83 | pub position: Option<Position>, |
| 84 | + #[knuffel(child, unwrap(argument, str))] | ||
| 85 | + pub mode: Option<Mode>, | ||
| 84 | } | 86 | } |
| 85 | 87 | ||
| 86 | impl Default for Output { | 88 | impl Default for Output { |
| @@ -89,6 +91,7 @@ impl Default for Output { | |||
| 89 | name: String::new(), | 91 | name: String::new(), |
| 90 | scale: 1., | 92 | scale: 1., |
| 91 | position: None, | 93 | position: None, |
| 94 | + mode: None, | ||
| 92 | } | 95 | } |
| 93 | } | 96 | } |
| 94 | } | 97 | } |
| @@ -101,6 +104,13 @@ pub struct Position { | |||
| 101 | pub y: i32, | 104 | pub y: i32, |
| 102 | } | 105 | } |
| 103 | 106 | ||
| 107 | +#[derive(Debug, Clone, PartialEq)] | ||
| 108 | +pub struct Mode { | ||
| 109 | + pub width: u16, | ||
| 110 | + pub height: u16, | ||
| 111 | + pub refresh: Option<f64>, | ||
| 112 | +} | ||
| 113 | + | ||
| 104 | #[derive(knuffel::Decode, Debug, Clone, PartialEq, Eq)] | 114 | #[derive(knuffel::Decode, Debug, Clone, PartialEq, Eq)] |
| 105 | pub struct SpawnAtStartup { | 115 | pub struct SpawnAtStartup { |
| 106 | #[knuffel(arguments)] | 116 | #[knuffel(arguments)] |
| @@ -303,6 +313,41 @@ impl Default for Config { | |||
| 303 | } | 313 | } |
| 304 | } | 314 | } |
| 305 | 315 | ||
| 316 | +impl FromStr for Mode { | ||
| 317 | + type Err = miette::Error; | ||
| 318 | + | ||
| 319 | + fn from_str(s: &str) -> Result<Self, Self::Err> { | ||
| 320 | + let Some((width, rest)) = s.split_once('x') else { | ||
| 321 | + return Err(miette!("no 'x' separator found")); | ||
| 322 | + }; | ||
| 323 | + | ||
| 324 | + let (height, refresh) = match rest.split_once('@') { | ||
| 325 | + Some((height, refresh)) => (height, Some(refresh)), | ||
| 326 | + None => (rest, None), | ||
| 327 | + }; | ||
| 328 | + | ||
| 329 | + let width = width | ||
| 330 | + .parse() | ||
| 331 | + .into_diagnostic() | ||
| 332 | + .context("error parsing width")?; | ||
| 333 | + let height = height | ||
| 334 | + .parse() | ||
| 335 | + .into_diagnostic() | ||
| 336 | + .context("error parsing height")?; | ||
| 337 | + let refresh = refresh | ||
| 338 | + .map(str::parse) | ||
| 339 | + .transpose() | ||
| 340 | + .into_diagnostic() | ||
| 341 | + .context("error parsing refresh rate")?; | ||
| 342 | + | ||
| 343 | + Ok(Self { | ||
| 344 | + width, | ||
| 345 | + height, | ||
| 346 | + refresh, | ||
| 347 | + }) | ||
| 348 | + } | ||
| 349 | +} | ||
| 350 | + | ||
| 306 | impl FromStr for Key { | 351 | impl FromStr for Key { |
| 307 | type Err = miette::Error; | 352 | type Err = miette::Error; |
| 308 | 353 | ||
| @@ -377,6 +422,7 @@ mod tests { | |||
| 377 | output "eDP-1" { | 422 | output "eDP-1" { |
| 378 | scale 2.0 | 423 | scale 2.0 |
| 379 | position x=10 y=20 | 424 | position x=10 y=20 |
| 425 | + mode "1920x1080@144" | ||
| 380 | } | 426 | } |
| 381 | 427 | ||
| 382 | spawn-at-startup "alacritty" "-e" "fish" | 428 | spawn-at-startup "alacritty" "-e" "fish" |
| @@ -428,6 +474,11 @@ mod tests { | |||
| 428 | name: "eDP-1".to_owned(), | 474 | name: "eDP-1".to_owned(), |
| 429 | scale: 2., | 475 | scale: 2., |
| 430 | position: Some(Position { x: 10, y: 20 }), | 476 | position: Some(Position { x: 10, y: 20 }), |
| 477 | + mode: Some(Mode { | ||
| 478 | + width: 1920, | ||
| 479 | + height: 1080, | ||
| 480 | + refresh: Some(144.), | ||
| 481 | + }), | ||
| 431 | }], | 482 | }], |
| 432 | spawn_at_startup: vec![SpawnAtStartup { | 483 | spawn_at_startup: vec![SpawnAtStartup { |
| 433 | command: vec!["alacritty".to_owned(), "-e".to_owned(), "fish".to_owned()], | 484 | command: vec!["alacritty".to_owned(), "-e".to_owned(), "fish".to_owned()], |
| @@ -509,4 +560,30 @@ mod tests { | |||
| 509 | fn can_create_default_config() { | 560 | fn can_create_default_config() { |
| 510 | let _ = Config::default(); | 561 | let _ = Config::default(); |
| 511 | } | 562 | } |
| 563 | + | ||
| 564 | + #[test] | ||
| 565 | + fn parse_mode() { | ||
| 566 | + assert_eq!( | ||
| 567 | + "2560x1600@165.004".parse::<Mode>().unwrap(), | ||
| 568 | + Mode { | ||
| 569 | + width: 2560, | ||
| 570 | + height: 1600, | ||
| 571 | + refresh: Some(165.004), | ||
| 572 | + }, | ||
| 573 | + ); | ||
| 574 | + | ||
| 575 | + assert_eq!( | ||
| 576 | + "1920x1080".parse::<Mode>().unwrap(), | ||
| 577 | + Mode { | ||
| 578 | + width: 1920, | ||
| 579 | + height: 1080, | ||
| 580 | + refresh: None, | ||
| 581 | + }, | ||
| 582 | + ); | ||
| 583 | + | ||
| 584 | + assert!("1920".parse::<Mode>().is_err()); | ||
| 585 | + assert!("1920x".parse::<Mode>().is_err()); | ||
| 586 | + assert!("1920x1080@".parse::<Mode>().is_err()); | ||
| 587 | + assert!("1920x1080@60Hz".parse::<Mode>().is_err()); | ||
| 588 | + } | ||
| 512 | } | 589 | } |