From 64564e337b23b4d3099b18496bb1391fb9df5e95 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Fri, 31 May 2019 08:02:26 -0400 Subject: [PATCH 1/8] IOS/USB_KBD: Make key code lookup tables immutable and internally linked These aren't modified by the class, nor do they directly need anything related to the class state, so they can solely live within the cpp file, hidden from external view, and also be made const, so the compiler can place it within the read-only segment. --- Source/Core/Core/IOS/USB/USB_KBD.cpp | 314 +++++++++++++-------------- Source/Core/Core/IOS/USB/USB_KBD.h | 2 - 2 files changed, 157 insertions(+), 159 deletions(-) diff --git a/Source/Core/Core/IOS/USB/USB_KBD.cpp b/Source/Core/Core/IOS/USB/USB_KBD.cpp index d690cc27e6..d0f11a0b33 100644 --- a/Source/Core/Core/IOS/USB/USB_KBD.cpp +++ b/Source/Core/Core/IOS/USB/USB_KBD.cpp @@ -4,6 +4,7 @@ #include "Core/IOS/USB/USB_KBD.h" +#include #include #include @@ -22,6 +23,160 @@ namespace IOS::HLE::Device { +namespace +{ +// Crazy ugly +#ifdef _WIN32 +constexpr std::array s_key_codes_qwerty{ + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x2A, // Backspace + 0x2B, // Tab + 0x00, 0x00, + 0x00, // Clear + 0x28, // Return + 0x00, 0x00, + 0x00, // Shift + 0x00, // Control + 0x00, // ALT + 0x48, // Pause + 0x39, // Capital + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x29, // Escape + 0x00, 0x00, 0x00, 0x00, + 0x2C, // Space + 0x4B, // Prior + 0x4E, // Next + 0x4D, // End + 0x4A, // Home + 0x50, // Left + 0x52, // Up + 0x4F, // Right + 0x51, // Down + 0x00, 0x00, 0x00, + 0x46, // Print screen + 0x49, // Insert + 0x4C, // Delete + 0x00, + // 0 -> 9 + 0x27, 0x1E, 0x1F, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, + // A -> Z + 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13, + 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x00, 0x00, 0x00, 0x00, 0x00, + // Numpad 0 -> 9 + 0x62, 0x59, 0x5A, 0x5B, 0x5C, 0x5D, 0x5E, 0x5F, 0x60, 0x61, + 0x55, // Multiply + 0x57, // Add + 0x00, // Separator + 0x56, // Subtract + 0x63, // Decimal + 0x54, // Divide + // F1 -> F12 + 0x3A, 0x3B, 0x3C, 0x3D, 0x3E, 0x3F, 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, + // F13 -> F24 + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, + 0x53, // Numlock + 0x47, // Scroll lock + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + // Modifier keys + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x33, // ';' + 0x2E, // Plus + 0x36, // Comma + 0x2D, // Minus + 0x37, // Period + 0x38, // '/' + 0x35, // '~' + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x2F, // '[' + 0x32, // '\' + 0x30, // ']' + 0x34, // ''' + 0x00, // + 0x00, // Nothing interesting past this point. +}; + +constexpr std::array s_key_codes_azerty{ + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x2A, // Backspace + 0x2B, // Tab + 0x00, 0x00, + 0x00, // Clear + 0x28, // Return + 0x00, 0x00, + 0x00, // Shift + 0x00, // Control + 0x00, // ALT + 0x48, // Pause + 0x39, // Capital + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x29, // Escape + 0x00, 0x00, 0x00, 0x00, + 0x2C, // Space + 0x4B, // Prior + 0x4E, // Next + 0x4D, // End + 0x4A, // Home + 0x50, // Left + 0x52, // Up + 0x4F, // Right + 0x51, // Down + 0x00, 0x00, 0x00, + 0x46, // Print screen + 0x49, // Insert + 0x4C, // Delete + 0x00, + // 0 -> 9 + 0x27, 0x1E, 0x1F, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, + // A -> Z + 0x14, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x33, 0x11, 0x12, 0x13, + 0x04, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1D, 0x1B, 0x1C, 0x1A, 0x00, 0x00, 0x00, 0x00, 0x00, + // Numpad 0 -> 9 + 0x62, 0x59, 0x5A, 0x5B, 0x5C, 0x5D, 0x5E, 0x5F, 0x60, 0x61, + 0x55, // Multiply + 0x57, // Add + 0x00, // Separator + 0x56, // Substract + 0x63, // Decimal + 0x54, // Divide + // F1 -> F12 + 0x3A, 0x3B, 0x3C, 0x3D, 0x3E, 0x3F, 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, + // F13 -> F24 + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, + 0x53, // Numlock + 0x47, // Scroll lock + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + // Modifier keys + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x30, // '$' + 0x2E, // Plus + 0x10, // Comma + 0x00, // Minus + 0x36, // Period + 0x37, // '/' + 0x34, // ' ' + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x2D, // ')' + 0x32, // '\' + 0x2F, // '^' + 0x00, // ' ' + 0x38, // '!' + 0x00, // Nothing interesting past this point. +}; +#else +constexpr std::array s_key_codes_qwerty{}; + +constexpr std::array s_key_codes_azerty{}; +#endif +} // Anonymous namespace + USB_KBD::SMessageData::SMessageData(u32 type, u8 modifiers, u8* pressed_keys) { MsgType = Common::swap32(type); @@ -112,11 +267,11 @@ void USB_KBD::Update() switch (m_KeyboardLayout) { case KBD_LAYOUT_QWERTY: - KeyCode = m_KeyCodesQWERTY[i]; + KeyCode = s_key_codes_qwerty[i]; break; case KBD_LAYOUT_AZERTY: - KeyCode = m_KeyCodesAZERTY[i]; + KeyCode = s_key_codes_azerty[i]; break; } @@ -167,159 +322,4 @@ void USB_KBD::Update() if (GotEvent) m_MessageQueue.push(SMessageData(MSG_EVENT, Modifiers, PressedKeys)); } - -// Crazy ugly -#ifdef _WIN32 -u8 USB_KBD::m_KeyCodesQWERTY[256] = { - - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x2A, // Backspace - 0x2B, // Tab - 0x00, 0x00, - 0x00, // Clear - 0x28, // Return - 0x00, 0x00, - 0x00, // Shift - 0x00, // Control - 0x00, // ALT - 0x48, // Pause - 0x39, // Capital - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x29, // Escape - 0x00, 0x00, 0x00, 0x00, - 0x2C, // Space - 0x4B, // Prior - 0x4E, // Next - 0x4D, // End - 0x4A, // Home - 0x50, // Left - 0x52, // Up - 0x4F, // Right - 0x51, // Down - 0x00, 0x00, 0x00, - 0x46, // Print screen - 0x49, // Insert - 0x4C, // Delete - 0x00, - // 0 -> 9 - 0x27, 0x1E, 0x1F, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, - // A -> Z - 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13, - 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x00, 0x00, 0x00, 0x00, 0x00, - // Numpad 0 -> 9 - 0x62, 0x59, 0x5A, 0x5B, 0x5C, 0x5D, 0x5E, 0x5F, 0x60, 0x61, - 0x55, // Multiply - 0x57, // Add - 0x00, // Separator - 0x56, // Subtract - 0x63, // Decimal - 0x54, // Divide - // F1 -> F12 - 0x3A, 0x3B, 0x3C, 0x3D, 0x3E, 0x3F, 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, - // F13 -> F24 - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, - 0x53, // Numlock - 0x47, // Scroll lock - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - // Modifier keys - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x33, // ';' - 0x2E, // Plus - 0x36, // Comma - 0x2D, // Minus - 0x37, // Period - 0x38, // '/' - 0x35, // '~' - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x2F, // '[' - 0x32, // '\' - 0x30, // ']' - 0x34, // ''' - 0x00, // - 0x00, // Nothing interesting past this point. - -}; - -u8 USB_KBD::m_KeyCodesAZERTY[256] = { - - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x2A, // Backspace - 0x2B, // Tab - 0x00, 0x00, - 0x00, // Clear - 0x28, // Return - 0x00, 0x00, - 0x00, // Shift - 0x00, // Control - 0x00, // ALT - 0x48, // Pause - 0x39, // Capital - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x29, // Escape - 0x00, 0x00, 0x00, 0x00, - 0x2C, // Space - 0x4B, // Prior - 0x4E, // Next - 0x4D, // End - 0x4A, // Home - 0x50, // Left - 0x52, // Up - 0x4F, // Right - 0x51, // Down - 0x00, 0x00, 0x00, - 0x46, // Print screen - 0x49, // Insert - 0x4C, // Delete - 0x00, - // 0 -> 9 - 0x27, 0x1E, 0x1F, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, - // A -> Z - 0x14, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x33, 0x11, 0x12, 0x13, - 0x04, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1D, 0x1B, 0x1C, 0x1A, 0x00, 0x00, 0x00, 0x00, 0x00, - // Numpad 0 -> 9 - 0x62, 0x59, 0x5A, 0x5B, 0x5C, 0x5D, 0x5E, 0x5F, 0x60, 0x61, - 0x55, // Multiply - 0x57, // Add - 0x00, // Separator - 0x56, // Substract - 0x63, // Decimal - 0x54, // Divide - // F1 -> F12 - 0x3A, 0x3B, 0x3C, 0x3D, 0x3E, 0x3F, 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, - // F13 -> F24 - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, - 0x53, // Numlock - 0x47, // Scroll lock - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - // Modifier keys - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x30, // '$' - 0x2E, // Plus - 0x10, // Comma - 0x00, // Minus - 0x36, // Period - 0x37, // '/' - 0x34, // ' ' - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x2D, // ')' - 0x32, // '\' - 0x2F, // '^' - 0x00, // ' ' - 0x38, // '!' - 0x00, // Nothing interesting past this point. - -}; -#else -u8 USB_KBD::m_KeyCodesQWERTY[256] = {0}; - -u8 USB_KBD::m_KeyCodesAZERTY[256] = {0}; -#endif } // 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 92cc16cd63..f48af1fc21 100644 --- a/Source/Core/Core/IOS/USB/USB_KBD.h +++ b/Source/Core/Core/IOS/USB/USB_KBD.h @@ -57,7 +57,5 @@ private: KBD_LAYOUT_AZERTY = 1 }; int m_KeyboardLayout; - static u8 m_KeyCodesQWERTY[256]; - static u8 m_KeyCodesAZERTY[256]; }; } // namespace IOS::HLE::Device From e8cc1b8d8ac4798c9ceeae481988d3695ea4226a Mon Sep 17 00:00:00 2001 From: Lioncash Date: Fri, 31 May 2019 08:26:31 -0400 Subject: [PATCH 2/8] 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 From cc54652fb3d7c7f9782d56b32a58e6f5257f6942 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Fri, 31 May 2019 08:30:32 -0400 Subject: [PATCH 3/8] IOS/USB_KBD: Migrate TODO comment above if statement Prevents some wonky formatting from occurring. --- Source/Core/Core/IOS/USB/USB_KBD.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Source/Core/Core/IOS/USB/USB_KBD.cpp b/Source/Core/Core/IOS/USB/USB_KBD.cpp index 1fa661b356..606bd71276 100644 --- a/Source/Core/Core/IOS/USB/USB_KBD.cpp +++ b/Source/Core/Core/IOS/USB/USB_KBD.cpp @@ -291,8 +291,8 @@ void USB_KBD::Update() Modifiers |= 0x10; if (GetAsyncKeyState(VK_RSHIFT) & 0x8000) Modifiers |= 0x20; - if (GetAsyncKeyState(VK_MENU) & - 0x8000) // TODO: VK_MENU is for ALT, not for ALT GR (ALT GR seems to work though...) + // TODO: VK_MENU is for ALT, not for ALT GR (ALT GR seems to work though...) + if (GetAsyncKeyState(VK_MENU) & 0x8000) Modifiers |= 0x40; if (GetAsyncKeyState(VK_RWIN) & 0x8000) Modifiers |= 0x80; From e0552e0642fa8b2efd05de402f65263ff61eba4a Mon Sep 17 00:00:00 2001 From: Lioncash Date: Fri, 31 May 2019 08:33:47 -0400 Subject: [PATCH 4/8] IOS/USB_KBD: Rename SMessageData to MessageData We don't need to prefix this with S to signify that it's a struct. --- Source/Core/Core/IOS/USB/USB_KBD.cpp | 4 ++-- Source/Core/Core/IOS/USB/USB_KBD.h | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/Source/Core/Core/IOS/USB/USB_KBD.cpp b/Source/Core/Core/IOS/USB/USB_KBD.cpp index 606bd71276..ac006c1444 100644 --- a/Source/Core/Core/IOS/USB/USB_KBD.cpp +++ b/Source/Core/Core/IOS/USB/USB_KBD.cpp @@ -176,7 +176,7 @@ constexpr std::array s_key_codes_azerty{}; #endif } // Anonymous namespace -USB_KBD::SMessageData::SMessageData(u32 type, u8 modifiers, PressedKeyData pressed_keys) +USB_KBD::MessageData::MessageData(u32 type, u8 modifiers, PressedKeyData pressed_keys) : MsgType{Common::swap32(type)}, Modifiers{modifiers}, PressedKeys{pressed_keys} { } @@ -213,7 +213,7 @@ IPCCommandResult USB_KBD::IOCtl(const IOCtlRequest& request) if (SConfig::GetInstance().m_WiiKeyboard && !Core::WantsDeterminism() && ControlReference::InputGateOn() && !m_MessageQueue.empty()) { - Memory::CopyToEmu(request.buffer_out, &m_MessageQueue.front(), sizeof(SMessageData)); + Memory::CopyToEmu(request.buffer_out, &m_MessageQueue.front(), sizeof(MessageData)); m_MessageQueue.pop(); } return GetDefaultReply(IPC_SUCCESS); diff --git a/Source/Core/Core/IOS/USB/USB_KBD.h b/Source/Core/Core/IOS/USB/USB_KBD.h index fa5caf5f70..bd53e0ae0e 100644 --- a/Source/Core/Core/IOS/USB/USB_KBD.h +++ b/Source/Core/Core/IOS/USB/USB_KBD.h @@ -35,7 +35,7 @@ private: using PressedKeyData = std::array; #pragma pack(push, 1) - struct SMessageData + struct MessageData { u32 MsgType = 0; u32 Unk1 = 0; @@ -43,10 +43,10 @@ private: u8 Unk2 = 0; PressedKeyData PressedKeys{}; - SMessageData(u32 msg_type, u8 modifiers, PressedKeyData pressed_keys); + MessageData(u32 msg_type, u8 modifiers, PressedKeyData pressed_keys); }; #pragma pack(pop) - std::queue m_MessageQueue; + std::queue m_MessageQueue; std::array m_OldKeyBuffer{}; u8 m_OldModifiers = 0; From bcdc5b5f7e94ebee49ccc8de77cc54cb9d64b372 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Fri, 31 May 2019 08:36:05 -0400 Subject: [PATCH 5/8] IOS/USB_KBD: Add static assertion to enforce MessageData as trivially copyable MessageData must be a trivially copyable type, given it's copied into emulated memory via our memory copy function CopyToEmu. Under the covers, this function utilizes memcpy. One of memcpy's requirements is that pointers to it point to types that are trivially copyable, otherwise the behavior is undefined. Given that, we can enforce this requirement at compile-time. --- Source/Core/Core/IOS/USB/USB_KBD.h | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Source/Core/Core/IOS/USB/USB_KBD.h b/Source/Core/Core/IOS/USB/USB_KBD.h index bd53e0ae0e..d0039d4ba7 100644 --- a/Source/Core/Core/IOS/USB/USB_KBD.h +++ b/Source/Core/Core/IOS/USB/USB_KBD.h @@ -7,6 +7,7 @@ #include #include #include +#include #include "Common/CommonTypes.h" #include "Core/IOS/Device.h" @@ -45,6 +46,8 @@ private: MessageData(u32 msg_type, u8 modifiers, PressedKeyData pressed_keys); }; + static_assert(std::is_trivially_copyable_v, + "MessageData must be trivially copyable, as it's copied into emulated memory."); #pragma pack(pop) std::queue m_MessageQueue; From d89ea8bf410d204c9c5c05a4564cd7c927d390d5 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Fri, 31 May 2019 08:40:41 -0400 Subject: [PATCH 6/8] IOS/USB_KBD: Make IsKeyPressed() a non-virtual const member function No other class inherits the USB_KBD class, and this function doesn't actually modify instance state, so it can be made a const member function. --- Source/Core/Core/IOS/USB/USB_KBD.cpp | 7 ++----- Source/Core/Core/IOS/USB/USB_KBD.h | 2 +- 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/Source/Core/Core/IOS/USB/USB_KBD.cpp b/Source/Core/Core/IOS/USB/USB_KBD.cpp index ac006c1444..63b41cb230 100644 --- a/Source/Core/Core/IOS/USB/USB_KBD.cpp +++ b/Source/Core/Core/IOS/USB/USB_KBD.cpp @@ -219,13 +219,10 @@ IPCCommandResult USB_KBD::IOCtl(const IOCtlRequest& request) return GetDefaultReply(IPC_SUCCESS); } -bool USB_KBD::IsKeyPressed(int _Key) +bool USB_KBD::IsKeyPressed(int key) const { #ifdef _WIN32 - if (GetAsyncKeyState(_Key) & 0x8000) - return true; - else - return false; + return (GetAsyncKeyState(key) & 0x8000) != 0; #else // TODO: do it for non-Windows platforms return false; diff --git a/Source/Core/Core/IOS/USB/USB_KBD.h b/Source/Core/Core/IOS/USB/USB_KBD.h index d0039d4ba7..0936a697d5 100644 --- a/Source/Core/Core/IOS/USB/USB_KBD.h +++ b/Source/Core/Core/IOS/USB/USB_KBD.h @@ -54,7 +54,7 @@ private: std::array m_OldKeyBuffer{}; u8 m_OldModifiers = 0; - virtual bool IsKeyPressed(int _Key); + bool IsKeyPressed(int key) const; // This stuff should probably die enum From 7f5ca64c4d1b307f90cf26054502da95ee3b952e Mon Sep 17 00:00:00 2001 From: Lioncash Date: Fri, 31 May 2019 08:46:17 -0400 Subject: [PATCH 7/8] IOS/USB_KBD: Make the message type enum an enum class Avoids polluting the surrounding scope with enum values. We can also make use of it in MessageData's constructor to enforce proper type passing. --- Source/Core/Core/IOS/USB/USB_KBD.cpp | 9 +++++---- Source/Core/Core/IOS/USB/USB_KBD.h | 12 ++++++------ 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/Source/Core/Core/IOS/USB/USB_KBD.cpp b/Source/Core/Core/IOS/USB/USB_KBD.cpp index 63b41cb230..6ea209c547 100644 --- a/Source/Core/Core/IOS/USB/USB_KBD.cpp +++ b/Source/Core/Core/IOS/USB/USB_KBD.cpp @@ -176,8 +176,9 @@ constexpr std::array s_key_codes_azerty{}; #endif } // Anonymous namespace -USB_KBD::MessageData::MessageData(u32 type, u8 modifiers, PressedKeyData pressed_keys) - : MsgType{Common::swap32(type)}, Modifiers{modifiers}, PressedKeys{pressed_keys} +USB_KBD::MessageData::MessageData(MessageType type, u8 modifiers, PressedKeyData pressed_keys) + : MsgType{Common::swap32(static_cast(type))}, Modifiers{modifiers}, PressedKeys{ + pressed_keys} { } @@ -198,7 +199,7 @@ IPCCommandResult USB_KBD::Open(const OpenRequest& request) m_OldKeyBuffer.fill(false); m_OldModifiers = 0x00; - // m_MessageQueue.emplace(MSG_KBD_CONNECT, 0, PressedKeyData{}); + // m_MessageQueue.emplace(MessageType::KeyboardConnect, 0, PressedKeyData{}); return Device::Open(request); } @@ -304,6 +305,6 @@ void USB_KBD::Update() } if (GotEvent) - m_MessageQueue.emplace(MSG_EVENT, Modifiers, PressedKeys); + m_MessageQueue.emplace(MessageType::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 0936a697d5..7ac5f9eab7 100644 --- a/Source/Core/Core/IOS/USB/USB_KBD.h +++ b/Source/Core/Core/IOS/USB/USB_KBD.h @@ -26,11 +26,11 @@ public: void Update() override; private: - enum + enum class MessageType : u32 { - MSG_KBD_CONNECT = 0, - MSG_KBD_DISCONNECT = 1, - MSG_EVENT = 2 + KeyboardConnect = 0, + KeyboardDisconnect = 1, + Event = 2 }; using PressedKeyData = std::array; @@ -38,13 +38,13 @@ private: #pragma pack(push, 1) struct MessageData { - u32 MsgType = 0; + MessageType MsgType{}; u32 Unk1 = 0; u8 Modifiers = 0; u8 Unk2 = 0; PressedKeyData PressedKeys{}; - MessageData(u32 msg_type, u8 modifiers, PressedKeyData pressed_keys); + MessageData(MessageType msg_type, u8 modifiers, PressedKeyData pressed_keys); }; static_assert(std::is_trivially_copyable_v, "MessageData must be trivially copyable, as it's copied into emulated memory."); From c8b950e716ed3946e7e50623d1a80de2dd5a5f4c Mon Sep 17 00:00:00 2001 From: Lioncash Date: Fri, 31 May 2019 09:04:27 -0400 Subject: [PATCH 8/8] IOS/USB_KBD: Normalize variable names Now, with all of the tidying done, we can cap it off by normalizing variable names to abide by our code formatting guidelines. --- Source/Core/Core/IOS/USB/USB_KBD.cpp | 80 ++++++++++++++-------------- Source/Core/Core/IOS/USB/USB_KBD.h | 18 +++---- 2 files changed, 49 insertions(+), 49 deletions(-) diff --git a/Source/Core/Core/IOS/USB/USB_KBD.cpp b/Source/Core/Core/IOS/USB/USB_KBD.cpp index 6ea209c547..77deb65948 100644 --- a/Source/Core/Core/IOS/USB/USB_KBD.cpp +++ b/Source/Core/Core/IOS/USB/USB_KBD.cpp @@ -177,8 +177,8 @@ constexpr std::array s_key_codes_azerty{}; } // Anonymous namespace USB_KBD::MessageData::MessageData(MessageType type, u8 modifiers, PressedKeyData pressed_keys) - : MsgType{Common::swap32(static_cast(type))}, Modifiers{modifiers}, PressedKeys{ - pressed_keys} + : msg_type{Common::swap32(static_cast(type))}, modifiers{modifiers}, pressed_keys{ + pressed_keys} { } @@ -193,13 +193,13 @@ IPCCommandResult USB_KBD::Open(const OpenRequest& request) INFO_LOG(IOS, "USB_KBD: Open"); IniFile ini; ini.Load(File::GetUserPath(F_DOLPHINCONFIG_IDX)); - ini.GetOrCreateSection("USB Keyboard")->Get("Layout", &m_KeyboardLayout, KBD_LAYOUT_QWERTY); + ini.GetOrCreateSection("USB Keyboard")->Get("Layout", &m_keyboard_layout, KBD_LAYOUT_QWERTY); - m_MessageQueue = {}; - m_OldKeyBuffer.fill(false); - m_OldModifiers = 0x00; + m_message_queue = {}; + m_old_key_buffer.fill(false); + m_old_modifiers = 0x00; - // m_MessageQueue.emplace(MessageType::KeyboardConnect, 0, PressedKeyData{}); + // m_message_queue.emplace(MessageType::KeyboardConnect, 0, PressedKeyData{}); return Device::Open(request); } @@ -212,10 +212,10 @@ IPCCommandResult USB_KBD::Write(const ReadWriteRequest& request) IPCCommandResult USB_KBD::IOCtl(const IOCtlRequest& request) { if (SConfig::GetInstance().m_WiiKeyboard && !Core::WantsDeterminism() && - ControlReference::InputGateOn() && !m_MessageQueue.empty()) + ControlReference::InputGateOn() && !m_message_queue.empty()) { - Memory::CopyToEmu(request.buffer_out, &m_MessageQueue.front(), sizeof(MessageData)); - m_MessageQueue.pop(); + Memory::CopyToEmu(request.buffer_out, &m_message_queue.front(), sizeof(MessageData)); + m_message_queue.pop(); } return GetDefaultReply(IPC_SUCCESS); } @@ -235,76 +235,76 @@ void USB_KBD::Update() if (!SConfig::GetInstance().m_WiiKeyboard || Core::WantsDeterminism() || !m_is_active) return; - u8 Modifiers = 0x00; - PressedKeyData PressedKeys{0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; - bool GotEvent = false; + u8 modifiers = 0x00; + PressedKeyData pressed_keys{0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; + bool got_event = false; size_t num_pressed_keys = 0; - for (size_t i = 0; i < m_OldKeyBuffer.size(); i++) + for (size_t i = 0; i < m_old_key_buffer.size(); i++) { - bool KeyPressedNow = IsKeyPressed(static_cast(i)); - bool KeyPressedBefore = m_OldKeyBuffer[i]; - u8 KeyCode = 0; + const bool key_pressed_now = IsKeyPressed(static_cast(i)); + const bool key_pressed_before = m_old_key_buffer[i]; + u8 key_code = 0; - if (KeyPressedNow ^ KeyPressedBefore) + if (key_pressed_now ^ key_pressed_before) { - if (KeyPressedNow) + if (key_pressed_now) { - switch (m_KeyboardLayout) + switch (m_keyboard_layout) { case KBD_LAYOUT_QWERTY: - KeyCode = s_key_codes_qwerty[i]; + key_code = s_key_codes_qwerty[i]; break; case KBD_LAYOUT_AZERTY: - KeyCode = s_key_codes_azerty[i]; + key_code = s_key_codes_azerty[i]; break; } - if (KeyCode == 0x00) + if (key_code == 0x00) continue; - PressedKeys[num_pressed_keys] = KeyCode; + pressed_keys[num_pressed_keys] = key_code; num_pressed_keys++; - if (num_pressed_keys == PressedKeys.size()) + if (num_pressed_keys == pressed_keys.size()) break; } - GotEvent = true; + got_event = true; } - m_OldKeyBuffer[i] = KeyPressedNow; + m_old_key_buffer[i] = key_pressed_now; } #ifdef _WIN32 if (GetAsyncKeyState(VK_LCONTROL) & 0x8000) - Modifiers |= 0x01; + modifiers |= 0x01; if (GetAsyncKeyState(VK_LSHIFT) & 0x8000) - Modifiers |= 0x02; + modifiers |= 0x02; if (GetAsyncKeyState(VK_MENU) & 0x8000) - Modifiers |= 0x04; + modifiers |= 0x04; if (GetAsyncKeyState(VK_LWIN) & 0x8000) - Modifiers |= 0x08; + modifiers |= 0x08; if (GetAsyncKeyState(VK_RCONTROL) & 0x8000) - Modifiers |= 0x10; + modifiers |= 0x10; if (GetAsyncKeyState(VK_RSHIFT) & 0x8000) - Modifiers |= 0x20; + modifiers |= 0x20; // TODO: VK_MENU is for ALT, not for ALT GR (ALT GR seems to work though...) if (GetAsyncKeyState(VK_MENU) & 0x8000) - Modifiers |= 0x40; + modifiers |= 0x40; if (GetAsyncKeyState(VK_RWIN) & 0x8000) - Modifiers |= 0x80; + modifiers |= 0x80; #else // TODO: modifiers for non-Windows platforms #endif - if (Modifiers ^ m_OldModifiers) + if (modifiers ^ m_old_modifiers) { - GotEvent = true; - m_OldModifiers = Modifiers; + got_event = true; + m_old_modifiers = modifiers; } - if (GotEvent) - m_MessageQueue.emplace(MessageType::Event, Modifiers, PressedKeys); + if (got_event) + m_message_queue.emplace(MessageType::Event, modifiers, pressed_keys); } } // 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 7ac5f9eab7..3928714e20 100644 --- a/Source/Core/Core/IOS/USB/USB_KBD.h +++ b/Source/Core/Core/IOS/USB/USB_KBD.h @@ -38,21 +38,21 @@ private: #pragma pack(push, 1) struct MessageData { - MessageType MsgType{}; - u32 Unk1 = 0; - u8 Modifiers = 0; - u8 Unk2 = 0; - PressedKeyData PressedKeys{}; + MessageType msg_type{}; + u32 unk1 = 0; + u8 modifiers = 0; + u8 unk2 = 0; + PressedKeyData pressed_keys{}; MessageData(MessageType msg_type, u8 modifiers, PressedKeyData pressed_keys); }; static_assert(std::is_trivially_copyable_v, "MessageData must be trivially copyable, as it's copied into emulated memory."); #pragma pack(pop) - std::queue m_MessageQueue; + std::queue m_message_queue; - std::array m_OldKeyBuffer{}; - u8 m_OldModifiers = 0; + std::array m_old_key_buffer{}; + u8 m_old_modifiers = 0; bool IsKeyPressed(int key) const; @@ -62,6 +62,6 @@ private: KBD_LAYOUT_QWERTY = 0, KBD_LAYOUT_AZERTY = 1 }; - int m_KeyboardLayout = KBD_LAYOUT_QWERTY; + int m_keyboard_layout = KBD_LAYOUT_QWERTY; }; } // namespace IOS::HLE::Device