summaryrefslogtreecommitdiff
path: root/src/input_common/sdl/sdl_impl.cpp
diff options
context:
space:
mode:
authorGravatar B3n302018-10-01 15:10:37 +0200
committerGravatar fearlessTobi2019-03-02 19:12:46 +0100
commit71817afbe9483f7b2258a8891d0a6730c8c7b71e (patch)
tree78e568c08bb5ddd18b578ebb3032bdc93524cadc /src/input_common/sdl/sdl_impl.cpp
parentinput/sdl: lock map mutex after SDL call (diff)
downloadyuzu-71817afbe9483f7b2258a8891d0a6730c8c7b71e.tar.gz
yuzu-71817afbe9483f7b2258a8891d0a6730c8c7b71e.tar.xz
yuzu-71817afbe9483f7b2258a8891d0a6730c8c7b71e.zip
fixup! Joystick: Allow for background events; Add deadzone to SDLAnalog
Diffstat (limited to 'src/input_common/sdl/sdl_impl.cpp')
-rw-r--r--src/input_common/sdl/sdl_impl.cpp23
1 files changed, 17 insertions, 6 deletions
diff --git a/src/input_common/sdl/sdl_impl.cpp b/src/input_common/sdl/sdl_impl.cpp
index f5dbd015e..934339d3b 100644
--- a/src/input_common/sdl/sdl_impl.cpp
+++ b/src/input_common/sdl/sdl_impl.cpp
@@ -330,17 +330,24 @@ private:
330 330
331class SDLAnalog final : public Input::AnalogDevice { 331class SDLAnalog final : public Input::AnalogDevice {
332public: 332public:
333 SDLAnalog(std::shared_ptr<SDLJoystick> joystick_, int axis_x_, int axis_y_) 333 SDLAnalog(std::shared_ptr<SDLJoystick> joystick_, int axis_x_, int axis_y_, float deadzone_)
334 : joystick(std::move(joystick_)), axis_x(axis_x_), axis_y(axis_y_) {} 334 : joystick(std::move(joystick_)), axis_x(axis_x_), axis_y(axis_y_), deadzone(deadzone_) {}
335 335
336 std::tuple<float, float> GetStatus() const override { 336 std::tuple<float, float> GetStatus() const override {
337 return joystick->GetAnalog(axis_x, axis_y); 337 const auto [x, y] = joystick->GetAnalog(axis_x, axis_y);
338 const float r = std::sqrt((x * x) + (y * y));
339 if (r > deadzone) {
340 return std::make_tuple(x / r * (r - deadzone) / (1 - deadzone),
341 y / r * (r - deadzone) / (1 - deadzone));
342 }
343 return std::make_tuple<float, float>(0.0f, 0.0f);
338 } 344 }
339 345
340private: 346private:
341 std::shared_ptr<SDLJoystick> joystick; 347 std::shared_ptr<SDLJoystick> joystick;
342 int axis_x; 348 const int axis_x;
343 int axis_y; 349 const int axis_y;
350 const float deadzone;
344}; 351};
345 352
346/// A button device factory that creates button devices from SDL joystick 353/// A button device factory that creates button devices from SDL joystick
@@ -435,13 +442,14 @@ public:
435 const int port = params.Get("port", 0); 442 const int port = params.Get("port", 0);
436 const int axis_x = params.Get("axis_x", 0); 443 const int axis_x = params.Get("axis_x", 0);
437 const int axis_y = params.Get("axis_y", 1); 444 const int axis_y = params.Get("axis_y", 1);
445 float deadzone = std::clamp(params.Get("deadzone", 0.0f), 0.0f, .99f);
438 446
439 auto joystick = state.GetSDLJoystickByGUID(guid, port); 447 auto joystick = state.GetSDLJoystickByGUID(guid, port);
440 448
441 // This is necessary so accessing GetAxis with axis_x and axis_y won't crash 449 // This is necessary so accessing GetAxis with axis_x and axis_y won't crash
442 joystick->SetAxis(axis_x, 0); 450 joystick->SetAxis(axis_x, 0);
443 joystick->SetAxis(axis_y, 0); 451 joystick->SetAxis(axis_y, 0);
444 return std::make_unique<SDLAnalog>(joystick, axis_x, axis_y); 452 return std::make_unique<SDLAnalog>(joystick, axis_x, axis_y, deadzone);
445 } 453 }
446 454
447private: 455private:
@@ -459,6 +467,9 @@ SDLState::SDLState() {
459 LOG_CRITICAL(Input, "SDL_Init(SDL_INIT_JOYSTICK) failed with: {}", SDL_GetError()); 467 LOG_CRITICAL(Input, "SDL_Init(SDL_INIT_JOYSTICK) failed with: {}", SDL_GetError());
460 return; 468 return;
461 } 469 }
470 if (SDL_SetHint(SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS, "1") == SDL_FALSE) {
471 LOG_ERROR(Input, "Failed to set Hint for background events", SDL_GetError());
472 }
462 473
463 SDL_AddEventWatch(&SDLEventWatcher, this); 474 SDL_AddEventWatch(&SDLEventWatcher, this);
464 475