forked from Aloshi/EmulationStation
-
Notifications
You must be signed in to change notification settings - Fork 347
/
Copy pathInputManager.cpp
506 lines (420 loc) · 14.1 KB
/
InputManager.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
#include "InputManager.h"
#include "utils/FileSystemUtil.h"
#include "CECInput.h"
#include "Log.h"
#include "platform.h"
#include "Scripting.h"
#include "Window.h"
#include <pugixml.hpp>
#include <SDL.h>
#include <iostream>
#include <assert.h>
#define KEYBOARD_GUID_STRING "-1"
#define CEC_GUID_STRING "-2"
// SO HEY POTENTIAL POOR SAP WHO IS TRYING TO MAKE SENSE OF ALL THIS (by which I mean my future self)
// There are like four distinct IDs used for joysticks (crazy, right?)
// 1. Device index - this is the "lowest level" identifier, and is just the Nth joystick plugged in to the system (like /dev/js#).
// It can change even if the device is the same, and is only used to open joysticks (required to receive SDL events).
// 2. SDL_JoystickID - this is an ID for each joystick that is supposed to remain consistent between plugging and unplugging.
// ES doesn't care if it does, though.
// 3. "Device ID" - this is something I made up and is what InputConfig's getDeviceID() returns.
// This is actually just an SDL_JoystickID (also called instance ID), but -1 means "keyboard" instead of "error."
// 4. Joystick GUID - this is some squashed version of joystick vendor, version, and a bunch of other device-specific things.
// It should remain the same across runs of the program/system restarts/device reordering and is what I use to identify which joystick to load.
// hack for cec support
int SDL_USER_CECBUTTONDOWN = -1;
int SDL_USER_CECBUTTONUP = -1;
InputManager* InputManager::mInstance = NULL;
InputManager::InputManager() : mKeyboardInputConfig(NULL)
{
}
InputManager::~InputManager()
{
deinit();
}
InputManager* InputManager::getInstance()
{
if(!mInstance)
mInstance = new InputManager();
return mInstance;
}
void InputManager::init()
{
if(initialized())
deinit();
SDL_SetHint(SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS,
Settings::getInstance()->getBool("BackgroundJoystickInput") ? "1" : "0");
// Don't enable the HIDAPI drivers by default, it will break the existing configurations
// for a few controller types, since the names and the input mappings are different.
#if !defined(_WIN32)
#if SDL_VERSION_ATLEAST(2,0,9)
SDL_SetHint(SDL_HINT_JOYSTICK_HIDAPI, "0");
#endif
#endif
SDL_InitSubSystem(SDL_INIT_JOYSTICK);
SDL_JoystickEventState(SDL_ENABLE);
// first, open all currently present joysticks
int numJoysticks = SDL_NumJoysticks();
for(int i = 0; i < numJoysticks; i++)
{
addJoystickByDeviceIndex(i);
}
mKeyboardInputConfig = new InputConfig(DEVICE_KEYBOARD, "Keyboard", KEYBOARD_GUID_STRING);
loadInputConfig(mKeyboardInputConfig);
SDL_USER_CECBUTTONDOWN = SDL_RegisterEvents(2);
SDL_USER_CECBUTTONUP = SDL_USER_CECBUTTONDOWN + 1;
CECInput::init();
mCECInputConfig = new InputConfig(DEVICE_CEC, "CEC", CEC_GUID_STRING);
loadInputConfig(mCECInputConfig);
}
void InputManager::addJoystickByDeviceIndex(int id)
{
assert(id > -1);
assert(id < SDL_NumJoysticks());
// open joystick & add to our list
SDL_Joystick* joy = SDL_JoystickOpen(id);
assert(joy);
// add it to our list so we can close it again later
SDL_JoystickID joyId = SDL_JoystickInstanceID(joy);
mJoysticks[joyId] = joy;
char guid[65];
SDL_JoystickGetGUIDString(SDL_JoystickGetGUID(joy), guid, 65);
// create the InputConfig
mInputConfigs[joyId] = new InputConfig(joyId, SDL_JoystickName(joy), guid);
// add Vendor and Product IDs
mInputConfigs[joyId]->setVendorId(SDL_JoystickGetVendor(joy));
mInputConfigs[joyId]->setProductId(SDL_JoystickGetProduct(joy));
if(!loadInputConfig(mInputConfigs[joyId]))
{
LOG(LogInfo) << "Added unconfigured joystick '" << SDL_JoystickName(joy) << "' (GUID: " << guid << ", instance ID: " << joyId << ", device index: " << id << ").";
}else{
LOG(LogInfo) << "Added known joystick '" << SDL_JoystickName(joy) << "' (instance ID: " << joyId << ", device index: " << id << ")";
}
// set up the prevAxisValues
int numAxes = SDL_JoystickNumAxes(joy);
mPrevAxisValues[joyId] = new int[numAxes];
std::fill(mPrevAxisValues[joyId], mPrevAxisValues[joyId] + numAxes, 0); //initialize array to 0
}
void InputManager::removeJoystickByJoystickID(SDL_JoystickID joyId)
{
assert(joyId != -1);
// delete old prevAxisValues
auto axisIt = mPrevAxisValues.find(joyId);
delete[] axisIt->second;
mPrevAxisValues.erase(axisIt);
// delete old InputConfig
auto it = mInputConfigs.find(joyId);
delete it->second;
mInputConfigs.erase(it);
// close the joystick
auto joyIt = mJoysticks.find(joyId);
LOG(LogInfo) << "Removed joystick '" << SDL_JoystickName(joyIt->second) << "' (instance ID: " << joyId << ")";
SDL_JoystickClose(joyIt->second);
mJoysticks.erase(joyIt);
}
void InputManager::deinit()
{
if(!initialized())
return;
for(auto iter = mJoysticks.cbegin(); iter != mJoysticks.cend(); iter++)
{
SDL_JoystickClose(iter->second);
}
mJoysticks.clear();
for(auto iter = mInputConfigs.cbegin(); iter != mInputConfigs.cend(); iter++)
{
delete iter->second;
}
mInputConfigs.clear();
for(auto iter = mPrevAxisValues.cbegin(); iter != mPrevAxisValues.cend(); iter++)
{
delete[] iter->second;
}
mPrevAxisValues.clear();
if(mKeyboardInputConfig != NULL)
{
delete mKeyboardInputConfig;
mKeyboardInputConfig = NULL;
}
if(mCECInputConfig != NULL)
{
delete mCECInputConfig;
mCECInputConfig = NULL;
}
CECInput::deinit();
SDL_JoystickEventState(SDL_DISABLE);
SDL_QuitSubSystem(SDL_INIT_JOYSTICK);
}
int InputManager::getNumJoysticks() { return (int)mJoysticks.size(); }
int InputManager::getAxisCountByDevice(SDL_JoystickID id)
{
return SDL_JoystickNumAxes(mJoysticks[id]);
}
int InputManager::getButtonCountByDevice(SDL_JoystickID id)
{
if(id == DEVICE_KEYBOARD)
return 120; //it's a lot, okay.
else if(id == DEVICE_CEC)
#ifdef HAVE_CECLIB
return CEC::CEC_USER_CONTROL_CODE_MAX;
#else // HAVE_LIBCEF
return 0;
#endif // HAVE_CECLIB
else
return SDL_JoystickNumButtons(mJoysticks[id]);
}
InputConfig* InputManager::getInputConfigByDevice(int device)
{
if(device == DEVICE_KEYBOARD)
return mKeyboardInputConfig;
else if(device == DEVICE_CEC)
return mCECInputConfig;
else
return mInputConfigs[device];
}
bool InputManager::parseEvent(const SDL_Event& ev, Window* window)
{
bool causedEvent = false;
switch(ev.type)
{
case SDL_JOYAXISMOTION:
//if it switched boundaries
if((abs(ev.jaxis.value) > DEADZONE) != (abs(mPrevAxisValues[ev.jaxis.which][ev.jaxis.axis]) > DEADZONE))
{
int normValue;
if(abs(ev.jaxis.value) <= DEADZONE)
normValue = 0;
else
if(ev.jaxis.value > 0)
normValue = 1;
else
normValue = -1;
window->input(getInputConfigByDevice(ev.jaxis.which), Input(ev.jaxis.which, TYPE_AXIS, ev.jaxis.axis, normValue, false));
causedEvent = true;
}
mPrevAxisValues[ev.jaxis.which][ev.jaxis.axis] = ev.jaxis.value;
return causedEvent;
case SDL_JOYBUTTONDOWN:
case SDL_JOYBUTTONUP:
window->input(getInputConfigByDevice(ev.jbutton.which), Input(ev.jbutton.which, TYPE_BUTTON, ev.jbutton.button, ev.jbutton.state == SDL_PRESSED, false));
return true;
case SDL_JOYHATMOTION:
window->input(getInputConfigByDevice(ev.jhat.which), Input(ev.jhat.which, TYPE_HAT, ev.jhat.hat, ev.jhat.value, false));
return true;
case SDL_KEYDOWN:
if(ev.key.keysym.sym == SDLK_BACKSPACE && SDL_IsTextInputActive())
{
window->textInput("\b");
}
if(ev.key.repeat)
return false;
if(ev.key.keysym.sym == SDLK_F4)
{
SDL_Event* quit = new SDL_Event();
quit->type = SDL_QUIT;
SDL_PushEvent(quit);
return false;
}
window->input(getInputConfigByDevice(DEVICE_KEYBOARD), Input(DEVICE_KEYBOARD, TYPE_KEY, ev.key.keysym.sym, 1, false));
return true;
case SDL_KEYUP:
window->input(getInputConfigByDevice(DEVICE_KEYBOARD), Input(DEVICE_KEYBOARD, TYPE_KEY, ev.key.keysym.sym, 0, false));
return true;
case SDL_TEXTINPUT:
window->textInput(ev.text.text);
break;
case SDL_JOYDEVICEADDED:
addJoystickByDeviceIndex(ev.jdevice.which); // ev.jdevice.which is a device index
return true;
case SDL_JOYDEVICEREMOVED:
removeJoystickByJoystickID(ev.jdevice.which); // ev.jdevice.which is an SDL_JoystickID (instance ID)
return false;
}
if((ev.type == (unsigned int)SDL_USER_CECBUTTONDOWN) || (ev.type == (unsigned int)SDL_USER_CECBUTTONUP))
{
window->input(getInputConfigByDevice(DEVICE_CEC), Input(DEVICE_CEC, TYPE_CEC_BUTTON, ev.user.code, ev.type == (unsigned int)SDL_USER_CECBUTTONDOWN, false));
return true;
}
return false;
}
bool InputManager::loadInputConfig(InputConfig* config)
{
std::string path = getConfigPath();
if(!Utils::FileSystem::exists(path))
return false;
pugi::xml_document doc;
pugi::xml_parse_result res = doc.load_file(path.c_str());
if(!res)
{
LOG(LogError) << "Error parsing input config: " << res.description();
return false;
}
pugi::xml_node root = doc.child("inputList");
if(!root)
return false;
pugi::xml_node configNode = root.find_child_by_attribute("inputConfig", "deviceGUID", config->getDeviceGUIDString().c_str());
if(!configNode)
configNode = root.find_child_by_attribute("inputConfig", "deviceName", config->getDeviceName().c_str());
if(!configNode)
return false;
config->loadFromXML(configNode);
return true;
}
//used in an "emergency" where no keyboard config could be loaded from the inputmanager config file
//allows the user to select to reconfigure in menus if this happens without having to delete es_input.cfg manually
void InputManager::loadDefaultKBConfig()
{
InputConfig* cfg = getInputConfigByDevice(DEVICE_KEYBOARD);
cfg->clear();
cfg->mapInput("up", Input(DEVICE_KEYBOARD, TYPE_KEY, SDLK_UP, 1, true));
cfg->mapInput("down", Input(DEVICE_KEYBOARD, TYPE_KEY, SDLK_DOWN, 1, true));
cfg->mapInput("left", Input(DEVICE_KEYBOARD, TYPE_KEY, SDLK_LEFT, 1, true));
cfg->mapInput("right", Input(DEVICE_KEYBOARD, TYPE_KEY, SDLK_RIGHT, 1, true));
cfg->mapInput("a", Input(DEVICE_KEYBOARD, TYPE_KEY, SDLK_RETURN, 1, true));
cfg->mapInput("b", Input(DEVICE_KEYBOARD, TYPE_KEY, SDLK_ESCAPE, 1, true));
cfg->mapInput("start", Input(DEVICE_KEYBOARD, TYPE_KEY, SDLK_F1, 1, true));
cfg->mapInput("select", Input(DEVICE_KEYBOARD, TYPE_KEY, SDLK_F2, 1, true));
cfg->mapInput("leftshoulder", Input(DEVICE_KEYBOARD, TYPE_KEY, SDLK_LEFTBRACKET, 1, true));
cfg->mapInput("rightshoulder", Input(DEVICE_KEYBOARD, TYPE_KEY, SDLK_RIGHTBRACKET, 1, true));
}
void InputManager::writeDeviceConfig(InputConfig* config)
{
assert(initialized());
std::string path = getConfigPath();
pugi::xml_document doc;
if(Utils::FileSystem::exists(path))
{
// merge files
pugi::xml_parse_result result = doc.load_file(path.c_str());
if(!result)
{
LOG(LogError) << "Error parsing input config: " << result.description();
}
else
{
// successfully loaded, delete the old entry if it exists
pugi::xml_node root = doc.child("inputList");
if(root)
{
// if inputAction @type=onfinish is set, let onfinish command take care for creating input configuration.
// we just put the input configuration into a temporary input config file.
pugi::xml_node actionnode = root.find_child_by_attribute("inputAction", "type", "onfinish");
if(actionnode)
{
path = getTemporaryConfigPath();
doc.reset();
root = doc.append_child("inputList");
}
else
{
pugi::xml_node oldEntry = root.find_child_by_attribute("inputConfig", "deviceGUID",
config->getDeviceGUIDString().c_str());
if(oldEntry)
{
root.remove_child(oldEntry);
}
oldEntry = root.find_child_by_attribute("inputConfig", "deviceName",
config->getDeviceName().c_str());
if(oldEntry)
{
root.remove_child(oldEntry);
}
}
}
}
}
pugi::xml_node root = doc.child("inputList");
if(!root)
root = doc.append_child("inputList");
config->writeToXML(root);
doc.save_file(path.c_str());
Scripting::fireEvent("config-changed");
Scripting::fireEvent("controls-changed");
// execute any onFinish commands and re-load the config for changes
doOnFinish();
loadInputConfig(config);
}
void InputManager::doOnFinish()
{
assert(initialized());
std::string path = getConfigPath();
pugi::xml_document doc;
if(Utils::FileSystem::exists(path))
{
pugi::xml_parse_result result = doc.load_file(path.c_str());
if(!result)
{
LOG(LogError) << "Error parsing input config: " << result.description();
}
else
{
pugi::xml_node root = doc.child("inputList");
if(root)
{
root = root.find_child_by_attribute("inputAction", "type", "onfinish");
if(root)
{
for(pugi::xml_node command = root.child("command"); command;
command = command.next_sibling("command"))
{
std::string tocall = command.text().get();
LOG(LogInfo) << " " << tocall;
std::cout << "==============================================\ninput config finish command:\n";
int exitCode = runSystemCommand(tocall);
std::cout << "==============================================\n";
if(exitCode != 0)
{
LOG(LogWarning) << "...launch terminated with nonzero exit code " << exitCode << "!";
}
}
}
}
}
}
}
std::string InputManager::getConfigPath()
{
std::string path = Utils::FileSystem::getHomePath();
path += "/.emulationstation/es_input.cfg";
return path;
}
std::string InputManager::getTemporaryConfigPath()
{
std::string path = Utils::FileSystem::getHomePath();
path += "/.emulationstation/es_temporaryinput.cfg";
return path;
}
bool InputManager::initialized() const
{
return mKeyboardInputConfig != NULL;
}
int InputManager::getNumConfiguredDevices()
{
int num = 0;
for(auto it = mInputConfigs.cbegin(); it != mInputConfigs.cend(); it++)
{
if(it->second->isConfigured())
num++;
}
if(mKeyboardInputConfig->isConfigured())
num++;
if(mCECInputConfig->isConfigured())
num++;
return num;
}
std::string InputManager::getDeviceGUIDString(int deviceId)
{
if(deviceId == DEVICE_KEYBOARD)
return KEYBOARD_GUID_STRING;
if(deviceId == DEVICE_CEC)
return CEC_GUID_STRING;
auto it = mJoysticks.find(deviceId);
if(it == mJoysticks.cend())
{
LOG(LogError) << "getDeviceGUIDString - deviceId " << deviceId << " not found!";
return "something went horribly wrong";
}
char guid[65];
SDL_JoystickGetGUIDString(SDL_JoystickGetGUID(it->second), guid, 65);
return std::string(guid);
}