diff options
-rw-r--r-- | src/osd/modules/input/input_common.h | 94 | ||||
-rw-r--r-- | src/osd/modules/input/input_dinput.cpp | 8 | ||||
-rw-r--r-- | src/osd/modules/input/input_rawinput.cpp | 33 | ||||
-rw-r--r-- | src/osd/modules/input/input_sdl.cpp | 51 | ||||
-rw-r--r-- | src/osd/modules/input/input_sdlcommon.cpp | 33 | ||||
-rw-r--r-- | src/osd/modules/input/input_sdlcommon.h | 33 | ||||
-rw-r--r-- | src/osd/modules/input/input_win32.cpp | 26 | ||||
-rw-r--r-- | src/osd/modules/input/input_winhybrid.cpp | 4 | ||||
-rw-r--r-- | src/osd/modules/input/input_xinput.cpp | 3 | ||||
-rw-r--r-- | src/osd/modules/input/input_xinput.h | 11 |
10 files changed, 134 insertions, 162 deletions
diff --git a/src/osd/modules/input/input_common.h b/src/osd/modules/input/input_common.h index 45752639d0a..43c77f16b06 100644 --- a/src/osd/modules/input/input_common.h +++ b/src/osd/modules/input/input_common.h @@ -17,10 +17,10 @@ #include <memory> #include <mutex> #include <queue> -#include <string> +#include <algorithm> +#include <functional> #undef min #undef max -#include <utility> //============================================================ @@ -287,55 +287,37 @@ public: class input_device_list { -protected: +private: std::vector<std::unique_ptr<device_info>> m_list; public: - input_device_list() - { - } + size_t size() const { return m_list.size(); } + auto begin() { return m_list.begin(); } + auto end() { return m_list.end(); } void poll_devices() { - for (auto iter = m_list.begin(); iter != m_list.end(); ++iter) - iter->get()->poll(); + for (auto &device: m_list) + device->poll(); } void reset_devices() { - for (auto iter = m_list.begin(); iter != m_list.end(); ++iter) - iter->get()->reset(); + for (auto &device: m_list) + device->reset(); } - void free_device(device_info * devinfo) + void free_device(device_info* devinfo) { - // remove us from the list - for (auto iter = m_list.begin(); iter != m_list.end(); ++iter) - { - if (iter->get() == devinfo) - { - m_list.erase(iter); - break; - } - } + // find the device to remove + auto device_matches = [devinfo](std::unique_ptr<device_info> &device) { return devinfo == device.get(); }; + m_list.erase(std::remove_if(std::begin(m_list), std::end(m_list), device_matches), m_list.end()); } - int find_index(device_info* devinfo) + void for_each_device(std::function<void (device_info*)> action) { - // remove us from the list - int i = 0; - for (auto iter = m_list.begin(); iter != m_list.end(); ++iter) - { - if (iter->get() == devinfo) - { - break; - } - - i++; - } - - // return the index or -1 if we couldn't find it - return i == m_list.size() ? -1 : i; + for (auto &device: m_list) + action(device.get()); } void free_all_devices() @@ -344,49 +326,17 @@ public: m_list.pop_back(); } - int size() const - { - return m_list.size(); - } - - device_info* at(int index) - { - return m_list.at(index).get(); - } - - template <typename TActual> - TActual* create_device(running_machine &machine, const char *name, input_module &module) - { - // allocate the device object - auto devinfo = std::make_unique<TActual>(machine, name, module); - - return add_device_internal(machine, name, module, std::move(devinfo)); - } - - template <typename TActual, typename TArg> - TActual* create_device1(running_machine &machine, const char *name, input_module &module, TArg arg1) + template <typename TActual, typename... TArgs> + TActual* create_device(running_machine &machine, const char *name, input_module &module, TArgs&&... args) { // allocate the device object - auto devinfo = std::make_unique<TActual>(machine, name, module, arg1); + auto devinfo = std::make_unique<TActual>(machine, name, module, std::forward<TArgs>(args)...); - return add_device_internal(machine, name, module, std::move(devinfo)); - } - - template <class TActual> - TActual* at(int index) - { - return static_cast<TActual*>(m_list.at(index).get()); - } - -private: - template <typename TActual> - TActual* add_device_internal(running_machine &machine, const char *name, input_module &module, std::unique_ptr<TActual> allocated) - { // Add the device to the machine - allocated->m_device = machine.input().device_class(allocated->deviceclass()).add_device(allocated->name(), allocated.get()); + devinfo->m_device = machine.input().device_class(devinfo->deviceclass()).add_device(devinfo->name(), devinfo.get()); // append us to the list - m_list.push_back(std::move(allocated)); + m_list.push_back(std::move(devinfo)); return static_cast<TActual*>(m_list.back().get()); } diff --git a/src/osd/modules/input/input_dinput.cpp b/src/osd/modules/input/input_dinput.cpp index f12e30a26c2..01e920f2953 100644 --- a/src/osd/modules/input/input_dinput.cpp +++ b/src/osd/modules/input/input_dinput.cpp @@ -25,13 +25,10 @@ #undef min #undef max -#include <utility> #include <mutex> -#include <thread> // MAME headers #include "emu.h" -#include "osdepend.h" #include "strconv.h" // MAMEOS headers @@ -139,7 +136,8 @@ void dinput_keyboard_device::reset() dinput_api_helper::dinput_api_helper(int version) : m_dinput(nullptr), - m_dinput_version(version) + m_dinput_version(version), + m_dinput_create_prt(nullptr) { } @@ -385,7 +383,7 @@ public: result = dinput_set_dword_property(devinfo->dinput.device, DIPROP_AXISMODE, 0, DIPH_DEVICE, DIPROPAXISMODE_REL); if (result != DI_OK && result != DI_PROPNOEFFECT) { - osd_printf_error("DirectInput: Unable to set relative mode for mouse %d (%s)\n", devicelist()->size(), devinfo->name()); + osd_printf_error("DirectInput: Unable to set relative mode for mouse %u (%s)\n", static_cast<unsigned int>(devicelist()->size()), devinfo->name()); goto error; } diff --git a/src/osd/modules/input/input_rawinput.cpp b/src/osd/modules/input/input_rawinput.cpp index 0a9466598cc..ee6dbf4a511 100644 --- a/src/osd/modules/input/input_rawinput.cpp +++ b/src/osd/modules/input/input_rawinput.cpp @@ -21,10 +21,10 @@ #include <mutex> #include <functional> +#include <algorithm> // MAME headers #include "emu.h" -#include "osdepend.h" #include "strconv.h" // MAMEOS headers @@ -467,7 +467,11 @@ private: public: rawinput_module(const char *type, const char* name) - : wininput_module(type, name) + : wininput_module(type, name), + get_rawinput_device_list(nullptr), + get_rawinput_data(nullptr), + get_rawinput_device_info(nullptr), + register_rawinput_devices(nullptr) { } @@ -615,24 +619,23 @@ protected: { std::lock_guard<std::mutex> scope_lock(m_module_lock); - rawinput_device *devinfo; + RAWINPUT *input = reinterpret_cast<RAWINPUT*>(data); + // find the device in the list and update - for (int i = 0; i < devicelist()->size(); i++) + auto target_device = std::find_if(devicelist()->begin(), devicelist()->end(), [input](auto &device) + { + auto devinfo = dynamic_cast<rawinput_device*>(device.get()); + return devinfo != nullptr && input->header.hDevice == devinfo->device_handle(); + }); + + if (target_device != devicelist()->end()) { - devinfo = dynamic_cast<rawinput_device*>(devicelist()->at(i)); - if (devinfo) - { - RAWINPUT *input = reinterpret_cast<RAWINPUT*>(data); - if (input->header.hDevice == devinfo->device_handle()) - { - devinfo->queue_events(input, 1); - result = true; - } - } + static_cast<rawinput_device*>((*target_device).get())->queue_events(input, 1); + return true; } } - return result; + return false; } }; diff --git a/src/osd/modules/input/input_sdl.cpp b/src/osd/modules/input/input_sdl.cpp index 0df3f5b7014..9d53ab5c3c9 100644 --- a/src/osd/modules/input/input_sdl.cpp +++ b/src/osd/modules/input/input_sdl.cpp @@ -19,15 +19,15 @@ // standard sdl header #include <SDL2/SDL.h> #include <ctype.h> +// ReSharper disable once CppUnusedIncludeDirective #include <stddef.h> #include <mutex> #include <memory> #include <queue> +#include <algorithm> // MAME headers #include "emu.h" -#include "osdepend.h" -#include "ui/uimain.h" #include "uiinput.h" #include "strconv.h" @@ -487,11 +487,12 @@ public: return sdl_event_manager::instance().has_focus() && input_enabled(); } - virtual void handle_event(SDL_Event &sdlevent) override + void handle_event(SDL_Event &sdlevent) override { // By default dispatch event to every device - for (int i = 0; i < devicelist()->size(); i++) - downcast<sdl_device*>(devicelist()->at(i))->queue_events(&sdlevent, 1); + devicelist()->for_each_device([&sdlevent](auto device) { + downcast<sdl_device*>(device)->queue_events(&sdlevent, 1); + }); } }; @@ -554,7 +555,7 @@ private: char *keymap_filename; FILE *keymap_file; int line = 1; - int index, i, sk, vk, ak; + int index, len, sk, vk, ak; char buf[256]; char mks[41]; char sks[41]; @@ -592,9 +593,9 @@ private: if (ret && buf[0] != '\n' && buf[0] != '#') { buf[255] = 0; - i = strlen(buf); - if (i && buf[i - 1] == '\n') - buf[i - 1] = 0; + len = strlen(buf); + if (len && buf[len - 1] == '\n') + buf[len - 1] = 0; if (strncmp(buf, "[SDL2]", 6) == 0) { sdl2section = 1; @@ -861,43 +862,41 @@ public: virtual void handle_event(SDL_Event &sdlevent) override { // Figure out which joystick this event id destined for - for (int i = 0; i < devicelist()->size(); i++) + auto target_device = std::find_if(devicelist()->begin(), devicelist()->end(), [&sdlevent](auto &device) { - auto joy = downcast<sdl_joystick_device*>(devicelist()->at(i)); - - // If we find a matching joystick, dispatch the event to the joystick - if (joy->sdl_state.joystick_id == sdlevent.jdevice.which) - joy->queue_events(&sdlevent, 1); + std::unique_ptr<device_info> &ptr = device; + return downcast<sdl_joystick_device*>(ptr.get())->sdl_state.joystick_id == sdlevent.jdevice.which; + }); + + // If we find a matching joystick, dispatch the event to the joystick + if (target_device != devicelist()->end()) + { + downcast<sdl_joystick_device*>((*target_device).get())->queue_events(&sdlevent, 1); } } private: sdl_joystick_device* create_joystick_device(running_machine &machine, device_map_t *devmap, int index, input_device_class devclass) { - sdl_joystick_device *devinfo = nullptr; char tempname[20]; - if (devmap->map[index].name.length() == 0) + if (devmap->map[index].name.empty()) { - /* only map place holders if there were mappings specified is enabled */ + // only map place holders if there were mappings specified if (devmap->initialized) { snprintf(tempname, ARRAY_LENGTH(tempname), "NC%d", index); - devinfo = m_sixaxis_mode + m_sixaxis_mode ? devicelist()->create_device<sdl_sixaxis_joystick_device>(machine, tempname, *this) : devicelist()->create_device<sdl_joystick_device>(machine, tempname, *this); } return nullptr; } - else - { - devinfo = m_sixaxis_mode - ? devicelist()->create_device<sdl_sixaxis_joystick_device>(machine, devmap->map[index].name.c_str(), *this) - : devicelist()->create_device<sdl_joystick_device>(machine, devmap->map[index].name.c_str(), *this); - } - return devinfo; + return m_sixaxis_mode + ? devicelist()->create_device<sdl_sixaxis_joystick_device>(machine, devmap->map[index].name.c_str(), *this) + : devicelist()->create_device<sdl_joystick_device>(machine, devmap->map[index].name.c_str(), *this); } }; diff --git a/src/osd/modules/input/input_sdlcommon.cpp b/src/osd/modules/input/input_sdlcommon.cpp index 4abcbd0251c..028c4d53ee9 100644 --- a/src/osd/modules/input/input_sdlcommon.cpp +++ b/src/osd/modules/input/input_sdlcommon.cpp @@ -19,6 +19,7 @@ #include <stddef.h> #include <mutex> #include <memory> +#include <algorithm> // MAME headers #include "emu.h" @@ -35,19 +36,20 @@ #define GET_WINDOW(ev) window_from_id((ev)->windowID) //#define GET_WINDOW(ev) ((ev)->windowID) -static inline std::shared_ptr<sdl_window_info> window_from_id(Uint32 windowID) +static std::shared_ptr<sdl_window_info> window_from_id(Uint32 windowID) { - SDL_Window *window = SDL_GetWindowFromID(windowID); + SDL_Window *sdl_window = SDL_GetWindowFromID(windowID); - for (auto w : osd_common_t::s_window_list) + auto& windows = osd_common_t::s_window_list; + auto window = std::find_if(windows.begin(), windows.end(), [sdl_window](std::shared_ptr<osd_window> w) { - //printf("w->window_id: %d\n", w->window_id); - if (w->platform_window<SDL_Window*>() == window) - { - return std::static_pointer_cast<sdl_window_info>(w); - } - } - return nullptr; + return w->platform_window<SDL_Window*>() == sdl_window; + }); + + if (window == windows.end()) + return nullptr; + + return std::static_pointer_cast<sdl_window_info>(*window); } void sdl_event_manager::process_events(running_machine &machine) @@ -64,8 +66,10 @@ void sdl_event_manager::process_events(running_machine &machine) auto subscribers = m_subscription_index.equal_range(sdlevent.type); // Dispatch the events - for (auto iter = subscribers.first; iter != subscribers.second; ++iter) - iter->second->handle_event(sdlevent); + std::for_each(subscribers.first, subscribers.second, [&sdlevent](auto sub) + { + sub.second->handle_event(sdlevent); + }); } } @@ -74,7 +78,10 @@ void sdl_event_manager::process_window_event(running_machine &machine, SDL_Event std::shared_ptr<sdl_window_info> window = GET_WINDOW(&sdlevent.window); if (window == nullptr) + { + osd_printf_warning("Skipped window event due to missing window param from SDL\n"); return; + } switch (sdlevent.window.event) { @@ -98,7 +105,7 @@ void sdl_event_manager::process_window_event(running_machine &machine, SDL_Event break; case SDL_WINDOWEVENT_RESIZED: -#ifndef SDLMAME_WIN32 +#ifdef SDLMAME_LINUX /* FIXME: SDL2 sends some spurious resize events on Ubuntu * while in fullscreen mode. Ignore them for now. */ diff --git a/src/osd/modules/input/input_sdlcommon.h b/src/osd/modules/input/input_sdlcommon.h index cfc5a647793..70dd5511271 100644 --- a/src/osd/modules/input/input_sdlcommon.h +++ b/src/osd/modules/input/input_sdlcommon.h @@ -11,10 +11,8 @@ #ifndef INPUT_SDLCOMMON_H_ #define INPUT_SDLCOMMON_H_ -#include <vector> #include <unordered_map> #include <algorithm> -#include <queue> #define MAX_DEVMAP_ENTRIES 16 #define SDL_MODULE_EVENT_BUFFER_SIZE 5 @@ -96,17 +94,18 @@ public: { std::lock_guard<std::mutex> scope_lock(m_lock); - // Loop over the entries and find ones that match our subscriber - std::vector<typename std::unordered_multimap<int, TSubscriber*>::iterator> remove; - for (auto iter = m_subscription_index.begin(); iter != m_subscription_index.end(); ++iter) + // Remove the events that match the subscriber + for (auto it = begin(m_subscription_index); it != end(m_subscription_index);) { - if (iter->second == subscriber) - remove.push_back(iter); + if (it->second == subscriber) + { + it = m_subscription_index.erase(it); + } + else + { + ++it; + } } - - // remove those that matched - for (int i = 0; i < remove.size(); i++) - m_subscription_index.erase(remove[i]); } virtual void process_events(running_machine &machine) = 0; @@ -117,14 +116,14 @@ class sdl_window_info; class sdl_event_manager : public event_manager_t<sdl_event_subscriber> { private: - bool m_mouse_over_window; - bool m_has_focus; + bool m_mouse_over_window; + bool m_has_focus; std::shared_ptr<sdl_window_info> m_focus_window; sdl_event_manager() : m_mouse_over_window(true), - m_has_focus(true), - m_focus_window(nullptr) + m_has_focus(true), + m_focus_window(nullptr) { } @@ -151,12 +150,12 @@ private: static inline int devmap_leastfree(device_map_t *devmap) { - int i; - for (i = 0; i < MAX_DEVMAP_ENTRIES; i++) + for (int i = 0; i < MAX_DEVMAP_ENTRIES; i++) { if (devmap->map[i].name.length() == 0) return i; } + return -1; } diff --git a/src/osd/modules/input/input_win32.cpp b/src/osd/modules/input/input_win32.cpp index ce1cf4e883f..78e162f296b 100644 --- a/src/osd/modules/input/input_win32.cpp +++ b/src/osd/modules/input/input_win32.cpp @@ -20,7 +20,6 @@ // MAME headers #include "emu.h" -#include "osdepend.h" // MAMEOS headers #include "winmain.h" @@ -104,8 +103,12 @@ public: case INPUT_EVENT_KEYDOWN: case INPUT_EVENT_KEYUP: args = static_cast<KeyPressEventArgs*>(eventdata); - for (int i = 0; i < devicelist()->size(); i++) - downcast<win32_keyboard_device*>(devicelist()->at(i))->queue_events(args, 1); + devicelist()->for_each_device([args](auto device) + { + auto keyboard = dynamic_cast<win32_keyboard_device*>(device); + if (keyboard != nullptr) + keyboard->queue_events(args, 1); + }); return true; @@ -234,8 +237,12 @@ public: return false; auto args = static_cast<MouseButtonEventArgs*>(eventdata); - for (int i = 0; i < devicelist()->size(); i++) - downcast<win32_mouse_device*>(devicelist()->at(i))->queue_events(args, 1); + devicelist()->for_each_device([args](auto device) + { + auto mouse = dynamic_cast<win32_mouse_device*>(device); + if (mouse != nullptr) + mouse->queue_events(args, 1); + }); return true; } @@ -417,8 +424,13 @@ public: if (!input_enabled() || !lightgun_enabled() || eventid != INPUT_EVENT_MOUSE_BUTTON) return false; - for (int i = 0; i < devicelist()->size(); i++) - downcast<win32_lightgun_device*>(devicelist()->at(i))->queue_events(static_cast<MouseButtonEventArgs*>(eventdata), 1); + auto args = static_cast<MouseButtonEventArgs*>(eventdata); + devicelist()->for_each_device([args](auto device) + { + auto lightgun = dynamic_cast<win32_lightgun_device*>(device); + if (lightgun != nullptr) + lightgun->queue_events(args, 1); + }); return true; } diff --git a/src/osd/modules/input/input_winhybrid.cpp b/src/osd/modules/input/input_winhybrid.cpp index 80a1280c511..91b55f48523 100644 --- a/src/osd/modules/input/input_winhybrid.cpp +++ b/src/osd/modules/input/input_winhybrid.cpp @@ -30,11 +30,9 @@ // MAME headers #include "emu.h" -#include "osdepend.h" // MAMEOS headers #include "strconv.h" -#include "winutil.h" #include "winmain.h" #include "input_common.h" @@ -296,7 +294,7 @@ private: bstr_ptr bstrClassName; bstr_ptr bstrNamespace; DWORD uReturned = 0; - UINT iDevice = 0; + UINT iDevice; VARIANT var; HRESULT hr; diff --git a/src/osd/modules/input/input_xinput.cpp b/src/osd/modules/input/input_xinput.cpp index e64b166c287..ba25e4e1cd9 100644 --- a/src/osd/modules/input/input_xinput.cpp +++ b/src/osd/modules/input/input_xinput.cpp @@ -24,7 +24,6 @@ // MAME headers #include "emu.h" -#include "osdepend.h" // MAMEOS headers #include "winutil.h" @@ -75,7 +74,7 @@ xinput_joystick_device * xinput_api_helper::create_xinput_device(running_machine snprintf(device_name, sizeof(device_name), "XInput Player %u", index + 1); // allocate the device object - devinfo = module.devicelist()->create_device1<xinput_joystick_device>(machine, device_name, module, shared_from_this()); + devinfo = module.devicelist()->create_device<xinput_joystick_device>(machine, device_name, module, shared_from_this()); // Set the player ID devinfo->xinput_state.player_index = index; diff --git a/src/osd/modules/input/input_xinput.h b/src/osd/modules/input/input_xinput.h index 0c0a4de897e..4dddc3069fd 100644 --- a/src/osd/modules/input/input_xinput.h +++ b/src/osd/modules/input/input_xinput.h @@ -97,15 +97,22 @@ typedef DWORD (WINAPI *xinput_get_caps_fn)(DWORD, DWORD, XINPUT_CAPABILITIES *); class xinput_api_helper : public std::enable_shared_from_this<xinput_api_helper> { public: + xinput_api_helper() + : m_xinput_dll(nullptr), + XInputGetState(nullptr), + XInputGetCapabilities(nullptr) + { + } + int initialize(); xinput_joystick_device * create_xinput_device(running_machine &machine, UINT index, wininput_module &module); - inline DWORD xinput_get_state(DWORD dwUserindex, XINPUT_STATE *pState) + DWORD xinput_get_state(DWORD dwUserindex, XINPUT_STATE *pState) const { return (*XInputGetState)(dwUserindex, pState); } - inline DWORD xinput_get_capabilities(DWORD dwUserindex, DWORD dwFlags, XINPUT_CAPABILITIES* pCapabilities) + DWORD xinput_get_capabilities(DWORD dwUserindex, DWORD dwFlags, XINPUT_CAPABILITIES* pCapabilities) const { return (*XInputGetCapabilities)(dwUserindex, dwFlags, pCapabilities); } |