Render cursors
This commit is contained in:
parent
4e238d8848
commit
db6ca9e61c
10 changed files with 481 additions and 46 deletions
358
src/backend/cursor.rs
Normal file
358
src/backend/cursor.rs
Normal file
|
|
@ -0,0 +1,358 @@
|
|||
// SPDX-License-Identifier: GPL-3.0-only
|
||||
|
||||
use std::{
|
||||
cell::RefCell,
|
||||
io::Read,
|
||||
rc::Rc,
|
||||
sync::Mutex,
|
||||
};
|
||||
use smithay::{
|
||||
backend::{
|
||||
renderer::{Frame, ImportAll, Renderer, Texture, gles2},
|
||||
SwapBuffersError,
|
||||
},
|
||||
desktop::space::{RenderElement, SpaceOutputTuple, SurfaceTree, DynamicRenderElements},
|
||||
reexports::wayland_server::protocol::wl_surface,
|
||||
utils::{Logical, Buffer, Point, Rectangle, Size, Transform},
|
||||
wayland::{
|
||||
compositor::{get_role, with_states},
|
||||
seat::{Seat, CursorImageAttributes, CursorImageStatus},
|
||||
},
|
||||
};
|
||||
use xcursor::{
|
||||
parser::{parse_xcursor, Image},
|
||||
CursorTheme,
|
||||
};
|
||||
use crate::state::get_dnd_icon;
|
||||
|
||||
static FALLBACK_CURSOR_DATA: &[u8] = include_bytes!("../../resources/cursor.rgba");
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Cursor {
|
||||
icons: Vec<Image>,
|
||||
size: u32,
|
||||
}
|
||||
|
||||
impl Cursor {
|
||||
pub fn load() -> Cursor {
|
||||
let name = std::env::var("XCURSOR_THEME")
|
||||
.ok()
|
||||
.unwrap_or_else(|| "default".into());
|
||||
let size = std::env::var("XCURSOR_SIZE")
|
||||
.ok()
|
||||
.and_then(|s| s.parse().ok())
|
||||
.unwrap_or(24);
|
||||
|
||||
let theme = CursorTheme::load(&name);
|
||||
let icons = load_icon(&theme)
|
||||
.map_err(|err| slog_scope::warn!("Unable to load xcursor: {}, using fallback cursor", err))
|
||||
.unwrap_or_else(|_| {
|
||||
vec![Image {
|
||||
size: 32,
|
||||
width: 64,
|
||||
height: 64,
|
||||
xhot: 1,
|
||||
yhot: 1,
|
||||
delay: 1,
|
||||
pixels_rgba: Vec::from(FALLBACK_CURSOR_DATA),
|
||||
pixels_argb: vec![], //unused
|
||||
}]
|
||||
});
|
||||
|
||||
Cursor { icons, size }
|
||||
}
|
||||
|
||||
pub fn get_image(&self, scale: u32, millis: u32) -> Image {
|
||||
let size = self.size * scale;
|
||||
frame(millis, size, &self.icons)
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for Cursor {
|
||||
fn default() -> Cursor { Cursor::load() }
|
||||
}
|
||||
|
||||
fn nearest_images(size: u32, images: &[Image]) -> impl Iterator<Item = &Image> {
|
||||
// Follow the nominal size of the cursor to choose the nearest
|
||||
let nearest_image = images
|
||||
.iter()
|
||||
.min_by_key(|image| (size as i32 - image.size as i32).abs())
|
||||
.unwrap();
|
||||
|
||||
images
|
||||
.iter()
|
||||
.filter(move |image| image.width == nearest_image.width && image.height == nearest_image.height)
|
||||
}
|
||||
|
||||
fn frame(mut millis: u32, size: u32, images: &[Image]) -> Image {
|
||||
let total = nearest_images(size, images).fold(0, |acc, image| acc + image.delay);
|
||||
millis %= total;
|
||||
|
||||
for img in nearest_images(size, images) {
|
||||
if millis < img.delay {
|
||||
return img.clone();
|
||||
}
|
||||
millis -= img.delay;
|
||||
}
|
||||
|
||||
unreachable!()
|
||||
}
|
||||
|
||||
#[derive(thiserror::Error, Debug)]
|
||||
enum Error {
|
||||
#[error("Theme has no default cursor")]
|
||||
NoDefaultCursor,
|
||||
#[error("Error opening xcursor file: {0}")]
|
||||
File(#[from] std::io::Error),
|
||||
#[error("Failed to parse XCursor file")]
|
||||
Parse,
|
||||
}
|
||||
|
||||
fn load_icon(theme: &CursorTheme) -> Result<Vec<Image>, Error> {
|
||||
let icon_path = theme.load_icon("default").ok_or(Error::NoDefaultCursor)?;
|
||||
let mut cursor_file = std::fs::File::open(&icon_path)?;
|
||||
let mut cursor_data = Vec::new();
|
||||
cursor_file.read_to_end(&mut cursor_data)?;
|
||||
parse_xcursor(&cursor_data).ok_or(Error::Parse)
|
||||
}
|
||||
|
||||
pub fn draw_surface_cursor<R, F, E, T>(
|
||||
surface: wl_surface::WlSurface,
|
||||
location: impl Into<Point<i32, Logical>>,
|
||||
) -> impl RenderElement<R, F, E, T>
|
||||
where
|
||||
R: Renderer<Error = E, TextureId = T, Frame = F> + ImportAll + 'static,
|
||||
F: Frame<Error = E, TextureId = T> + 'static,
|
||||
E: std::error::Error + Into<SwapBuffersError> + 'static,
|
||||
T: Texture + 'static,
|
||||
{
|
||||
let mut position = location.into();
|
||||
let ret = with_states(&surface, |states| {
|
||||
Some(
|
||||
states
|
||||
.data_map
|
||||
.get::<Mutex<CursorImageAttributes>>()
|
||||
.unwrap()
|
||||
.lock()
|
||||
.unwrap()
|
||||
.hotspot,
|
||||
)
|
||||
})
|
||||
.unwrap_or(None);
|
||||
position -= match ret {
|
||||
Some(h) => h,
|
||||
None => {
|
||||
slog_scope::warn!("Trying to display as a cursor a surface that does not have the CursorImage role.");
|
||||
(0, 0).into()
|
||||
}
|
||||
};
|
||||
SurfaceTree { surface, position }
|
||||
}
|
||||
|
||||
pub fn draw_dnd_icon<R, F, E, T>(
|
||||
surface: wl_surface::WlSurface,
|
||||
location: impl Into<Point<i32, Logical>>,
|
||||
) -> impl RenderElement<R, F, E, T>
|
||||
where
|
||||
R: Renderer<Error = E, TextureId = T, Frame = F> + ImportAll + 'static,
|
||||
F: Frame<Error = E, TextureId = T> + 'static,
|
||||
E: std::error::Error + Into<SwapBuffersError> + 'static,
|
||||
T: Texture + 'static,
|
||||
{
|
||||
if get_role(&surface) != Some("dnd_icon") {
|
||||
slog_scope::warn!("Trying to display as a dnd icon a surface that does not have the DndIcon role.");
|
||||
}
|
||||
SurfaceTree {
|
||||
surface,
|
||||
position: location.into(),
|
||||
}
|
||||
}
|
||||
|
||||
pub struct PointerElement<T: Texture> {
|
||||
texture: T,
|
||||
position: Point<i32, Logical>,
|
||||
size: Size<i32, Logical>,
|
||||
new_frame: bool,
|
||||
}
|
||||
|
||||
impl<T: Texture> PointerElement<T> {
|
||||
pub fn new(texture: T, relative_pointer_pos: Point<i32, Logical>, new_frame: bool) -> PointerElement<T> {
|
||||
let size = texture.size().to_logical(1, Transform::Normal);
|
||||
PointerElement {
|
||||
texture,
|
||||
position: relative_pointer_pos,
|
||||
size,
|
||||
new_frame,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<R, F, E, T> RenderElement<R, F, E, T> for PointerElement<T>
|
||||
where
|
||||
R: Renderer<Error = E, TextureId = T, Frame = F> + ImportAll + 'static,
|
||||
F: Frame<Error = E, TextureId = T> + 'static,
|
||||
E: std::error::Error + Into<SwapBuffersError> + 'static,
|
||||
T: Texture + 'static,
|
||||
{
|
||||
fn id(&self) -> usize {
|
||||
0
|
||||
}
|
||||
|
||||
fn geometry(&self) -> Rectangle<i32, Logical> {
|
||||
Rectangle::from_loc_and_size(self.position, self.size)
|
||||
}
|
||||
|
||||
fn accumulated_damage(&self, _: Option<SpaceOutputTuple<'_, '_>>) -> Vec<Rectangle<i32, Logical>> {
|
||||
if self.new_frame { vec![Rectangle::from_loc_and_size((0, 0), self.size)] } else { vec![] }
|
||||
}
|
||||
|
||||
fn draw(
|
||||
&self,
|
||||
_renderer: &mut R,
|
||||
frame: &mut F,
|
||||
scale: f64,
|
||||
position: Point<i32, Logical>,
|
||||
damage: &[Rectangle<i32, Logical>],
|
||||
_log: &slog::Logger,
|
||||
) -> Result<(), R::Error> {
|
||||
frame.render_texture_at(
|
||||
&self.texture,
|
||||
position.to_f64().to_physical(scale as f64).to_i32_round(),
|
||||
1,
|
||||
scale as f64,
|
||||
Transform::Normal,
|
||||
&*damage
|
||||
.iter()
|
||||
.map(|rect| rect.to_buffer(1, Transform::Normal, &self.size))
|
||||
.collect::<Vec<_>>(),
|
||||
1.0,
|
||||
)?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Default)]
|
||||
struct CursorState {
|
||||
cursor: Cursor,
|
||||
current_image: RefCell<Option<Image>>,
|
||||
}
|
||||
|
||||
pub type Textures = Vec<(Image, gles2::Gles2Texture)>;
|
||||
|
||||
pub fn draw_cursor(
|
||||
renderer: &mut gles2::Gles2Renderer,
|
||||
seat: &Seat,
|
||||
start_time: &std::time::Instant,
|
||||
draw_default: bool,
|
||||
) -> Option<DynamicRenderElements<gles2::Gles2Renderer>>
|
||||
{
|
||||
let pointer = match seat.get_pointer() {
|
||||
Some(ptr) => ptr,
|
||||
None => return None,
|
||||
};
|
||||
let location = pointer.current_location();
|
||||
|
||||
// draw the dnd icon if applicable
|
||||
{
|
||||
if let Some(wl_surface) = get_dnd_icon(seat) {
|
||||
if wl_surface.as_ref().is_alive() {
|
||||
return Some(Box::new(draw_dnd_icon(
|
||||
wl_surface,
|
||||
location.to_i32_round(),
|
||||
)));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// draw the cursor as relevant
|
||||
{
|
||||
// reset the cursor if the surface is no longer alive
|
||||
let cursor_status = seat
|
||||
.user_data()
|
||||
.get::<RefCell<CursorImageStatus>>()
|
||||
.map(|cell| {
|
||||
let mut cursor_status = cell.borrow_mut();
|
||||
if let CursorImageStatus::Image(ref surface) = *cursor_status {
|
||||
if !surface.as_ref().is_alive() {
|
||||
*cursor_status = CursorImageStatus::Default;
|
||||
}
|
||||
}
|
||||
cursor_status.clone()
|
||||
})
|
||||
.unwrap_or(CursorImageStatus::Default);
|
||||
|
||||
if let CursorImageStatus::Image(wl_surface) = cursor_status {
|
||||
Some(Box::new(draw_surface_cursor(
|
||||
wl_surface.clone(),
|
||||
location.to_i32_round(),
|
||||
)))
|
||||
} else if draw_default {
|
||||
let seat_userdata = seat.user_data();
|
||||
seat_userdata.insert_if_missing(CursorState::default);
|
||||
let state = seat_userdata.get::<CursorState>().unwrap();
|
||||
let frame = state.cursor.get_image(1, start_time.elapsed().as_millis() as u32);
|
||||
let new_frame = state.current_image.borrow().as_ref() != Some(&frame);
|
||||
|
||||
let egl_userdata = renderer.egl_context().user_data();
|
||||
egl_userdata.insert_if_missing(|| Rc::new(RefCell::new(Textures::new())));
|
||||
let pointer_images = egl_userdata.get::<Rc<RefCell<Textures>>>().unwrap().clone();
|
||||
let pointer_images_ref = &mut *pointer_images.borrow_mut();
|
||||
let pointer_image = pointer_images_ref
|
||||
.iter()
|
||||
.find_map(|(image, texture)| if image == &frame { Some(texture) } else { None })
|
||||
.cloned()
|
||||
.unwrap_or_else(|| {
|
||||
let texture = import_bitmap(renderer, &frame.pixels_rgba, gles2::ffi::RGBA, (frame.width as i32, frame.height as i32))
|
||||
.expect("Failed to import cursor bitmap");
|
||||
pointer_images_ref.push((frame.clone(), texture.clone()));
|
||||
texture
|
||||
});
|
||||
let hotspot = Point::<i32, Logical>::from((frame.xhot as i32, frame.yhot as i32));
|
||||
*state.current_image.borrow_mut() = Some(frame);
|
||||
|
||||
Some(Box::new(PointerElement::new(
|
||||
pointer_image.clone(),
|
||||
location.to_i32_round() - hotspot,
|
||||
new_frame,
|
||||
)))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn import_bitmap(
|
||||
renderer: &mut gles2::Gles2Renderer,
|
||||
image: impl std::convert::AsRef<[u8]>,
|
||||
format: gles2::ffi::types::GLenum,
|
||||
size: impl Into<Size<i32, Buffer>>,
|
||||
) -> Result<gles2::Gles2Texture, gles2::Gles2Error> {
|
||||
use smithay::backend::renderer::gles2::ffi;
|
||||
|
||||
let size = size.into();
|
||||
renderer.with_context(|renderer, gl| unsafe {
|
||||
let mut tex = 0;
|
||||
gl.GenTextures(1, &mut tex);
|
||||
gl.BindTexture(ffi::TEXTURE_2D, tex);
|
||||
gl.TexParameteri(ffi::TEXTURE_2D, ffi::TEXTURE_WRAP_S, ffi::CLAMP_TO_EDGE as i32);
|
||||
gl.TexParameteri(ffi::TEXTURE_2D, ffi::TEXTURE_WRAP_T, ffi::CLAMP_TO_EDGE as i32);
|
||||
gl.TexImage2D(
|
||||
ffi::TEXTURE_2D,
|
||||
0,
|
||||
format as i32,
|
||||
size.w,
|
||||
size.h,
|
||||
0,
|
||||
format,
|
||||
ffi::UNSIGNED_BYTE as u32,
|
||||
image.as_ref().as_ptr() as *const _,
|
||||
);
|
||||
gl.BindTexture(ffi::TEXTURE_2D, 0);
|
||||
|
||||
gles2::Gles2Texture::from_raw(
|
||||
renderer,
|
||||
tex,
|
||||
size,
|
||||
)
|
||||
})
|
||||
}
|
||||
|
|
@ -1,6 +1,7 @@
|
|||
// SPDX-License-Identifier: GPL-3.0-only
|
||||
|
||||
use crate::{
|
||||
backend::cursor,
|
||||
state::{BackendData, Common, State},
|
||||
utils::GlobalDrop,
|
||||
};
|
||||
|
|
@ -45,7 +46,7 @@ use std::{
|
|||
time::{Duration, Instant, SystemTime},
|
||||
};
|
||||
|
||||
mod crtc_mapping;
|
||||
mod drm_helpers;
|
||||
mod session_fd;
|
||||
use session_fd::*;
|
||||
|
||||
|
|
@ -260,7 +261,7 @@ impl Device {
|
|||
let drm = &mut *self.drm.as_source_mut();
|
||||
|
||||
// enumerate our outputs
|
||||
let config = crtc_mapping::display_configuration(drm, self.supports_atomic)?;
|
||||
let config = drm_helpers::display_configuration(drm, self.supports_atomic)?;
|
||||
|
||||
let surfaces = self.surfaces.iter()
|
||||
.map(|(c, s)| (*c, s.surface.current_connectors().into_iter().next().unwrap()))
|
||||
|
|
@ -293,9 +294,9 @@ impl Device {
|
|||
let drm = &mut *self.drm.as_source_mut();
|
||||
let crtc_info = drm.get_crtc(crtc)?;
|
||||
let conn_info = drm.get_connector(conn)?;
|
||||
let vrr = crtc_mapping::set_vrr(drm, crtc, conn, true)?;
|
||||
let interface = crtc_mapping::interface_name(drm, conn)?;
|
||||
let edid_info = crtc_mapping::edid_info(drm, conn)?;
|
||||
let vrr = drm_helpers::set_vrr(drm, crtc, conn, true)?;
|
||||
let interface = drm_helpers::interface_name(drm, conn)?;
|
||||
let edid_info = drm_helpers::edid_info(drm, conn)?;
|
||||
let mode = crtc_info.mode().unwrap_or(conn_info.modes()[0]);
|
||||
let mut surface = drm.create_surface(crtc, mode, &[conn])?;
|
||||
surface.link(signaler);
|
||||
|
|
@ -351,7 +352,7 @@ impl Device {
|
|||
_global: output_global.into(),
|
||||
surface: target,
|
||||
vrr,
|
||||
refresh_rate: crtc_mapping::calculate_refresh_rate(mode),
|
||||
refresh_rate: drm_helpers::calculate_refresh_rate(mode),
|
||||
last_submit: None,
|
||||
pending: true,
|
||||
render_timer: timer_handle,
|
||||
|
|
@ -369,7 +370,30 @@ impl Surface {
|
|||
renderer: &mut Gles2Renderer,
|
||||
state: &mut Common,
|
||||
) -> Result<()> {
|
||||
let custom_elements = Vec::new();
|
||||
#[allow(unused_mut)]
|
||||
let mut custom_elements = Vec::new();
|
||||
|
||||
#[cfg(feature = "debug")]
|
||||
{
|
||||
let space = state.spaces.active_space(&self.output);
|
||||
let size = space.output_geometry(&self.output).unwrap();
|
||||
let scale = space.output_scale(&self.output).unwrap();
|
||||
let frame = debug_ui(state, &self.fps, size, scale, true);
|
||||
custom_elements.push(
|
||||
Box::new(frame) as smithay::desktop::space::DynamicRenderElements<Gles2Renderer>
|
||||
);
|
||||
}
|
||||
|
||||
for seat in &state.seats {
|
||||
if let Some(cursor) = cursor::draw_cursor(
|
||||
renderer,
|
||||
seat,
|
||||
&state.start_time,
|
||||
true,
|
||||
) {
|
||||
custom_elements.push(cursor)
|
||||
}
|
||||
}
|
||||
|
||||
let space = state.spaces.active_space_mut(&self.output);
|
||||
let (buffer, age) = self
|
||||
|
|
@ -390,6 +414,10 @@ impl Surface {
|
|||
self.surface
|
||||
.queue_buffer()
|
||||
.with_context(|| "Failed to submit buffer for display")?;
|
||||
#[cfg(feature = "debug")]
|
||||
{
|
||||
self.fps.tick();
|
||||
}
|
||||
}
|
||||
Err(err) => {
|
||||
self.surface.reset_buffers();
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@ use crate::state::State;
|
|||
use anyhow::Result;
|
||||
use smithay::reexports::calloop::EventLoop;
|
||||
|
||||
pub mod cursor;
|
||||
pub mod kms;
|
||||
pub mod winit;
|
||||
pub mod x11;
|
||||
|
|
|
|||
|
|
@ -1,9 +1,9 @@
|
|||
// SPDX-License-Identifier: GPL-3.0-only
|
||||
|
||||
use crate::state::Common;
|
||||
use crate::{
|
||||
backend::cursor,
|
||||
input::{set_active_output, Devices},
|
||||
state::{BackendData, State},
|
||||
state::{BackendData, State, Common},
|
||||
};
|
||||
use anyhow::{Context, Result};
|
||||
use smithay::{
|
||||
|
|
@ -58,6 +58,17 @@ impl WinitState {
|
|||
|
||||
let space = state.spaces.active_space_mut(&self.output);
|
||||
let mut backend = self.backend.borrow_mut();
|
||||
|
||||
for seat in &state.seats {
|
||||
if let Some(cursor) = cursor::draw_cursor(
|
||||
backend.renderer(),
|
||||
seat,
|
||||
&state.start_time,
|
||||
false,
|
||||
) {
|
||||
custom_elements.push(cursor)
|
||||
}
|
||||
}
|
||||
|
||||
backend.bind().with_context(|| "Failed to bind buffer")?;
|
||||
let age = backend.buffer_age().unwrap_or(0);
|
||||
|
|
|
|||
|
|
@ -1,9 +1,9 @@
|
|||
// SPDX-License-Identifier: GPL-3.0-only
|
||||
|
||||
use crate::state::Common;
|
||||
use crate::{
|
||||
backend::cursor,
|
||||
input::{set_active_output, Devices},
|
||||
state::{BackendData, State},
|
||||
state::{BackendData, State, Common},
|
||||
utils::GlobalDrop,
|
||||
};
|
||||
use anyhow::{Context, Result};
|
||||
|
|
@ -166,6 +166,17 @@ impl Surface {
|
|||
);
|
||||
}
|
||||
|
||||
for seat in &state.seats {
|
||||
if let Some(cursor) = cursor::draw_cursor(
|
||||
renderer,
|
||||
seat,
|
||||
&state.start_time,
|
||||
false,
|
||||
) {
|
||||
custom_elements.push(cursor)
|
||||
}
|
||||
}
|
||||
|
||||
let space = state.spaces.active_space_mut(&self.output);
|
||||
let (buffer, age) = self
|
||||
.surface
|
||||
|
|
|
|||
28
src/state.rs
28
src/state.rs
|
|
@ -7,10 +7,13 @@ use crate::{
|
|||
use smithay::{
|
||||
reexports::{
|
||||
calloop::LoopHandle,
|
||||
wayland_server::Display,
|
||||
wayland_server::{
|
||||
Display,
|
||||
protocol::wl_surface::WlSurface,
|
||||
},
|
||||
},
|
||||
wayland::{
|
||||
data_device::{default_action_chooser, init_data_device},
|
||||
data_device::{default_action_chooser, init_data_device, DataDeviceEvent},
|
||||
output::{xdg::init_xdg_output_manager, Output},
|
||||
seat::Seat,
|
||||
shell::xdg::ToplevelSurface,
|
||||
|
|
@ -110,6 +113,15 @@ impl BackendData {
|
|||
}
|
||||
}
|
||||
|
||||
struct DnDIcon {
|
||||
surface: RefCell<Option<WlSurface>>,
|
||||
}
|
||||
|
||||
pub fn get_dnd_icon(seat: &Seat) -> Option<WlSurface> {
|
||||
let userdata = seat.user_data();
|
||||
userdata.get::<DnDIcon>().and_then(|x| x.surface.borrow().clone())
|
||||
}
|
||||
|
||||
impl State {
|
||||
pub fn new(mut display: Display, handle: LoopHandle<'static, State>) -> State {
|
||||
init_shm_global(&mut display, vec![], None);
|
||||
|
|
@ -118,7 +130,17 @@ impl State {
|
|||
let initial_seat = crate::input::add_seat(&mut display, "seat-0".into());
|
||||
init_data_device(
|
||||
&mut display,
|
||||
|_dnd_event| { /* TODO */ },
|
||||
|dnd_event| match dnd_event {
|
||||
DataDeviceEvent::DnDStarted { icon, seat, .. } => {
|
||||
let user_data = seat.user_data();
|
||||
user_data.insert_if_missing(|| DnDIcon { surface: RefCell::new(None) });
|
||||
*user_data.get::<DnDIcon>().unwrap().surface.borrow_mut() = icon;
|
||||
},
|
||||
DataDeviceEvent::DnDDropped { seat } => {
|
||||
seat.user_data().get::<DnDIcon>().unwrap().surface.borrow_mut().take();
|
||||
},
|
||||
_ => {},
|
||||
},
|
||||
default_action_chooser,
|
||||
None,
|
||||
);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue