Skip to content

Commit

Permalink
wayland.selection: Remove TypeId in favor of DataDeviceKind
Browse files Browse the repository at this point in the history
  • Loading branch information
PolyMeilex committed Dec 26, 2024
1 parent 306d436 commit 869ed11
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 33 deletions.
32 changes: 7 additions & 25 deletions src/wayland/selection/device.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
use std::any::Any;
use std::any::TypeId;

use wayland_protocols::wp::primary_selection::zv1::server::zwp_primary_selection_device_v1::ZwpPrimarySelectionDeviceV1 as PrimaryDevice;
use wayland_protocols_wlr::data_control::v1::server::zwlr_data_control_device_v1::ZwlrDataControlDeviceV1;
use wayland_server::backend::ObjectId;
Expand All @@ -18,28 +15,14 @@ use super::primary_selection::PrimaryDeviceUserData;
use super::private::selection_dispatch;
use super::wlr_data_control::DataControlDeviceUserData;

#[derive(Debug, PartialEq, Eq)]
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub(super) enum DataDeviceKind {
Core,
Primary,
WlrDataControl,
ExtDataControl,
}

impl DataDeviceKind {
pub(super) fn from_device_type_id(id: TypeId) -> Option<Self> {
let kind = match id {
_ if id == TypeId::of::<WlDataDevice>() => Self::Core,
_ if id == TypeId::of::<PrimaryDevice>() => Self::Primary,
_ if id == TypeId::of::<ZwlrDataControlDeviceV1>() => Self::WlrDataControl,
_ if id == TypeId::of::<ExtDataControlDeviceV1>() => Self::ExtDataControl,
_ => return None,
};

Some(kind)
}
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum DataControlDevice {
Wlr(ZwlrDataControlDeviceV1),
Expand Down Expand Up @@ -104,7 +87,7 @@ impl DataControlDevice {
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum SelectionDevice {
pub(super) enum SelectionDevice {
DataDevice(WlDataDevice),
Primary(PrimaryDevice),
DataControl(DataControlDevice),
Expand All @@ -123,13 +106,12 @@ impl SelectionDevice {
selection_dispatch!(self; Self(device) => device.id())
}

/// Get the [`TypeId`] of the underlying data device provider.
pub fn inner_type_id(&self) -> TypeId {
pub fn device_kind(&self) -> DataDeviceKind {
match self {
Self::DataDevice(device) => device.type_id(),
Self::Primary(device) => device.type_id(),
Self::DataControl(DataControlDevice::Wlr(device)) => device.type_id(),
Self::DataControl(DataControlDevice::Ext(device)) => device.type_id(),
Self::DataDevice(_) => DataDeviceKind::Core,
Self::Primary(_) => DataDeviceKind::Primary,
Self::DataControl(DataControlDevice::Wlr(_)) => DataDeviceKind::WlrDataControl,
Self::DataControl(DataControlDevice::Ext(_)) => DataDeviceKind::ExtDataControl,
}
}

Expand Down
12 changes: 4 additions & 8 deletions src/wayland/selection/offer.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use std::any::TypeId;
use std::os::unix::io::OwnedFd;
use std::sync::Arc;

Expand Down Expand Up @@ -95,16 +94,14 @@ impl SelectionOffer {
// NOTE: the types are tied to the `SelectionDevice`, so every
// RTTI like checking is safe and reliable.

let type_id = device.inner_type_id();
let device_kind = device.device_kind();
let data = Arc::new(OfferReplyData {
type_id,
device_kind,
seat: device.seat(),
source: data,
});
let backend = dh.backend_handle();

let device_kind = DataDeviceKind::from_device_type_id(type_id).unwrap();

let interface = match device_kind {
DataDeviceKind::Core => WlDataOffer::interface(),
DataDeviceKind::Primary => PrimaryOffer::interface(),
Expand Down Expand Up @@ -134,7 +131,7 @@ impl SelectionOffer {
}

struct OfferReplyData<U: Clone + Send + Sync + 'static> {
type_id: TypeId,
device_kind: DataDeviceKind,
source: OfferReplySource<U>,
seat: WlSeat,
}
Expand All @@ -151,12 +148,11 @@ where
msg: Message<ObjectId, OwnedFd>,
) -> Option<Arc<dyn ObjectData<D>>> {
let dh = DisplayHandle::from(dh.clone());
let type_id = self.type_id;

// NOTE: we can't parse message more than once, since it expects the `OwnedFd` which
// we can't clone. To achieve that, we use RTTI passed along the selection data, to
// make the parsing work only once.
let (mime_type, fd, object_name) = match DataDeviceKind::from_device_type_id(type_id)? {
let (mime_type, fd, object_name) = match self.device_kind {
DataDeviceKind::Core => {
if let Ok((_resource, DataOfferRequest::Receive { mime_type, fd })) =
WlDataOffer::parse_request(&dh, msg)
Expand Down

0 comments on commit 869ed11

Please sign in to comment.