1
0

Add support partly for modifier with tap key.

This commit is contained in:
tmk 2013-01-15 19:04:58 +09:00
parent cb795497fa
commit a295aa6aa9
2 changed files with 231 additions and 169 deletions

View File

@ -10,11 +10,12 @@
#include "action.h" #include "action.h"
static void process(keyevent_t event, action_t action); static void process(keyevent_t event, action_t action);
static void register_code(uint8_t code); static void register_code(uint8_t code);
static void unregister_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(void);
static void clear_keyboard_but_mods(void); static void clear_keyboard_but_mods(void);
static bool sending_anykey(void); static bool sending_anykey(void);
@ -24,7 +25,6 @@ static void layer_switch(uint8_t new_layer);
/* tap */ /* tap */
#define TAP_TIME 200 #define TAP_TIME 200
static keyevent_t last_event = {}; static keyevent_t last_event = {};
static uint16_t last_event_time = 0;
static uint8_t tap_count = 0; static uint8_t tap_count = 0;
/* layer */ /* layer */
@ -32,6 +32,7 @@ uint8_t default_layer = 0;
uint8_t current_layer = 0; uint8_t current_layer = 0;
keyrecord_t delaying_layer = {}; keyrecord_t delaying_layer = {};
/* waiting keys buffer */
#define WAITING_KEYS_BUFFER 3 #define WAITING_KEYS_BUFFER 3
static keyrecord_t waiting_keys[WAITING_KEYS_BUFFER] = {}; static keyrecord_t waiting_keys[WAITING_KEYS_BUFFER] = {};
static uint8_t waiting_keys_head = 0; 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) static void process(keyevent_t event, action_t action)
{ {
//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); debug("\n"); debug("action: "); debug_hex16(action.code);
if (event.pressed) debug("[down]\n"); else debug("[up]\n");
switch (action.kind.id) { switch (action.kind.id) {
/* Key and Mods */ /* Key and Mods */
case ACT_LMODS: 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) { if (event.pressed) {
uint8_t tmp_mods = host_get_mods(); uint8_t tmp_mods = host_get_mods();
if (action.key.mods) { if (action.key.mods) {
@ -105,6 +177,11 @@ static void process(keyevent_t event, action_t action)
} }
break; break;
case ACT_RMODS: 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) { if (event.pressed) {
uint8_t tmp_mods = host_get_mods(); uint8_t tmp_mods = host_get_mods();
if (action.key.mods) { if (action.key.mods) {
@ -124,9 +201,49 @@ static void process(keyevent_t event, action_t action)
unregister_code(action.key.code); unregister_code(action.key.code);
} }
break; 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; 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; break;
/* other HID usage */ /* other HID usage */
@ -186,6 +303,7 @@ static void process(keyevent_t event, action_t action)
// with tap key // with tap key
if (event.pressed) { if (event.pressed) {
if (tap_count == 0) { if (tap_count == 0) {
// not tapping yet
if (host_has_anykey()) { if (host_has_anykey()) {
register_code(action.layer.code); register_code(action.layer.code);
} else { } else {
@ -197,22 +315,14 @@ static void process(keyevent_t event, action_t action)
}; };
} }
} else if (tap_count > 0) { } 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); register_code(action.layer.code);
} }
} else { } else {
// tap key // released after tapping
if (KEYEQ(event.key, delaying_layer.event.key) && debug("tap[layer](unregister): "); debug_hex(tap_count); debug("\n");
timer_elapsed(delaying_layer.event.time) <= TAP_TIME) { unregister_code(action.layer.code);
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){};
} }
break; break;
} }
@ -220,7 +330,7 @@ static void process(keyevent_t event, action_t action)
case ACT_LAYER_RELEASED: case ACT_LAYER_RELEASED:
switch (action.layer.code) { switch (action.layer.code) {
case 0x00: case 0x00:
if (event.pressed) { if (!event.pressed) {
layer_switch(action.layer.opt); layer_switch(action.layer.opt);
} }
break; break;
@ -274,7 +384,7 @@ static void process(keyevent_t event, action_t action)
}; };
} }
} else if (tap_count > 0) { } 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); register_code(action.layer.code);
} }
} else { } 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) static void register_code(uint8_t code)
{ {
if (code == KC_NO) { 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) static void clear_keyboard(void)
{ {
host_clear_mods(); host_clear_mods();

View File

@ -5,55 +5,57 @@
/* Key Action(16bit code) /* 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) Keyboard Keys
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) ACT_LMODS(0000):
0 0 0 1| mods(4) | 0 0 0 0 0 0| 0 0 Rmods Momentary 0000|0000|000000|00 No action
0 0 0 1| mods(4) | 0 0 0 0 0 0| 0 1 Rmods OneShot 0000|mods|000000|00 Left mods Momentary
0 0 0 1| mods(4) | 0 0 0 0 0 0| 1 0 (reserved) 0000|mods|000000|01 Left mods OneShot
0 0 0 1| mods(4) | 0 0 0 0 0 0| 1 1 (reserved) 0000|mods|000000|10 (reserved)
0 0 0 1| mods(4) | keycode(8) Key+Rmod 0000|mods|000000|11 (reserved)
0000|0000| keycode Key
0000|mods| keycode Key+Left mods
ACT_LMODS_TAP(0010) ACT_RMODS(0001):
0 0 1 0| 0 0 0 0| X X X X X X X X (reserved)[00-FF] 0001|0000|000000|00 No action
0 0 1 0| mods(4) | 0 0 0 0 0 0| X X (reserved) 0001|mods|000000|00 Right mods Momentary
0 0 1 0| mods(4) | keycode(8) Lmods+tap Key 0001|mods|000000|01 Right mods OneShot
0 0 1 0| mods(4) | 1 1 1 1| X X X X (reserved)[F0-FF] 0001|mods|000000|10 (reserved)
0001|mods|000000|11 (reserved)
0001|0000| keycode Key
0001|mods| keycode Key+Right mods
ACT_RMODS_TAP(0011) ACT_LMODS_TAP(0010):
0 0 1 1| 0 0 0 0| X X X X X X X X (reserved)[00-FF] 0010|mods| keycode Left mods+tap Key
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 ACT_RMODS_TAP(0011):
0 0 1 1| mods(4) | 1 1 1 1| X X X X (reserved)[F0-FF] 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) Other HID Usage
0 1 0 1| X X X X| keycode(8) Mouse key ---------------
??? TODO: refactor This action handles other usages than keyboard.
0 1 0 1| 0 0 X X| accel(5) |cursor(3) Mouse key ACT_USAGE(0100):
0 1 0 1| 0 1 X X| accel(5) |wheel(3) Mouse key 0100|00| usage(10) System control(0x80) - General Desktop page(0x01)
0 1 0 1| 1 0 X X| button(8) Mouse key 0100|01| usage(10) Consumer control(0x01) - Consumer page(0x0C)
0 1 0 1| 1 1 X X| button(8) Mouse key 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
Layer Action
------------
1000|LLLL|0000 0000 set layer L when pressed 1000|LLLL|0000 0000 set layer L when pressed
1001|LLLL|0000 0000 set layer L when released 1001|LLLL|0000 0000 set layer L when released
1010|BBBB|0000 0000 on/off bit B when pressed/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] 1011|0001| keyocde set default layer when released[tap is ignored/not used]
ACT_MACRO(1100) Extensions(11XX)
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) NOTE: NOT FIXED
1 1 0 0| 1 1 1 1| macro-table id(8) Macro record
ACT_COMMAND(1110) ACT_MACRO(1100):
1 1 1 0| option(4) | comamnd id(8) Built-in Command exec 1100|opt | id(8) Macro play
1100|1111| id(8) Macro record
ACT_FUNCTION(1111) ACT_COMMAND(1110):
1 1 1 1| function address(4K range) Function 1110|opt | id(8) Built-in Command exec
ACT_FUNCTION(1111):
1111| address(12) Function
Macro record(dynamicly) Macro record(dynamicly)
Macro play(dynamicly) Macro play(dynamicly)
TODO: modifier + [tap key /w mod] TODO: modifier + [tap key /w mod]
@ -98,19 +103,22 @@ TODO: modifier + [tap key /w mod]
*/ */
enum action_id { enum action_id {
ACT_LMODS = 0b0000, ACT_LMODS = 0b0000,
ACT_RMODS = 0b0001, ACT_RMODS = 0b0001,
ACT_LMOD_TAP = 0b0010, ACT_LMODS_TAP = 0b0010,
ACT_RMOD_TAP = 0b0011, ACT_RMODS_TAP = 0b0011,
ACT_USAGE = 0b0100,
ACT_MOUSEKEY = 0b0101, ACT_USAGE = 0b0100,
ACT_LAYER_PRESSED = 0b1000, ACT_MOUSEKEY = 0b0101,
ACT_LAYER_RELEASED = 0b1001,
ACT_LAYER_BIT = 0b1010, ACT_LAYER_PRESSED = 0b1000,
ACT_LAYER_EXT = 0b1011, ACT_LAYER_RELEASED = 0b1001,
ACT_MACRO = 0b1100, ACT_LAYER_BIT = 0b1010,
ACT_COMMAND = 0b1110, ACT_LAYER_EXT = 0b1011,
ACT_FUNCTION = 0b1111
ACT_MACRO = 0b1100,
ACT_COMMAND = 0b1110,
ACT_FUNCTION = 0b1111
}; };
// TODO: not portable across compiler/endianness? // TODO: not portable across compiler/endianness?
@ -169,20 +177,13 @@ typedef struct {
void action_exec(keyevent_t event); 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 */ /* action_t utility */
/*
#define ACTION_NO { .code = 0 }
#define ACTION(kind, param) { .code = ((kind)<<12 | (param)) }
*/
#define ACTION_NO 0 #define ACTION_NO 0
#define ACTION(kind, param) ((kind)<<12 | (param)) #define ACTION(kind, param) ((kind)<<12 | (param))
#define MOD_BITS(mods) (((mods)>>4 | (mods)) & 0x0F)
/* Key & Mods */ /* Key & Mods */
#define ACTION_KEY(key) ACTION(ACT_LMODS, key) #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_SWITCH(mods, tap) ACTION(ACT_RMODS, (mods)<<8 | 0xF0 | (tap))
#define ACTION_RMODS_TOGGLE(mods, tap) ACTION(ACT_RMODS, (mods)<<8 | 0xF1 | (tap)) #define ACTION_RMODS_TOGGLE(mods, tap) ACTION(ACT_RMODS, (mods)<<8 | 0xF1 | (tap))
/* Mods + Tap key */ /* Mods + Tap key */
#define ACTION_LMODS_TAP(mods, key) ACTION(ACT_LMODS_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,(mods)<<8 | (key)) #define ACTION_RMODS_TAP(mods, key) ACTION(ACT_RMODS_TAP, MOD_BITS(mods)<<8 | (key))
/* Layer Switch */ /* Layer Switch */
#define ACTION_LAYER_SET_ON_PRESSED(layer) ACTION(ACT_LAYER_PRESSED, (layer)<<8 | 0x00) #define ACTION_LAYER_SET_ON_PRESSED(layer) ACTION(ACT_LAYER_PRESSED, (layer)<<8 | 0x00)