From 8d48921bab3c57b90881c3b30bfe691255605e71 Mon Sep 17 00:00:00 2001 From: p-sam <17620180+p-sam@users.noreply.github.com> Date: Sat, 4 Aug 2018 09:14:36 +0000 Subject: [PATCH 1/2] home button block/unblock via hid:dbg and hid:sys --- nx/include/switch.h | 2 + nx/include/switch/services/hid_dbg.h | 17 ++++++ nx/include/switch/services/hid_sys.h | 14 +++++ nx/source/services/hid_dbg.c | 77 ++++++++++++++++++++++++++++ nx/source/services/hid_sys.c | 70 +++++++++++++++++++++++++ 5 files changed, 180 insertions(+) create mode 100644 nx/include/switch/services/hid_dbg.h create mode 100644 nx/include/switch/services/hid_sys.h create mode 100644 nx/source/services/hid_dbg.c create mode 100644 nx/source/services/hid_sys.c 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 From 3cf003ca454c5c1e315e9ad75f57d06e1af0c380 Mon Sep 17 00:00:00 2001 From: p-sam Date: Sat, 4 Aug 2018 15:29:30 +0000 Subject: [PATCH 2/2] add a return at the end of each file --- nx/include/switch/services/hid_dbg.h | 2 +- nx/include/switch/services/hid_sys.h | 2 +- nx/source/services/hid_dbg.c | 2 +- nx/source/services/hid_sys.c | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/nx/include/switch/services/hid_dbg.h b/nx/include/switch/services/hid_dbg.h index fd97f93f..a64beccf 100644 --- a/nx/include/switch/services/hid_dbg.h +++ b/nx/include/switch/services/hid_dbg.h @@ -14,4 +14,4 @@ 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 +Result hidDbgDeactivateHomeButton(); diff --git a/nx/include/switch/services/hid_sys.h b/nx/include/switch/services/hid_sys.h index f80dd928..799243da 100644 --- a/nx/include/switch/services/hid_sys.h +++ b/nx/include/switch/services/hid_sys.h @@ -11,4 +11,4 @@ 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 +Result hidSysActivateHomeButton(); diff --git a/nx/source/services/hid_dbg.c b/nx/source/services/hid_dbg.c index d1349747..381f5155 100644 --- a/nx/source/services/hid_dbg.c +++ b/nx/source/services/hid_dbg.c @@ -74,4 +74,4 @@ Result hidDbgDeactivateHomeButton() { } return rc; -} \ No newline at end of file +} diff --git a/nx/source/services/hid_sys.c b/nx/source/services/hid_sys.c index 30244871..2e7e81d9 100644 --- a/nx/source/services/hid_sys.c +++ b/nx/source/services/hid_sys.c @@ -67,4 +67,4 @@ Result hidSysActivateHomeButton() { } return rc; -} \ No newline at end of file +}