niri
Clone
https://git.tahinli.com/Tahinli/niri.git
0 watching·0 forks·Rust·GPL-3.0·12.1 MiB·64a9351·Ivan Molodetskikh·2 years ago·1319 commits
| 1 | use std::collections::hash_map::Entry; |
| 2 | use std::collections::HashMap; |
| 3 | use std::iter::zip; |
| 4 | use std::mem; |
| 5 | |
| 6 | use niri_config::{FloatOrInt, Vrr}; |
| 7 | use niri_ipc::Transform; |
| 8 | use smithay::reexports::wayland_protocols_wlr::output_management::v1::server::{ |
| 9 | zwlr_output_configuration_head_v1, zwlr_output_configuration_v1, zwlr_output_head_v1, |
| 10 | zwlr_output_manager_v1, zwlr_output_mode_v1, |
| 11 | }; |
| 12 | use smithay::reexports::wayland_server::backend::ClientId; |
| 13 | use smithay::reexports::wayland_server::protocol::wl_output::Transform as WlTransform; |
| 14 | use smithay::reexports::wayland_server::{ |
| 15 | Client, DataInit, Dispatch, DisplayHandle, GlobalDispatch, New, Resource, WEnum, |
| 16 | }; |
| 17 | use zwlr_output_configuration_head_v1::ZwlrOutputConfigurationHeadV1; |
| 18 | use zwlr_output_configuration_v1::ZwlrOutputConfigurationV1; |
| 19 | use zwlr_output_head_v1::{AdaptiveSyncState, ZwlrOutputHeadV1}; |
| 20 | use zwlr_output_manager_v1::ZwlrOutputManagerV1; |
| 21 | use zwlr_output_mode_v1::ZwlrOutputModeV1; |
| 22 | |
| 23 | use crate::backend::OutputId; |
| 24 | use crate::niri::State; |
| 25 | use crate::utils::ipc_transform_to_smithay; |
| 26 | |
| 27 | const VERSION: u32 = 4; |
| 28 | |
| 29 | #[derive(Debug)] |
| 30 | struct ClientData { |
| 31 | heads: HashMap<OutputId, (ZwlrOutputHeadV1, Vec<ZwlrOutputModeV1>)>, |
| 32 | confs: HashMap<ZwlrOutputConfigurationV1, OutputConfigurationState>, |
| 33 | manager: ZwlrOutputManagerV1, |
| 34 | } |
| 35 | |
| 36 | pub struct OutputManagementManagerState { |
| 37 | display: DisplayHandle, |
| 38 | serial: u32, |
| 39 | clients: HashMap<ClientId, ClientData>, |
| 40 | current_state: HashMap<OutputId, niri_ipc::Output>, |
| 41 | current_config: niri_config::Outputs, |
| 42 | } |
| 43 | |
| 44 | pub struct OutputManagementManagerGlobalData { |
| 45 | filter: Box<dyn for<'c> Fn(&'c Client) -> bool + Send + Sync>, |
| 46 | } |
| 47 | |
| 48 | pub trait OutputManagementHandler { |
| 49 | fn output_management_state(&mut self) -> &mut OutputManagementManagerState; |
| 50 | fn apply_output_config(&mut self, config: niri_config::Outputs); |
| 51 | } |
| 52 | |
| 53 | #[derive(Debug)] |
| 54 | enum OutputConfigurationState { |
| 55 | Ongoing(HashMap<OutputId, niri_config::Output>), |
| 56 | Finished, |
| 57 | } |
| 58 | |
| 59 | pub enum OutputConfigurationHeadState { |
| 60 | Cancelled, |
| 61 | Ok(OutputId, ZwlrOutputConfigurationV1), |
| 62 | } |
| 63 | |
| 64 | impl OutputManagementManagerState { |
| 65 | pub fn new<D, F>(display: &DisplayHandle, filter: F) -> Self |
| 66 | where |
| 67 | D: GlobalDispatch<ZwlrOutputManagerV1, OutputManagementManagerGlobalData>, |
| 68 | D: Dispatch<ZwlrOutputManagerV1, ()>, |
| 69 | D: Dispatch<ZwlrOutputHeadV1, OutputId>, |
| 70 | D: Dispatch<ZwlrOutputConfigurationV1, u32>, |
| 71 | D: Dispatch<ZwlrOutputConfigurationHeadV1, OutputConfigurationHeadState>, |
| 72 | D: Dispatch<ZwlrOutputModeV1, ()>, |
| 73 | D: OutputManagementHandler, |
| 74 | D: 'static, |
| 75 | F: for<'c> Fn(&'c Client) -> bool + Send + Sync + 'static, |
| 76 | { |
| 77 | let global_data = OutputManagementManagerGlobalData { |
| 78 | filter: Box::new(filter), |
| 79 | }; |
| 80 | display.create_global::<D, ZwlrOutputManagerV1, _>(VERSION, global_data); |
| 81 | |
| 82 | Self { |
| 83 | display: display.clone(), |
| 84 | clients: HashMap::new(), |
| 85 | serial: 0, |
| 86 | current_state: HashMap::new(), |
| 87 | current_config: Default::default(), |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | pub fn on_config_changed(&mut self, new_config: niri_config::Outputs) { |
| 92 | self.current_config = new_config; |
| 93 | } |
| 94 | |
| 95 | pub fn notify_changes(&mut self, new_state: HashMap<OutputId, niri_ipc::Output>) { |
| 96 | let mut changed = false; /* most likely to end up true */ |
| 97 | for (output, conf) in new_state.iter() { |
| 98 | if let Some(old) = self.current_state.get(output) { |
| 99 | if old.vrr_enabled != conf.vrr_enabled { |
| 100 | changed = true; |
| 101 | for client in self.clients.values() { |
| 102 | if let Some((head, _)) = client.heads.get(output) { |
| 103 | if head.version() >= zwlr_output_head_v1::EVT_ADAPTIVE_SYNC_SINCE { |
| 104 | head.adaptive_sync(match conf.vrr_enabled { |
| 105 | true => AdaptiveSyncState::Enabled, |
| 106 | false => AdaptiveSyncState::Disabled, |
| 107 | }); |
| 108 | } |
| 109 | } |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | // TTY outputs can't change modes I think, however, winit and virtual outputs can. |
| 114 | let modes_changed = old.modes != conf.modes; |
| 115 | if modes_changed { |
| 116 | changed = true; |
| 117 | if old.modes.len() != conf.modes.len() { |
| 118 | error!("output's old mode count doesn't match new modes"); |
| 119 | } else { |
| 120 | for client in self.clients.values() { |
| 121 | if let Some((_, modes)) = client.heads.get(output) { |
| 122 | for (wl_mode, mode) in zip(modes, &conf.modes) { |
| 123 | wl_mode.size(i32::from(mode.width), i32::from(mode.height)); |
| 124 | if let Ok(refresh_rate) = mode.refresh_rate.try_into() { |
| 125 | wl_mode.refresh(refresh_rate); |
| 126 | } |
| 127 | } |
| 128 | } |
| 129 | } |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | match (old.current_mode, conf.current_mode) { |
| 134 | (Some(old_index), Some(new_index)) => { |
| 135 | if old.modes.len() == conf.modes.len() |
| 136 | && (modes_changed || old_index != new_index) |
| 137 | { |
| 138 | changed = true; |
| 139 | for client in self.clients.values() { |
| 140 | if let Some((head, modes)) = client.heads.get(output) { |
| 141 | if let Some(new_mode) = modes.get(new_index) { |
| 142 | head.current_mode(new_mode); |
| 143 | } else { |
| 144 | error!( |
| 145 | "output new mode doesnt exist for the client's output" |
| 146 | ); |
| 147 | } |
| 148 | } |
| 149 | } |
| 150 | } |
| 151 | } |
| 152 | (Some(_), None) => { |
| 153 | changed = true; |
| 154 | for client in self.clients.values() { |
| 155 | if let Some((head, _)) = client.heads.get(output) { |
| 156 | head.enabled(0); |
| 157 | } |
| 158 | } |
| 159 | } |
| 160 | (None, Some(new_index)) => { |
| 161 | if old.modes.len() == conf.modes.len() { |
| 162 | changed = true; |
| 163 | for client in self.clients.values() { |
| 164 | if let Some((head, modes)) = client.heads.get(output) { |
| 165 | head.enabled(1); |
| 166 | if let Some(mode) = modes.get(new_index) { |
| 167 | head.current_mode(mode); |
| 168 | } else { |
| 169 | error!( |
| 170 | "output new mode doesnt exist for the client's output" |
| 171 | ); |
| 172 | } |
| 173 | } |
| 174 | } |
| 175 | } |
| 176 | } |
| 177 | (None, None) => {} |
| 178 | } |
| 179 | match (old.logical, conf.logical) { |
| 180 | (Some(old_logical), Some(new_logical)) => { |
| 181 | if old_logical != new_logical { |
| 182 | changed = true; |
| 183 | for client in self.clients.values() { |
| 184 | if let Some((head, _)) = client.heads.get(output) { |
| 185 | if old_logical.x != new_logical.x |
| 186 | || old_logical.y != new_logical.y |
| 187 | { |
| 188 | head.position(new_logical.x, new_logical.y); |
| 189 | } |
| 190 | if old_logical.scale != new_logical.scale { |
| 191 | head.scale(new_logical.scale); |
| 192 | } |
| 193 | if old_logical.transform != new_logical.transform { |
| 194 | head.transform( |
| 195 | ipc_transform_to_smithay(new_logical.transform).into(), |
| 196 | ); |
| 197 | } |
| 198 | } |
| 199 | } |
| 200 | } |
| 201 | } |
| 202 | (None, Some(new_logical)) => { |
| 203 | changed = true; |
| 204 | for client in self.clients.values() { |
| 205 | if let Some((head, _)) = client.heads.get(output) { |
| 206 | // head enable in the mode diff check |
| 207 | head.position(new_logical.x, new_logical.y); |
| 208 | head.transform( |
| 209 | ipc_transform_to_smithay(new_logical.transform).into(), |
| 210 | ); |
| 211 | head.scale(new_logical.scale); |
| 212 | } |
| 213 | } |
| 214 | } |
| 215 | (Some(_), None) => { |
| 216 | // heads disabled in the mode diff check |
| 217 | } |
| 218 | (None, None) => {} |
| 219 | } |
| 220 | } else { |
| 221 | changed = true; |
| 222 | notify_new_head(self, output, conf); |
| 223 | } |
| 224 | } |
| 225 | for (old, _) in self.current_state.iter() { |
| 226 | if !new_state.contains_key(old) { |
| 227 | changed = true; |
| 228 | notify_removed_head(&mut self.clients, old); |
| 229 | } |
| 230 | } |
| 231 | if changed { |
| 232 | self.current_state = new_state; |
| 233 | self.serial += 1; |
| 234 | for data in self.clients.values() { |
| 235 | data.manager.done(self.serial); |
| 236 | for conf in data.confs.keys() { |
| 237 | conf.cancelled(); |
| 238 | } |
| 239 | } |
| 240 | } |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | impl<D> GlobalDispatch<ZwlrOutputManagerV1, OutputManagementManagerGlobalData, D> |
| 245 | for OutputManagementManagerState |
| 246 | where |
| 247 | D: GlobalDispatch<ZwlrOutputManagerV1, OutputManagementManagerGlobalData>, |
| 248 | D: Dispatch<ZwlrOutputManagerV1, ()>, |
| 249 | D: Dispatch<ZwlrOutputHeadV1, OutputId>, |
| 250 | D: Dispatch<ZwlrOutputConfigurationV1, u32>, |
| 251 | D: Dispatch<ZwlrOutputConfigurationHeadV1, OutputConfigurationHeadState>, |
| 252 | D: Dispatch<ZwlrOutputModeV1, ()>, |
| 253 | D: OutputManagementHandler, |
| 254 | D: 'static, |
| 255 | { |
| 256 | fn bind( |
| 257 | state: &mut D, |
| 258 | display: &DisplayHandle, |
| 259 | client: &Client, |
| 260 | manager: New<ZwlrOutputManagerV1>, |
| 261 | _manager_state: &OutputManagementManagerGlobalData, |
| 262 | data_init: &mut DataInit<'_, D>, |
| 263 | ) { |
| 264 | let manager = data_init.init(manager, ()); |
| 265 | let g_state = state.output_management_state(); |
| 266 | let mut client_data = ClientData { |
| 267 | heads: HashMap::new(), |
| 268 | confs: HashMap::new(), |
| 269 | manager: manager.clone(), |
| 270 | }; |
| 271 | for (output, conf) in &g_state.current_state { |
| 272 | send_new_head::<D>(display, client, &mut client_data, *output, conf); |
| 273 | } |
| 274 | g_state.clients.insert(client.id(), client_data); |
| 275 | manager.done(g_state.serial); |
| 276 | } |
| 277 | |
| 278 | fn can_view(client: Client, global_data: &OutputManagementManagerGlobalData) -> bool { |
| 279 | (global_data.filter)(&client) |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | impl<D> Dispatch<ZwlrOutputManagerV1, (), D> for OutputManagementManagerState |
| 284 | where |
| 285 | D: GlobalDispatch<ZwlrOutputManagerV1, OutputManagementManagerGlobalData>, |
| 286 | D: Dispatch<ZwlrOutputManagerV1, ()>, |
| 287 | D: Dispatch<ZwlrOutputHeadV1, OutputId>, |
| 288 | D: Dispatch<ZwlrOutputConfigurationV1, u32>, |
| 289 | D: Dispatch<ZwlrOutputConfigurationHeadV1, OutputConfigurationHeadState>, |
| 290 | D: Dispatch<ZwlrOutputModeV1, ()>, |
| 291 | D: OutputManagementHandler, |
| 292 | D: 'static, |
| 293 | { |
| 294 | fn request( |
| 295 | state: &mut D, |
| 296 | client: &Client, |
| 297 | _manager: &ZwlrOutputManagerV1, |
| 298 | request: zwlr_output_manager_v1::Request, |
| 299 | _data: &(), |
| 300 | _display: &DisplayHandle, |
| 301 | data_init: &mut DataInit<'_, D>, |
| 302 | ) { |
| 303 | match request { |
| 304 | zwlr_output_manager_v1::Request::CreateConfiguration { id, serial } => { |
| 305 | let g_state = state.output_management_state(); |
| 306 | let conf = data_init.init(id, serial); |
| 307 | if let Some(client_data) = g_state.clients.get_mut(&client.id()) { |
| 308 | if serial != g_state.serial { |
| 309 | conf.cancelled(); |
| 310 | } |
| 311 | let state = OutputConfigurationState::Ongoing(HashMap::new()); |
| 312 | client_data.confs.insert(conf, state); |
| 313 | } else { |
| 314 | error!("CreateConfiguration: missing client data"); |
| 315 | } |
| 316 | } |
| 317 | zwlr_output_manager_v1::Request::Stop => { |
| 318 | if let Some(c) = state.output_management_state().clients.remove(&client.id()) { |
| 319 | c.manager.finished() |
| 320 | } |
| 321 | } |
| 322 | _ => unreachable!(), |
| 323 | } |
| 324 | } |
| 325 | fn destroyed(state: &mut D, client: ClientId, _resource: &ZwlrOutputManagerV1, _data: &()) { |
| 326 | state.output_management_state().clients.remove(&client); |
| 327 | } |
| 328 | } |
| 329 | |
| 330 | impl<D> Dispatch<ZwlrOutputConfigurationV1, u32, D> for OutputManagementManagerState |
| 331 | where |
| 332 | D: GlobalDispatch<ZwlrOutputManagerV1, OutputManagementManagerGlobalData>, |
| 333 | D: Dispatch<ZwlrOutputManagerV1, ()>, |
| 334 | D: Dispatch<ZwlrOutputHeadV1, OutputId>, |
| 335 | D: Dispatch<ZwlrOutputConfigurationV1, u32>, |
| 336 | D: Dispatch<ZwlrOutputConfigurationHeadV1, OutputConfigurationHeadState>, |
| 337 | D: Dispatch<ZwlrOutputModeV1, ()>, |
| 338 | D: OutputManagementHandler, |
| 339 | D: 'static, |
| 340 | { |
| 341 | fn request( |
| 342 | state: &mut D, |
| 343 | client: &Client, |
| 344 | conf: &ZwlrOutputConfigurationV1, |
| 345 | request: zwlr_output_configuration_v1::Request, |
| 346 | serial: &u32, |
| 347 | _display: &DisplayHandle, |
| 348 | data_init: &mut DataInit<'_, D>, |
| 349 | ) { |
| 350 | let g_state = state.output_management_state(); |
| 351 | let outdated = *serial != g_state.serial; |
| 352 | if outdated { |
| 353 | debug!("OutputConfiguration: request from an outdated configuration"); |
| 354 | } |
| 355 | |
| 356 | let new_config = g_state |
| 357 | .clients |
| 358 | .get_mut(&client.id()) |
| 359 | .and_then(|data| data.confs.get_mut(conf)); |
| 360 | if new_config.is_none() { |
| 361 | error!("OutputConfiguration: request from unknown configuration object"); |
| 362 | } |
| 363 | |
| 364 | match request { |
| 365 | zwlr_output_configuration_v1::Request::EnableHead { id, head } => { |
| 366 | let Some(output) = head.data::<OutputId>() else { |
| 367 | error!("EnableHead: Missing attached output"); |
| 368 | let _fail = data_init.init(id, OutputConfigurationHeadState::Cancelled); |
| 369 | return; |
| 370 | }; |
| 371 | if outdated { |
| 372 | let _fail = data_init.init(id, OutputConfigurationHeadState::Cancelled); |
| 373 | return; |
| 374 | } |
| 375 | |
| 376 | let Some(new_config) = new_config else { |
| 377 | let _fail = data_init.init(id, OutputConfigurationHeadState::Cancelled); |
| 378 | return; |
| 379 | }; |
| 380 | |
| 381 | let OutputConfigurationState::Ongoing(new_config) = new_config else { |
| 382 | let _fail = data_init.init(id, OutputConfigurationHeadState::Cancelled); |
| 383 | conf.post_error( |
| 384 | zwlr_output_configuration_v1::Error::AlreadyUsed, |
| 385 | "configuration had already been used", |
| 386 | ); |
| 387 | return; |
| 388 | }; |
| 389 | |
| 390 | let Some(current_config) = g_state.current_state.get(output) else { |
| 391 | error!("EnableHead: output missing from current config"); |
| 392 | let _fail = data_init.init(id, OutputConfigurationHeadState::Cancelled); |
| 393 | return; |
| 394 | }; |
| 395 | |
| 396 | match new_config.entry(*output) { |
| 397 | Entry::Occupied(_) => { |
| 398 | let _fail = data_init.init(id, OutputConfigurationHeadState::Cancelled); |
| 399 | conf.post_error( |
| 400 | zwlr_output_configuration_v1::Error::AlreadyConfiguredHead, |
| 401 | "head has been already configured", |
| 402 | ); |
| 403 | return; |
| 404 | } |
| 405 | Entry::Vacant(entry) => { |
| 406 | let mut config = g_state |
| 407 | .current_config |
| 408 | .find(¤t_config.name) |
| 409 | .cloned() |
| 410 | .unwrap_or_else(|| niri_config::Output { |
| 411 | name: current_config.name.clone(), |
| 412 | ..Default::default() |
| 413 | }); |
| 414 | config.off = false; |
| 415 | entry.insert(config); |
| 416 | } |
| 417 | }; |
| 418 | |
| 419 | data_init.init(id, OutputConfigurationHeadState::Ok(*output, conf.clone())); |
| 420 | } |
| 421 | zwlr_output_configuration_v1::Request::DisableHead { head } => { |
| 422 | if outdated { |
| 423 | return; |
| 424 | } |
| 425 | let Some(output) = head.data::<OutputId>() else { |
| 426 | error!("DisableHead: missing attached output head name"); |
| 427 | return; |
| 428 | }; |
| 429 | |
| 430 | let Some(new_config) = new_config else { |
| 431 | return; |
| 432 | }; |
| 433 | |
| 434 | let OutputConfigurationState::Ongoing(new_config) = new_config else { |
| 435 | conf.post_error( |
| 436 | zwlr_output_configuration_v1::Error::AlreadyUsed, |
| 437 | "configuration had already been used", |
| 438 | ); |
| 439 | return; |
| 440 | }; |
| 441 | |
| 442 | let Some(current_config) = g_state.current_state.get(output) else { |
| 443 | error!("EnableHead: output missing from current config"); |
| 444 | return; |
| 445 | }; |
| 446 | |
| 447 | match new_config.entry(*output) { |
| 448 | Entry::Occupied(_) => { |
| 449 | conf.post_error( |
| 450 | zwlr_output_configuration_v1::Error::AlreadyConfiguredHead, |
| 451 | "head has been already configured", |
| 452 | ); |
| 453 | } |
| 454 | Entry::Vacant(entry) => { |
| 455 | let mut config = g_state |
| 456 | .current_config |
| 457 | .find(¤t_config.name) |
| 458 | .cloned() |
| 459 | .unwrap_or_else(|| niri_config::Output { |
| 460 | name: current_config.name.clone(), |
| 461 | ..Default::default() |
| 462 | }); |
| 463 | config.off = true; |
| 464 | entry.insert(config); |
| 465 | } |
| 466 | }; |
| 467 | } |
| 468 | zwlr_output_configuration_v1::Request::Apply => { |
| 469 | if outdated { |
| 470 | conf.cancelled(); |
| 471 | return; |
| 472 | } |
| 473 | |
| 474 | let Some(new_config) = new_config else { |
| 475 | return; |
| 476 | }; |
| 477 | |
| 478 | let OutputConfigurationState::Ongoing(new_config) = |
| 479 | mem::replace(new_config, OutputConfigurationState::Finished) |
| 480 | else { |
| 481 | conf.post_error( |
| 482 | zwlr_output_configuration_v1::Error::AlreadyUsed, |
| 483 | "configuration had already been used", |
| 484 | ); |
| 485 | return; |
| 486 | }; |
| 487 | |
| 488 | let any_enabled = new_config.values().any(|c| !c.off); |
| 489 | if !any_enabled { |
| 490 | conf.failed(); |
| 491 | return; |
| 492 | } |
| 493 | |
| 494 | state.apply_output_config(new_config.into_values().collect()); |
| 495 | // FIXME: verify that it had been applied successfully (which may be difficult). |
| 496 | conf.succeeded(); |
| 497 | } |
| 498 | zwlr_output_configuration_v1::Request::Test => { |
| 499 | if outdated { |
| 500 | conf.cancelled(); |
| 501 | return; |
| 502 | } |
| 503 | |
| 504 | let Some(new_config) = new_config else { |
| 505 | return; |
| 506 | }; |
| 507 | |
| 508 | let OutputConfigurationState::Ongoing(new_config) = |
| 509 | mem::replace(new_config, OutputConfigurationState::Finished) |
| 510 | else { |
| 511 | conf.post_error( |
| 512 | zwlr_output_configuration_v1::Error::AlreadyUsed, |
| 513 | "configuration had already been used", |
| 514 | ); |
| 515 | return; |
| 516 | }; |
| 517 | |
| 518 | let any_enabled = new_config.values().any(|c| !c.off); |
| 519 | if !any_enabled { |
| 520 | conf.failed(); |
| 521 | return; |
| 522 | } |
| 523 | |
| 524 | // FIXME: actually test the configuration with TTY. |
| 525 | conf.succeeded() |
| 526 | } |
| 527 | zwlr_output_configuration_v1::Request::Destroy => { |
| 528 | g_state |
| 529 | .clients |
| 530 | .get_mut(&client.id()) |
| 531 | .map(|d| d.confs.remove(conf)); |
| 532 | } |
| 533 | _ => unreachable!(), |
| 534 | } |
| 535 | } |
| 536 | } |
| 537 | |
| 538 | impl<D> Dispatch<ZwlrOutputConfigurationHeadV1, OutputConfigurationHeadState, D> |
| 539 | for OutputManagementManagerState |
| 540 | where |
| 541 | D: GlobalDispatch<ZwlrOutputManagerV1, OutputManagementManagerGlobalData>, |
| 542 | D: Dispatch<ZwlrOutputManagerV1, ()>, |
| 543 | D: Dispatch<ZwlrOutputHeadV1, OutputId>, |
| 544 | D: Dispatch<ZwlrOutputConfigurationV1, u32>, |
| 545 | D: Dispatch<ZwlrOutputConfigurationHeadV1, OutputConfigurationHeadState>, |
| 546 | D: Dispatch<ZwlrOutputModeV1, ()>, |
| 547 | D: OutputManagementHandler, |
| 548 | D: 'static, |
| 549 | { |
| 550 | fn request( |
| 551 | state: &mut D, |
| 552 | client: &Client, |
| 553 | conf_head: &ZwlrOutputConfigurationHeadV1, |
| 554 | request: zwlr_output_configuration_head_v1::Request, |
| 555 | data: &OutputConfigurationHeadState, |
| 556 | _display: &DisplayHandle, |
| 557 | _data_init: &mut DataInit<'_, D>, |
| 558 | ) { |
| 559 | let g_state = state.output_management_state(); |
| 560 | let Some(client_data) = g_state.clients.get_mut(&client.id()) else { |
| 561 | error!("ConfigurationHead: missing client data"); |
| 562 | return; |
| 563 | }; |
| 564 | let OutputConfigurationHeadState::Ok(output_id, conf) = data else { |
| 565 | warn!("ConfigurationHead: request sent to a cancelled head"); |
| 566 | return; |
| 567 | }; |
| 568 | let Some(serial) = conf.data::<u32>() else { |
| 569 | error!("ConfigurationHead: missing serial"); |
| 570 | return; |
| 571 | }; |
| 572 | if *serial != g_state.serial { |
| 573 | warn!("ConfigurationHead: request sent to an outdated"); |
| 574 | return; |
| 575 | } |
| 576 | let Some(new_config) = client_data.confs.get_mut(conf) else { |
| 577 | error!("ConfigurationHead: unknown configuration"); |
| 578 | return; |
| 579 | }; |
| 580 | let OutputConfigurationState::Ongoing(new_config) = new_config else { |
| 581 | conf.post_error( |
| 582 | zwlr_output_configuration_v1::Error::AlreadyUsed, |
| 583 | "configuration had already been used", |
| 584 | ); |
| 585 | return; |
| 586 | }; |
| 587 | let Some(new_config) = new_config.get_mut(output_id) else { |
| 588 | error!("ConfigurationHead: config missing from enabled heads"); |
| 589 | return; |
| 590 | }; |
| 591 | |
| 592 | match request { |
| 593 | zwlr_output_configuration_head_v1::Request::SetMode { mode } => { |
| 594 | let index = match client_data |
| 595 | .heads |
| 596 | .get(output_id) |
| 597 | .map(|(_, mods)| mods.iter().position(|m| m.id() == mode.id())) |
| 598 | { |
| 599 | Some(Some(index)) => index, |
| 600 | _ => { |
| 601 | warn!("SetMode: failed to find requested mode"); |
| 602 | conf_head.post_error( |
| 603 | zwlr_output_configuration_head_v1::Error::InvalidMode, |
| 604 | "failed to find requested mode", |
| 605 | ); |
| 606 | return; |
| 607 | } |
| 608 | }; |
| 609 | |
| 610 | let Some(current_config) = g_state.current_state.get(output_id) else { |
| 611 | warn!("SetMode: output missing from the current config"); |
| 612 | return; |
| 613 | }; |
| 614 | |
| 615 | let Some(mode) = current_config.modes.get(index) else { |
| 616 | error!("SetMode: requested mode is out of range"); |
| 617 | return; |
| 618 | }; |
| 619 | |
| 620 | new_config.mode = Some(niri_ipc::ConfiguredMode { |
| 621 | width: mode.width, |
| 622 | height: mode.height, |
| 623 | refresh: Some(mode.refresh_rate as f64 / 1000.), |
| 624 | }); |
| 625 | } |
| 626 | zwlr_output_configuration_head_v1::Request::SetCustomMode { |
| 627 | width, |
| 628 | height, |
| 629 | refresh, |
| 630 | } => { |
| 631 | // FIXME: Support custom mode |
| 632 | let (width, height, refresh): (u16, u16, u32) = |
| 633 | match (width.try_into(), height.try_into(), refresh.try_into()) { |
| 634 | (Ok(width), Ok(height), Ok(refresh)) => (width, height, refresh), |
| 635 | _ => { |
| 636 | warn!("SetCustomMode: invalid input data"); |
| 637 | return; |
| 638 | } |
| 639 | }; |
| 640 | |
| 641 | let Some(current_config) = g_state.current_state.get(output_id) else { |
| 642 | warn!("SetMode: output missing from the current config"); |
| 643 | return; |
| 644 | }; |
| 645 | |
| 646 | let Some(mode) = current_config.modes.iter().find(|m| { |
| 647 | m.width == width |
| 648 | && m.height == height |
| 649 | && (refresh == 0 || m.refresh_rate == refresh) |
| 650 | }) else { |
| 651 | warn!("SetCustomMode: no matching mode"); |
| 652 | return; |
| 653 | }; |
| 654 | |
| 655 | new_config.mode = Some(niri_ipc::ConfiguredMode { |
| 656 | width: mode.width, |
| 657 | height: mode.height, |
| 658 | refresh: Some(mode.refresh_rate as f64 / 1000.), |
| 659 | }); |
| 660 | } |
| 661 | zwlr_output_configuration_head_v1::Request::SetPosition { x, y } => { |
| 662 | new_config.position = Some(niri_config::Position { x, y }); |
| 663 | } |
| 664 | zwlr_output_configuration_head_v1::Request::SetTransform { transform } => { |
| 665 | let transform = match transform { |
| 666 | WEnum::Value(WlTransform::Normal) => Transform::Normal, |
| 667 | WEnum::Value(WlTransform::_90) => Transform::_90, |
| 668 | WEnum::Value(WlTransform::_180) => Transform::_180, |
| 669 | WEnum::Value(WlTransform::_270) => Transform::_270, |
| 670 | WEnum::Value(WlTransform::Flipped) => Transform::Flipped, |
| 671 | WEnum::Value(WlTransform::Flipped90) => Transform::Flipped90, |
| 672 | WEnum::Value(WlTransform::Flipped180) => Transform::Flipped180, |
| 673 | WEnum::Value(WlTransform::Flipped270) => Transform::Flipped270, |
| 674 | _ => { |
| 675 | warn!("SetTransform: unknown requested transform"); |
| 676 | conf_head.post_error( |
| 677 | zwlr_output_configuration_head_v1::Error::InvalidTransform, |
| 678 | "unknown transform value", |
| 679 | ); |
| 680 | return; |
| 681 | } |
| 682 | }; |
| 683 | new_config.transform = transform; |
| 684 | } |
| 685 | zwlr_output_configuration_head_v1::Request::SetScale { scale } => { |
| 686 | if scale <= 0. { |
| 687 | conf_head.post_error( |
| 688 | zwlr_output_configuration_head_v1::Error::InvalidScale, |
| 689 | "scale is negative or zero", |
| 690 | ); |
| 691 | return; |
| 692 | } |
| 693 | new_config.scale = Some(FloatOrInt(scale)); |
| 694 | } |
| 695 | zwlr_output_configuration_head_v1::Request::SetAdaptiveSync { state } => { |
| 696 | let vrr = match state { |
| 697 | WEnum::Value(AdaptiveSyncState::Enabled) => Some(Vrr { on_demand: false }), |
| 698 | WEnum::Value(AdaptiveSyncState::Disabled) => None, |
| 699 | _ => { |
| 700 | warn!("SetAdaptativeSync: unknown requested adaptative sync"); |
| 701 | conf_head.post_error( |
| 702 | zwlr_output_configuration_head_v1::Error::InvalidAdaptiveSyncState, |
| 703 | "unknown adaptive sync value", |
| 704 | ); |
| 705 | return; |
| 706 | } |
| 707 | }; |
| 708 | new_config.variable_refresh_rate = vrr; |
| 709 | } |
| 710 | _ => unreachable!(), |
| 711 | } |
| 712 | } |
| 713 | } |
| 714 | |
| 715 | impl<D> Dispatch<ZwlrOutputHeadV1, OutputId, D> for OutputManagementManagerState |
| 716 | where |
| 717 | D: GlobalDispatch<ZwlrOutputManagerV1, OutputManagementManagerGlobalData>, |
| 718 | D: Dispatch<ZwlrOutputManagerV1, ()>, |
| 719 | D: Dispatch<ZwlrOutputHeadV1, OutputId>, |
| 720 | D: Dispatch<ZwlrOutputConfigurationV1, u32>, |
| 721 | D: Dispatch<ZwlrOutputConfigurationHeadV1, OutputConfigurationHeadState>, |
| 722 | D: Dispatch<ZwlrOutputModeV1, ()>, |
| 723 | D: OutputManagementHandler, |
| 724 | D: 'static, |
| 725 | { |
| 726 | fn request( |
| 727 | _state: &mut D, |
| 728 | _client: &Client, |
| 729 | _output_head: &ZwlrOutputHeadV1, |
| 730 | request: zwlr_output_head_v1::Request, |
| 731 | _data: &OutputId, |
| 732 | _display: &DisplayHandle, |
| 733 | _data_init: &mut DataInit<'_, D>, |
| 734 | ) { |
| 735 | match request { |
| 736 | zwlr_output_head_v1::Request::Release => {} |
| 737 | _ => unreachable!(), |
| 738 | } |
| 739 | } |
| 740 | fn destroyed(state: &mut D, client: ClientId, _resource: &ZwlrOutputHeadV1, data: &OutputId) { |
| 741 | if let Some(c) = state.output_management_state().clients.get_mut(&client) { |
| 742 | c.heads.remove(data); |
| 743 | } |
| 744 | } |
| 745 | } |
| 746 | |
| 747 | impl<D> Dispatch<ZwlrOutputModeV1, (), D> for OutputManagementManagerState |
| 748 | where |
| 749 | D: GlobalDispatch<ZwlrOutputManagerV1, OutputManagementManagerGlobalData>, |
| 750 | D: Dispatch<ZwlrOutputManagerV1, ()>, |
| 751 | D: Dispatch<ZwlrOutputHeadV1, OutputId>, |
| 752 | D: Dispatch<ZwlrOutputConfigurationV1, u32>, |
| 753 | D: Dispatch<ZwlrOutputConfigurationHeadV1, OutputConfigurationHeadState>, |
| 754 | D: Dispatch<ZwlrOutputModeV1, ()>, |
| 755 | D: OutputManagementHandler, |
| 756 | D: 'static, |
| 757 | { |
| 758 | fn request( |
| 759 | _state: &mut D, |
| 760 | _client: &Client, |
| 761 | _mode: &ZwlrOutputModeV1, |
| 762 | request: zwlr_output_mode_v1::Request, |
| 763 | _data: &(), |
| 764 | _display: &DisplayHandle, |
| 765 | _data_init: &mut DataInit<'_, D>, |
| 766 | ) { |
| 767 | match request { |
| 768 | zwlr_output_mode_v1::Request::Release => {} |
| 769 | _ => unreachable!(), |
| 770 | } |
| 771 | } |
| 772 | } |
| 773 | |
| 774 | #[macro_export] |
| 775 | macro_rules! delegate_output_management{ |
| 776 | ($(@<$( $lt:tt $( : $clt:tt $(+ $dlt:tt )* )? ),+>)? $ty: ty) => { |
| 777 | smithay::reexports::wayland_server::delegate_global_dispatch!($(@< $( $lt $( : $clt $(+ $dlt )* )? ),+ >)? $ty: [ |
| 778 | smithay::reexports::wayland_protocols_wlr::output_management::v1::server::zwlr_output_manager_v1::ZwlrOutputManagerV1: $crate::protocols::output_management::OutputManagementManagerGlobalData |
| 779 | ] => $crate::protocols::output_management::OutputManagementManagerState); |
| 780 | smithay::reexports::wayland_server::delegate_dispatch!($(@< $( $lt $( : $clt $(+ $dlt )* )? ),+ >)? $ty: [ |
| 781 | smithay::reexports::wayland_protocols_wlr::output_management::v1::server::zwlr_output_manager_v1::ZwlrOutputManagerV1: () |
| 782 | ] => $crate::protocols::output_management::OutputManagementManagerState); |
| 783 | smithay::reexports::wayland_server::delegate_dispatch!($(@< $( $lt $( : $clt $(+ $dlt )* )? ),+ >)? $ty: [ |
| 784 | smithay::reexports::wayland_protocols_wlr::output_management::v1::server::zwlr_output_configuration_v1::ZwlrOutputConfigurationV1: u32 |
| 785 | ] => $crate::protocols::output_management::OutputManagementManagerState); |
| 786 | smithay::reexports::wayland_server::delegate_dispatch!($(@< $( $lt $( : $clt $(+ $dlt )* )? ),+ >)? $ty: [ |
| 787 | smithay::reexports::wayland_protocols_wlr::output_management::v1::server::zwlr_output_head_v1::ZwlrOutputHeadV1: $crate::backend::OutputId |
| 788 | ] => $crate::protocols::output_management::OutputManagementManagerState); |
| 789 | smithay::reexports::wayland_server::delegate_dispatch!($(@< $( $lt $( : $clt $(+ $dlt )* )? ),+ >)? $ty: [ |
| 790 | smithay::reexports::wayland_protocols_wlr::output_management::v1::server::zwlr_output_mode_v1::ZwlrOutputModeV1: () |
| 791 | ] => $crate::protocols::output_management::OutputManagementManagerState); |
| 792 | smithay::reexports::wayland_server::delegate_dispatch!($(@< $( $lt $( : $clt $(+ $dlt )* )? ),+ >)? $ty: [ |
| 793 | smithay::reexports::wayland_protocols_wlr::output_management::v1::server::zwlr_output_configuration_head_v1::ZwlrOutputConfigurationHeadV1: $crate::protocols::output_management::OutputConfigurationHeadState |
| 794 | ] => $crate::protocols::output_management::OutputManagementManagerState); |
| 795 | }; |
| 796 | } |
| 797 | |
| 798 | fn notify_removed_head(clients: &mut HashMap<ClientId, ClientData>, head: &OutputId) { |
| 799 | for data in clients.values_mut() { |
| 800 | if let Some((head, mods)) = data.heads.remove(head) { |
| 801 | mods.iter().for_each(|m| m.finished()); |
| 802 | head.finished(); |
| 803 | } |
| 804 | } |
| 805 | } |
| 806 | |
| 807 | fn notify_new_head( |
| 808 | state: &mut OutputManagementManagerState, |
| 809 | output: &OutputId, |
| 810 | conf: &niri_ipc::Output, |
| 811 | ) { |
| 812 | let display = &state.display; |
| 813 | let clients = &mut state.clients; |
| 814 | for data in clients.values_mut() { |
| 815 | if let Some(client) = data.manager.client() { |
| 816 | send_new_head::<State>(display, &client, data, *output, conf); |
| 817 | } |
| 818 | } |
| 819 | } |
| 820 | |
| 821 | fn send_new_head<D>( |
| 822 | display: &DisplayHandle, |
| 823 | client: &Client, |
| 824 | client_data: &mut ClientData, |
| 825 | output: OutputId, |
| 826 | conf: &niri_ipc::Output, |
| 827 | ) where |
| 828 | D: GlobalDispatch<ZwlrOutputManagerV1, OutputManagementManagerGlobalData>, |
| 829 | D: Dispatch<ZwlrOutputManagerV1, ()>, |
| 830 | D: Dispatch<ZwlrOutputConfigurationV1, u32>, |
| 831 | D: Dispatch<ZwlrOutputConfigurationHeadV1, OutputConfigurationHeadState>, |
| 832 | D: Dispatch<ZwlrOutputModeV1, ()>, |
| 833 | D: OutputManagementHandler, |
| 834 | D: 'static, |
| 835 | D: Dispatch<ZwlrOutputHeadV1, OutputId>, |
| 836 | D: Dispatch<ZwlrOutputModeV1, ()>, |
| 837 | D: 'static, |
| 838 | { |
| 839 | let new_head = client |
| 840 | .create_resource::<ZwlrOutputHeadV1, _, D>(display, client_data.manager.version(), output) |
| 841 | .unwrap(); |
| 842 | client_data.manager.head(&new_head); |
| 843 | new_head.name(conf.name.clone()); |
| 844 | new_head.description(format!("{} - {} - {}", conf.make, conf.model, conf.name)); |
| 845 | if let Some((width, height)) = conf.physical_size { |
| 846 | if let (Ok(a), Ok(b)) = (width.try_into(), height.try_into()) { |
| 847 | new_head.physical_size(a, b); |
| 848 | } |
| 849 | } |
| 850 | let mut new_modes = Vec::with_capacity(conf.modes.len()); |
| 851 | for (index, mode) in conf.modes.iter().enumerate() { |
| 852 | let new_mode = client |
| 853 | .create_resource::<ZwlrOutputModeV1, _, D>(display, new_head.version(), ()) |
| 854 | .unwrap(); |
| 855 | new_head.mode(&new_mode); |
| 856 | new_mode.size(i32::from(mode.width), i32::from(mode.height)); |
| 857 | if mode.is_preferred { |
| 858 | new_mode.preferred(); |
| 859 | } |
| 860 | if let Ok(refresh_rate) = mode.refresh_rate.try_into() { |
| 861 | new_mode.refresh(refresh_rate); |
| 862 | } |
| 863 | if Some(index) == conf.current_mode { |
| 864 | new_head.current_mode(&new_mode); |
| 865 | } |
| 866 | new_modes.push(new_mode); |
| 867 | } |
| 868 | if let Some(logical) = conf.logical { |
| 869 | new_head.position(logical.x, logical.y); |
| 870 | new_head.transform(ipc_transform_to_smithay(logical.transform).into()); |
| 871 | new_head.scale(logical.scale); |
| 872 | } |
| 873 | new_head.enabled(conf.current_mode.is_some() as i32); |
| 874 | if new_head.version() >= zwlr_output_head_v1::EVT_MAKE_SINCE { |
| 875 | new_head.make(conf.make.clone()); |
| 876 | } |
| 877 | if new_head.version() >= zwlr_output_head_v1::EVT_MODEL_SINCE { |
| 878 | new_head.model(conf.model.clone()); |
| 879 | } |
| 880 | |
| 881 | if new_head.version() >= zwlr_output_head_v1::EVT_ADAPTIVE_SYNC_SINCE { |
| 882 | new_head.adaptive_sync(match conf.vrr_enabled { |
| 883 | true => AdaptiveSyncState::Enabled, |
| 884 | false => AdaptiveSyncState::Disabled, |
| 885 | }); |
| 886 | } |
| 887 | // new_head.serial_number(output.serial); |
| 888 | client_data.heads.insert(output, (new_head, new_modes)); |
| 889 | } |