forge
Explore
Log in

Don't you dare go Hollow

Reposition all outputs on any change

Browse files at this commit

da4967d43c5d24c58e77a0f296a3df388a06e37eIvan MolodetskikhParent: d958a96
This way the positioning is independent of the order of plugging in.
niri-config/src/lib.rsmodified+2 -2
@@ -204,7 +204,7 @@ impl Default for Output {
204 }204 }
205 }205 }
206 206
207-#[derive(knuffel::Decode, Debug, Clone, PartialEq, Eq)]207+#[derive(knuffel::Decode, Debug, Clone, Copy, PartialEq, Eq)]
208 pub struct Position {208 pub struct Position {
209 #[knuffel(property)]209 #[knuffel(property)]
210 pub x: i32,210 pub x: i32,
@@ -212,7 +212,7 @@ pub struct Position {
212 pub y: i32,212 pub y: i32,
213 }213 }
214 214
215-#[derive(Debug, Clone, PartialEq)]215+#[derive(Debug, Clone, Copy, PartialEq)]
216 pub struct Mode {216 pub struct Mode {
217 pub width: u16,217 pub width: u16,
218 pub height: u16,218 pub height: u16,
src/niri.rsmodified+123 -62
@@ -912,72 +912,129 @@ impl Niri {
912 Ok(())912 Ok(())
913 }913 }
914 914
915- pub fn add_output(&mut self, output: Output, refresh_interval: Option<Duration>) {915+ /// Repositions all outputs, optionally adding a new output.
916- let global = output.create_global::<State>(&self.display_handle);916+ pub fn reposition_outputs(&mut self, new_output: Option<&Output>) {
917+ let _span = tracy_client::span!("Niri::reposition_outputs");
917 918
918- let name = output.name();919+ #[derive(Debug)]
919- let config = self920+ struct Data {
920- .config921+ output: Output,
921- .borrow()922+ name: String,
922- .outputs923+ position: Option<Point<i32, Logical>>,
923- .iter()924+ config: Option<niri_config::Position>,
924- .find(|o| o.name == name)925+ }
925- .cloned()
926- .unwrap_or_default();
927-
928- let size = output_size(&output);
929- let position = config
930- .position
931- .map(|pos| Point::from((pos.x, pos.y)))
932- .filter(|pos| {
933- // Ensure that the requested position does not overlap any existing output.
934- let target_geom = Rectangle::from_loc_and_size(*pos, size);
935-
936- let overlap = self
937- .global_space
938- .outputs()
939- .map(|output| self.global_space.output_geometry(output).unwrap())
940- .find(|geom| geom.overlaps(target_geom));
941-
942- if let Some(overlap) = overlap {
943- warn!(
944- "new output {name} at x={} y={} sized {}x{} \
945- overlaps an existing output at x={} y={} sized {}x{}, \
946- falling back to automatic placement",
947- pos.x,
948- pos.y,
949- size.w,
950- size.h,
951- overlap.loc.x,
952- overlap.loc.y,
953- overlap.size.w,
954- overlap.size.h,
955- );
956 926
957- false927+ let config = self.config.borrow();
958- } else {928+ let mut outputs = vec![];
959- true929+ for output in self.global_space.outputs().chain(new_output) {
960- }930+ let name = output.name();
961- })931+ let position = self.global_space.output_geometry(output).map(|geo| geo.loc);
962- .unwrap_or_else(|| {932+ let config = config
963- let x = self933+ .outputs
964- .global_space934+ .iter()
965- .outputs()935+ .find(|o| o.name == name)
966- .map(|output| self.global_space.output_geometry(output).unwrap())936+ .and_then(|c| c.position);
967- .map(|geom| geom.loc.x + geom.size.w)937+
968- .max()938+ outputs.push(Data {
969- .unwrap_or(0);939+ output: output.clone(),
970-940+ name,
971- Point::from((x, 0))941+ position,
942+ config,
972 });943 });
944+ }
945+ drop(config);
946+
947+ for Data { output, .. } in &outputs {
948+ self.global_space.unmap_output(output);
949+ }
950+
951+ // Connectors can appear in udev in any order. If we sort by name then we get output
952+ // positioning that does not depend on the order they appeared.
953+ //
954+ // All outputs must have different (connector) names.
955+ outputs.sort_unstable_by(|a, b| Ord::cmp(&a.name, &b.name));
956+
957+ // Place all outputs with explicitly configured position first, then the unconfigured ones.
958+ outputs.sort_by_key(|d| d.config.is_none());
973 959
974- debug!(960+ trace!(
975- "putting new output {name} at x={} y={}",961+ "placing outputs in order: {:?}",
976- position.x, position.y962+ outputs.iter().map(|d| &d.name)
977 );963 );
978- self.global_space.map_output(&output, position);964+
965+ for data in outputs.into_iter() {
966+ let Data {
967+ output,
968+ name,
969+ position,
970+ config,
971+ } = data;
972+
973+ let size = output_size(&output);
974+
975+ let new_position = config
976+ .map(|pos| Point::from((pos.x, pos.y)))
977+ .filter(|pos| {
978+ // Ensure that the requested position does not overlap any existing output.
979+ let target_geom = Rectangle::from_loc_and_size(*pos, size);
980+
981+ let overlap = self
982+ .global_space
983+ .outputs()
984+ .map(|output| self.global_space.output_geometry(output).unwrap())
985+ .find(|geom| geom.overlaps(target_geom));
986+
987+ if let Some(overlap) = overlap {
988+ warn!(
989+ "output {name} at x={} y={} sized {}x{} \
990+ overlaps an existing output at x={} y={} sized {}x{}, \
991+ falling back to automatic placement",
992+ pos.x,
993+ pos.y,
994+ size.w,
995+ size.h,
996+ overlap.loc.x,
997+ overlap.loc.y,
998+ overlap.size.w,
999+ overlap.size.h,
1000+ );
1001+
1002+ false
1003+ } else {
1004+ true
1005+ }
1006+ })
1007+ .unwrap_or_else(|| {
1008+ let x = self
1009+ .global_space
1010+ .outputs()
1011+ .map(|output| self.global_space.output_geometry(output).unwrap())
1012+ .map(|geom| geom.loc.x + geom.size.w)
1013+ .max()
1014+ .unwrap_or(0);
1015+
1016+ Point::from((x, 0))
1017+ });
1018+
1019+ self.global_space.map_output(&output, new_position);
1020+
1021+ // By passing new_output as an Option, rather than mapping it into a bogus location
1022+ // in global_space, we ensure that this branch always runs for it.
1023+ if Some(new_position) != position {
1024+ debug!(
1025+ "putting output {name} at x={} y={}",
1026+ new_position.x, new_position.y
1027+ );
1028+ output.change_current_state(None, None, None, Some(new_position));
1029+ self.queue_redraw(output);
1030+ }
1031+ }
1032+ }
1033+
1034+ pub fn add_output(&mut self, output: Output, refresh_interval: Option<Duration>) {
1035+ let global = output.create_global::<State>(&self.display_handle);
1036+
979 self.layout.add_output(output.clone());1037 self.layout.add_output(output.clone());
980- output.change_current_state(None, None, None, Some(position));
981 1038
982 let lock_render_state = if self.is_locked() {1039 let lock_render_state = if self.is_locked() {
983 // We haven't rendered anything yet so it's as good as locked.1040 // We haven't rendered anything yet so it's as good as locked.
@@ -986,6 +1043,7 @@ impl Niri {
986 LockRenderState::Unlocked1043 LockRenderState::Unlocked
987 };1044 };
988 1045
1046+ let size = output_size(&output);
989 let state = OutputState {1047 let state = OutputState {
990 global,1048 global,
991 redraw_state: RedrawState::Idle,1049 redraw_state: RedrawState::Idle,
@@ -999,14 +1057,17 @@ impl Niri {
999 };1057 };
1000 let rv = self.output_state.insert(output.clone(), state);1058 let rv = self.output_state.insert(output.clone(), state);
1001 assert!(rv.is_none(), "output was already tracked");1059 assert!(rv.is_none(), "output was already tracked");
1002- let rv = self.output_by_name.insert(name, output);1060+ let rv = self.output_by_name.insert(output.name(), output.clone());
1003 assert!(rv.is_none(), "output was already tracked");1061 assert!(rv.is_none(), "output was already tracked");
1062+
1063+ // Must be last since it will call queue_redraw(output) which needs things to be filled-in.
1064+ self.reposition_outputs(Some(&output));
1004 }1065 }
1005 1066
1006 pub fn remove_output(&mut self, output: &Output) {1067 pub fn remove_output(&mut self, output: &Output) {
1007 self.layout.remove_output(output);1068 self.layout.remove_output(output);
1008 self.global_space.unmap_output(output);1069 self.global_space.unmap_output(output);
1009- // FIXME: reposition outputs so they are adjacent.1070+ self.reposition_outputs(None);
1010 1071
1011 let state = self.output_state.remove(output).unwrap();1072 let state = self.output_state.remove(output).unwrap();
1012 self.output_by_name.remove(&output.name()).unwrap();1073 self.output_by_name.remove(&output.name()).unwrap();