diff options
| author | MaxVerevkin <maxxverrr@gmail.com> | 2023-02-20 15:49:37 +0200 |
|---|---|---|
| committer | MaxVerevkin <maxxverrr@gmail.com> | 2023-02-20 15:49:37 +0200 |
| commit | f54d43fb7c9c0ed65f1021e31c3f03d8572460c3 (patch) | |
| tree | 43a9ae598f4531d0bf0999c56c00ee00768f091b /src | |
| parent | dfedf575b12e1af13df99711945958f78b05db15 (diff) | |
| download | i3bar-mango-f54d43fb7c9c0ed65f1021e31c3f03d8572460c3.tar.gz | |
update dependencies
Diffstat (limited to 'src')
| -rw-r--r-- | src/bar.rs | 13 | ||||
| -rw-r--r-- | src/cursor.rs | 129 | ||||
| -rw-r--r-- | src/main.rs | 1 | ||||
| -rw-r--r-- | src/state.rs | 18 |
4 files changed, 14 insertions, 147 deletions
@@ -3,6 +3,7 @@ use std::collections::BinaryHeap; use pangocairo::cairo; use wayrs_client::connection::Connection; +use wayrs_shm_alloc::BufferSpec; use crate::button_manager::ButtonManager; use crate::color::Color; @@ -106,10 +107,12 @@ impl Bar { let (buffer, canvas) = ss.shm.alloc_buffer( conn, - pix_width as i32, - pix_height as i32, - pix_width as i32 * 4, - wl_shm::Format::Argb8888, + BufferSpec { + width: pix_width, + height: pix_height, + stride: pix_width * 4, + format: wl_shm::Format::Argb8888, + }, ); let cairo_surf = unsafe { @@ -233,7 +236,7 @@ impl Bar { self.viewport .set_destination(conn, self.width as i32, self.height as i32); - self.surface.attach(conn, buffer.wl, 0, 0); + self.surface.attach(conn, buffer.into_wl_buffer(), 0, 0); self.surface.damage(conn, 0, 0, i32::MAX, i32::MAX); self.surface.commit(conn); } diff --git a/src/cursor.rs b/src/cursor.rs deleted file mode 100644 index ee7509b..0000000 --- a/src/cursor.rs +++ /dev/null @@ -1,129 +0,0 @@ -use std::collections::HashMap; -use std::io; - -use wayrs_client::connection::Connection; -use wayrs_client::protocol::*; -use wayrs_shm_alloc::ShmAlloc; - -use xcursor::parser::Image; - -use crate::state::State; - -#[derive(Debug, thiserror::Error)] -pub enum CursorError { - #[error("cursor '{0}' not found")] - CursorNotFound(String), - #[error("theme could not be parsed")] - ThemeParseError, - #[error(transparent)] - ReadError(#[from] io::Error), -} - -pub struct CursorTheme { - xcursor_size: u32, - theme: xcursor::CursorTheme, - cursors: HashMap<String, Vec<Image>>, -} - -impl CursorTheme { - pub fn new() -> Self { - let theme_name = std::env::var("XCURSOR_THEME").ok(); - let theme_name = theme_name.as_deref().unwrap_or("default"); - - let xcursor_size = std::env::var("XCURSOR_SIZE") - .ok() - .and_then(|x| x.parse().ok()) - .unwrap_or(24); - - let theme = xcursor::CursorTheme::load(theme_name); - - CursorTheme { - xcursor_size, - theme, - cursors: HashMap::new(), - } - } - - /// Use this to handle errors ahead of time. - pub fn ensure_cursor_is_loaded(&mut self, cursor: &str) -> Result<(), CursorError> { - let _ = self.get_images(cursor)?; - Ok(()) - } - - #[allow(clippy::too_many_arguments)] - pub fn set_cursor( - &mut self, - conn: &mut Connection<State>, - cursor: &str, - scale: u32, - serial: u32, - shm: &mut ShmAlloc, - surface: WlSurface, - pointer: WlPointer, - ) -> Result<(), CursorError> { - let target_size = self.xcursor_size * scale; - - let images = self.get_images(cursor)?; - - let image = match images.binary_search_by_key(&target_size, |img| img.size) { - Ok(indx) => &images[indx], - Err(indx) if indx == 0 => images.first().unwrap(), - Err(indx) if indx >= images.len() => images.last().unwrap(), - Err(indx) => { - let a = &images[indx - 1]; - let b = &images[indx]; - if target_size - a.size < b.size - target_size { - a - } else { - b - } - } - }; - - let (buffer, canvas) = shm.alloc_buffer( - conn, - image.width as i32, - image.height as i32, - (image.width * 4) as i32, - wl_shm::Format::Argb8888, - ); - - assert_eq!(image.pixels_rgba.len(), canvas.len()); - canvas.copy_from_slice(&image.pixels_rgba); - - surface.attach(conn, buffer.wl, 0, 0); - surface.damage(conn, 0, 0, i32::MAX, i32::MAX); - surface.set_buffer_scale(conn, scale as i32); - surface.commit(conn); - - pointer.set_cursor( - conn, - serial, - surface, - (image.xhot / scale) as i32, - (image.yhot / scale) as i32, - ); - - Ok(()) - } - - fn get_images(&mut self, cursor: &'_ str) -> Result<&[Image], CursorError> { - // Borrow checker does't allow `if let Some(...) = ...` here for some reason :( - if self.cursors.get(cursor).is_some() { - return Ok(self.cursors.get(cursor).unwrap()); - } - - let theme_path = self - .theme - .load_icon(cursor) - .ok_or_else(|| CursorError::CursorNotFound(cursor.to_owned()))?; - - let raw_theme = std::fs::read(theme_path)?; - - let mut images = - xcursor::parser::parse_xcursor(&raw_theme).ok_or(CursorError::ThemeParseError)?; - images.sort_unstable_by_key(|img| img.size); - - Ok(self.cursors.entry(cursor.to_owned()).or_insert(images)) - } -} diff --git a/src/main.rs b/src/main.rs index 2a4c4c2..241cd19 100644 --- a/src/main.rs +++ b/src/main.rs @@ -9,7 +9,6 @@ mod bar; mod button_manager; mod color; mod config; -mod cursor; mod i3bar_protocol; mod ord_adaptor; mod pointer_btn; diff --git a/src/state.rs b/src/state.rs index 5a18a80..b9f11df 100644 --- a/src/state.rs +++ b/src/state.rs @@ -1,5 +1,4 @@ use crate::config::Position; -use crate::cursor::CursorTheme; use crate::protocol::*; use crate::wm_info_provider::*; @@ -8,7 +7,8 @@ use std::future::pending; use wayrs_client::connection::Connection; use wayrs_client::global::{Global, GlobalExt, Globals, GlobalsExt}; use wayrs_client::proxy::Proxy; -use wayrs_shm_alloc::{ShmAlloc, ShmAllocState}; +use wayrs_cursor::CursorTheme; +use wayrs_shm_alloc::ShmAlloc; use crate::{ bar::Bar, config::Config, i3bar_protocol::Block, pointer_btn::PointerBtn, @@ -57,9 +57,9 @@ impl State { conn.set_callback_for(conn.registry(), wl_registry_cb); - let shm = globals.bind(conn, 1..=1).expect("could not bind wl_shm"); + let wl_shm = globals.bind(conn, 1..=1).expect("could not bind wl_shm"); - let mut cursor_theme = CursorTheme::new(); + let mut cursor_theme = CursorTheme::new(None, None); let cursor_theme_ok = cursor_theme .ensure_cursor_is_loaded("default") .map_err(|e| error = Err(e.into())) @@ -75,7 +75,7 @@ impl State { bars: Vec::new(), shared_state: SharedState { - shm: ShmAlloc::new(conn, shm, 1024), + shm: ShmAlloc::new(wl_shm), config, status_cmd, blocks: Vec::new(), @@ -258,12 +258,6 @@ impl State { } } -impl ShmAllocState for State { - fn shm_alloc(&mut self) -> &mut ShmAlloc { - &mut self.shared_state.shm - } -} - fn wl_registry_cb( conn: &mut Connection<State>, state: &mut State, @@ -455,10 +449,10 @@ fn wl_pointer_cb( cursor_theme .set_cursor( conn, + &mut state.shared_state.shm, "default", bar.scale, args.serial, - &mut state.shared_state.shm, seat.cursor_surface, pointer, ) |
