]> git.friedersdorff.com Git - max/tmk_keyboard.git/commitdiff
core: Clean up code of Locking key support
authortmk <hasu@tmk-kbd.com>
Sat, 7 Nov 2020 07:39:15 +0000 (16:39 +0900)
committertmk <hasu@tmk-kbd.com>
Sat, 7 Nov 2020 08:17:13 +0000 (17:17 +0900)
tmk_core/common/action.c

index 85a0631ffe3960a14220d0a2a41c4165110e7675..80eb3a8aee0d615ec5b70317219029aea5d91dbf 100644 (file)
@@ -375,37 +375,28 @@ void register_code(uint8_t code)
     }
 
 #ifdef LOCKING_SUPPORT_ENABLE
-    else if (KC_LOCKING_CAPS == code) {
-#ifdef LOCKING_RESYNC_ENABLE
-        // Resync: ignore if caps lock already is on
-        if (host_keyboard_leds() & (1<<USB_LED_CAPS_LOCK)) return;
-#endif
-        add_key(KC_CAPSLOCK);
-        send_keyboard_report();
-        wait_ms(100);
-        del_key(KC_CAPSLOCK);
-        send_keyboard_report();
-    }
-
-    else if (KC_LOCKING_NUM == code) {
-#ifdef LOCKING_RESYNC_ENABLE
-        if (host_keyboard_leds() & (1<<USB_LED_NUM_LOCK)) return;
-#endif
-        add_key(KC_NUMLOCK);
-        send_keyboard_report();
-        wait_ms(100);
-        del_key(KC_NUMLOCK);
-        send_keyboard_report();
-    }
-
-    else if (KC_LOCKING_SCROLL == code) {
+    else if (code == KC_LOCKING_CAPS ||
+                code == KC_LOCKING_NUM ||
+                code == KC_LOCKING_SCROLL) {
+        uint8_t c, l;
+        if (code == KC_LOCKING_CAPS) {
+            c = KC_CAPSLOCK;
+            l = 1<<USB_LED_CAPS_LOCK;
+        } else if (code == KC_LOCKING_NUM) {
+            c = KC_NUMLOCK;
+            l = 1<<USB_LED_NUM_LOCK;
+        } else if (code == KC_LOCKING_SCROLL) {
+            c = KC_SCROLLLOCK;
+            l = 1<<USB_LED_SCROLL_LOCK;
+        }
 #ifdef LOCKING_RESYNC_ENABLE
-        if (host_keyboard_leds() & (1<<USB_LED_SCROLL_LOCK)) return;
+        // Resync: ignore if lock indicator is already on
+        if (host_keyboard_leds() & l) return;
 #endif
-        add_key(KC_SCROLLLOCK);
+        add_key(c);
         send_keyboard_report();
-        wait_ms(100);
-        del_key(KC_SCROLLLOCK);
+        wait_ms(100); // Delay for MacOS #390
+        del_key(c);
         send_keyboard_report();
     }
 #endif
@@ -453,37 +444,28 @@ void unregister_code(uint8_t code)
     }
 
 #ifdef LOCKING_SUPPORT_ENABLE
-    else if (KC_LOCKING_CAPS == code) {
-#ifdef LOCKING_RESYNC_ENABLE
-        // Resync: ignore if caps lock already is off
-        if (!(host_keyboard_leds() & (1<<USB_LED_CAPS_LOCK))) return;
-#endif
-        add_key(KC_CAPSLOCK);
-        send_keyboard_report();
-        wait_ms(100);
-        del_key(KC_CAPSLOCK);
-        send_keyboard_report();
-    }
-
-    else if (KC_LOCKING_NUM == code) {
-#ifdef LOCKING_RESYNC_ENABLE
-        if (!(host_keyboard_leds() & (1<<USB_LED_NUM_LOCK))) return;
-#endif
-        add_key(KC_NUMLOCK);
-        send_keyboard_report();
-        wait_ms(100);
-        del_key(KC_NUMLOCK);
-        send_keyboard_report();
-    }
-
-    else if (KC_LOCKING_SCROLL == code) {
+    else if (code == KC_LOCKING_CAPS ||
+                code == KC_LOCKING_NUM ||
+                code == KC_LOCKING_SCROLL) {
+        uint8_t c, l;
+        if (code == KC_LOCKING_CAPS) {
+            c = KC_CAPSLOCK;
+            l = 1<<USB_LED_CAPS_LOCK;
+        } else if (code == KC_LOCKING_NUM) {
+            c = KC_NUMLOCK;
+            l = 1<<USB_LED_NUM_LOCK;
+        } else if (code == KC_LOCKING_SCROLL) {
+            c = KC_SCROLLLOCK;
+            l = 1<<USB_LED_SCROLL_LOCK;
+        }
 #ifdef LOCKING_RESYNC_ENABLE
-        if (!(host_keyboard_leds() & (1<<USB_LED_SCROLL_LOCK))) return;
+        // Resync: ignore if lock indicator is already off
+        if (!(host_keyboard_leds() & l)) return;
 #endif
-        add_key(KC_SCROLLLOCK);
+        add_key(c);
         send_keyboard_report();
-        wait_ms(100);
-        del_key(KC_SCROLLLOCK);
+        wait_ms(100); // Delay for MacOS #390
+        del_key(c);
         send_keyboard_report();
     }
 #endif