summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoel Linn <jl@conductive.de>2020-11-28 16:17:08 +0100
committerRick Gibbed <rick@gibbed.us>2020-11-28 14:22:50 -0600
commit842ac86b1fb6a77133c9d705e930600c4c143a83 (patch)
treedf1d0c597232a85d68acac51689c4226ee2cbabc
parentff56fbdf46e8a15f73f6cc60f1445e25084d46c6 (diff)
[HID] More modern c++ in SDL backend.
-rw-r--r--src/xenia/hid/sdl/sdl_input_driver.cc68
-rw-r--r--src/xenia/hid/sdl/sdl_input_driver.h11
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<std::mutex> 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<std::mutex> 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<std::mutex> 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<uint8_t>(event->caxis.value >> 7);
+ pad.left_trigger = static_cast<uint8_t>(event.caxis.value >> 7);
break;
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;
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<std::mutex> 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<bool, size_t> SDLInputDriver::GetControllerIndexFromInstanceID(
+std::optional<size_t> 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<bool, size_t> 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 <array>
#include <atomic>
#include <mutex>
+#include <optional>
#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<bool, size_t> 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<size_t> GetControllerIndexFromInstanceID(
SDL_JoystickID instance_id);
ControllerState* GetControllerState(uint32_t user_index);
bool TestSDLVersion() const;