From 2fa935af37af333acd438cf99142108f9304f520 Mon Sep 17 00:00:00 2001 From: Eric Tang Date: Tue, 8 Aug 2017 12:01:34 -0700 Subject: [PATCH 1/8] Revert "Add some temporary code to ensure that only the default keymap is applied" This reverts commit 711769cb50fed87c48211960ed7a141643232145. --- right/src/config_parser/parse_keymap.c | 7 ------- 1 file changed, 7 deletions(-) diff --git a/right/src/config_parser/parse_keymap.c b/right/src/config_parser/parse_keymap.c index 27695cd..455ba26 100644 --- a/right/src/config_parser/parse_keymap.c +++ b/right/src/config_parser/parse_keymap.c @@ -185,28 +185,21 @@ parser_error_t ParseKeymap(config_buffer_t *buffer) {; const char *name = readString(buffer, &nameLen); const char *description = readString(buffer, &descriptionLen); uint16_t layerCount = readCompactLength(buffer); - bool temp; (void)name; (void)description; if (layerCount != LAYER_COUNT) { return ParserError_InvalidLayerCount; } - temp = ParserRunDry; - if (!isDefault) { - ParserRunDry = true; - } if (!ParserRunDry) { LedDisplay_SetText(abbreviationLen, abbreviation); } for (uint16_t layerIdx = 0; layerIdx < layerCount; layerIdx++) { errorCode = parseLayer(buffer, layerIdx); if (errorCode != ParserError_Success) { - ParserRunDry = temp; return errorCode; } } - ParserRunDry = temp; return ParserError_Success; } From 709e7644e5a74534720ffe62df2b1c9eb5a43103 Mon Sep 17 00:00:00 2001 From: Eric Tang Date: Sun, 6 Aug 2017 20:37:51 -0700 Subject: [PATCH 2/8] Fully implement parseSwitchKeymapAction --- right/src/config_parser/parse_keymap.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/right/src/config_parser/parse_keymap.c b/right/src/config_parser/parse_keymap.c index 455ba26..7bea9eb 100644 --- a/right/src/config_parser/parse_keymap.c +++ b/right/src/config_parser/parse_keymap.c @@ -51,9 +51,8 @@ static parser_error_t parseSwitchLayerAction(key_action_t *KeyAction, config_buf static parser_error_t parseSwitchKeymapAction(key_action_t *keyAction, config_buffer_t *buffer) { uint8_t keymapIndex = readUInt8(buffer); - (void)keymapIndex; keyAction->type = KeyActionType_SwitchKeymap; - // TODO: Implement this + keyAction->switchKeymap.keymapId = keymapIndex; return ParserError_Success; } From 599e70147963150eff822aeaca9f203dcff8e902 Mon Sep 17 00:00:00 2001 From: Eric Tang Date: Sun, 6 Aug 2017 20:41:51 -0700 Subject: [PATCH 3/8] Rename current_keymap.* to keymaps.* --- right/src/config_parser/parse_keymap.c | 2 +- right/src/{current_keymap.c => keymaps.c} | 0 right/src/{current_keymap.h => keymaps.h} | 4 ++-- right/src/usb_report_updater.c | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) rename right/src/{current_keymap.c => keymaps.c} (100%) rename right/src/{current_keymap.h => keymaps.h} (72%) diff --git a/right/src/config_parser/parse_keymap.c b/right/src/config_parser/parse_keymap.c index 7bea9eb..b61516f 100644 --- a/right/src/config_parser/parse_keymap.c +++ b/right/src/config_parser/parse_keymap.c @@ -1,6 +1,6 @@ #include "config_parser/parse_keymap.h" #include "key_action.h" -#include "current_keymap.h" +#include "keymaps.h" #include "led_display.h" static parser_error_t parseNoneAction(key_action_t *keyAction, config_buffer_t *buffer) { diff --git a/right/src/current_keymap.c b/right/src/keymaps.c similarity index 100% rename from right/src/current_keymap.c rename to right/src/keymaps.c diff --git a/right/src/current_keymap.h b/right/src/keymaps.h similarity index 72% rename from right/src/current_keymap.h rename to right/src/keymaps.h index 513a682..3e644d6 100644 --- a/right/src/current_keymap.h +++ b/right/src/keymaps.h @@ -1,5 +1,5 @@ -#ifndef __CURRENT_KEYMAP_H__ -#define __CURRENT_KEYMAP_H__ +#ifndef __KEYMAPS_H__ +#define __KEYMAPS_H__ // Includes: diff --git a/right/src/usb_report_updater.c b/right/src/usb_report_updater.c index 00d7651..19a940f 100644 --- a/right/src/usb_report_updater.c +++ b/right/src/usb_report_updater.c @@ -3,7 +3,7 @@ #include "led_display.h" #include "layer.h" #include "usb_interfaces/usb_interface_mouse.h" -#include "current_keymap.h" +#include "keymaps.h" #include "test_states.h" #include "peripherals/test_led.h" #include "slave_drivers/is31fl3731_driver.h" From 2621707206065fa86c1134fd2859f3da7db40ae1 Mon Sep 17 00:00:00 2001 From: Eric Tang Date: Tue, 8 Aug 2017 09:40:31 -0700 Subject: [PATCH 4/8] Implement keymap switching --- right/src/config_parser/parse_config.c | 2 +- right/src/config_parser/parse_keymap.c | 11 +++++++++-- right/src/config_parser/parse_keymap.h | 2 +- right/src/keymaps.c | 16 +++++++++++++++- right/src/keymaps.h | 21 +++++++++++++++++++++ right/src/usb_protocol_handler.c | 19 +++++++++++++++++++ right/src/usb_report_updater.c | 3 +++ 7 files changed, 69 insertions(+), 5 deletions(-) diff --git a/right/src/config_parser/parse_config.c b/right/src/config_parser/parse_config.c index 1867818..948df44 100644 --- a/right/src/config_parser/parse_config.c +++ b/right/src/config_parser/parse_config.c @@ -38,7 +38,7 @@ parser_error_t ParseConfig(config_buffer_t *buffer) { } keymapCount = readCompactLength(buffer); for (uint16_t keymapIdx = 0; keymapIdx < keymapCount; keymapIdx++) { - errorCode = ParseKeymap(buffer); + errorCode = ParseKeymap(buffer, keymapIdx, keymapCount); if (errorCode != ParserError_Success) { return errorCode; } diff --git a/right/src/config_parser/parse_keymap.c b/right/src/config_parser/parse_keymap.c index b61516f..a5333c8 100644 --- a/right/src/config_parser/parse_keymap.c +++ b/right/src/config_parser/parse_keymap.c @@ -174,7 +174,8 @@ static parser_error_t parseLayer(config_buffer_t *buffer, uint8_t layer) { return ParserError_Success; } -parser_error_t ParseKeymap(config_buffer_t *buffer) {; +parser_error_t ParseKeymap(config_buffer_t *buffer, uint8_t keymapIdx, uint8_t keymapCount) {; + uint16_t offset = buffer->offset; parser_error_t errorCode; uint16_t abbreviationLen; uint16_t nameLen; @@ -191,7 +192,13 @@ parser_error_t ParseKeymap(config_buffer_t *buffer) {; return ParserError_InvalidLayerCount; } if (!ParserRunDry) { - LedDisplay_SetText(abbreviationLen, abbreviation); + AllKeymapsCount = keymapCount; + AllKeymaps[keymapIdx].abbreviation = abbreviation; + AllKeymaps[keymapIdx].abbreviationLen = abbreviationLen; + AllKeymaps[keymapIdx].offset = offset; + if (isDefault) { + DefaultKeymapIndex = keymapIdx; + } } for (uint16_t layerIdx = 0; layerIdx < layerCount; layerIdx++) { errorCode = parseLayer(buffer, layerIdx); diff --git a/right/src/config_parser/parse_keymap.h b/right/src/config_parser/parse_keymap.h index 2e09d56..f0ad4ab 100644 --- a/right/src/config_parser/parse_keymap.h +++ b/right/src/config_parser/parse_keymap.h @@ -52,6 +52,6 @@ // Functions: - parser_error_t ParseKeymap(config_buffer_t *buffer); + parser_error_t ParseKeymap(config_buffer_t *buffer, uint8_t keymapIdx, uint8_t keymapCount); #endif diff --git a/right/src/keymaps.c b/right/src/keymaps.c index c285a97..535ebfe 100644 --- a/right/src/keymaps.c +++ b/right/src/keymaps.c @@ -1,9 +1,23 @@ -#include "key_action.h" #include "arduino_hid/ConsumerAPI.h" #include "arduino_hid/SystemAPI.h" +#include "keymaps.h" +#include "led_display.h" +#include "config_parser/parse_keymap.h" // TODO: Restore Ctrl and Super keys and Mod+N. +keymap_reference_t AllKeymaps[MAX_KEYMAP_NUM] = { { "QTY", 3 } }; +uint8_t AllKeymapsCount; +uint8_t DefaultKeymapIndex; +uint8_t CurrentKeymapIndex = 0; + +void Keymaps_Switch(uint8_t index) { + CurrentKeymapIndex = index; + UserConfigBuffer.offset = AllKeymaps[index].offset; + ParseKeymap(&UserConfigBuffer, index, AllKeymapsCount); + LedDisplay_SetText(AllKeymaps[index].abbreviationLen, AllKeymaps[index].abbreviation); +} + key_action_t CurrentKeymap[LAYER_COUNT][SLOT_COUNT][MAX_KEY_COUNT_PER_MODULE] = { // Base layer { diff --git a/right/src/keymaps.h b/right/src/keymaps.h index 3e644d6..6675c87 100644 --- a/right/src/keymaps.h +++ b/right/src/keymaps.h @@ -4,9 +4,30 @@ // Includes: #include + #include "key_action.h" + +// Macros: + + #define MAX_KEYMAP_NUM 255 + +// Typedefs: + + typedef struct { + const char *abbreviation; + uint16_t abbreviationLen; + uint16_t offset; + } keymap_reference_t; // Variables: + extern keymap_reference_t AllKeymaps[MAX_KEYMAP_NUM]; + extern uint8_t AllKeymapsCount; + extern uint8_t DefaultKeymapIndex; + extern uint8_t CurrentKeymapIndex; extern key_action_t CurrentKeymap[LAYER_COUNT][SLOT_COUNT][MAX_KEY_COUNT_PER_MODULE]; +// Functions: + + void Keymaps_Switch(uint8_t index); + #endif diff --git a/right/src/usb_protocol_handler.c b/right/src/usb_protocol_handler.c index c1f7c08..6f60f01 100644 --- a/right/src/usb_protocol_handler.c +++ b/right/src/usb_protocol_handler.c @@ -12,6 +12,7 @@ #include "wormhole.h" #include "peripherals/adc.h" #include "eeprom.h" +#include "keymaps.h" // Functions for setting error statuses @@ -86,6 +87,8 @@ void readMergeSensor(void) void applyConfig(void) { uint8_t *temp; + char oldKeymapAbbreviation[3]; + char oldKeymapAbbreviationLen; ParserRunDry = true; StagingUserConfigBuffer.offset = 0; @@ -96,6 +99,8 @@ void applyConfig(void) if (GenericHidOutBuffer[0]) { return; } + memcpy(oldKeymapAbbreviation, AllKeymaps[CurrentKeymapIndex].abbreviation, 3); + oldKeymapAbbreviationLen = AllKeymaps[CurrentKeymapIndex].abbreviationLen; ParserRunDry = false; temp = UserConfigBuffer.buffer; UserConfigBuffer.buffer = StagingUserConfigBuffer.buffer; @@ -105,6 +110,20 @@ void applyConfig(void) GenericHidOutBuffer[1] = UserConfigBuffer.offset; GenericHidOutBuffer[2] = UserConfigBuffer.offset >> 8; GenericHidOutBuffer[3] = 1; + if (GenericHidOutBuffer[0]) { + return; + } + for (uint8_t i = 0; i < AllKeymapsCount; i++) { + if (AllKeymaps[i].abbreviationLen != oldKeymapAbbreviationLen) { + continue; + } + if (memcmp(oldKeymapAbbreviation, AllKeymaps[i].abbreviation, oldKeymapAbbreviationLen)) { + continue; + } + Keymaps_Switch(i); + return; + } + Keymaps_Switch(DefaultKeymapIndex); } void setLedPwm(void) diff --git a/right/src/usb_report_updater.c b/right/src/usb_report_updater.c index 19a940f..4ac23d9 100644 --- a/right/src/usb_report_updater.c +++ b/right/src/usb_report_updater.c @@ -174,6 +174,9 @@ void UpdateActiveUsbReports() case KeyActionType_Test: processTestAction(action); break; + case KeyActionType_SwitchKeymap: + Keymaps_Switch(action.switchKeymap.keymapId); + break; } } } From 31de620fee32ee6f1f7f22a451cf5e924319f68e Mon Sep 17 00:00:00 2001 From: Eric Tang Date: Tue, 8 Aug 2017 09:47:39 -0700 Subject: [PATCH 5/8] Add error checking to parseSwitchKeymapAction --- right/src/config_parser/parse_config.h | 1 + right/src/config_parser/parse_keymap.c | 6 ++++++ 2 files changed, 7 insertions(+) diff --git a/right/src/config_parser/parse_config.h b/right/src/config_parser/parse_config.h index b770e55..117c3a4 100644 --- a/right/src/config_parser/parse_config.h +++ b/right/src/config_parser/parse_config.h @@ -16,6 +16,7 @@ ParserError_InvalidModuleCount, ParserError_InvalidActionCount, ParserError_InvalidSerializedMacroActionType, + ParserError_InvalidSerializedSwitchKeymapAction, } parser_error_t; // Functions: diff --git a/right/src/config_parser/parse_keymap.c b/right/src/config_parser/parse_keymap.c index a5333c8..a43a96a 100644 --- a/right/src/config_parser/parse_keymap.c +++ b/right/src/config_parser/parse_keymap.c @@ -3,6 +3,8 @@ #include "keymaps.h" #include "led_display.h" +uint8_t tempKeymapCount; + static parser_error_t parseNoneAction(key_action_t *keyAction, config_buffer_t *buffer) { keyAction->type = KeyActionType_None; return ParserError_Success; @@ -51,6 +53,9 @@ static parser_error_t parseSwitchLayerAction(key_action_t *KeyAction, config_buf static parser_error_t parseSwitchKeymapAction(key_action_t *keyAction, config_buffer_t *buffer) { uint8_t keymapIndex = readUInt8(buffer); + if (keymapIndex >= tempKeymapCount) { + return ParserError_InvalidSerializedSwitchKeymapAction; + } keyAction->type = KeyActionType_SwitchKeymap; keyAction->switchKeymap.keymapId = keymapIndex; return ParserError_Success; @@ -200,6 +205,7 @@ parser_error_t ParseKeymap(config_buffer_t *buffer, uint8_t keymapIdx, uint8_t k DefaultKeymapIndex = keymapIdx; } } + tempKeymapCount = keymapCount; for (uint16_t layerIdx = 0; layerIdx < layerCount; layerIdx++) { errorCode = parseLayer(buffer, layerIdx); if (errorCode != ParserError_Success) { From b07aba1766fd1bc6a636d79445b14595acb25b2f Mon Sep 17 00:00:00 2001 From: Eric Tang Date: Tue, 8 Aug 2017 12:40:19 -0700 Subject: [PATCH 6/8] Set AllKeymapsCount only once when applying a configuration --- right/src/config_parser/parse_config.c | 3 +++ right/src/config_parser/parse_keymap.c | 1 - 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/right/src/config_parser/parse_config.c b/right/src/config_parser/parse_config.c index 948df44..73b5092 100644 --- a/right/src/config_parser/parse_config.c +++ b/right/src/config_parser/parse_config.c @@ -43,5 +43,8 @@ parser_error_t ParseConfig(config_buffer_t *buffer) { return errorCode; } } + if (!ParserRunDry) { + AllKeymapsCount = keymapCount; + } return ParserError_Success; } diff --git a/right/src/config_parser/parse_keymap.c b/right/src/config_parser/parse_keymap.c index a43a96a..a78e0b3 100644 --- a/right/src/config_parser/parse_keymap.c +++ b/right/src/config_parser/parse_keymap.c @@ -197,7 +197,6 @@ parser_error_t ParseKeymap(config_buffer_t *buffer, uint8_t keymapIdx, uint8_t k return ParserError_InvalidLayerCount; } if (!ParserRunDry) { - AllKeymapsCount = keymapCount; AllKeymaps[keymapIdx].abbreviation = abbreviation; AllKeymaps[keymapIdx].abbreviationLen = abbreviationLen; AllKeymaps[keymapIdx].offset = offset; From b91d8ca472be4d3023a11de0950a2107903ef21e Mon Sep 17 00:00:00 2001 From: Eric Tang Date: Tue, 8 Aug 2017 13:00:08 -0700 Subject: [PATCH 7/8] Add a missing include --- right/src/config_parser/parse_config.c | 1 + 1 file changed, 1 insertion(+) diff --git a/right/src/config_parser/parse_config.c b/right/src/config_parser/parse_config.c index 73b5092..d2b7dad 100644 --- a/right/src/config_parser/parse_config.c +++ b/right/src/config_parser/parse_config.c @@ -1,6 +1,7 @@ #include "parse_config.h" #include "parse_keymap.h" #include "parse_macro.h" +#include "keymaps.h" static parser_error_t parseModuleConfiguration(config_buffer_t *buffer) { uint8_t id = readUInt8(buffer); From aff968f0f9f65fb1a4977c7ae8f0154f92c9d953 Mon Sep 17 00:00:00 2001 From: Eric Tang Date: Tue, 8 Aug 2017 13:00:29 -0700 Subject: [PATCH 8/8] Fix the type of oldKeymapAbbreviation --- right/src/usb_protocol_handler.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/right/src/usb_protocol_handler.c b/right/src/usb_protocol_handler.c index 6f60f01..b39cda0 100644 --- a/right/src/usb_protocol_handler.c +++ b/right/src/usb_protocol_handler.c @@ -88,7 +88,7 @@ void applyConfig(void) { uint8_t *temp; char oldKeymapAbbreviation[3]; - char oldKeymapAbbreviationLen; + uint8_t oldKeymapAbbreviationLen; ParserRunDry = true; StagingUserConfigBuffer.offset = 0;