diff --git a/src/citra_qt/config.cpp b/src/citra_qt/config.cpp index ae27dd497..67670e018 100644 --- a/src/citra_qt/config.cpp +++ b/src/citra_qt/config.cpp @@ -125,7 +125,7 @@ void Config::SaveValues() { qt_config->beginGroup("Controls"); for (int i = 0; i < Settings::NativeInput::NUM_INPUTS; ++i) { qt_config->setValue(QString::fromStdString(Settings::NativeInput::Mapping[i]), - QString::fromStdString(Settings::values.input_mappings[Settings::NativeInput::All[i]].toString())); + QString::fromStdString(Settings::values.input_mappings[Settings::NativeInput::All[i]].ToString())); } qt_config->setValue("pad_circle_modifier_scale", (double)Settings::values.pad_circle_modifier_scale); qt_config->endGroup(); diff --git a/src/core/settings.h b/src/core/settings.h index ae1032c68..ffcd522ab 100644 --- a/src/core/settings.h +++ b/src/core/settings.h @@ -52,62 +52,57 @@ namespace Settings { Keyboard, Gamepad }; struct InputDeviceMapping { - DeviceFramework framework; - int number; - Device device; + DeviceFramework framework = DeviceFramework::Qt; + int number = 0; + Device device = Device::Keyboard; std::string key; - InputDeviceMapping() { - this->framework = DeviceFramework::Qt; - this->number = 0; - this->device = Device::Keyboard; - this->key = ""; - } + InputDeviceMapping() { } InputDeviceMapping(std::string input) { std::vector parts; Common::SplitString(input, '/', parts); if (parts.size() == 4) { if (parts[0] == "Qt") - this->framework = DeviceFramework::Qt; + framework = DeviceFramework::Qt; else if (parts[0] == "SDL") - this->framework = DeviceFramework::SDL; + framework = DeviceFramework::SDL; - this->number = std::stoi(parts[1]); + number = std::stoi(parts[1]); if (parts[2] == "Keyboard") - this->device = Device::Keyboard; + device = Device::Keyboard; else if (parts[2] == "Gamepad") - this->device = Device::Gamepad; - this->key = parts[3]; + device = Device::Gamepad; + key = parts[3]; } else { //default if can't read properly - this->framework = DeviceFramework::Qt; - this->number = 0; - this->device = Device::Keyboard; - this->key = ""; + framework = DeviceFramework::Qt; + number = 0; + device = Device::Keyboard; + key = ""; } } bool operator==(const InputDeviceMapping& rhs) const { return (this->device == rhs.device) && (this->framework == rhs.framework) && (this->number == rhs.number); } - std::string toString() { - std::string result = ""; - if (this->framework == DeviceFramework::Qt) + std::string ToString() const{ + std::string result; + if (framework == DeviceFramework::Qt) result = "Qt"; - else if (this->framework == DeviceFramework::SDL) + else if (framework == DeviceFramework::SDL) result = "SDL"; result += "/"; result += std::to_string(this->number); result += "/"; - if (this->device == Device::Keyboard) + if (device == Device::Keyboard) result += "Keyboard"; - else if (this->device == Device::Gamepad) + else if (device == Device::Gamepad) result += "Gamepad"; result += "/"; - result += this->key; + result += key; return result; } }; diff --git a/src/input_core/devices/IDevice.h b/src/input_core/devices/IDevice.h index 6a5a522cb..4df588772 100644 --- a/src/input_core/devices/IDevice.h +++ b/src/input_core/devices/IDevice.h @@ -8,8 +8,10 @@ #include "core/settings.h" #include "input_core/key_map.h" + class IDevice { public: + virtual ~IDevice() = default; std::map> keyMapping; /// Maps the string in the settings file to the HID Padstate object virtual bool InitDevice(int number, std::map> keyMap) = 0; diff --git a/src/input_core/devices/Keyboard.cpp b/src/input_core/devices/Keyboard.cpp index b320ce949..afa1cfe59 100644 --- a/src/input_core/devices/Keyboard.cpp +++ b/src/input_core/devices/Keyboard.cpp @@ -2,9 +2,10 @@ // Licensed under GPLv2 or any later version // Refer to the license.txt file included. -#include "Keyboard.h" #include +#include "input_core/devices/Keyboard.h" + Keyboard::Keyboard() { } @@ -17,9 +18,9 @@ bool Keyboard::InitDevice(int number, std::map lock(m); auto keysPressedCopy = keysPressed; - m.unlock(); + lock.~lock_guard(); for (auto const &ent1 : keyMapping) { int scancode = std::stoul(ent1.first, nullptr, 16); KeyboardKey proxy = KeyboardKey(0, scancode, ""); @@ -43,13 +44,11 @@ bool Keyboard::CloseDevice() { } void Keyboard::KeyPressed(KeyboardKey key) { - m.lock(); + std::lock_guard lock(m); keysPressed[key] = true; - m.unlock(); } void Keyboard::KeyReleased(KeyboardKey key) { - m.lock(); + std::lock_guard lock(m); keysPressed[key] = false; - m.unlock(); } \ No newline at end of file diff --git a/src/input_core/devices/Keyboard.h b/src/input_core/devices/Keyboard.h index 084881e24..5b2c00942 100644 --- a/src/input_core/devices/Keyboard.h +++ b/src/input_core/devices/Keyboard.h @@ -8,7 +8,7 @@ #include #include -#include "IDevice.h" +#include "input_core/devices/IDevice.h" struct KeyboardKey; @@ -16,7 +16,7 @@ class Keyboard : public IDevice { private: std::map keysPressed; std::map keysPressedLast; - std::mutex m; /// Keys pressed from frontend is on a separate thread. + std::mutex m; ///< Keys pressed from frontend is on a separate thread. public: Keyboard(); ~Keyboard(); @@ -36,10 +36,10 @@ struct KeyboardKey { scancode = Scancode; character = Character; } - bool operator==(KeyboardKey& other) { + bool operator==(const KeyboardKey& other) const { return (this->scancode == other.scancode); } - bool operator==(uint32_t other) { + bool operator==(uint32_t other) const { return (this->scancode == other); } bool operator<(const KeyboardKey &o) const { diff --git a/src/input_core/devices/SDLGamepad.cpp b/src/input_core/devices/SDLGamepad.cpp index 717516c2d..37f499dd4 100644 --- a/src/input_core/devices/SDLGamepad.cpp +++ b/src/input_core/devices/SDLGamepad.cpp @@ -2,14 +2,16 @@ // Licensed under GPLv2 or any later version // Refer to the license.txt file included. -#include #include - -#include "SDLGamepad.h" +#include #include + + #include "common/assert.h" #include "common/logging/log.h" +#include "input_core/devices/SDLGamepad.h" + bool SDLGamepad::SDLInitialized = false; SDLGamepad::SDLGamepad() { } diff --git a/src/input_core/devices/SDLGamepad.h b/src/input_core/devices/SDLGamepad.h index f48ffd107..528968e41 100644 --- a/src/input_core/devices/SDLGamepad.h +++ b/src/input_core/devices/SDLGamepad.h @@ -5,9 +5,17 @@ #pragma once #include -#include "IDevice.h" + +#include "input_core/devices/IDevice.h" class SDLGamepad : public IDevice { +public: + SDLGamepad(); + ~SDLGamepad(); + + bool InitDevice(int number, std::map> keyMap) override; + void ProcessInput() override; + bool CloseDevice() override; private: std::map friendlyNameMapping = { /// Maps the friendly name shown on GUI with the string name for getting the SDL button instance. { "Button A","a" }, @@ -38,11 +46,4 @@ private: static bool SDLInitialized; std::map keysPressed; SDL_GameController* gamepad; -public: - SDLGamepad(); - ~SDLGamepad(); - - virtual bool InitDevice(int number, std::map> keyMap) override; - virtual void ProcessInput() override; - bool CloseDevice() override; }; diff --git a/src/input_core/input_core.cpp b/src/input_core/input_core.cpp index 08986164b..f43ae8434 100644 --- a/src/input_core/input_core.cpp +++ b/src/input_core/input_core.cpp @@ -11,97 +11,92 @@ #include "input_core/devices/SDLGamepad.h" namespace InputCore { - using std::vector; - using std::shared_ptr; - using std::string; - constexpr u64 frame_ticks = 268123480ull / 60; - static int tick_event; - Service::HID::PadState pad_state; - std::tuple circle_pad = { 0,0 }; - shared_ptr main_keyboard; - vector> devices; ///< Devices that are handling input for the game +constexpr u64 frame_ticks = 268123480ull / 60; +static int tick_event; +Service::HID::PadState pad_state; +std::tuple circle_pad = { 0,0 }; +std::shared_ptr main_keyboard; +std::vector> devices; ///< Devices that are handling input for the game - static void InputTickCallback(u64, int cycles_late) { - for (auto& device : devices) - device->ProcessInput(); +static void InputTickCallback(u64, int cycles_late) { + for (auto& device : devices) + device->ProcessInput(); - Service::HID::Update(); + Service::HID::Update(); - // Reschedule recurrent event - CoreTiming::ScheduleEvent(frame_ticks - cycles_late, tick_event); - } + // Reschedule recurrent event + CoreTiming::ScheduleEvent(frame_ticks - cycles_late, tick_event); +} - void Init() { - devices = ParseSettings(); - tick_event = CoreTiming::RegisterEvent("InputCore::tick_event", InputTickCallback); - CoreTiming::ScheduleEvent(frame_ticks, tick_event); - } +void Init() { + devices = ParseSettings(); + tick_event = CoreTiming::RegisterEvent("InputCore::tick_event", InputTickCallback); + CoreTiming::ScheduleEvent(frame_ticks, tick_event); +} - void Shutdown() { - devices.clear(); - } +void Shutdown() { + devices.clear(); +} - vector> ParseSettings() { - vector> devices; - vector uniqueMappings; //unique mappings from settings file, used to init devices. +std::vector> ParseSettings() { + std::vector> devices; + std::vector uniqueMappings; //unique mappings from settings file, used to init devices. - //Get Unique input mappings from settings - for (auto& mapping : Settings::values.input_mappings) { - if (!CheckIfMappingExists(uniqueMappings, mapping)) { - uniqueMappings.push_back(mapping); - } + //Get Unique input mappings from settings + for (auto& mapping : Settings::values.input_mappings) { + if (!CheckIfMappingExists(uniqueMappings, mapping)) { + uniqueMappings.push_back(mapping); } + } - //Generate a device for each unique mapping - shared_ptr input; - for (auto& mapping : uniqueMappings) { - switch (mapping.framework) { - case Settings::DeviceFramework::Qt: - { + //Generate a device for each unique mapping + std::shared_ptr input; + for (auto& mapping : uniqueMappings) { + switch (mapping.framework) { + case Settings::DeviceFramework::Qt: + { + main_keyboard = std::make_shared(); + input = main_keyboard; + break; + } + case Settings::DeviceFramework::SDL: + { + if (mapping.device == Settings::Device::Keyboard) { main_keyboard = std::make_shared(); input = main_keyboard; break; } - case Settings::DeviceFramework::SDL: - { - if (mapping.device == Settings::Device::Keyboard) { - main_keyboard = std::make_shared(); - input = main_keyboard; - break; - } - else if (mapping.device == Settings::Device::Gamepad) { - input = std::make_shared(); - break; - } + else if (mapping.device == Settings::Device::Gamepad) { + input = std::make_shared(); + break; } - } - devices.push_back(input); - - //Build list of inputs to listen for, for this device - std::map> keyMapping; - for (int i = 0; i < Settings::values.input_mappings.size(); i++) { - KeyMap::KeyTarget val = KeyMap::mapping_targets[i]; - std::string key = Settings::values.input_mappings[i].key; - if (Settings::values.input_mappings[i] == mapping) { - keyMapping[key].push_back(val); - } - } - - input->InitDevice(mapping.number, keyMapping); } - //init keyboard, if it hasn't already - if (main_keyboard == nullptr) - main_keyboard = std::make_shared(); - - return devices; - } - - bool CheckIfMappingExists(vector uniqueMapping, Settings::InputDeviceMapping mappingToCheck) { - for (auto& mapping : uniqueMapping) { - if (mapping == mappingToCheck) - return true; } - return false; + devices.push_back(input); + + //Build map of inputs to listen for, for this device + std::map> keyMapping; + for (int i = 0; i < Settings::values.input_mappings.size(); i++) { + KeyMap::KeyTarget val = KeyMap::mapping_targets[i]; + std::string key = Settings::values.input_mappings[i].key; + if (Settings::values.input_mappings[i] == mapping) { + keyMapping[key].push_back(val); + } + } + + input->InitDevice(mapping.number, keyMapping); } + //init keyboard, if it hasn't already + if (main_keyboard == nullptr) + main_keyboard = std::make_shared(); + + return devices; +} + +bool CheckIfMappingExists(const std::vector uniqueMapping, Settings::InputDeviceMapping mappingToCheck) { + return std::any_of(uniqueMapping.begin(), uniqueMapping.end(), [mappingToCheck](const auto& mapping) { + return mapping == mappingToCheck; + }); +} } diff --git a/src/input_core/key_map.cpp b/src/input_core/key_map.cpp index d6136d8f2..bb0362400 100644 --- a/src/input_core/key_map.cpp +++ b/src/input_core/key_map.cpp @@ -2,14 +2,14 @@ // Licensed under GPLv2 or any later version // Refer to the license.txt file included. -#include #include #include +#include #include "common/emu_window.h" -#include "input_core/key_map.h" #include "input_core/input_core.h" +#include "input_core/key_map.h" namespace KeyMap { constexpr int MAX_CIRCLEPAD_POS = 0x9C; /// Max value for a circle pad position diff --git a/src/input_core/key_map.h b/src/input_core/key_map.h index 17f4ee739..18ff2f485 100644 --- a/src/input_core/key_map.h +++ b/src/input_core/key_map.h @@ -6,6 +6,7 @@ #include #include + #include "core/hle/service/hid/hid.h" class EmuWindow;