Skip to content

Commit

Permalink
Merge pull request #295 from mahkoh/jorth/various-improvements
Browse files Browse the repository at this point in the history
Various improvements
  • Loading branch information
mahkoh authored Oct 17, 2024
2 parents 437c6b0 + cf5d544 commit 2a3a924
Show file tree
Hide file tree
Showing 30 changed files with 137 additions and 129 deletions.
30 changes: 19 additions & 11 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ impl Clients {
is_xwayland,
effective_caps,
bounding_caps,
last_enter_serial: Cell::new(0),
last_enter_serial: Default::default(),
pid_info: get_pid_info(uid, pid),
serials: Default::default(),
symmetric_delete: Cell::new(false),
Expand Down Expand Up @@ -277,7 +277,7 @@ pub struct Client {
pub is_xwayland: bool,
pub effective_caps: ClientCaps,
pub bounding_caps: ClientCaps,
pub last_enter_serial: Cell<u32>,
pub last_enter_serial: Cell<Option<u64>>,
pub pid_info: PidInfo,
pub serials: RefCell<VecDeque<SerialRange>>,
pub symmetric_delete: Cell<bool>,
Expand All @@ -291,8 +291,8 @@ pub struct Client {
pub const NUM_CACHED_SERIAL_RANGES: usize = 64;

pub struct SerialRange {
pub lo: u32,
pub hi: u32,
pub lo: u64,
pub hi: u64,
}

impl Client {
Expand Down Expand Up @@ -320,20 +320,28 @@ impl Client {
}
}

pub fn valid_serial(&self, serial: u32) -> bool {
pub fn map_serial(&self, serial: u32) -> Option<u64> {
let serials = self.serials.borrow_mut();
let latest = serials.back()?;
let mut serial = ((latest.hi >> 32) << 32) | (serial as u64);
if serial > latest.hi {
serial = serial.checked_sub(1 << 32)?;
}
for range in serials.iter().rev() {
if serial.wrapping_sub(range.hi) as i32 > 0 {
return false;
if serial > range.hi {
return None;
}
if serial.wrapping_sub(range.lo) as i32 >= 0 {
return true;
if serial >= range.lo {
return Some(serial);
}
}
serials.len() == NUM_CACHED_SERIAL_RANGES
if serials.len() == NUM_CACHED_SERIAL_RANGES {
return Some(serial);
}
None
}

pub fn next_serial(&self) -> u32 {
pub fn next_serial(&self) -> u64 {
self.state.next_serial(Some(self))
}

Expand Down
4 changes: 2 additions & 2 deletions src/ifs/ipc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ pub trait DynDataOffer: 'static {
any::type_name_of_val(self)
)
}
fn send_enter(&self, surface: WlSurfaceId, x: Fixed, y: Fixed, serial: u32) {
fn send_enter(&self, surface: WlSurfaceId, x: Fixed, y: Fixed, serial: u64) {
let _ = surface;
let _ = x;
let _ = y;
Expand Down Expand Up @@ -155,7 +155,7 @@ pub trait IpcVtable: Sized {
fn set_seat_selection(
seat: &Rc<WlSeatGlobal>,
source: &Rc<Self::Source>,
serial: Option<u32>,
serial: Option<u64>,
) -> Result<(), WlSeatError>;
fn create_offer(
dd: &Rc<Self::Device>,
Expand Down
21 changes: 10 additions & 11 deletions src/ifs/ipc/wl_data_device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,11 @@ impl WlDataDevice {
x: Fixed,
y: Fixed,
offer: WlDataOfferId,
serial: u32,
serial: u64,
) {
self.client.event(Enter {
self_id: self.id,
serial,
serial: serial as _,
surface,
x,
y,
Expand All @@ -103,10 +103,10 @@ impl WlDataDeviceRequestHandler for WlDataDevice {
type Error = WlDataDeviceError;

fn start_drag(&self, req: StartDrag, _slf: &Rc<Self>) -> Result<(), Self::Error> {
if !self.client.valid_serial(req.serial) {
let Some(serial) = self.client.map_serial(req.serial) else {
log::warn!("Client tried to start_drag with an invalid serial");
return Ok(());
}
};
let origin = self.client.lookup(req.origin)?;
let source = if req.source.is_some() {
Some(self.client.lookup(req.source)?)
Expand All @@ -119,16 +119,16 @@ impl WlDataDeviceRequestHandler for WlDataDevice {
} else {
None
};
self.seat.start_drag(&origin, source, icon, req.serial)?;
self.seat.start_drag(&origin, source, icon, serial)?;
Ok(())
}

fn set_selection(&self, req: SetSelection, _slf: &Rc<Self>) -> Result<(), Self::Error> {
if !self.client.valid_serial(req.serial) {
let Some(serial) = self.client.map_serial(req.serial) else {
log::warn!("Client tried to set_selection with an invalid serial");
return Ok(());
}
if !self.seat.may_modify_selection(&self.client, req.serial) {
};
if !self.seat.may_modify_selection(&self.client, serial) {
log::warn!("Ignoring disallowed set_selection request");
return Ok(());
}
Expand All @@ -137,8 +137,7 @@ impl WlDataDeviceRequestHandler for WlDataDevice {
} else {
Some(self.client.lookup(req.source)?)
};
self.seat
.set_wl_data_source_selection(src, Some(req.serial))?;
self.seat.set_wl_data_source_selection(src, Some(serial))?;
Ok(())
}

Expand Down Expand Up @@ -177,7 +176,7 @@ impl IpcVtable for ClipboardIpc {
fn set_seat_selection(
seat: &Rc<WlSeatGlobal>,
source: &Rc<Self::Source>,
serial: Option<u32>,
serial: Option<u64>,
) -> Result<(), WlSeatError> {
seat.set_wl_data_source_selection(Some(source.clone()), serial)
}
Expand Down
2 changes: 1 addition & 1 deletion src/ifs/ipc/wl_data_offer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ impl DynDataOffer for WlDataOffer {
cancel_offer::<ClipboardIpc>(self);
}

fn send_enter(&self, surface: WlSurfaceId, x: Fixed, y: Fixed, serial: u32) {
fn send_enter(&self, surface: WlSurfaceId, x: Fixed, y: Fixed, serial: u64) {
self.device.send_enter(surface, x, y, self.id, serial);
}

Expand Down
2 changes: 1 addition & 1 deletion src/ifs/ipc/x_data_device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ impl<T: XIpc> IpcVtable for T {
fn set_seat_selection(
seat: &Rc<WlSeatGlobal>,
source: &Rc<Self::Source>,
_serial: Option<u32>,
_serial: Option<u64>,
) -> Result<(), WlSeatError> {
match source.location {
IpcLocation::Clipboard => seat.set_selection(Some(source.clone())),
Expand Down
2 changes: 1 addition & 1 deletion src/ifs/ipc/zwlr_data_control_device_v1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ impl<T: WlrIpc> IpcVtable for WlrIpcImpl<T> {
fn set_seat_selection(
seat: &Rc<WlSeatGlobal>,
source: &Rc<Self::Source>,
serial: Option<u32>,
serial: Option<u64>,
) -> Result<(), WlSeatError> {
debug_assert!(serial.is_none());
let _ = serial;
Expand Down
10 changes: 5 additions & 5 deletions src/ifs/ipc/zwp_primary_selection_device_v1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,13 @@ impl ZwpPrimarySelectionDeviceV1RequestHandler for ZwpPrimarySelectionDeviceV1 {
type Error = ZwpPrimarySelectionDeviceV1Error;

fn set_selection(&self, req: SetSelection, _slf: &Rc<Self>) -> Result<(), Self::Error> {
if !self.client.valid_serial(req.serial) {
let Some(serial) = self.client.map_serial(req.serial) else {
log::warn!("Client tried to set_selection with an invalid serial");
return Ok(());
}
};
if !self
.seat
.may_modify_primary_selection(&self.client, Some(req.serial))
.may_modify_primary_selection(&self.client, Some(serial))
{
log::warn!("Ignoring disallowed set_selection request");
return Ok(());
Expand All @@ -85,7 +85,7 @@ impl ZwpPrimarySelectionDeviceV1RequestHandler for ZwpPrimarySelectionDeviceV1 {
} else {
Some(self.client.lookup(req.source)?)
};
self.seat.set_zwp_primary_selection(src, Some(req.serial))?;
self.seat.set_zwp_primary_selection(src, Some(serial))?;
Ok(())
}

Expand Down Expand Up @@ -124,7 +124,7 @@ impl IpcVtable for PrimarySelectionIpc {
fn set_seat_selection(
seat: &Rc<WlSeatGlobal>,
source: &Rc<Self::Source>,
serial: Option<u32>,
serial: Option<u64>,
) -> Result<(), WlSeatError> {
seat.set_zwp_primary_selection(Some(source.clone()), serial)
}
Expand Down
20 changes: 9 additions & 11 deletions src/ifs/wl_seat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,9 @@ pub struct WlSeatGlobal {
tree_changed: Rc<AsyncEvent>,
tree_changed_needs_layout: Cell<bool>,
selection: CloneCell<Option<Rc<dyn DynDataSource>>>,
selection_serial: Cell<u32>,
selection_serial: Cell<u64>,
primary_selection: CloneCell<Option<Rc<dyn DynDataSource>>>,
primary_selection_serial: Cell<u32>,
primary_selection_serial: Cell<u64>,
pointer_owner: PointerOwnerHolder,
kb_owner: KbOwnerHolder,
gesture_owner: GestureOwnerHolder,
Expand Down Expand Up @@ -766,7 +766,7 @@ impl WlSeatGlobal {
origin: &Rc<WlSurface>,
source: Option<Rc<WlDataSource>>,
icon: Option<Rc<DndIcon>>,
serial: u32,
serial: u64,
) -> Result<(), WlSeatError> {
if let Some(icon) = &icon {
icon.surface().set_output(&self.pointer_cursor.output());
Expand Down Expand Up @@ -798,7 +798,7 @@ impl WlSeatGlobal {
pub fn set_wl_data_source_selection(
self: &Rc<Self>,
selection: Option<Rc<WlDataSource>>,
serial: Option<u32>,
serial: Option<u64>,
) -> Result<(), WlSeatError> {
if let Some(serial) = serial {
self.selection_serial.set(serial);
Expand All @@ -825,18 +825,16 @@ impl WlSeatGlobal {
self.selection.get()
}

pub fn may_modify_selection(&self, client: &Rc<Client>, serial: u32) -> bool {
let dist = serial.wrapping_sub(self.selection_serial.get()) as i32;
if dist < 0 {
pub fn may_modify_selection(&self, client: &Rc<Client>, serial: u64) -> bool {
if serial < self.selection_serial.get() {
return false;
}
self.keyboard_node.get().node_client_id() == Some(client.id)
}

pub fn may_modify_primary_selection(&self, client: &Rc<Client>, serial: Option<u32>) -> bool {
pub fn may_modify_primary_selection(&self, client: &Rc<Client>, serial: Option<u64>) -> bool {
if let Some(serial) = serial {
let dist = serial.wrapping_sub(self.primary_selection_serial.get()) as i32;
if dist < 0 {
if serial < self.primary_selection_serial.get() {
return false;
}
}
Expand All @@ -851,7 +849,7 @@ impl WlSeatGlobal {
pub fn set_zwp_primary_selection(
self: &Rc<Self>,
selection: Option<Rc<ZwpPrimarySelectionSourceV1>>,
serial: Option<u32>,
serial: Option<u64>,
) -> Result<(), WlSeatError> {
if let Some(serial) = serial {
self.primary_selection_serial.set(serial);
Expand Down
6 changes: 3 additions & 3 deletions src/ifs/wl_seat/event_handling.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1137,7 +1137,7 @@ impl WlSeatGlobal {
time_usec: u64,
button: u32,
state: KeyState,
serial: u32,
serial: u64,
) {
let (state, pressed) = match state {
KeyState::Released => (wl_pointer::RELEASED, false),
Expand Down Expand Up @@ -1250,7 +1250,7 @@ impl WlSeatGlobal {

pub fn enter_surface(&self, n: &WlSurface, x: Fixed, y: Fixed) {
let serial = n.client.next_serial();
n.client.last_enter_serial.set(serial);
n.client.last_enter_serial.set(Some(serial));
self.surface_pointer_event(Version::ALL, n, |p| p.send_enter(serial, n.id, x, y));
self.surface_pointer_frame(n);
for (_, constraint) in &n.constraints {
Expand Down Expand Up @@ -1428,7 +1428,7 @@ impl WlSeatGlobal {
dnd: &Dnd,
x: Fixed,
y: Fixed,
serial: u32,
serial: u64,
) {
if let Some(src) = &dnd.src {
if !surface.client.is_xwayland {
Expand Down
12 changes: 6 additions & 6 deletions src/ifs/wl_seat/pointer_owner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ impl PointerOwnerHolder {
origin: &Rc<WlSurface>,
source: Option<Rc<WlDataSource>>,
icon: Option<Rc<DndIcon>>,
serial: u32,
serial: u64,
) -> Result<(), WlSeatError> {
self.owner
.get()
Expand Down Expand Up @@ -230,7 +230,7 @@ trait PointerOwner {
origin: &Rc<WlSurface>,
source: Option<Rc<WlDataSource>>,
icon: Option<Rc<DndIcon>>,
serial: u32,
serial: u64,
) -> Result<(), WlSeatError> {
let _ = origin;
let _ = icon;
Expand Down Expand Up @@ -284,7 +284,7 @@ struct SimpleGrabPointerOwner<T> {
usecase: T,
buttons: SmallMap<u32, (), 1>,
node: Rc<dyn Node>,
serial: u32,
serial: u64,
}

struct DndPointerOwner {
Expand Down Expand Up @@ -473,7 +473,7 @@ impl<T: SimplePointerOwnerUsecase> PointerOwner for SimpleGrabPointerOwner<T> {
origin: &Rc<WlSurface>,
src: Option<Rc<WlDataSource>>,
icon: Option<Rc<DndIcon>>,
serial: u32,
serial: u64,
) -> Result<(), WlSeatError> {
self.usecase
.start_drag(self, seat, origin, src, icon, serial)
Expand Down Expand Up @@ -627,7 +627,7 @@ trait SimplePointerOwnerUsecase: Sized + Clone + 'static {
origin: &Rc<WlSurface>,
src: Option<Rc<WlDataSource>>,
icon: Option<Rc<DndIcon>>,
serial: u32,
serial: u64,
) -> Result<(), WlSeatError> {
let _ = grab;
let _ = origin;
Expand Down Expand Up @@ -720,7 +720,7 @@ impl SimplePointerOwnerUsecase for DefaultPointerUsecase {
origin: &Rc<WlSurface>,
src: Option<Rc<WlDataSource>>,
icon: Option<Rc<DndIcon>>,
serial: u32,
serial: u64,
) -> Result<(), WlSeatError> {
let button = match grab.buttons.iter().next() {
Some((b, _)) => b,
Expand Down
4 changes: 2 additions & 2 deletions src/ifs/wl_seat/tablet/zwp_tablet_pad_group_v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,11 @@ impl ZwpTabletPadGroupV2 {
self.client.event(Done { self_id: self.id });
}

pub fn send_mode_switch(&self, time: u32, serial: u32, mode: u32) {
pub fn send_mode_switch(&self, time: u32, serial: u64, mode: u32) {
self.client.event(ModeSwitch {
self_id: self.id,
time,
serial,
serial: serial as _,
mode,
});
}
Expand Down
8 changes: 4 additions & 4 deletions src/ifs/wl_seat/tablet/zwp_tablet_pad_v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,21 +68,21 @@ impl ZwpTabletPadV2 {
});
}

pub fn send_enter(&self, serial: u32, tablet: &ZwpTabletV2, surface: &WlSurface) {
pub fn send_enter(&self, serial: u64, tablet: &ZwpTabletV2, surface: &WlSurface) {
self.entered.set(true);
self.client.event(Enter {
self_id: self.id,
serial,
serial: serial as _,
tablet: tablet.id,
surface: surface.id,
});
}

pub fn send_leave(&self, serial: u32, surface: &WlSurface) {
pub fn send_leave(&self, serial: u64, surface: &WlSurface) {
self.entered.set(false);
self.client.event(Leave {
self_id: self.id,
serial,
serial: serial as _,
surface: surface.id,
});
}
Expand Down
Loading

0 comments on commit 2a3a924

Please sign in to comment.