diff --git a/nx/include/switch.h b/nx/include/switch.h index 9b2c846f..a753bf43 100644 --- a/nx/include/switch.h +++ b/nx/include/switch.h @@ -48,6 +48,8 @@ extern "C" { #include "switch/services/time.h" #include "switch/services/usb.h" #include "switch/services/hid.h" +#include "switch/services/hid_sys.h" +#include "switch/services/hid_dbg.h" #include "switch/services/irs.h" #include "switch/services/pl.h" #include "switch/services/vi.h" diff --git a/nx/include/switch/services/hid_dbg.h b/nx/include/switch/services/hid_dbg.h new file mode 100644 index 00000000..fd97f93f --- /dev/null +++ b/nx/include/switch/services/hid_dbg.h @@ -0,0 +1,17 @@ +/** + * @file hid.h + * @brief Human input device (hid:dbg) service IPC wrapper. + * @author p-sam + * @copyright libnx Authors + */ +#pragma once +#include "../types.h" + +#define HID_DBG_DHB_MAX_TRIES 100 +#define HID_DBG_DHB_RC (0x16eca) + +Result hidDbgInitialize(void); +void hidDbgExit(void); + +/// Enables the default behavior of the HOME button (going back to the main menu) if it was disabled +Result hidDbgDeactivateHomeButton(); \ No newline at end of file diff --git a/nx/include/switch/services/hid_sys.h b/nx/include/switch/services/hid_sys.h new file mode 100644 index 00000000..f80dd928 --- /dev/null +++ b/nx/include/switch/services/hid_sys.h @@ -0,0 +1,14 @@ +/** + * @file hid.h + * @brief Human input device (hid:sys) service IPC wrapper. + * @author p-sam + * @copyright libnx Authors + */ + #pragma once +#include "../types.h" + +Result hidSysInitialize(void); +void hidSysExit(void); + +/// Disables the default behavior of the HOME button (going back to the main menu) if it was enabled +Result hidSysActivateHomeButton(); \ No newline at end of file diff --git a/nx/source/services/hid_dbg.c b/nx/source/services/hid_dbg.c new file mode 100644 index 00000000..d1349747 --- /dev/null +++ b/nx/source/services/hid_dbg.c @@ -0,0 +1,77 @@ +#include "types.h" +#include "arm/atomics.h" +#include "kernel/ipc.h" +#include "services/sm.h" +#include "services/hid_dbg.h" + +static Service g_hidDbgSrv; +static u64 g_refCnt; + +Result hidDbgInitialize(void) +{ + atomicIncrement64(&g_refCnt); + + if (serviceIsActive(&g_hidDbgSrv)) + return 0; + + return smGetService(&g_hidDbgSrv, "hid:dbg"); +} + +void hidDbgExit(void) +{ + if (atomicDecrement64(&g_refCnt) == 0) + { + serviceClose(&g_hidDbgSrv); + } +} + +Result _hidDbgDeactivateHomeButton() { + Result rc; + + IpcCommand c; + ipcInitialize(&c); + + struct { + u64 magic; + u64 cmd_id; + } *raw; + + raw = ipcPrepareHeader(&c, sizeof(*raw)); + + raw->magic = SFCI_MAGIC; + raw->cmd_id = 110; + + rc = serviceIpcDispatch(&g_hidDbgSrv); + + if (R_SUCCEEDED(rc)) { + IpcParsedCommand r; + ipcParse(&r); + + struct { + u64 magic; + u64 result; + } *resp = r.Raw; + + rc = resp->result; + } + + return rc; +} + +Result hidDbgDeactivateHomeButton() { + int i = HID_DBG_DHB_MAX_TRIES; + Result rc; + + do { + i--; + rc = _hidDbgDeactivateHomeButton(); + } while(rc == 0 && i > 0); + + if(rc == 0) { + return HID_DBG_DHB_RC; + } else if(rc == HID_DBG_DHB_RC) { + return 0; + } + + return rc; +} \ No newline at end of file diff --git a/nx/source/services/hid_sys.c b/nx/source/services/hid_sys.c new file mode 100644 index 00000000..30244871 --- /dev/null +++ b/nx/source/services/hid_sys.c @@ -0,0 +1,70 @@ +#include "types.h" +#include "arm/atomics.h" +#include "kernel/ipc.h" +#include "services/sm.h" +#include "services/applet.h" +#include "services/hid_sys.h" + +static Service g_hidSysSrv; +static u64 g_refCnt; + +Result hidSysInitialize(void) +{ + atomicIncrement64(&g_refCnt); + + if (serviceIsActive(&g_hidSysSrv)) + return 0; + + return smGetService(&g_hidSysSrv, "hid:sys"); +} + +void hidSysExit(void) +{ + if (atomicDecrement64(&g_refCnt) == 0) + { + serviceClose(&g_hidSysSrv); + } +} + +Result hidSysActivateHomeButton() { + 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 = 111; + raw->AppletResourceUserId = AppletResourceUserId; + + rc = serviceIpcDispatch(&g_hidSysSrv); + + if (R_SUCCEEDED(rc)) { + IpcParsedCommand r; + ipcParse(&r); + + struct { + u64 magic; + u64 result; + } *resp = r.Raw; + + rc = resp->result; + } + + return rc; +} \ No newline at end of file