[HID] More modern c++ in SDL backend.

This commit is contained in:
Joel Linn 2020-11-28 16:17:08 +01:00 committed by Rick Gibbed
parent ff56fbdf46
commit 842ac86b1f
2 changed files with 40 additions and 39 deletions

View File

@ -77,7 +77,7 @@ X_STATUS SDLInputDriver::Setup() {
sdl_events_initialized_ = true; sdl_events_initialized_ = true;
SDL_EventFilter event_filter{[](void* userdata, SDL_Event* event) -> int { SDL_EventFilter event_filter{[](void* userdata, SDL_Event* event) -> int {
if (!userdata) { if (!userdata || !event) {
assert_always(); assert_always();
return 0; return 0;
} }
@ -102,17 +102,17 @@ X_STATUS SDLInputDriver::Setup() {
} }
switch (type) { switch (type) {
case SDL_CONTROLLERDEVICEADDED: case SDL_CONTROLLERDEVICEADDED:
driver->OnControllerDeviceAdded(event); driver->OnControllerDeviceAdded(*event);
break; break;
case SDL_CONTROLLERDEVICEREMOVED: case SDL_CONTROLLERDEVICEREMOVED:
driver->OnControllerDeviceRemoved(event); driver->OnControllerDeviceRemoved(*event);
break; break;
case SDL_CONTROLLERAXISMOTION: case SDL_CONTROLLERAXISMOTION:
driver->OnControllerDeviceAxisMotion(event); driver->OnControllerDeviceAxisMotion(*event);
break; break;
case SDL_CONTROLLERBUTTONDOWN: case SDL_CONTROLLERBUTTONDOWN:
case SDL_CONTROLLERBUTTONUP: case SDL_CONTROLLERBUTTONUP:
driver->OnControllerDeviceButtonChanged(event); driver->OnControllerDeviceButtonChanged(*event);
break; break;
default: default:
break; break;
@ -407,12 +407,12 @@ X_RESULT SDLInputDriver::GetKeystroke(uint32_t users, uint32_t flags,
return X_ERROR_EMPTY; return X_ERROR_EMPTY;
} }
void SDLInputDriver::OnControllerDeviceAdded(SDL_Event* event) { void SDLInputDriver::OnControllerDeviceAdded(const SDL_Event& event) {
assert(window()->loop()->is_on_loop_thread()); assert(window()->loop()->is_on_loop_thread());
std::unique_lock<std::mutex> guard(controllers_mutex_); std::unique_lock<std::mutex> guard(controllers_mutex_);
// Open the controller. // Open the controller.
const auto controller = SDL_GameControllerOpen(event->cdevice.which); const auto controller = SDL_GameControllerOpen(event.cdevice.which);
if (!controller) { if (!controller) {
assert_always(); assert_always();
return; 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()); assert(window()->loop()->is_on_loop_thread());
std::unique_lock<std::mutex> guard(controllers_mutex_); std::unique_lock<std::mutex> guard(controllers_mutex_);
// Find the disconnected gamecontroller and close it. // Find the disconnected gamecontroller and close it.
auto [found, i] = GetControllerIndexFromInstanceID(event->cdevice.which); auto idx = GetControllerIndexFromInstanceID(event.cdevice.which);
assert(found); assert(idx);
SDL_GameControllerClose(controllers_.at(i).sdl); SDL_GameControllerClose(controllers_.at(*idx).sdl);
controllers_.at(i) = {}; controllers_.at(*idx) = {};
keystroke_states_.at(i) = {}; keystroke_states_.at(*idx) = {};
} }
void SDLInputDriver::OnControllerDeviceAxisMotion(SDL_Event* event) { void SDLInputDriver::OnControllerDeviceAxisMotion(const SDL_Event& event) {
assert(window()->loop()->is_on_loop_thread()); assert(window()->loop()->is_on_loop_thread());
std::unique_lock<std::mutex> guard(controllers_mutex_); std::unique_lock<std::mutex> guard(controllers_mutex_);
auto [found, i] = GetControllerIndexFromInstanceID(event->caxis.which); auto idx = GetControllerIndexFromInstanceID(event.caxis.which);
assert(found); assert(idx);
auto& pad = controllers_.at(i).state.gamepad; auto& pad = controllers_.at(*idx).state.gamepad;
switch (event->caxis.axis) { switch (event.caxis.axis) {
case SDL_CONTROLLER_AXIS_LEFTX: case SDL_CONTROLLER_AXIS_LEFTX:
pad.thumb_lx = event->caxis.value; pad.thumb_lx = event.caxis.value;
break; break;
case SDL_CONTROLLER_AXIS_LEFTY: case SDL_CONTROLLER_AXIS_LEFTY:
pad.thumb_ly = ~event->caxis.value; pad.thumb_ly = ~event.caxis.value;
break; break;
case SDL_CONTROLLER_AXIS_RIGHTX: case SDL_CONTROLLER_AXIS_RIGHTX:
pad.thumb_rx = event->caxis.value; pad.thumb_rx = event.caxis.value;
break; break;
case SDL_CONTROLLER_AXIS_RIGHTY: case SDL_CONTROLLER_AXIS_RIGHTY:
pad.thumb_ry = ~event->caxis.value; pad.thumb_ry = ~event.caxis.value;
break; break;
case SDL_CONTROLLER_AXIS_TRIGGERLEFT: case SDL_CONTROLLER_AXIS_TRIGGERLEFT:
pad.left_trigger = static_cast<uint8_t>(event->caxis.value >> 7); pad.left_trigger = static_cast<uint8_t>(event.caxis.value >> 7);
break; break;
case SDL_CONTROLLER_AXIS_TRIGGERRIGHT: case SDL_CONTROLLER_AXIS_TRIGGERRIGHT:
pad.right_trigger = static_cast<uint8_t>(event->caxis.value >> 7); pad.right_trigger = static_cast<uint8_t>(event.caxis.value >> 7);
break; break;
default: default:
assert_always(); assert_always();
break; 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()); assert(window()->loop()->is_on_loop_thread());
std::unique_lock<std::mutex> guard(controllers_mutex_); std::unique_lock<std::mutex> guard(controllers_mutex_);
@ -515,15 +515,15 @@ void SDLInputDriver::OnControllerDeviceButtonChanged(SDL_Event* event) {
X_INPUT_GAMEPAD_DPAD_LEFT, X_INPUT_GAMEPAD_DPAD_LEFT,
X_INPUT_GAMEPAD_DPAD_RIGHT}; X_INPUT_GAMEPAD_DPAD_RIGHT};
auto [found, i] = GetControllerIndexFromInstanceID(event->cbutton.which); auto idx = GetControllerIndexFromInstanceID(event.cbutton.which);
assert(found); assert(idx);
auto& controller = controllers_.at(i); auto& controller = controllers_.at(*idx);
uint16_t xbuttons = controller.state.gamepad.buttons; uint16_t xbuttons = controller.state.gamepad.buttons;
// Lookup the XInput button code. // Lookup the XInput button code.
auto xbutton = xbutton_lookup.at(event->cbutton.button); auto xbutton = xbutton_lookup.at(event.cbutton.button);
// Pressed or released? // Pressed or released?
if (event->cbutton.state == SDL_PRESSED) { if (event.cbutton.state == SDL_PRESSED) {
if (xbutton == X_INPUT_GAMEPAD_GUIDE && !cvars::guide_button) { if (xbutton == X_INPUT_GAMEPAD_GUIDE && !cvars::guide_button) {
return; return;
} }
@ -535,7 +535,7 @@ void SDLInputDriver::OnControllerDeviceButtonChanged(SDL_Event* event) {
controller.state_changed = true; controller.state_changed = true;
} }
std::pair<bool, size_t> SDLInputDriver::GetControllerIndexFromInstanceID( std::optional<size_t> SDLInputDriver::GetControllerIndexFromInstanceID(
SDL_JoystickID instance_id) { SDL_JoystickID instance_id) {
// Loop through our controllers and try to match the given ID. // Loop through our controllers and try to match the given ID.
for (size_t i = 0; i < controllers_.size(); i++) { for (size_t i = 0; i < controllers_.size(); i++) {
@ -548,10 +548,10 @@ std::pair<bool, size_t> SDLInputDriver::GetControllerIndexFromInstanceID(
auto joy_instance_id = SDL_JoystickInstanceID(joystick); auto joy_instance_id = SDL_JoystickInstanceID(joystick);
assert(joy_instance_id >= 0); assert(joy_instance_id >= 0);
if (joy_instance_id == instance_id) { if (joy_instance_id == instance_id) {
return {true, i}; return i;
} }
} }
return {false, 0}; return std::nullopt;
} }
SDLInputDriver::ControllerState* SDLInputDriver::GetControllerState( SDLInputDriver::ControllerState* SDLInputDriver::GetControllerState(

View File

@ -13,6 +13,7 @@
#include <array> #include <array>
#include <atomic> #include <atomic>
#include <mutex> #include <mutex>
#include <optional>
#include "SDL.h" #include "SDL.h"
#include "xenia/hid/input_driver.h" #include "xenia/hid/input_driver.h"
@ -64,11 +65,11 @@ class SDLInputDriver : public InputDriver {
}; };
protected: protected:
void OnControllerDeviceAdded(SDL_Event* event); void OnControllerDeviceAdded(const SDL_Event& event);
void OnControllerDeviceRemoved(SDL_Event* event); void OnControllerDeviceRemoved(const SDL_Event& event);
void OnControllerDeviceAxisMotion(SDL_Event* event); void OnControllerDeviceAxisMotion(const SDL_Event& event);
void OnControllerDeviceButtonChanged(SDL_Event* event); void OnControllerDeviceButtonChanged(const SDL_Event& event);
std::pair<bool, size_t> GetControllerIndexFromInstanceID( std::optional<size_t> GetControllerIndexFromInstanceID(
SDL_JoystickID instance_id); SDL_JoystickID instance_id);
ControllerState* GetControllerState(uint32_t user_index); ControllerState* GetControllerState(uint32_t user_index);
bool TestSDLVersion() const; bool TestSDLVersion() const;