1
0
Fork 0
forked from wry/wry

data-control: remove wlr-specific code

This commit is contained in:
Julian Orth 2024-10-08 13:11:42 +02:00
parent 40f7bc2542
commit dfc0a11935
12 changed files with 120 additions and 99 deletions

View file

@ -0,0 +1,296 @@
use {
crate::{
client::{Client, ClientError},
ifs::{
ipc::{
break_device_loops,
data_control::{
zwlr_data_control_device_v1::private::{
WlrClipboardIpcCore, WlrIpcImpl, WlrPrimarySelectionIpcCore,
},
zwlr_data_control_offer_v1::ZwlrDataControlOfferV1,
zwlr_data_control_source_v1::ZwlrDataControlSourceV1,
DataControlDeviceId, DynDataControlDevice,
},
destroy_data_device, offer_source_to_data_control_device, DeviceData,
DynDataSource, IpcLocation, IpcVtable, OfferData, Role,
},
wl_seat::{WlSeatError, WlSeatGlobal},
},
leaks::Tracker,
object::{Object, Version},
wire::{
zwlr_data_control_device_v1::*, ZwlrDataControlDeviceV1Id, ZwlrDataControlOfferV1Id,
ZwlrDataControlSourceV1Id,
},
},
std::rc::Rc,
thiserror::Error,
};
pub const PRIMARY_SELECTION_SINCE: Version = Version(2);
pub struct ZwlrDataControlDeviceV1 {
pub id: ZwlrDataControlDeviceV1Id,
pub data_control_device_id: DataControlDeviceId,
pub client: Rc<Client>,
pub version: Version,
pub seat: Rc<WlSeatGlobal>,
pub clipboard_data: DeviceData<ZwlrDataControlOfferV1>,
pub primary_selection_data: DeviceData<ZwlrDataControlOfferV1>,
pub tracker: Tracker<Self>,
}
impl ZwlrDataControlDeviceV1 {
pub fn new(
id: ZwlrDataControlDeviceV1Id,
client: &Rc<Client>,
version: Version,
seat: &Rc<WlSeatGlobal>,
) -> Self {
Self {
id,
data_control_device_id: client.state.data_control_device_ids.next(),
client: client.clone(),
version,
seat: seat.clone(),
clipboard_data: Default::default(),
primary_selection_data: Default::default(),
tracker: Default::default(),
}
}
pub fn send_data_offer(&self, offer: &Rc<ZwlrDataControlOfferV1>) {
self.client.event(DataOffer {
self_id: self.id,
id: offer.id,
})
}
pub fn send_selection(&self, offer: Option<&Rc<ZwlrDataControlOfferV1>>) {
let id = offer
.map(|o| o.id)
.unwrap_or(ZwlrDataControlOfferV1Id::NONE);
self.client.event(Selection {
self_id: self.id,
id,
})
}
pub fn send_primary_selection(&self, offer: Option<&Rc<ZwlrDataControlOfferV1>>) {
let id = offer
.map(|o| o.id)
.unwrap_or(ZwlrDataControlOfferV1Id::NONE);
self.client.event(PrimarySelection {
self_id: self.id,
id,
})
}
fn use_source(
&self,
source: ZwlrDataControlSourceV1Id,
location: IpcLocation,
) -> Result<Option<Rc<ZwlrDataControlSourceV1>>, ZwlrDataControlDeviceV1Error> {
if source.is_none() {
Ok(None)
} else {
let src = self.client.lookup(source)?;
if src.used.replace(true) {
return Err(ZwlrDataControlDeviceV1Error::AlreadyUsed);
}
src.location.set(location);
Ok(Some(src))
}
}
}
impl ZwlrDataControlDeviceV1RequestHandler for ZwlrDataControlDeviceV1 {
type Error = ZwlrDataControlDeviceV1Error;
fn set_selection(&self, req: SetSelection, _slf: &Rc<Self>) -> Result<(), Self::Error> {
let src = self.use_source(req.source, IpcLocation::Clipboard)?;
self.seat.set_selection(src)?;
Ok(())
}
fn destroy(&self, _req: Destroy, _slf: &Rc<Self>) -> Result<(), Self::Error> {
destroy_data_device::<WlrClipboardIpc>(self);
destroy_data_device::<WlrPrimarySelectionIpc>(self);
self.seat.remove_data_control_device(self);
self.client.remove_obj(self)?;
Ok(())
}
fn set_primary_selection(
&self,
req: SetPrimarySelection,
_slf: &Rc<Self>,
) -> Result<(), Self::Error> {
let src = self.use_source(req.source, IpcLocation::PrimarySelection)?;
self.seat.set_primary_selection(src)?;
Ok(())
}
}
mod private {
use std::marker::PhantomData;
pub struct WlrClipboardIpcCore;
pub struct WlrPrimarySelectionIpcCore;
pub struct WlrIpcImpl<T>(PhantomData<T>);
}
pub type WlrClipboardIpc = WlrIpcImpl<WlrClipboardIpcCore>;
pub type WlrPrimarySelectionIpc = WlrIpcImpl<WlrPrimarySelectionIpcCore>;
trait WlrIpc {
const LOCATION: IpcLocation;
fn wlr_get_device_data(dd: &ZwlrDataControlDeviceV1) -> &DeviceData<ZwlrDataControlOfferV1>;
fn wlr_send_selection(dd: &ZwlrDataControlDeviceV1, offer: Option<&Rc<ZwlrDataControlOfferV1>>);
fn wlr_unset(seat: &Rc<WlSeatGlobal>);
}
impl WlrIpc for WlrClipboardIpcCore {
const LOCATION: IpcLocation = IpcLocation::Clipboard;
fn wlr_get_device_data(dd: &ZwlrDataControlDeviceV1) -> &DeviceData<ZwlrDataControlOfferV1> {
&dd.clipboard_data
}
fn wlr_send_selection(
dd: &ZwlrDataControlDeviceV1,
offer: Option<&Rc<ZwlrDataControlOfferV1>>,
) {
dd.send_selection(offer)
}
fn wlr_unset(seat: &Rc<WlSeatGlobal>) {
seat.unset_selection()
}
}
impl WlrIpc for WlrPrimarySelectionIpcCore {
const LOCATION: IpcLocation = IpcLocation::PrimarySelection;
fn wlr_get_device_data(dd: &ZwlrDataControlDeviceV1) -> &DeviceData<ZwlrDataControlOfferV1> {
&dd.primary_selection_data
}
fn wlr_send_selection(
dd: &ZwlrDataControlDeviceV1,
offer: Option<&Rc<ZwlrDataControlOfferV1>>,
) {
dd.send_primary_selection(offer)
}
fn wlr_unset(seat: &Rc<WlSeatGlobal>) {
seat.unset_primary_selection()
}
}
impl<T: WlrIpc> IpcVtable for WlrIpcImpl<T> {
type Device = ZwlrDataControlDeviceV1;
type Source = ZwlrDataControlSourceV1;
type Offer = ZwlrDataControlOfferV1;
fn get_device_data(dd: &Self::Device) -> &DeviceData<Self::Offer> {
T::wlr_get_device_data(dd)
}
fn get_device_seat(dd: &Self::Device) -> Rc<WlSeatGlobal> {
dd.seat.clone()
}
fn create_offer(
device: &Rc<ZwlrDataControlDeviceV1>,
offer_data: OfferData<Self::Device>,
) -> Result<Rc<Self::Offer>, ClientError> {
let rc = Rc::new(ZwlrDataControlOfferV1 {
id: device.client.new_id()?,
offer_id: device.client.state.data_offer_ids.next(),
client: device.client.clone(),
device: device.clone(),
data: offer_data,
location: T::LOCATION,
tracker: Default::default(),
});
track!(device.client, rc);
device.client.add_server_obj(&rc);
Ok(rc)
}
fn send_selection(dd: &Self::Device, offer: Option<&Rc<Self::Offer>>) {
T::wlr_send_selection(dd, offer)
}
fn send_offer(dd: &Self::Device, offer: &Rc<Self::Offer>) {
dd.send_data_offer(offer);
}
fn unset(seat: &Rc<WlSeatGlobal>, _role: Role) {
T::wlr_unset(seat)
}
fn device_client(dd: &Rc<Self::Device>) -> &Rc<Client> {
&dd.client
}
}
impl DynDataControlDevice for ZwlrDataControlDeviceV1 {
fn id(&self) -> DataControlDeviceId {
self.data_control_device_id
}
fn handle_new_source(
self: Rc<Self>,
location: IpcLocation,
source: Option<Rc<dyn DynDataSource>>,
) {
match location {
IpcLocation::Clipboard => match source {
Some(src) => offer_source_to_data_control_device::<WlrClipboardIpc>(src, &self),
_ => self.send_selection(None),
},
IpcLocation::PrimarySelection => {
if self.version >= PRIMARY_SELECTION_SINCE {
match source {
Some(src) => offer_source_to_data_control_device::<WlrPrimarySelectionIpc>(
src, &self,
),
_ => self.send_primary_selection(None),
}
}
}
}
}
}
object_base! {
self = ZwlrDataControlDeviceV1;
version = self.version;
}
impl Object for ZwlrDataControlDeviceV1 {
fn break_loops(&self) {
break_device_loops::<WlrClipboardIpc>(self);
break_device_loops::<WlrPrimarySelectionIpc>(self);
self.seat.remove_data_control_device(self);
}
}
simple_add_obj!(ZwlrDataControlDeviceV1);
#[derive(Debug, Error)]
pub enum ZwlrDataControlDeviceV1Error {
#[error(transparent)]
ClientError(Box<ClientError>),
#[error(transparent)]
WlSeatError(Box<WlSeatError>),
#[error("The source has already been used")]
AlreadyUsed,
}
efrom!(ZwlrDataControlDeviceV1Error, ClientError);
efrom!(ZwlrDataControlDeviceV1Error, WlSeatError);

