Move mouse keys to a library

The primary reason for the move is to be able to disable mouse keys
completely, and not even compile them in. For this to work, it needs to
be in a separate library, otherwise it will always be included, even if
not active.

So, this patch turns mouse-keys into a simple plugin, included with the
core firmware! This makes the default event handler a bit simpler, the
code marginally smaller, and the feature completely optional.

Signed-off-by: Gergely Nagy <algernon@madhouse-project.org>
pull/50/head
Gergely Nagy 8 years ago
parent cedb0565e7
commit 28aa497452

@ -100,6 +100,7 @@ compile: dirs
-tools $(ARDUINO_PATH)/tools-builder \ -tools $(ARDUINO_PATH)/tools-builder \
-fqbn $(FQBN) \ -fqbn $(FQBN) \
-libraries $(ARDUINO_LOCAL_LIB_PATH) \ -libraries $(ARDUINO_LOCAL_LIB_PATH) \
-libraries libraries/ \
-libraries . \ -libraries . \
$(VERBOSE) \ $(VERBOSE) \
-build-path $(BUILD_PATH) \ -build-path $(BUILD_PATH) \

@ -4,6 +4,7 @@
#define DEBUG_SERIAL false #define DEBUG_SERIAL false
#include "MouseKeys.h"
#include "KeyboardioFirmware.h" #include "KeyboardioFirmware.h"
#include "generated/keymaps.h" #include "generated/keymaps.h"

@ -0,0 +1,9 @@
name=Model01-MouseKeys
version=0.0.1
author=Jesse Vincent
maintainer=Jesse Vincent <jesse@keyboard.io>
sentence=Mouse keys for the Keyboardio Model 01.
paragraph=...
category=Communication
url=https://github.com/keyboardio/KeyboardioFirmware
architectures=avr

@ -0,0 +1,43 @@
#pragma once
#include "key_defs.h"
#define IS_MOUSE_KEY B00010000
// Synthetic, not internal
#define KEY_MOUSE_BTN_L 0x01 // Synthetic key
#define KEY_MOUSE_BTN_M 0x02 // Synthetic key
#define KEY_MOUSE_BTN_R 0x04 // Synthetic key
#define KEY_MOUSE_UP B0000001
#define KEY_MOUSE_DOWN B0000010
#define KEY_MOUSE_LEFT B0000100
#define KEY_MOUSE_RIGHT B0001000
#define KEY_MOUSE_CENTER B0010000
#define KEY_MOUSE_WARP B0100000
#define KEY_MOUSE_WARP_END B1000000
#define Key_mouseWarpNW (Key){ KEY_FLAGS|SYNTHETIC|IS_MOUSE_KEY, KEY_MOUSE_WARP| KEY_MOUSE_UP | KEY_MOUSE_LEFT }
#define Key_mouseWarpNE (Key){ KEY_FLAGS|SYNTHETIC|IS_MOUSE_KEY, KEY_MOUSE_WARP| KEY_MOUSE_UP | KEY_MOUSE_RIGHT }
#define Key_mouseWarpSW (Key){ KEY_FLAGS|SYNTHETIC|IS_MOUSE_KEY, KEY_MOUSE_WARP| KEY_MOUSE_DOWN | KEY_MOUSE_LEFT }
#define Key_mouseWarpSE (Key){ KEY_FLAGS|SYNTHETIC|IS_MOUSE_KEY, KEY_MOUSE_WARP| KEY_MOUSE_DOWN | KEY_MOUSE_RIGHT }
#define Key_mouseWarpEnd (Key){ KEY_FLAGS|SYNTHETIC|IS_MOUSE_KEY, KEY_MOUSE_WARP| KEY_MOUSE_WARP_END}
#define Key_mouseUpL (Key){ KEY_FLAGS|SYNTHETIC|IS_MOUSE_KEY, KEY_MOUSE_UP | KEY_MOUSE_LEFT }
#define Key_mouseUp (Key){ KEY_FLAGS|SYNTHETIC|IS_MOUSE_KEY, KEY_MOUSE_UP }
#define Key_mouseUpR (Key){ KEY_FLAGS|SYNTHETIC|IS_MOUSE_KEY, KEY_MOUSE_UP | KEY_MOUSE_RIGHT }
#define Key_mouseL (Key){ KEY_FLAGS|SYNTHETIC|IS_MOUSE_KEY, KEY_MOUSE_LEFT }
#define Key_mouseR (Key){ KEY_FLAGS|SYNTHETIC|IS_MOUSE_KEY, KEY_MOUSE_RIGHT }
#define Key_mouseDnL (Key){ KEY_FLAGS|SYNTHETIC|IS_MOUSE_KEY, KEY_MOUSE_DOWN | KEY_MOUSE_LEFT }
#define Key_mouseDn (Key){ KEY_FLAGS|SYNTHETIC|IS_MOUSE_KEY, KEY_MOUSE_DOWN }
#define Key_mouseDnR (Key){ KEY_FLAGS|SYNTHETIC|IS_MOUSE_KEY, KEY_MOUSE_DOWN | KEY_MOUSE_RIGHT }
#define Key_mouseScrollUp
#define Key_mouseScrollDn
#define Key_mouseScrollL
#define Key_mouseScrollR
#define Key_mouseBtnL (Key){ KEY_FLAGS | SYNTHETIC, KEY_MOUSE_BTN_L }
#define Key_mouseBtnM (Key){ KEY_FLAGS | SYNTHETIC, KEY_MOUSE_BTN_M }
#define Key_mouseBtnR (Key){ KEY_FLAGS | SYNTHETIC, KEY_MOUSE_BTN_R }

