Skip to content

Commit fa4bffd

Browse files
ids1024Drakulix
authored andcommitted
Add cursor_image_status/set_cursor_image_status to SeatExt
The `cursor_image_status()` function saves some duplication in various places. The `set_cursor_image_status()` saves a bit less, but is also handy.
1 parent ddc23fc commit fa4bffd

File tree

8 files changed

+52
-105
lines changed

8 files changed

+52
-105
lines changed

src/backend/render/cursor.rs

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@ use smithay::{
2020
reexports::wayland_server::protocol::wl_surface,
2121
render_elements,
2222
utils::{
23-
Buffer as BufferCoords, IsAlive, Logical, Monotonic, Physical, Point, Scale, Size, Time,
24-
Transform,
23+
Buffer as BufferCoords, Logical, Monotonic, Physical, Point, Scale, Size, Time, Transform,
2524
},
2625
wayland::compositor::{get_role, with_states},
2726
};
@@ -266,20 +265,7 @@ where
266265
R::TextureId: Send + Clone + 'static,
267266
{
268267
// draw the cursor as relevant
269-
// reset the cursor if the surface is no longer alive
270-
let cursor_status = seat
271-
.user_data()
272-
.get::<Mutex<CursorImageStatus>>()
273-
.map(|lock| {
274-
let mut cursor_status = lock.lock().unwrap();
275-
if let CursorImageStatus::Surface(ref surface) = *cursor_status {
276-
if !surface.alive() {
277-
*cursor_status = CursorImageStatus::default_named();
278-
}
279-
}
280-
cursor_status.clone()
281-
})
282-
.unwrap_or(CursorImageStatus::default_named());
268+
let cursor_status = seat.cursor_image_status();
283269

284270
let seat_userdata = seat.user_data();
285271
let mut state_ref = seat_userdata.get::<CursorState>().unwrap().lock().unwrap();

src/shell/element/stack.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1333,8 +1333,7 @@ impl PointerTarget<State> for CosmicStack {
13331333
.lock()
13341334
.unwrap();
13351335
cursor_state.set_shape(next.cursor_shape());
1336-
let cursor_status = seat.user_data().get::<Mutex<CursorImageStatus>>().unwrap();
1337-
*cursor_status.lock().unwrap() = CursorImageStatus::default_named();
1336+
seat.set_cursor_image_status(CursorImageStatus::default_named());
13381337
});
13391338

13401339
event.location -= self.0.with_program(|p| {
@@ -1363,8 +1362,7 @@ impl PointerTarget<State> for CosmicStack {
13631362
.lock()
13641363
.unwrap();
13651364
cursor_state.set_shape(next.cursor_shape());
1366-
let cursor_status = seat.user_data().get::<Mutex<CursorImageStatus>>().unwrap();
1367-
*cursor_status.lock().unwrap() = CursorImageStatus::default_named();
1365+
seat.set_cursor_image_status(CursorImageStatus::default_named());
13681366
});
13691367

13701368
let active_window_geo = self.0.with_program(|p| {

src/shell/element/window.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -711,8 +711,7 @@ impl PointerTarget<State> for CosmicWindow {
711711

712712
let cursor_state = seat.user_data().get::<CursorState>().unwrap();
713713
cursor_state.lock().unwrap().set_shape(next.cursor_shape());
714-
let cursor_status = seat.user_data().get::<Mutex<CursorImageStatus>>().unwrap();
715-
*cursor_status.lock().unwrap() = CursorImageStatus::default_named();
714+
seat.set_cursor_image_status(CursorImageStatus::default_named());
716715
}
717716
});
718717

@@ -736,8 +735,7 @@ impl PointerTarget<State> for CosmicWindow {
736735

737736
let cursor_state = seat.user_data().get::<CursorState>().unwrap();
738737
cursor_state.lock().unwrap().set_shape(next.cursor_shape());
739-
let cursor_status = seat.user_data().get::<Mutex<CursorImageStatus>>().unwrap();
740-
*cursor_status.lock().unwrap() = CursorImageStatus::default_named();
738+
seat.set_cursor_image_status(CursorImageStatus::default_named());
741739
}
742740
});
743741

src/shell/mod.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1427,12 +1427,7 @@ impl Common {
14271427
}
14281428