View file

@ -0,0 +1,134 @@
use {
crate::{
client::{Client, ClientCaps, ClientError, CAP_DATA_CONTROL_MANAGER},
globals::{Global, GlobalName},
ifs::ipc::{
data_control::{
zwlr_data_control_device_v1::ZwlrDataControlDeviceV1,
zwlr_data_control_source_v1::ZwlrDataControlSourceV1, DynDataControlDevice,
},
IpcLocation,
},
leaks::Tracker,
object::{Object, Version},
wire::{zwlr_data_control_manager_v1::*, ZwlrDataControlManagerV1Id},
},
std::rc::Rc,
thiserror::Error,
};
pub struct ZwlrDataControlManagerV1Global {
name: GlobalName,
}
pub struct ZwlrDataControlManagerV1 {
pub id: ZwlrDataControlManagerV1Id,
pub client: Rc<Client>,
pub version: Version,
tracker: Tracker<Self>,
}
impl ZwlrDataControlManagerV1Global {
pub fn new(name: GlobalName) -> Self {
Self { name }
}
fn bind_(
self: Rc<Self>,
id: ZwlrDataControlManagerV1Id,
client: &Rc<Client>,
version: Version,
) -> Result<(), ZwlrDataControlManagerV1Error> {
let obj = Rc::new(ZwlrDataControlManagerV1 {
id,
client: client.clone(),
version,
tracker: Default::default(),
});
track!(client, obj);
client.add_client_obj(&obj)?;
Ok(())
}
}
impl ZwlrDataControlManagerV1RequestHandler for ZwlrDataControlManagerV1 {
type Error = ZwlrDataControlManagerV1Error;
fn create_data_source(
&self,
req: CreateDataSource,
_slf: &Rc<Self>,
) -> Result<(), Self::Error> {
let res = Rc::new(ZwlrDataControlSourceV1::new(
req.id,
&self.client,
self.version,
));
track!(self.client, res);
self.client.add_client_obj(&res)?;
Ok(())
}
fn get_data_device(&self, req: GetDataDevice, _slf: &Rc<Self>) -> Result<(), Self::Error> {
let seat = self.client.lookup(req.seat)?;
let dev = Rc::new(ZwlrDataControlDeviceV1::new(
req.id,
&self.client,
self.version,
&seat.global,
));
track!(self.client, dev);
seat.global.add_data_control_device(dev.clone());
self.client.add_client_obj(&dev)?;
dev.clone()
.handle_new_source(IpcLocation::Clipboard, seat.global.get_selection());
dev.clone().handle_new_source(
IpcLocation::PrimarySelection,
seat.global.get_primary_selection(),
);
Ok(())
}
fn destroy(&self, _req: Destroy, _slf: &Rc<Self>) -> Result<(), Self::Error> {
self.client.remove_obj(self)?;
Ok(())
}
}
global_base!(
ZwlrDataControlManagerV1Global,
ZwlrDataControlManagerV1,
ZwlrDataControlManagerV1Error
);
impl Global for ZwlrDataControlManagerV1Global {
fn singleton(&self) -> bool {
true
}
fn version(&self) -> u32 {
2
}
fn required_caps(&self) -> ClientCaps {
CAP_DATA_CONTROL_MANAGER
}
}
simple_add_global!(ZwlrDataControlManagerV1Global);
object_base! {
self = ZwlrDataControlManagerV1;
version = self.version;
}
impl Object for ZwlrDataControlManagerV1 {}
simple_add_obj!(ZwlrDataControlManagerV1);
#[derive(Debug, Error)]
pub enum ZwlrDataControlManagerV1Error {
#[error(transparent)]
ClientError(Box<ClientError>),
}
efrom!(ZwlrDataControlManagerV1Error, ClientError);

