From 9c7e9cc6f2b3703fc6a9e982f1ff2536d7a72897 Mon Sep 17 00:00:00 2001 From: Gregory Hainaut Date: Mon, 10 Apr 2017 18:19:23 +0200 Subject: [PATCH] onepad: use range loop and vector of unique_ptr to manage the joystick --- plugins/onepad/GamePad.cpp | 9 ++++----- plugins/onepad/GamePad.h | 6 +++--- plugins/onepad/Linux/dialog.cpp | 13 ++++++------- plugins/onepad/Linux/linux.cpp | 10 +--------- plugins/onepad/SDL/joystick.cpp | 26 ++++++-------------------- plugins/onepad/SDL/joystick.h | 10 ++++++---- plugins/onepad/onepad.h | 1 + 7 files changed, 27 insertions(+), 48 deletions(-) diff --git a/plugins/onepad/GamePad.cpp b/plugins/onepad/GamePad.cpp index 0d0b8158dc..755f2a6f6b 100644 --- a/plugins/onepad/GamePad.cpp +++ b/plugins/onepad/GamePad.cpp @@ -3,7 +3,8 @@ #include "SDL/joystick.h" #endif -vector s_vgamePad; +std::vector> s_vgamePad; + bool GamePadIdWithinBounds(int GamePadId) { return ((GamePadId >= 0) && (GamePadId < (int)s_vgamePad.size())); @@ -17,7 +18,7 @@ bool GamePadIdWithinBounds(int GamePadId) /** * Find every interesting devices and create right structure for them(depend on backend) **/ -void GamePad::EnumerateGamePads(vector &vgamePad) +void GamePad::EnumerateGamePads(std::vector> &vgamePad) { #ifdef SDL_BUILD JoystickInfo::EnumerateJoysticks(vgamePad); @@ -38,9 +39,7 @@ void GamePad::DoRumble(int type, int pad) { u32 id = conf->get_joyid(pad); if (GamePadIdWithinBounds(id)) { - GamePad *gamePad = s_vgamePad[id]; - if (gamePad) - gamePad->Rumble(type, pad); + s_vgamePad[id]->Rumble(type, pad); } } diff --git a/plugins/onepad/GamePad.h b/plugins/onepad/GamePad.h index 030b66a0b3..9cb19137fc 100644 --- a/plugins/onepad/GamePad.h +++ b/plugins/onepad/GamePad.h @@ -38,7 +38,7 @@ public: /** * Find every interesting devices and create right structure for them(depend on backend) **/ - static void EnumerateGamePads(vector &vgamePad); + static void EnumerateGamePads(std::vector> &vgamePad); static void UpdateReleaseState(); /** * Update state of every attached devices @@ -148,8 +148,8 @@ protected: int numbuttons, numaxes, numhats; int deadzone; int pad; - vector vbuttonstate, vaxisstate, vhatstate; + std::vector vbuttonstate, vaxisstate, vhatstate; }; -extern vector s_vgamePad; +extern std::vector> s_vgamePad; extern bool GamePadIdWithinBounds(int joyid); diff --git a/plugins/onepad/Linux/dialog.cpp b/plugins/onepad/Linux/dialog.cpp index 7dc220ddb8..d4b0ac4947 100644 --- a/plugins/onepad/Linux/dialog.cpp +++ b/plugins/onepad/Linux/dialog.cpp @@ -534,7 +534,6 @@ void Dialog::config_key(int pad, int key) // I don't have any guarantee that not-yet-pressed state is egual to released state GamePad::UpdateReleaseState(); while (!captured) { - vector::iterator itjoy; if (PollX11KeyboardMouseEvent(key_pressed)) { // special case for keyboard/mouse to handle multiple keys // Note: key_pressed == 0 when ESC is hit to abort the capture @@ -548,25 +547,25 @@ void Dialog::config_key(int pad, int key) } else { GamePad::UpdateGamePadState(); - itjoy = s_vgamePad.begin(); - while ((itjoy != s_vgamePad.end()) && (!captured)) { - if ((*itjoy)->PollButtons(key_pressed)) { + for (auto &j : s_vgamePad) { + if (j->PollButtons(key_pressed)) { clear_key(pad, key); set_key(pad, key, key_pressed); m_map_images[pad][key_pressed] = key; captured = true; - } else if ((*itjoy)->PollAxes(key_pressed)) { + } else if (j->PollAxes(key_pressed)) { clear_key(pad, key); set_key(pad, key, key_pressed); m_map_images[pad][key_pressed] = key; captured = true; - } else if ((*itjoy)->PollHats(key_pressed)) { + } else if (j->PollHats(key_pressed)) { clear_key(pad, key); set_key(pad, key, key_pressed); m_map_images[pad][key_pressed] = key; captured = true; } - ++itjoy; + if (captured) + break; } } } diff --git a/plugins/onepad/Linux/linux.cpp b/plugins/onepad/Linux/linux.cpp index 43f6b27820..48fd31b4cc 100644 --- a/plugins/onepad/Linux/linux.cpp +++ b/plugins/onepad/Linux/linux.cpp @@ -78,14 +78,6 @@ void _PADclose() { SetAutoRepeat(true); - vector::iterator it = s_vgamePad.begin(); - - // Delete everything in the vector vjoysticks. - while (it != s_vgamePad.end()) { - delete *it; - ++it; - } - s_vgamePad.clear(); } @@ -97,7 +89,7 @@ void PollForJoystickInput(int cpad) GamePad::UpdateGamePadState(); for (int i = 0; i < MAX_KEYS; i++) { - GamePad *gamePad = s_vgamePad[joyid]; + auto &gamePad = s_vgamePad[joyid]; switch (type_of_joykey(cpad, i)) { case PAD_JOYBUTTONS: { diff --git a/plugins/onepad/SDL/joystick.cpp b/plugins/onepad/SDL/joystick.cpp index df670ae201..789c68853c 100644 --- a/plugins/onepad/SDL/joystick.cpp +++ b/plugins/onepad/SDL/joystick.cpp @@ -30,21 +30,16 @@ static u32 s_bSDLInit = false; void JoystickInfo::UpdateReleaseState() { - vector::iterator itjoy = s_vgamePad.begin(); - SDL_JoystickUpdate(); // Save everything in the vector s_vjoysticks. - while (itjoy != s_vgamePad.end()) { - (*itjoy)->SaveState(); - ++itjoy; - } + for (auto &j : s_vgamePad) + j->SaveState(); } // opens handles to all possible joysticks -void JoystickInfo::EnumerateJoysticks(vector &vjoysticks) +void JoystickInfo::EnumerateJoysticks(std::vector> &vjoysticks) { - if (!s_bSDLInit) { // Tell SDL to catch event even if the windows isn't focussed SDL_SetHint(SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS, "1"); @@ -61,19 +56,10 @@ void JoystickInfo::EnumerateJoysticks(vector &vjoysticks) s_bSDLInit = true; } - vector::iterator it = vjoysticks.begin(); + vjoysticks.clear(); - // Delete everything in the vector vjoysticks. - while (it != vjoysticks.end()) { - delete *it; - ++it; - } - - vjoysticks.resize(SDL_NumJoysticks()); - - for (int i = 0; i < (int)vjoysticks.size(); ++i) { - vjoysticks[i] = new JoystickInfo(); - vjoysticks[i]->Init(i); + for (int i = 0; i < SDL_NumJoysticks(); ++i) { + vjoysticks.push_back(std::unique_ptr(new JoystickInfo(i))); } } diff --git a/plugins/onepad/SDL/joystick.h b/plugins/onepad/SDL/joystick.h index 097b1736a6..37d87a97a6 100644 --- a/plugins/onepad/SDL/joystick.h +++ b/plugins/onepad/SDL/joystick.h @@ -32,7 +32,7 @@ class JoystickInfo : GamePad { public: - JoystickInfo() + JoystickInfo(int id) : GamePad() , joy(nullptr) { @@ -40,6 +40,7 @@ public: first = true; memset(effects, 0, sizeof(effects)); memset(effects_id, 0, sizeof(effects_id)); + Init(id); } ~JoystickInfo() @@ -47,12 +48,13 @@ public: Destroy(); } - JoystickInfo(const JoystickInfo &); // copy constructor - JoystickInfo &operator=(const JoystickInfo &); // assignment + JoystickInfo(const JoystickInfo &) = delete; // copy constructor + JoystickInfo &operator=(const JoystickInfo &) = delete; // assignment + void Destroy(); // opens handles to all possible joysticks - static void EnumerateJoysticks(vector &vjoysticks); + static void EnumerateJoysticks(std::vector> &vjoysticks); void Rumble(int type, int pad); diff --git a/plugins/onepad/onepad.h b/plugins/onepad/onepad.h index d4840e0512..75429778ef 100644 --- a/plugins/onepad/onepad.h +++ b/plugins/onepad/onepad.h @@ -44,6 +44,7 @@ #include #include #include +#include using namespace std; #define PADdefs