1
0
mirror of https://github.com/MatMoul/g810-led.git synced 2024-12-23 09:16:11 +00:00

Code formating

This commit is contained in:
MatMoul 2017-04-29 16:58:02 +02:00
parent c475fc46fd
commit 7c96ec51bc
3 changed files with 141 additions and 142 deletions

View File

@ -31,30 +31,30 @@ vector<LedKeyboard::DeviceInfo> LedKeyboard::listKeyboards() {
devs = hid_enumerate(0x0, 0x0); devs = hid_enumerate(0x0, 0x0);
dev = devs; dev = devs;
while (dev) { while (dev) {
for (size_t i=0; i<SupportedKeyboards.size(); i++) { for (size_t i = 0; i < SupportedKeyboards.size(); i++) {
if (dev->vendor_id == SupportedKeyboards[i][0]) { if (dev->vendor_id == SupportedKeyboards[i][0]) {
if (dev->product_id == SupportedKeyboards[i][1]) { if (dev->product_id == SupportedKeyboards[i][1]) {
DeviceInfo deviceInfo; DeviceInfo deviceInfo;
deviceInfo.vendorID=dev->vendor_id; deviceInfo.vendorID=dev->vendor_id;
deviceInfo.productID=dev->product_id; deviceInfo.productID=dev->product_id;
if (dev->serial_number != NULL) { if (dev->serial_number != NULL) {
char buf[256]; char buf[256];
wcstombs(buf,dev->serial_number,256); wcstombs(buf, dev->serial_number, 256);
deviceInfo.serialNumber = string(buf); deviceInfo.serialNumber = string(buf);
} }
if (dev->manufacturer_string != NULL) if (dev->manufacturer_string != NULL)
{ {
char buf[256]; char buf[256];
wcstombs(buf,dev->manufacturer_string,256); wcstombs(buf, dev->manufacturer_string, 256);
deviceInfo.manufacturer = string(buf); deviceInfo.manufacturer = string(buf);
} }
if (dev->product_string != NULL) if (dev->product_string != NULL)
{ {
char buf[256]; char buf[256];
wcstombs(buf,dev->product_string,256); wcstombs(buf, dev->product_string, 256);
deviceInfo.product = string(buf); deviceInfo.product = string(buf);
} }
@ -84,8 +84,8 @@ vector<LedKeyboard::DeviceInfo> LedKeyboard::listKeyboards() {
if (desc.idProduct == SupportedKeyboards[i][1]) { if (desc.idProduct == SupportedKeyboards[i][1]) {
unsigned char buf[256]; unsigned char buf[256];
DeviceInfo deviceInfo; DeviceInfo deviceInfo;
deviceInfo.vendorID=desc.idVendor; deviceInfo.vendorID=desc.idVendor;
deviceInfo.productID=desc.idProduct; deviceInfo.productID=desc.idProduct;
if (libusb_open(device, &m_hidHandle) != 0) continue; if (libusb_open(device, &m_hidHandle) != 0) continue;
@ -123,7 +123,7 @@ bool LedKeyboard::isOpen() {
bool LedKeyboard::open() { bool LedKeyboard::open() {
if (m_isOpen) return true; if (m_isOpen) return true;
return open(0x0,0x0,""); return open(0x0, 0x0, "");
} }
bool LedKeyboard::open(uint16_t vendorID, uint16_t productID, string serial) { bool LedKeyboard::open(uint16_t vendorID, uint16_t productID, string serial) {
@ -131,23 +131,22 @@ bool LedKeyboard::open(uint16_t vendorID, uint16_t productID, string serial) {
currentDevice.model = KeyboardModel::unknown; currentDevice.model = KeyboardModel::unknown;
#if defined(hidapi) #if defined(hidapi)
if (hid_init() < 0) return false; if (hid_init() < 0) return false;
struct hid_device_info *devs, *dev; struct hid_device_info *devs, *dev;
devs = hid_enumerate(vendorID, productID); devs = hid_enumerate(vendorID, productID);
dev = devs; dev = devs;
wstring wideSerial; wstring wideSerial;
if (!serial.empty()) { if (!serial.empty()) {
wchar_t tempSerial[256]; wchar_t tempSerial[256];
if (mbstowcs(tempSerial, serial.c_str(), 256) < 1) return false; if (mbstowcs(tempSerial, serial.c_str(), 256) < 1) return false;
wideSerial = wstring(tempSerial); wideSerial = wstring(tempSerial);
} }
while (dev) { while (dev) {
for (int i=0; i<(int)SupportedKeyboards.size(); i++) { for (int i=0; i<(int)SupportedKeyboards.size(); i++) {
if (dev->vendor_id == SupportedKeyboards[i][0]) { if (dev->vendor_id == SupportedKeyboards[i][0] && dev->product_id == SupportedKeyboards[i][1]) {
if (dev->product_id == SupportedKeyboards[i][1]) {
if (!serial.empty() && dev->serial_number != NULL && wideSerial.compare(dev->serial_number) != 0) break; //Serial didn't match if (!serial.empty() && dev->serial_number != NULL && wideSerial.compare(dev->serial_number) != 0) break; //Serial didn't match
if (dev->serial_number != NULL) { if (dev->serial_number != NULL) {
@ -176,150 +175,148 @@ bool LedKeyboard::open(uint16_t vendorID, uint16_t productID, string serial) {
break; break;
} }
} }
if (currentDevice.model != KeyboardModel::unknown) break;
dev = dev->next;
} }
if (currentDevice.model != KeyboardModel::unknown) break;
dev = dev->next;
}
hid_free_enumeration(devs); hid_free_enumeration(devs);
if (! dev) { if (! dev) {
currentDevice.model = KeyboardModel::unknown; currentDevice.model = KeyboardModel::unknown;
hid_exit(); hid_exit();
return false; return false;
} }
if (wideSerial.empty()) m_hidHandle = hid_open(currentDevice.vendorID, currentDevice.productID, NULL); if (wideSerial.empty()) m_hidHandle = hid_open(currentDevice.vendorID, currentDevice.productID, NULL);
else m_hidHandle = hid_open(currentDevice.vendorID, currentDevice.productID, wideSerial.c_str()); else m_hidHandle = hid_open(currentDevice.vendorID, currentDevice.productID, wideSerial.c_str());
if(m_hidHandle == 0) { if(m_hidHandle == 0) {
hid_exit(); hid_exit();
return false; return false;
} }
m_isOpen = true; m_isOpen = true;
return true; return true;
#elif defined(libusb) #elif defined(libusb)
if (libusb_init(&m_ctx) < 0) return false; if (libusb_init(&m_ctx) < 0) return false;
libusb_device **devs; libusb_device **devs;
libusb_device *dev = NULL; libusb_device *dev = NULL;
ssize_t cnt = libusb_get_device_list(m_ctx, &devs); ssize_t cnt = libusb_get_device_list(m_ctx, &devs);
if(cnt >= 0) { if(cnt >= 0) {
for(ssize_t i = 0; i < cnt; i++) { for(ssize_t i = 0; i < cnt; i++) {
libusb_device *device = devs[i]; libusb_device *device = devs[i];
libusb_device_descriptor desc; libusb_device_descriptor desc;
libusb_get_device_descriptor(device, &desc); libusb_get_device_descriptor(device, &desc);
if (vendorID != 0x0 && desc.idVendor != vendorID) continue; if (vendorID != 0x0 && desc.idVendor != vendorID) continue;
else if (productID != 0x0 && desc.idProduct != productID) continue; else if (productID != 0x0 && desc.idProduct != productID) continue;
else if (! serial.empty()) { else if (! serial.empty()) {
if (desc.iSerialNumber <= 0) continue; //Device does not populate serial number if (desc.iSerialNumber <= 0) continue; //Device does not populate serial number
unsigned char buf[256]; unsigned char buf[256];
if (libusb_open(device, &m_hidHandle) != 0){ if (libusb_open(device, &m_hidHandle) != 0){
m_hidHandle = NULL; m_hidHandle = NULL;
continue; continue;
} }
if (libusb_get_string_descriptor_ascii(m_hidHandle, desc.iSerialNumber, buf, 256) >= 1 && serial.compare((char*)buf) == 0) { if (libusb_get_string_descriptor_ascii(m_hidHandle, desc.iSerialNumber, buf, 256) >= 1 && serial.compare((char*)buf) == 0) {
//Make sure entry is a supported keyboard and get model //Make sure entry is a supported keyboard and get model
for (int i=0; i<(int)SupportedKeyboards.size(); i++) { for (int i=0; i<(int)SupportedKeyboards.size(); i++) {
if (desc.idVendor == SupportedKeyboards[i][0]) { if (desc.idVendor == SupportedKeyboards[i][0]) {
if (desc.idProduct == SupportedKeyboards[i][1]) { if (desc.idProduct == SupportedKeyboards[i][1]) {
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.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.iProduct, buf, 256) >= 1) currentDevice.product = string((char*)buf);
currentDevice.serialNumber = serial; currentDevice.serialNumber = serial;
currentDevice.vendorID = desc.idVendor; currentDevice.vendorID = desc.idVendor;
currentDevice.productID = desc.idProduct; currentDevice.productID = desc.idProduct;
currentDevice.model = (KeyboardModel)SupportedKeyboards[i][2]; currentDevice.model = (KeyboardModel)SupportedKeyboards[i][2];
dev = device; dev = device;
libusb_close(m_hidHandle); libusb_close(m_hidHandle);
m_hidHandle = NULL; m_hidHandle = NULL;
break; break;
}
} }
} }
} }
} else {
else {
libusb_close(m_hidHandle);
m_hidHandle = NULL;
continue; //Serial number set but doesn't match
}
}
//For the case where serial is not specified, find first supported device
for (int i=0; i<(int)SupportedKeyboards.size(); i++) {
if (desc.idVendor == SupportedKeyboards[i][0]) {
if (desc.idProduct == SupportedKeyboards[i][1]) {
unsigned char buf[256];
if (libusb_open(device, &m_hidHandle) != 0){
m_hidHandle = NULL;
continue;
}
currentDevice.vendorID = desc.idVendor;
currentDevice.productID = desc.idProduct;
currentDevice.model = (KeyboardModel)SupportedKeyboards[i][2];
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);
libusb_close(m_hidHandle); libusb_close(m_hidHandle);
m_hidHandle=NULL; m_hidHandle = NULL;
break; continue; //Serial number set but doesn't match
} }
} }
//For the case where serial is not specified, find first supported device
for (int i=0; i<(int)SupportedKeyboards.size(); i++) {
if (desc.idVendor == SupportedKeyboards[i][0]) {
if (desc.idProduct == SupportedKeyboards[i][1]) {
unsigned char buf[256];
if (libusb_open(device, &m_hidHandle) != 0){
m_hidHandle = NULL;
continue;
}
currentDevice.vendorID = desc.idVendor;
currentDevice.productID = desc.idProduct;
currentDevice.model = (KeyboardModel)SupportedKeyboards[i][2];
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);
libusb_close(m_hidHandle);
m_hidHandle=NULL;
break;
}
}
}
if (currentDevice.model != KeyboardModel::unknown) break;
} }
if (currentDevice.model != KeyboardModel::unknown) break; libusb_free_device_list(devs, 1);
} }
libusb_free_device_list(devs, 1);
}
if (currentDevice.model == KeyboardModel::unknown) { if (currentDevice.model == KeyboardModel::unknown) {
libusb_exit(m_ctx);
m_ctx = NULL;
return false;
}
if (dev == NULL) m_hidHandle = libusb_open_device_with_vid_pid(m_ctx, currentDevice.vendorID, currentDevice.productID);
else libusb_open(dev, &m_hidHandle);
if(m_hidHandle == NULL) {
libusb_exit(m_ctx);
m_ctx = NULL;
return false;
}
if(libusb_kernel_driver_active(m_hidHandle, 1) == 1) {
if(libusb_detach_kernel_driver(m_hidHandle, 1) != 0) {
libusb_exit(m_ctx); libusb_exit(m_ctx);
m_ctx = NULL; m_ctx = NULL;
return false; return false;
} }
m_isKernellDetached = true;
}
if(libusb_claim_interface(m_hidHandle, 1) < 0) { if (dev == NULL) m_hidHandle = libusb_open_device_with_vid_pid(m_ctx, currentDevice.vendorID, currentDevice.productID);
if(m_isKernellDetached==true) { else libusb_open(dev, &m_hidHandle);
libusb_attach_kernel_driver(m_hidHandle, 1);
m_isKernellDetached = false; if(m_hidHandle == NULL) {
libusb_exit(m_ctx);
m_ctx = NULL;
return false;
} }
libusb_exit(m_ctx);
m_ctx = NULL;
return false;
}
m_isOpen = true; if(libusb_kernel_driver_active(m_hidHandle, 1) == 1) {
return true; if(libusb_detach_kernel_driver(m_hidHandle, 1) != 0) {
libusb_exit(m_ctx);
m_ctx = NULL;
return false;
}
m_isKernellDetached = true;
}
if(libusb_claim_interface(m_hidHandle, 1) < 0) {
if(m_isKernellDetached==true) {
libusb_attach_kernel_driver(m_hidHandle, 1);
m_isKernellDetached = false;
}
libusb_exit(m_ctx);
m_ctx = NULL;
return false;
}
m_isOpen = true;
return true;
#endif #endif
return false; //In case neither is defined return false; //In case neither is defined
} }
LedKeyboard::DeviceInfo LedKeyboard::getCurrentDevice() LedKeyboard::DeviceInfo LedKeyboard::getCurrentDevice() {
{
return currentDevice; return currentDevice;
} }
@ -332,7 +329,6 @@ bool LedKeyboard::close() {
m_hidHandle = NULL; m_hidHandle = NULL;
hid_exit(); hid_exit();
return true; return true;
#elif defined(libusb) #elif defined(libusb)
if (m_hidHandle == NULL) return true; if (m_hidHandle == NULL) return true;
if(libusb_release_interface(m_hidHandle, 1) != 0) return false; if(libusb_release_interface(m_hidHandle, 1) != 0) return false;

View File

@ -224,4 +224,5 @@ namespace utils {
uint16 = std::stoul("0x" + val, nullptr, 16); uint16 = std::stoul("0x" + val, nullptr, 16);
return true; return true;
} }
} }

View File

@ -15,8 +15,7 @@ int commit(LedKeyboard &kbd) {
return 1; return 1;
} }
void printDeviceInfo(LedKeyboard::DeviceInfo device) void printDeviceInfo(LedKeyboard::DeviceInfo device) {
{
std::cout<<"Device: "<<device.manufacturer<<" - "<<device.product<<std::endl; std::cout<<"Device: "<<device.manufacturer<<" - "<<device.product<<std::endl;
std::cout<<"\tVendor ID: "<<std::hex<<std::setw(4)<<std::setfill('0')<<device.vendorID<<std::endl; std::cout<<"\tVendor ID: "<<std::hex<<std::setw(4)<<std::setfill('0')<<device.vendorID<<std::endl;
std::cout<<"\tProduct ID: "<<std::hex<<std::setw(4)<<std::setfill('0')<<device.productID<<std::endl; std::cout<<"\tProduct ID: "<<std::hex<<std::setw(4)<<std::setfill('0')<<device.productID<<std::endl;
@ -302,14 +301,17 @@ int main(int argc, char **argv) {
else if (argc > (argIndex + 1) && arg == "-mr") return setMRKey(kbd, argv[argIndex + 1]); else if (argc > (argIndex + 1) && arg == "-mr") return setMRKey(kbd, argv[argIndex + 1]);
else if (argc > (argIndex + 1) && arg == "-mn") return setMNKey(kbd, argv[argIndex + 1]); else if (argc > (argIndex + 1) && arg == "-mn") return setMNKey(kbd, argv[argIndex + 1]);
else if (argc > (argIndex + 1) && arg == "-an") return setAllKeys(kbd, argv[argIndex + 1], false); else if (argc > (argIndex + 1) && arg == "-an") return setAllKeys(kbd, argv[argIndex + 1], false);
else if (argc > (argIndex + 2) && arg == "-gn") return setGroupKeys(kbd, argv[argIndex + 1], argv[argIndex + 2], false); else if (argc > (argIndex + 2) && arg == "-gn")
return setGroupKeys(kbd, argv[argIndex + 1], argv[argIndex + 2], false);
else if (argc > (argIndex + 2) && arg == "-kn") return setKey(kbd, argv[argIndex + 1], argv[argIndex + 2], false); else if (argc > (argIndex + 2) && arg == "-kn") return setKey(kbd, argv[argIndex + 1], argv[argIndex + 2], false);
else if (argc > (argIndex + 2) && arg == "-r") return setRegion(kbd, argv[argIndex + 1], argv[argIndex + 2]); else if (argc > (argIndex + 2) && arg == "-r") return setRegion(kbd, argv[argIndex + 1], argv[argIndex + 2]);
else if (argc > (argIndex + 1) && arg == "-gkm") return setGKeysMode(kbd, argv[argIndex + 1]); else if (argc > (argIndex + 1) && arg == "-gkm") return setGKeysMode(kbd, argv[argIndex + 1]);
else if (argc > (argIndex + 1) && arg == "-p") return loadProfile(kbd, argv[argIndex + 1]); else if (argc > (argIndex + 1) && arg == "-p") return loadProfile(kbd, argv[argIndex + 1]);
else if (arg == "-pp") return pipeProfile(kbd); else if (arg == "-pp") return pipeProfile(kbd);
else if (argc > (argIndex + 4) && arg == "-fx") return setFX(kbd, argv[argIndex + 1], argv[argIndex + 2], argv[argIndex + 3], argv[argIndex + 4]); else if (argc > (argIndex + 4) && arg == "-fx")
else if (argc > (argIndex + 3) && arg == "-fx") return setFX(kbd, argv[argIndex + 1], argv[argIndex + 2], argv[argIndex + 3]); return setFX(kbd, argv[argIndex + 1], argv[argIndex + 2], argv[argIndex + 3], argv[argIndex + 4]);
else if (argc > (argIndex + 3) && arg == "-fx")
return setFX(kbd, argv[argIndex + 1], argv[argIndex + 2], argv[argIndex + 3]);
else if (argc > (argIndex + 1) && arg == "--startup-mode") return setStartupMode(kbd, argv[argIndex + 1]); else if (argc > (argIndex + 1) && arg == "--startup-mode") return setStartupMode(kbd, argv[argIndex + 1]);
else { help::usage(argv[0]); return 1; } else { help::usage(argv[0]); return 1; }
} }