From 3cff588efff44a74aca28e00d718112851a5a0e5 Mon Sep 17 00:00:00 2001 From: Gregory Hainaut Date: Sun, 16 Apr 2017 17:41:05 +0200 Subject: [PATCH] onepad: add an unique identifier --- plugins/onepad/GamePad.cpp | 23 +++++++++++++++++++++++ plugins/onepad/GamePad.h | 5 +++++ plugins/onepad/SDL/joystick.cpp | 8 ++++++++ plugins/onepad/SDL/joystick.h | 3 +++ 4 files changed, 39 insertions(+) diff --git a/plugins/onepad/GamePad.cpp b/plugins/onepad/GamePad.cpp index 08ebb92b60..21aa77c0a6 100644 --- a/plugins/onepad/GamePad.cpp +++ b/plugins/onepad/GamePad.cpp @@ -35,3 +35,26 @@ void GamePad::DoRumble(unsigned type, unsigned pad) s_vgamePad[id]->Rumble(type, pad); } } + +size_t GamePad::index_to_uid(int index) +{ + if ((index >= 0) && (index < (int)s_vgamePad.size())) + return s_vgamePad[index]->GetUniqueIdentifier(); + else + return 0; +} + +int GamePad::uid_to_index(size_t uid) +{ + for (int i = 0; i < (int)s_vgamePad.size(); ++i) { + if (s_vgamePad[i]->GetUniqueIdentifier() == uid) + return i; + } + + // Current uid wasn't found maybe the pad was unplugged + // Fallback to the first pad which more friendly than nothing + if (!s_vgamePad.empty()) + return 0; + + return -1; +} diff --git a/plugins/onepad/GamePad.h b/plugins/onepad/GamePad.h index 353582a4f8..4b37dca9eb 100644 --- a/plugins/onepad/GamePad.h +++ b/plugins/onepad/GamePad.h @@ -57,6 +57,11 @@ public: return m_deadzone; } + virtual size_t GetUniqueIdentifier() = 0; + + static size_t index_to_uid(int index); + static int uid_to_index(size_t uid); + bool IsProperlyInitialized() { return m_no_error; diff --git a/plugins/onepad/SDL/joystick.cpp b/plugins/onepad/SDL/joystick.cpp index 40955fe817..5c649739e4 100644 --- a/plugins/onepad/SDL/joystick.cpp +++ b/plugins/onepad/SDL/joystick.cpp @@ -151,6 +151,9 @@ JoystickInfo::JoystickInfo(int id) return; } + std::hash hash_me; + m_unique_id = hash_me(std::string(guid)); + // Default haptic effect SDL_HapticEffect effects[NB_EFFECT]; for (int i = 0; i < NB_EFFECT; i++) { @@ -207,6 +210,11 @@ const char *JoystickInfo::GetName() return SDL_JoystickName(SDL_GameControllerGetJoystick(m_controller)); } +size_t JoystickInfo::GetUniqueIdentifier() +{ + return m_unique_id; +} + bool JoystickInfo::TestForce(float strength = 0.60) { // This code just use standard rumble to check that SDL handles the pad correctly! --3kinox diff --git a/plugins/onepad/SDL/joystick.h b/plugins/onepad/SDL/joystick.h index 1fce625c9b..bface1b00e 100644 --- a/plugins/onepad/SDL/joystick.h +++ b/plugins/onepad/SDL/joystick.h @@ -52,8 +52,11 @@ public: virtual void UpdateGamePadState(); + virtual size_t GetUniqueIdentifier(); + private: SDL_GameController *m_controller; SDL_Haptic *m_haptic; std::array m_effects_id; + size_t m_unique_id; };