vial/tap-dance: allow complex keycodes

This commit is contained in:
Ilya Zhuravlev 2021-07-03 19:48:01 -04:00
parent 274d9dcf07
commit e0c7388e5d
4 changed files with 82 additions and 57 deletions

View File

@ -341,9 +341,7 @@ void dynamic_keymap_set_buffer(uint16_t offset, uint16_t size, uint8_t *data) {
} }
} }
#ifdef VIAL_ENCODERS_ENABLE
extern uint16_t g_vial_magic_keycode_override; extern uint16_t g_vial_magic_keycode_override;
#endif
// This overrides the one in quantum/keymap_common.c // This overrides the one in quantum/keymap_common.c
uint16_t keymap_key_to_keycode(uint8_t layer, keypos_t key) { uint16_t keymap_key_to_keycode(uint8_t layer, keypos_t key) {
@ -351,10 +349,8 @@ uint16_t keymap_key_to_keycode(uint8_t layer, keypos_t key) {
/* Disable any keycode processing while unlocking */ /* Disable any keycode processing while unlocking */
if (vial_unlock_in_progress) if (vial_unlock_in_progress)
return KC_NO; return KC_NO;
#endif
#ifdef VIAL_ENCODERS_ENABLE if (key.row == VIAL_MATRIX_MAGIC && key.col == VIAL_MATRIX_MAGIC)
if (key.row == VIAL_ENCODER_MATRIX_MAGIC && key.col == VIAL_ENCODER_MATRIX_MAGIC)
return g_vial_magic_keycode_override; return g_vial_magic_keycode_override;
#endif #endif

View File

@ -224,37 +224,51 @@ void vial_handle_cmd(uint8_t *msg, uint8_t length) {
} }
} }
#ifdef VIAL_ENCODERS_ENABLE
uint16_t g_vial_magic_keycode_override; uint16_t g_vial_magic_keycode_override;
static void exec_keycode(uint16_t keycode) { static void vial_keycode_down(uint16_t keycode) {
#ifdef VIAL_ENCODER_SIMPLE_TAP
register_code16(keycode);
#if VIAL_ENCODER_KEYCODE_DELAY > 0
wait_ms(VIAL_ENCODER_KEYCODE_DELAY);
#endif
unregister_code16(keycode);
#else
g_vial_magic_keycode_override = keycode; g_vial_magic_keycode_override = keycode;
keyrecord_t record = {.event = (keyevent_t){.key = { VIAL_ENCODER_MATRIX_MAGIC, VIAL_ENCODER_MATRIX_MAGIC }, .pressed = true, .time = (timer_read() | 1)}}; if (keycode <= QK_MODS_MAX) {
if (keycode <= QK_MODS_MAX)
register_code16(keycode); register_code16(keycode);
else } else {
process_record_quantum_helper(keycode, &record); action_exec((keyevent_t){
.key = (keypos_t){.row = VIAL_MATRIX_MAGIC, .col = VIAL_MATRIX_MAGIC}, .pressed = 1, .time = (timer_read() | 1) /* time should not be 0 */
});
}
}
static void vial_keycode_up(uint16_t keycode) {
g_vial_magic_keycode_override = keycode;
if (keycode <= QK_MODS_MAX) {
unregister_code16(keycode);
} else {
action_exec((keyevent_t){
.key = (keypos_t){.row = VIAL_MATRIX_MAGIC, .col = VIAL_MATRIX_MAGIC}, .pressed = 0, .time = (timer_read() | 1) /* time should not be 0 */
});
}
}
static void vial_keycode_tap(uint16_t keycode) {
vial_keycode_down(keycode);
#if TAP_CODE_DELAY > 0
wait_ms(TAP_CODE_DELAY);
#endif
vial_keycode_up(keycode);
}
#ifdef VIAL_ENCODERS_ENABLE
static void exec_keycode(uint16_t keycode) {
vial_keycode_down(keycode);
#if VIAL_ENCODER_KEYCODE_DELAY > 0 #if VIAL_ENCODER_KEYCODE_DELAY > 0
wait_ms(VIAL_ENCODER_KEYCODE_DELAY); wait_ms(VIAL_ENCODER_KEYCODE_DELAY);
#endif #endif
record.event.time = timer_read() | 1;
record.event.pressed = false;
if (keycode <= QK_MODS_MAX) vial_keycode_up(keycode);
unregister_code16(keycode);
else
process_record_quantum_helper(keycode, &record);
#endif
} }
bool vial_encoder_update(uint8_t index, bool clockwise) { bool vial_encoder_update(uint8_t index, bool clockwise) {
@ -315,11 +329,11 @@ static void on_dance(qk_tap_dance_state_t *state, void *user_data) {
uint16_t kc = td_entry.on_tap; uint16_t kc = td_entry.on_tap;
if (kc) { if (kc) {
if (state->count == 3) { if (state->count == 3) {
tap_code16(kc); vial_keycode_tap(kc);
tap_code16(kc); vial_keycode_tap(kc);
tap_code16(kc); vial_keycode_tap(kc);
} else if (state->count > 3) { } else if (state->count > 3) {
tap_code16(kc); vial_keycode_tap(kc);
} }
} }
} }
@ -332,45 +346,45 @@ static void on_dance_finished(qk_tap_dance_state_t *state, void *user_data) {
switch (dance_state[index]) { switch (dance_state[index]) {
case SINGLE_TAP: { case SINGLE_TAP: {
if (td_entry.on_tap) if (td_entry.on_tap)
register_code16(td_entry.on_tap); vial_keycode_down(td_entry.on_tap);
break; break;
} }
case SINGLE_HOLD: { case SINGLE_HOLD: {
if (td_entry.on_hold) if (td_entry.on_hold)
register_code16(td_entry.on_hold); vial_keycode_down(td_entry.on_hold);
else if (td_entry.on_tap) else if (td_entry.on_tap)
register_code16(td_entry.on_tap); vial_keycode_down(td_entry.on_tap);
break; break;
} }
case DOUBLE_TAP: { case DOUBLE_TAP: {
if (td_entry.on_double_tap) { if (td_entry.on_double_tap) {
register_code16(td_entry.on_double_tap); vial_keycode_down(td_entry.on_double_tap);
} else if (td_entry.on_tap) { } else if (td_entry.on_tap) {
tap_code16(td_entry.on_tap); vial_keycode_tap(td_entry.on_tap);
register_code16(td_entry.on_tap); vial_keycode_down(td_entry.on_tap);
} }
break; break;
} }
case DOUBLE_HOLD: { case DOUBLE_HOLD: {
if (td_entry.on_tap_hold) { if (td_entry.on_tap_hold) {
register_code16(td_entry.on_tap_hold); vial_keycode_down(td_entry.on_tap_hold);
} else { } else {
if (td_entry.on_tap) { if (td_entry.on_tap) {
tap_code16(td_entry.on_tap); vial_keycode_tap(td_entry.on_tap);
if (td_entry.on_hold) if (td_entry.on_hold)
register_code16(td_entry.on_hold); vial_keycode_down(td_entry.on_hold);
else else
register_code16(td_entry.on_tap); vial_keycode_down(td_entry.on_tap);
} else if (td_entry.on_hold) { } else if (td_entry.on_hold) {
register_code16(td_entry.on_hold); vial_keycode_down(td_entry.on_hold);
} }
} }
break; break;
} }
case DOUBLE_SINGLE_TAP: { case DOUBLE_SINGLE_TAP: {
if (td_entry.on_tap) { if (td_entry.on_tap) {
tap_code16(td_entry.on_tap); vial_keycode_tap(td_entry.on_tap);
register_code16(td_entry.on_tap); vial_keycode_down(td_entry.on_tap);
} }
break; break;
} }
@ -381,50 +395,52 @@ static void on_dance_reset(qk_tap_dance_state_t *state, void *user_data) {
uint8_t index = (uintptr_t)user_data; uint8_t index = (uintptr_t)user_data;
if (dynamic_keymap_get_tap_dance(index, &td_entry) != 0) if (dynamic_keymap_get_tap_dance(index, &td_entry) != 0)
return; return;
switch (dance_state[index]) { uint8_t st = dance_state[index];
state->count = 0;
dance_state[index] = 0;
switch (st) {
case SINGLE_TAP: { case SINGLE_TAP: {
if (td_entry.on_tap) if (td_entry.on_tap)
unregister_code16(td_entry.on_tap); vial_keycode_up(td_entry.on_tap);
break; break;
} }
case SINGLE_HOLD: { case SINGLE_HOLD: {
if (td_entry.on_hold) if (td_entry.on_hold)
unregister_code16(td_entry.on_hold); vial_keycode_up(td_entry.on_hold);
else if (td_entry.on_tap) else if (td_entry.on_tap)
unregister_code16(td_entry.on_tap); vial_keycode_up(td_entry.on_tap);
break; break;
} }
case DOUBLE_TAP: { case DOUBLE_TAP: {
if (td_entry.on_double_tap) { if (td_entry.on_double_tap) {
unregister_code16(td_entry.on_double_tap); vial_keycode_up(td_entry.on_double_tap);
} else if (td_entry.on_tap) { } else if (td_entry.on_tap) {
unregister_code16(td_entry.on_tap); vial_keycode_up(td_entry.on_tap);
} }
break; break;
} }
case DOUBLE_HOLD: { case DOUBLE_HOLD: {
if (td_entry.on_tap_hold) { if (td_entry.on_tap_hold) {
unregister_code16(td_entry.on_tap_hold); vial_keycode_up(td_entry.on_tap_hold);
} else { } else {
if (td_entry.on_tap) { if (td_entry.on_tap) {
if (td_entry.on_hold) if (td_entry.on_hold)
unregister_code16(td_entry.on_hold); vial_keycode_up(td_entry.on_hold);
else else
unregister_code16(td_entry.on_tap); vial_keycode_up(td_entry.on_tap);
} else if (td_entry.on_hold) { } else if (td_entry.on_hold) {
unregister_code16(td_entry.on_hold); vial_keycode_up(td_entry.on_hold);
} }
} }
break; break;
} }
case DOUBLE_SINGLE_TAP: { case DOUBLE_SINGLE_TAP: {
if (td_entry.on_tap) { if (td_entry.on_tap) {
unregister_code16(td_entry.on_tap); vial_keycode_up(td_entry.on_tap);
} }
break; break;
} }
} }
dance_state[index] = 0;
} }
qk_tap_dance_action_t tap_dance_actions[VIAL_TAP_DANCE_ENTRIES]; qk_tap_dance_action_t tap_dance_actions[VIAL_TAP_DANCE_ENTRIES];

View File

@ -54,8 +54,9 @@ enum {
dynamic_vial_tap_dance_set = 0x02, dynamic_vial_tap_dance_set = 0x02,
}; };
/* Fake encoder position in keyboard matrix, can't use 255 as that is immediately rejected by IS_NOEVENT */ /* Fake position in keyboard matrix, can't use 255 as that is immediately rejected by IS_NOEVENT
#define VIAL_ENCODER_MATRIX_MAGIC 254 used to send arbitrary keycodes thru process_record_quantum_helper */
#define VIAL_MATRIX_MAGIC 254
#ifdef TAP_DANCE_ENABLE #ifdef TAP_DANCE_ENABLE

View File

@ -10,6 +10,10 @@
# include "nodebug.h" # include "nodebug.h"
#endif #endif
#ifdef VIAL_ENABLE
#include "vial.h"
#endif
/** \brief Default Layer State /** \brief Default Layer State
*/ */
layer_state_t default_layer_state = 0; layer_state_t default_layer_state = 0;
@ -192,6 +196,10 @@ uint8_t source_layers_cache[(MATRIX_ROWS * MATRIX_COLS + 7) / 8][MAX_LAYER_BITS]
* Updates the cached keys when changing layers * Updates the cached keys when changing layers
*/ */
void update_source_layers_cache(keypos_t key, uint8_t layer) { void update_source_layers_cache(keypos_t key, uint8_t layer) {
#ifdef VIAL_ENABLE
if (key.row == VIAL_MATRIX_MAGIC) return;
#endif
const uint8_t key_number = key.col + (key.row * MATRIX_COLS); const uint8_t key_number = key.col + (key.row * MATRIX_COLS);
const uint8_t storage_row = key_number / 8; const uint8_t storage_row = key_number / 8;
const uint8_t storage_bit = key_number % 8; const uint8_t storage_bit = key_number % 8;
@ -206,6 +214,10 @@ void update_source_layers_cache(keypos_t key, uint8_t layer) {
* reads the cached keys stored when the layer was changed * reads the cached keys stored when the layer was changed
*/ */
uint8_t read_source_layers_cache(keypos_t key) { uint8_t read_source_layers_cache(keypos_t key) {
#ifdef VIAL_ENABLE
if (key.row == VIAL_MATRIX_MAGIC) return 0;
#endif
const uint8_t key_number = key.col + (key.row * MATRIX_COLS); const uint8_t key_number = key.col + (key.row * MATRIX_COLS);
const uint8_t storage_row = key_number / 8; const uint8_t storage_row = key_number / 8;
const uint8_t storage_bit = key_number % 8; const uint8_t storage_bit = key_number % 8;