From d52d554360d3bf06189bfd4f386fa99348d8a0a8 Mon Sep 17 00:00:00 2001 From: tmk Date: Fri, 4 Oct 2013 03:30:09 +0900 Subject: [PATCH] Fix mod stuck of MODS_KEY when leaving layer #62 - Add action_util.c and remove action_oneshot.c - Add oneshot_mods for MODS_ONESHOT - Add weak_mods for MODS_KEY and MACRO - weak_mods is cleared when layer switching --- common.mk | 2 +- common/action.c | 138 ++++++++++++++--------------- common/action.h | 6 +- common/action_code.h | 6 +- common/action_oneshot.c | 21 ----- common/action_oneshot.h | 52 ----------- common/action_util.c | 192 ++++++++++++++++++++++++++++++++++++++++ common/action_util.h | 56 ++++++++++++ common/command.c | 1 + common/host.c | 150 +------------------------------ common/host.h | 17 ---- doc/keymap.md | 12 +-- 12 files changed, 330 insertions(+), 323 deletions(-) delete mode 100644 common/action_oneshot.c delete mode 100644 common/action_oneshot.h create mode 100644 common/action_util.c create mode 100644 common/action_util.h diff --git a/common.mk b/common.mk index 3403fd7a..5b70db94 100644 --- a/common.mk +++ b/common.mk @@ -3,9 +3,9 @@ SRC += $(COMMON_DIR)/host.c \ $(COMMON_DIR)/keyboard.c \ $(COMMON_DIR)/action.c \ $(COMMON_DIR)/action_tapping.c \ - $(COMMON_DIR)/action_oneshot.c \ $(COMMON_DIR)/action_macro.c \ $(COMMON_DIR)/action_layer.c \ + $(COMMON_DIR)/action_util.c \ $(COMMON_DIR)/keymap.c \ $(COMMON_DIR)/timer.c \ $(COMMON_DIR)/print.c \ diff --git a/common/action.c b/common/action.c index 59c6f252..ecd5a7e9 100644 --- a/common/action.c +++ b/common/action.c @@ -23,8 +23,8 @@ along with this program. If not, see . #include "backlight.h" #include "action_layer.h" #include "action_tapping.h" -#include "action_oneshot.h" #include "action_macro.h" +#include "action_util.h" #include "action.h" #ifdef DEBUG_ACTION @@ -79,15 +79,15 @@ void process_action(keyrecord_t *record) action.key.mods<<4; if (event.pressed) { if (mods) { - host_add_mods(mods); - host_send_keyboard_report(); + add_weak_mods(mods); + send_keyboard_report(); } register_code(action.key.code); } else { unregister_code(action.key.code); if (mods) { - host_del_mods(mods); - host_send_keyboard_report(); + del_weak_mods(mods); + send_keyboard_report(); } } } @@ -105,11 +105,11 @@ void process_action(keyrecord_t *record) if (event.pressed) { if (tap_count == 0) { dprint("MODS_TAP: Oneshot: add_mods\n"); - add_mods(mods); + register_mods(mods); } else if (tap_count == 1) { dprint("MODS_TAP: Oneshot: start\n"); - oneshot_start(mods); + set_oneshot_mods(mods); } else if (tap_count == TAPPING_TOGGLE) { dprint("MODS_TAP: Oneshot: toggle\n"); @@ -118,25 +118,23 @@ void process_action(keyrecord_t *record) else { dprint("MODS_TAP: Oneshot: cancel&add_mods\n"); // double tap cancels oneshot and works as normal modifier. - oneshot_cancel(); - add_mods(mods); + clear_oneshot_mods(); + register_mods(mods); } } else { if (tap_count == 0) { dprint("MODS_TAP: Oneshot: cancel/del_mods\n"); // cancel oneshot on hold - oneshot_cancel(); - del_mods(mods); + clear_oneshot_mods(); + unregister_mods(mods); } else if (tap_count == 1) { - dprint("MODS_TAP: Oneshot: del_mods\n"); - // retain Oneshot - del_mods(mods); + // Oneshot } else { dprint("MODS_TAP: Oneshot: del_mods\n"); // cancel Mods - del_mods(mods); + unregister_mods(mods); } } break; @@ -148,14 +146,14 @@ void process_action(keyrecord_t *record) dprint("MODS_TAP: Tap: Cancel: add_mods\n"); // ad hoc: set 0 to cancel tap record->tap.count = 0; - add_mods(mods); + register_mods(mods); } else { dprint("MODS_TAP: Tap: register_code\n"); register_code(action.key.code); } } else { dprint("MODS_TAP: No tap: add_mods\n"); - add_mods(mods); + register_mods(mods); } } else { if (tap_count > 0) { @@ -163,7 +161,7 @@ void process_action(keyrecord_t *record) unregister_code(action.key.code); } else { dprint("MODS_TAP: No tap: add_mods\n"); - del_mods(mods); + unregister_mods(mods); } } break; @@ -343,30 +341,30 @@ void register_code(uint8_t code) // Resync: ignore if caps lock already is on if (host_keyboard_leds() & (1<. * 000r|0000|0000 0001 Transparent code * 000r|0000| keycode Key * 000r|mods|0000 0000 Modifiers - * 000r|mods| keycode Key and Modifiers + * 000r|mods| keycode Modifiers+Key(Modified key) * r: Left/Right flag(Left:0, Right:1) * * ACT_MODS_TAP(001r): * 001r|mods|0000 0000 Modifiers with OneShot * 001r|mods|0000 00xx (reserved) - * 001r|mods| keycode Modifiers with Tap Key + * 001r|mods| keycode Modifiers with Tap Key(Dual role) * * * Other Keys(01xx) @@ -69,7 +69,7 @@ along with this program. If not, see . * 1001|oopp|BBBB BBBB 8-bit Bitwise Operation??? * * ACT_LAYER_TAP(101x): - * 101E|LLLL| keycode Invert with tap key + * 101E|LLLL| keycode On/Off with tap key * 101E|LLLL|1110 xxxx Reserved(0xE0-EF) * 101E|LLLL|1111 0000 Invert with tap toggle(0xF0) * 101E|LLLL|1111 0001 On/Off diff --git a/common/action_oneshot.c b/common/action_oneshot.c deleted file mode 100644 index d34f44b5..00000000 --- a/common/action_oneshot.c +++ /dev/null @@ -1,21 +0,0 @@ -#include "action_oneshot.h" - - -#ifndef NO_ACTION_ONESHOT -oneshot_state_t oneshot_state; - -void oneshot_start(uint8_t mods) -{ - oneshot_state.mods = mods; -} - -void oneshot_cancel(void) -{ - oneshot_state.mods = 0; -} - -void oneshot_toggle(void) -{ - oneshot_state.disabled = !oneshot_state.disabled; -} -#endif diff --git a/common/action_oneshot.h b/common/action_oneshot.h deleted file mode 100644 index 36ef9e9b..00000000 --- a/common/action_oneshot.h +++ /dev/null @@ -1,52 +0,0 @@ -/* -Copyright 2013 Jun Wako - -This program is free software: you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation, either version 2 of the License, or -(at your option) any later version. - -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. - -You should have received a copy of the GNU General Public License -along with this program. If not, see . -*/ -#ifndef ACTION_ONESHOT_H -#define ACTION_ONESHOT_H - -#include -#include - -#ifdef NO_ACTION_TAPPING - #define NO_ACTION_ONESHOT -#endif - -#ifndef NO_ACTION_ONESHOT -/* Oneshot modifier - * - * Problem: Want to capitalize like 'The' but the result tends to be 'THe'. - * Solution: Oneshot modifier have its effect on only one key coming next. - * Tap Shift, then type 't', 'h' and 'e'. Not need to hold Shift key. - * - * Hold: works as normal modifier. - * Tap: one shot modifier. - * 2 Tap: cancel one shot modifier. - * 5-Tap: toggles enable/disable oneshot feature. - */ -typedef struct { - uint8_t mods; - bool disabled; -} oneshot_state_t; - - -oneshot_state_t oneshot_state; - -void oneshot_start(uint8_t mods); -void oneshot_cancel(void); -void oneshot_toggle(void); -#endif - -#endif diff --git a/common/action_util.c b/common/action_util.c new file mode 100644 index 00000000..50d686a0 --- /dev/null +++ b/common/action_util.c @@ -0,0 +1,192 @@ +/* +Copyright 2013 Jun Wako + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . +*/ +#include "host.h" +#include "report.h" +#include "debug.h" +#include "action_util.h" + +static inline void add_key_byte(uint8_t code); +static inline void del_key_byte(uint8_t code); +#ifdef NKRO_ENABLE +static inline void add_key_bit(uint8_t code); +static inline void del_key_bit(uint8_t code); +#endif + +static uint8_t real_mods = 0; +static uint8_t weak_mods = 0; + + +// TODO: pointer variable is not needed +//report_keyboard_t keyboard_report = {}; +report_keyboard_t *keyboard_report = &(report_keyboard_t){}; + +#ifndef NO_ACTION_ONESHOT +static bool oneshot_enabled = true; +static int8_t oneshot_mods = 0; +#endif + +void send_keyboard_report(void) { + keyboard_report->mods = real_mods; + keyboard_report->mods |= weak_mods; +#ifndef NO_ACTION_ONESHOT + keyboard_report->mods |= oneshot_mods; + if (has_anykey()) { + clear_oneshot_mods(); + } +#endif + host_keyboard_send(keyboard_report); +} + +/* key */ +void add_key(uint8_t key) +{ +#ifdef NKRO_ENABLE + if (keyboard_nkro) { + add_key_bit(key); + return; + } +#endif + add_key_byte(key); +} + +void del_key(uint8_t key) +{ +#ifdef NKRO_ENABLE + if (keyboard_nkro) { + del_key_bit(key); + return; + } +#endif + del_key_byte(key); +} + +void clear_keys(void) +{ + // not clear mods + for (int8_t i = 1; i < REPORT_SIZE; i++) { + keyboard_report->raw[i] = 0; + } +} + + +/* modifier */ +uint8_t get_mods(void) { return real_mods; } +void add_mods(uint8_t mods) { real_mods |= mods; } +void del_mods(uint8_t mods) { real_mods &= ~mods; } +void set_mods(uint8_t mods) { real_mods = mods; } +void clear_mods(void) { real_mods = 0; } + +/* weak modifier */ +uint8_t get_weak_mods(void) { return weak_mods; } +void add_weak_mods(uint8_t mods) { weak_mods |= mods; } +void del_weak_mods(uint8_t mods) { weak_mods &= ~mods; } +void set_weak_mods(uint8_t mods) { weak_mods = mods; } +void clear_weak_mods(void) { weak_mods = 0; } + +/* Oneshot modifier */ +#ifndef NO_ACTION_ONESHOT +void set_oneshot_mods(uint8_t mods) { oneshot_mods = mods; } +void clear_oneshot_mods(void) { oneshot_mods = 0; } +void oneshot_toggle(void) { oneshot_enabled = !oneshot_enabled; } +void oneshot_enable(void) { oneshot_enabled = true; } +void oneshot_disable(void) { oneshot_enabled = false; } +#endif + + + + +/* + * inspect keyboard state + */ +uint8_t has_anykey(void) +{ + uint8_t cnt = 0; + for (uint8_t i = 1; i < REPORT_SIZE; i++) { + if (keyboard_report->raw[i]) + cnt++; + } + return cnt; +} + +uint8_t has_anymod(void) +{ + return bitpop(real_mods); +} + +uint8_t get_first_key(void) +{ +#ifdef NKRO_ENABLE + if (keyboard_nkro) { + uint8_t i = 0; + for (; i < REPORT_BITS && !keyboard_report->nkro.bits[i]; i++) + ; + return i<<3 | biton(keyboard_report->nkro.bits[i]); + } +#endif + return keyboard_report->keys[0]; +} + + + +/* local functions */ +static inline void add_key_byte(uint8_t code) +{ + int8_t i = 0; + int8_t empty = -1; + for (; i < REPORT_KEYS; i++) { + if (keyboard_report->keys[i] == code) { + break; + } + if (empty == -1 && keyboard_report->keys[i] == 0) { + empty = i; + } + } + if (i == REPORT_KEYS) { + if (empty != -1) { + keyboard_report->keys[empty] = code; + } + } +} + +static inline void del_key_byte(uint8_t code) +{ + for (uint8_t i = 0; i < REPORT_KEYS; i++) { + if (keyboard_report->keys[i] == code) { + keyboard_report->keys[i] = 0; + } + } +} + +#ifdef NKRO_ENABLE +static inline void add_key_bit(uint8_t code) +{ + if ((code>>3) < REPORT_BITS) { + keyboard_report->nkro.bits[code>>3] |= 1<<(code&7); + } else { + dprintf("add_key_bit: can't add: %02X\n", code); + } +} + +static inline void del_key_bit(uint8_t code) +{ + if ((code>>3) < REPORT_BITS) { + keyboard_report->nkro.bits[code>>3] &= ~(1<<(code&7)); + } else { + dprintf("del_key_bit: can't del: %02X\n", code); + } +} +#endif diff --git a/common/action_util.h b/common/action_util.h new file mode 100644 index 00000000..939bc2b6 --- /dev/null +++ b/common/action_util.h @@ -0,0 +1,56 @@ +/* +Copyright 2013 Jun Wako + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . +*/ +#ifndef ACTION_UTIL_H +#define ACTION_UTIL_H + +#include + +extern report_keyboard_t *keyboard_report; + +void send_keyboard_report(void); + +/* key */ +void add_key(uint8_t key); +void del_key(uint8_t key); +void clear_keys(void); + +/* modifier */ +uint8_t get_mods(void); +void add_mods(uint8_t mods); +void del_mods(uint8_t mods); +void set_mods(uint8_t mods); +void clear_mods(void); + +/* weak modifier */ +uint8_t get_weak_mods(void); +void add_weak_mods(uint8_t mods); +void del_weak_mods(uint8_t mods); +void set_weak_mods(uint8_t mods); +void clear_weak_mods(void); + +/* oneshot modifier */ +void set_oneshot_mods(uint8_t mods); +void clear_oneshot_mods(void); +void oneshot_toggle(void); +void oneshot_enable(void); +void oneshot_disable(void); + +/* inspect */ +uint8_t has_anykey(void); +uint8_t has_anymod(void); +uint8_t get_first_key(void); +#endif diff --git a/common/command.c b/common/command.c index 8a8a81d6..f6f27695 100644 --- a/common/command.c +++ b/common/command.c @@ -27,6 +27,7 @@ along with this program. If not, see . #include "keyboard.h" #include "bootloader.h" #include "action_layer.h" +#include "action_util.h" #include "eeconfig.h" #include "sleep_led.h" #include "led.h" diff --git a/common/host.c b/common/host.c index 56945165..0703dba0 100644 --- a/common/host.c +++ b/common/host.c @@ -27,7 +27,6 @@ along with this program. If not, see . bool keyboard_nkro = false; #endif -report_keyboard_t *keyboard_report = &(report_keyboard_t){}; report_mouse_t mouse_report = {}; @@ -35,13 +34,6 @@ static host_driver_t *driver; static uint16_t last_system_report = 0; static uint16_t last_consumer_report = 0; -static inline void add_key_byte(uint8_t code); -static inline void del_key_byte(uint8_t code); -#ifdef NKRO_ENABLE -static inline void add_key_bit(uint8_t code); -static inline void del_key_bit(uint8_t code); -#endif - void host_set_driver(host_driver_t *d) { @@ -67,7 +59,7 @@ void host_keyboard_send(report_keyboard_t *report) if (debug_keyboard) { dprint("keyboard_report: "); for (uint8_t i = 0; i < REPORT_SIZE; i++) { - dprintf("%02X ", keyboard_report->raw[i]); + dprintf("%02X ", report->raw[i]); } dprint("\n"); } @@ -97,98 +89,6 @@ void host_consumer_send(uint16_t report) (*driver->send_consumer)(report); } - - -/* keyboard report utils */ -void host_add_key(uint8_t key) -{ -#ifdef NKRO_ENABLE - if (keyboard_nkro) { - add_key_bit(key); - return; - } -#endif - add_key_byte(key); -} - -void host_del_key(uint8_t key) -{ -#ifdef NKRO_ENABLE - if (keyboard_nkro) { - del_key_bit(key); - return; - } -#endif - del_key_byte(key); -} - -void host_clear_keys(void) -{ - // not clea mods - for (int8_t i = 1; i < REPORT_SIZE; i++) { - keyboard_report->raw[i] = 0; - } -} - -uint8_t host_get_mods(void) -{ - return keyboard_report->mods; -} - -void host_add_mods(uint8_t mods) -{ - keyboard_report->mods |= mods; -} - -void host_del_mods(uint8_t mods) -{ - keyboard_report->mods &= ~mods; -} - -void host_set_mods(uint8_t mods) -{ - keyboard_report->mods = mods; -} - -void host_clear_mods(void) -{ - keyboard_report->mods = 0; -} - -uint8_t host_has_anykey(void) -{ - uint8_t cnt = 0; - for (uint8_t i = 1; i < REPORT_SIZE; i++) { - if (keyboard_report->raw[i]) - cnt++; - } - return cnt; -} - -uint8_t host_has_anymod(void) -{ - return bitpop(keyboard_report->mods); -} - -uint8_t host_get_first_key(void) -{ -#ifdef NKRO_ENABLE - if (keyboard_nkro) { - uint8_t i = 0; - for (; i < REPORT_BITS && !keyboard_report->nkro.bits[i]; i++) - ; - return i<<3 | biton(keyboard_report->nkro.bits[i]); - } -#endif - return keyboard_report->keys[0]; -} - -void host_send_keyboard_report(void) -{ - if (!driver) return; - host_keyboard_send(keyboard_report); -} - uint8_t host_mouse_in_use(void) { return (mouse_report.buttons | mouse_report.x | mouse_report.y | mouse_report.v | mouse_report.h); @@ -203,51 +103,3 @@ uint16_t host_last_consumer_report(void) { return last_consumer_report; } - -static inline void add_key_byte(uint8_t code) -{ - int8_t i = 0; - int8_t empty = -1; - for (; i < REPORT_KEYS; i++) { - if (keyboard_report->keys[i] == code) { - break; - } - if (empty == -1 && keyboard_report->keys[i] == 0) { - empty = i; - } - } - if (i == REPORT_KEYS) { - if (empty != -1) { - keyboard_report->keys[empty] = code; - } - } -} - -static inline void del_key_byte(uint8_t code) -{ - for (uint8_t i = 0; i < REPORT_KEYS; i++) { - if (keyboard_report->keys[i] == code) { - keyboard_report->keys[i] = 0; - } - } -} - -#ifdef NKRO_ENABLE -static inline void add_key_bit(uint8_t code) -{ - if ((code>>3) < REPORT_BITS) { - keyboard_report->nkro.bits[code>>3] |= 1<<(code&7); - } else { - dprintf("add_key_bit: can't add: %02X\n", code); - } -} - -static inline void del_key_bit(uint8_t code) -{ - if ((code>>3) < REPORT_BITS) { - keyboard_report->nkro.bits[code>>3] &= ~(1<<(code&7)); - } else { - dprintf("del_key_bit: can't del: %02X\n", code); - } -} -#endif diff --git a/common/host.h b/common/host.h index 7c4f0660..c1a0fbac 100644 --- a/common/host.h +++ b/common/host.h @@ -33,7 +33,6 @@ extern bool keyboard_nkro; #endif /* report */ -extern report_keyboard_t *keyboard_report; extern report_mouse_t mouse_report; @@ -48,22 +47,6 @@ void host_mouse_send(report_mouse_t *report); void host_system_send(uint16_t data); void host_consumer_send(uint16_t data); -/* keyboard report utils */ -void host_add_key(uint8_t key); -void host_del_key(uint8_t key); -void host_clear_keys(void); - -uint8_t host_get_mods(void); -void host_add_mods(uint8_t mods); -void host_del_mods(uint8_t mods); -void host_set_mods(uint8_t mods); -void host_clear_mods(void); - -uint8_t host_has_anykey(void); -uint8_t host_has_anymod(void); -uint8_t host_get_first_key(void); -void host_send_keyboard_report(void); - /* mouse report utils */ uint8_t host_mouse_in_use(void); diff --git a/doc/keymap.md b/doc/keymap.md index e4728b50..c509651b 100644 --- a/doc/keymap.md +++ b/doc/keymap.md @@ -227,7 +227,7 @@ You can define these actions on *'A'* key and *'left shift'* modifier with: ACTION_KEY(KC_A) ACTION_KEY(KC_LSFT) -#### 2.1.2 Key with modifiers +#### 2.1.2 Modified key This action is comprised of strokes of modifiers and a key. `Macro` action is needed if you want more complex key strokes. Say you want to assign a key to `Shift + 1` to get charactor *'!'* or `Alt + Tab` to switch application windows. @@ -244,7 +244,7 @@ Registers multiple modifiers with pressing a key. To specify multiple modifiers ACTION_MODS(MOD_ALT | MOD_LSFT) -#### 2.1.3 Modifier with tap key +#### 2.1.3 Modifier with Tap key([Dual role][dual_role]) Works as a modifier key while holding, but registers a key on tap(press and release quickly). @@ -497,7 +497,7 @@ Number of taps can be configured with `TAPPING_TOGGLE` in `config.h`, `5` by def Tapping is to press and release a key quickly. Tapping speed is determined with setting of `TAPPING_TERM`, which can be defined in `config.h`, 200ms by default. ### 4.1 Tap Key -This is a feature to assign normal key action and modifier including layer switching to just same one physical key. This is a kind of [Dual role modifier][dual_role]. It works as modifier when holding the key but registers normal key when tapping. +This is a feature to assign normal key action and modifier including layer switching to just same one physical key. This is a kind of [Dual role key][dual_role]. It works as modifier when holding the key but registers normal key when tapping. Modifier with tap key: @@ -507,7 +507,7 @@ Layer switching with tap key: ACTION_LAYER_TAP_KEY(2, KC_SCLN) -[dual_role]: http://en.wikipedia.org/wiki/Modifier_key#Dual-role_modifier_keys +[dual_role]: http://en.wikipedia.org/wiki/Modifier_key#Dual-role_keys ### 4.2 Tap Toggle @@ -571,5 +571,5 @@ Top layer has higher precedence than lower layers. is to press and release a key quickly. ### Fn key is key which executes a special action like layer switching, mouse key, macro or etc. -### dual role modifier - +### dual role key +