diff --git a/examples/AppSwitcher/AppSwitcher.ino b/examples/AppSwitcher/AppSwitcher.ino index ba135c7d..d72feca0 100644 --- a/examples/AppSwitcher/AppSwitcher.ino +++ b/examples/AppSwitcher/AppSwitcher.ino @@ -48,20 +48,20 @@ const Key keymaps[][ROWS][COLS] PROGMEM = { const macro_t *macroAction(uint8_t macroIndex, uint8_t keyState) { - switch (macroIndex) { - case M_APPSWITCH: - return macroAppSwitch (keyState); - case M_APPCANCEL: - return macroAppCancel (keyState); - } - return MACRO_NONE; + switch (macroIndex) { + case M_APPSWITCH: + return macroAppSwitch(keyState); + case M_APPCANCEL: + return macroAppCancel(keyState); + } + return MACRO_NONE; } -void setup () { - Kaleidoscope.setup (KEYMAP_SIZE); - Kaleidoscope.use (&HostOS, &Macros, NULL); +void setup() { + Kaleidoscope.setup(KEYMAP_SIZE); + Kaleidoscope.use(&HostOS, &Macros, NULL); } -void loop () { - Kaleidoscope.loop (); +void loop() { + Kaleidoscope.loop(); } diff --git a/examples/AppSwitcher/Macros.cpp b/examples/AppSwitcher/Macros.cpp index 8c186035..d60cfe4f 100644 --- a/examples/AppSwitcher/Macros.cpp +++ b/examples/AppSwitcher/Macros.cpp @@ -27,31 +27,31 @@ using namespace KaleidoscopePlugins::HostOS; static bool appSwitchActive = false; -const macro_t *macroAppSwitch (uint8_t keyState) { - Key mod = Key_LeftAlt; - - if (HostOS.os () == OSX) - mod = Key_LeftGui; - - // Key was just pressed, or is being held - if (key_is_pressed(keyState)) { - return MACRO(Dr(mod), D(Tab), END); - } - // Key was just released - if (key_toggled_off(keyState)) { - return MACRO(U(Tab), Dr(mod), END); - } - // Key is not pressed, and was not just released. - // if appSwitchActive is true, we continue holding Alt. - if (appSwitchActive) { - return MACRO(Dr(mod), END); - } - // otherwise we do nothing - return MACRO_NONE; +const macro_t *macroAppSwitch(uint8_t keyState) { + Key mod = Key_LeftAlt; + + if (HostOS.os() == OSX) + mod = Key_LeftGui; + + // Key was just pressed, or is being held + if (key_is_pressed(keyState)) { + return MACRO(Dr(mod), D(Tab), END); + } + // Key was just released + if (key_toggled_off(keyState)) { + return MACRO(U(Tab), Dr(mod), END); + } + // Key is not pressed, and was not just released. + // if appSwitchActive is true, we continue holding Alt. + if (appSwitchActive) { + return MACRO(Dr(mod), END); + } + // otherwise we do nothing + return MACRO_NONE; } -const macro_t *macroAppCancel (uint8_t keyState) { - if (key_toggled_on (keyState)) - appSwitchActive = false; - return MACRO_NONE; +const macro_t *macroAppCancel(uint8_t keyState) { + if (key_toggled_on(keyState)) + appSwitchActive = false; + return MACRO_NONE; } diff --git a/examples/AppSwitcher/Macros.h b/examples/AppSwitcher/Macros.h index 2c74654f..f6f6f581 100644 --- a/examples/AppSwitcher/Macros.h +++ b/examples/AppSwitcher/Macros.h @@ -22,9 +22,9 @@ #include enum { - M_APPSWITCH, - M_APPCANCEL, + M_APPSWITCH, + M_APPCANCEL, }; -const macro_t *macroAppSwitch (uint8_t keyState); -const macro_t *macroAppCancel (uint8_t keyState); +const macro_t *macroAppSwitch(uint8_t keyState); +const macro_t *macroAppCancel(uint8_t keyState); diff --git a/examples/Kaleidoscope/Kaleidoscope.ino b/examples/Kaleidoscope/Kaleidoscope.ino index ee380867..4c1777cf 100644 --- a/examples/Kaleidoscope/Kaleidoscope.ino +++ b/examples/Kaleidoscope/Kaleidoscope.ino @@ -21,91 +21,91 @@ #define GENERIC_FN2 KEYMAP_STACKED ( \ - ___, Key_F1, Key_F2, Key_F3, Key_F4, Key_F5, XXX, \ - Key_Tab, Key_mouseBtnM, Key_mouseUp, ___, Key_mouseWarpNW, Key_mouseWarpNE, Consumer_ScanNextTrack, \ - Key_Home, Key_mouseL, Key_mouseDn, Key_mouseR, Key_mouseWarpSW, Key_mouseWarpSE, \ - Key_End, Key_Z, Key_X, Key_C, Key_V, Key_mouseWarpEnd, ___, \ - Key_LeftControl, Key_mouseBtnL, Key_LeftGui, Key_LeftShift, \ - ___,\ + ___, Key_F1, Key_F2, Key_F3, Key_F4, Key_F5, XXX, \ + Key_Tab, Key_mouseBtnM, Key_mouseUp, ___, Key_mouseWarpNW, Key_mouseWarpNE, Consumer_ScanNextTrack, \ + Key_Home, Key_mouseL, Key_mouseDn, Key_mouseR, Key_mouseWarpSW, Key_mouseWarpSE, \ + Key_End, Key_Z, Key_X, Key_C, Key_V, Key_mouseWarpEnd, ___, \ + Key_LeftControl, Key_mouseBtnL, Key_LeftGui, Key_LeftShift, \ + ___, \ \ - XXX, Key_F6, Key_F7, Key_F8, Key_F9, ___, ___,\ - Key_Delete, Consumer_PlaySlashPause, Key_LeftCurlyBracket, Key_RightCurlyBracket, Key_LeftBracket, Key_RightBracket, System_Sleep,\ - Key_LeftArrow, Key_DownArrow, Key_UpArrow, Key_RightArrow, Key_F11, Key_F12,\ - ___, Consumer_VolumeDecrement, Consumer_VolumeIncrement, Key_BacklightDown, Key_BacklightUp, Key_Backslash, Key_Pipe,\ - Key_RightShift, Key_RightAlt, Key_mouseBtnR, Key_RightControl,\ - ___\ + XXX, Key_F6, Key_F7, Key_F8, Key_F9, ___, ___, \ + Key_Delete, Consumer_PlaySlashPause, Key_LeftCurlyBracket, Key_RightCurlyBracket, Key_LeftBracket, Key_RightBracket, System_Sleep, \ + Key_LeftArrow, Key_DownArrow, Key_UpArrow, Key_RightArrow, Key_F11, Key_F12, \ + ___, Consumer_VolumeDecrement, Consumer_VolumeIncrement, Key_BacklightDown, Key_BacklightUp, Key_Backslash, Key_Pipe, \ + Key_RightShift, Key_RightAlt, Key_mouseBtnR, Key_RightControl, \ + ___\ ) #define NUMPAD KEYMAP (\ - ___, ___, ___, ___, ___, ___, ___, ___, ___, Key_Keypad7, Key_Keypad8, Key_Keypad9, Key_KeypadSubtract, ___, \ - ___, ___, ___, ___, ___, ___, ___, ___, ___, Key_Keypad4, Key_Keypad5, Key_Keypad6, Key_KeypadAdd, ___, \ - ___, ___, ___, ___, ___, ___, ___, Key_Keypad1, Key_Keypad2, Key_Keypad3, Key_Equals, Key_Quote, \ - ___, ___, ___, ___, ___, ___, ___, ___, ___, Key_Keypad0, Key_KeypadDot, Key_KeypadMultiply, Key_KeypadDivide, Key_Enter, \ - Key_LeftControl, Key_Backspace, Key_LeftGui, Key_LeftShift, Key_RightShift, Key_RightAlt, Key_Spacebar, Key_RightControl,\ - Key_Keymap1_Momentary, Key_Keymap1_Momentary \ + ___, ___, ___, ___, ___, ___, ___, ___, ___, Key_Keypad7, Key_Keypad8, Key_Keypad9, Key_KeypadSubtract, ___, \ + ___, ___, ___, ___, ___, ___, ___, ___, ___, Key_Keypad4, Key_Keypad5, Key_Keypad6, Key_KeypadAdd, ___, \ + ___, ___, ___, ___, ___, ___, ___, Key_Keypad1, Key_Keypad2, Key_Keypad3, Key_Equals, Key_Quote, \ + ___, ___, ___, ___, ___, ___, ___, ___, ___, Key_Keypad0, Key_KeypadDot, Key_KeypadMultiply, Key_KeypadDivide, Key_Enter, \ + Key_LeftControl, Key_Backspace, Key_LeftGui, Key_LeftShift, Key_RightShift, Key_RightAlt, Key_Spacebar, Key_RightControl, \ + Key_Keymap1_Momentary, Key_Keymap1_Momentary \ ) #define QWERTY KEYMAP ( \ - ___, Key_1, Key_2, Key_3, Key_4, Key_5, Key_LEDEffectNext, ___, Key_6, Key_7, Key_8, Key_9, Key_0, Key_ToggleNumlock, \ - Key_Backtick, Key_Q, Key_W, Key_E, Key_R, Key_T, Key_Tab, Key_Enter, Key_Y, Key_U, Key_I, Key_O, Key_P, Key_Equals, \ - Key_PageUp, Key_A, Key_S, Key_D, Key_F, Key_G, Key_H, Key_J, Key_K, Key_L, Key_Semicolon, Key_Quote, \ - Key_PageDown, Key_Z, Key_X, Key_C, Key_V, Key_B, Key_Escape, ___, Key_N, Key_M, Key_Comma, Key_Period, Key_Slash, Key_Minus, \ - Key_LeftControl, Key_Backspace, Key_LeftGui, Key_LeftShift, Key_RightShift, Key_RightAlt, Key_Spacebar, Key_RightControl,\ - Key_KeymapNext_Momentary, Key_KeymapNext_Momentary \ + ___, Key_1, Key_2, Key_3, Key_4, Key_5, Key_LEDEffectNext, ___, Key_6, Key_7, Key_8, Key_9, Key_0, Key_ToggleNumlock, \ + Key_Backtick, Key_Q, Key_W, Key_E, Key_R, Key_T, Key_Tab, Key_Enter, Key_Y, Key_U, Key_I, Key_O, Key_P, Key_Equals, \ + Key_PageUp, Key_A, Key_S, Key_D, Key_F, Key_G, Key_H, Key_J, Key_K, Key_L, Key_Semicolon, Key_Quote, \ + Key_PageDown, Key_Z, Key_X, Key_C, Key_V, Key_B, Key_Escape, ___, Key_N, Key_M, Key_Comma, Key_Period, Key_Slash, Key_Minus, \ + Key_LeftControl, Key_Backspace, Key_LeftGui, Key_LeftShift, Key_RightShift, Key_RightAlt, Key_Spacebar, Key_RightControl, \ + Key_KeymapNext_Momentary, Key_KeymapNext_Momentary \ ) const Key keymaps[][ROWS][COLS] PROGMEM = { - QWERTY, - GENERIC_FN2, - NUMPAD + QWERTY, + GENERIC_FN2, + NUMPAD }; -static LEDSolidColor solidRed (60, 0, 0); -static LEDSolidColor solidOrange (60, 20, 0); -static LEDSolidColor solidYellow (40, 35, 0); -static LEDSolidColor solidGreen (0, 100, 0); -static LEDSolidColor solidBlue (0, 15, 100); -static LEDSolidColor solidIndigo (0, 0, 100); -static LEDSolidColor solidViolet (70, 0, 60); +static LEDSolidColor solidRed(60, 0, 0); +static LEDSolidColor solidOrange(60, 20, 0); +static LEDSolidColor solidYellow(40, 35, 0); +static LEDSolidColor solidGreen(0, 100, 0); +static LEDSolidColor solidBlue(0, 15, 100); +static LEDSolidColor solidIndigo(0, 0, 100); +static LEDSolidColor solidViolet(70, 0, 60); const macro_t *macroAction(uint8_t macroIndex, uint8_t keyState) { - if (macroIndex == TOGGLENUMLOCK && key_toggled_on(keyState)) { - return NumLock.toggle (Macros.row, Macros.col, NUMPAD_KEYMAP); - } - - if (macroIndex == 1 && key_toggled_on(keyState)) { - Serial.print("Keyboard.IO keyboard driver v0.00"); - return MACRO(I(25), - D(LeftShift), T(M), U(LeftShift), T(O), T(D), T(E), T(L), - T(Spacebar), - W(100), - T(0), T(1), - END); - } - return MACRO_NONE; + if (macroIndex == TOGGLENUMLOCK && key_toggled_on(keyState)) { + return NumLock.toggle(Macros.row, Macros.col, NUMPAD_KEYMAP); + } + + if (macroIndex == 1 && key_toggled_on(keyState)) { + Serial.print("Keyboard.IO keyboard driver v0.00"); + return MACRO(I(25), + D(LeftShift), T(M), U(LeftShift), T(O), T(D), T(E), T(L), + T(Spacebar), + W(100), + T(0), T(1), + END); + } + return MACRO_NONE; } void setup() { - Kaleidoscope.setup(); + Kaleidoscope.setup(); - Kaleidoscope.use(&TestMode, - &LEDControl, &LEDOff, - &solidRed, &solidOrange, &solidYellow, &solidGreen, &solidBlue, &solidIndigo, &solidViolet, - &LEDBreatheEffect, &LEDRainbowEffect, &LEDChaseEffect, &NumLock, + Kaleidoscope.use(&TestMode, + &LEDControl, &LEDOff, + &solidRed, &solidOrange, &solidYellow, &solidGreen, &solidBlue, &solidIndigo, &solidViolet, + &LEDBreatheEffect, &LEDRainbowEffect, &LEDChaseEffect, &NumLock, - &Macros, - &MouseKeys, - NULL); + &Macros, + &MouseKeys, + NULL); - LEDOff.activate(); + LEDOff.activate(); } void loop() { - Kaleidoscope.loop(); + Kaleidoscope.loop(); } diff --git a/src/Kaleidoscope.cpp b/src/Kaleidoscope.cpp index ca9b2c8b..590e18f1 100644 --- a/src/Kaleidoscope.cpp +++ b/src/Kaleidoscope.cpp @@ -9,131 +9,131 @@ Kaleidoscope_::Kaleidoscope_(void) { void Kaleidoscope_::setup(void) { - KeyboardHardware.setup(); - Keyboard.begin(); + KeyboardHardware.setup(); + Keyboard.begin(); - // A workaround, so that the compiler does not optimize this out... - handle_keyswitch_event (Key_NoKey, 255, 255, 0); + // A workaround, so that the compiler does not optimize this out... + handle_keyswitch_event(Key_NoKey, 255, 255, 0); } void Kaleidoscope_::loop(void) { - KeyboardHardware.scan_matrix(); + KeyboardHardware.scan_matrix(); - for (byte i = 0; loopHooks[i] != NULL && i < HOOK_MAX; i++) { - loopHook hook = loopHooks[i]; - (*hook)(false); - } + for (byte i = 0; loopHooks[i] != NULL && i < HOOK_MAX; i++) { + loopHook hook = loopHooks[i]; + (*hook)(false); + } - Keyboard.sendReport(); - Keyboard.releaseAll(); + Keyboard.sendReport(); + Keyboard.releaseAll(); - for (byte i = 0; loopHooks[i] != NULL && i < HOOK_MAX; i++) { - loopHook hook = loopHooks[i]; - (*hook)(true); - } + for (byte i = 0; loopHooks[i] != NULL && i < HOOK_MAX; i++) { + loopHook hook = loopHooks[i]; + (*hook)(true); + } } void Kaleidoscope_::use(KaleidoscopePlugin *plugin, ...) { - va_list ap; - KaleidoscopePlugin *p; - - plugin->begin(); - va_start(ap, plugin); - while ((p = va_arg(ap, KaleidoscopePlugin*)) != NULL) { - p->begin(); - } - va_end(ap); + va_list ap; + KaleidoscopePlugin *p; + + plugin->begin(); + va_start(ap, plugin); + while ((p = va_arg(ap, KaleidoscopePlugin*)) != NULL) { + p->begin(); + } + va_end(ap); } void Kaleidoscope_::replaceEventHandlerHook(eventHandlerHook oldHook, eventHandlerHook newHook) { - for (byte i = 0; i < HOOK_MAX; i++) { - if (eventHandlers[i] == oldHook) { - eventHandlers[i] = newHook; - return; - } + for (byte i = 0; i < HOOK_MAX; i++) { + if (eventHandlers[i] == oldHook) { + eventHandlers[i] = newHook; + return; } + } } void -Kaleidoscope_::appendEventHandlerHook (eventHandlerHook hook) { - replaceEventHandlerHook((eventHandlerHook)NULL, hook); +Kaleidoscope_::appendEventHandlerHook(eventHandlerHook hook) { + replaceEventHandlerHook((eventHandlerHook)NULL, hook); } void -Kaleidoscope_::useEventHandlerHook (eventHandlerHook hook) { - for (byte i = 0; i < HOOK_MAX; i++) { - if (eventHandlers[i] == hook) - return; - } - appendEventHandlerHook(hook); +Kaleidoscope_::useEventHandlerHook(eventHandlerHook hook) { + for (byte i = 0; i < HOOK_MAX; i++) { + if (eventHandlers[i] == hook) + return; + } + appendEventHandlerHook(hook); } void Kaleidoscope_::replaceLoopHook(loopHook oldHook, loopHook newHook) { - for (byte i = 0; i < HOOK_MAX; i++) { - if (loopHooks[i] == oldHook) { - loopHooks[i] = newHook; - return; - } + for (byte i = 0; i < HOOK_MAX; i++) { + if (loopHooks[i] == oldHook) { + loopHooks[i] = newHook; + return; } + } } void Kaleidoscope_::appendLoopHook(loopHook hook) { - replaceLoopHook((loopHook)NULL, hook); + replaceLoopHook((loopHook)NULL, hook); } void Kaleidoscope_::useLoopHook(loopHook hook) { - for (byte i = 0; i < HOOK_MAX; i++) { - if (loopHooks[i] == hook) - return; - } - appendLoopHook (hook); + for (byte i = 0; i < HOOK_MAX; i++) { + if (loopHooks[i] == hook) + return; + } + appendLoopHook(hook); } bool Kaleidoscope_::focusHook(const char *command) { - enum { - ON, - OFF, - GETSTATE, - } subCommand; - - if (strncmp_P(command, PSTR("layer."), 6) != 0) - return false; - - if (strcmp_P (command + 6, PSTR("on")) == 0) - subCommand = ON; - else if (strcmp_P(command + 6, PSTR("off")) == 0) - subCommand = OFF; - else if (strcmp_P(command + 6, PSTR("getState")) == 0) - subCommand = GETSTATE; - else - return false; - - switch (subCommand) { - case ON: { - uint8_t layer = Serial.parseInt(); - Layer.on(layer); - break; - } - - case OFF: { - uint8_t layer = Serial.parseInt(); - Layer.off(layer); - break; - } - - case GETSTATE: - Serial.println(Layer.getLayerState(), BIN); - break; - } - - return true; + enum { + ON, + OFF, + GETSTATE, + } subCommand; + + if (strncmp_P(command, PSTR("layer."), 6) != 0) + return false; + + if (strcmp_P(command + 6, PSTR("on")) == 0) + subCommand = ON; + else if (strcmp_P(command + 6, PSTR("off")) == 0) + subCommand = OFF; + else if (strcmp_P(command + 6, PSTR("getState")) == 0) + subCommand = GETSTATE; + else + return false; + + switch (subCommand) { + case ON: { + uint8_t layer = Serial.parseInt(); + Layer.on(layer); + break; + } + + case OFF: { + uint8_t layer = Serial.parseInt(); + Layer.off(layer); + break; + } + + case GETSTATE: + Serial.println(Layer.getLayerState(), BIN); + break; + } + + return true; } Kaleidoscope_ Kaleidoscope; diff --git a/src/Kaleidoscope.h b/src/Kaleidoscope.h index 6a4a715e..7488ed5e 100644 --- a/src/Kaleidoscope.h +++ b/src/Kaleidoscope.h @@ -52,51 +52,51 @@ extern HARDWARE_IMPLEMENTATION KeyboardHardware; }) class KaleidoscopePlugin { - public: - virtual void begin(void) = 0; + public: + virtual void begin(void) = 0; }; class Kaleidoscope_ { - public: - Kaleidoscope_(void); - - void setup(const byte keymap_count) { - setup(); - } - void setup(void); - void loop(void); - void use(KaleidoscopePlugin *plugin, ...) __attribute__((sentinel)); - - // ---- hooks ---- - - /* - * In most cases, one only wants a single copy of a hook. On the other hand, - * plugins that depend on other plugins, may want to make it easier for the - * end-user to use the plugin, and call the setup function of the dependent - * plugins too. In case the end-user calls the same setup function, we'd end up - * with hooks registered multiple times. - * - * To avoid this, protection against double-registration has been introduced. - * The `event_handler_hook_use` and `loop_hook_use` functions will only allow - * one copy of the hook. The `event_handler_hook_append` and `loop_hook_append` - * functions will, on the other hand, just append the hooks, and not care about - * protection. - */ - typedef Key (*eventHandlerHook)(Key mappedKey, byte row, byte col, uint8_t keyState); - static eventHandlerHook eventHandlers[HOOK_MAX]; - - static void replaceEventHandlerHook(eventHandlerHook oldHook, eventHandlerHook newHook); - static void appendEventHandlerHook(eventHandlerHook hook); - static void useEventHandlerHook(eventHandlerHook hook); - - typedef void (*loopHook)(bool postClear); - static loopHook loopHooks[HOOK_MAX]; - - static void replaceLoopHook(loopHook oldHook, loopHook newHook); - static void appendLoopHook(loopHook hook); - static void useLoopHook(loopHook hook); - - static bool focusHook(const char *command); + public: + Kaleidoscope_(void); + + void setup(const byte keymap_count) { + setup(); + } + void setup(void); + void loop(void); + void use(KaleidoscopePlugin *plugin, ...) __attribute__((sentinel)); + + // ---- hooks ---- + + /* + * In most cases, one only wants a single copy of a hook. On the other hand, + * plugins that depend on other plugins, may want to make it easier for the + * end-user to use the plugin, and call the setup function of the dependent + * plugins too. In case the end-user calls the same setup function, we'd end up + * with hooks registered multiple times. + * + * To avoid this, protection against double-registration has been introduced. + * The `event_handler_hook_use` and `loop_hook_use` functions will only allow + * one copy of the hook. The `event_handler_hook_append` and `loop_hook_append` + * functions will, on the other hand, just append the hooks, and not care about + * protection. + */ + typedef Key(*eventHandlerHook)(Key mappedKey, byte row, byte col, uint8_t keyState); + static eventHandlerHook eventHandlers[HOOK_MAX]; + + static void replaceEventHandlerHook(eventHandlerHook oldHook, eventHandlerHook newHook); + static void appendEventHandlerHook(eventHandlerHook hook); + static void useEventHandlerHook(eventHandlerHook hook); + + typedef void (*loopHook)(bool postClear); + static loopHook loopHooks[HOOK_MAX]; + + static void replaceLoopHook(loopHook oldHook, loopHook newHook); + static void appendLoopHook(loopHook hook); + static void useLoopHook(loopHook hook); + + static bool focusHook(const char *command); }; extern Kaleidoscope_ Kaleidoscope; diff --git a/src/key_defs.h b/src/key_defs.h index adaea5d5..6ee93635 100644 --- a/src/key_defs.h +++ b/src/key_defs.h @@ -12,49 +12,49 @@ typedef union Key_ { - struct { - uint8_t keyCode; - uint8_t flags; - }; - uint16_t raw; - - inline bool operator==(uint16_t rhs) { - return this->raw == rhs; - } - inline bool operator==(const Key_ rhs) { - return this->raw == rhs.raw; - } - inline Key_& operator=(uint16_t raw) { - this->raw = raw; - return *this; - } - inline bool operator!=(const Key_& rhs) { - return !(*this == rhs); - } - inline bool operator>=(uint16_t raw) { - return this->raw >= raw; - } - inline bool operator<=(uint16_t raw) { - return this->raw <= raw; - } - inline bool operator>(uint16_t raw) { - return this->raw > raw; - } - inline bool operator<(uint16_t raw) { - return this->raw < raw; - } - inline bool operator>=(const Key_& other) { - return this->raw >= other.raw; - } - inline bool operator<=(const Key_& other) { - return this->raw <= other.raw; - } - inline bool operator>(const Key_& other) { - return this->raw > other.raw; - } - inline bool operator<(const Key_& other) { - return this->raw < other.raw; - } + struct { + uint8_t keyCode; + uint8_t flags; + }; + uint16_t raw; + + inline bool operator==(uint16_t rhs) { + return this->raw == rhs; + } + inline bool operator==(const Key_ rhs) { + return this->raw == rhs.raw; + } + inline Key_& operator=(uint16_t raw) { + this->raw = raw; + return *this; + } + inline bool operator!=(const Key_& rhs) { + return !(*this == rhs); + } + inline bool operator>=(uint16_t raw) { + return this->raw >= raw; + } + inline bool operator<=(uint16_t raw) { + return this->raw <= raw; + } + inline bool operator>(uint16_t raw) { + return this->raw > raw; + } + inline bool operator<(uint16_t raw) { + return this->raw < raw; + } + inline bool operator>=(const Key_& other) { + return this->raw >= other.raw; + } + inline bool operator<=(const Key_& other) { + return this->raw <= other.raw; + } + inline bool operator>(const Key_& other) { + return this->raw > other.raw; + } + inline bool operator<(const Key_& other) { + return this->raw < other.raw; + } } Key; diff --git a/src/key_events.cpp b/src/key_events.cpp index 455422f3..fde7f78f 100644 --- a/src/key_events.cpp +++ b/src/key_events.cpp @@ -1,96 +1,96 @@ #include "Kaleidoscope.h" static bool handle_synthetic_keyswitch_event(Key mappedKey, uint8_t keyState) { - if (mappedKey.flags & RESERVED) - return false; + if (mappedKey.flags & RESERVED) + return false; - if (!(mappedKey.flags & SYNTHETIC)) - return false; + if (!(mappedKey.flags & SYNTHETIC)) + return false; - if (mappedKey.flags & IS_INTERNAL) { - return false; - } else if (mappedKey.flags & IS_CONSUMER) { - if (key_is_pressed(keyState)) - ConsumerControl.press(mappedKey.keyCode); - else if (key_was_pressed(keyState)) - ConsumerControl.release(mappedKey.keyCode); - } else if (mappedKey.flags & IS_SYSCTL) { - if (key_is_pressed(keyState)) - SystemControl.press(mappedKey.keyCode); - else if (key_was_pressed(keyState)) - SystemControl.release(); - } else if (mappedKey.flags & SWITCH_TO_KEYMAP) { - // Should not happen, handled elsewhere. - } + if (mappedKey.flags & IS_INTERNAL) { + return false; + } else if (mappedKey.flags & IS_CONSUMER) { + if (key_is_pressed(keyState)) + ConsumerControl.press(mappedKey.keyCode); + else if (key_was_pressed(keyState)) + ConsumerControl.release(mappedKey.keyCode); + } else if (mappedKey.flags & IS_SYSCTL) { + if (key_is_pressed(keyState)) + SystemControl.press(mappedKey.keyCode); + else if (key_was_pressed(keyState)) + SystemControl.release(); + } else if (mappedKey.flags & SWITCH_TO_KEYMAP) { + // Should not happen, handled elsewhere. + } - return true; + return true; } static bool handle_keyswitch_event_default(Key mappedKey, byte row, byte col, uint8_t keyState) { - //for every newly pressed button, figure out what logical key it is and send a key down event - // for every newly released button, figure out what logical key it is and send a key up event + //for every newly pressed button, figure out what logical key it is and send a key down event + // for every newly released button, figure out what logical key it is and send a key up event - if (mappedKey.flags & SYNTHETIC) { - handle_synthetic_keyswitch_event( mappedKey, keyState); - } else if (key_is_pressed(keyState)) { - press_key(mappedKey); - } else if (key_toggled_off(keyState) && (keyState & INJECTED)) { - release_key(mappedKey); - } - return true; + if (mappedKey.flags & SYNTHETIC) { + handle_synthetic_keyswitch_event(mappedKey, keyState); + } else if (key_is_pressed(keyState)) { + press_key(mappedKey); + } else if (key_toggled_off(keyState) && (keyState & INJECTED)) { + release_key(mappedKey); + } + return true; } void press_key(Key mappedKey) { - if (mappedKey.flags & SHIFT_HELD) { - Keyboard.press(Key_LeftShift.keyCode); - } - if (mappedKey.flags & CTRL_HELD) { - Keyboard.press(Key_LeftControl.keyCode); - } - if (mappedKey.flags & LALT_HELD) { - Keyboard.press(Key_LeftAlt.keyCode); - } - if (mappedKey.flags & RALT_HELD) { - Keyboard.press(Key_RightAlt.keyCode); - } - if (mappedKey.flags & GUI_HELD) { - Keyboard.press(Key_LeftGui.keyCode); - } - Keyboard.press(mappedKey.keyCode); + if (mappedKey.flags & SHIFT_HELD) { + Keyboard.press(Key_LeftShift.keyCode); + } + if (mappedKey.flags & CTRL_HELD) { + Keyboard.press(Key_LeftControl.keyCode); + } + if (mappedKey.flags & LALT_HELD) { + Keyboard.press(Key_LeftAlt.keyCode); + } + if (mappedKey.flags & RALT_HELD) { + Keyboard.press(Key_RightAlt.keyCode); + } + if (mappedKey.flags & GUI_HELD) { + Keyboard.press(Key_LeftGui.keyCode); + } + Keyboard.press(mappedKey.keyCode); } void release_key(Key mappedKey) { - if (mappedKey.flags & SHIFT_HELD) { - Keyboard.release(Key_LeftShift.keyCode); - } - if (mappedKey.flags & CTRL_HELD) { - Keyboard.release(Key_LeftControl.keyCode); - } - if (mappedKey.flags & LALT_HELD) { - Keyboard.release(Key_LeftAlt.keyCode); - } - if (mappedKey.flags & RALT_HELD) { - Keyboard.release(Key_RightAlt.keyCode); - } - if (mappedKey.flags & GUI_HELD) { - Keyboard.release(Key_LeftGui.keyCode); - } - Keyboard.release(mappedKey.keyCode); + if (mappedKey.flags & SHIFT_HELD) { + Keyboard.release(Key_LeftShift.keyCode); + } + if (mappedKey.flags & CTRL_HELD) { + Keyboard.release(Key_LeftControl.keyCode); + } + if (mappedKey.flags & LALT_HELD) { + Keyboard.release(Key_LeftAlt.keyCode); + } + if (mappedKey.flags & RALT_HELD) { + Keyboard.release(Key_RightAlt.keyCode); + } + if (mappedKey.flags & GUI_HELD) { + Keyboard.release(Key_LeftGui.keyCode); + } + Keyboard.release(mappedKey.keyCode); } void handle_keyswitch_event(Key mappedKey, byte row, byte col, uint8_t keyState) { - if (!(keyState & INJECTED)) { - mappedKey = Layer.lookup(row, col); - } - for (byte i = 0; Kaleidoscope.eventHandlers[i] != NULL && i < HOOK_MAX; i++) { - Kaleidoscope_::eventHandlerHook handler = Kaleidoscope.eventHandlers[i]; - mappedKey = (*handler)(mappedKey, row, col, keyState); - if (mappedKey.raw == Key_NoKey.raw) - return; - } - mappedKey = Layer.eventHandler(mappedKey, row, col, keyState); + if (!(keyState & INJECTED)) { + mappedKey = Layer.lookup(row, col); + } + for (byte i = 0; Kaleidoscope.eventHandlers[i] != NULL && i < HOOK_MAX; i++) { + Kaleidoscope_::eventHandlerHook handler = Kaleidoscope.eventHandlers[i]; + mappedKey = (*handler)(mappedKey, row, col, keyState); if (mappedKey.raw == Key_NoKey.raw) - return; - handle_keyswitch_event_default(mappedKey, row, col, keyState); + return; + } + mappedKey = Layer.eventHandler(mappedKey, row, col, keyState); + if (mappedKey.raw == Key_NoKey.raw) + return; + handle_keyswitch_event_default(mappedKey, row, col, keyState); } diff --git a/src/layers.cpp b/src/layers.cpp index 4b40a00a..836ef0e4 100644 --- a/src/layers.cpp +++ b/src/layers.cpp @@ -5,145 +5,145 @@ static uint32_t LayerState; uint8_t Layer_::highestLayer; uint8_t Layer_::keyMap[ROWS][COLS]; -Key (*Layer_::getKey)(uint8_t layer, byte row, byte col) = Layer.getKeyFromPROGMEM; +Key(*Layer_::getKey)(uint8_t layer, byte row, byte col) = Layer.getKeyFromPROGMEM; static void handle_keymap_keyswitch_event(Key keymapEntry, uint8_t keyState) { - if (keymapEntry.keyCode >= MOMENTARY_OFFSET) { - uint8_t target = keymapEntry.keyCode - MOMENTARY_OFFSET; - - if (key_toggled_on(keyState)) { - if ( target == KEYMAP_NEXT) { - Layer.next(); - } else if ( target == KEYMAP_PREVIOUS) { - Layer.previous(); - } else { - Layer.on(target); - } - } - if (key_toggled_off(keyState)) { - if ( target == KEYMAP_NEXT) { - Layer.previous(); - } else if ( target == KEYMAP_PREVIOUS) { - Layer.next(); - } else { - Layer.off(target); - } - } - - // switch keymap and stay there - } else if (key_toggled_on(keyState)) { - if (Layer.isOn (keymapEntry.keyCode) && keymapEntry.keyCode) - Layer.off(keymapEntry.keyCode); - else - Layer.on(keymapEntry.keyCode); + if (keymapEntry.keyCode >= MOMENTARY_OFFSET) { + uint8_t target = keymapEntry.keyCode - MOMENTARY_OFFSET; + + if (key_toggled_on(keyState)) { + if (target == KEYMAP_NEXT) { + Layer.next(); + } else if (target == KEYMAP_PREVIOUS) { + Layer.previous(); + } else { + Layer.on(target); + } } + if (key_toggled_off(keyState)) { + if (target == KEYMAP_NEXT) { + Layer.previous(); + } else if (target == KEYMAP_PREVIOUS) { + Layer.next(); + } else { + Layer.off(target); + } + } + + // switch keymap and stay there + } else if (key_toggled_on(keyState)) { + if (Layer.isOn(keymapEntry.keyCode) && keymapEntry.keyCode) + Layer.off(keymapEntry.keyCode); + else + Layer.on(keymapEntry.keyCode); + } } Key Layer_::eventHandler(Key mappedKey, byte row, byte col, uint8_t keyState) { - if (mappedKey.flags != (SYNTHETIC | SWITCH_TO_KEYMAP)) - return mappedKey; + if (mappedKey.flags != (SYNTHETIC | SWITCH_TO_KEYMAP)) + return mappedKey; - handle_keymap_keyswitch_event(mappedKey, keyState); - return Key_NoKey; + handle_keymap_keyswitch_event(mappedKey, keyState); + return Key_NoKey; } -Layer_::Layer_ (void) { - defaultLayer (0); +Layer_::Layer_(void) { + defaultLayer(0); } Key -Layer_::getKeyFromPROGMEM (uint8_t layer, byte row, byte col) { - Key key; +Layer_::getKeyFromPROGMEM(uint8_t layer, byte row, byte col) { + Key key; - key.raw = pgm_read_word(&(keymaps[layer][row][col])); + key.raw = pgm_read_word(&(keymaps[layer][row][col])); - return key; + return key; } void Layer_::mergeLayers(void) { - memset (keyMap, DefaultLayer, ROWS * COLS); + memset(keyMap, DefaultLayer, ROWS * COLS); - if (LayerState == (uint32_t)(1 << DefaultLayer)) - return; + if (LayerState == (uint32_t)(1 << DefaultLayer)) + return; - for (uint8_t r = 0; r < ROWS; r++) { - for (uint8_t c = 0; c < COLS; c++) { - int8_t layer = highestLayer; + for (uint8_t r = 0; r < ROWS; r++) { + for (uint8_t c = 0; c < COLS; c++) { + int8_t layer = highestLayer; - while (layer > DefaultLayer) { - if (Layer.isOn (layer)) { - Key mappedKey = (*getKey)(layer, r, c); + while (layer > DefaultLayer) { + if (Layer.isOn(layer)) { + Key mappedKey = (*getKey)(layer, r, c); - if (mappedKey != Key_Transparent) { - keyMap[r][c] = layer; - break; - } - } - layer--; - } + if (mappedKey != Key_Transparent) { + keyMap[r][c] = layer; + break; + } } + layer--; + } } + } } Key Layer_::lookup(byte row, byte col) { - uint8_t layer = keyMap[row][col]; + uint8_t layer = keyMap[row][col]; - return (*getKey)(layer, row, col); + return (*getKey)(layer, row, col); } -uint8_t Layer_::top (void) { - for (int8_t i = 31; i >= 0; i--) { - if (bitRead (LayerState, i)) - return i; - } - return 0; +uint8_t Layer_::top(void) { + for (int8_t i = 31; i >= 0; i--) { + if (bitRead(LayerState, i)) + return i; + } + return 0; } -void Layer_::move (uint8_t layer) { - LayerState = 0; - on (layer); +void Layer_::move(uint8_t layer) { + LayerState = 0; + on(layer); } -void Layer_::on (uint8_t layer) { - bitSet (LayerState, layer); - if (layer > highestLayer) - highestLayer = layer; - mergeLayers(); +void Layer_::on(uint8_t layer) { + bitSet(LayerState, layer); + if (layer > highestLayer) + highestLayer = layer; + mergeLayers(); } -void Layer_::off (uint8_t layer) { - bitClear (LayerState, layer); - if (layer == highestLayer) - highestLayer = top(); - mergeLayers(); +void Layer_::off(uint8_t layer) { + bitClear(LayerState, layer); + if (layer == highestLayer) + highestLayer = top(); + mergeLayers(); } -boolean Layer_::isOn (uint8_t layer) { - return bitRead(LayerState, layer); +boolean Layer_::isOn(uint8_t layer) { + return bitRead(LayerState, layer); } -void Layer_::next (void) { - on (top () + 1); +void Layer_::next(void) { + on(top() + 1); } -void Layer_::previous (void) { - off (top ()); +void Layer_::previous(void) { + off(top()); } -void Layer_::defaultLayer (uint8_t layer) { - move (layer); - DefaultLayer = layer; +void Layer_::defaultLayer(uint8_t layer) { + move(layer); + DefaultLayer = layer; } -uint8_t Layer_::defaultLayer (void) { - return DefaultLayer; +uint8_t Layer_::defaultLayer(void) { + return DefaultLayer; } -uint32_t Layer_::getLayerState (void) { - return LayerState; +uint32_t Layer_::getLayerState(void) { + return LayerState; } Layer_ Layer; diff --git a/src/layers.h b/src/layers.h index c78157c3..91225dd7 100644 --- a/src/layers.h +++ b/src/layers.h @@ -5,38 +5,38 @@ #include KALEIDOSCOPE_HARDWARE_H class Layer_ { - public: - Layer_(void); + public: + Layer_(void); - static Key lookup(byte row, byte col); - static void on(uint8_t layer); - static void off(uint8_t layer); - static void move(uint8_t layer); + static Key lookup(byte row, byte col); + static void on(uint8_t layer); + static void off(uint8_t layer); + static void move(uint8_t layer); - static uint8_t top(void); - static void next(void); - static void previous(void); + static uint8_t top(void); + static void next(void); + static void previous(void); - static boolean isOn(uint8_t layer); + static boolean isOn(uint8_t layer); - static void defaultLayer(uint8_t layer); - static uint8_t defaultLayer(void); + static void defaultLayer(uint8_t layer); + static uint8_t defaultLayer(void); - static uint32_t getLayerState(void); + static uint32_t getLayerState(void); - static Key eventHandler(Key mappedKey, byte row, byte col, uint8_t keyState); + static Key eventHandler(Key mappedKey, byte row, byte col, uint8_t keyState); - static Key (*getKey)(uint8_t layer, byte row, byte col); + static Key(*getKey)(uint8_t layer, byte row, byte col); - static Key getKeyFromPROGMEM(uint8_t layer, byte row, byte col); + static Key getKeyFromPROGMEM(uint8_t layer, byte row, byte col); - private: - static uint8_t highestLayer; - static uint8_t keyMap[ROWS][COLS]; + private: + static uint8_t highestLayer; + static uint8_t keyMap[ROWS][COLS]; - static void mergeLayers(void); + static void mergeLayers(void); }; -Key layer_getKey (uint8_t layer, uint8_t r, uint8_t c); +Key layer_getKey(uint8_t layer, uint8_t r, uint8_t c); extern Layer_ Layer;