diff --git a/src/frontend/InputSettings.cpp b/src/frontend/InputSettings.cpp index 4370325a..3fec129e 100644 --- a/src/frontend/InputSettings.cpp +++ b/src/frontend/InputSettings.cpp @@ -2,6 +2,9 @@ #include #include +bool QueryDevices(void *userdata, SDL_Event *event); +bool PollGamepad(void *userdata, SDL_Event *event); + InputSettings::InputSettings(nlohmann::json &settings) : settings(settings) { int i = 0; for(auto& kb : kbButtons) { @@ -11,6 +14,8 @@ InputSettings::InputSettings(nlohmann::json &settings) : settings(settings) { devices.addItem({"Keyboard/Mouse"}); SDL_InitSubSystem(SDL_INIT_GAMEPAD); + SDL_AddEventWatch(QueryDevices, this); + SDL_AddEventWatch(PollGamepad, this); } bool InputSettings::render() { @@ -32,9 +37,6 @@ bool InputSettings::render() { whichGrabbing = i; } - QueryDevices(); - PollGamepad(); - if((i % 2 == 0) || i == 0) // only go down every 2 buttons... just... i like it this way ImGui::SameLine(); @@ -56,99 +58,97 @@ std::array InputSettings::GetMappedKeys() { return ret; } -void InputSettings::QueryDevices() noexcept { - if (!devices.isEnabled()) - return; +bool QueryDevices(void *userdata, SDL_Event *event) { + auto _this = (InputSettings*)userdata; + if (!_this->devices.isEnabled()) + return false; - SDL_Event e; - while (SDL_PollEvent(&e)) { - switch (e.type) { - case SDL_EVENT_GAMEPAD_ADDED: - { - const auto index = e.gdevice.which; + switch (event->type) { + case SDL_EVENT_GAMEPAD_ADDED: + { + const auto index = event->gdevice.which; - const auto gamepad = SDL_OpenGamepad(index); - Util::info("Found controller!"); - const auto serial = SDL_GetGamepadSerial(gamepad); - const auto name = SDL_GetGamepadName(gamepad); - const auto path = SDL_GetGamepadPath(gamepad); + const auto gamepad = SDL_OpenGamepad(index); + Util::info("Found controller!"); + const auto serial = SDL_GetGamepadSerial(gamepad); + const auto name = SDL_GetGamepadName(gamepad); + const auto path = SDL_GetGamepadPath(gamepad); - if (name) { - if (!gamepadIndexes.contains(index)) { - gamepadIndexes[index] = name; - } - devices.addItem({name}); - } else if (serial) { - if (!gamepadIndexes.contains(index)) { - gamepadIndexes[index] = serial; - } - devices.addItem({serial}); - } else if (path) { - if (!gamepadIndexes.contains(index)) { - gamepadIndexes[index] = path; - } - devices.addItem({path}); + if (name) { + if (!_this->gamepadIndexes.contains(index)) { + _this->gamepadIndexes[index] = name; } - - SDL_CloseGamepad(gamepad); + _this->devices.addItem({name}); + } else if (serial) { + if (!_this->gamepadIndexes.contains(index)) { + _this->gamepadIndexes[index] = serial; + } + _this->devices.addItem({serial}); + } else if (path) { + if (!_this->gamepadIndexes.contains(index)) { + _this->gamepadIndexes[index] = path; + } + _this->devices.addItem({path}); } - break; - case SDL_EVENT_GAMEPAD_REMOVED: - { - const auto index = e.gdevice.which; - if (gamepadIndexes.contains(index)) - devices.removeItem(gamepadIndexes[index]); - } - break; + SDL_CloseGamepad(gamepad); } + return true; + case SDL_EVENT_GAMEPAD_REMOVED: + { + const auto index = event->gdevice.which; + + if (_this->gamepadIndexes.contains(index)) + _this->devices.removeItem(_this->gamepadIndexes[index]); + } + return true; + default: + return false; } } -void InputSettings::PollGamepad() noexcept { - SDL_Event e; - while (SDL_PollEvent(&e)) { - switch (e.type) { - case SDL_EVENT_GAMEPAD_BUTTON_DOWN: - { - if (!selectedDeviceIsNotKeyboard) - return; - - const auto k = SDL_GetGamepadStringForButton(static_cast(e.gbutton.button)); - JSONSetField(settings, "input", kbButtons[whichGrabbing].getName(), k); - kbButtons[whichGrabbing].setLabel(k); - devices.setEnabled(true); - for (auto &kbButton : kbButtons) { - kbButton.setEnabled(true); - } - - grabbing = false; - whichGrabbing = -1; - - modified = true; +bool PollGamepad(void *userdata, SDL_Event *event) { + auto _this = (InputSettings*)userdata; + switch (event->type) { + case SDL_EVENT_GAMEPAD_BUTTON_DOWN: + { + if (!_this->selectedDeviceIsNotKeyboard) + return false; + + const auto k = SDL_GetGamepadStringForButton(static_cast(event->gbutton.button)); + JSONSetField(_this->settings, "input", _this->kbButtons[_this->whichGrabbing].getName(), k); + _this->kbButtons[_this->whichGrabbing].setLabel(k); + _this->devices.setEnabled(true); + for (auto &kbButton : _this->kbButtons) { + kbButton.setEnabled(true); } - break; - case SDL_EVENT_KEY_DOWN: - { - if (selectedDeviceIsNotKeyboard) - return; - - const auto k = SDL_GetKeyName(e.key.key); - JSONSetField(settings, "input", kbButtons[whichGrabbing].getName(), k); - kbButtons[whichGrabbing].setLabel(k); - devices.setEnabled(true); - for (auto &kbButton : kbButtons) { - kbButton.setEnabled(true); - } - - grabbing = false; - whichGrabbing = -1; - - modified = true; - } - break; - default: - break; + + _this->grabbing = false; + _this->whichGrabbing = -1; + + _this->modified = true; } + return true; + case SDL_EVENT_KEY_DOWN: + { + if (_this->selectedDeviceIsNotKeyboard) + return false; + + const auto k = SDL_GetKeyName(event->key.key); + JSONSetField(_this->settings, "input", _this->kbButtons[_this->whichGrabbing].getName(), k); + _this->kbButtons[_this->whichGrabbing].setLabel(k); + _this->devices.setEnabled(true); + for (auto &kbButton : _this->kbButtons) { + kbButton.setEnabled(true); + } + + _this->grabbing = false; + _this->whichGrabbing = -1; + + _this->modified = true; + } + return true; + default: + return false; } } diff --git a/src/frontend/InputSettings.hpp b/src/frontend/InputSettings.hpp index 2f5f4be6..d5b8d8ee 100644 --- a/src/frontend/InputSettings.hpp +++ b/src/frontend/InputSettings.hpp @@ -6,12 +6,10 @@ #include class InputSettings final { +public: bool grabbing = false; int whichGrabbing = -1; - void QueryDevices() noexcept; - void PollGamepad() noexcept; - std::unordered_map gamepadIndexes{}; std::array kbButtons = { gui::PushButton{"", "A"}, @@ -54,7 +52,6 @@ class InputSettings final { //std::unique_ptr devicesLabel = std::make_unique("Device:"); //std::unique_ptr devices = std::make_unique(); //Q_OBJECT -public: bool render(); bool selectedDeviceIsNotKeyboard = false; explicit InputSettings(nlohmann::json &);