Debian Codegraph @

target-ethos/input/input.cpp

raw symbols SCIP
1 #include "../ethos.hpp"
2 #include "hotkeys.cpp"
3 InputManager* inputManager = nullptr;
4 HID::Null hidNull;
5
6 void AbstractInput::bind() {
7 inputList.reset();
8 lstring list = mapping.split(",");
9
10 for(auto& mapping : list) {
11 lstring values = mapping.split("/");
12 if(values.size() == 1) continue; //skip "None" mapping
13
14 uint64_t id = hex(values[0]);
15 string group = values(1, "");
16 string input = values(2, "");
17 string qualifier = values(3, "");
18
19 Input item;
20 for(auto device : inputManager->devices) {
21 if(id != device->id) continue;
22 if(group == "Rumble") {
23 item.device = device;
24 item.id = id;
25 item.group = 0;
26 item.input = 0;
27 break;
28 }
29 if(auto groupID = device->find(group)) {
30 if(auto inputID = device->group[groupID()].find(input)) {
31 item.device = device;
32 item.id = id;
33 item.group = groupID();
34 item.input = inputID();
35 item.qualifier = Input::Qualifier::None;
36 if(qualifier == "Lo") item.qualifier = Input::Qualifier::Lo;
37 if(qualifier == "Hi") item.qualifier = Input::Qualifier::Hi;
38 break;
39 }
40 }
41 }
42 if(item.device == nullptr) continue;
43
44 inputList.append(item);
45 }
46 }
47
48 bool AbstractInput::append(string encode) {
49 lstring mappings = mapping.split(",");
50 if(mappings.find(encode)) return true; //mapping already bound
51 if(mapping.empty() || mapping == "None") mapping = encode; //remove "None"
52 else mapping.append(",", encode); //add to existing mapping list
53 bind();
54 return true;
55 }
56
57 //
58
59 bool DigitalInput::bind(HID::Device& device, unsigned group, unsigned input, int16_t oldValue, int16_t newValue) {
60 if(device.isNull() || (device.isKeyboard() && device.group[group].input[input].name == "Escape")) {
61 inputList.reset();
62 mapping = "None";
63 return true;
64 }
65
66 string encode = {hex(device.id), "/", device.group[group].name, "/", device.group[group].input[input].name};
67
68 if((device.isKeyboard() && group == HID::Keyboard::GroupID::Button)
69 || (device.isMouse() && group == HID::Mouse::GroupID::Button)
70 || (device.isJoypad() && group == HID::Joypad::GroupID::Button)
71 ) {
72 if(newValue != 0) return append(encode);
73 }
74
75 if((device.isJoypad() && group == HID::Joypad::GroupID::Axis)
76 || (device.isJoypad() && group == HID::Joypad::GroupID::Hat)
77 ) {
78 if(newValue < -16384) return append({encode, "/Lo"});
79 if(newValue > +16384) return append({encode, "/Hi"});
80 }
81
82 return false;
83 }
84
85 int16_t DigitalInput::poll() {
86 if(program->focused() == false) return 0;
87 if(inputList.size() == 0) return 0;
88 bool result = logic;
89
90 for(auto& item : inputList) {
91 HID::Device& device = *(item.device);
92 int16_t value = device.group[item.group].input[item.input].value;
93 bool output = logic;
94 if((device.isKeyboard() && item.group == HID::Keyboard::GroupID::Button)
95 || (device.isMouse() && item.group == HID::Mouse::GroupID::Button)
96 || (device.isJoypad() && item.group == HID::Joypad::GroupID::Button)
97 ) {
98 output = value;
99 }
100 if((device.isJoypad() && item.group == HID::Joypad::GroupID::Axis)
101 || (device.isJoypad() && item.group == HID::Joypad::GroupID::Hat)
102 ) {
103 if(item.qualifier == Input::Qualifier::Lo) output = value < -16384;
104 if(item.qualifier == Input::Qualifier::Hi) output = value > +16384;
105 }
106 if(logic == 0) result |= output;
107 if(logic == 1) result &= output;
108 }
109
110 return result;
111 }
112
113 //
114
115 bool RelativeInput::bind(HID::Device& device, unsigned group, unsigned input, int16_t oldValue, int16_t newValue) {
116 if(device.isNull() || (device.isKeyboard() && device.group[group].input[input].name == "Escape")) {
117 inputList.reset();
118 mapping = "None";
119 return true;
120 }
121
122 string encode = {hex(device.id), "/", device.group[group].name, "/", device.group[group].input[input].name};
123
124 if((device.isMouse() && group == HID::Mouse::GroupID::Axis)
125 || (device.isJoypad() && group == HID::Joypad::GroupID::Axis)
126 || (device.isJoypad() && group == HID::Joypad::GroupID::Hat)
127 ) {
128 if(newValue < -16384) return append(encode);
129 if(newValue > +16384) return append(encode);
130 }
131
132 return false;
133 }
134
135 int16_t RelativeInput::poll() {
136 if(program->focused() == false) return 0;
137 if(inputList.size() == 0) return 0;
138 int16_t result = 0;
139
140 for(auto& item : inputList) {
141 HID::Device& device = *(item.device);
142 int16_t value = device.group[item.group].input[item.input].value;
143 if(device.isJoypad() && item.group == HID::Joypad::GroupID::Axis) value >>= 8;
144 if(device.isJoypad() && item.group == HID::Joypad::GroupID::Hat) value = (value < 0 ? -1 : value > 0 ? + 1 : 0);
145 if(device.isMouse() && input.acquired() == false) value = 0;
146 result += value;
147 }
148
149 return result;
150 }
151
152 //
153
154 bool RumbleInput::bind(HID::Device& device, unsigned group, unsigned input, int16_t oldValue, int16_t newValue) {
155 if(device.isNull() || (device.isKeyboard() && device.group[group].input[input].name == "Escape")) {
156 inputList.reset();
157 mapping = "None";
158 return true;
159 }
160
161 string encode = {hex(device.id), "/Rumble"};
162
163 if(device.isJoypad() && group == HID::Joypad::GroupID::Button) {
164 if(newValue != 0) return append(encode);
165 }
166
167 return false;
168 }
169
170 void RumbleInput::rumble(bool enable) {
171 if(program->focused() == false) return;
172 if(inputList.size() == 0) return;
173
174 for(auto& item : inputList) {
175 input.rumble(item.id, enable);
176 }
177 }
178
179 //
180
181 HotkeyInput::HotkeyInput() {
182 logic = 1; //AND
183 inputManager->hotkeyMap.append(this);
184 }
185
186 //
187
188 //convert an input mapping string to a more human-readable form for the UI
189 string InputManager::sanitize(string mapping, string concatenate) const {
190 lstring values = mapping.split(",");
191 for(auto& value : values) {
192 lstring part = value.split("/");
193 if(part.size() < 2) continue; //skip "None" mapping
194 if(part[0] == "1") part[0] = "Keyboard";
195 else if(part[0] == "2") part[0] = "Mouse";
196 else part[0] = {"Joypad(", part[0].slice(0, 3), ")"};
197 value = part.merge(".");
198 }
199 return values.merge(concatenate);
200 }
201
202 void InputManager::onChange(HID::Device& device, unsigned group, unsigned input, int16_t oldValue, int16_t newValue) {
203 if(settings->focused()) {
204 inputSettings->inputEvent(device, group, input, oldValue, newValue);
205 hotkeySettings->inputEvent(device, group, input, oldValue, newValue);
206 }
207 }
208
209 HID::Device* InputManager::findMouse() {
210 for(auto device : devices) {
211 if(device->isMouse()) return device;
212 }
213 return nullptr;
214 }
215
216 void InputManager::bind() {
217 for(auto& input : inputMap) input->bind();
218 for(auto& input : hotkeyMap) input->bind();
219 }
220
221 void InputManager::poll() {
222 auto devices = input.poll();
223 bool changed = devices.size() != this->devices.size();
224 if(changed == false) {
225 for(unsigned n = 0; n < devices.size(); n++) {
226 changed = devices[n] != this->devices[n];
227 if(changed) break;
228 }
229 }
230 if(changed == true) {
231 this->devices = devices;
232 bind();
233 }
234
235 if(presentation->focused()) pollHotkeys();
236 }
237
238 void InputManager::saveConfiguration() {
239 config.save(program->path("input.bml"));
240 }
241
242 InputManager::InputManager() {
243 inputManager = this;
244 bootstrap();
245
246 input.onChange = {&InputManager::onChange, this};
247 }
248
249 void InputManager::bootstrap() {
250 unsigned guid = 0;
251 for(auto& emulator : program->emulator) {
252 Configuration::Node emulatorNode;
253
254 for(auto& port : emulator->port) {
255 Configuration::Node portNode;
256
257 for(auto& device : port.device) {
258 Configuration::Node deviceNode;
259
260 for(auto& number : device.order) {
261 auto& input = device.input[number];
262
263 AbstractInput* abstract = nullptr;
264 if(input.type == 0) abstract = new DigitalInput;
265 if(input.type == 1) abstract = new RelativeInput;
266 if(input.type == 2) abstract = new RumbleInput;
267 if(abstract == nullptr) continue;
268
269 abstract->name = string{input.name}.replace(" ", "");
270 abstract->mapping = "None";
271 abstract->logic = 0; //OR
272
273 input.guid = guid++;
274 inputMap.append(abstract);
275
276 deviceNode.append(abstract->mapping, abstract->name);
277 }
278
279 portNode.append(deviceNode, string{device.name}.replace(" ", ""));
280 }
281
282 emulatorNode.append(portNode, string{port.name}.replace(" ", ""));
283 }
284
285 config.append(emulatorNode, string{emulator->information.name}.replace(" ", ""));
286 }
287
288 appendHotkeys();
289
290 config.load(program->path("input.bml"));
291 config.save(program->path("input.bml"));
292
293 bind();
294 }