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

Port keystroke delay. #358

Merged
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
7 changes: 7 additions & 0 deletions right/src/postponer.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@ bool PostponerCore_IsActive(void)
void PostponerCore_TrackKeyEvent(key_state_t *keyState, bool active)
{
uint8_t pos = POS(bufferSize);

//if the buffer is totally filled, at least make sure the key doesn't get stuck
if (bufferSize == POSTPONER_BUFFER_SIZE) {
buffer[pos].key->current = buffer[bufferPosition].active;
consumeEvent(1);
}

buffer[pos] = (struct postponer_buffer_record_type_t) {
.key = keyState,
.active = active,
Expand Down
31 changes: 31 additions & 0 deletions right/src/usb_report_updater.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ static key_action_t actionCache[SLOT_COUNT][MAX_KEY_COUNT_PER_MODULE];

volatile uint8_t UsbReportUpdateSemaphore = 0;

static uint16_t keystrokeDelay = 0;

// Holds are applied on current base layer.
static void applyLayerHolds(key_state_t *keyState, key_action_t *action) {
if (action->type == KeyActionType_SwitchLayer && KeyState_Active(keyState)) {
Expand Down Expand Up @@ -381,11 +383,34 @@ static void updateActiveUsbReports(void)
ActiveUsbBasicKeyboardReport->modifiers |= stickyModifiers;
}

void justPreprocessInput(void) {
// Make preprocessKeyState push new events into postponer queue.
// As a side-effect, postpone first cycle after we switch back to regular update loop
PostponerCore_PostponeNCycles(0);
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];

preprocessKeyState(keyState);
}
}

for (uint8_t moduleSlotId=0; moduleSlotId<UHK_MODULE_MAX_SLOT_COUNT; moduleSlotId++) {
uhk_module_state_t *moduleState = UhkModuleStates + moduleSlotId;
if (moduleState->moduleId == ModuleId_Unavailable || moduleState->pointerCount == 0) {
continue;
}
moduleState->pointerDelta.x = 0;
moduleState->pointerDelta.y = 0;
}
}

uint32_t UsbReportUpdateCounter;

void UpdateUsbReports(void)
{
static uint32_t lastUpdateTime;
static uint32_t lastReportTime;

for (uint8_t keyId = 0; keyId < RIGHT_KEY_MATRIX_KEY_COUNT; keyId++) {
KeyStates[SlotId_RightKeyboardHalf][keyId].hardwareSwitchState = RightKeyMatrix.keyStates[keyId];
Expand All @@ -399,6 +424,11 @@ void UpdateUsbReports(void)
}
}

if(Timer_GetElapsedTime(&lastReportTime) < keystrokeDelay) {
justPreprocessInput();
return;
}

lastUpdateTime = CurrentTime;
UsbReportUpdateCounter++;

Expand All @@ -423,6 +453,7 @@ void UpdateUsbReports(void)
//TODO: consider either making it atomic, or lowering semaphore reset delay
UsbReportUpdateSemaphore &= ~(1 << USB_BASIC_KEYBOARD_INTERFACE_INDEX);
}
lastReportTime = CurrentTime;
}

if (HasUsbMediaKeyboardReportChanged) {
Expand Down