mirror of
https://github.com/citra-emu/citra.git
synced 2024-11-25 11:10:15 +00:00
Code cleanup for Lioncash
This commit is contained in:
parent
2239cc9f0c
commit
555556bb0f
@ -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();
|
||||
|
@ -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;
|
||||
}
|
||||
};
|
||||
|
@ -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;
|
||||
|
@ -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();
|
||||
}
|
@ -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 {
|
||||
|
@ -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() {
|
||||
}
|
||||
|
@ -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;
|
||||
};
|
||||
|
@ -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<s16, s16> circle_pad = { 0,0 };
|
||||
shared_ptr<Keyboard> main_keyboard;
|
||||
vector<shared_ptr<IDevice>> 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<s16, s16> circle_pad = { 0,0 };
|
||||
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)
|
||||
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<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) {
|
||||
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<IDevice> input;
|
||||
for (auto& mapping : uniqueMappings) {
|
||||
switch (mapping.framework) {
|
||||
case Settings::DeviceFramework::Qt:
|
||||
{
|
||||
//Generate a device for each unique mapping
|
||||
std::shared_ptr<IDevice> input;
|
||||
for (auto& mapping : uniqueMappings) {
|
||||
switch (mapping.framework) {
|
||||
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>();
|
||||
input = main_keyboard;
|
||||
break;
|
||||
}
|
||||
case Settings::DeviceFramework::SDL:
|
||||
{
|
||||
if (mapping.device == Settings::Device::Keyboard) {
|
||||
main_keyboard = std::make_shared<Keyboard>();
|
||||
input = main_keyboard;
|
||||
break;
|
||||
}
|
||||
else if (mapping.device == Settings::Device::Gamepad) {
|
||||
input = std::make_shared<SDLGamepad>();
|
||||
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;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
@ -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
|
||||
|
@ -6,6 +6,7 @@
|
||||
|
||||
#include <array>
|
||||
#include <tuple>
|
||||
|
||||
#include "core/hle/service/hid/hid.h"
|
||||
|
||||
class EmuWindow;
|
||||
|
Loading…
Reference in New Issue
Block a user