From e8cc1b8d8ac4798c9ceeae481988d3695ea4226a Mon Sep 17 00:00:00 2001 From: Lioncash Date: Fri, 31 May 2019 08:26:31 -0400 Subject: [PATCH] IOS/USB_KBD: Use std::array for member variables where applicable Simplifies initialization code quite a bit, and replaces a pointer variable for SMessageData with a type properly representing the whole set of data it needs. --- Source/Core/Core/IOS/USB/USB_KBD.cpp | 35 +++++++++------------------- Source/Core/Core/IOS/USB/USB_KBD.h | 21 ++++++++++------- 2 files changed, 23 insertions(+), 33 deletions(-) diff --git a/Source/Core/Core/IOS/USB/USB_KBD.cpp b/Source/Core/Core/IOS/USB/USB_KBD.cpp index d0f11a0b33..1fa661b356 100644 --- a/Source/Core/Core/IOS/USB/USB_KBD.cpp +++ b/Source/Core/Core/IOS/USB/USB_KBD.cpp @@ -5,7 +5,6 @@ #include "Core/IOS/USB/USB_KBD.h" #include -#include #include #include "Common/FileUtil.h" @@ -177,17 +176,9 @@ constexpr std::array s_key_codes_azerty{}; #endif } // Anonymous namespace -USB_KBD::SMessageData::SMessageData(u32 type, u8 modifiers, u8* pressed_keys) +USB_KBD::SMessageData::SMessageData(u32 type, u8 modifiers, PressedKeyData pressed_keys) + : MsgType{Common::swap32(type)}, Modifiers{modifiers}, PressedKeys{pressed_keys} { - MsgType = Common::swap32(type); - Unk1 = 0; // swapped - Modifiers = modifiers; - Unk2 = 0; - - if (pressed_keys) // Doesn't need to be in a specific order - memcpy(PressedKeys, pressed_keys, sizeof(PressedKeys)); - else - memset(PressedKeys, 0, sizeof(PressedKeys)); } // TODO: support in netplay/movies. @@ -203,15 +194,11 @@ IPCCommandResult USB_KBD::Open(const OpenRequest& request) ini.Load(File::GetUserPath(F_DOLPHINCONFIG_IDX)); ini.GetOrCreateSection("USB Keyboard")->Get("Layout", &m_KeyboardLayout, KBD_LAYOUT_QWERTY); - m_MessageQueue = std::queue(); - for (bool& pressed : m_OldKeyBuffer) - { - pressed = false; - } - + m_MessageQueue = {}; + m_OldKeyBuffer.fill(false); m_OldModifiers = 0x00; - // m_MessageQueue.push(SMessageData(MSG_KBD_CONNECT, 0, nullptr)); + // m_MessageQueue.emplace(MSG_KBD_CONNECT, 0, PressedKeyData{}); return Device::Open(request); } @@ -251,12 +238,12 @@ void USB_KBD::Update() return; u8 Modifiers = 0x00; - u8 PressedKeys[6] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; + PressedKeyData PressedKeys{0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; bool GotEvent = false; - int num_pressed_keys = 0; - for (int i = 0; i < 256; i++) + size_t num_pressed_keys = 0; + for (size_t i = 0; i < m_OldKeyBuffer.size(); i++) { - bool KeyPressedNow = IsKeyPressed(i); + bool KeyPressedNow = IsKeyPressed(static_cast(i)); bool KeyPressedBefore = m_OldKeyBuffer[i]; u8 KeyCode = 0; @@ -281,7 +268,7 @@ void USB_KBD::Update() PressedKeys[num_pressed_keys] = KeyCode; num_pressed_keys++; - if (num_pressed_keys == 6) + if (num_pressed_keys == PressedKeys.size()) break; } @@ -320,6 +307,6 @@ void USB_KBD::Update() } if (GotEvent) - m_MessageQueue.push(SMessageData(MSG_EVENT, Modifiers, PressedKeys)); + m_MessageQueue.emplace(MSG_EVENT, Modifiers, PressedKeys); } } // namespace IOS::HLE::Device diff --git a/Source/Core/Core/IOS/USB/USB_KBD.h b/Source/Core/Core/IOS/USB/USB_KBD.h index f48af1fc21..fa5caf5f70 100644 --- a/Source/Core/Core/IOS/USB/USB_KBD.h +++ b/Source/Core/Core/IOS/USB/USB_KBD.h @@ -4,6 +4,7 @@ #pragma once +#include #include #include @@ -31,22 +32,24 @@ private: MSG_EVENT = 2 }; + using PressedKeyData = std::array; + #pragma pack(push, 1) struct SMessageData { - u32 MsgType; - u32 Unk1; - u8 Modifiers; - u8 Unk2; - u8 PressedKeys[6]; + u32 MsgType = 0; + u32 Unk1 = 0; + u8 Modifiers = 0; + u8 Unk2 = 0; + PressedKeyData PressedKeys{}; - SMessageData(u32 msg_type, u8 modifiers, u8* pressed_keys); + SMessageData(u32 msg_type, u8 modifiers, PressedKeyData pressed_keys); }; #pragma pack(pop) std::queue m_MessageQueue; - bool m_OldKeyBuffer[256]; - u8 m_OldModifiers; + std::array m_OldKeyBuffer{}; + u8 m_OldModifiers = 0; virtual bool IsKeyPressed(int _Key); @@ -56,6 +59,6 @@ private: KBD_LAYOUT_QWERTY = 0, KBD_LAYOUT_AZERTY = 1 }; - int m_KeyboardLayout; + int m_KeyboardLayout = KBD_LAYOUT_QWERTY; }; } // namespace IOS::HLE::Device