summaryrefslogtreecommitdiff
path: root/src/input_common
diff options
context:
space:
mode:
Diffstat (limited to 'src/input_common')
-rw-r--r--src/input_common/sdl/sdl_impl.cpp54
-rw-r--r--src/input_common/udp/client.cpp23
2 files changed, 51 insertions, 26 deletions
diff --git a/src/input_common/sdl/sdl_impl.cpp b/src/input_common/sdl/sdl_impl.cpp
index 822d0b555..b9b584b2a 100644
--- a/src/input_common/sdl/sdl_impl.cpp
+++ b/src/input_common/sdl/sdl_impl.cpp
@@ -323,7 +323,9 @@ void SDLState::CloseJoystick(SDL_Joystick* sdl_joystick) {
323 return joystick->GetSDLJoystick() == sdl_joystick; 323 return joystick->GetSDLJoystick() == sdl_joystick;
324 }); 324 });
325 325
326 (*joystick_it)->SetSDLJoystick(nullptr, nullptr); 326 if (joystick_it != joystick_guid_list.end()) {
327 (*joystick_it)->SetSDLJoystick(nullptr, nullptr);
328 }
327} 329}
328 330
329void SDLState::HandleGameControllerEvent(const SDL_Event& event) { 331void SDLState::HandleGameControllerEvent(const SDL_Event& event) {
@@ -1315,51 +1317,51 @@ public:
1315 void Start(const std::string& device_id) override { 1317 void Start(const std::string& device_id) override {
1316 SDLPoller::Start(device_id); 1318 SDLPoller::Start(device_id);
1317 // Reset stored axes 1319 // Reset stored axes
1318 analog_x_axis = -1; 1320 first_axis = -1;
1319 analog_y_axis = -1;
1320 } 1321 }
1321 1322
1322 Common::ParamPackage GetNextInput() override { 1323 Common::ParamPackage GetNextInput() override {
1323 SDL_Event event; 1324 SDL_Event event;
1324 while (state.event_queue.Pop(event)) { 1325 while (state.event_queue.Pop(event)) {
1325 // Filter out axis events that are below a threshold 1326 if (event.type != SDL_JOYAXISMOTION) {
1326 if (event.type == SDL_JOYAXISMOTION && std::abs(event.jaxis.value / 32767.0) < 0.5) { 1327 // Check for a button press
1327 continue;
1328 }
1329 if (event.type == SDL_JOYAXISMOTION) {
1330 const auto axis = event.jaxis.axis;
1331 // In order to return a complete analog param, we need inputs for both axes.
1332 // First we take the x-axis (horizontal) input, then the y-axis (vertical) input.
1333 if (analog_x_axis == -1) {
1334 analog_x_axis = axis;
1335 } else if (analog_y_axis == -1 && analog_x_axis != axis) {
1336 analog_y_axis = axis;
1337 }
1338 } else {
1339 // If the press wasn't accepted as a joy axis, check for a button press
1340 auto button_press = button_poller.FromEvent(event); 1328 auto button_press = button_poller.FromEvent(event);
1341 if (button_press) { 1329 if (button_press) {
1342 return *button_press; 1330 return *button_press;
1343 } 1331 }
1332 continue;
1333 }
1334 const auto axis = event.jaxis.axis;
1335
1336 // Filter out axis events that are below a threshold
1337 if (std::abs(event.jaxis.value / 32767.0) < 0.5) {
1338 continue;
1339 }
1340
1341 // Filter out axis events that are the same
1342 if (first_axis == axis) {
1343 continue;
1344 }
1345
1346 // In order to return a complete analog param, we need inputs for both axes.
1347 // If the first axis isn't set we set the value then wait till next event
1348 if (first_axis == -1) {
1349 first_axis = axis;
1350 continue;
1344 } 1351 }
1345 }
1346 1352
1347 if (analog_x_axis != -1 && analog_y_axis != -1) {
1348 if (const auto joystick = state.GetSDLJoystickBySDLID(event.jaxis.which)) { 1353 if (const auto joystick = state.GetSDLJoystickBySDLID(event.jaxis.which)) {
1349 auto params = BuildParamPackageForAnalog(joystick->GetPort(), joystick->GetGUID(), 1354 auto params = BuildParamPackageForAnalog(joystick->GetPort(), joystick->GetGUID(),
1350 analog_x_axis, analog_y_axis); 1355 first_axis, axis);
1351 analog_x_axis = -1; 1356 first_axis = -1;
1352 analog_y_axis = -1;
1353 return params; 1357 return params;
1354 } 1358 }
1355 } 1359 }
1356
1357 return {}; 1360 return {};
1358 } 1361 }
1359 1362
1360private: 1363private:
1361 int analog_x_axis = -1; 1364 int first_axis = -1;
1362 int analog_y_axis = -1;
1363 SDLButtonPoller button_poller; 1365 SDLButtonPoller button_poller;
1364}; 1366};
1365} // namespace Polling 1367} // namespace Polling
diff --git a/src/input_common/udp/client.cpp b/src/input_common/udp/client.cpp
index 8a38a380d..bc1dfab3d 100644
--- a/src/input_common/udp/client.cpp
+++ b/src/input_common/udp/client.cpp
@@ -86,6 +86,7 @@ private:
86 case Type::PadData: { 86 case Type::PadData: {
87 Response::PadData pad_data; 87 Response::PadData pad_data;
88 std::memcpy(&pad_data, &receive_buffer[sizeof(Header)], sizeof(Response::PadData)); 88 std::memcpy(&pad_data, &receive_buffer[sizeof(Header)], sizeof(Response::PadData));
89 SanitizeMotion(pad_data);
89 callback.pad_data(std::move(pad_data)); 90 callback.pad_data(std::move(pad_data));
90 break; 91 break;
91 } 92 }
@@ -114,6 +115,28 @@ private:
114 StartSend(timer.expiry()); 115 StartSend(timer.expiry());
115 } 116 }
116 117
118 void SanitizeMotion(Response::PadData& data) {
119 // Zero out any non number value
120 if (!std::isnormal(data.gyro.pitch)) {
121 data.gyro.pitch = 0;
122 }
123 if (!std::isnormal(data.gyro.roll)) {
124 data.gyro.roll = 0;
125 }
126 if (!std::isnormal(data.gyro.yaw)) {
127 data.gyro.yaw = 0;
128 }
129 if (!std::isnormal(data.accel.x)) {
130 data.accel.x = 0;
131 }
132 if (!std::isnormal(data.accel.y)) {
133 data.accel.y = 0;
134 }
135 if (!std::isnormal(data.accel.z)) {
136 data.accel.z = 0;
137 }
138 }
139
117 SocketCallback callback; 140 SocketCallback callback;
118 boost::asio::io_service io_service; 141 boost::asio::io_service io_service;
119 boost::asio::basic_waitable_timer<clock> timer; 142 boost::asio::basic_waitable_timer<clock> timer;