2017-04-27 16:39:40 +00:00
|
|
|
#include <iomanip>
|
2017-01-21 08:15:18 +00:00
|
|
|
#include <iostream>
|
2017-01-06 05:08:09 +00:00
|
|
|
#include <unistd.h>
|
2017-01-21 08:15:18 +00:00
|
|
|
#include <fstream>
|
|
|
|
#include <map>
|
2016-06-12 20:16:07 +00:00
|
|
|
|
2017-01-21 08:15:18 +00:00
|
|
|
#include "helpers/help.h"
|
|
|
|
#include "helpers/utils.h"
|
|
|
|
#include "classes/Keyboard.h"
|
2016-10-29 13:20:41 +00:00
|
|
|
|
2016-06-12 20:16:07 +00:00
|
|
|
|
2017-01-21 08:15:18 +00:00
|
|
|
int commit(LedKeyboard &kbd) {
|
|
|
|
if (! kbd.open()) return 1;
|
|
|
|
if (kbd.commit()) return 0;
|
2016-12-08 21:43:08 +00:00
|
|
|
return 1;
|
2016-06-12 20:16:07 +00:00
|
|
|
}
|
|
|
|
|
2017-04-29 14:58:02 +00:00
|
|
|
void printDeviceInfo(LedKeyboard::DeviceInfo device) {
|
2017-04-27 16:39:40 +00:00
|
|
|
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();
|
2017-05-06 20:41:08 +00:00
|
|
|
if (deviceList.empty()) {
|
|
|
|
std::cout<<"Matching or compatible device not found !"<<std::endl;
|
|
|
|
return 1;
|
|
|
|
}
|
2017-04-27 16:39:40 +00:00
|
|
|
|
|
|
|
std::vector<LedKeyboard::DeviceInfo>::iterator iterator;
|
|
|
|
for (iterator = deviceList.begin(); iterator != deviceList.end(); iterator++) {
|
|
|
|
LedKeyboard::DeviceInfo device = *iterator;
|
|
|
|
printDeviceInfo(device);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-01-21 08:15:18 +00:00
|
|
|
int setAllKeys(LedKeyboard &kbd, std::string arg2, bool commit = true) {
|
|
|
|
LedKeyboard::Color color;
|
|
|
|
if (! utils::parseColor(arg2, color)) return 1;
|
|
|
|
if (! kbd.open()) return 1;
|
|
|
|
if(! kbd.setAllKeys(color)) return 1;
|
|
|
|
if (commit) if(! kbd.commit()) return 1;
|
|
|
|
return 0;
|
2016-06-12 20:16:07 +00:00
|
|
|
}
|
|
|
|
|
2017-01-21 08:15:18 +00:00
|
|
|
int setGroupKeys(LedKeyboard &kbd, std::string arg2, std::string arg3, bool commit = true) {
|
|
|
|
LedKeyboard::KeyGroup keyGroup;
|
|
|
|
LedKeyboard::Color color;
|
|
|
|
if (! utils::parseKeyGroup(arg2, keyGroup)) return 1;
|
|
|
|
if (! utils::parseColor(arg3, color)) return 1;
|
|
|
|
if (! kbd.open()) return 1;
|
|
|
|
if (! kbd.setGroupKeys(keyGroup, color)) return 1;
|
|
|
|
if (commit) if(! kbd.commit()) return 1;
|
|
|
|
return 0;
|
2016-06-12 20:16:07 +00:00
|
|
|
}
|
|
|
|
|
2017-01-21 08:15:18 +00:00
|
|
|
int setKey(LedKeyboard &kbd, std::string arg2, std::string arg3, bool commit = true) {
|
|
|
|
LedKeyboard::Key key;
|
|
|
|
LedKeyboard::Color color;
|
|
|
|
if (! utils::parseKey(arg2, key)) return 1;
|
|
|
|
if (! utils::parseColor(arg3, color)) return 1;
|
|
|
|
LedKeyboard::KeyValue keyValue = { key, color };
|
|
|
|
if (! kbd.open()) return 1;
|
|
|
|
if (! kbd.setKey(keyValue)) return 1;
|
|
|
|
if (commit) if(! kbd.commit()) return 1;
|
|
|
|
return 0;
|
2016-06-12 20:16:07 +00:00
|
|
|
}
|
|
|
|
|
2017-03-11 20:34:29 +00:00
|
|
|
int setMRKey(LedKeyboard &kbd, std::string arg2) {
|
2017-03-11 16:56:40 +00:00
|
|
|
uint8_t value;
|
|
|
|
if (! utils::parseUInt8(arg2, value)) return 1;
|
|
|
|
if (! kbd.open()) return 1;
|
|
|
|
if (! kbd.setMRKey(value)) return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-03-11 20:34:29 +00:00
|
|
|
int setMNKey(LedKeyboard &kbd, std::string arg2) {
|
2017-03-11 16:56:40 +00:00
|
|
|
uint8_t value;
|
|
|
|
if (! utils::parseUInt8(arg2, value)) return 1;
|
|
|
|
if (! kbd.open()) return 1;
|
|
|
|
if (! kbd.setMNKey(value)) return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-03-11 20:34:29 +00:00
|
|
|
int setGKeysMode(LedKeyboard &kbd, std::string arg2) {
|
2017-03-11 20:14:50 +00:00
|
|
|
uint8_t value;
|
|
|
|
if (! utils::parseUInt8(arg2, value)) return 1;
|
|
|
|
if (! kbd.open()) return 1;
|
|
|
|
if (! kbd.setGKeysMode(value)) return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-04-17 16:37:57 +00:00
|
|
|
int setRegion(LedKeyboard &kbd, std::string arg2, std::string arg3) {
|
|
|
|
uint8_t region = 0;
|
|
|
|
LedKeyboard::Color color;
|
|
|
|
if (! utils::parseColor(arg3, color)) return 1;
|
|
|
|
if (! utils::parseUInt8(arg2, region)) return 1;
|
2017-04-21 21:00:21 +00:00
|
|
|
if (kbd.setRegion(region, color)) return 0;
|
2017-04-17 16:37:57 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2016-12-30 00:30:17 +00:00
|
|
|
|
2017-01-21 08:15:18 +00:00
|
|
|
int setFX(LedKeyboard &kbd, std::string arg2, std::string arg3, std::string arg4, std::string arg5 = "") {
|
|
|
|
LedKeyboard::NativeEffect effect;
|
|
|
|
LedKeyboard::NativeEffectPart effectPart;
|
|
|
|
uint8_t speed = 0;
|
|
|
|
LedKeyboard::Color color;
|
|
|
|
if (! utils::parseNativeEffect(arg2, effect)) return 1;
|
|
|
|
if (! utils::parseNativeEffectPart(arg3, effectPart)) return 1;
|
2017-05-26 14:11:16 +00:00
|
|
|
|
2017-01-21 08:15:18 +00:00
|
|
|
switch (effect) {
|
|
|
|
case LedKeyboard::NativeEffect::color:
|
|
|
|
if (! utils::parseColor(arg4, color)) return 1;
|
|
|
|
break;
|
|
|
|
case LedKeyboard::NativeEffect::breathing:
|
|
|
|
if (! utils::parseColor(arg4, color)) return 1;
|
|
|
|
if (arg5 == "") return 1;
|
|
|
|
if (! utils::parseSpeed(arg5, speed)) return 1;
|
|
|
|
break;
|
|
|
|
case LedKeyboard::NativeEffect::cycle:
|
|
|
|
case LedKeyboard::NativeEffect::hwave:
|
|
|
|
case LedKeyboard::NativeEffect::vwave:
|
|
|
|
case LedKeyboard::NativeEffect::cwave:
|
|
|
|
if (! utils::parseSpeed(arg4, speed)) return 1;
|
|
|
|
break;
|
2016-12-30 16:55:19 +00:00
|
|
|
}
|
2017-05-26 14:11:16 +00:00
|
|
|
|
2017-01-21 08:15:18 +00:00
|
|
|
if (! kbd.open()) return 1;
|
2017-05-26 14:11:16 +00:00
|
|
|
|
|
|
|
if (! kbd.setNativeEffect(effect, effectPart, speed, color)) return 1;
|
|
|
|
|
|
|
|
return 0;
|
2016-12-30 16:55:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-01-21 08:15:18 +00:00
|
|
|
int setStartupMode(LedKeyboard &kbd, std::string arg2) {
|
|
|
|
LedKeyboard::StartupMode startupMode;
|
|
|
|
if (! utils::parseStartupMode(arg2, startupMode)) return 1;
|
|
|
|
if (! kbd.open()) return 1;
|
|
|
|
if (kbd.setStartupMode(startupMode)) return 0;
|
2016-12-30 20:35:26 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2016-12-30 16:55:19 +00:00
|
|
|
|
|
|
|
|
2017-01-21 08:15:18 +00:00
|
|
|
int parseProfile(LedKeyboard &kbd, std::istream &stream) {
|
|
|
|
std::string line;
|
|
|
|
std::map<std::string, std::string> vars;
|
|
|
|
LedKeyboard::KeyValueArray keys = {};
|
|
|
|
int retval = 0;
|
|
|
|
while (!stream.eof()) {
|
|
|
|
getline(stream, line);
|
|
|
|
if (line.size() > 0 && line.substr(0, 1) != "#") {
|
|
|
|
std::vector<std::string> args = {};
|
|
|
|
while (line.size() > 0) {
|
|
|
|
uint32_t ind = line.find(" ");
|
|
|
|
std::string argValue = line.substr(0, ind);
|
|
|
|
if (argValue.substr(0, 1) == "$") argValue = vars[argValue.substr(1)];
|
|
|
|
args.push_back(argValue);
|
|
|
|
if (line.substr(0, ind) == line) line.clear();
|
|
|
|
else line = line.substr(ind + 1);
|
2017-01-06 05:08:09 +00:00
|
|
|
}
|
2017-01-21 08:15:18 +00:00
|
|
|
if (args[0] == "var" && args.size() > 2) {
|
|
|
|
vars[args[1]] = args[2];
|
|
|
|
} else if (args[0] == "c") {
|
|
|
|
if (kbd.open()) {
|
|
|
|
if (keys.size() > 0) {
|
|
|
|
if (! kbd.setKeys(keys)) retval = 1;
|
|
|
|
keys.clear();
|
|
|
|
}
|
|
|
|
if(! kbd.commit()) retval = 1;
|
|
|
|
} else retval = 1;
|
|
|
|
} else if (args[0] == "a" && args.size() > 1) {
|
|
|
|
if (setAllKeys(kbd, args[1], false) == 1) retval = 1;
|
|
|
|
} else if (args[0] == "g" && args.size() > 2) {
|
|
|
|
if (setGroupKeys(kbd, args[1], args[2], false) == 1) retval = 1;
|
|
|
|
} else if (args[0] == "k" && args.size() > 2) {
|
|
|
|
LedKeyboard::Key key;
|
|
|
|
LedKeyboard::Color color;
|
|
|
|
if (utils::parseKey(args[1], key))
|
|
|
|
if (utils::parseColor(args[2], color))
|
|
|
|
keys.push_back({ key, color });
|
2017-04-21 21:00:21 +00:00
|
|
|
} else if (args[0] == "r" && args.size() > 2) {
|
|
|
|
if (setRegion(kbd, args[1], args[2]) == 1) retval = 1;
|
2017-03-11 17:11:40 +00:00
|
|
|
} else if (args[0] == "mr" && args.size() > 1) {
|
2017-03-11 20:35:39 +00:00
|
|
|
if (setMRKey(kbd, args[1]) == 1) retval = 1;
|
2017-03-11 17:11:40 +00:00
|
|
|
} else if (args[0] == "mn" && args.size() > 1) {
|
2017-03-11 20:35:39 +00:00
|
|
|
if (setMNKey(kbd, args[1]) == 1) retval = 1;
|
2017-03-11 20:14:50 +00:00
|
|
|
} else if (args[0] == "gkm" && args.size() > 1) {
|
2017-03-11 20:35:39 +00:00
|
|
|
if (setGKeysMode(kbd, args[1]) == 1) retval = 1;
|
2017-01-21 08:15:18 +00:00
|
|
|
} else if (args[0] == "fx" && args.size() > 4) {
|
|
|
|
if (setFX(kbd, args[1], args[2], args[3], args[4]) == 1) retval = 1;
|
|
|
|
} else if (args[0] == "fx" && args.size() > 3) {
|
|
|
|
if (setFX(kbd, args[1], args[2], args[3]) == 1) retval = 1;
|
2017-01-08 02:23:13 +00:00
|
|
|
}
|
2016-12-08 21:43:08 +00:00
|
|
|
}
|
2017-01-06 05:08:09 +00:00
|
|
|
}
|
2017-01-21 08:15:18 +00:00
|
|
|
return retval;
|
2017-01-06 05:08:09 +00:00
|
|
|
}
|
2017-01-21 08:15:18 +00:00
|
|
|
|
|
|
|
int loadProfile(LedKeyboard &kbd, char *arg2) {
|
|
|
|
std::ifstream file;
|
|
|
|
file.open(arg2);
|
2017-01-06 05:08:09 +00:00
|
|
|
if (file.is_open()) {
|
2017-01-21 08:15:18 +00:00
|
|
|
int retval = 0;
|
|
|
|
retval = parseProfile(kbd, file);
|
2016-12-08 21:43:08 +00:00
|
|
|
file.close();
|
2017-01-06 05:08:09 +00:00
|
|
|
return retval;
|
2016-12-08 21:43:08 +00:00
|
|
|
}
|
|
|
|
return 1;
|
2016-06-12 20:16:07 +00:00
|
|
|
}
|
|
|
|
|
2017-01-21 08:15:18 +00:00
|
|
|
int pipeProfile(LedKeyboard &kbd) {
|
2017-01-06 05:08:09 +00:00
|
|
|
if (isatty(fileno(stdin))) return 1;
|
2017-01-21 08:15:18 +00:00
|
|
|
return parseProfile(kbd, std::cin);
|
2017-01-06 05:08:09 +00:00
|
|
|
}
|
|
|
|
|
2017-01-21 08:15:18 +00:00
|
|
|
|
|
|
|
|
|
|
|
int main(int argc, char **argv) {
|
2017-04-27 16:39:40 +00:00
|
|
|
if (argc < 2) {
|
|
|
|
help::usage(argv[0]);
|
|
|
|
return 1;
|
|
|
|
}
|
2017-01-21 08:15:18 +00:00
|
|
|
|
2017-04-27 16:39:40 +00:00
|
|
|
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;
|
2017-05-06 20:41:08 +00:00
|
|
|
} else if (argc > (argIndex + 1) && arg == "-dv"){
|
2017-04-27 16:39:40 +00:00
|
|
|
if (! utils::parseUInt16(argv[argIndex + 1], vendorID)) return 1;
|
|
|
|
argIndex += 2;
|
|
|
|
continue;
|
2017-05-06 20:41:08 +00:00
|
|
|
} else if (argc > (argIndex + 1) && arg == "-dp"){
|
2017-04-27 16:39:40 +00:00
|
|
|
if (! utils::parseUInt16(argv[argIndex + 1], productID)) return 1;
|
|
|
|
argIndex += 2;
|
|
|
|
continue;
|
2017-05-06 20:41:08 +00:00
|
|
|
} else if (argc > (argIndex + 1) && arg == "-tuk"){
|
|
|
|
uint8_t kbdProtocol = 0;
|
|
|
|
if (! utils::parseUInt8(argv[argIndex + 1], kbdProtocol)) return 1;
|
|
|
|
switch(kbdProtocol) {
|
|
|
|
case 1:
|
2017-05-07 02:02:34 +00:00
|
|
|
kbd.SupportedKeyboards = { { vendorID, productID, (u_int16_t)LedKeyboard::KeyboardModel::g810 } };
|
2017-05-06 20:41:08 +00:00
|
|
|
break;
|
|
|
|
case 2:
|
2017-05-07 02:02:34 +00:00
|
|
|
kbd.SupportedKeyboards = { { vendorID, productID, (u_int16_t)LedKeyboard::KeyboardModel::g910 } };
|
2017-05-06 20:41:08 +00:00
|
|
|
break;
|
|
|
|
case 3:
|
2017-05-07 02:02:34 +00:00
|
|
|
kbd.SupportedKeyboards = { { vendorID, productID, (u_int16_t)LedKeyboard::KeyboardModel::g213 } };
|
2017-05-06 20:41:08 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
argIndex += 2;
|
|
|
|
continue;
|
2017-04-27 16:39:40 +00:00
|
|
|
}
|
2017-05-06 20:41:08 +00:00
|
|
|
|
2017-04-27 16:39:40 +00:00
|
|
|
|
2017-04-29 12:54:14 +00:00
|
|
|
//Commands that do not need to initialize a specific device
|
|
|
|
if (arg == "--help" || arg == "-h") {help::usage(argv[0]); return 0;}
|
|
|
|
else if (arg == "--list-keyboards") return listKeyboards(kbd);
|
|
|
|
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;}
|
|
|
|
|
|
|
|
//Initialize the device for use
|
2017-04-27 16:39:40 +00:00
|
|
|
if (!kbd.open(vendorID, productID, serial)) {
|
2017-05-06 20:41:08 +00:00
|
|
|
std::cout << "Matching or compatible device not found !" << std::endl;
|
2017-04-27 16:39:40 +00:00
|
|
|
return 2;
|
|
|
|
}
|
2017-05-06 20:41:08 +00:00
|
|
|
|
2017-04-27 16:39:40 +00:00
|
|
|
// Command arguments, these will cause parsing to ignore anything beyond the command and its arguments
|
|
|
|
if (arg == "-c") return commit(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);
|
2017-04-29 14:58:02 +00:00
|
|
|
else if (argc > (argIndex + 2) && arg == "-gn")
|
|
|
|
return setGroupKeys(kbd, argv[argIndex + 1], argv[argIndex + 2], false);
|
2017-04-27 16:39:40 +00:00
|
|
|
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);
|
2017-04-29 14:58:02 +00:00
|
|
|
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]);
|
2017-04-27 16:39:40 +00:00
|
|
|
else if (argc > (argIndex + 1) && arg == "--startup-mode") return setStartupMode(kbd, argv[argIndex + 1]);
|
|
|
|
else { help::usage(argv[0]); return 1; }
|
2016-12-08 21:43:08 +00:00
|
|
|
}
|
2017-04-27 16:39:40 +00:00
|
|
|
|
|
|
|
return 0;
|
2016-06-12 20:16:07 +00:00
|
|
|
}
|