From b3bf1ae4212441e126d5f32b729526d9e94c6cbf Mon Sep 17 00:00:00 2001 From: Michael Richters Date: Tue, 15 Mar 2022 14:18:55 -0500 Subject: [PATCH] MouseKeys "janitorial" work (#1130) * Move MouseWrapper class into the `plugin::mousekeys` namespace This helper class is meant to be internal to MouseKeys, so it would be best to sequester it from the shared `kaleidoscope::plugin` namespace. Signed-off-by: Michael Richters * Remove unnecessary explicit MouseWrapper constructor Signed-off-by: Michael Richters * Remove trailing underscore from MouseWrapper class name Signed-off-by: Michael Richters * Rename Mousewrapper methods to comply with coding style guide Signed-off-by: Michael Richters * Make MouseWrapper warping utility methods private MouseKeys doesn't call these functions directly, and making them private helps make the API for MouseWrapper clearer. Signed-off-by: Michael Richters * Make MouseWrapper public variables comply with code style guide Variables should be in `snake_case`. Since MouseKeys is the only client of `MouseWrapper`, I don't feel it's necessary to go through a deprecation process for the sake of this rename. Signed-off-by: Michael Richters * Remove unnecessary explicit MouseKeys constructor Signed-off-by: Michael Richters * Remove trailing underscore from MouseKeys class name Because the `MouseKeys_` class is now contained in the `kaleidoscope::plugin` namespace, but the `MouseKeys` object is in the global namespace, we can safely drop the trailing underscore from the class name, like most other plugins. Signed-off-by: Michael Richters * Add some namespace comments Signed-off-by: Michael Richters * Make `MouseWrapper::subpixels_per_pixel` constexpr Signed-off-by: Michael Richters * Change MouseKeys variables from `static` to member variables Signed-off-by: Michael Richters * Change name of MouseWrapper object Instead of `kaleidoscope::plugin::MouseWrapper`, it is now named like a variable: `kaleidoscope::plugin::mousekeys::wrapper`. Signed-off-by: Michael Richters * Downcase directory name to match namespace Signed-off-by: Michael Richters * Adjust header includes to match new dirname Signed-off-by: Michael Richters --- .../src/kaleidoscope/plugin/MouseKeys.cpp | 76 +++++++++---------- .../src/kaleidoscope/plugin/MouseKeys.h | 28 ++++--- .../{MouseKeys => mousekeys}/MouseKeyDefs.h | 0 .../{MouseKeys => mousekeys}/MouseWarpModes.h | 0 .../{MouseKeys => mousekeys}/MouseWrapper.cpp | 66 ++++++++-------- .../{MouseKeys => mousekeys}/MouseWrapper.h | 30 ++++---- 6 files changed, 98 insertions(+), 102 deletions(-) rename plugins/Kaleidoscope-MouseKeys/src/kaleidoscope/plugin/{MouseKeys => mousekeys}/MouseKeyDefs.h (100%) rename plugins/Kaleidoscope-MouseKeys/src/kaleidoscope/plugin/{MouseKeys => mousekeys}/MouseWarpModes.h (100%) rename plugins/Kaleidoscope-MouseKeys/src/kaleidoscope/plugin/{MouseKeys => mousekeys}/MouseWrapper.cpp (65%) rename plugins/Kaleidoscope-MouseKeys/src/kaleidoscope/plugin/{MouseKeys => mousekeys}/MouseWrapper.h (75%) diff --git a/plugins/Kaleidoscope-MouseKeys/src/kaleidoscope/plugin/MouseKeys.cpp b/plugins/Kaleidoscope-MouseKeys/src/kaleidoscope/plugin/MouseKeys.cpp index 0a05bdb3..c3c90051 100644 --- a/plugins/Kaleidoscope-MouseKeys/src/kaleidoscope/plugin/MouseKeys.cpp +++ b/plugins/Kaleidoscope-MouseKeys/src/kaleidoscope/plugin/MouseKeys.cpp @@ -16,65 +16,59 @@ #include +#include "MouseKeys.h" +#include "kaleidoscope/plugin/mousekeys/MouseWrapper.h" + #include "kaleidoscope/Runtime.h" -#include "Kaleidoscope-MouseKeys.h" #include "Kaleidoscope-FocusSerial.h" #include "kaleidoscope/keyswitch_state.h" namespace kaleidoscope { namespace plugin { -uint8_t MouseKeys_::speed = 1; -uint16_t MouseKeys_::speedDelay = 1; - -uint8_t MouseKeys_::accelSpeed = 1; -uint16_t MouseKeys_::accelDelay = 64; - -uint8_t MouseKeys_::wheelSpeed = 1; -uint16_t MouseKeys_::wheelDelay = 50; +uint8_t MouseKeys::speed = 1; +uint16_t MouseKeys::speedDelay = 1; -uint16_t MouseKeys_::move_start_time_; -uint16_t MouseKeys_::accel_start_time_; -uint16_t MouseKeys_::wheel_start_time_; +uint8_t MouseKeys::accelSpeed = 1; +uint16_t MouseKeys::accelDelay = 64; -uint8_t MouseKeys_::directions_ = 0; -uint8_t MouseKeys_::pending_directions_ = 0; -uint8_t MouseKeys_::buttons_ = 0; +uint8_t MouseKeys::wheelSpeed = 1; +uint16_t MouseKeys::wheelDelay = 50; // ============================================================================= // Configuration functions -void MouseKeys_::setWarpGridSize(uint8_t grid_size) { - MouseWrapper.warp_grid_size = grid_size; +void MouseKeys::setWarpGridSize(uint8_t grid_size) { + mousekeys::wrapper.warp_grid_size = grid_size; } -void MouseKeys_::setSpeedLimit(uint8_t speed_limit) { - MouseWrapper.speedLimit = speed_limit; +void MouseKeys::setSpeedLimit(uint8_t speed_limit) { + mousekeys::wrapper.speed_limit = speed_limit; } // ============================================================================= // Key variant tests -bool MouseKeys_::isMouseKey(const Key& key) const { +bool MouseKeys::isMouseKey(const Key& key) const { return (key.getFlags() == (SYNTHETIC | IS_MOUSE_KEY)); } -bool MouseKeys_::isMouseButtonKey(const Key& key) const { +bool MouseKeys::isMouseButtonKey(const Key& key) const { uint8_t variant = key.getKeyCode() & (KEY_MOUSE_BUTTON | KEY_MOUSE_WARP); return variant == KEY_MOUSE_BUTTON; } -bool MouseKeys_::isMouseMoveKey(const Key& key) const { +bool MouseKeys::isMouseMoveKey(const Key& key) const { uint8_t mask = (KEY_MOUSE_BUTTON | KEY_MOUSE_WARP | KEY_MOUSE_WHEEL); uint8_t variant = key.getKeyCode() & mask; return variant == 0; } -bool MouseKeys_::isMouseWarpKey(const Key& key) const { +bool MouseKeys::isMouseWarpKey(const Key& key) const { return (key.getKeyCode() & KEY_MOUSE_WARP) != 0; } -bool MouseKeys_::isMouseWheelKey(const Key& key) const { +bool MouseKeys::isMouseWheelKey(const Key& key) const { uint8_t mask = (KEY_MOUSE_BUTTON | KEY_MOUSE_WARP | KEY_MOUSE_WHEEL); uint8_t variant = key.getKeyCode() & mask; return variant == KEY_MOUSE_WHEEL; @@ -84,12 +78,12 @@ bool MouseKeys_::isMouseWheelKey(const Key& key) const { // Event Handlers // ----------------------------------------------------------------------------- -EventHandlerResult MouseKeys_::onNameQuery() { +EventHandlerResult MouseKeys::onNameQuery() { return ::Focus.sendName(F("MouseKeys")); } // ----------------------------------------------------------------------------- -EventHandlerResult MouseKeys_::onSetup(void) { +EventHandlerResult MouseKeys::onSetup(void) { kaleidoscope::Runtime.hid().mouse().setup(); kaleidoscope::Runtime.hid().absoluteMouse().setup(); @@ -97,14 +91,14 @@ EventHandlerResult MouseKeys_::onSetup(void) { } // ----------------------------------------------------------------------------- -EventHandlerResult MouseKeys_::afterEachCycle() { +EventHandlerResult MouseKeys::afterEachCycle() { // Check timeout for accel update interval. if (Runtime.hasTimeExpired(accel_start_time_, accelDelay)) { accel_start_time_ = Runtime.millisAtCycleStart(); - // `accelStep` determines the movement speed of the mouse pointer, and gets + // `accel_step` determines the movement speed of the mouse pointer, and gets // reset to zero when no mouse movement keys is pressed (see below). - if (MouseWrapper.accelStep < 255 - accelSpeed) { - MouseWrapper.accelStep += accelSpeed; + if (mousekeys::wrapper.accel_step < 255 - accelSpeed) { + mousekeys::wrapper.accel_step += accelSpeed; } } @@ -120,7 +114,7 @@ EventHandlerResult MouseKeys_::afterEachCycle() { } // ----------------------------------------------------------------------------- -EventHandlerResult MouseKeys_::onKeyEvent(KeyEvent &event) { +EventHandlerResult MouseKeys::onKeyEvent(KeyEvent &event) { if (!isMouseKey(event.key)) return EventHandlerResult::OK; @@ -145,7 +139,7 @@ EventHandlerResult MouseKeys_::onKeyEvent(KeyEvent &event) { } // ----------------------------------------------------------------------------- -EventHandlerResult MouseKeys_::afterReportingState(const KeyEvent &event) { +EventHandlerResult MouseKeys::afterReportingState(const KeyEvent &event) { if (!isMouseKey(event.key)) return EventHandlerResult::OK; @@ -180,7 +174,7 @@ EventHandlerResult MouseKeys_::afterReportingState(const KeyEvent &event) { // will be up to date at the end of processing a mouse key event. We add bits // to the `pending` directions only; these get copied later if the event isn't // aborted. -EventHandlerResult MouseKeys_::onAddToReport(Key key) { +EventHandlerResult MouseKeys::onAddToReport(Key key) { if (!isMouseKey(key)) return EventHandlerResult::OK; @@ -200,15 +194,15 @@ EventHandlerResult MouseKeys_::onAddToReport(Key key) { // HID report helper functions // ----------------------------------------------------------------------------- -void MouseKeys_::sendMouseButtonReport() const { +void MouseKeys::sendMouseButtonReport() const { Runtime.hid().mouse().releaseAllButtons(); Runtime.hid().mouse().pressButtons(buttons_); Runtime.hid().mouse().sendReport(); } // ----------------------------------------------------------------------------- -void MouseKeys_::sendMouseWarpReport(const KeyEvent &event) const { - MouseWrapper.warp( +void MouseKeys::sendMouseWarpReport(const KeyEvent &event) const { + mousekeys::wrapper.warp( ((event.key.getKeyCode() & KEY_MOUSE_WARP_END) ? WARP_END : 0x00) | ((event.key.getKeyCode() & KEY_MOUSE_UP) ? WARP_UP : 0x00) | ((event.key.getKeyCode() & KEY_MOUSE_DOWN) ? WARP_DOWN : 0x00) | @@ -217,7 +211,7 @@ void MouseKeys_::sendMouseWarpReport(const KeyEvent &event) const { } // ----------------------------------------------------------------------------- -void MouseKeys_::sendMouseMoveReport() { +void MouseKeys::sendMouseMoveReport() { move_start_time_ = Runtime.millisAtCycleStart(); int8_t vx = 0; @@ -226,7 +220,7 @@ void MouseKeys_::sendMouseMoveReport() { if (direction == 0) { // If there are no mouse movement keys held, reset speed to zero. - MouseWrapper.accelStep = 0; + mousekeys::wrapper.accel_step = 0; } else { // For each active direction, add the mouse movement speed. if (direction & KEY_MOUSE_LEFT) @@ -239,14 +233,14 @@ void MouseKeys_::sendMouseMoveReport() { vy += speed; // Prepare the mouse report. - MouseWrapper.move(vx, vy); + mousekeys::wrapper.move(vx, vy); // Send the report. Runtime.hid().mouse().sendReport(); } } // ----------------------------------------------------------------------------- -void MouseKeys_::sendMouseWheelReport() { +void MouseKeys::sendMouseWheelReport() { wheel_start_time_ = Runtime.millisAtCycleStart(); int8_t vx = 0; @@ -275,4 +269,4 @@ void MouseKeys_::sendMouseWheelReport() { } // namespace plugin } // namespace kaleidoscope -kaleidoscope::plugin::MouseKeys_ MouseKeys; +kaleidoscope::plugin::MouseKeys MouseKeys; diff --git a/plugins/Kaleidoscope-MouseKeys/src/kaleidoscope/plugin/MouseKeys.h b/plugins/Kaleidoscope-MouseKeys/src/kaleidoscope/plugin/MouseKeys.h index c5ac49f0..579378ad 100644 --- a/plugins/Kaleidoscope-MouseKeys/src/kaleidoscope/plugin/MouseKeys.h +++ b/plugins/Kaleidoscope-MouseKeys/src/kaleidoscope/plugin/MouseKeys.h @@ -17,16 +17,13 @@ #pragma once #include "kaleidoscope/Runtime.h" -#include "kaleidoscope/plugin/MouseKeys/MouseKeyDefs.h" -#include "kaleidoscope/plugin/MouseKeys/MouseWarpModes.h" -#include "kaleidoscope/plugin/MouseKeys/MouseWrapper.h" +#include "kaleidoscope/plugin/mousekeys/MouseKeyDefs.h" +#include "kaleidoscope/plugin/mousekeys/MouseWarpModes.h" namespace kaleidoscope { namespace plugin { -class MouseKeys_ : public kaleidoscope::Plugin { +class MouseKeys : public kaleidoscope::Plugin { public: - MouseKeys_(void) {} - static uint8_t speed; static uint16_t speedDelay; static uint8_t accelSpeed; @@ -45,9 +42,9 @@ class MouseKeys_ : public kaleidoscope::Plugin { EventHandlerResult afterReportingState(const KeyEvent &event); private: - static uint16_t move_start_time_; - static uint16_t accel_start_time_; - static uint16_t wheel_start_time_; + uint16_t move_start_time_ = 0; + uint16_t accel_start_time_ = 0; + uint16_t wheel_start_time_ = 0; // Mouse cursor and wheel movement directions are stored in a single bitfield // to save space. The low four bits are for cursor movement, and the high @@ -55,9 +52,9 @@ class MouseKeys_ : public kaleidoscope::Plugin { static constexpr uint8_t wheel_offset_ = 4; static constexpr uint8_t wheel_mask_ = 0b11110000; static constexpr uint8_t move_mask_ = 0b00001111; - static uint8_t directions_; - static uint8_t pending_directions_; - static uint8_t buttons_; + uint8_t directions_ = 0; + uint8_t pending_directions_ = 0; + uint8_t buttons_ = 0; bool isMouseKey(const Key &key) const; bool isMouseButtonKey(const Key &key) const; @@ -71,7 +68,8 @@ class MouseKeys_ : public kaleidoscope::Plugin { void sendMouseWheelReport(); }; -} -} -extern kaleidoscope::plugin::MouseKeys_ MouseKeys; +} // namespace plugin +} // namespace kaleidoscope + +extern kaleidoscope::plugin::MouseKeys MouseKeys; diff --git a/plugins/Kaleidoscope-MouseKeys/src/kaleidoscope/plugin/MouseKeys/MouseKeyDefs.h b/plugins/Kaleidoscope-MouseKeys/src/kaleidoscope/plugin/mousekeys/MouseKeyDefs.h similarity index 100% rename from plugins/Kaleidoscope-MouseKeys/src/kaleidoscope/plugin/MouseKeys/MouseKeyDefs.h rename to plugins/Kaleidoscope-MouseKeys/src/kaleidoscope/plugin/mousekeys/MouseKeyDefs.h diff --git a/plugins/Kaleidoscope-MouseKeys/src/kaleidoscope/plugin/MouseKeys/MouseWarpModes.h b/plugins/Kaleidoscope-MouseKeys/src/kaleidoscope/plugin/mousekeys/MouseWarpModes.h similarity index 100% rename from plugins/Kaleidoscope-MouseKeys/src/kaleidoscope/plugin/MouseKeys/MouseWarpModes.h rename to plugins/Kaleidoscope-MouseKeys/src/kaleidoscope/plugin/mousekeys/MouseWarpModes.h diff --git a/plugins/Kaleidoscope-MouseKeys/src/kaleidoscope/plugin/MouseKeys/MouseWrapper.cpp b/plugins/Kaleidoscope-MouseKeys/src/kaleidoscope/plugin/mousekeys/MouseWrapper.cpp similarity index 65% rename from plugins/Kaleidoscope-MouseKeys/src/kaleidoscope/plugin/MouseKeys/MouseWrapper.cpp rename to plugins/Kaleidoscope-MouseKeys/src/kaleidoscope/plugin/mousekeys/MouseWrapper.cpp index 25eb8687..239dbddf 100644 --- a/plugins/Kaleidoscope-MouseKeys/src/kaleidoscope/plugin/MouseKeys/MouseWrapper.cpp +++ b/plugins/Kaleidoscope-MouseKeys/src/kaleidoscope/plugin/mousekeys/MouseWrapper.cpp @@ -19,29 +19,29 @@ // // #include -#include "kaleidoscope/plugin/MouseKeys/MouseWrapper.h" +#include "kaleidoscope/plugin/mousekeys/MouseWrapper.h" namespace kaleidoscope { namespace plugin { +namespace mousekeys { -uint8_t MouseWrapper_::warp_grid_size = MOUSE_WARP_GRID_2X2; -uint16_t MouseWrapper_::next_width; -uint16_t MouseWrapper_::next_height; -uint16_t MouseWrapper_::section_top; -uint16_t MouseWrapper_::section_left; -boolean MouseWrapper_::is_warping; +uint8_t MouseWrapper::warp_grid_size = MOUSE_WARP_GRID_2X2; +uint16_t MouseWrapper::next_width; +uint16_t MouseWrapper::next_height; +uint16_t MouseWrapper::section_top; +uint16_t MouseWrapper::section_left; +boolean MouseWrapper::is_warping; -uint8_t MouseWrapper_::accelStep; -uint8_t MouseWrapper_::speedLimit = 127; -uint8_t MouseWrapper_::subpixelsPerPixel = 16; +uint8_t MouseWrapper::accel_step; +uint8_t MouseWrapper::speed_limit = 127; -void MouseWrapper_::warp_jump(uint16_t left, uint16_t top, uint16_t height, uint16_t width) { +void MouseWrapper::warpJump(uint16_t left, uint16_t top, uint16_t height, uint16_t width) { uint16_t x_center = left + width / 2; uint16_t y_center = top + height / 2; Kaleidoscope.hid().absoluteMouse().moveTo(x_center, y_center, 0); } -void MouseWrapper_::begin_warping() { +void MouseWrapper::beginWarping() { section_left = WARP_ABS_LEFT; section_top = WARP_ABS_TOP; next_width = MAX_WARP_WIDTH; @@ -49,23 +49,23 @@ void MouseWrapper_::begin_warping() { is_warping = true; } -void MouseWrapper_::end_warping() { +void MouseWrapper::endWarping() { is_warping = false; } -void MouseWrapper_::reset_warping() { +void MouseWrapper::resetWarping() { if (is_warping == true) { - begin_warping(); + beginWarping(); } } -void MouseWrapper_::warp(uint8_t warp_cmd) { +void MouseWrapper::warp(uint8_t warp_cmd) { if (is_warping == false) { - begin_warping(); + beginWarping(); } if (warp_cmd & WARP_END) { - end_warping(); + endWarping(); return; } @@ -77,7 +77,7 @@ void MouseWrapper_::warp(uint8_t warp_cmd) { section_left += next_width; section_top += next_height; - warp_jump(section_left, section_top, next_height, next_width); + warpJump(section_left, section_top, next_height, next_width); return; } @@ -94,12 +94,12 @@ void MouseWrapper_::warp(uint8_t warp_cmd) { section_left += next_width; } - warp_jump(section_left, section_top, next_height, next_width); + warpJump(section_left, section_top, next_height, next_width); } // To approximate a sine wave, this uses two parabolas. Acceleration begins // slowly, grows rapidly in the middle, and slows again near the top. -uint8_t MouseWrapper_::acceleration(uint8_t cycles) { +uint8_t MouseWrapper::acceleration(uint8_t cycles) { if (cycles < 128) { uint16_t c2 = cycles * cycles; return 1 + (c2 >> 7); @@ -110,33 +110,35 @@ uint8_t MouseWrapper_::acceleration(uint8_t cycles) { } } -void MouseWrapper_::move(int8_t x, int8_t y) { +void MouseWrapper::move(int8_t x, int8_t y) { int16_t moveX = 0; int16_t moveY = 0; static int8_t remainderX = 0; static int8_t remainderY = 0; - int16_t effectiveSpeedLimit = speedLimit; + int16_t effectiveSpeedLimit = speed_limit; if (x != 0) { - moveX = remainderX + (x * acceleration(accelStep)); + moveX = remainderX + (x * acceleration(accel_step)); if (moveX > effectiveSpeedLimit) moveX = effectiveSpeedLimit; else if (moveX < -effectiveSpeedLimit) moveX = -effectiveSpeedLimit; } if (y != 0) { - moveY = remainderY + (y * acceleration(accelStep)); + moveY = remainderY + (y * acceleration(accel_step)); if (moveY > effectiveSpeedLimit) moveY = effectiveSpeedLimit; else if (moveY < -effectiveSpeedLimit) moveY = -effectiveSpeedLimit; } - end_warping(); + endWarping(); // move by whole pixels, not subpixels - Kaleidoscope.hid().mouse().move(moveX / subpixelsPerPixel, moveY / subpixelsPerPixel); + Kaleidoscope.hid().mouse().move(moveX / subpixels_per_pixel, moveY / subpixels_per_pixel); // save leftover subpixel movements for later - remainderX = moveX - moveX / subpixelsPerPixel * subpixelsPerPixel; - remainderY = moveY - moveY / subpixelsPerPixel * subpixelsPerPixel; -} -} + remainderX = moveX - moveX / subpixels_per_pixel * subpixels_per_pixel; + remainderY = moveY - moveY / subpixels_per_pixel * subpixels_per_pixel; } -kaleidoscope::plugin::MouseWrapper_ MouseWrapper; +MouseWrapper wrapper; + +} // namespace mousekeys +} // namespace plugin +} // namespace kaleidoscope diff --git a/plugins/Kaleidoscope-MouseKeys/src/kaleidoscope/plugin/MouseKeys/MouseWrapper.h b/plugins/Kaleidoscope-MouseKeys/src/kaleidoscope/plugin/mousekeys/MouseWrapper.h similarity index 75% rename from plugins/Kaleidoscope-MouseKeys/src/kaleidoscope/plugin/MouseKeys/MouseWrapper.h rename to plugins/Kaleidoscope-MouseKeys/src/kaleidoscope/plugin/mousekeys/MouseWrapper.h index e817c631..a7d1c753 100644 --- a/plugins/Kaleidoscope-MouseKeys/src/kaleidoscope/plugin/MouseKeys/MouseWrapper.h +++ b/plugins/Kaleidoscope-MouseKeys/src/kaleidoscope/plugin/mousekeys/MouseWrapper.h @@ -17,7 +17,7 @@ #pragma once #include "Arduino.h" -#include "kaleidoscope/plugin/MouseKeys/MouseWarpModes.h" +#include "kaleidoscope/plugin/mousekeys/MouseWarpModes.h" // Warping commands @@ -40,19 +40,16 @@ namespace kaleidoscope { namespace plugin { +namespace mousekeys { -class MouseWrapper_ { +class MouseWrapper { public: - MouseWrapper_() {} - static void move(int8_t x, int8_t y); static void warp(uint8_t warp_cmd); - static void end_warping(); - static void reset_warping(); - static uint8_t accelStep; - static uint8_t speedLimit; - static uint8_t subpixelsPerPixel; + static uint8_t accel_step; + static uint8_t speed_limit; + static constexpr uint8_t subpixels_per_pixel = 16; static uint8_t warp_grid_size; private: @@ -63,10 +60,15 @@ class MouseWrapper_ { static boolean is_warping; static uint8_t acceleration(uint8_t cycles); - static void begin_warping(); - static void warp_jump(uint16_t left, uint16_t top, uint16_t height, uint16_t width); + + static void beginWarping(); + static void endWarping(); + static void resetWarping(); + static void warpJump(uint16_t left, uint16_t top, uint16_t height, uint16_t width); }; -} -} -extern kaleidoscope::plugin::MouseWrapper_ MouseWrapper; +extern MouseWrapper wrapper; + +} // namespace mousekeys +} // namespace plugin +} // namespace kaleidoscope