cosmic-comp/src/wayland/handlers/data_device.rs

105 lines
3.1 KiB
Rust
Raw Normal View History

// SPDX-License-Identifier: GPL-3.0-only
2022-07-04 16:00:29 +02:00
use crate::state::State;
use smithay::{
2022-07-04 16:00:29 +02:00
delegate_data_device,
2022-08-31 13:01:23 +02:00
input::Seat,
2022-07-04 16:00:29 +02:00
reexports::wayland_server::protocol::{wl_data_source::WlDataSource, wl_surface::WlSurface},
2022-08-30 13:28:36 +02:00
utils::IsAlive,
2022-08-31 13:01:23 +02:00
wayland::data_device::{
with_source_metadata, ClientDndGrabHandler, DataDeviceHandler, DataDeviceState,
ServerDndGrabHandler,
},
xwayland::xwm::{SelectionType, XwmId},
};
use std::{cell::RefCell, os::unix::io::OwnedFd};
use tracing::warn;
pub struct DnDIcon {
surface: RefCell<Option<WlSurface>>,
}
pub fn get_dnd_icon(seat: &Seat<State>) -> Option<WlSurface> {
let userdata = seat.user_data();
userdata
.get::<DnDIcon>()
.and_then(|x| x.surface.borrow().clone())
2022-08-05 16:28:05 +02:00
.filter(IsAlive::alive)
}
impl ClientDndGrabHandler for State {
fn started(
2022-07-04 16:00:29 +02:00
&mut self,
_source: Option<WlDataSource>,
icon: Option<WlSurface>,
seat: Seat<Self>,
) {
let user_data = seat.user_data();
user_data.insert_if_missing(|| DnDIcon {
surface: RefCell::new(None),
});
*user_data.get::<DnDIcon>().unwrap().surface.borrow_mut() = icon;
}
fn dropped(&mut self, seat: Seat<Self>) {
seat.user_data()
.get::<DnDIcon>()
.unwrap()
.surface
.borrow_mut()
.take();
}
}
impl ServerDndGrabHandler for State {}
impl DataDeviceHandler for State {
type SelectionUserData = XwmId;
fn data_device_state(&self) -> &DataDeviceState {
&self.common.data_device_state
}
fn new_selection(&mut self, source: Option<WlDataSource>, _seat: Seat<State>) {
2023-03-07 20:28:41 +01:00
if let Some(state) = self.common.xwayland_state.as_mut() {
if let Some(xwm) = state.xwm.as_mut() {
if let Some(source) = &source {
if let Ok(Err(err)) = with_source_metadata(source, |metadata| {
xwm.new_selection(
SelectionType::Clipboard,
Some(metadata.mime_types.clone()),
)
}) {
warn!(?err, "Failed to set Xwayland clipboard selection.");
}
} else if let Err(err) = xwm.new_selection(SelectionType::Clipboard, None) {
warn!(?err, "Failed to clear Xwayland clipboard selection.");
}
}
}
}
fn send_selection(
&mut self,
mime_type: String,
fd: OwnedFd,
_seat: Seat<State>,
2023-03-07 20:28:41 +01:00
_user_data: &Self::SelectionUserData,
) {
if let Some(xwm) = self
.common
.xwayland_state
2023-03-07 20:28:41 +01:00
.as_mut()
.and_then(|xstate| xstate.xwm.as_mut())
{
if let Err(err) = xwm.send_selection(
SelectionType::Clipboard,
mime_type,
fd,
self.common.event_loop_handle.clone(),
) {
warn!(?err, "Failed to send clipboard (X11 -> Wayland).");
}
}
}
}
delegate_data_device!(State);