2015-10-14 13:05:02 +00:00
|
|
|
#include "GamePad.h"
|
2016-09-04 12:10:02 +00:00
|
|
|
#ifdef SDL_BUILD
|
2015-10-14 13:05:02 +00:00
|
|
|
#include "SDL/joystick.h"
|
|
|
|
#endif
|
|
|
|
|
2017-04-10 16:19:23 +00:00
|
|
|
std::vector<std::unique_ptr<GamePad>> s_vgamePad;
|
|
|
|
|
2015-10-14 13:05:02 +00:00
|
|
|
/**
|
|
|
|
* Following static methods are just forwarders to their backend
|
|
|
|
* This is where link between agnostic and specific code is done
|
|
|
|
**/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Find every interesting devices and create right structure for them(depend on backend)
|
|
|
|
**/
|
2017-04-10 16:19:23 +00:00
|
|
|
void GamePad::EnumerateGamePads(std::vector<std::unique_ptr<GamePad>> &vgamePad)
|
2015-10-14 13:05:02 +00:00
|
|
|
{
|
2016-09-04 12:10:02 +00:00
|
|
|
#ifdef SDL_BUILD
|
|
|
|
JoystickInfo::EnumerateJoysticks(vgamePad);
|
2015-10-14 13:05:02 +00:00
|
|
|
#endif
|
|
|
|
}
|
2015-11-10 07:12:20 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Safely dispatch to the Rumble method above
|
|
|
|
**/
|
2017-04-14 18:14:42 +00:00
|
|
|
void GamePad::DoRumble(unsigned type, unsigned pad)
|
2015-11-10 07:12:20 +00:00
|
|
|
{
|
2017-08-09 19:23:06 +00:00
|
|
|
int index = uid_to_index(pad);
|
2017-04-16 15:49:05 +00:00
|
|
|
if (index >= 0)
|
|
|
|
s_vgamePad[index]->Rumble(type, pad);
|
2015-11-10 07:12:20 +00:00
|
|
|
}
|
2017-04-16 15:41:05 +00:00
|
|
|
|
|
|
|
size_t GamePad::index_to_uid(int index)
|
|
|
|
{
|
|
|
|
if ((index >= 0) && (index < (int)s_vgamePad.size()))
|
|
|
|
return s_vgamePad[index]->GetUniqueIdentifier();
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-08-09 19:23:06 +00:00
|
|
|
int GamePad::uid_to_index(int pad)
|
2017-04-16 15:41:05 +00:00
|
|
|
{
|
2017-08-09 19:23:06 +00:00
|
|
|
size_t uid = g_conf.get_joy_uid(pad);
|
|
|
|
|
2017-04-16 15:41:05 +00:00
|
|
|
for (int i = 0; i < (int)s_vgamePad.size(); ++i) {
|
|
|
|
if (s_vgamePad[i]->GetUniqueIdentifier() == uid)
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
2017-08-09 19:23:06 +00:00
|
|
|
// Current uid wasn't found maybe the pad was unplugged. Or
|
|
|
|
// user didn't select it. Fallback to 1st pad for
|
|
|
|
// 1st player. And 2nd pad for 2nd player.
|
|
|
|
if ((int)s_vgamePad.size() > pad)
|
|
|
|
return pad;
|
2017-04-16 15:41:05 +00:00
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|