| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | use std::mem; |
| 7 | use std::sync::{Arc, Mutex}; |
| 8 | |
| 9 | use niri_config::OutputName; |
| 10 | use smithay::backend::allocator::dmabuf::Dmabuf; |
| 11 | use smithay::backend::renderer::element::RenderElementStates; |
| 12 | use smithay::backend::renderer::gles::GlesRenderer; |
| 13 | use smithay::output::{Mode, Output, PhysicalProperties, Subpixel}; |
| 14 | use smithay::reexports::wayland_protocols::wp::presentation_time::server::wp_presentation_feedback; |
| 15 | use smithay::utils::Size; |
| 16 | use smithay::wayland::presentation::Refresh; |
| 17 | |
| 18 | use super::{IpcOutputMap, OutputId, RenderResult}; |
| 19 | use crate::niri::{Niri, RedrawState}; |
| 20 | use crate::utils::{get_monotonic_time, logical_output}; |
| 21 | |
| 22 | pub struct Headless { |
| 23 | ipc_outputs: Arc<Mutex<IpcOutputMap>>, |
| 24 | } |
| 25 | |
| 26 | impl Headless { |
| 27 | pub fn new() -> Self { |
| 28 | Self { |
| 29 | ipc_outputs: Default::default(), |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | pub fn init(&mut self, _niri: &mut Niri) {} |
| 34 | |
| 35 | pub fn add_output(&mut self, niri: &mut Niri, n: u8, size: (u16, u16)) { |
| 36 | let connector = format!("headless-{n}"); |
| 37 | let make = "niri".to_string(); |
| 38 | let model = "headless".to_string(); |
| 39 | let serial = n.to_string(); |
| 40 | |
| 41 | let output = Output::new( |
| 42 | connector.clone(), |
| 43 | PhysicalProperties { |
| 44 | size: (0, 0).into(), |
| 45 | subpixel: Subpixel::Unknown, |
| 46 | make: make.clone(), |
| 47 | model: model.clone(), |
| 48 | }, |
| 49 | ); |
| 50 | |
| 51 | let mode = Mode { |
| 52 | size: Size::from((i32::from(size.0), i32::from(size.1))), |
| 53 | refresh: 60_000, |
| 54 | }; |
| 55 | output.change_current_state(Some(mode), None, None, None); |
| 56 | output.set_preferred(mode); |
| 57 | |
| 58 | output.user_data().insert_if_missing(|| OutputName { |
| 59 | connector, |
| 60 | make: Some(make), |
| 61 | model: Some(model), |
| 62 | serial: Some(serial), |
| 63 | }); |
| 64 | |
| 65 | let physical_properties = output.physical_properties(); |
| 66 | self.ipc_outputs.lock().unwrap().insert( |
| 67 | OutputId::next(), |
| 68 | niri_ipc::Output { |
| 69 | name: output.name(), |
| 70 | make: physical_properties.make, |
| 71 | model: physical_properties.model, |
| 72 | serial: None, |
| 73 | physical_size: None, |
| 74 | modes: vec![niri_ipc::Mode { |
| 75 | width: size.0, |
| 76 | height: size.1, |
| 77 | refresh_rate: 60_000, |
| 78 | is_preferred: true, |
| 79 | }], |
| 80 | current_mode: Some(0), |
| 81 | vrr_supported: false, |
| 82 | vrr_enabled: false, |
| 83 | logical: Some(logical_output(&output)), |
| 84 | }, |
| 85 | ); |
| 86 | |
| 87 | niri.add_output(output, None, false); |
| 88 | } |
| 89 | |
| 90 | pub fn seat_name(&self) -> String { |
| 91 | "headless".to_owned() |
| 92 | } |
| 93 | |
| 94 | pub fn with_primary_renderer<T>( |
| 95 | &mut self, |
| 96 | _f: impl FnOnce(&mut GlesRenderer) -> T, |
| 97 | ) -> Option<T> { |
| 98 | None |
| 99 | } |
| 100 | |
| 101 | pub fn render(&mut self, niri: &mut Niri, output: &Output) -> RenderResult { |
| 102 | let states = RenderElementStates::default(); |
| 103 | let mut presentation_feedbacks = niri.take_presentation_feedbacks(output, &states); |
| 104 | presentation_feedbacks.presented::<_, smithay::utils::Monotonic>( |
| 105 | get_monotonic_time(), |
| 106 | Refresh::Unknown, |
| 107 | 0, |
| 108 | wp_presentation_feedback::Kind::empty(), |
| 109 | ); |
| 110 | |
| 111 | let output_state = niri.output_state.get_mut(output).unwrap(); |
| 112 | match mem::replace(&mut output_state.redraw_state, RedrawState::Idle) { |
| 113 | RedrawState::Idle => unreachable!(), |
| 114 | RedrawState::Queued => (), |
| 115 | RedrawState::WaitingForVBlank { .. } => unreachable!(), |
| 116 | RedrawState::WaitingForEstimatedVBlank(_) => unreachable!(), |
| 117 | RedrawState::WaitingForEstimatedVBlankAndQueued(_) => unreachable!(), |
| 118 | } |
| 119 | |
| 120 | output_state.frame_callback_sequence = output_state.frame_callback_sequence.wrapping_add(1); |
| 121 | |
| 122 | |
| 123 | |
| 124 | RenderResult::Submitted |
| 125 | } |
| 126 | |
| 127 | pub fn import_dmabuf(&mut self, _dmabuf: &Dmabuf) -> bool { |
| 128 | unimplemented!() |
| 129 | } |
| 130 | |
| 131 | pub fn ipc_outputs(&self) -> Arc<Mutex<IpcOutputMap>> { |
| 132 | self.ipc_outputs.clone() |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | impl Default for Headless { |
| 137 | fn default() -> Self { |
| 138 | Self::new() |
| 139 | } |
| 140 | } |