diff options
Diffstat (limited to 'common/action.c')
| -rw-r--r-- | common/action.c | 883 |
1 files changed, 883 insertions, 0 deletions
diff --git a/common/action.c b/common/action.c new file mode 100644 index 000000000..6d5336752 --- /dev/null +++ b/common/action.c | |||
| @@ -0,0 +1,883 @@ | |||
| 1 | /* | ||
| 2 | Copyright 2012,2013 Jun Wako <wakojun@gmail.com> | ||
| 3 | |||
| 4 | This program is free software: you can redistribute it and/or modify | ||
| 5 | it under the terms of the GNU General Public License as published by | ||
| 6 | the Free Software Foundation, either version 2 of the License, or | ||
| 7 | (at your option) any later version. | ||
| 8 | |||
| 9 | This program is distributed in the hope that it will be useful, | ||
| 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 12 | GNU General Public License for more details. | ||
| 13 | |||
| 14 | You should have received a copy of the GNU General Public License | ||
| 15 | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| 16 | */ | ||
| 17 | #include "host.h" | ||
| 18 | #include "timer.h" | ||
| 19 | #include "keymap.h" | ||
| 20 | #include "keycode.h" | ||
| 21 | #include "keyboard.h" | ||
| 22 | #include "mousekey.h" | ||
| 23 | #include "command.h" | ||
| 24 | #include "util.h" | ||
| 25 | #include "debug.h" | ||
| 26 | #include "action.h" | ||
| 27 | |||
| 28 | |||
| 29 | static void process_action(keyrecord_t *record); | ||
| 30 | static bool process_tapping(keyrecord_t *record); | ||
| 31 | static void waiting_buffer_scan_tap(void); | ||
| 32 | |||
| 33 | static void debug_event(keyevent_t event); | ||
| 34 | static void debug_record(keyrecord_t record); | ||
| 35 | static void debug_action(action_t action); | ||
| 36 | static void debug_tapping_key(void); | ||
| 37 | static void debug_waiting_buffer(void); | ||
| 38 | |||
| 39 | |||
| 40 | /* | ||
| 41 | * Tapping | ||
| 42 | */ | ||
| 43 | /* period of tapping(ms) */ | ||
| 44 | #ifndef TAPPING_TERM | ||
| 45 | #define TAPPING_TERM 200 | ||
| 46 | #endif | ||
| 47 | |||
| 48 | /* tap count needed for toggling a feature */ | ||
| 49 | #ifndef TAPPING_TOGGLE | ||
| 50 | #define TAPPING_TOGGLE 5 | ||
| 51 | #endif | ||
| 52 | |||
| 53 | /* stores a key event of current tap. */ | ||
| 54 | static keyrecord_t tapping_key = {}; | ||
| 55 | |||
| 56 | #define IS_TAPPING() !IS_NOEVENT(tapping_key.event) | ||
| 57 | #define IS_TAPPING_PRESSED() (IS_TAPPING() && tapping_key.event.pressed) | ||
| 58 | #define IS_TAPPING_RELEASED() (IS_TAPPING() && !tapping_key.event.pressed) | ||
| 59 | #define IS_TAPPING_KEY(k) (IS_TAPPING() && KEYEQ(tapping_key.event.key, (k))) | ||
| 60 | #define WITHIN_TAPPING_TERM(e) (TIMER_DIFF_16(e.time, tapping_key.event.time) < TAPPING_TERM) | ||
| 61 | |||
| 62 | |||
| 63 | /* | ||
| 64 | * Waiting buffer | ||
| 65 | * | ||
| 66 | * stores key events waiting for settling current tap. | ||
| 67 | */ | ||
| 68 | #define WAITING_BUFFER_SIZE 8 | ||
| 69 | static keyrecord_t waiting_buffer[WAITING_BUFFER_SIZE] = {}; | ||
| 70 | |||
| 71 | /* point to empty cell to enq */ | ||
| 72 | static uint8_t waiting_buffer_head = 0; | ||
| 73 | |||
| 74 | /* point to the oldest data cell to deq */ | ||
| 75 | static uint8_t waiting_buffer_tail = 0; | ||
| 76 | |||
| 77 | static bool waiting_buffer_enq(keyrecord_t record) | ||
| 78 | { | ||
| 79 | if (IS_NOEVENT(record.event)) { | ||
| 80 | return true; | ||
| 81 | } | ||
| 82 | |||
| 83 | if ((waiting_buffer_head + 1) % WAITING_BUFFER_SIZE == waiting_buffer_tail) { | ||
| 84 | debug("waiting_buffer_enq: Over flow.\n"); | ||
| 85 | return false; | ||
| 86 | } | ||
| 87 | |||
| 88 | waiting_buffer[waiting_buffer_head] = record; | ||
| 89 | waiting_buffer_head = (waiting_buffer_head + 1) % WAITING_BUFFER_SIZE; | ||
| 90 | |||
| 91 | debug("waiting_buffer_enq: "); debug_waiting_buffer(); | ||
| 92 | return true; | ||
| 93 | } | ||
| 94 | |||
| 95 | static void waiting_buffer_clear(void) | ||
| 96 | { | ||
| 97 | waiting_buffer_head = 0; | ||
| 98 | waiting_buffer_tail = 0; | ||
| 99 | } | ||
| 100 | |||
| 101 | #if TAPPING_TERM >= 500 | ||
| 102 | static bool waiting_buffer_typed(keyevent_t event) | ||
| 103 | { | ||
| 104 | for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) { | ||
| 105 | if (KEYEQ(event.key, waiting_buffer[i].event.key) && event.pressed != waiting_buffer[i].event.pressed) { | ||
| 106 | return true; | ||
| 107 | } | ||
| 108 | } | ||
| 109 | return false; | ||
| 110 | } | ||
| 111 | #endif | ||
| 112 | |||
| 113 | bool waiting_buffer_has_anykey_pressed(void) | ||
| 114 | { | ||
| 115 | for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) { | ||
| 116 | if (waiting_buffer[i].event.pressed) return true; | ||
| 117 | } | ||
| 118 | return false; | ||
| 119 | } | ||
| 120 | |||
| 121 | |||
| 122 | /* Oneshot modifier | ||
| 123 | * | ||
| 124 | * Problem: Want to capitalize like 'The' but the result tends to be 'THe'. | ||
| 125 | * Solution: Oneshot modifier have its effect on only one key coming next. | ||
| 126 | * Tap Shift, then type 't', 'h' and 'e'. Not need to hold Shift key. | ||
| 127 | * | ||
| 128 | * Hold: works as normal modifier. | ||
| 129 | * Tap: one shot modifier. | ||
| 130 | * 2 Tap: cancel one shot modifier. | ||
| 131 | * 5-Tap: toggles enable/disable oneshot feature. | ||
| 132 | */ | ||
| 133 | static struct { | ||
| 134 | uint8_t mods; | ||
| 135 | uint8_t time; | ||
| 136 | bool ready; | ||
| 137 | bool disabled; | ||
| 138 | } oneshot_state; | ||
| 139 | |||
| 140 | static void oneshot_start(uint8_t mods, uint16_t time) | ||
| 141 | { | ||
| 142 | oneshot_state.mods = mods; | ||
| 143 | oneshot_state.time = time; | ||
| 144 | oneshot_state.ready = true; | ||
| 145 | } | ||
| 146 | |||
| 147 | static void oneshot_cancel(void) | ||
| 148 | { | ||
| 149 | oneshot_state.mods = 0; | ||
| 150 | oneshot_state.time = 0; | ||
| 151 | oneshot_state.ready = false; | ||
| 152 | } | ||
| 153 | |||
| 154 | static void oneshot_toggle(void) | ||
| 155 | { | ||
| 156 | oneshot_state.disabled = !oneshot_state.disabled; | ||
| 157 | } | ||
| 158 | |||
| 159 | |||
| 160 | |||
| 161 | void action_exec(keyevent_t event) | ||
| 162 | { | ||
| 163 | if (!IS_NOEVENT(event)) { | ||
| 164 | debug("\n---- action_exec: start -----\n"); | ||
| 165 | debug("EVENT: "); debug_event(event); debug("\n"); | ||
| 166 | } | ||
| 167 | |||
| 168 | keyrecord_t record = { .event = event }; | ||
| 169 | |||
| 170 | // pre-process on tapping | ||
| 171 | if (process_tapping(&record)) { | ||
| 172 | if (!IS_NOEVENT(record.event)) { | ||
| 173 | debug("processed: "); debug_record(record); debug("\n"); | ||
| 174 | } | ||
| 175 | } else { | ||
| 176 | // enqueue | ||
| 177 | if (!waiting_buffer_enq(record)) { | ||
| 178 | // clear all in case of overflow. | ||
| 179 | debug("OVERFLOW: CLEAR ALL STATES\n"); | ||
| 180 | clear_keyboard(); | ||
| 181 | waiting_buffer_clear(); | ||
| 182 | tapping_key = (keyrecord_t){}; | ||
| 183 | } | ||
| 184 | } | ||
| 185 | |||
| 186 | // process waiting_buffer | ||
| 187 | if (!IS_NOEVENT(event) && waiting_buffer_head != waiting_buffer_tail) { | ||
| 188 | debug("---- action_exec: process waiting_buffer -----\n"); | ||
| 189 | } | ||
| 190 | |||
| 191 | for (; waiting_buffer_tail != waiting_buffer_head; waiting_buffer_tail = (waiting_buffer_tail + 1) % WAITING_BUFFER_SIZE) { | ||
| 192 | if (process_tapping(&waiting_buffer[waiting_buffer_tail])) { | ||
| 193 | debug("processed: waiting_buffer["); debug_dec(waiting_buffer_tail); debug("] = "); | ||
| 194 | debug_record(waiting_buffer[waiting_buffer_tail]); debug("\n\n"); | ||
| 195 | } else { | ||
| 196 | break; | ||
| 197 | } | ||
| 198 | } | ||
| 199 | if (!IS_NOEVENT(event)) { | ||
| 200 | debug("\n"); | ||
| 201 | } | ||
| 202 | } | ||
| 203 | |||
| 204 | static void process_action(keyrecord_t *record) | ||
| 205 | { | ||
| 206 | keyevent_t event = record->event; | ||
| 207 | uint8_t tap_count = record->tap_count; | ||
| 208 | |||
| 209 | if (IS_NOEVENT(event)) { return; } | ||
| 210 | |||
| 211 | action_t action = keymap_get_action(current_layer, event.key.pos.row, event.key.pos.col); | ||
| 212 | //debug("action: "); debug_hex16(action.code); if (event.pressed) debug("d\n"); else debug("u\n"); | ||
| 213 | debug("ACTION: "); debug_action(action); debug("\n"); | ||
| 214 | |||
| 215 | switch (action.kind.id) { | ||
| 216 | /* Key and Mods */ | ||
| 217 | case ACT_LMODS: | ||
| 218 | case ACT_RMODS: | ||
| 219 | { | ||
| 220 | uint8_t mods = (action.kind.id == ACT_LMODS) ? action.key.mods : | ||
| 221 | action.key.mods<<4; | ||
| 222 | if (event.pressed) { | ||
| 223 | uint8_t tmp_mods = host_get_mods(); | ||
| 224 | if (mods) { | ||
| 225 | host_add_mods(mods); | ||
| 226 | host_send_keyboard_report(); | ||
| 227 | } | ||
| 228 | register_code(action.key.code); | ||
| 229 | if (mods && action.key.code) { | ||
| 230 | host_set_mods(tmp_mods); | ||
| 231 | host_send_keyboard_report(); | ||
| 232 | } | ||
| 233 | } else { | ||
| 234 | if (mods && !action.key.code) { | ||
| 235 | host_del_mods(mods); | ||
| 236 | host_send_keyboard_report(); | ||
| 237 | } | ||
| 238 | unregister_code(action.key.code); | ||
| 239 | } | ||
| 240 | } | ||
| 241 | break; | ||
| 242 | case ACT_LMODS_TAP: | ||
| 243 | case ACT_RMODS_TAP: | ||
| 244 | { | ||
| 245 | uint8_t mods = (action.kind.id == ACT_LMODS_TAP) ? action.key.mods : | ||
| 246 | action.key.mods<<4; | ||
| 247 | switch (action.layer.code) { | ||
| 248 | case 0x00: | ||
| 249 | // Oneshot modifier | ||
| 250 | if (event.pressed) { | ||
| 251 | if (tap_count == 0) { | ||
| 252 | debug("MODS_TAP: Oneshot: add_mods\n"); | ||
| 253 | add_mods(mods); | ||
| 254 | } | ||
| 255 | else if (tap_count == 1) { | ||
| 256 | debug("MODS_TAP: Oneshot: start\n"); | ||
| 257 | oneshot_start(mods, event.time); | ||
| 258 | } | ||
| 259 | else if (tap_count == TAPPING_TOGGLE) { | ||
| 260 | debug("MODS_TAP: Oneshot: toggle\n"); | ||
| 261 | oneshot_toggle(); | ||
| 262 | } | ||
| 263 | else { | ||
| 264 | debug("MODS_TAP: Oneshot: cancel&add_mods\n"); | ||
| 265 | // double tap cancels oneshot and works as normal modifier. | ||
| 266 | oneshot_cancel(); | ||
| 267 | add_mods(mods); | ||
| 268 | } | ||
| 269 | } else { | ||
| 270 | if (tap_count == 0) { | ||
| 271 | debug("MODS_TAP: Oneshot: cancel/del_mods\n"); | ||
| 272 | // cancel oneshot by holding. | ||
| 273 | oneshot_cancel(); | ||
| 274 | del_mods(mods); | ||
| 275 | } | ||
| 276 | else if (tap_count == 1) { | ||
| 277 | debug("MODS_TAP: Oneshot: del_mods\n"); | ||
| 278 | // retain Oneshot | ||
| 279 | del_mods(mods); | ||
| 280 | } | ||
| 281 | else { | ||
| 282 | debug("MODS_TAP: Oneshot: del_mods\n"); | ||
| 283 | // cancel Mods | ||
| 284 | del_mods(mods); | ||
| 285 | } | ||
| 286 | } | ||
| 287 | break; | ||
| 288 | default: | ||
| 289 | if (event.pressed) { | ||
| 290 | if (tap_count > 0) { | ||
| 291 | if (waiting_buffer_has_anykey_pressed()) { | ||
| 292 | debug("MODS_TAP: Tap: Cancel: add_mods\n"); | ||
| 293 | // ad hoc: set 0 to cancel tap | ||
| 294 | record->tap_count = 0; | ||
| 295 | add_mods(mods); | ||
| 296 | } else { | ||
| 297 | debug("MODS_TAP: Tap: register_code\n"); | ||
| 298 | register_code(action.key.code); | ||
| 299 | } | ||
| 300 | } else { | ||
| 301 | debug("MODS_TAP: No tap: add_mods\n"); | ||
| 302 | add_mods(mods); | ||
| 303 | } | ||
| 304 | } else { | ||
| 305 | if (tap_count > 0) { | ||
| 306 | debug("MODS_TAP: Tap: unregister_code\n"); | ||
| 307 | unregister_code(action.key.code); | ||
| 308 | } else { | ||
| 309 | debug("MODS_TAP: No tap: add_mods\n"); | ||
| 310 | del_mods(mods); | ||
| 311 | } | ||
| 312 | } | ||
| 313 | break; | ||
| 314 | } | ||
| 315 | } | ||
| 316 | break; | ||
| 317 | |||
| 318 | /* other HID usage */ | ||
| 319 | case ACT_USAGE: | ||
| 320 | #ifdef EXTRAKEY_ENABLE | ||
| 321 | switch (action.usage.page) { | ||
| 322 | case PAGE_SYSTEM: | ||
| 323 | if (event.pressed) { | ||
| 324 | host_system_send(action.usage.code); | ||
| 325 | } else { | ||
| 326 | host_system_send(0); | ||
| 327 | } | ||
| 328 | break; | ||
| 329 | case PAGE_CONSUMER: | ||
| 330 | if (event.pressed) { | ||
| 331 | host_consumer_send(action.usage.code); | ||
| 332 | } else { | ||
| 333 | host_consumer_send(0); | ||
| 334 | } | ||
| 335 | break; | ||
| 336 | } | ||
| 337 | #endif | ||
| 338 | break; | ||
| 339 | |||
| 340 | /* Mouse key */ | ||
| 341 | case ACT_MOUSEKEY: | ||
| 342 | #ifdef MOUSEKEY_ENABLE | ||
| 343 | if (event.pressed) { | ||
| 344 | mousekey_on(action.key.code); | ||
| 345 | mousekey_send(); | ||
| 346 | } else { | ||
| 347 | mousekey_off(action.key.code); | ||
| 348 | mousekey_send(); | ||
| 349 | } | ||
| 350 | #endif | ||
| 351 | break; | ||
| 352 | |||
| 353 | /* Layer key */ | ||
| 354 | case ACT_LAYER: | ||
| 355 | switch (action.layer.code) { | ||
| 356 | case LAYER_MOMENTARY: /* momentary */ | ||
| 357 | if (event.pressed) { | ||
| 358 | layer_switch(action.layer.val); | ||
| 359 | } | ||
| 360 | else { | ||
| 361 | layer_switch(default_layer); | ||
| 362 | } | ||
| 363 | break; | ||
| 364 | case LAYER_ON_PRESS: | ||
| 365 | if (event.pressed) { | ||
| 366 | layer_switch(action.layer.val); | ||
| 367 | } | ||
| 368 | break; | ||
| 369 | case LAYER_ON_RELEASE: | ||
| 370 | if (!event.pressed) { | ||
| 371 | layer_switch(action.layer.val); | ||
| 372 | } | ||
| 373 | break; | ||
| 374 | case LAYER_DEFAULT: /* default layer */ | ||
| 375 | switch (action.layer.val) { | ||
| 376 | case DEFAULT_ON_BOTH: | ||
| 377 | layer_switch(default_layer); | ||
| 378 | break; | ||
| 379 | case DEFAULT_ON_PRESS: | ||
| 380 | if (event.pressed) { | ||
| 381 | layer_switch(default_layer); | ||
| 382 | } | ||
| 383 | break; | ||
| 384 | case DEFAULT_ON_RELEASE: | ||
| 385 | if (!event.pressed) { | ||
| 386 | layer_switch(default_layer); | ||
| 387 | } | ||
| 388 | break; | ||
| 389 | } | ||
| 390 | break; | ||
| 391 | case LAYER_TAP_TOGGLE: /* switch on hold and toggle on several taps */ | ||
| 392 | if (event.pressed) { | ||
| 393 | if (tap_count < TAPPING_TOGGLE) { | ||
| 394 | layer_switch(action.layer.val); | ||
| 395 | } | ||
| 396 | } else { | ||
| 397 | if (tap_count >= TAPPING_TOGGLE) { | ||
| 398 | debug("LAYER_PRESSED: tap toggle.\n"); | ||
| 399 | layer_switch(action.layer.val); | ||
| 400 | } | ||
| 401 | } | ||
| 402 | break; | ||
| 403 | case LAYER_CHANGE_DEFAULT: /* change default layer */ | ||
| 404 | if (event.pressed) { | ||
| 405 | default_layer = action.layer.val; | ||
| 406 | layer_switch(default_layer); | ||
| 407 | } | ||
| 408 | break; | ||
| 409 | default: /* switch layer on hold and key on tap*/ | ||
| 410 | if (event.pressed) { | ||
| 411 | if (tap_count > 0) { | ||
| 412 | debug("LAYER_PRESSED: Tap: register_code\n"); | ||
| 413 | register_code(action.layer.code); | ||
| 414 | } else { | ||
| 415 | debug("LAYER_PRESSED: No tap: layer_switch\n"); | ||
| 416 | layer_switch(action.layer.val); | ||
| 417 | } | ||
| 418 | } else { | ||
| 419 | if (tap_count > 0) { | ||
| 420 | debug("LAYER_PRESSED: Tap: unregister_code\n"); | ||
| 421 | unregister_code(action.layer.code); | ||
| 422 | } else { | ||
| 423 | //debug("LAYER_PRESSED: No tap: NO ACTION\n"); | ||
| 424 | //TODO: this is ok? | ||
| 425 | debug("LAYER_PRESSED: No tap: return to default layer\n"); | ||
| 426 | layer_switch(default_layer); | ||
| 427 | } | ||
| 428 | } | ||
| 429 | break; | ||
| 430 | } | ||
| 431 | break; | ||
| 432 | case ACT_LAYER_BIT: | ||
| 433 | switch (action.layer.code) { | ||
| 434 | case LAYER_MOMENTARY: /* momentary */ | ||
| 435 | if (event.pressed) { | ||
| 436 | layer_switch(current_layer ^ action.layer.val); | ||
| 437 | } else { | ||
| 438 | layer_switch(current_layer ^ action.layer.val); | ||
| 439 | } | ||
| 440 | break; | ||
| 441 | case LAYER_ON_PRESS: | ||
| 442 | if (event.pressed) { | ||
| 443 | layer_switch(current_layer ^ action.layer.val); | ||
| 444 | } | ||
| 445 | break; | ||
| 446 | case LAYER_ON_RELEASE: | ||
| 447 | if (!event.pressed) { | ||
| 448 | layer_switch(current_layer ^ action.layer.val); | ||
| 449 | } | ||
| 450 | break; | ||
| 451 | case LAYER_TAP_TOGGLE: /* switch on hold and toggle on several taps */ | ||
| 452 | if (event.pressed) { | ||
| 453 | if (tap_count < TAPPING_TOGGLE) { | ||
| 454 | debug("LAYER_BIT: tap toggle(press).\n"); | ||
| 455 | layer_switch(current_layer ^ action.layer.val); | ||
| 456 | } | ||
| 457 | } else { | ||
| 458 | if (tap_count <= TAPPING_TOGGLE) { | ||
| 459 | debug("LAYER_BIT: tap toggle(release).\n"); | ||
| 460 | layer_switch(current_layer ^ action.layer.val); | ||
| 461 | } | ||
| 462 | } | ||
| 463 | break; | ||
| 464 | case 0xFF: | ||
| 465 | // change default layer | ||
| 466 | if (event.pressed) { | ||
| 467 | default_layer = current_layer ^ action.layer.val; | ||
| 468 | layer_switch(default_layer); | ||
| 469 | } else { | ||
| 470 | default_layer = current_layer ^ action.layer.val; | ||
| 471 | layer_switch(default_layer); | ||
| 472 | } | ||
| 473 | break; | ||
| 474 | default: | ||
| 475 | // with tap key | ||
| 476 | if (event.pressed) { | ||
| 477 | if (IS_TAPPING_KEY(event.key) && tap_count > 0) { | ||
| 478 | debug("LAYER_BIT: Tap: register_code\n"); | ||
| 479 | register_code(action.layer.code); | ||
| 480 | } else { | ||
| 481 | debug("LAYER_BIT: No tap: layer_switch(bit on)\n"); | ||
| 482 | layer_switch(current_layer ^ action.layer.val); | ||
| 483 | } | ||
| 484 | } else { | ||
| 485 | if (IS_TAPPING_KEY(event.key) && tap_count > 0) { | ||
| 486 | debug("LAYER_BIT: Tap: unregister_code\n"); | ||
| 487 | unregister_code(action.layer.code); | ||
| 488 | } else { | ||
| 489 | debug("LAYER_BIT: No tap: layer_switch(bit off)\n"); | ||
| 490 | layer_switch(current_layer ^ action.layer.val); | ||
| 491 | } | ||
| 492 | } | ||
| 493 | break; | ||
| 494 | } | ||
| 495 | break; | ||
| 496 | |||
| 497 | /* Extentions */ | ||
| 498 | case ACT_MACRO: | ||
| 499 | break; | ||
| 500 | case ACT_COMMAND: | ||
| 501 | break; | ||
| 502 | case ACT_FUNCTION: | ||
| 503 | // TODO | ||
| 504 | keymap_call_function(record, action.func.id, action.func.opt); | ||
| 505 | break; | ||
| 506 | default: | ||
| 507 | break; | ||
| 508 | } | ||
| 509 | } | ||
| 510 | |||
| 511 | /* Tapping | ||
| 512 | * | ||
| 513 | * Rule: Tap key is typed(pressed and released) within TAPPING_TERM. | ||
| 514 | * (without interfering by typing other key) | ||
| 515 | */ | ||
| 516 | /* return true when key event is processed or consumed. */ | ||
| 517 | static bool process_tapping(keyrecord_t *keyp) | ||
| 518 | { | ||
| 519 | keyevent_t event = keyp->event; | ||
| 520 | |||
| 521 | // if tapping | ||
| 522 | if (IS_TAPPING_PRESSED()) { | ||
| 523 | if (WITHIN_TAPPING_TERM(event)) { | ||
| 524 | if (tapping_key.tap_count == 0) { | ||
| 525 | if (IS_TAPPING_KEY(event.key) && !event.pressed) { | ||
| 526 | // first tap! | ||
| 527 | debug("Tapping: First tap(0->1).\n"); | ||
| 528 | tapping_key.tap_count = 1; | ||
| 529 | debug_tapping_key(); | ||
| 530 | process_action(&tapping_key); | ||
| 531 | |||
| 532 | // enqueue | ||
| 533 | keyp->tap_count = tapping_key.tap_count; | ||
| 534 | return false; | ||
| 535 | } | ||
| 536 | #if TAPPING_TERM >= 500 | ||
| 537 | /* This can prevent from typing some tap keys in a row at a time. */ | ||
| 538 | else if (!event.pressed && waiting_buffer_typed(event)) { | ||
| 539 | // other key typed. not tap. | ||
| 540 | debug("Tapping: End. No tap. Interfered by typing key\n"); | ||
| 541 | process_action(&tapping_key); | ||
| 542 | tapping_key = (keyrecord_t){}; | ||
| 543 | debug_tapping_key(); | ||
| 544 | |||
| 545 | // enqueue | ||
| 546 | return false; | ||
| 547 | } | ||
| 548 | #endif | ||
| 549 | else { | ||
| 550 | // other key events shall be enq'd till tapping state settles. | ||
| 551 | return false; | ||
| 552 | } | ||
| 553 | } | ||
| 554 | // tap_count > 0 | ||
| 555 | else { | ||
| 556 | if (IS_TAPPING_KEY(event.key) && !event.pressed) { | ||
| 557 | debug("Tapping: Tap release("); debug_dec(tapping_key.tap_count); debug(")\n"); | ||
| 558 | keyp->tap_count = tapping_key.tap_count; | ||
| 559 | process_action(keyp); | ||
| 560 | tapping_key = *keyp; | ||
| 561 | debug_tapping_key(); | ||
| 562 | return true; | ||
| 563 | } | ||
| 564 | else if (is_tap_key(keyp->event.key) && event.pressed) { | ||
| 565 | if (tapping_key.tap_count > 1) { | ||
| 566 | debug("Tapping: Start new tap with releasing last tap(>1).\n"); | ||
| 567 | // unregister key | ||
| 568 | process_action(&(keyrecord_t){ | ||
| 569 | .tap_count = tapping_key.tap_count, | ||
| 570 | .event.key = tapping_key.event.key, | ||
| 571 | .event.time = event.time, | ||
| 572 | .event.pressed = false | ||
| 573 | }); | ||
| 574 | } else { | ||
| 575 | debug("Tapping: Start while last tap(1).\n"); | ||
| 576 | } | ||
| 577 | tapping_key = *keyp; | ||
| 578 | waiting_buffer_scan_tap(); | ||
| 579 | debug_tapping_key(); | ||
| 580 | return true; | ||
| 581 | } | ||
| 582 | else { | ||
| 583 | if (!IS_NOEVENT(keyp->event)) { | ||
| 584 | debug("Tapping: key event while last tap(>0).\n"); | ||
| 585 | } | ||
| 586 | process_action(keyp); | ||
| 587 | return true; | ||
| 588 | } | ||
| 589 | } | ||
| 590 | } | ||
| 591 | // after TAPPING_TERM | ||
| 592 | else { | ||
| 593 | if (tapping_key.tap_count == 0) { | ||
| 594 | debug("Tapping: End. Timeout. Not tap(0): "); | ||
| 595 | debug_event(event); debug("\n"); | ||
| 596 | process_action(&tapping_key); | ||
| 597 | tapping_key = (keyrecord_t){}; | ||
| 598 | debug_tapping_key(); | ||
| 599 | return false; | ||
| 600 | } else { | ||
| 601 | if (IS_TAPPING_KEY(event.key) && !event.pressed) { | ||
| 602 | debug("Tapping: End. last timeout tap release(>0)."); | ||
| 603 | keyp->tap_count = tapping_key.tap_count; | ||
| 604 | process_action(keyp); | ||
| 605 | tapping_key = (keyrecord_t){}; | ||
| 606 | return true; | ||
| 607 | } | ||
| 608 | else if (is_tap_key(keyp->event.key) && event.pressed) { | ||
| 609 | if (tapping_key.tap_count > 1) { | ||
| 610 | debug("Tapping: Start new tap with releasing last timeout tap(>1).\n"); | ||
| 611 | // unregister key | ||
| 612 | process_action(&(keyrecord_t){ | ||
| 613 | .tap_count = tapping_key.tap_count, | ||
| 614 | .event.key = tapping_key.event.key, | ||
| 615 | .event.time = event.time, | ||
| 616 | .event.pressed = false | ||
| 617 | }); | ||
| 618 | } else { | ||
| 619 | debug("Tapping: Start while last timeout tap(1).\n"); | ||
| 620 | } | ||
| 621 | tapping_key = *keyp; | ||
| 622 | waiting_buffer_scan_tap(); | ||
| 623 | debug_tapping_key(); | ||
| 624 | return true; | ||
| 625 | } | ||
| 626 | else { | ||
| 627 | if (!IS_NOEVENT(keyp->event)) { | ||
| 628 | debug("Tapping: key event while last timeout tap(>0).\n"); | ||
| 629 | } | ||
| 630 | process_action(keyp); | ||
| 631 | return true; | ||
| 632 | } | ||
| 633 | } | ||
| 634 | } | ||
| 635 | } else if (IS_TAPPING_RELEASED()) { | ||
| 636 | if (WITHIN_TAPPING_TERM(event)) { | ||
| 637 | if (tapping_key.tap_count > 0 && IS_TAPPING_KEY(event.key) && event.pressed) { | ||
| 638 | // sequential tap. | ||
| 639 | keyp->tap_count = tapping_key.tap_count + 1; | ||
| 640 | debug("Tapping: Tap press("); debug_dec(keyp->tap_count); debug(")\n"); | ||
| 641 | process_action(keyp); | ||
| 642 | tapping_key = *keyp; | ||
| 643 | debug_tapping_key(); | ||
| 644 | return true; | ||
| 645 | } else if (event.pressed && is_tap_key(event.key)) { | ||
| 646 | // Sequential tap can be interfered with other tap key. | ||
| 647 | debug("Tapping: Start with interfering other tap.\n"); | ||
| 648 | tapping_key = *keyp; | ||
| 649 | waiting_buffer_scan_tap(); | ||
| 650 | debug_tapping_key(); | ||
| 651 | return true; | ||
| 652 | } else { | ||
| 653 | if (!IS_NOEVENT(keyp->event)) debug("Tapping: other key just after tap.\n"); | ||
| 654 | process_action(keyp); | ||
| 655 | return true; | ||
| 656 | } | ||
| 657 | } else { | ||
| 658 | // timeout. no sequential tap. | ||
| 659 | debug("Tapping: End(Timeout after releasing last tap): "); | ||
| 660 | debug_event(event); debug("\n"); | ||
| 661 | tapping_key = (keyrecord_t){}; | ||
| 662 | debug_tapping_key(); | ||
| 663 | return false; | ||
| 664 | } | ||
| 665 | } | ||
| 666 | // not tapping satate | ||
| 667 | else { | ||
| 668 | if (event.pressed && is_tap_key(event.key)) { | ||
| 669 | debug("Tapping: Start(Press tap key).\n"); | ||
| 670 | tapping_key = *keyp; | ||
| 671 | waiting_buffer_scan_tap(); | ||
| 672 | debug_tapping_key(); | ||
| 673 | return true; | ||
| 674 | } else { | ||
| 675 | process_action(keyp); | ||
| 676 | return true; | ||
| 677 | } | ||
| 678 | } | ||
| 679 | } | ||
| 680 | |||
| 681 | /* scan buffer for tapping */ | ||
| 682 | static void waiting_buffer_scan_tap(void) | ||
| 683 | { | ||
| 684 | // tapping already is settled | ||
| 685 | if (tapping_key.tap_count > 0) return; | ||
| 686 | // invalid state: tapping_key released && tap_count == 0 | ||
| 687 | if (!tapping_key.event.pressed) return; | ||
| 688 | |||
| 689 | for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) { | ||
| 690 | if (IS_TAPPING_KEY(waiting_buffer[i].event.key) && | ||
| 691 | !waiting_buffer[i].event.pressed && | ||
| 692 | WITHIN_TAPPING_TERM(waiting_buffer[i].event)) { | ||
| 693 | tapping_key.tap_count = 1; | ||
| 694 | waiting_buffer[i].tap_count = 1; | ||
| 695 | process_action(&tapping_key); | ||
| 696 | |||
| 697 | debug("waiting_buffer_scan_tap: found at ["); debug_dec(i); debug("]\n"); | ||
| 698 | debug_waiting_buffer(); | ||
| 699 | return; | ||
| 700 | } | ||
| 701 | } | ||
| 702 | } | ||
| 703 | |||
| 704 | |||
| 705 | |||
| 706 | /* | ||
| 707 | * Utilities for actions. | ||
| 708 | */ | ||
| 709 | void register_code(uint8_t code) | ||
| 710 | { | ||
| 711 | if (code == KC_NO) { | ||
| 712 | return; | ||
| 713 | } | ||
| 714 | else if IS_KEY(code) { | ||
| 715 | // TODO: should push command_proc out of this block? | ||
| 716 | if (command_proc(code)) return; | ||
| 717 | |||
| 718 | if (oneshot_state.mods && oneshot_state.ready && !oneshot_state.disabled) { | ||
| 719 | uint8_t tmp_mods = host_get_mods(); | ||
| 720 | host_add_mods(oneshot_state.mods); | ||
| 721 | host_add_key(code); | ||
| 722 | host_send_keyboard_report(); | ||
| 723 | |||
| 724 | host_set_mods(tmp_mods); | ||
| 725 | oneshot_state.ready = false; | ||
| 726 | } else { | ||
| 727 | host_add_key(code); | ||
| 728 | host_send_keyboard_report(); | ||
| 729 | } | ||
| 730 | } | ||
| 731 | else if IS_MOD(code) { | ||
| 732 | host_add_mods(MOD_BIT(code)); | ||
| 733 | host_send_keyboard_report(); | ||
| 734 | } | ||
| 735 | } | ||
| 736 | |||
| 737 | void unregister_code(uint8_t code) | ||
| 738 | { | ||
| 739 | if IS_KEY(code) { | ||
| 740 | host_del_key(code); | ||
| 741 | host_send_keyboard_report(); | ||
| 742 | } | ||
| 743 | else if IS_MOD(code) { | ||
| 744 | host_del_mods(MOD_BIT(code)); | ||
| 745 | host_send_keyboard_report(); | ||
| 746 | } | ||
| 747 | } | ||
| 748 | |||
| 749 | void add_mods(uint8_t mods) | ||
| 750 | { | ||
| 751 | if (mods) { | ||
| 752 | host_add_mods(mods); | ||
| 753 | host_send_keyboard_report(); | ||
| 754 | } | ||
| 755 | } | ||
| 756 | |||
| 757 | void del_mods(uint8_t mods) | ||
| 758 | { | ||
| 759 | if (mods) { | ||
| 760 | host_del_mods(mods); | ||
| 761 | host_send_keyboard_report(); | ||
| 762 | } | ||
| 763 | } | ||
| 764 | |||
| 765 | void set_mods(uint8_t mods) | ||
| 766 | { | ||
| 767 | host_set_mods(mods); | ||
| 768 | host_send_keyboard_report(); | ||
| 769 | } | ||
| 770 | |||
| 771 | void clear_keyboard(void) | ||
| 772 | { | ||
| 773 | host_clear_mods(); | ||
| 774 | clear_keyboard_but_mods(); | ||
| 775 | } | ||
| 776 | |||
| 777 | void clear_keyboard_but_mods(void) | ||
| 778 | { | ||
| 779 | host_clear_keys(); | ||
| 780 | host_send_keyboard_report(); | ||
| 781 | #ifdef MOUSEKEY_ENABLE | ||
| 782 | mousekey_clear(); | ||
| 783 | mousekey_send(); | ||
| 784 | #endif | ||
| 785 | #ifdef EXTRAKEY_ENABLE | ||
| 786 | host_system_send(0); | ||
| 787 | host_consumer_send(0); | ||
| 788 | #endif | ||
| 789 | } | ||
| 790 | |||
| 791 | bool sending_anykey(void) | ||
| 792 | { | ||
| 793 | return (host_has_anykey() || host_mouse_in_use() || | ||
| 794 | host_last_sysytem_report() || host_last_consumer_report()); | ||
| 795 | } | ||
| 796 | |||
| 797 | void layer_switch(uint8_t new_layer) | ||
| 798 | { | ||
| 799 | if (current_layer != new_layer) { | ||
| 800 | debug("Layer Switch: "); debug_hex(current_layer); | ||
| 801 | debug(" -> "); debug_hex(new_layer); debug("\n"); | ||
| 802 | |||
| 803 | current_layer = new_layer; | ||
| 804 | clear_keyboard_but_mods(); // To avoid stuck keys | ||
| 805 | // NOTE: update mods with full scan of matrix? if modifier changes between layers | ||
| 806 | } | ||
| 807 | } | ||
| 808 | |||
| 809 | bool is_tap_key(key_t key) | ||
| 810 | { | ||
| 811 | action_t action = keymap_get_action(current_layer, key.pos.row, key.pos.col); | ||
| 812 | switch (action.kind.id) { | ||
| 813 | case ACT_LMODS_TAP: | ||
| 814 | case ACT_RMODS_TAP: | ||
| 815 | return true; | ||
| 816 | case ACT_LAYER: | ||
| 817 | case ACT_LAYER_BIT: | ||
| 818 | switch (action.layer.code) { | ||
| 819 | case LAYER_MOMENTARY: | ||
| 820 | case LAYER_ON_PRESS: | ||
| 821 | case LAYER_ON_RELEASE: | ||
| 822 | case LAYER_DEFAULT: | ||
| 823 | return false; | ||
| 824 | case LAYER_TAP_TOGGLE: | ||
| 825 | default: /* tap key */ | ||
| 826 | return true; | ||
| 827 | } | ||
| 828 | return false; | ||
| 829 | case ACT_FUNCTION: | ||
| 830 | if (action.func.opt & FUNC_TAP) { return true; } | ||
| 831 | return false; | ||
| 832 | } | ||
| 833 | return false; | ||
| 834 | } | ||
| 835 | |||
| 836 | |||
| 837 | /* | ||
| 838 | * debug print | ||
| 839 | */ | ||
| 840 | static void debug_event(keyevent_t event) | ||
| 841 | { | ||
| 842 | debug_hex16(event.key.raw); | ||
| 843 | if (event.pressed) debug("d("); else debug("u("); | ||
| 844 | debug_dec(event.time); debug(")"); | ||
| 845 | } | ||
| 846 | static void debug_record(keyrecord_t record) | ||
| 847 | { | ||
| 848 | debug_event(record.event); debug(":"); debug_dec(record.tap_count); | ||
| 849 | } | ||
| 850 | static void debug_action(action_t action) | ||
| 851 | { | ||
| 852 | switch (action.kind.id) { | ||
| 853 | case ACT_LMODS: debug("ACT_LMODS"); break; | ||
| 854 | case ACT_RMODS: debug("ACT_RMODS"); break; | ||
| 855 | case ACT_LMODS_TAP: debug("ACT_LMODS_TAP"); break; | ||
| 856 | case ACT_RMODS_TAP: debug("ACT_RMODS_TAP"); break; | ||
| 857 | case ACT_USAGE: debug("ACT_USAGE"); break; | ||
| 858 | case ACT_MOUSEKEY: debug("ACT_MOUSEKEY"); break; | ||
| 859 | case ACT_LAYER: debug("ACT_LAYER"); break; | ||
| 860 | case ACT_LAYER_BIT: debug("ACT_LAYER_BIT"); break; | ||
| 861 | case ACT_MACRO: debug("ACT_MACRO"); break; | ||
| 862 | case ACT_COMMAND: debug("ACT_COMMAND"); break; | ||
| 863 | case ACT_FUNCTION: debug("ACT_FUNCTION"); break; | ||
| 864 | default: debug("UNKNOWN"); break; | ||
| 865 | } | ||
| 866 | debug("["); | ||
| 867 | debug_hex4(action.kind.param>>8); | ||
| 868 | debug(":"); | ||
| 869 | debug_hex8(action.kind.param & 0xff); | ||
| 870 | debug("]"); | ||
| 871 | } | ||
| 872 | static void debug_tapping_key(void) | ||
| 873 | { | ||
| 874 | debug("TAPPING_KEY="); debug_record(tapping_key); debug("\n"); | ||
| 875 | } | ||
| 876 | static void debug_waiting_buffer(void) | ||
| 877 | { | ||
| 878 | debug("{ "); | ||
| 879 | for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) { | ||
| 880 | debug("["); debug_dec(i); debug("]="); debug_record(waiting_buffer[i]); debug(" "); | ||
| 881 | } | ||
| 882 | debug("}\n"); | ||
| 883 | } | ||
