1
0
mirror of https://github.com/MatMoul/g810-led.git synced 2024-12-22 17:06:10 +00:00

Full refactor code

This commit is contained in:
MatMoul 2017-01-21 09:15:18 +01:00
parent 2795151541
commit c2c8f6b7ac
22 changed files with 1254 additions and 1597 deletions

3
.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
g810-led.*
bin/*
.

View File

@ -14,10 +14,8 @@ ArchLinux :</br>
`sudo pacman -S git gcc make libusb`</br>
Debian :</br>
`sudo apt-get install git g++ make libusb-1.0-0-dev`</br>
OpenSUSE :</br>
`sudo zypper in git make gcc-c++ libstdc++-devel libusb-1_0-devel`</br>
CentOS :</br>
`sudo yum install git make gcc-c++ libstdc++-devel libusb-devel`</br>
Fedora :</br>
`sudo dnf install git make gcc-c++ libusb-devel`</br>
## Installation :</br>
`git clone https://github.com/MatMoul/g810-led.git`</br>

View File

@ -11,7 +11,7 @@ Linux LED controller for Logitech Led Keyboards.</br>
- **G910 Orion Spectrum**</br>
##Install and use :</br>
* https://github.com/MatMoul/g810-led/blob/master/INSTALL.md
* INSTALL.md
##Help :</br>
`g410-led --help`</br>
@ -25,12 +25,12 @@ Linux LED controller for Logitech Led Keyboards.</br>
`g810-led -a 00ff00 # Set color of all keys`</br>
`g810-led -g fkeys ff00ff # Set color of a group of keys`</br>
`g810-led -s color # Set keyboard power on effect`</br>
`g810-led -fx-color 00ff00 # Set fixed color effect`</br>
`g810-led -fx-breathing 00ff00 0a # Set breathing effect`</br>
`g810-led -fx-cycle 0a # Set color cycle effect`</br>
`g810-led -fx-hwave 0a # Set horizontal wave effect`</br>
`g810-led -fx-vwave 0a # Set vertical wave effect`</br>
`g810-led -fx-cwave 0a # Set center wave effect`</br>
`g810-led -fx color keys 00ff00 # Set fixed color effect`</br>
`g810-led -fx breathing logo 00ff00 0a # Set breathing effect`</br>
`g810-led -fx cycle all 0a # Set color cycle effect`</br>
`g810-led -fx hwave keys 0a # Set horizontal wave effect`</br>
`g810-led -fx vwave keys 0a # Set vertical wave effect`</br>
`g810-led -fx cwave keys 0a # Set center wave effect`</br>
##Samples with no commit :</br>
`g810-led -an 000000 # Set color of all key with no action`</br>

View File

@ -1,24 +1,23 @@
CC=g++
CFLAGS=-Wall -O2 -std=gnu++11
LIBUSB_INC?=-I/usr/include/libusb-1.0
LDFLAGS=-lusb-1.0
PROGN=g810-led
SYSTEMDDIR?=/usr/lib/systemd
.PHONY: all debug clean install uninstall
.PHONY: all debug clean
all: bin/$(PROGN)
bin/$(PROGN): src/main.cpp src/classes/*.cpp src/classes/*.h
bin/$(PROGN): src/main.cpp src/helpers/*.cpp src/helpers/*.h src/classes/*.cpp src/classes/*.h
@mkdir -p bin
$(CC) $(CFLAGS) $(LIBUSB_INC) -o $@ $^ $(LDFLAGS)
$(CC) $(CFLAGS) $^ -o $@ $(LDFLAGS)
debug: CFLAGS += -g -Wextra -pedantic
debug: bin/$(PROGN)
clean:
@rm -rf bin
install:
@install -m 755 -d \
$(DESTDIR)/etc/$(PROGN)/samples \

Binary file not shown.

Before

Width:  |  Height:  |  Size: 58 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 56 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 42 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 21 KiB

View File

@ -2,4 +2,4 @@ a 0000ff # Set all keys blue
c # Commit changes
fxl-breathing ff0000 10 # Set breathing effect with red color and speed 10
fx breathing logo ff0000 10 # Set breathing effect with red color and speed 10

View File

@ -1,2 +1,2 @@
fx-breathing ff0000 10 # Set breathing effect with red color and speed 10
fx breathing all ff0000 20 # Set breathing effect with red color and speed 20

View File

@ -1,3 +1,3 @@
# Green Profile
fx-color 00ff00 # Set all keys green
fx color all 00ff00 # Set all keys green

View File

@ -1,2 +1,2 @@
fx-cwave 10 # Set center wave effect with speed 10
fx cwave all 20 # Set center wave effect with speed 20

2
sample_profiles/fx_cycle Normal file
View File

@ -0,0 +1,2 @@
fx cycle all 20 # Set color cycle effect with speed 20

View File

@ -1,2 +1,2 @@
fx-hwave 10 # Set horizontal wave effect with speed 10
fx hwave all 20 # Set horizontal wave effect with speed 20

View File

@ -1,2 +1,2 @@
fx-vwave 10 # Set vertical wave effect with speed 10
fx vwave all 20 # Set vertical wave effect with speed 20

File diff suppressed because it is too large Load Diff

View File

@ -1,81 +1,195 @@
#ifndef DEF_KEYBOARD
#define DEF_KEYBOARD
#ifndef KEYBOARD_CLASS
#define KEYBOARD_CLASS
#include <iostream>
#include <libusb.h>
#include <vector>
class Keyboard {
#include "libusb-1.0/libusb.h"
class LedKeyboard {
private:
const std::vector<std::vector<uint16_t>> SuportedKeyboards = {
{ 0x46d, 0xc330, (u_int16_t)KeyboardModel::g410 },
{ 0x46d, 0xc333, (u_int16_t)KeyboardModel::g610 },
{ 0x46d, 0xc338, (u_int16_t)KeyboardModel::g610 },
{ 0x46d, 0xc331, (u_int16_t)KeyboardModel::g810 },
{ 0x46d, 0xc337, (u_int16_t)KeyboardModel::g810 },
{ 0x46d, 0xc32b, (u_int16_t)KeyboardModel::g910 },
{ 0x46d, 0xc335, (u_int16_t)KeyboardModel::g910 }
};
enum class KeyAddressGroup : uint8_t {
logo = 0x00,
indicators,
multimedia,
gkeys,
keys
};
public:
enum class KeyboardProtocol { generic, g910 };
enum class PowerOnEffect { rainbow, color };
enum class KeyAddressGroup { logo, indicators, multimedia, keys, gkeys };
enum class Key { // 127 items
logo, logo2,
caps, num, scroll, game, backlight,
mute, play, stop, prev, next,
f1, f2, f3, f4, f5, f6, f7, f8, f9, f10, f11, f12,
shift_left, ctrl_left, win_left, alt_left, alt_right, win_right, ctrl_right, shift_right, menu,
arrow_top, arrow_left, arrow_bottom, arrow_right,
num_1, num_2, num_3, num_4, num_5, num_6, num_7, num_8, num_9, num_0, num_dot, num_enter, num_plus, num_minus, num_asterisk, num_slash, num_lock,
esc, scroll_lock,
insert, del, home, end, page_up, page_down, print_screen, pause_break,
n1, n2, n3, n4, n5, n6, n7, n8, n9, n0,
tab, caps_lock, space, backspace, enter,
a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z,
tilde, minus, equal,
open_bracket, close_bracket, backslash,
semicolon, quote, dollar,
intl_backslash, comma, period, slash,
g1, g2, g3, g4, g5, g6, g7, g8, g9
enum class KeyboardModel : uint8_t {
unknown = 0x00,
g410,
g610,
g810,
g910
};
enum class StartupMode : uint8_t {
wave = 0x01,
color
};
enum class NativeEffect : uint8_t {
color = 0x01,
breathing,
cycle,
hwave,
vwave,
cwave
};
enum class NativeEffectPart : uint8_t {
all = 0xff,
keys = 0x00,
logo
};
enum class KeyGroup : uint8_t {
logo = 0x00,
indicators,
multimedia,
gkeys,
fkeys,
modifiers,
functions,
arrows,
numeric,
keys
};
enum class Key : uint16_t { // 127 items
logo = static_cast<uint8_t>(KeyAddressGroup::logo) << 8 | 0x01,
logo2,
backlight = static_cast<uint8_t>(KeyAddressGroup::indicators) << 8| 0x01,
game, caps, scroll, num,
next = static_cast<uint8_t>(KeyAddressGroup::multimedia) << 8 | 0xb5,
prev, stop,
play = static_cast<uint8_t>(KeyAddressGroup::multimedia) << 8 | 0xcd,
mute = static_cast<uint8_t>(KeyAddressGroup::multimedia) << 8 | 0xe2,
g1 = static_cast<uint8_t>(KeyAddressGroup::gkeys) << 8 | 0x01,
g2, g3, g4, g5, g6, g7, g8, g9,
a = static_cast<uint8_t>(KeyAddressGroup::keys) << 8 | 0x04,
b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z,
n1, n2, n3, n4, n5, n6, n7, n8, n9, n0,
enter, esc, backspace, tab, space, minus, equal, open_bracket, close_bracket,
backslash, dollar, semicolon, quote, tilde, comma, period, slash, caps_lock,
f1, f2, f3, f4, f5, f6, f7, f8, f9, f10, f11, f12,
print_screen, scroll_lock, pause_break, insert, home, page_up, del, end, page_down,
arrow_right, arrow_left, arrow_bottom, arrow_top, num_lock, num_slash, num_asterisk,
num_minus, num_plus, num_enter,
num_1, num_2, num_3, num_4, num_5, num_6, num_7, num_8, num_9, num_0,
num_dot, intl_backslash, menu,
ctrl_left = static_cast<uint8_t>(KeyAddressGroup::keys) << 8 | 0xe0,
shift_left, alt_left, win_left,
ctrl_right, shift_right, alt_right, win_right
};
enum class KeyGroup { logo, indicators, multimedia, fkeys, modifiers, arrows, numeric, functions, keys, gkeys};
struct KeyColors { uint8_t red; uint8_t green; uint8_t blue; };
struct KeyAddress { KeyAddressGroup addressGroup; uint8_t id; };
struct KeyValue { KeyAddress key; KeyColors colors; };
bool isAttached();
bool attach();
bool detach();
struct Color {
uint8_t red;
uint8_t green;
uint8_t blue;
};
struct KeyValue {
LedKeyboard::Key key;
LedKeyboard::Color color;
};
typedef std::vector<KeyValue> KeyValueArray;
~LedKeyboard();
bool listKeyboards();
bool isOpen();
bool open();
bool close();
KeyboardModel getKeyboardModel();
bool commit();
bool getKeyAddress(Key key, KeyAddress &keyAddress);
bool parsePowerOnEffect(std::string effect, PowerOnEffect &powerOnEffect);
bool parseKey(std::string key, KeyAddress &keyAddress);
bool parseKeyGroup(std::string key, KeyGroup &keyGroup);
bool parseColor(std::string color, KeyColors &colors);
bool parseSpeed(std::string speed, uint8_t &speedValue);
bool setPowerOnEffect(PowerOnEffect powerOnEffect);
bool setKey(KeyValue keyValue);
bool setKey(Key key, KeyColors colors);
bool setKeys(KeyValue keyValue[], size_t keyValueCount);
bool setAllKeys(KeyColors colors);
bool setGroupKeys(KeyGroup keyGroup, KeyColors colors);
bool setFXColorKeys(KeyColors colors);
bool setFXColorLogo(KeyColors colors);
bool setFXBreathingKeys(KeyColors colors, uint8_t speed);
bool setFXBreathingLogo(KeyColors colors, uint8_t speed);
bool setFXColorCycleKeys(uint8_t speed);
bool setFXColorCycleLogo(uint8_t speed);
bool setFXHWaveKeys(uint8_t speed);
bool setFXVWaveKeys(uint8_t speed);
bool setFXCWaveKeys(uint8_t speed);
bool setKeys(KeyValueArray keyValues);
bool setGroupKeys(KeyGroup keyGroup, Color color);
bool setAllKeys(Color color);
bool setStartupMode(StartupMode startupMode);
bool setNativeEffect(NativeEffect effect, NativeEffectPart part, uint8_t speed, Color color);
private:
bool m_isAttached = false;
bool m_isKernellDetached = false;
KeyboardProtocol kbdProtocol = KeyboardProtocol::generic;
libusb_device **devs;
libusb_device_handle *dev_handle;
libusb_context *ctx = NULL;
typedef std::vector<unsigned char> byte_buffer_t;
typedef std::vector<Key> KeyArray;
bool populateAddressGroupInternal(KeyAddressGroup addressGroup, unsigned char *data);
bool populateFXAddressInternal(unsigned char *data);
bool sendDataInternal(unsigned char *data, uint16_t data_size);
bool setKeysInternal(KeyAddressGroup addressGroup, KeyValue keyValues[], size_t keyValueCount);
const KeyArray keyGroupLogo = { Key::logo, Key::logo2 };
const KeyArray keyGroupIndicators = { Key::caps, Key::num, Key::scroll, Key::game, Key::backlight };
const KeyArray keyGroupMultimedia = { Key::next, Key::prev, Key::stop, Key::play, Key::mute };
const KeyArray keyGroupGKeys = { Key::g1, Key::g2, Key::g3, Key::g4, Key::g5, Key::g6, Key::g7, Key::g8, Key::g9 };
const KeyArray keyGroupFKeys = {
Key::f1, Key::f2, Key::f3, Key::f4, Key::f5, Key::f6,
Key::f7, Key::f8, Key::f9, Key::f10, Key::f11, Key::f12
};
const KeyArray keyGroupModifiers = {
Key::shift_left, Key::ctrl_left, Key::win_left, Key::alt_left,
Key::alt_right, Key::win_right, Key::ctrl_right, Key::shift_right, Key::menu };
const KeyArray keyGroupFunctions = {
Key::esc, Key::print_screen, Key::scroll_lock, Key::pause_break,
Key::insert, Key::del, Key::home, Key::end, Key::page_up, Key::page_down
};
const KeyArray keyGroupArrows = { Key::arrow_top, Key::arrow_left, Key::arrow_bottom, Key::arrow_right };
const KeyArray keyGroupNumeric = {
Key::num_1, Key::num_2, Key::num_3, Key::num_4, Key::num_5,
Key::num_6, Key::num_7, Key::num_8, Key::num_9, Key::num_0,
Key::num_dot, Key::num_enter, Key::num_plus, Key::num_minus,
Key::num_asterisk, Key::num_slash, Key::num_lock
};
const KeyArray keyGroupKeys = {
Key::a, Key::b, Key::c, Key::d, Key::e, Key::f, Key::g, Key::h, Key::i, Key::j, Key::k, Key::l, Key::m,
Key::n, Key::o, Key::p, Key::q, Key::r, Key::s, Key::t, Key::u, Key::v, Key::w, Key::x, Key::y, Key::z,
Key::n1, Key::n2, Key::n3, Key::n4, Key::n5, Key::n6, Key::n7, Key::n8, Key::n9, Key::n0,
Key::enter, Key::esc, Key::backspace, Key::tab, Key::space, Key::minus, Key::equal,
Key::open_bracket, Key::close_bracket, Key::backslash, Key::dollar, Key::semicolon, Key::quote, Key::tilde,
Key::comma, Key::period, Key::slash, Key::caps_lock, Key::intl_backslash
};
bool m_isOpen = false;
bool m_isKernellDetached = false;
uint16_t m_vendorID = 0;
uint16_t m_productID = 0;
KeyboardModel m_keyboardModel = KeyboardModel::unknown;
libusb_device_handle *m_hidHandle;
libusb_context *m_ctx = NULL;
bool sendDataInternal(const byte_buffer_t &data);
byte_buffer_t getKeyGroupAddress(KeyAddressGroup keyAddressGroup);
};

187
src/helpers/help.cpp Normal file
View File

@ -0,0 +1,187 @@
#include "help.h"
#include <iostream>
#include "utils.h"
using namespace std;
namespace help {
void usage(char *arg0) {
string cmdName = utils::getCmdName(arg0);
cout<<cmdName<<endl;
cout<<"--------"<<endl;
cout<<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;
cout<<endl;
cout<<" -an {color}\t\t\t\tSet all keys color without commit"<<endl;
cout<<" -gn {keygroup} {color}\t\tSet key group color without commit"<<endl;
cout<<" -kn {key} {color}\t\t\tSet key color without commit"<<endl;
cout<<" -c\t\t\t\t\tCommit change"<<endl;
cout<<endl;
cout<<" -fx ...\t\t\t\tUse --help-effects for more detail"<<endl;
cout<<endl;
cout<<" < {profile}\t\t\t\tSet a profile from a file (use --help-samples for more detail)"<<endl;
cout<<" |\t\t\t\t\tSet a profile from stdin (for scripting) (use --help-samples for more detail)"<<endl;
cout<<endl;
cout<<" --startup-mode {startup mode}\t\tSet startup mode"<<endl;
cout<<endl;
cout<<" --list-keyboards \t\t\tList connected keyboards"<<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;
if (cmdName == "g610-led")
cout<<"color formats :\t\t\t\tII (hex value for intensity)"<<endl;
else
cout<<"color formats :\t\t\t\tRRGGBB (hex value for red, green and blue)"<<endl;
cout<<"speed formats :\t\t\t\tSS (hex value for speed 01 to ff)"<<endl;
cout<<""<<endl;
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;
}
void keys(char *arg0) {
string cmdName = utils::getCmdName(arg0);
cout<<cmdName<<" Keys"<<endl;
cout<<"-------------"<<endl;
cout<<endl;
cout<<"Group List :"<<endl;
cout<<" logo"<<endl;
cout<<" indicators"<<endl;
cout<<" gkeys"<<endl;
cout<<" fkeys"<<endl;
cout<<" modifiers"<<endl;
cout<<" multimedia"<<endl;
cout<<" arrows"<<endl;
cout<<" numeric"<<endl;
cout<<" functions"<<endl;
cout<<" keys"<<endl;
cout<<endl;
cout<<endl;
cout<<"Group logo :"<<endl;
cout<<" logo"<<endl;
cout<<" logo2"<<endl;
cout<<""<<endl;
cout<<"Group indicators :"<<endl;
cout<<" num_indicator, numindicator, num"<<endl;
cout<<" caps_indicator, capsindicator, caps"<<endl;
cout<<" scroll_indicator, scrollindicator, scroll"<<endl;
cout<<" game_mode, gamemode, game"<<endl;
cout<<" back_light, backlight, light"<<endl;
cout<<""<<endl;
cout<<"Group gkeys :"<<endl;
cout<<" g1 - g9"<<endl;
cout<<""<<endl;
cout<<"Group fkeys :"<<endl;
cout<<" f1 - f12"<<endl;
cout<<""<<endl;
cout<<"Group modifiers :"<<endl;
cout<<" shift_left, shiftleft, shiftl"<<endl;
cout<<" ctrl_left, ctrlleft, ctrll"<<endl;
cout<<" win_left, winleft, win_left"<<endl;
cout<<" alt_left, altleft, altl"<<endl;
cout<<" alt_right, altright, altr, altgr"<<endl;
cout<<" win_right, winright, winr"<<endl;
cout<<" menu"<<endl;
cout<<" ctrl_right, ctrlright, ctrlr"<<endl;
cout<<" shift_right, shiftright, shiftr"<<endl;
cout<<""<<endl;
cout<<"Group multimedia :"<<endl;
cout<<" mute"<<endl;
cout<<" play_pause, playpause, play"<<endl;
cout<<" stop"<<endl;
cout<<" previous, prev"<<endl;
cout<<" next"<<endl;
cout<<""<<endl;
cout<<"Group arrows :"<<endl;
cout<<" arrow_top, arrowtop, top"<<endl;
cout<<" arrow_left, arrowleft, left"<<endl;
cout<<" arrow_bottom, arrowbottom, bottom"<<endl;
cout<<" arrow_right, arrowright, right"<<endl;
cout<<""<<endl;
cout<<"Group numeric :"<<endl;
cout<<" num_lock, numlock"<<endl;
cout<<" num_slash, numslash, num/"<<endl;
cout<<" num_asterisk, numasterisk, num*"<<endl;
cout<<" num_minus, numminus, num-"<<endl;
cout<<" num_plus, numplus, num+"<<endl;
cout<<" numenter"<<endl;
cout<<" num0 - num9"<<endl;
cout<<" num_dot, numdot, num."<<endl;
cout<<""<<endl;
cout<<"Group functions :"<<endl;
cout<<" escape, esc"<<endl;
cout<<" print_screen, printscreen, printscr"<<endl;
cout<<" scroll_lock, scrolllock"<<endl;
cout<<" pause_break, pausebreak"<<endl;
cout<<" insert, ins"<<endl;
cout<<" home"<<endl;
cout<<" page_up, pageup"<<endl;
cout<<" delete, del"<<endl;
cout<<" end"<<endl;
cout<<" page_down, pagedown"<<endl;
cout<<""<<endl;
cout<<"Group keys :"<<endl;
cout<<" 0 - 9"<<endl;
cout<<" a - z"<<endl;
cout<<" tab"<<endl;
cout<<" caps_lock, capslock"<<endl;
cout<<" space"<<endl;
cout<<" backspace, back"<<endl;
cout<<" enter"<<endl;
cout<<" tilde"<<endl;
cout<<" minus"<<endl;
cout<<" equal"<<endl;
cout<<" open_bracket"<<endl;
cout<<" close_bracket"<<endl;
cout<<" backslash"<<endl;
cout<<" semicolon"<<endl;
cout<<" dollar"<<endl;
cout<<" quote"<<endl;
cout<<" intl_backslash"<<endl;
cout<<" comma"<<endl;
cout<<" period"<<endl;
cout<<" slash"<<endl;
}
void effects(char *arg0) {
string cmdName = utils::getCmdName(arg0);
cout<<cmdName<<" Effects"<<endl;
cout<<"----------------"<<endl;
cout<<endl;
cout<<" -fx {effect} {target}"<<endl;
cout<<endl;
cout<<" -fx color {target} {color}"<<endl;
cout<<" -fx breathing {target} {color} {speed}"<<endl;
cout<<" -fx cycle {target} {speed}"<<endl;
cout<<" -fx hwave {target} {speed}"<<endl;
cout<<" -fx vwave {target} {speed}"<<endl;
cout<<" -fx cwave {target} {speed}"<<endl;
cout<<endl;
cout<<"target value :\t\t\t\tall, keys, logo"<<endl;
cout<<"color formats :\t\t\t\tRRGGBB (hex value for red, green and blue)"<<endl;
cout<<"speed formats :\t\t\t\tSS (hex value for speed 01 to ff)"<<endl;
cout<<endl;
}
void samples(char *arg0) {
string cmdName = utils::getCmdName(arg0);
cout<<cmdName<<" Samples"<<endl;
cout<<"----------------"<<endl;
cout<<endl;
cout<<cmdName<<" -k logo ff0000"<<endl;
cout<<cmdName<<" -a 00ff00"<<endl;
cout<<cmdName<<" -g fkeys ff00ff"<<endl;
}
}

15
src/helpers/help.h Normal file
View File

@ -0,0 +1,15 @@
#ifndef HELP_HELPER
#define HELP_HELPER
#include <iostream>
namespace help {
void usage(char *arg0);
void keys(char *arg0);
void effects(char *arg0);
void samples(char *arg0);
}
#endif

212
src/helpers/utils.cpp Normal file
View File

@ -0,0 +1,212 @@
#include "utils.h"
#include <iostream>
#include <algorithm>
#include "../classes/Keyboard.h"
namespace utils {
std::string getCmdName(std::string cmd) {
return cmd.substr(cmd.find_last_of("/\\") + 1);
}
bool parseStartupMode(std::string val, LedKeyboard::StartupMode &startupMode) {
if (val == "wave") startupMode = LedKeyboard::StartupMode::wave;
else if (val == "color") startupMode = LedKeyboard::StartupMode::color;
else return false;
return true;
}
bool parseNativeEffect(std::string val, LedKeyboard::NativeEffect &nativeEffect) {
if (val == "color") nativeEffect = LedKeyboard::NativeEffect::color;
else if (val == "cycle") nativeEffect = LedKeyboard::NativeEffect::cycle;
else if (val == "breathing") nativeEffect = LedKeyboard::NativeEffect::breathing;
else if (val == "hwave") nativeEffect = LedKeyboard::NativeEffect::hwave;
else if (val == "vwave") nativeEffect = LedKeyboard::NativeEffect::vwave;
else if (val == "cwave") nativeEffect = LedKeyboard::NativeEffect::cwave;
else return false;
return true;
}
bool parseNativeEffectPart(std::string val, LedKeyboard::NativeEffectPart &nativeEffectPart) {
if (val == "all") nativeEffectPart = LedKeyboard::NativeEffectPart::all;
else if (val == "keys") nativeEffectPart = LedKeyboard::NativeEffectPart::keys;
else if (val == "logo") nativeEffectPart = LedKeyboard::NativeEffectPart::logo;
else return false;
return true;
}
bool parseKey(std::string val, LedKeyboard::Key &key) {
std::transform(val.begin(), val.end(), val.begin(), ::tolower);
if (val == "logo") key = LedKeyboard::Key::logo;
else if (val == "logo2") key = LedKeyboard::Key::logo2;
else if (val == "back_light" || val == "backlight" || val == "light") key = LedKeyboard::Key::backlight;
else if (val == "game_mode" || val == "gamemode" || val == "game") key = LedKeyboard::Key::game;
else if (val == "caps_indicator" || val == "capsindicator" || val == "caps") key = LedKeyboard::Key::caps;
else if (val == "scroll_indicator" || val == "scrollindicator" || val == "scroll") key = LedKeyboard::Key::scroll;
else if (val == "num_indicator" || val == "numindicator" || val == "num") key = LedKeyboard::Key::num;
else if (val == "next") key = LedKeyboard::Key::next;
else if (val == "prev" || val == "previous") key = LedKeyboard::Key::prev;
else if (val == "stop") key = LedKeyboard::Key::stop;
else if (val == "play_pause" || val == "playpause" || val == "play") key = LedKeyboard::Key::play;
else if (val == "mute") key = LedKeyboard::Key::mute;
else if (val == "a") key = LedKeyboard::Key::a;
else if (val == "b") key = LedKeyboard::Key::b;
else if (val == "c") key = LedKeyboard::Key::c;
else if (val == "d") key = LedKeyboard::Key::d;
else if (val == "e") key = LedKeyboard::Key::e;
else if (val == "f") key = LedKeyboard::Key::f;
else if (val == "g") key = LedKeyboard::Key::g;
else if (val == "h") key = LedKeyboard::Key::h;
else if (val == "i") key = LedKeyboard::Key::i;
else if (val == "j") key = LedKeyboard::Key::j;
else if (val == "k") key = LedKeyboard::Key::k;
else if (val == "l") key = LedKeyboard::Key::l;
else if (val == "m") key = LedKeyboard::Key::m;
else if (val == "n") key = LedKeyboard::Key::n;
else if (val == "o") key = LedKeyboard::Key::o;
else if (val == "p") key = LedKeyboard::Key::p;
else if (val == "q") key = LedKeyboard::Key::q;
else if (val == "r") key = LedKeyboard::Key::r;
else if (val == "s") key = LedKeyboard::Key::s;
else if (val == "t") key = LedKeyboard::Key::t;
else if (val == "u") key = LedKeyboard::Key::u;
else if (val == "v") key = LedKeyboard::Key::v;
else if (val == "w") key = LedKeyboard::Key::w;
else if (val == "x") key = LedKeyboard::Key::x;
else if (val == "z") key = LedKeyboard::Key::z;
else if (val == "y") key = LedKeyboard::Key::y;
else if (val == "1" || val == "one") key = LedKeyboard::Key::n1;
else if (val == "2" || val == "two") key = LedKeyboard::Key::n2;
else if (val == "3" || val == "three") key = LedKeyboard::Key::n3;
else if (val == "4" || val == "four") key = LedKeyboard::Key::n4;
else if (val == "5" || val == "five") key = LedKeyboard::Key::n5;
else if (val == "6" || val == "six") key = LedKeyboard::Key::n6;
else if (val == "7" || val == "seven") key = LedKeyboard::Key::n7;
else if (val == "8" || val == "eight") key = LedKeyboard::Key::n8;
else if (val == "9" || val == "nine") key = LedKeyboard::Key::n9;
else if (val == "0" || val == "zero") key = LedKeyboard::Key::n0;
else if (val == "enter") key = LedKeyboard::Key::enter;
else if (val == "esc" || val == "escape") key = LedKeyboard::Key::esc;
else if (val == "back" || val == "backspace") key = LedKeyboard::Key::backspace;
else if (val == "tab") key = LedKeyboard::Key::tab;
else if (val == "space") key = LedKeyboard::Key::space;
else if (val == "tilde" || val == "~") key = LedKeyboard::Key::tilde;
else if (val == "minus" || val == "-") key = LedKeyboard::Key::minus;
else if (val == "equal" || val == "=") key = LedKeyboard::Key::equal;
else if (val == "open_bracket" || val == "[") key = LedKeyboard::Key::open_bracket;
else if (val == "close_bracket" || val == "]") key = LedKeyboard::Key::close_bracket;
else if (val == "backslash" || val == "\\") key = LedKeyboard::Key::backslash;
else if (val == "semicolon" || val == ";") key = LedKeyboard::Key::semicolon;
else if (val == "quote" || val == "\"") key = LedKeyboard::Key::quote;
else if (val == "dollar" || val == "$") key = LedKeyboard::Key::dollar;
else if (val == "comma" || val == ",") key = LedKeyboard::Key::comma;
else if (val == "period" || val == ".") key = LedKeyboard::Key::period;
else if (val == "slash" || val == "/") key = LedKeyboard::Key::slash;
else if (val == "caps_lock" || val == "capslock") key = LedKeyboard::Key::caps_lock;
else if (val == "f1") key = LedKeyboard::Key::f1;
else if (val == "f2") key = LedKeyboard::Key::f2;
else if (val == "f3") key = LedKeyboard::Key::f3;
else if (val == "f4") key = LedKeyboard::Key::f4;
else if (val == "f5") key = LedKeyboard::Key::f5;
else if (val == "f6") key = LedKeyboard::Key::f6;
else if (val == "f7") key = LedKeyboard::Key::f7;
else if (val == "f8") key = LedKeyboard::Key::f8;
else if (val == "f9") key = LedKeyboard::Key::f9;
else if (val == "f10") key = LedKeyboard::Key::f10;
else if (val == "f11") key = LedKeyboard::Key::f11;
else if (val == "f12") key = LedKeyboard::Key::f12;
else if (val == "print_screen" || val == "printscreen" || val == "printscr" || val == "print")
key = LedKeyboard::Key::print_screen;
else if (val == "scroll_lock" || val == "scrolllock") key = LedKeyboard::Key::scroll_lock;
else if (val == "pause_break" || val == "pausebreak" || val == "pause" || val == "break")
key = LedKeyboard::Key::pause_break;
else if (val == "insert" || val == "ins") key = LedKeyboard::Key::insert;
else if (val == "home") key = LedKeyboard::Key::home;
else if (val == "page_up" || val == "pageup") key = LedKeyboard::Key::page_up;
else if (val == "delete" || val == "del") key = LedKeyboard::Key::del;
else if (val == "end") key = LedKeyboard::Key::end;
else if (val == "page_down" || val == "pagedown") key = LedKeyboard::Key::page_down;
else if (val == "arrow_right" || val == "arrowright" || val == "right") key = LedKeyboard::Key::arrow_right;
else if (val == "arrow_left" || val == "arrowleft" || val == "left") key = LedKeyboard::Key::arrow_left;
else if (val == "arrow_bottom" || val == "arrowbottom" || val == "bottom") key = LedKeyboard::Key::arrow_bottom;
else if (val == "arrow_top" || val == "arrowtop" || val == "top") key = LedKeyboard::Key::arrow_top;
else if (val == "num_lock" || val == "numlock") key = LedKeyboard::Key::num_lock;
else if (val == "num/" || val == "num_slash" || val == "numslash") key = LedKeyboard::Key::num_slash;
else if (val == "num*" || val == "num_asterisk" || val == "numasterisk") key = LedKeyboard::Key::num_asterisk;
else if (val == "num-" || val == "num_minus" || val == "numminus") key = LedKeyboard::Key::num_minus;
else if (val == "num+" || val == "num_plus" || val == "numplus") key = LedKeyboard::Key::num_plus;
else if (val == "numenter") key = LedKeyboard::Key::num_enter;
else if (val == "num1") key = LedKeyboard::Key::num_1;
else if (val == "num2") key = LedKeyboard::Key::num_2;
else if (val == "num3") key = LedKeyboard::Key::num_3;
else if (val == "num4") key = LedKeyboard::Key::num_4;
else if (val == "num5") key = LedKeyboard::Key::num_5;
else if (val == "num6") key = LedKeyboard::Key::num_6;
else if (val == "num7") key = LedKeyboard::Key::num_7;
else if (val == "num8") key = LedKeyboard::Key::num_8;
else if (val == "num9") key = LedKeyboard::Key::num_9;
else if (val == "num0") key = LedKeyboard::Key::num_0;
else if (val == "num." || val == "num_period" || val == "numperiod") key = LedKeyboard::Key::num_dot;
else if (val == "intl_backslash" || val == "<") key = LedKeyboard::Key::intl_backslash;
else if (val == "menu") key = LedKeyboard::Key::menu;
else if (val == "ctrl_left" || val == "ctrlleft" || val == "ctrll") key = LedKeyboard::Key::ctrl_left;
else if (val == "shift_left" || val == "shiftleft" || val == "shiftl") key = LedKeyboard::Key::shift_left;
else if (val == "alt_left" || val == "altleft" || val == "altl") key = LedKeyboard::Key::alt_left;
else if (val == "win_left" || val == "winleft" || val == "winl") key = LedKeyboard::Key::win_left;
else if (val == "meta_left" || val == "metaleft" || val == "metal") key = LedKeyboard::Key::win_left;
else if (val == "ctrl_right" || val == "ctrlright" || val == "ctrlr") key = LedKeyboard::Key::ctrl_right;
else if (val == "shift_right" || val == "shiftright" || val == "shiftr") key = LedKeyboard::Key::shift_right;
else if (val == "alt_right" || val == "altright" || val == "altr" || val == "altgr")
key = LedKeyboard::Key::alt_right;
else if (val == "win_right" || val == "winright" || val == "winr") key = LedKeyboard::Key::win_right;
else if (val == "meta_right" || val == "metaright" || val == "metar") key = LedKeyboard::Key::win_right;
else if (val == "g1") key = LedKeyboard::Key::g1;
else if (val == "g2") key = LedKeyboard::Key::g2;
else if (val == "g3") key = LedKeyboard::Key::g3;
else if (val == "g4") key = LedKeyboard::Key::g4;
else if (val == "g5") key = LedKeyboard::Key::g5;
else if (val == "g6") key = LedKeyboard::Key::g6;
else if (val == "g7") key = LedKeyboard::Key::g7;
else if (val == "g8") key = LedKeyboard::Key::g8;
else if (val == "g9") key = LedKeyboard::Key::g9;
else return false;
return true;
}
bool parseKeyGroup(std::string val, LedKeyboard::KeyGroup &keyGroup) {
if (val == "logo") keyGroup = LedKeyboard::KeyGroup::logo;
else if (val == "indicators") keyGroup = LedKeyboard::KeyGroup::indicators;
else if (val == "multimedia") keyGroup = LedKeyboard::KeyGroup::multimedia;
else if (val == "fkeys") keyGroup = LedKeyboard::KeyGroup::fkeys;
else if (val == "modifiers") keyGroup = LedKeyboard::KeyGroup::modifiers;
else if (val == "arrows") keyGroup = LedKeyboard::KeyGroup::arrows;
else if (val == "numeric") keyGroup = LedKeyboard::KeyGroup::numeric;
else if (val == "functions") keyGroup = LedKeyboard::KeyGroup::functions;
else if (val == "keys") keyGroup = LedKeyboard::KeyGroup::keys;
else if (val == "gkeys") keyGroup = LedKeyboard::KeyGroup::gkeys;
else return false;
return true;
}
bool parseColor(std::string val, LedKeyboard::Color &color) {
if (val.length() == 2) val = val + "0000"; // For G610
if (val.length() != 6) return false;
color.red = std::stoul("0x"+val.substr(0,2), nullptr, 16);
color.green = std::stoul("0x"+val.substr(2,2), nullptr, 16);
color.blue = std::stoul("0x"+val.substr(4,2), nullptr, 16);
return true;
}
bool parseSpeed(std::string val, uint8_t &speed) {
if (val.length() == 1) val = "0" + val;
if (val.length() != 2) return false;
speed = std::stoul("0x" + val, nullptr, 16);
return true;
}
}

21
src/helpers/utils.h Normal file
View File

@ -0,0 +1,21 @@
#ifndef UTILS_HELPER
#define UTILS_HELPER
#include <iostream>
#include "../classes/Keyboard.h"
namespace utils {
std::string getCmdName(std::string cmd);
bool parseStartupMode(std::string val, LedKeyboard::StartupMode &startupMode);
bool parseNativeEffect(std::string val, LedKeyboard::NativeEffect &nativeEffect);
bool parseNativeEffectPart(std::string val, LedKeyboard::NativeEffectPart &nativeEffectPart);
bool parseKey(std::string val, LedKeyboard::Key &key);
bool parseKeyGroup(std::string val, LedKeyboard::KeyGroup &keyGroup);
bool parseColor(std::string val, LedKeyboard::Color &color);
bool parseSpeed(std::string val, uint8_t &speed);
}
#endif

View File

@ -1,511 +1,228 @@
#include <map>
#include <vector>
#include <fstream>
#include <istream>
#include <iostream>
#include <unistd.h>
#include <fstream>
#include <map>
#include "helpers/help.h"
#include "helpers/utils.h"
#include "classes/Keyboard.h"
using namespace std;
string appname = "g810-led";
void usage() {
cout<<appname<<" Usages :"<<endl;
cout<<"-----------------"<<endl;
cout<<""<<endl;
cout<<" -s effect :\t\t\tSet keyboard startup effect"<<endl;
cout<<""<<endl;
cout<<" -a color :\t\t\tSet all keys"<<endl;
cout<<" -g group, color :\t\tSet a group of keys"<<endl;
cout<<" -k key, color :\t\tSet a key"<<endl;
cout<<""<<endl;
cout<<" -an color :\t\t\tSet all keys without commit"<<endl;
cout<<" -gn group, color :\t\tSet a group of keys without commit"<<endl;
cout<<" -kn key, color :\t\tSet a key without commit"<<endl;
cout<<" -c :\t\t\t\tCommit changes"<<endl;
cout<<""<<endl;
cout<<" -fx-color color :\t\tSet static color effect"<<endl;
cout<<" -fx-breathing color, speed :\tSet breathing effect"<<endl;
cout<<" -fx-cycle speed :\t\tSet color cycle effect"<<endl;
cout<<" -fx-hwave speed :\t\tSet horizontal color wave effect"<<endl;
cout<<" -fx-vwave speed :\t\tSet vertical color wave effect"<<endl;
cout<<" -fx-cwave speed :\t\tSet center color wave effect"<<endl;
cout<<""<<endl;
cout<<" -fxk-color color :\t\tSet static color effect (keys only)"<<endl;
cout<<" -fxk-breathing color, speed :\tSet breathing effect (keys only)"<<endl;
cout<<" -fxk-cycle speed :\t\tSet color cycle effect (keys only)"<<endl;
cout<<" -fxk-hwave speed :\t\tSet horizontal color wave effect (keys only)"<<endl;
cout<<" -fxk-vwave speed :\t\tSet vertical color wave effect (keys only)"<<endl;
cout<<" -fxk-cwave speed :\t\tSet center color wave effect (keys only)"<<endl;
cout<<""<<endl;
cout<<" -fxl-color color :\t\tSet static color effect (logo only)"<<endl;
cout<<" -fxl-breathing color, speed :\tSet breathing effect (logo only)"<<endl;
cout<<" -fxl-cycle speed :\t\tSet color cycle effect (logo only)"<<endl;
cout<<""<<endl;
cout<<" -p profilefile :\t\tLoad a profile from a file"<<endl;
cout<<" -pp profilepipe :\t\tLoad a profile from stdin"<<endl;
cout<<""<<endl;
cout<<" -h | --help :\t\t\tthis help message"<<endl;
cout<<" -lk | --list-keys :\t\tList keys in groups"<<endl;
cout<<""<<endl;
cout<<"color formats :\t\t\tRRGGBB (hex value for red, green and blue)"<<endl;
cout<<"speed formats :\t\t\tSS (hex value for speed)"<<endl;
cout<<""<<endl;
cout<<"effect values :\t\t\trainbow, color"<<endl;
cout<<"key values :\t\t\tabc... 123... and other (use -lk for more detail)"<<endl;
cout<<"group values :\t\t\tlogo, indicators, fkeys, modifiers, multimedia, arrows, numeric, functions, keys, gkeys"<<endl;
cout<<""<<endl;
cout<<"samples :"<<endl;
cout<<appname<<" -k logo ff0000"<<endl;
cout<<appname<<" -a 00ff00"<<endl;
cout<<appname<<" -g fkeys ff00ff"<<endl;
cout<<appname<<" -s color"<<endl;
cout<<appname<<" -fx-cycle 1f"<<endl;
cout<<appname<<" -p profilefile"<<endl;
cout<<""<<endl;
cout<<"samples with pipe :"<<endl;
cout<<appname<<" -pp < profilefile"<<endl;
cout<<"echo -e \"k w ff0000\\nk a ff0000\\nk s ff0000\\nk d ff0000\\nc\" | "<<appname<<" -pp"<<endl;
int commit(LedKeyboard &kbd) {
if (! kbd.open()) return 1;
if (kbd.commit()) return 0;
return 1;
}
void listkeys() {
cout<<appname<<" Keys in groups :"<<endl;
cout<<"-------------------------"<<endl;
cout<<""<<endl;
cout<<"Group logo :"<<endl;
cout<<" logo"<<endl;
cout<<" logo2"<<endl;
cout<<""<<endl;
cout<<"Group indicators :"<<endl;
cout<<" num_indicator, numindicator, num"<<endl;
cout<<" caps_indicator, capsindicator, caps"<<endl;
cout<<" scroll_indicator, scrollindicator, scroll"<<endl;
cout<<" game_mode, gamemode, game"<<endl;
cout<<" back_light, backlight, light"<<endl;
cout<<""<<endl;
cout<<"Group fkeys :"<<endl;
cout<<" f1 - f12"<<endl;
cout<<""<<endl;
cout<<"Group modifiers :"<<endl;
cout<<" shift_left, shiftleft, shiftl"<<endl;
cout<<" ctrl_left, ctrlleft, ctrll"<<endl;
cout<<" win_left, winleft, win_left"<<endl;
cout<<" alt_left, altleft, altl"<<endl;
cout<<" alt_right, altright, altr, altgr"<<endl;
cout<<" win_right, winright, winr"<<endl;
cout<<" menu"<<endl;
cout<<" ctrl_right, ctrlright, ctrlr"<<endl;
cout<<" shift_right, shiftright, shiftr"<<endl;
cout<<""<<endl;
cout<<"Group multimedia :"<<endl;
cout<<" mute"<<endl;
cout<<" play_pause, playpause, play"<<endl;
cout<<" stop"<<endl;
cout<<" previous, prev"<<endl;
cout<<" next"<<endl;
cout<<""<<endl;
cout<<"Group arrows :"<<endl;
cout<<" arrow_top, arrowtop, top"<<endl;
cout<<" arrow_left, arrowleft, left"<<endl;
cout<<" arrow_bottom, arrowbottom, bottom"<<endl;
cout<<" arrow_right, arrowright, right"<<endl;
cout<<""<<endl;
cout<<"Group numeric :"<<endl;
cout<<" num_lock, numlock"<<endl;
cout<<" num_slash, numslash, num/"<<endl;
cout<<" num_asterisk, numasterisk, num*"<<endl;
cout<<" num_minus, numminus, num-"<<endl;
cout<<" num_plus, numplus, num+"<<endl;
cout<<" numenter"<<endl;
cout<<" num0 - num9"<<endl;
cout<<" num_dot, numdot, num."<<endl;
cout<<""<<endl;
cout<<"Group functions :"<<endl;
cout<<" escape, esc"<<endl;
cout<<" print_screen, printscreen, printscr"<<endl;
cout<<" scroll_lock, scrolllock"<<endl;
cout<<" pause_break, pausebreak"<<endl;
cout<<" insert, ins"<<endl;
cout<<" home"<<endl;
cout<<" page_up, pageup"<<endl;
cout<<" delete, del"<<endl;
cout<<" end"<<endl;
cout<<" page_down, pagedown"<<endl;
cout<<""<<endl;
cout<<"Group keys :"<<endl;
cout<<" 0 - 9"<<endl;
cout<<" a - z"<<endl;
cout<<" tab"<<endl;
cout<<" caps_lock, capslock"<<endl;
cout<<" space"<<endl;
cout<<" backspace, back"<<endl;
cout<<" enter"<<endl;
cout<<" tilde"<<endl;
cout<<" minus"<<endl;
cout<<" equal"<<endl;
cout<<" open_bracket"<<endl;
cout<<" close_bracket"<<endl;
cout<<" backslash"<<endl;
cout<<" semicolon"<<endl;
cout<<" dollar"<<endl;
cout<<" quote"<<endl;
cout<<" intl_backslash"<<endl;
cout<<" comma"<<endl;
cout<<" period"<<endl;
cout<<" slash"<<endl;
cout<<"Group gkeys :"<<endl;
cout<<" g1 - g9"<<endl;
}
int commit() {
Keyboard lg_kbd;
lg_kbd.attach();
lg_kbd.commit();
lg_kbd.detach();
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;
}
int setStartupEffect(string effect) {
Keyboard lg_kbd;
Keyboard::PowerOnEffect powerOnEffect;
if (lg_kbd.parsePowerOnEffect(effect, powerOnEffect) == true) {
lg_kbd.attach();
lg_kbd.setPowerOnEffect(powerOnEffect);
lg_kbd.commit();
lg_kbd.detach();
return 0;
}
return 1;
}
int setKey(string key, string color, bool commit) {
Keyboard lg_kbd;
Keyboard::KeyAddress keyAddress;
if (lg_kbd.parseKey(key, keyAddress) == true) {
Keyboard::KeyColors colors;
if (lg_kbd.parseColor(color, colors) == true) {
Keyboard::KeyValue keyValue;
keyValue.key = keyAddress;
keyValue.colors = colors;
lg_kbd.attach();
lg_kbd.setKey(keyValue);
if (commit == true) lg_kbd.commit();
lg_kbd.detach();
return 0;
}
}
return 1;
}
int setAllKeys(string color, bool commit) {
Keyboard lg_kbd;
Keyboard::KeyColors colors;
if (lg_kbd.parseColor(color, colors) == true) {
lg_kbd.attach();
lg_kbd.setAllKeys(colors);
if (commit == true) lg_kbd.commit();
lg_kbd.detach();
return 0;
}
return 1;
}
int setGroupKeys(string groupKeys, string color, bool commit) {
Keyboard lg_kbd;
Keyboard::KeyGroup keyGroup;
if (lg_kbd.parseKeyGroup(groupKeys, keyGroup) == true) {
Keyboard::KeyColors colors;
if (lg_kbd.parseColor(color, colors) == true) {
lg_kbd.attach();
lg_kbd.setGroupKeys(keyGroup, colors);
if (commit == true) lg_kbd.commit();
lg_kbd.detach();
return 0;
}
}
return 1;
}
int setFXColor(string color, bool keys, bool logo) {
Keyboard lg_kbd;
Keyboard::KeyColors colors;
if (lg_kbd.parseColor(color, colors) == true) {
lg_kbd.attach();
lg_kbd.setGroupKeys(Keyboard::KeyGroup::indicators, colors);
lg_kbd.commit();
if(keys) lg_kbd.setFXColorKeys(colors);
if(logo) lg_kbd.setFXColorLogo(colors);
lg_kbd.detach();
return 0;
}
return 1;
}
int setFXBreathing(string color, string speed, bool keys, bool logo) {
Keyboard lg_kbd;
Keyboard::KeyColors colors;
uint8_t speedValue;
if (lg_kbd.parseColor(color, colors) == true) {
if (lg_kbd.parseSpeed(speed, speedValue) == true) {
lg_kbd.attach();
if(keys) lg_kbd.setFXBreathingKeys(colors, speedValue);
if(logo) lg_kbd.setFXBreathingLogo(colors, speedValue);
lg_kbd.detach();
return 0;
}
}
return 1;
}
int setFXColorCycle(string speed, bool keys, bool logo) {
Keyboard lg_kbd;
uint8_t speedValue;
if (lg_kbd.parseSpeed(speed, speedValue) == true) {
lg_kbd.attach();
if(keys) lg_kbd.setFXColorCycleKeys(speedValue);
if(logo) lg_kbd.setFXColorCycleLogo(speedValue);
lg_kbd.detach();
return 0;
}
return 1;
}
int setFXHWave(string speed, bool keys, bool logo) {
Keyboard lg_kbd;
uint8_t speedValue;
if (lg_kbd.parseSpeed(speed, speedValue) == true) {
lg_kbd.attach();
if(keys) lg_kbd.setFXHWaveKeys(speedValue);
if(logo) lg_kbd.setFXColorCycleLogo(speedValue);
lg_kbd.detach();
return 0;
}
return 1;
}
int setFXVWave(string speed, bool keys, bool logo) {
Keyboard lg_kbd;
uint8_t speedValue;
if (lg_kbd.parseSpeed(speed, speedValue) == true) {
lg_kbd.attach();
if(keys) lg_kbd.setFXVWaveKeys(speedValue);
if(logo) lg_kbd.setFXColorCycleLogo(speedValue);
lg_kbd.detach();
return 0;
}
return 1;
}
int setFXCWave(string speed, bool keys, bool logo) {
Keyboard lg_kbd;
uint8_t speedValue;
if (lg_kbd.parseSpeed(speed, speedValue) == true) {
lg_kbd.attach();
if(keys) lg_kbd.setFXCWaveKeys(speedValue);
if(logo) lg_kbd.setFXColorCycleLogo(speedValue);
lg_kbd.detach();
return 0;
}
return 1;
}
int parseProfile(istream &is) {
string line;
int lineCount = 1;
int ind;
Keyboard lg_kbd;
Keyboard::KeyGroup keyGroup;
Keyboard::KeyAddress keyAddress;
Keyboard::KeyValue keyValue;
Keyboard::KeyColors colors;
uint8_t speedValue;
map<string, string> var;
vector<Keyboard::KeyValue> keys;
lg_kbd.attach();
while (!is.eof()) {
getline(is, 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);
if (lg_kbd.parseColor(line, colors) == true) {
keys.clear();
lg_kbd.setAllKeys(colors);
} else cout<<"Error on line "<<lineCount<<" : "<<line<<"\n";
} else if (line.substr(0,1) == "g") {
line = line.substr(2);
ind = line.find(" ");
if (lg_kbd.parseKeyGroup(line.substr(0, ind), keyGroup) == true) {
line = line.substr(ind + 1);
if (line.substr(0, 1) == "$") {
ind = line.find(" ");
line = var[line.substr(1, ind - 1)];
};
if (lg_kbd.parseColor(line.substr(0, 6), colors) == true) {
lg_kbd.setGroupKeys(keyGroup, colors);
} 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(" ");
if (lg_kbd.parseKey(line.substr(0, ind), keyAddress) == true) {
line = line.substr(ind + 1);
if (line.substr(0, 1) == "$") {
ind = line.find(" ");
line = var[line.substr(1, ind - 1)];
}
if (lg_kbd.parseColor(line.substr(0, 6), colors) == true) {
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") {
lg_kbd.commit();
lg_kbd.setKeys(&keys[0], keys.size());
keys.clear();
lg_kbd.commit();
} else if (line.substr(0,2) == "fx") {
bool fxkeys = true;
bool fxlogo = true;
line = line.substr(2);
if (line.substr(0,1) == "-") {
line = line.substr(1);
} else if (line.substr(0,2) == "k-") {
fxlogo = false;
line = line.substr(2);
} else if (line.substr(0,2) == "l-") {
fxkeys = false;
line = line.substr(2);
}
if (line.substr(0,5) == "color") {
line = line.substr(6);
if (line.substr(0, 1) == "$") {
ind = line.find(" ");
line = var[line.substr(1, ind - 1)];
} else line = line.substr(0, 6);
if (lg_kbd.parseColor(line, colors) == true) {
lg_kbd.setGroupKeys(Keyboard::KeyGroup::indicators, colors);
lg_kbd.commit();
if(fxkeys) lg_kbd.setFXColorKeys(colors);
if(fxlogo) lg_kbd.setFXColorLogo(colors);
} else cout<<"Error on line "<<lineCount<<" : "<<line<<"\n";
} else if (line.substr(0,9) == "breathing") {
line = line.substr(10);
if (line.substr(0, 1) == "$") {
ind = line.find(" ");
line = var[line.substr(1, ind - 1)] + line.substr(ind);
};
ind = line.find(" ");
if(ind < 6) line.insert(2, "0000");
if (lg_kbd.parseColor(line.substr(0, 6), colors) == true) {
ind = line.find(" ");
line = line.substr(ind + 1, 2);
if (lg_kbd.parseSpeed(line.substr(0, 2), speedValue) == true) {
if(fxkeys) lg_kbd.setFXBreathingKeys(colors, speedValue);
if(fxlogo) lg_kbd.setFXBreathingLogo(colors, speedValue);
} else cout<<"Error on line "<<lineCount<<" : "<<line<<"\n";
} else cout<<"Error on line "<<lineCount<<" : "<<line<<"\n";
} else if (line.substr(0,5) == "cycle") {
line = line.substr(6);
if (lg_kbd.parseSpeed(line.substr(0, 2), speedValue) == true) {
if(fxkeys) lg_kbd.setFXColorCycleKeys(speedValue);
if(fxlogo) lg_kbd.setFXColorCycleLogo(speedValue);
} else cout<<"Error on line "<<lineCount<<" : "<<line<<"\n";
} else if (line.substr(0,5) == "hwave") {
line = line.substr(6);
if (lg_kbd.parseSpeed(line.substr(0, 2), speedValue) == true) {
if(fxkeys) lg_kbd.setFXHWaveKeys(speedValue);
if(fxkeys && fxlogo) lg_kbd.setFXColorCycleLogo(speedValue);
} else cout<<"Error on line "<<lineCount<<" : "<<line<<"\n";
} else if (line.substr(0,5) == "vwave") {
line = line.substr(6);
if (lg_kbd.parseSpeed(line.substr(0, 2), speedValue) == true) {
if(fxkeys) lg_kbd.setFXVWaveKeys(speedValue);
if(fxkeys && fxlogo) lg_kbd.setFXColorCycleLogo(speedValue);
} else cout<<"Error on line "<<lineCount<<" : "<<line<<"\n";
} else if (line.substr(0,5) == "cwave") {
line = line.substr(6);
if (lg_kbd.parseSpeed(line.substr(0, 2), speedValue) == true) {
if(fxkeys) lg_kbd.setFXCWaveKeys(speedValue);
if(fxkeys && fxlogo) lg_kbd.setFXColorCycleLogo(speedValue);
} else cout<<"Error on line "<<lineCount<<" : "<<line<<"\n";
}
} else if ((line.substr(0, 1) != "#") && (line.substr(0, 1) != "")) {
cout<<"Error on line "<<lineCount<<" : "<<line<<"\n";
}
lineCount++;
}
lg_kbd.detach();
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;
}
int loadProfile(string profileFile) {
ifstream file;
file.open(profileFile);
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;
}
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;
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;
}
if (! kbd.open()) return 1;
int retval = 0;
switch (effectPart) {
case LedKeyboard::NativeEffectPart::all:
switch (effect) {
case LedKeyboard::NativeEffect::color:
if (! kbd.setGroupKeys(LedKeyboard::KeyGroup::indicators, color)) retval = 1;
if (! kbd.commit()) retval = 1;
break;
case LedKeyboard::NativeEffect::breathing:
if (! kbd.setGroupKeys(LedKeyboard::KeyGroup::indicators, color)) retval = 1;
if (! kbd.commit()) retval = 1;
break;
case LedKeyboard::NativeEffect::cycle:
case LedKeyboard::NativeEffect::hwave:
case LedKeyboard::NativeEffect::vwave:
case LedKeyboard::NativeEffect::cwave:
if (! kbd.setGroupKeys(
LedKeyboard::KeyGroup::indicators,
LedKeyboard::Color({0x00, 0xff, 0xff}))
) retval = 1;
if (! kbd.commit()) retval = 1;
break;
}
if (! kbd.setNativeEffect(effect, LedKeyboard::NativeEffectPart::keys, speed, color)) retval = 1;
if (! kbd.setNativeEffect(effect, LedKeyboard::NativeEffectPart::logo, speed, color)) retval = 1;
break;
default:
if (! kbd.setNativeEffect(effect, effectPart, speed, color)) retval = 1;
break;
}
return retval;
}
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;
return 1;
}
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);
}
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 });
} 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;
}
}
}
return retval;
}
int loadProfile(LedKeyboard &kbd, char *arg2) {
std::ifstream file;
file.open(arg2);
if (file.is_open()) {
int retval = parseProfile(file);
int retval = 0;
retval = parseProfile(kbd, file);
file.close();
return retval;
}
return 1;
}
int pipeProfile() {
int pipeProfile(LedKeyboard &kbd) {
if (isatty(fileno(stdin))) return 1;
return parseProfile(cin);
return parseProfile(kbd, std::cin);
}
int main(int argc, char *argv[]) {
string str = argv[0];
size_t split = str.find_last_of("/\\");
str = str.substr(split + 1);
appname = str;
int main(int argc, char **argv) {
if (argc > 1) {
string argCmd = argv[1];
if (argCmd == "-h" || argCmd == "--help") { usage(); return 0; }
else if (argCmd == "-lk" || argCmd == "--list-keys") { listkeys(); return 0; }
else if (argCmd == "-s" && argc == 3) return setStartupEffect(argv[2]);
else if (argCmd == "-a" && argc == 3) return setAllKeys(argv[2], true);
else if (argCmd == "-an" && argc == 3) return setAllKeys(argv[2], false);
else if (argCmd == "-g" && argc == 4) return setGroupKeys(argv[2], argv[3], true);
else if (argCmd == "-gn" && argc == 4) return setGroupKeys(argv[2], argv[3], false);
else if (argCmd == "-k" && argc == 4) return setKey(argv[2], argv[3], true);
else if (argCmd == "-kn" && argc == 4) return setKey(argv[2], argv[3], false);
else if (argCmd == "-c" && argc == 2) return commit();
else if (argCmd == "-p" && argc == 3) return loadProfile(argv[2]);
else if (argCmd == "-pp" && argc == 2) return pipeProfile();
else if (argCmd == "-fx-color" && argc == 3) return setFXColor(argv[2], true, true);
else if (argCmd == "-fx-breathing" && argc == 4) return setFXBreathing(argv[2], argv[3], true, true);
else if (argCmd == "-fx-cycle" && argc == 3) return setFXColorCycle(argv[2], true, true);
else if (argCmd == "-fx-hwave" && argc == 3) return setFXHWave(argv[2], true, true);
else if (argCmd == "-fx-vwave" && argc == 3) return setFXVWave(argv[2], true, true);
else if (argCmd == "-fx-cwave" && argc == 3) return setFXCWave(argv[2], true, true);
else if (argCmd == "-fxk-color" && argc == 3) return setFXColor(argv[2], true, false);
else if (argCmd == "-fxk-breathing" && argc == 4) return setFXBreathing(argv[2], argv[3], true, false);
else if (argCmd == "-fxk-cycle" && argc == 3) return setFXColorCycle(argv[2], true, false);
else if (argCmd == "-fxk-hwave" && argc == 3) return setFXHWave(argv[2], true, false);
else if (argCmd == "-fxk-vwave" && argc == 3) return setFXVWave(argv[2], true, false);
else if (argCmd == "-fxk-cwave" && argc == 3) return setFXCWave(argv[2], true, false);
else if (argCmd == "-fxl-color" && argc == 3) return setFXColor(argv[2], false, true);
else if (argCmd == "-fxl-breathing" && argc == 4) return setFXBreathing(argv[2], argv[3], false, true);
else if (argCmd == "-fxl-cycle" && argc == 3) return setFXColorCycle(argv[2], false, true);
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 == "-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 > 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;
}
usage();
help::usage(argv[0]);
return 1;
}