Skip to content

Commit

Permalink
Fix Caps Word capitalization when used with Combos + Auto Shift. (qmk…
Browse files Browse the repository at this point in the history
…#17549)

Signed-off-by: Dušan <[email protected]>
  • Loading branch information
getreuer authored and Dušan committed Dec 14, 2022
1 parent f793d0a commit 655d2f8
Show file tree
Hide file tree
Showing 9 changed files with 430 additions and 1 deletion.
7 changes: 6 additions & 1 deletion quantum/process_keycode/process_auto_shift.c
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,12 @@ bool get_autoshift_shift_state(uint16_t keycode) {
/** \brief Restores the shift key if it was cancelled by Auto Shift */
static void autoshift_flush_shift(void) {
autoshift_flags.holding_shift = false;
del_weak_mods(MOD_BIT(KC_LSFT));
# ifdef CAPS_WORD_ENABLE
if (!is_caps_word_on())
# endif // CAPS_WORD_ENABLE
{
del_weak_mods(MOD_BIT(KC_LSFT));
}
if (autoshift_flags.cancelling_lshift) {
autoshift_flags.cancelling_lshift = false;
add_mods(MOD_BIT(KC_LSFT));
Expand Down
4 changes: 4 additions & 0 deletions quantum/process_keycode/process_caps_word.c
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,11 @@ bool process_caps_word(uint16_t keycode, keyrecord_t* record) {
#endif // SWAP_HANDS_ENABLE
}

#ifdef AUTO_SHIFT_ENABLE
del_weak_mods(get_autoshift_state() ? ~MOD_BIT(KC_LSFT) : 0xff);
#else
clear_weak_mods();
#endif // AUTO_SHIFT_ENABLE
if (caps_word_press_user(keycode)) {
send_keyboard_report();
return true;
Expand Down
9 changes: 9 additions & 0 deletions quantum/process_keycode/process_combo.c
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,16 @@ static inline void dump_key_buffer(void) {
#endif
}
record->event.time = 0;

#if defined(CAPS_WORD_ENABLE) && defined(AUTO_SHIFT_ENABLE)
// Edge case: preserve the weak Left Shift mod if both Caps Word and
// Auto Shift are on. Caps Word capitalizes by setting the weak Left
// Shift mod during the press event, but Auto Shift doesn't send the
// key until it receives the release event.
del_weak_mods((is_caps_word_on() && get_autoshift_state()) ? ~MOD_BIT(KC_LSFT) : 0xff);
#else
clear_weak_mods();
#endif // defined(CAPS_WORD_ENABLE) && defined(AUTO_SHIFT_ENABLE)

#if TAP_CODE_DELAY > 0
// only delay once and for a non-tapping key
Expand Down
137 changes: 137 additions & 0 deletions tests/caps_word/caps_word_autoshift/test_caps_word_autoshift.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
// Copyright 2022 Google LLC
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

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

// Allow reports with no keys or only KC_LSFT.
// clang-format off
#define EXPECT_EMPTY_OR_LSFT(driver) \
EXPECT_CALL(driver, send_keyboard_mock(AnyOf( \
KeyboardReport(), \
KeyboardReport(KC_LSFT))))
// clang-format on

using ::testing::_;
using ::testing::AnyNumber;
using ::testing::AnyOf;
using ::testing::InSequence;

class CapsWord : public TestFixture {
public:
void SetUp() override {
caps_word_off();
}
};

// Tests that with Auto Shift, letter keys are shifted by Caps Word
// regardless of whether they are released before AUTO_SHIFT_TIMEOUT.
TEST_F(CapsWord, AutoShiftKeys) {
TestDriver driver;
KeymapKey key_a(0, 0, 0, KC_A);
KeymapKey key_spc(0, 1, 0, KC_SPC);
set_keymap({key_a, key_spc});

EXPECT_EMPTY_OR_LSFT(driver).Times(AnyNumber());
{ // Expect: "A, A, space, a".
InSequence s;
EXPECT_REPORT(driver, (KC_LSFT, KC_A));
EXPECT_REPORT(driver, (KC_LSFT, KC_A));
EXPECT_REPORT(driver, (KC_SPC));
EXPECT_REPORT(driver, (KC_A));
}

// Turn on Caps Word and type "A (quick tap), A (long press), space, A".
caps_word_on();

tap_key(key_a); // Tap A quickly.
tap_key(key_a, AUTO_SHIFT_TIMEOUT + 1); // Long press A.
tap_key(key_spc);
tap_key(key_a);

testing::Mock::VerifyAndClearExpectations(&driver);
}

// Test Caps Word + Auto Shift where keys A and B are rolled.
TEST_F(CapsWord, AutoShiftRolledShiftedKeys) {
TestDriver driver;
KeymapKey key_a(0, 0, 0, KC_A);
KeymapKey key_b(0, 0, 1, KC_B);
set_keymap({key_a, key_b});

EXPECT_EMPTY_OR_LSFT(driver).Times(AnyNumber());
{ // Expect: "A, B, A, B".
InSequence s;
EXPECT_REPORT(driver, (KC_LSFT, KC_A));
EXPECT_REPORT(driver, (KC_LSFT, KC_B));
EXPECT_REPORT(driver, (KC_LSFT, KC_A));
EXPECT_REPORT(driver, (KC_LSFT, KC_B));
}

caps_word_on();

key_a.press(); // Overlapping taps: A down, B down, A up, B up.
run_one_scan_loop();
key_b.press();
run_one_scan_loop();
key_a.release();
run_one_scan_loop();
key_b.release();
run_one_scan_loop();

key_a.press(); // Nested taps: A down, B down, B up, A up.
run_one_scan_loop();
key_b.press();
run_one_scan_loop();
key_b.release();
run_one_scan_loop();
key_a.release();
run_one_scan_loop();

caps_word_off();
testing::Mock::VerifyAndClearExpectations(&driver);
}

// Tests that with tap-hold keys with Retro Shift, letter keys are shifted by
// Caps Word regardless of whether they are retroshifted.
TEST_F(CapsWord, RetroShiftKeys) {
TestDriver driver;
KeymapKey key_modtap_a(0, 0, 0, LCTL_T(KC_A));
KeymapKey key_layertap_b(0, 1, 0, LT(1, KC_B));
set_keymap({key_modtap_a, key_layertap_b});

EXPECT_EMPTY_OR_LSFT(driver).Times(AnyNumber());
{ // Expect: "B, A, B, A".
InSequence s;
EXPECT_REPORT(driver, (KC_LSFT, KC_B));
EXPECT_REPORT(driver, (KC_LSFT, KC_A));
EXPECT_REPORT(driver, (KC_LSFT, KC_B));
EXPECT_REPORT(driver, (KC_LSFT, KC_A));
}

// Turn on Caps Word and type "B, A (long press), B (long press), A".
caps_word_on();

tap_key(key_layertap_b); // Tap B quickly.
tap_key(key_modtap_a, TAPPING_TERM + 1); // Long press A.
tap_key(key_layertap_b, TAPPING_TERM + 1); // Long press B.
tap_key(key_modtap_a); // Tap A quickly.

EXPECT_EQ(is_caps_word_on(), true);
testing::Mock::VerifyAndClearExpectations(&driver);
}
20 changes: 20 additions & 0 deletions tests/caps_word/caps_word_combo/config.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright 2022 Google LLC
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

#pragma once

#include "test_common.h"

#define TAPPING_TERM 200
19 changes: 19 additions & 0 deletions tests/caps_word/caps_word_combo/test.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Copyright 2022 Google LLC
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

CAPS_WORD_ENABLE = yes
COMBO_ENABLE = yes
AUTO_SHIFT_ENABLE = yes

Loading

0 comments on commit 655d2f8

Please sign in to comment.