diff options
author | Brad Hughes <bradhugh@outlook.com> | 2016-08-29 22:16:32 -0400 |
---|---|---|
committer | Brad Hughes <bradhugh@outlook.com> | 2016-08-31 18:01:53 -0400 |
commit | 5281612e2a194fec7f52201b334b2038d7940c97 (patch) | |
tree | fe0c56032b90ef3f0de9cde8c15717579c5617a5 /src/osd/modules/input | |
parent | dcdd2cc5ba91a43953ef17ca705b72d0a46fdf71 (diff) |
Partially fix SDL joysticks with same name
Fixes #1334
Diffstat (limited to 'src/osd/modules/input')
-rw-r--r-- | src/osd/modules/input/input_sdl.cpp | 33 | ||||
-rw-r--r-- | src/osd/modules/input/input_sdlcommon.h | 12 |
2 files changed, 17 insertions, 28 deletions
diff --git a/src/osd/modules/input/input_sdl.cpp b/src/osd/modules/input/input_sdl.cpp index 0ff623ea8ff..20a08beb216 100644 --- a/src/osd/modules/input/input_sdl.cpp +++ b/src/osd/modules/input/input_sdl.cpp @@ -24,6 +24,7 @@ #include <mutex> #include <memory> #include <queue> +#include <iterator> #include <algorithm> // MAME headers @@ -687,29 +688,27 @@ public: }; -static void devmap_register(device_map_t *devmap, int physical_idx, const std::string &name) +static void devmap_register(device_map_t &devmap, int physical_idx, const std::string &name) { - int found = 0; - int stick, i; + // Attempt to find the entry by name + auto entry = std::find_if(std::begin(devmap.map), std::end(devmap.map), [&name](auto &item) + { + return item.name == name && item.physical < 0; + }); - for (i = 0; i < MAX_DEVMAP_ENTRIES; i++) + // If we didn't find it by name, find the first free slot + if (entry == std::end(devmap.map)) { - if (strcmp(name.c_str(), devmap->map[i].name.c_str()) == 0 && devmap->map[i].physical < 0) - { - devmap->map[i].physical = physical_idx; - found = 1; - devmap->logical[physical_idx] = i; - } + entry = std::find_if(std::begin(devmap.map), std::end(devmap.map), [](auto &item) { return item.name.empty(); }); } - if (found == 0) + if (entry != std::end(devmap.map)) { - stick = devmap_leastfree(devmap); - devmap->map[stick].physical = physical_idx; - devmap->map[stick].name = name; - devmap->logical[physical_idx] = stick; + entry->physical = physical_idx; + entry->name = name; + int logical_idx = std::distance(std::begin(devmap.map), entry); + devmap.logical[physical_idx] = logical_idx; } - } //============================================================ @@ -757,7 +756,7 @@ public: for (physical_stick = 0; physical_stick < SDL_NumJoysticks(); physical_stick++) { std::string joy_name = remove_spaces(SDL_JoystickNameForIndex(physical_stick)); - devmap_register(&m_joy_map, physical_stick, joy_name.c_str()); + devmap_register(m_joy_map, physical_stick, joy_name); } for (int stick = 0; stick < MAX_DEVMAP_ENTRIES; stick++) diff --git a/src/osd/modules/input/input_sdlcommon.h b/src/osd/modules/input/input_sdlcommon.h index 70dd5511271..9e271985997 100644 --- a/src/osd/modules/input/input_sdlcommon.h +++ b/src/osd/modules/input/input_sdlcommon.h @@ -148,17 +148,6 @@ private: // INLINE FUNCTIONS //============================================================ -static inline int devmap_leastfree(device_map_t *devmap) -{ - for (int i = 0; i < MAX_DEVMAP_ENTRIES; i++) - { - if (devmap->map[i].name.length() == 0) - return i; - } - - return -1; -} - static inline std::string remove_spaces(const char *s) { // Remove the spaces @@ -184,6 +173,7 @@ static inline void devmap_init(running_machine &machine, device_map_t *devmap, c // Initialize the map to default uninitialized values for (dev = 0; dev < MAX_DEVMAP_ENTRIES; dev++) { + devmap->map[dev].name.clear(); devmap->map[dev].physical = -1; devmap->logical[dev] = -1; } |