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.
This commit is contained in:
parent
ddc23fcaa5
commit
fa4bffdbe6
8 changed files with 52 additions and 105 deletions
|
|
@ -20,8 +20,7 @@ use smithay::{
|
||||||
reexports::wayland_server::protocol::wl_surface,
|
reexports::wayland_server::protocol::wl_surface,
|
||||||
render_elements,
|
render_elements,
|
||||||
utils::{
|
utils::{
|
||||||
Buffer as BufferCoords, IsAlive, Logical, Monotonic, Physical, Point, Scale, Size, Time,
|
Buffer as BufferCoords, Logical, Monotonic, Physical, Point, Scale, Size, Time, Transform,
|
||||||
Transform,
|
|
||||||
},
|
},
|
||||||
wayland::compositor::{get_role, with_states},
|
wayland::compositor::{get_role, with_states},
|
||||||
};
|
};
|
||||||
|
|
@ -266,20 +265,7 @@ where
|
||||||
R::TextureId: Send + Clone + 'static,
|
R::TextureId: Send + Clone + 'static,
|
||||||
{
|
{
|
||||||
// draw the cursor as relevant
|
// draw the cursor as relevant
|
||||||
// reset the cursor if the surface is no longer alive
|
let cursor_status = seat.cursor_image_status();
|
||||||
let cursor_status = seat
|
|
||||||
.user_data()
|
|
||||||
.get::<Mutex<CursorImageStatus>>()
|
|
||||||
.map(|lock| {
|
|
||||||
let mut cursor_status = lock.lock().unwrap();
|
|
||||||
if let CursorImageStatus::Surface(ref surface) = *cursor_status {
|
|
||||||
if !surface.alive() {
|
|
||||||
*cursor_status = CursorImageStatus::default_named();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
cursor_status.clone()
|
|
||||||
})
|
|
||||||
.unwrap_or(CursorImageStatus::default_named());
|
|
||||||
|
|
||||||
let seat_userdata = seat.user_data();
|
let seat_userdata = seat.user_data();
|
||||||
let mut state_ref = seat_userdata.get::<CursorState>().unwrap().lock().unwrap();
|
let mut state_ref = seat_userdata.get::<CursorState>().unwrap().lock().unwrap();
|
||||||
|
|
|
||||||
|
|
@ -1333,8 +1333,7 @@ impl PointerTarget<State> for CosmicStack {
|
||||||
.lock()
|
.lock()
|
||||||
.unwrap();
|
.unwrap();
|
||||||
cursor_state.set_shape(next.cursor_shape());
|
cursor_state.set_shape(next.cursor_shape());
|
||||||
let cursor_status = seat.user_data().get::<Mutex<CursorImageStatus>>().unwrap();
|
seat.set_cursor_image_status(CursorImageStatus::default_named());
|
||||||
*cursor_status.lock().unwrap() = CursorImageStatus::default_named();
|
|
||||||
});
|
});
|
||||||
|
|
||||||
event.location -= self.0.with_program(|p| {
|
event.location -= self.0.with_program(|p| {
|
||||||
|
|
@ -1363,8 +1362,7 @@ impl PointerTarget<State> for CosmicStack {
|
||||||
.lock()
|
.lock()
|
||||||
.unwrap();
|
.unwrap();
|
||||||
cursor_state.set_shape(next.cursor_shape());
|
cursor_state.set_shape(next.cursor_shape());
|
||||||
let cursor_status = seat.user_data().get::<Mutex<CursorImageStatus>>().unwrap();
|
seat.set_cursor_image_status(CursorImageStatus::default_named());
|
||||||
*cursor_status.lock().unwrap() = CursorImageStatus::default_named();
|
|
||||||
});
|
});
|
||||||
|
|
||||||
let active_window_geo = self.0.with_program(|p| {
|
let active_window_geo = self.0.with_program(|p| {
|
||||||
|
|
|
||||||
|
|
@ -711,8 +711,7 @@ impl PointerTarget<State> for CosmicWindow {
|
||||||
|
|
||||||
let cursor_state = seat.user_data().get::<CursorState>().unwrap();
|
let cursor_state = seat.user_data().get::<CursorState>().unwrap();
|
||||||
cursor_state.lock().unwrap().set_shape(next.cursor_shape());
|
cursor_state.lock().unwrap().set_shape(next.cursor_shape());
|
||||||
let cursor_status = seat.user_data().get::<Mutex<CursorImageStatus>>().unwrap();
|
seat.set_cursor_image_status(CursorImageStatus::default_named());
|
||||||
*cursor_status.lock().unwrap() = CursorImageStatus::default_named();
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
@ -736,8 +735,7 @@ impl PointerTarget<State> for CosmicWindow {
|
||||||
|
|
||||||
let cursor_state = seat.user_data().get::<CursorState>().unwrap();
|
let cursor_state = seat.user_data().get::<CursorState>().unwrap();
|
||||||
cursor_state.lock().unwrap().set_shape(next.cursor_shape());
|
cursor_state.lock().unwrap().set_shape(next.cursor_shape());
|
||||||
let cursor_status = seat.user_data().get::<Mutex<CursorImageStatus>>().unwrap();
|
seat.set_cursor_image_status(CursorImageStatus::default_named());
|
||||||
*cursor_status.lock().unwrap() = CursorImageStatus::default_named();
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1427,12 +1427,7 @@ impl Common {
|
||||||
}
|
}
|
||||||
|
|
||||||
let is_cursor_image = shell.seats.iter().any(|seat| {
|
let is_cursor_image = shell.seats.iter().any(|seat| {
|
||||||
seat.user_data()
|
matches!(seat.cursor_image_status(), CursorImageStatus::Surface(ref cursor_surface) if cursor_surface == surface)
|
||||||
.get::<Mutex<CursorImageStatus>>()
|
|
||||||
.map(|guard| {
|
|
||||||
matches!(*guard.lock().unwrap(), CursorImageStatus::Surface(ref cursor_surface) if cursor_surface == surface)
|
|
||||||
})
|
|
||||||
.unwrap_or(false)
|
|
||||||
});
|
});
|
||||||
|
|
||||||
if is_cursor_image {
|
if is_cursor_image {
|
||||||
|
|
|
||||||
|
|
@ -252,6 +252,8 @@ pub trait SeatExt {
|
||||||
loc: impl Into<Point<f64, Buffer>>,
|
loc: impl Into<Point<f64, Buffer>>,
|
||||||
time: Time<Monotonic>,
|
time: Time<Monotonic>,
|
||||||
) -> Option<(Rectangle<i32, Buffer>, Point<i32, Buffer>)>;
|
) -> Option<(Rectangle<i32, Buffer>, Point<i32, Buffer>)>;
|
||||||
|
fn cursor_image_status(&self) -> CursorImageStatus;
|
||||||
|
fn set_cursor_image_status(&self, status: CursorImageStatus);
|
||||||
}
|
}
|
||||||
|
|
||||||
impl SeatExt for Seat<State> {
|
impl SeatExt for Seat<State> {
|
||||||
|
|
@ -337,21 +339,7 @@ impl SeatExt for Seat<State> {
|
||||||
) -> Option<(Rectangle<i32, Buffer>, Point<i32, Buffer>)> {
|
) -> Option<(Rectangle<i32, Buffer>, Point<i32, Buffer>)> {
|
||||||
let location = loc.into().to_i32_round();
|
let location = loc.into().to_i32_round();
|
||||||
|
|
||||||
let cursor_status = self
|
match self.cursor_image_status() {
|
||||||
.user_data()
|
|
||||||
.get::<Mutex<CursorImageStatus>>()
|
|
||||||
.map(|lock| {
|
|
||||||
let mut cursor_status = lock.lock().unwrap();
|
|
||||||
if let CursorImageStatus::Surface(ref surface) = *cursor_status {
|
|
||||||
if !surface.alive() {
|
|
||||||
*cursor_status = CursorImageStatus::default_named();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
cursor_status.clone()
|
|
||||||
})
|
|
||||||
.unwrap_or(CursorImageStatus::default_named());
|
|
||||||
|
|
||||||
match cursor_status {
|
|
||||||
CursorImageStatus::Surface(surface) => {
|
CursorImageStatus::Surface(surface) => {
|
||||||
let hotspot = with_states(&surface, |states| {
|
let hotspot = with_states(&surface, |states| {
|
||||||
states
|
states
|
||||||
|
|
@ -387,4 +375,25 @@ impl SeatExt for Seat<State> {
|
||||||
CursorImageStatus::Hidden => None,
|
CursorImageStatus::Hidden => None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn cursor_image_status(&self) -> CursorImageStatus {
|
||||||
|
self.user_data()
|
||||||
|
.get::<Mutex<CursorImageStatus>>()
|
||||||
|
// Reset the cursor if the surface is no longer alive
|
||||||
|
.map(|lock| {
|
||||||
|
let mut cursor_status = lock.lock().unwrap();
|
||||||
|
if let CursorImageStatus::Surface(ref surface) = *cursor_status {
|
||||||
|
if !surface.alive() {
|
||||||
|
*cursor_status = CursorImageStatus::default_named();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
cursor_status.clone()
|
||||||
|
})
|
||||||
|
.unwrap_or(CursorImageStatus::default_named())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn set_cursor_image_status(&self, status: CursorImageStatus) {
|
||||||
|
let cursor_status = self.user_data().get::<Mutex<CursorImageStatus>>().unwrap();
|
||||||
|
*cursor_status.lock().unwrap() = status;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
32
src/state.rs
32
src/state.rs
|
|
@ -70,7 +70,7 @@ use smithay::{
|
||||||
Client, DisplayHandle, Resource,
|
Client, DisplayHandle, Resource,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
utils::{Clock, IsAlive, Monotonic, Point},
|
utils::{Clock, Monotonic, Point},
|
||||||
wayland::{
|
wayland::{
|
||||||
alpha_modifier::AlphaModifierState,
|
alpha_modifier::AlphaModifierState,
|
||||||
compositor::{CompositorClientState, CompositorState, SurfaceData},
|
compositor::{CompositorClientState, CompositorState, SurfaceData},
|
||||||
|
|
@ -118,7 +118,7 @@ use std::{
|
||||||
collections::HashSet,
|
collections::HashSet,
|
||||||
ffi::OsString,
|
ffi::OsString,
|
||||||
process::Child,
|
process::Child,
|
||||||
sync::{atomic::AtomicBool, Arc, Mutex, Once},
|
sync::{atomic::AtomicBool, Arc, Once},
|
||||||
time::{Duration, Instant},
|
time::{Duration, Instant},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -729,19 +729,7 @@ impl Common {
|
||||||
.iter()
|
.iter()
|
||||||
.filter(|seat| &seat.active_output() == output)
|
.filter(|seat| &seat.active_output() == output)
|
||||||
{
|
{
|
||||||
let cursor_status = seat
|
let cursor_status = seat.cursor_image_status();
|
||||||
.user_data()
|
|
||||||
.get::<Mutex<CursorImageStatus>>()
|
|
||||||
.map(|lock| {
|
|
||||||
let mut cursor_status = lock.lock().unwrap();
|
|
||||||
if let CursorImageStatus::Surface(ref surface) = *cursor_status {
|
|
||||||
if !surface.alive() {
|
|
||||||
*cursor_status = CursorImageStatus::default_named();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
cursor_status.clone()
|
|
||||||
})
|
|
||||||
.unwrap_or(CursorImageStatus::default_named());
|
|
||||||
|
|
||||||
// cursor ...
|
// cursor ...
|
||||||
if let CursorImageStatus::Surface(wl_surface) = cursor_status {
|
if let CursorImageStatus::Surface(wl_surface) = cursor_status {
|
||||||
|
|
@ -1021,19 +1009,7 @@ impl Common {
|
||||||
.iter()
|
.iter()
|
||||||
.filter(|seat| &seat.active_output() == output)
|
.filter(|seat| &seat.active_output() == output)
|
||||||
{
|
{
|
||||||
let cursor_status = seat
|
let cursor_status = seat.cursor_image_status();
|
||||||
.user_data()
|
|
||||||
.get::<Mutex<CursorImageStatus>>()
|
|
||||||
.map(|lock| {
|
|
||||||
let mut cursor_status = lock.lock().unwrap();
|
|
||||||
if let CursorImageStatus::Surface(ref surface) = *cursor_status {
|
|
||||||
if !surface.alive() {
|
|
||||||
*cursor_status = CursorImageStatus::default_named();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
cursor_status.clone()
|
|
||||||
})
|
|
||||||
.unwrap_or(CursorImageStatus::default_named());
|
|
||||||
|
|
||||||
if let CursorImageStatus::Surface(wl_surface) = cursor_status {
|
if let CursorImageStatus::Surface(wl_surface) = cursor_status {
|
||||||
send_frames_surface_tree(
|
send_frames_surface_tree(
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
// SPDX-License-Identifier: GPL-3.0-only
|
// SPDX-License-Identifier: GPL-3.0-only
|
||||||
|
|
||||||
use crate::state::State;
|
use crate::{state::State, utils::prelude::SeatExt};
|
||||||
use smithay::{
|
use smithay::{
|
||||||
delegate_data_device,
|
delegate_data_device,
|
||||||
input::{
|
input::{
|
||||||
|
|
@ -70,26 +70,20 @@ impl ClientDndGrabHandler for State {
|
||||||
let user_data = seat.user_data();
|
let user_data = seat.user_data();
|
||||||
user_data.insert_if_missing_threadsafe::<Mutex<Option<DnDIcon>>, _>(|| Default::default());
|
user_data.insert_if_missing_threadsafe::<Mutex<Option<DnDIcon>>, _>(|| Default::default());
|
||||||
|
|
||||||
let offset = seat
|
let offset = if let CursorImageStatus::Surface(ref surface) = seat.cursor_image_status() {
|
||||||
.user_data()
|
compositor::with_states(surface, |states| {
|
||||||
.get::<Mutex<CursorImageStatus>>()
|
let hotspot = states
|
||||||
.map(|guard| {
|
.data_map
|
||||||
if let CursorImageStatus::Surface(ref surface) = *guard.lock().unwrap() {
|
.get::<CursorImageSurfaceData>()
|
||||||
compositor::with_states(surface, |states| {
|
.unwrap()
|
||||||
let hotspot = states
|
.lock()
|
||||||
.data_map
|
.unwrap()
|
||||||
.get::<CursorImageSurfaceData>()
|
.hotspot;
|
||||||
.unwrap()
|
Point::from((-hotspot.x, -hotspot.y))
|
||||||
.lock()
|
|
||||||
.unwrap()
|
|
||||||
.hotspot;
|
|
||||||
Point::from((-hotspot.x, -hotspot.y))
|
|
||||||
})
|
|
||||||
} else {
|
|
||||||
(0, 0).into()
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
.unwrap_or_default();
|
} else {
|
||||||
|
(0, 0).into()
|
||||||
|
};
|
||||||
|
|
||||||
*user_data
|
*user_data
|
||||||
.get::<Mutex<Option<DnDIcon>>>()
|
.get::<Mutex<Option<DnDIcon>>>()
|
||||||
|
|
|
||||||
|
|
@ -4,12 +4,12 @@ use crate::{
|
||||||
shell::focus::target::{KeyboardFocusTarget, PointerFocusTarget},
|
shell::focus::target::{KeyboardFocusTarget, PointerFocusTarget},
|
||||||
shell::Devices,
|
shell::Devices,
|
||||||
state::State,
|
state::State,
|
||||||
|
utils::prelude::SeatExt,
|
||||||
};
|
};
|
||||||
use smithay::{
|
use smithay::{
|
||||||
delegate_cursor_shape, delegate_seat,
|
delegate_cursor_shape, delegate_seat,
|
||||||
input::{keyboard::LedState, pointer::CursorImageStatus, SeatHandler, SeatState},
|
input::{keyboard::LedState, pointer::CursorImageStatus, SeatHandler, SeatState},
|
||||||
};
|
};
|
||||||
use std::sync::Mutex;
|
|
||||||
|
|
||||||
impl SeatHandler for State {
|
impl SeatHandler for State {
|
||||||
type KeyboardFocus = KeyboardFocusTarget;
|
type KeyboardFocus = KeyboardFocusTarget;
|
||||||
|
|
@ -20,17 +20,8 @@ impl SeatHandler for State {
|
||||||
&mut self.common.seat_state
|
&mut self.common.seat_state
|
||||||
}
|
}
|
||||||
|
|
||||||
fn cursor_image(
|
fn cursor_image(&mut self, seat: &smithay::input::Seat<Self>, image: CursorImageStatus) {
|
||||||
&mut self,
|
seat.set_cursor_image_status(image);
|
||||||
seat: &smithay::input::Seat<Self>,
|
|
||||||
image: smithay::input::pointer::CursorImageStatus,
|
|
||||||
) {
|
|
||||||
*seat
|
|
||||||
.user_data()
|
|
||||||
.get::<Mutex<CursorImageStatus>>()
|
|
||||||
.unwrap()
|
|
||||||
.lock()
|
|
||||||
.unwrap() = image;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn focus_changed(
|
fn focus_changed(
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue