]> git.friedersdorff.com Git - max/tmk_keyboard.git/blobdiff - common/keyboard.c
Rename to hid_liber.
[max/tmk_keyboard.git] / common / keyboard.c
index 7a17a9e38bb37645dcc6ea620944f0e40e8cdbd1..fa22116f177175670e7dc64f76d77e15e9b8710c 100644 (file)
@@ -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
@@ -25,14 +25,16 @@ along with this program.  If not, see <http://www.gnu.org/licenses/>.
 #include "debug.h"
 #include "command.h"
 #include "util.h"
+#include "sendchar.h"
 #ifdef MOUSEKEY_ENABLE
 #include "mousekey.h"
 #endif
-#ifdef EXTRAKEY_ENABLE
-#include <util/delay.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 {
@@ -46,8 +48,13 @@ typedef enum keykind {
 typedef enum { IDLE, DELAYING, WAITING, PRESSING } kbdstate_t;
 
 
-uint8_t current_layer = 0;
+#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;
@@ -120,12 +127,13 @@ 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_keyboard_but_mods();
+    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");
 
-        debug("Layer Switch(on): "); debug_hex(current_layer);
-        current_layer = keymap_fn_layer(FN_INDEX(code));
-        debug(" -> "); debug_hex(current_layer); debug("\n");
+        clear_keyboard_but_mods();
+        current_layer = new_layer;
     }
 }
 
@@ -133,12 +141,13 @@ static bool layer_switch_off(uint8_t code)
 {
     if (!IS_FN(code)) return false;
     fn_state_bits &= ~FN_BIT(code);
-    if (current_layer != keymap_fn_layer(biton(fn_state_bits))) {
-        clear_keyboard_but_mods();
+    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");
 
-        debug("Layer Switch(off): "); debug_hex(current_layer);
-        current_layer = keymap_fn_layer(biton(fn_state_bits));
-        debug(" -> "); debug_hex(current_layer); debug("\n");
+        clear_keyboard_but_mods();
+        current_layer = new_layer;
         return true;
     }
     return false;
@@ -147,16 +156,20 @@ static bool layer_switch_off(uint8_t code)
 static void register_code(uint8_t code)
 {
     if IS_KEY(code) {
-        host_add_key(code);
-        host_send_keyboard_report();
+        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) {
-        host_add_key(keymap_fn_keycode(FN_INDEX(code)));
-        host_send_keyboard_report();
+        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
@@ -323,9 +336,9 @@ static void unregister_code(uint8_t code)
  *      Ld: Switch back to default layer(*unregister* all keys but modifiers)
  */
 #define NEXT(state)     do { \
-    debug("NEXT: "); print_P(state_str(kbdstate)); \
+    Kdebug("NEXT: "); Kdebug_P(state_str(kbdstate)); \
     kbdstate = state; \
-    debug(" -> "); print_P(state_str(kbdstate)); debug("\n"); \
+    Kdebug(" -> "); Kdebug_P(state_str(kbdstate)); Kdebug("\n"); \
 } while (0)
 
 static inline void process_key(keyevent_t event)
@@ -335,11 +348,11 @@ static inline void process_key(keyevent_t event)
 
     uint8_t tmp_mods;
 
-    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");
+    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:
@@ -531,7 +544,10 @@ static inline void process_key(keyevent_t event)
 
 void keyboard_init(void)
 {
-    debug_keyboard = true;
+    // TODO: to enable debug print magic key bind on boot time
+
+    // TODO: configuration of sendchar impl
+    print_sendchar_func = sendchar;
 
     timer_init();
     matrix_init();
@@ -543,17 +559,11 @@ void keyboard_init(void)
 void keyboard_task(void)
 {
     static matrix_row_t matrix_prev[MATRIX_ROWS];
+    static uint8_t led_status = 0;
     matrix_row_t matrix_row = 0;
     matrix_row_t matrix_change = 0;
 
     matrix_scan();
-    if (command_proc()) {
-        debug("COMMAND\n");
-        // 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];
@@ -606,11 +616,18 @@ void keyboard_task(void)
             is_matrix_on |= matrix_get_row(r);
         }
         if (!is_matrix_on) {
-            debug("FAIL SAFE: clear all keys.\n");
+            Kdebug("FAIL SAFE: clear all keys(default layer).\n");
             clear_keyboard();
+            current_layer = default_layer;
         }
     }
-    
+
+    // update LED
+    if (led_status != host_keyboard_leds()) {
+        led_status = host_keyboard_leds();
+        keyboard_set_leds(led_status);
+    }
+
     return;
 }