summaryrefslogtreecommitdiff
path: root/src/input_common/drivers/sdl_driver.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/input_common/drivers/sdl_driver.h')
-rw-r--r--src/input_common/drivers/sdl_driver.h120
1 files changed, 120 insertions, 0 deletions
diff --git a/src/input_common/drivers/sdl_driver.h b/src/input_common/drivers/sdl_driver.h
new file mode 100644
index 000000000..d8d350184
--- /dev/null
+++ b/src/input_common/drivers/sdl_driver.h
@@ -0,0 +1,120 @@
1// Copyright 2018 Citra Emulator Project
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#pragma once
6
7#include <atomic>
8#include <mutex>
9#include <thread>
10#include <unordered_map>
11
12#include <SDL.h>
13
14#include "common/common_types.h"
15#include "input_common/input_engine.h"
16
17union SDL_Event;
18using SDL_GameController = struct _SDL_GameController;
19using SDL_Joystick = struct _SDL_Joystick;
20using SDL_JoystickID = s32;
21
22using ButtonBindings =
23 std::array<std::pair<Settings::NativeButton::Values, SDL_GameControllerButton>, 17>;
24using ZButtonBindings =
25 std::array<std::pair<Settings::NativeButton::Values, SDL_GameControllerAxis>, 2>;
26
27namespace InputCommon {
28
29class SDLJoystick;
30
31class SDLDriver : public InputCommon::InputEngine {
32public:
33 /// Initializes and registers SDL device factories
34 SDLDriver(const std::string& input_engine_);
35
36 /// Unregisters SDL device factories and shut them down.
37 ~SDLDriver() override;
38
39 /// Handle SDL_Events for joysticks from SDL_PollEvent
40 void HandleGameControllerEvent(const SDL_Event& event);
41
42 /// Get the nth joystick with the corresponding GUID
43 std::shared_ptr<SDLJoystick> GetSDLJoystickBySDLID(SDL_JoystickID sdl_id);
44
45 /**
46 * Check how many identical joysticks (by guid) were connected before the one with sdl_id and so
47 * tie it to a SDLJoystick with the same guid and that port
48 */
49 std::shared_ptr<SDLJoystick> GetSDLJoystickByGUID(const std::string& guid, int port);
50
51 std::vector<Common::ParamPackage> GetInputDevices() const override;
52
53 ButtonMapping GetButtonMappingForDevice(const Common::ParamPackage& params) override;
54 AnalogMapping GetAnalogMappingForDevice(const Common::ParamPackage& params) override;
55 MotionMapping GetMotionMappingForDevice(const Common::ParamPackage& params) override;
56 std::string GetUIName(const Common::ParamPackage& params) const override;
57
58 std::string GetHatButtonName(u8 direction_value) const override;
59 u8 GetHatButtonId(const std::string direction_name) const override;
60
61 bool SetRumble(const PadIdentifier& identifier,
62 const Input::VibrationStatus vibration) override;
63
64private:
65 void InitJoystick(int joystick_index);
66 void CloseJoystick(SDL_Joystick* sdl_joystick);
67
68 /// Needs to be called before SDL_QuitSubSystem.
69 void CloseJoysticks();
70
71 Common::ParamPackage BuildAnalogParamPackageForButton(int port, std::string guid, s32 axis,
72 float value = 0.1f) const;
73 Common::ParamPackage BuildButtonParamPackageForButton(int port, std::string guid,
74 s32 button) const;
75
76 Common::ParamPackage BuildHatParamPackageForButton(int port, std::string guid, s32 hat,
77 u8 value) const;
78
79 Common::ParamPackage BuildMotionParam(int port, std::string guid) const;
80
81 Common::ParamPackage BuildParamPackageForBinding(
82 int port, const std::string& guid, const SDL_GameControllerButtonBind& binding) const;
83
84 Common::ParamPackage BuildParamPackageForAnalog(PadIdentifier identifier, int axis_x,
85 int axis_y, float offset_x,
86 float offset_y) const;
87
88 /// Returns the default button bindings list for generic controllers
89 ButtonBindings GetDefaultButtonBinding() const;
90
91 /// Returns the default button bindings list for nintendo controllers
92 ButtonBindings GetNintendoButtonBinding(const std::shared_ptr<SDLJoystick>& joystick) const;
93
94 /// Returns the button mappings from a single controller
95 ButtonMapping GetSingleControllerMapping(const std::shared_ptr<SDLJoystick>& joystick,
96 const ButtonBindings& switch_to_sdl_button,
97 const ZButtonBindings& switch_to_sdl_axis) const;
98
99 /// Returns the button mappings from two different controllers
100 ButtonMapping GetDualControllerMapping(const std::shared_ptr<SDLJoystick>& joystick,
101 const std::shared_ptr<SDLJoystick>& joystick2,
102 const ButtonBindings& switch_to_sdl_button,
103 const ZButtonBindings& switch_to_sdl_axis) const;
104
105 /// Returns true if the button is on the left joycon
106 bool IsButtonOnLeftSide(Settings::NativeButton::Values button) const;
107
108 // Set to true if SDL supports game controller subsystem
109 bool has_gamecontroller = false;
110
111 /// Map of GUID of a list of corresponding virtual Joysticks
112 std::unordered_map<std::string, std::vector<std::shared_ptr<SDLJoystick>>> joystick_map;
113 std::mutex joystick_map_mutex;
114
115 bool start_thread = false;
116 std::atomic<bool> initialized = false;
117
118 std::thread poll_thread;
119};
120} // namespace InputCommon