@ -0,0 +1,60 @@
#include <Arduino.h>
#include "MouseKeys.h"
#include "MouseWrapper.h"
#include "KeyboardioFirmware.h"
static void handle_mouse_key_event(Key mappedKey, uint8_t currentState, uint8_t previousState) {
if (!key_is_pressed(currentState,previousState))
return;
if (mappedKey.rawKey & KEY_MOUSE_UP) {
MouseWrapper.move(0,-1);
}
if (mappedKey.rawKey & KEY_MOUSE_DOWN) {
MouseWrapper.move(0,1);
}
if (mappedKey.rawKey & KEY_MOUSE_LEFT) {
MouseWrapper.move(-1,0);
}
if (mappedKey.rawKey & KEY_MOUSE_RIGHT) {
MouseWrapper.move(1,0);
}
}
static bool handleMouseKeys(Key mappedKey, byte row, byte col, uint8_t currentState, uint8_t previousState) {
if (! (mappedKey.flags & IS_INTERNAL)
&& (mappedKey.rawKey == KEY_MOUSE_BTN_L
|| mappedKey.rawKey == KEY_MOUSE_BTN_M
|| mappedKey.rawKey == KEY_MOUSE_BTN_R)) {
if (key_toggled_on(currentState, previousState)) {
MouseWrapper.press_button(mappedKey.rawKey);
} else if (key_toggled_off(currentState, previousState)) {
MouseWrapper.release_button(mappedKey.rawKey);
}
return true;
}
if (!(mappedKey.flags & IS_MOUSE_KEY))
return false;
if (!(mappedKey.rawKey & KEY_MOUSE_WARP)) {
handle_mouse_key_event(mappedKey, currentState, previousState);
} else if (key_toggled_on(currentState,previousState)) {
if (mappedKey.rawKey & KEY_MOUSE_WARP && mappedKey.flags & IS_MOUSE_KEY) {
// we don't pass in the left and up values because those are the
// default, "no-op" conditionals
MouseWrapper.warp( ((mappedKey.rawKey & KEY_MOUSE_WARP_END) ? WARP_END : 0x00) |
((mappedKey.rawKey & KEY_MOUSE_DOWN) ? WARP_DOWN : 0x00) |
((mappedKey.rawKey & KEY_MOUSE_RIGHT) ? WARP_RIGHT : 0x00) );
}
}
return true;
}
MouseKeys_::MouseKeys_(void) {
event_handler_hook_add (handleMouseKeys);
}
MouseKeys_ MouseKeys;

