summaryrefslogtreecommitdiff
path: root/quantum/os_detection.c
diff options
context:
space:
mode:
authorAndre Brait <andrebrait@gmail.com>2024-02-16 15:19:02 +0100
committerGitHub <noreply@github.com>2024-02-17 01:19:02 +1100
commit80f3da36e571fa702b1d3df693fd545801250eca (patch)
tree8fe09c9e53bbe8534372a87bab3d4edfd61e5fa8 /quantum/os_detection.c
parent77e88674986ee14bd1799b1ab19b4c94af083bac (diff)
[Core] Add OS detection callbacks (#21777)
Diffstat (limited to 'quantum/os_detection.c')
-rw-r--r--quantum/os_detection.c170
1 files changed, 120 insertions, 50 deletions
diff --git a/quantum/os_detection.c b/quantum/os_detection.c
index e606227136..b674f05b35 100644
--- a/quantum/os_detection.c
+++ b/quantum/os_detection.c
@@ -16,20 +16,25 @@
#include "os_detection.h"
-#include <string.h>
+#ifdef OS_DETECTION_ENABLE
-#ifdef OS_DETECTION_DEBUG_ENABLE
-# include "eeconfig.h"
-# include "eeprom.h"
-# include "print.h"
+# include <string.h>
+# include "timer.h"
+# ifdef OS_DETECTION_KEYBOARD_RESET
+# include "quantum.h"
+# endif
-# define STORED_USB_SETUPS 50
-# define EEPROM_USER_OFFSET (uint8_t*)EECONFIG_SIZE
+# ifdef OS_DETECTION_DEBUG_ENABLE
+# include "eeconfig.h"
+# include "eeprom.h"
+# include "print.h"
-uint16_t usb_setups[STORED_USB_SETUPS];
-#endif
+# define STORED_USB_SETUPS 50
+# define EEPROM_USER_OFFSET (uint8_t*)EECONFIG_SIZE
+
+static uint16_t usb_setups[STORED_USB_SETUPS];
+# endif
-#ifdef OS_DETECTION_ENABLE
struct setups_data_t {
uint8_t count;
uint8_t cnt_02;
@@ -45,43 +50,63 @@ struct setups_data_t setups_data = {
.cnt_ff = 0,
};
-os_variant_t detected_os = OS_UNSURE;
+# ifndef OS_DETECTION_DEBOUNCE
+# define OS_DETECTION_DEBOUNCE 200
+# endif
-// Some collected sequences of wLength can be found in tests.
-void make_guess(void) {
- if (setups_data.count < 3) {
- return;
- }
- if (setups_data.cnt_ff >= 2 && setups_data.cnt_04 >= 1) {
- detected_os = OS_WINDOWS;
- return;
- }
- if (setups_data.count == setups_data.cnt_ff) {
- // Linux has 3 packets with 0xFF.
- detected_os = OS_LINUX;
- return;
- }
- if (setups_data.count == 5 && setups_data.last_wlength == 0xFF && setups_data.cnt_ff == 1 && setups_data.cnt_02 == 2) {
- detected_os = OS_MACOS;
- return;
- }
- if (setups_data.count == 4 && setups_data.cnt_ff == 0 && setups_data.cnt_02 == 2) {
- // iOS and iPadOS don't have the last 0xFF packet.
- detected_os = OS_IOS;
- return;
- }
- if (setups_data.cnt_ff == 0 && setups_data.cnt_02 == 3 && setups_data.cnt_04 == 1) {
- // This is actually PS5.
- detected_os = OS_LINUX;
- return;
+// 2s should always be more than enough (otherwise, you may have other issues)
+# if OS_DETECTION_DEBOUNCE > 2000
+# undef OS_DETECTION_DEBOUNCE
+# define OS_DETECTION_DEBOUNCE 2000
+# endif
+
+typedef uint16_t debouncing_t;
+
+static volatile os_variant_t detected_os = OS_UNSURE;
+static os_variant_t reported_os = OS_UNSURE;
+
+// we need to be able to report OS_UNSURE if that is the stable result of the guesses
+static bool first_report = true;
+
+// to react on USB state changes
+static volatile enum usb_device_state current_usb_device_state = USB_DEVICE_STATE_INIT;
+static enum usb_device_state reported_usb_device_state = USB_DEVICE_STATE_INIT;
+
+// the OS detection might be unstable for a while, "debounce" it
+static volatile bool debouncing = false;
+static volatile fast_timer_t last_time;
+
+void os_detection_task(void) {
+ if (current_usb_device_state == USB_DEVICE_STATE_CONFIGURED) {
+ // debouncing goes for both the detected OS as well as the USB state
+ if (debouncing && timer_elapsed_fast(last_time) >= OS_DETECTION_DEBOUNCE) {
+ debouncing = false;
+ reported_usb_device_state = current_usb_device_state;
+ if (detected_os != reported_os || first_report) {
+ first_report = false;
+ reported_os = detected_os;
+ process_detected_host_os_kb(detected_os);
+ }
+ }
}
- if (setups_data.cnt_ff >= 1 && setups_data.cnt_02 == 0 && setups_data.cnt_04 == 0) {
- // This is actually Quest 2 or Nintendo Switch.
- detected_os = OS_LINUX;
- return;
+# ifdef OS_DETECTION_KEYBOARD_RESET
+ // resetting the keyboard on the USB device state change callback results in instability, so delegate that to this task
+ // only take action if it's been stable at least once, to avoid issues with some KVMs
+ else if (current_usb_device_state == USB_DEVICE_STATE_INIT && reported_usb_device_state != USB_DEVICE_STATE_INIT) {
+ soft_reset_keyboard();
}
+# endif
}
+__attribute__((weak)) bool process_detected_host_os_kb(os_variant_t detected_os) {
+ return process_detected_host_os_user(detected_os);
+}
+
+__attribute__((weak)) bool process_detected_host_os_user(os_variant_t detected_os) {
+ return true;
+}
+
+// Some collected sequences of wLength can be found in tests.
void process_wlength(const uint16_t w_length) {
# ifdef OS_DETECTION_DEBUG_ENABLE
usb_setups[setups_data.count] = w_length;
@@ -95,7 +120,37 @@ void process_wlength(const uint16_t w_length) {
} else if (w_length == 0xFF) {
setups_data.cnt_ff++;
}
- make_guess();
+
+ // now try to make a guess
+ os_variant_t guessed = OS_UNSURE;
+ if (setups_data.count >= 3) {
+ if (setups_data.cnt_ff >= 2 && setups_data.cnt_04 >= 1) {
+ guessed = OS_WINDOWS;
+ } else if (setups_data.count == setups_data.cnt_ff) {
+ // Linux has 3 packets with 0xFF.
+ guessed = OS_LINUX;
+ } else if (setups_data.count == 5 && setups_data.last_wlength == 0xFF && setups_data.cnt_ff == 1 && setups_data.cnt_02 == 2) {
+ guessed = OS_MACOS;
+ } else if (setups_data.count == 4 && setups_data.cnt_ff == 0 && setups_data.cnt_02 == 2) {
+ // iOS and iPadOS don't have the last 0xFF packet.
+ guessed = OS_IOS;
+ } else if (setups_data.cnt_ff == 0 && setups_data.cnt_02 == 3 && setups_data.cnt_04 == 1) {
+ // This is actually PS5.
+ guessed = OS_LINUX;
+ } else if (setups_data.cnt_ff >= 1 && setups_data.cnt_02 == 0 && setups_data.cnt_04 == 0) {
+ // This is actually Quest 2 or Nintendo Switch.
+ guessed = OS_LINUX;
+ }
+ }
+
+ // only replace the guessed value if not unsure
+ if (guessed != OS_UNSURE) {
+ detected_os = guessed;
+ }
+
+ // whatever the result, debounce
+ last_time = timer_read_fast();
+ debouncing = true;
}
os_variant_t detected_host_os(void) {
@@ -104,25 +159,38 @@ os_variant_t detected_host_os(void) {
void erase_wlength_data(void) {
memset(&setups_data, 0, sizeof(setups_data));
- detected_os = OS_UNSURE;
+ detected_os = OS_UNSURE;
+ reported_os = OS_UNSURE;
+ current_usb_device_state = USB_DEVICE_STATE_INIT;
+ reported_usb_device_state = USB_DEVICE_STATE_INIT;
+ debouncing = false;
+ first_report = true;
+}
+
+void os_detection_notify_usb_device_state_change(enum usb_device_state usb_device_state) {
+ // treat this like any other source of instability
+ current_usb_device_state = usb_device_state;
+ last_time = timer_read_fast();
+ debouncing = true;
}
# if defined(SPLIT_KEYBOARD) && defined(SPLIT_DETECTED_OS_ENABLE)
void slave_update_detected_host_os(os_variant_t os) {
detected_os = os;
+ last_time = timer_read_fast();
+ debouncing = true;
}
-# endif // defined(SPLIT_KEYBOARD) && defined(SPLIT_DETECTED_OS_ENABLE)
-#endif // OS_DETECTION_ENABLE
+# endif
-#ifdef OS_DETECTION_DEBUG_ENABLE
+# ifdef OS_DETECTION_DEBUG_ENABLE
void print_stored_setups(void) {
-# ifdef CONSOLE_ENABLE
+# ifdef CONSOLE_ENABLE
uint8_t cnt = eeprom_read_byte(EEPROM_USER_OFFSET);
for (uint16_t i = 0; i < cnt; ++i) {
uint16_t* addr = (uint16_t*)EEPROM_USER_OFFSET + i * sizeof(uint16_t) + sizeof(uint8_t);
xprintf("i: %d, wLength: 0x%02X\n", i, eeprom_read_word(addr));
}
-# endif
+# endif
}
void store_setups_in_eeprom(void) {
@@ -133,4 +201,6 @@ void store_setups_in_eeprom(void) {
}
}
-#endif // OS_DETECTION_DEBUG_ENABLE
+# endif // OS_DETECTION_DEBUG_ENABLE
+
+#endif