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.
This commit is contained in:
Lioncash 2019-05-31 08:26:31 -04:00
parent 64564e337b
commit e8cc1b8d8a
2 changed files with 23 additions and 33 deletions

View File

@ -5,7 +5,6 @@
#include "Core/IOS/USB/USB_KBD.h" #include "Core/IOS/USB/USB_KBD.h"
#include <array> #include <array>
#include <cstring>
#include <queue> #include <queue>
#include "Common/FileUtil.h" #include "Common/FileUtil.h"
@ -177,17 +176,9 @@ constexpr std::array<u8, 256> s_key_codes_azerty{};
#endif #endif
} // Anonymous namespace } // 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. // TODO: support in netplay/movies.
@ -203,15 +194,11 @@ IPCCommandResult USB_KBD::Open(const OpenRequest& request)
ini.Load(File::GetUserPath(F_DOLPHINCONFIG_IDX)); 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_KeyboardLayout, KBD_LAYOUT_QWERTY);
m_MessageQueue = std::queue<SMessageData>(); m_MessageQueue = {};
for (bool& pressed : m_OldKeyBuffer) m_OldKeyBuffer.fill(false);
{
pressed = false;
}
m_OldModifiers = 0x00; m_OldModifiers = 0x00;
// m_MessageQueue.push(SMessageData(MSG_KBD_CONNECT, 0, nullptr)); // m_MessageQueue.emplace(MSG_KBD_CONNECT, 0, PressedKeyData{});
return Device::Open(request); return Device::Open(request);
} }
@ -251,12 +238,12 @@ void USB_KBD::Update()
return; return;
u8 Modifiers = 0x00; u8 Modifiers = 0x00;
u8 PressedKeys[6] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; PressedKeyData PressedKeys{0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
bool GotEvent = false; bool GotEvent = false;
int num_pressed_keys = 0; size_t num_pressed_keys = 0;
for (int i = 0; i < 256; i++) for (size_t i = 0; i < m_OldKeyBuffer.size(); i++)
{ {
bool KeyPressedNow = IsKeyPressed(i); bool KeyPressedNow = IsKeyPressed(static_cast<int>(i));
bool KeyPressedBefore = m_OldKeyBuffer[i]; bool KeyPressedBefore = m_OldKeyBuffer[i];
u8 KeyCode = 0; u8 KeyCode = 0;
@ -281,7 +268,7 @@ void USB_KBD::Update()
PressedKeys[num_pressed_keys] = KeyCode; PressedKeys[num_pressed_keys] = KeyCode;
num_pressed_keys++; num_pressed_keys++;
if (num_pressed_keys == 6) if (num_pressed_keys == PressedKeys.size())
break; break;
} }
@ -320,6 +307,6 @@ void USB_KBD::Update()
} }
if (GotEvent) if (GotEvent)
m_MessageQueue.push(SMessageData(MSG_EVENT, Modifiers, PressedKeys)); m_MessageQueue.emplace(MSG_EVENT, Modifiers, PressedKeys);
} }
} // namespace IOS::HLE::Device } // namespace IOS::HLE::Device

View File

@ -4,6 +4,7 @@
#pragma once #pragma once
#include <array>
#include <queue> #include <queue>
#include <string> #include <string>
@ -31,22 +32,24 @@ private:
MSG_EVENT = 2 MSG_EVENT = 2
}; };
using PressedKeyData = std::array<u8, 6>;
#pragma pack(push, 1) #pragma pack(push, 1)
struct SMessageData struct SMessageData
{ {
u32 MsgType; u32 MsgType = 0;
u32 Unk1; u32 Unk1 = 0;
u8 Modifiers; u8 Modifiers = 0;
u8 Unk2; u8 Unk2 = 0;
u8 PressedKeys[6]; PressedKeyData PressedKeys{};
SMessageData(u32 msg_type, u8 modifiers, u8* pressed_keys); SMessageData(u32 msg_type, u8 modifiers, PressedKeyData pressed_keys);
}; };
#pragma pack(pop) #pragma pack(pop)
std::queue<SMessageData> m_MessageQueue; std::queue<SMessageData> m_MessageQueue;
bool m_OldKeyBuffer[256]; std::array<bool, 256> m_OldKeyBuffer{};
u8 m_OldModifiers; u8 m_OldModifiers = 0;
virtual bool IsKeyPressed(int _Key); virtual bool IsKeyPressed(int _Key);
@ -56,6 +59,6 @@ private:
KBD_LAYOUT_QWERTY = 0, KBD_LAYOUT_QWERTY = 0,
KBD_LAYOUT_AZERTY = 1 KBD_LAYOUT_AZERTY = 1
}; };
int m_KeyboardLayout; int m_KeyboardLayout = KBD_LAYOUT_QWERTY;
}; };
} // namespace IOS::HLE::Device } // namespace IOS::HLE::Device