From 31144f67ce4b61ad938ec7f5f7ee570a39deeec6 Mon Sep 17 00:00:00 2001 From: MatMoul Date: Sat, 12 Nov 2016 00:34:59 +0100 Subject: [PATCH] Move keyboard detection to Keyboard.cpp --- src/classes/Keyboard.cpp | 27 +++++++++++++++++++-- src/classes/Keyboard.h | 2 +- src/main.cpp | 52 +++++----------------------------------- 3 files changed, 32 insertions(+), 49 deletions(-) diff --git a/src/classes/Keyboard.cpp b/src/classes/Keyboard.cpp index 6850dfc..98169de 100644 --- a/src/classes/Keyboard.cpp +++ b/src/classes/Keyboard.cpp @@ -9,12 +9,35 @@ bool Keyboard::isAttached() { return m_isAttached; } -bool Keyboard::attach(int lg_pid) { +bool Keyboard::attach() { if (m_isAttached == true) return false; int r; r = libusb_init(&ctx); if (r < 0) return false; - dev_handle = libusb_open_device_with_vid_pid(ctx, 0x046d, lg_pid); + + libusb_device **devs; + ssize_t cnt = libusb_get_device_list(ctx, &devs); + if(cnt < 0) return false; + int pid = 0; + for(ssize_t i = 0; i < cnt; i++) { + libusb_device *device = devs[i]; + libusb_device_descriptor desc = {0}; + libusb_get_device_descriptor(device, &desc); + if (desc.idVendor == 0x046d) { + if (desc.idProduct == 0xc331) { pid = desc.idProduct; break; } // G810 + if (desc.idProduct == 0xc337) { pid = desc.idProduct; break; } // G810 + if (desc.idProduct == 0xc330) { pid = desc.idProduct; break; } // G410 + if (desc.idProduct == 0xc333) { pid = desc.idProduct; break; } // G610 + } + } + libusb_free_device_list(devs, 1); + if (pid == 0) { + libusb_exit(ctx); + ctx = NULL; + return false; + } + + dev_handle = libusb_open_device_with_vid_pid(ctx, 0x046d, pid); if (dev_handle == NULL) { libusb_exit(ctx); ctx = NULL; diff --git a/src/classes/Keyboard.h b/src/classes/Keyboard.h index 515d373..f4dfbb4 100644 --- a/src/classes/Keyboard.h +++ b/src/classes/Keyboard.h @@ -35,7 +35,7 @@ class Keyboard { struct KeyValue { KeyAddress key; KeyColors colors; }; bool isAttached(); - bool attach(int lg_pid); + bool attach(); bool detach(); bool commit(); bool getKeyAddress(Key key, KeyAddress &keyAddress); diff --git a/src/main.cpp b/src/main.cpp index d84e1b2..76b10fb 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,21 +1,12 @@ -#include -#include #include -#include #include -#include #include -#include -#include "/usr/include/libusb-1.0/libusb.h" #include "classes/Keyboard.h" using namespace std; -int lg_pid; - void usage() { string appname = "g810-led"; - if (lg_pid == 0xc330) appname = "g410-led"; cout< var; vector keys; - lg_kbd.attach(lg_pid); + lg_kbd.attach(); while (!file.eof()) { getline(file, line); @@ -294,40 +284,10 @@ int loadProfile(string profileFile) { return 1; } - -void findG810() { - libusb_device **devs; - libusb_context *ctx = NULL; - int r; - ssize_t cnt; - r = libusb_init(&ctx); - if(r < 0) return; - cnt = libusb_get_device_list(ctx, &devs); - if(cnt < 0) return; - ssize_t i; - for(i = 0; i < cnt; i++) { - libusb_device *device = devs[i]; - libusb_device_descriptor desc = {0}; - libusb_get_device_descriptor(device, &desc); - if (desc.idVendor == 0x046d) { - if (desc.idProduct == 0xc331) { lg_pid=0xc331; break; } - if (desc.idProduct == 0xc337) { lg_pid=0xc337; break; } - } - } - libusb_free_device_list(devs, 1); - libusb_exit(ctx); -} - - - int main(int argc, char *argv[]) { string str = argv[0]; size_t split = str.find_last_of("/\\"); str = str.substr(split + 1); - - if (str == "g410-led") lg_pid=0xc330; - else findG810(); - if (argc > 1) { string argCmd = argv[1]; if (argCmd == "-h" || argCmd == "--help") { usage(); return 0; }