aboutsummaryrefslogtreecommitdiff
path: root/common
diff options
context:
space:
mode:
authortmk <nobody@nowhere>2013-01-27 16:38:19 +0900
committertmk <nobody@nowhere>2013-01-27 16:38:19 +0900
commitbfd7fe586297d70f824a402fd476c3daa889fa56 (patch)
treec903d4a878aa7317ca3c2bb62ece03fdb745d51b /common
parent66d5dd284271d85da4c161529e12526b50b0f71d (diff)
downloadqmk_firmware-bfd7fe586297d70f824a402fd476c3daa889fa56.tar.gz
qmk_firmware-bfd7fe586297d70f824a402fd476c3daa889fa56.zip
Add oneshot modifier action.
Diffstat (limited to 'common')
-rw-r--r--common/action.c201
-rw-r--r--common/action.h198
2 files changed, 227 insertions, 172 deletions
diff --git a/common/action.c b/common/action.c
index db31613bf..22f0bf0a0 100644
--- a/common/action.c
+++ b/common/action.c
@@ -97,6 +97,40 @@ static void waiting_buffer_process(void)
97{ 97{
98} 98}
99 99
100/* Oneshot modifier
101 *
102 * Problem: Want to capitalize like 'The' but the result tends to be 'THe'.
103 * Solution: Oneshot modifier have its effect on only one key coming next.
104 * Tap Shift, then type 't', 'h' and 'e'. Not need to hold Shift key.
105 *
106 * Hold: works as normal modifier.
107 * Tap: one shot modifier.
108 * 2 Tap: cancel one shot modifier.
109 * 5-Tap: toggles enable/disable oneshot feature.
110 */
111static struct {
112 uint8_t mods;
113 uint8_t time;
114 bool ready;
115 bool disabled;
116} oneshot_state;
117static void oneshot_start(uint8_t mods, uint16_t time)
118{
119 oneshot_state.mods = mods;
120 oneshot_state.time = time;
121 oneshot_state.ready = true;
122}
123static void oneshot_cancel(void)
124{
125 oneshot_state.mods = 0;
126 oneshot_state.time = 0;
127 oneshot_state.ready = false;
128}
129static void oneshot_toggle(void)
130{
131 oneshot_state.disabled = !oneshot_state.disabled;
132}
133
100 134
101/* 135/*
102 * Rule to judge tap: 136 * Rule to judge tap:
@@ -271,83 +305,102 @@ static void process(keyrecord_t *record)
271 switch (action.kind.id) { 305 switch (action.kind.id) {
272 /* Key and Mods */ 306 /* Key and Mods */
273 case ACT_LMODS: 307 case ACT_LMODS:
274 // |pressed |released
275 // --------------+---------------------------------+------------
276 // key |down(key) |up(key)
277 // mods |add(mods) |del(mods)
278 // key with mods |add(mods), down(key), unset(mods)|up(key)
279 if (event.pressed) {
280 uint8_t tmp_mods = host_get_mods();
281 if (action.key.mods) {
282 host_add_mods(action.key.mods);
283 host_send_keyboard_report();
284 }
285 register_code(action.key.code);
286 if (action.key.mods && action.key.code) {
287 host_set_mods(tmp_mods);
288 host_send_keyboard_report();
289 }
290 } else {
291 if (action.key.mods && !action.key.code) {
292 host_del_mods(action.key.mods);
293 host_send_keyboard_report();
294 }
295 unregister_code(action.key.code);
296 }
297 break;
298 case ACT_RMODS: 308 case ACT_RMODS:
299 // |pressed |released 309 {
300 // --------------+---------------------------------+------------ 310 uint8_t mods = (action.kind.id == ACT_LMODS) ? action.key.mods :
301 // key |down(key) |up(key) 311 action.key.mods<<4;
302 // mods |add(mods) |del(mods) 312 if (event.pressed) {
303 // key with mods |add(mods), down(key), unset(mods)|up(key) 313 uint8_t tmp_mods = host_get_mods();
304 if (event.pressed) { 314 if (mods) {
305 uint8_t tmp_mods = host_get_mods(); 315 host_add_mods(mods);
306 if (action.key.mods) { 316 host_send_keyboard_report();
307 host_add_mods(action.key.mods<<4); 317 }
308 host_send_keyboard_report(); 318 register_code(action.key.code);
309 } 319 if (mods && action.key.code) {
310 register_code(action.key.code); 320 host_set_mods(tmp_mods);
311 if (action.key.mods && action.key.code) { 321 host_send_keyboard_report();
312 host_set_mods(tmp_mods); 322 }
313 host_send_keyboard_report(); 323 } else {
314 } 324 if (mods && !action.key.code) {
315 } else { 325 host_del_mods(mods);
316 if (action.key.mods && !action.key.code) { 326 host_send_keyboard_report();
317 host_del_mods(action.key.mods<<4); 327 }
318 host_send_keyboard_report(); 328 unregister_code(action.key.code);
319 } 329 }
320 unregister_code(action.key.code);
321 } 330 }
322 break; 331 break;
323 case ACT_LMODS_TAP: 332 case ACT_LMODS_TAP:
324 case ACT_RMODS_TAP: 333 case ACT_RMODS_TAP:
325 { 334 {
326 uint8_t tmp_mods = (action.kind.id == ACT_LMODS_TAP) ? action.key.mods : 335 uint8_t mods = (action.kind.id == ACT_LMODS_TAP) ? action.key.mods :
327 action.key.mods<<4; 336 action.key.mods<<4;
328 if (event.pressed) { 337 switch (action.layer.code) {
329 if (IS_TAPPING_KEY(event.key) && tap_count > 0) { 338 case 0x00:
330 if (waiting_buffer_has_anykey_pressed()) { 339 // Oneshot modifier
331 debug("MODS_TAP: Tap: Cancel: add_mods\n"); 340 if (event.pressed) {
332 // ad hoc: set 0 to cancel tap 341 if (tap_count == 0) {
333 record->tap_count = 0; 342 debug("MODS_TAP: Oneshot: add_mods\n");
334 add_mods(tmp_mods); 343 add_mods(mods);
344 }
345 else if (tap_count == 1) {
346 debug("MODS_TAP: Oneshot: start\n");
347 oneshot_start(mods, event.time);
348 }
349 else if (tap_count == 5) {
350 debug("MODS_TAP: Oneshot: toggle\n");
351 oneshot_toggle();
352 }
353 else {
354 debug("MODS_TAP: Oneshot: cancel&add_mods\n");
355 // double tap cancels oneshot and works as normal modifier.
356 oneshot_cancel();
357 add_mods(mods);
358 }
335 } else { 359 } else {
336 debug("MODS_TAP: Tap: register_code\n"); 360 if (tap_count == 0) {
337 register_code(action.key.code); 361 debug("MODS_TAP: Oneshot: cancel/del_mods\n");
362 // cancel oneshot by holding.
363 oneshot_cancel();
364 del_mods(mods);
365 }
366 else if (tap_count == 1) {
367 debug("MODS_TAP: Oneshot: del_mods\n");
368 // retain Oneshot
369 del_mods(mods);
370 }
371 else {
372 debug("MODS_TAP: Oneshot: del_mods\n");
373 // cancel Mods
374 del_mods(mods);
375 }
338 } 376 }
339 } else { 377 break;
340 debug("MODS_TAP: No tap: add_mods\n"); 378 default:
341 add_mods(tmp_mods); 379 if (event.pressed) {
342 } 380 if (tap_count > 0) {
343 } else { 381 if (waiting_buffer_has_anykey_pressed()) {
344 if (IS_TAPPING_KEY(event.key) && tap_count > 0) { 382 debug("MODS_TAP: Tap: Cancel: add_mods\n");
345 debug("MODS_TAP: Tap: unregister_code\n"); 383 // ad hoc: set 0 to cancel tap
346 unregister_code(action.key.code); 384 record->tap_count = 0;
347 } else { 385 add_mods(mods);
348 debug("MODS_TAP: No tap: add_mods\n"); 386 } else {
349 del_mods(tmp_mods); 387 debug("MODS_TAP: Tap: register_code\n");
350 } 388 register_code(action.key.code);
389 }
390 } else {
391 debug("MODS_TAP: No tap: add_mods\n");
392 add_mods(mods);
393 }
394 } else {
395 if (tap_count > 0) {
396 debug("MODS_TAP: Tap: unregister_code\n");
397 unregister_code(action.key.code);
398 } else {
399 debug("MODS_TAP: No tap: add_mods\n");
400 del_mods(mods);
401 }
402 }
403 break;
351 } 404 }
352 } 405 }
353 break; 406 break;
@@ -579,7 +632,17 @@ void register_code(uint8_t code)
579 } 632 }
580 else if IS_KEY(code) { 633 else if IS_KEY(code) {
581 // TODO: should push command_proc out of this block? 634 // TODO: should push command_proc out of this block?
582 if (!command_proc(code)) { 635 if (command_proc(code)) return;
636
637 if (oneshot_state.mods && oneshot_state.ready && !oneshot_state.disabled) {
638 uint8_t tmp_mods = host_get_mods();
639 host_add_mods(oneshot_state.mods);
640 host_add_key(code);
641 host_send_keyboard_report();
642
643 host_set_mods(tmp_mods);
644 oneshot_state.ready = false;
645 } else {
583 host_add_key(code); 646 host_add_key(code);
584 host_send_keyboard_report(); 647 host_send_keyboard_report();
585 } 648 }
diff --git a/common/action.h b/common/action.h
index 327a009ef..1b5b30d86 100644
--- a/common/action.h
+++ b/common/action.h
@@ -3,10 +3,79 @@
3 3
4#include "keyboard.h" 4#include "keyboard.h"
5 5
6
7/* Action struct.
8 *
9 * In avr-gcc bit field seems to be assigned from LSB(bit0) to MSB(bit15).
10 * AVR looks like a little endian in avr-gcc.
11 *
12 * TODO: not portable across compiler/endianness?
13 * Byte order and bit order of 0x1234:
14 * Big endian: 15 ... 8 7 ... 210
15 * | 0x12 | 0x34 |
16 * 0001 0010 0011 0100
17 * Little endian: 012 ... 7 8 ... 15
18 * | 0x34 | 0x12 |
19 * 0010 1100 0100 1000
20 */
21typedef union {
22 uint16_t code;
23 struct action_kind {
24 uint16_t param :12;
25 uint16_t id :4;
26 } kind;
27 struct action_key {
28 uint16_t code :8;
29 uint16_t mods :4;
30 uint16_t kind :4;
31 } key;
32 struct action_layer {
33 uint16_t code :8;
34 uint16_t opt :4;
35 uint16_t kind :4;
36 } layer;
37 struct action_usage {
38 uint16_t code :10;
39 uint16_t page :2;
40 uint16_t kind :4;
41 } usage;
42 struct action_command {
43 uint16_t id :8;
44 uint16_t option :4;
45 uint16_t kind :4;
46 } command;
47 struct action_function {
48 uint8_t id :8;
49 uint8_t opt :4;
50 uint8_t kind :4;
51 } func;
52} action_t;
53
54/* Action record. For internal use. */
55typedef struct {
56 keyevent_t event;
57 uint8_t tap_count;
58} keyrecord_t;
59
60
61/* Tap count: Tap is comprised of press and release within TAP_TERM.
62 * 0 means no tap.
63 * >1 means tap.
64 */
6extern uint8_t tap_count; 65extern uint8_t tap_count;
66
67/* current tap key event */
7extern keyevent_t tapping_event; 68extern keyevent_t tapping_event;
8 69
9 70
71/* action function */
72typedef void (*action_func_t)(keyevent_t event, uint8_t opt);
73
74// TODO: legacy keymap support
75void action_exec(keyevent_t event);
76void action_call_function(keyevent_t event, uint8_t id);
77
78
10/* 79/*
11 * Utilities for actions. 80 * Utilities for actions.
12 */ 81 */
@@ -25,33 +94,36 @@ bool is_tap_key(key_t key);
25 94
26 95
27/* 96/*
28Action codes 97 * Action codes
2916bit code: action_kind(4bit) + action_parameter(12bit) 98 * ============
30 99 * 16bit code: action_kind(4bit) + action_parameter(12bit)
100 *
31Keyboard Keys 101Keyboard Keys
32------------- 102-------------
33ACT_LMODS(0000): 103ACT_LMODS(0000):
340000|0000|000000|00 No action 1040000|0000|000000|00 No action
350000|mods|000000|00 Left mods Momentary
360000|mods|000000|01 Left mods OneShot
370000|mods|000000|10 (reserved)
380000|mods|000000|11 (reserved)
390000|0000| keycode Key 1050000|0000| keycode Key
1060010|mods|000000|00 Left mods Momentary
400000|mods| keycode Key+Left mods 1070000|mods| keycode Key+Left mods
41 108
42ACT_RMODS(0001): 109ACT_RMODS(0001):
430001|0000|000000|00 No action 1100001|0000|000000|00 No action
1110001|0000| keycode Key(no used)
440001|mods|000000|00 Right mods Momentary 1120001|mods|000000|00 Right mods Momentary
450001|mods|000000|01 Right mods OneShot
460001|mods|000000|10 (reserved)
470001|mods|000000|11 (reserved)
480001|0000| keycode Key
490001|mods| keycode Key+Right mods 1130001|mods| keycode Key+Right mods
50 114
51ACT_LMODS_TAP(0010): 115ACT_LMODS_TAP(0010):
1160010|mods|000000|00 Left mods OneShot
1170010|mods|000000|01 (reserved)
1180010|mods|000000|10 (reserved)
1190010|mods|000000|11 (reserved)
520010|mods| keycode Left mods+tap Key 1200010|mods| keycode Left mods+tap Key
53 121
54ACT_RMODS_TAP(0011): 122ACT_RMODS_TAP(0011):
1230011|mods|000000|00 Right mods OneShot
1240011|mods|000000|01 (reserved)
1250011|mods|000000|10 (reserved)
1260011|mods|000000|11 (reserved)
550011|mods| keycode Right mods+tap Key 1270011|mods| keycode Right mods+tap Key
56 128
57 129
@@ -108,24 +180,22 @@ Extensions(11XX)
108NOTE: NOT FIXED 180NOTE: NOT FIXED
109 181
110ACT_MACRO(1100): 182ACT_MACRO(1100):
1111100|opt | id(8) Macro play 1831100|opt | id(8) Macro play?
1121100|1111| id(8) Macro record 1841100|1111| id(8) Macro record?
113 185
114ACT_COMMAND(1110): 186ACT_COMMAND(1110):
1151110|opt | id(8) Built-in Command exec 1871110|opt | id(8) Built-in Command exec
116 188
117ACT_FUNCTION(1111): 189ACT_FUNCTION(1111):
1181111| address(12) Function 1901111| address(12) Function?
1191111|opt | id(8) Function 1911111|opt | id(8) Function?
120 Macro record(dynamicly) 192
121 Macro play(dynamicly) 193TODO: modifier + function by tap?
122TODO: modifier + [tap key /w mod]
123 : layerkey + [tap key /w mod]
124 for example: LShift + '('[Shift+9] and RShift + ')'[Shift+0] 194 for example: LShift + '('[Shift+9] and RShift + ')'[Shift+0]
125 http://deskthority.net/workshop-f7/tmk-keyboard-firmware-collection-t4478.html#p90052 195 http://deskthority.net/workshop-f7/tmk-keyboard-firmware-collection-t4478.html#p90052
126*/ 196 */
127 197
128enum action_id { 198enum action_kind_id {
129 ACT_LMODS = 0b0000, 199 ACT_LMODS = 0b0000,
130 ACT_RMODS = 0b0001, 200 ACT_RMODS = 0b0001,
131 ACT_LMODS_TAP = 0b0010, 201 ACT_LMODS_TAP = 0b0010,
@@ -144,74 +214,11 @@ enum action_id {
144 ACT_FUNCTION = 0b1111 214 ACT_FUNCTION = 0b1111
145}; 215};
146 216
147// TODO: not portable across compiler/endianness? 217enum acion_param {
148/* 218 ONE_SHOT = 0x00,
149In avr-gcc bit fields seems to be assigned from LSB(bit0) to MSB(bit15).
150AVR looks like a little endian in avr-gcc.
151
152Byte order and bit order of 0x1234:
153Big endian: 15 ... 8 7 ... 210
154 | 0x12 | 0x34 |
155 0001 0010 0011 0100
156Little endian: 012 ... 7 8 ... 15
157 | 0x34 | 0x12 |
158 0010 1100 0100 1000
159*/
160typedef union {
161 uint16_t code;
162 struct action_kind {
163 uint16_t param :12;
164 uint16_t id :4;
165 } kind;
166 struct action_key {
167 uint16_t code :8;
168 uint16_t mods :4;
169 uint16_t kind :4;
170 } key;
171 struct action_layer {
172 uint16_t code :8;
173 uint16_t opt :4;
174 uint16_t kind :4;
175 } layer;
176 struct action_usage {
177 uint16_t code :10;
178 uint16_t page :2;
179 uint16_t kind :4;
180 } usage;
181 struct action_command {
182 uint16_t id :8;
183 uint16_t option :4;
184 uint16_t kind :4;
185 } command;
186 struct action_function {
187 uint8_t id :8;
188 uint8_t opt :4;
189 uint8_t kind :4;
190 } func;
191} action_t;
192
193
194enum stroke_cmd {
195 STROKE_DOWN,
196 STROKE_UP,
197 STROKE_ALLUP, /* release all keys in reverse order */
198}; 219};
199 220
200typedef struct {
201 keyevent_t event;
202 uint8_t tap_count;
203} keyrecord_t;
204
205/* action function */
206typedef void (*action_func_t)(keyevent_t event, uint8_t opt);
207 221
208
209// TODO: legacy keymap support
210void action_exec(keyevent_t event);
211void action_call_function(keyevent_t event, uint8_t id);
212
213
214// TODO: proper names
215/* action_t utility */ 222/* action_t utility */
216#define ACTION_NO 0 223#define ACTION_NO 0
217#define ACTION(kind, param) ((kind)<<12 | (param)) 224#define ACTION(kind, param) ((kind)<<12 | (param))
@@ -221,16 +228,12 @@ void action_call_function(keyevent_t event, uint8_t id);
221#define ACTION_KEY(key) ACTION(ACT_LMODS, key) 228#define ACTION_KEY(key) ACTION(ACT_LMODS, key)
222#define ACTION_LMODS(mods) ACTION(ACT_LMODS, (mods)<<8 | 0x00) 229#define ACTION_LMODS(mods) ACTION(ACT_LMODS, (mods)<<8 | 0x00)
223#define ACTION_LMODS_KEY(mods, key) ACTION(ACT_LMODS, (mods)<<8 | (key)) 230#define ACTION_LMODS_KEY(mods, key) ACTION(ACT_LMODS, (mods)<<8 | (key))
224#define ACTION_LMODS_ONESHOT(mods) ACTION(ACT_LMODS, (mods)<<8 | 0x01)
225#define ACTION_LMODS_SWITCH(mods, tap) ACTION(ACT_LMODS, (mods)<<8 | 0xF0 | (tap))
226#define ACTION_LMODS_TOGGLE(mods, tap) ACTION(ACT_LMODS, (mods)<<8 | 0xF1 | (tap))
227#define ACTION_RMODS(mods) ACTION(ACT_RMODS, (mods)<<8 | 0x00) 231#define ACTION_RMODS(mods) ACTION(ACT_RMODS, (mods)<<8 | 0x00)
228#define ACTION_RMODS_KEY(mods, key) ACTION(ACT_RMODS, (mods)<<8 | (key)) 232#define ACTION_RMODS_KEY(mods, key) ACTION(ACT_RMODS, (mods)<<8 | (key))
229#define ACTION_RMODS_ONESHOT(mods) ACTION(ACT_RMODS, (mods)<<8 | 0x01) 233
230#define ACTION_RMODS_SWITCH(mods, tap) ACTION(ACT_RMODS, (mods)<<8 | 0xF0 | (tap))
231#define ACTION_RMODS_TOGGLE(mods, tap) ACTION(ACT_RMODS, (mods)<<8 | 0xF1 | (tap))
232/* Mods + Tap key */ 234/* Mods + Tap key */
233#define ACTION_LMODS_TAP(mods, key) ACTION(ACT_LMODS_TAP, MOD_BITS(mods)<<8 | (key)) 235#define ACTION_LMODS_TAP(mods, key) ACTION(ACT_LMODS_TAP, MOD_BITS(mods)<<8 | (key))
236#define ACTION_LMODS_ONESHOT(mods) ACTION(ACT_LMODS_TAP, MOD_BITS(mods)<<8 | ONE_SHOT)
234#define ACTION_RMODS_TAP(mods, key) ACTION(ACT_RMODS_TAP, MOD_BITS(mods)<<8 | (key)) 237#define ACTION_RMODS_TAP(mods, key) ACTION(ACT_RMODS_TAP, MOD_BITS(mods)<<8 | (key))
235 238
236/* Layer Switch */ 239/* Layer Switch */
@@ -268,15 +271,4 @@ void action_call_function(keyevent_t event, uint8_t id);
268/* Function */ 271/* Function */
269#define ACTION_FUNCTION(id, opt) ACTION(ACT_FUNCTION, (opt)<<8 | id) 272#define ACTION_FUNCTION(id, opt) ACTION(ACT_FUNCTION, (opt)<<8 | id)
270 273
271
272/* helpers for readability */
273#define LAYER(layer) (layer)
274#define TAP(tap) (tap)
275#define DOUBLE_TAP 2
276#define TRIPLE_TAP 3
277#define QUADRUPLE_TAP 4
278#define QUINTUPLE_TAP 5
279#define DOWN(key) (key)
280#define UP(key) STROKE_UP, (key)
281
282#endif /* ACTION_H */ 274#endif /* ACTION_H */