Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Stuck keys fixes #820

Merged
merged 3 commits into from
Dec 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion right/src/event_scheduler.c
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ void EventVector_ReportMask(const char* prefix, uint32_t mask) {
REPORT_MASK(MouseKeysReportsUsed);
REPORT_MASK(MouseControllerMouseReportsUsed);
REPORT_MASK(MouseControllerKeyboardReportsUsed);
REPORT_MASK(ReportsChanged);
REPORT_MASK(SendUsbReports);
REPORT_MASK(NativeActionsPostponing);
REPORT_MASK(MacroEnginePostponing);

Expand Down
15 changes: 7 additions & 8 deletions right/src/event_scheduler.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@
EventVector_MouseController = 1 << 4,
EventVector_Postponer = 1 << 5,
EventVector_LayerHolds = 1 << 6,
EventVector_ZeroScan = 1 << 7,
EventVector_EventScheduler = 1 << 8,
EventVector_EventScheduler = 1 << 7,
EventVector_MainTriggers = ((EventVector_EventScheduler << 1) - 1),


// some other minor triggers
Expand All @@ -63,24 +63,23 @@
EventVector_LedMapUpdateNeeded = 1 << 15,
EventVector_ApplyConfig = 1 << 16,
EventVector_NewMessage = 1 << 17,

EventVector_ReportUpdateMask = ((1 << 9) - 1) & ~EventVector_EventScheduler,
EventVector_UserLogicUpdateMask = ((1 << 18) - 1) & ~EventVector_EventScheduler,
EventVector_AuxiliaryTriggers = ((EventVector_NewMessage << 1) - 1),

// events that are informational only
EventVector_NativeActionReportsUsed = 1 << 18,
EventVector_MacroReportsUsed = 1 << 19,
EventVector_MouseKeysReportsUsed = 1 << 20,
EventVector_MouseControllerMouseReportsUsed = 1 << 21,
EventVector_MouseControllerKeyboardReportsUsed = 1 << 22,
EventVector_ReportsChanged = 1 << 23,
EventVector_SendUsbReports = 1 << 23,
EventVector_NativeActionsPostponing = 1 << 24,
EventVector_MacroEnginePostponing = 1 << 25,
EventVector_MouseControllerPostponing = 1 << 26,

// helpers
// helper masks
EventVector_ReportUpdateMask = EventVector_MainTriggers & ~EventVector_EventScheduler,
EventVector_UserLogicUpdateMask = EventVector_AuxiliaryTriggers & ~EventVector_EventScheduler,
EventVector_SomeonePostponing = EventVector_NativeActionsPostponing | EventVector_MacroEnginePostponing | EventVector_MouseControllerPostponing,

} event_vector_event_t;

