Fix more lioncash code style issues

This commit is contained in:
Anon 2016-08-06 15:41:09 -05:00
parent 555556bb0f
commit 743c4647b8
12 changed files with 129 additions and 90 deletions

View File

@ -42,7 +42,7 @@ void EmuWindow_SDL2::OnMouseButton(u32 button, u8 state, s32 x, s32 y) {
} }
void EmuWindow_SDL2::OnKeyEvent(SDL_Keysym key, u8 state) { void EmuWindow_SDL2::OnKeyEvent(SDL_Keysym key, u8 state) {
auto& keyboard = InputCore::main_keyboard; auto& keyboard = InputCore::GetKeyboard();
KeyboardKey param = KeyboardKey(key.sym, key.scancode, SDL_GetKeyName(key.scancode)); KeyboardKey param = KeyboardKey(key.sym, key.scancode, SDL_GetKeyName(key.scancode));
if (state == SDL_PRESSED) { if (state == SDL_PRESSED) {

View File

@ -237,14 +237,14 @@ void GRenderWindow::closeEvent(QCloseEvent* event) {
void GRenderWindow::keyPressEvent(QKeyEvent* event) void GRenderWindow::keyPressEvent(QKeyEvent* event)
{ {
auto& keyboard = InputCore::main_keyboard; auto& keyboard = InputCore::GetKeyboard();
KeyboardKey param = KeyboardKey(event->key(), event->nativeScanCode(), QKeySequence(event->key()).toString().toStdString()); KeyboardKey param = KeyboardKey(event->key(), event->nativeScanCode(), QKeySequence(event->key()).toString().toStdString());
keyboard->KeyPressed(param); keyboard->KeyPressed(param);
} }
void GRenderWindow::keyReleaseEvent(QKeyEvent* event) void GRenderWindow::keyReleaseEvent(QKeyEvent* event)
{ {
auto& keyboard = InputCore::main_keyboard; auto& keyboard = InputCore::GetKeyboard();
KeyboardKey param = KeyboardKey(event->key(), event->nativeScanCode(), QKeySequence(event->key()).toString().toStdString()); KeyboardKey param = KeyboardKey(event->key(), event->nativeScanCode(), QKeySequence(event->key()).toString().toStdString());
keyboard->KeyReleased(param); keyboard->KeyReleased(param);
} }

View File

@ -76,10 +76,10 @@ void Update() {
return; return;
} }
PadState state = InputCore::pad_state; PadState state = InputCore::GetPadState();
// Get current circle pad position and update circle pad direction // Get current circle pad position and update circle pad direction
s16 circle_pad_x, circle_pad_y; s16 circle_pad_x, circle_pad_y;
std::tie(circle_pad_x, circle_pad_y) = InputCore::circle_pad; std::tie(circle_pad_x, circle_pad_y) = InputCore::GetCirclePad();
state.hex |= GetCirclePadDirectionState(circle_pad_x, circle_pad_y).hex; state.hex |= GetCirclePadDirectionState(circle_pad_x, circle_pad_y).hex;
mem->pad.current_state.hex = state.hex; mem->pad.current_state.hex = state.hex;

View File

@ -56,11 +56,14 @@ namespace Settings {
int number = 0; int number = 0;
Device device = Device::Keyboard; Device device = Device::Keyboard;
std::string key; std::string key;
InputDeviceMapping() { }
InputDeviceMapping(std::string input) { InputDeviceMapping() = default;
InputDeviceMapping(const 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)
return;
if (parts[0] == "Qt") if (parts[0] == "Qt")
framework = DeviceFramework::Qt; framework = DeviceFramework::Qt;
else if (parts[0] == "SDL") else if (parts[0] == "SDL")
@ -74,14 +77,6 @@ namespace Settings {
device = Device::Gamepad; device = Device::Gamepad;
key = parts[3]; key = parts[3];
} }
else {
//default if can't read properly
framework = DeviceFramework::Qt;
number = 0;
device = Device::Keyboard;
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);
@ -101,6 +96,7 @@ namespace Settings {
result += "Keyboard"; result += "Keyboard";
else if (device == Device::Gamepad) else if (device == Device::Gamepad)
result += "Gamepad"; result += "Gamepad";
result += "/"; result += "/";
result += key; result += key;
return result; return result;

View File

@ -12,9 +12,11 @@
class IDevice { class IDevice {
public: public:
virtual ~IDevice() = default; 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; virtual bool InitDevice(int number, const std::map<std::string, std::vector<KeyMap::KeyTarget>>& keyMap) = 0;
virtual void ProcessInput() = 0; virtual void ProcessInput() = 0;
virtual bool CloseDevice() = 0; virtual bool CloseDevice() = 0;
protected:
std::map<std::string, std::vector<KeyMap::KeyTarget>> keyMapping; ///< Maps the string in the settings file to the HID Padstate object
}; };

View File

@ -12,7 +12,7 @@ Keyboard::Keyboard() {
Keyboard::~Keyboard() { Keyboard::~Keyboard() {
} }
bool Keyboard::InitDevice(int number, std::map<std::string, std::vector<KeyMap::KeyTarget>> keyMap) { bool Keyboard::InitDevice(int number, const std::map<std::string, std::vector<KeyMap::KeyTarget>>& keyMap) {
keyMapping = keyMap; keyMapping = keyMap;
return true; return true;
} }
@ -21,17 +21,17 @@ void Keyboard::ProcessInput() {
std::lock_guard<std::mutex> lock(m); std::lock_guard<std::mutex> lock(m);
auto keysPressedCopy = keysPressed; auto keysPressedCopy = keysPressed;
lock.~lock_guard(); lock.~lock_guard();
for (auto const &ent1 : keyMapping) { for (const auto &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, "");
if (keysPressedCopy[proxy] == true && keysPressedLast[scancode] == false) { if (keysPressedCopy[proxy] == true && keysPressedLast[scancode] == false) {
for (auto& key : ent1.second) { for (const auto& key : ent1.second) {
KeyMap::PressKey(key, 1.0); KeyMap::PressKey(key, 1.0);
} }
keysPressedLast[scancode] = true; keysPressedLast[scancode] = true;
} }
else if (keysPressedCopy[proxy] == false && keysPressedLast[scancode] == true) { else if (keysPressedCopy[proxy] == false && keysPressedLast[scancode] == true) {
for (auto& key : ent1.second) { for (const auto& key : ent1.second) {
KeyMap::ReleaseKey(key); KeyMap::ReleaseKey(key);
} }
keysPressedLast[scancode] = false; keysPressedLast[scancode] = false;

View File

@ -20,7 +20,7 @@ private:
public: public:
Keyboard(); Keyboard();
~Keyboard(); ~Keyboard();
bool InitDevice(int number, std::map<std::string, std::vector<KeyMap::KeyTarget>> keyMap) override; bool InitDevice(int number, const std::map<std::string, std::vector<KeyMap::KeyTarget>>& keyMap) override;
void ProcessInput() override; void ProcessInput() override;
bool CloseDevice() override; bool CloseDevice() override;
void KeyPressed(KeyboardKey key); void KeyPressed(KeyboardKey key);
@ -31,18 +31,16 @@ struct KeyboardKey {
uint32_t key; uint32_t key;
uint32_t scancode; uint32_t scancode;
std::string character; std::string character;
KeyboardKey(uint32_t Key, uint32_t Scancode, std::string Character) {
key = Key; KeyboardKey(uint32_t key_, uint32_t scancode_, std::string character_) : key(key_), scancode(scancode_), character(std::move(character_)) {
scancode = Scancode;
character = Character;
} }
bool operator==(const KeyboardKey& other) const { bool operator==(const KeyboardKey& other) const {
return (this->scancode == other.scancode); return scancode == other.scancode;
} }
bool operator==(uint32_t other) const { bool operator==(uint32_t other) const {
return (this->scancode == other); return scancode == other;
} }
bool operator<(const KeyboardKey &o) const { bool operator<(const KeyboardKey& other) const {
return (this->scancode < o.scancode); return scancode < other.scancode;
} }
}; };

View File

@ -20,7 +20,7 @@ SDLGamepad::~SDLGamepad() {
CloseDevice(); CloseDevice();
} }
bool SDLGamepad::InitDevice(int number, std::map<std::string, std::vector<KeyMap::KeyTarget>> keyMap) { bool SDLGamepad::InitDevice(int number, const std::map<std::string, std::vector<KeyMap::KeyTarget>>& keyMap) {
if (!SDLGamepad::SDLInitialized && SDL_Init(SDL_INIT_GAMECONTROLLER) < 0) { if (!SDLGamepad::SDLInitialized && SDL_Init(SDL_INIT_GAMECONTROLLER) < 0) {
LOG_CRITICAL(Input, "SDL_Init(SDL_INIT_GAMECONTROLLER) failed"); LOG_CRITICAL(Input, "SDL_Init(SDL_INIT_GAMECONTROLLER) failed");
return false; return false;
@ -36,7 +36,7 @@ bool SDLGamepad::InitDevice(int number, std::map<std::string, std::vector<KeyMap
} }
} }
keyMapping = keyMap; keyMapping = keyMap;
for (auto& entry : keyMapping) { for (const auto& entry : keyMapping) {
keysPressed[entry.first] = false; keysPressed[entry.first] = false;
} }
@ -47,18 +47,18 @@ void SDLGamepad::ProcessInput() {
if (gamepad == nullptr) if (gamepad == nullptr)
return; return;
SDL_GameControllerUpdate(); SDL_GameControllerUpdate();
for (auto const &ent1 : keyMapping) { for (const auto &ent1 : keyMapping) {
SDL_GameControllerButton button = SDL_GameControllerGetButtonFromString(friendlyNameMapping[ent1.first].c_str()); SDL_GameControllerButton button = SDL_GameControllerGetButtonFromString(friendlyNameMapping[ent1.first].c_str());
if (button != SDL_GameControllerButton::SDL_CONTROLLER_BUTTON_INVALID) { if (button != SDL_GameControllerButton::SDL_CONTROLLER_BUTTON_INVALID) {
Uint8 pressed = SDL_GameControllerGetButton(gamepad, button); Uint8 pressed = SDL_GameControllerGetButton(gamepad, button);
if (pressed == 1 && keysPressed[ent1.first] == false) { if (pressed == 1 && keysPressed[ent1.first] == false) {
for (auto& padstate : ent1.second) { for (const auto& padstate : ent1.second) {
KeyMap::PressKey(padstate, 1.0); KeyMap::PressKey(padstate, 1.0);
keysPressed[ent1.first] = true; keysPressed[ent1.first] = true;
} }
} }
else if (pressed == 0 && keysPressed[ent1.first] == true) { else if (pressed == 0 && keysPressed[ent1.first] == true) {
for (auto& padstate : ent1.second) { for (const auto& padstate : ent1.second) {
KeyMap::ReleaseKey(padstate); KeyMap::ReleaseKey(padstate);
keysPressed[ent1.first] = false; keysPressed[ent1.first] = false;
} }
@ -69,8 +69,9 @@ void SDLGamepad::ProcessInput() {
SDL_GameControllerAxis axis = SDL_GameControllerGetAxisFromString(friendlyNameMapping[ent1.first].c_str()); SDL_GameControllerAxis axis = SDL_GameControllerGetAxisFromString(friendlyNameMapping[ent1.first].c_str());
if (axis != SDL_GameControllerAxis::SDL_CONTROLLER_AXIS_INVALID) { if (axis != SDL_GameControllerAxis::SDL_CONTROLLER_AXIS_INVALID) {
Sint16 value = SDL_GameControllerGetAxis(gamepad, axis); Sint16 value = SDL_GameControllerGetAxis(gamepad, axis);
for (auto& padstate : ent1.second) { for (const auto& padstate : ent1.second) {
if (abs(value) < 0.2 * 32767.0) // dont process if in deadzone. Replace later with settings for deadzone. // dont process if in deadzone. Replace later with settings for deadzone.
if (abs(value) < 0.2 * 32767.0)
KeyMap::ReleaseKey(padstate); KeyMap::ReleaseKey(padstate);
else else
KeyMap::PressKey(padstate, (float)value / 32767.0); KeyMap::PressKey(padstate, (float)value / 32767.0);

View File

@ -13,7 +13,7 @@ public:
SDLGamepad(); SDLGamepad();
~SDLGamepad(); ~SDLGamepad();
bool InitDevice(int number, std::map<std::string, std::vector<KeyMap::KeyTarget>> keyMap) override; bool InitDevice(int number, const std::map<std::string, std::vector<KeyMap::KeyTarget>>& keyMap) override;
void ProcessInput() override; void ProcessInput() override;
bool CloseDevice() override; bool CloseDevice() override;
private: private:
@ -45,5 +45,5 @@ 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 = nullptr;
}; };

View File

@ -2,6 +2,7 @@
// 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 <algorithm>
#include <map> #include <map>
#include "core/core_timing.h" #include "core/core_timing.h"
@ -11,7 +12,6 @@
#include "input_core/devices/SDLGamepad.h" #include "input_core/devices/SDLGamepad.h"
namespace InputCore { namespace InputCore {
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;
@ -30,7 +30,7 @@ static void InputTickCallback(u64, int cycles_late) {
} }
void Init() { void Init() {
devices = ParseSettings(); 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);
} }
@ -39,18 +39,52 @@ void Shutdown() {
devices.clear(); devices.clear();
} }
std::vector<std::shared_ptr<IDevice>> ParseSettings() { Service::HID::PadState GetPadState() {
std::vector<std::shared_ptr<IDevice>> devices; return pad_state;
std::vector<Settings::InputDeviceMapping> uniqueMappings; //unique mappings from settings file, used to init devices. }
void SetPadState(Service::HID::PadState& state) {
pad_state.hex = state.hex;
}
std::tuple<s16, s16> GetCirclePad() {
return circle_pad;
}
void SetCirclePad(std::tuple<s16, s16>& pad) {
circle_pad = pad;
}
std::shared_ptr<Keyboard> GetKeyboard() {
return main_keyboard;
}
///Get Unique input mappings from settings
std::vector<Settings::InputDeviceMapping> GatherUniqueMappings() {
std::vector<Settings::InputDeviceMapping> uniqueMappings;
//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);
} }
} }
return uniqueMappings;
}
//Generate a device for each unique mapping std::map<std::string, std::vector<KeyMap::KeyTarget>> BuildKeyMapping(Settings::InputDeviceMapping mapping) {
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);
}
}
return keyMapping;
}
///Generate a device for each unique mapping
void GenerateUniqueDevices(std::vector<Settings::InputDeviceMapping> uniqueMappings) {
std::shared_ptr<IDevice> input; std::shared_ptr<IDevice> input;
for (auto& mapping : uniqueMappings) { for (auto& mapping : uniqueMappings) {
switch (mapping.framework) { switch (mapping.framework) {
@ -76,25 +110,23 @@ std::vector<std::shared_ptr<IDevice>> ParseSettings() {
devices.push_back(input); devices.push_back(input);
//Build map of inputs to listen for, for this device //Build map of inputs to listen for, for this device
std::map<std::string, std::vector<KeyMap::KeyTarget>> keyMapping; auto keyMapping = BuildKeyMapping(mapping);
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); input->InitDevice(mapping.number, keyMapping);
} }
}
void ParseSettings() {
std::vector<std::shared_ptr<IDevice>> devices;
auto uniqueMappings = GatherUniqueMappings();
GenerateUniqueDevices(uniqueMappings);
//init keyboard, if it hasn't already //init keyboard, if it hasn't already
if (main_keyboard == nullptr) if (main_keyboard == nullptr)
main_keyboard = std::make_shared<Keyboard>(); main_keyboard = std::make_shared<Keyboard>();
return devices;
} }
bool CheckIfMappingExists(const std::vector<Settings::InputDeviceMapping> uniqueMapping, Settings::InputDeviceMapping mappingToCheck) { bool CheckIfMappingExists(const std::vector<Settings::InputDeviceMapping>& uniqueMapping, Settings::InputDeviceMapping mappingToCheck) {
return std::any_of(uniqueMapping.begin(), uniqueMapping.end(), [mappingToCheck](const auto& mapping) { return std::any_of(uniqueMapping.begin(), uniqueMapping.end(), [mappingToCheck](const auto& mapping) {
return mapping == mappingToCheck; return mapping == mappingToCheck;
}); });

View File

@ -3,6 +3,7 @@
// Refer to the license.txt file included. // Refer to the license.txt file included.
#pragma once #pragma once
#include <memory> #include <memory>
#include <tuple> #include <tuple>
@ -13,17 +14,16 @@
class Keyboard; class Keyboard;
namespace InputCore { namespace InputCore {
extern Service::HID::PadState pad_state;
extern std::tuple<s16, s16> circle_pad;
extern std::shared_ptr<Keyboard> main_keyboard; ///< Instance of main keyboard device. Always initialized regardless of settings.
void Init(); void Init();
void Shutdown(); void Shutdown();
Service::HID::PadState GetPadState();
void SetPadState(Service::HID::PadState& state);
std::tuple<s16, s16> GetCirclePad();
void SetCirclePad(std::tuple<s16, s16>& circle );
std::shared_ptr<Keyboard> GetKeyboard();
/// Read settings to initialize devices /// Read settings to initialize devices
std::vector<std::shared_ptr<IDevice>> ParseSettings(); void ParseSettings();
/// Helper method to check if device was already initialized /// Helper method to check if device was already initialized
bool CheckIfMappingExists(std::vector<Settings::InputDeviceMapping> uniqueMapping, Settings::InputDeviceMapping mappingToCheck); bool CheckIfMappingExists(const std::vector<Settings::InputDeviceMapping>& uniqueMapping, Settings::InputDeviceMapping mappingToCheck);
} }

View File

@ -34,30 +34,40 @@ namespace KeyMap {
}; };
void PressKey(KeyTarget target, const float strength) { void PressKey(KeyTarget target, const float strength) {
if (std::find(std::begin(analog_inputs), std::end(analog_inputs), target) == std::end(analog_inputs)) { // If is digital keytarget auto pad_state = InputCore::GetPadState();
InputCore::pad_state.hex |= target.target.direct_target_hex; // If is digital keytarget
if (std::find(std::begin(analog_inputs), std::end(analog_inputs), target) == std::end(analog_inputs)) {
pad_state.hex |= target.target.direct_target_hex;
InputCore::SetPadState(pad_state);
} }
else { // it is analog input else { // it is analog input
auto circle_pad = InputCore::GetCirclePad();
if (target == Service::HID::PAD_CIRCLE_UP || target == Service::HID::PAD_CIRCLE_DOWN) { if (target == Service::HID::PAD_CIRCLE_UP || target == Service::HID::PAD_CIRCLE_DOWN) {
std::get<1>(InputCore::circle_pad) = MAX_CIRCLEPAD_POS * strength * -1; std::get<1>(circle_pad) = MAX_CIRCLEPAD_POS * strength * -1;
} }
else if (target == Service::HID::PAD_CIRCLE_LEFT || target == Service::HID::PAD_CIRCLE_RIGHT) { else if (target == Service::HID::PAD_CIRCLE_LEFT || target == Service::HID::PAD_CIRCLE_RIGHT) {
std::get<0>(InputCore::circle_pad) = MAX_CIRCLEPAD_POS * strength; std::get<0>(circle_pad) = MAX_CIRCLEPAD_POS * strength;
} }
InputCore::SetCirclePad(circle_pad);
} }
} }
void ReleaseKey(KeyTarget target) { void ReleaseKey(KeyTarget target) {
if (std::find(std::begin(analog_inputs), std::end(analog_inputs), target) == std::end(analog_inputs)) { // If is digital keytarget auto pad_state = InputCore::GetPadState();
InputCore::pad_state.hex &= ~target.target.direct_target_hex; // If is digital keytarget
if (std::find(std::begin(analog_inputs), std::end(analog_inputs), target) == std::end(analog_inputs)) {
pad_state.hex &= ~target.target.direct_target_hex;
InputCore::SetPadState(pad_state);
} }
else { // it is analog input else { // it is analog input
auto circle_pad = InputCore::GetCirclePad();
if (target == Service::HID::PAD_CIRCLE_UP || target == Service::HID::PAD_CIRCLE_DOWN) { if (target == Service::HID::PAD_CIRCLE_UP || target == Service::HID::PAD_CIRCLE_DOWN) {
std::get<1>(InputCore::circle_pad) = 0; std::get<1>(circle_pad) = 0;
} }
else if (target == Service::HID::PAD_CIRCLE_LEFT || target == Service::HID::PAD_CIRCLE_RIGHT) { else if (target == Service::HID::PAD_CIRCLE_LEFT || target == Service::HID::PAD_CIRCLE_RIGHT) {
std::get<0>(InputCore::circle_pad) = 0; std::get<0>(circle_pad) = 0;
} }
InputCore::SetCirclePad(circle_pad);
} }
} }
} }