Skip to content

Commit

Permalink
Make "detected_host_os()" available on the SLAVE side of the split ke…
Browse files Browse the repository at this point in the history
…yboard (qmk#19854)

Co-authored-by: ngs.ch <ngs.ch>
Co-authored-by: Drashna Jaelre <[email protected]>
  • Loading branch information
ngs-ch and drashna authored Apr 3, 2023
1 parent d82c664 commit ccda5d2
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 19 deletions.
45 changes: 26 additions & 19 deletions quantum/os_detection.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,53 +31,53 @@ uint16_t usb_setups[STORED_USB_SETUPS];

#ifdef OS_DETECTION_ENABLE
struct setups_data_t {
uint8_t count;
uint8_t cnt_02;
uint8_t cnt_04;
uint8_t cnt_ff;
uint16_t last_wlength;
os_variant_t detected_os;
uint8_t count;
uint8_t cnt_02;
uint8_t cnt_04;
uint8_t cnt_ff;
uint16_t last_wlength;
};

struct setups_data_t setups_data = {
.count = 0,
.cnt_02 = 0,
.cnt_04 = 0,
.cnt_ff = 0,
.detected_os = OS_UNSURE,
.count = 0,
.cnt_02 = 0,
.cnt_04 = 0,
.cnt_ff = 0,
};

os_variant_t detected_os = OS_UNSURE;

// 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) {
setups_data.detected_os = OS_WINDOWS;
detected_os = OS_WINDOWS;
return;
}
if (setups_data.count == setups_data.cnt_ff) {
// Linux has 3 packets with 0xFF.
setups_data.detected_os = OS_LINUX;
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) {
setups_data.detected_os = OS_MACOS;
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.
setups_data.detected_os = OS_IOS;
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.
setups_data.detected_os = OS_LINUX;
detected_os = OS_LINUX;
return;
}
if (setups_data.cnt_ff >= 1 && setups_data.cnt_02 == 0 && setups_data.cnt_04 == 0) {
// This is actually Quest 2 or Nintendo Switch.
setups_data.detected_os = OS_LINUX;
detected_os = OS_LINUX;
return;
}
}
Expand All @@ -99,13 +99,20 @@ void process_wlength(const uint16_t w_length) {
}

os_variant_t detected_host_os(void) {
return setups_data.detected_os;
return detected_os;
}

void erase_wlength_data(void) {
memset(&setups_data, 0, sizeof(setups_data));
detected_os = OS_UNSURE;
}

# if defined(SPLIT_KEYBOARD) && defined(SPLIT_DETECTED_OS_ENABLE)
void slave_update_detected_host_os(os_variant_t os) {
detected_os = os;
}
#endif // OS_DETECTION_ENABLE
# endif // defined(SPLIT_KEYBOARD) && defined(SPLIT_DETECTED_OS_ENABLE)
#endif // OS_DETECTION_ENABLE

