From 462595ef03f48916e8eff5a2726da4f44882bf1a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A1szl=C3=B3=20Monda?= Date: Thu, 7 Jun 2018 14:50:49 +0200 Subject: [PATCH] Extract handleSwitchLayerAction() --- right/src/usb_report_updater.c | 60 ++++++++++++++++++++-------------- 1 file changed, 36 insertions(+), 24 deletions(-) diff --git a/right/src/usb_report_updater.c b/right/src/usb_report_updater.c index 637ec8b..e96767c 100644 --- a/right/src/usb_report_updater.c +++ b/right/src/usb_report_updater.c @@ -191,21 +191,14 @@ static void processMouseActions() } static layer_id_t previousLayer = LayerId_Base; -static uint8_t basicScancodeIndex = 0; -static uint8_t mediaScancodeIndex = 0; -static uint8_t systemScancodeIndex = 0; -static void applyKeyAction(key_state_t *keyState, key_action_t *action) +static void handleSwitchLayerAction(key_state_t *keyState, key_action_t *action) { static key_state_t *doubleTapSwitchLayerKey; static uint32_t doubleTapSwitchLayerStartTime; static uint32_t doubleTapSwitchLayerTriggerTime; static bool IsLayerDoubleTapToggled; - if (keyState->suppressed) { - return; - } - if (doubleTapSwitchLayerKey && doubleTapSwitchLayerKey != keyState && !keyState->previous) { doubleTapSwitchLayerKey = NULL; } @@ -215,6 +208,40 @@ static void applyKeyAction(key_state_t *keyState, key_action_t *action) IsLayerDoubleTapToggled = false; } + if (action->type != KeyActionType_SwitchLayer) { + return; + } + + if (keyState->previous && doubleTapSwitchLayerKey == keyState && + Timer_GetElapsedTime(&doubleTapSwitchLayerTriggerTime) > DoubleTapSwitchLayerReleaseTimeout) + { + ToggledLayer = LayerId_Base; + } + + if (!keyState->previous && previousLayer == LayerId_Base && action->switchLayer.mode == SwitchLayerMode_HoldAndDoubleTapToggle) { + if (doubleTapSwitchLayerKey && Timer_GetElapsedTimeAndSetCurrent(&doubleTapSwitchLayerStartTime) < DoubleTapSwitchLayerTimeout) { + ToggledLayer = action->switchLayer.layer; + IsLayerDoubleTapToggled = true; + doubleTapSwitchLayerTriggerTime = Timer_GetCurrentTime(); + } else { + doubleTapSwitchLayerKey = keyState; + } + doubleTapSwitchLayerStartTime = Timer_GetCurrentTime(); + } +} + +static uint8_t basicScancodeIndex = 0; +static uint8_t mediaScancodeIndex = 0; +static uint8_t systemScancodeIndex = 0; + +static void applyKeyAction(key_state_t *keyState, key_action_t *action) +{ + if (keyState->suppressed) { + return; + } + + handleSwitchLayerAction(keyState, action); + switch (action->type) { case KeyActionType_Keystroke: ActiveUsbBasicKeyboardReport->modifiers |= action->keystroke.modifiers; @@ -244,22 +271,7 @@ static void applyKeyAction(key_state_t *keyState, key_action_t *action) activeMouseStates[action->mouseAction] = true; break; case KeyActionType_SwitchLayer: - if (keyState->previous && doubleTapSwitchLayerKey == keyState && - Timer_GetElapsedTime(&doubleTapSwitchLayerTriggerTime) > DoubleTapSwitchLayerReleaseTimeout) - { - ToggledLayer = LayerId_Base; - } - - if (!keyState->previous && previousLayer == LayerId_Base && action->switchLayer.mode == SwitchLayerMode_HoldAndDoubleTapToggle) { - if (doubleTapSwitchLayerKey && Timer_GetElapsedTimeAndSetCurrent(&doubleTapSwitchLayerStartTime) < DoubleTapSwitchLayerTimeout) { - ToggledLayer = action->switchLayer.layer; - IsLayerDoubleTapToggled = true; - doubleTapSwitchLayerTriggerTime = Timer_GetCurrentTime(); - } else { - doubleTapSwitchLayerKey = keyState; - } - doubleTapSwitchLayerStartTime = Timer_GetCurrentTime(); - } + // Handled by handleSwitchLayerAction() break; case KeyActionType_SwitchKeymap: if (!keyState->previous) {