summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPascal Getreuer <50221757+getreuer@users.noreply.github.com>2023-04-03 16:11:26 -0700
committerGitHub <noreply@github.com>2023-04-04 09:11:26 +1000
commitae63c0f509fae71270fb5885d504ee26cbad95ff (patch)
treeddb9844e52d36bbcdb144e20ad349031fbdee034
parent368fee9655b8c0611808212b680674b5acd4349c (diff)
[Core] Caps Word "Invert on shift" option: pressing Shift inverts the shift state. (#20092)
Co-authored-by: Nick Brassel <nick@tzarc.org>
-rw-r--r--data/mappings/info_config.hjson1
-rw-r--r--data/schemas/keyboard.jsonschema3
-rw-r--r--docs/feature_caps_word.md20
-rw-r--r--quantum/process_keycode/process_caps_word.c66
-rw-r--r--tests/caps_word/caps_word_invert_on_shift/config.h21
-rw-r--r--tests/caps_word/caps_word_invert_on_shift/test.mk17
-rw-r--r--tests/caps_word/caps_word_invert_on_shift/test_caps_word_invert_on_shift.cpp215
7 files changed, 342 insertions, 1 deletions
diff --git a/data/mappings/info_config.hjson b/data/mappings/info_config.hjson
index bc4f46c353..7c1a4ee36b 100644
--- a/data/mappings/info_config.hjson
+++ b/data/mappings/info_config.hjson
@@ -28,6 +28,7 @@
28 "BOOTMAGIC_LITE_COLUMN_RIGHT": {"info_key": "split.bootmagic.matrix.1", "value_type": "int"}, 28 "BOOTMAGIC_LITE_COLUMN_RIGHT": {"info_key": "split.bootmagic.matrix.1", "value_type": "int"},
29 "BOTH_SHIFTS_TURNS_ON_CAPS_WORD": {"info_key": "caps_word.both_shifts_turns_on", "value_type": "bool"}, 29 "BOTH_SHIFTS_TURNS_ON_CAPS_WORD": {"info_key": "caps_word.both_shifts_turns_on", "value_type": "bool"},
30 "CAPS_WORD_IDLE_TIMEOUT": {"info_key": "caps_word.idle_timeout", "value_type": "int"}, 30 "CAPS_WORD_IDLE_TIMEOUT": {"info_key": "caps_word.idle_timeout", "value_type": "int"},
31 "CAPS_WORD_INVERT_ON_SHIFT": {"info_key": "caps_word.invert_on_shift", "value_type": "bool"},
31 "COMBO_COUNT": {"info_key": "combo.count", "value_type": "int"}, 32 "COMBO_COUNT": {"info_key": "combo.count", "value_type": "int"},
32 "COMBO_TERM": {"info_key": "combo.term", "value_type": "int"}, 33 "COMBO_TERM": {"info_key": "combo.term", "value_type": "int"},
33 "DEBOUNCE": {"info_key": "debounce", "value_type": "int"}, 34 "DEBOUNCE": {"info_key": "debounce", "value_type": "int"},
diff --git a/data/schemas/keyboard.jsonschema b/data/schemas/keyboard.jsonschema
index ee6ecf28e4..6c4ff49855 100644
--- a/data/schemas/keyboard.jsonschema
+++ b/data/schemas/keyboard.jsonschema
@@ -227,7 +227,8 @@
227 "enabled": {"type": "boolean"}, 227 "enabled": {"type": "boolean"},
228 "both_shifts_turns_on": {"type": "boolean"}, 228 "both_shifts_turns_on": {"type": "boolean"},
229 "double_tap_shift_turns_on": {"type": "boolean"}, 229 "double_tap_shift_turns_on": {"type": "boolean"},
230 "idle_timeout": {"$ref": "qmk.definitions.v1#/unsigned_int"} 230 "idle_timeout": {"$ref": "qmk.definitions.v1#/unsigned_int"},
231 "invert_on_shift": {"type": "boolean"}
231 } 232 }
232 }, 233 },
233 "combo": { 234 "combo": {
diff --git a/docs/feature_caps_word.md b/docs/feature_caps_word.md
index c58d1a56e2..7f726b059d 100644
--- a/docs/feature_caps_word.md
+++ b/docs/feature_caps_word.md
@@ -90,6 +90,26 @@ by defining `IS_COMMAND()` in config.h:
90 90
91## Customizing Caps Word :id=customizing-caps-word 91## Customizing Caps Word :id=customizing-caps-word
92 92
93### Invert on shift :id=invert-on-shift
94
95By default, Caps Word turns off when Shift keys are pressed, considering them as
96word-breaking. Alternatively with the `CAPS_WORD_INVERT_ON_SHIFT` option,
97pressing the Shift key continues Caps Word and inverts the shift state. This
98is convenient for uncapitalizing one or a few letters within a word, for
99example with Caps Word on, typing "D, B, Shift+A, Shift+A, S" produces "DBaaS",
100or typing "P, D, F, Shift+S" produces "PDFs".
101
102Enable it by adding in config.h
103
104```c
105#define CAPS_WORD_INVERT_ON_SHIFT
106```
107
108This option works with regular Shift keys `KC_LSFT` and `KC_RSFT`, mod-tap Shift
109keys, and one-shot Shift keys. Note that while Caps Word is on, one-shot Shift
110keys behave like regular Shift keys, and have effect only while they are held.
111
112
93### Idle timeout :id=idle-timeout 113### Idle timeout :id=idle-timeout
94 114
95Caps Word turns off automatically if no keys are pressed for 115Caps Word turns off automatically if no keys are pressed for
diff --git a/quantum/process_keycode/process_caps_word.c b/quantum/process_keycode/process_caps_word.c
index 94302b29ae..8f2ee1db8b 100644
--- a/quantum/process_keycode/process_caps_word.c
+++ b/quantum/process_keycode/process_caps_word.c
@@ -14,6 +14,54 @@
14 14
15#include "process_caps_word.h" 15#include "process_caps_word.h"
16 16
17#ifdef CAPS_WORD_INVERT_ON_SHIFT
18static uint8_t held_mods = 0;
19
20static bool handle_shift(uint16_t keycode, keyrecord_t* record) {
21 switch (keycode) {
22 case OSM(MOD_LSFT):
23 keycode = KC_LSFT;
24 break;
25 case OSM(MOD_RSFT):
26 keycode = KC_RSFT;
27 break;
28
29# ifndef NO_ACTION_TAPPING
30 case QK_MOD_TAP ... QK_MOD_TAP_MAX:
31 if (record->tap.count == 0) { // Mod-tap key is held.
32 switch (QK_MOD_TAP_GET_MODS(keycode)) {
33 case MOD_LSFT:
34 keycode = KC_LSFT;
35 break;
36 case MOD_RSFT:
37 keycode = KC_RSFT;
38 break;
39 }
40 }
41# endif // NO_ACTION_TAPPING
42 }
43
44 if (keycode == KC_LSFT || keycode == KC_RSFT) {
45 const uint8_t mod = MOD_BIT(keycode);
46
47 if (is_caps_word_on()) {
48 if (record->event.pressed) {
49 held_mods |= mod;
50 } else {
51 held_mods &= ~mod;
52 }
53 return false;
54 } else if ((held_mods & mod) != 0) {
55 held_mods &= ~mod;
56 del_mods(mod);
57 return record->event.pressed;
58 }
59 }
60
61 return true;
62}
63#endif // CAPS_WORD_INVERT_ON_SHIFT
64
17bool process_caps_word(uint16_t keycode, keyrecord_t* record) { 65bool process_caps_word(uint16_t keycode, keyrecord_t* record) {
18 if (keycode == QK_CAPS_WORD_TOGGLE) { 66 if (keycode == QK_CAPS_WORD_TOGGLE) {
19 if (record->event.pressed) { 67 if (record->event.pressed) {
@@ -21,6 +69,11 @@ bool process_caps_word(uint16_t keycode, keyrecord_t* record) {
21 } 69 }
22 return false; 70 return false;
23 } 71 }
72#ifdef CAPS_WORD_INVERT_ON_SHIFT
73 if (!handle_shift(keycode, record)) {
74 return false;
75 }
76#endif // CAPS_WORD_INVERT_ON_SHIFT
24 77
25#ifndef NO_ACTION_ONESHOT 78#ifndef NO_ACTION_ONESHOT
26 const uint8_t mods = get_mods() | get_oneshot_mods(); 79 const uint8_t mods = get_mods() | get_oneshot_mods();
@@ -111,12 +164,14 @@ bool process_caps_word(uint16_t keycode, keyrecord_t* record) {
111 if (record->tap.count == 0) { // Mod-tap key is held. 164 if (record->tap.count == 0) { // Mod-tap key is held.
112 const uint8_t mods = QK_MOD_TAP_GET_MODS(keycode); 165 const uint8_t mods = QK_MOD_TAP_GET_MODS(keycode);
113 switch (mods) { 166 switch (mods) {
167# ifndef CAPS_WORD_INVERT_ON_SHIFT
114 case MOD_LSFT: 168 case MOD_LSFT:
115 keycode = KC_LSFT; 169 keycode = KC_LSFT;
116 break; 170 break;
117 case MOD_RSFT: 171 case MOD_RSFT:
118 keycode = KC_RSFT; 172 keycode = KC_RSFT;
119 break; 173 break;
174# endif // CAPS_WORD_INVERT_ON_SHIFT
120 case MOD_RSFT | MOD_RALT: 175 case MOD_RSFT | MOD_RALT:
121 keycode = RSFT(KC_RALT); 176 keycode = RSFT(KC_RALT);
122 break; 177 break;
@@ -124,6 +179,9 @@ bool process_caps_word(uint16_t keycode, keyrecord_t* record) {
124 return true; 179 return true;
125 default: 180 default:
126 caps_word_off(); 181 caps_word_off();
182# ifdef CAPS_WORD_INVERT_ON_SHIFT
183 add_mods(held_mods);
184# endif // CAPS_WORD_INVERT_ON_SHIFT
127 return true; 185 return true;
128 } 186 }
129 } else { 187 } else {
@@ -163,12 +221,20 @@ bool process_caps_word(uint16_t keycode, keyrecord_t* record) {
163 clear_weak_mods(); 221 clear_weak_mods();
164#endif // AUTO_SHIFT_ENABLE 222#endif // AUTO_SHIFT_ENABLE
165 if (caps_word_press_user(keycode)) { 223 if (caps_word_press_user(keycode)) {
224#ifdef CAPS_WORD_INVERT_ON_SHIFT
225 if (held_mods) {
226 set_weak_mods(get_weak_mods() ^ MOD_BIT(KC_LSFT));
227 }
228#endif // CAPS_WORD_INVERT_ON_SHIFT
166 send_keyboard_report(); 229 send_keyboard_report();
167 return true; 230 return true;
168 } 231 }
169 } 232 }
170 233
171 caps_word_off(); 234 caps_word_off();
235#ifdef CAPS_WORD_INVERT_ON_SHIFT
236 add_mods(held_mods);
237#endif // CAPS_WORD_INVERT_ON_SHIFT
172 return true; 238 return true;
173} 239}
174 240
diff --git a/tests/caps_word/caps_word_invert_on_shift/config.h b/tests/caps_word/caps_word_invert_on_shift/config.h
new file mode 100644
index 0000000000..7a3ec846f9
--- /dev/null
+++ b/tests/caps_word/caps_word_invert_on_shift/config.h
@@ -0,0 +1,21 @@
1// Copyright 2023 Google LLC
2//
3// This program is free software: you can redistribute it and/or modify
4// it under the terms of the GNU General Public License as published by
5// the Free Software Foundation, either version 2 of the License, or
6// (at your option) any later version.
7//
8// This program is distributed in the hope that it will be useful,
9// but WITHOUT ANY WARRANTY; without even the implied warranty of
10// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11// GNU General Public License for more details.
12//
13// You should have received a copy of the GNU General Public License
14// along with this program. If not, see <http://www.gnu.org/licenses/>.
15
16#pragma once
17
18#include "test_common.h"
19
20#define CAPS_WORD_INVERT_ON_SHIFT
21#define PERMISSIVE_HOLD
diff --git a/tests/caps_word/caps_word_invert_on_shift/test.mk b/tests/caps_word/caps_word_invert_on_shift/test.mk
new file mode 100644
index 0000000000..319c04d67a
--- /dev/null
+++ b/tests/caps_word/caps_word_invert_on_shift/test.mk
@@ -0,0 +1,17 @@
1# Copyright 2023 Google LLC
2#
3# This program is free software: you can redistribute it and/or modify
4# it under the terms of the GNU General Public License as published by
5# the Free Software Foundation, either version 2 of the License, or
6# (at your option) any later version.
7#
8# This program is distributed in the hope that it will be useful,
9# but WITHOUT ANY WARRANTY; without even the implied warranty of
10# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11# GNU General Public License for more details.
12#
13# You should have received a copy of the GNU General Public License
14# along with this program. If not, see <http://www.gnu.org/licenses/>.
15
16CAPS_WORD_ENABLE = yes
17
diff --git a/tests/caps_word/caps_word_invert_on_shift/test_caps_word_invert_on_shift.cpp b/tests/caps_word/caps_word_invert_on_shift/test_caps_word_invert_on_shift.cpp
new file mode 100644
index 0000000000..d322448181
--- /dev/null
+++ b/tests/caps_word/caps_word_invert_on_shift/test_caps_word_invert_on_shift.cpp
@@ -0,0 +1,215 @@
1// Copyright 2023 Google LLC
2//
3// This program is free software: you can redistribute it and/or modify
4// it under the terms of the GNU General Public License as published by
5// the Free Software Foundation, either version 2 of the License, or
6// (at your option) any later version.
7//
8// This program is distributed in the hope that it will be useful,
9// but WITHOUT ANY WARRANTY; without even the implied warranty of
10// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11// GNU General Public License for more details.
12//
13// You should have received a copy of the GNU General Public License
14// along with this program. If not, see <http://www.gnu.org/licenses/>.
15
16#include "keyboard_report_util.hpp"
17#include "keycode.h"
18#include "test_common.hpp"
19#include "test_fixture.hpp"
20#include "test_keymap_key.hpp"
21
22using ::testing::_;
23using ::testing::AnyNumber;
24using ::testing::AnyOf;
25using ::testing::InSequence;
26using ::testing::TestParamInfo;
27
28namespace {
29
30struct ShiftKeyParams {
31 std::string name;
32 uint16_t keycode;
33 uint16_t report_shift_code;
34
35 static const std::string& GetName(const TestParamInfo<ShiftKeyParams>& info) {
36 return info.param.name;
37 }
38};
39
40class CapsWordInvertOnShift : public ::testing::WithParamInterface<ShiftKeyParams>, public TestFixture {
41 void SetUp() override {
42 caps_word_off();
43 }
44};
45
46// With Caps Word on, type "A, 4, Shift(A, 4, A), A, Shift(A), 4".
47TEST_P(CapsWordInvertOnShift, ShiftWithinWord) {
48 TestDriver driver;
49 KeymapKey key_shift(0, 0, 0, GetParam().keycode);
50 KeymapKey key_a(0, 1, 0, KC_A);
51 KeymapKey key_4(0, 2, 0, KC_4);
52 set_keymap({key_shift, key_a, key_4});
53
54 // Allow any number of reports with no keys or only KC_LSFT.
55 // clang-format off
56 EXPECT_CALL(driver, send_keyboard_mock(AnyOf(
57 KeyboardReport(),
58 KeyboardReport(KC_LSFT))))
59 .Times(AnyNumber());
60 // clang-format on
61
62 { // Expect: "A4a$aAa4"
63 InSequence s;
64 EXPECT_REPORT(driver, (KC_LSFT, KC_A));
65 EXPECT_REPORT(driver, (KC_4));
66 EXPECT_REPORT(driver, (KC_A));
67 EXPECT_REPORT(driver, (KC_LSFT, KC_4));
68 EXPECT_REPORT(driver, (KC_A));
69 EXPECT_REPORT(driver, (KC_LSFT, KC_A));
70 EXPECT_REPORT(driver, (KC_A));
71 EXPECT_REPORT(driver, (KC_4));
72 }
73
74 caps_word_on();
75 tap_keys(key_a, key_4); // Type "A, 4".
76
77 key_shift.press(); // Type "Shift(A, 4, A)".
78 run_one_scan_loop();
79 tap_keys(key_a, key_4, key_a);
80 key_shift.release();
81 run_one_scan_loop();
82
83 tap_key(key_a); // Type "A".
84
85 key_shift.press(); // Type "Shift(A)".
86 run_one_scan_loop();
87 tap_key(key_a);
88 key_shift.release();
89 run_one_scan_loop();
90
91 tap_key(key_4); // Type "4".
92
93 VERIFY_AND_CLEAR(driver);
94}
95
96TEST_P(CapsWordInvertOnShift, ShiftHeldAtWordEnd) {
97 TestDriver driver;
98 KeymapKey key_shift(0, 0, 0, GetParam().keycode);
99 KeymapKey key_a(0, 1, 0, KC_A);
100 KeymapKey key_slsh(0, 2, 0, KC_SLSH);
101 set_keymap({key_shift, key_a, key_slsh});
102
103 // Allow any number of reports with no keys or only KC_LSFT.
104 // clang-format off
105 EXPECT_CALL(driver, send_keyboard_mock(AnyOf(
106 KeyboardReport(),
107 KeyboardReport(KC_LSFT),
108 KeyboardReport(KC_RSFT))))
109 .Times(AnyNumber());
110 // clang-format on
111
112 { // Expect: "Aa?A"
113 InSequence s;
114 EXPECT_REPORT(driver, (KC_LSFT, KC_A));
115 EXPECT_REPORT(driver, (KC_A));
116 EXPECT_REPORT(driver, (GetParam().report_shift_code, KC_SLSH));
117 EXPECT_REPORT(driver, (GetParam().report_shift_code, KC_A));
118 }
119
120 caps_word_on();
121 tap_key(key_a);
122
123 key_shift.press(); // Press Shift.
124 run_one_scan_loop();
125
126 EXPECT_EQ(get_mods(), 0);
127
128 tap_key(key_a);
129 tap_key(key_slsh); // Tap '/' key, which is word breaking, ending Caps Word.
130
131 EXPECT_FALSE(is_caps_word_on());
132 EXPECT_EQ(get_mods(), MOD_BIT(GetParam().report_shift_code));
133
134 tap_key(key_a);
135 key_shift.release(); // Release Shift.
136 run_one_scan_loop();
137
138 EXPECT_EQ(get_mods(), 0);
139 VERIFY_AND_CLEAR(driver);
140}
141
142TEST_P(CapsWordInvertOnShift, TwoShiftsHeld) {
143 TestDriver driver;
144 KeymapKey key_shift1(0, 0, 0, GetParam().keycode);
145 KeymapKey key_shift2(0, 1, 0, GetParam().report_shift_code);
146 KeymapKey key_a(0, 2, 0, KC_A);
147 KeymapKey key_slsh(0, 3, 0, KC_SLSH);
148 set_keymap({key_shift1, key_shift2, key_a, key_slsh});
149
150 // Allow any number of reports with no keys or only KC_LSFT.
151 // clang-format off
152 EXPECT_CALL(driver, send_keyboard_mock(AnyOf(
153 KeyboardReport(),
154 KeyboardReport(KC_LSFT),
155 KeyboardReport(KC_RSFT))))
156 .Times(AnyNumber());
157 // clang-format on
158
159 { // Expect: "Aa?a"
160 InSequence s;
161 EXPECT_REPORT(driver, (KC_LSFT, KC_A));
162 EXPECT_REPORT(driver, (KC_A));
163 EXPECT_REPORT(driver, (GetParam().report_shift_code, KC_SLSH));
164 EXPECT_REPORT(driver, (KC_A));
165 }
166
167 caps_word_on();
168 tap_key(key_a);
169
170 key_shift1.press(); // Press shift1.
171 run_one_scan_loop();
172
173 EXPECT_EQ(get_mods(), 0);
174
175 tap_key(key_a);
176 tap_key(key_slsh); // Tap '/' key, which is word breaking, ending Caps Word.
177
178 EXPECT_FALSE(is_caps_word_on());
179 EXPECT_EQ(get_mods(), MOD_BIT(GetParam().report_shift_code));
180
181 key_shift2.press(); // Press shift2.
182 run_one_scan_loop();
183
184 EXPECT_EQ(get_mods(), MOD_BIT(GetParam().report_shift_code));
185
186 key_shift1.release(); // Release shift1.
187 run_one_scan_loop();
188
189 EXPECT_EQ(get_mods(), 0);
190 tap_key(key_a);
191
192 key_shift2.release(); // Release shift2.
193 run_one_scan_loop();
194
195 EXPECT_EQ(get_mods(), 0);
196 VERIFY_AND_CLEAR(driver);
197}
198
199// clang-format off
200INSTANTIATE_TEST_CASE_P(
201 Shifts,
202 CapsWordInvertOnShift,
203 ::testing::Values(
204 ShiftKeyParams{"KC_LSFT", KC_LSFT, KC_LSFT},
205 ShiftKeyParams{"KC_RSFT", KC_RSFT, KC_RSFT},
206 ShiftKeyParams{"LSFT_T", LSFT_T(KC_A), KC_LSFT},
207 ShiftKeyParams{"RSFT_T", RSFT_T(KC_A), KC_RSFT},
208 ShiftKeyParams{"OSM_LSFT", OSM(MOD_LSFT), KC_LSFT},
209 ShiftKeyParams{"OSM_RSFT", OSM(MOD_RSFT), KC_RSFT}
210 ),
211 ShiftKeyParams::GetName
212 );
213// clang-format on
214
215} // namespace