From 2b7c3fee901ce8700eae449e1c6e6cb97c298d1b Mon Sep 17 00:00:00 2001 From: yellows8 Date: Mon, 23 Nov 2020 23:09:31 -0500 Subject: [PATCH] hiddbg: Use structs for handles and various adjustments. --- nx/include/switch/services/hiddbg.h | 58 +++++++++++++--------- nx/source/services/hiddbg.c | 76 ++++++++++++++--------------- 2 files changed, 72 insertions(+), 62 deletions(-) diff --git a/nx/include/switch/services/hiddbg.h b/nx/include/switch/services/hiddbg.h index 11093ae4..4912e01f 100644 --- a/nx/include/switch/services/hiddbg.h +++ b/nx/include/switch/services/hiddbg.h @@ -9,6 +9,11 @@ #include "../services/hidsys.h" #include "../sf/service.h" +/// HdlsHandle +typedef struct { + u64 handle; ///< Handle +} HiddbgHdlsHandle; + /// HdlsDeviceInfo, for [7.0.0-8.1.0]. typedef struct { u32 deviceTypeInternal; ///< Only one bit can be set. BIT(N*4+0) = Pro-Controller, BIT(N*4+1) = Joy-Con Left, BIT(N*4+2) = Joy-Con Right, BIT(N*4+3) = invalid. Where N is 0-1. BIT(8-10) = Pro-Controller, BIT(11) = Famicom-Controller, BIT(12) = Famicom-Controller II with microphone, BIT(13) = NES-Controller(DeviceType=0x200), BIT(14) = NES-Controller(DeviceType=0x400), BIT(15-16) = invalid, BIT(17) = unknown(DeviceType=0x8000), BIT(18-20) = invalid, BIT(21-23) = unknown(DeviceType=0x80000000). @@ -31,20 +36,20 @@ typedef struct { /// HdlsState, for [7.0.0-8.1.0]. typedef struct { - u8 powerConnected; ///< powerConnected for the main PowerInfo, see \ref HidFlags. - u8 flags; ///< ORRed with powerConnected to set the value of the first byte for \ref HidFlags. For example, value 1 here will set isCharging for the main PowerInfo. + u8 is_powered; ///< IsPowered for the main PowerInfo, see \ref HidNpadSystemProperties. + u8 flags; ///< ORRed with IsPowered to set the value of the first byte for \ref HidNpadSystemProperties. For example, value 1 here will set IsCharging for the main PowerInfo. u8 unk_x2[0x6]; ///< Unknown - u32 batteryCharge; ///< batteryCharge for the main PowerInfo, see \ref HidPowerInfo. + u32 battery_level; ///< BatteryLevel for the main PowerInfo, see \ref HidPowerInfo. u32 buttons; ///< See \ref HidControllerKeys. JoystickPosition joysticks[JOYSTICK_NUM_STICKS]; ///< \ref JoystickPosition u8 unk_x20; ///< Unused for input. Set with output from \ref hiddbgDumpHdlsStates. Not set by \ref hiddbgGetAbstractedPadsState. u8 padding[0x3]; ///< Padding } HiddbgHdlsStateV7; -/// HdlsState, for [9.0.0+]. Converted to/from \ref HiddbgHdlsDeviceInfoV7 on prior sysvers. +/// HdlsState, for [9.0.0+]. Converted to/from \ref HiddbgHdlsStateV7 on prior sysvers. typedef struct { - u32 batteryCharge; ///< batteryCharge for the main PowerInfo, see \ref HidPowerInfo. - u32 flags; ///< Used to set the main PowerInfo for \ref HidFlags. BIT(0) -> powerConnected, BIT(1) -> isCharging. + u32 battery_level; ///< BatteryLevel for the main PowerInfo, see \ref HidPowerInfo. + u32 flags; ///< Used to set the main PowerInfo for \ref HidNpadSystemProperties. BIT(0) -> IsPowered, BIT(1) -> IsCharging. u64 buttons; ///< See \ref HidControllerKeys. [9.0.0+] Masked with 0xfffffffff00fffff. JoystickPosition joysticks[JOYSTICK_NUM_STICKS]; ///< \ref JoystickPosition u8 unk_x20; ///< Unused for input. Set with output from \ref hiddbgDumpHdlsStates. @@ -53,7 +58,7 @@ typedef struct { /// HdlsNpadAssignmentEntry typedef struct { - u64 HdlsHandle; ///< HdlsHandle + HiddbgHdlsHandle handle; ///< \ref HiddbgHdlsHandle u32 unk_x8; ///< Unknown u32 unk_xc; ///< Unknown u64 unk_x10; ///< Unknown @@ -70,9 +75,9 @@ typedef struct { /// HdlsStateListEntryV7, for [7.0.0-8.1.0]. typedef struct { - u64 HdlsHandle; ///< HdlsHandle + HiddbgHdlsHandle handle; ///< \ref HiddbgHdlsHandle HiddbgHdlsDeviceInfoV7 device; ///< \ref HiddbgHdlsDeviceInfoV7. With \ref hiddbgApplyHdlsStateList this is only used when creating new devices. - HiddbgHdlsStateV7 state; ///< \ref HiddbgHdlsState + HiddbgHdlsStateV7 state; ///< \ref HiddbgHdlsState } HiddbgHdlsStateListEntryV7; /// HdlsStateListV7, for [7.0.0-8.1.0]. This contains a list of all controllers, including non-virtual controllers. @@ -84,7 +89,7 @@ typedef struct { /// HdlsStateListEntry, for [9.0.0+]. Converted to/from \ref HiddbgHdlsStateListEntryV7 on prior sysvers. typedef struct { - u64 HdlsHandle; ///< HdlsHandle + HiddbgHdlsHandle handle; ///< \ref HiddbgHdlsHandle HiddbgHdlsDeviceInfo device; ///< \ref HiddbgHdlsDeviceInfo. With \ref hiddbgApplyHdlsStateList this is only used when creating new devices. alignas(8) HiddbgHdlsState state; ///< \ref HiddbgHdlsState } HiddbgHdlsStateListEntry; @@ -97,6 +102,11 @@ typedef struct { HiddbgHdlsStateListEntry entries[0x10]; ///< \ref HiddbgHdlsStateListEntry } HiddbgHdlsStateList; +/// AbstractedPadHandle +typedef struct { + u64 handle; ///< Handle +} HiddbgAbstractedPadHandle; + /// AbstractedPadState typedef struct { u32 type; ///< Type. Converted to HiddbgHdlsDeviceInfoV7::type internally by \ref hiddbgSetAutoPilotVirtualPadState. BIT(0) -> BIT(0), BIT(1) -> BIT(15), BIT(2-3) -> BIT(1-2), BIT(4-5) -> BIT(1-2), BIT(6) -> BIT(3). BIT(7-11) -> BIT(11-15), BIT(12-14) -> BIT(12-14), BIT(15) -> BIT(17), BIT(31) -> BIT(21). @@ -108,9 +118,9 @@ typedef struct { u8 npadInterfaceType; ///< See HiddbgHdlsDeviceInfo::npadInterfaceType. u8 pad2[0x3]; ///< Padding - HiddbgHdlsStateV7 state; ///< State + HiddbgHdlsStateV7 state; ///< State - u8 unused[0x60]; ///< Unused with \ref hiddbgSetAutoPilotVirtualPadState. Not set by \ref hiddbgGetAbstractedPadsState. + u8 unused[0x60]; ///< Unused with \ref hiddbgSetAutoPilotVirtualPadState. Not set by \ref hiddbgGetAbstractedPadsState. } HiddbgAbstractedPadState; /// Initialize hiddbg. @@ -156,17 +166,17 @@ Result hiddbgGetOperationResult(HidsysUniquePadId unique_pad_id); /// Pre-9.0.0 the output is an u32, with [9.0.0+] it's an u8. Result hiddbgGetUniquePadDeviceTypeSetInternal(HidsysUniquePadId unique_pad_id, u32 *out); -/// Gets a list of AbstractedPadHandles, where AbstractedPadHandles is the output array with max entries = count. total_entries is total entries written to the output array. +/// Gets a list of \ref HiddbgAbstractedPadHandle, where handles is the output array with max entries = count. total_out is total entries written to the output array. /// Only available with [5.0.0-8.1.0]. -Result hiddbgGetAbstractedPadHandles(u64 *AbstractedPadHandles, s32 count, s32 *total_entries); +Result hiddbgGetAbstractedPadHandles(HiddbgAbstractedPadHandle *handles, s32 count, s32 *total_out); -/// Gets the state for the specified AbstractedPadHandle. +/// Gets the state for the specified \ref HiddbgAbstractedPadHandle. /// Only available with [5.0.0-8.1.0]. -Result hiddbgGetAbstractedPadState(u64 AbstractedPadHandle, HiddbgAbstractedPadState *state); +Result hiddbgGetAbstractedPadState(HiddbgAbstractedPadHandle handle, HiddbgAbstractedPadState *state); /// Similar to \ref hiddbgGetAbstractedPadHandles except this also returns the state for each pad in output array states. /// Only available with [5.0.0-8.1.0]. -Result hiddbgGetAbstractedPadsState(u64 *AbstractedPadHandles, HiddbgAbstractedPadState *states, s32 count, s32 *total_entries); +Result hiddbgGetAbstractedPadsState(HiddbgAbstractedPadHandle *handles, HiddbgAbstractedPadState *states, s32 count, s32 *total_entries); /// Sets AutoPilot state for the specified pad. /// AbstractedVirtualPadId can be any unique value as long as it's within bounds. For example, 0-7 is usable. @@ -186,8 +196,8 @@ Result hiddbgAttachHdlsWorkBuffer(void); /// Exit Hdls, must be called at some point prior to hiddbgExit. Only available with [7.0.0+]. Result hiddbgReleaseHdlsWorkBuffer(void); -/// Checks if the given HdlsHandle is still attached, where the result is written to isAttached. Only available with [7.0.0+]. -Result hiddbgIsHdlsVirtualDeviceAttached(u64 HdlsHandle, bool *isAttached); +/// Checks if the given device is still attached, where the result is written to out. Only available with [7.0.0+]. +Result hiddbgIsHdlsVirtualDeviceAttached(HiddbgHdlsHandle handle, bool *out); /// Gets state for \ref HiddbgHdlsNpadAssignment. Only available with [7.0.0+]. Result hiddbgDumpHdlsNpadAssignmentState(HiddbgHdlsNpadAssignment *state); @@ -199,15 +209,15 @@ Result hiddbgDumpHdlsStates(HiddbgHdlsStateList *state); Result hiddbgApplyHdlsNpadAssignmentState(const HiddbgHdlsNpadAssignment *state, bool flag); /// Sets state for \ref HiddbgHdlsStateList. Only available with [7.0.0+]. -/// The HiddbgHdlsState will be applied for each HdlsHandle. If a HdlsHandle is not found, code similar to \ref hiddbgAttachHdlsVirtualDevice will run with the \ref HiddbgHdlsDeviceInfo, then it will continue with applying state with the new device. +/// The HiddbgHdlsState will be applied for each \ref HiddbgHdlsHandle. If a \ref HiddbgHdlsHandle is not found, code similar to \ref hiddbgAttachHdlsVirtualDevice will run with the \ref HiddbgHdlsDeviceInfo, then it will continue with applying state with the new device. Result hiddbgApplyHdlsStateList(const HiddbgHdlsStateList *state); -/// Attach a device with the input info, where the output handle is written to HdlsHandle. Only available with [7.0.0+]. -Result hiddbgAttachHdlsVirtualDevice(u64 *HdlsHandle, const HiddbgHdlsDeviceInfo *info); +/// Attach a device with the input info, where the output handle is written to handle. Only available with [7.0.0+]. +Result hiddbgAttachHdlsVirtualDevice(HiddbgHdlsHandle *handle, const HiddbgHdlsDeviceInfo *info); /// Detach the specified device. Only available with [7.0.0+]. -Result hiddbgDetachHdlsVirtualDevice(u64 HdlsHandle); +Result hiddbgDetachHdlsVirtualDevice(HiddbgHdlsHandle handle); /// Sets state for the specified device. Only available with [7.0.0+]. -Result hiddbgSetHdlsState(u64 HdlsHandle, const HiddbgHdlsState *state); +Result hiddbgSetHdlsState(HiddbgHdlsHandle handle, const HiddbgHdlsState *state); diff --git a/nx/source/services/hiddbg.c b/nx/source/services/hiddbg.c index c5b05b22..7e15ca1f 100644 --- a/nx/source/services/hiddbg.c +++ b/nx/source/services/hiddbg.c @@ -218,24 +218,24 @@ Result hiddbgGetUniquePadDeviceTypeSetInternal(HidsysUniquePadId unique_pad_id, return rc; } -Result hiddbgGetAbstractedPadHandles(u64 *AbstractedPadHandles, s32 count, s32 *total_entries) { +Result hiddbgGetAbstractedPadHandles(HiddbgAbstractedPadHandle *handles, s32 count, s32 *total_out) { if (hosversionBefore(5,0,0) || hosversionAtLeast(9,0,0)) return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer); - return serviceDispatchOut(&g_hiddbgSrv, 301, *total_entries, + return serviceDispatchOut(&g_hiddbgSrv, 301, *total_out, .buffer_attrs = { SfBufferAttr_HipcPointer | SfBufferAttr_Out }, - .buffers = { { AbstractedPadHandles, count*sizeof(u64) } }, + .buffers = { { handles, count*sizeof(HiddbgAbstractedPadHandle) } }, ); } -Result hiddbgGetAbstractedPadState(u64 AbstractedPadHandle, HiddbgAbstractedPadState *state) { +Result hiddbgGetAbstractedPadState(HiddbgAbstractedPadHandle handle, HiddbgAbstractedPadState *state) { if (hosversionBefore(5,0,0) || hosversionAtLeast(9,0,0)) return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer); - return serviceDispatchInOut(&g_hiddbgSrv, 302, AbstractedPadHandle, *state); + return serviceDispatchInOut(&g_hiddbgSrv, 302, handle, *state); } -Result hiddbgGetAbstractedPadsState(u64 *AbstractedPadHandles, HiddbgAbstractedPadState *states, s32 count, s32 *total_entries) { +Result hiddbgGetAbstractedPadsState(HiddbgAbstractedPadHandle *handles, HiddbgAbstractedPadState *states, s32 count, s32 *total_entries) { if (hosversionBefore(5,0,0) || hosversionAtLeast(9,0,0)) return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer); @@ -245,7 +245,7 @@ Result hiddbgGetAbstractedPadsState(u64 *AbstractedPadHandles, HiddbgAbstractedP SfBufferAttr_HipcAutoSelect | SfBufferAttr_Out, }, .buffers = { - { AbstractedPadHandles, count*sizeof(u64) }, + { handles, count*sizeof(HiddbgAbstractedPadHandle) }, { states, count*sizeof(HiddbgAbstractedPadState) }, }, ); @@ -312,9 +312,9 @@ static void _hiddbgConvertHdlsDeviceInfoFromV7(HiddbgHdlsDeviceInfo *out, const static void _hiddbgConvertHiddbgHdlsStateToV7(HiddbgHdlsStateV7 *out, const HiddbgHdlsState *in) { memset(out, 0, sizeof(*out)); - out->powerConnected = (in->flags & BIT(0)) != 0; + out->is_powered = (in->flags & BIT(0)) != 0; out->flags = (in->flags & BIT(1)) != 0; - out->batteryCharge = in->batteryCharge; + out->battery_level = in->battery_level; out->buttons = in->buttons; memcpy(out->joysticks, in->joysticks, sizeof(in->joysticks)); out->unk_x20 = in->unk_x20; @@ -323,8 +323,8 @@ static void _hiddbgConvertHiddbgHdlsStateToV7(HiddbgHdlsStateV7 *out, const Hidd static void _hiddbgConvertHiddbgHdlsStateFromV7(HiddbgHdlsState *out, const HiddbgHdlsStateV7 *in) { memset(out, 0, sizeof(*out)); - out->batteryCharge = in->batteryCharge; - out->flags = (in->powerConnected & 1) | ((in->flags & 1)<<1); + out->battery_level = in->battery_level; + out->flags = (in->is_powered & 1) | ((in->flags & 1)<<1); out->buttons = in->buttons; memcpy(out->joysticks, in->joysticks, sizeof(in->joysticks)); out->unk_x20 = in->unk_x20; @@ -337,7 +337,7 @@ static void _hiddbgConvertHdlsStateListToV7(HiddbgHdlsStateListV7 *out, const Hi count = out->total_entries > 0x10 ? 0x10 : out->total_entries; for (s32 i=0; ientries[i].HdlsHandle = in->entries[i].HdlsHandle; + out->entries[i].handle = in->entries[i].handle; _hiddbgConvertHdlsDeviceInfoToV7(&out->entries[i].device, &in->entries[i].device); _hiddbgConvertHiddbgHdlsStateToV7(&out->entries[i].state, &in->entries[i].state); } @@ -350,7 +350,7 @@ static void _hiddbgConvertHdlsStateListFromV7(HiddbgHdlsStateList *out, const Hi count = out->total_entries > 0x10 ? 0x10 : out->total_entries; for (s32 i=0; ientries[i].HdlsHandle = in->entries[i].HdlsHandle; + out->entries[i].handle = in->entries[i].handle; _hiddbgConvertHdlsDeviceInfoFromV7(&out->entries[i].device, &in->entries[i].device); _hiddbgConvertHiddbgHdlsStateFromV7(&out->entries[i].state, &in->entries[i].state); } @@ -395,7 +395,7 @@ Result hiddbgReleaseHdlsWorkBuffer(void) { return rc; } -Result hiddbgIsHdlsVirtualDeviceAttached(u64 HdlsHandle, bool *isAttached) { +Result hiddbgIsHdlsVirtualDeviceAttached(HiddbgHdlsHandle handle, bool *out) { Result rc = 0; if (hosversionBefore(7,0,0)) @@ -406,13 +406,13 @@ Result hiddbgIsHdlsVirtualDeviceAttached(u64 HdlsHandle, bool *isAttached) { rc = _hiddbgCmdNoIO(327); if (R_FAILED(rc)) return rc; - if (isAttached) { - *isAttached = false; + if (out) { + *out = false; if (hosversionBefore(9,0,0)) { HiddbgHdlsStateListV7 *stateList = (HiddbgHdlsStateListV7*)(g_hiddbgHdlsTmem.src_addr); for (s32 i=0; itotal_entries; i++) { - if (stateList->entries[i].HdlsHandle == HdlsHandle) { - *isAttached = true; + if (stateList->entries[i].handle.handle == handle.handle) { + *out = true; break; } } @@ -420,8 +420,8 @@ Result hiddbgIsHdlsVirtualDeviceAttached(u64 HdlsHandle, bool *isAttached) { else { HiddbgHdlsStateList *stateList = (HiddbgHdlsStateList*)(g_hiddbgHdlsTmem.src_addr); for (s32 i=0; itotal_entries; i++) { - if (stateList->entries[i].HdlsHandle == HdlsHandle) { - *isAttached = true; + if (stateList->entries[i].handle.handle == handle.handle) { + *out = true; break; } } @@ -503,15 +503,15 @@ Result hiddbgApplyHdlsStateList(const HiddbgHdlsStateList *state) { return _hiddbgCmdNoIO(329); } -static Result _hiddbgAttachHdlsVirtualDeviceV7(u64 *HdlsHandle, const HiddbgHdlsDeviceInfoV7 *info) { - return serviceDispatchInOut(&g_hiddbgSrv, 330, *info, *HdlsHandle); +static Result _hiddbgAttachHdlsVirtualDeviceV7(HiddbgHdlsHandle *handle, const HiddbgHdlsDeviceInfoV7 *info) { + return serviceDispatchInOut(&g_hiddbgSrv, 330, *info, *handle); } -static Result _hiddbgAttachHdlsVirtualDevice(u64 *HdlsHandle, const HiddbgHdlsDeviceInfo *info) { - return serviceDispatchInOut(&g_hiddbgSrv, 330, *info, *HdlsHandle); +static Result _hiddbgAttachHdlsVirtualDevice(HiddbgHdlsHandle *handle, const HiddbgHdlsDeviceInfo *info) { + return serviceDispatchInOut(&g_hiddbgSrv, 330, *info, *handle); } -Result hiddbgAttachHdlsVirtualDevice(u64 *HdlsHandle, const HiddbgHdlsDeviceInfo *info) { +Result hiddbgAttachHdlsVirtualDevice(HiddbgHdlsHandle *handle, const HiddbgHdlsDeviceInfo *info) { if (hosversionBefore(7,0,0)) return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer); @@ -521,42 +521,42 @@ Result hiddbgAttachHdlsVirtualDevice(u64 *HdlsHandle, const HiddbgHdlsDeviceInfo if (hosversionBefore(9,0,0)) { HiddbgHdlsDeviceInfoV7 infov7; _hiddbgConvertHdlsDeviceInfoToV7(&infov7, info); - return _hiddbgAttachHdlsVirtualDeviceV7(HdlsHandle, &infov7); + return _hiddbgAttachHdlsVirtualDeviceV7(handle, &infov7); } else - return _hiddbgAttachHdlsVirtualDevice(HdlsHandle, info); + return _hiddbgAttachHdlsVirtualDevice(handle, info); } -Result hiddbgDetachHdlsVirtualDevice(u64 HdlsHandle) { +Result hiddbgDetachHdlsVirtualDevice(HiddbgHdlsHandle handle) { if (hosversionBefore(7,0,0)) return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer); if (!g_hiddbgHdlsInitialized) return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); - return _hiddbgCmdInU64NoOut(HdlsHandle, 331); + return _hiddbgCmdInU64NoOut(handle.handle, 331); } -static Result _hiddbgSetHdlsStateV7(u64 HdlsHandle, const HiddbgHdlsState *state) { +static Result _hiddbgSetHdlsStateV7(HiddbgHdlsHandle handle, const HiddbgHdlsState *state) { struct { HiddbgHdlsStateV7 state; - u64 handle; - } in = { .handle = HdlsHandle }; + HiddbgHdlsHandle handle; + } in = { .handle = handle }; _hiddbgConvertHiddbgHdlsStateToV7(&in.state, state); return serviceDispatchIn(&g_hiddbgSrv, 332, in); } -static Result _hiddbgSetHdlsState(u64 HdlsHandle, const HiddbgHdlsState *state) { +static Result _hiddbgSetHdlsState(HiddbgHdlsHandle handle, const HiddbgHdlsState *state) { const struct { - u64 handle; + HiddbgHdlsHandle handle; HiddbgHdlsState state; - } in = { HdlsHandle, *state }; + } in = { handle, *state }; return serviceDispatchIn(&g_hiddbgSrv, 332, in); } -Result hiddbgSetHdlsState(u64 HdlsHandle, const HiddbgHdlsState *state) { +Result hiddbgSetHdlsState(HiddbgHdlsHandle handle, const HiddbgHdlsState *state) { if (hosversionBefore(7,0,0)) return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer); @@ -564,8 +564,8 @@ Result hiddbgSetHdlsState(u64 HdlsHandle, const HiddbgHdlsState *state) { return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); if (hosversionBefore(9,0,0)) - return _hiddbgSetHdlsStateV7(HdlsHandle, state); + return _hiddbgSetHdlsStateV7(handle, state); else - return _hiddbgSetHdlsState(HdlsHandle, state); + return _hiddbgSetHdlsState(handle, state); }