diff options
author | 2023-01-14 03:07:57 +1100 | |
---|---|---|
committer | 2023-01-14 03:13:59 +1100 | |
commit | bf2707f4c76abaeaeda2d9c4444a608d7d0afed5 (patch) | |
tree | 992e3090717dd4c36922b80615d8680588183384 /src/osd/modules/input/input_sdl.cpp | |
parent | e817a3d0c8e7697b55ce75c8e0939328022f5ecc (diff) |
osd: Added support for mapping files to sdlgame joystick provider and made it default with SDL.
This changes behaviour, however I think it's a net positive:
* Most games using Steam Input or SDL2 to read game controllers have
this behaviour, so users have come to expect it.
* This module is better at giving meaningful names to buttons on
common controller, and assigning axes consistently.
* Button/axis mapping files using a widely-used format are supported.
* The old behaviour is still available with `-joystickprovider sdljoy`
if anyone wants it.
The new option for controller mapping files is in the general OSD
options rather than SDL options as it can be extended to DirectInput in
the future.
Diffstat (limited to 'src/osd/modules/input/input_sdl.cpp')
-rw-r--r-- | src/osd/modules/input/input_sdl.cpp | 38 |
1 files changed, 33 insertions, 5 deletions
diff --git a/src/osd/modules/input/input_sdl.cpp b/src/osd/modules/input/input_sdl.cpp index 0a56d1b4f23..3ac99ffef22 100644 --- a/src/osd/modules/input/input_sdl.cpp +++ b/src/osd/modules/input/input_sdl.cpp @@ -35,6 +35,7 @@ #include <algorithm> #include <cctype> #include <cstddef> +#include <cstring> #include <iterator> #include <memory> #include <optional> @@ -523,7 +524,7 @@ int lookup_sdl_code(const char *scode) while (sdl_lookup_table[i].code >= 0) { - if (!strcmp(scode, sdl_lookup_table[i].name)) + if (!std::strcmp(scode, sdl_lookup_table[i].name)) return sdl_lookup_table[i].code; i++; } @@ -1939,18 +1940,33 @@ public: { SDL_SetHint(SDL_HINT_ACCELEROMETER_AS_JOYSTICK, "0"); + auto &sdlopts = downcast<sdl_options const &>(*options()); + bool const sixaxis_mode = sdlopts.sixaxis(); + init_joystick(); if (!have_joystick()) return; m_initialized_game_controller = !SDL_InitSubSystem(SDL_INIT_GAMECONTROLLER); - if (!m_initialized_game_controller) + if (m_initialized_game_controller) + { + char const *const mapfile = sdlopts.controller_mapping_file(); + if (mapfile && *mapfile && std::strcmp(mapfile, OSDOPTVAL_NONE)) + { + auto const count = SDL_GameControllerAddMappingsFromFile(mapfile); + if (0 <= count) + osd_printf_verbose("Game Controller: %d controller mapping(s) added from file [%s].\n", count, mapfile); + else + osd_printf_error("Game Controller: Error adding mappings from file [%s]: %s.\n", mapfile, SDL_GetError()); + } + } + else + { osd_printf_warning("Could not initialize SDL Game Controller: %s.\n", SDL_GetError()); + } sdl_joystick_module_base::input_init(machine); - bool const sixaxis_mode = downcast<const sdl_options *>(options())->sixaxis(); - osd_printf_verbose("Game Controller: Start initialization\n"); for (int physical_stick = 0; physical_stick < SDL_NumJoysticks(); physical_stick++) { @@ -1970,6 +1986,14 @@ public: create_game_controller_device(machine, physical_stick, ctrl); } + static int const joy_event_types[] = { + int(SDL_JOYAXISMOTION), + int(SDL_JOYBALLMOTION), + int(SDL_JOYHATMOTION), + int(SDL_JOYBUTTONDOWN), + int(SDL_JOYBUTTONUP), + int(SDL_JOYDEVICEADDED), + int(SDL_JOYDEVICEREMOVED) }; static int const event_types[] = { int(SDL_JOYAXISMOTION), int(SDL_JOYBALLMOTION), @@ -1983,7 +2007,10 @@ public: int(SDL_CONTROLLERBUTTONUP), int(SDL_CONTROLLERDEVICEADDED), int(SDL_CONTROLLERDEVICEREMOVED) }; - sdl_event_manager::instance().subscribe(event_types, this); + if (m_initialized_game_controller) + sdl_event_manager::instance().subscribe(event_types, this); + else + sdl_event_manager::instance().subscribe(joy_event_types, this); osd_printf_verbose("Game Controller: End initialization\n"); } @@ -2036,6 +2063,7 @@ public: // for devices supported by the game controller API, this is received before the corresponding SDL_JOYDEVICEADDED case SDL_CONTROLLERDEVICEADDED: + if (m_initialized_game_controller) { SDL_GameController *const ctrl = SDL_GameControllerOpen(sdlevent.cdevice.which); if (!ctrl) |