forge
Explore
Log in

Don't you dare go Hollow

niri

Clone
★ 0

0 watching0 forksRustGPL-3.012.1 MiB9d8f4f8Ivan Molodetskikh1 month ago2997 commits

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