1 /*
2   This file is part of g810-led.
3 
4   g810-led is free software: you can redistribute it and/or modify
5   it under the terms of the GNU General Public License as published by
6   the Free Software Foundation, version 3 of the License.
7 
8   g810-led is distributed in the hope that it will be useful,
9   but WITHOUT ANY WARRANTY; without even the implied warranty of
10   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11   GNU General Public License for more details.
12 
13   You should have received a copy of the GNU General Public License
14   along with g810-led.  If not, see <https://www.gnu.org/licenses/>.
15 */
16 
17 #include "utils.h"
18 
19 #include <iostream>
20 #include <algorithm>
21 
22 #include "../classes/Keyboard.h"
23 
24 
25 namespace utils {
26 
getCmdName(std::string cmd)27 	std::string getCmdName(std::string cmd) {
28 		return cmd.substr(cmd.find_last_of("/\\") + 1);
29 	}
30 
31 
32 
parseStartupMode(std::string val,LedKeyboard::StartupMode & startupMode)33 	bool parseStartupMode(std::string val, LedKeyboard::StartupMode &startupMode) {
34 		if (val == "wave") startupMode = LedKeyboard::StartupMode::wave;
35 		else if (val == "color") startupMode = LedKeyboard::StartupMode::color;
36 		else return false;
37 		return true;
38 	}
39 
parseOnBoardMode(std::string val,LedKeyboard::OnBoardMode & onBoardMode)40 	bool parseOnBoardMode(std::string val, LedKeyboard::OnBoardMode &onBoardMode) {
41 		if (val == "software") onBoardMode = LedKeyboard::OnBoardMode ::software;
42 		else if (val == "board") onBoardMode = LedKeyboard::OnBoardMode::board;
43 		else return false;
44 		return true;
45 	}
46 
parseNativeEffect(std::string val,LedKeyboard::NativeEffect & nativeEffect)47 	bool parseNativeEffect(std::string val, LedKeyboard::NativeEffect &nativeEffect) {
48 		if (val == "color") nativeEffect = LedKeyboard::NativeEffect::color;
49 		else if (val == "cycle") nativeEffect = LedKeyboard::NativeEffect::cycle;
50 		else if (val == "breathing") nativeEffect = LedKeyboard::NativeEffect::breathing;
51 		else if (val == "waves") nativeEffect = LedKeyboard::NativeEffect::waves;
52 		else if (val == "hwave") nativeEffect = LedKeyboard::NativeEffect::hwave;
53 		else if (val == "vwave") nativeEffect = LedKeyboard::NativeEffect::vwave;
54 		else if (val == "cwave") nativeEffect = LedKeyboard::NativeEffect::cwave;
55 		else return false;
56 		return true;
57 	}
58 
parseNativeEffectPart(std::string val,LedKeyboard::NativeEffectPart & nativeEffectPart)59 	bool parseNativeEffectPart(std::string val, LedKeyboard::NativeEffectPart &nativeEffectPart) {
60 		if (val == "all") nativeEffectPart = LedKeyboard::NativeEffectPart::all;
61 		else if (val == "keys") nativeEffectPart = LedKeyboard::NativeEffectPart::keys;
62 		else if (val == "logo") nativeEffectPart = LedKeyboard::NativeEffectPart::logo;
63 		else return false;
64 		return true;
65 	}
66 
parseKey(std::string val,LedKeyboard::Key & key)67 	bool parseKey(std::string val, LedKeyboard::Key &key) {
68 		std::transform(val.begin(), val.end(), val.begin(), ::tolower);
69 		if (val == "logo") key = LedKeyboard::Key::logo;
70 		else if (val == "logo2") key = LedKeyboard::Key::logo2;
71 		else if (val == "back_light" || val == "backlight" || val == "light") key = LedKeyboard::Key::backlight;
72 		else if (val == "game_mode" || val == "gamemode" || val == "game") key = LedKeyboard::Key::game;
73 		else if (val == "caps_indicator" || val == "capsindicator" || val == "caps") key = LedKeyboard::Key::caps;
74 		else if (val == "scroll_indicator" || val == "scrollindicator" || val == "scroll") key = LedKeyboard::Key::scroll;
75 		else if (val == "num_indicator" || val == "numindicator" || val == "num") key = LedKeyboard::Key::num;
76 		else if (val == "next") key = LedKeyboard::Key::next;
77 		else if (val == "prev" || val == "previous") key = LedKeyboard::Key::prev;
78 		else if (val == "stop") key = LedKeyboard::Key::stop;
79 		else if (val == "play_pause" || val == "playpause" || val == "play") key = LedKeyboard::Key::play;
80 		else if (val == "mute") key = LedKeyboard::Key::mute;
81 		else if (val == "a") key = LedKeyboard::Key::a;
82 		else if (val == "b") key = LedKeyboard::Key::b;
83 		else if (val == "c") key = LedKeyboard::Key::c;
84 		else if (val == "d") key = LedKeyboard::Key::d;
85 		else if (val == "e") key = LedKeyboard::Key::e;
86 		else if (val == "f") key = LedKeyboard::Key::f;
87 		else if (val == "g") key = LedKeyboard::Key::g;
88 		else if (val == "h") key = LedKeyboard::Key::h;
89 		else if (val == "i") key = LedKeyboard::Key::i;
90 		else if (val == "j") key = LedKeyboard::Key::j;
91 		else if (val == "k") key = LedKeyboard::Key::k;
92 		else if (val == "l") key = LedKeyboard::Key::l;
93 		else if (val == "m") key = LedKeyboard::Key::m;
94 		else if (val == "n") key = LedKeyboard::Key::n;
95 		else if (val == "o") key = LedKeyboard::Key::o;
96 		else if (val == "p") key = LedKeyboard::Key::p;
97 		else if (val == "q") key = LedKeyboard::Key::q;
98 		else if (val == "r") key = LedKeyboard::Key::r;
99 		else if (val == "s") key = LedKeyboard::Key::s;
100 		else if (val == "t") key = LedKeyboard::Key::t;
101 		else if (val == "u") key = LedKeyboard::Key::u;
102 		else if (val == "v") key = LedKeyboard::Key::v;
103 		else if (val == "w") key = LedKeyboard::Key::w;
104 		else if (val == "x") key = LedKeyboard::Key::x;
105 		else if (val == "z") key = LedKeyboard::Key::z;
106 		else if (val == "y") key = LedKeyboard::Key::y;
107 		else if (val == "1" || val == "one") key = LedKeyboard::Key::n1;
108 		else if (val == "2" || val == "two") key = LedKeyboard::Key::n2;
109 		else if (val == "3" || val == "three") key = LedKeyboard::Key::n3;
110 		else if (val == "4" || val == "four") key = LedKeyboard::Key::n4;
111 		else if (val == "5" || val == "five") key = LedKeyboard::Key::n5;
112 		else if (val == "6" || val == "six") key = LedKeyboard::Key::n6;
113 		else if (val == "7" || val == "seven") key = LedKeyboard::Key::n7;
114 		else if (val == "8" || val == "eight") key = LedKeyboard::Key::n8;
115 		else if (val == "9" || val == "nine") key = LedKeyboard::Key::n9;
116 		else if (val == "0" || val == "zero") key = LedKeyboard::Key::n0;
117 		else if (val == "enter") key = LedKeyboard::Key::enter;
118 		else if (val == "esc" || val == "escape") key = LedKeyboard::Key::esc;
119 		else if (val == "back" || val == "backspace") key = LedKeyboard::Key::backspace;
120 		else if (val == "tab") key = LedKeyboard::Key::tab;
121 		else if (val == "space") key = LedKeyboard::Key::space;
122 		else if (val == "tilde" || val == "~") key = LedKeyboard::Key::tilde;
123 		else if (val == "minus" || val == "-") key = LedKeyboard::Key::minus;
124 		else if (val == "equal" || val == "=") key = LedKeyboard::Key::equal;
125 		else if (val == "open_bracket" || val == "[") key = LedKeyboard::Key::open_bracket;
126 		else if (val == "close_bracket" || val == "]") key = LedKeyboard::Key::close_bracket;
127 		else if (val == "backslash" || val == "\\") key = LedKeyboard::Key::backslash;
128 		else if (val == "semicolon" || val == ";") key = LedKeyboard::Key::semicolon;
129 		else if (val == "quote" || val == "\"") key = LedKeyboard::Key::quote;
130 		else if (val == "dollar" || val == "$") key = LedKeyboard::Key::dollar;
131 		else if (val == "comma" || val == ",") key = LedKeyboard::Key::comma;
132 		else if (val == "period" || val == ".") key = LedKeyboard::Key::period;
133 		else if (val == "slash" || val == "/") key = LedKeyboard::Key::slash;
134 		else if (val == "caps_lock" || val == "capslock") key = LedKeyboard::Key::caps_lock;
135 		else if (val == "f1") key = LedKeyboard::Key::f1;
136 		else if (val == "f2") key = LedKeyboard::Key::f2;
137 		else if (val == "f3") key = LedKeyboard::Key::f3;
138 		else if (val == "f4") key = LedKeyboard::Key::f4;
139 		else if (val == "f5") key = LedKeyboard::Key::f5;
140 		else if (val == "f6") key = LedKeyboard::Key::f6;
141 		else if (val == "f7") key = LedKeyboard::Key::f7;
142 		else if (val == "f8") key = LedKeyboard::Key::f8;
143 		else if (val == "f9") key = LedKeyboard::Key::f9;
144 		else if (val == "f10") key = LedKeyboard::Key::f10;
145 		else if (val == "f11") key = LedKeyboard::Key::f11;
146 		else if (val == "f12") key = LedKeyboard::Key::f12;
147 		else if (val == "print_screen" || val == "printscreen" || val == "printscr" || val == "print")
148 			key = LedKeyboard::Key::print_screen;
149 		else if (val == "scroll_lock" || val == "scrolllock") key = LedKeyboard::Key::scroll_lock;
150 		else if (val == "pause_break" || val == "pausebreak" || val == "pause" || val == "break")
151 			key = LedKeyboard::Key::pause_break;
152 		else if (val == "insert" || val == "ins") key = LedKeyboard::Key::insert;
153 		else if (val == "home") key = LedKeyboard::Key::home;
154 		else if (val == "page_up" || val == "pageup") key = LedKeyboard::Key::page_up;
155 		else if (val == "delete" || val == "del") key = LedKeyboard::Key::del;
156 		else if (val == "end") key = LedKeyboard::Key::end;
157 		else if (val == "page_down" || val == "pagedown") key = LedKeyboard::Key::page_down;
158 		else if (val == "arrow_right" || val == "arrowright" || val == "right") key = LedKeyboard::Key::arrow_right;
159 		else if (val == "arrow_left" || val == "arrowleft" || val == "left") key = LedKeyboard::Key::arrow_left;
160 		else if (val == "arrow_bottom" || val == "arrowbottom" || val == "bottom") key = LedKeyboard::Key::arrow_bottom;
161 		else if (val == "arrow_top" || val == "arrowtop" || val == "top") key = LedKeyboard::Key::arrow_top;
162 		else if (val == "num_lock" || val == "numlock") key = LedKeyboard::Key::num_lock;
163 		else if (val == "num/" || val == "num_slash" || val == "numslash") key = LedKeyboard::Key::num_slash;
164 		else if (val == "num*" || val == "num_asterisk" || val == "numasterisk") key = LedKeyboard::Key::num_asterisk;
165 		else if (val == "num-" || val == "num_minus" || val == "numminus") key = LedKeyboard::Key::num_minus;
166 		else if (val == "num+" || val == "num_plus" || val == "numplus") key = LedKeyboard::Key::num_plus;
167 		else if (val == "numenter") key = LedKeyboard::Key::num_enter;
168 		else if (val == "num1") key = LedKeyboard::Key::num_1;
169 		else if (val == "num2") key = LedKeyboard::Key::num_2;
170 		else if (val == "num3") key = LedKeyboard::Key::num_3;
171 		else if (val == "num4") key = LedKeyboard::Key::num_4;
172 		else if (val == "num5") key = LedKeyboard::Key::num_5;
173 		else if (val == "num6") key = LedKeyboard::Key::num_6;
174 		else if (val == "num7") key = LedKeyboard::Key::num_7;
175 		else if (val == "num8") key = LedKeyboard::Key::num_8;
176 		else if (val == "num9") key = LedKeyboard::Key::num_9;
177 		else if (val == "num0") key = LedKeyboard::Key::num_0;
178 		else if (val == "num." || val == "num_period" || val == "numperiod") key = LedKeyboard::Key::num_dot;
179 		else if (val == "intl_backslash" || val == "<") key = LedKeyboard::Key::intl_backslash;
180 		else if (val == "menu") key = LedKeyboard::Key::menu;
181 		else if (val == "ctrl_left" || val == "ctrlleft" || val == "ctrll") key = LedKeyboard::Key::ctrl_left;
182 		else if (val == "shift_left" || val == "shiftleft" || val == "shiftl") key = LedKeyboard::Key::shift_left;
183 		else if (val == "alt_left" || val == "altleft" || val == "altl") key = LedKeyboard::Key::alt_left;
184 		else if (val == "win_left" || val == "winleft" || val == "winl") key = LedKeyboard::Key::win_left;
185 		else if (val == "meta_left" || val == "metaleft" || val == "metal") key = LedKeyboard::Key::win_left;
186 		else if (val == "ctrl_right" || val == "ctrlright" || val == "ctrlr") key = LedKeyboard::Key::ctrl_right;
187 		else if (val == "shift_right" || val == "shiftright" || val == "shiftr") key = LedKeyboard::Key::shift_right;
188 		else if (val == "alt_right" || val == "altright" || val == "altr" || val == "altgr")
189 			key = LedKeyboard::Key::alt_right;
190 		else if (val == "win_right" || val == "winright" || val == "winr") key = LedKeyboard::Key::win_right;
191 		else if (val == "meta_right" || val == "metaright" || val == "metar") key = LedKeyboard::Key::win_right;
192 		else if (val == "g1") key = LedKeyboard::Key::g1;
193 		else if (val == "g2") key = LedKeyboard::Key::g2;
194 		else if (val == "g3") key = LedKeyboard::Key::g3;
195 		else if (val == "g4") key = LedKeyboard::Key::g4;
196 		else if (val == "g5") key = LedKeyboard::Key::g5;
197 		else if (val == "g6") key = LedKeyboard::Key::g6;
198 		else if (val == "g7") key = LedKeyboard::Key::g7;
199 		else if (val == "g8") key = LedKeyboard::Key::g8;
200 		else if (val == "g9") key = LedKeyboard::Key::g9;
201 		else return false;
202 		return true;
203 	}
204 
parseKeyGroup(std::string val,LedKeyboard::KeyGroup & keyGroup)205 	bool parseKeyGroup(std::string val, LedKeyboard::KeyGroup &keyGroup) {
206 		if (val == "logo") keyGroup = LedKeyboard::KeyGroup::logo;
207 		else if (val == "indicators") keyGroup = LedKeyboard::KeyGroup::indicators;
208 		else if (val == "multimedia") keyGroup = LedKeyboard::KeyGroup::multimedia;
209 		else if (val == "fkeys") keyGroup = LedKeyboard::KeyGroup::fkeys;
210 		else if (val == "modifiers") keyGroup = LedKeyboard::KeyGroup::modifiers;
211 		else if (val == "arrows") keyGroup = LedKeyboard::KeyGroup::arrows;
212 		else if (val == "numeric") keyGroup = LedKeyboard::KeyGroup::numeric;
213 		else if (val == "functions") keyGroup = LedKeyboard::KeyGroup::functions;
214 		else if (val == "keys") keyGroup = LedKeyboard::KeyGroup::keys;
215 		else if (val == "gkeys") keyGroup = LedKeyboard::KeyGroup::gkeys;
216 		else return false;
217 		return true;
218 	}
219 
parseColor(std::string val,LedKeyboard::Color & color)220 	bool parseColor(std::string val, LedKeyboard::Color &color) {
221 		if (val.length() == 2) val = val + "0000";  // For G610
222 		if (val.length() != 6) return false;
223 		color.red = std::stoul("0x"+val.substr(0,2), nullptr, 16);
224 		color.green = std::stoul("0x"+val.substr(2,2), nullptr, 16);
225 		color.blue = std::stoul("0x"+val.substr(4,2), nullptr, 16);
226 		return true;
227 	}
228 
parsePeriod(std::string val,std::chrono::duration<uint16_t,std::milli> & period)229 	bool parsePeriod(std::string val, std::chrono::duration<uint16_t, std::milli> &period) {
230 		if (!val.empty() && val.back() == 's') {
231 			if ((val.length() >= 2) && (val[val.length()-2] == 'm'))
232 				period = std::chrono::milliseconds(std::stoul(val, nullptr));
233 			else
234 				period = std::chrono::seconds(std::stoul(val, nullptr));
235 		} else {
236 			if (val.length() == 1) val = "0" + val;
237 			if (val.length() != 2) return false;
238 			period = std::chrono::milliseconds(std::stoul("0x" + val, nullptr, 16) << 8);
239 		}
240 		return true;
241 	}
242 
parseUInt8(std::string val,uint8_t & uint8)243 	bool parseUInt8(std::string val, uint8_t &uint8) {
244 		if (val.length() == 1) val = "0" + val;
245 		if (val.length() != 2) return false;
246 		uint8 = std::stoul("0x" + val, nullptr, 16);
247 		return true;
248 	}
249 
parseUInt16(std::string val,uint16_t & uint16)250 	bool parseUInt16(std::string val, uint16_t &uint16) {
251 		if (val.length() == 1) val = "0" + val;
252 		if (val.length() == 2) val = "0" + val;
253 		if (val.length() == 3) val = "0" + val;
254 		if (val.length() != 4) return false;
255 		uint16 = std::stoul("0x" + val, nullptr, 16);
256 		return true;
257 	}
258 
259 }
260