/**
Expand Down
4 changes: 2 additions & 2 deletions right/src/macros/core.c
Original file line number Diff line number Diff line change
Expand Up @@ -624,7 +624,7 @@ static void executePreemptive(void)

if (res & MacroResult_BlockingFlag) {
SchedulerPostponing = true;
EventVector_Set(EventVector_ReportsChanged);
EventVector_Set(EventVector_SendUsbReports);
}

if (S->ms.macroInterrupted) {
Expand Down Expand Up @@ -784,7 +784,7 @@ static void executeBlocking(void)

if ((someoneBlocking = (res & MacroResult_BlockingFlag))) {
SchedulerPostponing = true;
EventVector_Set(EventVector_ReportsChanged);
EventVector_Set(EventVector_SendUsbReports);
break;
}

Expand Down
2 changes: 1 addition & 1 deletion right/src/mouse_controller.c
Original file line number Diff line number Diff line change
Expand Up @@ -777,7 +777,7 @@ void MouseController_ProcessMouseActions()
EventVector_SetValue(EventVector_MouseControllerMouseReportsUsed, mouseReportsUsed);

if (keyboardReportsUsed || mouseReportsUsed || caretModeActionWasRunningSomewhere || modsChanged) {
EventVector_Set(EventVector_ReportsChanged);
EventVector_Set(EventVector_SendUsbReports);
}
if (!caretModeActionIsRunningSomewhere) {
EventVector_Unset(EventVector_MouseControllerPostponing);
Expand Down
4 changes: 2 additions & 2 deletions right/src/mouse_keys.c
Original file line number Diff line number Diff line change
Expand Up @@ -210,13 +210,13 @@ void MouseKeys_ProcessMouseActions()
EventVector_Set(EventVector_MouseKeysReportsUsed);
}
if (wasMoving) {
EventVector_Set(EventVector_MouseKeys | EventVector_ReportsChanged);
EventVector_Set(EventVector_MouseKeys | EventVector_SendUsbReports);
}
}

void MouseKeys_SetState(serialized_mouse_action_t action, bool lock, bool activate)
{
EventVector_Set(EventVector_MouseKeys | EventVector_ReportsChanged);
EventVector_Set(EventVector_MouseKeys | EventVector_SendUsbReports);
if (activate) {
if (lock) {
EventVector_Set(EventVector_NativeActions);
Expand Down
2 changes: 2 additions & 0 deletions right/src/usb_interfaces/usb_interface_basic_keyboard.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <string.h>
#include "event_scheduler.h"
#include "led_display.h"
#include "lufa/HIDClassCommon.h"
#include "macros/core.h"
Expand Down Expand Up @@ -206,6 +207,7 @@ void UsbBasicKeyboardSendActiveReport(void)
//This is *not* asynchronously safe as long as multiple reports of different type can be sent at the same time.
//TODO: consider either making it atomic, or lowering semaphore reset delay
UsbReportUpdateSemaphore &= ~(1 << USB_BASIC_KEYBOARD_INTERFACE_INDEX);
EventVector_Set(EventVector_SendUsbReports);
}
#endif
}
Expand Down
2 changes: 2 additions & 0 deletions right/src/usb_interfaces/usb_interface_mouse.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "usb_composite_device.h"
#include "usb_report_updater.h"
#include <string.h>
#include "event_scheduler.h"

#ifdef __ZEPHYR__
#include "usb/usb_compatibility.h"
Expand Down Expand Up @@ -167,6 +168,7 @@ void UsbMouseSendActiveReport(void)
usb_status_t status = UsbMouseAction();
if (status != kStatus_USB_Success) {
UsbReportUpdateSemaphore &= ~(1 << USB_MOUSE_INTERFACE_INDEX);
EventVector_Set(EventVector_SendUsbReports);
}
#endif
}
Expand Down
45 changes: 28 additions & 17 deletions right/src/usb_report_updater.c
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ static void resetStickyMods(key_action_cached_t *cachedAction)
{
StickyModifiers = 0;
StickyModifiersNegative = cachedAction->modifierLayerMask;
EventVector_Set(EventVector_ReportsChanged);
EventVector_Set(EventVector_SendUsbReports);
}

static void activateStickyMods(key_state_t *keyState, key_action_cached_t *action)
Expand All @@ -230,7 +230,7 @@ static void activateStickyMods(key_state_t *keyState, key_action_cached_t *actio
StickyModifiers = action->action.keystroke.modifiers;
stickyModifierKey = keyState;
stickyModifierShouldStick = shouldStickAction(&action->action);
EventVector_Set(EventVector_ReportsChanged);
EventVector_Set(EventVector_SendUsbReports);
}

void ActivateStickyMods(key_state_t *keyState, uint8_t mods)
Expand All @@ -239,12 +239,12 @@ void ActivateStickyMods(key_state_t *keyState, uint8_t mods)
StickyModifiers = mods;
stickyModifierKey = keyState;
stickyModifierShouldStick = ActiveLayerHeld;
EventVector_Set(EventVector_ReportsChanged);
EventVector_Set(EventVector_SendUsbReports);
}

static void applyKeystrokePrimary(key_state_t *keyState, key_action_cached_t *cachedAction, usb_keyboard_reports_t* reports)
{
EventVector_Set(EventVector_ReportsChanged);
EventVector_Set(EventVector_SendUsbReports);

if (KeyState_Active(keyState)) {
key_action_t* action = &cachedAction->action;
Expand All @@ -259,7 +259,7 @@ static void applyKeystrokePrimary(key_state_t *keyState, key_action_cached_t *ca
} else {
if (action->keystroke.modifiers) {
reports->inputModifiers |= action->keystroke.modifiers;
EventVector_Set(EventVector_ReportsChanged | reports->reportsUsedVectorMask);
EventVector_Set(reports->reportsUsedVectorMask);
}
}
// If there are mods: first cycle send just mods, in next cycle start sending mods+scancode
Expand Down Expand Up @@ -288,7 +288,7 @@ static void applyKeystrokePrimary(key_state_t *keyState, key_action_cached_t *ca
StickyModifiersNegative = 0;
EventVector_Set(
reports->recomputeStateVectorMask | // trigger next update in order to clear them, usually EventVector_NativeActions here
EventVector_ReportsChanged | reports->reportsUsedVectorMask |
reports->reportsUsedVectorMask |
reports->postponeMask
);
}
Expand All @@ -310,7 +310,7 @@ static void applyKeystrokeSecondary(key_state_t *keyState, key_action_t *action,
} else if (IS_SECONDARY_ROLE_MODIFIER(secondaryRole)) {
if (KeyState_Active(keyState)) {
reports->inputModifiers |= SECONDARY_ROLE_MODIFIER_TO_HID_MODIFIER(secondaryRole);
EventVector_Set(EventVector_ReportsChanged | reports->reportsUsedVectorMask);
EventVector_Set(EventVector_SendUsbReports | reports->reportsUsedVectorMask);
}
}
}
Expand Down Expand Up @@ -512,7 +512,7 @@ static void handleUsbStackTestMode() {
Cfg.MouseMoveState.xOut = isEven ? -5 : 5;
}
EventVector_Set(EventVector_NativeActions);
EventVector_Set(EventVector_ReportsChanged);
EventVector_Set(EventVector_SendUsbReports);
EventVector_Set(EventVector_NativeActionReportsUsed);
}
}
Expand All @@ -527,7 +527,7 @@ static void handleLayerChanges() {
previousLayer = ActiveLayer;
StickyModifiers = 0;
StickyModifiersNegative = 0;
EventVector_Set(EventVector_ReportsChanged);
EventVector_Set(EventVector_SendUsbReports);
}
}

Expand Down Expand Up @@ -602,13 +602,12 @@ static void updateActionStates() {
}
uint8_t currentMods = NativeKeyboardReports.basic.modifiers | NativeKeyboardReports.inputModifiers;
if (currentMods != previousMods) {
EventVector_Set(EventVector_ReportsChanged);
EventVector_Set(EventVector_SendUsbReports);
}
}

static void updateActiveUsbReports(void)
{
EventVector_Unset(EventVector_ReportsChanged);
InputModifiersPrevious = InputModifiers;
OutputModifiers = 0;
static bool lastSomeonePostponing = false;
Expand Down Expand Up @@ -678,6 +677,9 @@ static void sendActiveReports() {
bool usbReportsChangedByAction = false;
bool usbReportsChangedByAnything = false;

// in case of usb error, this gets set back again
EventVector_Unset(EventVector_SendUsbReports);

if (UsbBasicKeyboardCheckReportReady() == kStatus_USB_Success) {
#ifdef __ZEPHYR__
if (InputInterceptor_RegisterReport(ActiveUsbBasicKeyboardReport)) {
Expand Down Expand Up @@ -718,6 +720,7 @@ static void sendActiveReports() {
usb_status_t status = UsbMediaKeyboardAction();
if (status != kStatus_USB_Success) {
UsbReportUpdateSemaphore &= ~(1 << USB_MEDIA_KEYBOARD_INTERFACE_INDEX);
EventVector_Set(EventVector_SendUsbReports);
}
UsbReportUpdater_LastActivityTime = CurrentTime;
usbReportsChangedByAction = true;
Expand All @@ -729,6 +732,7 @@ static void sendActiveReports() {
usb_status_t status = UsbSystemKeyboardAction();
if (status != kStatus_USB_Success) {
UsbReportUpdateSemaphore &= ~(1 << USB_SYSTEM_KEYBOARD_INTERFACE_INDEX);
EventVector_Set(EventVector_SendUsbReports);
}
UsbReportUpdater_LastActivityTime = CurrentTime;
usbReportsChangedByAction = true;
Expand All @@ -747,7 +751,12 @@ static void sendActiveReports() {
if (usbReportsChangedByAction) {
Macros_SignalUsbReportsChange();
}
EventVector_SetValue(EventVector_ZeroScan, usbReportsChangedByAnything);

// If anything changed, trigger one more update to send zero reports
// TODO: consider doing this depending on change of ReportsUsed mask(s) and actual module scans
if (usbReportsChangedByAnything) {
EventVector_Set(EventVector_SendUsbReports);
}
}

void UpdateUsbReports(void)
Expand All @@ -766,11 +775,13 @@ void UpdateUsbReports(void)

updateActiveUsbReports();

if (EventVector_IsSet(EventVector_ReportsChanged | EventVector_ZeroScan) && CurrentPowerMode < PowerMode_DeepSleep) {
mergeReports();
sendActiveReports();
} else {
EventVector_Unset(EventVector_ReportsChanged);
if (EventVector_IsSet(EventVector_SendUsbReports)) {
if (CurrentPowerMode < PowerMode_DeepSleep) {
mergeReports();
sendActiveReports();
} else {
EventVector_Unset(EventVector_SendUsbReports);
}
}

if (DisplaySleepModeActive || KeyBacklightSleepModeActive) {
Expand Down