diff options
author | tmk <nobody@nowhere> | 2013-02-11 12:48:49 +0900 |
---|---|---|
committer | tmk <nobody@nowhere> | 2013-02-11 12:48:49 +0900 |
commit | 8c80deb775ac151001dc1592a2e94e8677b49964 (patch) | |
tree | aa62c5cbef8419d9cdded26440cfdb42f7d806fd /common | |
parent | 4701b08b71b187cb3d015bec7e05d30e35c344ac (diff) | |
parent | 39d093339393e1758eede06f736b1f99a68ac74c (diff) | |
download | qmk_firmware-8c80deb775ac151001dc1592a2e94e8677b49964.tar.gz |
Merge branch 'keymap2'
Conflicts:
common/keyboard.c
Diffstat (limited to 'common')
-rw-r--r-- | common/action.c | 883 | ||||
-rw-r--r-- | common/action.h | 331 | ||||
-rw-r--r-- | common/action_macro.c | 67 | ||||
-rw-r--r-- | common/action_macro.h | 107 | ||||
-rw-r--r-- | common/command.c | 17 | ||||
-rw-r--r-- | common/debug.h | 1 | ||||
-rw-r--r-- | common/host.c | 13 | ||||
-rw-r--r-- | common/host.h | 7 | ||||
-rw-r--r-- | common/keyboard.c | 580 | ||||
-rw-r--r-- | common/keyboard.h | 38 | ||||
-rw-r--r-- | common/keycode.h | 55 | ||||
-rw-r--r-- | common/keymap.c | 73 | ||||
-rw-r--r-- | common/keymap.h | 20 | ||||
-rw-r--r-- | common/print.c | 7 | ||||
-rw-r--r-- | common/print.h | 2 | ||||
-rw-r--r-- | common/report.h | 44 |
16 files changed, 1634 insertions, 611 deletions
diff --git a/common/action.c b/common/action.c new file mode 100644 index 000000000..6d5336752 --- /dev/null +++ b/common/action.c @@ -0,0 +1,883 @@ +/* +Copyright 2012,2013 Jun Wako <wakojun@gmail.com> + +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 <http://www.gnu.org/licenses/>. +*/ +#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" + + +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); +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); + + +/* + * Tapping + */ +/* period of tapping(ms) */ +#ifndef TAPPING_TERM +#define TAPPING_TERM 200 +#endif + +/* tap count needed for toggling a feature */ +#ifndef TAPPING_TOGGLE +#define TAPPING_TOGGLE 5 +#endif + +/* stores a key event of current tap. */ +static keyrecord_t tapping_key = {}; + +#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_TAPPING_TERM(e) (TIMER_DIFF_16(e.time, tapping_key.event.time) < TAPPING_TERM) + + +/* + * 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; + +static bool waiting_buffer_enq(keyrecord_t record) +{ + if (IS_NOEVENT(record.event)) { + return true; + } + + if ((waiting_buffer_head + 1) % WAITING_BUFFER_SIZE == waiting_buffer_tail) { + debug("waiting_buffer_enq: Over flow.\n"); + return false; + } + + 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; +} + +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) { + if (KEYEQ(event.key, waiting_buffer[i].event.key) && event.pressed != waiting_buffer[i].event.pressed) { + return true; + } + } + return false; +} +#endif + +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; + } + return false; +} + + +/* 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; +} + + + +void action_exec(keyevent_t event) +{ + if (!IS_NOEVENT(event)) { + 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: "); debug_record(record); debug("\n"); + } + } else { + // 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){}; + } + } + + // 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_record(waiting_buffer[waiting_buffer_tail]); debug("\n\n"); + } else { + break; + } + } + if (!IS_NOEVENT(event)) { + debug("\n"); + } +} + +static void process_action(keyrecord_t *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); + //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 */ + case ACT_LMODS: + case ACT_RMODS: + { + 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); + } + } + break; + case ACT_LMODS_TAP: + case ACT_RMODS_TAP: + { + 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 == TAPPING_TOGGLE) { + 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 { + 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); + } + } + 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; + + /* other HID usage */ + case ACT_USAGE: +#ifdef EXTRAKEY_ENABLE + switch (action.usage.page) { + case PAGE_SYSTEM: + if (event.pressed) { + host_system_send(action.usage.code); + } else { + host_system_send(0); + } + break; + case PAGE_CONSUMER: + if (event.pressed) { + host_consumer_send(action.usage.code); + } else { + host_consumer_send(0); + } + break; + } +#endif + break; + + /* Mouse key */ + 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; + + /* Layer key */ + case ACT_LAYER: + switch (action.layer.code) { + case LAYER_MOMENTARY: /* momentary */ + if (event.pressed) { + layer_switch(action.layer.val); + } + else { + layer_switch(default_layer); + } + break; + 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); + } + } else { + if (tap_count >= TAPPING_TOGGLE) { + debug("LAYER_PRESSED: tap toggle.\n"); + layer_switch(action.layer.val); + } + } + break; + case LAYER_CHANGE_DEFAULT: /* change default layer */ + if (event.pressed) { + default_layer = action.layer.val; + layer_switch(default_layer); + } + break; + default: /* switch layer on hold and key on tap*/ + if (event.pressed) { + 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.val); + } + } else { + if (tap_count > 0) { + debug("LAYER_PRESSED: Tap: unregister_code\n"); + unregister_code(action.layer.code); + } else { + //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; + } + break; + case ACT_LAYER_BIT: + switch (action.layer.code) { + case LAYER_MOMENTARY: /* momentary */ + if (event.pressed) { + layer_switch(current_layer ^ action.layer.val); + } else { + layer_switch(current_layer ^ action.layer.val); + } + break; + case LAYER_ON_PRESS: + if (event.pressed) { + layer_switch(current_layer ^ action.layer.val); + } + break; + case LAYER_ON_RELEASE: + if (!event.pressed) { + layer_switch(current_layer ^ action.layer.val); + } + break; + 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"); + 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.val); + } + } + break; + case 0xFF: + // change default layer + if (event.pressed) { + default_layer = current_layer ^ action.layer.val; + layer_switch(default_layer); + } else { + default_layer = current_layer ^ action.layer.val; + layer_switch(default_layer); + } + break; + default: + // with tap key + if (event.pressed) { + if (IS_TAPPING_KEY(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.val); + } + } else { + if (IS_TAPPING_KEY(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.val); + } + } + break; + } + break; + + /* Extentions */ + case ACT_MACRO: + break; + case ACT_COMMAND: + break; + case ACT_FUNCTION: + // TODO + keymap_call_function(record, action.func.id, action.func.opt); + break; + default: + break; + } +} + +/* Tapping + * + * Rule: Tap key is typed(pressed and released) within TAPPING_TERM. + * (without interfering by typing other key) + */ +/* return true when key event is processed or consumed. */ +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(0->1).\n"); + tapping_key.tap_count = 1; + debug_tapping_key(); + process_action(&tapping_key); + + // enqueue + keyp->tap_count = tapping_key.tap_count; + return false; + } +#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\n"); + process_action(&tapping_key); + tapping_key = (keyrecord_t){}; + debug_tapping_key(); + + // enqueue + return false; + } +#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; + process_action(keyp); + tapping_key = *keyp; + debug_tapping_key(); + return true; + } + else if (is_tap_key(keyp->event.key) && event.pressed) { + 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; + waiting_buffer_scan_tap(); + debug_tapping_key(); + return true; + } + else { + if (!IS_NOEVENT(keyp->event)) { + debug("Tapping: key event while last tap(>0).\n"); + } + process_action(keyp); + return true; + } + } + } + // after TAPPING_TERM + else { + if (tapping_key.tap_count == 0) { + 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. last timeout tap release(>0)."); + keyp->tap_count = tapping_key.tap_count; + process_action(keyp); + tapping_key = (keyrecord_t){}; + return true; + } + 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; + } + } + } + } 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; + 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"); + process_action(keyp); + return true; + } + } else { + // timeout. no sequential tap. + debug("Tapping: End(Timeout after releasing last tap): "); + debug_event(event); debug("\n"); + tapping_key = (keyrecord_t){}; + debug_tapping_key(); + return false; + } + } + // not tapping satate + else { + if (event.pressed && is_tap_key(event.key)) { + debug("Tapping: Start(Press tap key).\n"); + tapping_key = *keyp; + waiting_buffer_scan_tap(); + debug_tapping_key(); + return true; + } else { + process_action(keyp); + return true; + } + } +} + +/* 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; + } + } +} + + + +/* + * Utilities for actions. + */ +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)) 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(); + } + } + else if IS_MOD(code) { + host_add_mods(MOD_BIT(code)); + host_send_keyboard_report(); + } +} + +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(); + } +} + +void add_mods(uint8_t mods) +{ + if (mods) { + host_add_mods(mods); + host_send_keyboard_report(); + } +} + +void del_mods(uint8_t mods) +{ + if (mods) { + host_del_mods(mods); + host_send_keyboard_report(); + } +} + +void set_mods(uint8_t mods) +{ + host_set_mods(mods); + host_send_keyboard_report(); +} + +void clear_keyboard(void) +{ + host_clear_mods(); + clear_keyboard_but_mods(); +} + +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 +} + +bool sending_anykey(void) +{ + return (host_has_anykey() || host_mouse_in_use() || + host_last_sysytem_report() || host_last_consumer_report()); +} + +void layer_switch(uint8_t new_layer) +{ + if (current_layer != new_layer) { + 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 + // NOTE: update mods with full scan of matrix? if modifier changes between layers + } +} + +bool is_tap_key(key_t key) +{ + 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: + return true; + case ACT_LAYER: + case ACT_LAYER_BIT: + switch (action.layer.code) { + case LAYER_MOMENTARY: + case LAYER_ON_PRESS: + case LAYER_ON_RELEASE: + case LAYER_DEFAULT: + return false; + case LAYER_TAP_TOGGLE: + default: /* tap key */ + return true; + } + return false; + case ACT_FUNCTION: + if (action.func.opt & FUNC_TAP) { return true; } + return false; + } + 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: debug("ACT_LAYER"); break; + case ACT_LAYER_BIT: debug("ACT_LAYER_BIT"); 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"); +} diff --git a/common/action.h b/common/action.h new file mode 100644 index 000000000..9b559cb18 --- /dev/null +++ b/common/action.h @@ -0,0 +1,331 @@ +/* +Copyright 2012,2013 Jun Wako <wakojun@gmail.com> + +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 <http://www.gnu.org/licenses/>. +*/ +#ifndef ACTION_H +#define ACTION_H + +#include "keyboard.h" +#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). + * AVR looks like a little endian in avr-gcc. + * + * NOTE: 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; + uint8_t id :4; + } kind; + struct action_key { + uint8_t code :8; + uint8_t mods :4; + uint8_t kind :4; + } key; + struct action_layer { + uint8_t code :8; + uint8_t val :4; + uint8_t kind :4; + } layer; + struct action_usage { + uint16_t code :10; + uint8_t page :2; + uint8_t kind :4; + } usage; + struct action_command { + uint8_t id :8; + uint8_t opt :4; + uint8_t kind :4; + } command; + struct action_function { + uint8_t id :8; + uint8_t opt :4; + uint8_t kind :4; + } func; +} action_t; + + +/* + * 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(key_t key); +bool waiting_buffer_has_anykey_pressed(void); + + + + +/* + * Action codes + * ============ + * 16bit code: action_kind(4bit) + action_parameter(12bit) + * +Keyboard Keys +------------- +ACT_LMODS(0000): +0000|0000|000000|00 No action +0000|0000| keycode Key +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 +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 + + +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 +---------- +TODO: can be combined with 'Other HID Usage'? to save action kind id. +ACT_MOUSEKEY(0110): +0101|XXXX| keycode Mouse key + + +Layer Actions +------------- +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) +---------------- +NOTE: NOT FIXED + +ACT_MACRO(1100): +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? + + */ +enum action_kind_id { + ACT_LMODS = 0b0000, + ACT_RMODS = 0b0001, + ACT_LMODS_TAP = 0b0010, + ACT_RMODS_TAP = 0b0011, + + ACT_USAGE = 0b0100, + ACT_MOUSEKEY = 0b0101, + + ACT_LAYER = 0b1000, + ACT_LAYER_BIT = 0b1001, + + ACT_MACRO = 0b1100, + ACT_COMMAND = 0b1110, + ACT_FUNCTION = 0b1111 +}; + + +/* action utility */ +#define ACTION_NO 0 +#define ACTION(kind, param) ((kind)<<12 | (param)) +#define MODS4(mods) (((mods)>>4 | (mods)) & 0x0F) + +/* 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 */ +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 | 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 | 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 | 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 | 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 */ +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) + +/* 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 */ +enum function_opts { + 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) + +#endif /* ACTION_H */ 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 <wakojun@gmail.com> + +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 <http://www.gnu.org/licenses/>. +*/ +#include <util/delay.h> +#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 <wakojun@gmail.com> + +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 <http://www.gnu.org/licenses/>. +*/ +#ifndef ACTION_MACRO_H +#define ACTION_MACRO_H +#include <stdint.h> +#include <avr/pgmspace.h> + + +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/command.c b/common/command.c index 5cdd168d4..6d4e4c642 100644 --- a/common/command.c +++ b/common/command.c @@ -19,6 +19,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. #include <util/delay.h> #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; @@ -556,18 +556,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/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 <http://www.gnu.org/licenses/>. #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/host.c b/common/host.c index 261ec6472..6ed3d780f 100644 --- a/common/host.c +++ b/common/host.c @@ -127,14 +127,19 @@ void host_clear_keys(void) } } -void host_add_mod_bit(uint8_t mod) +uint8_t host_get_mods(void) { - keyboard_report->mods |= mod; + return keyboard_report->mods; } -void host_del_mod_bit(uint8_t mod) +void host_add_mods(uint8_t mods) { - keyboard_report->mods &= ~mod; + keyboard_report->mods |= mods; +} + +void host_del_mods(uint8_t mods) +{ + keyboard_report->mods &= ~mods; } void host_set_mods(uint8_t mods) diff --git a/common/host.h b/common/host.h index 841798796..7c4f06601 100644 --- a/common/host.h +++ b/common/host.h @@ -52,10 +52,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); -void host_add_mod_bit(uint8_t mod); -void host_del_mod_bit(uint8_t mod); + +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 2dee51d4b..5e9945baf 100644 --- a/common/keyboard.c +++ b/common/keyboard.c @@ -1,5 +1,5 @@ /* -Copyright 2011,2012 Jun Wako <wakojun@gmail.com> +Copyright 2011,2012,2013 Jun Wako <wakojun@gmail.com> 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,536 +26,39 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. #include "command.h" #include "util.h" #include "sendchar.h" +#include "bootloader.h" #ifdef MOUSEKEY_ENABLE #include "mousekey.h" #endif -#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) - -#define LAYER_DELAY 250 - -typedef enum keykind { - NONE, - FN_DOWN, FN_UP, - FNK_DOWN, FNK_UP, - KEY_DOWN, KEY_UP, - MOD_DOWN, MOD_UP, -} keykind_t; - -typedef enum { IDLE, DELAYING, WAITING, PRESSING } kbdstate_t; - - -#ifdef KEYMAP_DEFAULT_LAYER -uint8_t default_layer = KEYMAP_DEFAULT_LAYER; -uint8_t current_layer = KEYMAP_DEFAULT_LAYER; -#else -uint8_t default_layer = 0; -uint8_t current_layer = 0; -#endif - -/* keyboard internal states */ -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 inline keykind_t get_keykind(uint8_t code, bool pressed) -{ - if IS_KEY(code) return (pressed ? KEY_DOWN : KEY_UP); - if IS_MOD(code) return (pressed ? MOD_DOWN : MOD_UP); - if IS_FN(code) { - if (keymap_fn_keycode(FN_INDEX(code))) - return (pressed ? FNK_DOWN : FNK_UP); - else - return (pressed ? FN_DOWN : FN_UP); - } - if IS_MOUSEKEY(code) return (pressed ? KEY_DOWN : KEY_UP); - if IS_SYSTEM(code) return (pressed ? KEY_DOWN : KEY_UP); - if IS_CONSUMER(code) return (pressed ? KEY_DOWN : KEY_UP); - return NONE; -} - -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 -} - -static void clear_keyboard_but_mods(void) -{ - host_clear_keys(); - host_send_keyboard_report(); - - host_system_send(0); - host_consumer_send(0); - -#ifdef MOUSEKEY_ENABLE - mousekey_clear(); - mousekey_send(); -#endif -} - -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) -{ - if (!IS_FN(code)) return; - fn_state_bits |= FN_BIT(code); - uint8_t new_layer = (fn_state_bits ? keymap_fn_layer(biton(fn_state_bits)) : default_layer); - if (current_layer != new_layer) { - Kdebug("Layer Switch(on): "); Kdebug_hex(current_layer); - Kdebug(" -> "); Kdebug_hex(new_layer); Kdebug("\n"); - - clear_keyboard_but_mods(); - current_layer = new_layer; - } -} - -static bool layer_switch_off(uint8_t code) -{ - if (!IS_FN(code)) return false; - fn_state_bits &= ~FN_BIT(code); - uint8_t new_layer = (fn_state_bits ? keymap_fn_layer(biton(fn_state_bits)) : default_layer); - if (current_layer != new_layer) { - Kdebug("Layer Switch(off): "); Kdebug_hex(current_layer); - Kdebug(" -> "); Kdebug_hex(new_layer); Kdebug("\n"); - - clear_keyboard_but_mods(); - current_layer = new_layer; - return true; - } - return false; -} - -static void register_code(uint8_t code) -{ - if IS_KEY(code) { - if (!command_proc(code)) { - host_add_key(code); - host_send_keyboard_report(); - } - } - else if IS_MOD(code) { - host_add_mod_bit(MOD_BIT(code)); - host_send_keyboard_report(); - } - else if IS_FN(code) { - if (!command_proc(keymap_fn_keycode(FN_INDEX(code)))) { - host_add_key(keymap_fn_keycode(FN_INDEX(code))); - host_send_keyboard_report(); - } - } - else if IS_MOUSEKEY(code) { -#ifdef MOUSEKEY_ENABLE - mousekey_on(code); - mousekey_send(); -#endif - } - 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); - } - -} - -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_mod_bit(MOD_BIT(code)); - host_send_keyboard_report(); - } - else if IS_FN(code) { - host_del_key(keymap_fn_keycode(FN_INDEX(code))); - host_send_keyboard_report(); - } - else if IS_MOUSEKEY(code) { -#ifdef MOUSEKEY_ENABLE - mousekey_off(code); - mousekey_send(); -#endif - } - else if IS_CONSUMER(code) { - host_consumer_send(0x0000); - } - else if IS_SYSTEM(code) { - host_system_send(0x0000); - } -} - -/* - * - * 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) - */ -#define NEXT(state) do { \ - Kdebug("NEXT: "); Kdebug_P(state_str(kbdstate)); \ - kbdstate = state; \ - Kdebug(" -> "); Kdebug_P(state_str(kbdstate)); Kdebug("\n"); \ -} while (0) - -static inline void process_key(keyevent_t event) -{ - uint8_t code = keymap_get_keycode(current_layer, event.key.row, event.key.col); - keykind_t kind = get_keykind(code, event.pressed); - - uint8_t tmp_mods; - - Kdebug("state: "); Kdebug_P(state_str(kbdstate)); - Kdebug(" kind: "); Kdebug_hex(kind); - Kdebug(" code: "); Kdebug_hex(code); - if (event.pressed) { Kdebug("d"); } else { Kdebug("u"); } - Kdebug("\n"); - - switch (kbdstate) { - case IDLE: - switch (kind) { - case FN_DOWN: - layer_switch_on(code); - break; - case FN_UP: - layer_switch_off(code); - break; - case FNK_DOWN: - // repeat Fn alt key when press Fn key down, up then down again quickly - if (KEYEQ(delayed_fn.event.key, event.key) && - timer_elapsed(delayed_fn.time) < LAYER_DELAY) { - register_code(code); - NEXT(PRESSING); - } else { - delayed_fn = (keyrecord_t) { - .event = event, - .code = code, - .mods = keyboard_report->mods, - .time = timer_read() - }; - NEXT(DELAYING); - } - break; - case FNK_UP: - layer_switch_off(code); - break; - case KEY_DOWN: - register_code(code); - NEXT(PRESSING); - break; - case MOD_DOWN: - register_code(code); - break; - case KEY_UP: - case MOD_UP: - unregister_code(code); - break; - default: - break; - } - break; - case PRESSING: - switch (kind) { - case FN_DOWN: - // ignored when any key is pressed - break; - case FN_UP: - if (layer_switch_off(code)) - NEXT(IDLE); - break; - case FNK_DOWN: - register_code(code); - break; - case FNK_UP: - if (layer_switch_off(code)) { - NEXT(IDLE); - } else { - unregister_code(code); - if (!anykey_sent_to_host()) - NEXT(IDLE); - } - break; - case KEY_DOWN: - case MOD_DOWN: - register_code(code); - break; - case KEY_UP: - case MOD_UP: - unregister_code(code); - if (!anykey_sent_to_host()) - NEXT(IDLE); - break; - default: - break; - } - break; - case DELAYING: - switch (kind) { - case FN_DOWN: - case FNK_DOWN: - case KEY_DOWN: - waiting_key = (keyrecord_t) { - .event = event, - .code = code, - .mods = keyboard_report->mods, - .time = timer_read() - }; - NEXT(WAITING); - break; - case MOD_DOWN: - register_code(code); - break; - case FN_UP: - if (layer_switch_off(code)) - NEXT(IDLE); - break; - case FNK_UP: - if (code == delayed_fn.code) { - // type Fn with alt keycode - // restore the mod status at the time of pressing Fn key - tmp_mods = keyboard_report->mods; - host_set_mods(delayed_fn.mods); - register_code(delayed_fn.code); - unregister_code(delayed_fn.code); - host_set_mods(tmp_mods); - NEXT(IDLE); - } else { - if (layer_switch_off(code)) - NEXT(IDLE); - } - break; - case KEY_UP: - case MOD_UP: - unregister_code(code); - break; - default: - break; - } - break; - case WAITING: - switch (kind) { - case FN_DOWN: - case FNK_DOWN: - case KEY_DOWN: - 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; - case MOD_DOWN: - register_code(code); - break; - case FN_UP: - if (layer_switch_off(code)) - NEXT(IDLE); - break; - case FNK_UP: - if (code == delayed_fn.code) { - // alt down, key down, alt up - 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); - unregister_code(delayed_fn.code); - host_set_mods(tmp_mods); - NEXT(IDLE); - } else { - if (layer_switch_off(code)) - NEXT(IDLE); - } - break; - case KEY_UP: - 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); - process_key(waiting_key.event); - host_set_mods(tmp_mods); - process_key(event); - } else { - unregister_code(code); - } - break; - case MOD_UP: - unregister_code(code); - break; - default: - break; - } - break; - } -} - void keyboard_init(void) { - // TODO: to enable debug print magic key bind on boot time - // TODO: configuration of sendchar impl print_sendchar_func = sendchar; timer_init(); matrix_init(); + + /* boot magic keys goes here */ + matrix_scan(); +#ifdef IS_BOOTMAGIC_BOOTLOADER + /* kick up bootloader */ + if (IS_BOOTMAGIC_BOOTLOADER()) bootloader_jump(); +#endif +#ifdef IS_BOOTMAGIC_DEBUG + if (IS_BOOTMAGIC_DEBUG()) debug_enable = true; +#endif + #ifdef PS2_MOUSE_ENABLE ps2_mouse_init(); #endif } +/* + * Do keyboard routine jobs: scan mantrix, light LEDs, ... + * This is repeatedly called as fast as possible. + */ void keyboard_task(void) { static matrix_row_t matrix_prev[MATRIX_ROWS]; @@ -572,9 +75,10 @@ void keyboard_task(void) for (uint8_t c = 0; c < MATRIX_COLS; c++) { if (matrix_change & ((matrix_row_t)1<<c)) { - process_key((keyevent_t){ - .key = (key_t){ .row = r, .col = c }, - .pressed = (matrix_row & ((matrix_row_t)1<<c)) + action_exec((keyevent_t){ + .key.pos = (keypos_t){ .row = r, .col = c }, + .pressed = (matrix_row & (1<<c)), + .time = (timer_read() | 1) /* time should not be 0 */ }); // record a processed key matrix_prev[r] ^= ((matrix_row_t)1<<c); @@ -584,55 +88,19 @@ void keyboard_task(void) } } } - MATRIX_LOOP_END: - - // layer switch when delay term elapses - if (kbdstate == DELAYING || kbdstate == WAITING) { - if (timer_elapsed(delayed_fn.time) > LAYER_DELAY) { - if (kbdstate == DELAYING) { - layer_switch_on(delayed_fn.code); - NEXT(IDLE); - } - if (kbdstate == WAITING) { - layer_switch_on(delayed_fn.code); - NEXT(IDLE); - uint8_t tmp_mods = keyboard_report->mods; - host_set_mods(waiting_key.mods); - process_key(waiting_key.event); - host_set_mods(tmp_mods); - } - } - } + // call with pseudo tick event when no real key event. + action_exec(TICK); +MATRIX_LOOP_END: #ifdef MOUSEKEY_ENABLE // mousekey repeat & acceleration mousekey_task(); #endif - - // FAIL SAFE: clear all key if no key down - if (matrix_change) { - matrix_row_t is_matrix_on = 0; - for (int r = 0; r < MATRIX_ROWS; r++) { - is_matrix_on |= matrix_get_row(r); - } - if (!is_matrix_on) { - Kdebug("FAIL SAFE: clear all keys(default layer).\n"); - clear_keyboard(); - current_layer = default_layer; - fn_state_bits = 0; - delayed_fn = (keyrecord_t){}; - waiting_key = (keyrecord_t){}; - NEXT(IDLE); - } - } - // update LED if (led_status != host_keyboard_leds()) { led_status = host_keyboard_leds(); keyboard_set_leds(led_status); } - - return; } void keyboard_set_leds(uint8_t leds) diff --git a/common/keyboard.h b/common/keyboard.h index 2353805e1..e1cab3119 100644 --- a/common/keyboard.h +++ b/common/keyboard.h @@ -1,5 +1,5 @@ /* -Copyright 2011 Jun Wako <wakojun@gmail.com> +Copyright 2011,2012,2013 Jun Wako <wakojun@gmail.com> 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,28 +26,44 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. extern "C" { #endif +/* key matrix position */ typedef struct { - uint8_t row; uint8_t col; + uint8_t row; +} keypos_t; + +// TODO: need raw? keypos_t -> key_t? +typedef union { + uint16_t raw; + keypos_t pos; } key_t; +/* key event */ typedef struct { key_t key; bool pressed; + uint16_t time; } keyevent_t; -typedef struct { - keyevent_t event; - uint8_t code; - uint8_t mods; - uint16_t time; -} keyrecord_t; +/* equivalent test of key_t */ +#define KEYEQ(keya, keyb) ((keya).raw == (keyb).raw) -#define KEYEQ(keya, keyb) (keya.row == keyb.row && keya.col == keyb.col) +/* (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() | 1) \ +} -extern uint8_t current_layer; -extern uint8_t default_layer; void keyboard_init(void); void keyboard_task(void); diff --git a/common/keycode.h b/common/keycode.h index f9331cdbf..4f57a5887 100644 --- a/common/keycode.h +++ b/common/keycode.h @@ -28,14 +28,14 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. #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) @@ -43,6 +43,8 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. #define MOD_INDEX(code) ((code) & 0x07) #define FN_BIT(code) (1<<FN_INDEX(code)) #define FN_INDEX(code) ((code) - KC_FN0) +#define FN_MIN KC_FN0 +#define FN_MAX KC_FN31 /* @@ -388,11 +390,10 @@ enum internal_special_keycodes { /* System Control */ KC_SYSTEM_POWER = 0xA5, KC_SYSTEM_SLEEP, - KC_SYSTEM_WAKE, /* 0xA7 */ - /* 0xA8-AF */ + KC_SYSTEM_WAKE, - /* Consumer Page */ - KC_AUDIO_MUTE = 0xB0, + /* Media Control */ + KC_AUDIO_MUTE, KC_AUDIO_VOL_UP, KC_AUDIO_VOL_DOWN, KC_MEDIA_NEXT_TRACK, @@ -408,21 +409,47 @@ enum internal_special_keycodes { KC_WWW_BACK, KC_WWW_FORWARD, KC_WWW_STOP, - KC_WWW_REFRESH, /* 0xC0 */ - KC_WWW_FAVORITES, /* 0xC1 */ - /* 0xC2-DF vacant for future use */ + KC_WWW_REFRESH, + KC_WWW_FAVORITES, /* 0xB9 */ - /* 0xE0-E7 for Modifiers. DO NOT USE. */ - - /* Layer Switching */ - KC_FN0 = 0xE8, + /* Fn key */ + KC_FN0 = 0xC0, KC_FN1, KC_FN2, KC_FN3, KC_FN4, KC_FN5, KC_FN6, - KC_FN7, /* 0xEF */ + KC_FN7, + KC_FN8, + KC_FN9, + KC_FN10, + KC_FN11, + KC_FN12, + KC_FN13, + KC_FN14, + KC_FN15, + + KC_FN16 = 0xD0, + KC_FN17, + KC_FN18, + KC_FN19, + KC_FN20, + KC_FN21, + KC_FN22, + KC_FN23, + KC_FN24, + KC_FN25, + KC_FN26, + KC_FN27, + KC_FN28, + KC_FN29, + KC_FN30, + KC_FN31, /* 0xDF */ + + /**************************************/ + /* 0xE0-E7 for Modifiers. DO NOT USE. */ + /**************************************/ /* Mousekey */ KC_MS_UP = 0xF0, diff --git a/common/keymap.c b/common/keymap.c new file mode 100644 index 000000000..8302c2704 --- /dev/null +++ b/common/keymap.c @@ -0,0 +1,73 @@ +/* +Copyright 2013 Jun Wako <wakojun@gmail.com> + +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 <http://www.gnu.org/licenses/>. +*/ +#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 keymap_call_function(keyrecord_t *event, uint8_t id, uint8_t opt) +{ +} diff --git a/common/keymap.h b/common/keymap.h index 7dfd6c2a1..30d73f797 100644 --- a/common/keymap.h +++ b/common/keymap.h @@ -20,9 +20,26 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. #include <stdint.h> #include <stdbool.h> +#include "action.h" -/* keycode in specific layer */ +// TODO: move to action.h? +/* layer used currently */ +extern uint8_t current_layer; +/* layer to return or start with */ +extern uint8_t default_layer; + + +/* 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, uint8_t opt); + + +#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 */ @@ -30,5 +47,6 @@ 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); +#endif #endif diff --git a/common/print.c b/common/print.c index 84400a1df..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,6 +136,12 @@ 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<<i)) ? '1' : '0'); + } +} void print_bin8(uint8_t data) { diff --git a/common/print.h b/common/print.h index 9c31b24a2..b22509477 100644 --- a/common/print.h +++ b/common/print.h @@ -87,11 +87,13 @@ void print_dec(uint16_t data); void print_decs(int16_t data); /* hex */ +void print_hex4(uint8_t data); void print_hex8(uint8_t data); void print_hex16(uint16_t data); void print_hex32(uint32_t data); /* binary */ +void print_bin4(uint8_t data); void print_bin8(uint8_t data); void print_bin16(uint16_t data); void print_bin32(uint32_t data); diff --git a/common/report.h b/common/report.h index a73e0aba1..0995189b3 100644 --- a/common/report.h +++ b/common/report.h @@ -1,5 +1,5 @@ /* -Copyright 2011 Jun Wako <wakojun@gmail.com> +Copyright 2011,2012 Jun Wako <wakojun@gmail.com> 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,6 +19,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. #define REPORT_H #include <stdint.h> +#include "keycode.h" /* report id */ @@ -33,8 +34,9 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. #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 @@ -42,10 +44,12 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. #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 @@ -53,20 +57,20 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. #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 @@ -97,6 +101,34 @@ typedef struct { int8_t h; } __attribute__ ((packed)) report_mouse_t; + +/* 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 } #endif |