View file

@ -0,0 +1,125 @@
use {
crate::{
client::{Client, ClientError, ClientId},
ifs::{
ipc::{
break_offer_loops, cancel_offer,
data_control::zwlr_data_control_device_v1::{
WlrClipboardIpc, WlrPrimarySelectionIpc, ZwlrDataControlDeviceV1,
},
destroy_data_offer, receive_data_offer, DataOffer, DataOfferId, DynDataOffer,
IpcLocation, OfferData,
},
wl_seat::WlSeatGlobal,
},
leaks::Tracker,
object::Object,
wire::{zwlr_data_control_offer_v1::*, ZwlrDataControlOfferV1Id},
},
std::rc::Rc,
thiserror::Error,
};
pub struct ZwlrDataControlOfferV1 {
pub id: ZwlrDataControlOfferV1Id,
pub offer_id: DataOfferId,
pub client: Rc<Client>,
pub device: Rc<ZwlrDataControlDeviceV1>,
pub data: OfferData<ZwlrDataControlDeviceV1>,
pub location: IpcLocation,
pub tracker: Tracker<Self>,
}
impl DataOffer for ZwlrDataControlOfferV1 {
type Device = ZwlrDataControlDeviceV1;
fn offer_data(&self) -> &OfferData<ZwlrDataControlDeviceV1> {
&self.data
}
}
impl DynDataOffer for ZwlrDataControlOfferV1 {
fn offer_id(&self) -> DataOfferId {
self.offer_id
}
fn client_id(&self) -> ClientId {
self.client.id
}
fn send_offer(&self, mime_type: &str) {
ZwlrDataControlOfferV1::send_offer(self, mime_type)
}
fn cancel(&self) {
match self.location {
IpcLocation::Clipboard => cancel_offer::<WlrClipboardIpc>(self),
IpcLocation::PrimarySelection => cancel_offer::<WlrPrimarySelectionIpc>(self),
}
}
fn get_seat(&self) -> Rc<WlSeatGlobal> {
self.device.seat.clone()
}
fn is_privileged(&self) -> bool {
true
}
}
impl ZwlrDataControlOfferV1 {
pub fn send_offer(&self, mime_type: &str) {
self.client.event(Offer {
self_id: self.id,
mime_type,
})
}
}
impl ZwlrDataControlOfferV1RequestHandler for ZwlrDataControlOfferV1 {
type Error = ZwlrDataControlOfferV1Error;
fn receive(&self, req: Receive, _slf: &Rc<Self>) -> Result<(), Self::Error> {
match self.location {
IpcLocation::Clipboard => {
receive_data_offer::<WlrClipboardIpc>(self, req.mime_type, req.fd)
}
IpcLocation::PrimarySelection => {
receive_data_offer::<WlrPrimarySelectionIpc>(self, req.mime_type, req.fd)
}
}
Ok(())
}
fn destroy(&self, _req: Destroy, _slf: &Rc<Self>) -> Result<(), Self::Error> {
match self.location {
IpcLocation::Clipboard => destroy_data_offer::<WlrClipboardIpc>(self),
IpcLocation::PrimarySelection => destroy_data_offer::<WlrPrimarySelectionIpc>(self),
}
self.client.remove_obj(self)?;
Ok(())
}
}
object_base! {
self = ZwlrDataControlOfferV1;
version = self.device.version;
}
impl Object for ZwlrDataControlOfferV1 {
fn break_loops(&self) {
match self.location {
IpcLocation::Clipboard => break_offer_loops::<WlrClipboardIpc>(self),
IpcLocation::PrimarySelection => break_offer_loops::<WlrPrimarySelectionIpc>(self),
}
}
}
simple_add_obj!(ZwlrDataControlOfferV1);
#[derive(Debug, Error)]
pub enum ZwlrDataControlOfferV1Error {
#[error(transparent)]
ClientError(Box<ClientError>),
}
efrom!(ZwlrDataControlOfferV1Error, ClientError);

