Skip to content

Commit

Permalink
Port keystroke delay. (#358)
Browse files Browse the repository at this point in the history
* Port keystroke delay.

* Fix keystroke delay.

* Make postponer overfill reset keys.

* Reset pointer deltas during keystroke delay pauses.
  • Loading branch information
kareltucek authored May 2, 2021
1 parent e973842 commit 21594d1
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
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

0 comments on commit 21594d1

Please sign in to comment.