From 835aca734675e1690cddabba8a31e298a9f78c44 Mon Sep 17 00:00:00 2001 From: Marcin Date: Thu, 2 Sep 2021 13:41:35 +0200 Subject: [PATCH] Add support for device interface numbers when using hidapi --- src/classes/Keyboard.cpp | 12 ++++++------ src/classes/Keyboard.h | 29 +++++++++++++++-------------- src/helpers/help.cpp | 3 ++- src/main.cpp | 32 +++++++++++++++++++++++++------- 4 files changed, 48 insertions(+), 28 deletions(-) diff --git a/src/classes/Keyboard.cpp b/src/classes/Keyboard.cpp index f6c1ec0..6e7a450 100644 --- a/src/classes/Keyboard.cpp +++ b/src/classes/Keyboard.cpp @@ -163,7 +163,7 @@ bool LedKeyboard::open(uint16_t vendorID, uint16_t productID, string serial) { while (dev) { for (int i=0; i<(int)SupportedKeyboards.size(); i++) { - if (dev->vendor_id == SupportedKeyboards[i][0] && dev->product_id == SupportedKeyboards[i][1]) { + if (dev->vendor_id == SupportedKeyboards[i][0] && dev->product_id == SupportedKeyboards[i][1] && dev->interface_number == SupportedKeyboards[i][2]) { if (!serial.empty() && dev->serial_number != NULL && wideSerial.compare(dev->serial_number) != 0) break; //Serial didn't match if (dev->serial_number != NULL) { @@ -188,7 +188,8 @@ bool LedKeyboard::open(uint16_t vendorID, uint16_t productID, string serial) { currentDevice.vendorID = dev->vendor_id; currentDevice.productID = dev->product_id; - currentDevice.model = (KeyboardModel)SupportedKeyboards[i][2]; + currentDevice.path = dev->path; + currentDevice.model = (KeyboardModel)SupportedKeyboards[i][3]; break; } } @@ -204,8 +205,7 @@ bool LedKeyboard::open(uint16_t vendorID, uint16_t productID, string serial) { return false; } - if (wideSerial.empty()) m_hidHandle = hid_open(currentDevice.vendorID, currentDevice.productID, NULL); - else m_hidHandle = hid_open(currentDevice.vendorID, currentDevice.productID, wideSerial.c_str()); + m_hidHandle = hid_open_path(currentDevice.path.c_str()); if(m_hidHandle == 0) { hid_exit(); @@ -248,7 +248,7 @@ bool LedKeyboard::open(uint16_t vendorID, uint16_t productID, string serial) { currentDevice.serialNumber = serial; currentDevice.vendorID = desc.idVendor; currentDevice.productID = desc.idProduct; - currentDevice.model = (KeyboardModel)SupportedKeyboards[i][2]; + currentDevice.model = (KeyboardModel)SupportedKeyboards[i][3]; dev = device; libusb_close(m_hidHandle); @@ -276,7 +276,7 @@ bool LedKeyboard::open(uint16_t vendorID, uint16_t productID, string serial) { } currentDevice.vendorID = desc.idVendor; currentDevice.productID = desc.idProduct; - currentDevice.model = (KeyboardModel)SupportedKeyboards[i][2]; + currentDevice.model = (KeyboardModel)SupportedKeyboards[i][3]; if (libusb_get_string_descriptor_ascii(m_hidHandle, desc.iManufacturer, buf, 256) >= 1) currentDevice.manufacturer = string((char*)buf); if (libusb_get_string_descriptor_ascii(m_hidHandle, desc.iProduct, buf, 256) >= 1) currentDevice.product = string((char*)buf); if (libusb_get_string_descriptor_ascii(m_hidHandle, desc.iSerialNumber, buf, 256) >= 1) currentDevice.serialNumber = string((char*)buf); diff --git a/src/classes/Keyboard.h b/src/classes/Keyboard.h index 7344763..d8a0378 100644 --- a/src/classes/Keyboard.h +++ b/src/classes/Keyboard.h @@ -45,20 +45,20 @@ class LedKeyboard { public: std::vector> SupportedKeyboards = { - { 0x46d, 0xc336, (uint16_t)KeyboardModel::g213 }, - { 0x46d, 0xc330, (uint16_t)KeyboardModel::g410 }, - { 0x46d, 0xc33a, (uint16_t)KeyboardModel::g413 }, - { 0x46d, 0xc342, (uint16_t)KeyboardModel::g512 }, - { 0x46d, 0xc33c, (uint16_t)KeyboardModel::g513 }, - { 0x46d, 0xc333, (uint16_t)KeyboardModel::g610 }, - { 0x46d, 0xc338, (uint16_t)KeyboardModel::g610 }, - { 0x46d, 0xc331, (uint16_t)KeyboardModel::g810 }, - { 0x46d, 0xc337, (uint16_t)KeyboardModel::g810 }, - { 0x46d, 0xc33f, (uint16_t)KeyboardModel::g815 }, - { 0x46d, 0xc32b, (uint16_t)KeyboardModel::g910 }, - { 0x46d, 0xc335, (uint16_t)KeyboardModel::g910 }, - { 0x46d, 0xc541, (uint16_t)KeyboardModel::g915 }, - { 0x46d, 0xc339, (uint16_t)KeyboardModel::gpro } + { 0x46d, 0xc336, 1, (uint16_t)KeyboardModel::g213 }, + { 0x46d, 0xc330, 1, (uint16_t)KeyboardModel::g410 }, + { 0x46d, 0xc33a, 1, (uint16_t)KeyboardModel::g413 }, + { 0x46d, 0xc342, 1, (uint16_t)KeyboardModel::g512 }, + { 0x46d, 0xc33c, 1, (uint16_t)KeyboardModel::g513 }, + { 0x46d, 0xc333, 1, (uint16_t)KeyboardModel::g610 }, + { 0x46d, 0xc338, 1, (uint16_t)KeyboardModel::g610 }, + { 0x46d, 0xc331, 1, (uint16_t)KeyboardModel::g810 }, + { 0x46d, 0xc337, 1, (uint16_t)KeyboardModel::g810 }, + { 0x46d, 0xc33f, 1, (uint16_t)KeyboardModel::g815 }, + { 0x46d, 0xc32b, 1, (uint16_t)KeyboardModel::g910 }, + { 0x46d, 0xc335, 1, (uint16_t)KeyboardModel::g910 }, + { 0x46d, 0xc541, 2, (uint16_t)KeyboardModel::g915 }, + { 0x46d, 0xc339, 1, (uint16_t)KeyboardModel::gpro } }; enum class KeyboardModel : uint8_t { @@ -167,6 +167,7 @@ class LedKeyboard { std::string manufacturer = ""; std::string product = ""; std::string serialNumber = ""; + std::string path = ""; KeyboardModel model; } DeviceInfo; diff --git a/src/helpers/help.cpp b/src/helpers/help.cpp index e673738..580275d 100644 --- a/src/helpers/help.cpp +++ b/src/helpers/help.cpp @@ -108,7 +108,8 @@ namespace help { cout<<" -dv\t\t\t\t\tDevice vendor ID, such as 046d for Logitech. Can be omitted to match any vendor ID"< (argIndex + 1) && arg == "-dp"){ - if (! utils::parseUInt16(argv[argIndex + 1], productID)) return 1; + } else if (argc > (argIndex + 1) && arg == "-dp") { + if (!utils::parseUInt16(argv[argIndex + 1], productID)) return 1; + argIndex += 2; + continue; + } else if (argc > (argIndex + 1) && arg == "-di") { + if (!utils::parseUInt8(argv[argIndex + 1], interfaceNumber)) return 1; argIndex += 2; continue; } else if (argc > (argIndex + 1) && arg == "-tuk"){ uint8_t kbdProtocol = 0; if (! utils::parseUInt8(argv[argIndex + 1], kbdProtocol)) return 1; + uint8_t ifNum; + LedKeyboard::KeyboardModel model = LedKeyboard::KeyboardModel::unknown; switch(kbdProtocol) { case 1: - kbd.SupportedKeyboards = { { vendorID, productID, (uint16_t)LedKeyboard::KeyboardModel::g810 } }; + ifNum = 1; + model = LedKeyboard::KeyboardModel::g810; break; case 2: - kbd.SupportedKeyboards = { { vendorID, productID, (uint16_t)LedKeyboard::KeyboardModel::g910 } }; + ifNum = 1; + model = LedKeyboard::KeyboardModel::g910; break; case 3: - kbd.SupportedKeyboards = { { vendorID, productID, (uint16_t)LedKeyboard::KeyboardModel::g213 } }; + ifNum = 1; + model = LedKeyboard::KeyboardModel::g213; break; case 4: - kbd.SupportedKeyboards = { { vendorID, productID, (uint16_t)LedKeyboard::KeyboardModel::g815 } }; + ifNum = 1; + model = LedKeyboard::KeyboardModel::g815; break; case 5: - kbd.SupportedKeyboards = { { vendorID, productID, (uint16_t)LedKeyboard::KeyboardModel::g915 } }; + ifNum = 2; + model = LedKeyboard::KeyboardModel::g915; break; default: break; } + + if (model != LedKeyboard::KeyboardModel::unknown) { + if (interfaceNumber != 0xff) ifNum = interfaceNumber; + kbd.SupportedKeyboards = { { vendorID, productID, ifNum, (uint16_t)model } }; + } + argIndex += 2; continue; }