View file

@ -0,0 +1,138 @@
use {
crate::{
client::{Client, ClientError},
ifs::{
ipc::{
add_data_source_mime_type, break_source_loops, cancel_offers,
data_control::zwlr_data_control_device_v1::{
WlrClipboardIpc, WlrPrimarySelectionIpc,
},
destroy_data_source, detach_seat, offer_source_to_x,
x_data_device::{XClipboardIpc, XIpcDevice, XPrimarySelectionIpc},
DataSource, DynDataSource, IpcLocation, SourceData,
},
wl_seat::WlSeatGlobal,
},
leaks::Tracker,
object::{Object, Version},
wire::{zwlr_data_control_source_v1::*, ZwlrDataControlSourceV1Id},
},
std::{cell::Cell, rc::Rc},
thiserror::Error,
uapi::OwnedFd,
};
pub struct ZwlrDataControlSourceV1 {
pub id: ZwlrDataControlSourceV1Id,
pub data: SourceData,
pub version: Version,
pub location: Cell<IpcLocation>,
pub used: Cell<bool>,
pub tracker: Tracker<Self>,
}
impl DataSource for ZwlrDataControlSourceV1 {
fn send_cancelled(&self, _seat: &Rc<WlSeatGlobal>) {
ZwlrDataControlSourceV1::send_cancelled(self);
}
}
impl DynDataSource for ZwlrDataControlSourceV1 {
fn source_data(&self) -> &SourceData {
&self.data
}
fn send_send(&self, mime_type: &str, fd: Rc<OwnedFd>) {
ZwlrDataControlSourceV1::send_send(&self, mime_type, fd);
}
fn offer_to_x(self: Rc<Self>, dd: &Rc<XIpcDevice>) {
match self.location.get() {
IpcLocation::Clipboard => offer_source_to_x::<XClipboardIpc>(self, dd),
IpcLocation::PrimarySelection => offer_source_to_x::<XPrimarySelectionIpc>(self, dd),
}
}
fn detach_seat(&self, seat: &Rc<WlSeatGlobal>) {
detach_seat(self, seat)
}
fn cancel_unprivileged_offers(&self) {
cancel_offers(self, false)
}
}
impl ZwlrDataControlSourceV1 {
pub fn new(id: ZwlrDataControlSourceV1Id, client: &Rc<Client>, version: Version) -> Self {
Self {
id,
tracker: Default::default(),
data: SourceData::new(client),
version,
location: Cell::new(IpcLocation::Clipboard),
used: Cell::new(false),
}
}
pub fn send_send(&self, mime_type: &str, fd: Rc<OwnedFd>) {
self.data.client.event(Send {
self_id: self.id,
mime_type,
fd,
})
}
pub fn send_cancelled(&self) {
self.data.client.event(Cancelled { self_id: self.id })
}
}
impl ZwlrDataControlSourceV1RequestHandler for ZwlrDataControlSourceV1 {
type Error = ZwlrDataControlSourceV1Error;
fn offer(&self, req: Offer, _slf: &Rc<Self>) -> Result<(), Self::Error> {
if self.used.get() {
return Err(ZwlrDataControlSourceV1Error::AlreadyUsed);
}
add_data_source_mime_type::<WlrClipboardIpc>(self, req.mime_type);
Ok(())
}
fn destroy(&self, _req: Destroy, _slf: &Rc<Self>) -> Result<(), Self::Error> {
match self.location.get() {
IpcLocation::Clipboard => destroy_data_source::<WlrClipboardIpc>(self),
IpcLocation::PrimarySelection => destroy_data_source::<WlrPrimarySelectionIpc>(self),
}
self.data.client.remove_obj(self)?;
Ok(())
}
}
object_base! {
self = ZwlrDataControlSourceV1;
version = self.version;
}
impl Object for ZwlrDataControlSourceV1 {
fn break_loops(&self) {
match self.location.get() {
IpcLocation::Clipboard => break_source_loops::<WlrClipboardIpc>(self),
IpcLocation::PrimarySelection => break_source_loops::<WlrPrimarySelectionIpc>(self),
}
}
}
dedicated_add_obj!(
ZwlrDataControlSourceV1,
ZwlrDataControlSourceV1Id,
zwlr_data_sources
);
#[derive(Debug, Error)]
pub enum ZwlrDataControlSourceV1Error {
#[error(transparent)]
ClientError(Box<ClientError>),
#[error("The source has already been used")]
AlreadyUsed,
}
efrom!(ZwlrDataControlSourceV1Error, ClientError);