diff options
Diffstat (limited to 'common/action.c')
| -rw-r--r-- | common/action.c | 249 |
1 files changed, 155 insertions, 94 deletions
diff --git a/common/action.c b/common/action.c index 389fc5df1..7299a874a 100644 --- a/common/action.c +++ b/common/action.c | |||
| @@ -10,11 +10,12 @@ | |||
| 10 | #include "action.h" | 10 | #include "action.h" |
| 11 | 11 | ||
| 12 | 12 | ||
| 13 | |||
| 14 | |||
| 15 | static void process(keyevent_t event, action_t action); | 13 | static void process(keyevent_t event, action_t action); |
| 16 | static void register_code(uint8_t code); | 14 | static void register_code(uint8_t code); |
| 17 | static void unregister_code(uint8_t code); | 15 | static void unregister_code(uint8_t code); |
| 16 | static void add_mods(uint8_t mods); | ||
| 17 | static void del_mods(uint8_t mods); | ||
| 18 | static void set_mods(uint8_t mods); | ||
| 18 | static void clear_keyboard(void); | 19 | static void clear_keyboard(void); |
| 19 | static void clear_keyboard_but_mods(void); | 20 | static void clear_keyboard_but_mods(void); |
| 20 | static bool sending_anykey(void); | 21 | static bool sending_anykey(void); |
| @@ -24,7 +25,6 @@ static void layer_switch(uint8_t new_layer); | |||
| 24 | /* tap */ | 25 | /* tap */ |
| 25 | #define TAP_TIME 200 | 26 | #define TAP_TIME 200 |
| 26 | static keyevent_t last_event = {}; | 27 | static keyevent_t last_event = {}; |
| 27 | static uint16_t last_event_time = 0; | ||
| 28 | static uint8_t tap_count = 0; | 28 | static uint8_t tap_count = 0; |
| 29 | 29 | ||
| 30 | /* layer */ | 30 | /* layer */ |
| @@ -32,6 +32,7 @@ uint8_t default_layer = 0; | |||
| 32 | uint8_t current_layer = 0; | 32 | uint8_t current_layer = 0; |
| 33 | keyrecord_t delaying_layer = {}; | 33 | keyrecord_t delaying_layer = {}; |
| 34 | 34 | ||
| 35 | /* waiting keys buffer */ | ||
| 35 | #define WAITING_KEYS_BUFFER 3 | 36 | #define WAITING_KEYS_BUFFER 3 |
| 36 | static keyrecord_t waiting_keys[WAITING_KEYS_BUFFER] = {}; | 37 | static keyrecord_t waiting_keys[WAITING_KEYS_BUFFER] = {}; |
| 37 | static uint8_t waiting_keys_head = 0; | 38 | static uint8_t waiting_keys_head = 0; |
| @@ -75,16 +76,87 @@ static void waiting_keys_process_in_current_layer(void) | |||
| 75 | } | 76 | } |
| 76 | 77 | ||
| 77 | 78 | ||
| 79 | void action_exec(keyevent_t event) | ||
| 80 | { | ||
| 81 | /* When delaying layer switch */ | ||
| 82 | if (delaying_layer.action.code) { | ||
| 83 | /* Layer switch when tap time elapses or waiting key is released */ | ||
| 84 | if ((timer_elapsed(delaying_layer.event.time) > TAP_TIME) || | ||
| 85 | (!event.pressed && waiting_keys_has(event.key))) { | ||
| 86 | /* layer switch */ | ||
| 87 | switch (delaying_layer.action.kind.id) { | ||
| 88 | case ACT_LAYER_PRESSED: | ||
| 89 | layer_switch(delaying_layer.action.layer.opt); | ||
| 90 | break; | ||
| 91 | case ACT_LAYER_BIT: | ||
| 92 | layer_switch(current_layer | delaying_layer.action.layer.opt); | ||
| 93 | break; | ||
| 94 | } | ||
| 95 | delaying_layer = (keyrecord_t){}; | ||
| 96 | |||
| 97 | /* Process waiting keys in new layer */ | ||
| 98 | waiting_keys_process_in_current_layer(); | ||
| 99 | } | ||
| 100 | /* when delaying layer key is released within delay term */ | ||
| 101 | else if (!event.pressed && KEYEQ(event.key, delaying_layer.event.key)) { | ||
| 102 | /* tap key down */ | ||
| 103 | debug("tap[delaying_layer](register): fist\n"); | ||
| 104 | uint8_t tmp_mods = host_get_mods(); | ||
| 105 | host_set_mods(delaying_layer.mods); | ||
| 106 | register_code(delaying_layer.action.layer.code); | ||
| 107 | delaying_layer = (keyrecord_t){}; | ||
| 108 | host_set_mods(tmp_mods); | ||
| 109 | |||
| 110 | /* process waiting keys */ | ||
| 111 | waiting_keys_process_in_current_layer(); | ||
| 112 | } | ||
| 113 | } | ||
| 114 | |||
| 115 | // not real event. event just to update delaying layer. | ||
| 116 | if (IS_NOEVENT(event)) { | ||
| 117 | return; | ||
| 118 | } | ||
| 119 | |||
| 120 | /* count tap when key is up */ | ||
| 121 | if (KEYEQ(event.key, last_event.key) && timer_elapsed(last_event.time) <= TAP_TIME) { | ||
| 122 | if (!event.pressed) tap_count++; | ||
| 123 | } else { | ||
| 124 | tap_count = 0; | ||
| 125 | } | ||
| 126 | |||
| 127 | action_t action = keymap_get_action(current_layer, event.key.row, event.key.col); | ||
| 128 | |||
| 129 | // TODO: all key events(pressed, released) should be recorded? | ||
| 130 | /* postpone key-down events while delaying layer */ | ||
| 131 | if (delaying_layer.action.code) { | ||
| 132 | if (event.pressed) { | ||
| 133 | waiting_keys_enqueue(event, action); | ||
| 134 | } else { | ||
| 135 | process(event, action); | ||
| 136 | } | ||
| 137 | } else { | ||
| 138 | process(event, action); | ||
| 139 | } | ||
| 140 | |||
| 141 | /* last event */ | ||
| 142 | last_event = event; | ||
| 143 | } | ||
| 144 | |||
| 145 | |||
| 78 | static void process(keyevent_t event, action_t action) | 146 | static void process(keyevent_t event, action_t action) |
| 79 | { | 147 | { |
| 80 | //action_t action = keymap_get_action(current_layer, event.key.row, event.key.col); | 148 | //action_t action = keymap_get_action(current_layer, event.key.row, event.key.col); |
| 81 | debug("action: "); debug_hex16(action.code); debug("\n"); | 149 | debug("action: "); debug_hex16(action.code); |
| 82 | 150 | if (event.pressed) debug("[down]\n"); else debug("[up]\n"); | |
| 83 | 151 | ||
| 84 | switch (action.kind.id) { | 152 | switch (action.kind.id) { |
| 85 | /* Key and Mods */ | 153 | /* Key and Mods */ |
| 86 | case ACT_LMODS: | 154 | case ACT_LMODS: |
| 87 | // normal key or key plus mods | 155 | // |pressed |released |
| 156 | // --------------+---------------------------------+------------ | ||
| 157 | // key |down(key) |up(key) | ||
| 158 | // mods |add(mods) |del(mods) | ||
| 159 | // key with mods |add(mods), down(key), unset(mods)|up(key) | ||
| 88 | if (event.pressed) { | 160 | if (event.pressed) { |
| 89 | uint8_t tmp_mods = host_get_mods(); | 161 | uint8_t tmp_mods = host_get_mods(); |
| 90 | if (action.key.mods) { | 162 | if (action.key.mods) { |
| @@ -105,6 +177,11 @@ static void process(keyevent_t event, action_t action) | |||
| 105 | } | 177 | } |
| 106 | break; | 178 | break; |
| 107 | case ACT_RMODS: | 179 | case ACT_RMODS: |
| 180 | // |pressed |released | ||
| 181 | // --------------+---------------------------------+------------ | ||
| 182 | // key |down(key) |up(key) | ||
| 183 | // mods |add(mods) |del(mods) | ||
| 184 | // key with mods |add(mods), down(key), unset(mods)|up(key) | ||
| 108 | if (event.pressed) { | 185 | if (event.pressed) { |
| 109 | uint8_t tmp_mods = host_get_mods(); | 186 | uint8_t tmp_mods = host_get_mods(); |
| 110 | if (action.key.mods) { | 187 | if (action.key.mods) { |
| @@ -124,9 +201,49 @@ static void process(keyevent_t event, action_t action) | |||
| 124 | unregister_code(action.key.code); | 201 | unregister_code(action.key.code); |
| 125 | } | 202 | } |
| 126 | break; | 203 | break; |
| 127 | case ACT_LMOD_TAP: | 204 | case ACT_LMODS_TAP: |
| 205 | if (event.pressed) { | ||
| 206 | if (tap_count == 0) { | ||
| 207 | add_mods(action.key.mods); | ||
| 208 | } else { | ||
| 209 | debug("tap[lmods](register): "); debug_hex(tap_count); debug("\n"); | ||
| 210 | register_code(action.key.code); | ||
| 211 | } | ||
| 212 | } else { | ||
| 213 | if (tap_count == 0) { | ||
| 214 | del_mods(action.key.mods); | ||
| 215 | } else if (tap_count == 1) { | ||
| 216 | debug("tap[lmods](register/unregister): "); debug_hex(tap_count); debug("\n"); | ||
| 217 | del_mods(action.key.mods); | ||
| 218 | register_code(action.key.code); | ||
| 219 | unregister_code(action.key.code); | ||
| 220 | } else { | ||
| 221 | debug("tap[lmods](unregister): "); debug_hex(tap_count); debug("\n"); | ||
| 222 | unregister_code(action.key.code); | ||
| 223 | } | ||
| 224 | } | ||
| 128 | break; | 225 | break; |
| 129 | case ACT_RMOD_TAP: | 226 | case ACT_RMODS_TAP: |
| 227 | if (event.pressed) { | ||
| 228 | if (tap_count == 0) { | ||
| 229 | add_mods(action.key.mods<<4); | ||
| 230 | } else { | ||
| 231 | debug("tap[rmods](register): "); debug_hex(tap_count); debug("\n"); | ||
| 232 | register_code(action.key.code); | ||
| 233 | } | ||
| 234 | } else { | ||
| 235 | if (tap_count == 0) { | ||
| 236 | del_mods(action.key.mods<<4); | ||
| 237 | } else if (tap_count == 1) { | ||
| 238 | debug("tap[rmods](register/unregister): "); debug_hex(tap_count); debug("\n"); | ||
| 239 | del_mods(action.key.mods<<4); | ||
| 240 | register_code(action.key.code); | ||
| 241 | unregister_code(action.key.code); | ||
| 242 | } else { | ||
| 243 | debug("tap[rmods](unregister): "); debug_hex(tap_count); debug("\n"); | ||
| 244 | unregister_code(action.key.code); | ||
| 245 | } | ||
| 246 | } | ||
| 130 | break; | 247 | break; |
| 131 | 248 | ||
| 132 | /* other HID usage */ | 249 | /* other HID usage */ |
| @@ -186,6 +303,7 @@ static void process(keyevent_t event, action_t action) | |||
| 186 | // with tap key | 303 | // with tap key |
| 187 | if (event.pressed) { | 304 | if (event.pressed) { |
| 188 | if (tap_count == 0) { | 305 | if (tap_count == 0) { |
| 306 | // not tapping yet | ||
| 189 | if (host_has_anykey()) { | 307 | if (host_has_anykey()) { |
| 190 | register_code(action.layer.code); | 308 | register_code(action.layer.code); |
| 191 | } else { | 309 | } else { |
| @@ -197,22 +315,14 @@ static void process(keyevent_t event, action_t action) | |||
| 197 | }; | 315 | }; |
| 198 | } | 316 | } |
| 199 | } else if (tap_count > 0) { | 317 | } else if (tap_count > 0) { |
| 200 | debug("tap: "); debug_hex(tap_count); debug("\n"); | 318 | // pressed after tapping |
| 319 | debug("tap[layer](register): "); debug_hex(tap_count); debug("\n"); | ||
| 201 | register_code(action.layer.code); | 320 | register_code(action.layer.code); |
| 202 | } | 321 | } |
| 203 | } else { | 322 | } else { |
| 204 | // tap key | 323 | // released after tapping |
| 205 | if (KEYEQ(event.key, delaying_layer.event.key) && | 324 | debug("tap[layer](unregister): "); debug_hex(tap_count); debug("\n"); |
| 206 | timer_elapsed(delaying_layer.event.time) <= TAP_TIME) { | 325 | unregister_code(action.layer.code); |
| 207 | uint8_t tmp_mods = host_get_mods(); | ||
| 208 | host_set_mods(delaying_layer.mods); | ||
| 209 | register_code(delaying_layer.action.layer.code); | ||
| 210 | host_set_mods(tmp_mods); | ||
| 211 | unregister_code(delaying_layer.action.layer.code); | ||
| 212 | } else { | ||
| 213 | unregister_code(action.layer.code); | ||
| 214 | } | ||
| 215 | delaying_layer = (keyrecord_t){}; | ||
| 216 | } | 326 | } |
| 217 | break; | 327 | break; |
| 218 | } | 328 | } |
| @@ -220,7 +330,7 @@ static void process(keyevent_t event, action_t action) | |||
| 220 | case ACT_LAYER_RELEASED: | 330 | case ACT_LAYER_RELEASED: |
| 221 | switch (action.layer.code) { | 331 | switch (action.layer.code) { |
| 222 | case 0x00: | 332 | case 0x00: |
| 223 | if (event.pressed) { | 333 | if (!event.pressed) { |
| 224 | layer_switch(action.layer.opt); | 334 | layer_switch(action.layer.opt); |
| 225 | } | 335 | } |
| 226 | break; | 336 | break; |
| @@ -274,7 +384,7 @@ static void process(keyevent_t event, action_t action) | |||
| 274 | }; | 384 | }; |
| 275 | } | 385 | } |
| 276 | } else if (tap_count > 0) { | 386 | } else if (tap_count > 0) { |
| 277 | debug("tap: "); debug_hex(tap_count); debug("\n"); | 387 | debug("tap[layer_bit](register): "); debug_hex(tap_count); debug("\n"); |
| 278 | register_code(action.layer.code); | 388 | register_code(action.layer.code); |
| 279 | } | 389 | } |
| 280 | } else { | 390 | } else { |
| @@ -348,77 +458,6 @@ static void process(keyevent_t event, action_t action) | |||
| 348 | } | 458 | } |
| 349 | } | 459 | } |
| 350 | 460 | ||
| 351 | void action_exec(keyevent_t event) | ||
| 352 | { | ||
| 353 | /* | ||
| 354 | debug("key["); debug_hex8(event.key.row); debug(":"); debug_hex8(event.key.col); | ||
| 355 | if (event.pressed) debug("]down\n"); else debug("]up\n"); | ||
| 356 | */ | ||
| 357 | |||
| 358 | /* When delaying layer switch */ | ||
| 359 | if (delaying_layer.action.code) { | ||
| 360 | /* Layer switch when tap time elapses or waiting key is released */ | ||
| 361 | if ((timer_elapsed(delaying_layer.event.time) > TAP_TIME) || | ||
| 362 | (!event.pressed && waiting_keys_has(event.key))) { | ||
| 363 | /* layer switch */ | ||
| 364 | switch (delaying_layer.action.kind.id) { | ||
| 365 | case ACT_LAYER_PRESSED: | ||
| 366 | layer_switch(delaying_layer.action.layer.opt); | ||
| 367 | break; | ||
| 368 | case ACT_LAYER_BIT: | ||
| 369 | layer_switch(current_layer | delaying_layer.action.layer.opt); | ||
| 370 | break; | ||
| 371 | } | ||
| 372 | delaying_layer = (keyrecord_t){}; | ||
| 373 | |||
| 374 | /* Process waiting keys in new layer */ | ||
| 375 | waiting_keys_process_in_current_layer(); | ||
| 376 | } | ||
| 377 | /* when delaying layer key is released within delay term */ | ||
| 378 | else if (!event.pressed && KEYEQ(event.key, delaying_layer.event.key)) { | ||
| 379 | /* tap key down */ | ||
| 380 | uint8_t tmp_mods = host_get_mods(); | ||
| 381 | host_set_mods(delaying_layer.mods); | ||
| 382 | register_code(delaying_layer.action.layer.code); | ||
| 383 | delaying_layer = (keyrecord_t){}; | ||
| 384 | host_set_mods(tmp_mods); | ||
| 385 | |||
| 386 | /* process waiting keys */ | ||
| 387 | waiting_keys_process_in_current_layer(); | ||
| 388 | } | ||
| 389 | } | ||
| 390 | |||
| 391 | // not real event. event just to update delaying layer. | ||
| 392 | if (IS_NOEVENT(event)) { | ||
| 393 | return; | ||
| 394 | } | ||
| 395 | |||
| 396 | /* count tap when key is up */ | ||
| 397 | if (KEYEQ(event.key, last_event.key) && timer_elapsed(last_event.time) <= TAP_TIME) { | ||
| 398 | if (!event.pressed) tap_count++; | ||
| 399 | } else { | ||
| 400 | tap_count = 0; | ||
| 401 | } | ||
| 402 | |||
| 403 | action_t action = keymap_get_action(current_layer, event.key.row, event.key.col); | ||
| 404 | |||
| 405 | // TODO: all key events(pressed, released) should be recorded? | ||
| 406 | /* postpone key-down events while delaying layer */ | ||
| 407 | if (delaying_layer.action.code) { | ||
| 408 | if (event.pressed) { | ||
| 409 | waiting_keys_enqueue(event, action); | ||
| 410 | } else { | ||
| 411 | process(event, action); | ||
| 412 | } | ||
| 413 | } else { | ||
| 414 | process(event, action); | ||
| 415 | } | ||
| 416 | |||
| 417 | /* last event */ | ||
| 418 | last_event = event; | ||
| 419 | } | ||
| 420 | |||
| 421 | |||
| 422 | static void register_code(uint8_t code) | 461 | static void register_code(uint8_t code) |
| 423 | { | 462 | { |
| 424 | if (code == KC_NO) { | 463 | if (code == KC_NO) { |
| @@ -449,6 +488,28 @@ static void unregister_code(uint8_t code) | |||
| 449 | } | 488 | } |
| 450 | } | 489 | } |
| 451 | 490 | ||
| 491 | static void add_mods(uint8_t mods) | ||
| 492 | { | ||
| 493 | if (mods) { | ||
| 494 | host_add_mods(mods); | ||
| 495 | host_send_keyboard_report(); | ||
| 496 | } | ||
| 497 | } | ||
| 498 | |||
| 499 | static void del_mods(uint8_t mods) | ||
| 500 | { | ||
| 501 | if (mods) { | ||
| 502 | host_del_mods(mods); | ||
| 503 | host_send_keyboard_report(); | ||
| 504 | } | ||
| 505 | } | ||
| 506 | |||
| 507 | static void set_mods(uint8_t mods) | ||
| 508 | { | ||
| 509 | host_set_mods(mods); | ||
| 510 | host_send_keyboard_report(); | ||
| 511 | } | ||
| 512 | |||
| 452 | static void clear_keyboard(void) | 513 | static void clear_keyboard(void) |
| 453 | { | 514 | { |
| 454 | host_clear_mods(); | 515 | host_clear_mods(); |
