From 7cbe5720c68047abf0f8523b1516f43939172890 Mon Sep 17 00:00:00 2001 From: Julian Orth Date: Sat, 30 Mar 2024 20:51:25 +0100 Subject: [PATCH] ipc: make source/offer ids type safe --- src/compositor.rs | 1 + src/ifs/ipc.rs | 10 +++++++--- src/ifs/ipc/wl_data_device.rs | 13 +++++-------- src/ifs/ipc/wl_data_offer.rs | 6 +++--- src/ifs/ipc/zwp_primary_selection_device_v1.rs | 14 +++++--------- src/ifs/ipc/zwp_primary_selection_offer_v1.rs | 4 ++-- src/ifs/wl_seat.rs | 2 +- src/state.rs | 4 +++- 8 files changed, 27 insertions(+), 27 deletions(-) diff --git a/src/compositor.rs b/src/compositor.rs index 4f30dc91..798cfe81 100644 --- a/src/compositor.rs +++ b/src/compositor.rs @@ -200,6 +200,7 @@ fn start_compositor2( config_file_id: NumCell::new(1), tracker: Default::default(), data_offer_ids: Default::default(), + data_source_ids: Default::default(), drm_dev_ids: Default::default(), ring: ring.clone(), lock: ScreenlockState { diff --git a/src/ifs/ipc.rs b/src/ifs/ipc.rs index 36993c9c..45cabda7 100644 --- a/src/ifs/ipc.rs +++ b/src/ifs/ipc.rs @@ -26,6 +26,9 @@ pub mod zwp_primary_selection_device_v1; pub mod zwp_primary_selection_offer_v1; pub mod zwp_primary_selection_source_v1; +linear_ids!(DataSourceIds, DataSourceId, u64); +linear_ids!(DataOfferIds, DataOfferId, u64); + #[derive(Debug, Copy, Clone, Eq, PartialEq)] pub enum Role { Selection, @@ -57,14 +60,13 @@ pub trait IpcVtable: Sized { ) -> Result, ClientError>; fn send_selection(dd: &Self::Device, offer: Option<&Rc>); fn send_cancelled(source: &Rc, seat: &Rc); - fn get_offer_id(offer: &Self::Offer) -> u64; + fn get_offer_id(offer: &Self::Offer) -> DataOfferId; fn send_offer(dd: &Self::Device, offer: &Rc); fn send_mime_type(offer: &Rc, mime_type: &str); fn unset(seat: &Rc, role: Role); fn send_send(src: &Rc, mime_type: &str, fd: Rc); fn remove_from_seat(device: &Self::Device); fn get_offer_seat(offer: &Self::Offer) -> Rc; - fn source_eq(left: &Self::Source, right: &Self::Source) -> bool; } pub struct DeviceData { @@ -108,7 +110,8 @@ const SOURCE_STATE_DROPPED_OR_CANCELLED: u32 = SOURCE_STATE_DROPPED | SOURCE_STA pub struct SourceData { pub seat: CloneCell>>, - offers: SmallMap, 1>, + pub id: DataSourceId, + offers: SmallMap, 1>, offer_client: Cell, mime_types: RefCell>, pub client: Rc, @@ -143,6 +146,7 @@ impl SourceData { fn new(client: &Rc, is_xwm: bool) -> Self { Self { seat: Default::default(), + id: client.state.data_source_ids.next(), offers: Default::default(), offer_client: Cell::new(client.id), mime_types: Default::default(), diff --git a/src/ifs/ipc/wl_data_device.rs b/src/ifs/ipc/wl_data_device.rs index 820209b2..2fb6bc87 100644 --- a/src/ifs/ipc/wl_data_device.rs +++ b/src/ifs/ipc/wl_data_device.rs @@ -5,7 +5,8 @@ use { ifs::{ ipc::{ break_device_loops, destroy_data_device, wl_data_offer::WlDataOffer, - wl_data_source::WlDataSource, DeviceData, IpcVtable, OfferData, Role, SourceData, + wl_data_source::WlDataSource, DataOfferId, DeviceData, IpcVtable, OfferData, Role, + SourceData, }, wl_seat::{WlSeatError, WlSeatGlobal}, wl_surface::{SurfaceRole, WlSurfaceError}, @@ -232,7 +233,7 @@ impl IpcVtable for ClipboardIpc { ) -> Result, ClientError> { let rc = Rc::new(WlDataOffer { id: client.new_id()?, - u64_id: client.state.data_offer_ids.fetch_add(1), + offer_id: client.state.data_offer_ids.next(), client: client.clone(), device: device.clone(), data: offer_data, @@ -250,8 +251,8 @@ impl IpcVtable for ClipboardIpc { source.send_cancelled(seat); } - fn get_offer_id(offer: &Self::Offer) -> u64 { - offer.u64_id + fn get_offer_id(offer: &Self::Offer) -> DataOfferId { + offer.offer_id } fn send_offer(dd: &Self::Device, offer: &Rc) { @@ -280,10 +281,6 @@ impl IpcVtable for ClipboardIpc { fn get_offer_seat(offer: &Self::Offer) -> Rc { offer.device.seat.clone() } - - fn source_eq(left: &Self::Source, right: &Self::Source) -> bool { - left as *const _ == right as *const _ - } } object_base! { diff --git a/src/ifs/ipc/wl_data_offer.rs b/src/ifs/ipc/wl_data_offer.rs index 83d16834..4c13fc4b 100644 --- a/src/ifs/ipc/wl_data_offer.rs +++ b/src/ifs/ipc/wl_data_offer.rs @@ -5,8 +5,8 @@ use { break_offer_loops, destroy_data_offer, receive_data_offer, wl_data_device::{ClipboardIpc, WlDataDevice}, wl_data_device_manager::DND_ALL, - OfferData, Role, OFFER_STATE_ACCEPTED, OFFER_STATE_DROPPED, OFFER_STATE_FINISHED, - SOURCE_STATE_FINISHED, + DataOfferId, OfferData, Role, OFFER_STATE_ACCEPTED, OFFER_STATE_DROPPED, + OFFER_STATE_FINISHED, SOURCE_STATE_FINISHED, }, leaks::Tracker, object::Object, @@ -32,7 +32,7 @@ const INVALID_OFFER: u32 = 3; pub struct WlDataOffer { pub id: WlDataOfferId, - pub u64_id: u64, + pub offer_id: DataOfferId, pub client: Rc, pub device: Rc, pub data: OfferData, diff --git a/src/ifs/ipc/zwp_primary_selection_device_v1.rs b/src/ifs/ipc/zwp_primary_selection_device_v1.rs index 9c470925..0c589d30 100644 --- a/src/ifs/ipc/zwp_primary_selection_device_v1.rs +++ b/src/ifs/ipc/zwp_primary_selection_device_v1.rs @@ -5,8 +5,8 @@ use { ipc::{ break_device_loops, destroy_data_device, zwp_primary_selection_offer_v1::ZwpPrimarySelectionOfferV1, - zwp_primary_selection_source_v1::ZwpPrimarySelectionSourceV1, DeviceData, - IpcVtable, OfferData, Role, SourceData, + zwp_primary_selection_source_v1::ZwpPrimarySelectionSourceV1, DataOfferId, + DeviceData, IpcVtable, OfferData, Role, SourceData, }, wl_seat::{WlSeatError, WlSeatGlobal}, }, @@ -179,7 +179,7 @@ impl IpcVtable for PrimarySelectionIpc { }; let rc = Rc::new(ZwpPrimarySelectionOfferV1 { id, - u64_id: client.state.data_offer_ids.fetch_add(1), + offer_id: client.state.data_offer_ids.next(), seat: device.seat.clone(), client: client.clone(), data: offer_data, @@ -197,8 +197,8 @@ impl IpcVtable for PrimarySelectionIpc { source.send_cancelled(); } - fn get_offer_id(offer: &Self::Offer) -> u64 { - offer.u64_id + fn get_offer_id(offer: &Self::Offer) -> DataOfferId { + offer.offer_id } fn send_offer(dd: &Self::Device, offer: &Rc) { @@ -224,10 +224,6 @@ impl IpcVtable for PrimarySelectionIpc { fn get_offer_seat(offer: &Self::Offer) -> Rc { offer.seat.clone() } - - fn source_eq(left: &Self::Source, right: &Self::Source) -> bool { - left as *const _ == right as *const _ - } } object_base! { diff --git a/src/ifs/ipc/zwp_primary_selection_offer_v1.rs b/src/ifs/ipc/zwp_primary_selection_offer_v1.rs index c70d6d82..41d4de02 100644 --- a/src/ifs/ipc/zwp_primary_selection_offer_v1.rs +++ b/src/ifs/ipc/zwp_primary_selection_offer_v1.rs @@ -4,7 +4,7 @@ use { ifs::{ ipc::{ break_offer_loops, destroy_data_offer, receive_data_offer, - zwp_primary_selection_device_v1::PrimarySelectionIpc, OfferData, + zwp_primary_selection_device_v1::PrimarySelectionIpc, DataOfferId, OfferData, }, wl_seat::WlSeatGlobal, }, @@ -20,7 +20,7 @@ use { pub struct ZwpPrimarySelectionOfferV1 { pub id: ZwpPrimarySelectionOfferV1Id, - pub u64_id: u64, + pub offer_id: DataOfferId, pub seat: Rc, pub client: Rc, pub data: OfferData, diff --git a/src/ifs/wl_seat.rs b/src/ifs/wl_seat.rs index e9c225ca..75b17516 100644 --- a/src/ifs/wl_seat.rs +++ b/src/ifs/wl_seat.rs @@ -715,7 +715,7 @@ impl WlSeatGlobal { src: Option>, ) -> Result<(), WlSeatError> { if let (Some(new), Some(old)) = (&src, &field.get()) { - if T::source_eq(old, new) { + if T::get_source_data(new).id == T::get_source_data(old).id { return Ok(()); } } diff --git a/src/state.rs b/src/state.rs index a6047fe4..89601ebe 100644 --- a/src/state.rs +++ b/src/state.rs @@ -26,6 +26,7 @@ use { ifs::{ ext_foreign_toplevel_list_v1::ExtForeignToplevelListV1, ext_session_lock_v1::ExtSessionLockV1, + ipc::{DataOfferIds, DataSourceIds}, jay_render_ctx::JayRenderCtx, jay_seat_events::JaySeatEvents, jay_workspace_watcher::JayWorkspaceWatcher, @@ -149,7 +150,8 @@ pub struct State { pub config_dir: Option, pub config_file_id: NumCell, pub tracker: Tracker, - pub data_offer_ids: NumCell, + pub data_offer_ids: DataOfferIds, + pub data_source_ids: DataSourceIds, pub ring: Rc, pub lock: ScreenlockState, pub scales: RefCounted,