Make output position configurable
Browse files at this commit21737abbfdb78698e323c0a9490759b427370a74Parent: a413f3e3 changed files +85 -9
- resources/default-config.kdl +11 -0
- src/config.rs +13 -0
- src/niri.rs +61 -9
resources/default-config.kdlmodified+11 -0
| @@ -33,6 +33,17 @@ input { | ||
| 33 | 33 | /-output "eDP-1" { |
| 34 | 34 | // Scale is a floating-point number, but at the moment only integer values work. |
| 35 | 35 | scale 2.0 |
| 36 | + | |
| 37 | + // Position of the output in the global coordinate space. | |
| 38 | + // This affects directional monitor actions like "focus-monitor-left", and cursor movement. | |
| 39 | + // The cursor can only move between directly adjacent outputs. | |
| 40 | + // Output scale has to be taken into account for positioning: | |
| 41 | + // outputs are sized in logical, or scaled, pixels. | |
| 42 | + // For example, a 3840×2160 output with scale 2.0 will have a logical size of 1920×1080, | |
| 43 | + // so to put another output directly adjacent to it on the right, set its x to 1920. | |
| 44 | + // It the position is unset or results in an overlap, the output is instead placed | |
| 45 | + // automatically. | |
| 46 | + position x=1280 y=0 | |
| 36 | 47 | } |
| 37 | 48 | |
| 38 | 49 | // Add lines like this to spawn processes at startup. |
src/config.rsmodified+13 -0
| @@ -77,6 +77,8 @@ pub struct Output { | ||
| 77 | 77 | pub name: String, |
| 78 | 78 | #[knuffel(child, unwrap(argument), default = 1.)] |
| 79 | 79 | pub scale: f64, |
| 80 | + #[knuffel(child)] | |
| 81 | + pub position: Option<Position>, | |
| 80 | 82 | } |
| 81 | 83 | |
| 82 | 84 | impl Default for Output { |
| @@ -84,10 +86,19 @@ impl Default for Output { | ||
| 84 | 86 | Self { |
| 85 | 87 | name: String::new(), |
| 86 | 88 | scale: 1., |
| 89 | + position: None, | |
| 87 | 90 | } |
| 88 | 91 | } |
| 89 | 92 | } |
| 90 | 93 | |
| 94 | +#[derive(knuffel::Decode, Debug, Clone, PartialEq, Eq)] | |
| 95 | +pub struct Position { | |
| 96 | + #[knuffel(property)] | |
| 97 | + pub x: i32, | |
| 98 | + #[knuffel(property)] | |
| 99 | + pub y: i32, | |
| 100 | +} | |
| 101 | + | |
| 91 | 102 | #[derive(knuffel::Decode, Debug, Clone, PartialEq, Eq)] |
| 92 | 103 | pub struct SpawnAtStartup { |
| 93 | 104 | #[knuffel(arguments)] |
| @@ -342,6 +353,7 @@ mod tests { | ||
| 342 | 353 | |
| 343 | 354 | output "eDP-1" { |
| 344 | 355 | scale 2.0 |
| 356 | + position x=10 y=20 | |
| 345 | 357 | } |
| 346 | 358 | |
| 347 | 359 | spawn-at-startup "alacritty" "-e" "fish" |
| @@ -387,6 +399,7 @@ mod tests { | ||
| 387 | 399 | outputs: vec![Output { |
| 388 | 400 | name: "eDP-1".to_owned(), |
| 389 | 401 | scale: 2., |
| 402 | + position: Some(Position { x: 10, y: 20 }), | |
| 390 | 403 | }], |
| 391 | 404 | spawn_at_startup: vec![SpawnAtStartup { |
| 392 | 405 | command: vec!["alacritty".to_owned(), "-e".to_owned(), "fish".to_owned()], |
src/niri.rsmodified+61 -9
| @@ -74,7 +74,7 @@ use crate::dbus::mutter_display_config::DisplayConfig; | ||
| 74 | 74 | use crate::dbus::mutter_screen_cast::{self, ScreenCast, ToNiriMsg}; |
| 75 | 75 | use crate::dbus::mutter_service_channel::ServiceChannel; |
| 76 | 76 | use crate::frame_clock::FrameClock; |
| 77 | -use crate::layout::{MonitorRenderElement, MonitorSet}; | |
| 77 | +use crate::layout::{output_size, MonitorRenderElement, MonitorSet}; | |
| 78 | 78 | use crate::pw_utils::{Cast, PipeWire}; |
| 79 | 79 | use crate::utils::{center, get_monotonic_time, load_default_cursor, make_screenshot_path}; |
| 80 | 80 | |
| @@ -668,15 +668,67 @@ impl Niri { | ||
| 668 | 668 | pub fn add_output(&mut self, output: Output, refresh_interval: Option<Duration>) { |
| 669 | 669 | let global = output.create_global::<State>(&self.display_handle); |
| 670 | 670 | |
| 671 | - let x = self | |
| 672 | - .global_space | |
| 673 | - .outputs() | |
| 674 | - .map(|output| self.global_space.output_geometry(output).unwrap()) | |
| 675 | - .map(|geom| geom.loc.x + geom.size.w) | |
| 676 | - .max() | |
| 677 | - .unwrap_or(0); | |
| 671 | + let name = output.name(); | |
| 672 | + let config = self | |
| 673 | + .config | |
| 674 | + .borrow() | |
| 675 | + .outputs | |
| 676 | + .iter() | |
| 677 | + .find(|o| o.name == name) | |
| 678 | + .cloned() | |
| 679 | + .unwrap_or_default(); | |
| 680 | + | |
| 681 | + let size = output_size(&output); | |
| 682 | + let position = config | |
| 683 | + .position | |
| 684 | + .map(|pos| Point::from((pos.x, pos.y))) | |
| 685 | + .filter(|pos| { | |
| 686 | + // Ensure that the requested position does not overlap any existing output. | |
| 687 | + let target_geom = Rectangle::from_loc_and_size(*pos, size); | |
| 688 | + | |
| 689 | + let overlap = self | |
| 690 | + .global_space | |
| 691 | + .outputs() | |
| 692 | + .map(|output| self.global_space.output_geometry(output).unwrap()) | |
| 693 | + .find(|geom| geom.overlaps(target_geom)); | |
| 694 | + | |
| 695 | + if let Some(overlap) = overlap { | |
| 696 | + warn!( | |
| 697 | + "new output {name} at x={} y={} sized {}x{} \ | |
| 698 | + overlaps an existing output at x={} y={} sized {}x{}, \ | |
| 699 | + falling back to automatic placement", | |
| 700 | + pos.x, | |
| 701 | + pos.y, | |
| 702 | + size.w, | |
| 703 | + size.h, | |
| 704 | + overlap.loc.x, | |
| 705 | + overlap.loc.y, | |
| 706 | + overlap.size.w, | |
| 707 | + overlap.size.h, | |
| 708 | + ); | |
| 709 | + | |
| 710 | + false | |
| 711 | + } else { | |
| 712 | + true | |
| 713 | + } | |
| 714 | + }) | |
| 715 | + .unwrap_or_else(|| { | |
| 716 | + let x = self | |
| 717 | + .global_space | |
| 718 | + .outputs() | |
| 719 | + .map(|output| self.global_space.output_geometry(output).unwrap()) | |
| 720 | + .map(|geom| geom.loc.x + geom.size.w) | |
| 721 | + .max() | |
| 722 | + .unwrap_or(0); | |
| 723 | + | |
| 724 | + Point::from((x, 0)) | |
| 725 | + }); | |
| 678 | 726 | |
| 679 | - self.global_space.map_output(&output, (x, 0)); | |
| 727 | + debug!( | |
| 728 | + "putting new output {name} at x={} y={}", | |
| 729 | + position.x, position.y | |
| 730 | + ); | |
| 731 | + self.global_space.map_output(&output, position); | |
| 680 | 732 | self.monitor_set.add_output(output.clone()); |
| 681 | 733 | |
| 682 | 734 | let state = OutputState { |