From 16e4e9e2b33b5e76d3b095916c46b124ac9897ab Mon Sep 17 00:00:00 2001 From: yellows8 Date: Sat, 12 Oct 2019 12:29:51 -0400 Subject: [PATCH] hid: Updated for new-ipc. Updated param types. Minor improvements and improved docs. --- nx/include/switch/services/hid.h | 177 +++--- nx/source/services/hid.c | 971 ++++++------------------------- 2 files changed, 282 insertions(+), 866 deletions(-) diff --git a/nx/include/switch/services/hid.h b/nx/include/switch/services/hid.h index eea00652..a41cc89c 100644 --- a/nx/include/switch/services/hid.h +++ b/nx/include/switch/services/hid.h @@ -8,12 +8,12 @@ #pragma once #include "../types.h" #include "../kernel/event.h" -#include "../services/sm.h" +#include "../sf/service.h" // Begin enums and output structs -typedef enum -{ +/// HidMouseButton +typedef enum { MOUSE_LEFT = BIT(0), MOUSE_RIGHT = BIT(1), MOUSE_MIDDLE = BIT(2), @@ -21,8 +21,8 @@ typedef enum MOUSE_BACK = BIT(4), } HidMouseButton; -typedef enum -{ +/// HidKeyboardModifier +typedef enum { KBD_MOD_LCTRL = BIT(0), KBD_MOD_LSHIFT = BIT(1), KBD_MOD_LALT = BIT(2), @@ -36,8 +36,8 @@ typedef enum KBD_MOD_NUMLOCK = BIT(10), } HidKeyboardModifier; -typedef enum -{ +/// HidKeyboardScancode +typedef enum { KBD_NONE = 0x00, KBD_ERR_OVF = 0x01, @@ -215,8 +215,7 @@ typedef enum } HidKeyboardScancode; /// HID controller type -typedef enum -{ +typedef enum { TYPE_PROCONTROLLER = BIT(0), TYPE_HANDHELD = BIT(1), TYPE_JOYCON_PAIR = BIT(2), @@ -227,8 +226,8 @@ typedef enum TYPE_SYSTEM = BIT(30), } HidControllerType; -typedef enum -{ +/// HidControllerLayoutType +typedef enum { LAYOUT_PROCONTROLLER = 0, ///< Pro Controller or Hid gamepad. LAYOUT_HANDHELD = 1, ///< Two Joy-Con docked to rails. LAYOUT_SINGLE = 2, ///< Single Joy-Con or pair of Joy-Con, only available in dual-mode with no orientation adjustment. @@ -238,14 +237,13 @@ typedef enum LAYOUT_DEFAULT = 6, ///< Safe default. Single-mode and ::HidJoyHoldType_Horizontal: Joy-Con have buttons/sticks rotated for orientation, where physical Z(L/R) are unavailable and S(L/R) are mapped to L/R (with physical L/R unavailable). } HidControllerLayoutType; -typedef enum -{ +/// HidControllerColorDescription +typedef enum { COLORS_NONEXISTENT = BIT(1), } HidControllerColorDescription; /// HidControllerKeys -typedef enum -{ +typedef enum { KEY_A = BIT(0), ///< A KEY_B = BIT(1), ///< B KEY_X = BIT(2), ///< X @@ -296,22 +294,22 @@ typedef enum KEY_SR = KEY_SR_LEFT | KEY_SR_RIGHT, ///< SR on Left or Right Joy-Con } HidControllerKeys; -typedef enum -{ +/// HidControllerJoystick +typedef enum { JOYSTICK_LEFT = 0, JOYSTICK_RIGHT = 1, JOYSTICK_NUM_STICKS = 2, } HidControllerJoystick; -typedef enum -{ +/// HidControllerConnectionState +typedef enum { CONTROLLER_STATE_CONNECTED = BIT(0), CONTROLLER_STATE_WIRED = BIT(1), } HidControllerConnectionState; -typedef enum -{ +/// HidControllerID +typedef enum { CONTROLLER_PLAYER_1 = 0, CONTROLLER_PLAYER_2 = 1, CONTROLLER_PLAYER_3 = 2, @@ -325,8 +323,8 @@ typedef enum CONTROLLER_P1_AUTO = 10, ///< Not an actual HID-sysmodule ID. Only for hidKeys*()/hidJoystickRead()/hidSixAxisSensorValuesRead()/hidGetControllerType()/hidGetControllerColors()/hidIsControllerConnected(). Automatically uses CONTROLLER_PLAYER_1 when connected, otherwise uses CONTROLLER_HANDHELD. } HidControllerID; -typedef enum -{ +/// JoyHoldType +typedef enum { HidJoyHoldType_Default = 0, ///< Default / Joy-Con held vertically. HidJoyHoldType_Horizontal = 1, ///< Joy-Con held horizontally with HID state orientation adjustment, see \ref HidControllerLayoutType. } HidJoyHoldType; @@ -369,16 +367,15 @@ typedef enum { } HidDeviceType; /// NpadInterfaceType -typedef enum -{ +typedef enum { NpadInterfaceType_Bluetooth = 1, ///< Bluetooth. NpadInterfaceType_Rail = 2, ///< Rail. NpadInterfaceType_USB = 3, ///< USB. NpadInterfaceType_Unknown4 = 4, ///< Unknown. } HidNpadInterfaceType; -typedef struct touchPosition -{ +/// touchPosition +typedef struct touchPosition { u32 id; u32 px; u32 py; @@ -387,14 +384,14 @@ typedef struct touchPosition u32 angle; } touchPosition; -typedef struct JoystickPosition -{ +/// JoystickPosition +typedef struct JoystickPosition { s32 dx; s32 dy; } JoystickPosition; -typedef struct MousePosition -{ +/// MousePosition +typedef struct MousePosition { s32 x; s32 y; s32 velocityX; @@ -403,15 +400,15 @@ typedef struct MousePosition s32 scrollVelocityY; } MousePosition; -typedef struct HidVector -{ +/// HidVector +typedef struct HidVector { float x; float y; float z; } HidVector; -typedef struct SixAxisSensorValues -{ +/// SixAxisSensorValues +typedef struct SixAxisSensorValues { HidVector accelerometer; HidVector gyroscope; HidVector unk; @@ -425,8 +422,8 @@ typedef struct SixAxisSensorValues // Begin HidTouchScreen -typedef struct HidTouchScreenHeader -{ +/// HidTouchScreenHeader +typedef struct HidTouchScreenHeader { u64 timestampTicks; u64 numEntries; u64 latestEntry; @@ -434,14 +431,14 @@ typedef struct HidTouchScreenHeader u64 timestamp; } HidTouchScreenHeader; -typedef struct HidTouchScreenEntryHeader -{ +/// HidTouchScreenEntryHeader +typedef struct HidTouchScreenEntryHeader { u64 timestamp; u64 numTouches; } HidTouchScreenEntryHeader; -typedef struct HidTouchScreenEntryTouch -{ +/// HidTouchScreenEntryTouch +typedef struct HidTouchScreenEntryTouch { u64 timestamp; u32 padding; u32 touchIndex; @@ -453,15 +450,15 @@ typedef struct HidTouchScreenEntryTouch u32 padding_2; } HidTouchScreenEntryTouch; -typedef struct HidTouchScreenEntry -{ +/// HidTouchScreenEntry +typedef struct HidTouchScreenEntry { HidTouchScreenEntryHeader header; HidTouchScreenEntryTouch touches[16]; u64 unk; } HidTouchScreenEntry; -typedef struct HidTouchScreen -{ +/// HidTouchScreen +typedef struct HidTouchScreen { HidTouchScreenHeader header; HidTouchScreenEntry entries[17]; u8 padding[0x3c0]; @@ -471,24 +468,24 @@ typedef struct HidTouchScreen // Begin HidMouse -typedef struct HidMouseHeader -{ +/// HidMouseHeader +typedef struct HidMouseHeader { u64 timestampTicks; u64 numEntries; u64 latestEntry; u64 maxEntryIndex; } HidMouseHeader; -typedef struct HidMouseEntry -{ +/// HidMouseEntry +typedef struct HidMouseEntry { u64 timestamp; u64 timestamp_2; MousePosition position; u64 buttons; } HidMouseEntry; -typedef struct HidMouse -{ +/// HidMouse +typedef struct HidMouse { HidMouseHeader header; HidMouseEntry entries[17]; u8 padding[0xB0]; @@ -498,24 +495,24 @@ typedef struct HidMouse // Begin HidKeyboard -typedef struct HidKeyboardHeader -{ +/// HidKeyboardHeader +typedef struct HidKeyboardHeader { u64 timestampTicks; u64 numEntries; u64 latestEntry; u64 maxEntryIndex; } HidKeyboardHeader; -typedef struct HidKeyboardEntry -{ +/// HidKeyboardEntry +typedef struct HidKeyboardEntry { u64 timestamp; u64 timestamp_2; u64 modifier; u32 keys[8]; } HidKeyboardEntry; -typedef struct HidKeyboard -{ +/// HidKeyboard +typedef struct HidKeyboard { HidKeyboardHeader header; HidKeyboardEntry entries[17]; u8 padding[0x28]; @@ -525,16 +522,16 @@ typedef struct HidKeyboard // Begin HidController -typedef struct HidControllerMAC -{ +/// HidControllerMAC +typedef struct HidControllerMAC { u64 timestamp; u8 mac[0x8]; u64 unk; u64 timestamp_2; } HidControllerMAC; -typedef struct HidControllerHeader -{ +/// HidControllerHeader +typedef struct HidControllerHeader { u32 type; u32 isHalf; u32 singleColorsDescriptor; @@ -562,16 +559,16 @@ typedef struct HidControllerColors u32 rightColorButtons; ///< RGBA Right Buttons Color } HidControllerColors; -typedef struct HidControllerLayoutHeader -{ +/// HidControllerLayoutHeader +typedef struct HidControllerLayoutHeader { u64 timestampTicks; u64 numEntries; u64 latestEntry; u64 maxEntryIndex; } HidControllerLayoutHeader; -typedef struct HidControllerInputEntry -{ +/// HidControllerInputEntry +typedef struct HidControllerInputEntry { u64 timestamp; u64 timestamp_2; u64 buttons; @@ -579,22 +576,22 @@ typedef struct HidControllerInputEntry u64 connectionState; } HidControllerInputEntry; -typedef struct HidControllerLayout -{ +/// HidControllerLayout +typedef struct HidControllerLayout { HidControllerLayoutHeader header; HidControllerInputEntry entries[17]; } HidControllerLayout; -typedef struct HidControllerSixAxisHeader -{ +/// HidControllerSixAxisHeader +typedef struct HidControllerSixAxisHeader { u64 timestamp; u64 numEntries; u64 latestEntry; u64 maxEntryIndex; } HidControllerSixAxisHeader; -typedef struct HidControllerSixAxisEntry -{ +/// HidControllerSixAxisEntry +typedef struct HidControllerSixAxisEntry { u64 timestamp; u64 unk_1; u64 timestamp_2; @@ -602,8 +599,8 @@ typedef struct HidControllerSixAxisEntry u64 unk_3; } HidControllerSixAxisEntry; -typedef struct HidControllerSixAxisLayout -{ +/// HidControllerSixAxisLayout +typedef struct HidControllerSixAxisLayout { HidControllerSixAxisHeader header; HidControllerSixAxisEntry entries[17]; } HidControllerSixAxisLayout; @@ -629,6 +626,7 @@ typedef struct { u32 unintendedHomeButtonInputProtectionDisabled : 1; } HidFlags; +/// HidControllerMisc typedef struct { u32 deviceType; u32 pad; @@ -639,14 +637,15 @@ typedef struct { HidControllerMAC macRight; } HidControllerMisc; +/// HidPowerInfo typedef struct { bool powerConnected; bool isCharging; u32 batteryCharge; ///< Battery charge, always 0-4. } HidPowerInfo; -typedef struct HidController -{ +/// HidController +typedef struct HidController { HidControllerHeader header; HidControllerLayout layouts[7]; HidControllerSixAxisLayout sixaxis[6]; @@ -656,8 +655,8 @@ typedef struct HidController // End HidController -typedef struct HidSharedMemory -{ +/// HidSharedMemory +typedef struct HidSharedMemory { u8 header[0x400]; HidTouchScreen touchscreen; HidMouse mouse; @@ -675,14 +674,14 @@ typedef struct HidSharedMemory u8 unkSection9[0x4600]; } HidSharedMemory; -typedef struct HidVibrationDeviceInfo -{ +/// HidVibrationDeviceInfo +typedef struct HidVibrationDeviceInfo { u32 unk_x0; u32 unk_x4; ///< 0x1 for left-joycon, 0x2 for right-joycon. } HidVibrationDeviceInfo; -typedef struct HidVibrationValue -{ +/// HidVibrationValue +typedef struct HidVibrationValue { float amp_low; ///< Low Band amplitude. 1.0f: Max amplitude. float freq_low; ///< Low Band frequency in Hz. float amp_high; ///< High Band amplitude. 1.0f: Max amplitude. @@ -701,12 +700,18 @@ static inline HidControllerID hidControllerIDFromOfficial(u32 id) { return CONTROLLER_UNKNOWN; } -/// Initializes hid, called automatically during app startup. +/// Initialize hid. Called automatically during app startup. Result hidInitialize(void); + +/// Exit hid. Called automatically during app exit. void hidExit(void); + void hidReset(void); +/// Gets the Service object for the actual hid service session. Service* hidGetServiceSession(void); + +/// Gets the address of the SharedMemory. void* hidGetSharedmemAddr(void); void hidSetControllerLayout(HidControllerID id, HidControllerLayoutType layoutType); @@ -768,7 +773,7 @@ Result hidSetSupportedNpadIdType(HidControllerID *buf, size_t count); /// Gets an event with the specified autoclear for the input controller. /// The user *must* close the event when finished with it / before the app exits. /// This is signaled when the \ref hidGetControllerType output is updated for the controller. -Result hidAcquireNpadStyleSetUpdateEventHandle(HidControllerID id, Event* event, bool autoclear); +Result hidAcquireNpadStyleSetUpdateEventHandle(HidControllerID id, Event* out_event, bool autoclear); /// Sets the hold-type, see \ref HidJoyHoldType. Result hidSetNpadJoyHoldType(HidJoyHoldType type); @@ -789,7 +794,7 @@ Result hidSetNpadJoyAssignmentModeDual(HidControllerID id); /// If successful, the id of the resulting dual controller is set to id0. Result hidMergeSingleJoyAsDualJoy(HidControllerID id0, HidControllerID id1); -Result hidInitializeVibrationDevices(u32 *VibrationDeviceHandles, size_t total_handles, HidControllerID id, HidControllerType type); +Result hidInitializeVibrationDevices(u32 *VibrationDeviceHandles, s32 total_handles, HidControllerID id, HidControllerType type); /// Gets HidVibrationDeviceInfo for the specified VibrationDeviceHandle. Result hidGetVibrationDeviceInfo(u32 *VibrationDeviceHandle, HidVibrationDeviceInfo *VibrationDeviceInfo); @@ -807,10 +812,10 @@ Result hidPermitVibration(bool flag); Result hidIsVibrationPermitted(bool *flag); /// Send VibrationValues[index] to VibrationDeviceHandles[index], where count is the number of entries in the VibrationDeviceHandles/VibrationValues arrays. -Result hidSendVibrationValues(u32 *VibrationDeviceHandles, HidVibrationValue *VibrationValues, size_t count); +Result hidSendVibrationValues(u32 *VibrationDeviceHandles, HidVibrationValue *VibrationValues, s32 count); /// Gets SixAxisSensorHandles. total_handles==2 can only be used with TYPE_JOYCON_PAIR. -Result hidGetSixAxisSensorHandles(u32 *SixAxisSensorHandles, size_t total_handles, HidControllerID id, HidControllerType type); +Result hidGetSixAxisSensorHandles(u32 *SixAxisSensorHandles, s32 total_handles, HidControllerID id, HidControllerType type); /// Starts the SixAxisSensor for the specified handle. Result hidStartSixAxisSensor(u32 SixAxisSensorHandle); diff --git a/nx/source/services/hid.c b/nx/source/services/hid.c index 25f4581a..406d3cc7 100644 --- a/nx/source/services/hid.c +++ b/nx/source/services/hid.c @@ -1,21 +1,17 @@ +#define NX_SERVICE_ASSUME_NON_DOMAIN +#include "service_guard.h" #include #include #include -#include "types.h" -#include "result.h" -#include "arm/atomics.h" -#include "kernel/ipc.h" #include "kernel/shmem.h" #include "kernel/rwlock.h" #include "kernel/event.h" #include "services/applet.h" #include "services/hid.h" -#include "services/sm.h" #include "runtime/hosversion.h" static Service g_hidSrv; static Service g_hidIAppletResource; -static u64 g_refCnt; static SharedMemory g_hidSharedmem; static HidTouchScreenEntry g_touchEntry; @@ -52,8 +48,9 @@ static Result _hidDeactivateNpad(void); static Result _hidSetDualModeAll(void); -Result hidInitialize(void) -{ +NX_GENERATE_SERVICE_GUARD(hid); + +Result _hidInitialize(void) { HidControllerID idbuf[9] = { CONTROLLER_PLAYER_1, CONTROLLER_PLAYER_2, @@ -65,12 +62,7 @@ Result hidInitialize(void) CONTROLLER_PLAYER_8, CONTROLLER_HANDHELD}; - atomicIncrement64(&g_refCnt); - - if (serviceIsActive(&g_hidSrv)) - return 0; - - Result rc; + Result rc=0; Handle sharedmem_handle; // If this failed (for example because we're a sysmodule) AppletResourceUserId stays zero @@ -86,8 +78,7 @@ Result hidInitialize(void) if (R_SUCCEEDED(rc)) rc = _hidGetSharedMemoryHandle(&g_hidIAppletResource, &sharedmem_handle); - if (R_SUCCEEDED(rc)) - { + if (R_SUCCEEDED(rc)) { shmemLoadRemote(&g_hidSharedmem, sharedmem_handle, 0x40000, Perm_R); rc = shmemMap(&g_hidSharedmem); @@ -108,33 +99,25 @@ Result hidInitialize(void) if (R_SUCCEEDED(rc)) rc = hidSetNpadJoyHoldType(HidJoyHoldType_Default); - if (R_FAILED(rc)) - hidExit(); - hidReset(); return rc; } -void hidExit(void) -{ - if (atomicDecrement64(&g_refCnt) == 0) - { - hidFinalizeSevenSixAxisSensor(); +void _hidCleanup(void) { + hidFinalizeSevenSixAxisSensor(); - hidSetNpadJoyHoldType(HidJoyHoldType_Default); + hidSetNpadJoyHoldType(HidJoyHoldType_Default); - _hidSetDualModeAll(); + _hidSetDualModeAll(); - _hidDeactivateNpad(); + _hidDeactivateNpad(); - serviceClose(&g_hidIAppletResource); - serviceClose(&g_hidSrv); - shmemClose(&g_hidSharedmem); - } + serviceClose(&g_hidIAppletResource); + serviceClose(&g_hidSrv); + shmemClose(&g_hidSharedmem); } -void hidReset(void) -{ +void hidReset(void) { rwlockWriteLock(&g_hidLock); // Reset internal state @@ -649,261 +632,126 @@ static Result _hidSetDualModeAll(void) { return rc; } -static Result _hidCreateAppletResource(Service* srv, Service* srv_out, u64 AppletResourceUserId) { - IpcCommand c; - ipcInitialize(&c); +static Result _hidCmdGetHandle(Service* srv, Handle* handle_out, u32 cmd_id) { + return serviceDispatch(srv, cmd_id, + .out_handle_attrs = { SfOutHandleAttr_HipcCopy }, + .out_handles = handle_out, + ); +} - struct { - u64 magic; - u64 cmd_id; +static Result _hidCmdGetSession(Service* srv_out, u32 cmd_id) { + return serviceDispatch(&g_hidSrv, cmd_id, + .out_num_objects = 1, + .out_objects = srv_out, + ); +} + +static Result _hidCmdWithNoInput(u32 cmd_id) { + Result rc=0; + u64 AppletResourceUserId; + + rc = appletGetAppletResourceUserId(&AppletResourceUserId); + if (R_FAILED(rc)) + AppletResourceUserId = 0; + + return serviceDispatchIn(&g_hidSrv, cmd_id, AppletResourceUserId, + .in_send_pid = true, + ); +} + +static Result _hidCmdInU8NoOut(u8 inval, u32 cmd_id) { + return serviceDispatchIn(&g_hidSrv, cmd_id, inval); +} + +static Result _hidCmdInBoolNoOut(bool inval, u32 cmd_id) { + return _hidCmdInU8NoOut(inval!=0, cmd_id); +} + +static Result _hidCmdInU32NoOut(Service* srv, u32 inval, u32 cmd_id) { + return serviceDispatchIn(srv, cmd_id, inval); +} + +static Result _hidCmdWithInputU32(u32 inval, u32 cmd_id) { + Result rc; + u64 AppletResourceUserId; + + rc = appletGetAppletResourceUserId(&AppletResourceUserId); + if (R_FAILED(rc)) + AppletResourceUserId = 0; + + const struct { + u32 inval; u64 AppletResourceUserId; - } *raw; + } in = { inval, AppletResourceUserId }; - ipcSendPid(&c); + return serviceDispatchIn(&g_hidSrv, cmd_id, in, + .in_send_pid = true, + ); +} - raw = ipcPrepareHeader(&c, sizeof(*raw)); +static Result _hidCmdWithInputU64(u64 inval, u32 cmd_id) { + Result rc; + u64 AppletResourceUserId; - raw->magic = SFCI_MAGIC; - raw->cmd_id = 0; - raw->AppletResourceUserId = AppletResourceUserId; + rc = appletGetAppletResourceUserId(&AppletResourceUserId); + if (R_FAILED(rc)) + AppletResourceUserId = 0; - Result rc = serviceIpcDispatch(srv); + const struct { + u64 AppletResourceUserId; + u64 inval; + } in = { AppletResourceUserId, inval }; - if (R_SUCCEEDED(rc)) { - IpcParsedCommand r; - ipcParse(&r); + return serviceDispatchIn(&g_hidSrv, cmd_id, in, + .in_send_pid = true, + ); +} - struct { - u64 magic; - u64 result; - } *resp = r.Raw; +static Result _hidCmdOutU32(u32 *out, u32 cmd_id) { + Result rc; + u64 AppletResourceUserId; - rc = resp->result; + rc = appletGetAppletResourceUserId(&AppletResourceUserId); + if (R_FAILED(rc)) + AppletResourceUserId = 0; - if (R_SUCCEEDED(rc)) { - serviceCreate(srv_out, r.Handles[0]); - } - } + return serviceDispatchInOut(&g_hidSrv, cmd_id, AppletResourceUserId, *out, + .in_send_pid = true, + ); +} +static Result _hidCmdNoInOutU8(u8 *out, u32 cmd_id) { + return serviceDispatchOut(&g_hidSrv, cmd_id, *out); +} + +static Result _hidCmdNoInOutBool(bool *out, u32 cmd_id) { + u8 tmp=0; + Result rc = _hidCmdNoInOutU8(&tmp, cmd_id); + if (R_SUCCEEDED(rc) && out) *out = tmp!=0; return rc; } +static Result _hidCreateAppletResource(Service* srv, Service* srv_out, u64 AppletResourceUserId) { + return serviceDispatchIn(srv, 0, AppletResourceUserId, + .in_send_pid = true, + .out_num_objects = 1, + .out_objects = srv_out, + ); +} + static Result _hidGetSharedMemoryHandle(Service* srv, Handle* handle_out) { - IpcCommand c; - ipcInitialize(&c); - - struct { - u64 magic; - u64 cmd_id; - } *raw; - - raw = ipcPrepareHeader(&c, sizeof(*raw)); - - raw->magic = SFCI_MAGIC; - raw->cmd_id = 0; - - Result rc = serviceIpcDispatch(srv); - - if (R_SUCCEEDED(rc)) { - IpcParsedCommand r; - ipcParse(&r); - - struct { - u64 magic; - u64 result; - } *resp = r.Raw; - - rc = resp->result; - - if (R_SUCCEEDED(rc)) { - *handle_out = r.Handles[0]; - } - } - - return rc; -} - -static Result _hidCmdWithInputU32(u64 cmd_id, u32 inputval) { - Result rc; - u64 AppletResourceUserId; - - rc = appletGetAppletResourceUserId(&AppletResourceUserId); - if (R_FAILED(rc)) - AppletResourceUserId = 0; - - IpcCommand c; - ipcInitialize(&c); - - struct { - u64 magic; - u64 cmd_id; - u32 val; - u64 AppletResourceUserId; - } *raw; - - ipcSendPid(&c); - - raw = ipcPrepareHeader(&c, sizeof(*raw)); - - raw->magic = SFCI_MAGIC; - raw->cmd_id = cmd_id; - raw->val = inputval; - raw->AppletResourceUserId = AppletResourceUserId; - - rc = serviceIpcDispatch(&g_hidSrv); - - if (R_SUCCEEDED(rc)) { - IpcParsedCommand r; - ipcParse(&r); - - struct { - u64 magic; - u64 result; - } *resp = r.Raw; - - rc = resp->result; - } - - return rc; -} - -static Result _hidCmdOutU32(u64 cmd_id, u32 *out) { - Result rc; - u64 AppletResourceUserId; - - rc = appletGetAppletResourceUserId(&AppletResourceUserId); - if (R_FAILED(rc)) - AppletResourceUserId = 0; - - IpcCommand c; - ipcInitialize(&c); - - struct { - u64 magic; - u64 cmd_id; - u64 AppletResourceUserId; - } *raw; - - ipcSendPid(&c); - - raw = ipcPrepareHeader(&c, sizeof(*raw)); - - raw->magic = SFCI_MAGIC; - raw->cmd_id = cmd_id; - raw->AppletResourceUserId = AppletResourceUserId; - - rc = serviceIpcDispatch(&g_hidSrv); - - if (R_SUCCEEDED(rc)) { - IpcParsedCommand r; - ipcParse(&r); - - struct { - u64 magic; - u64 result; - u32 out; - } *resp = r.Raw; - - rc = resp->result; - - if (R_SUCCEEDED(rc) && out) *out = resp->out; - } - - return rc; -} - -static Result _hidCmdWithInputU64(u64 cmd_id, u64 inputval) { - Result rc; - u64 AppletResourceUserId; - - rc = appletGetAppletResourceUserId(&AppletResourceUserId); - if (R_FAILED(rc)) - AppletResourceUserId = 0; - - IpcCommand c; - ipcInitialize(&c); - - struct { - u64 magic; - u64 cmd_id; - u64 AppletResourceUserId; - u64 val; - } *raw; - - ipcSendPid(&c); - - raw = ipcPrepareHeader(&c, sizeof(*raw)); - - raw->magic = SFCI_MAGIC; - raw->cmd_id = cmd_id; - raw->val = inputval; - raw->AppletResourceUserId = AppletResourceUserId; - - rc = serviceIpcDispatch(&g_hidSrv); - - if (R_SUCCEEDED(rc)) { - IpcParsedCommand r; - ipcParse(&r); - - struct { - u64 magic; - u64 result; - } *resp = r.Raw; - - rc = resp->result; - } - - return rc; -} - -static Result _hidCmdWithNoInput(u64 cmd_id) { - Result rc; - u64 AppletResourceUserId; - - rc = appletGetAppletResourceUserId(&AppletResourceUserId); - if (R_FAILED(rc)) - AppletResourceUserId = 0; - - IpcCommand c; - ipcInitialize(&c); - - struct { - u64 magic; - u64 cmd_id; - u64 AppletResourceUserId; - } *raw; - - ipcSendPid(&c); - - raw = ipcPrepareHeader(&c, sizeof(*raw)); - - raw->magic = SFCI_MAGIC; - raw->cmd_id = cmd_id; - raw->AppletResourceUserId = AppletResourceUserId; - - rc = serviceIpcDispatch(&g_hidSrv); - - if (R_SUCCEEDED(rc)) { - IpcParsedCommand r; - ipcParse(&r); - - struct { - u64 magic; - u64 result; - } *resp = r.Raw; - - rc = resp->result; - } - - return rc; + return _hidCmdGetHandle(srv, handle_out, 0); } Result hidSetSupportedNpadStyleSet(HidControllerType type) { - return _hidCmdWithInputU32(100, type); + return _hidCmdWithInputU32(type, 100); } Result hidGetSupportedNpadStyleSet(HidControllerType *type) { - return _hidCmdOutU32(101, type); + u32 tmp=0; + Result rc = _hidCmdOutU32(&tmp, 101); + if (R_SUCCEEDED(rc) && type) *type = tmp; + return rc; } Result hidSetSupportedNpadIdType(HidControllerID *buf, size_t count) { @@ -933,40 +781,11 @@ Result hidSetSupportedNpadIdType(HidControllerID *buf, size_t count) { tmpbuf[i] = tmpval; } - IpcCommand c; - ipcInitialize(&c); - - struct { - u64 magic; - u64 cmd_id; - u64 AppletResourceUserId; - } *raw; - - ipcSendPid(&c); - - ipcAddSendStatic(&c, tmpbuf, sizeof(u32)*count, 0); - - raw = ipcPrepareHeader(&c, sizeof(*raw)); - - raw->magic = SFCI_MAGIC; - raw->cmd_id = 102; - raw->AppletResourceUserId = AppletResourceUserId; - - rc = serviceIpcDispatch(&g_hidSrv); - - if (R_SUCCEEDED(rc)) { - IpcParsedCommand r; - ipcParse(&r); - - struct { - u64 magic; - u64 result; - } *resp = r.Raw; - - rc = resp->result; - } - - return rc; + return serviceDispatchIn(&g_hidSrv, 102, AppletResourceUserId, + .buffer_attrs = { SfBufferAttr_HipcPointer | SfBufferAttr_In }, + .buffers = { { tmpbuf, count*sizeof(u32) } }, + .in_send_pid = true, + ); } static Result _hidActivateNpad(void) { @@ -977,66 +796,40 @@ static Result _hidDeactivateNpad(void) { return _hidCmdWithNoInput(104); } -Result hidAcquireNpadStyleSetUpdateEventHandle(HidControllerID id, Event* event, bool autoclear) { +Result hidAcquireNpadStyleSetUpdateEventHandle(HidControllerID id, Event* out_event, bool autoclear) { Result rc; + Handle tmp_handle = INVALID_HANDLE; u64 AppletResourceUserId; rc = appletGetAppletResourceUserId(&AppletResourceUserId); if (R_FAILED(rc)) AppletResourceUserId = 0; - IpcCommand c; - ipcInitialize(&c); - - struct { - u64 magic; - u64 cmd_id; + const struct { u32 id; u64 AppletResourceUserId; - u64 event_ptr; - } *raw; - - ipcSendPid(&c); - - raw = ipcPrepareHeader(&c, sizeof(*raw)); - - raw->magic = SFCI_MAGIC; - raw->cmd_id = 106; - raw->id = hidControllerIDToOfficial(id); - raw->AppletResourceUserId = AppletResourceUserId; - raw->event_ptr = 0;//Official sw sets this to a ptr, which the sysmodule doesn't seem to use. - - rc = serviceIpcDispatch(&g_hidSrv); - - if (R_SUCCEEDED(rc)) { - IpcParsedCommand r; - ipcParse(&r); - - struct { - u64 magic; - u64 result; - } *resp = r.Raw; - - rc = resp->result; - - if (R_SUCCEEDED(rc)) { - eventLoadRemote(event, r.Handles[0], autoclear); - } - } + u64 event_ptr; // Official sw sets this to a ptr, which the sysmodule doesn't seem to use. + } in = { hidControllerIDToOfficial(id), AppletResourceUserId, 0 }; + rc = serviceDispatchIn(&g_hidSrv, 106, in, + .in_send_pid = true, + .out_handle_attrs = { SfOutHandleAttr_HipcCopy }, + .out_handles = &tmp_handle, + ); + if (R_SUCCEEDED(rc)) eventLoadRemote(out_event, tmp_handle, autoclear); return rc; } Result hidSetNpadJoyHoldType(HidJoyHoldType type) { - return _hidCmdWithInputU64(120, type); + return _hidCmdWithInputU64(type, 120); } Result hidSetNpadJoyAssignmentModeSingleByDefault(HidControllerID id) { - return _hidCmdWithInputU32(122, hidControllerIDToOfficial(id)); + return _hidCmdWithInputU32(hidControllerIDToOfficial(id), 122); } Result hidSetNpadJoyAssignmentModeDual(HidControllerID id) { - return _hidCmdWithInputU32(124, hidControllerIDToOfficial(id)); + return _hidCmdWithInputU32(hidControllerIDToOfficial(id), 124); } Result hidMergeSingleJoyAsDualJoy(HidControllerID id0, HidControllerID id1) { @@ -1047,147 +840,26 @@ Result hidMergeSingleJoyAsDualJoy(HidControllerID id0, HidControllerID id1) { if (R_FAILED(rc)) AppletResourceUserId = 0; - IpcCommand c; - ipcInitialize(&c); - - struct { - u64 magic; - u64 cmd_id; + const struct { u32 id0, id1; u64 AppletResourceUserId; - } *raw; + } in = { hidControllerIDToOfficial(id0), hidControllerIDToOfficial(id1), AppletResourceUserId }; - ipcSendPid(&c); - - raw = ipcPrepareHeader(&c, sizeof(*raw)); - - raw->magic = SFCI_MAGIC; - raw->cmd_id = 125; - raw->id0 = hidControllerIDToOfficial(id0); - raw->id1 = hidControllerIDToOfficial(id1); - raw->AppletResourceUserId = AppletResourceUserId; - - rc = serviceIpcDispatch(&g_hidSrv); - - if (R_SUCCEEDED(rc)) { - IpcParsedCommand r; - ipcParse(&r); - - struct { - u64 magic; - u64 result; - } *resp = r.Raw; - - rc = resp->result; - } - - return rc; + return serviceDispatchIn(&g_hidSrv, 125, in, + .in_send_pid = true, + ); } static Result _hidCreateActiveVibrationDeviceList(Service* srv_out) { - IpcCommand c; - ipcInitialize(&c); - - struct { - u64 magic; - u64 cmd_id; - } *raw; - - raw = ipcPrepareHeader(&c, sizeof(*raw)); - - raw->magic = SFCI_MAGIC; - raw->cmd_id = 203; - - Result rc = serviceIpcDispatch(&g_hidSrv); - - if (R_SUCCEEDED(rc)) { - IpcParsedCommand r; - ipcParse(&r); - - struct { - u64 magic; - u64 result; - } *resp = r.Raw; - - rc = resp->result; - - if (R_SUCCEEDED(rc)) { - serviceCreate(srv_out, r.Handles[0]); - } - } - - return rc; + return _hidCmdGetSession(srv_out, 203); } static Result _hidActivateVibrationDevice(Service* srv, u32 VibrationDeviceHandle) { - IpcCommand c; - ipcInitialize(&c); - - struct { - u64 magic; - u64 cmd_id; - u32 VibrationDeviceHandle; - } *raw; - - raw = ipcPrepareHeader(&c, sizeof(*raw)); - - raw->magic = SFCI_MAGIC; - raw->cmd_id = 0; - raw->VibrationDeviceHandle = VibrationDeviceHandle; - - Result rc = serviceIpcDispatch(srv); - - if (R_SUCCEEDED(rc)) { - IpcParsedCommand r; - ipcParse(&r); - - struct { - u64 magic; - u64 result; - } *resp = r.Raw; - - rc = resp->result; - } - - return rc; + return _hidCmdInU32NoOut(srv, VibrationDeviceHandle, 0); } Result hidGetVibrationDeviceInfo(u32 *VibrationDeviceHandle, HidVibrationDeviceInfo *VibrationDeviceInfo) { - Result rc; - - IpcCommand c; - ipcInitialize(&c); - - struct { - u64 magic; - u64 cmd_id; - u32 VibrationDeviceHandle; - } *raw; - - raw = ipcPrepareHeader(&c, sizeof(*raw)); - - raw->magic = SFCI_MAGIC; - raw->cmd_id = 200; - raw->VibrationDeviceHandle = *VibrationDeviceHandle; - - rc = serviceIpcDispatch(&g_hidSrv); - - if (R_SUCCEEDED(rc)) { - IpcParsedCommand r; - ipcParse(&r); - - struct { - u64 magic; - u64 result; - HidVibrationDeviceInfo VibrationDeviceInfo; - } *resp = r.Raw; - - rc = resp->result; - - if (R_SUCCEEDED(rc) && VibrationDeviceInfo) memcpy(VibrationDeviceInfo, &resp->VibrationDeviceInfo, sizeof(HidVibrationDeviceInfo)); - } - - return rc; + return serviceDispatchInOut(&g_hidSrv, 200, *VibrationDeviceHandle, *VibrationDeviceInfo); } Result hidSendVibrationValue(u32 *VibrationDeviceHandle, HidVibrationValue *VibrationValue) { @@ -1198,42 +870,15 @@ Result hidSendVibrationValue(u32 *VibrationDeviceHandle, HidVibrationValue *Vibr if (R_FAILED(rc)) AppletResourceUserId = 0; - IpcCommand c; - ipcInitialize(&c); - - struct { - u64 magic; - u64 cmd_id; + const struct { u32 VibrationDeviceHandle; HidVibrationValue VibrationValue; u64 AppletResourceUserId; - } *raw; + } in = { *VibrationDeviceHandle, *VibrationValue, AppletResourceUserId }; - ipcSendPid(&c); - - raw = ipcPrepareHeader(&c, sizeof(*raw)); - - raw->magic = SFCI_MAGIC; - raw->cmd_id = 201; - raw->VibrationDeviceHandle = *VibrationDeviceHandle; - raw->AppletResourceUserId = AppletResourceUserId; - memcpy(&raw->VibrationValue, VibrationValue, sizeof(HidVibrationValue)); - - rc = serviceIpcDispatch(&g_hidSrv); - - if (R_SUCCEEDED(rc)) { - IpcParsedCommand r; - ipcParse(&r); - - struct { - u64 magic; - u64 result; - } *resp = r.Raw; - - rc = resp->result; - } - - return rc; + return serviceDispatchIn(&g_hidSrv, 201, in, + .in_send_pid = true, + ); } Result hidGetActualVibrationValue(u32 *VibrationDeviceHandle, HidVibrationValue *VibrationValue) { @@ -1244,114 +889,25 @@ Result hidGetActualVibrationValue(u32 *VibrationDeviceHandle, HidVibrationValue if (R_FAILED(rc)) AppletResourceUserId = 0; - IpcCommand c; - ipcInitialize(&c); - - struct { - u64 magic; - u64 cmd_id; + const struct { u32 VibrationDeviceHandle; u64 AppletResourceUserId; - } *raw; + } in = { *VibrationDeviceHandle, AppletResourceUserId }; - ipcSendPid(&c); - - raw = ipcPrepareHeader(&c, sizeof(*raw)); - - raw->magic = SFCI_MAGIC; - raw->cmd_id = 202; - raw->VibrationDeviceHandle = *VibrationDeviceHandle; - raw->AppletResourceUserId = AppletResourceUserId; - - rc = serviceIpcDispatch(&g_hidSrv); - - if (R_SUCCEEDED(rc)) { - IpcParsedCommand r; - ipcParse(&r); - - struct { - u64 magic; - u64 result; - HidVibrationValue VibrationValue; - } *resp = r.Raw; - - rc = resp->result; - - if (R_SUCCEEDED(rc) && VibrationValue) memcpy(VibrationValue, &resp->VibrationValue, sizeof(HidVibrationValue)); - } - - return rc; + return serviceDispatchInOut(&g_hidSrv, 202, in, *VibrationValue, + .in_send_pid = true, + ); } Result hidPermitVibration(bool flag) { - IpcCommand c; - ipcInitialize(&c); - - struct { - u64 magic; - u64 cmd_id; - u8 flag; - } *raw; - - raw = ipcPrepareHeader(&c, sizeof(*raw)); - - raw->magic = SFCI_MAGIC; - raw->cmd_id = 204; - raw->flag = !!flag; - - Result rc = serviceIpcDispatch(&g_hidSrv); - - if (R_SUCCEEDED(rc)) { - IpcParsedCommand r; - ipcParse(&r); - - struct { - u64 magic; - u64 result; - } *resp = r.Raw; - - rc = resp->result; - } - - return rc; + return _hidCmdInBoolNoOut(flag, 204); } Result hidIsVibrationPermitted(bool *flag) { - IpcCommand c; - ipcInitialize(&c); - - struct { - u64 magic; - u64 cmd_id; - } *raw; - - raw = ipcPrepareHeader(&c, sizeof(*raw)); - - raw->magic = SFCI_MAGIC; - raw->cmd_id = 205; - - Result rc = serviceIpcDispatch(&g_hidSrv); - - if (R_SUCCEEDED(rc)) { - IpcParsedCommand r; - ipcParse(&r); - - struct { - u64 magic; - u64 result; - u8 flag; - } *resp = r.Raw; - - rc = resp->result; - - if (R_SUCCEEDED(rc) && flag) - *flag = resp->flag & 1; - } - - return rc; + return _hidCmdNoInOutBool(flag, 205); } -Result hidSendVibrationValues(u32 *VibrationDeviceHandles, HidVibrationValue *VibrationValues, size_t count) { +Result hidSendVibrationValues(u32 *VibrationDeviceHandles, HidVibrationValue *VibrationValues, s32 count) { Result rc; u64 AppletResourceUserId; @@ -1359,47 +915,24 @@ Result hidSendVibrationValues(u32 *VibrationDeviceHandles, HidVibrationValue *Vi if (R_FAILED(rc)) AppletResourceUserId = 0; - IpcCommand c; - ipcInitialize(&c); - - struct { - u64 magic; - u64 cmd_id; - u64 AppletResourceUserId; - } *raw; - - ipcAddSendStatic(&c, VibrationDeviceHandles, sizeof(u32)*count, 0); - ipcAddSendStatic(&c, VibrationValues, sizeof(HidVibrationValue)*count, 0); - - raw = ipcPrepareHeader(&c, sizeof(*raw)); - - raw->magic = SFCI_MAGIC; - raw->cmd_id = 206; - raw->AppletResourceUserId = AppletResourceUserId; - - rc = serviceIpcDispatch(&g_hidSrv); - - if (R_SUCCEEDED(rc)) { - IpcParsedCommand r; - ipcParse(&r); - - struct { - u64 magic; - u64 result; - } *resp = r.Raw; - - rc = resp->result; - } - - return rc; + return serviceDispatchIn(&g_hidSrv, 206, AppletResourceUserId, + .buffer_attrs = { + SfBufferAttr_HipcPointer | SfBufferAttr_In, + SfBufferAttr_HipcPointer | SfBufferAttr_In, + }, + .buffers = { + { VibrationDeviceHandles, count*sizeof(u32) }, + { VibrationValues, count*sizeof(HidVibrationValue) }, + }, + ); } -static Result _hidGetDeviceHandles(u32 devicetype, u32 *DeviceHandles, size_t total_handles, HidControllerID id, HidControllerType type) { +static Result _hidGetDeviceHandles(u32 devicetype, u32 *DeviceHandles, s32 total_handles, HidControllerID id, HidControllerType type) { Result rc=0; u32 tmp_type = type & 0xff; u32 tmp_id = id; - if (total_handles == 0 || total_handles > 2 || devicetype > 1) + if (total_handles <= 0 || total_handles > 2 || devicetype > 1) return MAKERESULT(Module_Libnx, LibnxError_BadInput); if (tmp_id == CONTROLLER_HANDHELD) @@ -1421,11 +954,10 @@ static Result _hidGetDeviceHandles(u32 devicetype, u32 *DeviceHandles, size_t to tmp_type = 7; tmp_type |= 0x010000; } - //Official sw checks for these bits but libnx hid.h doesn't have these currently. - else if (tmp_type & BIT(29)) { + else if (tmp_type & TYPE_SYSTEM_EXT) { tmp_type = 0x20; } - else if (tmp_type & BIT(30)) { + else if (tmp_type & TYPE_SYSTEM) { tmp_type = 0x21; } else { @@ -1453,10 +985,10 @@ static Result _hidGetDeviceHandles(u32 devicetype, u32 *DeviceHandles, size_t to return rc; } -Result hidInitializeVibrationDevices(u32 *VibrationDeviceHandles, size_t total_handles, HidControllerID id, HidControllerType type) { +Result hidInitializeVibrationDevices(u32 *VibrationDeviceHandles, s32 total_handles, HidControllerID id, HidControllerType type) { Result rc=0; Service srv; - size_t i; + s32 i; rc = _hidGetDeviceHandles(0, VibrationDeviceHandles, total_handles, id, type); if (R_FAILED(rc)) return rc; @@ -1476,12 +1008,12 @@ Result hidInitializeVibrationDevices(u32 *VibrationDeviceHandles, size_t total_h return rc; } -Result hidGetSixAxisSensorHandles(u32 *SixAxisSensorHandles, size_t total_handles, HidControllerID id, HidControllerType type) { +Result hidGetSixAxisSensorHandles(u32 *SixAxisSensorHandles, s32 total_handles, HidControllerID id, HidControllerType type) { return _hidGetDeviceHandles(1, SixAxisSensorHandles, total_handles, id, type); } Result hidStartSixAxisSensor(u32 SixAxisSensorHandle) { - u32 rc = _hidCmdWithInputU32(66, SixAxisSensorHandle); + u32 rc = _hidCmdWithInputU32(SixAxisSensorHandle, 66); if (R_SUCCEEDED(rc)) { int controller = (SixAxisSensorHandle >> 8) & 0xff; if (controller == 0x20) @@ -1496,7 +1028,7 @@ Result hidStartSixAxisSensor(u32 SixAxisSensorHandle) { } Result hidStopSixAxisSensor(u32 SixAxisSensorHandle) { - u32 rc = _hidCmdWithInputU32(67, SixAxisSensorHandle); + u32 rc = _hidCmdWithInputU32(SixAxisSensorHandle, 67); if (R_SUCCEEDED(rc)) { int controller = (SixAxisSensorHandle >> 8) & 0xff; if (controller == 0x20) @@ -1539,45 +1071,17 @@ static Result _hidInitializeSevenSixAxisSensor(TransferMemory *tmem0, TransferMe if (R_FAILED(rc)) AppletResourceUserId = 0; - IpcCommand c; - ipcInitialize(&c); - - ipcSendPid(&c); - ipcSendHandleCopy(&c, tmem0->handle); - ipcSendHandleCopy(&c, tmem1->handle); - - struct { - u64 magic; - u64 cmd_id; + const struct { u64 AppletResourceUserId; u64 size0; u64 size1; - } *raw; + } in = { AppletResourceUserId, tmem0->size, tmem1->size }; - raw = serviceIpcPrepareHeader(&g_hidSrv, &c, sizeof(*raw)); - - raw->magic = SFCI_MAGIC; - raw->cmd_id = 306; - raw->AppletResourceUserId = AppletResourceUserId; - raw->size0 = tmem0->size; - raw->size1 = tmem1->size; - - rc = serviceIpcDispatch(&g_hidSrv); - - if (R_SUCCEEDED(rc)) { - IpcParsedCommand r; - struct { - u64 magic; - u64 result; - } *resp; - - serviceIpcParse(&g_hidSrv, &r, sizeof(*resp)); - resp = r.Raw; - - rc = resp->result; - } - - return rc; + return serviceDispatchIn(&g_hidSrv, 306, in, + .in_send_pid = true, + .in_num_handles = 2, + .in_handles = { tmem0->handle, tmem1->handle }, + ); } Result hidInitializeSevenSixAxisSensor(void) { @@ -1646,41 +1150,14 @@ Result hidSetSevenSixAxisSensorFusionStrength(float strength) { if (R_FAILED(rc)) AppletResourceUserId = 0; - IpcCommand c; - ipcInitialize(&c); - - struct { - u64 magic; - u64 cmd_id; + const struct { float strength; u64 AppletResourceUserId; - } *raw; + } in = { strength, AppletResourceUserId }; - ipcSendPid(&c); - - raw = serviceIpcPrepareHeader(&g_hidSrv, &c, sizeof(*raw)); - - raw->magic = SFCI_MAGIC; - raw->cmd_id = 308; - raw->strength = strength; - raw->AppletResourceUserId = AppletResourceUserId; - - rc = serviceIpcDispatch(&g_hidSrv); - - if (R_SUCCEEDED(rc)) { - IpcParsedCommand r; - struct { - u64 magic; - u64 result; - } *resp; - - serviceIpcParse(&g_hidSrv, &r, sizeof(*resp)); - resp = r.Raw; - - rc = resp->result; - } - - return rc; + return serviceDispatchIn(&g_hidSrv, 308, in, + .in_send_pid = true, + ); } Result hidGetSevenSixAxisSensorFusionStrength(float *strength) { @@ -1694,42 +1171,9 @@ Result hidGetSevenSixAxisSensorFusionStrength(float *strength) { if (R_FAILED(rc)) AppletResourceUserId = 0; - IpcCommand c; - ipcInitialize(&c); - - struct { - u64 magic; - u64 cmd_id; - u64 AppletResourceUserId; - } *raw; - - ipcSendPid(&c); - - raw = serviceIpcPrepareHeader(&g_hidSrv, &c, sizeof(*raw)); - - raw->magic = SFCI_MAGIC; - raw->cmd_id = 309; - raw->AppletResourceUserId = AppletResourceUserId; - - rc = serviceIpcDispatch(&g_hidSrv); - - if (R_SUCCEEDED(rc)) { - IpcParsedCommand r; - struct { - u64 magic; - u64 result; - float strength; - } *resp; - - serviceIpcParse(&g_hidSrv, &r, sizeof(*resp)); - resp = r.Raw; - - rc = resp->result; - - if (R_SUCCEEDED(rc) && strength) *strength = resp->strength; - } - - return rc; + return serviceDispatchInOut(&g_hidSrv, 309, AppletResourceUserId, *strength, + .in_send_pid = true, + ); } Result hidResetSevenSixAxisSensorTimestamp(void) { @@ -1743,40 +1187,7 @@ Result hidGetNpadInterfaceType(HidControllerID id, u8 *out) { if (hosversionBefore(4,0,0)) return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer); - Result rc; - - IpcCommand c; - ipcInitialize(&c); - - struct { - u64 magic; - u64 cmd_id; - u32 id; - } *raw; - - raw = ipcPrepareHeader(&c, sizeof(*raw)); - - raw->magic = SFCI_MAGIC; - raw->cmd_id = 405; - raw->id = hidControllerIDToOfficial(id); - - rc = serviceIpcDispatch(&g_hidSrv); - - if (R_SUCCEEDED(rc)) { - IpcParsedCommand r; - ipcParse(&r); - - struct { - u64 magic; - u64 result; - u8 out; - } *resp = r.Raw; - - rc = resp->result; - - if (R_SUCCEEDED(rc) && out) *out = resp->out; - } - - return rc; + u32 tmp = hidControllerIDToOfficial(id); + return serviceDispatchInOut(&g_hidSrv, 405, tmp, *out); }