summaryrefslogtreecommitdiff
path: root/src/input_common/sdl/sdl_impl.cpp
diff options
context:
space:
mode:
authorGravatar bunnei2020-10-15 20:59:34 -0700
committerGravatar GitHub2020-10-15 20:59:34 -0700
commit64f967fd4958abb5a02191a81e91fc8b33bcf4c5 (patch)
tree97a73da4871f006b39eafca3a881ae2ea42f206a /src/input_common/sdl/sdl_impl.cpp
parentMerge pull request #4784 from bunnei/cancelbuffer (diff)
parentinput_common/CMakeLists: Make some warnings errors (diff)
downloadyuzu-64f967fd4958abb5a02191a81e91fc8b33bcf4c5.tar.gz
yuzu-64f967fd4958abb5a02191a81e91fc8b33bcf4c5.tar.xz
yuzu-64f967fd4958abb5a02191a81e91fc8b33bcf4c5.zip
Merge pull request #4790 from lioncash/input-common
input_common/CMakeLists: Make some warnings errors
Diffstat (limited to '')
-rw-r--r--src/input_common/sdl/sdl_impl.cpp77
1 files changed, 40 insertions, 37 deletions
diff --git a/src/input_common/sdl/sdl_impl.cpp b/src/input_common/sdl/sdl_impl.cpp
index bd480570a..8c2cef35d 100644
--- a/src/input_common/sdl/sdl_impl.cpp
+++ b/src/input_common/sdl/sdl_impl.cpp
@@ -56,9 +56,9 @@ static int SDLEventWatcher(void* user_data, SDL_Event* event) {
56class SDLJoystick { 56class SDLJoystick {
57public: 57public:
58 SDLJoystick(std::string guid_, int port_, SDL_Joystick* joystick, 58 SDLJoystick(std::string guid_, int port_, SDL_Joystick* joystick,
59 SDL_GameController* gamecontroller) 59 SDL_GameController* game_controller)
60 : guid{std::move(guid_)}, port{port_}, sdl_joystick{joystick, &SDL_JoystickClose}, 60 : guid{std::move(guid_)}, port{port_}, sdl_joystick{joystick, &SDL_JoystickClose},
61 sdl_controller{gamecontroller, &SDL_GameControllerClose} {} 61 sdl_controller{game_controller, &SDL_GameControllerClose} {}
62 62
63 void SetButton(int button, bool value) { 63 void SetButton(int button, bool value) {
64 std::lock_guard lock{mutex}; 64 std::lock_guard lock{mutex};
@@ -77,10 +77,10 @@ public:
77 77
78 float GetAxis(int axis, float range) const { 78 float GetAxis(int axis, float range) const {
79 std::lock_guard lock{mutex}; 79 std::lock_guard lock{mutex};
80 return state.axes.at(axis) / (32767.0f * range); 80 return static_cast<float>(state.axes.at(axis)) / (32767.0f * range);
81 } 81 }
82 82
83 bool RumblePlay(f32 amp_low, f32 amp_high, int time) { 83 bool RumblePlay(f32 amp_low, f32 amp_high, u32 time) {
84 const u16 raw_amp_low = static_cast<u16>(amp_low * 0xFFFF); 84 const u16 raw_amp_low = static_cast<u16>(amp_low * 0xFFFF);
85 const u16 raw_amp_high = static_cast<u16>(amp_high * 0xFFFF); 85 const u16 raw_amp_high = static_cast<u16>(amp_high * 0xFFFF);
86 // Lower drastically the number of state changes 86 // Lower drastically the number of state changes
@@ -124,7 +124,7 @@ public:
124 return std::make_tuple(x, y); 124 return std::make_tuple(x, y);
125 } 125 }
126 126
127 const InputCommon::MotionInput& GetMotion() const { 127 const MotionInput& GetMotion() const {
128 return motion; 128 return motion;
129 } 129 }
130 130
@@ -172,15 +172,15 @@ private:
172 } state; 172 } state;
173 std::string guid; 173 std::string guid;
174 int port; 174 int port;
175 u16 last_state_rumble_high; 175 u16 last_state_rumble_high = 0;
176 u16 last_state_rumble_low; 176 u16 last_state_rumble_low = 0;
177 std::chrono::time_point<std::chrono::system_clock> last_vibration; 177 std::chrono::time_point<std::chrono::system_clock> last_vibration;
178 std::unique_ptr<SDL_Joystick, decltype(&SDL_JoystickClose)> sdl_joystick; 178 std::unique_ptr<SDL_Joystick, decltype(&SDL_JoystickClose)> sdl_joystick;
179 std::unique_ptr<SDL_GameController, decltype(&SDL_GameControllerClose)> sdl_controller; 179 std::unique_ptr<SDL_GameController, decltype(&SDL_GameControllerClose)> sdl_controller;
180 mutable std::mutex mutex; 180 mutable std::mutex mutex;
181 181
182 // motion is initalized without PID values as motion input is not aviable for SDL2 182 // Motion is initialized without PID values as motion input is not aviable for SDL2
183 InputCommon::MotionInput motion{0.0f, 0.0f, 0.0f}; 183 MotionInput motion{0.0f, 0.0f, 0.0f};
184}; 184};
185 185
186std::shared_ptr<SDLJoystick> SDLState::GetSDLJoystickByGUID(const std::string& guid, int port) { 186std::shared_ptr<SDLJoystick> SDLState::GetSDLJoystickByGUID(const std::string& guid, int port) {
@@ -192,7 +192,7 @@ std::shared_ptr<SDLJoystick> SDLState::GetSDLJoystickByGUID(const std::string& g
192 nullptr, nullptr); 192 nullptr, nullptr);
193 it->second.emplace_back(std::move(joystick)); 193 it->second.emplace_back(std::move(joystick));
194 } 194 }
195 return it->second[port]; 195 return it->second[static_cast<std::size_t>(port)];
196 } 196 }
197 auto joystick = std::make_shared<SDLJoystick>(guid, 0, nullptr, nullptr); 197 auto joystick = std::make_shared<SDLJoystick>(guid, 0, nullptr, nullptr);
198 return joystick_map[guid].emplace_back(std::move(joystick)); 198 return joystick_map[guid].emplace_back(std::move(joystick));
@@ -212,7 +212,7 @@ std::shared_ptr<SDLJoystick> SDLState::GetSDLJoystickBySDLID(SDL_JoystickID sdl_
212 return sdl_joystick == joystick->GetSDLJoystick(); 212 return sdl_joystick == joystick->GetSDLJoystick();
213 }); 213 });
214 if (vec_it != map_it->second.end()) { 214 if (vec_it != map_it->second.end()) {
215 // This is the common case: There is already an existing SDL_Joystick maped to a 215 // This is the common case: There is already an existing SDL_Joystick mapped to a
216 // SDLJoystick. return the SDLJoystick 216 // SDLJoystick. return the SDLJoystick
217 return *vec_it; 217 return *vec_it;
218 } 218 }
@@ -220,7 +220,7 @@ std::shared_ptr<SDLJoystick> SDLState::GetSDLJoystickBySDLID(SDL_JoystickID sdl_
220 // Search for a SDLJoystick without a mapped SDL_Joystick... 220 // Search for a SDLJoystick without a mapped SDL_Joystick...
221 const auto nullptr_it = std::find_if(map_it->second.begin(), map_it->second.end(), 221 const auto nullptr_it = std::find_if(map_it->second.begin(), map_it->second.end(),
222 [](const std::shared_ptr<SDLJoystick>& joystick) { 222 [](const std::shared_ptr<SDLJoystick>& joystick) {
223 return !joystick->GetSDLJoystick(); 223 return joystick->GetSDLJoystick() == nullptr;
224 }); 224 });
225 if (nullptr_it != map_it->second.end()) { 225 if (nullptr_it != map_it->second.end()) {
226 // ... and map it 226 // ... and map it
@@ -273,22 +273,21 @@ void SDLState::InitJoystick(int joystick_index) {
273void SDLState::CloseJoystick(SDL_Joystick* sdl_joystick) { 273void SDLState::CloseJoystick(SDL_Joystick* sdl_joystick) {
274 const std::string guid = GetGUID(sdl_joystick); 274 const std::string guid = GetGUID(sdl_joystick);
275 275
276 std::shared_ptr<SDLJoystick> joystick; 276 std::shared_ptr<SDLJoystick> found_joystick;
277 { 277 {
278 std::lock_guard lock{joystick_map_mutex}; 278 std::lock_guard lock{joystick_map_mutex};
279 // This call to guid is safe since the joystick is guaranteed to be in the map 279 // This call to guid is safe since the joystick is guaranteed to be in the map
280 const auto& joystick_guid_list = joystick_map[guid]; 280 const auto& joystick_guid_list = joystick_map[guid];
281 const auto joystick_it = 281 const auto joystick_it = std::find_if(joystick_guid_list.begin(), joystick_guid_list.end(),
282 std::find_if(joystick_guid_list.begin(), joystick_guid_list.end(), 282 [&sdl_joystick](const auto& joystick) {
283 [&sdl_joystick](const std::shared_ptr<SDLJoystick>& joystick) { 283 return joystick->GetSDLJoystick() == sdl_joystick;
284 return joystick->GetSDLJoystick() == sdl_joystick; 284 });
285 }); 285 found_joystick = *joystick_it;
286 joystick = *joystick_it;
287 } 286 }
288 287
289 // Destruct SDL_Joystick outside the lock guard because SDL can internally call the 288 // Destruct SDL_Joystick outside the lock guard because SDL can internally call the
290 // event callback which locks the mutex again. 289 // event callback which locks the mutex again.
291 joystick->SetSDLJoystick(nullptr, nullptr); 290 found_joystick->SetSDLJoystick(nullptr, nullptr);
292} 291}
293 292
294void SDLState::HandleGameControllerEvent(const SDL_Event& event) { 293void SDLState::HandleGameControllerEvent(const SDL_Event& event) {
@@ -392,8 +391,8 @@ private:
392 391
393class SDLAnalog final : public Input::AnalogDevice { 392class SDLAnalog final : public Input::AnalogDevice {
394public: 393public:
395 SDLAnalog(std::shared_ptr<SDLJoystick> joystick_, int axis_x_, int axis_y_, float deadzone_, 394 explicit SDLAnalog(std::shared_ptr<SDLJoystick> joystick_, int axis_x_, int axis_y_,
396 float range_) 395 float deadzone_, float range_)
397 : joystick(std::move(joystick_)), axis_x(axis_x_), axis_y(axis_y_), deadzone(deadzone_), 396 : joystick(std::move(joystick_)), axis_x(axis_x_), axis_y(axis_y_), deadzone(deadzone_),
398 range(range_) {} 397 range(range_) {}
399 398
@@ -672,13 +671,13 @@ SDLState::SDLState() {
672 RegisterFactory<ButtonDevice>("sdl", button_factory); 671 RegisterFactory<ButtonDevice>("sdl", button_factory);
673 RegisterFactory<MotionDevice>("sdl", motion_factory); 672 RegisterFactory<MotionDevice>("sdl", motion_factory);
674 673
675 // If the frontend is going to manage the event loop, then we dont start one here 674 // If the frontend is going to manage the event loop, then we don't start one here
676 start_thread = !SDL_WasInit(SDL_INIT_JOYSTICK); 675 start_thread = SDL_WasInit(SDL_INIT_JOYSTICK) == 0;
677 if (start_thread && SDL_Init(SDL_INIT_JOYSTICK) < 0) { 676 if (start_thread && SDL_Init(SDL_INIT_JOYSTICK) < 0) {
678 LOG_CRITICAL(Input, "SDL_Init(SDL_INIT_JOYSTICK) failed with: {}", SDL_GetError()); 677 LOG_CRITICAL(Input, "SDL_Init(SDL_INIT_JOYSTICK) failed with: {}", SDL_GetError());
679 return; 678 return;
680 } 679 }
681 has_gamecontroller = SDL_InitSubSystem(SDL_INIT_GAMECONTROLLER); 680 has_gamecontroller = SDL_InitSubSystem(SDL_INIT_GAMECONTROLLER) != 0;
682 if (SDL_SetHint(SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS, "1") == SDL_FALSE) { 681 if (SDL_SetHint(SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS, "1") == SDL_FALSE) {
683 LOG_ERROR(Input, "Failed to set hint for background events with: {}", SDL_GetError()); 682 LOG_ERROR(Input, "Failed to set hint for background events with: {}", SDL_GetError());
684 } 683 }
@@ -723,8 +722,8 @@ std::vector<Common::ParamPackage> SDLState::GetInputDevices() {
723 std::vector<Common::ParamPackage> devices; 722 std::vector<Common::ParamPackage> devices;
724 for (const auto& [key, value] : joystick_map) { 723 for (const auto& [key, value] : joystick_map) {
725 for (const auto& joystick : value) { 724 for (const auto& joystick : value) {
726 auto joy = joystick->GetSDLJoystick(); 725 auto* joy = joystick->GetSDLJoystick();
727 if (auto controller = joystick->GetSDLGameController()) { 726 if (auto* controller = joystick->GetSDLGameController()) {
728 std::string name = 727 std::string name =
729 fmt::format("{} {}", SDL_GameControllerName(controller), joystick->GetPort()); 728 fmt::format("{} {}", SDL_GameControllerName(controller), joystick->GetPort());
730 devices.emplace_back(Common::ParamPackage{ 729 devices.emplace_back(Common::ParamPackage{
@@ -748,7 +747,7 @@ std::vector<Common::ParamPackage> SDLState::GetInputDevices() {
748} 747}
749 748
750namespace { 749namespace {
751Common::ParamPackage BuildAnalogParamPackageForButton(int port, std::string guid, u8 axis, 750Common::ParamPackage BuildAnalogParamPackageForButton(int port, std::string guid, s32 axis,
752 float value = 0.1f) { 751 float value = 0.1f) {
753 Common::ParamPackage params({{"engine", "sdl"}}); 752 Common::ParamPackage params({{"engine", "sdl"}});
754 params.Set("port", port); 753 params.Set("port", port);
@@ -764,7 +763,7 @@ Common::ParamPackage BuildAnalogParamPackageForButton(int port, std::string guid
764 return params; 763 return params;
765} 764}
766 765
767Common::ParamPackage BuildButtonParamPackageForButton(int port, std::string guid, u8 button) { 766Common::ParamPackage BuildButtonParamPackageForButton(int port, std::string guid, s32 button) {
768 Common::ParamPackage params({{"engine", "sdl"}}); 767 Common::ParamPackage params({{"engine", "sdl"}});
769 params.Set("port", port); 768 params.Set("port", port);
770 params.Set("guid", std::move(guid)); 769 params.Set("guid", std::move(guid));
@@ -772,7 +771,7 @@ Common::ParamPackage BuildButtonParamPackageForButton(int port, std::string guid
772 return params; 771 return params;
773} 772}
774 773
775Common::ParamPackage BuildHatParamPackageForButton(int port, std::string guid, u8 hat, u8 value) { 774Common::ParamPackage BuildHatParamPackageForButton(int port, std::string guid, s32 hat, s32 value) {
776 Common::ParamPackage params({{"engine", "sdl"}}); 775 Common::ParamPackage params({{"engine", "sdl"}});
777 776
778 params.Set("port", port); 777 params.Set("port", port);
@@ -802,17 +801,19 @@ Common::ParamPackage SDLEventToButtonParamPackage(SDLState& state, const SDL_Eve
802 case SDL_JOYAXISMOTION: { 801 case SDL_JOYAXISMOTION: {
803 const auto joystick = state.GetSDLJoystickBySDLID(event.jaxis.which); 802 const auto joystick = state.GetSDLJoystickBySDLID(event.jaxis.which);
804 return BuildAnalogParamPackageForButton(joystick->GetPort(), joystick->GetGUID(), 803 return BuildAnalogParamPackageForButton(joystick->GetPort(), joystick->GetGUID(),
805 event.jaxis.axis, event.jaxis.value); 804 static_cast<s32>(event.jaxis.axis),
805 event.jaxis.value);
806 } 806 }
807 case SDL_JOYBUTTONUP: { 807 case SDL_JOYBUTTONUP: {
808 const auto joystick = state.GetSDLJoystickBySDLID(event.jbutton.which); 808 const auto joystick = state.GetSDLJoystickBySDLID(event.jbutton.which);
809 return BuildButtonParamPackageForButton(joystick->GetPort(), joystick->GetGUID(), 809 return BuildButtonParamPackageForButton(joystick->GetPort(), joystick->GetGUID(),
810 event.jbutton.button); 810 static_cast<s32>(event.jbutton.button));
811 } 811 }
812 case SDL_JOYHATMOTION: { 812 case SDL_JOYHATMOTION: {
813 const auto joystick = state.GetSDLJoystickBySDLID(event.jhat.which); 813 const auto joystick = state.GetSDLJoystickBySDLID(event.jhat.which);
814 return BuildHatParamPackageForButton(joystick->GetPort(), joystick->GetGUID(), 814 return BuildHatParamPackageForButton(joystick->GetPort(), joystick->GetGUID(),
815 event.jhat.hat, event.jhat.value); 815 static_cast<s32>(event.jhat.hat),
816 static_cast<s32>(event.jhat.value));
816 } 817 }
817 } 818 }
818 return {}; 819 return {};
@@ -823,17 +824,19 @@ Common::ParamPackage SDLEventToMotionParamPackage(SDLState& state, const SDL_Eve
823 case SDL_JOYAXISMOTION: { 824 case SDL_JOYAXISMOTION: {
824 const auto joystick = state.GetSDLJoystickBySDLID(event.jaxis.which); 825 const auto joystick = state.GetSDLJoystickBySDLID(event.jaxis.which);
825 return BuildAnalogParamPackageForButton(joystick->GetPort(), joystick->GetGUID(), 826 return BuildAnalogParamPackageForButton(joystick->GetPort(), joystick->GetGUID(),
826 event.jaxis.axis, event.jaxis.value); 827 static_cast<s32>(event.jaxis.axis),
828 event.jaxis.value);
827 } 829 }
828 case SDL_JOYBUTTONUP: { 830 case SDL_JOYBUTTONUP: {
829 const auto joystick = state.GetSDLJoystickBySDLID(event.jbutton.which); 831 const auto joystick = state.GetSDLJoystickBySDLID(event.jbutton.which);
830 return BuildButtonParamPackageForButton(joystick->GetPort(), joystick->GetGUID(), 832 return BuildButtonParamPackageForButton(joystick->GetPort(), joystick->GetGUID(),
831 event.jbutton.button); 833 static_cast<s32>(event.jbutton.button));
832 } 834 }
833 case SDL_JOYHATMOTION: { 835 case SDL_JOYHATMOTION: {
834 const auto joystick = state.GetSDLJoystickBySDLID(event.jhat.which); 836 const auto joystick = state.GetSDLJoystickBySDLID(event.jhat.which);
835 return BuildHatParamPackageForButton(joystick->GetPort(), joystick->GetGUID(), 837 return BuildHatParamPackageForButton(joystick->GetPort(), joystick->GetGUID(),
836 event.jhat.hat, event.jhat.value); 838 static_cast<s32>(event.jhat.hat),
839 static_cast<s32>(event.jhat.value));
837 } 840 }
838 } 841 }
839 return {}; 842 return {};
@@ -1062,7 +1065,7 @@ public:
1062 if (event.type == SDL_JOYAXISMOTION) { 1065 if (event.type == SDL_JOYAXISMOTION) {
1063 const auto axis = event.jaxis.axis; 1066 const auto axis = event.jaxis.axis;
1064 const auto joystick = state.GetSDLJoystickBySDLID(event.jaxis.which); 1067 const auto joystick = state.GetSDLJoystickBySDLID(event.jaxis.which);
1065 const auto controller = joystick->GetSDLGameController(); 1068 auto* const controller = joystick->GetSDLGameController();
1066 if (controller) { 1069 if (controller) {
1067 const auto axis_left_x = 1070 const auto axis_left_x =
1068 SDL_GameControllerGetBindForAxis(controller, SDL_CONTROLLER_AXIS_LEFTX) 1071 SDL_GameControllerGetBindForAxis(controller, SDL_CONTROLLER_AXIS_LEFTX)