From 68676cc1d1eab81c0ff36490c55896f308665541 Mon Sep 17 00:00:00 2001 From: Daniel Prilik Date: Fri, 6 Nov 2020 00:48:38 -0500 Subject: [PATCH] put auto haptic params in mode meta --- src/controller/controls/media.rs | 7 ++----- src/controller/controls/null.rs | 7 ++----- src/controller/controls/paddle.rs | 5 +++-- src/controller/controls/scroll.rs | 7 ++----- src/controller/controls/scroll_mt.rs | 5 +++-- src/controller/controls/volume.rs | 7 ++----- src/controller/controls/zoom.rs | 7 ++----- src/controller/mod.rs | 22 +++++++++++++++++++--- src/dial_device/haptics.rs | 10 ++++------ 9 files changed, 39 insertions(+), 38 deletions(-) diff --git a/src/controller/controls/media.rs b/src/controller/controls/media.rs index 767fe45..ffadd14 100644 --- a/src/controller/controls/media.rs +++ b/src/controller/controls/media.rs @@ -18,14 +18,11 @@ impl ControlMode for Media { ControlModeMeta { name: "Media", icon: "applications-multimedia", + haptics: true, + steps: 36, } } - fn on_start(&mut self, haptics: &DialHaptics) -> Result<()> { - haptics.set_mode(true, Some(36))?; - Ok(()) - } - fn on_btn_press(&mut self, _: &DialHaptics) -> Result<()> { Ok(()) } diff --git a/src/controller/controls/null.rs b/src/controller/controls/null.rs index 9f345d5..0deaad1 100644 --- a/src/controller/controls/null.rs +++ b/src/controller/controls/null.rs @@ -7,14 +7,11 @@ impl ControlMode for () { ControlModeMeta { name: "null", icon: "", + haptics: false, + steps: 3600, } } - fn on_start(&mut self, haptics: &DialHaptics) -> Result<()> { - haptics.set_mode(false, Some(0))?; - Ok(()) - } - fn on_btn_press(&mut self, _haptics: &DialHaptics) -> Result<()> { Ok(()) } diff --git a/src/controller/controls/paddle.rs b/src/controller/controls/paddle.rs index c2d7422..e878a24 100644 --- a/src/controller/controls/paddle.rs +++ b/src/controller/controls/paddle.rs @@ -148,11 +148,12 @@ impl ControlMode for Paddle { ControlModeMeta { name: "Paddle", icon: "input-gaming", + haptics: false, + steps: 3600, } } - fn on_start(&mut self, haptics: &DialHaptics) -> Result<()> { - haptics.set_mode(false, Some(3600))?; + fn on_start(&mut self, _haptics: &DialHaptics) -> Result<()> { let _ = self.msg.send(Msg::Enabled(true)); Ok(()) } diff --git a/src/controller/controls/scroll.rs b/src/controller/controls/scroll.rs index c313bc3..e2acb01 100644 --- a/src/controller/controls/scroll.rs +++ b/src/controller/controls/scroll.rs @@ -16,14 +16,11 @@ impl ControlMode for Scroll { ControlModeMeta { name: "Scroll", icon: "input-mouse", + haptics: false, + steps: 90, } } - fn on_start(&mut self, haptics: &DialHaptics) -> Result<()> { - haptics.set_mode(false, Some(90))?; - Ok(()) - } - fn on_btn_press(&mut self, _: &DialHaptics) -> Result<()> { Ok(()) } diff --git a/src/controller/controls/scroll_mt.rs b/src/controller/controls/scroll_mt.rs index cbebac9..b785761 100644 --- a/src/controller/controls/scroll_mt.rs +++ b/src/controller/controls/scroll_mt.rs @@ -18,11 +18,12 @@ impl ControlMode for ScrollMT { ControlModeMeta { name: "Scroll", icon: "input-mouse", + haptics: false, + steps: 3600, } } - fn on_start(&mut self, haptics: &DialHaptics) -> Result<()> { - haptics.set_mode(false, Some(3600))?; + fn on_start(&mut self, _haptics: &DialHaptics) -> Result<()> { self.acc_delta = 0; fake_input::scroll_mt_start().map_err(Error::Evdev)?; diff --git a/src/controller/controls/volume.rs b/src/controller/controls/volume.rs index ef28ffe..a451886 100644 --- a/src/controller/controls/volume.rs +++ b/src/controller/controls/volume.rs @@ -18,14 +18,11 @@ impl ControlMode for Volume { ControlModeMeta { name: "Volume", icon: "audio-volume-high", + haptics: true, + steps: 36 * 2, } } - fn on_start(&mut self, haptics: &DialHaptics) -> Result<()> { - haptics.set_mode(true, Some(36 * 2))?; - Ok(()) - } - fn on_btn_press(&mut self, _: &DialHaptics) -> Result<()> { // TODO: support double-click to play/pause Ok(()) diff --git a/src/controller/controls/zoom.rs b/src/controller/controls/zoom.rs index f1379ba..9536f52 100644 --- a/src/controller/controls/zoom.rs +++ b/src/controller/controls/zoom.rs @@ -18,14 +18,11 @@ impl ControlMode for Zoom { ControlModeMeta { name: "Zoom", icon: "zoom-in", + haptics: true, + steps: 36, } } - fn on_start(&mut self, haptics: &DialHaptics) -> Result<()> { - haptics.set_mode(true, Some(36))?; - Ok(()) - } - fn on_btn_press(&mut self, _: &DialHaptics) -> Result<()> { Ok(()) } diff --git a/src/controller/mod.rs b/src/controller/mod.rs index a28eab8..ab22319 100644 --- a/src/controller/mod.rs +++ b/src/controller/mod.rs @@ -6,14 +6,25 @@ use crate::error::{Error, Result}; pub mod controls; pub struct ControlModeMeta { + /// Mode Name (as displayed in the Meta selection menu) name: &'static str, + /// Mode Icon (as displayed in the Meta selection menu) + /// + /// This can be a file:// url, or a standard FreeDesktop icon name. icon: &'static str, + /// Enable automatic haptic feedback when rotating the dial. + haptics: bool, + /// How many sections the dial should be divided into (from 0 to 3600). + steps: u16, } pub trait ControlMode { fn meta(&self) -> ControlModeMeta; - fn on_start(&mut self, haptics: &DialHaptics) -> Result<()>; + fn on_start(&mut self, _haptics: &DialHaptics) -> Result<()> { + Ok(()) + } + fn on_end(&mut self, _haptics: &DialHaptics) -> Result<()> { Ok(()) } @@ -66,7 +77,10 @@ impl DialController { if let Some(new_mode) = self.new_mode.lock().unwrap().take() { self.active_mode = ActiveMode::Normal(new_mode); - self.modes[new_mode].on_start(haptics)?; + let mode = &mut self.modes[new_mode]; + + haptics.set_mode(mode.meta().haptics, mode.meta().steps)?; + mode.on_start(haptics)?; } let mode = match self.active_mode { @@ -79,6 +93,7 @@ impl DialController { DialEventKind::Connect => { eprintln!("Dial Connected"); + haptics.set_mode(mode.meta().haptics, mode.meta().steps)?; mode.on_start(haptics)? } DialEventKind::Disconnect => { @@ -95,6 +110,7 @@ impl DialController { if !matches!(self.active_mode, ActiveMode::Meta) { mode.on_end(haptics)?; self.active_mode = ActiveMode::Meta; + // meta_mode sets haptic feedback manually self.meta_mode.on_start(haptics)?; } } @@ -157,11 +173,11 @@ impl ControlMode for MetaMode { .map_err(Error::Notif)?, ); + haptics.set_mode(true, 36)?; haptics.buzz(1)?; self.first_release = true; - haptics.set_mode(true, Some(36))?; Ok(()) } diff --git a/src/dial_device/haptics.rs b/src/dial_device/haptics.rs index 079cbc0..3a4b012 100644 --- a/src/dial_device/haptics.rs +++ b/src/dial_device/haptics.rs @@ -15,9 +15,8 @@ impl DialHaptics { } /// `steps` should be a value between 0 and 3600, which corresponds to the - /// number of subdivisions the dial should use. If left unspecified, this - /// defaults to 36 (an arbitrary choice that "feels good" most of the time) - pub fn set_mode(&self, haptics: bool, steps: Option) -> Result<()> { + /// number of subdivisions the dial should use. + pub fn set_mode(&self, haptics: bool, steps: u16) -> Result<()> { let _ = (self.msg).send(DialHapticsWorkerMsg::SetMode { haptics, steps }); Ok(()) } @@ -32,7 +31,7 @@ impl DialHaptics { pub(super) enum DialHapticsWorkerMsg { DialConnected, DialDisconnected, - SetMode { haptics: bool, steps: Option }, + SetMode { haptics: bool, steps: u16 }, Manual { repeat: u8 }, } @@ -87,8 +86,7 @@ impl DialHidWrapper { /// `steps` should be a value between 0 and 3600, which corresponds to the /// number of subdivisions the dial should use. If left unspecified, this /// defaults to 36 (an arbitrary choice that "feels good" most of the time) - fn set_mode(&self, haptics: bool, steps: Option) -> Result<()> { - let steps = steps.unwrap_or(36); + fn set_mode(&self, haptics: bool, steps: u16) -> Result<()> { assert!(steps <= 3600); let steps_lo = steps & 0xff;