From 9dbd1f994ee147fa9f7bea5334719bb3b5e7e503 Mon Sep 17 00:00:00 2001 From: Aleksander Date: Thu, 17 Oct 2024 20:56:45 +0200 Subject: [PATCH] Use strongly-typed OverlayID instead of usize --- src/backend/common.rs | 32 ++++++++++++++++---------------- src/backend/input.rs | 19 ++++++++++--------- src/backend/openvr/lines.rs | 4 ++-- src/backend/openxr/lines.rs | 4 ++-- src/backend/overlay.rs | 10 +++++++--- src/state.rs | 4 ++-- 6 files changed, 39 insertions(+), 34 deletions(-) diff --git a/src/backend/common.rs b/src/backend/common.rs index 79ae84c..a083589 100644 --- a/src/backend/common.rs +++ b/src/backend/common.rs @@ -21,7 +21,7 @@ use crate::{ state::AppState, }; -use super::overlay::OverlayData; +use super::overlay::{OverlayData, OverlayID}; #[derive(Error, Debug)] pub enum BackendError { @@ -97,7 +97,7 @@ where state.show_hide = true; } overlays.insert( - state.id, + state.id.0, OverlayData:: { state, backend, @@ -108,16 +108,16 @@ where } let anchor = create_anchor(app)?; - overlays.insert(anchor.state.id, anchor); + overlays.insert(anchor.state.id.0, anchor); let mut watch = create_watch::(app)?; watch.state.want_visible = true; - overlays.insert(watch.state.id, watch); + overlays.insert(watch.state.id.0, watch); let mut keyboard = create_keyboard(app, keymap)?; keyboard.state.show_hide = true; keyboard.state.want_visible = false; - overlays.insert(keyboard.state.id, keyboard); + overlays.insert(keyboard.state.id.0, keyboard); Ok(Self { overlays, wl }) } @@ -158,7 +158,7 @@ where create_ran = true; for (meta, state, backend) in data.screens { self.overlays.insert( - state.id, + state.id.0, OverlayData:: { state, backend, @@ -175,7 +175,7 @@ where }; let meta = &app.screens[idx]; - let removed = self.overlays.remove(meta.id).unwrap(); + let removed = self.overlays.remove(meta.id.0).unwrap(); removed_overlays.push(removed); log::info!("{}: Destroyed", meta.name); app.screens.remove(idx); @@ -187,7 +187,7 @@ where continue; }; let output = wl.outputs.get(id).unwrap(); - let Some(overlay) = self.overlays.get_mut(meta.id) else { + let Some(overlay) = self.overlays.get_mut(meta.id.0) else { continue; }; let logical_pos = @@ -209,7 +209,7 @@ where continue; }; let output = wl.outputs.get(id).unwrap(); - let Some(overlay) = self.overlays.get_mut(meta.id) else { + let Some(overlay) = self.overlays.get_mut(meta.id.0) else { continue; }; @@ -270,7 +270,7 @@ where pub fn remove_by_selector(&mut self, selector: &OverlaySelector) -> Option> { match selector { - OverlaySelector::Id(id) => self.overlays.remove(id), + OverlaySelector::Id(id) => self.overlays.remove(id.0), OverlaySelector::Name(name) => { let id = self .overlays @@ -282,12 +282,12 @@ where } } - pub fn get_by_id(&mut self, id: usize) -> Option<&OverlayData> { - self.overlays.get(id) + pub fn get_by_id(&mut self, id: OverlayID) -> Option<&OverlayData> { + self.overlays.get(id.0) } - pub fn mut_by_id(&mut self, id: usize) -> Option<&mut OverlayData> { - self.overlays.get_mut(id) + pub fn mut_by_id(&mut self, id: OverlayID) -> Option<&mut OverlayData> { + self.overlays.get_mut(id.0) } pub fn get_by_name<'a>(&'a mut self, name: &str) -> Option<&'a OverlayData> { @@ -307,7 +307,7 @@ where } pub fn add(&mut self, overlay: OverlayData) { - self.overlays.insert(overlay.state.id, overlay); + self.overlays.insert(overlay.state.id.0, overlay); } pub fn show_hide(&mut self, app: &mut AppState) { @@ -344,7 +344,7 @@ where #[derive(Clone, Deserialize, Debug)] #[serde(untagged)] pub enum OverlaySelector { - Id(usize), + Id(OverlayID), Name(Arc), } diff --git a/src/backend/input.rs b/src/backend/input.rs index 98d7b5f..7576e78 100644 --- a/src/backend/input.rs +++ b/src/backend/input.rs @@ -10,6 +10,7 @@ use crate::config::{AStrMapExt, GeneralConfig}; use crate::overlays::anchor::ANCHOR_NAME; use crate::state::AppState; +use super::overlay::OverlayID; use super::task::{TaskContainer, TaskType}; use super::{common::OverlayContainer, overlay::OverlayData}; @@ -140,8 +141,8 @@ impl InputState { pub struct InteractionState { pub mode: PointerMode, pub grabbed: Option, - pub clicked_id: Option, - pub hovered_id: Option, + pub clicked_id: Option, + pub hovered_id: Option, pub release_actions: VecDeque>, pub next_push: Instant, pub haptics: Option, @@ -204,7 +205,7 @@ pub struct PointerState { #[derive(Debug, Clone, Copy, Default)] pub struct PointerHit { pub pointer: usize, - pub overlay: usize, + pub overlay: OverlayID, pub mode: PointerMode, pub primary: bool, pub uv: Vec2, @@ -237,7 +238,7 @@ impl InteractionHandler for DummyInteractionHandler { #[derive(Debug, Clone, Copy, Default)] struct RayHit { - overlay: usize, + overlay: OverlayID, global_pos: Vec3A, local_pos: Vec2, dist: f32, @@ -246,7 +247,7 @@ struct RayHit { #[derive(Debug, Clone, Copy, Default)] pub struct GrabData { pub offset: Vec3A, - pub grabbed_id: usize, + pub grabbed_id: OverlayID, pub old_curvature: Option, pub grab_all: bool, } @@ -299,7 +300,7 @@ where &mut app.session.config, ); } else { - log::warn!("Grabbed overlay {} does not exist", grab_data.grabbed_id); + log::warn!("Grabbed overlay {} does not exist", grab_data.grabbed_id.0); pointer.interaction.grabbed = None; } return (0.1, None); @@ -341,7 +342,7 @@ where } } let Some(hovered) = overlays.mut_by_id(hit.overlay) else { - log::warn!("Hit overlay {} does not exist", hit.overlay); + log::warn!("Hit overlay {} does not exist", hit.overlay.0); return (0.0, None); // no hit }; @@ -535,7 +536,7 @@ impl Pointer { overlay.state.realign(hmd); overlay.state.dirty = true; } else { - log::error!("Grabbed overlay {} does not exist", overlay.state.id); + log::error!("Grabbed overlay {} does not exist", overlay.state.id.0); self.interaction.grabbed = None; } } else { @@ -572,7 +573,7 @@ impl Pointer { fn ray_test( &self, - overlay: usize, + overlay: OverlayID, transform: &Affine3A, curvature: &Option, ) -> Option { diff --git a/src/backend/openvr/lines.rs b/src/backend/openvr/lines.rs index ca1b950..c7b1b97 100644 --- a/src/backend/openvr/lines.rs +++ b/src/backend/openvr/lines.rs @@ -17,7 +17,7 @@ use crate::state::AppState; use super::overlay::OpenVrOverlayData; -static AUTO_INCREMENT: AtomicUsize = AtomicUsize::new(1); +static LINE_AUTO_INCREMENT: AtomicUsize = AtomicUsize::new(1); pub(super) struct LinePool { lines: IdMap>, @@ -59,7 +59,7 @@ impl LinePool { } pub fn allocate(&mut self) -> usize { - let id = AUTO_INCREMENT.fetch_add(1, Ordering::Relaxed); + let id = LINE_AUTO_INCREMENT.fetch_add(1, Ordering::Relaxed); let mut data = OverlayData:: { state: OverlayState { diff --git a/src/backend/openxr/lines.rs b/src/backend/openxr/lines.rs index ce982a6..e2f703e 100644 --- a/src/backend/openxr/lines.rs +++ b/src/backend/openxr/lines.rs @@ -21,7 +21,7 @@ use super::{ CompositionLayer, XrState, }; -static AUTO_INCREMENT: AtomicUsize = AtomicUsize::new(1); +static LINE_AUTO_INCREMENT: AtomicUsize = AtomicUsize::new(1); pub(super) const LINE_WIDTH: f32 = 0.002; pub(super) struct LinePool { @@ -66,7 +66,7 @@ impl LinePool { xr: &XrState, graphics: Arc, ) -> anyhow::Result { - let id = AUTO_INCREMENT.fetch_add(1, Ordering::Relaxed); + let id = LINE_AUTO_INCREMENT.fetch_add(1, Ordering::Relaxed); let srd = create_swapchain_render_data(xr, graphics, [1, 1, 1], SwapchainOpts::new().srgb())?; diff --git a/src/backend/overlay.rs b/src/backend/overlay.rs index 50a2a31..94f9a67 100644 --- a/src/backend/overlay.rs +++ b/src/backend/overlay.rs @@ -8,21 +8,25 @@ use std::{ use anyhow::Ok; use glam::{Affine2, Affine3A, Mat3A, Quat, Vec2, Vec3, Vec3A}; +use serde::Deserialize; use vulkano::image::view::ImageView; use crate::{config::AStrMapExt, state::AppState}; use super::input::{DummyInteractionHandler, Haptics, InteractionHandler, PointerHit}; -static AUTO_INCREMENT: AtomicUsize = AtomicUsize::new(0); +static OVERLAY_AUTO_INCREMENT: AtomicUsize = AtomicUsize::new(0); pub trait OverlayBackend: OverlayRenderer + InteractionHandler { fn set_renderer(&mut self, renderer: Box); fn set_interaction(&mut self, interaction: Box); } +#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize, Default)] +pub struct OverlayID(pub usize); + pub struct OverlayState { - pub id: usize, + pub id: OverlayID, pub name: Arc, pub want_visible: bool, pub show_hide: bool, @@ -48,7 +52,7 @@ pub struct OverlayState { impl Default for OverlayState { fn default() -> Self { OverlayState { - id: AUTO_INCREMENT.fetch_add(1, Ordering::Relaxed), + id: OverlayID(OVERLAY_AUTO_INCREMENT.fetch_add(1, Ordering::Relaxed)), name: Arc::from(""), want_visible: false, show_hide: false, diff --git a/src/state.rs b/src/state.rs index 124fb12..92da9af 100644 --- a/src/state.rs +++ b/src/state.rs @@ -9,7 +9,7 @@ use smallvec::{smallvec, SmallVec}; use vulkano::image::view::ImageView; use crate::{ - backend::{input::InputState, task::TaskContainer}, + backend::{input::InputState, overlay::OverlayID, task::TaskContainer}, config::{AStrMap, GeneralConfig}, config_io, graphics::WlxGraphics, @@ -160,7 +160,7 @@ impl AudioOutput { pub struct ScreenMeta { pub name: Arc, - pub id: usize, + pub id: OverlayID, pub native_handle: u32, }