-
Notifications
You must be signed in to change notification settings - Fork 2
/
JsonMacros.cpp
165 lines (151 loc) · 4.98 KB
/
JsonMacros.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*
* Copyright 2016 Clément Vuchener
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "JsonMacros.h"
#include "KeyUsage.h"
#include <list>
#include <iostream>
static uint8_t findKeyUsage (const std::list<const std::map<std::string, uint8_t> *> &maps,
std::string key_name)
{
for (const auto &map: maps) {
auto it = map->find (key_name);
if (it != map->end ())
return it->second;
}
return 0;
}
bool JsonToMacros (const Json::Value &profile,
std::vector<CorsairDevice::KeySettings> &keys,
const std::string &layout)
{
std::list<const std::map<std::string, uint8_t> *> keymaps = { &KeyUsage::keymap };
std::string key_str;
if (!layout.empty ()) {
auto it = KeyUsage::layouts.find (layout);
if (it != KeyUsage::layouts.end ()) {
keymaps.push_front (&it->second);
}
else
std::cerr << "warning: layout " << layout << "not found" << std::endl;
}
if (!profile.isArray ()) {
std::cerr << "profile is not an array" << std::endl;
return false;
}
keys.resize (profile.size ());
for (unsigned int i = 0; i < profile.size (); ++i) {
if (!profile[i].isMember ("key")) {
std::cerr << "Missing \"key\" member in key " << i << std::endl;
return false;
}
key_str = profile[i]["key"].asString ();
keys[i].key_usage = findKeyUsage (keymaps, key_str);
if (keys[i].key_usage == 0) {
std::cerr << "Unknown key: " << key_str << std::endl;
return false;
}
if (profile[i].isMember ("repeat_mode")) {
std::string repeat_mode = profile[i]["repeat_mode"].asString ();
if (repeat_mode == "fixed")
keys[i].repeat_mode = CorsairDevice::KeySettings::RepeatFixed;
else if (repeat_mode == "hold")
keys[i].repeat_mode = CorsairDevice::KeySettings::RepeatHold;
else if (repeat_mode == "toggle")
keys[i].repeat_mode = CorsairDevice::KeySettings::RepeatToggle;
else {
std::cerr << "Unknown repeat mode: " << repeat_mode << std::endl;
return false;
}
}
else
keys[i].repeat_mode = CorsairDevice::KeySettings::RepeatFixed;
if (profile[i].isMember ("type")) {
std::string type = profile[i]["type"].asString ();
if (type == "none")
keys[i].bind_type = CorsairDevice::KeySettings::BindNone;
else if (type == "key")
keys[i].bind_type = CorsairDevice::KeySettings::BindUsage;
else if (type == "macro")
keys[i].bind_type = CorsairDevice::KeySettings::BindMacro;
else {
std::cerr << "Unknown type: " << type << std::endl;
return false;
}
}
else
keys[i].bind_type = CorsairDevice::KeySettings::BindMacro;
switch (keys[i].bind_type) {
case CorsairDevice::KeySettings::BindNone:
break;
case CorsairDevice::KeySettings::BindUsage: {
if (!profile[i].isMember ("new_key")) {
std::cerr << "Missing \"new_key\" member for type \"key\"" << std::endl;
return false;
}
key_str = profile[i]["new_key"].asString ();
keys[i].target_usage = findKeyUsage (keymaps, key_str);
if (keys[i].target_usage == 0) {
std::cerr << "Unknown key: " << key_str << std::endl;
return false;
}
break;
}
case CorsairDevice::KeySettings::BindMacro: {
if (profile[i].isMember ("repeat_count"))
keys[i].repeat_count = profile[i]["repeat_count"].asUInt ();
else
keys[i].repeat_count = 1;
if (!profile[i].isMember ("macro")) {
std::cerr << "Missing \"macro\" member" << std::endl;
return false;
}
Json::Value macro = profile[i]["macro"];
if (!macro.isArray ()) {
std::cerr << "\"macro\" must be an array" << std::endl;
return false;
}
keys[i].macro.resize (macro.size ());
for (unsigned int j = 0; j < macro.size (); ++j) {
if (macro[j].isMember ("key")) {
keys[i].macro[j].type = CorsairDevice::MacroItem::Key;
key_str = macro[j]["key"].asString ();
keys[i].macro[j].key_event.usage = findKeyUsage (keymaps, key_str);
if (keys[i].macro[j].key_event.usage == 0) {
std::cerr << "Unknown key: " << key_str << std::endl;
return false;
}
if (!macro[j].isMember ("pressed")) {
std::cerr << "Missing \"pressed\" member in macro item" << std::endl;
return false;
}
keys[i].macro[j].key_event.pressed = macro[j]["pressed"].asBool ();
}
else if (macro[j].isMember ("delay")) {
keys[i].macro[j].type = CorsairDevice::MacroItem::Delay;
keys[i].macro[j].delay = macro[j]["delay"].asUInt ();
}
else {
std::cerr << "Invalid macro item" << std::endl;
}
}
break;
}
}
}
return true;
}