From 8a709c2750eab09ec0f83450410a13640931d48e Mon Sep 17 00:00:00 2001 From: tmk Date: Sun, 16 Dec 2012 02:32:07 +0900 Subject: Add initial fix for new keymap. --- common/action.c | 572 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 572 insertions(+) create mode 100644 common/action.c (limited to 'common/action.c') diff --git a/common/action.c b/common/action.c new file mode 100644 index 000000000..d4aae207f --- /dev/null +++ b/common/action.c @@ -0,0 +1,572 @@ +#include "host.h" +#include "timer.h" +//#include "keymap.h" +#include "keycode.h" +#include "keyboard.h" +#include "mousekey.h" +#include "command.h" +#include "util.h" +#include "debug.h" +#include "action.h" + +#define Kdebug(s) do { if (debug_keyboard) debug(s); } while(0) +#define Kdebug_P(s) do { if (debug_keyboard) debug_P(s); } while(0) +#define Kdebug_hex(s) do { if (debug_keyboard) debug_hex(s); } while(0) + + +/* + * + * Event/State|IDLE PRESSING DELAYING[f] WAITING[f,k] + * -----------+------------------------------------------------------------------ + * Fn Down |(L+) -*1 WAITING(Sk) IDLE(Rf,Ps)*7 + * Up |(L-) IDLE(L-)*8 IDLE(L-)*8 IDLE(L-)*8 + * Fnk Down |DELAYING(Sf)* (Rf) WAITING(Sk) IDLE(Rf,Ps,Rf) + * Up |(L-) IDLE(L-/Uf)*8 IDLE(Rf,Uf/L-)*3 IDLE(Rf,Ps,Uf/L-)*3 + * Key Down |PRESSING(Rk) (Rk) WAITING(Sk) IDLE(Rf,Ps,Rk) + * Up |(Uk) IDLE(Uk)*4 (Uk) IDLE(L+,Ps,Pk)/(Uk)*a + * | + * Delay |- - IDLE(L+) IDLE(L+,Ps) + * Magic Key |COMMAND*5 + * + * *1: ignore Fn if other key is down. + * *2: register Fnk if any key is pressing + * *3: register/unregister delayed Fnk and move to IDLE if code == delayed Fnk, else *8 + * *4: if no keys registered to host + * *5: unregister all keys + * *6: only if no keys down + * *7: ignore Fn because Fnk key and stored key are down. + * *8: move to IDLE if layer switch(off) occurs, else stay at current state + * *9: repeat key if pressing Fnk twice quickly(move to PRESSING) + * *a: layer switch and process waiting key and code if code == wainting key, else unregister key + * + * States: + * IDLE: No key is down except modifiers + * DELAYING: delay layer switch after pressing Fn with alt keycode + * WAITING: key is pressed during DELAYING + * + * Events: + * Fn: Fn key without alternative keycode + * Fnk: Fn key with alternative keycode + * -: ignore + * Delay: layer switch delay term is elapsed + * + * Actions: + * Rk: register key + * Uk: unregister key + * Rf: register Fn(alt keycode) + * Uf: unregister Fn(alt keycode) + * Rs: register stored key + * Us: unregister stored key + * Sk: Store key(waiting Key) + * Sf: Store Fn(delayed Fn) + * Ps: Process stored key + * Ps: Process key + * Is: Interpret stored keys in current layer + * L+: Switch to new layer(*unregister* all keys but modifiers) + * L-: Switch back to last layer(*unregister* all keys but modifiers) + * Ld: Switch back to default layer(*unregister* all keys but modifiers) + */ + + +typedef enum { IDLE, DELAYING, WAITING, PRESSING } kbdstate_t; +#define NEXT(state) do { \ + Kdebug("NEXT: "); Kdebug_P(state_str(kbdstate)); \ + kbdstate = state; \ + Kdebug(" -> "); Kdebug_P(state_str(kbdstate)); Kdebug("\n"); \ +} while (0) + + +static kbdstate_t kbdstate = IDLE; +static uint8_t fn_state_bits = 0; +static keyrecord_t delayed_fn = {}; +static keyrecord_t waiting_key = {}; + +static const char *state_str(kbdstate_t state) +{ + if (state == IDLE) return PSTR("IDLE"); + if (state == DELAYING) return PSTR("DELAYING"); + if (state == WAITING) return PSTR("WAITING"); + if (state == PRESSING) return PSTR("PRESSING"); + return PSTR("UNKNOWN"); +} +static bool anykey_sent_to_host(void) +{ + return (host_has_anykey() || host_mouse_in_use() || + host_last_sysytem_report() || host_last_consumer_report()); +} + + + +/* +static void layer_switch_on(uint8_t code); +static void layer_switch_off(uint8_t code); +static void key_action(uint8_t code, keyevent_t event); +static void key_pressed(uint8_t code, keyevent_t event); +static void key_released(uint8_t code, keyevent_t event); +static void mod_pressed(uint8_t code, keyevent_t event); +static void mod_released(uint8_t code, keyevent_t event); +*/ + +static void register_code(uint8_t code); +static void unregister_code(uint8_t code); +static void register_mods(uint8_t mods); +static void unregister_mods(uint8_t mods); +static void clear_keyboard(void); +static void clear_keyboard_but_mods(void); +static void layer_switch(uint8_t new_layer); + + +/* tap */ +#define TAP_TIME 200 +static keyevent_t last_event = {}; +static uint16_t last_event_time = 0; +static uint8_t tap_count = 0; + +/* layer */ +uint8_t default_layer = 0; +uint8_t current_layer = 0; +uint8_t waiting_layer = 0; + + +void action_exec(action_t action, keyevent_t event) +{ + /* count tap when key is up */ + if (KEYEQ(event.key, last_event.key) && timer_elapsed(last_event_time) < TAP_TIME) { + if (!event.pressed) tap_count++; + } else { + tap_count = 0; + } + + debug("action: "); debug_hex16(action.code); debug("\n"); + debug("kind.id: "); debug_hex(action.kind.id); debug("\n"); + debug("kind.param: "); debug_hex16(action.kind.param); debug("\n"); + debug("key.code: "); debug_hex(action.key.code); debug("\n"); + debug("key.mods: "); debug_hex(action.key.mods); debug("\n"); + + switch (action.kind.id) { + case ACT_LMODS: + if (event.pressed) { + register_mods(action.key.mods); + register_code(action.key.code); + } else { + unregister_code(action.key.code); + unregister_mods(action.key.mods); + } + break; + case ACT_RMODS: + if (event.pressed) { + register_mods(action.key.mods<<4); + register_code(action.key.code); + } else { + unregister_code(action.key.code); + unregister_mods(action.key.mods<<4); + } + break; + case ACT_LAYER: + switch (action.layer_key.code) { + case 0x00: // Momentary switch + // TODO: history of layer switch + if (event.pressed) { + layer_switch(action.layer_key.layer); + } else { + layer_switch(default_layer); + } + break; + case 0x01: // Oneshot switch + // TODO: + break; + case 0x02: // reserved + case 0x03: // reserved + break; + case 0xF0 ... 0xF7: // Tap to enable/disable + case 0xF8 ... 0xFF: // Tap to toggle layer + // TODO: + break; + default: // with keycode for tap + debug("tap: "); debug_hex(tap_count); debug("\n"); + // TODO: layer switch + // TODO: in case tap is interrupted by other key + + + if (event.pressed) { + // when any key down + if (host_has_anykey()) { + if (tap_count == 0) + register_code(action.layer_key.code); + } else { + } + + if (tap_count == 0) { + if (host_has_anykey()) { + register_code(action.layer_key.code); + } else { + waiting_layer = action.layer_key.layer; + } + } + // register key when press after a tap + if (tap_count > 0) { + register_code(action.layer_key.code); + } + } else { + // type key after tap + if (tap_count == 1) { + register_code(action.layer_key.code); + } + unregister_code(action.layer_key.code); + } + break; + } + break; + case ACT_USAGE: +#ifdef EXTRAKEY_ENABLE + switch (action.usage.page) { + case ACTION_USAGE_PAGE_SYSTEM: + if (event.pressed) { + host_system_send(action.usage.code); + } else { + host_system_send(0); + } + break; + case ACTION_USAGE_PAGE_CONSUMER: + if (event.pressed) { + host_consumer_send(action.usage.code); + } else { + host_consumer_send(0); + } + break; + } +#endif + break; + case ACT_MOUSEKEY: +#ifdef MOUSEKEY_ENABLE + if (event.pressed) { + mousekey_on(action.key.code); + mousekey_send(); + } else { + mousekey_off(action.key.code); + mousekey_send(); + } +#endif + break; + case ACT_LMOD_TAP: + case ACT_RMOD_TAP: + case ACT_MACRO: + case ACT_COMMAND: + case ACT_FUNCTION: + default: + break; + } + + /* last event */ + last_event = event; + last_event_time = timer_read(); +} + + +#if 0 +/* Key Action */ +inline +static void key_action(uint8_t code, keyevent_t event) +{ + if (event.pressed) + key_pressed(code, event); + else + key_released(code, event); +} + +void fn_action(uint8_t code, keyevent_t event) +{ +} + +/* Key */ +inline static void key_pressed(uint8_t code, keyevent_t event) +{ + uint8_t tmp_mods; + switch (kbdstate) { + case IDLE: + register_code(code); + NEXT(PRESSING); + break; + case PRESSING: + register_code(code); + break; + case DELAYING: + waiting_key = (keyrecord_t) { + .event = event, + .code = code, + .mods = keyboard_report->mods, + .time = timer_read() + }; + NEXT(WAITING); + break; + case WAITING: + // play back key stroke + tmp_mods = keyboard_report->mods; + host_set_mods(delayed_fn.mods); + register_code(delayed_fn.code); + host_set_mods(waiting_key.mods); + register_code(waiting_key.code); + host_set_mods(tmp_mods); + register_code(code); + NEXT(IDLE); + break; + } +} +inline static void key_released(uint8_t code, keyevent_t event) +{ + uint8_t tmp_mods; + switch (kbdstate) { + case IDLE: + unregister_code(code); + break; + case PRESSING: + unregister_code(code); + if (!anykey_sent_to_host()) + NEXT(IDLE); + break; + case DELAYING: + unregister_code(code); + break; + case WAITING: + if (code == waiting_key.code) { + layer_switch_on(delayed_fn.code); + NEXT(IDLE); + // process waiting_key + tmp_mods = keyboard_report->mods; + host_set_mods(waiting_key.mods); + keymap_process_event(waiting_key.event); + host_set_mods(tmp_mods); + keymap_process_event(event); + } else { + unregister_code(code); + } + break; + } +} + +/* layer switch momentary */ +inline static void layerkey_pressed(uint8_t code, keyevent_t event) +{ + uint8_t tmp_mods; + switch (kbdstate) { + case IDLE: + layer_switch_on(code); + break; + case PRESSING: + // ignore + break; + case DELAYING: + waiting_key = (keyrecord_t) { + .event = event, + .code = code, + .mods = keyboard_report->mods, + .time = timer_read() + }; + NEXT(WAITING); + break; + case WAITING: + tmp_mods = keyboard_report->mods; + host_set_mods(delayed_fn.mods); + register_code(delayed_fn.code); + host_set_mods(waiting_key.mods); + register_code(waiting_key.code); + host_set_mods(tmp_mods); + if (kind == FN_DOWN) { + // ignore Fn + } else if (kind == FNK_DOWN) { + register_code(code); + } else if (kind == KEY_DOWN) { + register_code(code); + } + NEXT(IDLE); + break; + } +} +inline static void layerkey_released(uint8_t code, keyevent_t event) +{ + switch (kbdstate) { + case IDLE: + layer_switch_off(code); + break; + case PRESSING: + case DELAYING: + case WAITING: + if (layer_switch_off(code)) + NEXT(IDLE); + break; + } +} +#endif + + +static void register_code(uint8_t code) +{ + if (code == KC_NO) { + return; + } + else if IS_KEY(code) { + // TODO: should push command_proc out of this block? + if (!command_proc(code)) { + host_add_key(code); + host_send_keyboard_report(); + } + } + else if IS_MOD(code) { + host_add_mods(MOD_BIT(code)); + host_send_keyboard_report(); + } +#ifdef MOUSEKEY_ENABLE + else if IS_MOUSEKEY(code) { + mousekey_on(code); + mousekey_send(); + } +#endif +#ifdef EXTRAKEY_ENABLE + else if IS_CONSUMER(code) { + uint16_t usage = 0; + switch (code) { + case KC_AUDIO_MUTE: + usage = AUDIO_MUTE; + break; + case KC_AUDIO_VOL_UP: + usage = AUDIO_VOL_UP; + break; + case KC_AUDIO_VOL_DOWN: + usage = AUDIO_VOL_DOWN; + break; + case KC_MEDIA_NEXT_TRACK: + usage = TRANSPORT_NEXT_TRACK; + break; + case KC_MEDIA_PREV_TRACK: + usage = TRANSPORT_PREV_TRACK; + break; + case KC_MEDIA_STOP: + usage = TRANSPORT_STOP; + break; + case KC_MEDIA_PLAY_PAUSE: + usage = TRANSPORT_PLAY_PAUSE; + break; + case KC_MEDIA_SELECT: + usage = AL_CC_CONFIG; + break; + case KC_MAIL: + usage = AL_EMAIL; + break; + case KC_CALCULATOR: + usage = AL_CALCULATOR; + break; + case KC_MY_COMPUTER: + usage = AL_LOCAL_BROWSER; + break; + case KC_WWW_SEARCH: + usage = AC_SEARCH; + break; + case KC_WWW_HOME: + usage = AC_HOME; + break; + case KC_WWW_BACK: + usage = AC_BACK; + break; + case KC_WWW_FORWARD: + usage = AC_FORWARD; + break; + case KC_WWW_STOP: + usage = AC_STOP; + break; + case KC_WWW_REFRESH: + usage = AC_REFRESH; + break; + case KC_WWW_FAVORITES: + usage = AC_BOOKMARKS; + break; + } + host_consumer_send(usage); + } + else if IS_SYSTEM(code) { + uint16_t usage = 0; + switch (code) { + case KC_SYSTEM_POWER: + usage = SYSTEM_POWER_DOWN; + break; + case KC_SYSTEM_SLEEP: + usage = SYSTEM_SLEEP; + break; + case KC_SYSTEM_WAKE: + usage = SYSTEM_WAKE_UP; + break; + } + host_system_send(usage); + } +#endif +} + +static void unregister_code(uint8_t code) +{ + if IS_KEY(code) { + host_del_key(code); + host_send_keyboard_report(); + } + else if IS_MOD(code) { + host_del_mods(MOD_BIT(code)); + host_send_keyboard_report(); + } +#ifdef MOUSEKEY_ENABLE + else if IS_MOUSEKEY(code) { + mousekey_off(code); + mousekey_send(); + } +#endif +#ifdef EXTRAKEY_ENABLE + else if IS_CONSUMER(code) { + host_consumer_send(0x0000); + } + else if IS_SYSTEM(code) { + host_system_send(0x0000); + } +#endif +} + +static void register_mods(uint8_t mods) +{ + if (!mods) return; + host_add_mods(mods); + host_send_keyboard_report(); +} + +static void unregister_mods(uint8_t mods) +{ + if (!mods) return; + host_del_mods(mods); + host_send_keyboard_report(); +} + +static void clear_keyboard(void) +{ + host_clear_mods(); + clear_keyboard_but_mods(); +} + +static void clear_keyboard_but_mods(void) +{ + host_clear_keys(); + host_send_keyboard_report(); +#ifdef MOUSEKEY_ENABLE + mousekey_clear(); + mousekey_send(); +#endif +#ifdef EXTRAKEY_ENABLE + host_system_send(0); + host_consumer_send(0); +#endif +} + +static void layer_switch(uint8_t new_layer) +{ + if (current_layer != new_layer) { + Kdebug("Layer Switch: "); Kdebug_hex(current_layer); + Kdebug(" -> "); Kdebug_hex(new_layer); Kdebug("\n"); + + current_layer = new_layer; + clear_keyboard_but_mods(); // To avoid stuck keys + } +} -- cgit v1.2.3-70-g09d2 From 4324e163360db4c6ebd25cab74d09d42b3021278 Mon Sep 17 00:00:00 2001 From: tmk Date: Mon, 17 Dec 2012 03:06:21 +0900 Subject: Fix action of system and consumer usage. --- common/action.c | 97 --------------------------------------- common/report.h | 121 +++++++++++++++---------------------------------- keyboard/hhkb/keymap.c | 4 +- 3 files changed, 38 insertions(+), 184 deletions(-) (limited to 'common/action.c') diff --git a/common/action.c b/common/action.c index d4aae207f..45e2276e7 100644 --- a/common/action.c +++ b/common/action.c @@ -415,89 +415,6 @@ static void register_code(uint8_t code) host_add_mods(MOD_BIT(code)); host_send_keyboard_report(); } -#ifdef MOUSEKEY_ENABLE - else if IS_MOUSEKEY(code) { - mousekey_on(code); - mousekey_send(); - } -#endif -#ifdef EXTRAKEY_ENABLE - else if IS_CONSUMER(code) { - uint16_t usage = 0; - switch (code) { - case KC_AUDIO_MUTE: - usage = AUDIO_MUTE; - break; - case KC_AUDIO_VOL_UP: - usage = AUDIO_VOL_UP; - break; - case KC_AUDIO_VOL_DOWN: - usage = AUDIO_VOL_DOWN; - break; - case KC_MEDIA_NEXT_TRACK: - usage = TRANSPORT_NEXT_TRACK; - break; - case KC_MEDIA_PREV_TRACK: - usage = TRANSPORT_PREV_TRACK; - break; - case KC_MEDIA_STOP: - usage = TRANSPORT_STOP; - break; - case KC_MEDIA_PLAY_PAUSE: - usage = TRANSPORT_PLAY_PAUSE; - break; - case KC_MEDIA_SELECT: - usage = AL_CC_CONFIG; - break; - case KC_MAIL: - usage = AL_EMAIL; - break; - case KC_CALCULATOR: - usage = AL_CALCULATOR; - break; - case KC_MY_COMPUTER: - usage = AL_LOCAL_BROWSER; - break; - case KC_WWW_SEARCH: - usage = AC_SEARCH; - break; - case KC_WWW_HOME: - usage = AC_HOME; - break; - case KC_WWW_BACK: - usage = AC_BACK; - break; - case KC_WWW_FORWARD: - usage = AC_FORWARD; - break; - case KC_WWW_STOP: - usage = AC_STOP; - break; - case KC_WWW_REFRESH: - usage = AC_REFRESH; - break; - case KC_WWW_FAVORITES: - usage = AC_BOOKMARKS; - break; - } - host_consumer_send(usage); - } - else if IS_SYSTEM(code) { - uint16_t usage = 0; - switch (code) { - case KC_SYSTEM_POWER: - usage = SYSTEM_POWER_DOWN; - break; - case KC_SYSTEM_SLEEP: - usage = SYSTEM_SLEEP; - break; - case KC_SYSTEM_WAKE: - usage = SYSTEM_WAKE_UP; - break; - } - host_system_send(usage); - } -#endif } static void unregister_code(uint8_t code) @@ -510,20 +427,6 @@ static void unregister_code(uint8_t code) host_del_mods(MOD_BIT(code)); host_send_keyboard_report(); } -#ifdef MOUSEKEY_ENABLE - else if IS_MOUSEKEY(code) { - mousekey_off(code); - mousekey_send(); - } -#endif -#ifdef EXTRAKEY_ENABLE - else if IS_CONSUMER(code) { - host_consumer_send(0x0000); - } - else if IS_SYSTEM(code) { - host_system_send(0x0000); - } -#endif } static void register_mods(uint8_t mods) diff --git a/common/report.h b/common/report.h index e8582d81f..0995189b3 100644 --- a/common/report.h +++ b/common/report.h @@ -1,5 +1,5 @@ /* -Copyright 2011 Jun Wako +Copyright 2011,2012 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 @@ -19,7 +19,7 @@ along with this program. If not, see . #define REPORT_H #include -#include +#include "keycode.h" /* report id */ @@ -34,8 +34,9 @@ along with this program. If not, see . #define MOUSE_BTN4 (1<<3) #define MOUSE_BTN5 (1<<4) -// Consumer Page(0x0C) -// following are supported by Windows: http://msdn.microsoft.com/en-us/windows/hardware/gg463372.aspx +/* Consumer Page(0x0C) + * following are supported by Windows: http://msdn.microsoft.com/en-us/windows/hardware/gg463372.aspx + */ #define AUDIO_MUTE 0x00E2 #define AUDIO_VOL_UP 0x00E9 #define AUDIO_VOL_DOWN 0x00EA @@ -43,10 +44,12 @@ along with this program. If not, see . #define TRANSPORT_PREV_TRACK 0x00B6 #define TRANSPORT_STOP 0x00B7 #define TRANSPORT_PLAY_PAUSE 0x00CD +/* application launch */ #define AL_CC_CONFIG 0x0183 #define AL_EMAIL 0x018A #define AL_CALCULATOR 0x0192 #define AL_LOCAL_BROWSER 0x0194 +/* application control */ #define AC_SEARCH 0x0221 #define AC_HOME 0x0223 #define AC_BACK 0x0224 @@ -54,20 +57,20 @@ along with this program. If not, see . #define AC_STOP 0x0226 #define AC_REFRESH 0x0227 #define AC_BOOKMARKS 0x022A -// supplement for Bluegiga iWRAP HID(not supported by Windows?) +/* supplement for Bluegiga iWRAP HID(not supported by Windows?) */ #define AL_LOCK 0x019E #define TRANSPORT_RECORD 0x00B2 #define TRANSPORT_REWIND 0x00B4 #define TRANSPORT_EJECT 0x00B8 #define AC_MINIMIZE 0x0206 -// Generic Desktop Page(0x01) +/* Generic Desktop Page(0x01) - system power control */ #define SYSTEM_POWER_DOWN 0x0081 #define SYSTEM_SLEEP 0x0082 #define SYSTEM_WAKE_UP 0x0083 -// key report size(NKRO or boot mode) +/* key report size(NKRO or boot mode) */ #if defined(HOST_PJRC) # include "usb.h" # if defined(KBD2_REPORT_KEYS) && KBD2_REPORT_KEYS > KBD_REPORT_KEYS @@ -99,84 +102,32 @@ typedef struct { } __attribute__ ((packed)) report_mouse_t; -static uint16_t key2system(uint8_t key) -{ - uint16_t usage = 0; - switch (key) { - case KC_SYSTEM_POWER: - usage = SYSTEM_POWER_DOWN; - break; - case KC_SYSTEM_SLEEP: - usage = SYSTEM_SLEEP; - break; - case KC_SYSTEM_WAKE: - usage = SYSTEM_WAKE_UP; - break; - } - return usage; -} - -static uint16_t key2consumer(uint8_t key) -{ - uint16_t usage = 0; - switch (key) { - case KC_AUDIO_MUTE: - usage = AUDIO_MUTE; - break; - case KC_AUDIO_VOL_UP: - usage = AUDIO_VOL_UP; - break; - case KC_AUDIO_VOL_DOWN: - usage = AUDIO_VOL_DOWN; - break; - case KC_MEDIA_NEXT_TRACK: - usage = TRANSPORT_NEXT_TRACK; - break; - case KC_MEDIA_PREV_TRACK: - usage = TRANSPORT_PREV_TRACK; - break; - case KC_MEDIA_STOP: - usage = TRANSPORT_STOP; - break; - case KC_MEDIA_PLAY_PAUSE: - usage = TRANSPORT_PLAY_PAUSE; - break; - case KC_MEDIA_SELECT: - usage = AL_CC_CONFIG; - break; - case KC_MAIL: - usage = AL_EMAIL; - break; - case KC_CALCULATOR: - usage = AL_CALCULATOR; - break; - case KC_MY_COMPUTER: - usage = AL_LOCAL_BROWSER; - break; - case KC_WWW_SEARCH: - usage = AC_SEARCH; - break; - case KC_WWW_HOME: - usage = AC_HOME; - break; - case KC_WWW_BACK: - usage = AC_BACK; - break; - case KC_WWW_FORWARD: - usage = AC_FORWARD; - break; - case KC_WWW_STOP: - usage = AC_STOP; - break; - case KC_WWW_REFRESH: - usage = AC_REFRESH; - break; - case KC_WWW_FAVORITES: - usage = AC_BOOKMARKS; - break; - } - return usage; -} +/* keycode to system usage */ +#define KEYCODE2SYSTEM(key) \ + (key == KC_SYSTEM_POWER ? SYSTEM_POWER_DOWN : \ + (key == KC_SYSTEM_SLEEP ? SYSTEM_SLEEP : \ + (key == KC_SYSTEM_WAKE ? SYSTEM_WAKE_UP : 0))) + +/* keycode to consumer usage */ +#define KEYCODE2CONSUMER(key) \ + (key == KC_AUDIO_MUTE ? AUDIO_MUTE : \ + (key == KC_AUDIO_VOL_UP ? AUDIO_VOL_UP : \ + (key == KC_AUDIO_VOL_DOWN ? AUDIO_VOL_DOWN : \ + (key == KC_MEDIA_NEXT_TRACK ? TRANSPORT_NEXT_TRACK : \ + (key == KC_MEDIA_PREV_TRACK ? TRANSPORT_PREV_TRACK : \ + (key == KC_MEDIA_STOP ? TRANSPORT_STOP : \ + (key == KC_MEDIA_PLAY_PAUSE ? TRANSPORT_PLAY_PAUSE : \ + (key == KC_MEDIA_SELECT ? AL_CC_CONFIG : \ + (key == KC_MAIL ? AL_EMAIL : \ + (key == KC_CALCULATOR ? AL_CALCULATOR : \ + (key == KC_MY_COMPUTER ? AL_LOCAL_BROWSER : \ + (key == KC_WWW_SEARCH ? AC_SEARCH : \ + (key == KC_WWW_HOME ? AC_HOME : \ + (key == KC_WWW_BACK ? AC_BACK : \ + (key == KC_WWW_FORWARD ? AC_FORWARD : \ + (key == KC_WWW_STOP ? AC_STOP : \ + (key == KC_WWW_REFRESH ? AC_REFRESH : \ + (key == KC_WWW_FAVORITES ? AC_BOOKMARKS : 0)))))))))))))))))) #ifdef __cplusplus } diff --git a/keyboard/hhkb/keymap.c b/keyboard/hhkb/keymap.c index 382996ec7..477ef6c33 100644 --- a/keyboard/hhkb/keymap.c +++ b/keyboard/hhkb/keymap.c @@ -186,10 +186,10 @@ action_t keymap_get_action(uint8_t layer, uint8_t row, uint8_t col) { action = (action_t)ACTION_KEY(key); break; case KC_SYSTEM_POWER ... KC_SYSTEM_WAKE: - action = (action_t)ACTION_USAGE_SYSTEM(key2system(key)); + action = (action_t)ACTION_USAGE_SYSTEM(KEYCODE2SYSTEM(key)); break; case KC_AUDIO_MUTE ... KC_WWW_FAVORITES: - action = (action_t)ACTION_USAGE_CONSUMER(key2consumer(key)); + action = (action_t)ACTION_USAGE_CONSUMER(KEYCODE2CONSUMER(key)); break; case KC_MS_UP ... KC_MS_ACCEL2: action = (action_t)ACTION_MOUSEKEY(key); -- cgit v1.2.3-70-g09d2 From 411de9cc22e927313a5a768f3bf41f2f99bca126 Mon Sep 17 00:00:00 2001 From: tmk Date: Wed, 9 Jan 2013 22:33:33 +0900 Subject: Add new layer actions. --- common/action.c | 394 +++++++++++++++++++++++-------------------------- common/action.h | 152 +++++++++++-------- common/keyboard.c | 5 +- common/keyboard.h | 8 +- common/keymap.h | 1 - keyboard/hhkb/keymap.c | 78 +++++----- 6 files changed, 323 insertions(+), 315 deletions(-) (limited to 'common/action.c') diff --git a/common/action.c b/common/action.c index 45e2276e7..425a2b00f 100644 --- a/common/action.c +++ b/common/action.c @@ -1,6 +1,6 @@ #include "host.h" #include "timer.h" -//#include "keymap.h" +#include "keymap.h" #include "keycode.h" #include "keyboard.h" #include "mousekey.h" @@ -78,8 +78,6 @@ typedef enum { IDLE, DELAYING, WAITING, PRESSING } kbdstate_t; static kbdstate_t kbdstate = IDLE; static uint8_t fn_state_bits = 0; -static keyrecord_t delayed_fn = {}; -static keyrecord_t waiting_key = {}; static const char *state_str(kbdstate_t state) { @@ -96,17 +94,6 @@ static bool anykey_sent_to_host(void) } - -/* -static void layer_switch_on(uint8_t code); -static void layer_switch_off(uint8_t code); -static void key_action(uint8_t code, keyevent_t event); -static void key_pressed(uint8_t code, keyevent_t event); -static void key_released(uint8_t code, keyevent_t event); -static void mod_pressed(uint8_t code, keyevent_t event); -static void mod_released(uint8_t code, keyevent_t event); -*/ - static void register_code(uint8_t code); static void unregister_code(uint8_t code); static void register_mods(uint8_t mods); @@ -118,6 +105,7 @@ static void layer_switch(uint8_t new_layer); /* tap */ #define TAP_TIME 200 +#define LAYER_DELAY 200 static keyevent_t last_event = {}; static uint16_t last_event_time = 0; static uint8_t tap_count = 0; @@ -125,10 +113,10 @@ static uint8_t tap_count = 0; /* layer */ uint8_t default_layer = 0; uint8_t current_layer = 0; -uint8_t waiting_layer = 0; +keyrecord_t delaying_layer = {}; -void action_exec(action_t action, keyevent_t event) +void action_exec(keyevent_t event) { /* count tap when key is up */ if (KEYEQ(event.key, last_event.key) && timer_elapsed(last_event_time) < TAP_TIME) { @@ -137,6 +125,20 @@ void action_exec(action_t action, keyevent_t event) tap_count = 0; } + /* layer switch after LAYER_DELAY */ + if (delaying_layer.action.code && timer_elapsed(delaying_layer.event.time) > LAYER_DELAY) { + switch (delaying_layer.action.kind.id) { + case ACT_LAYER_PRESSED: + layer_switch(delaying_layer.action.layer.opt); + break; + case ACT_LAYER_BIT: + layer_switch(current_layer | delaying_layer.action.layer.opt); + break; + } + delaying_layer = (keyrecord_t){}; + } + action_t action = keymap_get_action(current_layer, event.key.row, event.key.col); + debug("action: "); debug_hex16(action.code); debug("\n"); debug("kind.id: "); debug_hex(action.kind.id); debug("\n"); debug("kind.param: "); debug_hex16(action.kind.param); debug("\n"); @@ -145,6 +147,7 @@ void action_exec(action_t action, keyevent_t event) switch (action.kind.id) { case ACT_LMODS: + // normal key or key plus mods if (event.pressed) { register_mods(action.key.mods); register_code(action.key.code); @@ -162,94 +165,207 @@ void action_exec(action_t action, keyevent_t event) unregister_mods(action.key.mods<<4); } break; - case ACT_LAYER: - switch (action.layer_key.code) { - case 0x00: // Momentary switch - // TODO: history of layer switch + case ACT_LMOD_TAP: + break; + case ACT_RMOD_TAP: + break; + case ACT_USAGE: +#ifdef EXTRAKEY_ENABLE + switch (action.usage.page) { + case ACTION_USAGE_PAGE_SYSTEM: if (event.pressed) { - layer_switch(action.layer_key.layer); + host_system_send(action.usage.code); } else { - layer_switch(default_layer); + host_system_send(0); } break; - case 0x01: // Oneshot switch - // TODO: + case ACTION_USAGE_PAGE_CONSUMER: + if (event.pressed) { + host_consumer_send(action.usage.code); + } else { + host_consumer_send(0); + } break; - case 0x02: // reserved - case 0x03: // reserved + } +#endif + break; + case ACT_MOUSEKEY: +#ifdef MOUSEKEY_ENABLE + if (event.pressed) { + mousekey_on(action.key.code); + mousekey_send(); + } else { + mousekey_off(action.key.code); + mousekey_send(); + } +#endif + break; + case ACT_LAYER_PRESSED: + // layer action when pressed + switch (action.layer.code) { + case 0x00: + if (event.pressed) { + layer_switch(action.layer.opt); + } break; - case 0xF0 ... 0xF7: // Tap to enable/disable - case 0xF8 ... 0xFF: // Tap to toggle layer - // TODO: + case 0xF0: + // TODO: tap toggle break; - default: // with keycode for tap + case 0xFF: + if (event.pressed) { + default_layer = action.layer.opt; + layer_switch(default_layer); + } + break; + default: + // with tap key debug("tap: "); debug_hex(tap_count); debug("\n"); - // TODO: layer switch - // TODO: in case tap is interrupted by other key - - if (event.pressed) { - // when any key down - if (host_has_anykey()) { - if (tap_count == 0) - register_code(action.layer_key.code); - } else { - } - if (tap_count == 0) { if (host_has_anykey()) { - register_code(action.layer_key.code); + register_code(action.layer.code); } else { - waiting_layer = action.layer_key.layer; + delaying_layer = (keyrecord_t){ + .event = event, + .action = action, + .mods = keyboard_report->mods + }; } - } - // register key when press after a tap - if (tap_count > 0) { - register_code(action.layer_key.code); + } else if (tap_count > 0) { + register_code(action.layer.code); } } else { // type key after tap if (tap_count == 1) { - register_code(action.layer_key.code); + delaying_layer = (keyrecord_t){}; + register_code(action.layer.code); } - unregister_code(action.layer_key.code); + unregister_code(action.layer.code); } break; } break; - case ACT_USAGE: -#ifdef EXTRAKEY_ENABLE - switch (action.usage.page) { - case ACTION_USAGE_PAGE_SYSTEM: + case ACT_LAYER_RELEASED: + switch (action.layer.code) { + case 0x00: if (event.pressed) { - host_system_send(action.usage.code); + layer_switch(action.layer.opt); + } + break; + case 0xF0: + // Ignored. LAYER_RELEASED with tap toggle is invalid action. + break; + case 0xFF: + if (!event.pressed) { + default_layer = action.layer.opt; + layer_switch(default_layer); + } + break; + default: + // Ignored. LAYER_RELEASED with tap key is invalid action. + break; + } + break; + case ACT_LAYER_BIT: + switch (action.layer.code) { + case 0x00: + if (event.pressed) { + layer_switch(current_layer | action.layer.opt); } else { - host_system_send(0); + layer_switch(current_layer & ~action.layer.opt); } break; - case ACTION_USAGE_PAGE_CONSUMER: + case 0xF0: + // TODO: tap toggle + break; + case 0xFF: + // change default layer if (event.pressed) { - host_consumer_send(action.usage.code); + default_layer = current_layer | action.layer.opt; + layer_switch(default_layer); } else { - host_consumer_send(0); + default_layer = current_layer & ~action.layer.opt; + layer_switch(default_layer); + } + break; + default: + // with tap key + debug("tap: "); debug_hex(tap_count); debug("\n"); + if (event.pressed) { + if (tap_count == 0) { + if (host_has_anykey()) { + register_code(action.layer.code); + } else { + delaying_layer = (keyrecord_t){ + .event = event, + .action = action, + .mods = keyboard_report->mods + }; + } + } else if (tap_count > 0) { + register_code(action.layer.code); + } + } else { + if (tap_count == 0) { + // no tap + layer_switch(current_layer & ~action.layer.opt); + } else if (tap_count == 1) { + // tap + register_code(action.layer.code); + } + unregister_code(action.layer.code); } break; } -#endif - break; - case ACT_MOUSEKEY: -#ifdef MOUSEKEY_ENABLE - if (event.pressed) { - mousekey_on(action.key.code); - mousekey_send(); - } else { - mousekey_off(action.key.code); - mousekey_send(); + case ACT_LAYER_EXT: + switch (action.layer.opt) { + case 0x00: + // set default layer when pressed + switch (action.layer.code) { + case 0x00: + if (event.pressed) { + layer_switch(default_layer); + } + break; + case 0xF0: + // TODO: tap toggle + break; + case 0xFF: + if (event.pressed) { + default_layer = current_layer; + layer_switch(default_layer); + } + break; + default: + // TODO: tap key + break; + } + break; + case 0x01: + // set default layer when released + switch (action.layer.code) { + case 0x00: + if (!event.pressed) { + layer_switch(default_layer); + } + break; + case 0xFF: + if (!event.pressed) { + default_layer = current_layer; + layer_switch(default_layer); + } + break; + case 0xF0: + default: + // Ignore tap. + if (!event.pressed) { + layer_switch(default_layer); + } + break; + } + break; } -#endif break; - case ACT_LMOD_TAP: - case ACT_RMOD_TAP: case ACT_MACRO: case ACT_COMMAND: case ACT_FUNCTION: @@ -263,142 +379,6 @@ void action_exec(action_t action, keyevent_t event) } -#if 0 -/* Key Action */ -inline -static void key_action(uint8_t code, keyevent_t event) -{ - if (event.pressed) - key_pressed(code, event); - else - key_released(code, event); -} - -void fn_action(uint8_t code, keyevent_t event) -{ -} - -/* Key */ -inline static void key_pressed(uint8_t code, keyevent_t event) -{ - uint8_t tmp_mods; - switch (kbdstate) { - case IDLE: - register_code(code); - NEXT(PRESSING); - break; - case PRESSING: - register_code(code); - break; - case DELAYING: - waiting_key = (keyrecord_t) { - .event = event, - .code = code, - .mods = keyboard_report->mods, - .time = timer_read() - }; - NEXT(WAITING); - break; - case WAITING: - // play back key stroke - tmp_mods = keyboard_report->mods; - host_set_mods(delayed_fn.mods); - register_code(delayed_fn.code); - host_set_mods(waiting_key.mods); - register_code(waiting_key.code); - host_set_mods(tmp_mods); - register_code(code); - NEXT(IDLE); - break; - } -} -inline static void key_released(uint8_t code, keyevent_t event) -{ - uint8_t tmp_mods; - switch (kbdstate) { - case IDLE: - unregister_code(code); - break; - case PRESSING: - unregister_code(code); - if (!anykey_sent_to_host()) - NEXT(IDLE); - break; - case DELAYING: - unregister_code(code); - break; - case WAITING: - if (code == waiting_key.code) { - layer_switch_on(delayed_fn.code); - NEXT(IDLE); - // process waiting_key - tmp_mods = keyboard_report->mods; - host_set_mods(waiting_key.mods); - keymap_process_event(waiting_key.event); - host_set_mods(tmp_mods); - keymap_process_event(event); - } else { - unregister_code(code); - } - break; - } -} - -/* layer switch momentary */ -inline static void layerkey_pressed(uint8_t code, keyevent_t event) -{ - uint8_t tmp_mods; - switch (kbdstate) { - case IDLE: - layer_switch_on(code); - break; - case PRESSING: - // ignore - break; - case DELAYING: - waiting_key = (keyrecord_t) { - .event = event, - .code = code, - .mods = keyboard_report->mods, - .time = timer_read() - }; - NEXT(WAITING); - break; - case WAITING: - tmp_mods = keyboard_report->mods; - host_set_mods(delayed_fn.mods); - register_code(delayed_fn.code); - host_set_mods(waiting_key.mods); - register_code(waiting_key.code); - host_set_mods(tmp_mods); - if (kind == FN_DOWN) { - // ignore Fn - } else if (kind == FNK_DOWN) { - register_code(code); - } else if (kind == KEY_DOWN) { - register_code(code); - } - NEXT(IDLE); - break; - } -} -inline static void layerkey_released(uint8_t code, keyevent_t event) -{ - switch (kbdstate) { - case IDLE: - layer_switch_off(code); - break; - case PRESSING: - case DELAYING: - case WAITING: - if (layer_switch_off(code)) - NEXT(IDLE); - break; - } -} -#endif - - static void register_code(uint8_t code) { if (code == KC_NO) { diff --git a/common/action.h b/common/action.h index 08f8c5608..942ce191a 100644 --- a/common/action.h +++ b/common/action.h @@ -15,10 +15,6 @@ ACT_LMODS(0000) 0 0 0 0| mods(4) | 0 0 0 0 0 0| 1 0 (reserved) 0 0 0 0| mods(4) | 0 0 0 0 0 0| 1 1 (reserved) 0 0 0 0| mods(4) | keycode(8) Key+Lmods -??? - 0 0 0 0| mods(4) | 1 1 1 1 0| tap(3) Lmods+tap Switch(enable/disable) - 0 0 0 0| mods(4) | 1 1 1 1 1| tap(3) Lmods+tap Toggle(on/off) -??? ACT_RMODS(0001) 0 0 0 1| 0 0 0 0| 0 0 0 0 0 0 0 0 No action(not used) @@ -28,10 +24,6 @@ ACT_RMODS(0001) 0 0 0 1| mods(4) | 0 0 0 0 0 0| 1 0 (reserved) 0 0 0 1| mods(4) | 0 0 0 0 0 0| 1 1 (reserved) 0 0 0 1| mods(4) | keycode(8) Key+Rmod -??? - 0 0 0 1| mods(4) | 1 1 1 1 0| tap(3) Rmods+tap Switch(enable/disable) - 0 0 0 1| mods(4) | 1 1 1 1 1| tap(3) Rmods+tap Toggle(on/off) -??? ACT_LMODS_TAP(0010) 0 0 1 0| 0 0 0 0| X X X X X X X X (reserved)[00-FF] @@ -45,36 +37,47 @@ ACT_RMODS_TAP(0011) 0 0 1 1| mods(4) | keycode(8) Rmods+tap Key 0 0 1 1| mods(4) | 1 1 1 1| X X X X (reserved)[F0-FF] -ACT_LAYER(0100) - 0 1 0 0| layer(4) | 0 0 0 0 0 0| 0 0 Momentary - 0 1 0 0| layer(4) | 0 0 0 0 0 0| 0 1 Oneshot - 0 1 0 0| layer(4) | 0 0 0 0 0 0| 1 0 (reserved) - 0 1 0 0| layer(4) | 0 0 0 0 0 0| 1 1 (reserved) - 0 1 0 0| layer(4) | keycode(8) Fn momentary + tap Key - 0 1 0 0| layer(4) | 1 1 1 1 0| tap(3) Fn+tap Switch(enable/disable) - 0 1 0 0| layer(4) | 1 1 1 1 1| tap(3) Fn+tap Toggle(on/off) - -ACT_USAGE(0101) - 0 1 0 1| 0 0| usage(10) System usage - 0 1 0 1| 0 1| usage(10) Consumer usage - 0 1 0 1| 1 0| usage(10) (reserved) - 0 1 0 1| 1 1| usage(10) (reserved) +ACT_USAGE - other HID usage than keyboard + 0 1 0 0| 0 0| usage(10) System usage + 0 1 0 0| 0 1| usage(10) Consumer usage + 0 1 0 0| 1 0| usage(10) (reserved) + 0 1 0 0| 1 1| usage(10) (reserved) ACT_MOUSEKEY(0110) - 0 1 1 0| X X X X| keycode(8) Mouse key + 0 1 0 1| X X X X| keycode(8) Mouse key ??? TODO: refactor - 0 1 1 0| 0 0 X X| accel(5) |cursor(3) Mouse key - 0 1 1 0| 0 1 X X| accel(5) |wheel(3) Mouse key - 0 1 1 0| 1 0 X X| button(8) Mouse key - 0 1 1 0| 1 1 X X| button(8) Mouse key + 0 1 0 1| 0 0 X X| accel(5) |cursor(3) Mouse key + 0 1 0 1| 0 1 X X| accel(5) |wheel(3) Mouse key + 0 1 0 1| 1 0 X X| button(8) Mouse key + 0 1 0 1| 1 1 X X| button(8) Mouse key ??? - 0 1 1 1| (reserved) - 1 0 0 0| (reserved) - 1 0 0 1| (reserved) - 1 0 1 0| (reserved) - 1 0 1 1| (reserved) - 1 1 0 0| (reserved) +Layer Action +------------ +1000|LLLL|0000 0000 set layer L when pressed +1001|LLLL|0000 0000 set layer L when released +1010|BBBB|0000 0000 on/off bit B when pressed/released +1011|0000|0000 0000 set default layer when pressed +1011|0001|0000 0000 set default layer when released + +1000|LLLL|1111 0000 set layer L when pressed + tap toggle +1001|LLLL|1111 0000 set layer L when released[tap is ignored/not used] +1010|BBBB|1111 0000 on/off bit B when pressed/released + tap toggle +1011|0000|1111 0000 set default layer when pressed + tap toggle +1011|0001|1111 0000 set default layer when released[tap is ignored/not used] + +1000|LLLL|1111 1111 set L to default layer when pressed +1001|LLLL|1111 1111 set L to default layer when released +1010|BBBB|1111 1111 on/off bit B of default layer when pressed/released +1011|0000|1111 1111 set current to default layer when pressed +1011|0001|1111 1111 set current to default layer when released + +1000|LLLL| keycode set layer L when pressed + tap key +1001|LLLL| keyocde set layer L when released[tap is ignored/not used] +1010|BBBB| keyocde on/off bit B when pressed/released + tap key +1011|0000| keyocde set default layer when pressed + tap key +1011|0001| keyocde set default layer when released[tap is ignored/not used] + ACT_MACRO(1100) 1 1 0 0| option(4) | macro-table id(8) Macro play(Flash) @@ -88,25 +91,32 @@ ACT_FUNCTION(1111) 1 1 1 1| function address(4K range) Function Macro record(dynamicly) Macro play(dynamicly) +TODO: modifier + [tap key /w mod] + : layerkey + [tap key /w mod] + for example: LShift + '('[Shift+9] and RShift + ')'[Shift+0] + http://deskthority.net/workshop-f7/tmk-keyboard-firmware-collection-t4478.html#p90052 */ enum action_id { - ACT_LMODS = 0, - ACT_RMODS, - ACT_LMOD_TAP, - ACT_RMOD_TAP, - ACT_LAYER, - ACT_USAGE, - ACT_MOUSEKEY, - ACT_MACRO = 14, - ACT_COMMAND = 15, - ACT_FUNCTION = 16 + ACT_LMODS = 0b0000, + ACT_RMODS = 0b0001, + ACT_LMOD_TAP = 0b0010, + ACT_RMOD_TAP = 0b0011, + ACT_USAGE = 0b0100, + ACT_MOUSEKEY = 0b0101, + ACT_LAYER_PRESSED = 0b1000, + ACT_LAYER_RELEASED = 0b1001, + ACT_LAYER_BIT = 0b1010, + ACT_LAYER_EXT = 0b1011, + ACT_MACRO = 0b1100, + ACT_COMMAND = 0b1110, + ACT_FUNCTION = 0b1111 }; // TODO: not portable across compiler/endianness? /* In avr-gcc bit fields seems to be assigned from LSB(bit0) to MSB(bit15). -AVR seems like little endian in avr-gcc. +AVR looks like a little endian in avr-gcc. Byte order and bit order of 0x1234: Big endian: 15 ... 8 7 ... 210 @@ -127,17 +137,11 @@ typedef union { uint16_t mods :4; uint16_t kind :4; } key; - struct action_layer_key { + struct action_layer { uint16_t code :8; - uint16_t layer :4; - uint16_t kind :4; - } layer_key; - struct action_layer_tap { - uint16_t count :3; - uint16_t rest :5; - uint16_t layer :4; + uint16_t opt :4; uint16_t kind :4; - } layer_tap; + } layer; struct action_usage { uint16_t code :10; uint16_t page :2; @@ -157,7 +161,14 @@ enum stroke_cmd { STROKE_ALLUP, /* release all keys in reverse order */ }; -void action_exec(action_t act, keyevent_t event); +typedef struct { + keyevent_t event; + action_t action; + uint8_t mods; +} keyrecord_t; + + +void action_exec(keyevent_t event); /* void key_action(uint8_t code, keyevent_t event); void mod_action(uint8_t code, keyevent_t event); @@ -166,9 +177,12 @@ void fn_action(uint8_t code, keyevent_t event); /* action_t utility */ +/* +#define ACTION_NO { .code = 0 } #define ACTION(kind, param) { .code = ((kind)<<12 | (param)) } -#define NO_ACTION ACTION(0, 0) -#define LAYER_PARAM(layer, key) ((layer)<<8|(key)) +*/ +#define ACTION_NO 0 +#define ACTION(kind, param) ((kind)<<12 | (param)) /* Key & Mods */ #define ACTION_KEY(key) ACTION(ACT_LMODS, key) @@ -185,12 +199,28 @@ void fn_action(uint8_t code, keyevent_t event); /* Mods + Tap key */ #define ACTION_LMODS_TAP(mods, key) ACTION(ACT_LMODS_TAP,(mods)<<8 | (key)) #define ACTION_RMODS_TAP(mods, key) ACTION(ACT_RMODS_TAP,(mods)<<8 | (key)) + /* Layer Switch */ -#define ACTION_LAYER(layer) ACTION(ACT_LAYER, (layer)<<8 | 0x00) -#define ACTION_LAYER_ONESHOT(layer) ACTION(ACT_LAYER, (layer)<<8 | 0x01) -#define ACTION_LAYER_KEY(layer, key) ACTION(ACT_LAYER, (layer)<<8 | (key)) -#define ACTION_LAYER_SWITCH(layer, tap) ACTION(ACT_LAYER, (layer)<<8 | 0xF0 | (tap)) -#define ACTION_LAYER_TOGGLE(layer, tap) ACTION(ACT_LAYER, (layer)<<8 | 0xF1 | (tap)) +#define ACTION_LAYER_SET_ON_PRESSED(layer) ACTION(ACT_LAYER_PRESSED, (layer)<<8 | 0x00) +#define ACTION_LAYER_SET_ON_RELEASED(layer) ACTION(ACT_LAYER_RELEASED, (layer)<<8 | 0x00) +#define ACTION_LAYER_BIT(bits) ACTION(ACT_LAYER_BIT, (layer)<<8 | 0x00) +#define ACTION_LAYER_TO_DEFAULT_ON_PRESSED ACTION(ACT_LAYER_EXT, 0x0<<8 | 0x00) +#define ACTION_LAYER_TO_DEFAULT_ON_RELEASED ACTION(ACT_LAYER_EXT, 0x1<<8 | 0x00) + +#define ACTION_LAYER_TAP_TOGGLE(layer) ACTION(ACT_LAYER_PRESSED, (layer)<<8 | 0xF0) +#define ACTION_LAYER_BIT_TAP_TOGGLE(layer) ACTION(ACT_LAYER_BIT, (layer)<<8 | 0xF0) +#define ACTION_LAYER_DEFAULT_TAP_TOGGLE ACTION(ACT_LAYER_EXT, 0x0<<8 | 0xF0) + +#define ACTION_LAYER_DEFAULT_SET_ON_PRESSED(layer) ACTION(ACT_LAYER_PRESSED, (layer)<<8 | 0xFF) +#define ACTION_LAYER_DEFAULT_SET_ON_RELEASED(layer) ACTION(ACT_LAYER_RELEASED, (layer)<<8 | 0xFF) +#define ACTION_LAYER_DEFAULT_BIT(bits) ACTION(ACT_LAYER_BIT, (bits)<<8 | 0xFF) +#define ACTION_LAYER_DEFAULT_SET_CURRENT_ON_PRESSED ACTION(ACT_LAYER_EXT, 0x0<<8 | 0xFF) +#define ACTION_LAYER_DEFAULT_SET_CURRENT_ON_RELEASED ACTION(ACT_LAYER_EXT, 0x1<<8 | 0xFF) + +#define ACTION_LAYER_SET_TAP_KEY(layer, key) ACTION(ACT_LAYER_PRESSED, (layer)<<8 | (key)) +#define ACTION_LAYER_BIT_TAP_KEY(bits, key) ACTION(ACT_LAYER_BIT, (layer)<<8 | (key)) +#define ACTION_LAYER_DEFAULT_SET_TAP_KEY(key) ACTION(ACT_LAYER_EXT, 0x0<<8 | (key)) + /* HID Usage */ #define ACTION_USAGE_PAGE_SYSTEM 0 #define ACTION_USAGE_PAGE_CONSUMER 1 diff --git a/common/keyboard.c b/common/keyboard.c index 5e95fb984..1e0b8c3ed 100644 --- a/common/keyboard.c +++ b/common/keyboard.c @@ -65,9 +65,10 @@ void keyboard_task(void) for (int c = 0; c < MATRIX_COLS; c++) { if (matrix_change & (1<. } +/* static const action_t PROGMEM fn_actions[] = { - ACTION_LAYER(0), // Fn0 - ACTION_LAYER(1), // Fn1 - ACTION_LAYER_KEY(2, KC_SLASH), // Fn2 - ACTION_LAYER_KEY(3, KC_SCLN), // Fn3 - ACTION_LAYER(3), // Fn3 - ACTION_LAYER_KEY(5, KC_SPC), // Fn5 - NO_ACTION, // Fn6 - NO_ACTION, // Fn7 + ACTION_LAYER_TO_DEFAULT_ON_RELEASED, // Fn0 + ACTION_LAYER_SET_ON_PRESSED(1), // Fn1 + ACTION_LAYER_SET_TAP_KEY(2, KC_SLASH), // Fn2 + ACTION_LAYER_SET_TAP_KEY(3, KC_SCLN), // Fn3 + ACTION_LAYER_SET_ON_PRESSED(3), // Fn4 + ACTION_LAYER_SET_TAP_KEY(5, KC_SPC), // Fn5 + ACTION_NO, // Fn6 + ACTION_NO, // Fn7 +}; +*/ +static const uint16_t PROGMEM fn_actions[] = { + ACTION_LAYER_TO_DEFAULT_ON_RELEASED, // Fn0 + ACTION_LAYER_SET_ON_PRESSED(1), // Fn1 + ACTION_LAYER_SET_TAP_KEY(2, KC_SLASH), // Fn2 + ACTION_LAYER_SET_TAP_KEY(3, KC_SCLN), // Fn3 + ACTION_LAYER_SET_ON_PRESSED(3), // Fn4 + ACTION_LAYER_SET_TAP_KEY(5, KC_SPC), // Fn5 + ACTION_NO, // Fn6 + ACTION_NO, // Fn7 }; @@ -91,7 +103,7 @@ static const uint8_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = { * |-----------------------------------------------------------| * |Contro|VoD|VoU|Mut| | | *| /|Hom|PgU|Lef|Rig|Enter | * |-----------------------------------------------------------| - * |Shift | | | | | | +| -|End|PgD|Dow|Shift |xxx| + * |Shift | | | | | | +| -|End|PgD|Dow|Shift |Fn0| * `-----------------------------------------------------------' * |Gui |Alt |Space |Alt |xxx| * `--------------------------------------------' @@ -99,8 +111,8 @@ static const uint8_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = { KEYMAP(PWR, F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12, INS, DEL, \ CAPS,NO, NO, NO, NO, NO, NO, NO, PSCR,SLCK,BRK, UP, NO, BSPC, \ LCTL,VOLD,VOLU,MUTE,NO, NO, PAST,PSLS,HOME,PGUP,LEFT,RGHT,ENT, \ - LSFT,NO, NO, NO, NO, NO, PPLS,PMNS,END, PGDN,DOWN,RSFT,FN1, \ - LGUI,LALT, SPC, RALT,FN7), + LSFT,NO, NO, NO, NO, NO, PPLS,PMNS,END, PGDN,DOWN,RSFT,FN0, \ + LGUI,LALT, SPC, RALT,RGUI), /* Layer 2: Vi mode (Slash) * ,-----------------------------------------------------------. @@ -110,7 +122,7 @@ static const uint8_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = { * |-----------------------------------------------------------| * |Contro| |Lef|Dow|Rig| |Lef|Dow|Up |Rig| | |Return | * |-----------------------------------------------------------| - * |Shift | | | | | |Hom|PgD|PgUlEnd|xxx|Shift | | + * |Shift | | | | | |Hom|PgD|PgUlEnd|Fn0|Shift | | * `-----------------------------------------------------------' * |Gui|Alt |Space |Alt |Gui| * `-------------------------------------------' @@ -118,7 +130,7 @@ static const uint8_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = { KEYMAP(ESC, F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12, INS, DEL, \ TAB, HOME,PGDN,UP, PGUP,END, HOME,PGDN,PGUP,END, NO, NO, NO, BSPC, \ LCTL,NO, LEFT,DOWN,RGHT,NO, LEFT,DOWN,UP, RGHT,NO, NO, ENT, \ - LSFT,NO, NO, NO, NO, NO, HOME,PGDN,PGUP,END, FN2, RSFT,NO, \ + LSFT,NO, NO, NO, NO, NO, HOME,PGDN,PGUP,END, FN0, RSFT,NO, \ LGUI,LALT, SPC, RALT,RGUI), /* Layer 3: Mouse mode (Semicolon) @@ -127,19 +139,19 @@ static const uint8_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = { * |-----------------------------------------------------------| * |Tab |MwL|MwU|McU|MwD|MwR|MwL|MwD|MwU|MwR| | | |Backs| * |-----------------------------------------------------------| - * |Contro| |McL|McD|McR| |McL|McD|McU|McR|xxx| |Return | + * |Contro| |McL|McD|McR| |McL|McD|McU|McR|Fn0| |Return | * |-----------------------------------------------------------| * |Shift |Mb4|Mb5|Mb1|Mb2|Mb3|Mb2|Mb1|Mb4|Mb5| |Shift | | * `-----------------------------------------------------------' - * |Gui |Alt |Mb1 |Alt |Gui| + * |Gui |Alt |Mb1 |Alt |Fn0| * `--------------------------------------------' * Mc: Mouse Cursor / Mb: Mouse Button / Mw: Mouse Wheel */ KEYMAP(ESC, F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12, INS, DEL, \ TAB, NO, NO, NO, NO, NO, WH_L,WH_D,WH_U,WH_R,NO, NO, NO, BSPC, \ - LCTL,NO, ACL0,ACL1,ACL2,NO, MS_L,MS_D,MS_U,MS_R,FN3, NO, ENT, \ + LCTL,NO, ACL0,ACL1,ACL2,NO, MS_L,MS_D,MS_U,MS_R,FN0, NO, ENT, \ LSFT,NO, NO, NO, NO, BTN3,BTN2,BTN1,BTN4,BTN5,NO, RSFT,NO, \ - LGUI,LALT, BTN1, RALT,FN4), + LGUI,LALT, BTN1, RALT,FN0), /* Layer 4: Matias half keyboard style (Space) * ,-----------------------------------------------------------. @@ -151,21 +163,21 @@ static const uint8_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = { * |-----------------------------------------------------------| * |Shift | /| .| ,| M| N| B| V| C| X| Z|Shift | | * `-----------------------------------------------------------' - * |Gui |Alt |xxxxxxxxxxxxxxxxxxxxxxx|Alt |Gui| + * |Gui |Alt | Fn0 |Alt |Gui| * `--------------------------------------------' */ KEYMAP(MINS,0, 9, 8, 7, 6, 5, 4, 3, 2, 1, NO, NO, NO, ESC, \ BSPC,P, O, I, U, Y, T, R, E, W, Q, NO, NO, TAB, \ LCTL,SCLN,L, K, J, H, G, F, D, S, A, RCTL,RCTL, \ LSFT,SLSH,DOT, COMM,M, N, B, V, C, X, Z, RSFT,NO, \ - LGUI,LALT, FN5, RALT,RGUI), + LGUI,LALT, FN0, RALT,RGUI), /* Layer5: another Mouse mode (Space) */ KEYMAP(ESC, F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12, INS, DEL, \ TAB, NO, NO, NO, NO, NO, WH_L,WH_D,WH_U,WH_R,NO, NO, NO, BSPC, \ - LCTL,NO, ACL0,ACL1,ACL2,NO, MS_L,MS_D,MS_U,MS_R,FN3, NO, ENT, \ + LCTL,NO, ACL0,ACL1,ACL2,NO, MS_L,MS_D,MS_U,MS_R,FN0, NO, ENT, \ LSFT,NO, NO, NO, NO, BTN3,BTN2,BTN1,BTN4,BTN5,NO, RSFT,NO, \ - LGUI,LALT, FN5, RALT,RGUI), + LGUI,LALT, FN0, RALT,RGUI), }; #define KEYCODE(layer, row, col) (pgm_read_byte(&keymaps[(layer)][(row)][(col)])) @@ -183,39 +195,31 @@ action_t keymap_get_action(uint8_t layer, uint8_t row, uint8_t col) { action_t action; switch (key) { case KC_A ... KC_EXSEL: - action = (action_t)ACTION_KEY(key); + action.code = ACTION_KEY(key); break; case KC_SYSTEM_POWER ... KC_SYSTEM_WAKE: - action = (action_t)ACTION_USAGE_SYSTEM(KEYCODE2SYSTEM(key)); + action.code = ACTION_USAGE_SYSTEM(KEYCODE2SYSTEM(key)); break; case KC_AUDIO_MUTE ... KC_WWW_FAVORITES: - action = (action_t)ACTION_USAGE_CONSUMER(KEYCODE2CONSUMER(key)); + action.code = ACTION_USAGE_CONSUMER(KEYCODE2CONSUMER(key)); break; case KC_MS_UP ... KC_MS_ACCEL2: - action = (action_t)ACTION_MOUSEKEY(key); + action.code = ACTION_MOUSEKEY(key); break; case KC_LCTRL ... KC_LGUI: - action = (action_t)ACTION_LMODS(MOD_BIT(key)); + action.code = ACTION_LMODS(MOD_BIT(key)); break; case KC_RCTRL ... KC_RGUI: - action = (action_t)ACTION_RMODS(MOD_BIT(key)>>4); + action.code = ACTION_RMODS(MOD_BIT(key)>>4); break; case KC_FN0 ... KC_FN7: - action = (action_t)pgm_read_word(&fn_actions[FN_INDEX(key)]); + action.code = pgm_read_word(&fn_actions[FN_INDEX(key)]); break; case KC_NO ... KC_UNDEFINED: default: - action = (action_t)NO_ACTION; + action.code = ACTION_NO; break; } debug("action: "); debug_hex16(action.code); debug("\n"); return action; } - - -uint8_t keymap_process_event(keyevent_t event) -{ - action_t action = keymap_get_action(current_layer, event.key.row, event.key.col); - action_exec(action, event); - return 0; -} -- cgit v1.2.3-70-g09d2 From 32633a42c74c65462370ef4a39a44a5784a98a06 Mon Sep 17 00:00:00 2001 From: tmk Date: Sun, 13 Jan 2013 10:24:20 +0900 Subject: Fix tap key using delaying_layer and waiting_key. --- common/action.c | 167 ++++++++++++++++++++++++++++++++++++++++--------- common/host.c | 5 ++ common/host.h | 3 + common/keyboard.c | 2 +- keyboard/hhkb/keymap.c | 8 ++- 5 files changed, 152 insertions(+), 33 deletions(-) (limited to 'common/action.c') diff --git a/common/action.c b/common/action.c index 425a2b00f..1a86f16d3 100644 --- a/common/action.c +++ b/common/action.c @@ -115,29 +115,25 @@ uint8_t default_layer = 0; uint8_t current_layer = 0; keyrecord_t delaying_layer = {}; +keyrecord_t waiting_key = {}; -void action_exec(keyevent_t event) +// TODO: ring buffer: waiting_keys[] +/* +#define WAITING_KEYS_BUFFER 3 +static keyrecord_t waiting_keys[WAITING_KEYS_BUFFER] = {}; +static uint8_t waiting_keys_head = 0; +static uint8_t waiting_keys_tail = 0; +static void waiting_key_queue(keyevent_t event) { - /* count tap when key is up */ - if (KEYEQ(event.key, last_event.key) && timer_elapsed(last_event_time) < TAP_TIME) { - if (!event.pressed) tap_count++; - } else { - tap_count = 0; - } +} +static void waiting_key_dequeue(keyevent_t event) +{ +} +*/ - /* layer switch after LAYER_DELAY */ - if (delaying_layer.action.code && timer_elapsed(delaying_layer.event.time) > LAYER_DELAY) { - switch (delaying_layer.action.kind.id) { - case ACT_LAYER_PRESSED: - layer_switch(delaying_layer.action.layer.opt); - break; - case ACT_LAYER_BIT: - layer_switch(current_layer | delaying_layer.action.layer.opt); - break; - } - delaying_layer = (keyrecord_t){}; - } - action_t action = keymap_get_action(current_layer, event.key.row, event.key.col); +static void process(keyevent_t event, action_t action) +{ + //action_t action = keymap_get_action(current_layer, event.key.row, event.key.col); debug("action: "); debug_hex16(action.code); debug("\n"); debug("kind.id: "); debug_hex(action.kind.id); debug("\n"); @@ -146,29 +142,54 @@ void action_exec(keyevent_t event) debug("key.mods: "); debug_hex(action.key.mods); debug("\n"); switch (action.kind.id) { + /* Key and Mods */ case ACT_LMODS: // normal key or key plus mods if (event.pressed) { - register_mods(action.key.mods); + uint8_t tmp_mods = host_get_mods(); + if (action.key.mods) { + host_add_mods(action.key.mods); + host_send_keyboard_report(); + } register_code(action.key.code); + if (action.key.mods && action.key.code) { + host_set_mods(tmp_mods); + host_send_keyboard_report(); + } } else { + if (action.key.mods && !action.key.code) { + host_del_mods(action.key.mods); + host_send_keyboard_report(); + } unregister_code(action.key.code); - unregister_mods(action.key.mods); } break; case ACT_RMODS: if (event.pressed) { - register_mods(action.key.mods<<4); + uint8_t tmp_mods = host_get_mods(); + if (action.key.mods) { + host_add_mods(action.key.mods<<4); + host_send_keyboard_report(); + } register_code(action.key.code); + if (action.key.mods && action.key.code) { + host_set_mods(tmp_mods); + host_send_keyboard_report(); + } } else { + if (action.key.mods && !action.key.code) { + host_del_mods(action.key.mods<<4); + host_send_keyboard_report(); + } unregister_code(action.key.code); - unregister_mods(action.key.mods<<4); } break; case ACT_LMOD_TAP: break; case ACT_RMOD_TAP: break; + + /* other HID usage */ case ACT_USAGE: #ifdef EXTRAKEY_ENABLE switch (action.usage.page) { @@ -189,6 +210,8 @@ void action_exec(keyevent_t event) } #endif break; + + /* Mouse key */ case ACT_MOUSEKEY: #ifdef MOUSEKEY_ENABLE if (event.pressed) { @@ -200,6 +223,8 @@ void action_exec(keyevent_t event) } #endif break; + + /* Layer key */ case ACT_LAYER_PRESSED: // layer action when pressed switch (action.layer.code) { @@ -228,19 +253,25 @@ void action_exec(keyevent_t event) delaying_layer = (keyrecord_t){ .event = event, .action = action, - .mods = keyboard_report->mods + .mods = host_get_mods() }; } } else if (tap_count > 0) { register_code(action.layer.code); } } else { - // type key after tap - if (tap_count == 1) { - delaying_layer = (keyrecord_t){}; - register_code(action.layer.code); + // tap key + if (KEYEQ(event.key, delaying_layer.event.key) && + timer_elapsed(delaying_layer.event.time) < TAP_TIME) { + uint8_t tmp_mods = host_get_mods(); + host_set_mods(delaying_layer.mods); + register_code(delaying_layer.action.layer.code); + host_set_mods(tmp_mods); + unregister_code(delaying_layer.action.layer.code); + } else { + unregister_code(action.layer.code); } - unregister_code(action.layer.code); + delaying_layer = (keyrecord_t){}; } break; } @@ -366,12 +397,87 @@ void action_exec(keyevent_t event) break; } break; + + /* Extentions */ case ACT_MACRO: case ACT_COMMAND: case ACT_FUNCTION: default: break; } +} + +void action_exec(keyevent_t event) +{ + /* count tap when key is up */ + if (KEYEQ(event.key, last_event.key) && timer_elapsed(last_event_time) < TAP_TIME) { + if (!event.pressed) tap_count++; + } else { + tap_count = 0; + } + + /* When delaying layer switch */ + if (delaying_layer.action.code) { + /* Layer switch when delay time elapses or waiting key is released */ + if ((timer_elapsed(delaying_layer.event.time) > LAYER_DELAY) || + (!event.pressed && KEYEQ(event.key, waiting_key.event.key))) { + /* layer switch */ + switch (delaying_layer.action.kind.id) { + case ACT_LAYER_PRESSED: + layer_switch(delaying_layer.action.layer.opt); + break; + case ACT_LAYER_BIT: + layer_switch(current_layer | delaying_layer.action.layer.opt); + break; + } + delaying_layer = (keyrecord_t){}; + + /* Process waiting keys in new layer */ + if (waiting_key.event.time) { + uint8_t tmp_mods = host_get_mods(); + host_set_mods(waiting_key.mods); + process(waiting_key.event, keymap_get_action(current_layer, + waiting_key.event.key.row, + waiting_key.event.key.col)); + host_set_mods(tmp_mods); + waiting_key = (keyrecord_t){}; + } + } + /* when delaying layer key is released within delay term */ + else if (!event.pressed && KEYEQ(event.key, delaying_layer.event.key)) { + /* tap key down */ + uint8_t tmp_mods = host_get_mods(); + host_set_mods(delaying_layer.mods); + register_code(delaying_layer.action.layer.code); + delaying_layer = (keyrecord_t){}; + + /* process waiting keys */ + if (waiting_key.event.time) { + host_set_mods(waiting_key.mods); + process(waiting_key.event, waiting_key.action); + waiting_key = (keyrecord_t){}; + } + host_set_mods(tmp_mods); + } + } + + action_t action = keymap_get_action(current_layer, event.key.row, event.key.col); + + /* postpone key-down events while delaying layer */ + if (delaying_layer.action.code) { + if (event.pressed) { + // TODO: waiting_keys[] + waiting_key = (keyrecord_t){ + .event = event, + .action = action, + .mods = host_get_mods() + }; + } else { + process(event, action); + } + } else { + process(event, action); + } /* last event */ last_event = event; @@ -451,5 +557,6 @@ static void layer_switch(uint8_t new_layer) current_layer = new_layer; clear_keyboard_but_mods(); // To avoid stuck keys + // TODO: update mods with full scan of matrix? if modifier changes between layers } } diff --git a/common/host.c b/common/host.c index 28c8a819f..6ed3d780f 100644 --- a/common/host.c +++ b/common/host.c @@ -127,6 +127,11 @@ void host_clear_keys(void) } } +uint8_t host_get_mods(void) +{ + return keyboard_report->mods; +} + void host_add_mods(uint8_t mods) { keyboard_report->mods |= mods; diff --git a/common/host.h b/common/host.h index 4f1f234a9..c59fbfee6 100644 --- a/common/host.h +++ b/common/host.h @@ -51,10 +51,13 @@ void host_consumer_send(uint16_t data); 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); diff --git a/common/keyboard.c b/common/keyboard.c index 1e0b8c3ed..4e955e129 100644 --- a/common/keyboard.c +++ b/common/keyboard.c @@ -68,7 +68,7 @@ void keyboard_task(void) action_exec((keyevent_t){ .key = (keypos_t){ .row = r, .col = c }, .pressed = (matrix_row & (1<>4); break; +*/ case KC_FN0 ... KC_FN7: action.code = pgm_read_word(&fn_actions[FN_INDEX(key)]); break; -- cgit v1.2.3-70-g09d2 From f609712da3b94ea36612a6f210bd6ce902b74631 Mon Sep 17 00:00:00 2001 From: tmk Date: Tue, 15 Jan 2013 00:06:52 +0900 Subject: Fix waiting_keys and periodical update for delaying layer. --- common/action.c | 222 +++++++++++++++++-------------------------------- common/keyboard.c | 9 +- common/keyboard.h | 9 +- keyboard/hhkb/keymap.c | 1 - 4 files changed, 90 insertions(+), 151 deletions(-) (limited to 'common/action.c') diff --git a/common/action.c b/common/action.c index 1a86f16d3..389fc5df1 100644 --- a/common/action.c +++ b/common/action.c @@ -9,103 +9,20 @@ #include "debug.h" #include "action.h" -#define Kdebug(s) do { if (debug_keyboard) debug(s); } while(0) -#define Kdebug_P(s) do { if (debug_keyboard) debug_P(s); } while(0) -#define Kdebug_hex(s) do { if (debug_keyboard) debug_hex(s); } while(0) -/* - * - * Event/State|IDLE PRESSING DELAYING[f] WAITING[f,k] - * -----------+------------------------------------------------------------------ - * Fn Down |(L+) -*1 WAITING(Sk) IDLE(Rf,Ps)*7 - * Up |(L-) IDLE(L-)*8 IDLE(L-)*8 IDLE(L-)*8 - * Fnk Down |DELAYING(Sf)* (Rf) WAITING(Sk) IDLE(Rf,Ps,Rf) - * Up |(L-) IDLE(L-/Uf)*8 IDLE(Rf,Uf/L-)*3 IDLE(Rf,Ps,Uf/L-)*3 - * Key Down |PRESSING(Rk) (Rk) WAITING(Sk) IDLE(Rf,Ps,Rk) - * Up |(Uk) IDLE(Uk)*4 (Uk) IDLE(L+,Ps,Pk)/(Uk)*a - * | - * Delay |- - IDLE(L+) IDLE(L+,Ps) - * Magic Key |COMMAND*5 - * - * *1: ignore Fn if other key is down. - * *2: register Fnk if any key is pressing - * *3: register/unregister delayed Fnk and move to IDLE if code == delayed Fnk, else *8 - * *4: if no keys registered to host - * *5: unregister all keys - * *6: only if no keys down - * *7: ignore Fn because Fnk key and stored key are down. - * *8: move to IDLE if layer switch(off) occurs, else stay at current state - * *9: repeat key if pressing Fnk twice quickly(move to PRESSING) - * *a: layer switch and process waiting key and code if code == wainting key, else unregister key - * - * States: - * IDLE: No key is down except modifiers - * DELAYING: delay layer switch after pressing Fn with alt keycode - * WAITING: key is pressed during DELAYING - * - * Events: - * Fn: Fn key without alternative keycode - * Fnk: Fn key with alternative keycode - * -: ignore - * Delay: layer switch delay term is elapsed - * - * Actions: - * Rk: register key - * Uk: unregister key - * Rf: register Fn(alt keycode) - * Uf: unregister Fn(alt keycode) - * Rs: register stored key - * Us: unregister stored key - * Sk: Store key(waiting Key) - * Sf: Store Fn(delayed Fn) - * Ps: Process stored key - * Ps: Process key - * Is: Interpret stored keys in current layer - * L+: Switch to new layer(*unregister* all keys but modifiers) - * L-: Switch back to last layer(*unregister* all keys but modifiers) - * Ld: Switch back to default layer(*unregister* all keys but modifiers) - */ - - -typedef enum { IDLE, DELAYING, WAITING, PRESSING } kbdstate_t; -#define NEXT(state) do { \ - Kdebug("NEXT: "); Kdebug_P(state_str(kbdstate)); \ - kbdstate = state; \ - Kdebug(" -> "); Kdebug_P(state_str(kbdstate)); Kdebug("\n"); \ -} while (0) - - -static kbdstate_t kbdstate = IDLE; -static uint8_t fn_state_bits = 0; - -static const char *state_str(kbdstate_t state) -{ - if (state == IDLE) return PSTR("IDLE"); - if (state == DELAYING) return PSTR("DELAYING"); - if (state == WAITING) return PSTR("WAITING"); - if (state == PRESSING) return PSTR("PRESSING"); - return PSTR("UNKNOWN"); -} -static bool anykey_sent_to_host(void) -{ - return (host_has_anykey() || host_mouse_in_use() || - host_last_sysytem_report() || host_last_consumer_report()); -} - +static void process(keyevent_t event, action_t action); static void register_code(uint8_t code); static void unregister_code(uint8_t code); -static void register_mods(uint8_t mods); -static void unregister_mods(uint8_t mods); static void clear_keyboard(void); static void clear_keyboard_but_mods(void); +static bool sending_anykey(void); static void layer_switch(uint8_t new_layer); /* tap */ #define TAP_TIME 200 -#define LAYER_DELAY 200 static keyevent_t last_event = {}; static uint16_t last_event_time = 0; static uint8_t tap_count = 0; @@ -115,31 +32,54 @@ uint8_t default_layer = 0; uint8_t current_layer = 0; keyrecord_t delaying_layer = {}; -keyrecord_t waiting_key = {}; - -// TODO: ring buffer: waiting_keys[] -/* #define WAITING_KEYS_BUFFER 3 static keyrecord_t waiting_keys[WAITING_KEYS_BUFFER] = {}; static uint8_t waiting_keys_head = 0; -static uint8_t waiting_keys_tail = 0; -static void waiting_key_queue(keyevent_t event) +static bool waiting_keys_enqueue(keyevent_t event, action_t action) { + debug("waiting_keys["); debug_dec(waiting_keys_head); debug("] = "); + debug_hex16(action.code); debug("\n"); + if (waiting_keys_head < WAITING_KEYS_BUFFER) { + waiting_keys[waiting_keys_head++] = (keyrecord_t){ .event = event, + .action = action, + .mods = host_get_mods() }; + } else { + return true; + } } -static void waiting_key_dequeue(keyevent_t event) +static void waiting_keys_clear(void) { + waiting_keys_head = 0; } -*/ +static bool waiting_keys_has(keypos_t key) +{ + for (uint8_t i = 0; i < waiting_keys_head; i++) { + if KEYEQ(key, waiting_keys[i].event.key) return true; + } + return false; +} +static void waiting_keys_process_in_current_layer(void) +{ + // TODO: in case of including layer key in waiting keys + uint8_t tmp_mods = host_get_mods(); + for (uint8_t i = 0; i < waiting_keys_head; i++) { + /* revive status of mods */ + host_set_mods(waiting_keys[i].mods); + process(waiting_keys[i].event, keymap_get_action(current_layer, + waiting_keys[i].event.key.row, + waiting_keys[i].event.key.col)); + debug("waiting_keys_process_in_current_layer["); debug_dec(i); debug("]\n"); + } + host_set_mods(tmp_mods); + waiting_keys_clear(); +} + static void process(keyevent_t event, action_t action) { //action_t action = keymap_get_action(current_layer, event.key.row, event.key.col); - debug("action: "); debug_hex16(action.code); debug("\n"); - debug("kind.id: "); debug_hex(action.kind.id); debug("\n"); - debug("kind.param: "); debug_hex16(action.kind.param); debug("\n"); - debug("key.code: "); debug_hex(action.key.code); debug("\n"); - debug("key.mods: "); debug_hex(action.key.mods); debug("\n"); + switch (action.kind.id) { /* Key and Mods */ @@ -244,12 +184,12 @@ static void process(keyevent_t event, action_t action) break; default: // with tap key - debug("tap: "); debug_hex(tap_count); debug("\n"); if (event.pressed) { if (tap_count == 0) { if (host_has_anykey()) { register_code(action.layer.code); } else { + debug("Delay switching layer("); debug_hex8(action.layer.opt); debug(")\n"); delaying_layer = (keyrecord_t){ .event = event, .action = action, @@ -257,12 +197,13 @@ static void process(keyevent_t event, action_t action) }; } } else if (tap_count > 0) { + debug("tap: "); debug_hex(tap_count); debug("\n"); register_code(action.layer.code); } } else { // tap key if (KEYEQ(event.key, delaying_layer.event.key) && - timer_elapsed(delaying_layer.event.time) < TAP_TIME) { + timer_elapsed(delaying_layer.event.time) <= TAP_TIME) { uint8_t tmp_mods = host_get_mods(); host_set_mods(delaying_layer.mods); register_code(delaying_layer.action.layer.code); @@ -321,7 +262,6 @@ static void process(keyevent_t event, action_t action) break; default: // with tap key - debug("tap: "); debug_hex(tap_count); debug("\n"); if (event.pressed) { if (tap_count == 0) { if (host_has_anykey()) { @@ -334,6 +274,7 @@ static void process(keyevent_t event, action_t action) }; } } else if (tap_count > 0) { + debug("tap: "); debug_hex(tap_count); debug("\n"); register_code(action.layer.code); } } else { @@ -409,18 +350,16 @@ static void process(keyevent_t event, action_t action) void action_exec(keyevent_t event) { - /* count tap when key is up */ - if (KEYEQ(event.key, last_event.key) && timer_elapsed(last_event_time) < TAP_TIME) { - if (!event.pressed) tap_count++; - } else { - tap_count = 0; - } +/* + debug("key["); debug_hex8(event.key.row); debug(":"); debug_hex8(event.key.col); + if (event.pressed) debug("]down\n"); else debug("]up\n"); +*/ /* When delaying layer switch */ if (delaying_layer.action.code) { - /* Layer switch when delay time elapses or waiting key is released */ - if ((timer_elapsed(delaying_layer.event.time) > LAYER_DELAY) || - (!event.pressed && KEYEQ(event.key, waiting_key.event.key))) { + /* Layer switch when tap time elapses or waiting key is released */ + if ((timer_elapsed(delaying_layer.event.time) > TAP_TIME) || + (!event.pressed && waiting_keys_has(event.key))) { /* layer switch */ switch (delaying_layer.action.kind.id) { case ACT_LAYER_PRESSED: @@ -433,15 +372,7 @@ void action_exec(keyevent_t event) delaying_layer = (keyrecord_t){}; /* Process waiting keys in new layer */ - if (waiting_key.event.time) { - uint8_t tmp_mods = host_get_mods(); - host_set_mods(waiting_key.mods); - process(waiting_key.event, keymap_get_action(current_layer, - waiting_key.event.key.row, - waiting_key.event.key.col)); - host_set_mods(tmp_mods); - waiting_key = (keyrecord_t){}; - } + waiting_keys_process_in_current_layer(); } /* when delaying layer key is released within delay term */ else if (!event.pressed && KEYEQ(event.key, delaying_layer.event.key)) { @@ -450,28 +381,32 @@ void action_exec(keyevent_t event) host_set_mods(delaying_layer.mods); register_code(delaying_layer.action.layer.code); delaying_layer = (keyrecord_t){}; + host_set_mods(tmp_mods); /* process waiting keys */ - if (waiting_key.event.time) { - host_set_mods(waiting_key.mods); - process(waiting_key.event, waiting_key.action); - waiting_key = (keyrecord_t){}; - } - host_set_mods(tmp_mods); + waiting_keys_process_in_current_layer(); } } + // not real event. event just to update delaying layer. + if (IS_NOEVENT(event)) { + return; + } + + /* count tap when key is up */ + if (KEYEQ(event.key, last_event.key) && timer_elapsed(last_event.time) <= TAP_TIME) { + if (!event.pressed) tap_count++; + } else { + tap_count = 0; + } + action_t action = keymap_get_action(current_layer, event.key.row, event.key.col); + // TODO: all key events(pressed, released) should be recorded? /* postpone key-down events while delaying layer */ if (delaying_layer.action.code) { if (event.pressed) { - // TODO: waiting_keys[] - waiting_key = (keyrecord_t){ - .event = event, - .action = action, - .mods = host_get_mods() - }; + waiting_keys_enqueue(event, action); } else { process(event, action); } @@ -481,7 +416,6 @@ void action_exec(keyevent_t event) /* last event */ last_event = event; - last_event_time = timer_read(); } @@ -515,20 +449,6 @@ static void unregister_code(uint8_t code) } } -static void register_mods(uint8_t mods) -{ - if (!mods) return; - host_add_mods(mods); - host_send_keyboard_report(); -} - -static void unregister_mods(uint8_t mods) -{ - if (!mods) return; - host_del_mods(mods); - host_send_keyboard_report(); -} - static void clear_keyboard(void) { host_clear_mods(); @@ -549,11 +469,17 @@ static void clear_keyboard_but_mods(void) #endif } +static bool sending_anykey(void) +{ + return (host_has_anykey() || host_mouse_in_use() || + host_last_sysytem_report() || host_last_consumer_report()); +} + static void layer_switch(uint8_t new_layer) { if (current_layer != new_layer) { - Kdebug("Layer Switch: "); Kdebug_hex(current_layer); - Kdebug(" -> "); Kdebug_hex(new_layer); Kdebug("\n"); + debug("Layer Switch: "); debug_hex(current_layer); + debug(" -> "); debug_hex(new_layer); debug("\n"); current_layer = new_layer; clear_keyboard_but_mods(); // To avoid stuck keys diff --git a/common/keyboard.c b/common/keyboard.c index 4e955e129..2e32e91e0 100644 --- a/common/keyboard.c +++ b/common/keyboard.c @@ -78,7 +78,14 @@ void keyboard_task(void) } } } - MATRIX_LOOP_END: + // call to update delaying layer when no real event + action_exec((keyevent_t) { + .key = (keypos_t){ .row = 255, .col = 255 }, // assume this key doesn't exist + .pressed = false, + .time = 0, + }); + +MATRIX_LOOP_END: #ifdef MOUSEKEY_ENABLE // mousekey repeat & acceleration diff --git a/common/keyboard.h b/common/keyboard.h index 116653661..cf85b1233 100644 --- a/common/keyboard.h +++ b/common/keyboard.h @@ -37,7 +37,14 @@ typedef struct { uint16_t time; } keyevent_t; -#define KEYEQ(keya, keyb) (keya.row == keyb.row && keya.col == keyb.col) +#define KEYEQ(keya, keyb) (keya.row == keyb.row && keya.col == keyb.col) +#define IS_NOEVENT(event) (event.time == 0) +#define NOEVENT (keyevent_t) { \ + .key = (keypos_t){ .row = 255, .col = 255 }, \ + .pressed = false, \ + .time = 0, \ +} + extern uint8_t current_layer; extern uint8_t default_layer; diff --git a/keyboard/hhkb/keymap.c b/keyboard/hhkb/keymap.c index 9fe1237aa..38461290b 100644 --- a/keyboard/hhkb/keymap.c +++ b/keyboard/hhkb/keymap.c @@ -224,6 +224,5 @@ action_t keymap_get_action(uint8_t layer, uint8_t row, uint8_t col) { action.code = ACTION_NO; break; } - debug("action: "); debug_hex16(action.code); debug("\n"); return action; } -- cgit v1.2.3-70-g09d2 From 9f95e9cc27f2edf4336124b01c05d03dcd5ee5ac Mon Sep 17 00:00:00 2001 From: tmk Date: Tue, 15 Jan 2013 19:04:58 +0900 Subject: Add support partly for modifier with tap key. --- common/action.c | 249 ++++++++++++++++++++++++++++++------------------- common/action.h | 161 ++++++++++++++++---------------- keyboard/hhkb/keymap.c | 18 +--- 3 files changed, 239 insertions(+), 189 deletions(-) (limited to 'common/action.c') 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 @@ #include "action.h" - - static void process(keyevent_t event, action_t action); static void register_code(uint8_t code); static void unregister_code(uint8_t code); +static void add_mods(uint8_t mods); +static void del_mods(uint8_t mods); +static void set_mods(uint8_t mods); static void clear_keyboard(void); static void clear_keyboard_but_mods(void); static bool sending_anykey(void); @@ -24,7 +25,6 @@ static void layer_switch(uint8_t new_layer); /* tap */ #define TAP_TIME 200 static keyevent_t last_event = {}; -static uint16_t last_event_time = 0; static uint8_t tap_count = 0; /* layer */ @@ -32,6 +32,7 @@ uint8_t default_layer = 0; uint8_t current_layer = 0; keyrecord_t delaying_layer = {}; +/* waiting keys buffer */ #define WAITING_KEYS_BUFFER 3 static keyrecord_t waiting_keys[WAITING_KEYS_BUFFER] = {}; static uint8_t waiting_keys_head = 0; @@ -75,16 +76,87 @@ static void waiting_keys_process_in_current_layer(void) } +void action_exec(keyevent_t event) +{ + /* When delaying layer switch */ + if (delaying_layer.action.code) { + /* Layer switch when tap time elapses or waiting key is released */ + if ((timer_elapsed(delaying_layer.event.time) > TAP_TIME) || + (!event.pressed && waiting_keys_has(event.key))) { + /* layer switch */ + switch (delaying_layer.action.kind.id) { + case ACT_LAYER_PRESSED: + layer_switch(delaying_layer.action.layer.opt); + break; + case ACT_LAYER_BIT: + layer_switch(current_layer | delaying_layer.action.layer.opt); + break; + } + delaying_layer = (keyrecord_t){}; + + /* Process waiting keys in new layer */ + waiting_keys_process_in_current_layer(); + } + /* when delaying layer key is released within delay term */ + else if (!event.pressed && KEYEQ(event.key, delaying_layer.event.key)) { + /* tap key down */ + debug("tap[delaying_layer](register): fist\n"); + uint8_t tmp_mods = host_get_mods(); + host_set_mods(delaying_layer.mods); + register_code(delaying_layer.action.layer.code); + delaying_layer = (keyrecord_t){}; + host_set_mods(tmp_mods); + + /* process waiting keys */ + waiting_keys_process_in_current_layer(); + } + } + + // not real event. event just to update delaying layer. + if (IS_NOEVENT(event)) { + return; + } + + /* count tap when key is up */ + if (KEYEQ(event.key, last_event.key) && timer_elapsed(last_event.time) <= TAP_TIME) { + if (!event.pressed) tap_count++; + } else { + tap_count = 0; + } + + action_t action = keymap_get_action(current_layer, event.key.row, event.key.col); + + // TODO: all key events(pressed, released) should be recorded? + /* postpone key-down events while delaying layer */ + if (delaying_layer.action.code) { + if (event.pressed) { + waiting_keys_enqueue(event, action); + } else { + process(event, action); + } + } else { + process(event, action); + } + + /* last event */ + last_event = event; +} + + static void process(keyevent_t event, action_t action) { //action_t action = keymap_get_action(current_layer, event.key.row, event.key.col); - debug("action: "); debug_hex16(action.code); debug("\n"); - + debug("action: "); debug_hex16(action.code); + if (event.pressed) debug("[down]\n"); else debug("[up]\n"); switch (action.kind.id) { /* Key and Mods */ case ACT_LMODS: - // normal key or key plus mods + // |pressed |released + // --------------+---------------------------------+------------ + // key |down(key) |up(key) + // mods |add(mods) |del(mods) + // key with mods |add(mods), down(key), unset(mods)|up(key) if (event.pressed) { uint8_t tmp_mods = host_get_mods(); if (action.key.mods) { @@ -105,6 +177,11 @@ static void process(keyevent_t event, action_t action) } break; case ACT_RMODS: + // |pressed |released + // --------------+---------------------------------+------------ + // key |down(key) |up(key) + // mods |add(mods) |del(mods) + // key with mods |add(mods), down(key), unset(mods)|up(key) if (event.pressed) { uint8_t tmp_mods = host_get_mods(); if (action.key.mods) { @@ -124,9 +201,49 @@ static void process(keyevent_t event, action_t action) unregister_code(action.key.code); } break; - case ACT_LMOD_TAP: + case ACT_LMODS_TAP: + if (event.pressed) { + if (tap_count == 0) { + add_mods(action.key.mods); + } else { + debug("tap[lmods](register): "); debug_hex(tap_count); debug("\n"); + register_code(action.key.code); + } + } else { + if (tap_count == 0) { + del_mods(action.key.mods); + } else if (tap_count == 1) { + debug("tap[lmods](register/unregister): "); debug_hex(tap_count); debug("\n"); + del_mods(action.key.mods); + register_code(action.key.code); + unregister_code(action.key.code); + } else { + debug("tap[lmods](unregister): "); debug_hex(tap_count); debug("\n"); + unregister_code(action.key.code); + } + } break; - case ACT_RMOD_TAP: + case ACT_RMODS_TAP: + if (event.pressed) { + if (tap_count == 0) { + add_mods(action.key.mods<<4); + } else { + debug("tap[rmods](register): "); debug_hex(tap_count); debug("\n"); + register_code(action.key.code); + } + } else { + if (tap_count == 0) { + del_mods(action.key.mods<<4); + } else if (tap_count == 1) { + debug("tap[rmods](register/unregister): "); debug_hex(tap_count); debug("\n"); + del_mods(action.key.mods<<4); + register_code(action.key.code); + unregister_code(action.key.code); + } else { + debug("tap[rmods](unregister): "); debug_hex(tap_count); debug("\n"); + unregister_code(action.key.code); + } + } break; /* other HID usage */ @@ -186,6 +303,7 @@ static void process(keyevent_t event, action_t action) // with tap key if (event.pressed) { if (tap_count == 0) { + // not tapping yet if (host_has_anykey()) { register_code(action.layer.code); } else { @@ -197,22 +315,14 @@ static void process(keyevent_t event, action_t action) }; } } else if (tap_count > 0) { - debug("tap: "); debug_hex(tap_count); debug("\n"); + // pressed after tapping + debug("tap[layer](register): "); debug_hex(tap_count); debug("\n"); register_code(action.layer.code); } } else { - // tap key - if (KEYEQ(event.key, delaying_layer.event.key) && - timer_elapsed(delaying_layer.event.time) <= TAP_TIME) { - uint8_t tmp_mods = host_get_mods(); - host_set_mods(delaying_layer.mods); - register_code(delaying_layer.action.layer.code); - host_set_mods(tmp_mods); - unregister_code(delaying_layer.action.layer.code); - } else { - unregister_code(action.layer.code); - } - delaying_layer = (keyrecord_t){}; + // released after tapping + debug("tap[layer](unregister): "); debug_hex(tap_count); debug("\n"); + unregister_code(action.layer.code); } break; } @@ -220,7 +330,7 @@ static void process(keyevent_t event, action_t action) case ACT_LAYER_RELEASED: switch (action.layer.code) { case 0x00: - if (event.pressed) { + if (!event.pressed) { layer_switch(action.layer.opt); } break; @@ -274,7 +384,7 @@ static void process(keyevent_t event, action_t action) }; } } else if (tap_count > 0) { - debug("tap: "); debug_hex(tap_count); debug("\n"); + debug("tap[layer_bit](register): "); debug_hex(tap_count); debug("\n"); register_code(action.layer.code); } } else { @@ -348,77 +458,6 @@ static void process(keyevent_t event, action_t action) } } -void action_exec(keyevent_t event) -{ -/* - debug("key["); debug_hex8(event.key.row); debug(":"); debug_hex8(event.key.col); - if (event.pressed) debug("]down\n"); else debug("]up\n"); -*/ - - /* When delaying layer switch */ - if (delaying_layer.action.code) { - /* Layer switch when tap time elapses or waiting key is released */ - if ((timer_elapsed(delaying_layer.event.time) > TAP_TIME) || - (!event.pressed && waiting_keys_has(event.key))) { - /* layer switch */ - switch (delaying_layer.action.kind.id) { - case ACT_LAYER_PRESSED: - layer_switch(delaying_layer.action.layer.opt); - break; - case ACT_LAYER_BIT: - layer_switch(current_layer | delaying_layer.action.layer.opt); - break; - } - delaying_layer = (keyrecord_t){}; - - /* Process waiting keys in new layer */ - waiting_keys_process_in_current_layer(); - } - /* when delaying layer key is released within delay term */ - else if (!event.pressed && KEYEQ(event.key, delaying_layer.event.key)) { - /* tap key down */ - uint8_t tmp_mods = host_get_mods(); - host_set_mods(delaying_layer.mods); - register_code(delaying_layer.action.layer.code); - delaying_layer = (keyrecord_t){}; - host_set_mods(tmp_mods); - - /* process waiting keys */ - waiting_keys_process_in_current_layer(); - } - } - - // not real event. event just to update delaying layer. - if (IS_NOEVENT(event)) { - return; - } - - /* count tap when key is up */ - if (KEYEQ(event.key, last_event.key) && timer_elapsed(last_event.time) <= TAP_TIME) { - if (!event.pressed) tap_count++; - } else { - tap_count = 0; - } - - action_t action = keymap_get_action(current_layer, event.key.row, event.key.col); - - // TODO: all key events(pressed, released) should be recorded? - /* postpone key-down events while delaying layer */ - if (delaying_layer.action.code) { - if (event.pressed) { - waiting_keys_enqueue(event, action); - } else { - process(event, action); - } - } else { - process(event, action); - } - - /* last event */ - last_event = event; -} - - static void register_code(uint8_t code) { if (code == KC_NO) { @@ -449,6 +488,28 @@ static void unregister_code(uint8_t code) } } +static void add_mods(uint8_t mods) +{ + if (mods) { + host_add_mods(mods); + host_send_keyboard_report(); + } +} + +static void del_mods(uint8_t mods) +{ + if (mods) { + host_del_mods(mods); + host_send_keyboard_report(); + } +} + +static void set_mods(uint8_t mods) +{ + host_set_mods(mods); + host_send_keyboard_report(); +} + static void clear_keyboard(void) { host_clear_mods(); diff --git a/common/action.h b/common/action.h index 942ce191a..3115c67f4 100644 --- a/common/action.h +++ b/common/action.h @@ -5,55 +5,57 @@ /* Key Action(16bit code) - 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 ------------------------------------------------- -ACT_LMODS(0000) - 0 0 0 0| 0 0 0 0| 0 0 0 0 0 0| 0 0 No action - 0 0 0 0| 0 0 0 0| keycode(8) Key - 0 0 0 0| mods(4) | 0 0 0 0 0 0| 0 0 Lmods Momentary - 0 0 0 0| mods(4) | 0 0 0 0 0 0| 0 1 Lmods OneShot - 0 0 0 0| mods(4) | 0 0 0 0 0 0| 1 0 (reserved) - 0 0 0 0| mods(4) | 0 0 0 0 0 0| 1 1 (reserved) - 0 0 0 0| mods(4) | keycode(8) Key+Lmods - -ACT_RMODS(0001) - 0 0 0 1| 0 0 0 0| 0 0 0 0 0 0 0 0 No action(not used) - 0 0 0 1| 0 0 0 0| keycode(8) Key(not used) - 0 0 0 1| mods(4) | 0 0 0 0 0 0| 0 0 Rmods Momentary - 0 0 0 1| mods(4) | 0 0 0 0 0 0| 0 1 Rmods OneShot - 0 0 0 1| mods(4) | 0 0 0 0 0 0| 1 0 (reserved) - 0 0 0 1| mods(4) | 0 0 0 0 0 0| 1 1 (reserved) - 0 0 0 1| mods(4) | keycode(8) Key+Rmod - -ACT_LMODS_TAP(0010) - 0 0 1 0| 0 0 0 0| X X X X X X X X (reserved)[00-FF] - 0 0 1 0| mods(4) | 0 0 0 0 0 0| X X (reserved) - 0 0 1 0| mods(4) | keycode(8) Lmods+tap Key - 0 0 1 0| mods(4) | 1 1 1 1| X X X X (reserved)[F0-FF] - -ACT_RMODS_TAP(0011) - 0 0 1 1| 0 0 0 0| X X X X X X X X (reserved)[00-FF] - 0 0 1 1| mods(4) | 0 0 0 0 0 0| X X (reserved) - 0 0 1 1| mods(4) | keycode(8) Rmods+tap Key - 0 0 1 1| mods(4) | 1 1 1 1| X X X X (reserved)[F0-FF] + +Keyboard Keys +------------- +ACT_LMODS(0000): +0000|0000|000000|00 No action +0000|mods|000000|00 Left mods Momentary +0000|mods|000000|01 Left mods OneShot +0000|mods|000000|10 (reserved) +0000|mods|000000|11 (reserved) +0000|0000| keycode Key +0000|mods| keycode Key+Left mods + +ACT_RMODS(0001): +0001|0000|000000|00 No action +0001|mods|000000|00 Right mods Momentary +0001|mods|000000|01 Right mods OneShot +0001|mods|000000|10 (reserved) +0001|mods|000000|11 (reserved) +0001|0000| keycode Key +0001|mods| keycode Key+Right mods + +ACT_LMODS_TAP(0010): +0010|mods| keycode Left mods+tap Key + +ACT_RMODS_TAP(0011): +0011|mods| keycode Right mods+tap Key -ACT_USAGE - other HID usage than keyboard - 0 1 0 0| 0 0| usage(10) System usage - 0 1 0 0| 0 1| usage(10) Consumer usage - 0 1 0 0| 1 0| usage(10) (reserved) - 0 1 0 0| 1 1| usage(10) (reserved) - -ACT_MOUSEKEY(0110) - 0 1 0 1| X X X X| keycode(8) Mouse key -??? TODO: refactor - 0 1 0 1| 0 0 X X| accel(5) |cursor(3) Mouse key - 0 1 0 1| 0 1 X X| accel(5) |wheel(3) Mouse key - 0 1 0 1| 1 0 X X| button(8) Mouse key - 0 1 0 1| 1 1 X X| button(8) Mouse key -??? - -Layer Action ------------- + +Other HID Usage +--------------- +This action handles other usages than keyboard. +ACT_USAGE(0100): +0100|00| usage(10) System control(0x80) - General Desktop page(0x01) +0100|01| usage(10) Consumer control(0x01) - Consumer page(0x0C) +0100|10| usage(10) (reserved) +0100|11| usage(10) (reserved) + + +Mouse Keys +---------- +ACT_MOUSEKEY(0110): +0101|XXXX| keycode Mouse key + + +Layer Actions +------------- +ACT_LAYER_PRESSED(1000): Set layer on key pressed +ACT_LAYER_RELEASED(1001): Set layer on key released +ACT_LAYER_BIT(1010): On/Off layer bit +ACT_LAYER_EXT(1011): Extentions + 1000|LLLL|0000 0000 set layer L when pressed 1001|LLLL|0000 0000 set layer L when released 1010|BBBB|0000 0000 on/off bit B when pressed/released @@ -79,16 +81,19 @@ Layer Action 1011|0001| keyocde set default layer when released[tap is ignored/not used] -ACT_MACRO(1100) - 1 1 0 0| option(4) | macro-table id(8) Macro play(Flash) - 1 1 0 0| option(4) | macro-table id(8) Macro play(EEPROM) - 1 1 0 0| 1 1 1 1| macro-table id(8) Macro record +Extensions(11XX) +---------------- +NOTE: NOT FIXED + +ACT_MACRO(1100): +1100|opt | id(8) Macro play +1100|1111| id(8) Macro record -ACT_COMMAND(1110) - 1 1 1 0| option(4) | comamnd id(8) Built-in Command exec +ACT_COMMAND(1110): +1110|opt | id(8) Built-in Command exec -ACT_FUNCTION(1111) - 1 1 1 1| function address(4K range) Function +ACT_FUNCTION(1111): +1111| address(12) Function Macro record(dynamicly) Macro play(dynamicly) TODO: modifier + [tap key /w mod] @@ -98,19 +103,22 @@ TODO: modifier + [tap key /w mod] */ enum action_id { - ACT_LMODS = 0b0000, - ACT_RMODS = 0b0001, - ACT_LMOD_TAP = 0b0010, - ACT_RMOD_TAP = 0b0011, - ACT_USAGE = 0b0100, - ACT_MOUSEKEY = 0b0101, - ACT_LAYER_PRESSED = 0b1000, - ACT_LAYER_RELEASED = 0b1001, - ACT_LAYER_BIT = 0b1010, - ACT_LAYER_EXT = 0b1011, - ACT_MACRO = 0b1100, - ACT_COMMAND = 0b1110, - ACT_FUNCTION = 0b1111 + ACT_LMODS = 0b0000, + ACT_RMODS = 0b0001, + ACT_LMODS_TAP = 0b0010, + ACT_RMODS_TAP = 0b0011, + + ACT_USAGE = 0b0100, + ACT_MOUSEKEY = 0b0101, + + ACT_LAYER_PRESSED = 0b1000, + ACT_LAYER_RELEASED = 0b1001, + ACT_LAYER_BIT = 0b1010, + ACT_LAYER_EXT = 0b1011, + + ACT_MACRO = 0b1100, + ACT_COMMAND = 0b1110, + ACT_FUNCTION = 0b1111 }; // TODO: not portable across compiler/endianness? @@ -169,20 +177,13 @@ typedef struct { void action_exec(keyevent_t event); -/* -void key_action(uint8_t code, keyevent_t event); -void mod_action(uint8_t code, keyevent_t event); -void fn_action(uint8_t code, keyevent_t event); -*/ +// TODO: proper names /* action_t utility */ -/* -#define ACTION_NO { .code = 0 } -#define ACTION(kind, param) { .code = ((kind)<<12 | (param)) } -*/ #define ACTION_NO 0 #define ACTION(kind, param) ((kind)<<12 | (param)) +#define MOD_BITS(mods) (((mods)>>4 | (mods)) & 0x0F) /* Key & Mods */ #define ACTION_KEY(key) ACTION(ACT_LMODS, key) @@ -197,8 +198,8 @@ void fn_action(uint8_t code, keyevent_t event); #define ACTION_RMODS_SWITCH(mods, tap) ACTION(ACT_RMODS, (mods)<<8 | 0xF0 | (tap)) #define ACTION_RMODS_TOGGLE(mods, tap) ACTION(ACT_RMODS, (mods)<<8 | 0xF1 | (tap)) /* Mods + Tap key */ -#define ACTION_LMODS_TAP(mods, key) ACTION(ACT_LMODS_TAP,(mods)<<8 | (key)) -#define ACTION_RMODS_TAP(mods, key) ACTION(ACT_RMODS_TAP,(mods)<<8 | (key)) +#define ACTION_LMODS_TAP(mods, key) ACTION(ACT_LMODS_TAP, MOD_BITS(mods)<<8 | (key)) +#define ACTION_RMODS_TAP(mods, key) ACTION(ACT_RMODS_TAP, MOD_BITS(mods)<<8 | (key)) /* Layer Switch */ #define ACTION_LAYER_SET_ON_PRESSED(layer) ACTION(ACT_LAYER_PRESSED, (layer)<<8 | 0x00) diff --git a/keyboard/hhkb/keymap.c b/keyboard/hhkb/keymap.c index 38461290b..e29b37b16 100644 --- a/keyboard/hhkb/keymap.c +++ b/keyboard/hhkb/keymap.c @@ -51,18 +51,6 @@ along with this program. If not, see . } -/* -static const action_t PROGMEM fn_actions[] = { - ACTION_LAYER_TO_DEFAULT_ON_RELEASED, // Fn0 - ACTION_LAYER_SET_ON_PRESSED(1), // Fn1 - ACTION_LAYER_SET_TAP_KEY(2, KC_SLASH), // Fn2 - ACTION_LAYER_SET_TAP_KEY(3, KC_SCLN), // Fn3 - ACTION_LAYER_SET_ON_PRESSED(3), // Fn4 - ACTION_LAYER_SET_TAP_KEY(5, KC_SPC), // Fn5 - ACTION_NO, // Fn6 - ACTION_NO, // Fn7 -}; -*/ static const uint16_t PROGMEM fn_actions[] = { ACTION_LAYER_TO_DEFAULT_ON_RELEASED, // Fn0 ACTION_LAYER_SET_ON_PRESSED(1), // Fn1 @@ -70,8 +58,8 @@ static const uint16_t PROGMEM fn_actions[] = { ACTION_LAYER_SET_TAP_KEY(3, KC_SCLN), // Fn3 ACTION_LAYER_SET_ON_PRESSED(3), // Fn4 ACTION_LAYER_SET_TAP_KEY(5, KC_SPC), // Fn5 - ACTION_NO, // Fn6 - ACTION_NO, // Fn7 + ACTION_LMODS_TAP(MOD_BIT(KC_LCTL), KC_BSPC), // Fn6 + ACTION_RMODS_TAP(MOD_BIT(KC_RCTL), KC_ENT), // Fn7 }; @@ -91,7 +79,7 @@ static const uint8_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = { */ KEYMAP(ESC, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, MINS,EQL, BSLS,GRV, \ TAB, Q, W, E, R, T, Y, U, I, O, P, LBRC,RBRC,BSPC, \ - LCTL,A, S, D, F, G, H, J, K, L, FN3, QUOT,ENT, \ + FN6, A, S, D, F, G, H, J, K, L, FN3, QUOT,FN7, \ LSFT,Z, X, C, V, B, N, M, COMM,DOT, FN2, RSFT,FN1, \ LGUI,LALT, FN5, RALT,FN4), -- cgit v1.2.3-70-g09d2 From 567b2ae5259634a5293afbc6a710a19d7c45dcda Mon Sep 17 00:00:00 2001 From: tmk Date: Thu, 17 Jan 2013 11:46:38 +0900 Subject: Fix mods with tap key but some problems still exists. - stuck tap key after tapping. seq[mods(d), key(d), mods(u), key(u)] - tap key doesn't work when fast type. [key(d), mods(d), key(u), mods(u)] --- common/action.c | 168 ++++++++++++++++++++++++++++++++---------------------- common/keyboard.h | 1 + 2 files changed, 102 insertions(+), 67 deletions(-) (limited to 'common/action.c') diff --git a/common/action.c b/common/action.c index 7299a874a..d5040479e 100644 --- a/common/action.c +++ b/common/action.c @@ -10,7 +10,7 @@ #include "action.h" -static void process(keyevent_t event, action_t action); +static void process(keyevent_t event); static void register_code(uint8_t code); static void unregister_code(uint8_t code); static void add_mods(uint8_t mods); @@ -30,19 +30,26 @@ static uint8_t tap_count = 0; /* layer */ uint8_t default_layer = 0; uint8_t current_layer = 0; -keyrecord_t delaying_layer = {}; +static keyrecord_t tapping_key = {}; +// time 0 means no event. +#define IS_TAPPING (tapping_key.event.time != 0) +/* TODO: +#define IS_TAPPING_KEY(key) (tapping_key.event.time != 0 && KEYEQ(tapping_key.event.key, key)) + */ /* waiting keys buffer */ #define WAITING_KEYS_BUFFER 3 static keyrecord_t waiting_keys[WAITING_KEYS_BUFFER] = {}; +// TODO: double buffer? +static keyrecord_t waiting_keys0[WAITING_KEYS_BUFFER] = {}; +static keyrecord_t waiting_keys1[WAITING_KEYS_BUFFER] = {}; static uint8_t waiting_keys_head = 0; -static bool waiting_keys_enqueue(keyevent_t event, action_t action) +static bool waiting_keys_enqueue(keyevent_t event) { debug("waiting_keys["); debug_dec(waiting_keys_head); debug("] = "); - debug_hex16(action.code); debug("\n"); + debug_hex8(event.key.row); debug_hex8(event.key.col); debug("\n"); // TODO event.key.raw if (waiting_keys_head < WAITING_KEYS_BUFFER) { waiting_keys[waiting_keys_head++] = (keyrecord_t){ .event = event, - .action = action, .mods = host_get_mods() }; } else { return true; @@ -62,80 +69,64 @@ static bool waiting_keys_has(keypos_t key) static void waiting_keys_process_in_current_layer(void) { // TODO: in case of including layer key in waiting keys - uint8_t tmp_mods = host_get_mods(); for (uint8_t i = 0; i < waiting_keys_head; i++) { - /* revive status of mods */ - host_set_mods(waiting_keys[i].mods); - process(waiting_keys[i].event, keymap_get_action(current_layer, - waiting_keys[i].event.key.row, - waiting_keys[i].event.key.col)); debug("waiting_keys_process_in_current_layer["); debug_dec(i); debug("]\n"); + // TODO: no need action in waiting_keys? should get_action() in process()? + process(waiting_keys[i].event); } - host_set_mods(tmp_mods); waiting_keys_clear(); } void action_exec(keyevent_t event) { - /* When delaying layer switch */ - if (delaying_layer.action.code) { - /* Layer switch when tap time elapses or waiting key is released */ - if ((timer_elapsed(delaying_layer.event.time) > TAP_TIME) || + if (IS_TAPPING) { + /* when tap time elapses or waiting key is released */ + if ((timer_elapsed(tapping_key.event.time) > TAP_TIME) || (!event.pressed && waiting_keys_has(event.key))) { - /* layer switch */ - switch (delaying_layer.action.kind.id) { - case ACT_LAYER_PRESSED: - layer_switch(delaying_layer.action.layer.opt); - break; - case ACT_LAYER_BIT: - layer_switch(current_layer | delaying_layer.action.layer.opt); - break; - } - delaying_layer = (keyrecord_t){}; + + // TODO process tapping_key: layer swich, modifier, ... + // action is needed? + debug("notap: process tapping_key.\n"); + process(tapping_key.event); /* Process waiting keys in new layer */ waiting_keys_process_in_current_layer(); } - /* when delaying layer key is released within delay term */ - else if (!event.pressed && KEYEQ(event.key, delaying_layer.event.key)) { + /* when tapping key is released within tap time */ + else if (!event.pressed && KEYEQ(event.key, tapping_key.event.key)) { /* tap key down */ - debug("tap[delaying_layer](register): fist\n"); - uint8_t tmp_mods = host_get_mods(); - host_set_mods(delaying_layer.mods); - register_code(delaying_layer.action.layer.code); - delaying_layer = (keyrecord_t){}; - host_set_mods(tmp_mods); + debug("tap("); debug_hex8(tap_count); debug(")[tapping_key](register): "); debug_hex8(tapping_key.action.layer.code); debug("\n"); + register_code(tapping_key.action.layer.code); + tapping_key = (keyrecord_t){}; /* process waiting keys */ waiting_keys_process_in_current_layer(); } } - // not real event. event just to update delaying layer. + // not real event. event just to handle time out of tapping key. if (IS_NOEVENT(event)) { return; } - /* count tap when key is up */ + /* count up tap when key is up */ + // key: d u d u d + // tap: 0 1 1 2 2 + // key: u d u d u + // tap: 0 0 1 1 2 if (KEYEQ(event.key, last_event.key) && timer_elapsed(last_event.time) <= TAP_TIME) { if (!event.pressed) tap_count++; } else { tap_count = 0; } - action_t action = keymap_get_action(current_layer, event.key.row, event.key.col); - - // TODO: all key events(pressed, released) should be recorded? - /* postpone key-down events while delaying layer */ - if (delaying_layer.action.code) { - if (event.pressed) { - waiting_keys_enqueue(event, action); - } else { - process(event, action); - } + /* store key events while tapping */ + if (IS_TAPPING) { + // TODO: action is needed? + waiting_keys_enqueue(event); } else { - process(event, action); + process(event); } /* last event */ @@ -143,9 +134,9 @@ void action_exec(keyevent_t event) } -static void process(keyevent_t event, action_t action) +static void process(keyevent_t event) { - //action_t action = keymap_get_action(current_layer, event.key.row, event.key.col); + action_t action = keymap_get_action(current_layer, event.key.row, event.key.col); debug("action: "); debug_hex16(action.code); if (event.pressed) debug("[down]\n"); else debug("[up]\n"); @@ -204,21 +195,39 @@ static void process(keyevent_t event, action_t action) case ACT_LMODS_TAP: if (event.pressed) { if (tap_count == 0) { - add_mods(action.key.mods); + if (host_has_anykey()) { + // This key is a modifier essentially. + // Prioritize mods when key jam or rollover + add_mods(action.key.mods); + } else { + if (IS_TAPPING && KEYEQ(tapping_key.event.key, event.key)) { + // no tapping + add_mods(action.key.mods); + tapping_key = (keyrecord_t){}; + } else { + debug("tapping lmods("); debug_hex8(action.key.mods); debug(")\n"); + tapping_key = (keyrecord_t){ + .event = event, + .action = action, + .mods = host_get_mods() + }; + } + } } else { - debug("tap[lmods](register): "); debug_hex(tap_count); debug("\n"); + // pressed after tapping + debug("tap("); debug_hex(tap_count); debug(")[lmods](register): "); debug_hex8(action.key.code); debug("\n"); register_code(action.key.code); } } else { if (tap_count == 0) { + debug("tap(00)[lmods](del_mods): "); debug_hex8(action.key.mods); debug("\n"); del_mods(action.key.mods); } else if (tap_count == 1) { - debug("tap[lmods](register/unregister): "); debug_hex(tap_count); debug("\n"); + debug("tap(01)[lmods](del_mods/unregister): "); debug_hex8(action.key.mods); debug(" "); debug_hex8(action.key.code); debug("\n"); del_mods(action.key.mods); - register_code(action.key.code); unregister_code(action.key.code); } else { - debug("tap[lmods](unregister): "); debug_hex(tap_count); debug("\n"); + debug("tap("); debug_hex(tap_count); debug(")[lmods](unregister): "); debug_hex8(action.key.code); debug("\n"); unregister_code(action.key.code); } } @@ -226,21 +235,39 @@ static void process(keyevent_t event, action_t action) case ACT_RMODS_TAP: if (event.pressed) { if (tap_count == 0) { - add_mods(action.key.mods<<4); + if (host_has_anykey()) { + // This key is a modifier essentially. + // Prioritize mods when key jam or rollover + add_mods(action.key.mods<<4); + } else { + if (IS_TAPPING && KEYEQ(tapping_key.event.key, event.key)) { + // no tapping + add_mods(action.key.mods<<4); + tapping_key = (keyrecord_t){}; + } else { + debug("tapping rmods("); debug_hex8(action.key.mods); debug(")\n"); + tapping_key = (keyrecord_t){ + .event = event, + .action = action, + .mods = host_get_mods() + }; + } + } } else { - debug("tap[rmods](register): "); debug_hex(tap_count); debug("\n"); + // pressed after tapping + debug("tap("); debug_hex(tap_count); debug(")[rmods](register): "); debug_hex8(action.key.code); debug("\n"); register_code(action.key.code); } } else { if (tap_count == 0) { + debug("tap(00)[rmods](del_mods): "); debug_hex8(action.key.mods); debug("\n"); del_mods(action.key.mods<<4); } else if (tap_count == 1) { - debug("tap[rmods](register/unregister): "); debug_hex(tap_count); debug("\n"); + debug("tap(01)[rmods](del_mods/unregister): "); debug_hex8(action.key.mods); debug(" "); debug_hex8(action.key.code); debug("\n"); del_mods(action.key.mods<<4); - register_code(action.key.code); unregister_code(action.key.code); } else { - debug("tap[rmods](unregister): "); debug_hex(tap_count); debug("\n"); + debug("tap("); debug_hex(tap_count); debug(")[rmods](unregister): "); debug_hex8(action.key.code); debug("\n"); unregister_code(action.key.code); } } @@ -303,16 +330,22 @@ static void process(keyevent_t event, action_t action) // with tap key if (event.pressed) { if (tap_count == 0) { - // not tapping yet if (host_has_anykey()) { + // This key is a normal key than a leyar key essentially. + // Prioritize 'tap key' when key jam or rollover register_code(action.layer.code); } else { - debug("Delay switching layer("); debug_hex8(action.layer.opt); debug(")\n"); - delaying_layer = (keyrecord_t){ - .event = event, - .action = action, - .mods = host_get_mods() - }; + if (IS_TAPPING && KEYEQ(tapping_key.event.key, event.key)) { + layer_switch(action.layer.opt); + tapping_key = (keyrecord_t){}; + } else { + debug("tapping layer("); debug_hex8(action.layer.opt); debug(")\n"); + tapping_key = (keyrecord_t){ + .event = event, + .action = action, + .mods = host_get_mods() + }; + } } } else if (tap_count > 0) { // pressed after tapping @@ -371,13 +404,14 @@ static void process(keyevent_t event, action_t action) } break; default: + // TODO: see ACT_LAYER_PRESSED code // with tap key if (event.pressed) { if (tap_count == 0) { if (host_has_anykey()) { register_code(action.layer.code); } else { - delaying_layer = (keyrecord_t){ + tapping_key = (keyrecord_t){ .event = event, .action = action, .mods = keyboard_report->mods diff --git a/common/keyboard.h b/common/keyboard.h index cf85b1233..907ee1f97 100644 --- a/common/keyboard.h +++ b/common/keyboard.h @@ -26,6 +26,7 @@ along with this program. If not, see . extern "C" { #endif +// TODO: union {raw = row:col} typedef struct { uint8_t row; uint8_t col; -- cgit v1.2.3-70-g09d2 From ee7ce433357a1c1bbcaba54525fc5b5b5404aa82 Mon Sep 17 00:00:00 2001 From: tmk Date: Thu, 17 Jan 2013 15:00:41 +0900 Subject: Refactor struct keyevent_t. --- common/action.c | 13 +++++++++---- common/keyboard.c | 4 ++-- common/keyboard.h | 12 ++++++++---- 3 files changed, 19 insertions(+), 10 deletions(-) (limited to 'common/action.c') diff --git a/common/action.c b/common/action.c index d5040479e..d01038301 100644 --- a/common/action.c +++ b/common/action.c @@ -47,7 +47,7 @@ static uint8_t waiting_keys_head = 0; static bool waiting_keys_enqueue(keyevent_t event) { debug("waiting_keys["); debug_dec(waiting_keys_head); debug("] = "); - debug_hex8(event.key.row); debug_hex8(event.key.col); debug("\n"); // TODO event.key.raw + debug_hex16(event.key.raw); debug("\n"); if (waiting_keys_head < WAITING_KEYS_BUFFER) { waiting_keys[waiting_keys_head++] = (keyrecord_t){ .event = event, .mods = host_get_mods() }; @@ -59,7 +59,7 @@ static void waiting_keys_clear(void) { waiting_keys_head = 0; } -static bool waiting_keys_has(keypos_t key) +static bool waiting_keys_has(key_t key) { for (uint8_t i = 0; i < waiting_keys_head; i++) { if KEYEQ(key, waiting_keys[i].event.key) return true; @@ -71,7 +71,6 @@ static void waiting_keys_process_in_current_layer(void) // TODO: in case of including layer key in waiting keys for (uint8_t i = 0; i < waiting_keys_head; i++) { debug("waiting_keys_process_in_current_layer["); debug_dec(i); debug("]\n"); - // TODO: no need action in waiting_keys? should get_action() in process()? process(waiting_keys[i].event); } waiting_keys_clear(); @@ -80,6 +79,12 @@ static void waiting_keys_process_in_current_layer(void) void action_exec(keyevent_t event) { + if (!IS_NOEVENT(event)) { + debug("event: "); debug_hex16(event.key.raw); + debug("["); + if (event.pressed) debug("down"); else debug("up"); + debug("]\n"); + } if (IS_TAPPING) { /* when tap time elapses or waiting key is released */ if ((timer_elapsed(tapping_key.event.time) > TAP_TIME) || @@ -136,7 +141,7 @@ void action_exec(keyevent_t event) static void process(keyevent_t event) { - action_t action = keymap_get_action(current_layer, event.key.row, event.key.col); + action_t action = keymap_get_action(current_layer, event.key.pos.row, event.key.pos.col); debug("action: "); debug_hex16(action.code); if (event.pressed) debug("[down]\n"); else debug("[up]\n"); diff --git a/common/keyboard.c b/common/keyboard.c index 2e32e91e0..ea4d0ee7e 100644 --- a/common/keyboard.c +++ b/common/keyboard.c @@ -66,7 +66,7 @@ void keyboard_task(void) for (int c = 0; c < MATRIX_COLS; c++) { if (matrix_change & (1<. extern "C" { #endif -// TODO: union {raw = row:col} typedef struct { - uint8_t row; uint8_t col; + uint8_t row; } keypos_t; +typedef union { + uint16_t raw; + keypos_t pos; +} key_t; + typedef struct { - keypos_t key; + key_t key; bool pressed; uint16_t time; } keyevent_t; -#define KEYEQ(keya, keyb) (keya.row == keyb.row && keya.col == keyb.col) +#define KEYEQ(keya, keyb) (keya.raw == keyb.raw) #define IS_NOEVENT(event) (event.time == 0) #define NOEVENT (keyevent_t) { \ .key = (keypos_t){ .row = 255, .col = 255 }, \ -- cgit v1.2.3-70-g09d2 From f71a5217b762225eec294b02f9403f29a25ceb6a Mon Sep 17 00:00:00 2001 From: tmk Date: Sun, 20 Jan 2013 15:03:07 +0900 Subject: Fix mods with tapping. --- common/action.c | 380 +++++++++++++++++++++++++++--------------------------- common/keyboard.h | 2 +- 2 files changed, 190 insertions(+), 192 deletions(-) (limited to 'common/action.c') diff --git a/common/action.c b/common/action.c index d01038301..3a504a45f 100644 --- a/common/action.c +++ b/common/action.c @@ -23,57 +23,83 @@ static void layer_switch(uint8_t new_layer); /* tap */ -#define TAP_TIME 200 -static keyevent_t last_event = {}; +#define TAP_TIME 300 +/* This counts up when tap occurs */ static uint8_t tap_count = 0; +static bool is_tap_key(keyevent_t event) +{ + action_t action = keymap_get_action(current_layer, event.key.pos.row, event.key.pos.col); + switch (action.kind.id) { + case ACT_LMODS_TAP: + case ACT_RMODS_TAP: + return true; + case ACT_LAYER_PRESSED: + case ACT_LAYER_BIT: + switch (action.layer.code) { + case 0x00: + case 0xF1 ... 0xFF: + return false; + case 0xF0: + default: + return true; + } + return false; + } + return false; +} /* layer */ uint8_t default_layer = 0; uint8_t current_layer = 0; -static keyrecord_t tapping_key = {}; -// time 0 means no event. -#define IS_TAPPING (tapping_key.event.time != 0) -/* TODO: -#define IS_TAPPING_KEY(key) (tapping_key.event.time != 0 && KEYEQ(tapping_key.event.key, key)) - */ +static keyevent_t tapping_event = {}; + +/* TAPPING: This indicates that whether tap or not is not decided yet. */ +// NOTE: keyevent_t.time 0 means no event. +#define IS_TAPPING(k) (tapping_event.time != 0 && KEYEQ(tapping_event.key, (k))) /* waiting keys buffer */ -#define WAITING_KEYS_BUFFER 3 -static keyrecord_t waiting_keys[WAITING_KEYS_BUFFER] = {}; -// TODO: double buffer? -static keyrecord_t waiting_keys0[WAITING_KEYS_BUFFER] = {}; -static keyrecord_t waiting_keys1[WAITING_KEYS_BUFFER] = {}; -static uint8_t waiting_keys_head = 0; -static bool waiting_keys_enqueue(keyevent_t event) +#define WAITING_KEYS_BUFFER 8 +static keyevent_t waiting_events[WAITING_KEYS_BUFFER] = {}; +static uint8_t waiting_events_head = 0; +static bool waiting_events_enqueue(keyevent_t event) { - debug("waiting_keys["); debug_dec(waiting_keys_head); debug("] = "); - debug_hex16(event.key.raw); debug("\n"); - if (waiting_keys_head < WAITING_KEYS_BUFFER) { - waiting_keys[waiting_keys_head++] = (keyrecord_t){ .event = event, - .mods = host_get_mods() }; - } else { + if (IS_NOEVENT(event)) { return true; } + + if (waiting_events_head < WAITING_KEYS_BUFFER) { + debug("waiting_events["); debug_dec(waiting_events_head); debug("] = "); + debug_hex16(event.key.raw); debug("\n"); + waiting_events[waiting_events_head++] = event; return true; } + debug("waiting_events_enqueue: Over flow.\n"); + return false; } -static void waiting_keys_clear(void) +static void waiting_events_clear(void) { - waiting_keys_head = 0; + waiting_events_head = 0; } -static bool waiting_keys_has(key_t key) +static bool waiting_events_has(key_t key) { - for (uint8_t i = 0; i < waiting_keys_head; i++) { - if KEYEQ(key, waiting_keys[i].event.key) return true; + for (uint8_t i = 0; i < waiting_events_head; i++) { + if KEYEQ(key, waiting_events[i].key) return true; } return false; } -static void waiting_keys_process_in_current_layer(void) +static void waiting_events_process_in_current_layer(void) +{ + // TODO: in case of including tap key in waiting keys + for (uint8_t i = 0; i < waiting_events_head; i++) { + debug("waiting_events_process_in_current_layer["); debug_dec(i); debug("]\n"); + process(waiting_events[i]); + } + waiting_events_clear(); +} +static bool waiting_events_has_anykey_pressed(void) { - // TODO: in case of including layer key in waiting keys - for (uint8_t i = 0; i < waiting_keys_head; i++) { - debug("waiting_keys_process_in_current_layer["); debug_dec(i); debug("]\n"); - process(waiting_keys[i].event); + for (uint8_t i = 0; i < waiting_events_head; i++) { + if (waiting_events[i].pressed) return true; } - waiting_keys_clear(); + return false; } @@ -85,62 +111,106 @@ void action_exec(keyevent_t event) if (event.pressed) debug("down"); else debug("up"); debug("]\n"); } - if (IS_TAPPING) { - /* when tap time elapses or waiting key is released */ - if ((timer_elapsed(tapping_key.event.time) > TAP_TIME) || - (!event.pressed && waiting_keys_has(event.key))) { - - // TODO process tapping_key: layer swich, modifier, ... - // action is needed? - debug("notap: process tapping_key.\n"); - process(tapping_key.event); - - /* Process waiting keys in new layer */ - waiting_keys_process_in_current_layer(); - } - /* when tapping key is released within tap time */ - else if (!event.pressed && KEYEQ(event.key, tapping_key.event.key)) { - /* tap key down */ - debug("tap("); debug_hex8(tap_count); debug(")[tapping_key](register): "); debug_hex8(tapping_key.action.layer.code); debug("\n"); - register_code(tapping_key.action.layer.code); - tapping_key = (keyrecord_t){}; - - /* process waiting keys */ - waiting_keys_process_in_current_layer(); - } - } - // not real event. event just to handle time out of tapping key. - if (IS_NOEVENT(event)) { - return; - } + // In tapping term + if (tapping_event.time && timer_elapsed(tapping_event.time) < TAP_TIME) { + if (tapping_event.pressed) { + if (!event.pressed && KEYEQ(tapping_event.key, event.key)) { + debug("Tapping: Release tap key.\n"); + if (tap_count == 0) { + debug("Tapping: First tap.\n"); + // count up on release + tap_count++; - /* count up tap when key is up */ - // key: d u d u d - // tap: 0 1 1 2 2 - // key: u d u d u - // tap: 0 0 1 1 2 - if (KEYEQ(event.key, last_event.key) && timer_elapsed(last_event.time) <= TAP_TIME) { - if (!event.pressed) tap_count++; - } else { - tap_count = 0; - } + process(tapping_event); + waiting_events_process_in_current_layer(); + } + tapping_event = event; + process(event); + } else if (!event.pressed && waiting_events_has(event.key)) { + debug("Tapping: End(No tap by typing waiting key).\n"); - /* store key events while tapping */ - if (IS_TAPPING) { - // TODO: action is needed? - waiting_keys_enqueue(event); - } else { - process(event); + process(tapping_event); + waiting_events_process_in_current_layer(); + process(event); + + tap_count = 0; + tapping_event = (keyevent_t){}; + } else { + //debug("Tapping: pressing tap key.\n"); + if (tap_count == 0) { + // store event + waiting_events_enqueue(event); + return; + } + process(event); + } + } else { + //debug("Tapping after releasing tap.\n"); + // Sequential tap + if (tap_count && event.pressed && KEYEQ(tapping_event.key, event.key)) { + tap_count++; + tapping_event = event; + debug("Tapping: Sequential tap("); debug_hex(tap_count); debug(")\n"); + } + process(event); + } } + // Not in tapping term + else { + if (tapping_event.time) { + if (tapping_event.pressed) { + if (tap_count == 0) { + // Not tap, holding down normal key. + debug("Not tap.\n"); + process(tapping_event); + waiting_events_process_in_current_layer(); + + tap_count = 0; + tapping_event = (keyevent_t){}; + process(event); + } else { + // Holding down last tap key. + //debug("Time out with holding last tap.\n"); + process(event); + if (!event.pressed && KEYEQ(tapping_event.key, event.key)) { + debug("Tapping: End(Release holding last tap).\n"); + // clear after release last tap key + tap_count = 0; + tapping_event = (keyevent_t){}; + waiting_events_clear(); + } + } + } else { + // time out for sequential tap after complete last tap + debug("Tapping: End(Time out after releasing last tap).\n"); + tap_count = 0; + tapping_event = (keyevent_t){}; + waiting_events_clear(); + + process(event); + } + } else { + // Normal state without tapping + if (event.pressed && is_tap_key(event)) { + debug("Tapping: Start(Press tap key).\n"); + tapping_event = event; + tap_count = 0; + waiting_events_clear(); + } else { + //debug("Normal event(No tapping)\n"); + process(event); + } + } - /* last event */ - last_event = event; + } } static void process(keyevent_t event) { + if (IS_NOEVENT(event)) { return; } + action_t action = keymap_get_action(current_layer, event.key.pos.row, event.key.pos.col); debug("action: "); debug_hex16(action.code); if (event.pressed) debug("[down]\n"); else debug("[up]\n"); @@ -198,82 +268,32 @@ static void process(keyevent_t event) } break; case ACT_LMODS_TAP: - if (event.pressed) { - if (tap_count == 0) { - if (host_has_anykey()) { - // This key is a modifier essentially. - // Prioritize mods when key jam or rollover - add_mods(action.key.mods); - } else { - if (IS_TAPPING && KEYEQ(tapping_key.event.key, event.key)) { - // no tapping - add_mods(action.key.mods); - tapping_key = (keyrecord_t){}; + case ACT_RMODS_TAP: + { + uint8_t tmp_mods = (action.kind.id == ACT_LMODS_TAP) ? action.key.mods : + action.key.mods<<4; + if (event.pressed) { + if (IS_TAPPING(event.key) && tap_count > 0) { + if (waiting_events_has_anykey_pressed()) { + debug("MODS_TAP: Tap: Cancel: add_mods\n"); + tap_count = 0; + add_mods(tmp_mods); } else { - debug("tapping lmods("); debug_hex8(action.key.mods); debug(")\n"); - tapping_key = (keyrecord_t){ - .event = event, - .action = action, - .mods = host_get_mods() - }; + debug("MODS_TAP: Tap: register_code\n"); + register_code(action.key.code); } + } else { + debug("MODS_TAP: No tap: add_mods\n"); + add_mods(tmp_mods); } } else { - // pressed after tapping - debug("tap("); debug_hex(tap_count); debug(")[lmods](register): "); debug_hex8(action.key.code); debug("\n"); - register_code(action.key.code); - } - } else { - if (tap_count == 0) { - debug("tap(00)[lmods](del_mods): "); debug_hex8(action.key.mods); debug("\n"); - del_mods(action.key.mods); - } else if (tap_count == 1) { - debug("tap(01)[lmods](del_mods/unregister): "); debug_hex8(action.key.mods); debug(" "); debug_hex8(action.key.code); debug("\n"); - del_mods(action.key.mods); - unregister_code(action.key.code); - } else { - debug("tap("); debug_hex(tap_count); debug(")[lmods](unregister): "); debug_hex8(action.key.code); debug("\n"); - unregister_code(action.key.code); - } - } - break; - case ACT_RMODS_TAP: - if (event.pressed) { - if (tap_count == 0) { - if (host_has_anykey()) { - // This key is a modifier essentially. - // Prioritize mods when key jam or rollover - add_mods(action.key.mods<<4); + if (IS_TAPPING(event.key) && tap_count > 0) { + debug("MODS_TAP: Tap: unregister_code\n"); + unregister_code(action.key.code); } else { - if (IS_TAPPING && KEYEQ(tapping_key.event.key, event.key)) { - // no tapping - add_mods(action.key.mods<<4); - tapping_key = (keyrecord_t){}; - } else { - debug("tapping rmods("); debug_hex8(action.key.mods); debug(")\n"); - tapping_key = (keyrecord_t){ - .event = event, - .action = action, - .mods = host_get_mods() - }; - } + debug("MODS_TAP: No tap: add_mods\n"); + del_mods(tmp_mods); } - } else { - // pressed after tapping - debug("tap("); debug_hex(tap_count); debug(")[rmods](register): "); debug_hex8(action.key.code); debug("\n"); - register_code(action.key.code); - } - } else { - if (tap_count == 0) { - debug("tap(00)[rmods](del_mods): "); debug_hex8(action.key.mods); debug("\n"); - del_mods(action.key.mods<<4); - } else if (tap_count == 1) { - debug("tap(01)[rmods](del_mods/unregister): "); debug_hex8(action.key.mods); debug(" "); debug_hex8(action.key.code); debug("\n"); - del_mods(action.key.mods<<4); - unregister_code(action.key.code); - } else { - debug("tap("); debug_hex(tap_count); debug(")[rmods](unregister): "); debug_hex8(action.key.code); debug("\n"); - unregister_code(action.key.code); } } break; @@ -334,33 +354,20 @@ static void process(keyevent_t event) default: // with tap key if (event.pressed) { - if (tap_count == 0) { - if (host_has_anykey()) { - // This key is a normal key than a leyar key essentially. - // Prioritize 'tap key' when key jam or rollover - register_code(action.layer.code); - } else { - if (IS_TAPPING && KEYEQ(tapping_key.event.key, event.key)) { - layer_switch(action.layer.opt); - tapping_key = (keyrecord_t){}; - } else { - debug("tapping layer("); debug_hex8(action.layer.opt); debug(")\n"); - tapping_key = (keyrecord_t){ - .event = event, - .action = action, - .mods = host_get_mods() - }; - } - } - } else if (tap_count > 0) { - // pressed after tapping - debug("tap[layer](register): "); debug_hex(tap_count); debug("\n"); + if (IS_TAPPING(event.key) && tap_count > 0) { + debug("LAYER_PRESSED: Tap: register_code\n"); register_code(action.layer.code); + } else { + debug("LAYER_PRESSED: No tap: layer_switch\n"); + layer_switch(action.layer.opt); } } else { - // released after tapping - debug("tap[layer](unregister): "); debug_hex(tap_count); debug("\n"); - unregister_code(action.layer.code); + if (IS_TAPPING(event.key) && tap_count > 0) { + debug("LAYER_PRESSED: Tap: unregister_code\n"); + unregister_code(action.layer.code); + } else { + debug("LAYER_PRESSED: No tap: NO ACTION\n"); + } } break; } @@ -409,32 +416,23 @@ static void process(keyevent_t event) } break; default: - // TODO: see ACT_LAYER_PRESSED code // with tap key if (event.pressed) { - if (tap_count == 0) { - if (host_has_anykey()) { - register_code(action.layer.code); - } else { - tapping_key = (keyrecord_t){ - .event = event, - .action = action, - .mods = keyboard_report->mods - }; - } - } else if (tap_count > 0) { - debug("tap[layer_bit](register): "); debug_hex(tap_count); debug("\n"); + if (IS_TAPPING(event.key) && tap_count > 0) { + debug("LAYER_BIT: Tap: register_code\n"); register_code(action.layer.code); + } else { + debug("LAYER_BIT: No tap: layer_switch(bit on)\n"); + layer_switch(current_layer | action.layer.opt); } } else { - if (tap_count == 0) { - // no tap + if (IS_TAPPING(event.key) && tap_count > 0) { + debug("LAYER_BIT: Tap: unregister_code\n"); + unregister_code(action.layer.code); + } else { + debug("LAYER_BIT: No tap: layer_switch(bit off)\n"); layer_switch(current_layer & ~action.layer.opt); - } else if (tap_count == 1) { - // tap - register_code(action.layer.code); } - unregister_code(action.layer.code); } break; } diff --git a/common/keyboard.h b/common/keyboard.h index 4518cdddc..4a3ee85a8 100644 --- a/common/keyboard.h +++ b/common/keyboard.h @@ -37,7 +37,7 @@ typedef union { } key_t; typedef struct { - key_t key; + key_t key; bool pressed; uint16_t time; } keyevent_t; -- cgit v1.2.3-70-g09d2 From 28b5f69ce5c8b35d40725b490e7a2d4bfe922ad4 Mon Sep 17 00:00:00 2001 From: tmk Date: Wed, 23 Jan 2013 23:53:51 +0900 Subject: Add prototype of Action Function. --- common/action.c | 190 +++++++++++++++++++++++++++++++++---------------- common/action.h | 38 +++++++++- common/keyboard.c | 3 +- common/keyboard.h | 4 +- keyboard/hhkb/keymap.c | 20 +++++- 5 files changed, 185 insertions(+), 70 deletions(-) (limited to 'common/action.c') diff --git a/common/action.c b/common/action.c index 3a504a45f..88f8186c3 100644 --- a/common/action.c +++ b/common/action.c @@ -11,47 +11,26 @@ static void process(keyevent_t event); -static void register_code(uint8_t code); -static void unregister_code(uint8_t code); -static void add_mods(uint8_t mods); -static void del_mods(uint8_t mods); -static void set_mods(uint8_t mods); -static void clear_keyboard(void); -static void clear_keyboard_but_mods(void); -static bool sending_anykey(void); -static void layer_switch(uint8_t new_layer); - - -/* tap */ -#define TAP_TIME 300 -/* This counts up when tap occurs */ -static uint8_t tap_count = 0; -static bool is_tap_key(keyevent_t event) + +void test_func(keyevent_t event, uint8_t opt) { - action_t action = keymap_get_action(current_layer, event.key.pos.row, event.key.pos.col); - switch (action.kind.id) { - case ACT_LMODS_TAP: - case ACT_RMODS_TAP: - return true; - case ACT_LAYER_PRESSED: - case ACT_LAYER_BIT: - switch (action.layer.code) { - case 0x00: - case 0xF1 ... 0xFF: - return false; - case 0xF0: - default: - return true; - } - return false; + if (event.pressed) { + debug("test_func:pressed: "); debug_hex(opt); debug("\n"); + } else { + debug("test_func:released: "); debug_hex(opt); debug("\n"); } - return false; } /* layer */ uint8_t default_layer = 0; uint8_t current_layer = 0; -static keyevent_t tapping_event = {}; + +/* tap term(ms) */ +#define TAP_TIME 200 + +/* This counts up when tap occurs */ +uint8_t tap_count = 0; +keyevent_t tapping_event = {}; /* TAPPING: This indicates that whether tap or not is not decided yet. */ // NOTE: keyevent_t.time 0 means no event. @@ -61,26 +40,41 @@ static keyevent_t tapping_event = {}; #define WAITING_KEYS_BUFFER 8 static keyevent_t waiting_events[WAITING_KEYS_BUFFER] = {}; static uint8_t waiting_events_head = 0; +static uint8_t waiting_events_tail = 0; + static bool waiting_events_enqueue(keyevent_t event) { if (IS_NOEVENT(event)) { return true; } - if (waiting_events_head < WAITING_KEYS_BUFFER) { - debug("waiting_events["); debug_dec(waiting_events_head); debug("] = "); - debug_hex16(event.key.raw); debug("\n"); - waiting_events[waiting_events_head++] = event; - return true; + if ((waiting_events_head + 1) % WAITING_KEYS_BUFFER == waiting_events_tail) { + debug("waiting_events_enqueue: Over flow.\n"); + return false; } - debug("waiting_events_enqueue: Over flow.\n"); - return false; + + debug("waiting_events["); debug_dec(waiting_events_head); debug("] = "); + debug_hex16(event.key.raw); debug("\n"); + + waiting_events[waiting_events_head] = event; + waiting_events_head = (waiting_events_head + 1)% WAITING_KEYS_BUFFER; + return true; +} +static keyevent_t waiting_events_dequeue(void) +{ + if (waiting_events_head == waiting_events_tail) { + return (keyevent_t){}; + } + uint8_t tail = waiting_events_tail; + waiting_events_tail = waiting_events_tail + 1 % WAITING_KEYS_BUFFER; + return waiting_events[tail]; } static void waiting_events_clear(void) { waiting_events_head = 0; + waiting_events_tail = 0; } static bool waiting_events_has(key_t key) { - for (uint8_t i = 0; i < waiting_events_head; i++) { + for (uint8_t i = waiting_events_tail; i != waiting_events_head; i = (i + 1) % WAITING_KEYS_BUFFER) { if KEYEQ(key, waiting_events[i].key) return true; } return false; @@ -88,7 +82,7 @@ static bool waiting_events_has(key_t key) static void waiting_events_process_in_current_layer(void) { // TODO: in case of including tap key in waiting keys - for (uint8_t i = 0; i < waiting_events_head; i++) { + for (uint8_t i = waiting_events_tail; i != waiting_events_head; i = (i + 1) % WAITING_KEYS_BUFFER) { debug("waiting_events_process_in_current_layer["); debug_dec(i); debug("]\n"); process(waiting_events[i]); } @@ -96,7 +90,7 @@ static void waiting_events_process_in_current_layer(void) } static bool waiting_events_has_anykey_pressed(void) { - for (uint8_t i = 0; i < waiting_events_head; i++) { + for (uint8_t i = waiting_events_tail; i != waiting_events_head; i = (i + 1) % WAITING_KEYS_BUFFER) { if (waiting_events[i].pressed) return true; } return false; @@ -137,7 +131,7 @@ void action_exec(keyevent_t event) tap_count = 0; tapping_event = (keyevent_t){}; } else { - //debug("Tapping: pressing tap key.\n"); + if (!IS_NOEVENT(event)) debug("Tapping: other key while tapping.\n"); if (tap_count == 0) { // store event waiting_events_enqueue(event); @@ -146,14 +140,22 @@ void action_exec(keyevent_t event) process(event); } } else { - //debug("Tapping after releasing tap.\n"); - // Sequential tap + // Waiting for sequential tap if (tap_count && event.pressed && KEYEQ(tapping_event.key, event.key)) { tap_count++; tapping_event = event; debug("Tapping: Sequential tap("); debug_hex(tap_count); debug(")\n"); + process(event); + } else if (event.pressed && is_tap_key(event)) { + // Sequential tap can be interfered with other tap key. + debug("Tapping: Start with interfering other tap.\n"); + tapping_event = event; + tap_count = 0; + waiting_events_clear(); + } else { + if (!IS_NOEVENT(event)) debug("Tapping: other key just after tap.\n"); + process(event); } - process(event); } } // Not in tapping term @@ -162,7 +164,7 @@ void action_exec(keyevent_t event) if (tapping_event.pressed) { if (tap_count == 0) { // Not tap, holding down normal key. - debug("Not tap.\n"); + debug("Tapping: End. Not tap(time out).\n"); process(tapping_event); waiting_events_process_in_current_layer(); @@ -170,15 +172,27 @@ void action_exec(keyevent_t event) tapping_event = (keyevent_t){}; process(event); } else { - // Holding down last tap key. - //debug("Time out with holding last tap.\n"); - process(event); + // Holding down last tap key. waiting for releasing last tap key. if (!event.pressed && KEYEQ(tapping_event.key, event.key)) { - debug("Tapping: End(Release holding last tap).\n"); + debug("Tapping: End. Release holding last tap(time out).\n"); + process(event); // clear after release last tap key tap_count = 0; tapping_event = (keyevent_t){}; waiting_events_clear(); + } else if (event.pressed && is_tap_key(event)) { + debug("Tapping: Start with forcing to release last tap(time out).\n"); + process((keyevent_t){ + .key = tapping_event.key, + .time = event.time, + .pressed = false }); + + tap_count = 0; + tapping_event = event; + waiting_events_clear(); + } else { + if (!IS_NOEVENT(event)) debug("Tapping: other key while waiting for release of last tap(time out).\n"); + process(event); } } } else { @@ -206,7 +220,6 @@ void action_exec(keyevent_t event) } } - static void process(keyevent_t event) { if (IS_NOEVENT(event)) { return; } @@ -354,6 +367,20 @@ static void process(keyevent_t event) default: // with tap key if (event.pressed) { + if (IS_TAPPING(event.key)) { + if (tap_count > 0) { + debug("LAYER_PRESSED: Tap: register_code\n"); + register_code(action.layer.code); + } else { + debug("LAYER_PRESSED: No tap: layer_switch\n"); + layer_switch(action.layer.opt); + } + } else { + // TODO: while other key tapping + debug("LAYER_PRESSED: No tap: layer_switch\n"); + layer_switch(action.layer.opt); + } +/* if (IS_TAPPING(event.key) && tap_count > 0) { debug("LAYER_PRESSED: Tap: register_code\n"); register_code(action.layer.code); @@ -361,6 +388,7 @@ static void process(keyevent_t event) debug("LAYER_PRESSED: No tap: layer_switch\n"); layer_switch(action.layer.opt); } +*/ } else { if (IS_TAPPING(event.key) && tap_count > 0) { debug("LAYER_PRESSED: Tap: unregister_code\n"); @@ -488,14 +516,23 @@ static void process(keyevent_t event) /* Extentions */ case ACT_MACRO: + break; case ACT_COMMAND: + break; case ACT_FUNCTION: + action_call_function(event, action.func.id); + //test_func(event, action.func.opt); + break; default: break; } } -static void register_code(uint8_t code) + +/* + * Utilities for actions. + */ +void register_code(uint8_t code) { if (code == KC_NO) { return; @@ -513,7 +550,7 @@ static void register_code(uint8_t code) } } -static void unregister_code(uint8_t code) +void unregister_code(uint8_t code) { if IS_KEY(code) { host_del_key(code); @@ -525,7 +562,7 @@ static void unregister_code(uint8_t code) } } -static void add_mods(uint8_t mods) +void add_mods(uint8_t mods) { if (mods) { host_add_mods(mods); @@ -533,7 +570,7 @@ static void add_mods(uint8_t mods) } } -static void del_mods(uint8_t mods) +void del_mods(uint8_t mods) { if (mods) { host_del_mods(mods); @@ -541,19 +578,19 @@ static void del_mods(uint8_t mods) } } -static void set_mods(uint8_t mods) +void set_mods(uint8_t mods) { host_set_mods(mods); host_send_keyboard_report(); } -static void clear_keyboard(void) +void clear_keyboard(void) { host_clear_mods(); clear_keyboard_but_mods(); } -static void clear_keyboard_but_mods(void) +void clear_keyboard_but_mods(void) { host_clear_keys(); host_send_keyboard_report(); @@ -567,13 +604,13 @@ static void clear_keyboard_but_mods(void) #endif } -static bool sending_anykey(void) +bool sending_anykey(void) { return (host_has_anykey() || host_mouse_in_use() || host_last_sysytem_report() || host_last_consumer_report()); } -static void layer_switch(uint8_t new_layer) +void layer_switch(uint8_t new_layer) { if (current_layer != new_layer) { debug("Layer Switch: "); debug_hex(current_layer); @@ -584,3 +621,30 @@ static void layer_switch(uint8_t new_layer) // TODO: update mods with full scan of matrix? if modifier changes between layers } } + +bool is_tap_key(keyevent_t event) +{ + action_t action = keymap_get_action(current_layer, event.key.pos.row, event.key.pos.col); + switch (action.kind.id) { + case ACT_LMODS_TAP: + case ACT_RMODS_TAP: + return true; + case ACT_LAYER_PRESSED: + case ACT_LAYER_BIT: + switch (action.layer.code) { + case 0x00: + case 0xF1 ... 0xFF: + return false; + case 0xF0: + default: + return true; + } + return false; + case ACT_FUNCTION: + if (action.func.opt & 0x1) { + return true; + } + return false; + } + return false; +} diff --git a/common/action.h b/common/action.h index 3115c67f4..9aa1d78e9 100644 --- a/common/action.h +++ b/common/action.h @@ -3,8 +3,30 @@ #include "keyboard.h" +extern uint8_t tap_count; +extern keyevent_t tapping_event; -/* Key Action(16bit code) + +/* + * Utilities for actions. + */ +void register_code(uint8_t code); +void unregister_code(uint8_t code); +void add_mods(uint8_t mods); +void del_mods(uint8_t mods); +void set_mods(uint8_t mods); +void clear_keyboard(void); +void clear_keyboard_but_mods(void); +bool sending_anykey(void); +void layer_switch(uint8_t new_layer); +bool is_tap_key(keyevent_t event); + + + + +/* +Action codes +16bit code: action_kind(4bit) + action_parameter(12bit) Keyboard Keys ------------- @@ -94,6 +116,7 @@ ACT_COMMAND(1110): ACT_FUNCTION(1111): 1111| address(12) Function +1111|opt | id(8) Function Macro record(dynamicly) Macro play(dynamicly) TODO: modifier + [tap key /w mod] @@ -160,6 +183,11 @@ typedef union { uint16_t option :4; uint16_t kind :4; } command; + struct action_function { + uint8_t id :8; + uint8_t opt :4; + uint8_t kind :4; + } func; } action_t; @@ -169,14 +197,20 @@ enum stroke_cmd { STROKE_ALLUP, /* release all keys in reverse order */ }; +// TODO: not needed? typedef struct { keyevent_t event; action_t action; uint8_t mods; } keyrecord_t; +/* action function */ +typedef void (*action_func_t)(keyevent_t event, uint8_t opt); + +// TODO: legacy keymap support void action_exec(keyevent_t event); +void action_call_function(keyevent_t event, uint8_t id); // TODO: proper names @@ -234,7 +268,7 @@ void action_exec(keyevent_t event); /* Command */ #define ACTION_COMMAND(opt, id) ACTION(ACT_COMMAND, (opt)<<8 | (addr)) /* Function */ -#define ACTION_FUNCTION(addr) ACTION(ACT_FUNCTION, addr) +#define ACTION_FUNCTION(id, opt) ACTION(ACT_FUNCTION, (opt)<<8 | id) /* helpers for readability */ diff --git a/common/keyboard.c b/common/keyboard.c index 9f0ca7676..6677e8011 100644 --- a/common/keyboard.c +++ b/common/keyboard.c @@ -89,7 +89,8 @@ void keyboard_task(void) } } } - // call to update delaying layer when no real event + // call with not real event to update state of aciton + // TODO: use NOEVENT macro action_exec((keyevent_t) { .key.pos = (keypos_t){ .row = 255, .col = 255 }, // assume this key doesn't exist .pressed = false, diff --git a/common/keyboard.h b/common/keyboard.h index 4a3ee85a8..6d06c95bb 100644 --- a/common/keyboard.h +++ b/common/keyboard.h @@ -44,10 +44,10 @@ typedef struct { #define KEYEQ(keya, keyb) (keya.raw == keyb.raw) #define IS_NOEVENT(event) (event.time == 0) -#define NOEVENT (keyevent_t) { \ +#define NOEVENT (keyevent_t){ \ .key = (keypos_t){ .row = 255, .col = 255 }, \ .pressed = false, \ - .time = 0, \ + .time = 0 \ } diff --git a/keyboard/hhkb/keymap.c b/keyboard/hhkb/keymap.c index e29b37b16..e4eeb3e39 100644 --- a/keyboard/hhkb/keymap.c +++ b/keyboard/hhkb/keymap.c @@ -56,7 +56,8 @@ static const uint16_t PROGMEM fn_actions[] = { ACTION_LAYER_SET_ON_PRESSED(1), // Fn1 ACTION_LAYER_SET_TAP_KEY(2, KC_SLASH), // Fn2 ACTION_LAYER_SET_TAP_KEY(3, KC_SCLN), // Fn3 - ACTION_LAYER_SET_ON_PRESSED(3), // Fn4 + //ACTION_LAYER_SET_ON_PRESSED(3), // Fn4 + ACTION_FUNCTION(0x01, 0xA), // Fn4 ACTION_LAYER_SET_TAP_KEY(5, KC_SPC), // Fn5 ACTION_LMODS_TAP(MOD_BIT(KC_LCTL), KC_BSPC), // Fn6 ACTION_RMODS_TAP(MOD_BIT(KC_RCTL), KC_ENT), // Fn7 @@ -196,7 +197,7 @@ action_t keymap_get_action(uint8_t layer, uint8_t row, uint8_t col) { case KC_MS_UP ... KC_MS_ACCEL2: action.code = ACTION_MOUSEKEY(key); break; -/* +/* TODO case KC_LCTRL ... KC_LGUI: action.code = ACTION_LMODS(MOD_BIT(key)); break; @@ -214,3 +215,18 @@ action_t keymap_get_action(uint8_t layer, uint8_t row, uint8_t col) { } return action; } + +// TODO: how to define action function +void action_call_function(keyevent_t event, uint8_t id) +{ + // '(' Shift+9 + if (event.pressed) { + register_code(KC_LSHIFT); + register_code(KC_9); + debug("action_call_function: pressed: id: "); debug_hex(id); debug("\n"); + } else { + unregister_code(KC_9); + unregister_code(KC_LSHIFT); + debug("action_call_function: released: id: "); debug_hex(id); debug("\n"); + } +} -- cgit v1.2.3-70-g09d2 From ef873791bdbff5cc6971705544e120c69ca94212 Mon Sep 17 00:00:00 2001 From: tmk Date: Sun, 27 Jan 2013 02:42:48 +0900 Subject: New tapping logic. --- common/action.c | 359 +++++++++++++++++++++++++++++++----------------------- common/action.h | 6 +- common/keyboard.c | 6 +- common/keyboard.h | 9 +- 4 files changed, 216 insertions(+), 164 deletions(-) (limited to 'common/action.c') diff --git a/common/action.c b/common/action.c index 88f8186c3..815931548 100644 --- a/common/action.c +++ b/common/action.c @@ -10,7 +10,7 @@ #include "action.h" -static void process(keyevent_t event); +static void process(keyrecord_t *record); void test_func(keyevent_t event, uint8_t opt) { @@ -26,202 +26,250 @@ uint8_t default_layer = 0; uint8_t current_layer = 0; /* tap term(ms) */ -#define TAP_TIME 200 +#define TAP_TERM 200 /* This counts up when tap occurs */ uint8_t tap_count = 0; keyevent_t tapping_event = {}; +keyrecord_t tapping_key = {}; /* TAPPING: This indicates that whether tap or not is not decided yet. */ // NOTE: keyevent_t.time 0 means no event. -#define IS_TAPPING(k) (tapping_event.time != 0 && KEYEQ(tapping_event.key, (k))) +#define IS_TAPPING() (tapping_key.event.time != 0) +#define IS_TAPPING_PRESSED() (IS_TAPPING() && tapping_key.event.pressed) +#define IS_TAPPING_RELEASED() (IS_TAPPING() && !tapping_key.event.pressed) +#define IS_TAPPING_KEY(k) (IS_TAPPING() && KEYEQ(tapping_key.event.key, (k))) +#define WITHIN_TAP_TERM(e) (TIMER_DIFF_16(e.time, tapping_key.event.time) < TAP_TERM) /* waiting keys buffer */ -#define WAITING_KEYS_BUFFER 8 -static keyevent_t waiting_events[WAITING_KEYS_BUFFER] = {}; -static uint8_t waiting_events_head = 0; -static uint8_t waiting_events_tail = 0; - -static bool waiting_events_enqueue(keyevent_t event) +#define WAITING_BUFFER_SIZE 8 +static keyrecord_t waiting_buffer[WAITING_BUFFER_SIZE] = {}; +/* point to empty cell to enq */ +static uint8_t waiting_buffer_head = 0; +/* point to the oldest data cell to deq */ +static uint8_t waiting_buffer_tail = 0; + +static bool waiting_buffer_enq(keyrecord_t record) { - if (IS_NOEVENT(event)) { return true; } + if (IS_NOEVENT(record.event)) { + return true; + } - if ((waiting_events_head + 1) % WAITING_KEYS_BUFFER == waiting_events_tail) { - debug("waiting_events_enqueue: Over flow.\n"); + if ((waiting_buffer_head + 1) % WAITING_BUFFER_SIZE == waiting_buffer_tail) { + debug("waiting_buffer_enq: Over flow.\n"); return false; } - debug("waiting_events["); debug_dec(waiting_events_head); debug("] = "); - debug_hex16(event.key.raw); debug("\n"); + debug("waiting_buffer_enq["); debug_dec(waiting_buffer_head); debug("] = "); + debug_hex16(record.event.key.raw); debug("\n"); - waiting_events[waiting_events_head] = event; - waiting_events_head = (waiting_events_head + 1)% WAITING_KEYS_BUFFER; + waiting_buffer[waiting_buffer_head] = record; + waiting_buffer_head = (waiting_buffer_head + 1) % WAITING_BUFFER_SIZE; return true; } -static keyevent_t waiting_events_dequeue(void) +static keyrecord_t waiting_buffer_deq(void) { - if (waiting_events_head == waiting_events_tail) { - return (keyevent_t){}; + if (waiting_buffer_head == waiting_buffer_tail) { + return (keyrecord_t){}; } - uint8_t tail = waiting_events_tail; - waiting_events_tail = waiting_events_tail + 1 % WAITING_KEYS_BUFFER; - return waiting_events[tail]; + uint8_t last_tail = waiting_buffer_tail; + waiting_buffer_tail = waiting_buffer_tail + 1 % WAITING_BUFFER_SIZE; + return waiting_buffer[last_tail]; } -static void waiting_events_clear(void) +static bool waiting_buffer_is_empty(void) { - waiting_events_head = 0; - waiting_events_tail = 0; + return (waiting_buffer_head == waiting_buffer_tail); } -static bool waiting_events_has(key_t key) +static void waiting_buffer_clear(void) { - for (uint8_t i = waiting_events_tail; i != waiting_events_head; i = (i + 1) % WAITING_KEYS_BUFFER) { - if KEYEQ(key, waiting_events[i].key) return true; - } - return false; + waiting_buffer_head = 0; + waiting_buffer_tail = 0; } -static void waiting_events_process_in_current_layer(void) +static bool waiting_buffer_typed(keyevent_t event) { - // TODO: in case of including tap key in waiting keys - for (uint8_t i = waiting_events_tail; i != waiting_events_head; i = (i + 1) % WAITING_KEYS_BUFFER) { - debug("waiting_events_process_in_current_layer["); debug_dec(i); debug("]\n"); - process(waiting_events[i]); + for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) { + if (KEYEQ(event.key, waiting_buffer[i].event.key) && event.pressed != waiting_buffer[i].event.pressed) { + return true; + } } - waiting_events_clear(); + return false; } -static bool waiting_events_has_anykey_pressed(void) +static bool waiting_buffer_has_anykey_pressed(void) { - for (uint8_t i = waiting_events_tail; i != waiting_events_head; i = (i + 1) % WAITING_KEYS_BUFFER) { - if (waiting_events[i].pressed) return true; + for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) { + if (waiting_buffer[i].event.pressed) return true; } return false; } +static void waiting_buffer_process(void) +{ +} -void action_exec(keyevent_t event) +/* + * Rule to judge tap: + * Tap key is typed(pressed and released) within TAP_TERM + * without interfaring by typing other key. + */ +/* return true when key event is processed. */ +static bool process_tap(keyrecord_t *keyp) { - if (!IS_NOEVENT(event)) { - debug("event: "); debug_hex16(event.key.raw); - debug("["); - if (event.pressed) debug("down"); else debug("up"); - debug("]\n"); - } - - // In tapping term - if (tapping_event.time && timer_elapsed(tapping_event.time) < TAP_TIME) { - if (tapping_event.pressed) { - if (!event.pressed && KEYEQ(tapping_event.key, event.key)) { - debug("Tapping: Release tap key.\n"); - if (tap_count == 0) { + keyevent_t event = keyp->event; + + // if tapping + if (IS_TAPPING_PRESSED()) { + if (WITHIN_TAP_TERM(event)) { + if (tapping_key.tap_count == 0) { + if (IS_TAPPING_KEY(event.key) && !event.pressed) { + // first tap! debug("Tapping: First tap.\n"); - // count up on release - tap_count++; + tapping_key.tap_count = 1; + process(&tapping_key); - process(tapping_event); - waiting_events_process_in_current_layer(); - } - tapping_event = event; - process(event); - } else if (!event.pressed && waiting_events_has(event.key)) { - debug("Tapping: End(No tap by typing waiting key).\n"); - - process(tapping_event); - waiting_events_process_in_current_layer(); - process(event); + // enqueue + keyp->tap_count = tapping_key.tap_count; + return false; + } else if (!event.pressed && waiting_buffer_typed(event)) { + // other key typed. not tap. + debug("Tapping: End(No tap. Interfered by typing key).\n"); + process(&tapping_key); + tapping_key = (keyrecord_t){}; - tap_count = 0; - tapping_event = (keyevent_t){}; + // enqueue + return false; + } else { + // other key events shall be stored till tapping state settles. + return false; + } } else { - if (!IS_NOEVENT(event)) debug("Tapping: other key while tapping.\n"); - if (tap_count == 0) { - // store event - waiting_events_enqueue(event); - return; + if (IS_TAPPING_KEY(event.key) && !event.pressed) { + keyp->tap_count = tapping_key.tap_count; + debug("Tapping: tap release("); debug_dec(keyp->tap_count); debug(")\n"); + tapping_key = *keyp; + return false; + } + else if (is_tap_key(keyp->event.key) && event.pressed) { + debug("Tapping: Start with forcing to release last tap.\n"); + process(&(keyrecord_t){ + .tap_count = tapping_key.tap_count, + .event.key = tapping_key.event.key, + .event.time = event.time, + .event.pressed = false + }); + tapping_key = *keyp; + return false; + } + else { + if (!IS_NOEVENT(keyp->event)) debug("Tapping: key event while tap.\n"); + process(keyp); + return true; } - process(event); } - } else { - // Waiting for sequential tap - if (tap_count && event.pressed && KEYEQ(tapping_event.key, event.key)) { - tap_count++; - tapping_event = event; - debug("Tapping: Sequential tap("); debug_hex(tap_count); debug(")\n"); - process(event); - } else if (event.pressed && is_tap_key(event)) { + } + // not within TAP_TERM + else { + if (tapping_key.tap_count == 0) { + // timeout. not tap. + debug("Tapping: End. Not tap(time out).\n"); + process(&tapping_key); + tapping_key = (keyrecord_t){}; + return false; + } else { + if (IS_TAPPING_KEY(event.key) && !event.pressed) { + debug("Tapping: End. tap release."); + keyp->tap_count = tapping_key.tap_count; + process(keyp); + tapping_key = (keyrecord_t){}; + return true; + } else { + // other key after tap time out. + process(keyp); + return true; + } + } + } + } else if (IS_TAPPING_RELEASED()) { + if (WITHIN_TAP_TERM(event)) { + if (tapping_key.tap_count > 0 && IS_TAPPING_KEY(event.key) && event.pressed) { + // sequential tap. + keyp->tap_count = tapping_key.tap_count + 1; + debug("Tapping: tap press("); debug_dec(keyp->tap_count); debug(")\n"); + process(keyp); + tapping_key = *keyp; + return true; + } else if (event.pressed && is_tap_key(event.key)) { // Sequential tap can be interfered with other tap key. debug("Tapping: Start with interfering other tap.\n"); - tapping_event = event; - tap_count = 0; - waiting_events_clear(); + tapping_key = *keyp; + return true; } else { - if (!IS_NOEVENT(event)) debug("Tapping: other key just after tap.\n"); - process(event); + if (!IS_NOEVENT(keyp->event)) debug("Tapping: other key just after tap.\n"); + process(keyp); + return true; } + } else { + // timeout. no sequential tap. + debug("Tapping: End(Time out after releasing last tap).\n"); + tapping_key = (keyrecord_t){}; + process(keyp); + return true; + } + } else { + if (event.pressed && is_tap_key(event.key)) { + debug("Tapping: Start(Press tap key).\n"); + tapping_key = *keyp; + return true; + } else { + process(keyp); + return true; } } - // Not in tapping term - else { - if (tapping_event.time) { - if (tapping_event.pressed) { - if (tap_count == 0) { - // Not tap, holding down normal key. - debug("Tapping: End. Not tap(time out).\n"); - process(tapping_event); - waiting_events_process_in_current_layer(); - - tap_count = 0; - tapping_event = (keyevent_t){}; - process(event); - } else { - // Holding down last tap key. waiting for releasing last tap key. - if (!event.pressed && KEYEQ(tapping_event.key, event.key)) { - debug("Tapping: End. Release holding last tap(time out).\n"); - process(event); - // clear after release last tap key - tap_count = 0; - tapping_event = (keyevent_t){}; - waiting_events_clear(); - } else if (event.pressed && is_tap_key(event)) { - debug("Tapping: Start with forcing to release last tap(time out).\n"); - process((keyevent_t){ - .key = tapping_event.key, - .time = event.time, - .pressed = false }); - - tap_count = 0; - tapping_event = event; - waiting_events_clear(); - } else { - if (!IS_NOEVENT(event)) debug("Tapping: other key while waiting for release of last tap(time out).\n"); - process(event); - } - } - } else { - // time out for sequential tap after complete last tap - debug("Tapping: End(Time out after releasing last tap).\n"); - tap_count = 0; - tapping_event = (keyevent_t){}; - waiting_events_clear(); +} - process(event); - } - } else { - // Normal state without tapping - if (event.pressed && is_tap_key(event)) { - debug("Tapping: Start(Press tap key).\n"); - tapping_event = event; - tap_count = 0; - waiting_events_clear(); - } else { - //debug("Normal event(No tapping)\n"); - process(event); - } +void action_exec(keyevent_t event) +{ + if (!IS_NOEVENT(event)) { + debug("event: "); + debug_hex16(event.time); debug(": "); + debug_hex16(event.key.raw); + debug("["); + if (event.pressed) debug("down"); else debug("up"); + debug("]\n"); + } + + keyrecord_t record = { .event = event }; + + // pre-process on tapping + if (process_tap(&record)) { + if (!IS_NOEVENT(record.event)) debug("processed.\n"); + } else { + if (!IS_NOEVENT(record.event)) debug("enqueued.\n"); + if (!waiting_buffer_enq(record)) { + // clear all in case of overflow. + clear_keyboard(); + waiting_buffer_clear(); + tapping_key = (keyrecord_t){}; } + } + // TODO: need to process every time? + // process waiting_buffer + for (; waiting_buffer_tail != waiting_buffer_head; waiting_buffer_tail = (waiting_buffer_tail + 1) % WAITING_BUFFER_SIZE) { + if (process_tap(&waiting_buffer[waiting_buffer_tail])) { + debug("processed: waiting_buffer["); debug_dec(waiting_buffer_tail); debug("] = "); + debug_hex16(waiting_buffer[waiting_buffer_tail].event.key.raw); debug("\n"); + } else { + break; + } } } -static void process(keyevent_t event) +static void process(keyrecord_t *record) { + // TODO: use record + keyevent_t event = record->event; + uint8_t tap_count = record->tap_count; + if (IS_NOEVENT(event)) { return; } action_t action = keymap_get_action(current_layer, event.key.pos.row, event.key.pos.col); @@ -286,10 +334,11 @@ static void process(keyevent_t event) uint8_t tmp_mods = (action.kind.id == ACT_LMODS_TAP) ? action.key.mods : action.key.mods<<4; if (event.pressed) { - if (IS_TAPPING(event.key) && tap_count > 0) { - if (waiting_events_has_anykey_pressed()) { + if (IS_TAPPING_KEY(event.key) && tap_count > 0) { + if (waiting_buffer_has_anykey_pressed()) { debug("MODS_TAP: Tap: Cancel: add_mods\n"); - tap_count = 0; + // ad hoc: set 0 to cancel tap + record->tap_count = 0; add_mods(tmp_mods); } else { debug("MODS_TAP: Tap: register_code\n"); @@ -300,7 +349,7 @@ static void process(keyevent_t event) add_mods(tmp_mods); } } else { - if (IS_TAPPING(event.key) && tap_count > 0) { + if (IS_TAPPING_KEY(event.key) && tap_count > 0) { debug("MODS_TAP: Tap: unregister_code\n"); unregister_code(action.key.code); } else { @@ -367,7 +416,7 @@ static void process(keyevent_t event) default: // with tap key if (event.pressed) { - if (IS_TAPPING(event.key)) { + if (IS_TAPPING_KEY(event.key)) { if (tap_count > 0) { debug("LAYER_PRESSED: Tap: register_code\n"); register_code(action.layer.code); @@ -381,7 +430,7 @@ static void process(keyevent_t event) layer_switch(action.layer.opt); } /* - if (IS_TAPPING(event.key) && tap_count > 0) { + if (IS_TAPPING_KEY(event.key) && tap_count > 0) { debug("LAYER_PRESSED: Tap: register_code\n"); register_code(action.layer.code); } else { @@ -390,7 +439,7 @@ static void process(keyevent_t event) } */ } else { - if (IS_TAPPING(event.key) && tap_count > 0) { + if (IS_TAPPING_KEY(event.key) && tap_count > 0) { debug("LAYER_PRESSED: Tap: unregister_code\n"); unregister_code(action.layer.code); } else { @@ -446,7 +495,7 @@ static void process(keyevent_t event) default: // with tap key if (event.pressed) { - if (IS_TAPPING(event.key) && tap_count > 0) { + if (IS_TAPPING_KEY(event.key) && tap_count > 0) { debug("LAYER_BIT: Tap: register_code\n"); register_code(action.layer.code); } else { @@ -454,7 +503,7 @@ static void process(keyevent_t event) layer_switch(current_layer | action.layer.opt); } } else { - if (IS_TAPPING(event.key) && tap_count > 0) { + if (IS_TAPPING_KEY(event.key) && tap_count > 0) { debug("LAYER_BIT: Tap: unregister_code\n"); unregister_code(action.layer.code); } else { @@ -622,9 +671,9 @@ void layer_switch(uint8_t new_layer) } } -bool is_tap_key(keyevent_t event) +bool is_tap_key(key_t key) { - action_t action = keymap_get_action(current_layer, event.key.pos.row, event.key.pos.col); + action_t action = keymap_get_action(current_layer, key.pos.row, key.pos.col); switch (action.kind.id) { case ACT_LMODS_TAP: case ACT_RMODS_TAP: diff --git a/common/action.h b/common/action.h index 9aa1d78e9..327a009ef 100644 --- a/common/action.h +++ b/common/action.h @@ -19,7 +19,7 @@ void clear_keyboard(void); void clear_keyboard_but_mods(void); bool sending_anykey(void); void layer_switch(uint8_t new_layer); -bool is_tap_key(keyevent_t event); +bool is_tap_key(key_t key); @@ -197,11 +197,9 @@ enum stroke_cmd { STROKE_ALLUP, /* release all keys in reverse order */ }; -// TODO: not needed? typedef struct { keyevent_t event; - action_t action; - uint8_t mods; + uint8_t tap_count; } keyrecord_t; /* action function */ diff --git a/common/keyboard.c b/common/keyboard.c index 1aff81f54..2422fb758 100644 --- a/common/keyboard.c +++ b/common/keyboard.c @@ -80,7 +80,7 @@ void keyboard_task(void) action_exec((keyevent_t){ .key.pos = (keypos_t){ .row = r, .col = c }, .pressed = (matrix_row & (1< Date: Sun, 27 Jan 2013 10:37:15 +0900 Subject: Remove test_func. --- common/action.c | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) (limited to 'common/action.c') diff --git a/common/action.c b/common/action.c index 815931548..db31613bf 100644 --- a/common/action.c +++ b/common/action.c @@ -12,15 +12,7 @@ static void process(keyrecord_t *record); -void test_func(keyevent_t event, uint8_t opt) -{ - if (event.pressed) { - debug("test_func:pressed: "); debug_hex(opt); debug("\n"); - } else { - debug("test_func:released: "); debug_hex(opt); debug("\n"); - } -} - +// TODO /* layer */ uint8_t default_layer = 0; uint8_t current_layer = 0; @@ -570,7 +562,6 @@ static void process(keyrecord_t *record) break; case ACT_FUNCTION: action_call_function(event, action.func.id); - //test_func(event, action.func.opt); break; default: break; -- cgit v1.2.3-70-g09d2 From bfd7fe586297d70f824a402fd476c3daa889fa56 Mon Sep 17 00:00:00 2001 From: tmk Date: Sun, 27 Jan 2013 16:38:19 +0900 Subject: Add oneshot modifier action. --- common/action.c | 201 ++++++++++++++++++++++++++++++++----------------- common/action.h | 198 +++++++++++++++++++++++------------------------- keyboard/hhkb/keymap.c | 12 ++- 3 files changed, 236 insertions(+), 175 deletions(-) (limited to 'common/action.c') diff --git a/common/action.c b/common/action.c index db31613bf..22f0bf0a0 100644 --- a/common/action.c +++ b/common/action.c @@ -97,6 +97,40 @@ static void waiting_buffer_process(void) { } +/* 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. + */ +static struct { + uint8_t mods; + uint8_t time; + bool ready; + bool disabled; +} oneshot_state; +static void oneshot_start(uint8_t mods, uint16_t time) +{ + oneshot_state.mods = mods; + oneshot_state.time = time; + oneshot_state.ready = true; +} +static void oneshot_cancel(void) +{ + oneshot_state.mods = 0; + oneshot_state.time = 0; + oneshot_state.ready = false; +} +static void oneshot_toggle(void) +{ + oneshot_state.disabled = !oneshot_state.disabled; +} + /* * Rule to judge tap: @@ -271,83 +305,102 @@ static void process(keyrecord_t *record) switch (action.kind.id) { /* Key and Mods */ case ACT_LMODS: - // |pressed |released - // --------------+---------------------------------+------------ - // key |down(key) |up(key) - // mods |add(mods) |del(mods) - // key with mods |add(mods), down(key), unset(mods)|up(key) - if (event.pressed) { - uint8_t tmp_mods = host_get_mods(); - if (action.key.mods) { - host_add_mods(action.key.mods); - host_send_keyboard_report(); - } - register_code(action.key.code); - if (action.key.mods && action.key.code) { - host_set_mods(tmp_mods); - host_send_keyboard_report(); - } - } else { - if (action.key.mods && !action.key.code) { - host_del_mods(action.key.mods); - host_send_keyboard_report(); - } - unregister_code(action.key.code); - } - break; case ACT_RMODS: - // |pressed |released - // --------------+---------------------------------+------------ - // key |down(key) |up(key) - // mods |add(mods) |del(mods) - // key with mods |add(mods), down(key), unset(mods)|up(key) - if (event.pressed) { - uint8_t tmp_mods = host_get_mods(); - if (action.key.mods) { - host_add_mods(action.key.mods<<4); - host_send_keyboard_report(); - } - register_code(action.key.code); - if (action.key.mods && action.key.code) { - host_set_mods(tmp_mods); - host_send_keyboard_report(); - } - } else { - if (action.key.mods && !action.key.code) { - host_del_mods(action.key.mods<<4); - host_send_keyboard_report(); + { + uint8_t mods = (action.kind.id == ACT_LMODS) ? action.key.mods : + action.key.mods<<4; + if (event.pressed) { + uint8_t tmp_mods = host_get_mods(); + if (mods) { + host_add_mods(mods); + host_send_keyboard_report(); + } + register_code(action.key.code); + if (mods && action.key.code) { + host_set_mods(tmp_mods); + host_send_keyboard_report(); + } + } else { + if (mods && !action.key.code) { + host_del_mods(mods); + host_send_keyboard_report(); + } + unregister_code(action.key.code); } - unregister_code(action.key.code); } break; case ACT_LMODS_TAP: case ACT_RMODS_TAP: { - uint8_t tmp_mods = (action.kind.id == ACT_LMODS_TAP) ? action.key.mods : - action.key.mods<<4; - if (event.pressed) { - if (IS_TAPPING_KEY(event.key) && tap_count > 0) { - if (waiting_buffer_has_anykey_pressed()) { - debug("MODS_TAP: Tap: Cancel: add_mods\n"); - // ad hoc: set 0 to cancel tap - record->tap_count = 0; - add_mods(tmp_mods); + uint8_t mods = (action.kind.id == ACT_LMODS_TAP) ? action.key.mods : + action.key.mods<<4; + switch (action.layer.code) { + case 0x00: + // Oneshot modifier + if (event.pressed) { + if (tap_count == 0) { + debug("MODS_TAP: Oneshot: add_mods\n"); + add_mods(mods); + } + else if (tap_count == 1) { + debug("MODS_TAP: Oneshot: start\n"); + oneshot_start(mods, event.time); + } + else if (tap_count == 5) { + debug("MODS_TAP: Oneshot: toggle\n"); + oneshot_toggle(); + } + else { + debug("MODS_TAP: Oneshot: cancel&add_mods\n"); + // double tap cancels oneshot and works as normal modifier. + oneshot_cancel(); + add_mods(mods); + } } else { - debug("MODS_TAP: Tap: register_code\n"); - register_code(action.key.code); + if (tap_count == 0) { + debug("MODS_TAP: Oneshot: cancel/del_mods\n"); + // cancel oneshot by holding. + oneshot_cancel(); + del_mods(mods); + } + else if (tap_count == 1) { + debug("MODS_TAP: Oneshot: del_mods\n"); + // retain Oneshot + del_mods(mods); + } + else { + debug("MODS_TAP: Oneshot: del_mods\n"); + // cancel Mods + del_mods(mods); + } } - } else { - debug("MODS_TAP: No tap: add_mods\n"); - add_mods(tmp_mods); - } - } else { - if (IS_TAPPING_KEY(event.key) && tap_count > 0) { - debug("MODS_TAP: Tap: unregister_code\n"); - unregister_code(action.key.code); - } else { - debug("MODS_TAP: No tap: add_mods\n"); - del_mods(tmp_mods); - } + break; + default: + if (event.pressed) { + if (tap_count > 0) { + if (waiting_buffer_has_anykey_pressed()) { + debug("MODS_TAP: Tap: Cancel: add_mods\n"); + // ad hoc: set 0 to cancel tap + record->tap_count = 0; + add_mods(mods); + } else { + debug("MODS_TAP: Tap: register_code\n"); + register_code(action.key.code); + } + } else { + debug("MODS_TAP: No tap: add_mods\n"); + add_mods(mods); + } + } else { + if (tap_count > 0) { + debug("MODS_TAP: Tap: unregister_code\n"); + unregister_code(action.key.code); + } else { + debug("MODS_TAP: No tap: add_mods\n"); + del_mods(mods); + } + } + break; } } break; @@ -579,7 +632,17 @@ void register_code(uint8_t code) } else if IS_KEY(code) { // TODO: should push command_proc out of this block? - if (!command_proc(code)) { + if (command_proc(code)) return; + + if (oneshot_state.mods && oneshot_state.ready && !oneshot_state.disabled) { + uint8_t tmp_mods = host_get_mods(); + host_add_mods(oneshot_state.mods); + host_add_key(code); + host_send_keyboard_report(); + + host_set_mods(tmp_mods); + oneshot_state.ready = false; + } else { host_add_key(code); host_send_keyboard_report(); } diff --git a/common/action.h b/common/action.h index 327a009ef..1b5b30d86 100644 --- a/common/action.h +++ b/common/action.h @@ -3,10 +3,79 @@ #include "keyboard.h" + +/* Action struct. + * + * In avr-gcc bit field seems to be assigned from LSB(bit0) to MSB(bit15). + * AVR looks like a little endian in avr-gcc. + * + * TODO: not portable across compiler/endianness? + * Byte order and bit order of 0x1234: + * Big endian: 15 ... 8 7 ... 210 + * | 0x12 | 0x34 | + * 0001 0010 0011 0100 + * Little endian: 012 ... 7 8 ... 15 + * | 0x34 | 0x12 | + * 0010 1100 0100 1000 + */ +typedef union { + uint16_t code; + struct action_kind { + uint16_t param :12; + uint16_t id :4; + } kind; + struct action_key { + uint16_t code :8; + uint16_t mods :4; + uint16_t kind :4; + } key; + struct action_layer { + uint16_t code :8; + uint16_t opt :4; + uint16_t kind :4; + } layer; + struct action_usage { + uint16_t code :10; + uint16_t page :2; + uint16_t kind :4; + } usage; + struct action_command { + uint16_t id :8; + uint16_t option :4; + uint16_t kind :4; + } command; + struct action_function { + uint8_t id :8; + uint8_t opt :4; + uint8_t kind :4; + } func; +} action_t; + +/* Action record. For internal use. */ +typedef struct { + keyevent_t event; + uint8_t tap_count; +} keyrecord_t; + + +/* Tap count: Tap is comprised of press and release within TAP_TERM. + * 0 means no tap. + * >1 means tap. + */ extern uint8_t tap_count; + +/* current tap key event */ extern keyevent_t tapping_event; +/* action function */ +typedef void (*action_func_t)(keyevent_t event, uint8_t opt); + +// TODO: legacy keymap support +void action_exec(keyevent_t event); +void action_call_function(keyevent_t event, uint8_t id); + + /* * Utilities for actions. */ @@ -25,33 +94,36 @@ bool is_tap_key(key_t key); /* -Action codes -16bit code: action_kind(4bit) + action_parameter(12bit) - + * Action codes + * ============ + * 16bit code: action_kind(4bit) + action_parameter(12bit) + * Keyboard Keys ------------- ACT_LMODS(0000): 0000|0000|000000|00 No action -0000|mods|000000|00 Left mods Momentary -0000|mods|000000|01 Left mods OneShot -0000|mods|000000|10 (reserved) -0000|mods|000000|11 (reserved) 0000|0000| keycode Key +0010|mods|000000|00 Left mods Momentary 0000|mods| keycode Key+Left mods ACT_RMODS(0001): 0001|0000|000000|00 No action +0001|0000| keycode Key(no used) 0001|mods|000000|00 Right mods Momentary -0001|mods|000000|01 Right mods OneShot -0001|mods|000000|10 (reserved) -0001|mods|000000|11 (reserved) -0001|0000| keycode Key 0001|mods| keycode Key+Right mods ACT_LMODS_TAP(0010): +0010|mods|000000|00 Left mods OneShot +0010|mods|000000|01 (reserved) +0010|mods|000000|10 (reserved) +0010|mods|000000|11 (reserved) 0010|mods| keycode Left mods+tap Key ACT_RMODS_TAP(0011): +0011|mods|000000|00 Right mods OneShot +0011|mods|000000|01 (reserved) +0011|mods|000000|10 (reserved) +0011|mods|000000|11 (reserved) 0011|mods| keycode Right mods+tap Key @@ -108,24 +180,22 @@ Extensions(11XX) NOTE: NOT FIXED ACT_MACRO(1100): -1100|opt | id(8) Macro play -1100|1111| id(8) Macro record +1100|opt | id(8) Macro play? +1100|1111| id(8) Macro record? ACT_COMMAND(1110): 1110|opt | id(8) Built-in Command exec ACT_FUNCTION(1111): -1111| address(12) Function -1111|opt | id(8) Function - Macro record(dynamicly) - Macro play(dynamicly) -TODO: modifier + [tap key /w mod] - : layerkey + [tap key /w mod] +1111| address(12) Function? +1111|opt | id(8) Function? + +TODO: modifier + function by tap? for example: LShift + '('[Shift+9] and RShift + ')'[Shift+0] http://deskthority.net/workshop-f7/tmk-keyboard-firmware-collection-t4478.html#p90052 -*/ + */ -enum action_id { +enum action_kind_id { ACT_LMODS = 0b0000, ACT_RMODS = 0b0001, ACT_LMODS_TAP = 0b0010, @@ -144,74 +214,11 @@ enum action_id { ACT_FUNCTION = 0b1111 }; -// TODO: not portable across compiler/endianness? -/* -In avr-gcc bit fields seems to be assigned from LSB(bit0) to MSB(bit15). -AVR looks like a little endian in avr-gcc. - -Byte order and bit order of 0x1234: -Big endian: 15 ... 8 7 ... 210 - | 0x12 | 0x34 | - 0001 0010 0011 0100 -Little endian: 012 ... 7 8 ... 15 - | 0x34 | 0x12 | - 0010 1100 0100 1000 -*/ -typedef union { - uint16_t code; - struct action_kind { - uint16_t param :12; - uint16_t id :4; - } kind; - struct action_key { - uint16_t code :8; - uint16_t mods :4; - uint16_t kind :4; - } key; - struct action_layer { - uint16_t code :8; - uint16_t opt :4; - uint16_t kind :4; - } layer; - struct action_usage { - uint16_t code :10; - uint16_t page :2; - uint16_t kind :4; - } usage; - struct action_command { - uint16_t id :8; - uint16_t option :4; - uint16_t kind :4; - } command; - struct action_function { - uint8_t id :8; - uint8_t opt :4; - uint8_t kind :4; - } func; -} action_t; - - -enum stroke_cmd { - STROKE_DOWN, - STROKE_UP, - STROKE_ALLUP, /* release all keys in reverse order */ +enum acion_param { + ONE_SHOT = 0x00, }; -typedef struct { - keyevent_t event; - uint8_t tap_count; -} keyrecord_t; - -/* action function */ -typedef void (*action_func_t)(keyevent_t event, uint8_t opt); - -// TODO: legacy keymap support -void action_exec(keyevent_t event); -void action_call_function(keyevent_t event, uint8_t id); - - -// TODO: proper names /* action_t utility */ #define ACTION_NO 0 #define ACTION(kind, param) ((kind)<<12 | (param)) @@ -221,16 +228,12 @@ void action_call_function(keyevent_t event, uint8_t id); #define ACTION_KEY(key) ACTION(ACT_LMODS, key) #define ACTION_LMODS(mods) ACTION(ACT_LMODS, (mods)<<8 | 0x00) #define ACTION_LMODS_KEY(mods, key) ACTION(ACT_LMODS, (mods)<<8 | (key)) -#define ACTION_LMODS_ONESHOT(mods) ACTION(ACT_LMODS, (mods)<<8 | 0x01) -#define ACTION_LMODS_SWITCH(mods, tap) ACTION(ACT_LMODS, (mods)<<8 | 0xF0 | (tap)) -#define ACTION_LMODS_TOGGLE(mods, tap) ACTION(ACT_LMODS, (mods)<<8 | 0xF1 | (tap)) #define ACTION_RMODS(mods) ACTION(ACT_RMODS, (mods)<<8 | 0x00) #define ACTION_RMODS_KEY(mods, key) ACTION(ACT_RMODS, (mods)<<8 | (key)) -#define ACTION_RMODS_ONESHOT(mods) ACTION(ACT_RMODS, (mods)<<8 | 0x01) -#define ACTION_RMODS_SWITCH(mods, tap) ACTION(ACT_RMODS, (mods)<<8 | 0xF0 | (tap)) -#define ACTION_RMODS_TOGGLE(mods, tap) ACTION(ACT_RMODS, (mods)<<8 | 0xF1 | (tap)) + /* Mods + Tap key */ #define ACTION_LMODS_TAP(mods, key) ACTION(ACT_LMODS_TAP, MOD_BITS(mods)<<8 | (key)) +#define ACTION_LMODS_ONESHOT(mods) ACTION(ACT_LMODS_TAP, MOD_BITS(mods)<<8 | ONE_SHOT) #define ACTION_RMODS_TAP(mods, key) ACTION(ACT_RMODS_TAP, MOD_BITS(mods)<<8 | (key)) /* Layer Switch */ @@ -268,15 +271,4 @@ void action_call_function(keyevent_t event, uint8_t id); /* Function */ #define ACTION_FUNCTION(id, opt) ACTION(ACT_FUNCTION, (opt)<<8 | id) - -/* helpers for readability */ -#define LAYER(layer) (layer) -#define TAP(tap) (tap) -#define DOUBLE_TAP 2 -#define TRIPLE_TAP 3 -#define QUADRUPLE_TAP 4 -#define QUINTUPLE_TAP 5 -#define DOWN(key) (key) -#define UP(key) STROKE_UP, (key) - #endif /* ACTION_H */ diff --git a/keyboard/hhkb/keymap.c b/keyboard/hhkb/keymap.c index e4eeb3e39..b74dc71ba 100644 --- a/keyboard/hhkb/keymap.c +++ b/keyboard/hhkb/keymap.c @@ -61,6 +61,8 @@ static const uint16_t PROGMEM fn_actions[] = { ACTION_LAYER_SET_TAP_KEY(5, KC_SPC), // Fn5 ACTION_LMODS_TAP(MOD_BIT(KC_LCTL), KC_BSPC), // Fn6 ACTION_RMODS_TAP(MOD_BIT(KC_RCTL), KC_ENT), // Fn7 + + ACTION_LMODS_TAP(MOD_BIT(KC_LSFT), ONE_SHOT), // Fn8 }; @@ -81,7 +83,7 @@ static const uint8_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = { KEYMAP(ESC, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, MINS,EQL, BSLS,GRV, \ TAB, Q, W, E, R, T, Y, U, I, O, P, LBRC,RBRC,BSPC, \ FN6, A, S, D, F, G, H, J, K, L, FN3, QUOT,FN7, \ - LSFT,Z, X, C, V, B, N, M, COMM,DOT, FN2, RSFT,FN1, \ + FN8, Z, X, C, V, B, N, M, COMM,DOT, FN2, RSFT,FN1, \ LGUI,LALT, FN5, RALT,FN4), /* Layer 1: HHKB mode (HHKB Fn) @@ -205,8 +207,12 @@ action_t keymap_get_action(uint8_t layer, uint8_t row, uint8_t col) { action.code = ACTION_RMODS(MOD_BIT(key)>>4); break; */ - case KC_FN0 ... KC_FN7: - action.code = pgm_read_word(&fn_actions[FN_INDEX(key)]); + case KC_FN0 ... FN_MAX: + if (FN_INDEX(key) < sizeof(fn_actions) / sizeof(fn_actions[0])) { + action.code = pgm_read_word(&fn_actions[FN_INDEX(key)]); + } else { + action.code = ACTION_NO; + } break; case KC_NO ... KC_UNDEFINED: default: -- cgit v1.2.3-70-g09d2 From 854c803fdda30d7f7905c18d777ea85cac9b74d9 Mon Sep 17 00:00:00 2001 From: tmk Date: Mon, 28 Jan 2013 11:30:23 +0900 Subject: Add tap toggle feature to action. --- common/action.c | 162 ++++++++++++++++++++++++++++++++++++++----------- common/action.h | 26 ++++---- keyboard/hhkb/keymap.c | 6 +- 3 files changed, 145 insertions(+), 49 deletions(-) (limited to 'common/action.c') diff --git a/common/action.c b/common/action.c index 22f0bf0a0..4b3b1dd68 100644 --- a/common/action.c +++ b/common/action.c @@ -19,6 +19,8 @@ uint8_t current_layer = 0; /* tap term(ms) */ #define TAP_TERM 200 +/* number of tap which fires toggle feature */ +#define TAP_TOGGLE 5 /* This counts up when tap occurs */ uint8_t tap_count = 0; @@ -59,10 +61,11 @@ static bool waiting_buffer_enq(keyrecord_t record) waiting_buffer_head = (waiting_buffer_head + 1) % WAITING_BUFFER_SIZE; return true; } +/* static keyrecord_t waiting_buffer_deq(void) { if (waiting_buffer_head == waiting_buffer_tail) { - return (keyrecord_t){}; + return (keyrecord_t){}; // ??? } uint8_t last_tail = waiting_buffer_tail; waiting_buffer_tail = waiting_buffer_tail + 1 % WAITING_BUFFER_SIZE; @@ -72,6 +75,7 @@ static bool waiting_buffer_is_empty(void) { return (waiting_buffer_head == waiting_buffer_tail); } +*/ static void waiting_buffer_clear(void) { waiting_buffer_head = 0; @@ -93,9 +97,7 @@ static bool waiting_buffer_has_anykey_pressed(void) } return false; } -static void waiting_buffer_process(void) -{ -} + /* Oneshot modifier * @@ -270,6 +272,7 @@ void action_exec(keyevent_t event) if (!IS_NOEVENT(record.event)) debug("processed.\n"); } else { if (!IS_NOEVENT(record.event)) debug("enqueued.\n"); + // enqueue if (!waiting_buffer_enq(record)) { // clear all in case of overflow. clear_keyboard(); @@ -278,7 +281,6 @@ void action_exec(keyevent_t event) } } - // TODO: need to process every time? // process waiting_buffer for (; waiting_buffer_tail != waiting_buffer_head; waiting_buffer_tail = (waiting_buffer_tail + 1) % WAITING_BUFFER_SIZE) { if (process_tap(&waiting_buffer[waiting_buffer_tail])) { @@ -292,7 +294,6 @@ void action_exec(keyevent_t event) static void process(keyrecord_t *record) { - // TODO: use record keyevent_t event = record->event; uint8_t tap_count = record->tap_count; @@ -450,9 +451,20 @@ static void process(keyrecord_t *record) } break; case 0xF0: - // TODO: tap toggle + // tap toggle + if (event.pressed) { + if (tap_count < TAP_TOGGLE) { + layer_switch(action.layer.opt); + } + } else { + if (tap_count >= TAP_TOGGLE) { + debug("LAYER_PRESSED: tap toggle.\n"); + layer_switch(action.layer.opt); + } + } break; case 0xFF: + // change default layer if (event.pressed) { default_layer = action.layer.opt; layer_switch(default_layer); @@ -461,30 +473,15 @@ static void process(keyrecord_t *record) default: // with tap key if (event.pressed) { - if (IS_TAPPING_KEY(event.key)) { - if (tap_count > 0) { - debug("LAYER_PRESSED: Tap: register_code\n"); - register_code(action.layer.code); - } else { - debug("LAYER_PRESSED: No tap: layer_switch\n"); - layer_switch(action.layer.opt); - } - } else { - // TODO: while other key tapping - debug("LAYER_PRESSED: No tap: layer_switch\n"); - layer_switch(action.layer.opt); - } -/* - if (IS_TAPPING_KEY(event.key) && tap_count > 0) { + if (tap_count > 0) { debug("LAYER_PRESSED: Tap: register_code\n"); register_code(action.layer.code); - } else { + } else { debug("LAYER_PRESSED: No tap: layer_switch\n"); layer_switch(action.layer.opt); - } -*/ + } } else { - if (IS_TAPPING_KEY(event.key) && tap_count > 0) { + if (tap_count > 0) { debug("LAYER_PRESSED: Tap: unregister_code\n"); unregister_code(action.layer.code); } else { @@ -502,16 +499,43 @@ static void process(keyrecord_t *record) } break; case 0xF0: - // Ignored. LAYER_RELEASED with tap toggle is invalid action. + // tap toggle + if (event.pressed) { + if (tap_count >= TAP_TOGGLE) { + debug("LAYER_RELEASED: tap toggle.\n"); + layer_switch(action.layer.opt); + } + } else { + if (tap_count < TAP_TOGGLE) { + layer_switch(action.layer.opt); + } + } break; case 0xFF: + // change default layer if (!event.pressed) { default_layer = action.layer.opt; layer_switch(default_layer); } break; default: - // Ignored. LAYER_RELEASED with tap key is invalid action. + // with tap key + if (event.pressed) { + if (tap_count > 0) { + debug("LAYER_RELEASED: Tap: register_code\n"); + register_code(action.layer.code); + } else { + debug("LAYER_RELEASED: No tap: NO ACTION\n"); + } + } else { + if (tap_count > 0) { + debug("LAYER_RELEASED: Tap: unregister_code\n"); + unregister_code(action.layer.code); + } else { + debug("LAYER_RELEASED: No tap: layer_switch\n"); + layer_switch(action.layer.opt); + } + } break; } break; @@ -525,7 +549,21 @@ static void process(keyrecord_t *record) } break; case 0xF0: - // TODO: tap toggle + // tap toggle + if (event.pressed) { + if (tap_count < TAP_TOGGLE) { + debug("LAYER_BIT: tap toggle(press).\n"); + layer_switch(current_layer | action.layer.opt); + } + } else { + if (tap_count < TAP_TOGGLE) { + debug("LAYER_BIT: tap toggle(release).\n"); + layer_switch(current_layer & ~action.layer.opt); + } else { + debug("LAYER_BIT: tap toggle.\n"); + layer_switch(current_layer | action.layer.opt); + } + } break; case 0xFF: // change default layer @@ -558,6 +596,7 @@ static void process(keyrecord_t *record) } break; } + break; case ACT_LAYER_EXT: switch (action.layer.opt) { case 0x00: @@ -569,16 +608,43 @@ static void process(keyrecord_t *record) } break; case 0xF0: - // TODO: tap toggle + // tap toggle + if (event.pressed) { + if (tap_count < TAP_TOGGLE) { + layer_switch(default_layer); + } + } else { + if (tap_count >= TAP_TOGGLE) { + debug("LAYER_EXT_PRESSED: tap toggle.\n"); + layer_switch(default_layer); + } + } break; case 0xFF: + // change default layer if (event.pressed) { default_layer = current_layer; layer_switch(default_layer); } break; default: - // TODO: tap key + // with tap key + if (event.pressed) { + if (tap_count > 0) { + debug("LAYER_EXT_PRESSED: Tap: register_code\n"); + register_code(action.layer.code); + } else { + debug("LAYER_EXT_PRESSED: No tap: layer_switch\n"); + layer_switch(default_layer); + } + } else { + if (tap_count > 0) { + debug("LAYER_EXT_PRESSED: Tap: unregister_code\n"); + unregister_code(action.layer.code); + } else { + debug("LAYER_EXT_PRESSED: No tap: NO ACTION\n"); + } + } break; } break; @@ -590,17 +656,43 @@ static void process(keyrecord_t *record) layer_switch(default_layer); } break; + case 0xF0: + // tap toggle + if (event.pressed) { + if (tap_count >= TAP_TOGGLE) { + debug("LAYER_EXT_RELEASED: tap toggle.\n"); + layer_switch(default_layer); + } + } else { + if (tap_count < TAP_TOGGLE) { + layer_switch(default_layer); + } + } + break; case 0xFF: + // change default layer if (!event.pressed) { default_layer = current_layer; layer_switch(default_layer); } break; - case 0xF0: default: - // Ignore tap. - if (!event.pressed) { - layer_switch(default_layer); + // with tap key + if (event.pressed) { + if (tap_count > 0) { + debug("LAYER_EXT_RELEASED: Tap: register_code\n"); + register_code(action.layer.code); + } else { + debug("LAYER_EXT_RELEASED: No tap: NO ACTION\n"); + } + } else { + if (tap_count > 0) { + debug("LAYER_EXT_RELEASED: Tap: unregister_code\n"); + unregister_code(action.layer.code); + } else { + debug("LAYER_EXT_RELEASED: No tap: layer_switch\n"); + layer_switch(default_layer); + } } break; } diff --git a/common/action.h b/common/action.h index 1b5b30d86..bdd2d2f54 100644 --- a/common/action.h +++ b/common/action.h @@ -157,10 +157,10 @@ ACT_LAYER_EXT(1011): Extentions 1011|0001|0000 0000 set default layer when released 1000|LLLL|1111 0000 set layer L when pressed + tap toggle -1001|LLLL|1111 0000 set layer L when released[tap is ignored/not used] +1001|LLLL|1111 0000 set layer L when released + tap toggle 1010|BBBB|1111 0000 on/off bit B when pressed/released + tap toggle 1011|0000|1111 0000 set default layer when pressed + tap toggle -1011|0001|1111 0000 set default layer when released[tap is ignored/not used] +1011|0001|1111 0000 set default layer when released + tap toggle 1000|LLLL|1111 1111 set L to default layer when pressed 1001|LLLL|1111 1111 set L to default layer when released @@ -169,10 +169,10 @@ ACT_LAYER_EXT(1011): Extentions 1011|0001|1111 1111 set current to default layer when released 1000|LLLL| keycode set layer L when pressed + tap key -1001|LLLL| keyocde set layer L when released[tap is ignored/not used] +1001|LLLL| keyocde set layer L when released + tap key 1010|BBBB| keyocde on/off bit B when pressed/released + tap key 1011|0000| keyocde set default layer when pressed + tap key -1011|0001| keyocde set default layer when released[tap is ignored/not used] +1011|0001| keyocde set default layer when released + tap key Extensions(11XX) @@ -235,27 +235,29 @@ enum acion_param { #define ACTION_LMODS_TAP(mods, key) ACTION(ACT_LMODS_TAP, MOD_BITS(mods)<<8 | (key)) #define ACTION_LMODS_ONESHOT(mods) ACTION(ACT_LMODS_TAP, MOD_BITS(mods)<<8 | ONE_SHOT) #define ACTION_RMODS_TAP(mods, key) ACTION(ACT_RMODS_TAP, MOD_BITS(mods)<<8 | (key)) +#define ACTION_RMODS_ONESHOT(mods) ACTION(ACT_RMODS_TAP, MOD_BITS(mods)<<8 | ONE_SHOT) -/* Layer Switch */ +/* Switch current layer */ #define ACTION_LAYER_SET_ON_PRESSED(layer) ACTION(ACT_LAYER_PRESSED, (layer)<<8 | 0x00) #define ACTION_LAYER_SET_ON_RELEASED(layer) ACTION(ACT_LAYER_RELEASED, (layer)<<8 | 0x00) #define ACTION_LAYER_BIT(bits) ACTION(ACT_LAYER_BIT, (layer)<<8 | 0x00) #define ACTION_LAYER_TO_DEFAULT_ON_PRESSED ACTION(ACT_LAYER_EXT, 0x0<<8 | 0x00) #define ACTION_LAYER_TO_DEFAULT_ON_RELEASED ACTION(ACT_LAYER_EXT, 0x1<<8 | 0x00) - -#define ACTION_LAYER_TAP_TOGGLE(layer) ACTION(ACT_LAYER_PRESSED, (layer)<<8 | 0xF0) -#define ACTION_LAYER_BIT_TAP_TOGGLE(layer) ACTION(ACT_LAYER_BIT, (layer)<<8 | 0xF0) -#define ACTION_LAYER_DEFAULT_TAP_TOGGLE ACTION(ACT_LAYER_EXT, 0x0<<8 | 0xF0) - -#define ACTION_LAYER_DEFAULT_SET_ON_PRESSED(layer) ACTION(ACT_LAYER_PRESSED, (layer)<<8 | 0xFF) +/* Switch default layer */ +#define ACTION_LAYER_DEFAULT_SET_ON_PRESSED(layer) ACTION(ACT_LAYER_PRESSED, (layer)<<8 | 0xFF) #define ACTION_LAYER_DEFAULT_SET_ON_RELEASED(layer) ACTION(ACT_LAYER_RELEASED, (layer)<<8 | 0xFF) #define ACTION_LAYER_DEFAULT_BIT(bits) ACTION(ACT_LAYER_BIT, (bits)<<8 | 0xFF) #define ACTION_LAYER_DEFAULT_SET_CURRENT_ON_PRESSED ACTION(ACT_LAYER_EXT, 0x0<<8 | 0xFF) #define ACTION_LAYER_DEFAULT_SET_CURRENT_ON_RELEASED ACTION(ACT_LAYER_EXT, 0x1<<8 | 0xFF) - +/* Layer switch with tap key */ #define ACTION_LAYER_SET_TAP_KEY(layer, key) ACTION(ACT_LAYER_PRESSED, (layer)<<8 | (key)) #define ACTION_LAYER_BIT_TAP_KEY(bits, key) ACTION(ACT_LAYER_BIT, (layer)<<8 | (key)) #define ACTION_LAYER_DEFAULT_SET_TAP_KEY(key) ACTION(ACT_LAYER_EXT, 0x0<<8 | (key)) +/* with tap toggle */ +#define ACTION_LAYER_SET_ON_PRESSED_TAP_TOGGLE(layer) ACTION(ACT_LAYER_PRESSED, (layer)<<8 | 0xF0) +#define ACTION_LAYER_SET_ON_RELEASED_TAP_TOGGLE(layer) ACTION(ACT_LAYER_RELEASED, (layer)<<8 | 0xF0) +#define ACTION_LAYER_BIT_TAP_TOGGLE(layer) ACTION(ACT_LAYER_BIT, (layer)<<8 | 0xF0) +#define ACTION_LAYER_DEFAULT_TAP_TOGGLE ACTION(ACT_LAYER_EXT, 0x0<<8 | 0xF0) /* HID Usage */ #define ACTION_USAGE_PAGE_SYSTEM 0 diff --git a/keyboard/hhkb/keymap.c b/keyboard/hhkb/keymap.c index b74dc71ba..2d0dabd70 100644 --- a/keyboard/hhkb/keymap.c +++ b/keyboard/hhkb/keymap.c @@ -63,6 +63,8 @@ static const uint16_t PROGMEM fn_actions[] = { ACTION_RMODS_TAP(MOD_BIT(KC_RCTL), KC_ENT), // Fn7 ACTION_LMODS_TAP(MOD_BIT(KC_LSFT), ONE_SHOT), // Fn8 + ACTION_LAYER_SET_ON_RELEASED_TAP_TOGGLE(1), // Fn9 + ACTION_LAYER_BIT_TAP_TOGGLE(1), // Fn10 }; @@ -83,7 +85,7 @@ static const uint8_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = { KEYMAP(ESC, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, MINS,EQL, BSLS,GRV, \ TAB, Q, W, E, R, T, Y, U, I, O, P, LBRC,RBRC,BSPC, \ FN6, A, S, D, F, G, H, J, K, L, FN3, QUOT,FN7, \ - FN8, Z, X, C, V, B, N, M, COMM,DOT, FN2, RSFT,FN1, \ + FN8, Z, X, C, V, B, N, M, COMM,DOT, FN2, RSFT,FN10, \ LGUI,LALT, FN5, RALT,FN4), /* Layer 1: HHKB mode (HHKB Fn) @@ -102,7 +104,7 @@ static const uint8_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = { KEYMAP(PWR, F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12, INS, DEL, \ CAPS,NO, NO, NO, NO, NO, NO, NO, PSCR,SLCK,BRK, UP, NO, BSPC, \ LCTL,VOLD,VOLU,MUTE,NO, NO, PAST,PSLS,HOME,PGUP,LEFT,RGHT,ENT, \ - LSFT,NO, NO, NO, NO, NO, PPLS,PMNS,END, PGDN,DOWN,RSFT,FN0, \ + LSFT,NO, NO, NO, NO, NO, PPLS,PMNS,END, PGDN,DOWN,RSFT,FN10, \ LGUI,LALT, SPC, RALT,RGUI), /* Layer 2: Vi mode (Slash) -- cgit v1.2.3-70-g09d2 From 1e3e41a2c9ed8b2f7d44be0aed5d96ed557fa13d Mon Sep 17 00:00:00 2001 From: tmk Date: Mon, 28 Jan 2013 14:06:42 +0900 Subject: Clean code. --- common.mk | 1 + common/action.c | 331 ++++++++++++++++++++++++++----------------------- common/action.h | 39 +++--- common/command.c | 17 +-- common/keyboard.c | 10 +- common/keyboard.h | 17 ++- common/keymap.h | 6 + keyboard/hhkb/config.h | 4 + keyboard/hhkb/keymap.c | 2 +- 9 files changed, 226 insertions(+), 201 deletions(-) (limited to 'common/action.c') diff --git a/common.mk b/common.mk index bd8616e5f..7cdaa5f74 100644 --- a/common.mk +++ b/common.mk @@ -2,6 +2,7 @@ COMMON_DIR = common SRC += $(COMMON_DIR)/host.c \ $(COMMON_DIR)/keyboard.c \ $(COMMON_DIR)/action.c \ + $(COMMON_DIR)/keymap.c \ $(COMMON_DIR)/command.c \ $(COMMON_DIR)/timer.c \ $(COMMON_DIR)/print.c \ diff --git a/common/action.c b/common/action.c index 4b3b1dd68..fb06e463c 100644 --- a/common/action.c +++ b/common/action.c @@ -1,3 +1,19 @@ +/* +Copyright 2012,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 "timer.h" #include "keymap.h" @@ -10,36 +26,44 @@ #include "action.h" -static void process(keyrecord_t *record); +static bool process_tapping(keyrecord_t *record); +static void process_action(keyrecord_t *record); + -// TODO -/* layer */ -uint8_t default_layer = 0; -uint8_t current_layer = 0; +/* + * Tapping + */ +/* period of tapping(ms) */ +#ifndef TAPPING_TERM +#define TAPPING_TERM 200 +#endif -/* tap term(ms) */ -#define TAP_TERM 200 -/* number of tap which fires toggle feature */ -#define TAP_TOGGLE 5 +/* tap count needed for toggling a feature */ +#ifndef TAPPING_TOGGLE +#define TAPPING_TOGGLE 5 +#endif -/* This counts up when tap occurs */ -uint8_t tap_count = 0; -keyevent_t tapping_event = {}; -keyrecord_t tapping_key = {}; +/* stores a key event of current tap. */ +static keyrecord_t tapping_key = {}; -/* TAPPING: This indicates that whether tap or not is not decided yet. */ -// NOTE: keyevent_t.time 0 means no event. -#define IS_TAPPING() (tapping_key.event.time != 0) +#define IS_TAPPING() !IS_NOEVENT(tapping_key.event) #define IS_TAPPING_PRESSED() (IS_TAPPING() && tapping_key.event.pressed) #define IS_TAPPING_RELEASED() (IS_TAPPING() && !tapping_key.event.pressed) #define IS_TAPPING_KEY(k) (IS_TAPPING() && KEYEQ(tapping_key.event.key, (k))) -#define WITHIN_TAP_TERM(e) (TIMER_DIFF_16(e.time, tapping_key.event.time) < TAP_TERM) +#define WITHIN_TAPPING_TERM(e) (TIMER_DIFF_16(e.time, tapping_key.event.time) < TAPPING_TERM) + -/* waiting keys buffer */ +/* + * Waiting buffer + * + * stores key events waiting for settling current tap. + */ #define WAITING_BUFFER_SIZE 8 static keyrecord_t waiting_buffer[WAITING_BUFFER_SIZE] = {}; + /* point to empty cell to enq */ static uint8_t waiting_buffer_head = 0; + /* point to the oldest data cell to deq */ static uint8_t waiting_buffer_tail = 0; @@ -65,7 +89,7 @@ static bool waiting_buffer_enq(keyrecord_t record) static keyrecord_t waiting_buffer_deq(void) { if (waiting_buffer_head == waiting_buffer_tail) { - return (keyrecord_t){}; // ??? + return (keyrecord_t){}; } uint8_t last_tail = waiting_buffer_tail; waiting_buffer_tail = waiting_buffer_tail + 1 % WAITING_BUFFER_SIZE; @@ -134,125 +158,6 @@ static void oneshot_toggle(void) } -/* - * Rule to judge tap: - * Tap key is typed(pressed and released) within TAP_TERM - * without interfaring by typing other key. - */ -/* return true when key event is processed. */ -static bool process_tap(keyrecord_t *keyp) -{ - keyevent_t event = keyp->event; - - // if tapping - if (IS_TAPPING_PRESSED()) { - if (WITHIN_TAP_TERM(event)) { - if (tapping_key.tap_count == 0) { - if (IS_TAPPING_KEY(event.key) && !event.pressed) { - // first tap! - debug("Tapping: First tap.\n"); - tapping_key.tap_count = 1; - process(&tapping_key); - - // enqueue - keyp->tap_count = tapping_key.tap_count; - return false; - } else if (!event.pressed && waiting_buffer_typed(event)) { - // other key typed. not tap. - debug("Tapping: End(No tap. Interfered by typing key).\n"); - process(&tapping_key); - tapping_key = (keyrecord_t){}; - - // enqueue - return false; - } else { - // other key events shall be stored till tapping state settles. - return false; - } - } else { - if (IS_TAPPING_KEY(event.key) && !event.pressed) { - keyp->tap_count = tapping_key.tap_count; - debug("Tapping: tap release("); debug_dec(keyp->tap_count); debug(")\n"); - tapping_key = *keyp; - return false; - } - else if (is_tap_key(keyp->event.key) && event.pressed) { - debug("Tapping: Start with forcing to release last tap.\n"); - process(&(keyrecord_t){ - .tap_count = tapping_key.tap_count, - .event.key = tapping_key.event.key, - .event.time = event.time, - .event.pressed = false - }); - tapping_key = *keyp; - return false; - } - else { - if (!IS_NOEVENT(keyp->event)) debug("Tapping: key event while tap.\n"); - process(keyp); - return true; - } - } - } - // not within TAP_TERM - else { - if (tapping_key.tap_count == 0) { - // timeout. not tap. - debug("Tapping: End. Not tap(time out).\n"); - process(&tapping_key); - tapping_key = (keyrecord_t){}; - return false; - } else { - if (IS_TAPPING_KEY(event.key) && !event.pressed) { - debug("Tapping: End. tap release."); - keyp->tap_count = tapping_key.tap_count; - process(keyp); - tapping_key = (keyrecord_t){}; - return true; - } else { - // other key after tap time out. - process(keyp); - return true; - } - } - } - } else if (IS_TAPPING_RELEASED()) { - if (WITHIN_TAP_TERM(event)) { - if (tapping_key.tap_count > 0 && IS_TAPPING_KEY(event.key) && event.pressed) { - // sequential tap. - keyp->tap_count = tapping_key.tap_count + 1; - debug("Tapping: tap press("); debug_dec(keyp->tap_count); debug(")\n"); - process(keyp); - tapping_key = *keyp; - return true; - } else if (event.pressed && is_tap_key(event.key)) { - // Sequential tap can be interfered with other tap key. - debug("Tapping: Start with interfering other tap.\n"); - tapping_key = *keyp; - return true; - } else { - if (!IS_NOEVENT(keyp->event)) debug("Tapping: other key just after tap.\n"); - process(keyp); - return true; - } - } else { - // timeout. no sequential tap. - debug("Tapping: End(Time out after releasing last tap).\n"); - tapping_key = (keyrecord_t){}; - process(keyp); - return true; - } - } else { - if (event.pressed && is_tap_key(event.key)) { - debug("Tapping: Start(Press tap key).\n"); - tapping_key = *keyp; - return true; - } else { - process(keyp); - return true; - } - } -} void action_exec(keyevent_t event) { @@ -268,7 +173,7 @@ void action_exec(keyevent_t event) keyrecord_t record = { .event = event }; // pre-process on tapping - if (process_tap(&record)) { + if (process_tapping(&record)) { if (!IS_NOEVENT(record.event)) debug("processed.\n"); } else { if (!IS_NOEVENT(record.event)) debug("enqueued.\n"); @@ -283,7 +188,7 @@ void action_exec(keyevent_t event) // process waiting_buffer for (; waiting_buffer_tail != waiting_buffer_head; waiting_buffer_tail = (waiting_buffer_tail + 1) % WAITING_BUFFER_SIZE) { - if (process_tap(&waiting_buffer[waiting_buffer_tail])) { + if (process_tapping(&waiting_buffer[waiting_buffer_tail])) { debug("processed: waiting_buffer["); debug_dec(waiting_buffer_tail); debug("] = "); debug_hex16(waiting_buffer[waiting_buffer_tail].event.key.raw); debug("\n"); } else { @@ -292,7 +197,7 @@ void action_exec(keyevent_t event) } } -static void process(keyrecord_t *record) +static void process_action(keyrecord_t *record) { keyevent_t event = record->event; uint8_t tap_count = record->tap_count; @@ -453,11 +358,11 @@ static void process(keyrecord_t *record) case 0xF0: // tap toggle if (event.pressed) { - if (tap_count < TAP_TOGGLE) { + if (tap_count < TAPPING_TOGGLE) { layer_switch(action.layer.opt); } } else { - if (tap_count >= TAP_TOGGLE) { + if (tap_count >= TAPPING_TOGGLE) { debug("LAYER_PRESSED: tap toggle.\n"); layer_switch(action.layer.opt); } @@ -501,12 +406,12 @@ static void process(keyrecord_t *record) case 0xF0: // tap toggle if (event.pressed) { - if (tap_count >= TAP_TOGGLE) { + if (tap_count >= TAPPING_TOGGLE) { debug("LAYER_RELEASED: tap toggle.\n"); layer_switch(action.layer.opt); } } else { - if (tap_count < TAP_TOGGLE) { + if (tap_count < TAPPING_TOGGLE) { layer_switch(action.layer.opt); } } @@ -551,12 +456,12 @@ static void process(keyrecord_t *record) case 0xF0: // tap toggle if (event.pressed) { - if (tap_count < TAP_TOGGLE) { + if (tap_count < TAPPING_TOGGLE) { debug("LAYER_BIT: tap toggle(press).\n"); layer_switch(current_layer | action.layer.opt); } } else { - if (tap_count < TAP_TOGGLE) { + if (tap_count < TAPPING_TOGGLE) { debug("LAYER_BIT: tap toggle(release).\n"); layer_switch(current_layer & ~action.layer.opt); } else { @@ -610,11 +515,11 @@ static void process(keyrecord_t *record) case 0xF0: // tap toggle if (event.pressed) { - if (tap_count < TAP_TOGGLE) { + if (tap_count < TAPPING_TOGGLE) { layer_switch(default_layer); } } else { - if (tap_count >= TAP_TOGGLE) { + if (tap_count >= TAPPING_TOGGLE) { debug("LAYER_EXT_PRESSED: tap toggle.\n"); layer_switch(default_layer); } @@ -659,12 +564,12 @@ static void process(keyrecord_t *record) case 0xF0: // tap toggle if (event.pressed) { - if (tap_count >= TAP_TOGGLE) { + if (tap_count >= TAPPING_TOGGLE) { debug("LAYER_EXT_RELEASED: tap toggle.\n"); layer_switch(default_layer); } } else { - if (tap_count < TAP_TOGGLE) { + if (tap_count < TAPPING_TOGGLE) { layer_switch(default_layer); } } @@ -706,6 +611,7 @@ static void process(keyrecord_t *record) case ACT_COMMAND: break; case ACT_FUNCTION: + // TODO action_call_function(event, action.func.id); break; default: @@ -713,6 +619,127 @@ static void process(keyrecord_t *record) } } +/* Tapping + * + * Rule: Tap key is typed(pressed and released) within TAPPING_TERM + * without interfaring by typing other key. + */ +/* return true when key event is processed. */ +static bool process_tapping(keyrecord_t *keyp) +{ + keyevent_t event = keyp->event; + + // if tapping + if (IS_TAPPING_PRESSED()) { + if (WITHIN_TAPPING_TERM(event)) { + if (tapping_key.tap_count == 0) { + if (IS_TAPPING_KEY(event.key) && !event.pressed) { + // first tap! + debug("Tapping: First tap.\n"); + tapping_key.tap_count = 1; + process_action(&tapping_key); + + // enqueue + keyp->tap_count = tapping_key.tap_count; + return false; + } else if (!event.pressed && waiting_buffer_typed(event)) { + // other key typed. not tap. + debug("Tapping: End(No tap. Interfered by typing key).\n"); + process_action(&tapping_key); + tapping_key = (keyrecord_t){}; + + // enqueue + return false; + } else { + // other key events shall be stored till tapping state settles. + return false; + } + } else { + if (IS_TAPPING_KEY(event.key) && !event.pressed) { + keyp->tap_count = tapping_key.tap_count; + debug("Tapping: tap release("); debug_dec(keyp->tap_count); debug(")\n"); + tapping_key = *keyp; + return false; + } + else if (is_tap_key(keyp->event.key) && event.pressed) { + debug("Tapping: Start with forcing to release last tap.\n"); + process_action(&(keyrecord_t){ + .tap_count = tapping_key.tap_count, + .event.key = tapping_key.event.key, + .event.time = event.time, + .event.pressed = false + }); + tapping_key = *keyp; + return false; + } + else { + if (!IS_NOEVENT(keyp->event)) debug("Tapping: key event while tap.\n"); + process_action(keyp); + return true; + } + } + } + // not within TAPPING_TERM + else { + if (tapping_key.tap_count == 0) { + // timeout. not tap. + debug("Tapping: End. Not tap(time out).\n"); + process_action(&tapping_key); + tapping_key = (keyrecord_t){}; + return false; + } else { + if (IS_TAPPING_KEY(event.key) && !event.pressed) { + debug("Tapping: End. tap release."); + keyp->tap_count = tapping_key.tap_count; + process_action(keyp); + tapping_key = (keyrecord_t){}; + return true; + } else { + // other key after tap time out. + process_action(keyp); + return true; + } + } + } + } else if (IS_TAPPING_RELEASED()) { + if (WITHIN_TAPPING_TERM(event)) { + if (tapping_key.tap_count > 0 && IS_TAPPING_KEY(event.key) && event.pressed) { + // sequential tap. + keyp->tap_count = tapping_key.tap_count + 1; + debug("Tapping: tap press("); debug_dec(keyp->tap_count); debug(")\n"); + process_action(keyp); + tapping_key = *keyp; + return true; + } else if (event.pressed && is_tap_key(event.key)) { + // Sequential tap can be interfered with other tap key. + debug("Tapping: Start with interfering other tap.\n"); + tapping_key = *keyp; + return true; + } else { + if (!IS_NOEVENT(keyp->event)) debug("Tapping: other key just after tap.\n"); + process_action(keyp); + return true; + } + } else { + // timeout. no sequential tap. + debug("Tapping: End(Time out after releasing last tap).\n"); + tapping_key = (keyrecord_t){}; + process_action(keyp); + return true; + } + } else { + if (event.pressed && is_tap_key(event.key)) { + debug("Tapping: Start(Press tap key).\n"); + tapping_key = *keyp; + return true; + } else { + process_action(keyp); + return true; + } + } +} + + /* * Utilities for actions. @@ -813,7 +840,7 @@ void layer_switch(uint8_t new_layer) current_layer = new_layer; clear_keyboard_but_mods(); // To avoid stuck keys - // TODO: update mods with full scan of matrix? if modifier changes between layers + // NOTE: update mods with full scan of matrix? if modifier changes between layers } } diff --git a/common/action.h b/common/action.h index bdd2d2f54..ed3fff6c2 100644 --- a/common/action.h +++ b/common/action.h @@ -1,3 +1,19 @@ +/* +Copyright 2012,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_H #define ACTION_H @@ -9,7 +25,7 @@ * In avr-gcc bit field seems to be assigned from LSB(bit0) to MSB(bit15). * AVR looks like a little endian in avr-gcc. * - * TODO: not portable across compiler/endianness? + * NOTE: not portable across compiler/endianness? * Byte order and bit order of 0x1234: * Big endian: 15 ... 8 7 ... 210 * | 0x12 | 0x34 | @@ -51,29 +67,17 @@ typedef union { } func; } action_t; -/* Action record. For internal use. */ +/* Struct to record action and tap count */ typedef struct { keyevent_t event; uint8_t tap_count; } keyrecord_t; -/* Tap count: Tap is comprised of press and release within TAP_TERM. - * 0 means no tap. - * >1 means tap. - */ -extern uint8_t tap_count; - -/* current tap key event */ -extern keyevent_t tapping_event; - - -/* action function */ -typedef void (*action_func_t)(keyevent_t event, uint8_t opt); - -// TODO: legacy keymap support +/* execute action per keyevent */ void action_exec(keyevent_t event); -void action_call_function(keyevent_t event, uint8_t id); +typedef void (*action_func_t)(keyevent_t event, uint8_t opt); // TODO:no need? +void action_call_function(keyevent_t event, uint8_t id); // TODO: action function /* @@ -194,7 +198,6 @@ TODO: modifier + function by tap? for example: LShift + '('[Shift+9] and RShift + ')'[Shift+0] http://deskthority.net/workshop-f7/tmk-keyboard-firmware-collection-t4478.html#p90052 */ - enum action_kind_id { ACT_LMODS = 0b0000, ACT_RMODS = 0b0001, diff --git a/common/command.c b/common/command.c index a06e6a00d..8ca16b910 100644 --- a/common/command.c +++ b/common/command.c @@ -19,6 +19,7 @@ along with this program. If not, see . #include #include "keycode.h" #include "host.h" +#include "keymap.h" #include "print.h" #include "debug.h" #include "util.h" @@ -53,7 +54,6 @@ static void mousekey_console_help(void); static uint8_t numkey2num(uint8_t code); static void switch_layer(uint8_t layer); -static void clear_keyboard(void); typedef enum { ONESHOT, CONSOLE, MOUSEKEY } cmdstate_t; @@ -555,18 +555,3 @@ static void switch_layer(uint8_t layer) default_layer = layer; print("switch to "); print_val_hex8(layer); } - -static void clear_keyboard(void) -{ - host_clear_keys(); - host_clear_mods(); - host_send_keyboard_report(); - - host_system_send(0); - host_consumer_send(0); - -#ifdef MOUSEKEY_ENABLE - mousekey_clear(); - mousekey_send(); -#endif -} diff --git a/common/keyboard.c b/common/keyboard.c index 2422fb758..5e9945baf 100644 --- a/common/keyboard.c +++ b/common/keyboard.c @@ -1,5 +1,5 @@ /* -Copyright 2011,2012 Jun Wako +Copyright 2011,2012,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 @@ -34,8 +34,6 @@ along with this program. If not, see . void keyboard_init(void) { - // TODO: to enable debug print magic key bind on boot time - // TODO: configuration of sendchar impl print_sendchar_func = sendchar; @@ -80,7 +78,7 @@ void keyboard_task(void) action_exec((keyevent_t){ .key.pos = (keypos_t){ .row = r, .col = c }, .pressed = (matrix_row & (1< +Copyright 2011,2012,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 @@ -26,6 +26,7 @@ along with this program. If not, see . extern "C" { #endif +/* key matrix position */ typedef struct { uint8_t col; uint8_t row; @@ -36,29 +37,33 @@ typedef union { keypos_t pos; } key_t; +/* key event */ typedef struct { key_t key; bool pressed; uint16_t time; } keyevent_t; +/* equivalent test of key_t */ #define KEYEQ(keya, keyb) ((keya).raw == (keyb).raw) -#define IS_NOEVENT(event) ((event).key.pos.row == 255 && (event).key.pos.col == 255) + +/* (time == 0) means no event and assumes matrix has no 255 line. */ +#define IS_NOEVENT(event) ((event).time == 0 || ((event).key.pos.row == 255 && (event).key.pos.col == 255)) + #define NOEVENT (keyevent_t){ \ .key.pos = (keypos_t){ .row = 255, .col = 255 }, \ .pressed = false, \ .time = 0 \ } + +/* tick event */ #define TICK (keyevent_t){ \ .key.pos = (keypos_t){ .row = 255, .col = 255 }, \ .pressed = false, \ - .time = timer_read() \ + .time = (timer_read() | 1) \ } -extern uint8_t current_layer; -extern uint8_t default_layer; - void keyboard_init(void); void keyboard_task(void); void keyboard_set_leds(uint8_t leds); diff --git a/common/keymap.h b/common/keymap.h index f992be18e..f54fea90d 100644 --- a/common/keymap.h +++ b/common/keymap.h @@ -22,6 +22,12 @@ along with this program. If not, see . #include #include "action.h" + +/* layer used currently */ +extern uint8_t current_layer; +/* layer to return or start with */ +extern uint8_t default_layer; + /* * legacy keymap interface: keycode */ diff --git a/keyboard/hhkb/config.h b/keyboard/hhkb/config.h index 66dede9a5..5fcec95eb 100644 --- a/keyboard/hhkb/config.h +++ b/keyboard/hhkb/config.h @@ -59,6 +59,10 @@ along with this program. If not, see . # define MOUSEKEY_DELAY_TIME 100 #endif +/* period of tapping(ms) */ +#define TAPPING_TERM 200 +/* tap count needed for toggling a feature */ +#define TAPPING_TOGGLE 5 /* PS/2 mouse */ #ifdef PS2_MOUSE_ENABLE diff --git a/keyboard/hhkb/keymap.c b/keyboard/hhkb/keymap.c index 2d0dabd70..c7e4cfc38 100644 --- a/keyboard/hhkb/keymap.c +++ b/keyboard/hhkb/keymap.c @@ -1,5 +1,5 @@ /* -Copyright 2011 Jun Wako +Copyright 2011,2012,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 -- cgit v1.2.3-70-g09d2 From ddb560052a8a336b5cec64ce08399c8299c9b4da Mon Sep 17 00:00:00 2001 From: tmk Date: Tue, 29 Jan 2013 10:40:43 +0900 Subject: Clean debug print in action.c. --- common/action.c | 95 ++++++++++++++++++++++++++++++++++++++++++++------------- common/debug.h | 1 + common/print.c | 9 ++++-- common/print.h | 2 ++ 4 files changed, 84 insertions(+), 23 deletions(-) (limited to 'common/action.c') diff --git a/common/action.c b/common/action.c index fb06e463c..e93a9afe3 100644 --- a/common/action.c +++ b/common/action.c @@ -29,6 +29,41 @@ along with this program. If not, see . static bool process_tapping(keyrecord_t *record); static void process_action(keyrecord_t *record); +static void debug_event(keyevent_t event) +{ + debug_hex16(event.key.raw); + if (event.pressed) debug("d("); else debug("u("); + debug_dec(event.time); debug(")"); +} +static void debug_record(keyrecord_t record) +{ + debug_event(record.event); debug(":"); debug_dec(record.tap_count); +} +static void debug_action(action_t action) +{ + switch (action.kind.id) { + case ACT_LMODS: debug("ACT_LMODS"); break; + case ACT_RMODS: debug("ACT_RMODS"); break; + case ACT_LMODS_TAP: debug("ACT_LMODS_TAP"); break; + case ACT_RMODS_TAP: debug("ACT_RMODS_TAP"); break; + case ACT_USAGE: debug("ACT_USAGE"); break; + case ACT_MOUSEKEY: debug("ACT_MOUSEKEY"); break; + case ACT_LAYER_PRESSED: debug("ACT_LAYER_PRESSED"); break; + case ACT_LAYER_RELEASED: debug("ACT_LAYER_RELEASED"); break; + case ACT_LAYER_BIT: debug("ACT_LAYER_BIT"); break; + case ACT_LAYER_EXT: debug("ACT_LAYER_EXT"); break; + case ACT_MACRO: debug("ACT_MACRO"); break; + case ACT_COMMAND: debug("ACT_COMMAND"); break; + case ACT_FUNCTION: debug("ACT_FUNCTION"); break; + default: debug("UNKNOWN"); break; + } + debug("["); + debug_hex4(action.kind.param>>8); + debug(":"); + debug_hex8(action.kind.param & 0xff); + debug("]"); +} + /* * Tapping @@ -67,6 +102,14 @@ static uint8_t waiting_buffer_head = 0; /* point to the oldest data cell to deq */ static uint8_t waiting_buffer_tail = 0; +static void debug_waiting_buffer(void) +{ + debug("{ "); + for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) { + debug("["); debug_dec(i); debug("]="); debug_record(waiting_buffer[i]); debug(" "); + } + debug("}\n"); +} static bool waiting_buffer_enq(keyrecord_t record) { if (IS_NOEVENT(record.event)) { @@ -78,11 +121,10 @@ static bool waiting_buffer_enq(keyrecord_t record) return false; } - debug("waiting_buffer_enq["); debug_dec(waiting_buffer_head); debug("] = "); - debug_hex16(record.event.key.raw); debug("\n"); - waiting_buffer[waiting_buffer_head] = record; waiting_buffer_head = (waiting_buffer_head + 1) % WAITING_BUFFER_SIZE; + + debug("waiting_buffer_enq: "); debug_waiting_buffer(); return true; } /* @@ -162,21 +204,18 @@ static void oneshot_toggle(void) void action_exec(keyevent_t event) { if (!IS_NOEVENT(event)) { - debug("event: "); - debug_hex16(event.time); debug(": "); - debug_hex16(event.key.raw); - debug("["); - if (event.pressed) debug("down"); else debug("up"); - debug("]\n"); + debug("\n---- action_exec: start -----\n"); + debug("EVENT: "); debug_event(event); debug("\n"); } keyrecord_t record = { .event = event }; // pre-process on tapping if (process_tapping(&record)) { - if (!IS_NOEVENT(record.event)) debug("processed.\n"); + if (!IS_NOEVENT(record.event)) { + debug("processed: "); debug_record(record); debug("\n"); + } } else { - if (!IS_NOEVENT(record.event)) debug("enqueued.\n"); // enqueue if (!waiting_buffer_enq(record)) { // clear all in case of overflow. @@ -187,14 +226,20 @@ void action_exec(keyevent_t event) } // process waiting_buffer + if (!IS_NOEVENT(event) && waiting_buffer_head != waiting_buffer_tail) { + debug("---- action_exec: process waiting_buffer -----\n"); + } for (; waiting_buffer_tail != waiting_buffer_head; waiting_buffer_tail = (waiting_buffer_tail + 1) % WAITING_BUFFER_SIZE) { if (process_tapping(&waiting_buffer[waiting_buffer_tail])) { debug("processed: waiting_buffer["); debug_dec(waiting_buffer_tail); debug("] = "); - debug_hex16(waiting_buffer[waiting_buffer_tail].event.key.raw); debug("\n"); + debug_record(waiting_buffer[waiting_buffer_tail]); debug("\n\n"); } else { break; } } + if (!IS_NOEVENT(event)) { + debug("\n"); + } } static void process_action(keyrecord_t *record) @@ -205,8 +250,8 @@ static void process_action(keyrecord_t *record) if (IS_NOEVENT(event)) { return; } action_t action = keymap_get_action(current_layer, event.key.pos.row, event.key.pos.col); - debug("action: "); debug_hex16(action.code); - if (event.pressed) debug("[down]\n"); else debug("[up]\n"); + //debug("action: "); debug_hex16(action.code); if (event.pressed) debug("d\n"); else debug("u\n"); + debug("ACTION: "); debug_action(action); debug("\n"); switch (action.kind.id) { /* Key and Mods */ @@ -635,8 +680,8 @@ static bool process_tapping(keyrecord_t *keyp) if (tapping_key.tap_count == 0) { if (IS_TAPPING_KEY(event.key) && !event.pressed) { // first tap! - debug("Tapping: First tap.\n"); tapping_key.tap_count = 1; + debug("Tapping: First tap: tapping_key="); debug_record(tapping_key); debug("\n"); process_action(&tapping_key); // enqueue @@ -644,7 +689,7 @@ static bool process_tapping(keyrecord_t *keyp) return false; } else if (!event.pressed && waiting_buffer_typed(event)) { // other key typed. not tap. - debug("Tapping: End(No tap. Interfered by typing key).\n"); + debug("Tapping: End. No tap. Interfered by typing key: tapping_key={}\n"); process_action(&tapping_key); tapping_key = (keyrecord_t){}; @@ -654,15 +699,19 @@ static bool process_tapping(keyrecord_t *keyp) // other key events shall be stored till tapping state settles. return false; } - } else { + } + // tap_count > 0 + else { if (IS_TAPPING_KEY(event.key) && !event.pressed) { keyp->tap_count = tapping_key.tap_count; - debug("Tapping: tap release("); debug_dec(keyp->tap_count); debug(")\n"); + debug("Tapping: Tap release: tapping_key="); debug_record(*keyp); debug("\n"); tapping_key = *keyp; return false; } else if (is_tap_key(keyp->event.key) && event.pressed) { - debug("Tapping: Start with forcing to release last tap.\n"); + debug("Tapping: Start with forcing to release last tap: tapping_key="); + debug_record(*keyp); debug("\n"); +// TODO: need only when tap > 1? process_action(&(keyrecord_t){ .tap_count = tapping_key.tap_count, .event.key = tapping_key.event.key, @@ -673,7 +722,10 @@ static bool process_tapping(keyrecord_t *keyp) return false; } else { - if (!IS_NOEVENT(keyp->event)) debug("Tapping: key event while tap.\n"); + if (!IS_NOEVENT(keyp->event)) { + debug("Tapping: key event while tap: tapping_key="); + debug_record(tapping_key); debug("\n"); + } process_action(keyp); return true; } @@ -683,7 +735,7 @@ static bool process_tapping(keyrecord_t *keyp) else { if (tapping_key.tap_count == 0) { // timeout. not tap. - debug("Tapping: End. Not tap(time out).\n"); + debug("Tapping: End. Not tap(time out): tapping_key={}: "); debug_record(*keyp); debug("\n"); process_action(&tapping_key); tapping_key = (keyrecord_t){}; return false; @@ -731,6 +783,7 @@ static bool process_tapping(keyrecord_t *keyp) if (event.pressed && is_tap_key(event.key)) { debug("Tapping: Start(Press tap key).\n"); tapping_key = *keyp; + debug("tapping_key="); debug_record(*keyp); debug("\n"); return true; } else { process_action(keyp); diff --git a/common/debug.h b/common/debug.h index 648f0e096..e63d46f0e 100644 --- a/common/debug.h +++ b/common/debug.h @@ -36,6 +36,7 @@ along with this program. If not, see . #define debug_dec(data) do { if (debug_enable) print_dec(data); } while (0) #define debug_decs(data) do { if (debug_enable) print_decs(data); } while (0) +#define debug_hex4(data) do { if (debug_enable) print_hex4(data); } while (0) #define debug_hex8(data) do { if (debug_enable) print_hex8(data); } while (0) #define debug_hex16(data) do { if (debug_enable) print_hex16(data); } while (0) #define debug_hex32(data) do { if (debug_enable) print_hex32(data); } while (0) diff --git a/common/print.c b/common/print.c index d8a899b40..08d211f20 100644 --- a/common/print.c +++ b/common/print.c @@ -113,7 +113,6 @@ void print_decs(int16_t data) } -static inline void print_hex4(uint8_t data) { sendchar(data + ((data < 10) ? '0' : 'A' - 10)); @@ -137,8 +136,14 @@ void print_hex32(uint32_t data) print_hex16(data); } +void print_bin4(uint8_t data) +{ + for (int i = 4; i >= 0; i--) { + sendchar((data & (1<= 0; i--) { sendchar((data & (1< Date: Wed, 30 Jan 2013 20:31:32 +0900 Subject: Fix process_tapping(). --- common/action.c | 256 ++++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 166 insertions(+), 90 deletions(-) (limited to 'common/action.c') diff --git a/common/action.c b/common/action.c index e93a9afe3..28d9a95f5 100644 --- a/common/action.c +++ b/common/action.c @@ -26,43 +26,15 @@ along with this program. If not, see . #include "action.h" -static bool process_tapping(keyrecord_t *record); static void process_action(keyrecord_t *record); +static bool process_tapping(keyrecord_t *record); +static void waiting_buffer_scan_tap(void); -static void debug_event(keyevent_t event) -{ - debug_hex16(event.key.raw); - if (event.pressed) debug("d("); else debug("u("); - debug_dec(event.time); debug(")"); -} -static void debug_record(keyrecord_t record) -{ - debug_event(record.event); debug(":"); debug_dec(record.tap_count); -} -static void debug_action(action_t action) -{ - switch (action.kind.id) { - case ACT_LMODS: debug("ACT_LMODS"); break; - case ACT_RMODS: debug("ACT_RMODS"); break; - case ACT_LMODS_TAP: debug("ACT_LMODS_TAP"); break; - case ACT_RMODS_TAP: debug("ACT_RMODS_TAP"); break; - case ACT_USAGE: debug("ACT_USAGE"); break; - case ACT_MOUSEKEY: debug("ACT_MOUSEKEY"); break; - case ACT_LAYER_PRESSED: debug("ACT_LAYER_PRESSED"); break; - case ACT_LAYER_RELEASED: debug("ACT_LAYER_RELEASED"); break; - case ACT_LAYER_BIT: debug("ACT_LAYER_BIT"); break; - case ACT_LAYER_EXT: debug("ACT_LAYER_EXT"); break; - case ACT_MACRO: debug("ACT_MACRO"); break; - case ACT_COMMAND: debug("ACT_COMMAND"); break; - case ACT_FUNCTION: debug("ACT_FUNCTION"); break; - default: debug("UNKNOWN"); break; - } - debug("["); - debug_hex4(action.kind.param>>8); - debug(":"); - debug_hex8(action.kind.param & 0xff); - debug("]"); -} +static void debug_event(keyevent_t event); +static void debug_record(keyrecord_t record); +static void debug_action(action_t action); +static void debug_tapping_key(void); +static void debug_waiting_buffer(void); /* @@ -102,14 +74,6 @@ static uint8_t waiting_buffer_head = 0; /* point to the oldest data cell to deq */ static uint8_t waiting_buffer_tail = 0; -static void debug_waiting_buffer(void) -{ - debug("{ "); - for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) { - debug("["); debug_dec(i); debug("]="); debug_record(waiting_buffer[i]); debug(" "); - } - debug("}\n"); -} static bool waiting_buffer_enq(keyrecord_t record) { if (IS_NOEVENT(record.event)) { @@ -127,26 +91,14 @@ static bool waiting_buffer_enq(keyrecord_t record) debug("waiting_buffer_enq: "); debug_waiting_buffer(); return true; } -/* -static keyrecord_t waiting_buffer_deq(void) -{ - if (waiting_buffer_head == waiting_buffer_tail) { - return (keyrecord_t){}; - } - uint8_t last_tail = waiting_buffer_tail; - waiting_buffer_tail = waiting_buffer_tail + 1 % WAITING_BUFFER_SIZE; - return waiting_buffer[last_tail]; -} -static bool waiting_buffer_is_empty(void) -{ - return (waiting_buffer_head == waiting_buffer_tail); -} -*/ + static void waiting_buffer_clear(void) { waiting_buffer_head = 0; waiting_buffer_tail = 0; } + +#if TAPPING_TERM >= 500 static bool waiting_buffer_typed(keyevent_t event) { for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) { @@ -156,6 +108,8 @@ static bool waiting_buffer_typed(keyevent_t event) } return false; } +#endif + static bool waiting_buffer_has_anykey_pressed(void) { for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) { @@ -182,18 +136,21 @@ static struct { bool ready; bool disabled; } oneshot_state; + static void oneshot_start(uint8_t mods, uint16_t time) { oneshot_state.mods = mods; oneshot_state.time = time; oneshot_state.ready = true; } + static void oneshot_cancel(void) { oneshot_state.mods = 0; oneshot_state.time = 0; oneshot_state.ready = false; } + static void oneshot_toggle(void) { oneshot_state.disabled = !oneshot_state.disabled; @@ -219,6 +176,7 @@ void action_exec(keyevent_t event) // enqueue if (!waiting_buffer_enq(record)) { // clear all in case of overflow. + debug("OVERFLOW: CLEAR ALL STATES\n"); clear_keyboard(); waiting_buffer_clear(); tapping_key = (keyrecord_t){}; @@ -229,6 +187,7 @@ void action_exec(keyevent_t event) if (!IS_NOEVENT(event) && waiting_buffer_head != waiting_buffer_tail) { debug("---- action_exec: process waiting_buffer -----\n"); } + for (; waiting_buffer_tail != waiting_buffer_head; waiting_buffer_tail = (waiting_buffer_tail + 1) % WAITING_BUFFER_SIZE) { if (process_tapping(&waiting_buffer[waiting_buffer_tail])) { debug("processed: waiting_buffer["); debug_dec(waiting_buffer_tail); debug("] = "); @@ -666,10 +625,10 @@ static void process_action(keyrecord_t *record) /* Tapping * - * Rule: Tap key is typed(pressed and released) within TAPPING_TERM - * without interfaring by typing other key. + * Rule: Tap key is typed(pressed and released) within TAPPING_TERM. + * (without interfering by typing other key) */ -/* return true when key event is processed. */ +/* return true when key event is processed or consumed. */ static bool process_tapping(keyrecord_t *keyp) { keyevent_t event = keyp->event; @@ -680,74 +639,109 @@ static bool process_tapping(keyrecord_t *keyp) if (tapping_key.tap_count == 0) { if (IS_TAPPING_KEY(event.key) && !event.pressed) { // first tap! + debug("Tapping: First tap(0->1).\n"); tapping_key.tap_count = 1; - debug("Tapping: First tap: tapping_key="); debug_record(tapping_key); debug("\n"); + debug_tapping_key(); process_action(&tapping_key); // enqueue keyp->tap_count = tapping_key.tap_count; return false; - } else if (!event.pressed && waiting_buffer_typed(event)) { + } +#if TAPPING_TERM >= 500 + /* This can prevent from typing some tap keys in a row at a time. */ + else if (!event.pressed && waiting_buffer_typed(event)) { // other key typed. not tap. - debug("Tapping: End. No tap. Interfered by typing key: tapping_key={}\n"); + debug("Tapping: End. No tap. Interfered by typing key\n"); process_action(&tapping_key); tapping_key = (keyrecord_t){}; + debug_tapping_key(); // enqueue return false; - } else { - // other key events shall be stored till tapping state settles. + } +#endif + else { + // other key events shall be enq'd till tapping state settles. return false; } } // tap_count > 0 else { if (IS_TAPPING_KEY(event.key) && !event.pressed) { + debug("Tapping: Tap release("); debug_dec(tapping_key.tap_count); debug(")\n"); keyp->tap_count = tapping_key.tap_count; - debug("Tapping: Tap release: tapping_key="); debug_record(*keyp); debug("\n"); + process_action(keyp); tapping_key = *keyp; - return false; + debug_tapping_key(); + return true; } else if (is_tap_key(keyp->event.key) && event.pressed) { - debug("Tapping: Start with forcing to release last tap: tapping_key="); - debug_record(*keyp); debug("\n"); -// TODO: need only when tap > 1? - process_action(&(keyrecord_t){ - .tap_count = tapping_key.tap_count, - .event.key = tapping_key.event.key, - .event.time = event.time, - .event.pressed = false - }); + if (tapping_key.tap_count > 1) { + debug("Tapping: Start new tap with releasing last tap(>1).\n"); + // unregister key + process_action(&(keyrecord_t){ + .tap_count = tapping_key.tap_count, + .event.key = tapping_key.event.key, + .event.time = event.time, + .event.pressed = false + }); + } else { + debug("Tapping: Start while last tap(1).\n"); + } tapping_key = *keyp; - return false; + waiting_buffer_scan_tap(); + debug_tapping_key(); + return true; } else { if (!IS_NOEVENT(keyp->event)) { - debug("Tapping: key event while tap: tapping_key="); - debug_record(tapping_key); debug("\n"); + debug("Tapping: key event while last tap(>0).\n"); } process_action(keyp); return true; } } } - // not within TAPPING_TERM + // after TAPPING_TERM else { if (tapping_key.tap_count == 0) { - // timeout. not tap. - debug("Tapping: End. Not tap(time out): tapping_key={}: "); debug_record(*keyp); debug("\n"); + debug("Tapping: End. Timeout. Not tap(0): "); + debug_event(event); debug("\n"); process_action(&tapping_key); tapping_key = (keyrecord_t){}; + debug_tapping_key(); return false; } else { if (IS_TAPPING_KEY(event.key) && !event.pressed) { - debug("Tapping: End. tap release."); + debug("Tapping: End. last timeout tap release(>0)."); keyp->tap_count = tapping_key.tap_count; process_action(keyp); tapping_key = (keyrecord_t){}; return true; - } else { - // other key after tap time out. + } + else if (is_tap_key(keyp->event.key) && event.pressed) { + if (tapping_key.tap_count > 1) { + debug("Tapping: Start new tap with releasing last timeout tap(>1).\n"); + // unregister key + process_action(&(keyrecord_t){ + .tap_count = tapping_key.tap_count, + .event.key = tapping_key.event.key, + .event.time = event.time, + .event.pressed = false + }); + } else { + debug("Tapping: Start while last timeout tap(1).\n"); + } + tapping_key = *keyp; + waiting_buffer_scan_tap(); + debug_tapping_key(); + return true; + } + else { + if (!IS_NOEVENT(keyp->event)) { + debug("Tapping: key event while last timeout tap(>0).\n"); + } process_action(keyp); return true; } @@ -758,14 +752,17 @@ static bool process_tapping(keyrecord_t *keyp) if (tapping_key.tap_count > 0 && IS_TAPPING_KEY(event.key) && event.pressed) { // sequential tap. keyp->tap_count = tapping_key.tap_count + 1; - debug("Tapping: tap press("); debug_dec(keyp->tap_count); debug(")\n"); + debug("Tapping: Tap press("); debug_dec(keyp->tap_count); debug(")\n"); process_action(keyp); tapping_key = *keyp; + debug_tapping_key(); return true; } else if (event.pressed && is_tap_key(event.key)) { // Sequential tap can be interfered with other tap key. debug("Tapping: Start with interfering other tap.\n"); tapping_key = *keyp; + waiting_buffer_scan_tap(); + debug_tapping_key(); return true; } else { if (!IS_NOEVENT(keyp->event)) debug("Tapping: other key just after tap.\n"); @@ -774,16 +771,21 @@ static bool process_tapping(keyrecord_t *keyp) } } else { // timeout. no sequential tap. - debug("Tapping: End(Time out after releasing last tap).\n"); + debug("Tapping: End(Timeout after releasing last tap): "); + debug_event(event); debug("\n"); tapping_key = (keyrecord_t){}; + debug_tapping_key(); process_action(keyp); return true; } - } else { + } + // not tapping satate + else { if (event.pressed && is_tap_key(event.key)) { debug("Tapping: Start(Press tap key).\n"); tapping_key = *keyp; - debug("tapping_key="); debug_record(*keyp); debug("\n"); + waiting_buffer_scan_tap(); + debug_tapping_key(); return true; } else { process_action(keyp); @@ -792,6 +794,29 @@ static bool process_tapping(keyrecord_t *keyp) } } +/* scan buffer for tapping */ +static void waiting_buffer_scan_tap(void) +{ + // tapping already is settled + if (tapping_key.tap_count > 0) return; + // invalid state: tapping_key released && tap_count == 0 + if (!tapping_key.event.pressed) return; + + for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) { + if (IS_TAPPING_KEY(waiting_buffer[i].event.key) && + !waiting_buffer[i].event.pressed && + WITHIN_TAPPING_TERM(waiting_buffer[i].event)) { + tapping_key.tap_count = 1; + waiting_buffer[i].tap_count = 1; + process_action(&tapping_key); + + debug("waiting_buffer_scan_tap: found at ["); debug_dec(i); debug("]\n"); + debug_waiting_buffer(); + return; + } + } +} + /* @@ -923,3 +948,54 @@ bool is_tap_key(key_t key) } return false; } + + +/* + * debug print + */ +static void debug_event(keyevent_t event) +{ + debug_hex16(event.key.raw); + if (event.pressed) debug("d("); else debug("u("); + debug_dec(event.time); debug(")"); +} +static void debug_record(keyrecord_t record) +{ + debug_event(record.event); debug(":"); debug_dec(record.tap_count); +} +static void debug_action(action_t action) +{ + switch (action.kind.id) { + case ACT_LMODS: debug("ACT_LMODS"); break; + case ACT_RMODS: debug("ACT_RMODS"); break; + case ACT_LMODS_TAP: debug("ACT_LMODS_TAP"); break; + case ACT_RMODS_TAP: debug("ACT_RMODS_TAP"); break; + case ACT_USAGE: debug("ACT_USAGE"); break; + case ACT_MOUSEKEY: debug("ACT_MOUSEKEY"); break; + case ACT_LAYER_PRESSED: debug("ACT_LAYER_PRESSED"); break; + case ACT_LAYER_RELEASED: debug("ACT_LAYER_RELEASED"); break; + case ACT_LAYER_BIT: debug("ACT_LAYER_BIT"); break; + case ACT_LAYER_EXT: debug("ACT_LAYER_EXT"); break; + case ACT_MACRO: debug("ACT_MACRO"); break; + case ACT_COMMAND: debug("ACT_COMMAND"); break; + case ACT_FUNCTION: debug("ACT_FUNCTION"); break; + default: debug("UNKNOWN"); break; + } + debug("["); + debug_hex4(action.kind.param>>8); + debug(":"); + debug_hex8(action.kind.param & 0xff); + debug("]"); +} +static void debug_tapping_key(void) +{ + debug("TAPPING_KEY="); debug_record(tapping_key); debug("\n"); +} +static void debug_waiting_buffer(void) +{ + debug("{ "); + for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) { + debug("["); debug_dec(i); debug("]="); debug_record(waiting_buffer[i]); debug(" "); + } + debug("}\n"); +} -- cgit v1.2.3-70-g09d2 From 7e1093b70f424ed012a4122a91ba6bb1784b9eb8 Mon Sep 17 00:00:00 2001 From: tmk Date: Wed, 30 Jan 2013 22:47:16 +0900 Subject: Fix: action LAYER_BIT uses xor now instead of and/or. --- common/action.c | 21 +++++++++------------ common/action.h | 14 +++++++------- 2 files changed, 16 insertions(+), 19 deletions(-) (limited to 'common/action.c') diff --git a/common/action.c b/common/action.c index 28d9a95f5..eb8fc2ce2 100644 --- a/common/action.c +++ b/common/action.c @@ -452,9 +452,9 @@ static void process_action(keyrecord_t *record) switch (action.layer.code) { case 0x00: if (event.pressed) { - layer_switch(current_layer | action.layer.opt); + layer_switch(current_layer ^ action.layer.opt); } else { - layer_switch(current_layer & ~action.layer.opt); + layer_switch(current_layer ^ action.layer.opt); } break; case 0xF0: @@ -462,25 +462,22 @@ static void process_action(keyrecord_t *record) if (event.pressed) { if (tap_count < TAPPING_TOGGLE) { debug("LAYER_BIT: tap toggle(press).\n"); - layer_switch(current_layer | action.layer.opt); + layer_switch(current_layer ^ action.layer.opt); } } else { - if (tap_count < TAPPING_TOGGLE) { + if (tap_count <= TAPPING_TOGGLE) { debug("LAYER_BIT: tap toggle(release).\n"); - layer_switch(current_layer & ~action.layer.opt); - } else { - debug("LAYER_BIT: tap toggle.\n"); - layer_switch(current_layer | action.layer.opt); + layer_switch(current_layer ^ action.layer.opt); } } break; case 0xFF: // change default layer if (event.pressed) { - default_layer = current_layer | action.layer.opt; + default_layer = current_layer ^ action.layer.opt; layer_switch(default_layer); } else { - default_layer = current_layer & ~action.layer.opt; + default_layer = current_layer ^ action.layer.opt; layer_switch(default_layer); } break; @@ -492,7 +489,7 @@ static void process_action(keyrecord_t *record) register_code(action.layer.code); } else { debug("LAYER_BIT: No tap: layer_switch(bit on)\n"); - layer_switch(current_layer | action.layer.opt); + layer_switch(current_layer ^ action.layer.opt); } } else { if (IS_TAPPING_KEY(event.key) && tap_count > 0) { @@ -500,7 +497,7 @@ static void process_action(keyrecord_t *record) unregister_code(action.layer.code); } else { debug("LAYER_BIT: No tap: layer_switch(bit off)\n"); - layer_switch(current_layer & ~action.layer.opt); + layer_switch(current_layer ^ action.layer.opt); } } break; diff --git a/common/action.h b/common/action.h index ed3fff6c2..8600e4061 100644 --- a/common/action.h +++ b/common/action.h @@ -225,7 +225,6 @@ enum acion_param { /* action_t utility */ #define ACTION_NO 0 #define ACTION(kind, param) ((kind)<<12 | (param)) -#define MOD_BITS(mods) (((mods)>>4 | (mods)) & 0x0F) /* Key & Mods */ #define ACTION_KEY(key) ACTION(ACT_LMODS, key) @@ -235,15 +234,16 @@ enum acion_param { #define ACTION_RMODS_KEY(mods, key) ACTION(ACT_RMODS, (mods)<<8 | (key)) /* Mods + Tap key */ -#define ACTION_LMODS_TAP(mods, key) ACTION(ACT_LMODS_TAP, MOD_BITS(mods)<<8 | (key)) -#define ACTION_LMODS_ONESHOT(mods) ACTION(ACT_LMODS_TAP, MOD_BITS(mods)<<8 | ONE_SHOT) -#define ACTION_RMODS_TAP(mods, key) ACTION(ACT_RMODS_TAP, MOD_BITS(mods)<<8 | (key)) -#define ACTION_RMODS_ONESHOT(mods) ACTION(ACT_RMODS_TAP, MOD_BITS(mods)<<8 | ONE_SHOT) +#define MODS4(mods) (((mods)>>4 | (mods)) & 0x0F) +#define ACTION_LMODS_TAP(mods, key) ACTION(ACT_LMODS_TAP, MODS4(mods)<<8 | (key)) +#define ACTION_LMODS_ONESHOT(mods) ACTION(ACT_LMODS_TAP, MODS4(mods)<<8 | ONE_SHOT) +#define ACTION_RMODS_TAP(mods, key) ACTION(ACT_RMODS_TAP, MODS4(mods)<<8 | (key)) +#define ACTION_RMODS_ONESHOT(mods) ACTION(ACT_RMODS_TAP, MODS4(mods)<<8 | ONE_SHOT) /* Switch current layer */ #define ACTION_LAYER_SET_ON_PRESSED(layer) ACTION(ACT_LAYER_PRESSED, (layer)<<8 | 0x00) #define ACTION_LAYER_SET_ON_RELEASED(layer) ACTION(ACT_LAYER_RELEASED, (layer)<<8 | 0x00) -#define ACTION_LAYER_BIT(bits) ACTION(ACT_LAYER_BIT, (layer)<<8 | 0x00) +#define ACTION_LAYER_BIT(bits) ACTION(ACT_LAYER_BIT, (bits)<<8 | 0x00) #define ACTION_LAYER_TO_DEFAULT_ON_PRESSED ACTION(ACT_LAYER_EXT, 0x0<<8 | 0x00) #define ACTION_LAYER_TO_DEFAULT_ON_RELEASED ACTION(ACT_LAYER_EXT, 0x1<<8 | 0x00) /* Switch default layer */ @@ -254,7 +254,7 @@ enum acion_param { #define ACTION_LAYER_DEFAULT_SET_CURRENT_ON_RELEASED ACTION(ACT_LAYER_EXT, 0x1<<8 | 0xFF) /* Layer switch with tap key */ #define ACTION_LAYER_SET_TAP_KEY(layer, key) ACTION(ACT_LAYER_PRESSED, (layer)<<8 | (key)) -#define ACTION_LAYER_BIT_TAP_KEY(bits, key) ACTION(ACT_LAYER_BIT, (layer)<<8 | (key)) +#define ACTION_LAYER_BIT_TAP_KEY(bits, key) ACTION(ACT_LAYER_BIT, (bits)<<8 | (key)) #define ACTION_LAYER_DEFAULT_SET_TAP_KEY(key) ACTION(ACT_LAYER_EXT, 0x0<<8 | (key)) /* with tap toggle */ #define ACTION_LAYER_SET_ON_PRESSED_TAP_TOGGLE(layer) ACTION(ACT_LAYER_PRESSED, (layer)<<8 | 0xF0) -- cgit v1.2.3-70-g09d2 From e760953910a51e5972dda21aa9d03279e6f1230b Mon Sep 17 00:00:00 2001 From: tmk Date: Thu, 31 Jan 2013 15:07:01 +0900 Subject: Fix tapping. tap key just after tap timeout. --- common/action.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'common/action.c') diff --git a/common/action.c b/common/action.c index eb8fc2ce2..4838ea062 100644 --- a/common/action.c +++ b/common/action.c @@ -772,8 +772,7 @@ static bool process_tapping(keyrecord_t *keyp) debug_event(event); debug("\n"); tapping_key = (keyrecord_t){}; debug_tapping_key(); - process_action(keyp); - return true; + return false; } } // not tapping satate -- cgit v1.2.3-70-g09d2 From d95463f2e0369dc0e28497bb923b3012fb09e900 Mon Sep 17 00:00:00 2001 From: tmk Date: Thu, 31 Jan 2013 17:50:53 +0900 Subject: Add legacy keymap support. --- common/action.c | 9 +++++- common/action.h | 87 +++++++++++++++++++++++++++++++++++++------------------- common/keycode.h | 4 +-- common/keymap.c | 73 +++++++++++++++++++++++++++++++++++++++++++++++ common/keymap.h | 16 +++++++---- 5 files changed, 151 insertions(+), 38 deletions(-) create mode 100644 common/keymap.c (limited to 'common/action.c') diff --git a/common/action.c b/common/action.c index 4838ea062..d1f493fe0 100644 --- a/common/action.c +++ b/common/action.c @@ -358,6 +358,10 @@ static void process_action(keyrecord_t *record) if (event.pressed) { layer_switch(action.layer.opt); } +//TODO: this is ok? + else { + layer_switch(default_layer); + } break; case 0xF0: // tap toggle @@ -394,7 +398,10 @@ static void process_action(keyrecord_t *record) debug("LAYER_PRESSED: Tap: unregister_code\n"); unregister_code(action.layer.code); } else { - debug("LAYER_PRESSED: No tap: NO ACTION\n"); + //debug("LAYER_PRESSED: No tap: NO ACTION\n"); +//TODO: this is ok? + debug("LAYER_PRESSED: No tap: return to default layer\n"); + layer_switch(default_layer); } } break; diff --git a/common/action.h b/common/action.h index 8600e4061..d6530df42 100644 --- a/common/action.h +++ b/common/action.h @@ -18,6 +18,7 @@ along with this program. If not, see . #define ACTION_H #include "keyboard.h" +#include "keycode.h" /* Action struct. @@ -107,28 +108,28 @@ Keyboard Keys ACT_LMODS(0000): 0000|0000|000000|00 No action 0000|0000| keycode Key -0010|mods|000000|00 Left mods Momentary -0000|mods| keycode Key+Left mods +0000|mods|000000|00 Left mods +0000|mods| keycode Key & Left mods ACT_RMODS(0001): 0001|0000|000000|00 No action 0001|0000| keycode Key(no used) -0001|mods|000000|00 Right mods Momentary -0001|mods| keycode Key+Right mods +0001|mods|000000|00 Right mods +0001|mods| keycode Key & Right mods ACT_LMODS_TAP(0010): 0010|mods|000000|00 Left mods OneShot 0010|mods|000000|01 (reserved) 0010|mods|000000|10 (reserved) 0010|mods|000000|11 (reserved) -0010|mods| keycode Left mods+tap Key +0010|mods| keycode Left mods + tap Key ACT_RMODS_TAP(0011): 0011|mods|000000|00 Right mods OneShot 0011|mods|000000|01 (reserved) 0011|mods|000000|10 (reserved) 0011|mods|000000|11 (reserved) -0011|mods| keycode Right mods+tap Key +0011|mods| keycode Right mods + tap Key Other HID Usage @@ -143,12 +144,20 @@ ACT_USAGE(0100): Mouse Keys ---------- +TODO: can be combined with 'Other HID Usage'? to save action kind id. ACT_MOUSEKEY(0110): 0101|XXXX| keycode Mouse key Layer Actions ------------- +TODO: reconsider layer methods. +1 momemtary + tap key up: L, down: default +1 bitwise + tap key up: xor B, down: xor B +3 momemtary go + tap key? up: X, down: +3 toggle(mementary back) + tap key? up: down: Y +3 no tap up: X, down: Y + ACT_LAYER_PRESSED(1000): Set layer on key pressed ACT_LAYER_RELEASED(1001): Set layer on key released ACT_LAYER_BIT(1010): On/Off layer bit @@ -222,57 +231,77 @@ enum acion_param { }; -/* action_t utility */ +/* action utility */ #define ACTION_NO 0 #define ACTION(kind, param) ((kind)<<12 | (param)) +#define MODS4(mods) (((mods)>>4 | (mods)) & 0x0F) -/* Key & Mods */ +/* Key */ #define ACTION_KEY(key) ACTION(ACT_LMODS, key) +/* Mods & key */ #define ACTION_LMODS(mods) ACTION(ACT_LMODS, (mods)<<8 | 0x00) #define ACTION_LMODS_KEY(mods, key) ACTION(ACT_LMODS, (mods)<<8 | (key)) #define ACTION_RMODS(mods) ACTION(ACT_RMODS, (mods)<<8 | 0x00) #define ACTION_RMODS_KEY(mods, key) ACTION(ACT_RMODS, (mods)<<8 | (key)) +/* Mod & key */ +#define ACTION_LMOD(mod) ACTION(ACT_LMODS, MODS4(MOD_BIT(mod))<<8 | 0x00) +#define ACTION_LMOD_KEY(mod, key) ACTION(ACT_LMODS, MODS4(MOD_BIT(mod))<<8 | (key)) +#define ACTION_RMOD(mod) ACTION(ACT_RMODS, MODS4(MOD_BIT(mod))<<8 | 0x00) +#define ACTION_RMOD_KEY(mod, key) ACTION(ACT_RMODS, MODS4(MOD_BIT(mod))<<8 | (key)) /* Mods + Tap key */ -#define MODS4(mods) (((mods)>>4 | (mods)) & 0x0F) -#define ACTION_LMODS_TAP(mods, key) ACTION(ACT_LMODS_TAP, MODS4(mods)<<8 | (key)) +#define ACTION_LMODS_TAP_KEY(mods, key) ACTION(ACT_LMODS_TAP, MODS4(mods)<<8 | (key)) #define ACTION_LMODS_ONESHOT(mods) ACTION(ACT_LMODS_TAP, MODS4(mods)<<8 | ONE_SHOT) -#define ACTION_RMODS_TAP(mods, key) ACTION(ACT_RMODS_TAP, MODS4(mods)<<8 | (key)) +#define ACTION_RMODS_TAP_KEY(mods, key) ACTION(ACT_RMODS_TAP, MODS4(mods)<<8 | (key)) #define ACTION_RMODS_ONESHOT(mods) ACTION(ACT_RMODS_TAP, MODS4(mods)<<8 | ONE_SHOT) +/* Mod + Tap key */ +#define ACTION_LMOD_TAP_KEY(mod, key) ACTION(ACT_LMODS_TAP, MODS4(MOD_BIT(mod))<<8 | (key)) +#define ACTION_LMOD_ONESHOT(mod) ACTION(ACT_LMODS_TAP, MODS4(MOD_BIT(mod))<<8 | ONE_SHOT) +#define ACTION_RMOD_TAP_KEY(mod, key) ACTION(ACT_RMODS_TAP, MODS4(MOD_BIT(mod))<<8 | (key)) +#define ACTION_RMOD_ONESHOT(mod) ACTION(ACT_RMODS_TAP, MODS4(MOD_BIT(mod))<<8 | ONE_SHOT) +// TODO: contemplate about layer action /* Switch current layer */ -#define ACTION_LAYER_SET_ON_PRESSED(layer) ACTION(ACT_LAYER_PRESSED, (layer)<<8 | 0x00) -#define ACTION_LAYER_SET_ON_RELEASED(layer) ACTION(ACT_LAYER_RELEASED, (layer)<<8 | 0x00) -#define ACTION_LAYER_BIT(bits) ACTION(ACT_LAYER_BIT, (bits)<<8 | 0x00) -#define ACTION_LAYER_TO_DEFAULT_ON_PRESSED ACTION(ACT_LAYER_EXT, 0x0<<8 | 0x00) -#define ACTION_LAYER_TO_DEFAULT_ON_RELEASED ACTION(ACT_LAYER_EXT, 0x1<<8 | 0x00) +#define ACTION_LAYER_SET(layer) ACTION(ACT_LAYER_PRESSED, (layer)<<8 | 0x00) +#define ACTION_LAYER_SET_ON_PRESSED(layer) ACTION(ACT_LAYER_PRESSED, (layer)<<8 | 0x00) +#define ACTION_LAYER_SET_ON_RELEASED(layer) ACTION(ACT_LAYER_RELEASED, (layer)<<8 | 0x00) +#define ACTION_LAYER_BIT(bits) ACTION(ACT_LAYER_BIT, (bits)<<8 | 0x00) +#define ACTION_LAYER_SET_DEFAULT ACTION(ACT_LAYER_EXT, 0x0<<8 | 0x00) +#define ACTION_LAYER_RETURN_DEFAULT ACTION(ACT_LAYER_EXT, 0x1<<8 | 0x00) +#define ACTION_LAYER_SET_DEFAULT_ON_PRESSED ACTION(ACT_LAYER_EXT, 0x0<<8 | 0x00) +#define ACTION_LAYER_SET_DEFAULT_ON_RELEASED ACTION(ACT_LAYER_EXT, 0x1<<8 | 0x00) /* Switch default layer */ -#define ACTION_LAYER_DEFAULT_SET_ON_PRESSED(layer) ACTION(ACT_LAYER_PRESSED, (layer)<<8 | 0xFF) -#define ACTION_LAYER_DEFAULT_SET_ON_RELEASED(layer) ACTION(ACT_LAYER_RELEASED, (layer)<<8 | 0xFF) -#define ACTION_LAYER_DEFAULT_BIT(bits) ACTION(ACT_LAYER_BIT, (bits)<<8 | 0xFF) -#define ACTION_LAYER_DEFAULT_SET_CURRENT_ON_PRESSED ACTION(ACT_LAYER_EXT, 0x0<<8 | 0xFF) -#define ACTION_LAYER_DEFAULT_SET_CURRENT_ON_RELEASED ACTION(ACT_LAYER_EXT, 0x1<<8 | 0xFF) +#define ACTION_LAYER_DEFAULT_SET(layer) ACTION(ACT_LAYER_PRESSED, (layer)<<8 | 0xFF) +#define ACTION_LAYER_DEFAULT_SET_ON_PRESSED(layer) ACTION(ACT_LAYER_PRESSED, (layer)<<8 | 0xFF) +#define ACTION_LAYER_DEFAULT_SET_ON_RELEASED(layer) ACTION(ACT_LAYER_RELEASED, (layer)<<8 | 0xFF) +#define ACTION_LAYER_DEFAULT_BIT(bits) ACTION(ACT_LAYER_BIT, (bits)<<8 | 0xFF) +#define ACTION_LAYER_DEFAULT_SET_CURRENT_ON_PRESSED ACTION(ACT_LAYER_EXT, 0x0<<8 | 0xFF) +#define ACTION_LAYER_DEFAULT_SET_CURRENT_ON_RELEASED ACTION(ACT_LAYER_EXT, 0x1<<8 | 0xFF) /* Layer switch with tap key */ -#define ACTION_LAYER_SET_TAP_KEY(layer, key) ACTION(ACT_LAYER_PRESSED, (layer)<<8 | (key)) -#define ACTION_LAYER_BIT_TAP_KEY(bits, key) ACTION(ACT_LAYER_BIT, (bits)<<8 | (key)) -#define ACTION_LAYER_DEFAULT_SET_TAP_KEY(key) ACTION(ACT_LAYER_EXT, 0x0<<8 | (key)) -/* with tap toggle */ -#define ACTION_LAYER_SET_ON_PRESSED_TAP_TOGGLE(layer) ACTION(ACT_LAYER_PRESSED, (layer)<<8 | 0xF0) -#define ACTION_LAYER_SET_ON_RELEASED_TAP_TOGGLE(layer) ACTION(ACT_LAYER_RELEASED, (layer)<<8 | 0xF0) -#define ACTION_LAYER_BIT_TAP_TOGGLE(layer) ACTION(ACT_LAYER_BIT, (layer)<<8 | 0xF0) -#define ACTION_LAYER_DEFAULT_TAP_TOGGLE ACTION(ACT_LAYER_EXT, 0x0<<8 | 0xF0) +#define ACTION_LAYER_SET_TAP_KEY(layer, key) ACTION(ACT_LAYER_PRESSED, (layer)<<8 | (key)) +#define ACTION_LAYER_BIT_TAP_KEY(bits, key) ACTION(ACT_LAYER_BIT, (bits)<<8 | (key)) +#define ACTION_LAYER_DEFAULT_SET_TAP_KEY(key) ACTION(ACT_LAYER_EXT, 0x0<<8 | (key)) +/* Layer switch with tap toggle */ +#define ACTION_LAYER_SET_ON_PRESSED_TAP_TOGGLE(layer) ACTION(ACT_LAYER_PRESSED, (layer)<<8 | 0xF0) +#define ACTION_LAYER_SET_ON_RELEASED_TAP_TOGGLE(layer) ACTION(ACT_LAYER_RELEASED, (layer)<<8 | 0xF0) +#define ACTION_LAYER_BIT_TAP_TOGGLE(layer) ACTION(ACT_LAYER_BIT, (layer)<<8 | 0xF0) +#define ACTION_LAYER_DEFAULT_TAP_TOGGLE ACTION(ACT_LAYER_EXT, 0x0<<8 | 0xF0) /* HID Usage */ #define ACTION_USAGE_PAGE_SYSTEM 0 #define ACTION_USAGE_PAGE_CONSUMER 1 #define ACTION_USAGE_SYSTEM(id) ACTION(ACT_USAGE, ACTION_USAGE_PAGE_SYSTEM<<10 | (id)) #define ACTION_USAGE_CONSUMER(id) ACTION(ACT_USAGE, ACTION_USAGE_PAGE_CONSUMER<<10 | (id)) + /* Mousekey */ #define ACTION_MOUSEKEY(key) ACTION(ACT_MOUSEKEY, key) + /* Macro */ #define ACTION_MACRO(opt, id) ACTION(ACT_FUNCTION, (opt)<<8 | (addr)) + /* Command */ #define ACTION_COMMAND(opt, id) ACTION(ACT_COMMAND, (opt)<<8 | (addr)) + /* Function */ #define ACTION_FUNCTION(id, opt) ACTION(ACT_FUNCTION, (opt)<<8 | id) diff --git a/common/keycode.h b/common/keycode.h index cdd1e9758..341f23161 100644 --- a/common/keycode.h +++ b/common/keycode.h @@ -28,14 +28,14 @@ along with this program. If not, see . #define IS_KEY(code) (KC_A <= (code) && (code) <= KC_EXSEL) #define IS_MOD(code) (KC_LCTRL <= (code) && (code) <= KC_RGUI) -#define IS_FN(code) (KC_FN0 <= (code) && (code) <= KC_FN7) +#define IS_FN(code) (KC_FN0 <= (code) && (code) <= KC_FN31) #define IS_MOUSEKEY(code) (KC_MS_UP <= (code) && (code) <= KC_MS_ACCEL2) #define IS_MOUSEKEY_MOVE(code) (KC_MS_UP <= (code) && (code) <= KC_MS_RIGHT) #define IS_MOUSEKEY_BUTTON(code) (KC_MS_BTN1 <= (code) && (code) <= KC_MS_BTN5) #define IS_MOUSEKEY_WHEEL(code) (KC_MS_WH_UP <= (code) && (code) <= KC_MS_WH_RIGHT) #define IS_MOUSEKEY_ACCEL(code) (KC_MS_ACCEL0 <= (code) && (code) <= KC_MS_ACCEL2) -#define IS_SPECIAL(code) ((0xB0 <= (code) && (code) <= 0xDF) || (0xE8 <= (code) && (code) <= 0xFF)) +#define IS_SPECIAL(code) ((0xA5 <= (code) && (code) <= 0xDF) || (0xE8 <= (code) && (code) <= 0xFF)) #define IS_CONSUMER(code) (KC_MUTE <= (code) && (code) <= KC_WFAV) #define IS_SYSTEM(code) (KC_POWER <= (code) && (code) <= KC_WAKE) diff --git a/common/keymap.c b/common/keymap.c new file mode 100644 index 000000000..415121308 --- /dev/null +++ b/common/keymap.c @@ -0,0 +1,73 @@ +/* +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 "keymap.h" +#include "report.h" +#include "keycode.h" + + +/* layer */ +uint8_t default_layer = 0; +uint8_t current_layer = 0; + + +#ifndef NO_LEGACY_KEYMAP_SUPPORT +/* legacy support with weak reference */ +__attribute__ ((weak)) +action_t keymap_get_action(uint8_t layer, uint8_t row, uint8_t col) +{ + /* convert from legacy keycode to action */ + uint8_t key = keymap_get_keycode(layer, row, col); + action_t action; + switch (key) { + case KC_A ... KC_EXSEL: + action.code = ACTION_KEY(key); + break; + case KC_LCTRL ... KC_LGUI: + action.code = ACTION_LMOD(key); + break; + case KC_RCTRL ... KC_RGUI: + action.code = ACTION_RMOD(key); + break; + case KC_SYSTEM_POWER ... KC_SYSTEM_WAKE: + action.code = ACTION_USAGE_SYSTEM(KEYCODE2SYSTEM(key)); + break; + case KC_AUDIO_MUTE ... KC_WWW_FAVORITES: + action.code = ACTION_USAGE_CONSUMER(KEYCODE2CONSUMER(key)); + break; + case KC_MS_UP ... KC_MS_ACCEL2: + action.code = ACTION_MOUSEKEY(key); + break; + case KC_FN0 ... KC_FN31: + { + uint8_t layer = keymap_fn_layer(FN_INDEX(key)); + uint8_t code = keymap_fn_keycode(FN_INDEX(key)); + action.code = ACTION_LAYER_SET_TAP_KEY(layer, code); + } + break; + case KC_NO ... KC_UNDEFINED: + default: + action.code = ACTION_NO; + break; + } + return action; +} +#endif + +__attribute__ ((weak)) +void action_call_function(keyevent_t event, uint8_t id) +{ +} diff --git a/common/keymap.h b/common/keymap.h index f54fea90d..748761551 100644 --- a/common/keymap.h +++ b/common/keymap.h @@ -28,18 +28,22 @@ extern uint8_t current_layer; /* layer to return or start with */ extern uint8_t default_layer; + /* - * legacy keymap interface: keycode + * new keymap interface: action */ +action_t keymap_get_action(uint8_t layer, uint8_t row, uint8_t col); + + +#ifndef NO_LEGACY_KEYMAP_SUPPORT +/* keycode of key */ uint8_t keymap_get_keycode(uint8_t layer, uint8_t row, uint8_t col); + /* layer to move during press Fn key */ uint8_t keymap_fn_layer(uint8_t fn_bits); + /* keycode to send when release Fn key without using */ uint8_t keymap_fn_keycode(uint8_t fn_bits); - -/* - * new keymap interface: action - */ -action_t keymap_get_action(uint8_t layer, uint8_t row, uint8_t col); +#endif #endif -- cgit v1.2.3-70-g09d2 From 1d7962ba8a20323dc13cc913381608e117afaeb4 Mon Sep 17 00:00:00 2001 From: tmk Date: Fri, 1 Feb 2013 14:48:11 +0900 Subject: Add user defined function to action. --- common/action.c | 46 +++++++------- common/action.h | 47 +++++++------- common/keyboard.h | 1 + common/keymap.c | 2 +- common/keymap.h | 8 ++- keyboard/hhkb/keymap.c | 163 ++++++++++++++++++++++++++++++++++--------------- 6 files changed, 167 insertions(+), 100 deletions(-) (limited to 'common/action.c') diff --git a/common/action.c b/common/action.c index d1f493fe0..cb44e272a 100644 --- a/common/action.c +++ b/common/action.c @@ -110,7 +110,7 @@ static bool waiting_buffer_typed(keyevent_t event) } #endif -static bool waiting_buffer_has_anykey_pressed(void) +bool waiting_buffer_has_anykey_pressed(void) { for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) { if (waiting_buffer[i].event.pressed) return true; @@ -256,7 +256,7 @@ static void process_action(keyrecord_t *record) debug("MODS_TAP: Oneshot: start\n"); oneshot_start(mods, event.time); } - else if (tap_count == 5) { + else if (tap_count == TAPPING_TOGGLE) { debug("MODS_TAP: Oneshot: toggle\n"); oneshot_toggle(); } @@ -356,7 +356,7 @@ static void process_action(keyrecord_t *record) switch (action.layer.code) { case 0x00: if (event.pressed) { - layer_switch(action.layer.opt); + layer_switch(action.layer.val); } //TODO: this is ok? else { @@ -367,19 +367,19 @@ static void process_action(keyrecord_t *record) // tap toggle if (event.pressed) { if (tap_count < TAPPING_TOGGLE) { - layer_switch(action.layer.opt); + layer_switch(action.layer.val); } } else { if (tap_count >= TAPPING_TOGGLE) { debug("LAYER_PRESSED: tap toggle.\n"); - layer_switch(action.layer.opt); + layer_switch(action.layer.val); } } break; case 0xFF: // change default layer if (event.pressed) { - default_layer = action.layer.opt; + default_layer = action.layer.val; layer_switch(default_layer); } break; @@ -391,7 +391,7 @@ static void process_action(keyrecord_t *record) register_code(action.layer.code); } else { debug("LAYER_PRESSED: No tap: layer_switch\n"); - layer_switch(action.layer.opt); + layer_switch(action.layer.val); } } else { if (tap_count > 0) { @@ -411,7 +411,7 @@ static void process_action(keyrecord_t *record) switch (action.layer.code) { case 0x00: if (!event.pressed) { - layer_switch(action.layer.opt); + layer_switch(action.layer.val); } break; case 0xF0: @@ -419,18 +419,18 @@ static void process_action(keyrecord_t *record) if (event.pressed) { if (tap_count >= TAPPING_TOGGLE) { debug("LAYER_RELEASED: tap toggle.\n"); - layer_switch(action.layer.opt); + layer_switch(action.layer.val); } } else { if (tap_count < TAPPING_TOGGLE) { - layer_switch(action.layer.opt); + layer_switch(action.layer.val); } } break; case 0xFF: // change default layer if (!event.pressed) { - default_layer = action.layer.opt; + default_layer = action.layer.val; layer_switch(default_layer); } break; @@ -449,7 +449,7 @@ static void process_action(keyrecord_t *record) unregister_code(action.layer.code); } else { debug("LAYER_RELEASED: No tap: layer_switch\n"); - layer_switch(action.layer.opt); + layer_switch(action.layer.val); } } break; @@ -459,9 +459,9 @@ static void process_action(keyrecord_t *record) switch (action.layer.code) { case 0x00: if (event.pressed) { - layer_switch(current_layer ^ action.layer.opt); + layer_switch(current_layer ^ action.layer.val); } else { - layer_switch(current_layer ^ action.layer.opt); + layer_switch(current_layer ^ action.layer.val); } break; case 0xF0: @@ -469,22 +469,22 @@ static void process_action(keyrecord_t *record) if (event.pressed) { if (tap_count < TAPPING_TOGGLE) { debug("LAYER_BIT: tap toggle(press).\n"); - layer_switch(current_layer ^ action.layer.opt); + layer_switch(current_layer ^ action.layer.val); } } else { if (tap_count <= TAPPING_TOGGLE) { debug("LAYER_BIT: tap toggle(release).\n"); - layer_switch(current_layer ^ action.layer.opt); + layer_switch(current_layer ^ action.layer.val); } } break; case 0xFF: // change default layer if (event.pressed) { - default_layer = current_layer ^ action.layer.opt; + default_layer = current_layer ^ action.layer.val; layer_switch(default_layer); } else { - default_layer = current_layer ^ action.layer.opt; + default_layer = current_layer ^ action.layer.val; layer_switch(default_layer); } break; @@ -496,7 +496,7 @@ static void process_action(keyrecord_t *record) register_code(action.layer.code); } else { debug("LAYER_BIT: No tap: layer_switch(bit on)\n"); - layer_switch(current_layer ^ action.layer.opt); + layer_switch(current_layer ^ action.layer.val); } } else { if (IS_TAPPING_KEY(event.key) && tap_count > 0) { @@ -504,14 +504,14 @@ static void process_action(keyrecord_t *record) unregister_code(action.layer.code); } else { debug("LAYER_BIT: No tap: layer_switch(bit off)\n"); - layer_switch(current_layer ^ action.layer.opt); + layer_switch(current_layer ^ action.layer.val); } } break; } break; case ACT_LAYER_EXT: - switch (action.layer.opt) { + switch (action.layer.val) { case 0x00: // set default layer when pressed switch (action.layer.code) { @@ -620,7 +620,7 @@ static void process_action(keyrecord_t *record) break; case ACT_FUNCTION: // TODO - action_call_function(event, action.func.id); + keymap_call_function(record, action.func.id); break; default: break; @@ -944,7 +944,7 @@ bool is_tap_key(key_t key) } return false; case ACT_FUNCTION: - if (action.func.opt & 0x1) { + if (action.func.opt & O_TAP) { return true; } return false; diff --git a/common/action.h b/common/action.h index d6530df42..b657aa540 100644 --- a/common/action.h +++ b/common/action.h @@ -21,6 +21,16 @@ along with this program. If not, see . #include "keycode.h" +/* Execute action per keyevent */ +void action_exec(keyevent_t event); + + +/* Struct to record event and tap count */ +typedef struct { + keyevent_t event; + uint8_t tap_count; +} keyrecord_t; + /* Action struct. * * In avr-gcc bit field seems to be assigned from LSB(bit0) to MSB(bit15). @@ -48,7 +58,7 @@ typedef union { } key; struct action_layer { uint16_t code :8; - uint16_t opt :4; + uint16_t val :4; uint16_t kind :4; } layer; struct action_usage { @@ -58,7 +68,7 @@ typedef union { } usage; struct action_command { uint16_t id :8; - uint16_t option :4; + uint16_t opt :4; uint16_t kind :4; } command; struct action_function { @@ -68,18 +78,6 @@ typedef union { } func; } action_t; -/* Struct to record action and tap count */ -typedef struct { - keyevent_t event; - uint8_t tap_count; -} keyrecord_t; - - -/* execute action per keyevent */ -void action_exec(keyevent_t event); -typedef void (*action_func_t)(keyevent_t event, uint8_t opt); // TODO:no need? -void action_call_function(keyevent_t event, uint8_t id); // TODO: action function - /* * Utilities for actions. @@ -94,6 +92,7 @@ void clear_keyboard_but_mods(void); bool sending_anykey(void); void layer_switch(uint8_t new_layer); bool is_tap_key(key_t key); +bool waiting_buffer_has_anykey_pressed(void); @@ -203,9 +202,6 @@ ACT_FUNCTION(1111): 1111| address(12) Function? 1111|opt | id(8) Function? -TODO: modifier + function by tap? - for example: LShift + '('[Shift+9] and RShift + ')'[Shift+0] - http://deskthority.net/workshop-f7/tmk-keyboard-firmware-collection-t4478.html#p90052 */ enum action_kind_id { ACT_LMODS = 0b0000, @@ -226,8 +222,12 @@ enum action_kind_id { ACT_FUNCTION = 0b1111 }; -enum acion_param { - ONE_SHOT = 0x00, +enum params { + P_ONESHOT = 0x00, +}; + +enum options { + O_TAP = 0x8, }; @@ -251,14 +251,14 @@ enum acion_param { /* Mods + Tap key */ #define ACTION_LMODS_TAP_KEY(mods, key) ACTION(ACT_LMODS_TAP, MODS4(mods)<<8 | (key)) -#define ACTION_LMODS_ONESHOT(mods) ACTION(ACT_LMODS_TAP, MODS4(mods)<<8 | ONE_SHOT) +#define ACTION_LMODS_ONESHOT(mods) ACTION(ACT_LMODS_TAP, MODS4(mods)<<8 | P_ONESHOT) #define ACTION_RMODS_TAP_KEY(mods, key) ACTION(ACT_RMODS_TAP, MODS4(mods)<<8 | (key)) -#define ACTION_RMODS_ONESHOT(mods) ACTION(ACT_RMODS_TAP, MODS4(mods)<<8 | ONE_SHOT) +#define ACTION_RMODS_ONESHOT(mods) ACTION(ACT_RMODS_TAP, MODS4(mods)<<8 | P_ONESHOT) /* Mod + Tap key */ #define ACTION_LMOD_TAP_KEY(mod, key) ACTION(ACT_LMODS_TAP, MODS4(MOD_BIT(mod))<<8 | (key)) -#define ACTION_LMOD_ONESHOT(mod) ACTION(ACT_LMODS_TAP, MODS4(MOD_BIT(mod))<<8 | ONE_SHOT) +#define ACTION_LMOD_ONESHOT(mod) ACTION(ACT_LMODS_TAP, MODS4(MOD_BIT(mod))<<8 | P_ONESHOT) #define ACTION_RMOD_TAP_KEY(mod, key) ACTION(ACT_RMODS_TAP, MODS4(MOD_BIT(mod))<<8 | (key)) -#define ACTION_RMOD_ONESHOT(mod) ACTION(ACT_RMODS_TAP, MODS4(MOD_BIT(mod))<<8 | ONE_SHOT) +#define ACTION_RMOD_ONESHOT(mod) ACTION(ACT_RMODS_TAP, MODS4(MOD_BIT(mod))<<8 | P_ONESHOT) // TODO: contemplate about layer action /* Switch current layer */ @@ -304,5 +304,6 @@ enum acion_param { /* Function */ #define ACTION_FUNCTION(id, opt) ACTION(ACT_FUNCTION, (opt)<<8 | id) +#define ACTION_FUNCTION_TAP(id) ACTION(ACT_FUNCTION, O_TAP<<8 | id) #endif /* ACTION_H */ diff --git a/common/keyboard.h b/common/keyboard.h index 32c1bf464..e1cab3119 100644 --- a/common/keyboard.h +++ b/common/keyboard.h @@ -32,6 +32,7 @@ typedef struct { uint8_t row; } keypos_t; +// TODO: need raw? keypos_t -> key_t? typedef union { uint16_t raw; keypos_t pos; diff --git a/common/keymap.c b/common/keymap.c index 415121308..40d20f684 100644 --- a/common/keymap.c +++ b/common/keymap.c @@ -68,6 +68,6 @@ action_t keymap_get_action(uint8_t layer, uint8_t row, uint8_t col) #endif __attribute__ ((weak)) -void action_call_function(keyevent_t event, uint8_t id) +void keymap_call_function(keyrecord_t *event, uint8_t id) { } diff --git a/common/keymap.h b/common/keymap.h index 748761551..e0fafeaf2 100644 --- a/common/keymap.h +++ b/common/keymap.h @@ -29,11 +29,13 @@ extern uint8_t current_layer; extern uint8_t default_layer; -/* - * new keymap interface: action - */ +/* action for key */ +// TODO: should use struct key_t? action_t keymap_get_action(uint8_t layer, uint8_t row, uint8_t col); +/* user defined special function */ +void keymap_call_function(keyrecord_t *record, uint8_t id); + #ifndef NO_LEGACY_KEYMAP_SUPPORT /* keycode of key */ diff --git a/keyboard/hhkb/keymap.c b/keyboard/hhkb/keymap.c index c7e4cfc38..e11b4563a 100644 --- a/keyboard/hhkb/keymap.c +++ b/keyboard/hhkb/keymap.c @@ -51,23 +51,6 @@ along with this program. If not, see . } -static const uint16_t PROGMEM fn_actions[] = { - ACTION_LAYER_TO_DEFAULT_ON_RELEASED, // Fn0 - ACTION_LAYER_SET_ON_PRESSED(1), // Fn1 - ACTION_LAYER_SET_TAP_KEY(2, KC_SLASH), // Fn2 - ACTION_LAYER_SET_TAP_KEY(3, KC_SCLN), // Fn3 - //ACTION_LAYER_SET_ON_PRESSED(3), // Fn4 - ACTION_FUNCTION(0x01, 0xA), // Fn4 - ACTION_LAYER_SET_TAP_KEY(5, KC_SPC), // Fn5 - ACTION_LMODS_TAP(MOD_BIT(KC_LCTL), KC_BSPC), // Fn6 - ACTION_RMODS_TAP(MOD_BIT(KC_RCTL), KC_ENT), // Fn7 - - ACTION_LMODS_TAP(MOD_BIT(KC_LSFT), ONE_SHOT), // Fn8 - ACTION_LAYER_SET_ON_RELEASED_TAP_TOGGLE(1), // Fn9 - ACTION_LAYER_BIT_TAP_TOGGLE(1), // Fn10 -}; - - static const uint8_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = { /* Layer 0: Default Layer * ,-----------------------------------------------------------. @@ -85,7 +68,7 @@ static const uint8_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = { KEYMAP(ESC, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, MINS,EQL, BSLS,GRV, \ TAB, Q, W, E, R, T, Y, U, I, O, P, LBRC,RBRC,BSPC, \ FN6, A, S, D, F, G, H, J, K, L, FN3, QUOT,FN7, \ - FN8, Z, X, C, V, B, N, M, COMM,DOT, FN2, RSFT,FN10, \ + FN8, Z, X, C, V, B, N, M, COMM,DOT, FN2, FN12,FN10, \ LGUI,LALT, FN5, RALT,FN4), /* Layer 1: HHKB mode (HHKB Fn) @@ -173,24 +156,127 @@ static const uint8_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = { LGUI,LALT, FN0, RALT,RGUI), }; -#define KEYCODE(layer, row, col) (pgm_read_byte(&keymaps[(layer)][(row)][(col)])) -/* legacy interface */ -uint8_t keymap_get_keycode(uint8_t layer, uint8_t row, uint8_t col) { return 0; } -uint8_t keymap_fn_layer(uint8_t fn_bits) { return 0; } -uint8_t keymap_fn_keycode(uint8_t fn_bits) { return 0; } +/* id for user defined functions */ +enum function_id { + LSHIFT_LPAREN, + RSHIFT_RPAREN, +}; + +/* + * Fn action definition + */ +static const uint16_t PROGMEM fn_actions[] = { + ACTION_LAYER_RETURN_DEFAULT, // FN0 + ACTION_LAYER_SET(1), // FN1 + ACTION_LAYER_SET_TAP_KEY(2, KC_SLASH), // FN2 + ACTION_LAYER_SET_TAP_KEY(3, KC_SCLN), // FN3 + ACTION_LAYER_SET(3), // FN4 + ACTION_LAYER_SET_TAP_KEY(5, KC_SPC), // FN5 + ACTION_LMOD_TAP_KEY(KC_LCTL, KC_BSPC), // FN6 + ACTION_RMOD_TAP_KEY(KC_RCTL, KC_ENT), // FN7 + ACTION_LMOD_ONESHOT(KC_LSFT), // FN8 Oneshot Shift + ACTION_LAYER_SET_ON_RELEASED_TAP_TOGGLE(1), // FN9 + ACTION_LAYER_BIT_TAP_KEY(1, KC_GRV), // FN10 + //ACTION_LAYER_BIT(1), // FN10 + //ACTION_LAYER_BIT_TAP_TOGGLE(1), // FN10 + ACTION_FUNCTION_TAP(LSHIFT_LPAREN), // FN11 + ACTION_FUNCTION_TAP(RSHIFT_RPAREN), // FN12 +}; + +/* + * user defined action function + */ +void keymap_call_function(keyrecord_t *record, uint8_t id) +{ + keyevent_t event = record->event; + uint8_t tap_count = record->tap_count; + + debug("action_call_function: "); + if (event.pressed) debug("pressed"); else debug("released"); + debug(" id: "); debug_hex(id); + debug(" tap_count: "); debug_dec(tap_count); + debug("\n"); + switch (id) { + case LSHIFT_LPAREN: + // LShft + tap '(' + if (event.pressed) { + if (tap_count == 0) { + add_mods(MOD_BIT(KC_LSHIFT)); + } else { + if (waiting_buffer_has_anykey_pressed()) { + // ad hoc: set 0 to cancel tap + record->tap_count = 0; + add_mods(MOD_BIT(KC_LSHIFT)); + } else { + // NOTE to avoid conflicting command key bind(LShift+RShift) + //register_code(KC_LSHIFT); + //register_code(KC_9); + host_add_mods(MOD_BIT(KC_LSHIFT)); + host_add_key(KC_9); + host_send_keyboard_report(); + } + } + } else { + if (tap_count == 0) { + del_mods(MOD_BIT(KC_LSHIFT)); + } else { + //unregister_code(KC_9); + //unregister_code(KC_LSHIFT); + host_del_mods(MOD_BIT(KC_LSHIFT)); + host_del_key(KC_9); + host_send_keyboard_report(); + } + } + break; + case RSHIFT_RPAREN: + // RShift + tap ')' + if (event.pressed) { + if (tap_count == 0) { + add_mods(MOD_BIT(KC_RSHIFT)); + } else { + if (waiting_buffer_has_anykey_pressed()) { + // ad hoc: set 0 to cancel tap + record->tap_count = 0; + add_mods(MOD_BIT(KC_RSHIFT)); + } else { + //register_code(KC_RSHIFT); + //register_code(KC_0); + host_add_mods(MOD_BIT(KC_RSHIFT)); + host_add_key(KC_0); + host_send_keyboard_report(); + } + } + } else { + if (tap_count == 0) { + del_mods(MOD_BIT(KC_RSHIFT)); + } else { + //unregister_code(KC_0); + //unregister_code(KC_RSHIFT); + host_del_mods(MOD_BIT(KC_RSHIFT)); + host_del_key(KC_0); + host_send_keyboard_report(); + } + } + break; + } +} +/* convert keycode to action */ action_t keymap_get_action(uint8_t layer, uint8_t row, uint8_t col) { - /* convert from legacy keycode to action */ - uint8_t key = KEYCODE(layer, row, col); + uint8_t key = (pgm_read_byte(&keymaps[(layer)][(row)][(col)])); action_t action; switch (key) { case KC_A ... KC_EXSEL: + action.code = ACTION_KEY(key); + break; case KC_LCTRL ... KC_LGUI: + action.code = ACTION_LMOD(key); + break; case KC_RCTRL ... KC_RGUI: - action.code = ACTION_KEY(key); + action.code = ACTION_RMOD(key); break; case KC_SYSTEM_POWER ... KC_SYSTEM_WAKE: action.code = ACTION_USAGE_SYSTEM(KEYCODE2SYSTEM(key)); @@ -201,15 +287,7 @@ action_t keymap_get_action(uint8_t layer, uint8_t row, uint8_t col) { case KC_MS_UP ... KC_MS_ACCEL2: action.code = ACTION_MOUSEKEY(key); break; -/* TODO - case KC_LCTRL ... KC_LGUI: - action.code = ACTION_LMODS(MOD_BIT(key)); - break; - case KC_RCTRL ... KC_RGUI: - action.code = ACTION_RMODS(MOD_BIT(key)>>4); - break; -*/ - case KC_FN0 ... FN_MAX: + case KC_FN0 ... KC_FN31: if (FN_INDEX(key) < sizeof(fn_actions) / sizeof(fn_actions[0])) { action.code = pgm_read_word(&fn_actions[FN_INDEX(key)]); } else { @@ -223,18 +301,3 @@ action_t keymap_get_action(uint8_t layer, uint8_t row, uint8_t col) { } return action; } - -// TODO: how to define action function -void action_call_function(keyevent_t event, uint8_t id) -{ - // '(' Shift+9 - if (event.pressed) { - register_code(KC_LSHIFT); - register_code(KC_9); - debug("action_call_function: pressed: id: "); debug_hex(id); debug("\n"); - } else { - unregister_code(KC_9); - unregister_code(KC_LSHIFT); - debug("action_call_function: released: id: "); debug_hex(id); debug("\n"); - } -} -- cgit v1.2.3-70-g09d2 From aad91a30a34d61739e1261bb82a1cb1ace581afa Mon Sep 17 00:00:00 2001 From: tmk Date: Mon, 4 Feb 2013 22:53:45 +0900 Subject: Add macro feature. --- common.mk | 1 + common/action.c | 2 +- common/action.h | 24 +++++------ common/action_macro.c | 67 +++++++++++++++++++++++++++++++ common/action_macro.h | 107 +++++++++++++++++++++++++++++++++++++++++++++++++ common/keymap.c | 2 +- common/keymap.h | 3 +- keyboard/hhkb/keymap.c | 42 +++++++++++++++---- 8 files changed, 226 insertions(+), 22 deletions(-) create mode 100644 common/action_macro.c create mode 100644 common/action_macro.h (limited to 'common/action.c') diff --git a/common.mk b/common.mk index 7cdaa5f74..86518f03f 100644 --- a/common.mk +++ b/common.mk @@ -2,6 +2,7 @@ COMMON_DIR = common SRC += $(COMMON_DIR)/host.c \ $(COMMON_DIR)/keyboard.c \ $(COMMON_DIR)/action.c \ + $(COMMON_DIR)/action_macro.c \ $(COMMON_DIR)/keymap.c \ $(COMMON_DIR)/command.c \ $(COMMON_DIR)/timer.c \ diff --git a/common/action.c b/common/action.c index cb44e272a..301a9b6a0 100644 --- a/common/action.c +++ b/common/action.c @@ -620,7 +620,7 @@ static void process_action(keyrecord_t *record) break; case ACT_FUNCTION: // TODO - keymap_call_function(record, action.func.id); + keymap_call_function(record, action.func.id, action.func.opt); break; default: break; diff --git a/common/action.h b/common/action.h index b657aa540..b1e958a26 100644 --- a/common/action.h +++ b/common/action.h @@ -49,27 +49,27 @@ typedef union { uint16_t code; struct action_kind { uint16_t param :12; - uint16_t id :4; + uint8_t id :4; } kind; struct action_key { - uint16_t code :8; - uint16_t mods :4; - uint16_t kind :4; + uint8_t code :8; + uint8_t mods :4; + uint8_t kind :4; } key; struct action_layer { - uint16_t code :8; - uint16_t val :4; - uint16_t kind :4; + uint8_t code :8; + uint8_t val :4; + uint8_t kind :4; } layer; struct action_usage { uint16_t code :10; - uint16_t page :2; - uint16_t kind :4; + uint8_t page :2; + uint8_t kind :4; } usage; struct action_command { - uint16_t id :8; - uint16_t opt :4; - uint16_t kind :4; + uint8_t id :8; + uint8_t opt :4; + uint8_t kind :4; } command; struct action_function { uint8_t id :8; diff --git a/common/action_macro.c b/common/action_macro.c new file mode 100644 index 000000000..72859c0dd --- /dev/null +++ b/common/action_macro.c @@ -0,0 +1,67 @@ +/* +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 +#include "debug.h" +#include "action.h" +#include "action_macro.h" + + +#define MACRO_READ() (macro = pgm_read_byte(macro_p++)) +void action_macro_play(const prog_macro_t *macro_p) +{ + macro_t macro = END; + uint8_t interval = 0; + + if (!macro_p) return; + while (true) { + switch (MACRO_READ()) { + case INTERVAL: + interval = MACRO_READ(); + debug("INTERVAL("); debug_dec(interval); debug(")\n"); + break; + case WAIT: + MACRO_READ(); + debug("WAIT("); debug_dec(macro); debug(")\n"); + { uint8_t ms = macro; while (ms--) _delay_ms(1); } + break; + case MODS_DOWN: + MACRO_READ(); + debug("MODS_DOWN("); debug_hex(macro); debug(")\n"); + debug("MODS_UP("); debug_hex(macro); debug(")\n"); + add_mods(macro); + break; + case MODS_UP: + MACRO_READ(); + debug("MODS_UP("); debug_hex(macro); debug(")\n"); + del_mods(macro); + break; + case 0x04 ... 0x73: + debug("DOWN("); debug_hex(macro); debug(")\n"); + register_code(macro); + break; + case 0x84 ... 0xF3: + debug("UP("); debug_hex(macro); debug(")\n"); + unregister_code(macro&0x7F); + break; + case END: + default: + return; + } + // interval + { uint8_t ms = interval; while (ms--) _delay_ms(1); } + } +} diff --git a/common/action_macro.h b/common/action_macro.h new file mode 100644 index 000000000..3833c7c8a --- /dev/null +++ b/common/action_macro.h @@ -0,0 +1,107 @@ +/* +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_MACRO_H +#define ACTION_MACRO_H +#include +#include + + +typedef uint8_t macro_t; +typedef macro_t prog_macro_t PROGMEM; + + +void action_macro_play(const prog_macro_t *macro); + + + +/* TODO: NOT FINISHED +normal mode command: + key(down): 0x04-7f/73(F24) + key(up): 0x84-ff +command: 0x00-03, 0x80-83(0x74-7f, 0xf4-ff) + mods down 0x00 + mods up 0x01 + wait 0x02 + interval 0x03 + extkey down 0x80 + extkey up 0x81 + ext commad 0x82 + ext mode 0x83 + end 0xff + +extension mode command: NOT IMPLEMENTED + key down 0x00 + key up 0x01 + key down + wait + key up + wait + mods push + mods pop + wait + interval + if + loop + push + pop + all up + end +*/ +enum macro_command_id{ + /* 0x00 - 0x03 */ + END = 0x00, + MODS_DOWN = 0x01, + MODS_UP = 0x02, + MODS_SET, + MODS_PUSH, + MODS_POP, + + WAIT = 0x74, + INTERVAL, + /* 0x74 - 0x7f */ + /* 0x80 - 0x84 */ + + EXT_DOWN, + EXT_UP, + EXT_WAIT, + EXT_INTERVAL, + COMPRESSION_MODE, + + EXTENSION_MODE = 0xff, +}; + + +/* normal mode */ +#define DOWN(key) (key) +#define UP(key) ((key) | 0x80) +#define TYPE(key) (key), (key | 0x80) +#define MODS_DOWN(mods) MODS_DOWN, (mods) +#define MODS_UP(mods) MODS_UP, (mods) +#define WAIT(ms) WAIT, (ms) +#define INTERVAL(ms) INTERVAL, (ms) + +#define D(key) DOWN(KC_##key) +#define U(key) UP(KC_##key) +#define T(key) TYPE(KC_##key) +#define MD(key) MODS_DOWN(MOD_BIT(KC_##key)) +#define MU(key) MODS_UP(MOD_BIT(KC_##key)) +#define W(ms) WAIT(ms) +#define I(ms) INTERVAL(ms) + + +/* extension mode */ + + +#endif /* ACTION_MACRO_H */ diff --git a/common/keymap.c b/common/keymap.c index 40d20f684..8302c2704 100644 --- a/common/keymap.c +++ b/common/keymap.c @@ -68,6 +68,6 @@ action_t keymap_get_action(uint8_t layer, uint8_t row, uint8_t col) #endif __attribute__ ((weak)) -void keymap_call_function(keyrecord_t *event, uint8_t id) +void keymap_call_function(keyrecord_t *event, uint8_t id, uint8_t opt) { } diff --git a/common/keymap.h b/common/keymap.h index e0fafeaf2..30d73f797 100644 --- a/common/keymap.h +++ b/common/keymap.h @@ -23,6 +23,7 @@ along with this program. If not, see . #include "action.h" +// TODO: move to action.h? /* layer used currently */ extern uint8_t current_layer; /* layer to return or start with */ @@ -34,7 +35,7 @@ extern uint8_t default_layer; action_t keymap_get_action(uint8_t layer, uint8_t row, uint8_t col); /* user defined special function */ -void keymap_call_function(keyrecord_t *record, uint8_t id); +void keymap_call_function(keyrecord_t *record, uint8_t id, uint8_t opt); #ifndef NO_LEGACY_KEYMAP_SUPPORT diff --git a/keyboard/hhkb/keymap.c b/keyboard/hhkb/keymap.c index e11b4563a..f2f21e8ce 100644 --- a/keyboard/hhkb/keymap.c +++ b/keyboard/hhkb/keymap.c @@ -21,12 +21,11 @@ along with this program. If not, see . #include #include #include -#include "host.h" #include "keycode.h" -#include "print.h" -#include "debug.h" -#include "util.h" #include "action.h" +#include "action_macro.h" +#include "host.h" +#include "debug.h" #include "keymap.h" @@ -69,7 +68,7 @@ static const uint8_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = { TAB, Q, W, E, R, T, Y, U, I, O, P, LBRC,RBRC,BSPC, \ FN6, A, S, D, F, G, H, J, K, L, FN3, QUOT,FN7, \ FN8, Z, X, C, V, B, N, M, COMM,DOT, FN2, FN12,FN10, \ - LGUI,LALT, FN5, RALT,FN4), + LGUI,LALT, FN5, FN13,FN4), /* Layer 1: HHKB mode (HHKB Fn) * ,-----------------------------------------------------------. @@ -162,6 +161,7 @@ static const uint8_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = { enum function_id { LSHIFT_LPAREN, RSHIFT_RPAREN, + MACRO = 0xff }; /* @@ -172,7 +172,8 @@ static const uint16_t PROGMEM fn_actions[] = { ACTION_LAYER_SET(1), // FN1 ACTION_LAYER_SET_TAP_KEY(2, KC_SLASH), // FN2 ACTION_LAYER_SET_TAP_KEY(3, KC_SCLN), // FN3 - ACTION_LAYER_SET(3), // FN4 + //ACTION_LAYER_SET(3), // FN4 + ACTION_FUNCTION(MACRO, 0), // FN4 ACTION_LAYER_SET_TAP_KEY(5, KC_SPC), // FN5 ACTION_LMOD_TAP_KEY(KC_LCTL, KC_BSPC), // FN6 ACTION_RMOD_TAP_KEY(KC_RCTL, KC_ENT), // FN7 @@ -183,12 +184,36 @@ static const uint16_t PROGMEM fn_actions[] = { //ACTION_LAYER_BIT_TAP_TOGGLE(1), // FN10 ACTION_FUNCTION_TAP(LSHIFT_LPAREN), // FN11 ACTION_FUNCTION_TAP(RSHIFT_RPAREN), // FN12 + ACTION_FUNCTION(MACRO, 1), // FN13 }; + +/* + * Macro definition + */ +#define MACRO(...) ({ static prog_macro_t _m[] PROGMEM = { __VA_ARGS__ }; _m; }) +#define MACRO_NONE 0 +static const prog_macro_t *get_macro(uint8_t id, bool pressed) +{ + switch (id) { + case 0: + return (pressed ? + MACRO( MD(LSHIFT), D(D), END ) : + MACRO( U(D), MU(LSHIFT), END ) ); + case 1: + return (pressed ? + MACRO( I(255), T(H), T(E), T(L), T(L), W(255), T(O), END ) : + MACRO_NONE ); + } + return 0; +} + + + /* * user defined action function */ -void keymap_call_function(keyrecord_t *record, uint8_t id) +void keymap_call_function(keyrecord_t *record, uint8_t id, uint8_t opt) { keyevent_t event = record->event; uint8_t tap_count = record->tap_count; @@ -261,6 +286,9 @@ void keymap_call_function(keyrecord_t *record, uint8_t id) } } break; + case MACRO: + action_macro_play(get_macro(opt, event.pressed)); + break; } } -- cgit v1.2.3-70-g09d2 From b2aa142ee0a88ae6c38798f33cf5d833b0ae3864 Mon Sep 17 00:00:00 2001 From: tmk Date: Fri, 8 Feb 2013 00:50:51 +0900 Subject: Clean layer actions. --- common/action.c | 213 +++++++++++-------------------------------------- common/action.h | 190 ++++++++++++++++++++++++------------------- keyboard/hhkb/keymap.c | 77 +++++++++++------- 3 files changed, 199 insertions(+), 281 deletions(-) (limited to 'common/action.c') diff --git a/common/action.c b/common/action.c index 301a9b6a0..f6e50032e 100644 --- a/common/action.c +++ b/common/action.c @@ -319,14 +319,14 @@ static void process_action(keyrecord_t *record) case ACT_USAGE: #ifdef EXTRAKEY_ENABLE switch (action.usage.page) { - case ACTION_USAGE_PAGE_SYSTEM: + case PAGE_SYSTEM: if (event.pressed) { host_system_send(action.usage.code); } else { host_system_send(0); } break; - case ACTION_USAGE_PAGE_CONSUMER: + case PAGE_CONSUMER: if (event.pressed) { host_consumer_send(action.usage.code); } else { @@ -351,20 +351,44 @@ static void process_action(keyrecord_t *record) break; /* Layer key */ - case ACT_LAYER_PRESSED: - // layer action when pressed + case ACT_LAYER: switch (action.layer.code) { - case 0x00: + case LAYER_MOMENTARY: /* momentary */ if (event.pressed) { layer_switch(action.layer.val); } -//TODO: this is ok? else { layer_switch(default_layer); } break; - case 0xF0: - // tap toggle + case LAYER_ON_PRESS: + if (event.pressed) { + layer_switch(action.layer.val); + } + break; + case LAYER_ON_RELEASE: + if (!event.pressed) { + layer_switch(action.layer.val); + } + break; + case LAYER_DEFAULT: /* default layer */ + switch (action.layer.val) { + case DEFAULT_ON_BOTH: + layer_switch(default_layer); + break; + case DEFAULT_ON_PRESS: + if (event.pressed) { + layer_switch(default_layer); + } + break; + case DEFAULT_ON_RELEASE: + if (!event.pressed) { + layer_switch(default_layer); + } + break; + } + break; + case LAYER_TAP_TOGGLE: /* switch on hold and toggle on several taps */ if (event.pressed) { if (tap_count < TAPPING_TOGGLE) { layer_switch(action.layer.val); @@ -376,15 +400,13 @@ static void process_action(keyrecord_t *record) } } break; - case 0xFF: - // change default layer + case LAYER_CHANGE_DEFAULT: /* change default layer */ if (event.pressed) { default_layer = action.layer.val; layer_switch(default_layer); } break; - default: - // with tap key + default: /* switch layer on hold and key on tap*/ if (event.pressed) { if (tap_count > 0) { debug("LAYER_PRESSED: Tap: register_code\n"); @@ -407,65 +429,26 @@ static void process_action(keyrecord_t *record) break; } break; - case ACT_LAYER_RELEASED: + case ACT_LAYER_BIT: switch (action.layer.code) { - case 0x00: - if (!event.pressed) { - layer_switch(action.layer.val); - } - break; - case 0xF0: - // tap toggle + case LAYER_MOMENTARY: /* momentary */ if (event.pressed) { - if (tap_count >= TAPPING_TOGGLE) { - debug("LAYER_RELEASED: tap toggle.\n"); - layer_switch(action.layer.val); - } + layer_switch(current_layer ^ action.layer.val); } else { - if (tap_count < TAPPING_TOGGLE) { - layer_switch(action.layer.val); - } - } - break; - case 0xFF: - // change default layer - if (!event.pressed) { - default_layer = action.layer.val; - layer_switch(default_layer); + layer_switch(current_layer ^ action.layer.val); } break; - default: - // with tap key + case LAYER_ON_PRESS: if (event.pressed) { - if (tap_count > 0) { - debug("LAYER_RELEASED: Tap: register_code\n"); - register_code(action.layer.code); - } else { - debug("LAYER_RELEASED: No tap: NO ACTION\n"); - } - } else { - if (tap_count > 0) { - debug("LAYER_RELEASED: Tap: unregister_code\n"); - unregister_code(action.layer.code); - } else { - debug("LAYER_RELEASED: No tap: layer_switch\n"); - layer_switch(action.layer.val); - } + layer_switch(current_layer ^ action.layer.val); } break; - } - break; - case ACT_LAYER_BIT: - switch (action.layer.code) { - case 0x00: - if (event.pressed) { - layer_switch(current_layer ^ action.layer.val); - } else { + case LAYER_ON_RELEASE: + if (!event.pressed) { layer_switch(current_layer ^ action.layer.val); } break; - case 0xF0: - // tap toggle + case LAYER_TAP_TOGGLE: /* switch on hold and toggle on several taps */ if (event.pressed) { if (tap_count < TAPPING_TOGGLE) { debug("LAYER_BIT: tap toggle(press).\n"); @@ -510,108 +493,6 @@ static void process_action(keyrecord_t *record) break; } break; - case ACT_LAYER_EXT: - switch (action.layer.val) { - case 0x00: - // set default layer when pressed - switch (action.layer.code) { - case 0x00: - if (event.pressed) { - layer_switch(default_layer); - } - break; - case 0xF0: - // tap toggle - if (event.pressed) { - if (tap_count < TAPPING_TOGGLE) { - layer_switch(default_layer); - } - } else { - if (tap_count >= TAPPING_TOGGLE) { - debug("LAYER_EXT_PRESSED: tap toggle.\n"); - layer_switch(default_layer); - } - } - break; - case 0xFF: - // change default layer - if (event.pressed) { - default_layer = current_layer; - layer_switch(default_layer); - } - break; - default: - // with tap key - if (event.pressed) { - if (tap_count > 0) { - debug("LAYER_EXT_PRESSED: Tap: register_code\n"); - register_code(action.layer.code); - } else { - debug("LAYER_EXT_PRESSED: No tap: layer_switch\n"); - layer_switch(default_layer); - } - } else { - if (tap_count > 0) { - debug("LAYER_EXT_PRESSED: Tap: unregister_code\n"); - unregister_code(action.layer.code); - } else { - debug("LAYER_EXT_PRESSED: No tap: NO ACTION\n"); - } - } - break; - } - break; - case 0x01: - // set default layer when released - switch (action.layer.code) { - case 0x00: - if (!event.pressed) { - layer_switch(default_layer); - } - break; - case 0xF0: - // tap toggle - if (event.pressed) { - if (tap_count >= TAPPING_TOGGLE) { - debug("LAYER_EXT_RELEASED: tap toggle.\n"); - layer_switch(default_layer); - } - } else { - if (tap_count < TAPPING_TOGGLE) { - layer_switch(default_layer); - } - } - break; - case 0xFF: - // change default layer - if (!event.pressed) { - default_layer = current_layer; - layer_switch(default_layer); - } - break; - default: - // with tap key - if (event.pressed) { - if (tap_count > 0) { - debug("LAYER_EXT_RELEASED: Tap: register_code\n"); - register_code(action.layer.code); - } else { - debug("LAYER_EXT_RELEASED: No tap: NO ACTION\n"); - } - } else { - if (tap_count > 0) { - debug("LAYER_EXT_RELEASED: Tap: unregister_code\n"); - unregister_code(action.layer.code); - } else { - debug("LAYER_EXT_RELEASED: No tap: layer_switch\n"); - layer_switch(default_layer); - } - } - break; - } - break; - } - break; /* Extentions */ case ACT_MACRO: @@ -932,7 +813,7 @@ bool is_tap_key(key_t key) case ACT_LMODS_TAP: case ACT_RMODS_TAP: return true; - case ACT_LAYER_PRESSED: + case ACT_LAYER: case ACT_LAYER_BIT: switch (action.layer.code) { case 0x00: @@ -944,7 +825,7 @@ bool is_tap_key(key_t key) } return false; case ACT_FUNCTION: - if (action.func.opt & O_TAP) { + if (action.func.opt & FUNC_TAP) { return true; } return false; @@ -975,10 +856,8 @@ static void debug_action(action_t action) case ACT_RMODS_TAP: debug("ACT_RMODS_TAP"); break; case ACT_USAGE: debug("ACT_USAGE"); break; case ACT_MOUSEKEY: debug("ACT_MOUSEKEY"); break; - case ACT_LAYER_PRESSED: debug("ACT_LAYER_PRESSED"); break; - case ACT_LAYER_RELEASED: debug("ACT_LAYER_RELEASED"); break; + case ACT_LAYER: debug("ACT_LAYER"); break; case ACT_LAYER_BIT: debug("ACT_LAYER_BIT"); break; - case ACT_LAYER_EXT: debug("ACT_LAYER_EXT"); break; case ACT_MACRO: debug("ACT_MACRO"); break; case ACT_COMMAND: debug("ACT_COMMAND"); break; case ACT_FUNCTION: debug("ACT_FUNCTION"); break; diff --git a/common/action.h b/common/action.h index b1e958a26..800554eb8 100644 --- a/common/action.h +++ b/common/action.h @@ -150,42 +150,26 @@ ACT_MOUSEKEY(0110): Layer Actions ------------- -TODO: reconsider layer methods. -1 momemtary + tap key up: L, down: default -1 bitwise + tap key up: xor B, down: xor B -3 momemtary go + tap key? up: X, down: -3 toggle(mementary back) + tap key? up: down: Y -3 no tap up: X, down: Y - -ACT_LAYER_PRESSED(1000): Set layer on key pressed -ACT_LAYER_RELEASED(1001): Set layer on key released -ACT_LAYER_BIT(1010): On/Off layer bit -ACT_LAYER_EXT(1011): Extentions - -1000|LLLL|0000 0000 set layer L when pressed -1001|LLLL|0000 0000 set layer L when released -1010|BBBB|0000 0000 on/off bit B when pressed/released -1011|0000|0000 0000 set default layer when pressed -1011|0001|0000 0000 set default layer when released - -1000|LLLL|1111 0000 set layer L when pressed + tap toggle -1001|LLLL|1111 0000 set layer L when released + tap toggle -1010|BBBB|1111 0000 on/off bit B when pressed/released + tap toggle -1011|0000|1111 0000 set default layer when pressed + tap toggle -1011|0001|1111 0000 set default layer when released + tap toggle - -1000|LLLL|1111 1111 set L to default layer when pressed -1001|LLLL|1111 1111 set L to default layer when released -1010|BBBB|1111 1111 on/off bit B of default layer when pressed/released -1011|0000|1111 1111 set current to default layer when pressed -1011|0001|1111 1111 set current to default layer when released - -1000|LLLL| keycode set layer L when pressed + tap key -1001|LLLL| keyocde set layer L when released + tap key -1010|BBBB| keyocde on/off bit B when pressed/released + tap key -1011|0000| keyocde set default layer when pressed + tap key -1011|0001| keyocde set default layer when released + tap key - +ACT_LAYER(1000): Set layer +ACT_LAYER_BIT(1001): Bit-op layer + +1000|LLLL|0000 0000 set L to layer on press and set default on release(momentary) +1000|LLLL|0000 0001 set L to layer on press +1000|LLLL|0000 0010 set L to layer on release +1000|----|0000 0011 set default to layer on both(return to default layer) +1000|LLLL|xxxx xxxx set L to layer while hold and send key on tap +1000|LLLL|1111 0000 set L to layer while hold and toggle on several taps +1000|LLLL|1111 1111 set L to default and layer(on press) + +1001|BBBB|0000 0000 (not used) +1001|BBBB|0000 0001 bit-xor layer with B on press +1001|BBBB|0000 0010 bit-xor layer with B on release +1001|BBBB|0000 0011 bit-xor layer with B on both(momentary) +1001|BBBB|xxxx xxxx bit-xor layer with B while hold and send key on tap +1001|BBBB|1111 0000 bit-xor layer with B while hold and toggle on several taps +1001|BBBB|1111 1111 bit-xor default with B and set layer(on press) + + Extensions(11XX) ---------------- @@ -212,24 +196,14 @@ enum action_kind_id { ACT_USAGE = 0b0100, ACT_MOUSEKEY = 0b0101, - ACT_LAYER_PRESSED = 0b1000, - ACT_LAYER_RELEASED = 0b1001, - ACT_LAYER_BIT = 0b1010, - ACT_LAYER_EXT = 0b1011, + ACT_LAYER = 0b1000, + ACT_LAYER_BIT = 0b1001, ACT_MACRO = 0b1100, ACT_COMMAND = 0b1110, ACT_FUNCTION = 0b1111 }; -enum params { - P_ONESHOT = 0x00, -}; - -enum options { - O_TAP = 0x8, -}; - /* action utility */ #define ACTION_NO 0 @@ -250,48 +224,93 @@ enum options { #define ACTION_RMOD_KEY(mod, key) ACTION(ACT_RMODS, MODS4(MOD_BIT(mod))<<8 | (key)) /* Mods + Tap key */ +enum mods_codes { + MODS_ONESHOT = 0x00, +}; #define ACTION_LMODS_TAP_KEY(mods, key) ACTION(ACT_LMODS_TAP, MODS4(mods)<<8 | (key)) -#define ACTION_LMODS_ONESHOT(mods) ACTION(ACT_LMODS_TAP, MODS4(mods)<<8 | P_ONESHOT) +#define ACTION_LMODS_ONESHOT(mods) ACTION(ACT_LMODS_TAP, MODS4(mods)<<8 | MODS_ONESHOT) #define ACTION_RMODS_TAP_KEY(mods, key) ACTION(ACT_RMODS_TAP, MODS4(mods)<<8 | (key)) -#define ACTION_RMODS_ONESHOT(mods) ACTION(ACT_RMODS_TAP, MODS4(mods)<<8 | P_ONESHOT) +#define ACTION_RMODS_ONESHOT(mods) ACTION(ACT_RMODS_TAP, MODS4(mods)<<8 | MODS_ONESHOT) /* Mod + Tap key */ #define ACTION_LMOD_TAP_KEY(mod, key) ACTION(ACT_LMODS_TAP, MODS4(MOD_BIT(mod))<<8 | (key)) -#define ACTION_LMOD_ONESHOT(mod) ACTION(ACT_LMODS_TAP, MODS4(MOD_BIT(mod))<<8 | P_ONESHOT) +#define ACTION_LMOD_ONESHOT(mod) ACTION(ACT_LMODS_TAP, MODS4(MOD_BIT(mod))<<8 | MODS_ONESHOT) #define ACTION_RMOD_TAP_KEY(mod, key) ACTION(ACT_RMODS_TAP, MODS4(MOD_BIT(mod))<<8 | (key)) -#define ACTION_RMOD_ONESHOT(mod) ACTION(ACT_RMODS_TAP, MODS4(MOD_BIT(mod))<<8 | P_ONESHOT) - -// TODO: contemplate about layer action -/* Switch current layer */ -#define ACTION_LAYER_SET(layer) ACTION(ACT_LAYER_PRESSED, (layer)<<8 | 0x00) -#define ACTION_LAYER_SET_ON_PRESSED(layer) ACTION(ACT_LAYER_PRESSED, (layer)<<8 | 0x00) -#define ACTION_LAYER_SET_ON_RELEASED(layer) ACTION(ACT_LAYER_RELEASED, (layer)<<8 | 0x00) -#define ACTION_LAYER_BIT(bits) ACTION(ACT_LAYER_BIT, (bits)<<8 | 0x00) -#define ACTION_LAYER_SET_DEFAULT ACTION(ACT_LAYER_EXT, 0x0<<8 | 0x00) -#define ACTION_LAYER_RETURN_DEFAULT ACTION(ACT_LAYER_EXT, 0x1<<8 | 0x00) -#define ACTION_LAYER_SET_DEFAULT_ON_PRESSED ACTION(ACT_LAYER_EXT, 0x0<<8 | 0x00) -#define ACTION_LAYER_SET_DEFAULT_ON_RELEASED ACTION(ACT_LAYER_EXT, 0x1<<8 | 0x00) -/* Switch default layer */ -#define ACTION_LAYER_DEFAULT_SET(layer) ACTION(ACT_LAYER_PRESSED, (layer)<<8 | 0xFF) -#define ACTION_LAYER_DEFAULT_SET_ON_PRESSED(layer) ACTION(ACT_LAYER_PRESSED, (layer)<<8 | 0xFF) -#define ACTION_LAYER_DEFAULT_SET_ON_RELEASED(layer) ACTION(ACT_LAYER_RELEASED, (layer)<<8 | 0xFF) -#define ACTION_LAYER_DEFAULT_BIT(bits) ACTION(ACT_LAYER_BIT, (bits)<<8 | 0xFF) -#define ACTION_LAYER_DEFAULT_SET_CURRENT_ON_PRESSED ACTION(ACT_LAYER_EXT, 0x0<<8 | 0xFF) -#define ACTION_LAYER_DEFAULT_SET_CURRENT_ON_RELEASED ACTION(ACT_LAYER_EXT, 0x1<<8 | 0xFF) -/* Layer switch with tap key */ -#define ACTION_LAYER_SET_TAP_KEY(layer, key) ACTION(ACT_LAYER_PRESSED, (layer)<<8 | (key)) -#define ACTION_LAYER_BIT_TAP_KEY(bits, key) ACTION(ACT_LAYER_BIT, (bits)<<8 | (key)) -#define ACTION_LAYER_DEFAULT_SET_TAP_KEY(key) ACTION(ACT_LAYER_EXT, 0x0<<8 | (key)) -/* Layer switch with tap toggle */ -#define ACTION_LAYER_SET_ON_PRESSED_TAP_TOGGLE(layer) ACTION(ACT_LAYER_PRESSED, (layer)<<8 | 0xF0) -#define ACTION_LAYER_SET_ON_RELEASED_TAP_TOGGLE(layer) ACTION(ACT_LAYER_RELEASED, (layer)<<8 | 0xF0) -#define ACTION_LAYER_BIT_TAP_TOGGLE(layer) ACTION(ACT_LAYER_BIT, (layer)<<8 | 0xF0) -#define ACTION_LAYER_DEFAULT_TAP_TOGGLE ACTION(ACT_LAYER_EXT, 0x0<<8 | 0xF0) +#define ACTION_RMOD_ONESHOT(mod) ACTION(ACT_RMODS_TAP, MODS4(MOD_BIT(mod))<<8 | MODS_ONESHOT) + + +/* + * Switch layer + */ +enum layer_codes { + LAYER_MOMENTARY = 0, + LAYER_ON_PRESS = 1, + LAYER_ON_RELEASE = 2, + LAYER_DEFAULT =3, + LAYER_TAP_TOGGLE = 0xF0, + LAYER_CHANGE_DEFAULT = 0xFF +}; +enum layer_vals_default { + DEFAULT_ON_PRESS = 1, + DEFAULT_ON_RELEASE = 2, + DEFAULT_ON_BOTH = 3, +}; + +/* + * return to default layer + */ +#define ACTION_LAYER_DEFAULT ACTION_LAYER_DEFAULT_R +/* set default layer on press */ +#define ACTION_LAYER_DEFAULT_P ACTION(ACT_LAYER, DEFAULT_ON_PRESS<<8 | LAYER_DEFAULT) +/* set default layer on release */ +#define ACTION_LAYER_DEFAULT_R ACTION(ACT_LAYER, DEFAULT_ON_RELEASE<<8 | LAYER_DEFAULT) +/* change default layer and set layer */ + +/* + * Set layer + */ +/* set layer on press and set default on release */ +#define ACTION_LAYER_SET(layer) ACTION_LAYER_SET_MOMENTARY(layer) +#define ACTION_LAYER_SET_MOMENTARY(layer) ACTION(ACT_LAYER, (layer)<<8 | LAYER_MOMENTARY) +/* set layer on press and none on release */ +#define ACTION_LAYER_SET_TOGGLE(layer) ACTION_LAYER_SET_R(layer) +/* set layer while hold and send key on tap */ +#define ACTION_LAYER_SET_TAP_KEY(layer, key) ACTION(ACT_LAYER, (layer)<<8 | (key)) +/* set layer on press */ +#define ACTION_LAYER_SET_P(layer) ACTION(ACT_LAYER, (layer)<<8 | LAYER_ON_PRESS) +/* set layer on release */ +#define ACTION_LAYER_SET_R(layer) ACTION(ACT_LAYER, (layer)<<8 | LAYER_ON_RELEASE) +/* set layer on hold and toggle on several taps */ +#define ACTION_LAYER_SET_TAP_TOGGLE(layer) ACTION(ACT_LAYER, (layer)<<8 | LAYER_TAP_TOGGLE) +/* set default layer on both press and release */ +#define ACTION_LAYER_SET_DEFAULT(layer) ACTION(ACT_LAYER, (layer)<<8 | LAYER_CHANGE_DEFAULT) + +/* + * Bit-op layer + */ +/* bit-xor on both press and release */ +#define ACTION_LAYER_BIT(bits) ACTION_LAYER_BIT_MOMENTARY(bits) +#define ACTION_LAYER_BIT_MOMENTARY(bits) ACTION(ACT_LAYER_BIT, (bits)<<8 | LAYER_MOMENTARY) +/* bit-xor on press */ +#define ACTION_LAYER_BIT_TOGGLE(bits) ACTION_LAYER_BIT_R(bits) +/* bit-xor while hold and send key on tap */ +#define ACTION_LAYER_BIT_TAP_KEY(bits, key) ACTION(ACT_LAYER_BIT, (bits)<<8 | (key)) +/* bit-xor on press */ +#define ACTION_LAYER_BIT_P(bits) ACTION(ACT_LAYER_BIT, (bits)<<8 | LAYER_ON_PRESS) +/* bit-xor on release */ +#define ACTION_LAYER_BIT_R(bits) ACTION(ACT_LAYER_BIT, (bits)<<8 | LAYER_ON_RELEASE) +/* bit-xor while hold and toggle on several taps */ +#define ACTION_LAYER_BIT_TAP_TOGGLE(bits) ACTION(ACT_LAYER_BIT, (bits)<<8 | LAYER_TAP_TOGGLE) +/* bit-xor default layer and set layer */ +#define ACTION_LAYER_BIT_DEFAULT(bits) ACTION(ACT_LAYER, (bits)<<8 | LAYER_CHANGE_DEFAULT) + /* HID Usage */ -#define ACTION_USAGE_PAGE_SYSTEM 0 -#define ACTION_USAGE_PAGE_CONSUMER 1 -#define ACTION_USAGE_SYSTEM(id) ACTION(ACT_USAGE, ACTION_USAGE_PAGE_SYSTEM<<10 | (id)) -#define ACTION_USAGE_CONSUMER(id) ACTION(ACT_USAGE, ACTION_USAGE_PAGE_CONSUMER<<10 | (id)) +enum usage_pages { + PAGE_SYSTEM, + PAGE_CONSUMER +}; +#define ACTION_USAGE_SYSTEM(id) ACTION(ACT_USAGE, PAGE_SYSTEM<<10 | (id)) +#define ACTION_USAGE_CONSUMER(id) ACTION(ACT_USAGE, PAGE_CONSUMER<<10 | (id)) /* Mousekey */ #define ACTION_MOUSEKEY(key) ACTION(ACT_MOUSEKEY, key) @@ -303,7 +322,10 @@ enum options { #define ACTION_COMMAND(opt, id) ACTION(ACT_COMMAND, (opt)<<8 | (addr)) /* Function */ +enum function_opts { + FUNC_TAP = 0x8, +}; #define ACTION_FUNCTION(id, opt) ACTION(ACT_FUNCTION, (opt)<<8 | id) -#define ACTION_FUNCTION_TAP(id) ACTION(ACT_FUNCTION, O_TAP<<8 | id) +#define ACTION_FUNCTION_TAP(id) ACTION(ACT_FUNCTION, FUNC_TAP<<8 | id) #endif /* ACTION_H */ diff --git a/keyboard/hhkb/keymap.c b/keyboard/hhkb/keymap.c index f2f21e8ce..65ef89ad7 100644 --- a/keyboard/hhkb/keymap.c +++ b/keyboard/hhkb/keymap.c @@ -29,8 +29,6 @@ along with this program. If not, see . #include "keymap.h" -// Convert physical keyboard layout to matrix array. -// This is a macro to define keymap easily in keyboard layout form. #define KEYMAP( \ K31, K30, K00, K10, K11, K20, K21, K40, K41, K60, K61, K70, K71, K50, K51, \ K32, K01, K02, K13, K12, K23, K22, K42, K43, K62, K63, K73, K72, K52, \ @@ -50,6 +48,7 @@ along with this program. If not, see . } +// TODO: use [1] = KEYMAP(...) to prevent from changing index of element? static const uint8_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = { /* Layer 0: Default Layer * ,-----------------------------------------------------------. @@ -57,17 +56,17 @@ static const uint8_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = { * |-----------------------------------------------------------| * |Tab | Q| W| E| R| T| Y| U| I| O| P| [| ]|Backs| * |-----------------------------------------------------------| - * |Contro| A| S| D| F| G| H| J| K| L|Fn3| '|Return | + * |Fn6 | A| S| D| F| G| H| J| K| L|Fn3| '|Return | * |-----------------------------------------------------------| - * |Shift | Z| X| C| V| B| N| M| ,| .|Fn2|Shift |Fn1| + * |Fn8 | Z| X| C| V| B| N| M| ,| .|Fn2|Fn12 |Fn1| * `-----------------------------------------------------------' - * |Gui|Alt |Fn5 |Alt |Fn4| + * |Gui|Alt | Fn5 |Alt |Fn4| * `-------------------------------------------' */ KEYMAP(ESC, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, MINS,EQL, BSLS,GRV, \ TAB, Q, W, E, R, T, Y, U, I, O, P, LBRC,RBRC,BSPC, \ FN6, A, S, D, F, G, H, J, K, L, FN3, QUOT,FN7, \ - FN8, Z, X, C, V, B, N, M, COMM,DOT, FN2, FN12,FN10, \ + FN8, Z, X, C, V, B, N, M, COMM,DOT, FN2, FN12,FN9, \ LGUI,LALT, FN5, FN13,FN4), /* Layer 1: HHKB mode (HHKB Fn) @@ -80,13 +79,13 @@ static const uint8_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = { * |-----------------------------------------------------------| * |Shift | | | | | | +| -|End|PgD|Dow|Shift |Fn0| * `-----------------------------------------------------------' - * |Gui |Alt |Space |Alt |xxx| - * `--------------------------------------------' + * |Gui|Alt | Space |Alt |Gui| + * `-------------------------------------------' */ KEYMAP(PWR, F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12, INS, DEL, \ - CAPS,NO, NO, NO, NO, NO, NO, NO, PSCR,SLCK,BRK, UP, NO, BSPC, \ + CAPS,NO, NO, NO, NO, NO, NO, NO, PSCR,SLCK,PAUS, UP, NO, BSPC, \ LCTL,VOLD,VOLU,MUTE,NO, NO, PAST,PSLS,HOME,PGUP,LEFT,RGHT,ENT, \ - LSFT,NO, NO, NO, NO, NO, PPLS,PMNS,END, PGDN,DOWN,RSFT,FN10, \ + LSFT,NO, NO, NO, NO, NO, PPLS,PMNS,END, PGDN,DOWN,RSFT,FN0, \ LGUI,LALT, SPC, RALT,RGUI), /* Layer 2: Vi mode (Slash) @@ -99,7 +98,7 @@ static const uint8_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = { * |-----------------------------------------------------------| * |Shift | | | | | |Hom|PgD|PgUlEnd|Fn0|Shift | | * `-----------------------------------------------------------' - * |Gui|Alt |Space |Alt |Gui| + * |Gui|Alt | Space |Alt |Gui| * `-------------------------------------------' */ KEYMAP(ESC, F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12, INS, DEL, \ @@ -112,13 +111,13 @@ static const uint8_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = { * ,-----------------------------------------------------------. * |Esc| F1| F2| F3| F4| F5| F6| F7| F8| F9|F10|F11|F12|Ins|Del| * |-----------------------------------------------------------| - * |Tab |MwL|MwU|McU|MwD|MwR|MwL|MwD|MwU|MwR| | | |Backs| + * |Tab | | | | | |MwL|MwD|MwU|MwR| | | |Backs| * |-----------------------------------------------------------| - * |Contro| |McL|McD|McR| |McL|McD|McU|McR|Fn0| |Return | + * |Contro| | | | | |McL|McD|McU|McR|Fn0| |Return | * |-----------------------------------------------------------| - * |Shift |Mb4|Mb5|Mb1|Mb2|Mb3|Mb2|Mb1|Mb4|Mb5| |Shift | | + * |Shift | | | | |Mb3|Mb2|Mb1|Mb4|Mb5| |Shift | | * `-----------------------------------------------------------' - * |Gui |Alt |Mb1 |Alt |Fn0| + * |Gui |Alt | Mb1 |Alt |Fn0| * `--------------------------------------------' * Mc: Mouse Cursor / Mb: Mouse Button / Mw: Mouse Wheel */ @@ -147,10 +146,23 @@ static const uint8_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = { LSFT,SLSH,DOT, COMM,M, N, B, V, C, X, Z, RSFT,NO, \ LGUI,LALT, FN0, RALT,RGUI), - /* Layer5: another Mouse mode (Space) */ + /* Layer5: another Mouse mode (Space) + * ,-----------------------------------------------------------. + * |Esc| F1| F2| F3| F4| F5| F6| F7| F8| F9|F10|F11|F12|Ins|Del| + * |-----------------------------------------------------------| + * |Tab | | | | | |MwL|MwD|MwU|MwR| | | |Backs| + * |-----------------------------------------------------------| + * |Contro| | | | | |McL|McD|McU|McR|Fn0| |Return | + * |-----------------------------------------------------------| + * |Shift | | | | |Mb3|Mb2|Mb1|Mb4|Mb5| |Shift | | + * `-----------------------------------------------------------' + * |Gui |Alt | Fn0 |Alt |Fn0| + * `--------------------------------------------' + * Mc: Mouse Cursor / Mb: Mouse Button / Mw: Mouse Wheel + */ KEYMAP(ESC, F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12, INS, DEL, \ TAB, NO, NO, NO, NO, NO, WH_L,WH_D,WH_U,WH_R,NO, NO, NO, BSPC, \ - LCTL,NO, ACL0,ACL1,ACL2,NO, MS_L,MS_D,MS_U,MS_R,FN0, NO, ENT, \ + LCTL,NO, ACL0,ACL1,ACL2,NO, MS_L,MS_D,MS_U,MS_R,NO, NO, ENT, \ LSFT,NO, NO, NO, NO, BTN3,BTN2,BTN1,BTN4,BTN5,NO, RSFT,NO, \ LGUI,LALT, FN0, RALT,RGUI), }; @@ -164,27 +176,32 @@ enum function_id { MACRO = 0xff }; + /* * Fn action definition */ +// TODO: use [1] = KEYMAP(...) to prevent from changing index of element? static const uint16_t PROGMEM fn_actions[] = { - ACTION_LAYER_RETURN_DEFAULT, // FN0 + ACTION_LAYER_DEFAULT, // FN0 ACTION_LAYER_SET(1), // FN1 - ACTION_LAYER_SET_TAP_KEY(2, KC_SLASH), // FN2 - ACTION_LAYER_SET_TAP_KEY(3, KC_SCLN), // FN3 - //ACTION_LAYER_SET(3), // FN4 - ACTION_FUNCTION(MACRO, 0), // FN4 + ACTION_LAYER_SET_TAP_KEY(2, KC_SLASH), // FN2 Layer with Slash + ACTION_LAYER_SET_TAP_KEY(3, KC_SCLN), // FN3 Layer with Semicolon + + ACTION_LAYER_SET(3), // FN4 +// ACTION_LAYER_SET_TOGGLE(3), // FN4 +// ACTION_FUNCTION(MACRO, 0), // FN4 ACTION_LAYER_SET_TAP_KEY(5, KC_SPC), // FN5 - ACTION_LMOD_TAP_KEY(KC_LCTL, KC_BSPC), // FN6 - ACTION_RMOD_TAP_KEY(KC_RCTL, KC_ENT), // FN7 - ACTION_LMOD_ONESHOT(KC_LSFT), // FN8 Oneshot Shift - ACTION_LAYER_SET_ON_RELEASED_TAP_TOGGLE(1), // FN9 - ACTION_LAYER_BIT_TAP_KEY(1, KC_GRV), // FN10 +// ACTION_LMOD_TAP_KEY(KC_LCTL, KC_BSPC), // FN6 Control with tap Backspace + ACTION_LMOD_TAP_KEY(KC_LCTL, KC_ESC), // FN6 Control with tap Backspace + ACTION_RMOD_TAP_KEY(KC_RCTL, KC_ENT), // FN7 Control with tap Enter + ACTION_LMOD_ONESHOT(KC_LSFT), // FN8 Oneshot Shift + ACTION_LAYER_SET_TAP_TOGGLE(1), // FN9 + ACTION_LAYER_BIT_TAP_KEY(1, KC_GRV), // FN10 Layer with Grave //ACTION_LAYER_BIT(1), // FN10 //ACTION_LAYER_BIT_TAP_TOGGLE(1), // FN10 - ACTION_FUNCTION_TAP(LSHIFT_LPAREN), // FN11 - ACTION_FUNCTION_TAP(RSHIFT_RPAREN), // FN12 - ACTION_FUNCTION(MACRO, 1), // FN13 + ACTION_FUNCTION_TAP(LSHIFT_LPAREN), // FN11 Function: LShift with tap '(' + ACTION_FUNCTION_TAP(RSHIFT_RPAREN), // FN12 Function: RShift with tap ')' + ACTION_FUNCTION(MACRO, 1), // FN13 Macro: }; -- cgit v1.2.3-70-g09d2 From eea85c7e24c4ff61bff0a6db8246a19896b5b630 Mon Sep 17 00:00:00 2001 From: tmk Date: Mon, 11 Feb 2013 00:02:11 +0900 Subject: Fix is_tap_key() --- common/action.c | 14 +++++++------- common/action.h | 2 +- 2 files changed, 8 insertions(+), 8 deletions(-) (limited to 'common/action.c') diff --git a/common/action.c b/common/action.c index f6e50032e..6d5336752 100644 --- a/common/action.c +++ b/common/action.c @@ -816,18 +816,18 @@ bool is_tap_key(key_t key) case ACT_LAYER: case ACT_LAYER_BIT: switch (action.layer.code) { - case 0x00: - case 0xF1 ... 0xFF: + case LAYER_MOMENTARY: + case LAYER_ON_PRESS: + case LAYER_ON_RELEASE: + case LAYER_DEFAULT: return false; - case 0xF0: - default: + case LAYER_TAP_TOGGLE: + default: /* tap key */ return true; } return false; case ACT_FUNCTION: - if (action.func.opt & FUNC_TAP) { - return true; - } + if (action.func.opt & FUNC_TAP) { return true; } return false; } return false; diff --git a/common/action.h b/common/action.h index 800554eb8..9b559cb18 100644 --- a/common/action.h +++ b/common/action.h @@ -323,7 +323,7 @@ enum usage_pages { /* Function */ enum function_opts { - FUNC_TAP = 0x8, + FUNC_TAP = 0x8, /* indciates function is tappable */ }; #define ACTION_FUNCTION(id, opt) ACTION(ACT_FUNCTION, (opt)<<8 | id) #define ACTION_FUNCTION_TAP(id) ACTION(ACT_FUNCTION, FUNC_TAP<<8 | id) -- cgit v1.2.3-70-g09d2