#ifdef OS_DETECTION_DEBUG_ENABLE
void print_stored_setups(void) {
Expand Down
4 changes: 4 additions & 0 deletions quantum/os_detection.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ typedef enum {
void process_wlength(const uint16_t w_length);
os_variant_t detected_host_os(void);
void erase_wlength_data(void);

# if defined(SPLIT_KEYBOARD) && defined(SPLIT_DETECTED_OS_ENABLE)
void slave_update_detected_host_os(os_variant_t os);
# endif // defined(SPLIT_KEYBOARD) && defined(SPLIT_DETECTED_OS_ENABLE)
#endif

#ifdef OS_DETECTION_DEBUG_ENABLE
Expand Down
4 changes: 4 additions & 0 deletions quantum/split_common/transaction_id_define.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,10 @@ enum serial_transaction_id {
SPLIT_TRANSACTION_IDS_USER,
#endif // SPLIT_TRANSACTION_IDS_USER

#if defined(OS_DETECTION_ENABLE) && defined(SPLIT_DETECTED_OS_ENABLE)
PUT_DETECTED_OS,
#endif // defined(OS_DETECTION_ENABLE) && defined(SPLIT_DETECTED_OS_ENABLE)

NUM_TOTAL_TRANSACTIONS
};

Expand Down
31 changes: 31 additions & 0 deletions quantum/split_common/transactions.c
Original file line number Diff line number Diff line change
Expand Up @@ -819,6 +819,34 @@ static void activity_handlers_slave(matrix_row_t master_matrix[], matrix_row_t s

#endif // defined(SPLIT_ACTIVITY_ENABLE)

////////////////////////////////////////////////////
// Detected OS

#if defined(OS_DETECTION_ENABLE) && defined(SPLIT_DETECTED_OS_ENABLE)

static bool detected_os_handlers_master(matrix_row_t master_matrix[], matrix_row_t slave_matrix[]) {
static uint32_t last_detected_os_update = 0;
os_variant_t detected_os = detected_host_os();
bool okay = send_if_condition(PUT_DETECTED_OS, &last_detected_os_update, (detected_os != split_shmem->detected_os), &detected_os, sizeof(os_variant_t));
return okay;
}

static void detected_os_handlers_slave(matrix_row_t master_matrix[], matrix_row_t slave_matrix[]) {
slave_update_detected_host_os(split_shmem->detected_os);
}

# define TRANSACTIONS_DETECTED_OS_MASTER() TRANSACTION_HANDLER_MASTER(detected_os)
# define TRANSACTIONS_DETECTED_OS_SLAVE() TRANSACTION_HANDLER_SLAVE_AUTOLOCK(detected_os)
# define TRANSACTIONS_DETECTED_OS_REGISTRATIONS [PUT_DETECTED_OS] = trans_initiator2target_initializer(detected_os),

#else // defined(OS_DETECTION_ENABLE) && defined(SPLIT_DETECTED_OS_ENABLE)

# define TRANSACTIONS_DETECTED_OS_MASTER()
# define TRANSACTIONS_DETECTED_OS_SLAVE()
# define TRANSACTIONS_DETECTED_OS_REGISTRATIONS

#endif // defined(OS_DETECTION_ENABLE) && defined(SPLIT_DETECTED_OS_ENABLE)

////////////////////////////////////////////////////

split_transaction_desc_t split_transaction_table[NUM_TOTAL_TRANSACTIONS] = {
Expand Down Expand Up @@ -848,6 +876,7 @@ split_transaction_desc_t split_transaction_table[NUM_TOTAL_TRANSACTIONS] = {
TRANSACTIONS_WATCHDOG_REGISTRATIONS
TRANSACTIONS_HAPTIC_REGISTRATIONS
TRANSACTIONS_ACTIVITY_REGISTRATIONS
TRANSACTIONS_DETECTED_OS_REGISTRATIONS
// clang-format on

#if defined(SPLIT_TRANSACTION_IDS_KB) || defined(SPLIT_TRANSACTION_IDS_USER)
Expand Down Expand Up @@ -877,6 +906,7 @@ bool transactions_master(matrix_row_t master_matrix[], matrix_row_t slave_matrix
TRANSACTIONS_WATCHDOG_MASTER();
TRANSACTIONS_HAPTIC_MASTER();
TRANSACTIONS_ACTIVITY_MASTER();
TRANSACTIONS_DETECTED_OS_MASTER();
return true;
}

Expand All @@ -899,6 +929,7 @@ void transactions_slave(matrix_row_t master_matrix[], matrix_row_t slave_matrix[
TRANSACTIONS_WATCHDOG_SLAVE();
TRANSACTIONS_HAPTIC_SLAVE();
TRANSACTIONS_ACTIVITY_SLAVE();
TRANSACTIONS_DETECTED_OS_SLAVE();
}

#if defined(SPLIT_TRANSACTION_IDS_KB) || defined(SPLIT_TRANSACTION_IDS_USER)
Expand Down
8 changes: 8 additions & 0 deletions quantum/split_common/transport.h
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,10 @@ typedef struct _rpc_sync_info_t {
} rpc_sync_info_t;
#endif // defined(SPLIT_TRANSACTION_IDS_KB) || defined(SPLIT_TRANSACTION_IDS_USER)

#if defined(OS_DETECTION_ENABLE) && defined(SPLIT_DETECTED_OS_ENABLE)
# include "os_detection.h"
#endif // defined(OS_DETECTION_ENABLE) && defined(SPLIT_DETECTED_OS_ENABLE)

typedef struct _split_shared_memory_t {
#ifdef USE_I2C
int8_t transaction_id;
Expand Down Expand Up @@ -222,6 +226,10 @@ typedef struct _split_shared_memory_t {
uint8_t rpc_m2s_buffer[RPC_M2S_BUFFER_SIZE];
uint8_t rpc_s2m_buffer[RPC_S2M_BUFFER_SIZE];
#endif // defined(SPLIT_TRANSACTION_IDS_KB) || defined(SPLIT_TRANSACTION_IDS_USER)

#if defined(OS_DETECTION_ENABLE) && defined(SPLIT_DETECTED_OS_ENABLE)
os_variant_t detected_os;
#endif // defined(OS_DETECTION_ENABLE) && defined(SPLIT_DETECTED_OS_ENABLE)
} split_shared_memory_t;

extern split_shared_memory_t *const split_shmem;

0 comments on commit ccda5d2

Please sign in to comment.