niri
Clone
https://git.tahinli.com/Tahinli/niri.git
0 watching·0 forks·Rust·GPL-3.0·12.1 MiB·f51dd67·Ivan Molodetskikh·2 years ago·981 commits
| 1 | use std::sync::atomic::{AtomicBool, Ordering}; |
| 2 | use std::sync::Arc; |
| 3 | use std::time::UNIX_EPOCH; |
| 4 | |
| 5 | use smithay::output::Output; |
| 6 | use smithay::reexports::wayland_protocols_wlr::screencopy::v1::server::zwlr_screencopy_frame_v1::{ |
| 7 | Flags, ZwlrScreencopyFrameV1, |
| 8 | }; |
| 9 | use smithay::reexports::wayland_protocols_wlr::screencopy::v1::server::zwlr_screencopy_manager_v1::ZwlrScreencopyManagerV1; |
| 10 | use smithay::reexports::wayland_protocols_wlr::screencopy::v1::server::{ |
| 11 | zwlr_screencopy_frame_v1, zwlr_screencopy_manager_v1, |
| 12 | }; |
| 13 | use smithay::reexports::wayland_server::protocol::wl_buffer::WlBuffer; |
| 14 | use smithay::reexports::wayland_server::protocol::wl_shm; |
| 15 | use smithay::reexports::wayland_server::{ |
| 16 | Client, DataInit, Dispatch, DisplayHandle, GlobalDispatch, New, Resource, |
| 17 | }; |
| 18 | use smithay::utils::{Physical, Point, Rectangle, Size}; |
| 19 | use smithay::wayland::shm; |
| 20 | |
| 21 | // We do not support copy_with_damage() semantics yet. |
| 22 | const VERSION: u32 = 1; |
| 23 | |
| 24 | pub struct ScreencopyManagerState; |
| 25 | |
| 26 | pub struct ScreencopyManagerGlobalData { |
| 27 | filter: Box<dyn for<'c> Fn(&'c Client) -> bool + Send + Sync>, |
| 28 | } |
| 29 | |
| 30 | impl ScreencopyManagerState { |
| 31 | pub fn new<D, F>(display: &DisplayHandle, filter: F) -> Self |
| 32 | where |
| 33 | D: GlobalDispatch<ZwlrScreencopyManagerV1, ScreencopyManagerGlobalData>, |
| 34 | D: Dispatch<ZwlrScreencopyManagerV1, ()>, |
| 35 | D: Dispatch<ZwlrScreencopyFrameV1, ScreencopyFrameState>, |
| 36 | D: ScreencopyHandler, |
| 37 | D: 'static, |
| 38 | F: for<'c> Fn(&'c Client) -> bool + Send + Sync + 'static, |
| 39 | { |
| 40 | let global_data = ScreencopyManagerGlobalData { |
| 41 | filter: Box::new(filter), |
| 42 | }; |
| 43 | display.create_global::<D, ZwlrScreencopyManagerV1, _>(VERSION, global_data); |
| 44 | |
| 45 | Self |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | impl<D> GlobalDispatch<ZwlrScreencopyManagerV1, ScreencopyManagerGlobalData, D> |
| 50 | for ScreencopyManagerState |
| 51 | where |
| 52 | D: GlobalDispatch<ZwlrScreencopyManagerV1, ScreencopyManagerGlobalData>, |
| 53 | D: Dispatch<ZwlrScreencopyManagerV1, ()>, |
| 54 | D: Dispatch<ZwlrScreencopyFrameV1, ScreencopyFrameState>, |
| 55 | D: ScreencopyHandler, |
| 56 | D: 'static, |
| 57 | { |
| 58 | fn bind( |
| 59 | _state: &mut D, |
| 60 | _display: &DisplayHandle, |
| 61 | _client: &Client, |
| 62 | manager: New<ZwlrScreencopyManagerV1>, |
| 63 | _manager_state: &ScreencopyManagerGlobalData, |
| 64 | data_init: &mut DataInit<'_, D>, |
| 65 | ) { |
| 66 | data_init.init(manager, ()); |
| 67 | } |
| 68 | |
| 69 | fn can_view(client: Client, global_data: &ScreencopyManagerGlobalData) -> bool { |
| 70 | (global_data.filter)(&client) |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | impl<D> Dispatch<ZwlrScreencopyManagerV1, (), D> for ScreencopyManagerState |
| 75 | where |
| 76 | D: GlobalDispatch<ZwlrScreencopyManagerV1, ScreencopyManagerGlobalData>, |
| 77 | D: Dispatch<ZwlrScreencopyManagerV1, ()>, |
| 78 | D: Dispatch<ZwlrScreencopyFrameV1, ScreencopyFrameState>, |
| 79 | D: ScreencopyHandler, |
| 80 | D: 'static, |
| 81 | { |
| 82 | fn request( |
| 83 | _state: &mut D, |
| 84 | _client: &Client, |
| 85 | _manager: &ZwlrScreencopyManagerV1, |
| 86 | request: zwlr_screencopy_manager_v1::Request, |
| 87 | _data: &(), |
| 88 | _display: &DisplayHandle, |
| 89 | data_init: &mut DataInit<'_, D>, |
| 90 | ) { |
| 91 | let (frame, overlay_cursor, buffer_size, region_loc, output) = match request { |
| 92 | zwlr_screencopy_manager_v1::Request::CaptureOutput { |
| 93 | frame, |
| 94 | overlay_cursor, |
| 95 | output, |
| 96 | } => { |
| 97 | let output = Output::from_resource(&output).unwrap(); |
| 98 | let buffer_size = output.current_mode().unwrap().size; |
| 99 | let region_loc = Point::from((0, 0)); |
| 100 | |
| 101 | (frame, overlay_cursor, buffer_size, region_loc, output) |
| 102 | } |
| 103 | zwlr_screencopy_manager_v1::Request::CaptureOutputRegion { |
| 104 | frame, |
| 105 | overlay_cursor, |
| 106 | x, |
| 107 | y, |
| 108 | width, |
| 109 | height, |
| 110 | output, |
| 111 | } => { |
| 112 | if width <= 0 || height <= 0 { |
| 113 | trace!("screencopy client requested invalid sized region"); |
| 114 | let frame = data_init.init(frame, ScreencopyFrameState::Failed); |
| 115 | frame.failed(); |
| 116 | return; |
| 117 | } |
| 118 | |
| 119 | let output = Output::from_resource(&output).unwrap(); |
| 120 | let output_transform = output.current_transform(); |
| 121 | let output_physical_size = |
| 122 | output_transform.transform_size(output.current_mode().unwrap().size); |
| 123 | let output_rect = Rectangle::from_loc_and_size((0, 0), output_physical_size); |
| 124 | |
| 125 | let rect = Rectangle::from_loc_and_size((x, y), (width, height)); |
| 126 | |
| 127 | let output_scale = output.current_scale().integer_scale(); |
| 128 | let physical_rect = rect.to_physical(output_scale); |
| 129 | |
| 130 | // Clamp captured region to the output. |
| 131 | let Some(clamped_rect) = physical_rect.intersection(output_rect) else { |
| 132 | trace!("screencopy client requested region outside of output"); |
| 133 | let frame = data_init.init(frame, ScreencopyFrameState::Failed); |
| 134 | frame.failed(); |
| 135 | return; |
| 136 | }; |
| 137 | |
| 138 | let untransformed_rect = output_transform |
| 139 | .invert() |
| 140 | .transform_rect_in(clamped_rect, &output_physical_size); |
| 141 | |
| 142 | ( |
| 143 | frame, |
| 144 | overlay_cursor, |
| 145 | untransformed_rect.size, |
| 146 | clamped_rect.loc, |
| 147 | output, |
| 148 | ) |
| 149 | } |
| 150 | zwlr_screencopy_manager_v1::Request::Destroy => return, |
| 151 | _ => unreachable!(), |
| 152 | }; |
| 153 | |
| 154 | // Create the frame. |
| 155 | let overlay_cursor = overlay_cursor != 0; |
| 156 | let info = ScreencopyFrameInfo { |
| 157 | output, |
| 158 | overlay_cursor, |
| 159 | buffer_size, |
| 160 | region_loc, |
| 161 | }; |
| 162 | let frame = data_init.init( |
| 163 | frame, |
| 164 | ScreencopyFrameState::Pending { |
| 165 | info, |
| 166 | copied: Arc::new(AtomicBool::new(false)), |
| 167 | }, |
| 168 | ); |
| 169 | |
| 170 | // Send desired SHM buffer parameters. |
| 171 | frame.buffer( |
| 172 | wl_shm::Format::Argb8888, |
| 173 | buffer_size.w as u32, |
| 174 | buffer_size.h as u32, |
| 175 | buffer_size.w as u32 * 4, |
| 176 | ); |
| 177 | |
| 178 | // if manager.version() >= 3 { |
| 179 | // // Send desired DMA buffer parameters. |
| 180 | // frame.linux_dmabuf( |
| 181 | // Fourcc::Argb8888 as u32, |
| 182 | // buffer_size.w as u32, |
| 183 | // buffer_size.h as u32, |
| 184 | // ); |
| 185 | // |
| 186 | // // Notify client that all supported buffers were enumerated. |
| 187 | // frame.buffer_done(); |
| 188 | // } |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | /// Handler trait for wlr-screencopy. |
| 193 | pub trait ScreencopyHandler { |
| 194 | /// Handle new screencopy request. |
| 195 | fn frame(&mut self, frame: Screencopy); |
| 196 | } |
| 197 | |
| 198 | #[allow(missing_docs)] |
| 199 | #[macro_export] |
| 200 | macro_rules! delegate_screencopy { |
| 201 | ($(@<$( $lt:tt $( : $clt:tt $(+ $dlt:tt )* )? ),+>)? $ty: ty) => { |
| 202 | smithay::reexports::wayland_server::delegate_global_dispatch!($(@< $( $lt $( : $clt $(+ $dlt )* )? ),+ >)? $ty: [ |
| 203 | smithay::reexports::wayland_protocols_wlr::screencopy::v1::server::zwlr_screencopy_manager_v1::ZwlrScreencopyManagerV1: $crate::protocols::screencopy::ScreencopyManagerGlobalData |
| 204 | ] => $crate::protocols::screencopy::ScreencopyManagerState); |
| 205 | |
| 206 | smithay::reexports::wayland_server::delegate_dispatch!($(@< $( $lt $( : $clt $(+ $dlt )* )? ),+ >)? $ty: [ |
| 207 | smithay::reexports::wayland_protocols_wlr::screencopy::v1::server::zwlr_screencopy_manager_v1::ZwlrScreencopyManagerV1: () |
| 208 | ] => $crate::protocols::screencopy::ScreencopyManagerState); |
| 209 | |
| 210 | smithay::reexports::wayland_server::delegate_dispatch!($(@< $( $lt $( : $clt $(+ $dlt )* )? ),+ >)? $ty: [ |
| 211 | smithay::reexports::wayland_protocols_wlr::screencopy::v1::server::zwlr_screencopy_frame_v1::ZwlrScreencopyFrameV1: $crate::protocols::screencopy::ScreencopyFrameState |
| 212 | ] => $crate::protocols::screencopy::ScreencopyManagerState); |
| 213 | }; |
| 214 | } |
| 215 | |
| 216 | #[derive(Clone)] |
| 217 | pub struct ScreencopyFrameInfo { |
| 218 | output: Output, |
| 219 | buffer_size: Size<i32, Physical>, |
| 220 | region_loc: Point<i32, Physical>, |
| 221 | overlay_cursor: bool, |
| 222 | } |
| 223 | |
| 224 | pub enum ScreencopyFrameState { |
| 225 | Failed, |
| 226 | Pending { |
| 227 | info: ScreencopyFrameInfo, |
| 228 | copied: Arc<AtomicBool>, |
| 229 | }, |
| 230 | } |
| 231 | |
| 232 | impl<D> Dispatch<ZwlrScreencopyFrameV1, ScreencopyFrameState, D> for ScreencopyManagerState |
| 233 | where |
| 234 | D: Dispatch<ZwlrScreencopyFrameV1, ScreencopyFrameState>, |
| 235 | D: ScreencopyHandler, |
| 236 | D: 'static, |
| 237 | { |
| 238 | fn request( |
| 239 | state: &mut D, |
| 240 | _client: &Client, |
| 241 | frame: &ZwlrScreencopyFrameV1, |
| 242 | request: zwlr_screencopy_frame_v1::Request, |
| 243 | data: &ScreencopyFrameState, |
| 244 | _display: &DisplayHandle, |
| 245 | _data_init: &mut DataInit<'_, D>, |
| 246 | ) { |
| 247 | if matches!(request, zwlr_screencopy_frame_v1::Request::Destroy) { |
| 248 | return; |
| 249 | } |
| 250 | |
| 251 | let (info, copied) = match data { |
| 252 | ScreencopyFrameState::Failed => return, |
| 253 | ScreencopyFrameState::Pending { info, copied } => (info, copied), |
| 254 | }; |
| 255 | |
| 256 | if copied.load(Ordering::SeqCst) { |
| 257 | frame.post_error( |
| 258 | zwlr_screencopy_frame_v1::Error::AlreadyUsed, |
| 259 | "copy was already requested", |
| 260 | ); |
| 261 | return; |
| 262 | } |
| 263 | |
| 264 | let (buffer, with_damage) = match request { |
| 265 | zwlr_screencopy_frame_v1::Request::Copy { buffer } => (buffer, false), |
| 266 | // zwlr_screencopy_frame_v1::Request::CopyWithDamage { buffer } => (buffer, true), |
| 267 | _ => unreachable!(), |
| 268 | }; |
| 269 | |
| 270 | if !shm::with_buffer_contents(&buffer, |_buf, shm_len, buffer_data| { |
| 271 | buffer_data.format == wl_shm::Format::Argb8888 |
| 272 | && buffer_data.stride == info.buffer_size.w * 4 |
| 273 | && buffer_data.height == info.buffer_size.h |
| 274 | && shm_len as i32 == buffer_data.stride * buffer_data.height |
| 275 | }) |
| 276 | .unwrap_or(false) |
| 277 | { |
| 278 | frame.post_error( |
| 279 | zwlr_screencopy_frame_v1::Error::InvalidBuffer, |
| 280 | "invalid buffer", |
| 281 | ); |
| 282 | return; |
| 283 | } |
| 284 | |
| 285 | copied.store(true, Ordering::SeqCst); |
| 286 | |
| 287 | state.frame(Screencopy { |
| 288 | with_damage, |
| 289 | buffer, |
| 290 | frame: frame.clone(), |
| 291 | info: info.clone(), |
| 292 | submitted: false, |
| 293 | }); |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | /// Screencopy frame. |
| 298 | pub struct Screencopy { |
| 299 | info: ScreencopyFrameInfo, |
| 300 | frame: ZwlrScreencopyFrameV1, |
| 301 | #[allow(unused)] |
| 302 | with_damage: bool, |
| 303 | buffer: WlBuffer, |
| 304 | submitted: bool, |
| 305 | } |
| 306 | |
| 307 | impl Drop for Screencopy { |
| 308 | fn drop(&mut self) { |
| 309 | if !self.submitted { |
| 310 | self.frame.failed(); |
| 311 | } |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | impl Screencopy { |
| 316 | /// Get the target buffer to copy to. |
| 317 | pub fn buffer(&self) -> &WlBuffer { |
| 318 | &self.buffer |
| 319 | } |
| 320 | |
| 321 | pub fn region_loc(&self) -> Point<i32, Physical> { |
| 322 | self.info.region_loc |
| 323 | } |
| 324 | |
| 325 | pub fn buffer_size(&self) -> Size<i32, Physical> { |
| 326 | self.info.buffer_size |
| 327 | } |
| 328 | |
| 329 | pub fn output(&self) -> &Output { |
| 330 | &self.info.output |
| 331 | } |
| 332 | |
| 333 | pub fn overlay_cursor(&self) -> bool { |
| 334 | self.info.overlay_cursor |
| 335 | } |
| 336 | |
| 337 | // pub fn damage(&mut self, damage: &[Rectangle<i32, Physical>]) { |
| 338 | // assert!(self.with_damage); |
| 339 | // |
| 340 | // for Rectangle { loc, size } in damage { |
| 341 | // self.frame |
| 342 | // .damage(loc.x as u32, loc.y as u32, size.w as u32, size.h as u32); |
| 343 | // } |
| 344 | // } |
| 345 | |
| 346 | /// Submit the copied content. |
| 347 | pub fn submit(mut self, y_invert: bool) { |
| 348 | // Notify client that buffer is ordinary. |
| 349 | self.frame.flags(if y_invert { |
| 350 | Flags::YInvert |
| 351 | } else { |
| 352 | Flags::empty() |
| 353 | }); |
| 354 | |
| 355 | // Notify client about successful copy. |
| 356 | let time = UNIX_EPOCH.elapsed().unwrap(); |
| 357 | let tv_sec_hi = (time.as_secs() >> 32) as u32; |
| 358 | let tv_sec_lo = (time.as_secs() & 0xFFFFFFFF) as u32; |
| 359 | let tv_nsec = time.subsec_nanos(); |
| 360 | self.frame.ready(tv_sec_hi, tv_sec_lo, tv_nsec); |
| 361 | |
| 362 | // Mark frame as submitted to ensure destructor isn't run. |
| 363 | self.submitted = true; |
| 364 | } |
| 365 | |
| 366 | // pub fn submit_after_sync<T>( |
| 367 | // self, |
| 368 | // y_invert: bool, |
| 369 | // sync_point: Option<OwnedFd>, |
| 370 | // event_loop: &LoopHandle<'_, T>, |
| 371 | // ) { |
| 372 | // match sync_point { |
| 373 | // None => self.submit(y_invert), |
| 374 | // Some(sync_fd) => { |
| 375 | // let source = Generic::new(sync_fd, Interest::READ, Mode::OneShot); |
| 376 | // let mut screencopy = Some(self); |
| 377 | // event_loop |
| 378 | // .insert_source(source, move |_, _, _| { |
| 379 | // screencopy.take().unwrap().submit(y_invert); |
| 380 | // Ok(PostAction::Remove) |
| 381 | // }) |
| 382 | // .unwrap(); |
| 383 | // } |
| 384 | // } |
| 385 | // } |
| 386 | } |