]> git.donarmstrong.com Git - tmk_firmware.git/blobdiff - common/keyboard.c
Fix build option MOUSEKEY_ENABLE.
[tmk_firmware.git] / common / keyboard.c
old mode 100755 (executable)
new mode 100644 (file)
index 9f0c276..43abf42
@@ -19,7 +19,7 @@ along with this program.  If not, see <http://www.gnu.org/licenses/>.
 #include "keymap.h"
 #include "host.h"
 #include "led.h"
-#include "usb_keycodes.h"
+#include "keycode.h"
 #include "timer.h"
 #include "print.h"
 #include "debug.h"
@@ -41,8 +41,6 @@ typedef enum keykind {
     FNK_DOWN, FNK_UP,
     KEY_DOWN, KEY_UP,
     MOD_DOWN, MOD_UP,
-    MOUSEKEY_DOWN, MOUSEKEY_UP,
-    DELAY
 } keykind_t;
 
 typedef enum { IDLE, DELAYING, WAITING, PRESSING } kbdstate_t;
@@ -69,24 +67,56 @@ static const char *state_str(kbdstate_t state)
 
 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_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 ? MOUSEKEY_DOWN : MOUSEKEY_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 void layer_switch_on(uint8_t code)
 {
     if (!IS_FN(code)) return;
     fn_state_bits |= FN_BIT(code);
     if (current_layer != keymap_fn_layer(FN_INDEX(code))) {
-        //TODO: clear all key execpt Mod key
+        clear_keyboard_but_mods();
+
         debug("Layer Switch(on): "); debug_hex(current_layer);
         current_layer = keymap_fn_layer(FN_INDEX(code));
         debug(" -> "); debug_hex(current_layer); debug("\n");
@@ -98,18 +128,14 @@ static void layer_switch_off(uint8_t code)
     if (!IS_FN(code)) return;
     fn_state_bits &= ~FN_BIT(code);
     if (current_layer != keymap_fn_layer(biton(fn_state_bits))) {
-        //TODO: clear all key execpt Mod key
+        clear_keyboard_but_mods();
+
         debug("Layer Switch(off): "); debug_hex(current_layer);
         current_layer = keymap_fn_layer(biton(fn_state_bits));
         debug(" -> "); debug_hex(current_layer); debug("\n");
     }
 }
 
-static inline uint8_t get_keycode(key_t key)
-{
-    return keymap_get_keycode(current_layer, key.row, key.col);
-}
-
 // whether any key except modifier is down or not
 static inline bool is_anykey_down(void)
 {
@@ -117,7 +143,7 @@ static inline bool is_anykey_down(void)
         matrix_row_t matrix_row = matrix_get_row(r);
         for (int c = 0; c < MATRIX_COLS; c++) {
             if (matrix_row && (1<<c)) {
-                if (IS_KEY(get_keycode((key_t){ .row = r, .col = c }))) {
+                if (IS_KEY(keymap_get_keycode(current_layer, r, c))) {
                     return true;
                 }
             }
@@ -137,9 +163,87 @@ static void register_code(uint8_t 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)
@@ -153,8 +257,16 @@ static void unregister_code(uint8_t 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);
     }
 }
 
@@ -163,7 +275,7 @@ static void unregister_code(uint8_t code)
  * Event/State|IDLE             DELAYING[f]     WAITING[f,k]        PRESSING
  * -----------+------------------------------------------------------------------
  * Fn  Down   |IDLE(L+)         WAITING(Sk)     WAITING(Sk)         -
- *     Up     |IDLE(L-)         IDLE(L-)        IDLE(L-)            IDLE(L-)  
+ *     Up     |IDLE(L-)         IDLE(L-)        IDLE(L-)            IDLE(L-)
  * Fnk Down   |DELAYING(Sf)     WAITING(Sk)     WAINTING(Sk)        PRESSING(Rf)
  *     Up     |IDLE(L-)         IDLE(Rf,Uf)     IDLE(Rf,Ps,Uf)*3    PRESSING(Uf)
  * Key Down   |PRESSING(Rk)     WAITING(Sk)     WAITING(Sk)         PRESSING(Rk)
@@ -196,9 +308,9 @@ static void unregister_code(uint8_t code)
  *      Sk: store key
  *      Sf: store Fn
  *      Ps: play stored key(Interpret stored key and transit state)
- *      L+: Switch to new layer(*retain* Modifiers only)
- *      L-: Switch back to last layer(*clear* stored key/Fn, *unregister* all Modifier/key)
- *      Ld: Switch back to default layer(*clear* stored key/Fn, *unregister* all Modifier/key)
+ *      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 { \
     debug("NEXT: "); print_P(state_str(kbdstate)); \
@@ -208,24 +320,17 @@ static void unregister_code(uint8_t code)
 
 static inline void process_key(keyevent_t event)
 {
-    
-    /* TODO: ring buffer
-    static keyrecord_t waiting_keys[5];
-    static uint8_t waiting_keys_head = 0;
-    static uint8_t waiting_keys_tail = 0;
-    */
-
-    uint8_t code = get_keycode(event.key);
+    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;
 
-    //debug("kbdstate: "); debug_hex(kbdstate);
     debug("state: "); print_P(state_str(kbdstate));
     debug(" kind: "); debug_hex(kind);
     debug(" code: "); debug_hex(code);
     if (event.pressed) { debug("d"); } else { debug("u"); }
     debug("\n");
+
     switch (kbdstate) {
         case IDLE:
             switch (kind) {
@@ -236,15 +341,25 @@ static inline void process_key(keyevent_t event)
                     layer_switch_off(code);
                     break;
                 case FNK_DOWN:
-                    // store event
-                    delayed_fn = (keyrecord_t) { .event = event, .code = code, .mods = keyboard_report->mods, .time = timer_read() };
-                    NEXT(DELAYING);
+                    // 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(keymap_fn_keycode(FN_INDEX(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:
-                case MOUSEKEY_DOWN:
                     register_code(code);
                     NEXT(PRESSING);
                     break;
@@ -252,7 +367,6 @@ static inline void process_key(keyevent_t event)
                     register_code(code);
                     break;
                 case KEY_UP:
-                case MOUSEKEY_UP:
                 case MOD_UP:
                     unregister_code(code);
                     break;
@@ -273,18 +387,18 @@ static inline void process_key(keyevent_t event)
                     register_code(keymap_fn_keycode(FN_INDEX(code)));
                     break;
                 case FNK_UP:
+                    // can't know whether layer switched or not
+                    layer_switch_off(code);
                     unregister_code(keymap_fn_keycode(FN_INDEX(code)));
                     break;
                 case KEY_DOWN:
                 case MOD_DOWN:
-                case MOUSEKEY_DOWN:
                     register_code(code);
                     break;
                 case KEY_UP:
                 case MOD_UP:
-                case MOUSEKEY_UP:
                     unregister_code(code);
-                    // no key registered? mousekey, mediakey, systemkey
+                    // TODO: no key registered? mousekey, mediakey, systemkey
                     if (!host_has_anykey())
                         NEXT(IDLE);
                     break;
@@ -297,8 +411,12 @@ static inline void process_key(keyevent_t event)
                 case FN_DOWN:
                 case FNK_DOWN:
                 case KEY_DOWN:
-                case MOUSEKEY_DOWN:
-                    waiting_key = (keyrecord_t) { .event = event, .code = code, .mods = keyboard_report->mods, .time = timer_read() };
+                    waiting_key = (keyrecord_t) {
+                        .event = event,
+                        .code = code,
+                        .mods = keyboard_report->mods,
+                        .time = timer_read()
+                    };
                     NEXT(WAITING);
                     break;
                 case MOD_DOWN:
@@ -324,7 +442,6 @@ static inline void process_key(keyevent_t event)
                     }
                     break;
                 case KEY_UP:
-                case MOUSEKEY_UP:
                     unregister_code(code);
                     NEXT(IDLE);
                     break;
@@ -340,7 +457,6 @@ static inline void process_key(keyevent_t event)
                 case FN_DOWN:
                 case FNK_DOWN:
                 case KEY_DOWN:
-                case MOUSEKEY_DOWN:
                     tmp_mods = keyboard_report->mods;
                     host_set_mods(delayed_fn.mods);
                     register_code(keymap_fn_keycode(FN_INDEX(delayed_fn.code)));
@@ -374,7 +490,6 @@ static inline void process_key(keyevent_t event)
                     }
                     break;
                 case KEY_UP:
-                case MOUSEKEY_UP:
                     if (code == waiting_key.code) {
                         layer_switch_on(delayed_fn.code);
                         NEXT(IDLE);
@@ -396,8 +511,6 @@ static inline void process_key(keyevent_t event)
             }
             break;
     }
-
-    // TODO: FAIL SAFE: unregister all keys when no key down
 }
 
 void keyboard_init(void)
@@ -420,16 +533,15 @@ void keyboard_task(void)
     matrix_scan();
     if (command_proc()) {
         debug("COMMAND\n");
-        // TODO: clear all keys
-        host_clear_keyboard_report();
-        host_send_keyboard_report();
+        // TODO: COMMAND state?
+        clear_keyboard();
         return;
     }
+
     for (int r = 0; r < MATRIX_ROWS; r++) {
         matrix_row = matrix_get_row(r);
         matrix_change = matrix_row ^ matrix_prev[r];
         if (matrix_change) {
-            // TODO: print once per scan
             if (debug_matrix) matrix_print();
 
             for (int c = 0; c < MATRIX_COLS; c++) {
@@ -447,7 +559,6 @@ void keyboard_task(void)
         }
     }
     MATRIX_LOOP_END:
-    // TODO: FAIL SAFE: clear all key if no key down
 
     // layer switch when delay term elapses
     if (kbdstate == DELAYING || kbdstate == WAITING) {
@@ -467,9 +578,23 @@ void keyboard_task(void)
         }
     }
 
+#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) {
+            debug("FAIL SAFE: clear all keys.\n");
+            clear_keyboard();
+        }
+    }
+    
     return;
 }