From eac8fa799909817bfc7cb4043448f85551154c6b Mon Sep 17 00:00:00 2001 From: Ofer Plesser Date: Sat, 10 Dec 2016 00:49:11 +0200 Subject: Implemented basic key combination feature --- quantum/process_keycode/process_combo.c | 66 +++++++++++++++++++++++++++++++++ quantum/process_keycode/process_combo.h | 25 +++++++++++++ 2 files changed, 91 insertions(+) create mode 100644 quantum/process_keycode/process_combo.c create mode 100644 quantum/process_keycode/process_combo.h (limited to 'quantum/process_keycode') diff --git a/quantum/process_keycode/process_combo.c b/quantum/process_keycode/process_combo.c new file mode 100644 index 000000000..a6cfed11a --- /dev/null +++ b/quantum/process_keycode/process_combo.c @@ -0,0 +1,66 @@ +#include "process_combo.h" +#include "print.h" + +// __attribute__ ((weak)) +// combo_t key_combos[] = { + +// }; + +#define SEND_KEY(key) \ +do { \ + register_code16(key); \ + send_keyboard_report(); \ + unregister_code16(key); \ +} while(0) + + +#define ALL_COMBO_KEYS_ARE_DOWN (((1<state) +static bool process_single_combo(combo_t *combo, uint16_t keycode, keyrecord_t *record) +{ + uint8_t count = 0; + bool is_combo_key = false; + // bool combo_key_released = false; + + // Count the number of combo keys + for (const uint16_t *key = combo->keys; COMBO_END != pgm_read_word(key); ++key, ++count); + + for (uint8_t i = 0; i < count; ++i) { + uint16_t key = pgm_read_word(&combo->keys[i]); + + if (key == keycode) { + is_combo_key = true; + + if (record->event.pressed) { + combo->state |= (1<state) { + // The combo was sent, no need to send released key + return true; + } + + combo->state &= ~(1<action); + combo->state = 0; + } + + return is_combo_key; +} + + +bool process_combo(uint16_t keycode, keyrecord_t *record) +{ + bool is_combo_key = false; + + for (int i = 0; i < NUM_ELEMS(key_combos); ++i) { + combo_t *combo = &key_combos[i]; + is_combo_key |= process_single_combo(combo, keycode, record); + } + + return !is_combo_key; +} \ No newline at end of file diff --git a/quantum/process_keycode/process_combo.h b/quantum/process_keycode/process_combo.h new file mode 100644 index 000000000..68786c0f1 --- /dev/null +++ b/quantum/process_keycode/process_combo.h @@ -0,0 +1,25 @@ +#ifndef PROCESS_COMBO_H +#define PROCESS_COMBO_H + +#include +#include "progmem.h" +#include "quantum.h" + + +typedef struct +{ + const uint16_t *keys; + uint16_t action; + uint32_t state; +} combo_t; + + +#define COMBO_END 0 +#define NUM_ELEMS(a) (sizeof(a)/sizeof 0[a]) + + +extern combo_t key_combos[1]; + +bool process_combo(uint16_t keycode, keyrecord_t *record); + +#endif \ No newline at end of file -- cgit v1.2.3-24-g4f1b From b6bf4e0dce062a535685c4e772f613252d401ed3 Mon Sep 17 00:00:00 2001 From: Ofer Plesser Date: Sat, 10 Dec 2016 16:11:59 +0200 Subject: Added support for timing out combos if a key as been pressed for longer than COMBO_TERM --- quantum/process_keycode/process_combo.c | 107 +++++++++++++++++++++++--------- quantum/process_keycode/process_combo.h | 20 +++++- 2 files changed, 96 insertions(+), 31 deletions(-) (limited to 'quantum/process_keycode') diff --git a/quantum/process_keycode/process_combo.c b/quantum/process_keycode/process_combo.c index a6cfed11a..ff7e8aba5 100644 --- a/quantum/process_keycode/process_combo.c +++ b/quantum/process_keycode/process_combo.c @@ -1,11 +1,6 @@ #include "process_combo.h" #include "print.h" -// __attribute__ ((weak)) -// combo_t key_combos[] = { - -// }; - #define SEND_KEY(key) \ do { \ register_code16(key); \ @@ -13,54 +8,110 @@ do { \ unregister_code16(key); \ } while(0) +#define COMBO_TIMER_ELAPSED -1 + +#if COMBO_TERM +#define IS_COMBO_KEY_HELD(combo) (COMBO_TIMER_ELAPSED == combo->timer ? false : true) +#define RESET_COMBO_TIMER_AND_KEY(combo) combo->timer = 0; combo->key = 0 +#else +#define IS_COMBO_KEY_HELD(combo) (true) +#define RESET_COMBO_TIMER_AND_KEY(combo) do {} while (0) +#endif + + +__attribute__ ((weak)) +combo_t key_combos[COMBO_COUNT] = { + +}; + +static inline void reset_combo(combo_t *combo) +{ + combo->state = 0; + RESET_COMBO_TIMER_AND_KEY(combo); +} #define ALL_COMBO_KEYS_ARE_DOWN (((1<state) +#define NO_COMBO_KEYS_ARE_DOWN (0 == combo->state) +#define KEY_STATE_DOWN(key) do{ combo->state |= (1<state &= ~(1<keys; ;++count) { + uint16_t key = pgm_read_word(&keys[count]); + if (keycode == key) index = count; + if (COMBO_END == key) break; + } - // Count the number of combo keys - for (const uint16_t *key = combo->keys; COMBO_END != pgm_read_word(key); ++key, ++count); + /* Return if not a combo key */ + if (-1 == index) return false; - for (uint8_t i = 0; i < count; ++i) { - uint16_t key = pgm_read_word(&combo->keys[i]); + bool is_combo_active = IS_COMBO_KEY_HELD(combo); - if (key == keycode) { - is_combo_key = true; + if (record->event.pressed) { + KEY_STATE_DOWN(index); + +#if COMBO_TERM + if (is_combo_active) { + combo->timer = timer_read(); + combo->key = keycode; + } +#endif - if (record->event.pressed) { - combo->state |= (1<state) { - // The combo was sent, no need to send released key - return true; - } + } else { + if (is_combo_active && combo->state) { /* Combo key was tapped */ + RESET_COMBO_TIMER_AND_KEY(combo); + SEND_KEY(keycode); + } - combo->state &= ~(1<key) { /* Held combo key was released */ + unregister_code16(combo->key); } +#endif + + KEY_STATE_UP(index); } - if (ALL_COMBO_KEYS_ARE_DOWN) { + if (ALL_COMBO_KEYS_ARE_DOWN && is_combo_active) { SEND_KEY(combo->action); - combo->state = 0; + reset_combo(combo); + } + + if(NO_COMBO_KEYS_ARE_DOWN && !is_combo_active) { + reset_combo(combo); } - return is_combo_key; + return is_combo_active; } - bool process_combo(uint16_t keycode, keyrecord_t *record) { bool is_combo_key = false; - for (int i = 0; i < NUM_ELEMS(key_combos); ++i) { + for (int i = 0; i < COMBO_COUNT; ++i) { combo_t *combo = &key_combos[i]; is_combo_key |= process_single_combo(combo, keycode, record); } return !is_combo_key; +} + +void matrix_scan_combo(void) +{ +#if COMBO_TERM + for (int i = 0; i < COMBO_COUNT; ++i) { + combo_t *combo = &key_combos[i]; + if (combo->timer && + combo->timer != COMBO_TIMER_ELAPSED && + timer_elapsed(combo->timer) > COMBO_TERM) { + + combo->timer = COMBO_TIMER_ELAPSED; + unregister_code16(combo->key); + register_code16(combo->key); + } + } +#endif } \ No newline at end of file diff --git a/quantum/process_keycode/process_combo.h b/quantum/process_keycode/process_combo.h index 68786c0f1..c475acd33 100644 --- a/quantum/process_keycode/process_combo.h +++ b/quantum/process_keycode/process_combo.h @@ -5,21 +5,35 @@ #include "progmem.h" #include "quantum.h" +#ifndef COMBO_TERM +#define COMBO_TERM TAPPING_TERM +#endif typedef struct { const uint16_t *keys; uint16_t action; uint32_t state; +#if COMBO_TERM + uint16_t timer; + uint16_t key; +#endif } combo_t; +#if COMBO_TERM +#define COMBO(ck, ca) {.keys = &(ck)[0], .action = (ca), .state = 0, .timer = 0, .key = 0} +#else +#define COMBO(ck, ca) {.keys = &(ck)[0], .action = (ca), .state = 0 } +#endif #define COMBO_END 0 -#define NUM_ELEMS(a) (sizeof(a)/sizeof 0[a]) +#ifndef COMBO_COUNT +#define COMBO_COUNT 0 +#endif - -extern combo_t key_combos[1]; +extern combo_t key_combos[COMBO_COUNT]; bool process_combo(uint16_t keycode, keyrecord_t *record); +void matrix_scan_combo(void); #endif \ No newline at end of file -- cgit v1.2.3-24-g4f1b From 6e7cfa83b9424061914793b02757fa4ec75b356b Mon Sep 17 00:00:00 2001 From: Ofer Plesser Date: Fri, 16 Dec 2016 21:50:28 +0200 Subject: Refactored as well as added support for action keys in combos --- quantum/process_keycode/process_combo.c | 123 ++++++++++++++++++-------------- quantum/process_keycode/process_combo.h | 34 +++++---- 2 files changed, 89 insertions(+), 68 deletions(-) (limited to 'quantum/process_keycode') diff --git a/quantum/process_keycode/process_combo.c b/quantum/process_keycode/process_combo.c index ff7e8aba5..e2189ad98 100644 --- a/quantum/process_keycode/process_combo.c +++ b/quantum/process_keycode/process_combo.c @@ -1,39 +1,39 @@ #include "process_combo.h" #include "print.h" -#define SEND_KEY(key) \ -do { \ - register_code16(key); \ - send_keyboard_report(); \ - unregister_code16(key); \ -} while(0) #define COMBO_TIMER_ELAPSED -1 -#if COMBO_TERM -#define IS_COMBO_KEY_HELD(combo) (COMBO_TIMER_ELAPSED == combo->timer ? false : true) -#define RESET_COMBO_TIMER_AND_KEY(combo) combo->timer = 0; combo->key = 0 -#else -#define IS_COMBO_KEY_HELD(combo) (true) -#define RESET_COMBO_TIMER_AND_KEY(combo) do {} while (0) -#endif - __attribute__ ((weak)) -combo_t key_combos[COMBO_COUNT] = { +combo_t key_combos[] = { }; -static inline void reset_combo(combo_t *combo) +__attribute__ ((weak)) +void process_combo_event(uint8_t combo_index, bool pressed) { + +} + +static uint8_t current_combo_index = 0; + +static inline void send_combo(uint16_t action, bool pressed) { - combo->state = 0; - RESET_COMBO_TIMER_AND_KEY(combo); + if (action) { + if (pressed) { + register_code16(action); + } else { + unregister_code16(action); + } + } else { + process_combo_event(current_combo_index, pressed); + } } -#define ALL_COMBO_KEYS_ARE_DOWN (((1<state) -#define NO_COMBO_KEYS_ARE_DOWN (0 == combo->state) -#define KEY_STATE_DOWN(key) do{ combo->state |= (1<state &= ~(1<state) +#define NO_COMBO_KEYS_ARE_DOWN (0 == combo->state) +#define KEY_STATE_DOWN(key) do{ combo->state |= (1<state &= ~(1<timer ? false : true; if (record->event.pressed) { KEY_STATE_DOWN(index); - -#if COMBO_TERM + if (is_combo_active) { - combo->timer = timer_read(); - combo->key = keycode; - } + if (ALL_COMBO_KEYS_ARE_DOWN) { /* Combo was pressed */ + send_combo(combo->keycode, true); + combo->timer = COMBO_TIMER_ELAPSED; + } else { /* Combo key was pressed */ + combo->timer = timer_read(); +#ifdef COMBO_ALLOW_ACTION_KEYS + combo->prev_record = *record; +#else + combo->prev_key = keycode; #endif - + } + } } else { - if (is_combo_active && combo->state) { /* Combo key was tapped */ - RESET_COMBO_TIMER_AND_KEY(combo); - SEND_KEY(keycode); + if (ALL_COMBO_KEYS_ARE_DOWN) { /* Combo was released */ + send_combo(combo->keycode, false); } -#if COMBO_TERM - if (!is_combo_active && keycode == combo->key) { /* Held combo key was released */ - unregister_code16(combo->key); - } + if (is_combo_active) { /* Combo key was tapped */ +#ifdef COMBO_ALLOW_ACTION_KEYS + record->event.pressed = true; + process_action(record, store_or_get_action(record->event.pressed, record->event.key)); + record->event.pressed = false; + process_action(record, store_or_get_action(record->event.pressed, record->event.key)); +#else + register_code16(keycode); + send_keyboard_report(); + unregister_code16(keycode); #endif + combo->timer = 0; + } - KEY_STATE_UP(index); + KEY_STATE_UP(index); } - if (ALL_COMBO_KEYS_ARE_DOWN && is_combo_active) { - SEND_KEY(combo->action); - reset_combo(combo); - } - - if(NO_COMBO_KEYS_ARE_DOWN && !is_combo_active) { - reset_combo(combo); + if (NO_COMBO_KEYS_ARE_DOWN) { + combo->timer = 0; } return is_combo_active; @@ -91,8 +100,8 @@ bool process_combo(uint16_t keycode, keyrecord_t *record) { bool is_combo_key = false; - for (int i = 0; i < COMBO_COUNT; ++i) { - combo_t *combo = &key_combos[i]; + for (current_combo_index = 0; current_combo_index < COMBO_COUNT; ++current_combo_index) { + combo_t *combo = &key_combos[current_combo_index]; is_combo_key |= process_single_combo(combo, keycode, record); } @@ -101,17 +110,25 @@ bool process_combo(uint16_t keycode, keyrecord_t *record) void matrix_scan_combo(void) { -#if COMBO_TERM for (int i = 0; i < COMBO_COUNT; ++i) { combo_t *combo = &key_combos[i]; if (combo->timer && combo->timer != COMBO_TIMER_ELAPSED && timer_elapsed(combo->timer) > COMBO_TERM) { - + + /* This disables the combo, meaning key events for this + * combo will be handled by the next processors in the chain + */ combo->timer = COMBO_TIMER_ELAPSED; - unregister_code16(combo->key); - register_code16(combo->key); + +#ifdef COMBO_ALLOW_ACTION_KEYS + process_action(&combo->prev_record, + store_or_get_action(combo->prev_record.event.pressed, + combo->prev_record.event.key)); +#else + unregister_code16(combo->prev_key); + register_code16(combo->prev_key); +#endif } } -#endif -} \ No newline at end of file +} diff --git a/quantum/process_keycode/process_combo.h b/quantum/process_keycode/process_combo.h index c475acd33..847f2b737 100644 --- a/quantum/process_keycode/process_combo.h +++ b/quantum/process_keycode/process_combo.h @@ -5,35 +5,39 @@ #include "progmem.h" #include "quantum.h" -#ifndef COMBO_TERM -#define COMBO_TERM TAPPING_TERM -#endif - typedef struct { const uint16_t *keys; - uint16_t action; + uint16_t keycode; +#ifdef EXTRA_EXTRA_LONG_COMBOS uint32_t state; -#if COMBO_TERM +#elif EXTRA_LONG_COMBOS + uint16_t state; +#else + uint8_t state; +#endif uint16_t timer; - uint16_t key; +#ifdef COMBO_ALLOW_ACTION_KEYS + keyrecord_t prev_record; +#else + uint16_t prev_key; #endif } combo_t; -#if COMBO_TERM -#define COMBO(ck, ca) {.keys = &(ck)[0], .action = (ca), .state = 0, .timer = 0, .key = 0} -#else -#define COMBO(ck, ca) {.keys = &(ck)[0], .action = (ca), .state = 0 } -#endif +#define COMBO(ck, ca) {.keys = &(ck)[0], .keycode = (ca)} +#define COMBO_ACTION(ck) {.keys = &(ck)[0]} + #define COMBO_END 0 #ifndef COMBO_COUNT #define COMBO_COUNT 0 #endif - -extern combo_t key_combos[COMBO_COUNT]; +#ifndef COMBO_TERM +#define COMBO_TERM TAPPING_TERM +#endif bool process_combo(uint16_t keycode, keyrecord_t *record); void matrix_scan_combo(void); +void process_combo_event(uint8_t combo_index, bool pressed); -#endif \ No newline at end of file +#endif -- cgit v1.2.3-24-g4f1b From 0aa413af44b292e4b44d8f8aee1a92f2cb113438 Mon Sep 17 00:00:00 2001 From: Jonas Oberschweiber Date: Sat, 31 Dec 2016 19:37:56 +0100 Subject: Add support for supplementary planes for OS X --- quantum/process_keycode/process_unicode.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) (limited to 'quantum/process_keycode') diff --git a/quantum/process_keycode/process_unicode.c b/quantum/process_keycode/process_unicode.c index cd3a610b4..2606cf0c8 100644 --- a/quantum/process_keycode/process_unicode.c +++ b/quantum/process_keycode/process_unicode.c @@ -116,7 +116,16 @@ bool process_unicode_map(uint16_t keycode, keyrecord_t *record) { const uint32_t* map = unicode_map; uint16_t index = keycode & 0x7FF; uint32_t code = pgm_read_dword_far(&map[index]); - if ((code > 0xFFFF && input_mode == UC_OSX) || (code > 0xFFFFF && input_mode == UC_LNX)) { + if (code > 0xFFFF && code <= 0x10ffff && input_mode == UC_OSX) { + // Convert to UTF-16 surrogate pair + code -= 0x10000; + uint32_t lo = code & 0x3ff; + uint32_t hi = (code & 0xffc00) >> 10; + unicode_input_start(); + register_hex32(hi + 0xd800); + register_hex32(lo + 0xdc00); + unicode_input_finish(); + } else if ((code > 0x10ffff && input_mode == UC_OSX) || (code > 0xFFFFF && input_mode == UC_LNX)) { // when character is out of range supported by the OS unicode_map_input_error(); } else { -- cgit v1.2.3-24-g4f1b From 5a860b71a1943358d0722ace9d2c13bd5c77c971 Mon Sep 17 00:00:00 2001 From: SjB Date: Sun, 29 Jan 2017 13:04:43 -0500 Subject: race condition between oneshot_mods and tap_dance since the keycode for a tap dance process gets process only after the TAPPING_TERM timeout, you really only have ONESHOT_TIMEOUT - TAPPING_TERM time to tap or double tap on the key. This fix save the oneshot_mods into the action.state structure and applies the mods with the keycode when it's registered. It also unregisters the mod when the the tap dance process gets reset. --- quantum/process_keycode/process_tap_dance.c | 7 ++++++- quantum/process_keycode/process_tap_dance.h | 1 + 2 files changed, 7 insertions(+), 1 deletion(-) (limited to 'quantum/process_keycode') diff --git a/quantum/process_keycode/process_tap_dance.c b/quantum/process_keycode/process_tap_dance.c index 6ae362c4c..403dca538 100644 --- a/quantum/process_keycode/process_tap_dance.c +++ b/quantum/process_keycode/process_tap_dance.c @@ -43,12 +43,16 @@ static inline void process_tap_dance_action_on_dance_finished (qk_tap_dance_acti if (action->state.finished) return; action->state.finished = true; + add_mods(action->state.oneshot_mods); + send_keyboard_report(); _process_tap_dance_action_fn (&action->state, action->user_data, action->fn.on_dance_finished); } static inline void process_tap_dance_action_on_reset (qk_tap_dance_action_t *action) { _process_tap_dance_action_fn (&action->state, action->user_data, action->fn.on_reset); + del_mods(action->state.oneshot_mods); + send_keyboard_report(); } bool process_tap_dance(uint16_t keycode, keyrecord_t *record) { @@ -70,6 +74,7 @@ bool process_tap_dance(uint16_t keycode, keyrecord_t *record) { action->state.keycode = keycode; action->state.count++; action->state.timer = timer_read(); + action->state.oneshot_mods = get_oneshot_mods(); process_tap_dance_action_on_each_tap (action); if (last_td && last_td != keycode) { @@ -109,7 +114,7 @@ void matrix_scan_tap_dance () { if (highest_td == -1) return; - for (int i = 0; i <= highest_td; i++) { +for (int i = 0; i <= highest_td; i++) { qk_tap_dance_action_t *action = &tap_dance_actions[i]; if (action->state.count && timer_elapsed (action->state.timer) > TAPPING_TERM) { diff --git a/quantum/process_keycode/process_tap_dance.h b/quantum/process_keycode/process_tap_dance.h index f753cbba6..726752ecc 100644 --- a/quantum/process_keycode/process_tap_dance.h +++ b/quantum/process_keycode/process_tap_dance.h @@ -9,6 +9,7 @@ typedef struct { uint8_t count; + uint8_t oneshot_mods; uint16_t keycode; uint16_t timer; bool interrupted; -- cgit v1.2.3-24-g4f1b From 97816df7e7aa8710c8a0837b2abe008e0c765f2a Mon Sep 17 00:00:00 2001 From: Priyadi Iman Nurcahyo Date: Fri, 10 Feb 2017 06:06:59 +0700 Subject: Implement tap mod dual role for right side mods. --- quantum/process_keycode/process_unicode.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'quantum/process_keycode') diff --git a/quantum/process_keycode/process_unicode.c b/quantum/process_keycode/process_unicode.c index 9995ba9bd..9d01a592d 100644 --- a/quantum/process_keycode/process_unicode.c +++ b/quantum/process_keycode/process_unicode.c @@ -139,7 +139,7 @@ void unicode_map_input_error() {} bool process_unicode_map(uint16_t keycode, keyrecord_t *record) { if ((keycode & QK_UNICODE_MAP) == QK_UNICODE_MAP && record->event.pressed) { const uint32_t* map = unicode_map; - uint16_t index = keycode & 0x7FF; + uint16_t index = keycode - QK_UNICODE_MAP; uint32_t code = pgm_read_dword_far(&map[index]); if (code > 0xFFFF && code <= 0x10ffff && input_mode == UC_OSX) { // Convert to UTF-16 surrogate pair -- cgit v1.2.3-24-g4f1b