Store screenshot in clipboard
Browse files at this commite78def3e2c67a6b8cd50c46daab022454d3c7b8dParent: 082d7342 changed files +70 -8
- src/handlers/mod.rs +26 -2
- src/niri.rs +44 -6
src/handlers/mod.rsmodified+26 -2
| @@ -2,6 +2,12 @@ mod compositor; | |||
| 2 | mod layer_shell; | 2 | mod layer_shell; |
| 3 | mod xdg_shell; | 3 | mod xdg_shell; |
| 4 | 4 | ||
| 5 | +use std::fs::File; | ||
| 6 | +use std::io::Write; | ||
| 7 | +use std::os::fd::OwnedFd; | ||
| 8 | +use std::sync::Arc; | ||
| 9 | +use std::thread; | ||
| 10 | + | ||
| 5 | use smithay::backend::allocator::dmabuf::Dmabuf; | 11 | use smithay::backend::allocator::dmabuf::Dmabuf; |
| 6 | use smithay::backend::renderer::ImportDma; | 12 | use smithay::backend::renderer::ImportDma; |
| 7 | use smithay::desktop::PopupKind; | 13 | use smithay::desktop::PopupKind; |
| @@ -21,7 +27,7 @@ use smithay::wayland::selection::primary_selection::{ | |||
| 21 | set_primary_focus, PrimarySelectionHandler, PrimarySelectionState, | 27 | set_primary_focus, PrimarySelectionHandler, PrimarySelectionState, |
| 22 | }; | 28 | }; |
| 23 | use smithay::wayland::selection::wlr_data_control::{DataControlHandler, DataControlState}; | 29 | use smithay::wayland::selection::wlr_data_control::{DataControlHandler, DataControlState}; |
| 24 | -use smithay::wayland::selection::SelectionHandler; | 30 | +use smithay::wayland::selection::{SelectionHandler, SelectionTarget}; |
| 25 | use smithay::{ | 31 | use smithay::{ |
| 26 | delegate_data_control, delegate_data_device, delegate_dmabuf, delegate_input_method_manager, | 32 | delegate_data_control, delegate_data_device, delegate_dmabuf, delegate_input_method_manager, |
| 27 | delegate_output, delegate_pointer_gestures, delegate_presentation, delegate_primary_selection, | 33 | delegate_output, delegate_pointer_gestures, delegate_presentation, delegate_primary_selection, |
| @@ -76,7 +82,25 @@ delegate_input_method_manager!(State); | |||
| 76 | delegate_virtual_keyboard_manager!(State); | 82 | delegate_virtual_keyboard_manager!(State); |
| 77 | 83 | ||
| 78 | impl SelectionHandler for State { | 84 | impl SelectionHandler for State { |
| 79 | - type SelectionUserData = (); | 85 | + type SelectionUserData = Arc<[u8]>; |
| 86 | + | ||
| 87 | + fn send_selection( | ||
| 88 | + &mut self, | ||
| 89 | + _ty: SelectionTarget, | ||
| 90 | + _mime_type: String, | ||
| 91 | + fd: OwnedFd, | ||
| 92 | + _seat: Seat<Self>, | ||
| 93 | + user_data: &Self::SelectionUserData, | ||
| 94 | + ) { | ||
| 95 | + let _span = tracy_client::span!("send_selection"); | ||
| 96 | + | ||
| 97 | + let buf = user_data.clone(); | ||
| 98 | + thread::spawn(move || { | ||
| 99 | + if let Err(err) = File::from(fd).write_all(&buf) { | ||
| 100 | + warn!("error writing selection: {err:?}"); | ||
| 101 | + } | ||
| 102 | + }); | ||
| 103 | + } | ||
| 80 | } | 104 | } |
| 81 | 105 | ||
| 82 | impl DataDeviceHandler for State { | 106 | impl DataDeviceHandler for State { |
src/niri.rsmodified+44 -6
| @@ -9,6 +9,7 @@ use std::{env, mem, thread}; | |||
| 9 | 9 | ||
| 10 | use _server_decoration::server::org_kde_kwin_server_decoration_manager::Mode as KdeDecorationsMode; | 10 | use _server_decoration::server::org_kde_kwin_server_decoration_manager::Mode as KdeDecorationsMode; |
| 11 | use anyhow::Context; | 11 | use anyhow::Context; |
| 12 | +use image::ImageFormat; | ||
| 12 | use smithay::backend::allocator::Fourcc; | 13 | use smithay::backend::allocator::Fourcc; |
| 13 | use smithay::backend::renderer::element::surface::{ | 14 | use smithay::backend::renderer::element::surface::{ |
| 14 | render_elements_from_surface_tree, WaylandSurfaceRenderElement, | 15 | render_elements_from_surface_tree, WaylandSurfaceRenderElement, |
| @@ -34,7 +35,7 @@ use smithay::output::Output; | |||
| 34 | use smithay::reexports::calloop::generic::Generic; | 35 | use smithay::reexports::calloop::generic::Generic; |
| 35 | use smithay::reexports::calloop::timer::{TimeoutAction, Timer}; | 36 | use smithay::reexports::calloop::timer::{TimeoutAction, Timer}; |
| 36 | use smithay::reexports::calloop::{ | 37 | use smithay::reexports::calloop::{ |
| 37 | - Idle, Interest, LoopHandle, LoopSignal, Mode, PostAction, RegistrationToken, | 38 | + self, Idle, Interest, LoopHandle, LoopSignal, Mode, PostAction, RegistrationToken, |
| 38 | }; | 39 | }; |
| 39 | use smithay::reexports::nix::libc::CLOCK_MONOTONIC; | 40 | use smithay::reexports::nix::libc::CLOCK_MONOTONIC; |
| 40 | use smithay::reexports::wayland_protocols::xdg::shell::server::xdg_toplevel::WmCapabilities; | 41 | use smithay::reexports::wayland_protocols::xdg::shell::server::xdg_toplevel::WmCapabilities; |
| @@ -56,8 +57,10 @@ use smithay::wayland::input_method::InputMethodManagerState; | |||
| 56 | use smithay::wayland::output::OutputManagerState; | 57 | use smithay::wayland::output::OutputManagerState; |
| 57 | use smithay::wayland::pointer_gestures::PointerGesturesState; | 58 | use smithay::wayland::pointer_gestures::PointerGesturesState; |
| 58 | use smithay::wayland::presentation::PresentationState; | 59 | use smithay::wayland::presentation::PresentationState; |
| 59 | -use smithay::wayland::selection::data_device::DataDeviceState; | 60 | +use smithay::wayland::selection::data_device::{set_data_device_selection, DataDeviceState}; |
| 60 | -use smithay::wayland::selection::primary_selection::PrimarySelectionState; | 61 | +use smithay::wayland::selection::primary_selection::{ |
| 62 | + set_primary_selection, PrimarySelectionState, | ||
| 63 | +}; | ||
| 61 | use smithay::wayland::selection::wlr_data_control::DataControlState; | 64 | use smithay::wayland::selection::wlr_data_control::DataControlState; |
| 62 | use smithay::wayland::shell::kde::decoration::KdeDecorationState; | 65 | use smithay::wayland::shell::kde::decoration::KdeDecorationState; |
| 63 | use smithay::wayland::shell::wlr_layer::{Layer, WlrLayerShellState}; | 66 | use smithay::wayland::shell::wlr_layer::{Layer, WlrLayerShellState}; |
| @@ -308,7 +311,7 @@ impl State { | |||
| 308 | #[cfg(feature = "xdp-gnome-screencast")] | 311 | #[cfg(feature = "xdp-gnome-screencast")] |
| 309 | pub fn on_screen_cast_msg( | 312 | pub fn on_screen_cast_msg( |
| 310 | &mut self, | 313 | &mut self, |
| 311 | - to_niri: &smithay::reexports::calloop::channel::Sender<ScreenCastToNiri>, | 314 | + to_niri: &calloop::channel::Sender<ScreenCastToNiri>, |
| 312 | msg: ScreenCastToNiri, | 315 | msg: ScreenCastToNiri, |
| 313 | ) { | 316 | ) { |
| 314 | match msg { | 317 | match msg { |
| @@ -1510,14 +1513,49 @@ impl Niri { | |||
| 1510 | let path = make_screenshot_path().context("error making screenshot path")?; | 1513 | let path = make_screenshot_path().context("error making screenshot path")?; |
| 1511 | debug!("saving screenshot to {path:?}"); | 1514 | debug!("saving screenshot to {path:?}"); |
| 1512 | 1515 | ||
| 1516 | + // Prepare to set the encoded image as our clipboard selection. This must be done from the | ||
| 1517 | + // main thread. | ||
| 1518 | + let (tx, rx) = calloop::channel::sync_channel::<Arc<[u8]>>(1); | ||
| 1519 | + self.event_loop | ||
| 1520 | + .insert_source(rx, move |event, _, state| match event { | ||
| 1521 | + calloop::channel::Event::Msg(buf) => { | ||
| 1522 | + set_data_device_selection( | ||
| 1523 | + &state.niri.display_handle, | ||
| 1524 | + &state.niri.seat, | ||
| 1525 | + vec![String::from("image/png")], | ||
| 1526 | + buf.clone(), | ||
| 1527 | + ); | ||
| 1528 | + set_primary_selection( | ||
| 1529 | + &state.niri.display_handle, | ||
| 1530 | + &state.niri.seat, | ||
| 1531 | + vec![String::from("image/png")], | ||
| 1532 | + buf.clone(), | ||
| 1533 | + ); | ||
| 1534 | + } | ||
| 1535 | + calloop::channel::Event::Closed => (), | ||
| 1536 | + }) | ||
| 1537 | + .unwrap(); | ||
| 1538 | + | ||
| 1539 | + // Encode and save the image in a thread as it's slow. | ||
| 1513 | thread::spawn(move || { | 1540 | thread::spawn(move || { |
| 1514 | - if let Err(err) = image::save_buffer( | 1541 | + let mut buf = vec![]; |
| 1515 | - path, | 1542 | + |
| 1543 | + if let Err(err) = image::write_buffer_with_format( | ||
| 1544 | + &mut std::io::Cursor::new(&mut buf), | ||
| 1516 | &pixels, | 1545 | &pixels, |
| 1517 | size.w as u32, | 1546 | size.w as u32, |
| 1518 | size.h as u32, | 1547 | size.h as u32, |
| 1519 | image::ColorType::Rgba8, | 1548 | image::ColorType::Rgba8, |
| 1549 | + ImageFormat::Png, | ||
| 1520 | ) { | 1550 | ) { |
| 1551 | + warn!("error encoding screenshot image: {err:?}"); | ||
| 1552 | + return; | ||
| 1553 | + } | ||
| 1554 | + | ||
| 1555 | + let buf: Arc<[u8]> = Arc::from(buf.into_boxed_slice()); | ||
| 1556 | + let _ = tx.send(buf.clone()); | ||
| 1557 | + | ||
| 1558 | + if let Err(err) = std::fs::write(path, buf) { | ||
| 1521 | warn!("error saving screenshot image: {err:?}"); | 1559 | warn!("error saving screenshot image: {err:?}"); |
| 1522 | } | 1560 | } |
| 1523 | }); | 1561 | }); |