Skip to content

Commit

Permalink
Fix Mod-Tap combo regression (qmk#20669)
Browse files Browse the repository at this point in the history
* Add keyevent for combo keyrecord

* Fix formatting

* Update quantum/process_keycode/process_combo.c

Co-authored-by: Sergey Vlasov <[email protected]>

* Add combo unit-tests and hot-fix process_record_tap_hint

...as this function tries to lookup the combo keys passed in. This will
be refactored in a later pr.

---------

Co-authored-by: Sergey Vlasov <[email protected]>
Co-authored-by: Stefan Kerkmann <[email protected]>
  • Loading branch information
3 people authored May 10, 2023
1 parent 6f2a1e4 commit 8a332e6
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 6 deletions.
8 changes: 6 additions & 2 deletions quantum/action.c
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ bool is_swap_hands_on(void) {
*/
void process_hand_swap(keyevent_t *event) {
keypos_t pos = event->key;
if (pos.row < MATRIX_ROWS && pos.col < MATRIX_COLS) {
if (IS_KEYEVENT(*event) && pos.row < MATRIX_ROWS && pos.col < MATRIX_COLS) {
static uint8_t matrix_swap_state[((MATRIX_ROWS * MATRIX_COLS) + (CHAR_BIT)-1) / (CHAR_BIT)];
size_t index = (size_t)(pos.row * MATRIX_COLS) + pos.col;
bool do_swap = should_swap_hands(index, matrix_swap_state, event->pressed);
Expand All @@ -200,7 +200,7 @@ void process_hand_swap(keyevent_t *event) {
}
}
# ifdef ENCODER_MAP_ENABLE
else if (pos.row == KEYLOC_ENCODER_CW || pos.row == KEYLOC_ENCODER_CCW) {
else if (IS_ENCODEREVENT(*event) && pos.row == KEYLOC_ENCODER_CW || pos.row == KEYLOC_ENCODER_CCW) {
static uint8_t encoder_swap_state[((NUM_ENCODERS) + (CHAR_BIT)-1) / (CHAR_BIT)];
size_t index = pos.col;
bool do_swap = should_swap_hands(index, encoder_swap_state, event->pressed);
Expand Down Expand Up @@ -242,6 +242,10 @@ __attribute__((weak)) void post_process_record_quantum(keyrecord_t *record) {}
* FIXME: Needs documentation.
*/
void process_record_tap_hint(keyrecord_t *record) {
if (!IS_KEYEVENT(record->event)) {
return;
}

action_t action = layer_switch_get_action(record->event.key);

switch (action.kind.id) {
Expand Down
10 changes: 6 additions & 4 deletions quantum/action_tapping.c
Original file line number Diff line number Diff line change
Expand Up @@ -167,22 +167,24 @@ bool process_tapping(keyrecord_t *keyp) {

// state machine is in the "reset" state, no tapping key is to be
// processed
if (IS_NOEVENT(tapping_key.event) && IS_EVENT(event)) {
if (event.pressed && is_tap_record(keyp)) {
if (IS_NOEVENT(tapping_key.event)) {
if (!IS_EVENT(event)) {
// early return for tick events
} else if (event.pressed && is_tap_record(keyp)) {
// the currently pressed key is a tapping key, therefore transition
// into the "pressed" tapping key state
ac_dprintf("Tapping: Start(Press tap key).\n");
tapping_key = *keyp;
process_record_tap_hint(&tapping_key);
waiting_buffer_scan_tap();
debug_tapping_key();
return true;
} else {
// the current key is just a regular key, pass it on for regular
// processing
process_record(keyp);
return true;
}

return true;
}

TAP_DEFINE_KEYCODE;
Expand Down
1 change: 1 addition & 0 deletions quantum/process_keycode/process_combo.c
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,7 @@ void apply_combo(uint16_t combo_index, combo_t *combo) {
// this in the end executes the combo when the key_buffer is dumped.
record->keycode = combo->keycode;
record->event.type = COMBO_EVENT;
record->event.key = MAKE_KEYPOS(0, 0);

qrecord->combo_index = combo_index;
ACTIVATE_COMBO(combo);
Expand Down
8 changes: 8 additions & 0 deletions tests/combo/config.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Copyright 2023 Stefan Kerkmann (@KarlK90)
// SPDX-License-Identifier: GPL-2.0-or-later

#pragma once

#include "test_common.h"

#define TAPPING_TERM 200
4 changes: 4 additions & 0 deletions tests/combo/test.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Copyright 2023 Stefan Kerkmann (@KarlK90)
# SPDX-License-Identifier: GPL-2.0-or-later

COMBO_ENABLE = yes
72 changes: 72 additions & 0 deletions tests/combo/test_combo.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// Copyright 2023 Stefan Kerkmann (@KarlK90)
// Copyright 2023 @filterpaper
// SPDX-License-Identifier: GPL-2.0-or-later

#include "keyboard_report_util.hpp"
#include "quantum.h"
#include "keycode.h"
#include "test_common.h"
#include "test_driver.hpp"
#include "test_fixture.hpp"
#include "test_keymap_key.hpp"

extern "C" {
enum combos { modtest, osmshift, COMBO_LENGTH };
uint16_t COMBO_LEN = COMBO_LENGTH;

uint16_t const modtest_combo[] = {KC_Y, KC_U, COMBO_END};
uint16_t const osmshift_combo[] = {KC_Z, KC_X, COMBO_END};

// clang-format off
combo_t key_combos[] = {
[modtest] = COMBO(modtest_combo, RSFT_T(KC_SPACE)),
[osmshift] = COMBO(osmshift_combo, OSM(MOD_LSFT))
};
// clang-format on
}

using testing::_;
using testing::InSequence;

class Combo : public TestFixture {};

TEST_F(Combo, combo_modtest_tapped) {
TestDriver driver;
KeymapKey key_y(0, 0, 1, KC_Y);
KeymapKey key_u(0, 0, 2, KC_U);
set_keymap({key_y, key_u});

EXPECT_REPORT(driver, (KC_SPACE));
EXPECT_EMPTY_REPORT(driver);
tap_combo({key_y, key_u});
VERIFY_AND_CLEAR(driver);
}

TEST_F(Combo, combo_modtest_held_longer_than_tapping_term) {
TestDriver driver;
KeymapKey key_y(0, 0, 1, KC_Y);
KeymapKey key_u(0, 0, 2, KC_U);
set_keymap({key_y, key_u});

EXPECT_REPORT(driver, (KC_RIGHT_SHIFT));
EXPECT_EMPTY_REPORT(driver);
tap_combo({key_y, key_u}, TAPPING_TERM + 1);
VERIFY_AND_CLEAR(driver);
}

TEST_F(Combo, combo_osmshift_tapped) {
TestDriver driver;
KeymapKey key_z(0, 0, 1, KC_Z);
KeymapKey key_x(0, 0, 2, KC_X);
KeymapKey key_i(0, 0, 3, KC_I);
set_keymap({key_z, key_x, key_i});

EXPECT_NO_REPORT(driver);
tap_combo({key_z, key_x});
VERIFY_AND_CLEAR(driver);

EXPECT_REPORT(driver, (KC_I, KC_LEFT_SHIFT));
EXPECT_EMPTY_REPORT(driver);
tap_key(key_i);
VERIFY_AND_CLEAR(driver);
}

0 comments on commit 8a332e6

Please sign in to comment.