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");
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();

View File

@ -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<std::string> 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;
}
};

View File

@ -8,8 +8,10 @@
#include "core/settings.h"
#include "input_core/key_map.h"
class IDevice {
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
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
// Refer to the license.txt file included.
#include "Keyboard.h"
#include <SDL_keyboard.h>
#include "input_core/devices/Keyboard.h"
Keyboard::Keyboard() {
}
@ -17,9 +18,9 @@ bool Keyboard::InitDevice(int number, std::map<std::string, std::vector<KeyMap::
}
void Keyboard::ProcessInput() {
m.lock();
std::lock_guard<std::mutex> 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<std::mutex> lock(m);
keysPressed[key] = true;
m.unlock();
}
void Keyboard::KeyReleased(KeyboardKey key) {
m.lock();
std::lock_guard<std::mutex> lock(m);
keysPressed[key] = false;
m.unlock();
}

View File

@ -8,7 +8,7 @@
#include <memory>
#include <mutex>
#include "IDevice.h"
#include "input_core/devices/IDevice.h"
struct KeyboardKey;
@ -16,7 +16,7 @@ class Keyboard : public IDevice {
private:
std::map<KeyboardKey, bool> keysPressed;
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:
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 {

View File

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

View File

@ -5,9 +5,17 @@
#pragma once
#include <SDL_gamecontroller.h>
#include "IDevice.h"
#include "input_core/devices/IDevice.h"
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:
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" },
@ -38,11 +46,4 @@ private:
static bool SDLInitialized;
std::map<std::string, bool> keysPressed;
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,16 +11,13 @@
#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<s16, s16> circle_pad = { 0,0 };
shared_ptr<Keyboard> main_keyboard;
vector<shared_ptr<IDevice>> devices; ///< Devices that are handling input for the game
std::shared_ptr<Keyboard> main_keyboard;
std::vector<std::shared_ptr<IDevice>> devices; ///< Devices that are handling input for the game
static void InputTickCallback(u64, int cycles_late) {
for (auto& device : devices)
@ -42,9 +39,9 @@ namespace InputCore {
devices.clear();
}
vector<shared_ptr<IDevice>> ParseSettings() {
vector<shared_ptr<IDevice>> devices;
vector<Settings::InputDeviceMapping> uniqueMappings; //unique mappings from settings file, used to init devices.
std::vector<std::shared_ptr<IDevice>> ParseSettings() {
std::vector<std::shared_ptr<IDevice>> devices;
std::vector<Settings::InputDeviceMapping> uniqueMappings; //unique mappings from settings file, used to init devices.
//Get Unique input mappings from settings
for (auto& mapping : Settings::values.input_mappings) {
@ -54,7 +51,7 @@ namespace InputCore {
}
//Generate a device for each unique mapping
shared_ptr<IDevice> input;
std::shared_ptr<IDevice> input;
for (auto& mapping : uniqueMappings) {
switch (mapping.framework) {
case Settings::DeviceFramework::Qt:
@ -78,8 +75,8 @@ namespace InputCore {
}
devices.push_back(input);
//Build list of inputs to listen for, for this device
std::map<std::string, vector<KeyMap::KeyTarget>> keyMapping;
//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;
@ -97,11 +94,9 @@ namespace InputCore {
return devices;
}
bool CheckIfMappingExists(vector<Settings::InputDeviceMapping> uniqueMapping, Settings::InputDeviceMapping mappingToCheck) {
for (auto& mapping : uniqueMapping) {
if (mapping == mappingToCheck)
return true;
}
return false;
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
// Refer to the license.txt file included.
#include <map>
#include <algorithm>
#include <iterator>
#include <map>
#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

View File

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