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

Fix key stucks caused by (high) keystroke delays. #1028

Merged
merged 2 commits into from
Dec 15, 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
39 changes: 20 additions & 19 deletions right/src/event_scheduler.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,38 +49,39 @@
EventVector_MouseController = 1 << 4,
EventVector_Postponer = 1 << 5,
EventVector_LayerHolds = 1 << 6,
EventVector_EventScheduler = 1 << 7,
EventVector_SendUsbReports = 1 << 7,
EventVector_ResendUsbReports = 1 << 8,
EventVector_EventScheduler = 1 << 9,
EventVector_MainTriggers = ((EventVector_EventScheduler << 1) - 1),


// some other minor triggers
EventVector_KeyboardLedState = 1 << 9,
EventVector_UsbMacroCommandWaitingForExecution = 1 << 10,
EventVector_ProtocolChanged = 1 << 11,
EventVector_LedManagerFullUpdateNeeded = 1 << 12,
EventVector_KeymapReloadNeeded = 1 << 13,
EventVector_SegmentDisplayNeedsUpdate = 1 << 14,
EventVector_LedMapUpdateNeeded = 1 << 15,
EventVector_ApplyConfig = 1 << 16,
EventVector_NewMessage = 1 << 17,
EventVector_KeyboardLedState = 1 << 10,
EventVector_UsbMacroCommandWaitingForExecution = 1 << 11,
EventVector_ProtocolChanged = 1 << 12,
EventVector_LedManagerFullUpdateNeeded = 1 << 13,
EventVector_KeymapReloadNeeded = 1 << 14,
EventVector_SegmentDisplayNeedsUpdate = 1 << 15,
EventVector_LedMapUpdateNeeded = 1 << 16,
EventVector_ApplyConfig = 1 << 17,
EventVector_NewMessage = 1 << 18,
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_SendUsbReports = 1 << 23,
EventVector_ResendUsbReports = 1 << 24,
EventVector_NativeActionsPostponing = 1 << 25,
EventVector_NativeActionReportsUsed = 1 << 19,
EventVector_MacroReportsUsed = 1 << 20,
EventVector_MouseKeysReportsUsed = 1 << 21,
EventVector_MouseControllerMouseReportsUsed = 1 << 22,
EventVector_MouseControllerKeyboardReportsUsed = 1 << 23,
EventVector_NativeActionsPostponing = 1 << 24,
EventVector_KeystrokeDelayPostponing = 1 << 25,
EventVector_MacroEnginePostponing = 1 << 26,
EventVector_MouseControllerPostponing = 1 << 27,

// helper masks
EventVector_ReportUpdateMask = EventVector_MainTriggers & ~EventVector_EventScheduler,
EventVector_UserLogicUpdateMask = EventVector_AuxiliaryTriggers & ~EventVector_EventScheduler,
EventVector_SomeonePostponing = EventVector_NativeActionsPostponing | EventVector_MacroEnginePostponing | EventVector_MouseControllerPostponing,
EventVector_SomeonePostponing = EventVector_KeystrokeDelayPostponing | EventVector_NativeActionsPostponing | EventVector_MacroEnginePostponing | EventVector_MouseControllerPostponing,
} event_vector_event_t;

/**
Expand Down
2 changes: 1 addition & 1 deletion right/src/postponer.c
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ static void appendEvent(postponer_event_t event)

bool PostponerCore_EventsShouldBeQueued(void)
{
return runState.eventsShouldBeQueued || (EventScheduler_Vector & EventVector_NativeActionsPostponing) || (EventScheduler_Vector & EventVector_MacroEnginePostponing);
return runState.eventsShouldBeQueued || (EventScheduler_Vector & (EventVector_KeystrokeDelayPostponing | EventVector_NativeActionsPostponing | EventVector_MacroEnginePostponing /* should here be mouse controller too? */));
}

bool PostponerCore_IsActive(void)
Expand Down
29 changes: 24 additions & 5 deletions right/src/usb_report_updater.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <math.h>
#include "atomicity.h"
#include "event_scheduler.h"
#include "key_action.h"
#include "led_display.h"
Expand Down Expand Up @@ -656,9 +657,6 @@ static void updateActiveUsbReports(void)
}

void justPreprocessInput(void) {
// Make preprocessKeyState push new events into postponer queue.
EventVector_Set(EventVector_NativeActionsPostponing);

for (uint8_t slotId=0; slotId<SLOT_COUNT; slotId++) {
for (uint8_t keyId=0; keyId<MAX_KEY_COUNT_PER_MODULE; keyId++) {
key_state_t *keyState = &KeyStates[slotId][keyId];
Expand Down Expand Up @@ -759,10 +757,31 @@ static void sendActiveReports() {
}
}

static bool blockedByKeystrokeDelay() {
static uint32_t postponedMasks = 0;
if (CurrentTime < lastBasicReportTime + Cfg.KeystrokeDelay) {
DISABLE_IRQ();
postponedMasks |= EventScheduler_Vector & EventVector_MainTriggers;
EventScheduler_Vector = (EventScheduler_Vector & ~EventVector_MainTriggers) | EventVector_KeystrokeDelayPostponing;
ENABLE_IRQ();

// Make sure to wake up postponer so that it can process the events.
EventScheduler_Reschedule(lastBasicReportTime + Cfg.KeystrokeDelay, EventSchedulerEvent_Postponer, "keystroke delay");

justPreprocessInput();
return true;
} else if (postponedMasks) {
DISABLE_IRQ();
EventScheduler_Vector = (EventScheduler_Vector & ~EventVector_KeystrokeDelayPostponing) | postponedMasks;
postponedMasks = 0;
ENABLE_IRQ();
}
return false;
}

void UpdateUsbReports(void)
{
if (Timer_GetElapsedTime(&lastBasicReportTime) < Cfg.KeystrokeDelay) {
justPreprocessInput();
if (blockedByKeystrokeDelay()) {
return;
}

Expand Down