@ -0,0 +1,10 @@
#pragma once
#include "MouseKeyDefs.h"
class MouseKeys_ {
public:
MouseKeys_ (void);
};
extern MouseKeys_ MouseKeys;

@ -7,6 +7,7 @@
MouseWrapper_::MouseWrapper_(void) { MouseWrapper_::MouseWrapper_(void) {
Mouse.begin(); Mouse.begin();
AbsoluteMouse.begin();
} }
void MouseWrapper_::press_button(uint8_t button) { void MouseWrapper_::press_button(uint8_t button) {

@ -11,8 +11,6 @@ Keyboardio_::setup(const byte keymap_count) {
wdt_disable(); wdt_disable();
delay(100); delay(100);
Keyboard.begin(); Keyboard.begin();
Mouse.begin();
AbsoluteMouse.begin();
KeyboardHardware.setup(); KeyboardHardware.setup();
LEDControl.setup(); LEDControl.setup();

@ -26,7 +26,6 @@ typedef union {
#define IS_SYSCTL B00000010 #define IS_SYSCTL B00000010
#define IS_CONSUMER B00000100 #define IS_CONSUMER B00000100
#define IS_INTERNAL B00001000 #define IS_INTERNAL B00001000
#define IS_MOUSE_KEY B00010000
#define SWITCH_TO_KEYMAP B00100000 #define SWITCH_TO_KEYMAP B00100000
#define SWITCH_TO_KEYMAP_MOMENTARY B01000000 #define SWITCH_TO_KEYMAP_MOMENTARY B01000000
@ -36,23 +35,6 @@ typedef union {
#define LED_TOGGLE 0x01 // Synthetic, internal #define LED_TOGGLE 0x01 // Synthetic, internal
// Synthetic, not internal
#define KEY_MOUSE_BTN_L 0x01 // Synthetic key
#define KEY_MOUSE_BTN_M 0x02 // Synthetic key
#define KEY_MOUSE_BTN_R 0x04 // Synthetic key
#define KEY_MOUSE_UP B0000001
#define KEY_MOUSE_DOWN B0000010
#define KEY_MOUSE_LEFT B0000100
#define KEY_MOUSE_RIGHT B0001000
#define KEY_MOUSE_CENTER B0010000
#define KEY_MOUSE_WARP B0100000
#define KEY_MOUSE_WARP_END B1000000
#define KEYMAP_0 0 #define KEYMAP_0 0
#define KEYMAP_1 1 #define KEYMAP_1 1
#define KEYMAP_2 2 #define KEYMAP_2 2
@ -63,7 +45,6 @@ typedef union {
#define KEYMAP_7 7 #define KEYMAP_7 7
#define KEYMAP_PREVIOUS 253 #define KEYMAP_PREVIOUS 253
#define KEYMAP_NEXT 254 #define KEYMAP_NEXT 254
@ -74,34 +55,6 @@ typedef union {
#define Key_macroKey1 (Key){ KEY_FLAGS|SYNTHETIC|IS_MACRO, 1} #define Key_macroKey1 (Key){ KEY_FLAGS|SYNTHETIC|IS_MACRO, 1}
#define Key_mouseWarpNW (Key){ KEY_FLAGS|SYNTHETIC|IS_MOUSE_KEY, KEY_MOUSE_WARP| KEY_MOUSE_UP | KEY_MOUSE_LEFT }
#define Key_mouseWarpNE (Key){ KEY_FLAGS|SYNTHETIC|IS_MOUSE_KEY, KEY_MOUSE_WARP| KEY_MOUSE_UP | KEY_MOUSE_RIGHT }
#define Key_mouseWarpSW (Key){ KEY_FLAGS|SYNTHETIC|IS_MOUSE_KEY, KEY_MOUSE_WARP| KEY_MOUSE_DOWN | KEY_MOUSE_LEFT }
#define Key_mouseWarpSE (Key){ KEY_FLAGS|SYNTHETIC|IS_MOUSE_KEY, KEY_MOUSE_WARP| KEY_MOUSE_DOWN | KEY_MOUSE_RIGHT }
#define Key_mouseWarpEnd (Key){ KEY_FLAGS|SYNTHETIC|IS_MOUSE_KEY, KEY_MOUSE_WARP| KEY_MOUSE_WARP_END}
#define Key_mouseUpL (Key){ KEY_FLAGS|SYNTHETIC|IS_MOUSE_KEY, KEY_MOUSE_UP | KEY_MOUSE_LEFT }
#define Key_mouseUp (Key){ KEY_FLAGS|SYNTHETIC|IS_MOUSE_KEY, KEY_MOUSE_UP }
#define Key_mouseUpR (Key){ KEY_FLAGS|SYNTHETIC|IS_MOUSE_KEY, KEY_MOUSE_UP | KEY_MOUSE_RIGHT }
#define Key_mouseL (Key){ KEY_FLAGS|SYNTHETIC|IS_MOUSE_KEY, KEY_MOUSE_LEFT }
#define Key_mouseR (Key){ KEY_FLAGS|SYNTHETIC|IS_MOUSE_KEY, KEY_MOUSE_RIGHT }
#define Key_mouseDnL (Key){ KEY_FLAGS|SYNTHETIC|IS_MOUSE_KEY, KEY_MOUSE_DOWN | KEY_MOUSE_LEFT }
#define Key_mouseDn (Key){ KEY_FLAGS|SYNTHETIC|IS_MOUSE_KEY, KEY_MOUSE_DOWN }
#define Key_mouseDnR (Key){ KEY_FLAGS|SYNTHETIC|IS_MOUSE_KEY, KEY_MOUSE_DOWN | KEY_MOUSE_RIGHT }
#define Key_mouseScrollUp
#define Key_mouseScrollDn
#define Key_mouseScrollL
#define Key_mouseScrollR
#define Key_mouseBtnL (Key){ KEY_FLAGS | SYNTHETIC, KEY_MOUSE_BTN_L }
#define Key_mouseBtnM (Key){ KEY_FLAGS | SYNTHETIC , KEY_MOUSE_BTN_M }
#define Key_mouseBtnR (Key){ KEY_FLAGS | SYNTHETIC, KEY_MOUSE_BTN_R }
#define Key_powerDown (Key) {KEY_FLAGS | SYNTHETIC|IS_SYSCTL,HID_SYSTEM_POWER_DOWN } #define Key_powerDown (Key) {KEY_FLAGS | SYNTHETIC|IS_SYSCTL,HID_SYSTEM_POWER_DOWN }
#define Key_sleep (Key) {KEY_FLAGS | SYNTHETIC|IS_SYSCTL,HID_SYSTEM_SLEEP } #define Key_sleep (Key) {KEY_FLAGS | SYNTHETIC|IS_SYSCTL,HID_SYSTEM_SLEEP }
#define Key_wakeup (Key) {KEY_FLAGS | SYNTHETIC|IS_SYSCTL,HID_SYSTEM_WAKEUP } #define Key_wakeup (Key) {KEY_FLAGS | SYNTHETIC|IS_SYSCTL,HID_SYSTEM_WAKEUP }

@ -1,28 +1,8 @@
#include "key_events.h" #include "key_events.h"
void handle_synthetic_key_event(Key mappedKey, uint8_t currentState, uint8_t previousState) { void handle_synthetic_key_event(Key mappedKey, uint8_t currentState, uint8_t previousState) {
if (mappedKey.flags & IS_MOUSE_KEY && !( mappedKey.rawKey & KEY_MOUSE_WARP) ) { if (key_toggled_on(currentState,previousState)) {
handle_mouse_key_event(mappedKey, currentState, previousState); if (mappedKey.flags & IS_CONSUMER) {
} else if (! (mappedKey.flags & IS_INTERNAL)
&& (mappedKey.rawKey == KEY_MOUSE_BTN_L
|| mappedKey.rawKey == KEY_MOUSE_BTN_M
|| mappedKey.rawKey == KEY_MOUSE_BTN_R)) {
if (key_toggled_on(currentState, previousState)) {
MouseWrapper.press_button(mappedKey.rawKey);
} else if (key_toggled_off(currentState, previousState)) {
MouseWrapper.release_button(mappedKey.rawKey);
}
}
else if (key_toggled_on(currentState,previousState)) {
if (mappedKey.rawKey & KEY_MOUSE_WARP && mappedKey.flags & IS_MOUSE_KEY) {
// we don't pass in the left and up values because those are the
// default, "no-op" conditionals
MouseWrapper.warp( ((mappedKey.rawKey & KEY_MOUSE_WARP_END) ? WARP_END : 0x00) |
((mappedKey.rawKey & KEY_MOUSE_DOWN) ? WARP_DOWN : 0x00) |
((mappedKey.rawKey & KEY_MOUSE_RIGHT) ? WARP_RIGHT : 0x00) );
} else if (mappedKey.flags & IS_CONSUMER) {
ConsumerControl.press(mappedKey.rawKey); ConsumerControl.press(mappedKey.rawKey);
} else if (mappedKey.flags & IS_INTERNAL) { } else if (mappedKey.flags & IS_INTERNAL) {
if (mappedKey.rawKey == LED_TOGGLE) { if (mappedKey.rawKey == LED_TOGGLE) {
@ -121,21 +101,3 @@ void handle_keymap_key_event(Key keymapEntry, uint8_t currentState, uint8_t prev
Storage.save_primary_keymap(primary_keymap); Storage.save_primary_keymap(primary_keymap);
} }
} }
void handle_mouse_key_event(Key mappedKey, uint8_t currentState, uint8_t previousState) {
if (key_is_pressed(currentState,previousState)) {
if (mappedKey.rawKey & KEY_MOUSE_UP) {
MouseWrapper.move(0,-1);
}
if (mappedKey.rawKey & KEY_MOUSE_DOWN) {
MouseWrapper.move(0,1);
}
if (mappedKey.rawKey & KEY_MOUSE_LEFT) {
MouseWrapper.move(-1,0);
}
if (mappedKey.rawKey & KEY_MOUSE_RIGHT) {
MouseWrapper.move(1,0);
}
}
}

@ -1,9 +1,9 @@
#pragma once #pragma once
#include <Arduino.h> #include <Arduino.h>
#include "KeyboardioHID.h"
#include "key_defs.h" #include "key_defs.h"
#include "keyswitch_state.h" #include "keyswitch_state.h"
#include "MouseWrapper.h"
#include "LEDControl.h" #include "LEDControl.h"
#include "Storage.h" #include "Storage.h"
#include "keymap_metadata.h" #include "keymap_metadata.h"
@ -47,7 +47,6 @@ void handle_key_event(Key mappedKey, byte row, byte col, uint8_t currentState, u
void handle_synthetic_key_event( Key mappedKey, uint8_t currentState, uint8_t previousState); void handle_synthetic_key_event( Key mappedKey, uint8_t currentState, uint8_t previousState);
void press_key(Key mappedKey); void press_key(Key mappedKey);
void handle_keymap_key_event(Key keymapEntry, uint8_t currentState, uint8_t previousState); void handle_keymap_key_event(Key keymapEntry, uint8_t currentState, uint8_t previousState);
void handle_mouse_key_event(Key mappedKey, uint8_t currentState, uint8_t previousState);
bool handle_key_event_default(Key mappedKey, byte row, byte col, uint8_t currentState, uint8_t previousState); bool handle_key_event_default(Key mappedKey, byte row, byte col, uint8_t currentState, uint8_t previousState);
Key lookup_key(byte keymap, byte row, byte col); Key lookup_key(byte keymap, byte row, byte col);

Loading…
Cancel
Save