hid: Added hidGetKeyboardStates, which hidScanInput now uses.

Struct adjustments.
This commit is contained in:
yellows8 2020-11-19 17:53:05 -05:00 committed by fincs
parent 52bf73e047
commit d4c1d069f1
No known key found for this signature in database
GPG Key ID: 62C7609ADA219C60
2 changed files with 37 additions and 30 deletions

View File

@ -530,20 +530,25 @@ typedef struct HidMouseLifo {
// Begin HidKeyboard
/// HidKeyboardEntry
typedef struct HidKeyboardEntry {
/// HidKeyboardState
typedef struct HidKeyboardState {
u64 timestamp;
u64 timestamp_2;
u64 modifier;
u32 keys[8];
} HidKeyboardEntry;
} HidKeyboardState;
/// HidKeyboard
typedef struct HidKeyboard {
/// HidKeyboardStateAtomicStorage
typedef struct HidKeyboardStateAtomicStorage {
u64 timestamp;
HidKeyboardState state;
} HidKeyboardStateAtomicStorage;
/// HidKeyboardLifo
typedef struct HidKeyboardLifo {
HidCommonStateHeader header;
HidKeyboardEntry entries[17];
HidKeyboardStateAtomicStorage entries[17];
u8 padding[0x28];
} HidKeyboard;
} HidKeyboardLifo;
// End HidKeyboard
@ -769,7 +774,7 @@ typedef struct HidSharedMemory {
u8 debug_pad[0x400];
HidTouchScreenLifo touchscreen;
HidMouseLifo mouse;
HidKeyboard keyboard;
HidKeyboardLifo keyboard;
u8 digitizer[0x1000]; ///< [10.0.0+] Digitizer [1.0.0-9.2.0] BasicXpad
u8 home_button[0x200];
u8 sleep_button[0x200];
@ -879,6 +884,8 @@ size_t hidGetTouchScreenStates(HidTouchScreenState *states, size_t count);
size_t hidGetMouseStates(HidMouseState *states, size_t count);
size_t hidGetKeyboardStates(HidKeyboardState *states, size_t count);
/// Gets a bitfield of \ref HidNpadStyleTag for the specified controller.
u32 hidGetNpadStyleSet(HidNpadIdType id);

View File

@ -18,7 +18,7 @@ static SharedMemory g_hidSharedmem;
static HidTouchScreenState g_touchScreenState;
static HidMouseState g_mouseState;
static HidKeyboardEntry g_keyboardEntry;
static HidKeyboardState g_keyboardState;
static HidNpadStateEntry g_controllerEntries[10];
static u64 g_mouseOld, g_mouseHeld, g_mouseDown, g_mouseUp;
@ -26,8 +26,6 @@ static u64 g_keyboardModOld, g_keyboardModHeld, g_keyboardModDown, g_keyboardMod
static u32 g_keyboardOld[8], g_keyboardHeld[8], g_keyboardDown[8], g_keyboardUp[8];
static u64 g_controllerOld[10], g_controllerHeld[10], g_controllerDown[10], g_controllerUp[10];
static u64 g_keyboardTimestamp;
static HidControllerID g_controllerP1AutoID;
static u8* g_sevenSixAxisSensorBuffer;
@ -120,7 +118,7 @@ void hidReset(void) {
// Reset internal state
memset(&g_touchScreenState, 0, sizeof(HidTouchScreenState));
memset(&g_mouseState, 0, sizeof(HidMouseState));
memset(&g_keyboardEntry, 0, sizeof(HidKeyboardEntry));
memset(&g_keyboardState, 0, sizeof(HidKeyboardState));
memset(g_controllerEntries, 0, sizeof(g_controllerEntries));
g_mouseOld = g_mouseHeld = g_mouseDown = g_mouseUp = 0;
@ -146,8 +144,6 @@ void* hidGetSharedmemAddr(void) {
void hidScanInput(void) {
rwlockWriteLock(&g_hidLock);
HidSharedMemory *sharedMem = (HidSharedMemory*)hidGetSharedmemAddr();
g_mouseOld = g_mouseHeld;
g_keyboardModOld = g_keyboardModHeld;
memcpy(g_keyboardOld, g_keyboardHeld, sizeof(g_keyboardOld));
@ -159,7 +155,7 @@ void hidScanInput(void) {
memset(g_controllerHeld, 0, sizeof(g_controllerHeld));
memset(&g_touchScreenState, 0, sizeof(HidTouchScreenState));
memset(&g_mouseState, 0, sizeof(HidMouseState));
memset(&g_keyboardEntry, 0, sizeof(HidKeyboardEntry));
memset(&g_keyboardState, 0, sizeof(HidKeyboardState));
memset(g_controllerEntries, 0, sizeof(g_controllerEntries));
if (hidGetTouchScreenStates(&g_touchScreenState, 1)) {
@ -173,16 +169,10 @@ void hidScanInput(void) {
g_mouseUp = g_mouseOld & (~g_mouseHeld);
}
u64 latestKeyboardEntry = sharedMem->keyboard.header.latest_entry;
HidKeyboardEntry *newKeyboardEntry = &sharedMem->keyboard.entries[latestKeyboardEntry];
if ((s64)(newKeyboardEntry->timestamp - g_keyboardTimestamp) >= 0) {
memcpy(&g_keyboardEntry, newKeyboardEntry, sizeof(HidKeyboardEntry));
g_keyboardTimestamp = newKeyboardEntry->timestamp;
g_keyboardModHeld = g_keyboardEntry.modifier;
if (hidGetKeyboardStates(&g_keyboardState, 1)) {
g_keyboardModHeld = g_keyboardState.modifier;
for (u32 i = 0; i < 8; i++) {
g_keyboardHeld[i] = g_keyboardEntry.keys[i];
}
g_keyboardHeld[i] = g_keyboardState.keys[i];
}
g_keyboardModDown = (~g_keyboardModOld) & g_keyboardModHeld;
g_keyboardModUp = g_keyboardModOld & (~g_keyboardModHeld);
@ -190,6 +180,7 @@ void hidScanInput(void) {
g_keyboardDown[i] = (~g_keyboardOld[i]) & g_keyboardHeld[i];
g_keyboardUp[i] = g_keyboardOld[i] & (~g_keyboardHeld[i]);
}
}
for (u32 i = 0; i < 10; i++) {
HidNpadIdType id = hidControllerIDToNpadIdType(i);
@ -425,6 +416,15 @@ size_t hidGetMouseStates(HidMouseState *states, size_t count) {
return total;
}
size_t hidGetKeyboardStates(HidKeyboardState *states, size_t count) {
HidSharedMemory *sharedmem = (HidSharedMemory*)hidGetSharedmemAddr();
if (sharedmem == NULL)
diagAbortWithResult(MAKERESULT(Module_Libnx, LibnxError_NotInitialized));
size_t total = _hidGetStates(&sharedmem->keyboard.header, sharedmem->keyboard.entries, offsetof(HidKeyboardState,timestamp), states, sizeof(HidKeyboardState), count);
return total;
}
static size_t _hidGetNpadStates(HidNpad *npad, u32 layout, HidNpadStateEntry *states, size_t count) {
HidControllerLayout *states_buf = &npad->layouts[layout];
if (count > 17) count = 17;