Skip to content

Commit ccda5d2

Browse files
ngs-chdrashna
andauthored
Make "detected_host_os()" available on the SLAVE side of the split keyboard (qmk#19854)
Co-authored-by: ngs.ch <ngs.ch> Co-authored-by: Drashna Jaelre <[email protected]>
1 parent d82c664 commit ccda5d2

File tree

5 files changed

+73
-19
lines changed

5 files changed

+73
-19
lines changed

quantum/os_detection.c

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -31,53 +31,53 @@ uint16_t usb_setups[STORED_USB_SETUPS];
3131

3232
#ifdef OS_DETECTION_ENABLE
3333
struct setups_data_t {
34-
uint8_t count;
35-
uint8_t cnt_02;
36-
uint8_t cnt_04;
37-
uint8_t cnt_ff;
38-
uint16_t last_wlength;
39-
os_variant_t detected_os;
34+
uint8_t count;
35+
uint8_t cnt_02;
36+
uint8_t cnt_04;
37+
uint8_t cnt_ff;
38+
uint16_t last_wlength;
4039
};
4140

4241
struct setups_data_t setups_data = {
43-
.count = 0,
44-
.cnt_02 = 0,
45-
.cnt_04 = 0,
46-
.cnt_ff = 0,
47-
.detected_os = OS_UNSURE,
42+
.count = 0,
43+
.cnt_02 = 0,
44+
.cnt_04 = 0,
45+
.cnt_ff = 0,
4846
};
4947

48+
os_variant_t detected_os = OS_UNSURE;
49+
5050
// Some collected sequences of wLength can be found in tests.
5151
void make_guess(void) {
5252
if (setups_data.count < 3) {
5353
return;
5454
}
5555
if (setups_data.cnt_ff >= 2 && setups_data.cnt_04 >= 1) {
56-
setups_data.detected_os = OS_WINDOWS;
56+
detected_os = OS_WINDOWS;
5757
return;
5858
}
5959
if (setups_data.count == setups_data.cnt_ff) {
6060
// Linux has 3 packets with 0xFF.
61-
setups_data.detected_os = OS_LINUX;
61+
detected_os = OS_LINUX;
6262
return;
6363
}
6464
if (setups_data.count == 5 && setups_data.last_wlength == 0xFF && setups_data.cnt_ff == 1 && setups_data.cnt_02 == 2) {
65-
setups_data.detected_os = OS_MACOS;
65+
detected_os = OS_MACOS;
6666
return;
6767
}
6868
if (setups_data.count == 4 && setups_data.cnt_ff == 0 && setups_data.cnt_02 == 2) {
6969
// iOS and iPadOS don't have the last 0xFF packet.
70-
setups_data.detected_os = OS_IOS;
70+
detected_os = OS_IOS;
7171
return;
7272
}
7373
if (setups_data.cnt_ff == 0 && setups_data.cnt_02 == 3 && setups_data.cnt_04 == 1) {
7474
// This is actually PS5.
75-
setups_data.detected_os = OS_LINUX;
75+
detected_os = OS_LINUX;
7676
return;
7777
}
7878
if (setups_data.cnt_ff >= 1 && setups_data.cnt_02 == 0 && setups_data.cnt_04 == 0) {
7979
// This is actually Quest 2 or Nintendo Switch.
80-
setups_data.detected_os = OS_LINUX;
80+
detected_os = OS_LINUX;
8181
return;
8282
}
8383
}
@@ -99,13 +99,20 @@ void process_wlength(const uint16_t w_length) {
9999
}
100100

101101
os_variant_t detected_host_os(void) {
102-
return setups_data.detected_os;
102+
return detected_os;
103103
}
104104

105105
void erase_wlength_data(void) {
106106
memset(&setups_data, 0, sizeof(setups_data));
107+
detected_os = OS_UNSURE;
108+
}
109+
110+
# if defined(SPLIT_KEYBOARD) && defined(SPLIT_DETECTED_OS_ENABLE)
111+
void slave_update_detected_host_os(os_variant_t os) {
112+
detected_os = os;
107113
}
108-
#endif // OS_DETECTION_ENABLE
114+
# endif // defined(SPLIT_KEYBOARD) && defined(SPLIT_DETECTED_OS_ENABLE)
115+
#endif // OS_DETECTION_ENABLE
109116

110117
#ifdef OS_DETECTION_DEBUG_ENABLE
111118
void print_stored_setups(void) {

quantum/os_detection.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ typedef enum {
3030
void process_wlength(const uint16_t w_length);
3131
os_variant_t detected_host_os(void);
3232
void erase_wlength_data(void);
33+
34+
# if defined(SPLIT_KEYBOARD) && defined(SPLIT_DETECTED_OS_ENABLE)
35+
void slave_update_detected_host_os(os_variant_t os);
36+
# endif // defined(SPLIT_KEYBOARD) && defined(SPLIT_DETECTED_OS_ENABLE)
3337
#endif
3438

3539
#ifdef OS_DETECTION_DEBUG_ENABLE

quantum/split_common/transaction_id_define.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,10 @@ enum serial_transaction_id {
113113
SPLIT_TRANSACTION_IDS_USER,
114114
#endif // SPLIT_TRANSACTION_IDS_USER
115115

116+
#if defined(OS_DETECTION_ENABLE) && defined(SPLIT_DETECTED_OS_ENABLE)
117+
PUT_DETECTED_OS,
118+
#endif // defined(OS_DETECTION_ENABLE) && defined(SPLIT_DETECTED_OS_ENABLE)
119+
116120
NUM_TOTAL_TRANSACTIONS
117121
};
118122

quantum/split_common/transactions.c

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -819,6 +819,34 @@ static void activity_handlers_slave(matrix_row_t master_matrix[], matrix_row_t s
819819

820820
#endif // defined(SPLIT_ACTIVITY_ENABLE)
821821

822+
////////////////////////////////////////////////////
823+
// Detected OS
824+
825+
#if defined(OS_DETECTION_ENABLE) && defined(SPLIT_DETECTED_OS_ENABLE)
826+
827+
static bool detected_os_handlers_master(matrix_row_t master_matrix[], matrix_row_t slave_matrix[]) {
828+
static uint32_t last_detected_os_update = 0;
829+
os_variant_t detected_os = detected_host_os();
830+
bool okay = send_if_condition(PUT_DETECTED_OS, &last_detected_os_update, (detected_os != split_shmem->detected_os), &detected_os, sizeof(os_variant_t));
831+
return okay;
832+
}
833+
834+
static void detected_os_handlers_slave(matrix_row_t master_matrix[], matrix_row_t slave_matrix[]) {
835+
slave_update_detected_host_os(split_shmem->detected_os);
836+
}
837+
838+
# define TRANSACTIONS_DETECTED_OS_MASTER() TRANSACTION_HANDLER_MASTER(detected_os)
839+
# define TRANSACTIONS_DETECTED_OS_SLAVE() TRANSACTION_HANDLER_SLAVE_AUTOLOCK(detected_os)
840+
# define TRANSACTIONS_DETECTED_OS_REGISTRATIONS [PUT_DETECTED_OS] = trans_initiator2target_initializer(detected_os),
841+
842+
#else // defined(OS_DETECTION_ENABLE) && defined(SPLIT_DETECTED_OS_ENABLE)
843+
844+
# define TRANSACTIONS_DETECTED_OS_MASTER()
845+
# define TRANSACTIONS_DETECTED_OS_SLAVE()
846+
# define TRANSACTIONS_DETECTED_OS_REGISTRATIONS
847+
848+
#endif // defined(OS_DETECTION_ENABLE) && defined(SPLIT_DETECTED_OS_ENABLE)
849+
822850
////////////////////////////////////////////////////
823851

824852
split_transaction_desc_t split_transaction_table[NUM_TOTAL_TRANSACTIONS] = {
@@ -848,6 +876,7 @@ split_transaction_desc_t split_transaction_table[NUM_TOTAL_TRANSACTIONS] = {
848876
TRANSACTIONS_WATCHDOG_REGISTRATIONS
849877
TRANSACTIONS_HAPTIC_REGISTRATIONS
850878
TRANSACTIONS_ACTIVITY_REGISTRATIONS
879+
TRANSACTIONS_DETECTED_OS_REGISTRATIONS
851880
// clang-format on
852881

853882
#if defined(SPLIT_TRANSACTION_IDS_KB) || defined(SPLIT_TRANSACTION_IDS_USER)
@@ -877,6 +906,7 @@ bool transactions_master(matrix_row_t master_matrix[], matrix_row_t slave_matrix
877906
TRANSACTIONS_WATCHDOG_MASTER();
878907
TRANSACTIONS_HAPTIC_MASTER();
879908
TRANSACTIONS_ACTIVITY_MASTER();
909+
TRANSACTIONS_DETECTED_OS_MASTER();
880910
return true;
881911
}
882912

@@ -899,6 +929,7 @@ void transactions_slave(matrix_row_t master_matrix[], matrix_row_t slave_matrix[
899929
TRANSACTIONS_WATCHDOG_SLAVE();
900930
TRANSACTIONS_HAPTIC_SLAVE();
901931
TRANSACTIONS_ACTIVITY_SLAVE();
932+
TRANSACTIONS_DETECTED_OS_SLAVE();
902933
}
903934

904935
#if defined(SPLIT_TRANSACTION_IDS_KB) || defined(SPLIT_TRANSACTION_IDS_USER)

quantum/split_common/transport.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,10 @@ typedef struct _rpc_sync_info_t {
142142
} rpc_sync_info_t;
143143
#endif // defined(SPLIT_TRANSACTION_IDS_KB) || defined(SPLIT_TRANSACTION_IDS_USER)
144144

145+
#if defined(OS_DETECTION_ENABLE) && defined(SPLIT_DETECTED_OS_ENABLE)
146+
# include "os_detection.h"
147+
#endif // defined(OS_DETECTION_ENABLE) && defined(SPLIT_DETECTED_OS_ENABLE)
148+
145149
typedef struct _split_shared_memory_t {
146150
#ifdef USE_I2C
147151
int8_t transaction_id;
@@ -222,6 +226,10 @@ typedef struct _split_shared_memory_t {
222226
uint8_t rpc_m2s_buffer[RPC_M2S_BUFFER_SIZE];
223227
uint8_t rpc_s2m_buffer[RPC_S2M_BUFFER_SIZE];
224228
#endif // defined(SPLIT_TRANSACTION_IDS_KB) || defined(SPLIT_TRANSACTION_IDS_USER)
229+
230+
#if defined(OS_DETECTION_ENABLE) && defined(SPLIT_DETECTED_OS_ENABLE)
231+
os_variant_t detected_os;
232+
#endif // defined(OS_DETECTION_ENABLE) && defined(SPLIT_DETECTED_OS_ENABLE)
225233
} split_shared_memory_t;
226234

227235
extern split_shared_memory_t *const split_shmem;

0 commit comments

Comments
 (0)