Repository navigation
Expand file tree
/
Copy pathdevice_manager.cpp
More file actions
643 lines (550 loc) · 20.9 KB
/
Copy pathdevice_manager.cpp
File metadata and controls
643 lines (550 loc) · 20.9 KB
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
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
//
// SuperTuxKart - a fun racing game with go-kart
// Copyright (C) 2010-2015 SuperTuxKart-Team
//
// 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, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include "input/device_manager.hpp"
#include <iostream>
#include <fstream>
#include "config/user_config.hpp"
#include "graphics/irr_driver.hpp"
#include "input/gamepad_device.hpp"
#include "input/keyboard_device.hpp"
#include "input/multitouch_device.hpp"
#include "input/wiimote_manager.hpp"
#include "io/file_manager.hpp"
#include "states_screens/kart_selection.hpp"
#include "states_screens/state_manager.hpp"
#include "utils/file_utils.hpp"
#include "utils/log.hpp"
#include "utils/string_utils.hpp"
#include "utils/translation.hpp"
#include <IrrlichtDevice.h>
#define INPUT_MODE_DEBUG 0
static const char INPUT_FILE_NAME[] = "input.xml";
static const int INPUT_FILE_VERSION = 1;
DeviceManager::DeviceManager()
{
m_latest_used_device = NULL;
m_assign_mode = NO_ASSIGN;
m_single_player = NULL;
m_multitouch_device = NULL;
} // DeviceManager
// -----------------------------------------------------------------------------
DeviceManager::~DeviceManager()
{
delete m_multitouch_device;
} // ~DeviceManager
// -----------------------------------------------------------------------------
bool DeviceManager::initialize()
{
m_map_fire_to_select = false;
bool created = false;
// Shutdown in case the device manager is being re-initialized
shutdown();
if(UserConfigParams::logMisc())
{
Log::info("Device manager","Initializing Device Manager");
Log::info("-","---------------------------");
}
load();
// Assign a configuration to the keyboard, or create one if we haven't yet
if(UserConfigParams::logMisc()) Log::info("Device manager","Initializing keyboard support.");
if (m_keyboard_configs.size() == 0)
{
if(UserConfigParams::logMisc())
Log::info("Device manager","No keyboard configuration exists, creating one.");
m_keyboard_configs.push_back(new KeyboardConfig());
created = true;
}
const int keyboard_amount = m_keyboard_configs.size();
for (int n = 0; n < keyboard_amount; n++)
{
m_keyboards.push_back(new KeyboardDevice(m_keyboard_configs.get(n)));
}
if ((UserConfigParams::m_multitouch_active == 1 &&
irr_driver->getDevice()->supportsTouchDevice()) ||
UserConfigParams::m_multitouch_active > 1)
{
m_multitouch_device = new MultitouchDevice();
}
if (created) save();
return created;
} // initialize
// -----------------------------------------------------------------------------
void DeviceManager::clearKeyboard()
{
m_keyboards.clearAndDeleteAll();
} // clearKeyboard
// -----------------------------------------------------------------------------
void DeviceManager::clearGamepads()
{
m_gamepads.clearAndDeleteAll();
} // clearGamepads
// -----------------------------------------------------------------------------
void DeviceManager::clearMultitouchDevices()
{
delete m_multitouch_device;
m_multitouch_device = NULL;
} // clearMultitouchDevices
// -----------------------------------------------------------------------------
void DeviceManager::setAssignMode(const PlayerAssignMode assignMode)
{
m_assign_mode = assignMode;
#if INPUT_MODE_DEBUG
if (assignMode == NO_ASSIGN) Log::info("DeviceManager::setAssignMode(NO_ASSIGN)");
if (assignMode == ASSIGN) Log::info("DeviceManager::setAssignMode(ASSIGN)");
if (assignMode == DETECT_NEW) Log::info("DeviceManager::setAssignMode(DETECT_NEW)");
#endif
// when going back to no-assign mode, do some cleanup
if (assignMode == NO_ASSIGN)
{
for (unsigned int i=0; i < m_gamepads.size(); i++)
{
m_gamepads[i].setPlayer(NULL);
}
for (unsigned int i=0; i < m_keyboards.size(); i++)
{
m_keyboards[i].setPlayer(NULL);
}
if (m_multitouch_device != NULL)
m_multitouch_device->setPlayer(NULL);
}
} // setAssignMode
// -----------------------------------------------------------------------------
GamePadDevice* DeviceManager::getGamePadFromIrrID(const int id)
{
const int count = m_gamepads.size();
for (int i = 0; i < count; i++)
{
if (m_gamepads[i].getIrrIndex()== id)
{
return m_gamepads.get(i);
}
}
return NULL;
} // getGamePadFromIrrID
// -----------------------------------------------------------------------------
/**
* Check if we already have a config object for gamepad 'irr_id' as reported by
* irrLicht, If no, create one. Returns true if new configuration was created,
* otherwise false.
*/
bool DeviceManager::getConfigForGamepad(const int irr_id,
const std::string& name,
GamepadConfig **config)
{
bool found = false;
bool configCreated = false;
// Find appropriate configuration
for(unsigned int n=0; n < m_gamepad_configs.size(); n++)
{
if(m_gamepad_configs[n].getName() == name)
{
*config = m_gamepad_configs.get(n);
found = true;
}
}
// If we can't find an appropriate configuration then create one.
if (!found)
{
// The Wiimote manager and SDL controller will set number of buttons and
// axes
*config = new GamepadConfig(name.c_str());
// Add new config to list
m_gamepad_configs.push_back(*config);
configCreated = true;
}
return configCreated;
} // getConfigForGamepad
// -----------------------------------------------------------------------------
void DeviceManager::addKeyboard(KeyboardDevice* d)
{
m_keyboards.push_back(d);
} // addKeyboard
// -----------------------------------------------------------------------------
void DeviceManager::addEmptyKeyboard()
{
KeyboardConfig* newConf = new KeyboardConfig();
m_keyboard_configs.push_back(newConf);
m_keyboards.push_back( new KeyboardDevice(newConf) );
} // addEmptyKeyboard
// -----------------------------------------------------------------------------
void DeviceManager::addGamepad(GamePadDevice* d)
{
m_gamepads.push_back(d);
} // addGamepad
// -----------------------------------------------------------------------------
bool DeviceManager::deleteConfig(DeviceConfig* config)
{
for (unsigned int n=0; n<m_keyboards.size(); n++)
{
if (m_keyboards[n].getConfiguration() == config)
{
m_keyboards.erase(n);
n--;
}
}
for (unsigned int n=0; n<m_gamepads.size(); n++)
{
if (m_gamepads[n].getConfiguration() == config)
{
m_gamepads.erase(n);
n--;
}
}
if (m_keyboard_configs.erase(config))
{
return true;
}
if (m_gamepad_configs.erase(config))
{
return true;
}
return false;
} // deleteConfig
// -----------------------------------------------------------------------------
/** Helper method, only used internally. Takes care of analyzing keyboard input.
* \param[in] button_id Id of the key pressed.
* \param[in] mode Used to determine whether to determine menu actions
* or game actions
* \param[out] player Which player this input belongs to (only set in
* ASSIGN mode).
* \param[out] action Which action is related to this input trigger.
* \return The device to which this input belongs
*/
InputDevice* DeviceManager::mapKeyboardInput(int button_id,
InputManager::InputDriverMode mode,
StateManager::ActivePlayer **player,
PlayerAction *action /* out */)
{
const int keyboard_amount = m_keyboards.size();
for (int n=0; n<keyboard_amount; n++)
{
KeyboardDevice *keyboard = m_keyboards.get(n);
if (keyboard->processAndMapInput(Input::IT_KEYBOARD, button_id, mode, action))
{
if (m_single_player != NULL)
{
*player = m_single_player;
}
else if (m_assign_mode == NO_ASSIGN) // Don't set the player in NO_ASSIGN mode
{
*player = NULL;
}
else
{
*player = keyboard->getPlayer();
}
return keyboard;
}
}
return NULL; // no appropriate binding found
} // mapKeyboardInput
//-----------------------------------------------------------------------------
/** Helper method, only used internally. Takes care of analyzing gamepad
* input.
* \param[in] type Type of gamepad event (IT_STICKMOTION etc).
* \param[out] player Which player this input belongs to (only set in
* ASSIGN mode)
* \param[out] action Which action is related to this input trigger
* \param mode Used to determine whether to determine menu actions
* or game actions
* \return The device to which this input belongs
*/
InputDevice *DeviceManager::mapGamepadInput(Input::InputType type,
int device_id,
int button_id,
int axis_dir,
int *value /* inout */,
InputManager::InputDriverMode mode,
StateManager::ActivePlayer **player /* out */,
PlayerAction *action /* out */)
{
GamePadDevice *gPad = getGamePadFromIrrID(device_id);
if (gPad != NULL)
{
// Ignore deadzone events if this isn't the latest used device in
// single-player mode (allowing the player to switch device at any time)
int dz = static_cast<GamepadConfig*>(gPad->getConfiguration())->getDeadzone();
if (m_single_player != NULL && m_latest_used_device != gPad
&& *value > -dz && *value < dz)
{
*player = NULL;
return NULL;
}
if (gPad->processAndMapInput(type, button_id, mode, action, value))
{
if (m_single_player != NULL)
{
*player = m_single_player;
// in single-player mode, assign the gamepad as needed
if (gPad->getPlayer() != m_single_player) gPad->setPlayer(m_single_player);
}
else if (m_assign_mode == NO_ASSIGN) // Don't set the player in NO_ASSIGN mode
{
*player = NULL;
}
else
{
*player = gPad->getPlayer();
}
}
else gPad = NULL; // If no bind was found, return NULL
}
return gPad;
} // mapGamepadInput
//-----------------------------------------------------------------------------
void DeviceManager::updateMultitouchDevice()
{
if (m_multitouch_device == NULL)
return;
if (m_single_player != NULL)
{
// in single-player mode, assign the gamepad as needed
if (m_multitouch_device->getPlayer() != m_single_player)
m_multitouch_device->setPlayer(m_single_player);
}
else
{
m_multitouch_device->setPlayer(NULL);
}
m_multitouch_device->updateController();
} // updateMultitouchDevice
//-----------------------------------------------------------------------------
bool DeviceManager::translateInput( Input::InputType type,
int device_id,
int button_id,
int axis_dir,
int* value /* inout */,
InputManager::InputDriverMode mode,
StateManager::ActivePlayer** player /* out */,
PlayerAction* action /* out */ )
{
if (GUIEngine::getCurrentScreen() != NULL)
{
GUIEngine::getCurrentScreen()->filterInput(type, device_id, button_id, axis_dir, *value);
}
InputDevice *device = NULL;
// If the input event matches a bind on an input device, get a pointer to the device
switch (type)
{
case Input::IT_KEYBOARD:
device = mapKeyboardInput(button_id, mode, player, action);
// If the action is not recognised, check if it's a fire key
// that should be mapped to select
if(!device && m_map_fire_to_select)
{
PlayerAction fire_action = PA_BEFORE_FIRST;
device = mapKeyboardInput(button_id, InputManager::INGAME, player,
&fire_action);
if (device && fire_action == PA_FIRE)
*action = PA_MENU_SELECT;
else
device = NULL;
}
break;
case Input::IT_STICKBUTTON:
case Input::IT_STICKMOTION:
device = mapGamepadInput(type, device_id, button_id, axis_dir, value,
mode, player, action);
if(!device && m_map_fire_to_select)
{
PlayerAction fire_action = PA_BEFORE_FIRST;
device = mapGamepadInput(type, device_id, button_id, axis_dir, value,
InputManager::INGAME, player, &fire_action);
if (device && fire_action == PA_FIRE)
*action = PA_MENU_SELECT;
else
device = NULL;
}
break;
default:
break;
};
// Return true if input was successfully translated to an action and player
if (device != NULL && abs(*value) > Input::MAX_VALUE/2)
{
m_latest_used_device = device;
}
return (device != NULL);
} // translateInput
//-----------------------------------------------------------------------------
InputDevice* DeviceManager::getLatestUsedDevice()
{
// If none, probably the user clicked or used enter; give keyboard by default
if (m_latest_used_device == NULL)
{
//Log::info("DeviceManager", "No latest device, returning keyboard);
return m_keyboards.get(0); // FIXME: is this right?
}
return m_latest_used_device;
} // getLatestUsedDevice
// -----------------------------------------------------------------------------
void DeviceManager::clearLatestUsedDevice()
{
m_latest_used_device = NULL;
} // clearLatestUsedDevice
// -----------------------------------------------------------------------------
/** Loads the configuration from the user's input.xml file.
*/
bool DeviceManager::load()
{
std::string filepath = file_manager->getUserConfigFile(INPUT_FILE_NAME);
if(UserConfigParams::logMisc())
Log::info("Device manager","Loading input.xml...");
const XMLNode *input = file_manager->createXMLTree(filepath);
if(!input)
{
if(UserConfigParams::logMisc())
Log::warn("Device manager","No configuration file exists.");
return false;
}
if(input->getName()!="input")
{
Log::warn("DeviceManager", "Invalid input.xml file - no input node.");
delete input;
return false;
}
int version=0;
if(!input->get("version", &version) || version != INPUT_FILE_VERSION )
{
//I18N: shown when config file is too old
GUIEngine::showMessage(_("Please re-configure your key bindings."));
GUIEngine::showMessage(_("Your input config file is not compatible "
"with this version of STK."));
delete input;
return false;
}
for(unsigned int i=0; i<input->getNumNodes(); i++)
{
const XMLNode *config = input->getNode(i);
DeviceConfig *device_config = DeviceConfig::create(config);
if(!device_config)
{
Log::warn("DeviceManager",
"Invalid node '%s' in input.xml - ignored.",
config->getName().c_str());
continue;
}
if (config->getName() == "keyboard")
{
KeyboardConfig *kc = static_cast<KeyboardConfig*>(device_config);
m_keyboard_configs.push_back(kc);
}
else if (config->getName() == "gamepad")
{
GamepadConfig *gc = static_cast<GamepadConfig*>(device_config);
m_gamepad_configs.push_back(gc);
}
} // for i < getNumNodes
if (UserConfigParams::logMisc())
{
Log::info("Device manager",
"Found %d keyboard and %d gamepad configurations.",
m_keyboard_configs.size(), m_gamepad_configs.size());
}
// For Debugging....
/*
for (int n = 0; n < m_keyboard_configs.size(); n++)
printf("Config #%d\n%s", n + 1, m_keyboard_configs[n].toString().c_str());
for (int n = 0; n < m_gamepad_configs.size(); n++)
printf("%s", m_gamepad_configs[n].toString().c_str());
*/
delete input;
return true;
} // load
// -----------------------------------------------------------------------------
void DeviceManager::save()
{
std::string filepath = file_manager->getUserConfigFile(INPUT_FILE_NAME);
if(UserConfigParams::logMisc()) Log::info("Device manager","Serializing input.xml...");
// Save to a new file and rename later to avoid disk space problem, see #4709
std::ofstream configfile(FileUtils::getPortableWritingPath(filepath + "new"));
if(!configfile.is_open())
{
Log::error("Device manager","Failed to open %s for writing, controls won't be saved",filepath.c_str());
return;
}
try
{
configfile << "<input version=\"" << INPUT_FILE_VERSION << "\">\n\n";
configfile << "<!--\n"
<< "Event 1 : Keyboard button press\n"
<< " 'id' indicates which button, as defined by irrlicht's EKEY_CODE enum\n"
<< "Event 2 : Gamepad stick motion\n"
<< " 'id' indicates which stick, starting from 0\n"
<< " 'direction' 0 means negative, 1 means positive\n"
<< "Event 3 : Gamepad button press\n"
<< " 'id' indicates which button, starting from 0\n"
<< "-->\n\n";
for(unsigned int n=0; n<m_keyboard_configs.size(); n++)
{
m_keyboard_configs[n].save(configfile);
}
for(unsigned int n=0; n<m_gamepad_configs.size(); n++)
{
m_gamepad_configs[n].save(configfile);
}
configfile << "</input>\n";
configfile.close();
file_manager->removeFile(filepath);
FileUtils::renameU8Path(filepath + "new", filepath);
}
catch (std::exception& e)
{
Log::error("Device manager", "Failed to write %s to %s, "
"because %s", INPUT_FILE_NAME, filepath.c_str(), e.what());
}
if(UserConfigParams::logMisc()) Log::info("Device manager","Serialization complete.");
} // save
// -----------------------------------------------------------------------------
KeyboardDevice* DeviceManager::getKeyboardFromBtnID(const int button_id)
{
const int keyboard_amount = m_keyboards.size();
for (int n=0; n<keyboard_amount; n++)
{
if (m_keyboards[n].getConfiguration()->hasBindingFor(button_id))
return m_keyboards.get(n);
}
return NULL;
} // getKeyboardFromButtonID
// -----------------------------------------------------------------------------
GamePadDevice* DeviceManager::getGamePad(const int i)
{
if (i>= 0 && i < (int)m_gamepads.size())
return m_gamepads.get(i);
else
return NULL;
}
// -----------------------------------------------------------------------------
KeyboardDevice* DeviceManager::getKeyboard(const int i)
{
if (i >= 0 && i < (int)m_keyboards.size())
return m_keyboards.get(i);
else
return NULL;
}
// -----------------------------------------------------------------------------
void DeviceManager::shutdown()
{
m_gamepads.clearAndDeleteAll();
m_keyboards.clearAndDeleteAll();
m_gamepad_configs.clearAndDeleteAll();
m_keyboard_configs.clearAndDeleteAll();
delete m_multitouch_device;
m_multitouch_device = NULL;
m_latest_used_device = NULL;
} // shutdown