From 9916fe93a048ad5f9123b55d639b0d547bc72721 Mon Sep 17 00:00:00 2001 From: yellows8 Date: Wed, 2 Oct 2019 19:02:20 -0400 Subject: [PATCH] applet: Updated for new-ipc, various internal improvements, and changed the transfer_size param type for appletReadThemeStorage to u64. --- nx/include/switch/services/applet.h | 3 +- nx/source/services/applet.c | 3300 +++++---------------------- 2 files changed, 598 insertions(+), 2705 deletions(-) diff --git a/nx/include/switch/services/applet.h b/nx/include/switch/services/applet.h index fdfd67bf..42a14f84 100644 --- a/nx/include/switch/services/applet.h +++ b/nx/include/switch/services/applet.h @@ -7,7 +7,6 @@ */ #pragma once #include "../types.h" -#include "../services/sm.h" #include "../services/apm.h" #include "../services/pdm.h" #include "../services/caps.h" @@ -2137,7 +2136,7 @@ Result appletBeginToObserveHidInputForDevelop(void); * @param[in] offset Offset within the ThemeStorage. * @param[out] transfer_size Actual read size. */ -Result appletReadThemeStorage(void* buffer, size_t size, u64 offset, size_t *transfer_size); +Result appletReadThemeStorage(void* buffer, size_t size, u64 offset, u64 *transfer_size); /** * @brief Writes the ThemeStorage for the current applet. diff --git a/nx/source/services/applet.c b/nx/source/services/applet.c index ce2fe5cf..fda2dff5 100644 --- a/nx/source/services/applet.c +++ b/nx/source/services/applet.c @@ -1,9 +1,6 @@ #include -#include "types.h" -#include "result.h" -#include "arm/atomics.h" +#include "service_guard.h" #include "arm/counter.h" -#include "kernel/ipc.h" #include "kernel/tmem.h" #include "kernel/event.h" #include "services/fatal.h" @@ -27,7 +24,7 @@ __attribute__((weak)) u32 __nx_applet_exit_mode = 0; static Service g_appletSrv; static Service g_appletProxySession; -static u64 g_refCnt; +static bool g_appletLeftInitialized; static bool g_appletExitProcessFlag; static Result g_appletExitProcessResult; @@ -81,10 +78,12 @@ static ApmCpuBoostMode g_appletCpuBoostMode = ApmCpuBoostMode_Disabled; static AppletInfo g_appletInfo; static bool g_appletInfoInitialized; -static Result _appletGetHandle(Service* srv, Handle* handle_out, u64 cmd_id); -static Result _appletGetEvent(Service* srv, Event* out_event, u64 cmd_id, bool autoclear); -static Result _appletGetSession(Service* srv, Service* srv_out, u64 cmd_id); -static Result _appletGetSessionProxy(Service* srv_out, u64 cmd_id, Handle prochandle, const AppletAttribute *attr); +static Result _appletGetHandle(Service* srv, Handle* handle_out, u32 cmd_id); +static Result _appletGetEvent(Service* srv, Event* out_event, bool autoclear, u32 cmd_id); +static Result _appletGetSession(Service* srv, Service* srv_out, u32 cmd_id); + +static Result _appletOpenLibraryAppletProxy(Service* srv_out, Handle prochandle, const AppletAttribute *attr); +static Result _appletGetSessionProxy(Service* srv_out, Handle prochandle, u32 cmd_id); static Result _appletGetAppletResourceUserId(u64 *out); @@ -112,16 +111,17 @@ static Result _appletGetLaunchReason(AppletProcessLaunchReason *reason); static Result _appletOpenCallingLibraryApplet(AppletHolder *h); static Result _appletHolderCreateState(AppletHolder *h, LibAppletMode mode, bool creating_self); -static Result _appletOpenExistingLibraryApplet(AppletHolder *h, Service* srv, u64 cmd_id); +static Result _appletOpenExistingLibraryApplet(AppletHolder *h, Service* srv, u32 cmd_id); -Result appletInitialize(void) -{ +NX_GENERATE_SERVICE_GUARD(applet); + +Result _appletInitialize(void) { AppletAttribute *attr = NULL; - atomicIncrement64(&g_refCnt); - - if (serviceIsActive(&g_appletSrv)) + if (g_appletLeftInitialized) { + g_appletLeftInitialized = false; return 0; + } if (__nx_applet_type == AppletType_None) return 0; @@ -140,15 +140,15 @@ Result appletInitialize(void) memset(&g_appletInfo, 0, sizeof(g_appletInfo)); switch (__nx_applet_type) { - case AppletType_Default: - __nx_applet_type = AppletType_Application; - // Fallthrough. - case AppletType_Application: - rc = smGetService(&g_appletSrv, "appletOE"); - break; - default: - rc = smGetService(&g_appletSrv, "appletAE"); - break; + case AppletType_Default: + __nx_applet_type = AppletType_Application; + // Fallthrough. + case AppletType_Application: + rc = smGetService(&g_appletSrv, "appletOE"); + break; + default: + rc = smGetService(&g_appletSrv, "appletAE"); + break; } if (R_SUCCEEDED(rc)) { @@ -162,13 +162,13 @@ Result appletInitialize(void) u32 cmd_id; switch(__nx_applet_type) { - case AppletType_Application: cmd_id = 0; break; - case AppletType_SystemApplet: cmd_id = 100; break; - case AppletType_LibraryApplet: cmd_id = 200; break; - case AppletType_OverlayApplet: cmd_id = 300; break; - case AppletType_SystemApplication: cmd_id = 350; break; - // TODO: Replace error code - default: fatalSimple(MAKERESULT(Module_Libnx, LibnxError_AppletCmdidNotFound)); + case AppletType_Application: cmd_id = 0; break; + case AppletType_SystemApplet: cmd_id = 100; break; + case AppletType_LibraryApplet: cmd_id = 200; break; + case AppletType_OverlayApplet: cmd_id = 300; break; + case AppletType_SystemApplication: cmd_id = 350; break; + // TODO: Replace error code + default: fatalSimple(MAKERESULT(Module_Libnx, LibnxError_AppletCmdidNotFound)); } if (__nx_applet_type == AppletType_LibraryApplet && hosversionAtLeast(3,0,0)) { @@ -176,7 +176,10 @@ Result appletInitialize(void) attr = &__nx_applet_AppletAttribute; } - rc = _appletGetSessionProxy(&g_appletProxySession, cmd_id, CUR_PROCESS_HANDLE, attr); + if (attr==NULL) + rc = _appletGetSessionProxy(&g_appletProxySession, CUR_PROCESS_HANDLE, cmd_id); + else + rc = _appletOpenLibraryAppletProxy(&g_appletProxySession, CUR_PROCESS_HANDLE, attr); if (rc == AM_BUSY_ERROR) { svcSleepThread(10000000); @@ -248,7 +251,7 @@ Result appletInitialize(void) // ICommonStateGetter::GetEventHandle if (R_SUCCEEDED(rc)) - rc = _appletGetEvent(&g_appletICommonStateGetter, &g_appletMessageEvent, 0, false); + rc = _appletGetEvent(&g_appletICommonStateGetter, &g_appletMessageEvent, false, 0); if (R_SUCCEEDED(rc) && (__nx_applet_type == AppletType_Application)) { @@ -358,92 +361,89 @@ static bool _appletIsRegularApplication(void) { return __nx_applet_type == AppletType_Application; } -void appletExit(void) +void _appletCleanup(void) { - if (atomicDecrement64(&g_refCnt) == 0) - { - if (!g_appletExitProcessFlag) { - if (g_appletRecordingInitialized > 0) { - if (g_appletRecordingInitialized == 2) appletSetGamePlayRecordingState(0); - } - - if (__nx_applet_type == AppletType_Application) appletSetFocusHandlingMode(AppletFocusHandlingMode_NoSuspend); - - if (g_appletCpuBoostMode != ApmCpuBoostMode_Disabled) appletSetCpuBoostMode(ApmCpuBoostMode_Disabled); - } - - if ((envIsNso() && __nx_applet_exit_mode==0) || __nx_applet_exit_mode==1) { - if (_appletIsApplication() || - __nx_applet_type == AppletType_LibraryApplet) { - if (!g_appletExitProcessFlag) { - g_appletExitProcessFlag = 1; - atomicIncrement64(&g_refCnt); - envSetExitFuncPtr(_appletExitProcess); - return; - } - else { - if (_appletIsApplication()) - g_appletExitProcessResult = _appletSelfExit(); - if (__nx_applet_type == AppletType_LibraryApplet) - g_appletExitProcessResult = _appletExitProcessAndReturn(); - } - } - } - - if (g_appletInfoInitialized) { - if (g_appletInfo.caller_flag) appletHolderClose(&g_appletInfo.caller); - g_appletInfoInitialized = 0; - memset(&g_appletInfo, 0, sizeof(g_appletInfo)); - } - - eventClose(&g_appletLibraryAppletLaunchableEvent); - - eventClose(&g_appletMessageEvent); - - if (g_appletSuspendedTickInitialized) { - eventClose(&g_appletSuspendedTickEvent); - g_appletSuspendedTickInitialized = false; - } - - serviceClose(&g_appletIDebugFunctions); - serviceClose(&g_appletIDisplayController); - serviceClose(&g_appletIAudioController); - serviceClose(&g_appletIWindowController); - serviceClose(&g_appletISelfController); - serviceClose(&g_appletICommonStateGetter); - serviceClose(&g_appletILibraryAppletCreator); - - if (__nx_applet_type == AppletType_SystemApplet) { - serviceClose(&g_appletIApplicationCreator); - serviceClose(&g_appletIGlobalStateController); - } - - if (__nx_applet_type != AppletType_LibraryApplet) - serviceClose(&g_appletIFunctions); - - if (__nx_applet_type == AppletType_LibraryApplet) { - serviceClose(&g_appletIProcessWindingController); - serviceClose(&g_appletILibraryAppletSelfAccessor); - } - - serviceClose(&g_appletIAppletCommonFunctions); - - serviceClose(&g_appletProxySession); - serviceClose(&g_appletSrv); - g_appletResourceUserId = 0; - - apmExit(); - + if (!g_appletExitProcessFlag) { if (g_appletRecordingInitialized > 0) { - tmemClose(&g_appletRecordingTmem); - g_appletRecordingInitialized = 0; + if (g_appletRecordingInitialized == 2) appletSetGamePlayRecordingState(0); } - if (g_appletCopyrightInitialized) { - tmemClose(&g_appletCopyrightTmem); - g_appletCopyrightInitialized = 0; + if (__nx_applet_type == AppletType_Application) appletSetFocusHandlingMode(AppletFocusHandlingMode_NoSuspend); + + if (g_appletCpuBoostMode != ApmCpuBoostMode_Disabled) appletSetCpuBoostMode(ApmCpuBoostMode_Disabled); + } + + if ((envIsNso() && __nx_applet_exit_mode==0) || __nx_applet_exit_mode==1) { + if (_appletIsApplication() || + __nx_applet_type == AppletType_LibraryApplet) { + if (!g_appletExitProcessFlag) { + g_appletExitProcessFlag = 1; + g_appletLeftInitialized = true; + envSetExitFuncPtr(_appletExitProcess); + return; + } + else { + if (_appletIsApplication()) + g_appletExitProcessResult = _appletSelfExit(); + if (__nx_applet_type == AppletType_LibraryApplet) + g_appletExitProcessResult = _appletExitProcessAndReturn(); + } } } + + if (g_appletInfoInitialized) { + if (g_appletInfo.caller_flag) appletHolderClose(&g_appletInfo.caller); + g_appletInfoInitialized = 0; + memset(&g_appletInfo, 0, sizeof(g_appletInfo)); + } + + eventClose(&g_appletLibraryAppletLaunchableEvent); + + eventClose(&g_appletMessageEvent); + + if (g_appletSuspendedTickInitialized) { + eventClose(&g_appletSuspendedTickEvent); + g_appletSuspendedTickInitialized = false; + } + + serviceClose(&g_appletIDebugFunctions); + serviceClose(&g_appletIDisplayController); + serviceClose(&g_appletIAudioController); + serviceClose(&g_appletIWindowController); + serviceClose(&g_appletISelfController); + serviceClose(&g_appletICommonStateGetter); + serviceClose(&g_appletILibraryAppletCreator); + + if (__nx_applet_type == AppletType_SystemApplet) { + serviceClose(&g_appletIApplicationCreator); + serviceClose(&g_appletIGlobalStateController); + } + + if (__nx_applet_type != AppletType_LibraryApplet) + serviceClose(&g_appletIFunctions); + + if (__nx_applet_type == AppletType_LibraryApplet) { + serviceClose(&g_appletIProcessWindingController); + serviceClose(&g_appletILibraryAppletSelfAccessor); + } + + serviceClose(&g_appletIAppletCommonFunctions); + + serviceClose(&g_appletProxySession); + serviceClose(&g_appletSrv); + g_appletResourceUserId = 0; + + apmExit(); + + if (g_appletRecordingInitialized > 0) { + tmemClose(&g_appletRecordingTmem); + g_appletRecordingInitialized = 0; + } + + if (g_appletCopyrightInitialized) { + tmemClose(&g_appletCopyrightTmem); + g_appletCopyrightInitialized = 0; + } } Service* appletGetServiceSession_Proxy(void) { @@ -587,44 +587,16 @@ Result appletSetFocusHandlingMode(AppletFocusHandlingMode mode) { return rc; } -static Result _appletGetHandle(Service* srv, Handle* handle_out, u64 cmd_id) { - IpcCommand c; - ipcInitialize(&c); - - struct { - u64 magic; - u64 cmd_id; - } *raw; - - raw = serviceIpcPrepareHeader(srv, &c, sizeof(*raw)); - - raw->magic = SFCI_MAGIC; - raw->cmd_id = cmd_id; - - Result rc = serviceIpcDispatch(srv); - - if (R_SUCCEEDED(rc)) { - IpcParsedCommand r; - struct { - u64 magic; - u64 result; - } *resp; - - serviceIpcParse(srv, &r, sizeof(*resp)); - resp = r.Raw; - - rc = resp->result; - - if (R_SUCCEEDED(rc)) { - *handle_out = r.Handles[0]; - } - } - - return rc; +static Result _appletGetHandle(Service* srv, Handle* handle_out, u32 cmd_id) { + serviceAssumeDomain(srv); + return serviceDispatch(srv, cmd_id, + .out_handle_attrs = { SfOutHandleAttr_HipcCopy }, + .out_handles = handle_out, + ); } -static Result _appletGetEvent(Service* srv, Event* out_event, u64 cmd_id, bool autoclear) { - Handle tmp_handle=0; +static Result _appletGetEvent(Service* srv, Event* out_event, bool autoclear, u32 cmd_id) { + Handle tmp_handle = INVALID_HANDLE; Result rc = 0; rc = _appletGetHandle(srv, &tmp_handle, cmd_id); @@ -632,725 +604,195 @@ static Result _appletGetEvent(Service* srv, Event* out_event, u64 cmd_id, bool a return rc; } -static Result _appletGetSession(Service* srv, Service* srv_out, u64 cmd_id) { - IpcCommand c; - ipcInitialize(&c); - - struct { - u64 magic; - u64 cmd_id; - } *raw; - - raw = serviceIpcPrepareHeader(srv, &c, sizeof(*raw)); - - raw->magic = SFCI_MAGIC; - raw->cmd_id = cmd_id; - - Result rc = serviceIpcDispatch(srv); - - if (R_SUCCEEDED(rc)) { - IpcParsedCommand r; - struct { - u64 magic; - u64 result; - } *resp; - - serviceIpcParse(srv, &r, sizeof(*resp)); - resp = r.Raw; - - rc = resp->result; - - if (R_SUCCEEDED(rc)) { - serviceCreateSubservice(srv_out, srv, &r, 0); - } - } - - return rc; +static Result _appletGetSession(Service* srv, Service* srv_out, u32 cmd_id) { + serviceAssumeDomain(srv); + return serviceDispatch(srv, cmd_id, + .out_num_objects = 1, + .out_objects = srv_out, + ); } -static Result _appletGetSessionProxy(Service* srv_out, u64 cmd_id, Handle prochandle, const AppletAttribute *attr) { - IpcCommand c; - ipcInitialize(&c); - - struct { - u64 magic; - u64 cmd_id; - u64 reserved; - } *raw; - - ipcSendPid(&c); - ipcSendHandleCopy(&c, prochandle); - if (attr) ipcAddSendBuffer(&c, attr, sizeof(AppletAttribute), BufferType_Normal); - - raw = serviceIpcPrepareHeader(&g_appletSrv, &c, sizeof(*raw)); - - raw->magic = SFCI_MAGIC; - raw->cmd_id = cmd_id; - raw->reserved = 0; - - Result rc = serviceIpcDispatch(&g_appletSrv); - - if (R_SUCCEEDED(rc)) { - IpcParsedCommand r; - struct { - u64 magic; - u64 result; - } *resp; - - serviceIpcParse(&g_appletSrv, &r, sizeof(*resp)); - resp = r.Raw; - - rc = resp->result; - - if (R_SUCCEEDED(rc)) { - serviceCreateSubservice(srv_out, &g_appletSrv, &r, 0); - } - } - - return rc; +static Result _appletOpenLibraryAppletProxy(Service* srv_out, Handle prochandle, const AppletAttribute *attr) { + u64 reserved=0; + serviceAssumeDomain(&g_appletSrv); + return serviceDispatchIn(&g_appletSrv, 201, reserved, + .buffer_attrs = { SfBufferAttr_HipcMapAlias | SfBufferAttr_In }, + .buffers = { { attr, sizeof(*attr) } }, + .in_send_pid = true, + .in_num_handles = 1, + .in_handles = { prochandle }, + .out_num_objects = 1, + .out_objects = srv_out, + ); } -static Result _appletGetSessionIn64(Service* srv, Service* srv_out, u64 cmd_id, u64 inval) { - IpcCommand c; - ipcInitialize(&c); - - struct { - u64 magic; - u64 cmd_id; - u64 inval; - } *raw; - - raw = serviceIpcPrepareHeader(srv, &c, sizeof(*raw)); - - raw->magic = SFCI_MAGIC; - raw->cmd_id = cmd_id; - raw->inval = inval; - - Result rc = serviceIpcDispatch(srv); - - if (R_SUCCEEDED(rc)) { - IpcParsedCommand r; - struct { - u64 magic; - u64 result; - } *resp; - - serviceIpcParse(srv, &r, sizeof(*resp)); - resp = r.Raw; - - rc = resp->result; - - if (R_SUCCEEDED(rc)) { - serviceCreateSubservice(srv_out, srv, &r, 0); - } - } - - return rc; +static Result _appletGetSessionProxy(Service* srv_out, Handle prochandle, u32 cmd_id) { + u64 reserved=0; + serviceAssumeDomain(&g_appletSrv); + return serviceDispatchIn(&g_appletSrv, cmd_id, reserved, + .in_send_pid = true, + .in_num_handles = 1, + .in_handles = { prochandle }, + .out_num_objects = 1, + .out_objects = srv_out, + ); } -static Result _appletGetSessionIn32(Service* srv, Service* srv_out, u64 cmd_id, u32 inval) { - IpcCommand c; - ipcInitialize(&c); - - struct { - u64 magic; - u64 cmd_id; - u32 inval; - } *raw; - - raw = serviceIpcPrepareHeader(srv, &c, sizeof(*raw)); - - raw->magic = SFCI_MAGIC; - raw->cmd_id = cmd_id; - raw->inval = inval; - - Result rc = serviceIpcDispatch(srv); - - if (R_SUCCEEDED(rc)) { - IpcParsedCommand r; - struct { - u64 magic; - u64 result; - } *resp; - - serviceIpcParse(srv, &r, sizeof(*resp)); - resp = r.Raw; - - rc = resp->result; - - if (R_SUCCEEDED(rc)) { - serviceCreateSubservice(srv_out, srv, &r, 0); - } - } - - return rc; +static Result _appletGetSessionIn64(Service* srv, Service* srv_out, u64 inval, u32 cmd_id) { + serviceAssumeDomain(srv); + return serviceDispatchIn(srv, cmd_id, inval, + .out_num_objects = 1, + .out_objects = srv_out, + ); } -static Result _appletCmdNoIO(Service* session, u64 cmd_id) { - IpcCommand c; - ipcInitialize(&c); - - struct { - u64 magic; - u64 cmd_id; - } *raw; - - raw = serviceIpcPrepareHeader(session, &c, sizeof(*raw)); - - raw->magic = SFCI_MAGIC; - raw->cmd_id = cmd_id; - - Result rc = serviceIpcDispatch(session); - - if (R_SUCCEEDED(rc)) { - IpcParsedCommand r; - struct { - u64 magic; - u64 result; - } *resp; - - serviceIpcParse(session, &r, sizeof(*resp)); - resp = r.Raw; - - rc = resp->result; - } - - return rc; +static Result _appletGetSessionIn32(Service* srv, Service* srv_out, u32 inval, u32 cmd_id) { + serviceAssumeDomain(srv); + return serviceDispatchIn(srv, cmd_id, inval, + .out_num_objects = 1, + .out_objects = srv_out, + ); } -static Result _appletCmdNoInOut64(Service* srv, u64 *out, u64 cmd_id) { - IpcCommand c; - ipcInitialize(&c); - - struct { - u64 magic; - u64 cmd_id; - } *raw; - - raw = serviceIpcPrepareHeader(srv, &c, sizeof(*raw)); - - raw->magic = SFCI_MAGIC; - raw->cmd_id = cmd_id; - - Result rc = serviceIpcDispatch(srv); - - if (R_SUCCEEDED(rc)) { - IpcParsedCommand r; - struct { - u64 magic; - u64 result; - u64 out; - } *resp; - - serviceIpcParse(srv, &r, sizeof(*resp)); - resp = r.Raw; - - rc = resp->result; - - if (R_SUCCEEDED(rc) && out) { - *out = resp->out; - } - } - - return rc; +static Result _appletCmdNoIO(Service* srv, u32 cmd_id) { + serviceAssumeDomain(srv); + return serviceDispatch(srv, cmd_id); } -static Result _appletCmdNoInOut32(Service* srv, u32 *out, u64 cmd_id) { - IpcCommand c; - ipcInitialize(&c); - - struct { - u64 magic; - u64 cmd_id; - } *raw; - - raw = serviceIpcPrepareHeader(srv, &c, sizeof(*raw)); - - raw->magic = SFCI_MAGIC; - raw->cmd_id = cmd_id; - - Result rc = serviceIpcDispatch(srv); - - if (R_SUCCEEDED(rc)) { - IpcParsedCommand r; - struct { - u64 magic; - u64 result; - u32 out; - } *resp; - - serviceIpcParse(srv, &r, sizeof(*resp)); - resp = r.Raw; - - rc = resp->result; - - if (R_SUCCEEDED(rc) && out) { - *out = resp->out; - } - } - - return rc; +static Result _appletCmdNoInOut64(Service* srv, u64 *out, u32 cmd_id) { + serviceAssumeDomain(srv); + return serviceDispatchOut(srv, cmd_id, *out); } -static Result _appletCmdNoInOutU8(Service* srv, u8 *out, u64 cmd_id) { - IpcCommand c; - ipcInitialize(&c); - - struct { - u64 magic; - u64 cmd_id; - } *raw; - - raw = serviceIpcPrepareHeader(srv, &c, sizeof(*raw)); - - raw->magic = SFCI_MAGIC; - raw->cmd_id = cmd_id; - - Result rc = serviceIpcDispatch(srv); - - if (R_SUCCEEDED(rc)) { - IpcParsedCommand r; - struct { - u64 magic; - u64 result; - u8 out; - } *resp; - - serviceIpcParse(srv, &r, sizeof(*resp)); - resp = r.Raw; - - rc = resp->result; - - if (R_SUCCEEDED(rc) && out) { - *out = resp->out; - } - } - - return rc; +static Result _appletCmdNoInOut32(Service* srv, u32 *out, u32 cmd_id) { + serviceAssumeDomain(srv); + return serviceDispatchOut(srv, cmd_id, *out); } -static Result _appletCmdNoInOutBool(Service* srv, bool *out, u64 cmd_id) { +static Result _appletCmdNoInOutU8(Service* srv, u8 *out, u32 cmd_id) { + serviceAssumeDomain(srv); + return serviceDispatchOut(srv, cmd_id, *out); +} + +static Result _appletCmdNoInOutBool(Service* srv, bool *out, u32 cmd_id) { u8 tmp=0; + serviceAssumeDomain(srv); Result rc = _appletCmdNoInOutU8(srv, &tmp, cmd_id); if (R_SUCCEEDED(rc) && out) *out = tmp!=0; return rc; } static Result _appletCmdInU8(Service* srv, u8 inval, u64 cmd_id) { - IpcCommand c; - ipcInitialize(&c); - - struct { - u64 magic; - u64 cmd_id; - u8 inval; - } *raw; - - raw = serviceIpcPrepareHeader(srv, &c, sizeof(*raw)); - - raw->magic = SFCI_MAGIC; - raw->cmd_id = cmd_id; - raw->inval = inval; - - Result rc = serviceIpcDispatch(srv); - - if (R_SUCCEEDED(rc)) { - IpcParsedCommand r; - struct { - u64 magic; - u64 result; - } *resp; - - serviceIpcParse(srv, &r, sizeof(*resp)); - resp = r.Raw; - - rc = resp->result; - } - - return rc; + serviceAssumeDomain(srv); + return serviceDispatchIn(srv, cmd_id, inval); } -static Result _appletCmdInU32(Service* srv, u32 inval, u64 cmd_id) { - IpcCommand c; - ipcInitialize(&c); - - struct { - u64 magic; - u64 cmd_id; - u32 inval; - } PACKED *raw; - - raw = serviceIpcPrepareHeader(srv, &c, sizeof(*raw)); - - raw->magic = SFCI_MAGIC; - raw->cmd_id = cmd_id; - raw->inval = inval; - - Result rc = serviceIpcDispatch(srv); - - if (R_SUCCEEDED(rc)) { - IpcParsedCommand r; - struct { - u64 magic; - u64 result; - } *resp; - - serviceIpcParse(srv, &r, sizeof(*resp)); - resp = r.Raw; - - rc = resp->result; - } - - return rc; +static Result _appletCmdInU32(Service* srv, u32 inval, u32 cmd_id) { + serviceAssumeDomain(srv); + return serviceDispatchIn(srv, cmd_id, inval); } -static Result _appletCmdInU64(Service* srv, u64 inval, u64 cmd_id) { - IpcCommand c; - ipcInitialize(&c); - - struct { - u64 magic; - u64 cmd_id; - u64 inval; - } *raw; - - raw = serviceIpcPrepareHeader(srv, &c, sizeof(*raw)); - - raw->magic = SFCI_MAGIC; - raw->cmd_id = cmd_id; - raw->inval = inval; - - Result rc = serviceIpcDispatch(srv); - - if (R_SUCCEEDED(rc)) { - IpcParsedCommand r; - struct { - u64 magic; - u64 result; - } *resp; - - serviceIpcParse(srv, &r, sizeof(*resp)); - resp = r.Raw; - - rc = resp->result; - } - - return rc; +static Result _appletCmdInU64(Service* srv, u64 inval, u32 cmd_id) { + serviceAssumeDomain(srv); + return serviceDispatchIn(srv, cmd_id, inval); } -static Result _appletCmdInBool(Service* srv, bool inval, u64 cmd_id) { +static Result _appletCmdInBool(Service* srv, bool inval, u32 cmd_id) { + serviceAssumeDomain(srv); return _appletCmdInU8(srv, inval!=0, cmd_id); } -static Result _appletCmdInHandle64(Service* srv, Service* srv_out, u64 cmd_id, Handle handle, u64 inval) { - IpcCommand c; - ipcInitialize(&c); - - ipcSendHandleCopy(&c, handle); - - struct { - u64 magic; - u64 cmd_id; - u64 inval; - } *raw; - - raw = serviceIpcPrepareHeader(srv, &c, sizeof(*raw)); - - raw->magic = SFCI_MAGIC; - raw->cmd_id = cmd_id; - raw->inval = inval; - - Result rc = serviceIpcDispatch(srv); - - if (R_SUCCEEDED(rc)) { - IpcParsedCommand r; - struct { - u64 magic; - u64 result; - } *resp; - - serviceIpcParse(srv, &r, sizeof(*resp)); - resp = r.Raw; - - rc = resp->result; - - if (R_SUCCEEDED(rc) && srv_out) { - serviceCreateSubservice(srv_out, srv, &r, 0); - } - } - - return rc; +static Result _appletCmdInHandle64NoOut(Service* srv, Handle handle, u64 inval, u32 cmd_id) { + serviceAssumeDomain(srv); + return serviceDispatchIn(srv, cmd_id, inval, + .in_num_handles = 1, + .in_handles = { handle }, + ); } -static Result _appletCmdInTmem(Service* srv, Service* srv_out, u64 cmd_id, TransferMemory *tmem) { - return _appletCmdInHandle64(srv, srv_out, cmd_id, tmem->handle, tmem->size); +static Result _appletCmdInHandle64OutSession(Service* srv, Service* srv_out, Handle handle, u64 inval, u32 cmd_id) { + serviceAssumeDomain(srv); + return serviceDispatchIn(srv, cmd_id, inval, + .in_num_handles = 1, + .in_handles = { handle }, + .out_num_objects = 1, + .out_objects = srv_out, + ); } -static Result _appletCmdInSession(Service* srv, Service* srv_in, u64 cmd_id) { - IpcCommand c; - ipcInitialize(&c); - - serviceSendObject(srv_in, &c); - - struct { - u64 magic; - u64 cmd_id; - } *raw; - - raw = serviceIpcPrepareHeader(srv, &c, sizeof(*raw)); - - raw->magic = SFCI_MAGIC; - raw->cmd_id = cmd_id; - - Result rc = serviceIpcDispatch(srv); - - if (R_SUCCEEDED(rc)) { - IpcParsedCommand r; - struct { - u64 magic; - u64 result; - } *resp; - - serviceIpcParse(srv, &r, sizeof(*resp)); - resp = r.Raw; - - rc = resp->result; - } - - return rc; +static Result _appletCmdInTmemNoOut(Service* srv, TransferMemory *tmem, u32 cmd_id) { + serviceAssumeDomain(srv); + return _appletCmdInHandle64NoOut(srv, tmem->handle, tmem->size, cmd_id); } -static Result _appletCmdInStorage(Service* srv, AppletStorage* s, u64 cmd_id) { +static Result _appletCmdInTmemOutSession(Service* srv, Service* srv_out, TransferMemory *tmem, u32 cmd_id) { + serviceAssumeDomain(srv); + return _appletCmdInHandle64OutSession(srv, srv_out, tmem->handle, tmem->size, cmd_id); +} + +static Result _appletCmdInSession(Service* srv, Service* srv_in, u32 cmd_id) { + serviceAssumeDomain(srv); + return serviceDispatch(srv, cmd_id, + .in_num_objects = 1, + .in_objects = { srv_in }, + ); +} + +static Result _appletCmdInStorage(Service* srv, AppletStorage* s, u32 cmd_id) { Result rc=0; if (!serviceIsActive(&s->s)) return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); + serviceAssumeDomain(srv); rc =_appletCmdInSession(srv, &s->s, cmd_id); appletStorageClose(s); return rc; } -static Result _appletCmdNoInOutStorage(Service* srv, AppletStorage* s, u64 cmd_id) { +static Result _appletCmdNoInOutStorage(Service* srv, AppletStorage* s, u32 cmd_id) { memset(s, 0, sizeof(AppletStorage)); + serviceAssumeDomain(srv); return _appletGetSession(srv, &s->s, cmd_id); } -static Result _appletCmdSendBufNoOut(Service* srv, const void* buffer, size_t size, u64 cmd_id) { - IpcCommand c; - ipcInitialize(&c); - - ipcAddSendBuffer(&c, buffer, size, BufferType_Normal); - - struct { - u64 magic; - u64 cmd_id; - } *raw; - - raw = serviceIpcPrepareHeader(srv, &c, sizeof(*raw)); - - raw->magic = SFCI_MAGIC; - raw->cmd_id = cmd_id; - - Result rc = serviceIpcDispatch(srv); - - if (R_SUCCEEDED(rc)) { - IpcParsedCommand r; - struct { - u64 magic; - u64 result; - } *resp; - - serviceIpcParse(srv, &r, sizeof(*resp)); - resp = r.Raw; - - rc = resp->result; - } - - return rc; +static Result _appletCmdSendBufNoOut(Service* srv, const void* buffer, size_t size, u32 cmd_id) { + serviceAssumeDomain(srv); + return serviceDispatch(srv, cmd_id, + .buffer_attrs = { SfBufferAttr_HipcMapAlias | SfBufferAttr_In }, + .buffers = { { buffer, size } }, + ); } -static Result _appletCmdNoInRecvBuf(Service* srv, void* buffer, size_t size, u64 cmd_id) { - IpcCommand c; - ipcInitialize(&c); - - ipcAddRecvBuffer(&c, buffer, size, BufferType_Normal); - - struct { - u64 magic; - u64 cmd_id; - } *raw; - - raw = serviceIpcPrepareHeader(srv, &c, sizeof(*raw)); - - raw->magic = SFCI_MAGIC; - raw->cmd_id = cmd_id; - - Result rc = serviceIpcDispatch(srv); - - if (R_SUCCEEDED(rc)) { - IpcParsedCommand r; - struct { - u64 magic; - u64 result; - } *resp; - - serviceIpcParse(srv, &r, sizeof(*resp)); - resp = r.Raw; - - rc = resp->result; - } - - return rc; +static Result _appletCmdNoInRecvBuf(Service* srv, void* buffer, size_t size, u32 cmd_id) { + serviceAssumeDomain(srv); + return serviceDispatch(srv, cmd_id, + .buffer_attrs = { SfBufferAttr_HipcMapAlias | SfBufferAttr_Out }, + .buffers = { { buffer, size } }, + ); } -static Result _appletGetLibraryAppletInfo(Service* srv, LibAppletInfo *info, u64 cmd_id) { - IpcCommand c; - ipcInitialize(&c); - - struct { - u64 magic; - u64 cmd_id; - } *raw; - - raw = serviceIpcPrepareHeader(srv, &c, sizeof(*raw)); - - raw->magic = SFCI_MAGIC; - raw->cmd_id = cmd_id; - - Result rc = serviceIpcDispatch(srv); - - if (R_SUCCEEDED(rc)) { - IpcParsedCommand r; - struct { - u64 magic; - u64 result; - LibAppletInfo info; - } *resp; - - serviceIpcParse(srv, &r, sizeof(*resp)); - resp = r.Raw; - - rc = resp->result; - - if (R_SUCCEEDED(rc) && info) memcpy(info, &resp->info, sizeof(LibAppletInfo)); - } - - return rc; +static Result _appletGetLibraryAppletInfo(Service* srv, LibAppletInfo *info, u32 cmd_id) { + serviceAssumeDomain(srv); + return serviceDispatchOut(srv, cmd_id, *info); } -static Result _appletGetIdentityInfo(Service* srv, AppletIdentityInfo *info, u64 cmd_id) { - IpcCommand c; - ipcInitialize(&c); - - struct { - u64 magic; - u64 cmd_id; - } *raw; - - raw = serviceIpcPrepareHeader(srv, &c, sizeof(*raw)); - - raw->magic = SFCI_MAGIC; - raw->cmd_id = cmd_id; - - Result rc = serviceIpcDispatch(srv); - - if (R_SUCCEEDED(rc)) { - IpcParsedCommand r; - struct { - u64 magic; - u64 result; - AppletIdentityInfo info; - } *resp; - - serviceIpcParse(srv, &r, sizeof(*resp)); - resp = r.Raw; - - rc = resp->result; - - if (R_SUCCEEDED(rc) && info) memcpy(info, &resp->info, sizeof(AppletIdentityInfo)); - } - - return rc; +static Result _appletGetIdentityInfo(Service* srv, AppletIdentityInfo *info, u32 cmd_id) { + serviceAssumeDomain(srv); + return serviceDispatchOut(srv, cmd_id, *info); } -static Result _appletGetResolution(Service* srv, s32 *width, s32 *height, u64 cmd_id) { - IpcCommand c; - ipcInitialize(&c); - +static Result _appletGetResolution(Service* srv, s32 *width, s32 *height, u32 cmd_id) { struct { - u64 magic; - u64 cmd_id; - } *raw; - - raw = serviceIpcPrepareHeader(srv, &c, sizeof(*raw)); - - raw->magic = SFCI_MAGIC; - raw->cmd_id = cmd_id; - - Result rc = serviceIpcDispatch(srv); - - if (R_SUCCEEDED(rc)) { - IpcParsedCommand r; - struct { - u64 magic; - u64 result; - s32 width; - s32 height; - } *resp; - - serviceIpcParse(srv, &r, sizeof(*resp)); - resp = r.Raw; - - rc = resp->result; - - if (R_SUCCEEDED(rc)) { - if (width) *width = resp->width; - if (height) *height = resp->height; - } - } - - return rc; -} - -static Result _appletCreateMovieSession(Service* srv, Service* srv_out, u64 cmd_id, TransferMemory *tmem) { - IpcCommand c; - ipcInitialize(&c); - - ipcSendHandleCopy(&c, tmem->handle); - - struct { - u64 magic; - u64 cmd_id; - u64 size; - } *raw; - - raw = serviceIpcPrepareHeader(srv, &c, sizeof(*raw)); - - raw->magic = SFCI_MAGIC; - raw->cmd_id = cmd_id; - raw->size = tmem->size; - - Result rc = serviceIpcDispatch(srv); - - if (R_SUCCEEDED(rc)) { - IpcParsedCommand r; - struct { - u64 magic; - u64 result; - } *resp; - - serviceIpcParse(srv, &r, sizeof(*resp)); - resp = r.Raw; - - rc = resp->result; - - if (R_SUCCEEDED(rc) && srv_out) { - serviceCreateSubservice(srv_out, srv, &r, 0); - } - } + s32 width; + s32 height; + } out; + serviceAssumeDomain(srv); + Result rc = serviceDispatchOut(srv, cmd_id, out); + if (R_SUCCEEDED(rc) && width) *width = out.width; + if (R_SUCCEEDED(rc) && height) *height = out.height; return rc; } @@ -1384,7 +826,7 @@ static Result _appletGetCurrentFocusState(u8 *out) { } static Result _appletGetAcquiredSleepLockEvent(Event *out_event) { - return _appletGetEvent(&g_appletICommonStateGetter, out_event, 13, false); + return _appletGetEvent(&g_appletICommonStateGetter, out_event, false, 13); } static Result _appletWaitAcquiredSleepLockEvent(void) { @@ -1417,12 +859,12 @@ Result appletPushToGeneralChannel(AppletStorage *s) { return _appletCmdInStorage(&g_appletICommonStateGetter, s, 20); } -static Result _appletGetHomeButtonRwLockAccessor(Service* srv, AppletLockAccessor *a, u64 cmd_id) { +static Result _appletGetHomeButtonRwLockAccessor(Service* srv, AppletLockAccessor *a, u32 cmd_id) { Result rc = _appletGetSession(srv, &a->s, cmd_id); if (R_FAILED(rc)) return rc; - rc = _appletGetEvent(&a->s, &a->event, 3, false); + rc = _appletGetEvent(&a->s, &a->event, false, 3); if (R_FAILED(rc)) serviceClose(&a->s); return rc; } @@ -1431,12 +873,12 @@ Result appletGetHomeButtonReaderLockAccessor(AppletLockAccessor *a) { return _appletGetHomeButtonRwLockAccessor(&g_appletICommonStateGetter, a, 30); } -static Result _appletGetRwLockAccessor(Service* srv, AppletLockAccessor *a, u64 cmd_id, s32 inval) { - Result rc = _appletGetSessionIn32(srv, &a->s, cmd_id, inval); +static Result _appletGetRwLockAccessor(Service* srv, AppletLockAccessor *a, s32 inval, u32 cmd_id) { + Result rc = _appletGetSessionIn32(srv, &a->s, inval, cmd_id); if (R_FAILED(rc)) return rc; - rc = _appletGetEvent(&a->s, &a->event, 3, false); + rc = _appletGetEvent(&a->s, &a->event, false, 3); if (R_FAILED(rc)) serviceClose(&a->s); return rc; } @@ -1445,63 +887,39 @@ Result appletGetReaderLockAccessorEx(AppletLockAccessor *a, u32 inval) { if (hosversionBefore(2,0,0)) return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer); - return _appletGetRwLockAccessor(&g_appletICommonStateGetter, a, 31, inval); + return _appletGetRwLockAccessor(&g_appletICommonStateGetter, a, inval, 31); } Result appletGetWriterLockAccessorEx(AppletLockAccessor *a, u32 inval) { if (hosversionBefore(7,0,0)) { if (__nx_applet_type == AppletType_SystemApplet && hosversionAtLeast(2,0,0)) - return _appletGetRwLockAccessor(&g_appletIFunctions, a, 31, inval); + return _appletGetRwLockAccessor(&g_appletIFunctions, a, inval, 31); return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer); } - return _appletGetRwLockAccessor(&g_appletICommonStateGetter, a, 32, inval); + return _appletGetRwLockAccessor(&g_appletICommonStateGetter, a, inval, 32); } Result appletGetCradleFwVersion(u32 *out0, u32 *out1, u32 *out2, u32 *out3) { if (hosversionBefore(2,0,0)) return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer); - IpcCommand c; - ipcInitialize(&c); - struct { - u64 magic; - u64 cmd_id; - } *raw; - - raw = serviceIpcPrepareHeader(&g_appletICommonStateGetter, &c, sizeof(*raw)); - - raw->magic = SFCI_MAGIC; - raw->cmd_id = 40; - - Result rc = serviceIpcDispatch(&g_appletICommonStateGetter); + u32 out0; + u32 out1; + u32 out2; + u32 out3; + } out; + serviceAssumeDomain(&g_appletICommonStateGetter); + Result rc = serviceDispatchOut(&g_appletICommonStateGetter, 40, out); if (R_SUCCEEDED(rc)) { - IpcParsedCommand r; - struct { - u64 magic; - u64 result; - u32 out0; - u32 out1; - u32 out2; - u32 out3; - } *resp; - - serviceIpcParse(&g_appletICommonStateGetter, &r, sizeof(*resp)); - resp = r.Raw; - - rc = resp->result; - - if (R_SUCCEEDED(rc)) { - if (out0) *out0 = resp->out0; - if (out1) *out1 = resp->out1; - if (out2) *out2 = resp->out2; - if (out3) *out3 = resp->out3; - } + if (out0) *out0 = out.out0; + if (out1) *out1 = out.out1; + if (out2) *out2 = out.out2; + if (out3) *out3 = out.out3; } - return rc; } @@ -1547,7 +965,7 @@ Result appletGetDefaultDisplayResolutionChangeEvent(Event *out_event) { if (hosversionBefore(3,0,0)) return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer); - return _appletGetEvent(&g_appletICommonStateGetter, out_event, 61, true); + return _appletGetEvent(&g_appletICommonStateGetter, out_event, true, 61); } Result appletGetHdcpAuthenticationState(s32 *state) { @@ -1561,7 +979,7 @@ Result appletGetHdcpAuthenticationStateChangeEvent(Event *out_event) { if (hosversionBefore(4,0,0)) return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer); - return _appletGetEvent(&g_appletICommonStateGetter, out_event, 63, true); + return _appletGetEvent(&g_appletICommonStateGetter, out_event, true, 63); } Result appletSetTvPowerStateMatchingMode(AppletTvPowerStateMatchingMode mode) { @@ -1575,40 +993,11 @@ Result appletGetApplicationIdByContentActionName(u64 *titleID, const char *name) if (hosversionBefore(5,1,0)) return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer); - IpcCommand c; - ipcInitialize(&c); - - ipcAddSendBuffer(&c, name, strlen(name)+1, BufferType_Normal); - - struct { - u64 magic; - u64 cmd_id; - } *raw; - - raw = serviceIpcPrepareHeader(&g_appletICommonStateGetter, &c, sizeof(*raw)); - - raw->magic = SFCI_MAGIC; - raw->cmd_id = 65; - - Result rc = serviceIpcDispatch(&g_appletICommonStateGetter); - - if (R_SUCCEEDED(rc)) { - IpcParsedCommand r; - struct { - u64 magic; - u64 result; - u64 titleID; - } *resp; - - serviceIpcParse(&g_appletICommonStateGetter, &r, sizeof(*resp)); - resp = r.Raw; - - rc = resp->result; - - if (R_SUCCEEDED(rc) && titleID) *titleID = resp->titleID; - } - - return rc; + serviceAssumeDomain(&g_appletICommonStateGetter); + return serviceDispatchOut(&g_appletICommonStateGetter, 65, *titleID, + .buffer_attrs = { SfBufferAttr_HipcMapAlias | SfBufferAttr_In }, + .buffers = { { name, strlen(name)+1 } }, + ); } Result appletSetCpuBoostMode(ApmCpuBoostMode mode) { @@ -1675,7 +1064,7 @@ static Result _appletWaitLibraryAppletLaunchableEvent(void) { Result rc=0; if (!eventActive(&g_appletLibraryAppletLaunchableEvent)) - rc = _appletGetEvent(&g_appletISelfController, &g_appletLibraryAppletLaunchableEvent, 9, false); + rc = _appletGetEvent(&g_appletISelfController, &g_appletLibraryAppletLaunchableEvent, false, 9); if (R_SUCCEEDED(rc)) rc = eventWait(&g_appletLibraryAppletLaunchableEvent, U64_MAX); @@ -1695,41 +1084,14 @@ static Result _appletSetPerformanceModeChangedNotification(bool flag) { } static Result _appletSetFocusHandlingMode(bool inval0, bool inval1, bool inval2) { - IpcCommand c; - ipcInitialize(&c); - - struct { - u64 magic; - u64 cmd_id; + const struct { u8 inval0; u8 inval1; u8 inval2; - } *raw; + } in = { inval0!=0, inval1!=0, inval2!=0 }; - raw = serviceIpcPrepareHeader(&g_appletISelfController, &c, sizeof(*raw)); - - raw->magic = SFCI_MAGIC; - raw->cmd_id = 13; - raw->inval0 = inval0!=0; - raw->inval1 = inval1!=0; - raw->inval2 = inval2!=0; - - Result rc = serviceIpcDispatch(&g_appletISelfController); - - if (R_SUCCEEDED(rc)) { - IpcParsedCommand r; - struct { - u64 magic; - u64 result; - } *resp; - - serviceIpcParse(&g_appletISelfController, &r, sizeof(*resp)); - resp = r.Raw; - - rc = resp->result; - } - - return rc; + serviceAssumeDomain(&g_appletISelfController); + return serviceDispatchIn(&g_appletISelfController, 13, in); } Result appletSetRestartMessageEnabled(bool flag) { @@ -1740,37 +1102,8 @@ Result appletSetScreenShotAppletIdentityInfo(AppletIdentityInfo *info) { if (hosversionBefore(3,0,0)) return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer); - IpcCommand c; - ipcInitialize(&c); - - struct { - u64 magic; - u64 cmd_id; - AppletIdentityInfo info; - } *raw; - - raw = serviceIpcPrepareHeader(&g_appletISelfController, &c, sizeof(*raw)); - - raw->magic = SFCI_MAGIC; - raw->cmd_id = 15; - memcpy(&raw->info, info, sizeof(AppletIdentityInfo)); - - Result rc = serviceIpcDispatch(&g_appletISelfController); - - if (R_SUCCEEDED(rc)) { - IpcParsedCommand r; - struct { - u64 magic; - u64 result; - } *resp; - - serviceIpcParse(&g_appletISelfController, &r, sizeof(*resp)); - resp = r.Raw; - - rc = resp->result; - } - - return rc; + serviceAssumeDomain(&g_appletISelfController); + return serviceDispatchIn(&g_appletISelfController, 15, *info); } static Result _appletSetOutOfFocusSuspendingEnabled(bool flag) { @@ -1820,39 +1153,15 @@ Result appletGetSystemSharedLayerHandle(u64 *SharedBufferHandle, u64 *SharedLaye if (hosversionBefore(4,0,0)) return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer); - IpcCommand c; - ipcInitialize(&c); - struct { - u64 magic; - u64 cmd_id; - } *raw; - - raw = serviceIpcPrepareHeader(&g_appletISelfController, &c, sizeof(*raw)); - - raw->magic = SFCI_MAGIC; - raw->cmd_id = 42; - - Result rc = serviceIpcDispatch(&g_appletISelfController); - - if (R_SUCCEEDED(rc)) { - IpcParsedCommand r; - struct { - u64 magic; - u64 result; - u64 SharedBufferHandle; - u64 SharedLayerHandle; - } *resp; - - serviceIpcParse(&g_appletISelfController, &r, sizeof(*resp)); - resp = r.Raw; - - rc = resp->result; - - if (R_SUCCEEDED(rc) && SharedBufferHandle) *SharedBufferHandle = resp->SharedBufferHandle; - if (R_SUCCEEDED(rc) && SharedLayerHandle) *SharedLayerHandle = resp->SharedLayerHandle; - } + u64 SharedBufferHandle; + u64 SharedLayerHandle; + } out; + serviceAssumeDomain(&g_appletISelfController); + Result rc = serviceDispatchOut(&g_appletISelfController, 42, out); + if (R_SUCCEEDED(rc) && SharedBufferHandle) *SharedBufferHandle = out.SharedBufferHandle; + if (R_SUCCEEDED(rc) && SharedLayerHandle) *SharedLayerHandle = out.SharedLayerHandle; return rc; } @@ -1872,43 +1181,15 @@ Result appletApproveToDisplay(void) { } Result appletOverrideAutoSleepTimeAndDimmingTime(s32 inval0, s32 inval1, s32 inval2, s32 inval3) { - IpcCommand c; - ipcInitialize(&c); - - struct { - u64 magic; - u64 cmd_id; + const struct { s32 inval0; s32 inval1; s32 inval2; s32 inval3; - } *raw; + } in = { inval0, inval1, inval2, inval3 }; - raw = serviceIpcPrepareHeader(&g_appletISelfController, &c, sizeof(*raw)); - - raw->magic = SFCI_MAGIC; - raw->cmd_id = 60; - raw->inval0 = inval0; - raw->inval1 = inval1; - raw->inval2 = inval2; - raw->inval3 = inval3; - - Result rc = serviceIpcDispatch(&g_appletISelfController); - - if (R_SUCCEEDED(rc)) { - IpcParsedCommand r; - struct { - u64 magic; - u64 result; - } *resp; - - serviceIpcParse(&g_appletISelfController, &r, sizeof(*resp)); - resp = r.Raw; - - rc = resp->result; - } - - return rc; + serviceAssumeDomain(&g_appletISelfController); + return serviceDispatchIn(&g_appletISelfController, 60, in); } Result appletSetIdleTimeDetectionExtension(AppletIdleTimeDetectionExtension ext) { @@ -1934,38 +1215,8 @@ Result appletGetCurrentIlluminance(float *fLux) { if (hosversionBefore(3,0,0)) return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer); - IpcCommand c; - ipcInitialize(&c); - - struct { - u64 magic; - u64 cmd_id; - } *raw; - - raw = serviceIpcPrepareHeader(&g_appletISelfController, &c, sizeof(*raw)); - - raw->magic = SFCI_MAGIC; - raw->cmd_id = 66; - - Result rc = serviceIpcDispatch(&g_appletISelfController); - - if (R_SUCCEEDED(rc)) { - IpcParsedCommand r; - struct { - u64 magic; - u64 result; - float fLux; - } *resp; - - serviceIpcParse(&g_appletISelfController, &r, sizeof(*resp)); - resp = r.Raw; - - rc = resp->result; - - if (R_SUCCEEDED(rc) && fLux) *fLux = resp->fLux; - } - - return rc; + serviceAssumeDomain(&g_appletISelfController); + return serviceDispatchOut(&g_appletISelfController, 66, *fLux); } Result appletIsIlluminanceAvailable(bool *out) { @@ -1993,39 +1244,15 @@ Result appletGetCurrentIlluminanceEx(bool *bOverLimit, float *fLux) { if (hosversionBefore(5,0,0)) return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer); - IpcCommand c; - ipcInitialize(&c); - struct { - u64 magic; - u64 cmd_id; - } *raw; - - raw = serviceIpcPrepareHeader(&g_appletISelfController, &c, sizeof(*raw)); - - raw->magic = SFCI_MAGIC; - raw->cmd_id = 71; - - Result rc = serviceIpcDispatch(&g_appletISelfController); - - if (R_SUCCEEDED(rc)) { - IpcParsedCommand r; - struct { - u64 magic; - u64 result; - u8 bOverLimit; - float fLux; - } *resp; - - serviceIpcParse(&g_appletISelfController, &r, sizeof(*resp)); - resp = r.Raw; - - rc = resp->result; - - if (R_SUCCEEDED(rc) && bOverLimit) *bOverLimit = resp->bOverLimit!=0; - if (R_SUCCEEDED(rc) && fLux) *fLux = resp->fLux; - } + u8 bOverLimit; + float fLux; + } out; + serviceAssumeDomain(&g_appletISelfController); + Result rc = serviceDispatchOut(&g_appletISelfController, 71, out); + if (R_SUCCEEDED(rc) && bOverLimit) *bOverLimit = out.bOverLimit!=0; + if (R_SUCCEEDED(rc) && fLux) *fLux = out.fLux; return rc; } @@ -2054,7 +1281,7 @@ static Result _appletGetAccumulatedSuspendedTickChangedEvent(Event *out_event) { if (hosversionBefore(6,0,0)) return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer); - return _appletGetEvent(&g_appletISelfController, out_event, 91, true); + return _appletGetEvent(&g_appletISelfController, out_event, true, 91); } Result appletGetProgramTotalActiveTime(u64 *activeTime) { @@ -2080,37 +1307,11 @@ Result appletSetApplicationAlbumUserData(const void* buffer, size_t size) { if (hosversionBefore(8,0,0)) return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer); - IpcCommand c; - ipcInitialize(&c); - - ipcAddSendSmart(&c, g_appletISelfController.pointer_buffer_size, buffer, size, 0); - - struct { - u64 magic; - u64 cmd_id; - } *raw; - - raw = serviceIpcPrepareHeader(&g_appletISelfController, &c, sizeof(*raw)); - - raw->magic = SFCI_MAGIC; - raw->cmd_id = 110; - - Result rc = serviceIpcDispatch(&g_appletISelfController); - - if (R_SUCCEEDED(rc)) { - IpcParsedCommand r; - struct { - u64 magic; - u64 result; - } *resp; - - serviceIpcParse(&g_appletISelfController, &r, sizeof(*resp)); - resp = r.Raw; - - rc = resp->result; - } - - return rc; + serviceAssumeDomain(&g_appletISelfController); + return serviceDispatch(&g_appletISelfController, 110, + .buffer_attrs = { SfBufferAttr_HipcAutoSelect | SfBufferAttr_In }, + .buffers = { { buffer, size } }, + ); } // IWindowController @@ -2157,39 +1358,13 @@ Result appletSetAppletGpuTimeSlice(s64 val) { // IAudioController Result appletSetExpectedMasterVolume(float mainAppletVolume, float libraryAppletVolume) { - IpcCommand c; - ipcInitialize(&c); - - struct { - u64 magic; - u64 cmd_id; + const struct { float mainAppletVolume; float libraryAppletVolume; - } *raw; + } in = { mainAppletVolume, libraryAppletVolume }; - raw = serviceIpcPrepareHeader(&g_appletIAudioController, &c, sizeof(*raw)); - - raw->magic = SFCI_MAGIC; - raw->cmd_id = 0; - raw->mainAppletVolume = mainAppletVolume; - raw->libraryAppletVolume = libraryAppletVolume; - - Result rc = serviceIpcDispatch(&g_appletIAudioController); - - if (R_SUCCEEDED(rc)) { - IpcParsedCommand r; - struct { - u64 magic; - u64 result; - } *resp; - - serviceIpcParse(&g_appletIAudioController, &r, sizeof(*resp)); - resp = r.Raw; - - rc = resp->result; - } - - return rc; + serviceAssumeDomain(&g_appletIAudioController); + return serviceDispatchIn(&g_appletIAudioController, 0, in); } Result appletGetExpectedMasterVolume(float *mainAppletVolume, float *libraryAppletVolume) { @@ -2202,73 +1377,18 @@ Result appletGetExpectedMasterVolume(float *mainAppletVolume, float *libraryAppl } Result appletChangeMainAppletMasterVolume(float volume, u64 unk) { - IpcCommand c; - ipcInitialize(&c); - - struct { - u64 magic; - u64 cmd_id; + const struct { float volume; u64 unk; - } *raw; + } in = { volume, unk }; - raw = serviceIpcPrepareHeader(&g_appletIAudioController, &c, sizeof(*raw)); - - raw->magic = SFCI_MAGIC; - raw->cmd_id = 3; - raw->volume = volume; - raw->unk = unk; - - Result rc = serviceIpcDispatch(&g_appletIAudioController); - - if (R_SUCCEEDED(rc)) { - IpcParsedCommand r; - struct { - u64 magic; - u64 result; - } *resp; - - serviceIpcParse(&g_appletIAudioController, &r, sizeof(*resp)); - resp = r.Raw; - - rc = resp->result; - } - - return rc; + serviceAssumeDomain(&g_appletIAudioController); + return serviceDispatchIn(&g_appletIAudioController, 3, in); } Result appletSetTransparentVolumeRate(float val) { - IpcCommand c; - ipcInitialize(&c); - - struct { - u64 magic; - u64 cmd_id; - float val; - } *raw; - - raw = serviceIpcPrepareHeader(&g_appletIAudioController, &c, sizeof(*raw)); - - raw->magic = SFCI_MAGIC; - raw->cmd_id = 4; - raw->val = val; - - Result rc = serviceIpcDispatch(&g_appletIAudioController); - - if (R_SUCCEEDED(rc)) { - IpcParsedCommand r; - struct { - u64 magic; - u64 result; - } *resp; - - serviceIpcParse(&g_appletIAudioController, &r, sizeof(*resp)); - resp = r.Raw; - - rc = resp->result; - } - - return rc; + serviceAssumeDomain(&g_appletIAudioController); + return serviceDispatchIn(&g_appletIAudioController, 4, val); } // IDisplayController @@ -2281,40 +1401,14 @@ Result appletUpdateCallerAppletCaptureImage(void) { return _appletCmdNoIO(&g_appletIDisplayController, 4); } -static Result _appletGetCaptureImageEx(void* buffer, size_t size, bool *flag, u64 cmd_id) { - IpcCommand c; - ipcInitialize(&c); - - ipcAddRecvBuffer(&c, buffer, size, BufferType_Normal); - - struct { - u64 magic; - u64 cmd_id; - } *raw; - - raw = serviceIpcPrepareHeader(&g_appletIDisplayController, &c, sizeof(*raw)); - - raw->magic = SFCI_MAGIC; - raw->cmd_id = cmd_id; - - Result rc = serviceIpcDispatch(&g_appletIDisplayController); - - if (R_SUCCEEDED(rc)) { - IpcParsedCommand r; - struct { - u64 magic; - u64 result; - u8 flag; - } *resp; - - serviceIpcParse(&g_appletIDisplayController, &r, sizeof(*resp)); - resp = r.Raw; - - rc = resp->result; - - if (R_SUCCEEDED(rc) && flag) *flag = resp->flag!=0; - } - +static Result _appletGetCaptureImageEx(void* buffer, size_t size, bool *flag, u32 cmd_id) { + u8 tmp=0; + serviceAssumeDomain(&g_appletIDisplayController); + Result rc = serviceDispatchOut(&g_appletIDisplayController, cmd_id, tmp, + .buffer_attrs = { SfBufferAttr_HipcMapAlias | SfBufferAttr_Out }, + .buffers = { { buffer, size } }, + ); + if (R_SUCCEEDED(rc) && flag) *flag = tmp!=0; return rc; } @@ -2334,119 +1428,40 @@ Result appletTakeScreenShotOfOwnLayer(bool flag, AppletCaptureSharedBuffer captu if (hosversionBefore(2,0,0)) return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer); - IpcCommand c; - ipcInitialize(&c); - - struct { - u64 magic; - u64 cmd_id; + const struct { u8 flag; s32 captureBuf; - } *raw; + } in = { flag!=0, captureBuf }; - raw = serviceIpcPrepareHeader(&g_appletIDisplayController, &c, sizeof(*raw)); - - raw->magic = SFCI_MAGIC; - raw->cmd_id = 8; - raw->flag = flag!=0; - raw->captureBuf = captureBuf; - - Result rc = serviceIpcDispatch(&g_appletIDisplayController); - - if (R_SUCCEEDED(rc)) { - IpcParsedCommand r; - struct { - u64 magic; - u64 result; - } *resp; - - serviceIpcParse(&g_appletIDisplayController, &r, sizeof(*resp)); - resp = r.Raw; - - rc = resp->result; - } - - return rc; + serviceAssumeDomain(&g_appletIDisplayController); + return serviceDispatchIn(&g_appletIDisplayController, 8, in); } Result appletCopyBetweenCaptureBuffers(AppletCaptureSharedBuffer dstCaptureBuf, AppletCaptureSharedBuffer srcCaptureBuf) { if (hosversionBefore(5,0,0)) return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer); - IpcCommand c; - ipcInitialize(&c); - - struct { - u64 magic; - u64 cmd_id; + const struct { s32 dstCaptureBuf; s32 srcCaptureBuf; - } *raw; + } in = { dstCaptureBuf, srcCaptureBuf }; - raw = serviceIpcPrepareHeader(&g_appletIDisplayController, &c, sizeof(*raw)); - - raw->magic = SFCI_MAGIC; - raw->cmd_id = 9; - raw->dstCaptureBuf = dstCaptureBuf; - raw->srcCaptureBuf = srcCaptureBuf; - - Result rc = serviceIpcDispatch(&g_appletIDisplayController); - - if (R_SUCCEEDED(rc)) { - IpcParsedCommand r; - struct { - u64 magic; - u64 result; - } *resp; - - serviceIpcParse(&g_appletIDisplayController, &r, sizeof(*resp)); - resp = r.Raw; - - rc = resp->result; - } - - return rc; + serviceAssumeDomain(&g_appletIDisplayController); + return serviceDispatchIn(&g_appletIDisplayController, 9, in); } Result appletClearCaptureBuffer(bool flag, AppletCaptureSharedBuffer captureBuf, u32 color) { if (hosversionBefore(3,0,0)) return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer); - IpcCommand c; - ipcInitialize(&c); - - struct { - u64 magic; - u64 cmd_id; + const struct { u8 flag; s32 captureBuf; u32 color; - } *raw; + } in = { flag!=0, captureBuf, color }; - raw = serviceIpcPrepareHeader(&g_appletIDisplayController, &c, sizeof(*raw)); - - raw->magic = SFCI_MAGIC; - raw->cmd_id = 20; - raw->flag = flag!=0; - raw->captureBuf = captureBuf; - raw->color = color; - - Result rc = serviceIpcDispatch(&g_appletIDisplayController); - - if (R_SUCCEEDED(rc)) { - IpcParsedCommand r; - struct { - u64 magic; - u64 result; - } *resp; - - serviceIpcParse(&g_appletIDisplayController, &r, sizeof(*resp)); - resp = r.Raw; - - rc = resp->result; - } - - return rc; + serviceAssumeDomain(&g_appletIDisplayController); + return serviceDispatchIn(&g_appletIDisplayController, 20, in); } Result appletClearAppletTransitionBuffer(u32 color) { @@ -2457,39 +1472,15 @@ Result appletClearAppletTransitionBuffer(u32 color) { } static Result _appletAcquireCaptureSharedBuffer(bool *flag, s32 *id, u64 cmd_id) { - IpcCommand c; - ipcInitialize(&c); - struct { - u64 magic; - u64 cmd_id; - } *raw; - - raw = serviceIpcPrepareHeader(&g_appletIDisplayController, &c, sizeof(*raw)); - - raw->magic = SFCI_MAGIC; - raw->cmd_id = cmd_id; - - Result rc = serviceIpcDispatch(&g_appletIDisplayController); - - if (R_SUCCEEDED(rc)) { - IpcParsedCommand r; - struct { - u64 magic; - u64 result; - u8 flag; - s32 id; - } *resp; - - serviceIpcParse(&g_appletIDisplayController, &r, sizeof(*resp)); - resp = r.Raw; - - rc = resp->result; - - if (R_SUCCEEDED(rc) && flag) *flag = resp->flag!=0; - if (R_SUCCEEDED(rc) && id) *id = resp->id; - } + u8 flag; + s32 id; + } out; + serviceAssumeDomain(&g_appletIDisplayController); + Result rc = serviceDispatchOut(&g_appletIDisplayController, cmd_id, out); + if (R_SUCCEEDED(rc) && flag) *flag = out.flag!=0; + if (R_SUCCEEDED(rc) && id) *id = out.id; return rc; } @@ -2539,41 +1530,14 @@ Result appletTakeScreenShotOfOwnLayerEx(bool flag0, bool immediately, AppletCapt if (hosversionBefore(6,0,0)) return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer); - IpcCommand c; - ipcInitialize(&c); - - struct { - u64 magic; - u64 cmd_id; + const struct { u8 flag0; u8 immediately; s32 captureBuf; - } *raw; + } in = { flag0!=0, immediately!=0, captureBuf }; - raw = serviceIpcPrepareHeader(&g_appletIDisplayController, &c, sizeof(*raw)); - - raw->magic = SFCI_MAGIC; - raw->cmd_id = 28; - raw->flag0 = flag0!=0; - raw->immediately = immediately!=0; - raw->captureBuf = captureBuf; - - Result rc = serviceIpcDispatch(&g_appletIDisplayController); - - if (R_SUCCEEDED(rc)) { - IpcParsedCommand r; - struct { - u64 magic; - u64 result; - } *resp; - - serviceIpcParse(&g_appletIDisplayController, &r, sizeof(*resp)); - resp = r.Raw; - - rc = resp->result; - } - - return rc; + serviceAssumeDomain(&g_appletIDisplayController); + return serviceDispatchIn(&g_appletIDisplayController, 28, in); } // IProcessWindingController @@ -2582,38 +1546,8 @@ static Result _appletGetLaunchReason(AppletProcessLaunchReason *reason) { if (__nx_applet_type != AppletType_LibraryApplet) return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); - IpcCommand c; - ipcInitialize(&c); - - struct { - u64 magic; - u64 cmd_id; - } *raw; - - raw = serviceIpcPrepareHeader(&g_appletIProcessWindingController, &c, sizeof(*raw)); - - raw->magic = SFCI_MAGIC; - raw->cmd_id = 0; - - Result rc = serviceIpcDispatch(&g_appletIProcessWindingController); - - if (R_SUCCEEDED(rc)) { - IpcParsedCommand r; - struct { - u64 magic; - u64 result; - AppletProcessLaunchReason reason; - } *resp; - - serviceIpcParse(&g_appletIProcessWindingController, &r, sizeof(*resp)); - resp = r.Raw; - - rc = resp->result; - - if (R_SUCCEEDED(rc) && reason) *reason = resp->reason; - } - - return rc; + serviceAssumeDomain(&g_appletIProcessWindingController); + return serviceDispatchOut(&g_appletIProcessWindingController, 0, *reason); } static Result _appletOpenCallingLibraryApplet(AppletHolder *h) { @@ -2667,42 +1601,16 @@ void appletLockAccessorClose(AppletLockAccessor *a) { } static Result _appletLockAccessorTryLock(AppletLockAccessor *a, bool get_handle, Handle* handle_out, bool *outflag) { - IpcCommand c; - ipcInitialize(&c); - - struct { - u64 magic; - u64 cmd_id; - u8 inflag; - } *raw; - - raw = serviceIpcPrepareHeader(&a->s, &c, sizeof(*raw)); - - raw->magic = SFCI_MAGIC; - raw->cmd_id = 1; - raw->inflag = get_handle!=0; - - Result rc = serviceIpcDispatch(&a->s); - - if (R_SUCCEEDED(rc)) { - IpcParsedCommand r; - struct { - u64 magic; - u64 result; - u8 outflag; - } *resp; - - serviceIpcParse(&a->s, &r, sizeof(*resp)); - resp = r.Raw; - - rc = resp->result; - - if (R_SUCCEEDED(rc)) { - if (handle_out) *handle_out = r.Handles[0]; - if (outflag) *outflag = resp->outflag!=0; - } - } - + Handle tmphandle = INVALID_HANDLE; + u8 tmp = get_handle!=0; + u8 tmpout=0; + serviceAssumeDomain(&a->s); + Result rc = serviceDispatchInOut(&a->s, 1, tmp, tmpout, + .out_handle_attrs = { SfOutHandleAttr_HipcCopy }, + .out_handles = &tmphandle, + ); + if (R_SUCCEEDED(rc) && outflag) *outflag = tmpout!=0; + if (R_SUCCEEDED(rc) && handle_out) *handle_out = tmphandle; return rc; } @@ -2743,43 +1651,16 @@ Result appletLockAccessorUnlock(AppletLockAccessor *a) { // ILibraryAppletCreator static Result _appletCreateLibraryApplet(Service* srv_out, AppletId id, LibAppletMode mode) { - IpcCommand c; - ipcInitialize(&c); - - struct { - u64 magic; - u64 cmd_id; + const struct { u32 id; u32 mode; - } *raw; + } in = { id, mode }; - raw = serviceIpcPrepareHeader(&g_appletILibraryAppletCreator, &c, sizeof(*raw)); - - raw->magic = SFCI_MAGIC; - raw->cmd_id = 0; - raw->id = id; - raw->mode = mode; - - Result rc = serviceIpcDispatch(&g_appletILibraryAppletCreator); - - if (R_SUCCEEDED(rc)) { - IpcParsedCommand r; - struct { - u64 magic; - u64 result; - } *resp; - - serviceIpcParse(&g_appletILibraryAppletCreator, &r, sizeof(*resp)); - resp = r.Raw; - - rc = resp->result; - - if (R_SUCCEEDED(rc) && srv_out) { - serviceCreateSubservice(srv_out, &g_appletILibraryAppletCreator, &r, 0); - } - } - - return rc; + serviceAssumeDomain(&g_appletILibraryAppletCreator); + return serviceDispatchIn(&g_appletILibraryAppletCreator, 0, in, + .out_num_objects = 1, + .out_objects = srv_out, + ); } static Result _appletGetIndirectLayerConsumerHandle(Service* srv, u64 *out) { @@ -2789,42 +1670,10 @@ static Result _appletGetIndirectLayerConsumerHandle(Service* srv, u64 *out) { rc = appletGetAppletResourceUserId(&AppletResourceUserId); if (R_FAILED(rc)) return rc; - IpcCommand c; - ipcInitialize(&c); - - struct { - u64 magic; - u64 cmd_id; - u64 AppletResourceUserId; - } *raw; - - ipcSendPid(&c); - - raw = serviceIpcPrepareHeader(srv, &c, sizeof(*raw)); - - raw->magic = SFCI_MAGIC; - raw->cmd_id = 160; - raw->AppletResourceUserId = AppletResourceUserId; - - rc = serviceIpcDispatch(srv); - - if (R_SUCCEEDED(rc)) { - IpcParsedCommand r; - struct { - u64 magic; - u64 result; - u64 out; - } *resp; - - serviceIpcParse(srv, &r, sizeof(*resp)); - resp = r.Raw; - - rc = resp->result; - - if (R_SUCCEEDED(rc) && out) *out = resp->out; - } - - return rc; + serviceAssumeDomain(srv); + return serviceDispatchInOut(srv, 160, AppletResourceUserId, *out, + .in_send_pid = true, + ); } static Result _appletHolderCreateState(AppletHolder *h, LibAppletMode mode, bool creating_self) { @@ -2833,7 +1682,7 @@ static Result _appletHolderCreateState(AppletHolder *h, LibAppletMode mode, bool h->mode = mode; h->creating_self = creating_self; - if (R_SUCCEEDED(rc)) rc = _appletGetEvent(&h->s, &h->StateChangedEvent, 0, false);//GetAppletStateChangedEvent + if (R_SUCCEEDED(rc)) rc = _appletGetEvent(&h->s, &h->StateChangedEvent, false, 0);//GetAppletStateChangedEvent if (R_SUCCEEDED(rc) && hosversionAtLeast(2,0,0) && h->mode == LibAppletMode_BackgroundIndirect) rc = _appletGetIndirectLayerConsumerHandle(&h->s, &h->layer_handle); @@ -2854,7 +1703,7 @@ static Result _appletHolderCreate(AppletHolder *h, AppletId id, LibAppletMode mo return rc; } -static Result _appletOpenExistingLibraryApplet(AppletHolder *h, Service* srv, u64 cmd_id) { +static Result _appletOpenExistingLibraryApplet(AppletHolder *h, Service* srv, u32 cmd_id) { if (__nx_applet_type != AppletType_LibraryApplet) return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); @@ -2962,14 +1811,10 @@ Result appletHolderRequestExit(AppletHolder *h) { } Result appletHolderTerminate(AppletHolder *h) { - Result rc=0; - if (!serviceIsActive(&h->s)) return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); - rc = _appletCmdNoIO(&h->s, 25);//Terminate - - return rc; + return _appletCmdNoIO(&h->s, 25);//Terminate } static Result _appletAccessorRequestExitOrTerminate(Service* srv, u64 timeout) { @@ -2979,7 +1824,7 @@ static Result _appletAccessorRequestExitOrTerminate(Service* srv, u64 timeout) { if (!serviceIsActive(srv)) return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); - rc = _appletGetEvent(srv, &StateChangedEvent, 0, false);//GetAppletStateChangedEvent + rc = _appletGetEvent(srv, &StateChangedEvent, false, 0);//GetAppletStateChangedEvent if (R_SUCCEEDED(rc)) rc = _appletCmdNoIO(srv, 20);//RequestExit @@ -3037,13 +1882,9 @@ Result appletHolderSetOutOfFocusApplicationSuspendingEnabled(AppletHolder *h, bo } static Result _appletHolderGetPopInteractiveOutDataEvent(AppletHolder *h) { - Result rc=0; - if (eventActive(&h->PopInteractiveOutDataEvent)) return 0; - rc = _appletGetEvent(&h->s, &h->PopInteractiveOutDataEvent, 106, false); - - return rc; + return _appletGetEvent(&h->s, &h->PopInteractiveOutDataEvent, false, 106); } bool appletHolderWaitInteractiveOut(AppletHolder *h) { @@ -3088,49 +1929,22 @@ Result appletHolderGetLibraryAppletInfo(AppletHolder *h, LibAppletInfo *info) { Result appletCreateStorage(AppletStorage *s, s64 size) { memset(s, 0, sizeof(AppletStorage)); - return _appletGetSessionIn64(&g_appletILibraryAppletCreator, &s->s, 10, size); + return _appletGetSessionIn64(&g_appletILibraryAppletCreator, &s->s, size, 10); } static Result _appletCreateTransferMemoryStorage(Service* srv_out, TransferMemory *tmem, bool writable) { - IpcCommand c; - ipcInitialize(&c); - - ipcSendHandleCopy(&c, tmem->handle); - - struct { - u64 magic; - u64 cmd_id; + const struct { u8 writable; u64 size; - } *raw; + } in = { writable!=0, tmem->size }; - raw = serviceIpcPrepareHeader(&g_appletILibraryAppletCreator, &c, sizeof(*raw)); - - raw->magic = SFCI_MAGIC; - raw->cmd_id = 11; - raw->writable = writable!=0; - raw->size = tmem->size; - - Result rc = serviceIpcDispatch(&g_appletILibraryAppletCreator); - - if (R_SUCCEEDED(rc)) { - IpcParsedCommand r; - struct { - u64 magic; - u64 result; - } *resp; - - serviceIpcParse(&g_appletILibraryAppletCreator, &r, sizeof(*resp)); - resp = r.Raw; - - rc = resp->result; - - if (R_SUCCEEDED(rc) && srv_out) { - serviceCreateSubservice(srv_out, &g_appletILibraryAppletCreator, &r, 0); - } - } - - return rc; + serviceAssumeDomain(&g_appletILibraryAppletCreator); + return serviceDispatchIn(&g_appletILibraryAppletCreator, 11, in, + .in_num_handles = 1, + .in_handles = { tmem->handle }, + .out_num_objects = 1, + .out_objects = srv_out, + ); } Result appletCreateTransferMemoryStorage(AppletStorage *s, void* buffer, s64 size, bool writable) { @@ -3152,7 +1966,7 @@ Result appletCreateHandleStorage(AppletStorage *s, s64 inval, Handle handle) { if (hosversionBefore(2,0,0)) return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer); - return _appletCmdInHandle64(&g_appletILibraryAppletCreator, &s->s, 12, handle, inval); + return _appletCmdInHandle64OutSession(&g_appletILibraryAppletCreator, &s->s, handle, inval, 12); } Result appletCreateHandleStorageTmem(AppletStorage *s, void* buffer, s64 size) { @@ -3179,40 +1993,11 @@ void appletStorageCloseTmem(AppletStorage *s) { } static Result _appletStorageAccessorRW(Service* srv, size_t ipcbufsize, s64 offset, void* buffer, size_t size, bool rw) { - IpcCommand c; - ipcInitialize(&c); - - if(!rw)ipcAddRecvSmart(&c, ipcbufsize, buffer, size, 0); - if(rw)ipcAddSendSmart(&c, ipcbufsize, buffer, size, 0); - - struct { - u64 magic; - u64 cmd_id; - s64 offset; - } *raw; - - raw = serviceIpcPrepareHeader(srv, &c, sizeof(*raw)); - - raw->magic = SFCI_MAGIC; - raw->cmd_id = rw!=0 ? 10 : 11; - raw->offset = offset; - - Result rc = serviceIpcDispatch(srv); - - if (R_SUCCEEDED(rc)) { - IpcParsedCommand r; - struct { - u64 magic; - u64 result; - } *resp; - - serviceIpcParse(srv, &r, sizeof(*resp)); - resp = r.Raw; - - rc = resp->result; - } - - return rc; + serviceAssumeDomain(srv); + return serviceDispatchIn(srv, rw ? 10 : 11, offset, + .buffer_attrs = { SfBufferAttr_HipcAutoSelect | (rw ? SfBufferAttr_In : SfBufferAttr_Out) }, + .buffers = { { buffer, size } }, + ); } Result appletStorageGetSize(AppletStorage *s, s64 *size) { @@ -3256,40 +2041,13 @@ Result appletStorageRead(AppletStorage *s, s64 offset, void* buffer, size_t size } static Result _appletStorageGetHandle(Service* srv, s64 *out, Handle *handle) { - IpcCommand c; - ipcInitialize(&c); - - struct { - u64 magic; - u64 cmd_id; - } *raw; - - raw = serviceIpcPrepareHeader(srv, &c, sizeof(*raw)); - - raw->magic = SFCI_MAGIC; - raw->cmd_id = 1; - - Result rc = serviceIpcDispatch(srv); - - if (R_SUCCEEDED(rc)) { - IpcParsedCommand r; - struct { - u64 magic; - u64 result; - s64 out; - } *resp; - - serviceIpcParse(srv, &r, sizeof(*resp)); - resp = r.Raw; - - rc = resp->result; - - if (R_SUCCEEDED(rc)) { - if (out) *out = resp->out; - if (handle) *handle = r.Handles[0]; - } - } - + Handle tmphandle = INVALID_HANDLE; + serviceAssumeDomain(srv); + Result rc = serviceDispatchOut(srv, 1, *out, + .out_handle_attrs = { SfOutHandleAttr_HipcCopy }, + .out_handles = &tmphandle, + ); + if (R_SUCCEEDED(rc) && handle) *handle = tmphandle; return rc; } @@ -3335,120 +2093,37 @@ Result appletStorageMap(AppletStorage *s, void** addr, size_t *size) { // IApplicationFunctions Result appletPopLaunchParameter(AppletStorage *s, AppletLaunchParameterKind kind) { - IpcCommand c; - ipcInitialize(&c); - if (!serviceIsActive(&g_appletSrv) || !_appletIsApplication()) return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); - struct { - u64 magic; - u64 cmd_id; - u32 kind; - } *raw; - - raw = serviceIpcPrepareHeader(&g_appletIFunctions, &c, sizeof(*raw)); - - raw->magic = SFCI_MAGIC; - raw->cmd_id = 1; - raw->kind = kind; - - Result rc = serviceIpcDispatch(&g_appletIFunctions); - - if (R_SUCCEEDED(rc)) { - IpcParsedCommand r; - struct { - u64 magic; - u64 result; - } *resp; - - serviceIpcParse(&g_appletIFunctions, &r, sizeof(*resp)); - resp = r.Raw; - - rc = resp->result; - - if (R_SUCCEEDED(rc)) { - memset(s, 0, sizeof(AppletStorage)); - serviceCreateSubservice(&s->s, &g_appletIFunctions, &r, 0); - } - } - - return rc; + u32 tmp=kind; + memset(s, 0, sizeof(AppletStorage)); + serviceAssumeDomain(&g_appletIFunctions); + return serviceDispatchIn(&g_appletIFunctions, 1, tmp, + .out_num_objects = 1, + .out_objects = &s->s, + ); } -static Result _appletCreateApplicationAndPushAndRequestToStart(Service* srv, u64 cmd_id, u64 titleID, AppletStorage* s) { - IpcCommand c; - ipcInitialize(&c); - - serviceSendObject(&s->s, &c); - - struct { - u64 magic; - u64 cmd_id; - u64 titleID; - } *raw; - - raw = serviceIpcPrepareHeader(srv, &c, sizeof(*raw)); - - raw->magic = SFCI_MAGIC; - raw->cmd_id = cmd_id; - raw->titleID = titleID; - - Result rc = serviceIpcDispatch(srv); - - if (R_SUCCEEDED(rc)) { - IpcParsedCommand r; - struct { - u64 magic; - u64 result; - } *resp; - - serviceIpcParse(srv, &r, sizeof(*resp)); - resp = r.Raw; - - rc = resp->result; - } - - return rc; +static Result _appletCreateApplicationAndPushAndRequestToStart(Service* srv, u64 titleID, AppletStorage* s, u32 cmd_id) { + serviceAssumeDomain(srv); + return serviceDispatchIn(srv, cmd_id, titleID, + .in_num_objects = 1, + .in_objects = { &s->s }, + ); } static Result _appletCreateApplicationAndPushAndRequestToStartForQuest(u64 titleID, AppletStorage* s, const AppletApplicationAttributeForQuest *attr) { // [2.0.0+] - IpcCommand c; - ipcInitialize(&c); - - serviceSendObject(&s->s, &c); - - struct { - u64 magic; - u64 cmd_id; + const struct { u32 val0, val1; u64 titleID; - } PACKED *raw; + } in = { attr->unk_x0, attr->unk_x4, titleID }; - raw = serviceIpcPrepareHeader(&g_appletIFunctions, &c, sizeof(*raw)); - - raw->magic = SFCI_MAGIC; - raw->cmd_id = 11; - raw->val0 = attr->unk_x0; - raw->val1 = attr->unk_x4; - raw->titleID = titleID; - - Result rc = serviceIpcDispatch(&g_appletIFunctions); - - if (R_SUCCEEDED(rc)) { - IpcParsedCommand r; - struct { - u64 magic; - u64 result; - } *resp; - - serviceIpcParse(&g_appletIFunctions, &r, sizeof(*resp)); - resp = r.Raw; - - rc = resp->result; - } - - return rc; + serviceAssumeDomain(&g_appletIFunctions); + return serviceDispatchIn(&g_appletIFunctions, 11, in, + .in_num_objects = 1, + .in_objects = { &s->s }, + ); } static Result _appletCreateApplicationAndRequestToStart(u64 titleID) { // [4.0.0+] @@ -3456,114 +2131,31 @@ static Result _appletCreateApplicationAndRequestToStart(u64 titleID) { // [4.0.0 } static Result _appletCreateApplicationAndRequestToStartForQuest(u64 titleID, const AppletApplicationAttributeForQuest *attr) { // [4.0.0+] - IpcCommand c; - ipcInitialize(&c); - - struct { - u64 magic; - u64 cmd_id; + const struct { u32 val0, val1; u64 titleID; - } PACKED *raw; + } in = { attr->unk_x0, attr->unk_x4, titleID }; - raw = serviceIpcPrepareHeader(&g_appletIFunctions, &c, sizeof(*raw)); - - raw->magic = SFCI_MAGIC; - raw->cmd_id = 13; - raw->val0 = attr->unk_x0; - raw->val1 = attr->unk_x4; - raw->titleID = titleID; - - Result rc = serviceIpcDispatch(&g_appletIFunctions); - - if (R_SUCCEEDED(rc)) { - IpcParsedCommand r; - struct { - u64 magic; - u64 result; - } *resp; - - serviceIpcParse(&g_appletIFunctions, &r, sizeof(*resp)); - resp = r.Raw; - - rc = resp->result; - } - - return rc; + serviceAssumeDomain(&g_appletIFunctions); + return serviceDispatchIn(&g_appletIFunctions, 13, in); } static Result _appletCreateApplicationWithAttributeAndPushAndRequestToStartForQuest(u64 titleID, AppletStorage* s, const AppletApplicationAttribute *attr) { // [7.0.0+] - IpcCommand c; - ipcInitialize(&c); - - serviceSendObject(&s->s, &c); - - ipcAddSendBuffer(&c, attr, sizeof(*attr), BufferType_Normal); - - struct { - u64 magic; - u64 cmd_id; - u64 titleID; - } *raw; - - raw = serviceIpcPrepareHeader(&g_appletIFunctions, &c, sizeof(*raw)); - - raw->magic = SFCI_MAGIC; - raw->cmd_id = 14; - raw->titleID = titleID; - - Result rc = serviceIpcDispatch(&g_appletIFunctions); - - if (R_SUCCEEDED(rc)) { - IpcParsedCommand r; - struct { - u64 magic; - u64 result; - } *resp; - - serviceIpcParse(&g_appletIFunctions, &r, sizeof(*resp)); - resp = r.Raw; - - rc = resp->result; - } - - return rc; + serviceAssumeDomain(&g_appletIFunctions); + return serviceDispatchIn(&g_appletIFunctions, 14, titleID, + .buffer_attrs = { SfBufferAttr_HipcMapAlias | SfBufferAttr_In }, + .buffers = { { attr, sizeof(*attr) } }, + .in_num_objects = 1, + .in_objects = { &s->s }, + ); } static Result _appletCreateApplicationWithAttributeAndRequestToStartForQuest(u64 titleID, const AppletApplicationAttribute *attr) { // [7.0.0+] - IpcCommand c; - ipcInitialize(&c); - - ipcAddSendBuffer(&c, attr, sizeof(*attr), BufferType_Normal); - - struct { - u64 magic; - u64 cmd_id; - u64 titleID; - } *raw; - - raw = serviceIpcPrepareHeader(&g_appletIFunctions, &c, sizeof(*raw)); - - raw->magic = SFCI_MAGIC; - raw->cmd_id = 15; - raw->titleID = titleID; - - Result rc = serviceIpcDispatch(&g_appletIFunctions); - - if (R_SUCCEEDED(rc)) { - IpcParsedCommand r; - struct { - u64 magic; - u64 result; - } *resp; - - serviceIpcParse(&g_appletIFunctions, &r, sizeof(*resp)); - resp = r.Raw; - - rc = resp->result; - } - - return rc; + serviceAssumeDomain(&g_appletIFunctions); + return serviceDispatchIn(&g_appletIFunctions, 15, titleID, + .buffer_attrs = { SfBufferAttr_HipcMapAlias | SfBufferAttr_In }, + .buffers = { { attr, sizeof(*attr) } }, + ); } Result appletRequestLaunchApplication(u64 titleID, AppletStorage* s) { @@ -3583,14 +2175,14 @@ Result appletRequestLaunchApplication(u64 titleID, AppletStorage* s) { } if (is_libraryapplet) { - rc = _appletCreateApplicationAndPushAndRequestToStart(&g_appletILibraryAppletSelfAccessor, 90, titleID, s); + rc = _appletCreateApplicationAndPushAndRequestToStart(&g_appletILibraryAppletSelfAccessor, titleID, s, 90); } else { if (hosversionAtLeast(4,0,0) && s==NULL) { rc = _appletCreateApplicationAndRequestToStart(titleID); } else { - rc = _appletCreateApplicationAndPushAndRequestToStart(&g_appletIFunctions, 10, titleID, s); + rc = _appletCreateApplicationAndPushAndRequestToStart(&g_appletIFunctions, titleID, s, 10); } } @@ -3650,82 +2242,26 @@ Result appletGetDesiredLanguage(u64 *LanguageCode) { } Result appletSetTerminateResult(Result res) { - IpcCommand c; - ipcInitialize(&c); - if (!serviceIsActive(&g_appletSrv) || !_appletIsApplication()) return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); - struct { - u64 magic; - u64 cmd_id; - Result res; - } *raw; - - raw = serviceIpcPrepareHeader(&g_appletIFunctions, &c, sizeof(*raw)); - - raw->magic = SFCI_MAGIC; - raw->cmd_id = 22; - raw->res = res; - - Result rc = serviceIpcDispatch(&g_appletIFunctions); - - if (R_SUCCEEDED(rc)) { - IpcParsedCommand r; - struct { - u64 magic; - u64 result; - } *resp; - - serviceIpcParse(&g_appletIFunctions, &r, sizeof(*resp)); - resp = r.Raw; - - rc = resp->result; - } - - return rc; + return _appletCmdInU32(&g_appletIFunctions, res, 22); } Result appletGetDisplayVersion(char *displayVersion) { - IpcCommand c; - ipcInitialize(&c); + char out[0x10]={0}; if (displayVersion) memset(displayVersion, 0, 0x10); if (!serviceIsActive(&g_appletSrv) || !_appletIsApplication()) return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); - struct { - u64 magic; - u64 cmd_id; - } *raw; - - raw = serviceIpcPrepareHeader(&g_appletIFunctions, &c, sizeof(*raw)); - - raw->magic = SFCI_MAGIC; - raw->cmd_id = 23; - - Result rc = serviceIpcDispatch(&g_appletIFunctions); - - if (R_SUCCEEDED(rc)) { - IpcParsedCommand r; - struct { - u64 magic; - u64 result; - char displayVersion[0x10]; - } *resp; - - serviceIpcParse(&g_appletIFunctions, &r, sizeof(*resp)); - resp = r.Raw; - - rc = resp->result; - - if (R_SUCCEEDED(rc) && displayVersion) { - strncpy(displayVersion, resp->displayVersion, 0x10); - displayVersion[0xf] = 0; - } + serviceAssumeDomain(&g_appletIFunctions); + Result rc = serviceDispatchOut(&g_appletIFunctions, 23, out); + if (R_SUCCEEDED(rc) && displayVersion) { + strncpy(displayVersion, out, 0x10); + displayVersion[0xf] = 0; } - return rc; } @@ -3735,39 +2271,15 @@ Result appletGetLaunchStorageInfoForDebug(FsStorageId *app_storageId, FsStorageI if (hosversionBefore(2,0,0)) return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer); - IpcCommand c; - ipcInitialize(&c); - struct { - u64 magic; - u64 cmd_id; - } *raw; - - raw = serviceIpcPrepareHeader(&g_appletIFunctions, &c, sizeof(*raw)); - - raw->magic = SFCI_MAGIC; - raw->cmd_id = 24; - - Result rc = serviceIpcDispatch(&g_appletIFunctions); - - if (R_SUCCEEDED(rc)) { - IpcParsedCommand r; - struct { - u64 magic; - u64 result; - u8 app_storageId; - u8 update_storageId; - } *resp; - - serviceIpcParse(&g_appletIFunctions, &r, sizeof(*resp)); - resp = r.Raw; - - rc = resp->result; - - if (R_SUCCEEDED(rc) && app_storageId) *app_storageId = resp->app_storageId; - if (R_SUCCEEDED(rc) && update_storageId) *update_storageId = resp->update_storageId; - } + u8 app_storageId; + u8 update_storageId; + } out; + serviceAssumeDomain(&g_appletIFunctions); + Result rc = serviceDispatchOut(&g_appletIFunctions, 24, out); + if (R_SUCCEEDED(rc) && app_storageId) *app_storageId = out.app_storageId; + if (R_SUCCEEDED(rc) && update_storageId) *update_storageId = out.update_storageId; return rc; } @@ -3809,43 +2321,13 @@ void appletNotifyRunning(bool *out) { } Result appletGetPseudoDeviceId(u128 *out) { - IpcCommand c; - ipcInitialize(&c); - if (!serviceIsActive(&g_appletSrv) || !_appletIsApplication()) return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); - if (hosversionBefore(2,0,0)) return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer); - struct { - u64 magic; - u64 cmd_id; - } *raw; - - raw = serviceIpcPrepareHeader(&g_appletIFunctions, &c, sizeof(*raw)); - - raw->magic = SFCI_MAGIC; - raw->cmd_id = 50; - - Result rc = serviceIpcDispatch(&g_appletIFunctions); - - if (R_SUCCEEDED(rc)) { - IpcParsedCommand r; - struct { - u64 magic; - u64 result; - u128 out; - } *resp; - - serviceIpcParse(&g_appletIFunctions, &r, sizeof(*resp)); - resp = r.Raw; - - rc = resp->result; - if (R_SUCCEEDED(rc) && out) *out = resp->out; - } - - return rc; + serviceAssumeDomain(&g_appletIFunctions); + return serviceDispatchOut(&g_appletIFunctions, 50, *out); } Result appletSetMediaPlaybackState(bool state) { @@ -3859,45 +2341,12 @@ Result appletSetMediaPlaybackState(bool state) { } Result appletIsGamePlayRecordingSupported(bool *flag) { - IpcCommand c; - ipcInitialize(&c); - - if (flag) *flag = 0; - if (!serviceIsActive(&g_appletSrv) || !_appletIsRegularApplication()) return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); - if (hosversionBefore(3,0,0)) return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer); - struct { - u64 magic; - u64 cmd_id; - } *raw; - - raw = serviceIpcPrepareHeader(&g_appletIFunctions, &c, sizeof(*raw)); - - raw->magic = SFCI_MAGIC; - raw->cmd_id = 65; - - Result rc = serviceIpcDispatch(&g_appletIFunctions); - - if (R_SUCCEEDED(rc)) { - IpcParsedCommand r; - struct { - u64 magic; - u64 result; - u8 flag; - } *resp; - - serviceIpcParse(&g_appletIFunctions, &r, sizeof(*resp)); - resp = r.Raw; - - rc = resp->result; - if (R_SUCCEEDED(rc) && flag) *flag = resp->flag & 1; - } - - return rc; + return _appletCmdNoInOutBool(&g_appletIFunctions, flag, 65); } static Result _appletInitializeGamePlayRecording(TransferMemory *tmem) { @@ -3907,47 +2356,16 @@ static Result _appletInitializeGamePlayRecording(TransferMemory *tmem) { if (hosversionBefore(3,0,0)) return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer); - return _appletCmdInTmem(&g_appletIFunctions, NULL, 66, tmem); + return _appletCmdInTmemNoOut(&g_appletIFunctions, tmem, 66); } Result appletSetGamePlayRecordingState(bool state) { - IpcCommand c; - ipcInitialize(&c); - if (!serviceIsActive(&g_appletSrv) || !_appletIsRegularApplication() || g_appletRecordingInitialized==0) return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); - if (hosversionBefore(3,0,0)) return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer); - struct { - u64 magic; - u64 cmd_id; - u32 state; - } *raw; - - raw = serviceIpcPrepareHeader(&g_appletIFunctions, &c, sizeof(*raw)); - - raw->magic = SFCI_MAGIC; - raw->cmd_id = 67; - raw->state = state==0 ? 0 : 1; - - Result rc = serviceIpcDispatch(&g_appletIFunctions); - - if (R_SUCCEEDED(rc)) { - IpcParsedCommand r; - struct { - u64 magic; - u64 result; - } *resp; - - serviceIpcParse(&g_appletIFunctions, &r, sizeof(*resp)); - resp = r.Raw; - - rc = resp->result; - } - - return rc; + return _appletCmdInU32(&g_appletIFunctions, state!=0, 67); } Result appletInitializeGamePlayRecording(void) { @@ -4028,43 +2446,17 @@ Result appletExitAndRequestToShowThanksMessage(void) { } static Result _appletInitializeApplicationCopyrightFrameBuffer(TransferMemory *tmem, s32 width, s32 height) { - IpcCommand c; - ipcInitialize(&c); - - ipcSendHandleCopy(&c, tmem->handle); - - struct { - u64 magic; - u64 cmd_id; + const struct { s32 width; s32 height; u64 size; - } *raw; + } in = { width, height, tmem->size }; - raw = serviceIpcPrepareHeader(&g_appletIFunctions, &c, sizeof(*raw)); - - raw->magic = SFCI_MAGIC; - raw->cmd_id = 100; - raw->width = width; - raw->height = height; - raw->size = tmem->size; - - Result rc = serviceIpcDispatch(&g_appletIFunctions); - - if (R_SUCCEEDED(rc)) { - IpcParsedCommand r; - struct { - u64 magic; - u64 result; - } *resp; - - serviceIpcParse(&g_appletIFunctions, &r, sizeof(*resp)); - resp = r.Raw; - - rc = resp->result; - } - - return rc; + serviceAssumeDomain(&g_appletIFunctions); + return serviceDispatchIn(&g_appletIFunctions, 100, in, + .in_num_handles = 1, + .in_handles = { tmem->handle }, + ); } Result appletInitializeApplicationCopyrightFrameBuffer(void) { @@ -4100,47 +2492,19 @@ Result appletSetApplicationCopyrightImage(const void* buffer, size_t size, s32 x if (hosversionBefore(5,0,0)) return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer); - IpcCommand c; - ipcInitialize(&c); - - ipcAddSendBuffer(&c, buffer, size, BufferType_Type1); - - struct { - u64 magic; - u64 cmd_id; + const struct { s32 x; s32 y; s32 width; s32 height; s32 mode; - } *raw; + } in = { x, y, width, height, mode }; - raw = serviceIpcPrepareHeader(&g_appletIFunctions, &c, sizeof(*raw)); - - raw->magic = SFCI_MAGIC; - raw->cmd_id = 101; - raw->x = x; - raw->y = y; - raw->width = width; - raw->height = height; - raw->mode = mode; - - Result rc = serviceIpcDispatch(&g_appletIFunctions); - - if (R_SUCCEEDED(rc)) { - IpcParsedCommand r; - struct { - u64 magic; - u64 result; - } *resp; - - serviceIpcParse(&g_appletIFunctions, &r, sizeof(*resp)); - resp = r.Raw; - - rc = resp->result; - } - - return rc; + serviceAssumeDomain(&g_appletIFunctions); + return serviceDispatchIn(&g_appletIFunctions, 101, in, + .buffer_attrs = { SfBufferAttr_HipcMapTransferAllowsNonSecure | SfBufferAttr_HipcMapAlias | SfBufferAttr_In }, + .buffers = { { buffer, size } }, + ); } Result appletSetApplicationCopyrightVisibility(bool visible) { @@ -4154,93 +2518,41 @@ Result appletSetApplicationCopyrightVisibility(bool visible) { //Official sw has these under 'pdm'. Result appletQueryApplicationPlayStatistics(PdmApplicationPlayStatistics *stats, const u64 *titleIDs, s32 count, s32 *total_out) { - IpcCommand c; - ipcInitialize(&c); - if (!serviceIsActive(&g_appletSrv) || !_appletIsApplication()) return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); - if (hosversionBefore(5,0,0)) return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer); - ipcAddSendBuffer(&c, titleIDs, count*sizeof(u64), BufferType_Normal); - ipcAddRecvBuffer(&c, stats, count*sizeof(PdmApplicationPlayStatistics), BufferType_Normal); - - struct { - u64 magic; - u64 cmd_id; - } *raw; - - raw = serviceIpcPrepareHeader(&g_appletIFunctions, &c, sizeof(*raw)); - - raw->magic = SFCI_MAGIC; - raw->cmd_id = 110; - - Result rc = serviceIpcDispatch(&g_appletIFunctions); - - if (R_SUCCEEDED(rc)) { - IpcParsedCommand r; - struct { - u64 magic; - u64 result; - s32 total_out; - } *resp; - - serviceIpcParse(&g_appletIFunctions, &r, sizeof(*resp)); - resp = r.Raw; - - rc = resp->result; - - if (R_SUCCEEDED(rc) && total_out) *total_out = resp->total_out; - } - - return rc; + serviceAssumeDomain(&g_appletIFunctions); + return serviceDispatchOut(&g_appletIFunctions, 110, *total_out, + .buffer_attrs = { + SfBufferAttr_HipcMapAlias | SfBufferAttr_Out, + SfBufferAttr_HipcMapAlias | SfBufferAttr_In, + }, + .buffers = { + { stats, count*sizeof(PdmApplicationPlayStatistics) }, + { titleIDs, count*sizeof(u64) }, + }, + ); } Result appletQueryApplicationPlayStatisticsByUid(u128 userID, PdmApplicationPlayStatistics *stats, const u64 *titleIDs, s32 count, s32 *total_out) { - IpcCommand c; - ipcInitialize(&c); - if (!serviceIsActive(&g_appletSrv) || !_appletIsApplication()) return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); - if (hosversionBefore(6,0,0)) return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer); - ipcAddSendBuffer(&c, titleIDs, count*sizeof(u64), BufferType_Normal); - ipcAddRecvBuffer(&c, stats, count*sizeof(PdmApplicationPlayStatistics), BufferType_Normal); - - struct { - u64 magic; - u64 cmd_id; - u128 userID; - } *raw; - - raw = serviceIpcPrepareHeader(&g_appletIFunctions, &c, sizeof(*raw)); - - raw->magic = SFCI_MAGIC; - raw->cmd_id = 111; - raw->userID = userID; - - Result rc = serviceIpcDispatch(&g_appletIFunctions); - - if (R_SUCCEEDED(rc)) { - IpcParsedCommand r; - struct { - u64 magic; - u64 result; - s32 total_out; - } *resp; - - serviceIpcParse(&g_appletIFunctions, &r, sizeof(*resp)); - resp = r.Raw; - - rc = resp->result; - - if (R_SUCCEEDED(rc) && total_out) *total_out = resp->total_out; - } - - return rc; + serviceAssumeDomain(&g_appletIFunctions); + return serviceDispatchInOut(&g_appletIFunctions, 111, userID, *total_out, + .buffer_attrs = { + SfBufferAttr_HipcMapAlias | SfBufferAttr_Out, + SfBufferAttr_HipcMapAlias | SfBufferAttr_In, + }, + .buffers = { + { stats, count*sizeof(PdmApplicationPlayStatistics) }, + { titleIDs, count*sizeof(u64) }, + }, + ); } static Result _appletExecuteProgramCmd(AppletProgramSpecifyKind kind, u64 inval) { @@ -4249,39 +2561,13 @@ static Result _appletExecuteProgramCmd(AppletProgramSpecifyKind kind, u64 inval) if (hosversionBefore(5,0,0)) return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer); - IpcCommand c; - ipcInitialize(&c); - - struct { - u64 magic; - u64 cmd_id; + const struct { u32 kind; u64 inval; - } *raw; + } in = { kind, inval }; - raw = serviceIpcPrepareHeader(&g_appletIFunctions, &c, sizeof(*raw)); - - raw->magic = SFCI_MAGIC; - raw->cmd_id = 120; - raw->kind = kind; - raw->inval = inval; - - Result rc = serviceIpcDispatch(&g_appletIFunctions); - - if (R_SUCCEEDED(rc)) { - IpcParsedCommand r; - struct { - u64 magic; - u64 result; - } *resp; - - serviceIpcParse(&g_appletIFunctions, &r, sizeof(*resp)); - resp = r.Raw; - - rc = resp->result; - } - - return rc; + serviceAssumeDomain(&g_appletIFunctions); + return serviceDispatchIn(&g_appletIFunctions, 120, in); } static Result _appletClearUserChannel(void) { @@ -4352,11 +2638,10 @@ Result appletGetPreviousProgramIndex(s32 *programIndex) { Result appletGetGpuErrorDetectedSystemEvent(Event *out_event) { if (!serviceIsActive(&g_appletSrv) || !_appletIsApplication()) return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); - if (hosversionBefore(8,0,0)) return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer); - return _appletGetEvent(&g_appletIFunctions, out_event, 130, false); + return _appletGetEvent(&g_appletIFunctions, out_event, false, 130); } Result appletCreateMovieMaker(Service* srv_out, TransferMemory *tmem) { @@ -4365,7 +2650,7 @@ Result appletCreateMovieMaker(Service* srv_out, TransferMemory *tmem) { if (hosversionBefore(5,0,0)) return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer); - return _appletCreateMovieSession(&g_appletIFunctions, srv_out, 1000, tmem); + return _appletCmdInTmemOutSession(&g_appletIFunctions, srv_out, tmem, 1000); } Result appletPrepareForJit(void) { @@ -4411,7 +2696,7 @@ Result appletGetPopFromGeneralChannelEvent(Event *out_event) { if (__nx_applet_type != AppletType_SystemApplet) return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); - return _appletGetEvent(&g_appletIFunctions, out_event, 21, false); + return _appletGetEvent(&g_appletIFunctions, out_event, false, 21); } Result appletGetHomeButtonWriterLockAccessor(AppletLockAccessor *a) { @@ -4427,41 +2712,18 @@ Result appletPopRequestLaunchApplicationForDebug(u128 *userIDs, s32 count, u64 * if (hosversionBefore(6,0,0)) return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer); - IpcCommand c; - ipcInitialize(&c); - - ipcAddRecvBuffer(&c, userIDs, count*sizeof(u128), BufferType_Normal); - struct { - u64 magic; - u64 cmd_id; - } *raw; - - raw = serviceIpcPrepareHeader(&g_appletIFunctions, &c, sizeof(*raw)); - - raw->magic = SFCI_MAGIC; - raw->cmd_id = 100; - - Result rc = serviceIpcDispatch(&g_appletIFunctions); - - if (R_SUCCEEDED(rc)) { - IpcParsedCommand r; - struct { - u64 magic; - u64 result; - u64 titleID; - s32 total_out; - } *resp; - - serviceIpcParse(&g_appletIFunctions, &r, sizeof(*resp)); - resp = r.Raw; - - rc = resp->result; - - if (R_SUCCEEDED(rc) && titleID) *titleID = resp->titleID; - if (R_SUCCEEDED(rc) && total_out) *total_out = resp->total_out; - } + u64 titleID; + s32 total_out; + } out; + serviceAssumeDomain(&g_appletIFunctions); + Result rc = serviceDispatchOut(&g_appletIFunctions, 100, out, + .buffer_attrs = { SfBufferAttr_HipcMapAlias | SfBufferAttr_Out }, + .buffers = { { userIDs, count*sizeof(u128) } }, + ); + if (R_SUCCEEDED(rc) && titleID) *titleID = out.titleID; + if (R_SUCCEEDED(rc) && total_out) *total_out = out.total_out; return rc; } @@ -4557,20 +2819,16 @@ Result appletGetHdcpAuthenticationFailedEvent(Event *out_event) { if (hosversionBefore(4,0,0)) return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer); - return _appletGetEvent(&g_appletIGlobalStateController, out_event, 15, false); + return _appletGetEvent(&g_appletIGlobalStateController, out_event, false, 15); } // IApplicationCreator static Result _appletApplicationCreateState(AppletApplication *a) { - Result rc=0; - - rc = _appletGetEvent(&a->s, &a->StateChangedEvent, 0, false);//GetAppletStateChangedEvent - - return rc; + return _appletGetEvent(&a->s, &a->StateChangedEvent, false, 0);//GetAppletStateChangedEvent } -static Result _appletApplicationCreate(AppletApplication *a, Service* srv, u64 cmd_id) { +static Result _appletApplicationCreate(AppletApplication *a, Service* srv, u32 cmd_id) { Result rc=0; memset(a, 0, sizeof(AppletApplication)); @@ -4582,12 +2840,12 @@ static Result _appletApplicationCreate(AppletApplication *a, Service* srv, u64 c return rc; } -static Result _appletApplicationCreateIn64(AppletApplication *a, Service* srv, u64 cmd_id, u64 val) { +static Result _appletApplicationCreateIn64(AppletApplication *a, Service* srv, u64 val, u32 cmd_id) { Result rc=0; memset(a, 0, sizeof(AppletApplication)); - rc = _appletGetSessionIn64(srv, &a->s, cmd_id, val); + rc = _appletGetSessionIn64(srv, &a->s, val, cmd_id); if (R_SUCCEEDED(rc)) rc = _appletApplicationCreateState(a); @@ -4598,7 +2856,7 @@ Result appletCreateApplication(AppletApplication *a, u64 titleID) { if (__nx_applet_type != AppletType_SystemApplet) return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); - return _appletApplicationCreateIn64(a, &g_appletIApplicationCreator, 0, titleID); + return _appletApplicationCreateIn64(a, &g_appletIApplicationCreator, titleID, 0); } Result appletPopLaunchRequestedApplication(AppletApplication *a) { @@ -4612,7 +2870,7 @@ Result appletCreateSystemApplication(AppletApplication *a, u64 titleID) { if (__nx_applet_type != AppletType_SystemApplet) return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); - return _appletApplicationCreateIn64(a, &g_appletIApplicationCreator, 10, titleID); + return _appletApplicationCreateIn64(a, &g_appletIApplicationCreator, titleID, 10); } Result appletPopFloatingApplicationForDevelopment(AppletApplication *a) { @@ -4635,36 +2893,24 @@ bool appletApplicationActive(AppletApplication *a) { } Result appletApplicationStart(AppletApplication *a) { - Result rc=0; - if (!serviceIsActive(&a->s)) return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); - rc = _appletCmdNoIO(&a->s, 10);//Start - - return rc; + return _appletCmdNoIO(&a->s, 10);//Start } Result appletApplicationRequestExit(AppletApplication *a) { - Result rc=0; - if (!serviceIsActive(&a->s)) return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); - rc = _appletCmdNoIO(&a->s, 20);//RequestExit - - return rc; + return _appletCmdNoIO(&a->s, 20);//RequestExit } Result appletApplicationTerminate(AppletApplication *a) { - Result rc=0; - if (!serviceIsActive(&a->s)) return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); - rc = _appletCmdNoIO(&a->s, 25);//Terminate - - return rc; + return _appletCmdNoIO(&a->s, 25);//Terminate } void appletApplicationJoin(AppletApplication *a) { @@ -4701,14 +2947,10 @@ AppletApplicationExitReason appletApplicationGetExitReason(AppletApplication *a) } Result appletApplicationRequestForApplicationToGetForeground(AppletApplication *a) { - Result rc=0; - if (!serviceIsActive(&a->s)) return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); - rc = _appletCmdNoIO(&a->s, 101);//RequestForApplicationToGetForeground - - return rc; + return _appletCmdNoIO(&a->s, 101);//RequestForApplicationToGetForeground } Result appletApplicationTerminateAllLibraryApplets(AppletApplication *a) { @@ -4740,16 +2982,12 @@ Result appletApplicationRequestExitLibraryAppletOrTerminate(AppletApplication *a } Result appletApplicationGetApplicationId(AppletApplication *a, u64 *titleID) { - Result rc=0; - if (!serviceIsActive(&a->s)) return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); if (hosversionBefore(6,0,0)) return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer); - rc = _appletCmdNoInOut64(&a->s, titleID, 120);//GetApplicationId - - return rc; + return _appletCmdNoInOut64(&a->s, titleID, 120);//GetApplicationId } Result appletApplicationPushLaunchParameter(AppletApplication *a, AppletLaunchParameterKind kind, AppletStorage* s) { @@ -4758,40 +2996,13 @@ Result appletApplicationPushLaunchParameter(AppletApplication *a, AppletLaunchPa if (!serviceIsActive(&s->s)) return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); - IpcCommand c; - ipcInitialize(&c); - - serviceSendObject(&s->s, &c); - - struct { - u64 magic; - u64 cmd_id; - u32 kind; - } *raw; - - raw = serviceIpcPrepareHeader(&a->s, &c, sizeof(*raw)); - - raw->magic = SFCI_MAGIC; - raw->cmd_id = 121; - raw->kind = kind; - - Result rc = serviceIpcDispatch(&a->s); - - if (R_SUCCEEDED(rc)) { - IpcParsedCommand r; - struct { - u64 magic; - u64 result; - } *resp; - - serviceIpcParse(&a->s, &r, sizeof(*resp)); - resp = r.Raw; - - rc = resp->result; - } - + u32 tmpkind=kind; + serviceAssumeDomain(&a->s); + Result rc = serviceDispatchIn(&a->s, 121, tmpkind, + .in_num_objects = 1, + .in_objects = { &s->s }, + ); appletStorageClose(s); - return rc; } @@ -4817,38 +3028,8 @@ Result appletApplicationGetApplicationLaunchRequestInfo(AppletApplication *a, Ap if (hosversionBefore(6,0,0)) return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer); - IpcCommand c; - ipcInitialize(&c); - - struct { - u64 magic; - u64 cmd_id; - } *raw; - - raw = serviceIpcPrepareHeader(&a->s, &c, sizeof(*raw)); - - raw->magic = SFCI_MAGIC; - raw->cmd_id = 124; - - Result rc = serviceIpcDispatch(&a->s); - - if (R_SUCCEEDED(rc)) { - IpcParsedCommand r; - struct { - u64 magic; - u64 result; - AppletApplicationLaunchRequestInfo out; - } *resp; - - serviceIpcParse(&a->s, &r, sizeof(*resp)); - resp = r.Raw; - - rc = resp->result; - - if (R_SUCCEEDED(rc) && out) *out = resp->out; - } - - return rc; + serviceAssumeDomain(&a->s); + return serviceDispatchOut(&a->s, 124, *out); } Result appletApplicationSetUsers(AppletApplication *a, const u128 *userIDs, s32 count, bool flag) { @@ -4857,65 +3038,30 @@ Result appletApplicationSetUsers(AppletApplication *a, const u128 *userIDs, s32 if (hosversionBefore(6,0,0)) return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer); - IpcCommand c; - ipcInitialize(&c); - - ipcAddSendBuffer(&c, userIDs, count*sizeof(u128), BufferType_Normal); - - struct { - u64 magic; - u64 cmd_id; - u8 flag; - } *raw; - - raw = serviceIpcPrepareHeader(&a->s, &c, sizeof(*raw)); - - raw->magic = SFCI_MAGIC; - raw->cmd_id = 130; - raw->flag = flag!=0; - - Result rc = serviceIpcDispatch(&a->s); - - if (R_SUCCEEDED(rc)) { - IpcParsedCommand r; - struct { - u64 magic; - u64 result; - } *resp; - - serviceIpcParse(&a->s, &r, sizeof(*resp)); - resp = r.Raw; - - rc = resp->result; - } - - return rc; + u8 tmp = flag!=0; + serviceAssumeDomain(&a->s); + return serviceDispatchIn(&a->s, 130, tmp, + .buffer_attrs = { SfBufferAttr_HipcMapAlias | SfBufferAttr_In }, + .buffers = { { userIDs, count*sizeof(u128) } }, + ); } Result appletApplicationCheckRightsEnvironmentAvailable(AppletApplication *a, bool *out) { - Result rc=0; - if (!serviceIsActive(&a->s)) return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); if (hosversionBefore(6,0,0)) return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer); - rc = _appletCmdNoInOutBool(&a->s, out, 131);//CheckRightsEnvironmentAvailable - - return rc; + return _appletCmdNoInOutBool(&a->s, out, 131);//CheckRightsEnvironmentAvailable } Result appletApplicationGetNsRightsEnvironmentHandle(AppletApplication *a, u64 *handle) { - Result rc=0; - if (!serviceIsActive(&a->s)) return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); if (hosversionBefore(6,0,0)) return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer); - rc = _appletCmdNoInOut64(&a->s, handle, 132);//GetNsRightsEnvironmentHandle - - return rc; + return _appletCmdNoInOut64(&a->s, handle, 132);//GetNsRightsEnvironmentHandle } Result appletApplicationGetDesirableUids(AppletApplication *a, u128 *userIDs, s32 count, s32 *total_out) { @@ -4924,53 +3070,20 @@ Result appletApplicationGetDesirableUids(AppletApplication *a, u128 *userIDs, s3 if (hosversionBefore(6,0,0)) return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer); - IpcCommand c; - ipcInitialize(&c); - - ipcAddRecvBuffer(&c, userIDs, count*sizeof(u128), BufferType_Normal); - - struct { - u64 magic; - u64 cmd_id; - } *raw; - - raw = serviceIpcPrepareHeader(&a->s, &c, sizeof(*raw)); - - raw->magic = SFCI_MAGIC; - raw->cmd_id = 140; - - Result rc = serviceIpcDispatch(&a->s); - - if (R_SUCCEEDED(rc)) { - IpcParsedCommand r; - struct { - u64 magic; - u64 result; - s32 total_out; - } *resp; - - serviceIpcParse(&a->s, &r, sizeof(*resp)); - resp = r.Raw; - - rc = resp->result; - - if (R_SUCCEEDED(rc) && total_out) *total_out = resp->total_out; - } - - return rc; + serviceAssumeDomain(&a->s); + return serviceDispatchOut(&a->s, 140, *total_out, + .buffer_attrs = { SfBufferAttr_HipcMapAlias | SfBufferAttr_Out }, + .buffers = { { userIDs, count*sizeof(u128) } }, + ); } Result appletApplicationReportApplicationExitTimeout(AppletApplication *a) { - Result rc=0; - if (!serviceIsActive(&a->s)) return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); if (hosversionBefore(6,0,0)) return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer); - rc = _appletCmdNoIO(&a->s, 150);//ReportApplicationExitTimeout - - return rc; + return _appletCmdNoIO(&a->s, 150);//ReportApplicationExitTimeout } Result appletApplicationSetApplicationAttribute(AppletApplication *a, const AppletApplicationAttribute *attr) { @@ -4988,39 +3101,10 @@ Result appletApplicationHasSaveDataAccessPermission(AppletApplication *a, u64 ti if (hosversionBefore(8,0,0)) return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer); - IpcCommand c; - ipcInitialize(&c); - - struct { - u64 magic; - u64 cmd_id; - u64 titleID; - } *raw; - - raw = serviceIpcPrepareHeader(&a->s, &c, sizeof(*raw)); - - raw->magic = SFCI_MAGIC; - raw->cmd_id = 170; - raw->titleID = titleID; - - Result rc = serviceIpcDispatch(&a->s); - - if (R_SUCCEEDED(rc)) { - IpcParsedCommand r; - struct { - u64 magic; - u64 result; - u8 out; - } *resp; - - serviceIpcParse(&a->s, &r, sizeof(*resp)); - resp = r.Raw; - - rc = resp->result; - - if (R_SUCCEEDED(rc) && out) *out = resp->out!=0; - } - + u8 tmpout=0; + serviceAssumeDomain(&a->s); + Result rc = serviceDispatchInOut(&a->s, 170, titleID, tmpout); + if (R_SUCCEEDED(rc) && out) *out = tmpout!=0; return rc; } @@ -5058,14 +3142,14 @@ Result appletGetPopInDataEvent(Event *out_event) { if (__nx_applet_type != AppletType_LibraryApplet) return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); - return _appletGetEvent(&g_appletILibraryAppletSelfAccessor, out_event, 5, false); + return _appletGetEvent(&g_appletILibraryAppletSelfAccessor, out_event, false, 5); } Result appletGetPopInteractiveInDataEvent(Event *out_event) { if (__nx_applet_type != AppletType_LibraryApplet) return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); - return _appletGetEvent(&g_appletILibraryAppletSelfAccessor, out_event, 6, false); + return _appletGetEvent(&g_appletILibraryAppletSelfAccessor, out_event, false, 6); } static Result _appletExitProcessAndReturn(void) { @@ -5129,44 +3213,14 @@ Result appletGetMainAppletStorageId(FsStorageId *storageId) { Result appletGetCallerAppletIdentityInfoStack(AppletIdentityInfo *stack, s32 count, s32 *total_out) { if (__nx_applet_type != AppletType_LibraryApplet) return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); - if (hosversionBefore(3,0,0)) return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer); - IpcCommand c; - ipcInitialize(&c); - - ipcAddRecvBuffer(&c, stack, count*sizeof(AppletIdentityInfo), BufferType_Normal); - - struct { - u64 magic; - u64 cmd_id; - } *raw; - - raw = serviceIpcPrepareHeader(&g_appletILibraryAppletSelfAccessor, &c, sizeof(*raw)); - - raw->magic = SFCI_MAGIC; - raw->cmd_id = 17; - - Result rc = serviceIpcDispatch(&g_appletILibraryAppletSelfAccessor); - - if (R_SUCCEEDED(rc)) { - IpcParsedCommand r; - struct { - u64 magic; - u64 result; - s32 total_out; - } *resp; - - serviceIpcParse(&g_appletILibraryAppletSelfAccessor, &r, sizeof(*resp)); - resp = r.Raw; - - rc = resp->result; - - if (R_SUCCEEDED(rc) && total_out) *total_out = resp->total_out; - } - - return rc; + serviceAssumeDomain(&g_appletILibraryAppletSelfAccessor); + return serviceDispatchOut(&g_appletILibraryAppletSelfAccessor, 17, *total_out, + .buffer_attrs = { SfBufferAttr_HipcMapAlias | SfBufferAttr_Out }, + .buffers = { { stack, count*sizeof(AppletIdentityInfo) } }, + ); } Result appletGetNextReturnDestinationAppletIdentityInfo(AppletIdentityInfo *info) { @@ -5198,7 +3252,7 @@ Result appletGetPopExtraStorageEvent(Event *out_event) { if (__nx_applet_type != AppletType_LibraryApplet) return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); - return _appletGetEvent(&g_appletILibraryAppletSelfAccessor, out_event, 25, false); + return _appletGetEvent(&g_appletILibraryAppletSelfAccessor, out_event, false, 25); } Result appletUnpopInData(AppletStorage *s) { @@ -5257,7 +3311,7 @@ Result appletCreateGameMovieTrimmer(Service* srv_out, TransferMemory *tmem) { if (hosversionBefore(4,0,0)) return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer); - return _appletCreateMovieSession(&g_appletILibraryAppletSelfAccessor, srv_out, 100, tmem); + return _appletCmdInTmemOutSession(&g_appletILibraryAppletSelfAccessor, srv_out, tmem, 100); } Result appletReserveResourceForMovieOperation(void) { @@ -5284,41 +3338,18 @@ Result appletGetMainAppletAvailableUsers(u128 *userIDs, s32 count, bool *flag, s if (hosversionBefore(6,0,0)) return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer); - IpcCommand c; - ipcInitialize(&c); - - ipcAddRecvBuffer(&c, userIDs, count*sizeof(u128), BufferType_Normal); - struct { - u64 magic; - u64 cmd_id; - } *raw; - - raw = serviceIpcPrepareHeader(&g_appletILibraryAppletSelfAccessor, &c, sizeof(*raw)); - - raw->magic = SFCI_MAGIC; - raw->cmd_id = 110; - - Result rc = serviceIpcDispatch(&g_appletILibraryAppletSelfAccessor); - - if (R_SUCCEEDED(rc)) { - IpcParsedCommand r; - struct { - u64 magic; - u64 result; - u8 flag; - s32 total_out; - } *resp; - - serviceIpcParse(&g_appletILibraryAppletSelfAccessor, &r, sizeof(*resp)); - resp = r.Raw; - - rc = resp->result; - - if (R_SUCCEEDED(rc) && flag) *flag = resp->flag!=0; - if (R_SUCCEEDED(rc) && total_out) *total_out = resp->total_out; - } + u8 flag; + s32 total_out; + } out; + serviceAssumeDomain(&g_appletILibraryAppletSelfAccessor); + Result rc = serviceDispatchOut(&g_appletILibraryAppletSelfAccessor, 110, out, + .buffer_attrs = { SfBufferAttr_HipcMapAlias | SfBufferAttr_Out }, + .buffers = { { userIDs, count*sizeof(u128) } }, + ); + if (R_SUCCEEDED(rc) && flag) *flag = out.flag!=0; + if (R_SUCCEEDED(rc) && total_out) *total_out = out.total_out; return rc; } @@ -5425,85 +3456,26 @@ Result appletBeginToObserveHidInputForDevelop(void) { // IAppletCommonFunctions -Result appletReadThemeStorage(void* buffer, size_t size, u64 offset, size_t *transfer_size) { +Result appletReadThemeStorage(void* buffer, size_t size, u64 offset, u64 *transfer_size) { if (!serviceIsActive(&g_appletIAppletCommonFunctions)) return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); - IpcCommand c; - ipcInitialize(&c); - - ipcAddRecvSmart(&c, g_appletIAppletCommonFunctions.pointer_buffer_size, buffer, size, 0); - - struct { - u64 magic; - u64 cmd_id; - u64 offset; - } *raw; - - raw = serviceIpcPrepareHeader(&g_appletIAppletCommonFunctions, &c, sizeof(*raw)); - - raw->magic = SFCI_MAGIC; - raw->cmd_id = 10; - raw->offset = offset; - - Result rc = serviceIpcDispatch(&g_appletIAppletCommonFunctions); - - if (R_SUCCEEDED(rc)) { - IpcParsedCommand r; - struct { - u64 magic; - u64 result; - u64 transfer_size; - } *resp; - - serviceIpcParse(&g_appletIAppletCommonFunctions, &r, sizeof(*resp)); - resp = r.Raw; - - rc = resp->result; - - if (R_SUCCEEDED(rc) && transfer_size) *transfer_size = resp->transfer_size; - } - - return rc; + serviceAssumeDomain(&g_appletIAppletCommonFunctions); + return serviceDispatchInOut(&g_appletIAppletCommonFunctions, 10, offset, *transfer_size, + .buffer_attrs = { SfBufferAttr_HipcAutoSelect | SfBufferAttr_Out }, + .buffers = { { buffer, size } }, + ); } Result appletWriteThemeStorage(const void* buffer, size_t size, u64 offset) { if (!serviceIsActive(&g_appletIAppletCommonFunctions)) return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); - IpcCommand c; - ipcInitialize(&c); - - ipcAddSendSmart(&c, g_appletIAppletCommonFunctions.pointer_buffer_size, buffer, size, 0); - - struct { - u64 magic; - u64 cmd_id; - u64 offset; - } *raw; - - raw = serviceIpcPrepareHeader(&g_appletIAppletCommonFunctions, &c, sizeof(*raw)); - - raw->magic = SFCI_MAGIC; - raw->cmd_id = 11; - raw->offset = offset; - - Result rc = serviceIpcDispatch(&g_appletIAppletCommonFunctions); - - if (R_SUCCEEDED(rc)) { - IpcParsedCommand r; - struct { - u64 magic; - u64 result; - } *resp; - - serviceIpcParse(&g_appletIAppletCommonFunctions, &r, sizeof(*resp)); - resp = r.Raw; - - rc = resp->result; - } - - return rc; + serviceAssumeDomain(&g_appletIAppletCommonFunctions); + return serviceDispatchIn(&g_appletIAppletCommonFunctions, 11, offset, + .buffer_attrs = { SfBufferAttr_HipcAutoSelect | SfBufferAttr_In }, + .buffers = { { buffer, size } }, + ); } Result appletGetDisplayLogicalResolution(s32 *width, s32 *height) { @@ -5521,43 +3493,15 @@ Result appletSetDisplayMagnification(float x, float y, float width, float height if (hosversionBefore(8,0,0)) return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer); - IpcCommand c; - ipcInitialize(&c); - - struct { - u64 magic; - u64 cmd_id; + const struct { float x; float y; float width; float height; - } *raw; + } in = { x, y, width, height }; - raw = serviceIpcPrepareHeader(&g_appletIAppletCommonFunctions, &c, sizeof(*raw)); - - raw->magic = SFCI_MAGIC; - raw->cmd_id = 42; - raw->x = x; - raw->y = y; - raw->width = width; - raw->height = height; - - Result rc = serviceIpcDispatch(&g_appletIAppletCommonFunctions); - - if (R_SUCCEEDED(rc)) { - IpcParsedCommand r; - struct { - u64 magic; - u64 result; - } *resp; - - serviceIpcParse(&g_appletIAppletCommonFunctions, &r, sizeof(*resp)); - resp = r.Raw; - - rc = resp->result; - } - - return rc; + serviceAssumeDomain(&g_appletIAppletCommonFunctions); + return serviceDispatchIn(&g_appletIAppletCommonFunctions, 42, in); } Result appletSetHomeButtonDoubleClickEnabled(bool flag) { @@ -5596,80 +3540,30 @@ Result appletRequestLaunchApplicationWithUserAndArgumentForDebug(u64 titleID, u1 if (hosversionBefore(6,0,0)) return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer); - IpcCommand c; - ipcInitialize(&c); - - ipcAddSendBuffer(&c, userIDs, total_userIDs*sizeof(u128), BufferType_Normal); - ipcAddSendBuffer(&c, buffer, size, BufferType_Normal); - - struct { - u64 magic; - u64 cmd_id; + const struct { u8 flag; u64 titleID; - } *raw; + } in = { flag!=0, titleID }; - raw = serviceIpcPrepareHeader(&g_appletIDebugFunctions, &c, sizeof(*raw)); - - raw->magic = SFCI_MAGIC; - raw->cmd_id = 30; - raw->flag = flag!=0; - raw->titleID = titleID; - - Result rc = serviceIpcDispatch(&g_appletIDebugFunctions); - - if (R_SUCCEEDED(rc)) { - IpcParsedCommand r; - struct { - u64 magic; - u64 result; - } *resp; - - serviceIpcParse(&g_appletIDebugFunctions, &r, sizeof(*resp)); - resp = r.Raw; - - rc = resp->result; - } - - return rc; + serviceAssumeDomain(&g_appletIDebugFunctions); + return serviceDispatchIn(&g_appletIDebugFunctions, 30, in, + .buffer_attrs = { + SfBufferAttr_HipcMapAlias | SfBufferAttr_In, + SfBufferAttr_HipcMapAlias | SfBufferAttr_In, + }, + .buffers = { + { userIDs, total_userIDs*sizeof(u128) }, + { buffer, size }, + }, + ); } Result appletGetAppletResourceUsageInfo(AppletResourceUsageInfo *info) { if (hosversionBefore(6,0,0)) return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer); - IpcCommand c; - ipcInitialize(&c); - - struct { - u64 magic; - u64 cmd_id; - } *raw; - - raw = serviceIpcPrepareHeader(&g_appletIDebugFunctions, &c, sizeof(*raw)); - - raw->magic = SFCI_MAGIC; - raw->cmd_id = 40; - - Result rc = serviceIpcDispatch(&g_appletIDebugFunctions); - - if (R_SUCCEEDED(rc)) { - IpcParsedCommand r; - struct { - u64 magic; - u64 result; - AppletResourceUsageInfo info; - } *resp; - - serviceIpcParse(&g_appletIDebugFunctions, &r, sizeof(*resp)); - resp = r.Raw; - - rc = resp->result; - - if (R_SUCCEEDED(rc) && info) *info = resp->info; - } - - return rc; + serviceAssumeDomain(&g_appletIDebugFunctions); + return serviceDispatchOut(&g_appletIDebugFunctions, 40, *info); } // State / other @@ -5712,7 +3606,7 @@ Result appletGetMessage(u32 *msg) { } bool appletProcessMessage(u32 msg) { - Result rc; + Result rc=0; switch(msg) { case AppletMessage_ExitRequested: