Code cleanup for Lioncash

This commit is contained in:
Anon 2016-08-06 12:09:49 -05:00
parent 2239cc9f0c
commit 555556bb0f
10 changed files with 121 additions and 126 deletions

View File

@ -125,7 +125,7 @@ void Config::SaveValues() {
qt_config->beginGroup("Controls"); qt_config->beginGroup("Controls");
for (int i = 0; i < Settings::NativeInput::NUM_INPUTS; ++i) { for (int i = 0; i < Settings::NativeInput::NUM_INPUTS; ++i) {
qt_config->setValue(QString::fromStdString(Settings::NativeInput::Mapping[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->setValue("pad_circle_modifier_scale", (double)Settings::values.pad_circle_modifier_scale);
qt_config->endGroup(); qt_config->endGroup();

View File

@ -52,62 +52,57 @@ namespace Settings {
Keyboard, Gamepad Keyboard, Gamepad
}; };
struct InputDeviceMapping { struct InputDeviceMapping {
DeviceFramework framework; DeviceFramework framework = DeviceFramework::Qt;
int number; int number = 0;
Device device; Device device = Device::Keyboard;
std::string key; std::string key;
InputDeviceMapping() { InputDeviceMapping() { }
this->framework = DeviceFramework::Qt;
this->number = 0;
this->device = Device::Keyboard;
this->key = "";
}
InputDeviceMapping(std::string input) { InputDeviceMapping(std::string input) {
std::vector<std::string> parts; std::vector<std::string> parts;
Common::SplitString(input, '/', parts); Common::SplitString(input, '/', parts);
if (parts.size() == 4) { if (parts.size() == 4) {
if (parts[0] == "Qt") if (parts[0] == "Qt")
this->framework = DeviceFramework::Qt; framework = DeviceFramework::Qt;
else if (parts[0] == "SDL") 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") if (parts[2] == "Keyboard")
this->device = Device::Keyboard; device = Device::Keyboard;
else if (parts[2] == "Gamepad") else if (parts[2] == "Gamepad")
this->device = Device::Gamepad; device = Device::Gamepad;
this->key = parts[3]; key = parts[3];
} }
else { else {
//default if can't read properly //default if can't read properly
this->framework = DeviceFramework::Qt; framework = DeviceFramework::Qt;
this->number = 0; number = 0;
this->device = Device::Keyboard; device = Device::Keyboard;
this->key = ""; key = "";
} }
} }
bool operator==(const InputDeviceMapping& rhs) const { bool operator==(const InputDeviceMapping& rhs) const {
return (this->device == rhs.device) && (this->framework == rhs.framework) && (this->number == rhs.number); return (this->device == rhs.device) && (this->framework == rhs.framework) && (this->number == rhs.number);
} }
std::string toString() { std::string ToString() const{
std::string result = ""; std::string result;
if (this->framework == DeviceFramework::Qt) if (framework == DeviceFramework::Qt)
result = "Qt"; result = "Qt";
else if (this->framework == DeviceFramework::SDL) else if (framework == DeviceFramework::SDL)
result = "SDL"; result = "SDL";
result += "/"; result += "/";
result += std::to_string(this->number); result += std::to_string(this->number);
result += "/"; result += "/";
if (this->device == Device::Keyboard) if (device == Device::Keyboard)
result += "Keyboard"; result += "Keyboard";
else if (this->device == Device::Gamepad) else if (device == Device::Gamepad)
result += "Gamepad"; result += "Gamepad";
result += "/"; result += "/";
result += this->key; result += key;
return result; return result;
} }
}; };

View File

@ -8,8 +8,10 @@
#include "core/settings.h" #include "core/settings.h"
#include "input_core/key_map.h" #include "input_core/key_map.h"
class IDevice { class IDevice {
public: public:
virtual ~IDevice() = default;
std::map<std::string, std::vector<KeyMap::KeyTarget>> keyMapping; /// Maps the string in the settings file to the HID Padstate object std::map<std::string, std::vector<KeyMap::KeyTarget>> keyMapping; /// Maps the string in the settings file to the HID Padstate object
virtual bool InitDevice(int number, std::map<std::string, std::vector<KeyMap::KeyTarget>> keyMap) = 0; virtual bool InitDevice(int number, std::map<std::string, std::vector<KeyMap::KeyTarget>> keyMap) = 0;

View File

@ -2,9 +2,10 @@
// Licensed under GPLv2 or any later version // Licensed under GPLv2 or any later version
// Refer to the license.txt file included. // Refer to the license.txt file included.
#include "Keyboard.h"
#include <SDL_keyboard.h> #include <SDL_keyboard.h>
#include "input_core/devices/Keyboard.h"
Keyboard::Keyboard() { Keyboard::Keyboard() {
} }
@ -17,9 +18,9 @@ bool Keyboard::InitDevice(int number, std::map<std::string, std::vector<KeyMap::
} }
void Keyboard::ProcessInput() { void Keyboard::ProcessInput() {
m.lock(); std::lock_guard<std::mutex> lock(m);
auto keysPressedCopy = keysPressed; auto keysPressedCopy = keysPressed;
m.unlock(); lock.~lock_guard();
for (auto const &ent1 : keyMapping) { for (auto const &ent1 : keyMapping) {
int scancode = std::stoul(ent1.first, nullptr, 16); int scancode = std::stoul(ent1.first, nullptr, 16);
KeyboardKey proxy = KeyboardKey(0, scancode, ""); KeyboardKey proxy = KeyboardKey(0, scancode, "");
@ -43,13 +44,11 @@ bool Keyboard::CloseDevice() {
} }
void Keyboard::KeyPressed(KeyboardKey key) { void Keyboard::KeyPressed(KeyboardKey key) {
m.lock(); std::lock_guard<std::mutex> lock(m);
keysPressed[key] = true; keysPressed[key] = true;
m.unlock();
} }
void Keyboard::KeyReleased(KeyboardKey key) { void Keyboard::KeyReleased(KeyboardKey key) {
m.lock(); std::lock_guard<std::mutex> lock(m);
keysPressed[key] = false; keysPressed[key] = false;
m.unlock();
} }

View File

@ -8,7 +8,7 @@
#include <memory> #include <memory>
#include <mutex> #include <mutex>
#include "IDevice.h" #include "input_core/devices/IDevice.h"
struct KeyboardKey; struct KeyboardKey;
@ -16,7 +16,7 @@ class Keyboard : public IDevice {
private: private:
std::map<KeyboardKey, bool> keysPressed; std::map<KeyboardKey, bool> keysPressed;
std::map<int, bool> keysPressedLast; std::map<int, bool> 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: public:
Keyboard(); Keyboard();
~Keyboard(); ~Keyboard();
@ -36,10 +36,10 @@ struct KeyboardKey {
scancode = Scancode; scancode = Scancode;
character = Character; character = Character;
} }
bool operator==(KeyboardKey& other) { bool operator==(const KeyboardKey& other) const {
return (this->scancode == other.scancode); return (this->scancode == other.scancode);
} }
bool operator==(uint32_t other) { bool operator==(uint32_t other) const {
return (this->scancode == other); return (this->scancode == other);
} }
bool operator<(const KeyboardKey &o) const { bool operator<(const KeyboardKey &o) const {

View File

@ -2,14 +2,16 @@
// Licensed under GPLv2 or any later version // Licensed under GPLv2 or any later version
// Refer to the license.txt file included. // Refer to the license.txt file included.
#include <memory>
#include <cmath> #include <cmath>
#include <memory>
#include "SDLGamepad.h"
#include <SDL.h> #include <SDL.h>
#include "common/assert.h" #include "common/assert.h"
#include "common/logging/log.h" #include "common/logging/log.h"
#include "input_core/devices/SDLGamepad.h"
bool SDLGamepad::SDLInitialized = false; bool SDLGamepad::SDLInitialized = false;
SDLGamepad::SDLGamepad() { SDLGamepad::SDLGamepad() {
} }

View File

@ -5,9 +5,17 @@
#pragma once #pragma once
#include <SDL_gamecontroller.h> #include <SDL_gamecontroller.h>
#include "IDevice.h"
#include "input_core/devices/IDevice.h"
class SDLGamepad : public IDevice { class SDLGamepad : public IDevice {
public:
SDLGamepad();
~SDLGamepad();
bool InitDevice(int number, std::map<std::string, std::vector<KeyMap::KeyTarget>> keyMap) override;
void ProcessInput() override;
bool CloseDevice() override;
private: private:
std::map<std::string, std::string> friendlyNameMapping = { /// Maps the friendly name shown on GUI with the string name for getting the SDL button instance. std::map<std::string, std::string> friendlyNameMapping = { /// Maps the friendly name shown on GUI with the string name for getting the SDL button instance.
{ "Button A","a" }, { "Button A","a" },
@ -38,11 +46,4 @@ private:
static bool SDLInitialized; static bool SDLInitialized;
std::map<std::string, bool> keysPressed; std::map<std::string, bool> keysPressed;
SDL_GameController* gamepad; SDL_GameController* gamepad;
public:
SDLGamepad();
~SDLGamepad();
virtual bool InitDevice(int number, std::map<std::string, std::vector<KeyMap::KeyTarget>> keyMap) override;
virtual void ProcessInput() override;
bool CloseDevice() override;
}; };

View File

@ -11,97 +11,92 @@
#include "input_core/devices/SDLGamepad.h" #include "input_core/devices/SDLGamepad.h"
namespace InputCore { namespace InputCore {
using std::vector;
using std::shared_ptr;
using std::string;
constexpr u64 frame_ticks = 268123480ull / 60; constexpr u64 frame_ticks = 268123480ull / 60;
static int tick_event; static int tick_event;
Service::HID::PadState pad_state; Service::HID::PadState pad_state;
std::tuple<s16, s16> circle_pad = { 0,0 }; std::tuple<s16, s16> circle_pad = { 0,0 };
shared_ptr<Keyboard> main_keyboard; std::shared_ptr<Keyboard> main_keyboard;
vector<shared_ptr<IDevice>> devices; ///< Devices that are handling input for the game std::vector<std::shared_ptr<IDevice>> devices; ///< Devices that are handling input for the game
static void InputTickCallback(u64, int cycles_late) { static void InputTickCallback(u64, int cycles_late) {
for (auto& device : devices) for (auto& device : devices)
device->ProcessInput(); device->ProcessInput();
Service::HID::Update(); Service::HID::Update();
// Reschedule recurrent event // Reschedule recurrent event
CoreTiming::ScheduleEvent(frame_ticks - cycles_late, tick_event); CoreTiming::ScheduleEvent(frame_ticks - cycles_late, tick_event);
} }
void Init() { void Init() {
devices = ParseSettings(); devices = ParseSettings();
tick_event = CoreTiming::RegisterEvent("InputCore::tick_event", InputTickCallback); tick_event = CoreTiming::RegisterEvent("InputCore::tick_event", InputTickCallback);
CoreTiming::ScheduleEvent(frame_ticks, tick_event); CoreTiming::ScheduleEvent(frame_ticks, tick_event);
} }
void Shutdown() { void Shutdown() {
devices.clear(); devices.clear();
} }
vector<shared_ptr<IDevice>> ParseSettings() { std::vector<std::shared_ptr<IDevice>> ParseSettings() {
vector<shared_ptr<IDevice>> devices; std::vector<std::shared_ptr<IDevice>> devices;
vector<Settings::InputDeviceMapping> uniqueMappings; //unique mappings from settings file, used to init devices. std::vector<Settings::InputDeviceMapping> uniqueMappings; //unique mappings from settings file, used to init devices.
//Get Unique input mappings from settings //Get Unique input mappings from settings
for (auto& mapping : Settings::values.input_mappings) { for (auto& mapping : Settings::values.input_mappings) {
if (!CheckIfMappingExists(uniqueMappings, mapping)) { if (!CheckIfMappingExists(uniqueMappings, mapping)) {
uniqueMappings.push_back(mapping); uniqueMappings.push_back(mapping);
}
} }
}
//Generate a device for each unique mapping //Generate a device for each unique mapping
shared_ptr<IDevice> input; std::shared_ptr<IDevice> input;
for (auto& mapping : uniqueMappings) { for (auto& mapping : uniqueMappings) {
switch (mapping.framework) { switch (mapping.framework) {
case Settings::DeviceFramework::Qt: case Settings::DeviceFramework::Qt:
{ {
main_keyboard = std::make_shared<Keyboard>();
input = main_keyboard;
break;
}
case Settings::DeviceFramework::SDL:
{
if (mapping.device == Settings::Device::Keyboard) {
main_keyboard = std::make_shared<Keyboard>(); main_keyboard = std::make_shared<Keyboard>();
input = main_keyboard; input = main_keyboard;
break; break;
} }
case Settings::DeviceFramework::SDL: else if (mapping.device == Settings::Device::Gamepad) {
{ input = std::make_shared<SDLGamepad>();
if (mapping.device == Settings::Device::Keyboard) { break;
main_keyboard = std::make_shared<Keyboard>();
input = main_keyboard;
break;
}
else if (mapping.device == Settings::Device::Gamepad) {
input = std::make_shared<SDLGamepad>();
break;
}
} }
}
devices.push_back(input);
//Build list of inputs to listen for, for this device
std::map<std::string, vector<KeyMap::KeyTarget>> 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<Keyboard>();
return devices;
}
bool CheckIfMappingExists(vector<Settings::InputDeviceMapping> 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<std::string, std::vector<KeyMap::KeyTarget>> 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<Keyboard>();
return devices;
}
bool CheckIfMappingExists(const std::vector<Settings::InputDeviceMapping> uniqueMapping, Settings::InputDeviceMapping mappingToCheck) {
return std::any_of(uniqueMapping.begin(), uniqueMapping.end(), [mappingToCheck](const auto& mapping) {
return mapping == mappingToCheck;
});
}
} }

View File

@ -2,14 +2,14 @@
// Licensed under GPLv2 or any later version // Licensed under GPLv2 or any later version
// Refer to the license.txt file included. // Refer to the license.txt file included.
#include <map>
#include <algorithm> #include <algorithm>
#include <iterator> #include <iterator>
#include <map>
#include "common/emu_window.h" #include "common/emu_window.h"
#include "input_core/key_map.h"
#include "input_core/input_core.h" #include "input_core/input_core.h"
#include "input_core/key_map.h"
namespace KeyMap { namespace KeyMap {
constexpr int MAX_CIRCLEPAD_POS = 0x9C; /// Max value for a circle pad position constexpr int MAX_CIRCLEPAD_POS = 0x9C; /// Max value for a circle pad position

View File

@ -6,6 +6,7 @@
#include <array> #include <array>
#include <tuple> #include <tuple>
#include "core/hle/service/hid/hid.h" #include "core/hle/service/hid/hid.h"
class EmuWindow; class EmuWindow;