Add client cursor support
Browse files at this commit2465bba0858662d45ea1e5a321c6fbe11036d99fParent: b0d12992 changed files +55 -33
- src/handlers/mod.rs +8 -20
- src/niri.rs +47 -13
src/handlers/mod.rsmodified+8 -20
| @@ -2,13 +2,13 @@ mod compositor; | ||
| 2 | 2 | mod layer_shell; |
| 3 | 3 | mod xdg_shell; |
| 4 | 4 | |
| 5 | -// | |
| 6 | -// Wl Seat | |
| 5 | +use smithay::input::pointer::CursorImageStatus; | |
| 7 | 6 | use smithay::input::{Seat, SeatHandler, SeatState}; |
| 8 | 7 | use smithay::reexports::wayland_server::protocol::wl_surface::WlSurface; |
| 9 | 8 | use smithay::reexports::wayland_server::Resource; |
| 10 | 9 | use smithay::wayland::data_device::{ |
| 11 | - set_data_device_focus, ClientDndGrabHandler, DataDeviceHandler, ServerDndGrabHandler, | |
| 10 | + set_data_device_focus, ClientDndGrabHandler, DataDeviceHandler, DataDeviceState, | |
| 11 | + ServerDndGrabHandler, | |
| 12 | 12 | }; |
| 13 | 13 | use smithay::{delegate_data_device, delegate_output, delegate_seat}; |
| 14 | 14 | |
| @@ -22,11 +22,10 @@ impl SeatHandler for Niri { | ||
| 22 | 22 | &mut self.seat_state |
| 23 | 23 | } |
| 24 | 24 | |
| 25 | - fn cursor_image( | |
| 26 | - &mut self, | |
| 27 | - _seat: &Seat<Self>, | |
| 28 | - _image: smithay::input::pointer::CursorImageStatus, | |
| 29 | - ) { | |
| 25 | + fn cursor_image(&mut self, _seat: &Seat<Self>, image: CursorImageStatus) { | |
| 26 | + self.cursor_image = image; | |
| 27 | + // FIXME: more granular | |
| 28 | + self.queue_redraw_all(); | |
| 30 | 29 | } |
| 31 | 30 | |
| 32 | 31 | fn focus_changed(&mut self, seat: &Seat<Self>, focused: Option<&WlSurface>) { |
| @@ -35,27 +34,16 @@ impl SeatHandler for Niri { | ||
| 35 | 34 | set_data_device_focus(dh, seat, client); |
| 36 | 35 | } |
| 37 | 36 | } |
| 38 | - | |
| 39 | 37 | delegate_seat!(Niri); |
| 40 | 38 | |
| 41 | -// | |
| 42 | -// Wl Data Device | |
| 43 | -// | |
| 44 | - | |
| 45 | 39 | impl DataDeviceHandler for Niri { |
| 46 | 40 | type SelectionUserData = (); |
| 47 | - fn data_device_state(&self) -> &smithay::wayland::data_device::DataDeviceState { | |
| 41 | + fn data_device_state(&self) -> &DataDeviceState { | |
| 48 | 42 | &self.data_device_state |
| 49 | 43 | } |
| 50 | 44 | } |
| 51 | - | |
| 52 | 45 | impl ClientDndGrabHandler for Niri {} |
| 53 | 46 | impl ServerDndGrabHandler for Niri {} |
| 54 | - | |
| 55 | 47 | delegate_data_device!(Niri); |
| 56 | 48 | |
| 57 | -// | |
| 58 | -// Wl Output & Xdg Output | |
| 59 | -// | |
| 60 | - | |
| 61 | 49 | delegate_output!(Niri); |
src/niri.rsmodified+47 -13
| @@ -1,10 +1,12 @@ | ||
| 1 | 1 | use std::collections::HashMap; |
| 2 | 2 | use std::os::unix::io::AsRawFd; |
| 3 | -use std::sync::Arc; | |
| 3 | +use std::sync::{Arc, Mutex}; | |
| 4 | 4 | use std::time::Duration; |
| 5 | 5 | |
| 6 | 6 | use smithay::backend::renderer::element::solid::{SolidColorBuffer, SolidColorRenderElement}; |
| 7 | -use smithay::backend::renderer::element::surface::WaylandSurfaceRenderElement; | |
| 7 | +use smithay::backend::renderer::element::surface::{ | |
| 8 | + render_elements_from_surface_tree, WaylandSurfaceRenderElement, | |
| 9 | +}; | |
| 8 | 10 | use smithay::backend::renderer::element::{render_elements, AsRenderElements}; |
| 9 | 11 | use smithay::backend::renderer::gles::GlesRenderer; |
| 10 | 12 | use smithay::backend::renderer::ImportAll; |
| @@ -12,6 +14,7 @@ use smithay::desktop::{ | ||
| 12 | 14 | layer_map_for_output, LayerSurface, PopupManager, Space, Window, WindowSurfaceType, |
| 13 | 15 | }; |
| 14 | 16 | use smithay::input::keyboard::XkbConfig; |
| 17 | +use smithay::input::pointer::{CursorImageAttributes, CursorImageStatus}; | |
| 15 | 18 | use smithay::input::{Seat, SeatState}; |
| 16 | 19 | use smithay::output::Output; |
| 17 | 20 | use smithay::reexports::calloop::generic::Generic; |
| @@ -22,8 +25,8 @@ use smithay::reexports::wayland_server::backend::{ | ||
| 22 | 25 | }; |
| 23 | 26 | use smithay::reexports::wayland_server::protocol::wl_surface::WlSurface; |
| 24 | 27 | use smithay::reexports::wayland_server::{Display, DisplayHandle}; |
| 25 | -use smithay::utils::{Logical, Point, Scale, SERIAL_COUNTER}; | |
| 26 | -use smithay::wayland::compositor::{CompositorClientState, CompositorState}; | |
| 28 | +use smithay::utils::{IsAlive, Logical, Point, Scale, SERIAL_COUNTER}; | |
| 29 | +use smithay::wayland::compositor::{with_states, CompositorClientState, CompositorState}; | |
| 27 | 30 | use smithay::wayland::data_device::DataDeviceState; |
| 28 | 31 | use smithay::wayland::output::OutputManagerState; |
| 29 | 32 | use smithay::wayland::shell::wlr_layer::{Layer, WlrLayerShellState}; |
| @@ -68,6 +71,7 @@ pub struct Niri { | ||
| 68 | 71 | pub seat: Seat<Self>, |
| 69 | 72 | |
| 70 | 73 | pub pointer_buffer: SolidColorBuffer, |
| 74 | + pub cursor_image: CursorImageStatus, | |
| 71 | 75 | } |
| 72 | 76 | |
| 73 | 77 | pub struct OutputState { |
| @@ -171,6 +175,7 @@ impl Niri { | ||
| 171 | 175 | |
| 172 | 176 | seat, |
| 173 | 177 | pointer_buffer, |
| 178 | + cursor_image: CursorImageStatus::Default, | |
| 174 | 179 | } |
| 175 | 180 | } |
| 176 | 181 | |
| @@ -319,16 +324,45 @@ impl Niri { | ||
| 319 | 324 | state.queued_redraw = Some(idle); |
| 320 | 325 | } |
| 321 | 326 | |
| 322 | - pub fn pointer_element(&mut self, output: &Output) -> OutputRenderElements<GlesRenderer> { | |
| 327 | + pub fn pointer_element( | |
| 328 | + &mut self, | |
| 329 | + renderer: &mut GlesRenderer, | |
| 330 | + output: &Output, | |
| 331 | + ) -> Vec<OutputRenderElements<GlesRenderer>> { | |
| 323 | 332 | let output_pos = self.global_space.output_geometry(output).unwrap().loc; |
| 324 | 333 | let pointer_pos = self.seat.get_pointer().unwrap().current_location() - output_pos.to_f64(); |
| 325 | 334 | |
| 326 | - OutputRenderElements::Pointer(SolidColorRenderElement::from_buffer( | |
| 327 | - &self.pointer_buffer, | |
| 328 | - pointer_pos.to_physical_precise_round(1.), | |
| 329 | - 1., | |
| 330 | - 1., | |
| 331 | - )) | |
| 335 | + if let CursorImageStatus::Surface(surface) = &mut self.cursor_image { | |
| 336 | + if !surface.alive() { | |
| 337 | + self.cursor_image = CursorImageStatus::Default; | |
| 338 | + } | |
| 339 | + } | |
| 340 | + | |
| 341 | + match &self.cursor_image { | |
| 342 | + CursorImageStatus::Hidden => vec![], | |
| 343 | + CursorImageStatus::Default => vec![OutputRenderElements::DefaultPointer( | |
| 344 | + SolidColorRenderElement::from_buffer( | |
| 345 | + &self.pointer_buffer, | |
| 346 | + pointer_pos.to_physical_precise_round(1.), | |
| 347 | + 1., | |
| 348 | + 1., | |
| 349 | + ), | |
| 350 | + )], | |
| 351 | + CursorImageStatus::Surface(surface) => { | |
| 352 | + let hotspot = with_states(surface, |states| { | |
| 353 | + states | |
| 354 | + .data_map | |
| 355 | + .get::<Mutex<CursorImageAttributes>>() | |
| 356 | + .unwrap() | |
| 357 | + .lock() | |
| 358 | + .unwrap() | |
| 359 | + .hotspot | |
| 360 | + }); | |
| 361 | + let pos = (pointer_pos - hotspot.to_f64()).to_physical_precise_round(1.); | |
| 362 | + | |
| 363 | + render_elements_from_surface_tree(renderer, surface, pos, 1., 1.) | |
| 364 | + } | |
| 365 | + } | |
| 332 | 366 | } |
| 333 | 367 | |
| 334 | 368 | fn redraw(&mut self, backend: &mut dyn Backend, output: &Output) { |
| @@ -354,7 +388,7 @@ impl Niri { | ||
| 354 | 388 | .partition(|s| matches!(s.layer(), Layer::Background | Layer::Bottom)); |
| 355 | 389 | |
| 356 | 390 | // The pointer goes on the top. |
| 357 | - let mut elements = vec![self.pointer_element(output)]; | |
| 391 | + let mut elements = self.pointer_element(renderer, output); | |
| 358 | 392 | |
| 359 | 393 | // Then the upper layer-shell elements. |
| 360 | 394 | elements.extend( |
| @@ -423,7 +457,7 @@ render_elements! { | ||
| 423 | 457 | pub OutputRenderElements<R> where R: ImportAll; |
| 424 | 458 | Monitor = MonitorRenderElement<R>, |
| 425 | 459 | Wayland = WaylandSurfaceRenderElement<R>, |
| 426 | - Pointer = SolidColorRenderElement, | |
| 460 | + DefaultPointer = SolidColorRenderElement, | |
| 427 | 461 | } |
| 428 | 462 | |
| 429 | 463 | #[derive(Default)] |