14291429
let is_cursor_image = shell.seats.iter().any(|seat| {
1430-
seat.user_data()
1431-
.get::<Mutex<CursorImageStatus>>()
1432-
.map(|guard| {
1433-
matches!(*guard.lock().unwrap(), CursorImageStatus::Surface(ref cursor_surface) if cursor_surface == surface)
1434-
})
1435-
.unwrap_or(false)
1430+
matches!(seat.cursor_image_status(), CursorImageStatus::Surface(ref cursor_surface) if cursor_surface == surface)
14361431
});
14371432

14381433
if is_cursor_image {

src/shell/seats.rs

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,8 @@ pub trait SeatExt {
252252
loc: impl Into<Point<f64, Buffer>>,
253253
time: Time<Monotonic>,
254254
) -> Option<(Rectangle<i32, Buffer>, Point<i32, Buffer>)>;
255+
fn cursor_image_status(&self) -> CursorImageStatus;
256+
fn set_cursor_image_status(&self, status: CursorImageStatus);
255257
}
256258

257259
impl SeatExt for Seat<State> {
@@ -337,21 +339,7 @@ impl SeatExt for Seat<State> {
337339
) -> Option<(Rectangle<i32, Buffer>, Point<i32, Buffer>)> {
338340
let location = loc.into().to_i32_round();
339341

340-
let cursor_status = self
341-
.user_data()
342-
.get::<Mutex<CursorImageStatus>>()
343-
.map(|lock| {
344-
let mut cursor_status = lock.lock().unwrap();
345-
if let CursorImageStatus::Surface(ref surface) = *cursor_status {
346-
if !surface.alive() {
347-
*cursor_status = CursorImageStatus::default_named();
348-
}
349-
}
350-
cursor_status.clone()
351-
})
352-
.unwrap_or(CursorImageStatus::default_named());
353-
354-
match cursor_status {
342+
match self.cursor_image_status() {
355343
CursorImageStatus::Surface(surface) => {
356344
let hotspot = with_states(&surface, |states| {
357345
states
@@ -387,4 +375,25 @@ impl SeatExt for Seat<State> {
387375
CursorImageStatus::Hidden => None,
388376
}
389377
}
378+
379+
fn cursor_image_status(&self) -> CursorImageStatus {
380+
self.user_data()
381+
.get::<Mutex<CursorImageStatus>>()
382+
// Reset the cursor if the surface is no longer alive
383+
.map(|lock| {
384+
let mut cursor_status = lock.lock().unwrap();
385+
if let CursorImageStatus::Surface(ref surface) = *cursor_status {
386+
if !surface.alive() {
387+
*cursor_status = CursorImageStatus::default_named();
388+
}
389+
}
390+
cursor_status.clone()
391+
})
392+
.unwrap_or(CursorImageStatus::default_named())
393+
}
394+
395+
fn set_cursor_image_status(&self, status: CursorImageStatus) {
396+
let cursor_status = self.user_data().get::<Mutex<CursorImageStatus>>().unwrap();
397+
*cursor_status.lock().unwrap() = status;
398+
}
390399
}

src/state.rs

Lines changed: 4 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ use smithay::{
7070
Client, DisplayHandle, Resource,
7171
},
7272
},
73-
utils::{Clock, IsAlive, Monotonic, Point},
73+
utils::{Clock, Monotonic, Point},
7474
wayland::{
7575
alpha_modifier::AlphaModifierState,
7676
compositor::{CompositorClientState, CompositorState, SurfaceData},
@@ -118,7 +118,7 @@ use std::{
118118
collections::HashSet,
119119
ffi::OsString,
120120
process::Child,
121-
sync::{atomic::AtomicBool, Arc, Mutex, Once},
121+
sync::{atomic::AtomicBool, Arc, Once},
122122
time::{Duration, Instant},
123123
};
124124

@@ -729,19 +729,7 @@ impl Common {
729729
.iter()
730730
.filter(|seat| &seat.active_output() == output)
731731
{
732-
let cursor_status = seat
733-
.user_data()
734-
.get::<Mutex<CursorImageStatus>>()
735-
.map(|lock| {
736-
let mut cursor_status = lock.lock().unwrap();
737-
if let CursorImageStatus::Surface(ref surface) = *cursor_status {
738-
if !surface.alive() {
739-
*cursor_status = CursorImageStatus::default_named();
740-
}
741-
}
742-
cursor_status.clone()
743-
})
744-
.unwrap_or(CursorImageStatus::default_named());
732+
let cursor_status = seat.cursor_image_status();
745733

746734
// cursor ...
747735
if let CursorImageStatus::Surface(wl_surface) = cursor_status {
@@ -1021,19 +1009,7 @@ impl Common {
10211009
.iter()
10221010
.filter(|seat| &seat.active_output() == output)
10231011
{
1024-
let cursor_status = seat
1025-
.user_data()
1026-
.get::<Mutex<CursorImageStatus>>()
1027-
.map(|lock| {
1028-
let mut cursor_status = lock.lock().unwrap();
1029-
if let CursorImageStatus::Surface(ref surface) = *cursor_status {
1030-
if !surface.alive() {
1031-
*cursor_status = CursorImageStatus::default_named();
1032-
}
1033-
}
1034-
cursor_status.clone()
1035-
})
1036-
.unwrap_or(CursorImageStatus::default_named());
1012+
let cursor_status = seat.cursor_image_status();
10371013

10381014
if let CursorImageStatus::Surface(wl_surface) = cursor_status {
10391015
send_frames_surface_tree(

src/wayland/handlers/data_device.rs

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// SPDX-License-Identifier: GPL-3.0-only
22

3-
use crate::state::State;
3+
use crate::{state::State, utils::prelude::SeatExt};
44
use smithay::{
55
delegate_data_device,
66
input::{
@@ -70,26 +70,20 @@ impl ClientDndGrabHandler for State {
7070
let user_data = seat.user_data();
7171
user_data.insert_if_missing_threadsafe::<Mutex<Option<DnDIcon>>, _>(|| Default::default());
7272

73-
let offset = seat
74-
.user_data()
75-
.get::<Mutex<CursorImageStatus>>()
76-
.map(|guard| {
77-
if let CursorImageStatus::Surface(ref surface) = *guard.lock().unwrap() {
78-
compositor::with_states(surface, |states| {
79-
let hotspot = states
80-
.data_map
81-
.get::<CursorImageSurfaceData>()
82-
.unwrap()
83-
.lock()
84-
.unwrap()
85-
.hotspot;
86-
Point::from((-hotspot.x, -hotspot.y))
87-
})
88-
} else {
89-
(0, 0).into()
90-
}
73+
let offset = if let CursorImageStatus::Surface(ref surface) = seat.cursor_image_status() {
74+
compositor::with_states(surface, |states| {
75+
let hotspot = states
76+
.data_map
77+
.get::<CursorImageSurfaceData>()
78+
.unwrap()
79+
.lock()
80+
.unwrap()
81+
.hotspot;
82+
Point::from((-hotspot.x, -hotspot.y))
9183
})
92-
.unwrap_or_default();
84+
} else {
85+
(0, 0).into()
86+
};
9387

9488
*user_data
9589
.get::<Mutex<Option<DnDIcon>>>()

src/wayland/handlers/seat.rs

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ use crate::{
44
shell::focus::target::{KeyboardFocusTarget, PointerFocusTarget},
55
shell::Devices,
66
state::State,
7+
utils::prelude::SeatExt,
78
};
89
use smithay::{
910
delegate_cursor_shape, delegate_seat,
1011
input::{keyboard::LedState, pointer::CursorImageStatus, SeatHandler, SeatState},
1112
};
12-
use std::sync::Mutex;
1313

1414
impl SeatHandler for State {
1515
type KeyboardFocus = KeyboardFocusTarget;
@@ -20,17 +20,8 @@ impl SeatHandler for State {
2020
&mut self.common.seat_state
2121
}
2222

23-
fn cursor_image(
24-
&mut self,
25-
seat: &smithay::input::Seat<Self>,
26-
image: smithay::input::pointer::CursorImageStatus,
27-
) {
28-
*seat
29-
.user_data()
30-
.get::<Mutex<CursorImageStatus>>()
31-
.unwrap()
32-
.lock()
33-
.unwrap() = image;
23+
fn cursor_image(&mut self, seat: &smithay::input::Seat<Self>, image: CursorImageStatus) {
24+
seat.set_cursor_image_status(image);
3425
}
3526

3627
fn focus_changed(

0 commit comments

Comments
 (0)