From 842ac86b1fb6a77133c9d705e930600c4c143a83 Mon Sep 17 00:00:00 2001 From: Joel Linn Date: Sat, 28 Nov 2020 16:17:08 +0100 Subject: [PATCH] [HID] More modern c++ in SDL backend. --- src/xenia/hid/sdl/sdl_input_driver.cc | 68 +++++++++++++-------------- src/xenia/hid/sdl/sdl_input_driver.h | 11 +++-- 2 files changed, 40 insertions(+), 39 deletions(-) diff --git a/src/xenia/hid/sdl/sdl_input_driver.cc b/src/xenia/hid/sdl/sdl_input_driver.cc index c1404c358..97a21cc8d 100644 --- a/src/xenia/hid/sdl/sdl_input_driver.cc +++ b/src/xenia/hid/sdl/sdl_input_driver.cc @@ -77,7 +77,7 @@ X_STATUS SDLInputDriver::Setup() { sdl_events_initialized_ = true; SDL_EventFilter event_filter{[](void* userdata, SDL_Event* event) -> int { - if (!userdata) { + if (!userdata || !event) { assert_always(); return 0; } @@ -102,17 +102,17 @@ X_STATUS SDLInputDriver::Setup() { } switch (type) { case SDL_CONTROLLERDEVICEADDED: - driver->OnControllerDeviceAdded(event); + driver->OnControllerDeviceAdded(*event); break; case SDL_CONTROLLERDEVICEREMOVED: - driver->OnControllerDeviceRemoved(event); + driver->OnControllerDeviceRemoved(*event); break; case SDL_CONTROLLERAXISMOTION: - driver->OnControllerDeviceAxisMotion(event); + driver->OnControllerDeviceAxisMotion(*event); break; case SDL_CONTROLLERBUTTONDOWN: case SDL_CONTROLLERBUTTONUP: - driver->OnControllerDeviceButtonChanged(event); + driver->OnControllerDeviceButtonChanged(*event); break; default: break; @@ -407,12 +407,12 @@ X_RESULT SDLInputDriver::GetKeystroke(uint32_t users, uint32_t flags, return X_ERROR_EMPTY; } -void SDLInputDriver::OnControllerDeviceAdded(SDL_Event* event) { +void SDLInputDriver::OnControllerDeviceAdded(const SDL_Event& event) { assert(window()->loop()->is_on_loop_thread()); std::unique_lock guard(controllers_mutex_); // Open the controller. - const auto controller = SDL_GameControllerOpen(event->cdevice.which); + const auto controller = SDL_GameControllerOpen(event.cdevice.which); if (!controller) { assert_always(); return; @@ -446,52 +446,52 @@ void SDLInputDriver::OnControllerDeviceAdded(SDL_Event* event) { } } -void SDLInputDriver::OnControllerDeviceRemoved(SDL_Event* event) { +void SDLInputDriver::OnControllerDeviceRemoved(const SDL_Event& event) { assert(window()->loop()->is_on_loop_thread()); std::unique_lock guard(controllers_mutex_); // Find the disconnected gamecontroller and close it. - auto [found, i] = GetControllerIndexFromInstanceID(event->cdevice.which); - assert(found); - SDL_GameControllerClose(controllers_.at(i).sdl); - controllers_.at(i) = {}; - keystroke_states_.at(i) = {}; + auto idx = GetControllerIndexFromInstanceID(event.cdevice.which); + assert(idx); + SDL_GameControllerClose(controllers_.at(*idx).sdl); + controllers_.at(*idx) = {}; + keystroke_states_.at(*idx) = {}; } -void SDLInputDriver::OnControllerDeviceAxisMotion(SDL_Event* event) { +void SDLInputDriver::OnControllerDeviceAxisMotion(const SDL_Event& event) { assert(window()->loop()->is_on_loop_thread()); std::unique_lock guard(controllers_mutex_); - auto [found, i] = GetControllerIndexFromInstanceID(event->caxis.which); - assert(found); - auto& pad = controllers_.at(i).state.gamepad; - switch (event->caxis.axis) { + auto idx = GetControllerIndexFromInstanceID(event.caxis.which); + assert(idx); + auto& pad = controllers_.at(*idx).state.gamepad; + switch (event.caxis.axis) { case SDL_CONTROLLER_AXIS_LEFTX: - pad.thumb_lx = event->caxis.value; + pad.thumb_lx = event.caxis.value; break; case SDL_CONTROLLER_AXIS_LEFTY: - pad.thumb_ly = ~event->caxis.value; + pad.thumb_ly = ~event.caxis.value; break; case SDL_CONTROLLER_AXIS_RIGHTX: - pad.thumb_rx = event->caxis.value; + pad.thumb_rx = event.caxis.value; break; case SDL_CONTROLLER_AXIS_RIGHTY: - pad.thumb_ry = ~event->caxis.value; + pad.thumb_ry = ~event.caxis.value; break; case SDL_CONTROLLER_AXIS_TRIGGERLEFT: - pad.left_trigger = static_cast(event->caxis.value >> 7); + pad.left_trigger = static_cast(event.caxis.value >> 7); break; case SDL_CONTROLLER_AXIS_TRIGGERRIGHT: - pad.right_trigger = static_cast(event->caxis.value >> 7); + pad.right_trigger = static_cast(event.caxis.value >> 7); break; default: assert_always(); break; } - controllers_.at(i).state_changed = true; + controllers_.at(*idx).state_changed = true; } -void SDLInputDriver::OnControllerDeviceButtonChanged(SDL_Event* event) { +void SDLInputDriver::OnControllerDeviceButtonChanged(const SDL_Event& event) { assert(window()->loop()->is_on_loop_thread()); std::unique_lock guard(controllers_mutex_); @@ -515,15 +515,15 @@ void SDLInputDriver::OnControllerDeviceButtonChanged(SDL_Event* event) { X_INPUT_GAMEPAD_DPAD_LEFT, X_INPUT_GAMEPAD_DPAD_RIGHT}; - auto [found, i] = GetControllerIndexFromInstanceID(event->cbutton.which); - assert(found); - auto& controller = controllers_.at(i); + auto idx = GetControllerIndexFromInstanceID(event.cbutton.which); + assert(idx); + auto& controller = controllers_.at(*idx); uint16_t xbuttons = controller.state.gamepad.buttons; // Lookup the XInput button code. - auto xbutton = xbutton_lookup.at(event->cbutton.button); + auto xbutton = xbutton_lookup.at(event.cbutton.button); // Pressed or released? - if (event->cbutton.state == SDL_PRESSED) { + if (event.cbutton.state == SDL_PRESSED) { if (xbutton == X_INPUT_GAMEPAD_GUIDE && !cvars::guide_button) { return; } @@ -535,7 +535,7 @@ void SDLInputDriver::OnControllerDeviceButtonChanged(SDL_Event* event) { controller.state_changed = true; } -std::pair SDLInputDriver::GetControllerIndexFromInstanceID( +std::optional SDLInputDriver::GetControllerIndexFromInstanceID( SDL_JoystickID instance_id) { // Loop through our controllers and try to match the given ID. for (size_t i = 0; i < controllers_.size(); i++) { @@ -548,10 +548,10 @@ std::pair SDLInputDriver::GetControllerIndexFromInstanceID( auto joy_instance_id = SDL_JoystickInstanceID(joystick); assert(joy_instance_id >= 0); if (joy_instance_id == instance_id) { - return {true, i}; + return i; } } - return {false, 0}; + return std::nullopt; } SDLInputDriver::ControllerState* SDLInputDriver::GetControllerState( diff --git a/src/xenia/hid/sdl/sdl_input_driver.h b/src/xenia/hid/sdl/sdl_input_driver.h index 033862cd7..f98619a2f 100644 --- a/src/xenia/hid/sdl/sdl_input_driver.h +++ b/src/xenia/hid/sdl/sdl_input_driver.h @@ -13,6 +13,7 @@ #include #include #include +#include #include "SDL.h" #include "xenia/hid/input_driver.h" @@ -64,11 +65,11 @@ class SDLInputDriver : public InputDriver { }; protected: - void OnControllerDeviceAdded(SDL_Event* event); - void OnControllerDeviceRemoved(SDL_Event* event); - void OnControllerDeviceAxisMotion(SDL_Event* event); - void OnControllerDeviceButtonChanged(SDL_Event* event); - std::pair GetControllerIndexFromInstanceID( + void OnControllerDeviceAdded(const SDL_Event& event); + void OnControllerDeviceRemoved(const SDL_Event& event); + void OnControllerDeviceAxisMotion(const SDL_Event& event); + void OnControllerDeviceButtonChanged(const SDL_Event& event); + std::optional GetControllerIndexFromInstanceID( SDL_JoystickID instance_id); ControllerState* GetControllerState(uint32_t user_index); bool TestSDLVersion() const;