forge
Explore
Log in

Don't you dare go Hollow

Make output position configurable

Browse files at this commit

21737abbfdb78698e323c0a9490759b427370a74Ivan MolodetskikhParent: a413f3e
Implements https://github.com/YaLTeR/niri/issues/14
resources/default-config.kdlmodified+11 -0
@@ -33,6 +33,17 @@ input {
3333 /-output "eDP-1" {
3434 // Scale is a floating-point number, but at the moment only integer values work.
3535 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
3647 }
3748
3849 // Add lines like this to spawn processes at startup.
src/config.rsmodified+13 -0
@@ -77,6 +77,8 @@ pub struct Output {
7777 pub name: String,
7878 #[knuffel(child, unwrap(argument), default = 1.)]
7979 pub scale: f64,
80+ #[knuffel(child)]
81+ pub position: Option<Position>,
8082 }
8183
8284 impl Default for Output {
@@ -84,10 +86,19 @@ impl Default for Output {
8486 Self {
8587 name: String::new(),
8688 scale: 1.,
89+ position: None,
8790 }
8891 }
8992 }
9093
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+
91102 #[derive(knuffel::Decode, Debug, Clone, PartialEq, Eq)]
92103 pub struct SpawnAtStartup {
93104 #[knuffel(arguments)]
@@ -342,6 +353,7 @@ mod tests {
342353
343354 output "eDP-1" {
344355 scale 2.0
356+ position x=10 y=20
345357 }
346358
347359 spawn-at-startup "alacritty" "-e" "fish"
@@ -387,6 +399,7 @@ mod tests {
387399 outputs: vec![Output {
388400 name: "eDP-1".to_owned(),
389401 scale: 2.,
402+ position: Some(Position { x: 10, y: 20 }),
390403 }],
391404 spawn_at_startup: vec![SpawnAtStartup {
392405 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;
7474 use crate::dbus::mutter_screen_cast::{self, ScreenCast, ToNiriMsg};
7575 use crate::dbus::mutter_service_channel::ServiceChannel;
7676 use crate::frame_clock::FrameClock;
77-use crate::layout::{MonitorRenderElement, MonitorSet};
77+use crate::layout::{output_size, MonitorRenderElement, MonitorSet};
7878 use crate::pw_utils::{Cast, PipeWire};
7979 use crate::utils::{center, get_monotonic_time, load_default_cursor, make_screenshot_path};
8080
@@ -668,15 +668,67 @@ impl Niri {
668668 pub fn add_output(&mut self, output: Output, refresh_interval: Option<Duration>) {
669669 let global = output.create_global::<State>(&self.display_handle);
670670
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+ });
678726
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);
680732 self.monitor_set.add_output(output.clone());
681733
682734 let state = OutputState {