From 92a7a55e18752147f71fa4155ee74b948d78a3de Mon Sep 17 00:00:00 2001 From: MaxVerevkin Date: Tue, 31 Jan 2023 11:47:12 +0200 Subject: Make cursor.rs more generic Probably will move it into a separate crate (wayrs_cursor) at some point. --- src/state.rs | 52 ++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 36 insertions(+), 16 deletions(-) (limited to 'src/state.rs') diff --git a/src/state.rs b/src/state.rs index dfc2a2a..3c1ab89 100644 --- a/src/state.rs +++ b/src/state.rs @@ -1,4 +1,4 @@ -use crate::cursor::Cursor; +use crate::cursor::CursorTheme; use crate::protocol::*; use std::future::pending; @@ -24,14 +24,16 @@ pub struct State { pub bars: Vec, pub shared_state: SharedState, + + cursor_theme: Option, } pub struct Seat { seat: WlSeat, reg_name: u32, pointer: Option, - cursor: Cursor, - cur_surface: Option, + cursor_surface: WlSurface, + current_surface: Option, x: f64, y: f64, pending_button: Option, @@ -53,8 +55,15 @@ impl State { .and_then(|cmd| StatusCmd::new(cmd).map_err(|e| error = Err(e)).ok()); conn.set_callback_for(conn.registry(), wl_registry_cb); + let shm = globals.bind(conn, 1..=1).expect("could not bind wl_shm"); + let mut cursor_theme = CursorTheme::new(); + let cursor_theme_ok = cursor_theme + .ensure_cursor_is_loaded("default") + .map_err(|e| error = Err(e.into())) + .is_ok(); + let mut this = Self { wl_compositor: globals .bind(conn, 4..=5) @@ -76,6 +85,8 @@ impl State { blocks: Vec::new(), blocks_cache: Vec::new(), }, + + cursor_theme: cursor_theme_ok.then_some(cursor_theme), }; globals @@ -188,13 +199,12 @@ impl State { fn bind_seat(&mut self, conn: &mut Connection, global: &Global) { let seat: WlSeat = global.bind_with_cb(conn, 5..=8, wl_seat_cb).unwrap(); - let cursor = Cursor::new(conn, self.wl_compositor).unwrap(); self.seats.push(Seat { seat, reg_name: global.name, pointer: None, - cursor, - cur_surface: None, + cursor_surface: self.wl_compositor.create_surface(conn), + current_surface: None, x: 0.0, y: 0.0, pending_button: None, @@ -217,14 +227,18 @@ impl State { fn drop_seat(&mut self, conn: &mut Connection, seat_index: usize) { let seat = self.seats.swap_remove(seat_index); + if let Some(pointer) = seat.pointer { if pointer.version() >= 3 { pointer.release(conn); } } + if seat.seat.version() >= 5 { seat.seat.release(conn); } + + seat.cursor_surface.destroy(conn); } } @@ -360,7 +374,7 @@ fn wl_pointer_cb( Event::Frame => { let btn = seat.pending_button.take(); let scroll = seat.scroll_frame.finalize(); - if let Some(surface) = seat.cur_surface { + if let Some(surface) = seat.current_surface { let bar = state .bars .iter_mut() @@ -418,18 +432,24 @@ fn wl_pointer_cb( .iter() .find(|bar| bar.surface.id() == args.surface) .unwrap(); - seat.cur_surface = Some(bar.surface); + seat.current_surface = Some(bar.surface); seat.x = args.surface_x.as_f64(); seat.y = args.surface_y.as_f64(); - seat.cursor.set( - conn, - args.serial, - pointer, - bar.scale as u32, - &mut state.shared_state.shm, - ); + if let Some(cursor_theme) = &mut state.cursor_theme { + cursor_theme + .set_cursor( + conn, + "default", + bar.scale as u32, + args.serial, + &mut state.shared_state.shm, + seat.cursor_surface, + pointer, + ) + .unwrap(); + } } - Event::Leave(_) => seat.cur_surface = None, + Event::Leave(_) => seat.current_surface = None, Event::Motion(args) => { seat.x = args.surface_x.as_f64(); seat.y = args.surface_y.as_f64(); -- cgit v1.3.1