dbus: DisplayConfig: implement apply_monitors_config
Browse files at this commit890bbff0077cddaa08d7d6c05c797c41b0b4c09eParent: b853d5b3 changed files +192 -71
- src/dbus/mod.rs +19 -1
- src/dbus/mutter_display_config.rs +103 -3
- src/niri.rs +70 -67
src/dbus/mod.rsmodified+19 -1
| @@ -50,7 +50,25 @@ impl DBusServers { | |||
| 50 | } | 50 | } |
| 51 | 51 | ||
| 52 | if is_session_instance || config.debug.dbus_interfaces_in_non_session_instances { | 52 | if is_session_instance || config.debug.dbus_interfaces_in_non_session_instances { |
| 53 | - let display_config = DisplayConfig::new(backend.ipc_outputs()); | 53 | + let (to_niri, from_display_config) = calloop::channel::channel(); |
| 54 | + let display_config = DisplayConfig::new(to_niri, backend.ipc_outputs()); | ||
| 55 | + niri.event_loop | ||
| 56 | + .insert_source(from_display_config, move |event, _, state| match event { | ||
| 57 | + calloop::channel::Event::Msg(new_conf) => { | ||
| 58 | + for (name, conf) in new_conf { | ||
| 59 | + state.modify_output_config(&name, move |output| { | ||
| 60 | + if let Some(new_output) = conf { | ||
| 61 | + *output = new_output; | ||
| 62 | + } else { | ||
| 63 | + output.off = true; | ||
| 64 | + } | ||
| 65 | + }); | ||
| 66 | + } | ||
| 67 | + state.reload_output_config(); | ||
| 68 | + } | ||
| 69 | + calloop::channel::Event::Closed => (), | ||
| 70 | + }) | ||
| 71 | + .unwrap(); | ||
| 54 | dbus.conn_display_config = try_start(display_config); | 72 | dbus.conn_display_config = try_start(display_config); |
| 55 | 73 | ||
| 56 | let screen_saver = ScreenSaver::new(niri.is_fdo_idle_inhibited.clone()); | 74 | let screen_saver = ScreenSaver::new(niri.is_fdo_idle_inhibited.clone()); |
src/dbus/mutter_display_config.rsmodified+103 -3
| @@ -1,7 +1,8 @@ | |||
| 1 | use std::collections::HashMap; | 1 | use std::collections::HashMap; |
| 2 | +use std::str::FromStr; | ||
| 2 | use std::sync::{Arc, Mutex}; | 3 | use std::sync::{Arc, Mutex}; |
| 3 | 4 | ||
| 4 | -use serde::Serialize; | 5 | +use serde::{Deserialize, Serialize}; |
| 5 | use smithay::utils::Size; | 6 | use smithay::utils::Size; |
| 6 | use zbus::fdo::RequestNameFlags; | 7 | use zbus::fdo::RequestNameFlags; |
| 7 | use zbus::object_server::SignalEmitter; | 8 | use zbus::object_server::SignalEmitter; |
| @@ -14,6 +15,7 @@ use crate::utils::is_laptop_panel; | |||
| 14 | use crate::utils::scale::supported_scales; | 15 | use crate::utils::scale::supported_scales; |
| 15 | 16 | ||
| 16 | pub struct DisplayConfig { | 17 | pub struct DisplayConfig { |
| 18 | + to_niri: calloop::channel::Sender<HashMap<String, Option<niri_config::Output>>>, | ||
| 17 | ipc_outputs: Arc<Mutex<IpcOutputMap>>, | 19 | ipc_outputs: Arc<Mutex<IpcOutputMap>>, |
| 18 | } | 20 | } |
| 19 | 21 | ||
| @@ -46,6 +48,17 @@ pub struct LogicalMonitor { | |||
| 46 | properties: HashMap<String, OwnedValue>, | 48 | properties: HashMap<String, OwnedValue>, |
| 47 | } | 49 | } |
| 48 | 50 | ||
| 51 | +// ApplyMonitorsConfig | ||
| 52 | +#[derive(Deserialize, Type)] | ||
| 53 | +pub struct LogicalMonitorConfiguration { | ||
| 54 | + x: i32, | ||
| 55 | + y: i32, | ||
| 56 | + scale: f64, | ||
| 57 | + transform: u32, | ||
| 58 | + _is_primary: bool, | ||
| 59 | + monitors: Vec<(String, String, HashMap<String, OwnedValue>)>, | ||
| 60 | +} | ||
| 61 | + | ||
| 49 | #[interface(name = "org.gnome.Mutter.DisplayConfig")] | 62 | #[interface(name = "org.gnome.Mutter.DisplayConfig")] |
| 50 | impl DisplayConfig { | 63 | impl DisplayConfig { |
| 51 | async fn get_current_state( | 64 | async fn get_current_state( |
| @@ -158,6 +171,87 @@ impl DisplayConfig { | |||
| 158 | Ok((0, monitors, logical_monitors, properties)) | 171 | Ok((0, monitors, logical_monitors, properties)) |
| 159 | } | 172 | } |
| 160 | 173 | ||
| 174 | + async fn apply_monitors_config( | ||
| 175 | + &self, | ||
| 176 | + _serial: u32, | ||
| 177 | + method: u32, | ||
| 178 | + logical_monitor_configs: Vec<LogicalMonitorConfiguration>, | ||
| 179 | + _properties: HashMap<String, OwnedValue>, | ||
| 180 | + ) -> fdo::Result<()> { | ||
| 181 | + let current_conf = self.ipc_outputs.lock().unwrap(); | ||
| 182 | + let mut new_conf = HashMap::new(); | ||
| 183 | + for requested_config in logical_monitor_configs { | ||
| 184 | + if requested_config.monitors.len() > 1 { | ||
| 185 | + return Err(zbus::fdo::Error::Failed( | ||
| 186 | + "Mirroring is not yet supported".to_owned(), | ||
| 187 | + )); | ||
| 188 | + } | ||
| 189 | + for (connector, mode, _props) in requested_config.monitors { | ||
| 190 | + if !current_conf.values().any(|o| o.name == connector) { | ||
| 191 | + return Err(zbus::fdo::Error::Failed(format!( | ||
| 192 | + "Connector '{}' not found", | ||
| 193 | + connector | ||
| 194 | + ))); | ||
| 195 | + } | ||
| 196 | + new_conf.insert( | ||
| 197 | + connector.clone(), | ||
| 198 | + Some(niri_config::Output { | ||
| 199 | + off: false, | ||
| 200 | + name: connector, | ||
| 201 | + scale: Some(niri_config::FloatOrInt(requested_config.scale)), | ||
| 202 | + transform: match requested_config.transform { | ||
| 203 | + 0 => niri_ipc::Transform::Normal, | ||
| 204 | + 1 => niri_ipc::Transform::_90, | ||
| 205 | + 2 => niri_ipc::Transform::_180, | ||
| 206 | + 3 => niri_ipc::Transform::_270, | ||
| 207 | + 4 => niri_ipc::Transform::Flipped, | ||
| 208 | + 5 => niri_ipc::Transform::Flipped90, | ||
| 209 | + 6 => niri_ipc::Transform::Flipped180, | ||
| 210 | + 7 => niri_ipc::Transform::Flipped270, | ||
| 211 | + x => { | ||
| 212 | + return Err(zbus::fdo::Error::Failed(format!( | ||
| 213 | + "Unknown transform {}", | ||
| 214 | + x | ||
| 215 | + ))) | ||
| 216 | + } | ||
| 217 | + }, | ||
| 218 | + position: Some(niri_config::Position { | ||
| 219 | + x: requested_config.x, | ||
| 220 | + y: requested_config.y, | ||
| 221 | + }), | ||
| 222 | + mode: Some(niri_ipc::ConfiguredMode::from_str(&mode).map_err(|e| { | ||
| 223 | + zbus::fdo::Error::Failed(format!( | ||
| 224 | + "Could not parse mode '{}': {}", | ||
| 225 | + mode, e | ||
| 226 | + )) | ||
| 227 | + })?), | ||
| 228 | + // FIXME: VRR | ||
| 229 | + ..Default::default() | ||
| 230 | + }), | ||
| 231 | + ); | ||
| 232 | + } | ||
| 233 | + } | ||
| 234 | + if new_conf.is_empty() { | ||
| 235 | + return Err(zbus::fdo::Error::Failed( | ||
| 236 | + "At least one output must be enabled".to_owned(), | ||
| 237 | + )); | ||
| 238 | + } | ||
| 239 | + for output in current_conf.values() { | ||
| 240 | + if !new_conf.contains_key(&output.name) { | ||
| 241 | + new_conf.insert(output.name.clone(), None); | ||
| 242 | + } | ||
| 243 | + } | ||
| 244 | + if method == 0 { | ||
| 245 | + // 0 means "verify", so don't actually apply here | ||
| 246 | + return Ok(()); | ||
| 247 | + } | ||
| 248 | + if let Err(err) = self.to_niri.send(new_conf) { | ||
| 249 | + warn!("error sending message to niri: {err:?}"); | ||
| 250 | + return Err(fdo::Error::Failed("internal error".to_owned())); | ||
| 251 | + } | ||
| 252 | + Ok(()) | ||
| 253 | + } | ||
| 254 | + | ||
| 161 | #[zbus(signal)] | 255 | #[zbus(signal)] |
| 162 | pub async fn monitors_changed(ctxt: &SignalEmitter<'_>) -> zbus::Result<()>; | 256 | pub async fn monitors_changed(ctxt: &SignalEmitter<'_>) -> zbus::Result<()>; |
| 163 | 257 | ||
| @@ -188,8 +282,14 @@ impl DisplayConfig { | |||
| 188 | } | 282 | } |
| 189 | 283 | ||
| 190 | impl DisplayConfig { | 284 | impl DisplayConfig { |
| 191 | - pub fn new(ipc_outputs: Arc<Mutex<IpcOutputMap>>) -> Self { | 285 | + pub fn new( |
| 192 | - Self { ipc_outputs } | 286 | + to_niri: calloop::channel::Sender<HashMap<String, Option<niri_config::Output>>>, |
| 287 | + ipc_outputs: Arc<Mutex<IpcOutputMap>>, | ||
| 288 | + ) -> Self { | ||
| 289 | + Self { | ||
| 290 | + to_niri, | ||
| 291 | + ipc_outputs, | ||
| 292 | + } | ||
| 193 | } | 293 | } |
| 194 | } | 294 | } |
| 195 | 295 | ||
src/niri.rsmodified+70 -67
| @@ -1343,82 +1343,85 @@ impl State { | |||
| 1343 | self.niri.output_management_state.on_config_changed(config); | 1343 | self.niri.output_management_state.on_config_changed(config); |
| 1344 | } | 1344 | } |
| 1345 | 1345 | ||
| 1346 | - pub fn apply_transient_output_config(&mut self, name: &str, action: niri_ipc::OutputAction) { | 1346 | + pub fn modify_output_config<F>(&mut self, name: &str, fun: F) |
| 1347 | + where | ||
| 1348 | + F: FnOnce(&mut niri_config::Output), | ||
| 1349 | + { | ||
| 1350 | + // Try hard to find the output config section corresponding to the output set by the | ||
| 1351 | + // user. Since if we add a new section and some existing section also matches the | ||
| 1352 | + // output, then our new section won't do anything. | ||
| 1353 | + let temp; | ||
| 1354 | + let match_name = if let Some(output) = self.niri.output_by_name_match(name) { | ||
| 1355 | + output.user_data().get::<OutputName>().unwrap() | ||
| 1356 | + } else if let Some(output_name) = self | ||
| 1357 | + .backend | ||
| 1358 | + .tty_checked() | ||
| 1359 | + .and_then(|tty| tty.disconnected_connector_name_by_name_match(name)) | ||
| 1347 | { | 1360 | { |
| 1348 | - // Try hard to find the output config section corresponding to the output set by the | 1361 | + temp = output_name; |
| 1349 | - // user. Since if we add a new section and some existing section also matches the | 1362 | + &temp |
| 1350 | - // output, then our new section won't do anything. | 1363 | + } else { |
| 1351 | - let temp; | 1364 | + // Even if name is "make model serial", matching will work fine this way. |
| 1352 | - let match_name = if let Some(output) = self.niri.output_by_name_match(name) { | 1365 | + temp = OutputName { |
| 1353 | - output.user_data().get::<OutputName>().unwrap() | 1366 | + connector: name.to_owned(), |
| 1354 | - } else if let Some(output_name) = self | 1367 | + make: None, |
| 1355 | - .backend | 1368 | + model: None, |
| 1356 | - .tty_checked() | 1369 | + serial: None, |
| 1357 | - .and_then(|tty| tty.disconnected_connector_name_by_name_match(name)) | ||
| 1358 | - { | ||
| 1359 | - temp = output_name; | ||
| 1360 | - &temp | ||
| 1361 | - } else { | ||
| 1362 | - // Even if name is "make model serial", matching will work fine this way. | ||
| 1363 | - temp = OutputName { | ||
| 1364 | - connector: name.to_owned(), | ||
| 1365 | - make: None, | ||
| 1366 | - model: None, | ||
| 1367 | - serial: None, | ||
| 1368 | - }; | ||
| 1369 | - &temp | ||
| 1370 | }; | 1370 | }; |
| 1371 | + &temp | ||
| 1372 | + }; | ||
| 1371 | 1373 | ||
| 1372 | - let mut config = self.niri.config.borrow_mut(); | 1374 | + let mut config = self.niri.config.borrow_mut(); |
| 1373 | - let config = if let Some(config) = config.outputs.find_mut(match_name) { | 1375 | + let config = if let Some(config) = config.outputs.find_mut(match_name) { |
| 1374 | - config | 1376 | + config |
| 1375 | - } else { | 1377 | + } else { |
| 1376 | - config.outputs.0.push(niri_config::Output { | 1378 | + config.outputs.0.push(niri_config::Output { |
| 1377 | - // Save name as set by the user. | 1379 | + // Save name as set by the user. |
| 1378 | - name: String::from(name), | 1380 | + name: String::from(name), |
| 1379 | - ..Default::default() | 1381 | + ..Default::default() |
| 1380 | - }); | 1382 | + }); |
| 1381 | - config.outputs.0.last_mut().unwrap() | 1383 | + config.outputs.0.last_mut().unwrap() |
| 1382 | - }; | 1384 | + }; |
| 1383 | 1385 | ||
| 1384 | - match action { | 1386 | + fun(config); |
| 1385 | - niri_ipc::OutputAction::Off => config.off = true, | 1387 | + } |
| 1386 | - niri_ipc::OutputAction::On => config.off = false, | 1388 | + |
| 1387 | - niri_ipc::OutputAction::Mode { mode } => { | 1389 | + pub fn apply_transient_output_config(&mut self, name: &str, action: niri_ipc::OutputAction) { |
| 1388 | - config.mode = match mode { | 1390 | + self.modify_output_config(name, move |config| match action { |
| 1389 | - niri_ipc::ModeToSet::Automatic => None, | 1391 | + niri_ipc::OutputAction::Off => config.off = true, |
| 1390 | - niri_ipc::ModeToSet::Specific(mode) => Some(mode), | 1392 | + niri_ipc::OutputAction::On => config.off = false, |
| 1391 | - } | 1393 | + niri_ipc::OutputAction::Mode { mode } => { |
| 1394 | + config.mode = match mode { | ||
| 1395 | + niri_ipc::ModeToSet::Automatic => None, | ||
| 1396 | + niri_ipc::ModeToSet::Specific(mode) => Some(mode), | ||
| 1392 | } | 1397 | } |
| 1393 | - niri_ipc::OutputAction::Scale { scale } => { | 1398 | + } |
| 1394 | - config.scale = match scale { | 1399 | + niri_ipc::OutputAction::Scale { scale } => { |
| 1395 | - niri_ipc::ScaleToSet::Automatic => None, | 1400 | + config.scale = match scale { |
| 1396 | - niri_ipc::ScaleToSet::Specific(scale) => Some(FloatOrInt(scale)), | 1401 | + niri_ipc::ScaleToSet::Automatic => None, |
| 1397 | - } | 1402 | + niri_ipc::ScaleToSet::Specific(scale) => Some(FloatOrInt(scale)), |
| 1398 | } | 1403 | } |
| 1399 | - niri_ipc::OutputAction::Transform { transform } => config.transform = transform, | 1404 | + } |
| 1400 | - niri_ipc::OutputAction::Position { position } => { | 1405 | + niri_ipc::OutputAction::Transform { transform } => config.transform = transform, |
| 1401 | - config.position = match position { | 1406 | + niri_ipc::OutputAction::Position { position } => { |
| 1402 | - niri_ipc::PositionToSet::Automatic => None, | 1407 | + config.position = match position { |
| 1403 | - niri_ipc::PositionToSet::Specific(position) => { | 1408 | + niri_ipc::PositionToSet::Automatic => None, |
| 1404 | - Some(niri_config::Position { | 1409 | + niri_ipc::PositionToSet::Specific(position) => Some(niri_config::Position { |
| 1405 | - x: position.x, | 1410 | + x: position.x, |
| 1406 | - y: position.y, | 1411 | + y: position.y, |
| 1407 | - }) | 1412 | + }), |
| 1408 | - } | ||
| 1409 | - } | ||
| 1410 | } | 1413 | } |
| 1411 | - niri_ipc::OutputAction::Vrr { vrr } => { | 1414 | + } |
| 1412 | - config.variable_refresh_rate = if vrr.vrr { | 1415 | + niri_ipc::OutputAction::Vrr { vrr } => { |
| 1413 | - Some(niri_config::Vrr { | 1416 | + config.variable_refresh_rate = if vrr.vrr { |
| 1414 | - on_demand: vrr.on_demand, | 1417 | + Some(niri_config::Vrr { |
| 1415 | - }) | 1418 | + on_demand: vrr.on_demand, |
| 1416 | - } else { | 1419 | + }) |
| 1417 | - None | 1420 | + } else { |
| 1418 | - } | 1421 | + None |
| 1419 | } | 1422 | } |
| 1420 | } | 1423 | } |
| 1421 | - } | 1424 | + }); |
| 1422 | 1425 | ||
| 1423 | self.reload_output_config(); | 1426 | self.reload_output_config(); |
| 1424 | } | 1427 | } |