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

Augment command arg parsing to set specific device

Change argument parsing to segregate options from the command

Add -dv -dp and -ds options to allow matching a vendor ID, device ID,
and/or device serial number.

Modify --list-keyboards to parse the DeviceInfo vector and output
information to stdout.

Signed-off-by: Kevin Pearson <pearson.kevin.m@gmail.com>
This commit is contained in:
Kevin Pearson 2017-04-27 12:39:40 -04:00
parent 53876ac54e
commit 114fa9b703
2 changed files with 95 additions and 45 deletions

View File

@ -16,6 +16,8 @@ namespace help {
cout<<"--------"<<endl;
cout<<"Version : "<<version<<endl;
cout<<endl;
cout<<"Usage: "<<cmdName<<" [OPTIONS...] [command] (command arguments)"<<endl;
cout<<"Commands:"<<endl;
cout<<" -a {color}\t\t\t\tSet all keys color"<<endl;
cout<<" -g {keygroup} {color}\t\t\tSet key group color"<<endl;
cout<<" -k {key} {color}\t\t\tSet key color"<<endl;
@ -42,13 +44,18 @@ namespace help {
cout<<" --startup-mode {startup mode}\t\tSet startup mode"<<endl;
cout<<endl;
cout<<" --list-keyboards \t\t\tList connected keyboards"<<endl;
cout<<" --print-device\t\t\tPrint device information for the keyboard"<<endl;
cout<<endl;
cout<<" --help\t\t\t\tThis help"<<endl;
cout<<" --help-keys\t\t\t\tHelp for keys in groups"<<endl;
cout<<" --help-effects\t\t\tHelp for native effects"<<endl;
cout<<" --help-samples\t\t\tUsage samples"<<endl;
cout<<""<<endl;
cout<<""<<endl;
cout<<endl;
cout<<"Options:"<<endl;
cout<<" -dv\t\t\t\t\tDevice vendor ID, such as 046d for Logitech. Can be omitted to match any vendor ID"<<endl;
cout<<" -dp\t\t\t\t\tDevice product ID, such as c337 for Logitech G810. Can be omitted to match any product ID"<<endl;
cout<<" -ds\t\t\t\t\tDevice serial number, Can be omitted to match the first device found"<<endl;
cout<<"--------"<<endl;
if (cmdName == "g610-led")
cout<<"color formats :\t\t\t\tII (hex value for intensity)"<<endl;
else
@ -61,7 +68,7 @@ namespace help {
cout<<"key values :\t\t\t\tabc... 123... and other (use --help-keys for more detail)"<<endl;
cout<<"group values :\t\t\t\tlogo, indicators, fkeys, ... (use --help-keys for more detail)"<<endl;
cout<<"startup mode :\t\t\t\twave, color"<<endl;
cout<<""<<endl;
cout<<endl;
}
void keys(char *arg0) {

View File

@ -1,3 +1,4 @@
#include <iomanip>
#include <iostream>
#include <unistd.h>
#include <fstream>
@ -14,6 +15,27 @@ int commit(LedKeyboard &kbd) {
return 1;
}
void printDeviceInfo(LedKeyboard::DeviceInfo device)
{
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<<"\tProduct ID: "<<std::hex<<std::setw(4)<<std::setfill('0')<<device.productID<<std::endl;
std::cout<<"\tSerial Number: "<<device.serialNumber<<std::endl;
}
int listKeyboards(LedKeyboard &kbd) {
std::vector<LedKeyboard::DeviceInfo> deviceList = kbd.listKeyboards();
if (deviceList.empty()) return 1;
std::vector<LedKeyboard::DeviceInfo>::iterator iterator;
for (iterator = deviceList.begin(); iterator != deviceList.end(); iterator++) {
LedKeyboard::DeviceInfo device = *iterator;
printDeviceInfo(device);
}
return 0;
}
int setAllKeys(LedKeyboard &kbd, std::string arg2, bool commit = true) {
LedKeyboard::Color color;
if (! utils::parseColor(arg2, color)) return 1;
@ -226,47 +248,68 @@ int pipeProfile(LedKeyboard &kbd) {
int main(int argc, char **argv) {
if (argc > 1) {
std::string arg = argv[1];
if (arg == "--help" || arg == "-h") help::usage(argv[0]);
else if (arg == "--help-keys") help::keys(argv[0]);
else if (arg == "--help-effects") help::effects(argv[0]);
else if (arg == "--help-samples") help::samples(argv[0]);
else {
LedKeyboard kbd;
if (arg == "--list-keyboards") kbd.listKeyboards();
else if (arg == "-c") return commit(kbd);
else if (argc > 2 && arg == "-a") return setAllKeys(kbd, argv[2]);
else if (argc > 3 && arg == "-g") return setGroupKeys(kbd, argv[2], argv[3]);
else if (argc > 3 && arg == "-k") return setKey(kbd, argv[2], argv[3]);
else if (argc > 2 && arg == "-mr") return setMRKey(kbd, argv[2]);
else if (argc > 2 && arg == "-mn") return setMNKey(kbd, argv[2]);
else if (argc > 2 && arg == "-an") return setAllKeys(kbd, argv[2], false);
else if (argc > 3 && arg == "-gn") return setGroupKeys(kbd, argv[2], argv[3], false);
else if (argc > 3 && arg == "-kn") return setKey(kbd, argv[2], argv[3], false);
else if (argc > 3 && arg == "-r") return setRegion(kbd, argv[2], argv[3]);
else if (argc > 2 && arg == "-gkm") return setGKeysMode(kbd, argv[2]);
else if (argc > 2 && arg == "-p") return loadProfile(kbd, argv[2]);
else if (argc > 1 && arg == "-pp") return pipeProfile(kbd);
else if (argc > 5 && arg == "-fx") return setFX(kbd, argv[2], argv[3], argv[4], argv[5]);
else if (argc > 4 && arg == "-fx") return setFX(kbd, argv[2], argv[3], argv[4]);
else if (argc > 2 && arg == "--startup-mode") return setStartupMode(kbd, argv[2]);
else { help::usage(argv[0]); return 1; }
}
return 0;
if (argc < 2) {
help::usage(argv[0]);
return 1;
}
help::usage(argv[0]);
return 1;
LedKeyboard kbd;
std::string serial;
uint16_t vendorID = 0x0;
uint16_t productID = 0x0;
int argIndex = 1;
while (argIndex < argc)
{
std::string arg = argv[argIndex];
// Non-Command arguments
if (argc > (argIndex + 1) && arg == "-ds") {
serial = argv[argIndex + 1];
argIndex += 2;
continue;
}
else if (argc > (argIndex + 1) && arg == "-dv"){
if (! utils::parseUInt16(argv[argIndex + 1], vendorID)) return 1;
argIndex += 2;
continue;
}
else if (argc > (argIndex + 1) && arg == "-dp"){
if (! utils::parseUInt16(argv[argIndex + 1], productID)) return 1;
argIndex += 2;
continue;
}
if (!kbd.open(vendorID, productID, serial)) {
std::cout << "Matching or compatible device not found" << std::endl;
return 2;
}
// Command arguments, these will cause parsing to ignore anything beyond the command and its arguments
if (arg == "-c") return commit(kbd);
else if (arg == "--help" || arg == "-h") {help::usage(argv[0]); return 0;}
else if (arg == "--help-keys") {help::keys(argv[0]); return 0;}
else if (arg == "--help-effects") {help::effects(argv[0]); return 0;}
else if (arg == "--help-samples") {help::samples(argv[0]); return 0;}
else if (arg == "--list-keyboards") return listKeyboards(kbd);
else if (arg == "--print-device") {printDeviceInfo(kbd.getCurrentDevice()); return 0;}
else if (argc > (argIndex + 1) && arg == "-a") return setAllKeys(kbd, argv[argIndex + 1]);
else if (argc > (argIndex + 2) && arg == "-g") return setGroupKeys(kbd, argv[argIndex + 1], argv[argIndex + 2]);
else if (argc > (argIndex + 2) && arg == "-k") return setKey(kbd, argv[argIndex + 1], argv[argIndex + 2]);
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 == "-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 == "-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 + 1) && arg == "-gkm") return setGKeysMode(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 (argc > (argIndex + 4) && arg == "-fx") 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 { help::usage(argv[0]); return 1; }
}
return 0;
}