1
0
mirror of https://github.com/MatMoul/g810-led.git synced 2024-12-23 17:26:11 +00:00
g810-led/src/main.cpp

231 lines
6.9 KiB
C++
Raw Normal View History

2016-06-12 20:16:07 +00:00
#include <iostream>
#include <map>
#include <string>
#include <vector>
#include <algorithm>
#include <fstream>
2016-08-07 17:40:48 +00:00
#include <string>
2016-06-12 20:16:07 +00:00
#include "classes/Keyboard.h"
using namespace std;
2016-08-07 18:22:27 +00:00
int lg_pid = 0xc331; // g810 by default
2016-06-12 20:16:07 +00:00
void usage() {
2016-08-07 18:22:27 +00:00
string appname = "g810-led";
if (lg_pid == 0xc330) appname = "g410-led";
cout<<appname<<" Usages :\n";
2016-06-12 20:16:07 +00:00
cout<<"-----------------\n";
cout<<"\n";
cout<<" -s effect :\t\tSet keyboard startup effect\n";
cout<<"\n";
cout<<" -a color :\t\tSet all keys\n";
cout<<" -g group, color :\tSet a group of keys\n";
cout<<" -k key, color :\tSet a key\n";
2016-08-16 22:15:02 +00:00
cout<<"\n";
cout<<" -an color :\t\tSet all keys without commit\n";
cout<<" -gn group, color :\tSet a group of keys without commit\n";
cout<<" -kn key, color :\tSet a key without commit\n";
cout<<"\n";
cout<<" -c :\t\t\tCommit changes\n";
cout<<"\n";
2016-06-12 20:16:07 +00:00
cout<<" -p porfilefile :\tLoad a profile\n";
cout<<"\n";
cout<<" -h | --help :\t\tthis help message\n";
cout<<"\n";
cout<<"color formats :\t\tRRGGBB (hex value for red, green and blue)\n";
cout<<"\n";
cout<<"effect values :\t\trainbow, color\n";
cout<<"key values :\t\tabc... 123... and other\n";
cout<<"group values :\t\tlogo, indicators, fkeys, modifiers, multimedia, arrows, numeric, functions, keys\n";
cout<<"\n";
cout<<"sample :\n";
2016-08-07 18:22:27 +00:00
cout<<appname<<" -k logo ff0000\n";
cout<<appname<<" -a 00ff00\n";
cout<<appname<<" -g fkeys ff00ff\n";
cout<<appname<<" -s color\n";
2016-06-12 20:16:07 +00:00
}
int commit() {
2016-08-07 18:22:27 +00:00
Keyboard lg_kbd;
lg_kbd.attach(lg_pid);
lg_kbd.commit();
lg_kbd.detach();
2016-06-12 20:16:07 +00:00
return 0;
}
int setStartupEffect(string effect) {
2016-08-07 18:22:27 +00:00
Keyboard lg_kbd;
2016-06-12 20:16:07 +00:00
Keyboard::PowerOnEffect powerOnEffect;
2016-08-07 18:22:27 +00:00
if (lg_kbd.parsePowerOnEffect(effect, powerOnEffect) == true) {
lg_kbd.attach(lg_pid);
lg_kbd.setPowerOnEffect(powerOnEffect);
lg_kbd.commit();
lg_kbd.detach();
2016-06-12 20:16:07 +00:00
return 0;
}
return 1;
}
int setKey(string key, string color, bool commit) {
2016-08-07 18:22:27 +00:00
Keyboard lg_kbd;
2016-06-12 20:16:07 +00:00
Keyboard::KeyAddress keyAddress;
2016-08-07 18:22:27 +00:00
if (lg_kbd.parseKey(key, keyAddress) == true) {
2016-06-12 20:16:07 +00:00
Keyboard::KeyColors colors;
2016-08-07 18:22:27 +00:00
if (lg_kbd.parseColor(color, colors) == true) {
2016-06-12 20:16:07 +00:00
Keyboard::KeyValue keyValue;
keyValue.key = keyAddress;
keyValue.colors = colors;
2016-08-07 18:22:27 +00:00
lg_kbd.attach(lg_pid);
lg_kbd.setKey(keyValue);
if (commit == true) lg_kbd.commit();
lg_kbd.detach();
2016-06-12 20:16:07 +00:00
return 0;
}
}
return 1;
}
int setAllKeys(string color, bool commit) {
2016-08-07 18:22:27 +00:00
Keyboard lg_kbd;
2016-06-12 20:16:07 +00:00
Keyboard::KeyColors colors;
2016-08-07 18:22:27 +00:00
if (lg_kbd.parseColor(color, colors) == true) {
lg_kbd.attach(lg_pid);
lg_kbd.setAllKeys(colors);
if (commit == true) lg_kbd.commit();
lg_kbd.detach();
2016-06-12 20:16:07 +00:00
return 0;
}
return 1;
}
int setGroupKeys(string groupKeys, string color, bool commit) {
2016-08-07 18:22:27 +00:00
Keyboard lg_kbd;
2016-06-12 20:16:07 +00:00
Keyboard::KeyGroup keyGroup;
2016-08-07 18:22:27 +00:00
if (lg_kbd.parseKeyGroup(groupKeys, keyGroup) == true) {
2016-06-12 20:16:07 +00:00
Keyboard::KeyColors colors;
2016-08-07 18:22:27 +00:00
if (lg_kbd.parseColor(color, colors) == true) {
lg_kbd.attach(lg_pid);
lg_kbd.setGroupKeys(keyGroup, colors);
if (commit == true) lg_kbd.commit();
lg_kbd.detach();
2016-06-12 20:16:07 +00:00
return 0;
}
}
return 1;
}
int loadProfile(string profileFile) {
ifstream file;
file.open(profileFile);
if (file.is_open()) {
string line;
int lineCount = 1;
int ind;
2016-08-07 18:22:27 +00:00
Keyboard lg_kbd;
2016-06-12 20:16:07 +00:00
Keyboard::KeyGroup keyGroup;
Keyboard::KeyAddress keyAddress;
Keyboard::KeyValue keyValue;
Keyboard::KeyColors colors;
map<string, string> var;
vector<Keyboard::KeyValue> keys;
2016-08-07 18:22:27 +00:00
lg_kbd.attach(lg_pid);
2016-06-12 20:16:07 +00:00
while (!file.eof()) {
getline(file, line);
if (line.substr(0, 3) == "var") {
line = line.substr(4);
ind = line.find(" ");
var[line.substr(0, ind)] = line.substr(ind + 1, 6);
} else if (line.substr(0, 1) == "a") {
line = line.substr(2);
if (line.substr(0, 1) == "$") {
ind = line.find(" ");
line = var[line.substr(1, ind - 1)];
} else line = line.substr(0, 6);
2016-08-07 18:22:27 +00:00
if (lg_kbd.parseColor(line, colors) == true) {
2016-06-12 20:16:07 +00:00
keys.clear();
2016-08-07 18:22:27 +00:00
lg_kbd.setAllKeys(colors);
2016-06-12 20:16:07 +00:00
} else cout<<"Error on line "<<lineCount<<" : "<<line<<"\n";
} else if (line.substr(0,1) == "g") {
line = line.substr(2);
ind = line.find(" ");
2016-08-07 18:22:27 +00:00
if (lg_kbd.parseKeyGroup(line.substr(0, ind), keyGroup) == true) {
2016-06-12 20:16:07 +00:00
line = line.substr(ind + 1);
if (line.substr(0, 1) == "$") {
ind = line.find(" ");
line = var[line.substr(1, ind - 1)];
};
2016-08-07 18:22:27 +00:00
if (lg_kbd.parseColor(line.substr(0, 6), colors) == true) {
lg_kbd.setGroupKeys(keyGroup, colors);
2016-06-12 20:16:07 +00:00
} else cout<<"Error on line "<<lineCount<<" : "<<line<<"\n";
} else cout<<"Error on line "<<lineCount<<" : "<<line<<"\n";
} else if (line.substr(0,1) == "k") {
line = line.substr(2);
ind = line.find(" ");
2016-08-07 18:22:27 +00:00
if (lg_kbd.parseKey(line.substr(0, ind), keyAddress) == true) {
2016-06-12 20:16:07 +00:00
line = line.substr(ind + 1);
if (line.substr(0, 1) == "$") {
ind = line.find(" ");
line = var[line.substr(1, ind - 1)];
}
2016-08-07 18:22:27 +00:00
if (lg_kbd.parseColor(line.substr(0, 6), colors) == true) {
2016-06-12 20:16:07 +00:00
keyValue.key = keyAddress;
keyValue.colors = colors;
keys.push_back(keyValue);
} else cout<<"Error on line "<<lineCount<<" : "<<line<<"\n";
} else cout<<"Error on line "<<lineCount<<" : "<<line<<"\n";
} else if (line.substr(0,1) == "c") {
2016-08-07 18:22:27 +00:00
lg_kbd.commit();
lg_kbd.setKeys(&keys[0], keys.size());
2016-06-12 20:16:07 +00:00
keys.clear();
2016-08-07 18:22:27 +00:00
lg_kbd.commit();
2016-06-12 20:16:07 +00:00
} else if ((line.substr(0, 1) != "#") && (line.substr(0, 1) != "")) {
cout<<"Error on line "<<lineCount<<" : "<<line<<"\n";
}
lineCount++;
}
2016-08-07 18:22:27 +00:00
lg_kbd.detach();
2016-06-12 20:16:07 +00:00
file.close();
return 0;
}
return 1;
}
int main(int argc, char *argv[]) {
2016-08-07 17:40:48 +00:00
string str = argv[0];
size_t split = str.find_last_of("/\\");
str = str.substr(split + 1);
if (str == "g410-led") lg_pid = 0xc330;
2016-06-12 20:16:07 +00:00
if (argc > 1) {
string argCmd = argv[1];
if (argCmd == "-h" || argCmd == "--help") { usage(); return 0; }
else if (argCmd == "-s" && argc == 3) return setStartupEffect(argv[2]);
else if (argCmd == "-a" && argc == 3) return setAllKeys(argv[2], true);
2016-08-16 22:15:02 +00:00
else if (argCmd == "-an" && argc == 3) return setAllKeys(argv[2], false);
2016-06-12 20:16:07 +00:00
else if (argCmd == "-g" && argc == 4) return setGroupKeys(argv[2], argv[3], true);
2016-08-16 22:15:02 +00:00
else if (argCmd == "-gn" && argc == 4) return setGroupKeys(argv[2], argv[3], false);
2016-06-12 20:16:07 +00:00
else if (argCmd == "-k" && argc == 4) return setKey(argv[2], argv[3], true);
2016-08-16 22:15:02 +00:00
else if (argCmd == "-kn" && argc == 4) return setKey(argv[2], argv[3], false);
2016-06-12 20:16:07 +00:00
else if (argCmd == "-c" && argc == 2) return commit();
else if (argCmd == "-p" && argc == 3) return loadProfile(argv[2]);
}
usage();
return 1;
}