]> git.donarmstrong.com Git - tmk_firmware.git/blobdiff - common/keyboard.c
Fix build option MOUSEKEY_ENABLE.
[tmk_firmware.git] / common / keyboard.c
index 328941df3f68062bc29d1916337e0bb88f5d503b..43abf4236f62d6f693f242d99b31d4c7a7cd974d 100644 (file)
@@ -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"
@@ -81,17 +81,41 @@ static inline keykind_t get_keykind(uint8_t code, bool pressed)
     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))) {
-        // clear all key execpt Mod key
-        host_clear_all_keys_but_mods();
-        host_system_send(0);
-        host_consumer_send(0);
-        mousekey_clear();
-        mousekey_send();
+        clear_keyboard_but_mods();
 
         debug("Layer Switch(on): "); debug_hex(current_layer);
         current_layer = keymap_fn_layer(FN_INDEX(code));
@@ -104,12 +128,7 @@ 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))) {
-        // clear all key execpt Mod key
-        host_clear_all_keys_but_mods();
-        host_system_send(0);
-        host_consumer_send(0);
-        mousekey_clear();
-        mousekey_send();
+        clear_keyboard_but_mods();
 
         debug("Layer Switch(off): "); debug_hex(current_layer);
         current_layer = keymap_fn_layer(biton(fn_state_bits));
@@ -117,11 +136,6 @@ static void layer_switch_off(uint8_t code)
     }
 }
 
-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)
 {
@@ -129,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;
                 }
             }
@@ -140,7 +154,6 @@ static inline bool is_anykey_down(void)
 
 static void register_code(uint8_t code)
 {
-debug("register_code\n");
     if IS_KEY(code) {
         host_add_key(code);
         host_send_keyboard_report();
@@ -150,81 +163,81 @@ debug("register_code\n");
         host_send_keyboard_report();
     }
     else if IS_MOUSEKEY(code) {
+#ifdef MOUSEKEY_ENABLE
         mousekey_on(code);
         mousekey_send();
+#endif
     }
     else if IS_CONSUMER(code) {
-debug("consumer\n");
         uint16_t usage = 0;
         switch (code) {
-            case KB_AUDIO_MUTE:
+            case KC_AUDIO_MUTE:
                 usage = AUDIO_MUTE;
                 break;
-            case KB_AUDIO_VOL_UP:
+            case KC_AUDIO_VOL_UP:
                 usage = AUDIO_VOL_UP;
                 break;
-            case KB_AUDIO_VOL_DOWN:
+            case KC_AUDIO_VOL_DOWN:
                 usage = AUDIO_VOL_DOWN;
                 break;
-            case KB_MEDIA_NEXT_TRACK:
+            case KC_MEDIA_NEXT_TRACK:
                 usage = TRANSPORT_NEXT_TRACK;
                 break;
-            case KB_MEDIA_PREV_TRACK:
+            case KC_MEDIA_PREV_TRACK:
                 usage = TRANSPORT_PREV_TRACK;
                 break;
-            case KB_MEDIA_STOP:
+            case KC_MEDIA_STOP:
                 usage = TRANSPORT_STOP;
                 break;
-            case KB_MEDIA_PLAY_PAUSE:
+            case KC_MEDIA_PLAY_PAUSE:
                 usage = TRANSPORT_PLAY_PAUSE;
                 break;
-            case KB_MEDIA_SELECT:
+            case KC_MEDIA_SELECT:
                 usage = AL_CC_CONFIG;
                 break;
-            case KB_MAIL:
+            case KC_MAIL:
                 usage = AL_EMAIL;
                 break;
-            case KB_CALCULATOR:
+            case KC_CALCULATOR:
                 usage = AL_CALCULATOR;
                 break;
-            case KB_MY_COMPUTER:
+            case KC_MY_COMPUTER:
                 usage = AL_LOCAL_BROWSER;
                 break;
-            case KB_WWW_SEARCH:
+            case KC_WWW_SEARCH:
                 usage = AC_SEARCH;
                 break;
-            case KB_WWW_HOME:
+            case KC_WWW_HOME:
                 usage = AC_HOME;
                 break;
-            case KB_WWW_BACK:
+            case KC_WWW_BACK:
                 usage = AC_BACK;
                 break;
-            case KB_WWW_FORWARD:
+            case KC_WWW_FORWARD:
                 usage = AC_FORWARD;
                 break;
-            case KB_WWW_STOP:
+            case KC_WWW_STOP:
                 usage = AC_STOP;
                 break;
-            case KB_WWW_REFRESH:
+            case KC_WWW_REFRESH:
                 usage = AC_REFRESH;
                 break;
-            case KB_WWW_FAVORITES:
+            case KC_WWW_FAVORITES:
                 usage = AC_BOOKMARKS;
                 break;
         }
-debug("usage: "); phex16(usage); debug("\n");
         host_consumer_send(usage);
     }
     else if IS_SYSTEM(code) {
         uint16_t usage = 0;
         switch (code) {
-            case KB_SYSTEM_POWER:
+            case KC_SYSTEM_POWER:
                 usage = SYSTEM_POWER_DOWN;
                 break;
-            case KB_SYSTEM_SLEEP:
+            case KC_SYSTEM_SLEEP:
                 usage = SYSTEM_SLEEP;
                 break;
-            case KB_SYSTEM_WAKE:
+            case KC_SYSTEM_WAKE:
                 usage = SYSTEM_WAKE_UP;
                 break;
         }
@@ -244,8 +257,10 @@ 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);
@@ -293,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)); \
@@ -305,13 +320,7 @@ 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;
@@ -389,7 +398,7 @@ static inline void process_key(keyevent_t event)
                 case KEY_UP:
                 case MOD_UP:
                     unregister_code(code);
-                    // no key registered? mousekey, mediakey, systemkey
+                    // TODO: no key registered? mousekey, mediakey, systemkey
                     if (!host_has_anykey())
                         NEXT(IDLE);
                     break;
@@ -502,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)
@@ -526,11 +533,11 @@ 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];
@@ -552,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) {
@@ -572,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;
 }