From d742106eb02fe5df69c738a29dfd20f73390089f Mon Sep 17 00:00:00 2001 From: Pierre Fenoll Date: Sat, 27 Jul 2024 14:16:06 +0200 Subject: [PATCH 1/4] WIP: bumping atomic crate is stuck on non-fieldless enums Signed-off-by: Pierre Fenoll --- Cargo.toml | 3 ++- examples/demo.rs | 5 ++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 68f2153..dfff62c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,10 +12,11 @@ edition = "2021" [dependencies] log = "0.4.14" once_cell = "1.9.0" -atomic = "0.5.1" cgmath = "0.18.0" libc = "0.2.69" +atomic = "0.6.0" +bytemuck = { version = "1.13.1", features = ["derive"] } # framebuffer memmap2 = { version = "0.5.2", optional = true } diff --git a/examples/demo.rs b/examples/demo.rs index f9c3b82..7c83558 100644 --- a/examples/demo.rs +++ b/examples/demo.rs @@ -28,7 +28,10 @@ use std::sync::Mutex; use std::thread::sleep; use std::time::Duration; -#[derive(Copy, Clone, PartialEq)] +use bytemuck::NoUninit; + +#[derive(Copy, Clone, PartialEq, NoUninit)] +#[repr(i64)] enum DrawMode { Draw(u32), Erase(u32), From ff7ac4b067a595d2e1873bdb55417af011b6865c Mon Sep 17 00:00:00 2001 From: Pierre Fenoll Date: Sat, 27 Jul 2024 14:11:00 +0200 Subject: [PATCH 2/4] deps(atomic): use std AtomicBool instead of crate's Signed-off-by: Pierre Fenoll --- src/input/wacom.rs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/input/wacom.rs b/src/input/wacom.rs index 36bed02..ab1003c 100644 --- a/src/input/wacom.rs +++ b/src/input/wacom.rs @@ -3,11 +3,10 @@ use crate::device::rotate::CoordinatePart; use crate::device::CURRENT_DEVICE; use crate::input::scan::SCANNED; use crate::input::{InputDeviceState, InputEvent, WacomEvent, WacomPen}; -use atomic::Atomic; use evdev::InputEvent as EvInputEvent; use log::debug; use once_cell::sync::Lazy; -use std::sync::atomic::{AtomicU16, Ordering}; +use std::sync::atomic::{AtomicBool, AtomicU16, Ordering}; use crate::cgmath; use crate::dimensions::{DISPLAYHEIGHT, DISPLAYWIDTH, WACOMHEIGHT, WACOMWIDTH}; @@ -22,7 +21,7 @@ pub struct WacomState { last_ytilt: AtomicU16, last_dist: AtomicU16, last_pressure: AtomicU16, - last_touch_state: Atomic, + last_touch_state: AtomicBool, } impl ::std::default::Default for WacomState { @@ -34,7 +33,7 @@ impl ::std::default::Default for WacomState { last_ytilt: AtomicU16::new(0), last_dist: AtomicU16::new(0), last_pressure: AtomicU16::new(0), - last_touch_state: Atomic::new(false), + last_touch_state: AtomicBool::new(false), } } } From 4f09f1ba113038cadb2fe7bf7faec45edfdc7fb3 Mon Sep 17 00:00:00 2001 From: Pierre Fenoll Date: Sat, 27 Jul 2024 17:05:30 +0200 Subject: [PATCH 3/4] use AtomicU8 instead of Atomic for TouchMode Signed-off-by: Pierre Fenoll --- examples/demo.rs | 42 ++++++++++++++++++++++++++++++++---------- 1 file changed, 32 insertions(+), 10 deletions(-) diff --git a/examples/demo.rs b/examples/demo.rs index 7c83558..5a8e84a 100644 --- a/examples/demo.rs +++ b/examples/demo.rs @@ -15,7 +15,6 @@ use libremarkable::{end_bench, start_bench}; #[cfg(feature = "enable-runtime-benchmarking")] use libremarkable::stopwatch; -use atomic::Atomic; use chrono::{DateTime, Local}; use log::info; use once_cell::sync::Lazy; @@ -23,14 +22,12 @@ use once_cell::sync::Lazy; use std::collections::VecDeque; use std::fmt; use std::process::Command; -use std::sync::atomic::{AtomicBool, Ordering}; +use std::sync::atomic::{AtomicBool, AtomicU8, Ordering}; use std::sync::Mutex; use std::thread::sleep; use std::time::Duration; -use bytemuck::NoUninit; - -#[derive(Copy, Clone, PartialEq, NoUninit)] +#[derive(Copy, Clone, PartialEq)] #[repr(i64)] enum DrawMode { Draw(u32), @@ -91,6 +88,30 @@ impl fmt::Display for TouchMode { } } +impl Into for TouchMode { + fn into(self) -> u8 { + match self { + Self::OnlyUI => 1, + Self::Bezier => 2, + Self::Circles => 3, + Self::Diamonds => 4, + Self::FillDiamonds => 5, + } + } +} +impl From for TouchMode { + fn from(mode: u8) -> Self { + match mode { + 1 => Self::OnlyUI, + 2 => Self::Bezier, + 3 => Self::Circles, + 4 => Self::Diamonds, + 5 => Self::FillDiamonds, + _ => panic!("Unmapped mode value: {mode}"), + } + } +} + // This region will have the following size at rest: // raw: 5896 kB // zstd: 10 kB @@ -103,8 +124,9 @@ const CANVAS_REGION: mxcfb_rect = mxcfb_rect { type PointAndPressure = (cgmath::Point2, i32); -static G_TOUCH_MODE: Lazy> = Lazy::new(|| Atomic::new(TouchMode::OnlyUI)); -static G_DRAW_MODE: Lazy> = Lazy::new(|| Atomic::new(DrawMode::Draw(2))); +static G_TOUCH_MODE: Lazy = Lazy::new(|| AtomicU8::new(TouchMode::OnlyUI.into())); +static G_DRAW_MODE: Lazy> = + Lazy::new(|| atomic::Atomic::new(DrawMode::Draw(2))); static UNPRESS_OBSERVED: Lazy = Lazy::new(|| AtomicBool::new(false)); static WACOM_IN_RANGE: Lazy = Lazy::new(|| AtomicBool::new(false)); static WACOM_RUBBER_SIDE: Lazy = Lazy::new(|| AtomicBool::new(false)); @@ -327,8 +349,8 @@ fn on_toggle_eraser(app: &mut appctx::ApplicationContext<'_>, _: UIElementHandle } fn on_change_touchdraw_mode(app: &mut appctx::ApplicationContext<'_>, _: UIElementHandle) { - let new_val = G_TOUCH_MODE.load(Ordering::Relaxed).toggle(); - G_TOUCH_MODE.store(new_val, Ordering::Relaxed); + let new_val = TouchMode::from(G_TOUCH_MODE.load(Ordering::Relaxed)).toggle(); + G_TOUCH_MODE.store(new_val.into(), Ordering::Relaxed); let indicator = app.get_element_by_name("touchModeIndicator"); if let UIElement::Text { ref mut text, .. } = indicator.unwrap().write().inner { @@ -571,7 +593,7 @@ fn on_touch_handler(app: &mut appctx::ApplicationContext<'_>, input: input::Mult if !CANVAS_REGION.contains_point(&finger.pos.cast().unwrap()) { return; } - let rect = match G_TOUCH_MODE.load(Ordering::Relaxed) { + let rect = match TouchMode::from(G_TOUCH_MODE.load(Ordering::Relaxed)) { TouchMode::Bezier => { let position_float = finger.pos.cast().unwrap(); let points = [ From 6729ca3ca36e795e740eecffb24152fdea20bd16 Mon Sep 17 00:00:00 2001 From: Pierre Fenoll Date: Sat, 27 Jul 2024 17:41:54 +0200 Subject: [PATCH 4/4] Same idea again + drop atomic crate Signed-off-by: Pierre Fenoll --- Cargo.toml | 3 --- examples/demo.rs | 57 ++++++++++++++++++++++++++++++++---------------- 2 files changed, 38 insertions(+), 22 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index dfff62c..550f8c3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,9 +15,6 @@ once_cell = "1.9.0" cgmath = "0.18.0" libc = "0.2.69" -atomic = "0.6.0" -bytemuck = { version = "1.13.1", features = ["derive"] } - # framebuffer memmap2 = { version = "0.5.2", optional = true } ioctl-gen = { version = "0.1.1", optional = true } diff --git a/examples/demo.rs b/examples/demo.rs index 5a8e84a..fb73d2e 100644 --- a/examples/demo.rs +++ b/examples/demo.rs @@ -22,13 +22,12 @@ use once_cell::sync::Lazy; use std::collections::VecDeque; use std::fmt; use std::process::Command; -use std::sync::atomic::{AtomicBool, AtomicU8, Ordering}; +use std::sync::atomic::{AtomicBool, AtomicI32, AtomicU8, Ordering}; use std::sync::Mutex; use std::thread::sleep; use std::time::Duration; #[derive(Copy, Clone, PartialEq)] -#[repr(i64)] enum DrawMode { Draw(u32), Erase(u32), @@ -55,6 +54,24 @@ impl DrawMode { } } +impl From for i32 { + fn from(mode: DrawMode) -> Self { + match mode { + DrawMode::Draw(s) => s as i32, + DrawMode::Erase(s) => -(s as i32), + } + } +} +impl From for DrawMode { + fn from(mode: i32) -> Self { + if mode >= 0 { + Self::Draw(mode as u32) + } else { + Self::Erase((-mode) as u32) + } + } +} + #[derive(Copy, Clone, PartialEq, Debug)] enum TouchMode { OnlyUI, @@ -88,14 +105,14 @@ impl fmt::Display for TouchMode { } } -impl Into for TouchMode { - fn into(self) -> u8 { - match self { - Self::OnlyUI => 1, - Self::Bezier => 2, - Self::Circles => 3, - Self::Diamonds => 4, - Self::FillDiamonds => 5, +impl From for u8 { + fn from(mode: TouchMode) -> Self { + match mode { + TouchMode::OnlyUI => 1, + TouchMode::Bezier => 2, + TouchMode::Circles => 3, + TouchMode::Diamonds => 4, + TouchMode::FillDiamonds => 5, } } } @@ -125,8 +142,7 @@ const CANVAS_REGION: mxcfb_rect = mxcfb_rect { type PointAndPressure = (cgmath::Point2, i32); static G_TOUCH_MODE: Lazy = Lazy::new(|| AtomicU8::new(TouchMode::OnlyUI.into())); -static G_DRAW_MODE: Lazy> = - Lazy::new(|| atomic::Atomic::new(DrawMode::Draw(2))); +static G_DRAW_MODE: Lazy = Lazy::new(|| AtomicI32::new(DrawMode::Draw(2).into())); static UNPRESS_OBSERVED: Lazy = Lazy::new(|| AtomicBool::new(false)); static WACOM_IN_RANGE: Lazy = Lazy::new(|| AtomicBool::new(false)); static WACOM_RUBBER_SIDE: Lazy = Lazy::new(|| AtomicBool::new(false)); @@ -335,11 +351,11 @@ fn on_touch_rustlogo(app: &mut appctx::ApplicationContext<'_>, _element: UIEleme } fn on_toggle_eraser(app: &mut appctx::ApplicationContext<'_>, _: UIElementHandle) { - let (new_mode, name) = match G_DRAW_MODE.load(Ordering::Relaxed) { + let (new_mode, name) = match G_DRAW_MODE.load(Ordering::Relaxed).into() { DrawMode::Erase(s) => (DrawMode::Draw(s), "Black".to_owned()), DrawMode::Draw(s) => (DrawMode::Erase(s), "White".to_owned()), }; - G_DRAW_MODE.store(new_mode, Ordering::Relaxed); + G_DRAW_MODE.store(new_mode.into(), Ordering::Relaxed); let indicator = app.get_element_by_name("colorIndicator"); if let UIElement::Text { ref mut text, .. } = indicator.unwrap().write().inner { @@ -424,7 +440,7 @@ fn draw_color_test_rgb(app: &mut appctx::ApplicationContext<'_>, _element: UIEle } fn change_brush_width(app: &mut appctx::ApplicationContext<'_>, delta: i32) { - let current = G_DRAW_MODE.load(Ordering::Relaxed); + let current = DrawMode::from(G_DRAW_MODE.load(Ordering::Relaxed)); let current_size = current.get_size() as i32; let proposed_size = current_size + delta; let new_size = proposed_size.clamp(1, 99); @@ -432,7 +448,7 @@ fn change_brush_width(app: &mut appctx::ApplicationContext<'_>, delta: i32) { return; } - G_DRAW_MODE.store(current.set_size(new_size as u32), Ordering::Relaxed); + G_DRAW_MODE.store(current.set_size(new_size as u32).into(), Ordering::Relaxed); let element = app.get_element_by_name("displaySize").unwrap(); if let UIElement::Text { ref mut text, .. } = element.write().inner { @@ -496,7 +512,7 @@ fn on_wacom_input(app: &mut appctx::ApplicationContext<'_>, input: input::WacomE return; } - let (mut col, mut mult) = match G_DRAW_MODE.load(Ordering::Relaxed) { + let (mut col, mut mult) = match G_DRAW_MODE.load(Ordering::Relaxed).into() { DrawMode::Draw(s) => (color::BLACK, s), DrawMode::Erase(s) => (color::WHITE, s * 3), }; @@ -902,7 +918,7 @@ fn main() { onclick: None, inner: UIElement::Text { foreground: color::BLACK, - text: G_DRAW_MODE.load(Ordering::Relaxed).color_as_string(), + text: DrawMode::from(G_DRAW_MODE.load(Ordering::Relaxed)).color_as_string(), scale: 40.0, border_px: 0, }, @@ -952,7 +968,10 @@ fn main() { refresh: UIConstraintRefresh::Refresh, inner: UIElement::Text { foreground: color::BLACK, - text: format!("size: {0}", G_DRAW_MODE.load(Ordering::Relaxed).get_size()), + text: format!( + "size: {0}", + DrawMode::from(G_DRAW_MODE.load(Ordering::Relaxed)).get_size() + ), scale: 45.0, border_px: 0, },