forge
Explore
Log in

Don't you dare go Hollow

niri

Clone
★ 0

0 watching0 forksRustGPL-3.012.1 MiB64a9351Ivan Molodetskikh2 years ago1319 commits

mutter_display_config.rs185 lines, 6.2 KiBHistoryRawDownload
1use std::collections::HashMap;
2use std::sync::{Arc, Mutex};
3
4use serde::Serialize;
5use zbus::fdo::RequestNameFlags;
6use zbus::zvariant::{self, OwnedValue, Type};
7use zbus::{dbus_interface, fdo, SignalContext};
8
9use super::Start;
10use crate::backend::IpcOutputMap;
11
12pub struct DisplayConfig {
13 ipc_outputs: Arc<Mutex<IpcOutputMap>>,
14}
15
16#[derive(Serialize, Type)]
17pub struct Monitor {
18 names: (String, String, String, String),
19 modes: Vec<Mode>,
20 properties: HashMap<String, OwnedValue>,
21}
22
23#[derive(Serialize, Type)]
24pub struct Mode {
25 id: String,
26 width: i32,
27 height: i32,
28 refresh_rate: f64,
29 preferred_scale: f64,
30 supported_scales: Vec<f64>,
31 properties: HashMap<String, OwnedValue>,
32}
33
34#[derive(Serialize, Type)]
35pub struct LogicalMonitor {
36 x: i32,
37 y: i32,
38 scale: f64,
39 transform: u32,
40 is_primary: bool,
41 monitors: Vec<(String, String, String, String)>,
42 properties: HashMap<String, OwnedValue>,
43}
44
45#[dbus_interface(name = "org.gnome.Mutter.DisplayConfig")]
46impl DisplayConfig {
47 async fn get_current_state(
48 &self,
49 ) -> fdo::Result<(
50 u32,
51 Vec<Monitor>,
52 Vec<LogicalMonitor>,
53 HashMap<String, OwnedValue>,
54 )> {
55 // Construct the DBus response.
56 let mut monitors: Vec<(Monitor, LogicalMonitor)> = self
57 .ipc_outputs
58 .lock()
59 .unwrap()
60 .values()
61 // Take only enabled outputs.
62 .filter(|output| output.current_mode.is_some() && output.logical.is_some())
63 .map(|output| {
64 // Loosely matches the check in Mutter.
65 let c = &output.name;
66 let is_laptop_panel = matches!(c.get(..4), Some("eDP-" | "LVDS" | "DSI-"));
67
68 // FIXME: use proper serial when we have libdisplay-info.
69 // A serial is required for correct session restore by xdp-gnome.
70 let serial = c.clone();
71
72 let mut properties = HashMap::new();
73 if is_laptop_panel {
74 properties.insert(
75 String::from("display-name"),
76 OwnedValue::from(zvariant::Str::from_static("Built-in display")),
77 );
78 }
79 properties.insert(
80 String::from("is-builtin"),
81 OwnedValue::from(is_laptop_panel),
82 );
83
84 let mut modes: Vec<Mode> = output
85 .modes
86 .iter()
87 .map(|m| {
88 let niri_ipc::Mode {
89 width,
90 height,
91 refresh_rate,
92 is_preferred,
93 } = *m;
94 let refresh = refresh_rate as f64 / 1000.;
95
96 Mode {
97 id: format!("{width}x{height}@{refresh:.3}"),
98 width: i32::from(width),
99 height: i32::from(height),
100 refresh_rate: refresh,
101 preferred_scale: 1.,
102 supported_scales: vec![1., 2., 3.],
103 properties: HashMap::from([(
104 String::from("is-preferred"),
105 OwnedValue::from(is_preferred),
106 )]),
107 }
108 })
109 .collect();
110 modes[output.current_mode.unwrap()]
111 .properties
112 .insert(String::from("is-current"), OwnedValue::from(true));
113
114 let monitor = Monitor {
115 names: (c.clone(), String::new(), String::new(), serial),
116 modes,
117 properties,
118 };
119
120 let logical = output.logical.as_ref().unwrap();
121
122 let transform = match logical.transform {
123 niri_ipc::Transform::Normal => 0,
124 niri_ipc::Transform::_90 => 1,
125 niri_ipc::Transform::_180 => 2,
126 niri_ipc::Transform::_270 => 3,
127 niri_ipc::Transform::Flipped => 4,
128 niri_ipc::Transform::Flipped90 => 5,
129 niri_ipc::Transform::Flipped180 => 6,
130 niri_ipc::Transform::Flipped270 => 7,
131 };
132
133 let logical_monitor = LogicalMonitor {
134 x: logical.x,
135 y: logical.y,
136 scale: logical.scale,
137 transform,
138 is_primary: false,
139 monitors: vec![monitor.names.clone()],
140 properties: HashMap::new(),
141 };
142
143 (monitor, logical_monitor)
144 })
145 .collect();
146
147 // Sort the built-in monitor first, then by connector name.
148 monitors.sort_unstable_by(|a, b| {
149 let a_is_builtin = a.0.properties.contains_key("display-name");
150 let b_is_builtin = b.0.properties.contains_key("display-name");
151 a_is_builtin
152 .cmp(&b_is_builtin)
153 .reverse()
154 .then_with(|| a.0.names.0.cmp(&b.0.names.0))
155 });
156
157 let (monitors, logical_monitors) = monitors.into_iter().unzip();
158 let properties = HashMap::from([(String::from("layout-mode"), OwnedValue::from(1u32))]);
159 Ok((0, monitors, logical_monitors, properties))
160 }
161
162 #[dbus_interface(signal)]
163 pub async fn monitors_changed(ctxt: &SignalContext<'_>) -> zbus::Result<()>;
164}
165
166impl DisplayConfig {
167 pub fn new(ipc_outputs: Arc<Mutex<IpcOutputMap>>) -> Self {
168 Self { ipc_outputs }
169 }
170}
171
172impl Start for DisplayConfig {
173 fn start(self) -> anyhow::Result<zbus::blocking::Connection> {
174 let conn = zbus::blocking::Connection::session()?;
175 let flags = RequestNameFlags::AllowReplacement
176 | RequestNameFlags::ReplaceExisting
177 | RequestNameFlags::DoNotQueue;
178
179 conn.object_server()
180 .at("/org/gnome/Mutter/DisplayConfig", self)?;
181 conn.request_name_with_flags("org.gnome.Mutter.DisplayConfig", flags)?;
182
183 Ok(conn)
184 }
185}