From ad28b82bb88e70bb83f69d7c9edb49948898d91a Mon Sep 17 00:00:00 2001 From: vhaudiquet Date: Sun, 1 Oct 2023 13:04:34 +0200 Subject: [PATCH] wireless driver interface, g-wolves implementation --- README.md | 6 ++-- drivers/g-wolves/g-wolves.c | 56 +++++++++++++++++++++++++++++++++-- src/device/driver_interface.h | 3 ++ 3 files changed, 61 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index ee19472..56c9deb 100644 --- a/README.md +++ b/README.md @@ -49,9 +49,11 @@ int driver_mouse_angle_snap_get(void* handle, bool* output); /* * Wireless driver -* - Battery (state + amount) -* - Connection type (usb 2.4/bluetooth/wired) +* - Battery (state + level) +* - Connection type (wired/wireless) */ +int driver_wireless_battery_state_get(void* handle, int* battery_level, bool* charging); +int driver_wireless_connection_type_get(void* handle, bool* output); ``` diff --git a/drivers/g-wolves/g-wolves.c b/drivers/g-wolves/g-wolves.c index 9532cdc..fcfe121 100644 --- a/drivers/g-wolves/g-wolves.c +++ b/drivers/g-wolves/g-wolves.c @@ -1,6 +1,6 @@ #include "g-wolves.h" -#define DEVICE_WIRELESS(handle) (driver_get_capacity(handle) & DEVICE_CAPACITY_WIRELESS) +#define DEVICE_WIRELESS(handle) (is_connection_wireless(handle)) static int send_command(libusb_device_handle* hand, uint8_t command, uint8_t size, void* report, bool wireless) { @@ -86,10 +86,13 @@ device_capacity_t driver_get_capacity(void* handle) uint8_t wireless = 0; switch(desc.idProduct) { + case HTX_4K_PRODUCT_ID_WIRED: case HTX_4K_PRODUCT_ID_WIRELESS: + case HTS_PLUS_4K_PRODUCT_ID_WIRED: case HTS_PLUS_4K_PRODUCT_ID_WIRELESS: + case HSK_PRO_ACE_PRODUCT_ID_WIRED: case HSK_PRO_ACE_PRODUCT_ID_WIRELESS: - wireless = 1; + wireless = DEVICE_CAPACITY_WIRELESS; break; } @@ -147,6 +150,24 @@ char* driver_get_image(void* handle) } } +static bool is_connection_wireless(void* handle) +{ + libusb_device* dev = handle; + + struct libusb_device_descriptor desc; + libusb_get_device_descriptor(dev, &desc); + + switch(desc.idProduct) + { + case HTX_4K_PRODUCT_ID_WIRELESS: + case HTS_PLUS_4K_PRODUCT_ID_WIRELESS: + case HSK_PRO_ACE_PRODUCT_ID_WIRELESS: + return true; + } + + return false; +} + int driver_mouse_dpi_get(void* handle, struct MOUSE_DPI_LEVELS* output) { libusb_device* dev = handle; @@ -229,3 +250,34 @@ int driver_mouse_angle_snap_get(void* handle, bool* output) libusb_close(hand); return 0; } + +int driver_wireless_connection_type_get(void* handle, bool* output) +{ + *output = is_connection_wireless(handle); + return 0; +} + +int driver_wireless_battery_state_get(void* handle, int* battery_level, bool* charging) +{ + libusb_device* dev = handle; + + // Prepare usb device for transfer + libusb_device_handle* hand; + int openres = libusb_open(dev, &hand); + if(openres) return -1; + libusb_detach_kernel_driver(hand, 0x2); + + // Send command + struct REPORT_BATTERY report = {0}; + int res = send_command(hand, COMMAND_BATTERY, REPORT_BATTERY_SIZE, &report, DEVICE_WIRELESS(handle)); + if(res <= 0) return -1; + + // Format output values + *battery_level = report.battery_level; + *charging = report.battery_status; + + // Close and return + libusb_attach_kernel_driver(hand, 0x2); + libusb_close(hand); + return 0; +} diff --git a/src/device/driver_interface.h b/src/device/driver_interface.h index 0f17f33..f3edc06 100644 --- a/src/device/driver_interface.h +++ b/src/device/driver_interface.h @@ -41,5 +41,8 @@ int driver_mouse_dpi_get(void* handle, struct MOUSE_DPI_LEVELS* output); int driver_mouse_motion_sync_get(void* handle, bool* output); int driver_mouse_angle_snap_get(void* handle, bool* output); +/* Wireless drivers */ +int driver_wireless_battery_state_get(void* handle, int* battery_level, bool* charging); +int driver_wireless_connection_type_get(void* handle, bool* output); #endif \ No newline at end of file