#include #include "service_guard.h" #include "arm/counter.h" #include "kernel/tmem.h" #include "kernel/event.h" #include "services/fatal.h" #include "services/applet.h" #include "services/apm.h" #include "services/caps.h" #include "services/pm.h" #include "services/sm.h" #include "services/fs.h" #include "runtime/env.h" #include "runtime/hosversion.h" #include "nacp.h" __attribute__((weak)) u32 __nx_applet_type = AppletType_Default; __attribute__((weak)) bool __nx_applet_auto_notifyrunning = true; __attribute__((weak)) AppletAttribute __nx_applet_AppletAttribute; /// When set, controls the PerformanceConfiguration passed to apmSetPerformanceConfiguration during app startup, where the array index is the PerformanceMode. __attribute__((weak)) u32 __nx_applet_PerformanceConfiguration[2] = {/*0x92220008*//*0x20004*//*0x92220007*/0, 0}; //// Controls whether to use applet exit cmds during \ref appletExit. 0 (default): Only run exit cmds when running under a NSO. 1: Use exit cmds regardless. >1: Skip exit cmds. __attribute__((weak)) u32 __nx_applet_exit_mode = 0; static Service g_appletSrv; static Service g_appletProxySession; static bool g_appletLeftInitialized; static bool g_appletExitProcessFlag; static Result g_appletExitProcessResult; // From Get*Functions. static Service g_appletIAppletCommonFunctions; static Service g_appletIFunctions; static Service g_appletIGlobalStateController; static Service g_appletIApplicationCreator; static Service g_appletILibraryAppletSelfAccessor; static Service g_appletIProcessWindingController; static Service g_appletILibraryAppletCreator; static Service g_appletICommonStateGetter; static Service g_appletISelfController; static Service g_appletIWindowController; static Service g_appletIAudioController; static Service g_appletIDisplayController; static Service g_appletIDebugFunctions; static Event g_appletMessageEvent; static u64 g_appletResourceUserId = 0; static u8 g_appletOperationMode; static u32 g_appletPerformanceMode; static u8 g_appletFocusState; static bool g_appletNotifiedRunning = 0; static AppletHookCookie g_appletFirstHook; static TransferMemory g_appletRecordingTmem; static u32 g_appletRecordingInitialized; static TransferMemory g_appletCopyrightTmem; static bool g_appletCopyrightInitialized; static Event g_appletSuspendedTickEvent; static u64 g_appletInitTickBase; static u64 g_appletSuspendedTick; static bool g_appletSuspendedTickInitialized; static Event g_appletLibraryAppletLaunchableEvent; static AppletThemeColorType g_appletThemeColorType = AppletThemeColorType_Default; static ApmCpuBoostMode g_appletCpuBoostMode = ApmCpuBoostMode_Disabled; static AppletInfo g_appletInfo; static bool g_appletInfoInitialized; 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); static Result _appletGetCurrentFocusState(u8 *out); static Result _appletSetFocusHandlingMode(bool inval0, bool inval1, bool inval2); static Result _appletSetOutOfFocusSuspendingEnabled(bool inval); static Result _appletReceiveMessage(u32 *out); static Result _appletAcquireForegroundRights(void); static Result _appletGetOperationMode(u8 *out); static Result _appletGetPerformanceMode(u32 *out); static Result _appletSetOperationModeChangedNotification(bool flag); static Result _appletSetPerformanceModeChangedNotification(bool flag); static Result _appletSelfExit(void); static Result _appletExitProcessAndReturn(void); static Result _appletGetAccumulatedSuspendedTickValue(u64 *tick); static Result _appletGetAccumulatedSuspendedTickChangedEvent(Event *out_event); 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, u32 cmd_id); NX_GENERATE_SERVICE_GUARD(applet); Result _appletInitialize(void) { AppletAttribute *attr = NULL; if (g_appletLeftInitialized) { g_appletLeftInitialized = false; return 0; } if (__nx_applet_type == AppletType_None) return 0; if (R_FAILED(apmInitialize())) return MAKERESULT(Module_Libnx, LibnxError_ApmFailedToInitialize); Result rc = 0; g_appletResourceUserId = 0; g_appletNotifiedRunning = 0; g_appletExitProcessFlag = 0; g_appletRecordingInitialized = 0; g_appletInfoInitialized = 0; 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; } if (R_SUCCEEDED(rc)) { rc = serviceConvertToDomain(&g_appletSrv); } if (R_SUCCEEDED(rc)) { #define AM_BUSY_ERROR 0x19280 do { 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)); } if (__nx_applet_type == AppletType_LibraryApplet && hosversionAtLeast(3,0,0)) { cmd_id = 201; attr = &__nx_applet_AppletAttribute; } 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); } } while (rc == AM_BUSY_ERROR); } // [7.0.0+] GetAppletCommonFunctions if (R_SUCCEEDED(rc) && hosversionAtLeast(7,0,0)) { if (__nx_applet_type == AppletType_SystemApplet || __nx_applet_type == AppletType_LibraryApplet || __nx_applet_type == AppletType_OverlayApplet) { rc = _appletGetSession(&g_appletProxySession, &g_appletIAppletCommonFunctions, __nx_applet_type == AppletType_SystemApplet ? 23 : 21); } } // Get*Functions if (R_SUCCEEDED(rc) && __nx_applet_type != AppletType_LibraryApplet) rc = _appletGetSession(&g_appletProxySession, &g_appletIFunctions, 20); if (R_SUCCEEDED(rc) && __nx_applet_type == AppletType_SystemApplet) { //GetGlobalStateController rc = _appletGetSession(&g_appletProxySession, &g_appletIGlobalStateController, 21); //GetApplicationCreator if (R_SUCCEEDED(rc)) rc = _appletGetSession(&g_appletProxySession, &g_appletIApplicationCreator, 22); } if (R_SUCCEEDED(rc) && __nx_applet_type == AppletType_LibraryApplet) { //GetLibraryAppletSelfAccessor rc = _appletGetSession(&g_appletProxySession, &g_appletILibraryAppletSelfAccessor, 20); //GetProcessWindingController if (R_SUCCEEDED(rc)) rc = _appletGetSession(&g_appletProxySession, &g_appletIProcessWindingController, 10); } // GetLibraryAppletCreator if (R_SUCCEEDED(rc)) rc = _appletGetSession(&g_appletProxySession, &g_appletILibraryAppletCreator, 11); // GetCommonStateGetter if (R_SUCCEEDED(rc)) rc = _appletGetSession(&g_appletProxySession, &g_appletICommonStateGetter, 0); // GetSelfController if (R_SUCCEEDED(rc)) rc = _appletGetSession(&g_appletProxySession, &g_appletISelfController, 1); // GetWindowController if (R_SUCCEEDED(rc)) rc = _appletGetSession(&g_appletProxySession, &g_appletIWindowController, 2); // Get AppletResourceUserId. if (R_SUCCEEDED(rc)) rc = _appletGetAppletResourceUserId(&g_appletResourceUserId); // GetAudioController if (R_SUCCEEDED(rc)) rc = _appletGetSession(&g_appletProxySession, &g_appletIAudioController, 3); // GetDisplayController if (R_SUCCEEDED(rc)) rc = _appletGetSession(&g_appletProxySession, &g_appletIDisplayController, 4); // GetDebugFunctions if (R_SUCCEEDED(rc)) rc = _appletGetSession(&g_appletProxySession, &g_appletIDebugFunctions, 1000); Result rc2 = _appletGetAccumulatedSuspendedTickChangedEvent(&g_appletSuspendedTickEvent); if (R_SUCCEEDED(rc2)) { g_appletInitTickBase = armGetSystemTick(); g_appletSuspendedTick = 0; g_appletSuspendedTickInitialized = true; } // ICommonStateGetter::GetEventHandle if (R_SUCCEEDED(rc)) rc = _appletGetEvent(&g_appletICommonStateGetter, &g_appletMessageEvent, false, 0); if (R_SUCCEEDED(rc) && (__nx_applet_type == AppletType_Application)) { rc = _appletGetCurrentFocusState(&g_appletFocusState); //Don't enter this msg-loop when g_appletFocusState is already 1, it will hang when applet was previously initialized in the context of the current process for AppletType_Application. if (R_SUCCEEDED(rc) && g_appletFocusState != AppletFocusState_Focused) { do { eventWait(&g_appletMessageEvent, U64_MAX); u32 msg; rc = _appletReceiveMessage(&msg); if (R_FAILED(rc)) { if (R_VALUE(rc) == 0x680) continue; break; } if (msg != 0xF) continue; rc = _appletGetCurrentFocusState(&g_appletFocusState); if (R_FAILED(rc)) break; } while(g_appletFocusState != AppletFocusState_Focused); } if (R_SUCCEEDED(rc)) rc = _appletAcquireForegroundRights(); if (R_SUCCEEDED(rc)) rc = appletSetFocusHandlingMode(AppletFocusHandlingMode_SuspendHomeSleep); } if (R_SUCCEEDED(rc) && __nx_applet_auto_notifyrunning) appletNotifyRunning(NULL); if (R_SUCCEEDED(rc)) rc = _appletGetOperationMode(&g_appletOperationMode); if (R_SUCCEEDED(rc)) rc = _appletGetPerformanceMode(&g_appletPerformanceMode); if (R_SUCCEEDED(rc) && __nx_applet_type!=AppletType_Application) rc = _appletGetCurrentFocusState(&g_appletFocusState); if (R_SUCCEEDED(rc)) rc = _appletSetOperationModeChangedNotification(1); if (R_SUCCEEDED(rc)) rc = _appletSetPerformanceModeChangedNotification(1); // Official apps aren't known to use apmSetPerformanceConfiguration with mode=1. if (R_SUCCEEDED(rc)) { u32 i; for (i=0; i<2; i++) { if (__nx_applet_PerformanceConfiguration[i]) rc = apmSetPerformanceConfiguration(i, __nx_applet_PerformanceConfiguration[i]); if (R_FAILED(rc)) break; } } if (R_SUCCEEDED(rc) && __nx_applet_type == AppletType_LibraryApplet) { AppletProcessLaunchReason launchreason={0}; Result rc2 = appletGetLibraryAppletInfo(&g_appletInfo.info); if (R_SUCCEEDED(rc2)) rc2 = _appletGetLaunchReason(&launchreason); if (R_SUCCEEDED(rc2)) { g_appletInfo.caller_flag = launchreason.flag!=0; if (g_appletInfo.caller_flag) rc2 = _appletOpenCallingLibraryApplet(&g_appletInfo.caller); } if (R_SUCCEEDED(rc2)) g_appletInfoInitialized = true; } if (R_FAILED(rc)) appletExit(); return rc; } static void _appletInfiniteSleepLoop(void) { while(1) svcSleepThread(86400000000000ULL); } static void NORETURN _appletExitProcess(int result_code) { appletExit(); if (R_SUCCEEDED(g_appletExitProcessResult)) _appletInfiniteSleepLoop(); svcExitProcess(); __builtin_unreachable(); } static bool _appletIsApplication(void) { return __nx_applet_type == AppletType_Application || __nx_applet_type == AppletType_SystemApplication; } static bool _appletIsRegularApplication(void) { return __nx_applet_type == AppletType_Application; } void _appletCleanup(void) { 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; 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) { return &g_appletProxySession; } Service* appletGetServiceSession_AppletCommonFunctions(void) { return &g_appletIAppletCommonFunctions; } Service* appletGetServiceSession_Functions(void) { return &g_appletIFunctions; } Service* appletGetServiceSession_GlobalStateController(void) { return &g_appletIGlobalStateController; } Service* appletGetServiceSession_ApplicationCreator(void) { return &g_appletIApplicationCreator; } Service* appletGetServiceSession_LibraryAppletSelfAccessor(void) { return &g_appletILibraryAppletSelfAccessor; } Service* appletGetServiceSession_ProcessWindingController(void) { return &g_appletIProcessWindingController; } Service* appletGetServiceSession_LibraryAppletCreator(void) { return &g_appletILibraryAppletCreator; } Service* appletGetServiceSession_CommonStateGetter(void) { return &g_appletICommonStateGetter; } Service* appletGetServiceSession_SelfController(void) { return &g_appletISelfController; } Service* appletGetServiceSession_WindowController(void) { return &g_appletIWindowController; } Service* appletGetServiceSession_AudioController(void) { return &g_appletIAudioController; } Service* appletGetServiceSession_DisplayController(void) { return &g_appletIDisplayController; } Service* appletGetServiceSession_DebugFunctions(void) { return &g_appletIDebugFunctions; } AppletType appletGetAppletType(void) { return __nx_applet_type; } static void appletCallHook(AppletHookType hookType) { AppletHookCookie* c; for (c = &g_appletFirstHook; c && c->callback; c = c->next) c->callback(hookType, c->param); } void appletHook(AppletHookCookie* cookie, AppletHookFn callback, void* param) { if (callback == NULL) return; AppletHookCookie* hook = &g_appletFirstHook; *cookie = *hook; // Structure copy. hook->next = cookie; hook->callback = callback; hook->param = param; } void appletUnhook(AppletHookCookie* cookie) { AppletHookCookie* hook; for (hook = &g_appletFirstHook; hook; hook = hook->next) { if (hook->next == cookie) { *hook = *cookie; // Structure copy. break; } } } void appletSetThemeColorType(AppletThemeColorType theme) { g_appletThemeColorType = theme; } AppletThemeColorType appletGetThemeColorType(void) { return g_appletThemeColorType; } Result appletSetFocusHandlingMode(AppletFocusHandlingMode mode) { Result rc; bool invals[4]; if (mode >= AppletFocusHandlingMode_Max) return MAKERESULT(Module_Libnx, LibnxError_BadInput); memset(invals, 0, sizeof(invals)); if ((mode == AppletFocusHandlingMode_SuspendHomeSleep) || (mode == AppletFocusHandlingMode_AlwaysSuspend)) { invals[0] = 0; invals[1] = 0; invals[2] = 1; } if (mode != AppletFocusHandlingMode_AlwaysSuspend) { invals[3] = 0; if (mode == AppletFocusHandlingMode_NoSuspend) { invals[0] = 1; invals[1] = 1; invals[2] = 0; } else if (mode == AppletFocusHandlingMode_SuspendHomeSleepNotify) { invals[0] = 1; invals[1] = 0; invals[2] = 1; } } else { invals[3] = 1; } rc = _appletSetFocusHandlingMode(invals[0], invals[1], invals[2]); if (R_SUCCEEDED(rc) && hosversionAtLeast(2,0,0)) rc = _appletSetOutOfFocusSuspendingEnabled(invals[3]); 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, bool autoclear, u32 cmd_id) { Handle tmp_handle = INVALID_HANDLE; Result rc = 0; rc = _appletGetHandle(srv, &tmp_handle, cmd_id); if (R_SUCCEEDED(rc)) eventLoadRemote(out_event, tmp_handle, autoclear); 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 _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 _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 _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 _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 _appletCmdNoIO(Service* srv, u32 cmd_id) { serviceAssumeDomain(srv); return serviceDispatch(srv, cmd_id); } static Result _appletCmdNoInOut64(Service* srv, u64 *out, u32 cmd_id) { serviceAssumeDomain(srv); return serviceDispatchOut(srv, cmd_id, *out); } static Result _appletCmdNoInOut32(Service* srv, u32 *out, u32 cmd_id) { serviceAssumeDomain(srv); return serviceDispatchOut(srv, cmd_id, *out); } 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) { serviceAssumeDomain(srv); return serviceDispatchIn(srv, cmd_id, inval); } 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, u32 cmd_id) { serviceAssumeDomain(srv); return serviceDispatchIn(srv, cmd_id, inval); } static Result _appletCmdInBool(Service* srv, bool inval, u32 cmd_id) { serviceAssumeDomain(srv); return _appletCmdInU8(srv, inval!=0, cmd_id); } 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 _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 _appletCmdInTmemNoOut(Service* srv, TransferMemory *tmem, u32 cmd_id) { serviceAssumeDomain(srv); return _appletCmdInHandle64NoOut(srv, tmem->handle, tmem->size, 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, 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, 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, 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, u32 cmd_id) { serviceAssumeDomain(srv); return serviceDispatchOut(srv, cmd_id, *info); } 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, u32 cmd_id) { struct { 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; } // ICommonStateGetter static Result _appletReceiveMessage(u32 *out) { return _appletCmdNoInOut32(&g_appletICommonStateGetter, out, 1); } static Result _appletGetOperationMode(u8 *out) { return _appletCmdNoInOutU8(&g_appletICommonStateGetter, out, 5); } static Result _appletGetPerformanceMode(u32 *out) { return _appletCmdNoInOut32(&g_appletICommonStateGetter, out, 6); } Result appletGetCradleStatus(u8 *status) { return _appletCmdNoInOutU8(&g_appletICommonStateGetter, status, 7); } Result appletGetBootMode(PmBootMode *mode) { u8 tmp=0; Result rc = _appletCmdNoInOutU8(&g_appletICommonStateGetter, &tmp, 8); if (R_SUCCEEDED(rc) && mode) *mode = tmp; return rc; } static Result _appletGetCurrentFocusState(u8 *out) { return _appletCmdNoInOutU8(&g_appletICommonStateGetter, out, 9); } static Result _appletGetAcquiredSleepLockEvent(Event *out_event) { return _appletGetEvent(&g_appletICommonStateGetter, out_event, false, 13); } static Result _appletWaitAcquiredSleepLockEvent(void) { Result rc=0; Event tmpevent={0}; rc = _appletGetAcquiredSleepLockEvent(&tmpevent); if (R_SUCCEEDED(rc)) rc = eventWait(&tmpevent, U64_MAX); eventClose(&tmpevent); return rc; } Result appletRequestToAcquireSleepLock(void) { Result rc = _appletCmdNoIO(&g_appletICommonStateGetter, 10); if (R_SUCCEEDED(rc)) rc = _appletWaitAcquiredSleepLockEvent(); return rc; } Result appletReleaseSleepLock(void) { return _appletCmdNoIO(&g_appletICommonStateGetter, 11); } Result appletReleaseSleepLockTransiently(void) { Result rc = _appletCmdNoIO(&g_appletICommonStateGetter, 12); if (R_SUCCEEDED(rc)) rc = _appletWaitAcquiredSleepLockEvent(); return rc; } Result appletPushToGeneralChannel(AppletStorage *s) { return _appletCmdInStorage(&g_appletICommonStateGetter, s, 20); } 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, false, 3); if (R_FAILED(rc)) serviceClose(&a->s); return rc; } Result appletGetHomeButtonReaderLockAccessor(AppletLockAccessor *a) { return _appletGetHomeButtonRwLockAccessor(&g_appletICommonStateGetter, a, 30); } 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, false, 3); if (R_FAILED(rc)) serviceClose(&a->s); return rc; } Result appletGetReaderLockAccessorEx(AppletLockAccessor *a, u32 inval) { if (hosversionBefore(2,0,0)) return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer); 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, inval, 31); return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer); } 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); struct { u32 out0; u32 out1; u32 out2; u32 out3; } out; serviceAssumeDomain(&g_appletICommonStateGetter); Result rc = serviceDispatchOut(&g_appletICommonStateGetter, 40, out); if (R_SUCCEEDED(rc)) { if (out0) *out0 = out.out0; if (out1) *out1 = out.out1; if (out2) *out2 = out.out2; if (out3) *out3 = out.out3; } return rc; } Result appletIsVrModeEnabled(bool *out) { if (hosversionBefore(3,0,0)) return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer); return _appletCmdNoInOutBool(&g_appletICommonStateGetter, out, 50); } Result appletSetVrModeEnabled(bool flag) { if (hosversionBefore(6,0,0)) return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer); if (hosversionBefore(7,0,0)) return _appletCmdInBool(&g_appletICommonStateGetter, flag, 51); return _appletCmdNoIO(&g_appletICommonStateGetter, flag ? 53 : 54); } Result appletSetLcdBacklightOffEnabled(bool flag) { if (hosversionBefore(4,0,0)) return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer); return _appletCmdInBool(&g_appletICommonStateGetter, flag, 52); } Result appletIsInControllerFirmwareUpdateSection(bool *out) { if (hosversionBefore(3,0,0)) return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer); return _appletCmdNoInOutBool(&g_appletICommonStateGetter, out, 55); } Result appletGetDefaultDisplayResolution(s32 *width, s32 *height) { if (hosversionBefore(3,0,0)) return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer); return _appletGetResolution(&g_appletICommonStateGetter, width, height, 60); } Result appletGetDefaultDisplayResolutionChangeEvent(Event *out_event) { if (hosversionBefore(3,0,0)) return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer); return _appletGetEvent(&g_appletICommonStateGetter, out_event, true, 61); } Result appletGetHdcpAuthenticationState(s32 *state) { if (hosversionBefore(4,0,0)) return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer); return _appletCmdNoInOut32(&g_appletICommonStateGetter, (u32*)state, 62); } Result appletGetHdcpAuthenticationStateChangeEvent(Event *out_event) { if (hosversionBefore(4,0,0)) return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer); return _appletGetEvent(&g_appletICommonStateGetter, out_event, true, 63); } Result appletSetTvPowerStateMatchingMode(AppletTvPowerStateMatchingMode mode) { if (hosversionBefore(5,0,0)) return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer); return _appletCmdInU32(&g_appletICommonStateGetter, mode, 64); } Result appletGetApplicationIdByContentActionName(u64 *titleID, const char *name) { if (hosversionBefore(5,1,0)) return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer); serviceAssumeDomain(&g_appletICommonStateGetter); return serviceDispatchOut(&g_appletICommonStateGetter, 65, *titleID, .buffer_attrs = { SfBufferAttr_HipcMapAlias | SfBufferAttr_In }, .buffers = { { name, strlen(name)+1 } }, ); } Result appletSetCpuBoostMode(ApmCpuBoostMode mode) { Result rc=0; if (hosversionBefore(7,0,0)) return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer); rc = _appletCmdInU32(&g_appletICommonStateGetter, mode, 66); if (R_SUCCEEDED(rc)) g_appletCpuBoostMode = mode; return rc; } Result appletPerformSystemButtonPressingIfInFocus(AppletSystemButtonType type) { if (hosversionBefore(6,0,0)) return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer); return _appletCmdInU32(&g_appletICommonStateGetter, type, 80); } Result appletSetPerformanceConfigurationChangedNotification(bool flag) { if (hosversionBefore(7,0,0)) return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer); return _appletCmdInBool(&g_appletICommonStateGetter, flag, 90); } Result appletGetCurrentPerformanceConfiguration(u32 *PerformanceConfiguration) { if (hosversionBefore(7,0,0)) return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer); return _appletCmdNoInOut32(&g_appletICommonStateGetter, PerformanceConfiguration, 91); } Result appletGetOperationModeSystemInfo(u32 *info) { if (hosversionBefore(7,0,0)) return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer); return _appletCmdNoInOut32(&g_appletICommonStateGetter, info, 200); } // ISelfController static Result _appletSelfExit(void) { return _appletCmdNoIO(&g_appletISelfController, 0); } Result appletLockExit(void) { return _appletCmdNoIO(&g_appletISelfController, 1); } Result appletUnlockExit(void) { return _appletCmdNoIO(&g_appletISelfController, 2); } Result appletEnterFatalSection(void) { return _appletCmdNoIO(&g_appletISelfController, 3); } Result appletLeaveFatalSection(void) { return _appletCmdNoIO(&g_appletISelfController, 4); } static Result _appletWaitLibraryAppletLaunchableEvent(void) { Result rc=0; if (!eventActive(&g_appletLibraryAppletLaunchableEvent)) rc = _appletGetEvent(&g_appletISelfController, &g_appletLibraryAppletLaunchableEvent, false, 9); if (R_SUCCEEDED(rc)) rc = eventWait(&g_appletLibraryAppletLaunchableEvent, U64_MAX); return rc; } Result appletSetScreenShotPermission(AppletScreenShotPermission permission) { return _appletCmdInU32(&g_appletISelfController, permission, 10); } static Result _appletSetOperationModeChangedNotification(bool flag) { return _appletCmdInBool(&g_appletISelfController, flag, 11); } static Result _appletSetPerformanceModeChangedNotification(bool flag) { return _appletCmdInBool(&g_appletISelfController, flag, 12); } static Result _appletSetFocusHandlingMode(bool inval0, bool inval1, bool inval2) { const struct { u8 inval0; u8 inval1; u8 inval2; } in = { inval0!=0, inval1!=0, inval2!=0 }; serviceAssumeDomain(&g_appletISelfController); return serviceDispatchIn(&g_appletISelfController, 13, in); } Result appletSetRestartMessageEnabled(bool flag) { return _appletCmdInBool(&g_appletISelfController, flag, 14); } Result appletSetScreenShotAppletIdentityInfo(AppletIdentityInfo *info) { if (hosversionBefore(3,0,0)) return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer); serviceAssumeDomain(&g_appletISelfController); return serviceDispatchIn(&g_appletISelfController, 15, *info); } static Result _appletSetOutOfFocusSuspendingEnabled(bool flag) { return _appletCmdInBool(&g_appletISelfController, flag, 16); } Result appletSetControllerFirmwareUpdateSection(bool flag) { if (hosversionBefore(3,0,0)) return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer); return _appletCmdInBool(&g_appletISelfController, flag, 17); } Result appletSetRequiresCaptureButtonShortPressedMessage(bool flag) { if (hosversionBefore(3,0,0)) return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer); return _appletCmdInBool(&g_appletISelfController, flag, 18); } Result appletSetAlbumImageOrientation(AlbumImageOrientation orientation) { if (hosversionBefore(3,0,0)) return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer); return _appletCmdInU32(&g_appletISelfController, orientation, 19); } Result appletSetDesirableKeyboardLayout(u32 layout) { if (hosversionBefore(4,0,0)) return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer); return _appletCmdInU32(&g_appletISelfController, layout, 20); } Result appletCreateManagedDisplayLayer(u64 *out) { return _appletCmdNoInOut64(&g_appletISelfController, out, 40); } Result appletIsSystemBufferSharingEnabled(void) { if (hosversionBefore(4,0,0)) return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer); return _appletCmdNoIO(&g_appletISelfController, 41); } Result appletGetSystemSharedLayerHandle(u64 *SharedBufferHandle, u64 *SharedLayerHandle) { if (hosversionBefore(4,0,0)) return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer); struct { 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; } Result appletGetSystemSharedBufferHandle(u64 *SharedBufferHandle) { if (hosversionBefore(5,0,0)) return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer); return _appletCmdNoInOut64(&g_appletISelfController, SharedBufferHandle, 43); } Result appletSetHandlesRequestToDisplay(bool flag) { return _appletCmdInBool(&g_appletISelfController, flag, 50); } Result appletApproveToDisplay(void) { return _appletCmdNoIO(&g_appletISelfController, 51); } Result appletOverrideAutoSleepTimeAndDimmingTime(s32 inval0, s32 inval1, s32 inval2, s32 inval3) { const struct { s32 inval0; s32 inval1; s32 inval2; s32 inval3; } in = { inval0, inval1, inval2, inval3 }; serviceAssumeDomain(&g_appletISelfController); return serviceDispatchIn(&g_appletISelfController, 60, in); } Result appletSetIdleTimeDetectionExtension(AppletIdleTimeDetectionExtension ext) { return _appletCmdInU32(&g_appletISelfController, ext, 62); } Result appletGetIdleTimeDetectionExtension(AppletIdleTimeDetectionExtension *ext) { return _appletCmdNoInOut32(&g_appletISelfController, ext, 63); } Result appletSetInputDetectionSourceSet(u32 val) { return _appletCmdInU32(&g_appletISelfController, val, 64); } Result appletReportUserIsActive(void) { if (hosversionBefore(2,0,0)) return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer); return _appletCmdNoIO(&g_appletISelfController, 65); } Result appletGetCurrentIlluminance(float *fLux) { if (hosversionBefore(3,0,0)) return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer); serviceAssumeDomain(&g_appletISelfController); return serviceDispatchOut(&g_appletISelfController, 66, *fLux); } Result appletIsIlluminanceAvailable(bool *out) { if (hosversionBefore(3,0,0)) return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer); return _appletCmdNoInOutBool(&g_appletISelfController, out, 67); } Result appletSetAutoSleepDisabled(bool flag) { if (hosversionBefore(5,0,0)) return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer); return _appletCmdInBool(&g_appletISelfController, flag, 68); } Result appletIsAutoSleepDisabled(bool *out) { if (hosversionBefore(5,0,0)) return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer); return _appletCmdNoInOutBool(&g_appletISelfController, out, 69); } Result appletGetCurrentIlluminanceEx(bool *bOverLimit, float *fLux) { if (hosversionBefore(5,0,0)) return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer); struct { 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; } Result appletSetWirelessPriorityMode(AppletWirelessPriorityMode mode) { if (hosversionBefore(4,0,0)) return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer); return _appletCmdInU32(&g_appletISelfController, mode, 80); } Result appletSetAlbumImageTakenNotificationEnabled(bool flag) { if (hosversionBefore(7,0,0)) return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer); return _appletCmdInBool(&g_appletISelfController, flag, 100); } static Result _appletGetAccumulatedSuspendedTickValue(u64 *tick) { if (hosversionBefore(6,0,0)) return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer); return _appletCmdNoInOut64(&g_appletISelfController, tick, 90); } static Result _appletGetAccumulatedSuspendedTickChangedEvent(Event *out_event) { if (hosversionBefore(6,0,0)) return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer); return _appletGetEvent(&g_appletISelfController, out_event, true, 91); } Result appletGetProgramTotalActiveTime(u64 *activeTime) { if (!g_appletSuspendedTickInitialized) return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); Result rc=0; u64 suspendedTick = 0; while (R_SUCCEEDED(eventWait(&g_appletSuspendedTickEvent, 0))) { rc = _appletGetAccumulatedSuspendedTickValue(&suspendedTick); if (R_FAILED(rc)) return rc; if (suspendedTick > g_appletSuspendedTick) g_appletSuspendedTick = suspendedTick; } *activeTime = armTicksToNs(armGetSystemTick() - g_appletInitTickBase - g_appletSuspendedTick); return rc; } Result appletSetApplicationAlbumUserData(const void* buffer, size_t size) { if (hosversionBefore(8,0,0)) return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer); serviceAssumeDomain(&g_appletISelfController); return serviceDispatch(&g_appletISelfController, 110, .buffer_attrs = { SfBufferAttr_HipcAutoSelect | SfBufferAttr_In }, .buffers = { { buffer, size } }, ); } // IWindowController static Result _appletGetAppletResourceUserId(u64 *out) { return _appletCmdNoInOut64(&g_appletIWindowController, out, 1); } Result appletGetAppletResourceUserId(u64 *out) { if (!serviceIsActive(&g_appletSrv)) return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); *out = g_appletResourceUserId; return 0; } Result appletGetAppletResourceUserIdOfCallerApplet(u64 *out) { if (hosversionBefore(6,0,0)) return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer); if (!serviceIsActive(&g_appletSrv)) return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); return _appletCmdNoInOut64(&g_appletIWindowController, out, 2); } static Result _appletAcquireForegroundRights(void) { return _appletCmdNoIO(&g_appletIWindowController, 10); } Result appletSetAppletWindowVisibility(bool flag) { if (hosversionBefore(7,0,0)) return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer); return _appletCmdInBool(&g_appletIWindowController, flag, 20); } Result appletSetAppletGpuTimeSlice(s64 val) { if (hosversionBefore(7,0,0)) return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer); return _appletCmdInU64(&g_appletIWindowController, val, 21); } // IAudioController Result appletSetExpectedMasterVolume(float mainAppletVolume, float libraryAppletVolume) { const struct { float mainAppletVolume; float libraryAppletVolume; } in = { mainAppletVolume, libraryAppletVolume }; serviceAssumeDomain(&g_appletIAudioController); return serviceDispatchIn(&g_appletIAudioController, 0, in); } Result appletGetExpectedMasterVolume(float *mainAppletVolume, float *libraryAppletVolume) { Result rc=0; if (mainAppletVolume) rc = _appletCmdNoInOut32(&g_appletIAudioController, (u32*)mainAppletVolume, 1); // GetMainAppletExpectedMasterVolume if (R_SUCCEEDED(rc) && libraryAppletVolume) rc = _appletCmdNoInOut32(&g_appletIAudioController, (u32*)libraryAppletVolume, 2); // GetLibraryAppletExpectedMasterVolume return rc; } Result appletChangeMainAppletMasterVolume(float volume, u64 unk) { const struct { float volume; u64 unk; } in = { volume, unk }; serviceAssumeDomain(&g_appletIAudioController); return serviceDispatchIn(&g_appletIAudioController, 3, in); } Result appletSetTransparentVolumeRate(float val) { serviceAssumeDomain(&g_appletIAudioController); return serviceDispatchIn(&g_appletIAudioController, 4, val); } // IDisplayController Result appletUpdateLastForegroundCaptureImage(void) { return _appletCmdNoIO(&g_appletIDisplayController, 1); } Result appletUpdateCallerAppletCaptureImage(void) { return _appletCmdNoIO(&g_appletIDisplayController, 4); } 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; } Result appletGetLastForegroundCaptureImageEx(void* buffer, size_t size, bool *flag) { return _appletGetCaptureImageEx(buffer, size, flag, 5); } Result appletGetLastApplicationCaptureImageEx(void* buffer, size_t size, bool *flag) { return _appletGetCaptureImageEx(buffer, size, flag, 6); } Result appletGetCallerAppletCaptureImageEx(void* buffer, size_t size, bool *flag) { return _appletGetCaptureImageEx(buffer, size, flag, 7); } Result appletTakeScreenShotOfOwnLayer(bool flag, AppletCaptureSharedBuffer captureBuf) { if (hosversionBefore(2,0,0)) return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer); const struct { u8 flag; s32 captureBuf; } in = { flag!=0, captureBuf }; 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); const struct { s32 dstCaptureBuf; s32 srcCaptureBuf; } in = { dstCaptureBuf, srcCaptureBuf }; 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); const struct { u8 flag; s32 captureBuf; u32 color; } in = { flag!=0, captureBuf, color }; serviceAssumeDomain(&g_appletIDisplayController); return serviceDispatchIn(&g_appletIDisplayController, 20, in); } Result appletClearAppletTransitionBuffer(u32 color) { if (hosversionBefore(3,0,0)) return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer); return _appletCmdInU32(&g_appletIDisplayController, color, 21); } static Result _appletAcquireCaptureSharedBuffer(bool *flag, s32 *id, u64 cmd_id) { struct { 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; } Result appletAcquireLastApplicationCaptureSharedBuffer(bool *flag, s32 *id) { if (hosversionBefore(4,0,0)) return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer); return _appletAcquireCaptureSharedBuffer(flag, id, 22); } Result appletReleaseLastApplicationCaptureSharedBuffer(void) { if (hosversionBefore(4,0,0)) return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer); return _appletCmdNoIO(&g_appletIDisplayController, 23); } Result appletAcquireLastForegroundCaptureSharedBuffer(bool *flag, s32 *id) { if (hosversionBefore(4,0,0)) return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer); return _appletAcquireCaptureSharedBuffer(flag, id, 24); } Result appletReleaseLastForegroundCaptureSharedBuffer(void) { if (hosversionBefore(4,0,0)) return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer); return _appletCmdNoIO(&g_appletIDisplayController, 25); } Result appletAcquireCallerAppletCaptureSharedBuffer(bool *flag, s32 *id) { if (hosversionBefore(4,0,0)) return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer); return _appletAcquireCaptureSharedBuffer(flag, id, 26); } Result appletReleaseCallerAppletCaptureSharedBuffer(void) { if (hosversionBefore(4,0,0)) return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer); return _appletCmdNoIO(&g_appletIDisplayController, 27); } Result appletTakeScreenShotOfOwnLayerEx(bool flag0, bool immediately, AppletCaptureSharedBuffer captureBuf) { if (hosversionBefore(6,0,0)) return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer); const struct { u8 flag0; u8 immediately; s32 captureBuf; } in = { flag0!=0, immediately!=0, captureBuf }; serviceAssumeDomain(&g_appletIDisplayController); return serviceDispatchIn(&g_appletIDisplayController, 28, in); } // IProcessWindingController static Result _appletGetLaunchReason(AppletProcessLaunchReason *reason) { if (__nx_applet_type != AppletType_LibraryApplet) return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); serviceAssumeDomain(&g_appletIProcessWindingController); return serviceDispatchOut(&g_appletIProcessWindingController, 0, *reason); } static Result _appletOpenCallingLibraryApplet(AppletHolder *h) { if (__nx_applet_type != AppletType_LibraryApplet) return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); return _appletOpenExistingLibraryApplet(h, &g_appletIProcessWindingController, 11); } Result appletPushContext(AppletStorage *s) { if (__nx_applet_type != AppletType_LibraryApplet) return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); return _appletCmdInStorage(&g_appletIProcessWindingController, s, 21); } Result appletPopContext(AppletStorage *s) { if (__nx_applet_type != AppletType_LibraryApplet) return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); return _appletCmdNoInOutStorage(&g_appletIProcessWindingController, s, 22); } static Result _appletWindAndDoReserved(void) { if (__nx_applet_type != AppletType_LibraryApplet) return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); return _appletCmdNoIO(&g_appletIProcessWindingController, 30); } static Result _appletReserveToStartAndWaitAndUnwindThis(AppletHolder *h) { if (__nx_applet_type != AppletType_LibraryApplet) return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); return _appletCmdInSession(&g_appletIProcessWindingController, &h->s, 40); } static Result _appletReserveToStartAndWait(AppletHolder *h) { if (__nx_applet_type != AppletType_LibraryApplet) return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); if (hosversionBefore(4,0,0)) return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer); return _appletCmdInSession(&g_appletIProcessWindingController, &h->s, 41); } // LockAccessor void appletLockAccessorClose(AppletLockAccessor *a) { eventClose(&a->event); serviceClose(&a->s); } static Result _appletLockAccessorTryLock(AppletLockAccessor *a, bool get_handle, Handle* handle_out, bool *outflag) { 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; } Result appletLockAccessorTryLock(AppletLockAccessor *a, bool *flag) { Result rc=0; if (!serviceIsActive(&a->s)) return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); rc = eventWait(&a->event, 0); if (R_SUCCEEDED(rc)) rc = _appletLockAccessorTryLock(a, false, NULL, flag); return rc; } Result appletLockAccessorLock(AppletLockAccessor *a) { Result rc=0; bool flag=0; if (!serviceIsActive(&a->s)) return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); do { rc = eventWait(&a->event, U64_MAX); if (R_SUCCEEDED(rc)) rc = _appletLockAccessorTryLock(a, false, NULL, &flag); if (R_FAILED(rc)) break; } while(!flag); return rc; } Result appletLockAccessorUnlock(AppletLockAccessor *a) { if (!serviceIsActive(&a->s)) return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); return _appletCmdNoIO(&a->s, 2); } // ILibraryAppletCreator static Result _appletCreateLibraryApplet(Service* srv_out, AppletId id, LibAppletMode mode) { const struct { u32 id; u32 mode; } in = { id, mode }; serviceAssumeDomain(&g_appletILibraryAppletCreator); return serviceDispatchIn(&g_appletILibraryAppletCreator, 0, in, .out_num_objects = 1, .out_objects = srv_out, ); } static Result _appletGetIndirectLayerConsumerHandle(Service* srv, u64 *out) { Result rc; u64 AppletResourceUserId; rc = appletGetAppletResourceUserId(&AppletResourceUserId); if (R_FAILED(rc)) return rc; serviceAssumeDomain(srv); return serviceDispatchInOut(srv, 160, AppletResourceUserId, *out, .in_send_pid = true, ); } static Result _appletHolderCreateState(AppletHolder *h, LibAppletMode mode, bool creating_self) { Result rc=0; h->mode = mode; h->creating_self = creating_self; 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); return rc; } static Result _appletHolderCreate(AppletHolder *h, AppletId id, LibAppletMode mode, bool creating_self) { Result rc=0; memset(h, 0, sizeof(AppletHolder)); if (!h->creating_self) rc = _appletWaitLibraryAppletLaunchableEvent(); if (R_SUCCEEDED(rc)) rc = _appletCreateLibraryApplet(&h->s, id, mode); if (R_SUCCEEDED(rc)) rc = _appletHolderCreateState(h, mode, creating_self); return rc; } static Result _appletOpenExistingLibraryApplet(AppletHolder *h, Service* srv, u32 cmd_id) { if (__nx_applet_type != AppletType_LibraryApplet) return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); Result rc=0; LibAppletInfo info={0}; memset(h, 0, sizeof(AppletHolder)); rc = _appletGetSession(srv, &h->s, cmd_id); if (R_SUCCEEDED(rc)) rc = appletHolderGetLibraryAppletInfo(h, &info); if (R_SUCCEEDED(rc)) rc = _appletHolderCreateState(h, info.mode, false); if (R_FAILED(rc)) appletHolderClose(h); return rc; } Result appletCreateLibraryApplet(AppletHolder *h, AppletId id, LibAppletMode mode) { return _appletHolderCreate(h, id, mode, false); } Result appletCreateLibraryAppletSelf(AppletHolder *h, AppletId id, LibAppletMode mode) { return _appletHolderCreate(h, id, mode, true); } Result appletTerminateAllLibraryApplets(void) { return _appletCmdNoIO(&g_appletILibraryAppletCreator, 1); } Result appletAreAnyLibraryAppletsLeft(bool *out) { return _appletCmdNoInOutBool(&g_appletILibraryAppletCreator, out, 2); } // ILibraryAppletAccessor void appletHolderClose(AppletHolder *h) { eventClose(&h->PopInteractiveOutDataEvent); eventClose(&h->StateChangedEvent); serviceClose(&h->s); memset(h, 0, sizeof(AppletHolder)); } bool appletHolderActive(AppletHolder *h) { return serviceIsActive(&h->s); } Result appletHolderGetIndirectLayerConsumerHandle(AppletHolder *h, u64 *out) { if (!serviceIsActive(&h->s)) return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); if (h->mode!=LibAppletMode_BackgroundIndirect) return MAKERESULT(Module_Libnx, LibnxError_BadInput); if (hosversionBefore(2,0,0)) return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer); if (out) *out = h->layer_handle; return 0; } Result appletHolderStart(AppletHolder *h) { Result rc=0; if (!serviceIsActive(&h->s)) return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); if (!h->creating_self) rc = _appletWaitLibraryAppletLaunchableEvent(); if (R_SUCCEEDED(rc)) rc = _appletCmdNoIO(&h->s, 10);//Start return rc; } Result appletHolderJump(AppletHolder *h) { Result rc=0; if (!serviceIsActive(&h->s)) return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); if (!h->creating_self) rc = _appletWaitLibraryAppletLaunchableEvent(); if (hosversionBefore(4,0,0)) rc = _appletReserveToStartAndWaitAndUnwindThis(h); else rc = _appletReserveToStartAndWait(h); if (R_SUCCEEDED(rc)) rc = _appletWindAndDoReserved(); if (R_SUCCEEDED(rc)) _appletInfiniteSleepLoop(); return rc; } Result appletHolderRequestExit(AppletHolder *h) { Result rc=0; if (!serviceIsActive(&h->s)) return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); if (!appletHolderCheckFinished(h)) rc = _appletCmdNoIO(&h->s, 20);//RequestExit return rc; } Result appletHolderTerminate(AppletHolder *h) { if (!serviceIsActive(&h->s)) return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); return _appletCmdNoIO(&h->s, 25);//Terminate } static Result _appletAccessorRequestExitOrTerminate(Service* srv, u64 timeout) { Result rc=0; Event StateChangedEvent={0}; if (!serviceIsActive(srv)) return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); rc = _appletGetEvent(srv, &StateChangedEvent, false, 0);//GetAppletStateChangedEvent if (R_SUCCEEDED(rc)) rc = _appletCmdNoIO(srv, 20);//RequestExit if (R_SUCCEEDED(rc)) { rc = eventWait(&StateChangedEvent, timeout); if (R_FAILED(rc) && R_VALUE(rc) == KERNELRESULT(TimedOut)) rc = _appletCmdNoIO(srv, 25);//Terminate } eventClose(&StateChangedEvent); return rc; } Result appletHolderRequestExitOrTerminate(AppletHolder *h, u64 timeout) { return _appletAccessorRequestExitOrTerminate(&h->s, timeout); } void appletHolderJoin(AppletHolder *h) { Result rc=0; LibAppletExitReason res = LibAppletExitReason_Normal; u32 desc=0; eventWait(&h->StateChangedEvent, U64_MAX); rc = _appletCmdNoIO(&h->s, 30);//GetResult if (R_FAILED(rc)) { res = LibAppletExitReason_Unexpected; if (R_MODULE(rc) == 128) { desc = R_DESCRIPTION(rc); if (desc == 22) res = LibAppletExitReason_Canceled; else { if (desc >= 0x14 && desc < 0x32)res = LibAppletExitReason_Abnormal; } } } h->exitreason = res; } bool appletHolderCheckFinished(AppletHolder *h) { return R_SUCCEEDED(eventWait(&h->StateChangedEvent, 0)); } u32 appletHolderGetExitReason(AppletHolder *h) { return h->exitreason; } Result appletHolderSetOutOfFocusApplicationSuspendingEnabled(AppletHolder *h, bool flag) { if (!serviceIsActive(&g_appletSrv) || !_appletIsApplication()) return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); return _appletCmdInBool(&h->s, flag, 50); } static Result _appletHolderGetPopInteractiveOutDataEvent(AppletHolder *h) { if (eventActive(&h->PopInteractiveOutDataEvent)) return 0; return _appletGetEvent(&h->s, &h->PopInteractiveOutDataEvent, false, 106); } bool appletHolderWaitInteractiveOut(AppletHolder *h) { Result rc=0; s32 idx = 0; rc = _appletHolderGetPopInteractiveOutDataEvent(h); if (R_FAILED(rc)) return false; rc = waitMulti(&idx, U64_MAX, waiterForEvent(&h->PopInteractiveOutDataEvent), waiterForEvent(&h->StateChangedEvent)); if (R_FAILED(rc)) return false; return idx==0; } Result appletHolderPushInData(AppletHolder *h, AppletStorage *s) { return _appletCmdInStorage(&h->s, s, 100); } Result appletHolderPopOutData(AppletHolder *h, AppletStorage *s) { return _appletCmdNoInOutStorage(&h->s, s, 101); } Result appletHolderPushExtraStorage(AppletHolder *h, AppletStorage *s) { return _appletCmdInStorage(&h->s, s, 102); } Result appletHolderPushInteractiveInData(AppletHolder *h, AppletStorage *s) { return _appletCmdInStorage(&h->s, s, 103); } Result appletHolderPopInteractiveOutData(AppletHolder *h, AppletStorage *s) { return _appletCmdNoInOutStorage(&h->s, s, 104); } Result appletHolderGetLibraryAppletInfo(AppletHolder *h, LibAppletInfo *info) { return _appletGetLibraryAppletInfo(&h->s, info, 120); } // (ILibraryAppletCreator ->) IStorage Result appletCreateStorage(AppletStorage *s, s64 size) { memset(s, 0, sizeof(AppletStorage)); return _appletGetSessionIn64(&g_appletILibraryAppletCreator, &s->s, size, 10); } static Result _appletCreateTransferMemoryStorage(Service* srv_out, TransferMemory *tmem, bool writable) { const struct { u8 writable; u64 size; } in = { writable!=0, tmem->size }; 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) { Result rc=0; memset(s, 0, sizeof(AppletStorage)); if (buffer==NULL) rc = tmemCreate(&s->tmem, size, Perm_None); else rc = tmemCreateFromMemory(&s->tmem, buffer, size, Perm_None); if (R_FAILED(rc)) return rc; rc = _appletCreateTransferMemoryStorage(&s->s, &s->tmem, writable); if (R_FAILED(rc)) tmemClose(&s->tmem); return rc; } Result appletCreateHandleStorage(AppletStorage *s, s64 inval, Handle handle) { if (hosversionBefore(2,0,0)) return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer); return _appletCmdInHandle64OutSession(&g_appletILibraryAppletCreator, &s->s, handle, inval, 12); } Result appletCreateHandleStorageTmem(AppletStorage *s, void* buffer, s64 size) { Result rc=0; memset(s, 0, sizeof(AppletStorage)); if (buffer==NULL) rc = tmemCreate(&s->tmem, size, Perm_None); else rc = tmemCreateFromMemory(&s->tmem, buffer, size, Perm_None); if (R_FAILED(rc)) return rc; rc = appletCreateHandleStorage(s, s->tmem.size, s->tmem.handle); if (R_FAILED(rc)) tmemClose(&s->tmem); return rc; } void appletStorageClose(AppletStorage *s) { serviceClose(&s->s); } void appletStorageCloseTmem(AppletStorage *s) { tmemClose(&s->tmem); } static Result _appletStorageAccessorRW(Service* srv, size_t ipcbufsize, s64 offset, void* buffer, size_t size, bool rw) { 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) { Result rc=0; Service tmp_srv;//IStorageAccessor if (!serviceIsActive(&s->s)) return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); rc = _appletGetSession(&s->s, &tmp_srv, 0);//Open if (R_FAILED(rc)) return rc; rc = _appletCmdNoInOut64(&tmp_srv, (u64*)size, 0); serviceClose(&tmp_srv); return rc; } static Result _appletStorageRW(AppletStorage *s, s64 offset, void* buffer, size_t size, bool rw) { Result rc=0; Service tmp_srv;//IStorageAccessor if (!serviceIsActive(&s->s)) return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); rc = _appletGetSession(&s->s, &tmp_srv, 0);//Open if (R_FAILED(rc)) return rc; if (R_SUCCEEDED(rc)) rc = _appletStorageAccessorRW(&tmp_srv, tmp_srv.pointer_buffer_size, offset, buffer, size, rw); serviceClose(&tmp_srv); return rc; } Result appletStorageWrite(AppletStorage *s, s64 offset, const void* buffer, size_t size) { return _appletStorageRW(s, offset, (void*)buffer, size, true); } Result appletStorageRead(AppletStorage *s, s64 offset, void* buffer, size_t size) { return _appletStorageRW(s, offset, buffer, size, false); } static Result _appletStorageGetHandle(Service* srv, s64 *out, Handle *handle) { 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; } Result appletStorageGetHandle(AppletStorage *s, s64 *out, Handle *handle) { Result rc=0; Service tmp_srv;//ITransferStorageAccessor if (!serviceIsActive(&s->s)) return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); if (hosversionBefore(2,0,0)) return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer); rc = _appletGetSession(&s->s, &tmp_srv, 1);//OpenTransferStorage if (R_FAILED(rc)) return rc; rc = _appletStorageGetHandle(&tmp_srv, out, handle); serviceClose(&tmp_srv); return rc; } Result appletStorageMap(AppletStorage *s, void** addr, size_t *size) { Result rc=0; s64 tmpsize=0; Handle tmphandle=0; rc = appletStorageGetHandle(s, &tmpsize, &tmphandle); if (R_FAILED(rc)) return rc; tmemLoadRemote(&s->tmem, tmphandle, tmpsize, Perm_None); rc = tmemMap(&s->tmem); if (R_FAILED(rc)) tmemClose(&s->tmem); if (R_SUCCEEDED(rc)) { if (addr) *addr = s->tmem.map_addr; if (size) *size = s->tmem.size; } return rc; } // IApplicationFunctions Result appletPopLaunchParameter(AppletStorage *s, AppletLaunchParameterKind kind) { if (!serviceIsActive(&g_appletSrv) || !_appletIsApplication()) return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); 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 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+] const struct { u32 val0, val1; u64 titleID; } in = { attr->unk_x0, attr->unk_x4, titleID }; 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+] return _appletCmdInU64(&g_appletIFunctions, titleID, 12); } static Result _appletCreateApplicationAndRequestToStartForQuest(u64 titleID, const AppletApplicationAttributeForQuest *attr) { // [4.0.0+] const struct { u32 val0, val1; u64 titleID; } in = { attr->unk_x0, attr->unk_x4, titleID }; serviceAssumeDomain(&g_appletIFunctions); return serviceDispatchIn(&g_appletIFunctions, 13, in); } static Result _appletCreateApplicationWithAttributeAndPushAndRequestToStartForQuest(u64 titleID, AppletStorage* s, const AppletApplicationAttribute *attr) { // [7.0.0+] 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+] 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) { AppletStorage tmpstorage={0}; Result rc=0; bool is_libraryapplet = hosversionAtLeast(5,0,0) && __nx_applet_type == AppletType_LibraryApplet; if (!serviceIsActive(&g_appletSrv) || (!_appletIsApplication() && !is_libraryapplet)) return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); if (s && !serviceIsActive(&s->s)) return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); if ((hosversionBefore(4,0,0) || is_libraryapplet) && s==NULL) { s = &tmpstorage; rc = appletCreateStorage(&tmpstorage, 0); if (R_FAILED(rc)) return rc; } if (is_libraryapplet) { rc = _appletCreateApplicationAndPushAndRequestToStart(&g_appletILibraryAppletSelfAccessor, titleID, s, 90); } else { if (hosversionAtLeast(4,0,0) && s==NULL) { rc = _appletCreateApplicationAndRequestToStart(titleID); } else { rc = _appletCreateApplicationAndPushAndRequestToStart(&g_appletIFunctions, titleID, s, 10); } } if (s) appletStorageClose(s); return rc; } Result appletRequestLaunchApplicationForQuest(u64 titleID, AppletStorage* s, const AppletApplicationAttributeForQuest *attr) { AppletStorage tmpstorage={0}; AppletApplicationAttribute appattr={0}; Result rc=0; if (!serviceIsActive(&g_appletSrv) || !_appletIsApplication()) return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); if (s && !serviceIsActive(&s->s)) return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); if (hosversionBefore(4,0,0) && s==NULL) { s = &tmpstorage; rc = appletCreateStorage(&tmpstorage, 0); if (R_FAILED(rc)) return rc; } if (hosversionAtLeast(7,0,0)) { appattr.unk_x0 = attr->unk_x0; appattr.unk_x4 = attr->unk_x4; appattr.volume = attr->volume; } if (hosversionAtLeast(4,0,0) && s==NULL) { if (hosversionAtLeast(7,0,0)) rc = _appletCreateApplicationWithAttributeAndRequestToStartForQuest(titleID, &appattr); else rc = _appletCreateApplicationAndRequestToStartForQuest(titleID, attr); } else { if (hosversionBefore(3,0,0)) rc = MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer); if (R_SUCCEEDED(rc)) { if (hosversionAtLeast(7,0,0)) rc = _appletCreateApplicationWithAttributeAndPushAndRequestToStartForQuest(titleID, s, &appattr); else rc = _appletCreateApplicationAndPushAndRequestToStartForQuest(titleID, s, attr); } } if (s) appletStorageClose(s); return rc; } Result appletGetDesiredLanguage(u64 *LanguageCode) { if (!serviceIsActive(&g_appletSrv) || !_appletIsApplication()) return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); return _appletCmdNoInOut64(&g_appletIFunctions, LanguageCode, 21); } Result appletSetTerminateResult(Result res) { if (!serviceIsActive(&g_appletSrv) || !_appletIsApplication()) return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); return _appletCmdInU32(&g_appletIFunctions, res, 22); } Result appletGetDisplayVersion(char *displayVersion) { char out[0x10]={0}; if (displayVersion) memset(displayVersion, 0, 0x10); if (!serviceIsActive(&g_appletSrv) || !_appletIsApplication()) return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); serviceAssumeDomain(&g_appletIFunctions); Result rc = serviceDispatchOut(&g_appletIFunctions, 23, out); if (R_SUCCEEDED(rc) && displayVersion) { strncpy(displayVersion, out, 0x10); displayVersion[0xf] = 0; } return rc; } Result appletGetLaunchStorageInfoForDebug(FsStorageId *app_storageId, FsStorageId *update_storageId) { if (!serviceIsActive(&g_appletSrv) || !_appletIsApplication()) return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); if (hosversionBefore(2,0,0)) return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer); struct { 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; } Result appletBeginBlockingHomeButtonShortAndLongPressed(s64 val) { if (!serviceIsActive(&g_appletSrv) || !_appletIsApplication()) return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); return _appletCmdInU64(&g_appletIFunctions, val, 30); } Result appletEndBlockingHomeButtonShortAndLongPressed(void) { if (!serviceIsActive(&g_appletSrv) || !_appletIsApplication()) return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); return _appletCmdNoIO(&g_appletIFunctions, 31); } Result appletBeginBlockingHomeButton(s64 val) { if (!serviceIsActive(&g_appletSrv) || !_appletIsApplication()) return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); return _appletCmdInU64(&g_appletIFunctions, val, 32); } Result appletEndBlockingHomeButton(void) { if (!serviceIsActive(&g_appletSrv) || !_appletIsApplication()) return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); return _appletCmdNoIO(&g_appletIFunctions, 33); } void appletNotifyRunning(bool *out) { if (__nx_applet_type!=AppletType_Application || g_appletNotifiedRunning) return; g_appletNotifiedRunning = 1; Result rc = _appletCmdNoInOutBool(&g_appletIFunctions, out, 40); if (R_FAILED(rc)) fatalSimple(MAKERESULT(Module_Libnx, LibnxError_BadAppletNotifyRunning)); } Result appletGetPseudoDeviceId(u128 *out) { if (!serviceIsActive(&g_appletSrv) || !_appletIsApplication()) return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); if (hosversionBefore(2,0,0)) return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer); serviceAssumeDomain(&g_appletIFunctions); return serviceDispatchOut(&g_appletIFunctions, 50, *out); } Result appletSetMediaPlaybackState(bool state) { if (!serviceIsActive(&g_appletSrv)) return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); if (!_appletIsApplication()) return _appletCmdInBool(&g_appletISelfController, state, 61);//SetMediaPlaybackState return _appletCmdInBool(&g_appletIFunctions, state, 60);//SetMediaPlaybackStateForApplication } Result appletIsGamePlayRecordingSupported(bool *flag) { if (!serviceIsActive(&g_appletSrv) || !_appletIsRegularApplication()) return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); if (hosversionBefore(3,0,0)) return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer); return _appletCmdNoInOutBool(&g_appletIFunctions, flag, 65); } static Result _appletInitializeGamePlayRecording(TransferMemory *tmem) { if (!serviceIsActive(&g_appletSrv) || !_appletIsRegularApplication()) return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); if (hosversionBefore(3,0,0)) return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer); return _appletCmdInTmemNoOut(&g_appletIFunctions, tmem, 66); } Result appletSetGamePlayRecordingState(bool state) { 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); return _appletCmdInU32(&g_appletIFunctions, state!=0, 67); } Result appletInitializeGamePlayRecording(void) { Result rc=0; size_t size = 0x6000000; g_appletRecordingInitialized = 0; //These checks are done in the called applet funcs, but do it here too so that tmemCreate() doesn't run when it's not needed. if (!serviceIsActive(&g_appletSrv) || !_appletIsRegularApplication()) return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); if (hosversionBefore(3,0,0)) return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer); rc = tmemCreate(&g_appletRecordingTmem, size, Perm_None); if (R_FAILED(rc)) return rc; rc = _appletInitializeGamePlayRecording(&g_appletRecordingTmem); if (R_FAILED(rc)) { tmemClose(&g_appletRecordingTmem); return rc; } g_appletRecordingInitialized = 1; rc = appletSetGamePlayRecordingState(1); if (R_SUCCEEDED(rc)) g_appletRecordingInitialized = 2; return rc; } Result appletRequestFlushGamePlayingMovieForDebug(void) { if (!serviceIsActive(&g_appletSrv) || !_appletIsApplication()) return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); if (hosversionBefore(4,0,0)) return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer); return _appletCmdNoIO(&g_appletIFunctions, 68); } Result appletRequestToShutdown(void) { Result rc=0; if (!serviceIsActive(&g_appletSrv) || !_appletIsApplication()) return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); if (hosversionBefore(3,0,0)) return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer); rc = _appletCmdNoIO(&g_appletIFunctions, 70); if (R_SUCCEEDED(rc)) _appletInfiniteSleepLoop(); return rc; } Result appletRequestToReboot(void) { Result rc=0; if (!serviceIsActive(&g_appletSrv) || !_appletIsApplication()) return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); if (hosversionBefore(3,0,0)) return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer); rc = _appletCmdNoIO(&g_appletIFunctions, 71); if (R_SUCCEEDED(rc)) _appletInfiniteSleepLoop(); return rc; } Result appletExitAndRequestToShowThanksMessage(void) { Result rc=0; if (!serviceIsActive(&g_appletSrv) || !_appletIsApplication()) return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); if (hosversionBefore(4,0,0)) return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer); rc = _appletCmdNoIO(&g_appletIFunctions, 80); if (R_SUCCEEDED(rc)) _appletInfiniteSleepLoop(); return rc; } static Result _appletInitializeApplicationCopyrightFrameBuffer(TransferMemory *tmem, s32 width, s32 height) { const struct { s32 width; s32 height; u64 size; } in = { width, height, tmem->size }; serviceAssumeDomain(&g_appletIFunctions); return serviceDispatchIn(&g_appletIFunctions, 100, in, .in_num_handles = 1, .in_handles = { tmem->handle }, ); } Result appletInitializeApplicationCopyrightFrameBuffer(void) { Result rc=0; s32 width = 1280; s32 height = 720; size_t size = 0x3C0000; if (!serviceIsActive(&g_appletSrv) || !_appletIsApplication()) return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); if (hosversionBefore(5,0,0)) return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer); if (g_appletCopyrightInitialized) return MAKERESULT(Module_Libnx, LibnxError_AlreadyInitialized); rc = tmemCreate(&g_appletCopyrightTmem, size, Perm_None); if (R_FAILED(rc)) return rc; rc = _appletInitializeApplicationCopyrightFrameBuffer(&g_appletCopyrightTmem, width, height); if (R_FAILED(rc)) { tmemClose(&g_appletCopyrightTmem); return rc; } g_appletCopyrightInitialized = 1; return rc; } Result appletSetApplicationCopyrightImage(const void* buffer, size_t size, s32 x, s32 y, s32 width, s32 height, s32 mode) { if (!serviceIsActive(&g_appletSrv) || !_appletIsApplication()) return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); if (hosversionBefore(5,0,0)) return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer); const struct { s32 x; s32 y; s32 width; s32 height; s32 mode; } in = { x, y, width, height, mode }; serviceAssumeDomain(&g_appletIFunctions); return serviceDispatchIn(&g_appletIFunctions, 101, in, .buffer_attrs = { SfBufferAttr_HipcMapTransferAllowsNonSecure | SfBufferAttr_HipcMapAlias | SfBufferAttr_In }, .buffers = { { buffer, size } }, ); } Result appletSetApplicationCopyrightVisibility(bool visible) { if (!serviceIsActive(&g_appletSrv) || !_appletIsApplication()) return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); if (hosversionBefore(5,0,0)) return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer); return _appletCmdInBool(&g_appletIFunctions, visible, 102); } //Official sw has these under 'pdm'. Result appletQueryApplicationPlayStatistics(PdmApplicationPlayStatistics *stats, const u64 *titleIDs, s32 count, s32 *total_out) { if (!serviceIsActive(&g_appletSrv) || !_appletIsApplication()) return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); if (hosversionBefore(5,0,0)) return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer); 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) { if (!serviceIsActive(&g_appletSrv) || !_appletIsApplication()) return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); if (hosversionBefore(6,0,0)) return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer); 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) { if (!serviceIsActive(&g_appletSrv) || !_appletIsApplication()) return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); if (hosversionBefore(5,0,0)) return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer); const struct { u32 kind; u64 inval; } in = { kind, inval }; serviceAssumeDomain(&g_appletIFunctions); return serviceDispatchIn(&g_appletIFunctions, 120, in); } static Result _appletClearUserChannel(void) { if (!serviceIsActive(&g_appletSrv) || !_appletIsApplication()) return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); if (hosversionBefore(5,0,0)) return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer); return _appletCmdNoIO(&g_appletIFunctions, 121); } static Result _appletUnpopToUserChannel(AppletStorage *s) { if (!serviceIsActive(&g_appletSrv) || !_appletIsApplication()) return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); if (hosversionBefore(5,0,0)) return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer); return _appletCmdInStorage(&g_appletIFunctions, s, 122); } static Result _appletExecuteProgram(AppletProgramSpecifyKind kind, u64 inval, const void* buffer, size_t size) { Result rc=0; AppletStorage storage={0}; if (size > 0x1000) return MAKERESULT(Module_Libnx, LibnxError_BadInput); if (buffer!=NULL && size!=0) { rc = appletCreateStorage(&storage, size); if (R_SUCCEEDED(rc)) rc = appletStorageWrite(&storage, 0, buffer, size); } if (R_SUCCEEDED(rc)) rc = _appletClearUserChannel(); if (R_SUCCEEDED(rc) && buffer!=0 && size!=0) rc = _appletUnpopToUserChannel(&storage); if (R_SUCCEEDED(rc)) rc = _appletExecuteProgramCmd(kind, inval); appletStorageClose(&storage); return rc; } Result appletExecuteProgram(s32 programIndex, const void* buffer, size_t size) { Result rc=0; if (programIndex<0 || programIndex>0xff) rc = MAKERESULT(Module_Libnx, LibnxError_BadInput); if (R_SUCCEEDED(rc)) rc = _appletExecuteProgram(AppletProgramSpecifyKind_ExecuteProgram, (u64)programIndex, buffer, size); if (R_SUCCEEDED(rc)) _appletInfiniteSleepLoop(); return rc; } Result appletJumpToSubApplicationProgramForDevelopment(u64 titleID, const void* buffer, size_t size) { return _appletExecuteProgram(AppletProgramSpecifyKind_JumpToSubApplicationProgramForDevelopment, titleID, buffer, size); } Result appletRestartProgram(const void* buffer, size_t size) { return _appletExecuteProgram(AppletProgramSpecifyKind_RestartProgram, 0, buffer, size); } Result appletGetPreviousProgramIndex(s32 *programIndex) { if (!serviceIsActive(&g_appletSrv) || !_appletIsApplication()) return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); if (hosversionBefore(5,0,0)) return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer); return _appletCmdNoInOut32(&g_appletIFunctions, (u32*)programIndex, 123); } 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, false, 130); } Result appletCreateMovieMaker(Service* srv_out, TransferMemory *tmem) { if (!serviceIsActive(&g_appletSrv) || !_appletIsApplication()) return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); if (hosversionBefore(5,0,0)) return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer); return _appletCmdInTmemOutSession(&g_appletIFunctions, srv_out, tmem, 1000); } Result appletPrepareForJit(void) { if (!serviceIsActive(&g_appletSrv) || !_appletIsApplication()) return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); if (hosversionBefore(5,0,0)) return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer); return _appletCmdNoIO(&g_appletIFunctions, 1001); } // IHomeMenuFunctions Result appletRequestToGetForeground(void) { if (__nx_applet_type != AppletType_SystemApplet) return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); return _appletCmdNoIO(&g_appletIFunctions, 10); } Result appletLockForeground(void) { if (__nx_applet_type != AppletType_SystemApplet) return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); return _appletCmdNoIO(&g_appletIFunctions, 11); } Result appletUnlockForeground(void) { if (__nx_applet_type != AppletType_SystemApplet) return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); return _appletCmdNoIO(&g_appletIFunctions, 12); } Result appletPopFromGeneralChannel(AppletStorage *s) { if (__nx_applet_type != AppletType_SystemApplet) return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); return _appletCmdNoInOutStorage(&g_appletIFunctions, s, 20); } Result appletGetPopFromGeneralChannelEvent(Event *out_event) { if (__nx_applet_type != AppletType_SystemApplet) return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); return _appletGetEvent(&g_appletIFunctions, out_event, false, 21); } Result appletGetHomeButtonWriterLockAccessor(AppletLockAccessor *a) { if (__nx_applet_type != AppletType_SystemApplet) return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); return _appletGetHomeButtonRwLockAccessor(&g_appletIFunctions, a, 30); } Result appletPopRequestLaunchApplicationForDebug(u128 *userIDs, s32 count, u64 *titleID, s32 *total_out) { if (__nx_applet_type != AppletType_SystemApplet) return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); if (hosversionBefore(6,0,0)) return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer); struct { 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; } Result appletLaunchDevMenu(void) { Result rc=0; if (__nx_applet_type != AppletType_SystemApplet) return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); if (hosversionBefore(8,0,0)) return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer); rc = _appletCmdNoIO(&g_appletIFunctions, 200); if (R_SUCCEEDED(rc)) _appletInfiniteSleepLoop(); return rc; } // IGlobalStateController Result appletStartSleepSequence(bool flag) { if (__nx_applet_type != AppletType_SystemApplet) return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); return _appletCmdInBool(&g_appletIGlobalStateController, flag, 2); } Result appletStartShutdownSequence(void) { if (__nx_applet_type != AppletType_SystemApplet) return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); return _appletCmdNoIO(&g_appletIGlobalStateController, 3); } Result appletStartRebootSequence(void) { if (__nx_applet_type != AppletType_SystemApplet) return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); return _appletCmdNoIO(&g_appletIGlobalStateController, 4); } Result appletIsAutoPowerDownRequested(bool *out) { if (__nx_applet_type != AppletType_SystemApplet) return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); if (hosversionBefore(7,0,0)) return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer); return _appletCmdNoInOutBool(&g_appletIGlobalStateController, out, 9); } Result appletLoadAndApplyIdlePolicySettings(void) { if (__nx_applet_type != AppletType_SystemApplet) return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); return _appletCmdNoIO(&g_appletIGlobalStateController, 10); } Result appletNotifyCecSettingsChanged(void) { if (__nx_applet_type != AppletType_SystemApplet) return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); if (hosversionBefore(2,0,0)) return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer); return _appletCmdNoIO(&g_appletIGlobalStateController, 11); } Result appletSetDefaultHomeButtonLongPressTime(s64 val) { if (hosversionBefore(3,0,0)) return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer); return _appletCmdInU64(&g_appletIGlobalStateController, val, 12); } Result appletUpdateDefaultDisplayResolution(void) { if (__nx_applet_type != AppletType_SystemApplet) return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); if (hosversionBefore(3,0,0)) return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer); return _appletCmdNoIO(&g_appletIGlobalStateController, 13); } Result appletShouldSleepOnBoot(bool *out) { if (__nx_applet_type != AppletType_SystemApplet) return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); if (hosversionBefore(3,0,0)) return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer); return _appletCmdNoInOutBool(&g_appletIGlobalStateController, out, 14); } Result appletGetHdcpAuthenticationFailedEvent(Event *out_event) { if (__nx_applet_type != AppletType_SystemApplet) return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); if (hosversionBefore(4,0,0)) return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer); return _appletGetEvent(&g_appletIGlobalStateController, out_event, false, 15); } // IApplicationCreator static Result _appletApplicationCreateState(AppletApplication *a) { return _appletGetEvent(&a->s, &a->StateChangedEvent, false, 0);//GetAppletStateChangedEvent } static Result _appletApplicationCreate(AppletApplication *a, Service* srv, u32 cmd_id) { Result rc=0; memset(a, 0, sizeof(AppletApplication)); rc = _appletGetSession(srv, &a->s, cmd_id); if (R_SUCCEEDED(rc)) rc = _appletApplicationCreateState(a); return rc; } 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, val, cmd_id); if (R_SUCCEEDED(rc)) rc = _appletApplicationCreateState(a); return rc; } Result appletCreateApplication(AppletApplication *a, u64 titleID) { if (__nx_applet_type != AppletType_SystemApplet) return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); return _appletApplicationCreateIn64(a, &g_appletIApplicationCreator, titleID, 0); } Result appletPopLaunchRequestedApplication(AppletApplication *a) { if (__nx_applet_type != AppletType_SystemApplet) return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); return _appletApplicationCreate(a, &g_appletIApplicationCreator, 1); } Result appletCreateSystemApplication(AppletApplication *a, u64 titleID) { if (__nx_applet_type != AppletType_SystemApplet) return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); return _appletApplicationCreateIn64(a, &g_appletIApplicationCreator, titleID, 10); } Result appletPopFloatingApplicationForDevelopment(AppletApplication *a) { if (__nx_applet_type != AppletType_SystemApplet) return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); return _appletApplicationCreate(a, &g_appletIApplicationCreator, 100); } // IApplicationAccessor void appletApplicationClose(AppletApplication *a) { eventClose(&a->StateChangedEvent); serviceClose(&a->s); memset(a, 0, sizeof(AppletApplication)); } bool appletApplicationActive(AppletApplication *a) { return serviceIsActive(&a->s); } Result appletApplicationStart(AppletApplication *a) { if (!serviceIsActive(&a->s)) return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); return _appletCmdNoIO(&a->s, 10);//Start } Result appletApplicationRequestExit(AppletApplication *a) { if (!serviceIsActive(&a->s)) return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); return _appletCmdNoIO(&a->s, 20);//RequestExit } Result appletApplicationTerminate(AppletApplication *a) { if (!serviceIsActive(&a->s)) return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); return _appletCmdNoIO(&a->s, 25);//Terminate } void appletApplicationJoin(AppletApplication *a) { Result rc=0; AppletApplicationExitReason res = AppletApplicationExitReason_Normal; u32 desc=0; eventWait(&a->StateChangedEvent, U64_MAX); rc = _appletCmdNoIO(&a->s, 30);//GetResult if (R_FAILED(rc)) { res = AppletApplicationExitReason_Unexpected; if (R_MODULE(rc) == 128) { desc = R_DESCRIPTION(rc); if (desc >= 35 && desc < 40) res = AppletApplicationExitReason_Unknown5; else if (desc >= 31 && desc < 40) res = AppletApplicationExitReason_Unknown1; else if (desc == 23) res = AppletApplicationExitReason_Unknown2; else if (desc >= 40 && desc < 45) res = AppletApplicationExitReason_Unknown3; else if (desc == 51) res = AppletApplicationExitReason_Unknown4; } } a->exitreason = res; } bool appletApplicationCheckFinished(AppletApplication *a) { return R_SUCCEEDED(eventWait(&a->StateChangedEvent, 0)); } AppletApplicationExitReason appletApplicationGetExitReason(AppletApplication *a) { return a->exitreason; } Result appletApplicationRequestForApplicationToGetForeground(AppletApplication *a) { if (!serviceIsActive(&a->s)) return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); return _appletCmdNoIO(&a->s, 101);//RequestForApplicationToGetForeground } Result appletApplicationTerminateAllLibraryApplets(AppletApplication *a) { if (!serviceIsActive(&a->s)) return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); return _appletCmdNoIO(&a->s, 110); } Result appletApplicationAreAnyLibraryAppletsLeft(AppletApplication *a, bool *out) { if (!serviceIsActive(&a->s)) return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); return _appletCmdNoInOutBool(&a->s, out, 111); } Result appletApplicationRequestExitLibraryAppletOrTerminate(AppletApplication *a, u64 timeout) { Result rc=0; Service srv={0}; if (!serviceIsActive(&a->s)) return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); rc = _appletGetSession(&a->s, &srv, 112);//GetCurrentLibraryApplet if (R_SUCCEEDED(rc)) rc = _appletAccessorRequestExitOrTerminate(&srv, timeout); serviceClose(&srv); return rc; } Result appletApplicationGetApplicationId(AppletApplication *a, u64 *titleID) { if (!serviceIsActive(&a->s)) return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); if (hosversionBefore(6,0,0)) return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer); return _appletCmdNoInOut64(&a->s, titleID, 120);//GetApplicationId } Result appletApplicationPushLaunchParameter(AppletApplication *a, AppletLaunchParameterKind kind, AppletStorage* s) { if (!serviceIsActive(&a->s)) return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); if (!serviceIsActive(&s->s)) return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); 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; } Result appletApplicationGetApplicationControlProperty(AppletApplication *a, NacpStruct *nacp) { if (!serviceIsActive(&a->s)) return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); return _appletCmdNoInRecvBuf(&a->s, nacp, sizeof(*nacp), 122);//GetApplicationControlProperty } Result appletApplicationGetApplicationLaunchProperty(AppletApplication *a, AppletApplicationLaunchProperty *out) { if (!serviceIsActive(&a->s)) return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); if (hosversionBefore(2,0,0)) return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer); return _appletCmdNoInRecvBuf(&a->s, out, sizeof(*out), 123);//GetApplicationLaunchProperty } Result appletApplicationGetApplicationLaunchRequestInfo(AppletApplication *a, AppletApplicationLaunchRequestInfo *out) { if (!serviceIsActive(&a->s)) return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); if (hosversionBefore(6,0,0)) return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer); serviceAssumeDomain(&a->s); return serviceDispatchOut(&a->s, 124, *out); } Result appletApplicationSetUsers(AppletApplication *a, const u128 *userIDs, s32 count, bool flag) { if (!serviceIsActive(&a->s)) return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); if (hosversionBefore(6,0,0)) return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer); 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) { if (!serviceIsActive(&a->s)) return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); if (hosversionBefore(6,0,0)) return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer); return _appletCmdNoInOutBool(&a->s, out, 131);//CheckRightsEnvironmentAvailable } Result appletApplicationGetNsRightsEnvironmentHandle(AppletApplication *a, u64 *handle) { if (!serviceIsActive(&a->s)) return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); if (hosversionBefore(6,0,0)) return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer); return _appletCmdNoInOut64(&a->s, handle, 132);//GetNsRightsEnvironmentHandle } Result appletApplicationGetDesirableUids(AppletApplication *a, u128 *userIDs, s32 count, s32 *total_out) { if (!serviceIsActive(&a->s)) return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); if (hosversionBefore(6,0,0)) return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer); 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) { if (!serviceIsActive(&a->s)) return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); if (hosversionBefore(6,0,0)) return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer); return _appletCmdNoIO(&a->s, 150);//ReportApplicationExitTimeout } Result appletApplicationSetApplicationAttribute(AppletApplication *a, const AppletApplicationAttribute *attr) { if (!serviceIsActive(&a->s)) return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); if (hosversionBefore(8,0,0)) return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer); return _appletCmdSendBufNoOut(&a->s, attr, sizeof(*attr), 160);//SetApplicationAttribute } Result appletApplicationHasSaveDataAccessPermission(AppletApplication *a, u64 titleID, bool *out) { if (!serviceIsActive(&a->s)) return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); if (hosversionBefore(8,0,0)) return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer); u8 tmpout=0; serviceAssumeDomain(&a->s); Result rc = serviceDispatchInOut(&a->s, 170, titleID, tmpout); if (R_SUCCEEDED(rc) && out) *out = tmpout!=0; return rc; } // ILibraryAppletSelfAccessor Result appletPopInData(AppletStorage *s) { if (__nx_applet_type != AppletType_LibraryApplet) return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); return _appletCmdNoInOutStorage(&g_appletILibraryAppletSelfAccessor, s, 0); } Result appletPushOutData(AppletStorage *s) { if (__nx_applet_type != AppletType_LibraryApplet) return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); return _appletCmdInStorage(&g_appletILibraryAppletSelfAccessor, s, 1); } Result appletPopInteractiveInData(AppletStorage *s) { if (__nx_applet_type != AppletType_LibraryApplet) return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); return _appletCmdNoInOutStorage(&g_appletILibraryAppletSelfAccessor, s, 2); } Result appletPushInteractiveOutData(AppletStorage *s) { if (__nx_applet_type != AppletType_LibraryApplet) return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); return _appletCmdInStorage(&g_appletILibraryAppletSelfAccessor, s, 3); } Result appletGetPopInDataEvent(Event *out_event) { if (__nx_applet_type != AppletType_LibraryApplet) return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); 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, false, 6); } static Result _appletExitProcessAndReturn(void) { if (__nx_applet_type != AppletType_LibraryApplet) return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); return _appletCmdNoIO(&g_appletILibraryAppletSelfAccessor, 10); } Result appletGetLibraryAppletInfo(LibAppletInfo *info) { if (__nx_applet_type != AppletType_LibraryApplet) return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); return _appletGetLibraryAppletInfo(&g_appletILibraryAppletSelfAccessor, info, 11); } Result appletGetMainAppletIdentityInfo(AppletIdentityInfo *info) { if (__nx_applet_type != AppletType_LibraryApplet) return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); return _appletGetIdentityInfo(&g_appletILibraryAppletSelfAccessor, info, 12); } Result appletCanUseApplicationCore(bool *out) { if (__nx_applet_type != AppletType_LibraryApplet) return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); return _appletCmdNoInOutBool(&g_appletILibraryAppletSelfAccessor, out, 13); } Result appletGetCallerAppletIdentityInfo(AppletIdentityInfo *info) { if (__nx_applet_type != AppletType_LibraryApplet) return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); return _appletGetIdentityInfo(&g_appletILibraryAppletSelfAccessor, info, 14); } Result appletGetMainAppletApplicationControlProperty(NacpStruct *nacp) { if (__nx_applet_type != AppletType_LibraryApplet) return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); if (hosversionBefore(2,0,0)) return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer); return _appletCmdNoInRecvBuf(&g_appletILibraryAppletSelfAccessor, nacp, sizeof(*nacp), 15); } Result appletGetMainAppletStorageId(FsStorageId *storageId) { u8 tmp=0; Result rc=0; if (__nx_applet_type != AppletType_LibraryApplet) return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); if (hosversionBefore(2,0,0)) return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer); rc = _appletCmdNoInOutU8(&g_appletILibraryAppletSelfAccessor, &tmp, 16); if (R_SUCCEEDED(rc) && storageId) *storageId = tmp; return rc; } 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); 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) { if (__nx_applet_type != AppletType_LibraryApplet) return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); if (hosversionBefore(4,0,0)) return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer); return _appletGetIdentityInfo(&g_appletILibraryAppletSelfAccessor, info, 18); } Result appletGetDesirableKeyboardLayout(u32 *layout) { if (__nx_applet_type != AppletType_LibraryApplet) return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); if (hosversionBefore(4,0,0)) return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer); return _appletCmdNoInOut32(&g_appletILibraryAppletSelfAccessor, layout, 19); } Result appletPopExtraStorage(AppletStorage *s) { if (__nx_applet_type != AppletType_LibraryApplet) return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); return _appletCmdNoInOutStorage(&g_appletILibraryAppletSelfAccessor, s, 20); } Result appletGetPopExtraStorageEvent(Event *out_event) { if (__nx_applet_type != AppletType_LibraryApplet) return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); return _appletGetEvent(&g_appletILibraryAppletSelfAccessor, out_event, false, 25); } Result appletUnpopInData(AppletStorage *s) { if (__nx_applet_type != AppletType_LibraryApplet) return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); return _appletCmdInStorage(&g_appletILibraryAppletSelfAccessor, s, 30); } Result appletUnpopExtraStorage(AppletStorage *s) { if (__nx_applet_type != AppletType_LibraryApplet) return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); return _appletCmdInStorage(&g_appletILibraryAppletSelfAccessor, s, 31); } Result appletGetIndirectLayerProducerHandle(u64 *out) { if (__nx_applet_type != AppletType_LibraryApplet) return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); if (hosversionBefore(2,0,0)) return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer); return _appletCmdNoInOut64(&g_appletILibraryAppletSelfAccessor, out, 40); } Result appletGetMainAppletApplicationDesiredLanguage(u64 *LanguageCode) { if (__nx_applet_type != AppletType_LibraryApplet) return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); if (hosversionBefore(4,0,0)) return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer); return _appletCmdNoInOut64(&g_appletILibraryAppletSelfAccessor, LanguageCode, 60); } Result appletGetCurrentApplicationId(u64 *titleID) { if (__nx_applet_type != AppletType_LibraryApplet) return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); if (hosversionBefore(8,0,0)) return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer); return _appletCmdNoInOut64(&g_appletILibraryAppletSelfAccessor, titleID, 70); } Result appletRequestExitToSelf(void) { if (__nx_applet_type != AppletType_LibraryApplet) return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); if (hosversionBefore(6,0,0)) return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer); return _appletCmdNoIO(&g_appletILibraryAppletSelfAccessor, 80); } Result appletCreateGameMovieTrimmer(Service* srv_out, TransferMemory *tmem) { if (__nx_applet_type != AppletType_LibraryApplet) return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); if (hosversionBefore(4,0,0)) return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer); return _appletCmdInTmemOutSession(&g_appletILibraryAppletSelfAccessor, srv_out, tmem, 100); } Result appletReserveResourceForMovieOperation(void) { if (__nx_applet_type != AppletType_LibraryApplet) return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); if (hosversionBefore(5,0,0)) return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer); return _appletCmdNoIO(&g_appletILibraryAppletSelfAccessor, 101); } Result appletUnreserveResourceForMovieOperation(void) { if (__nx_applet_type != AppletType_LibraryApplet) return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); if (hosversionBefore(5,0,0)) return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer); return _appletCmdNoIO(&g_appletILibraryAppletSelfAccessor, 102); } Result appletGetMainAppletAvailableUsers(u128 *userIDs, s32 count, bool *flag, s32 *total_out) { if (__nx_applet_type != AppletType_LibraryApplet) return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); if (hosversionBefore(6,0,0)) return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer); struct { 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; } // IOverlayFunctions Result appletBeginToWatchShortHomeButtonMessage(void) { if (__nx_applet_type != AppletType_OverlayApplet) return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); return _appletCmdNoIO(&g_appletIFunctions, 0); } Result appletEndToWatchShortHomeButtonMessage(void) { if (__nx_applet_type != AppletType_OverlayApplet) return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); return _appletCmdNoIO(&g_appletIFunctions, 1); } Result appletGetApplicationIdForLogo(u64 *titleID) { if (__nx_applet_type != AppletType_OverlayApplet) return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); return _appletCmdNoInOut64(&g_appletIFunctions, titleID, 2); } Result appletSetGpuTimeSliceBoost(u64 val) { if (__nx_applet_type != AppletType_OverlayApplet) return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); return _appletCmdInU64(&g_appletIFunctions, val, 3); } Result appletSetAutoSleepTimeAndDimmingTimeEnabled(bool flag) { if (__nx_applet_type != AppletType_OverlayApplet) return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); if (hosversionBefore(2,0,0)) return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer); return _appletCmdInBool(&g_appletIFunctions, flag, 4); } Result appletTerminateApplicationAndSetReason(Result reason) { if (__nx_applet_type != AppletType_OverlayApplet) return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); if (hosversionBefore(2,0,0)) return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer); return _appletCmdInU32(&g_appletIFunctions, reason, 5); } Result appletSetScreenShotPermissionGlobally(bool flag) { if (__nx_applet_type != AppletType_OverlayApplet) return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); if (hosversionBefore(3,0,0)) return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer); return _appletCmdInBool(&g_appletIFunctions, flag, 6); } Result appletStartShutdownSequenceForOverlay(void) { Result rc=0; if (__nx_applet_type != AppletType_OverlayApplet) return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); if (hosversionBefore(6,0,0)) return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer); rc = _appletCmdNoIO(&g_appletIFunctions, 10); if (R_SUCCEEDED(rc)) _appletInfiniteSleepLoop(); return rc; } Result appletStartRebootSequenceForOverlay(void) { Result rc=0; if (__nx_applet_type != AppletType_OverlayApplet) return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); if (hosversionBefore(6,0,0)) return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer); rc = _appletCmdNoIO(&g_appletIFunctions, 11); if (R_SUCCEEDED(rc)) _appletInfiniteSleepLoop(); return rc; } Result appletSetHandlingHomeButtonShortPressedEnabled(bool flag) { if (__nx_applet_type != AppletType_OverlayApplet) return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); if (hosversionBefore(8,0,0)) return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer); return _appletCmdInBool(&g_appletIFunctions, flag, 20); } Result appletBeginToObserveHidInputForDevelop(void) { if (__nx_applet_type != AppletType_OverlayApplet) return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); if (hosversionBefore(5,0,0)) return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer); return _appletCmdNoIO(&g_appletIFunctions, 101); } // IAppletCommonFunctions Result appletReadThemeStorage(void* buffer, size_t size, u64 offset, u64 *transfer_size) { if (!serviceIsActive(&g_appletIAppletCommonFunctions)) return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); 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); serviceAssumeDomain(&g_appletIAppletCommonFunctions); return serviceDispatchIn(&g_appletIAppletCommonFunctions, 11, offset, .buffer_attrs = { SfBufferAttr_HipcAutoSelect | SfBufferAttr_In }, .buffers = { { buffer, size } }, ); } Result appletGetDisplayLogicalResolution(s32 *width, s32 *height) { if (!serviceIsActive(&g_appletIAppletCommonFunctions)) return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); if (hosversionBefore(8,0,0)) return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer); return _appletGetResolution(&g_appletIAppletCommonFunctions, width, height, 40); } Result appletSetDisplayMagnification(float x, float y, float width, float height) { if (!serviceIsActive(&g_appletIAppletCommonFunctions)) return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); if (hosversionBefore(8,0,0)) return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer); const struct { float x; float y; float width; float height; } in = { x, y, width, height }; serviceAssumeDomain(&g_appletIAppletCommonFunctions); return serviceDispatchIn(&g_appletIAppletCommonFunctions, 42, in); } Result appletSetHomeButtonDoubleClickEnabled(bool flag) { if (!serviceIsActive(&g_appletIAppletCommonFunctions)) return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); if (hosversionBefore(8,0,0)) return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer); return _appletCmdInBool(&g_appletIAppletCommonFunctions, flag, 50); } Result appletGetHomeButtonDoubleClickEnabled(bool *out) { if (!serviceIsActive(&g_appletIAppletCommonFunctions)) return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); if (hosversionBefore(8,0,0)) return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer); return _appletCmdNoInOutBool(&g_appletIAppletCommonFunctions, out, 51); } // IDebugFunctions Result appletOpenMainApplication(AppletApplication *a) { return _appletApplicationCreate(a, &g_appletIDebugFunctions, 1); } Result appletPerformSystemButtonPressing(AppletSystemButtonType type) { return _appletCmdInU32(&g_appletIDebugFunctions, type, 10); } Result appletInvalidateTransitionLayer(void) { return _appletCmdNoIO(&g_appletIDebugFunctions, 20); } Result appletRequestLaunchApplicationWithUserAndArgumentForDebug(u64 titleID, u128 *userIDs, size_t total_userIDs, bool flag, const void* buffer, size_t size) { if (hosversionBefore(6,0,0)) return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer); const struct { u8 flag; u64 titleID; } in = { flag!=0, titleID }; 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); serviceAssumeDomain(&g_appletIDebugFunctions); return serviceDispatchOut(&g_appletIDebugFunctions, 40, *info); } // State / other u8 appletGetOperationMode(void) { return g_appletOperationMode; } u32 appletGetPerformanceMode(void) { return g_appletPerformanceMode; } AppletFocusState appletGetFocusState(void) { return (AppletFocusState)g_appletFocusState; } AppletInfo *appletGetAppletInfo(void) { if (!g_appletInfoInitialized) return NULL; return &g_appletInfo; } Result appletGetMessage(u32 *msg) { Result rc=0; if (msg==NULL) return MAKERESULT(Module_Libnx, LibnxError_BadInput); rc = eventWait(&g_appletMessageEvent, 0); if (R_FAILED(rc)) return rc; rc = _appletReceiveMessage(msg); if (R_FAILED(rc)) { if (R_VALUE(rc) == MAKERESULT(128, 3)) return rc; fatalSimple(MAKERESULT(Module_Libnx, LibnxError_BadAppletReceiveMessage)); } return 0; } bool appletProcessMessage(u32 msg) { Result rc=0; switch(msg) { case AppletMessage_ExitRequested: appletCallHook(AppletHookType_OnExitRequest); return false; break; case AppletMessage_FocusStateChanged: rc = _appletGetCurrentFocusState(&g_appletFocusState); if (R_FAILED(rc)) fatalSimple(MAKERESULT(Module_Libnx, LibnxError_BadAppletGetCurrentFocusState)); appletCallHook(AppletHookType_OnFocusState); break; case AppletMessage_Restart: appletCallHook(AppletHookType_OnRestart); break; case AppletMessage_OperationModeChanged: rc = _appletGetOperationMode(&g_appletOperationMode); if (R_FAILED(rc)) fatalSimple(MAKERESULT(Module_Libnx, LibnxError_BadAppletGetOperationMode)); appletCallHook(AppletHookType_OnOperationMode); break; case AppletMessage_PerformanceModeChanged: rc = _appletGetPerformanceMode(&g_appletPerformanceMode); if (R_FAILED(rc)) fatalSimple(MAKERESULT(Module_Libnx, LibnxError_BadAppletGetPerformanceMode)); appletCallHook(AppletHookType_OnPerformanceMode); break; case AppletMessage_RequestToDisplay: appletCallHook(AppletHookType_RequestToDisplay); break; case AppletMessage_CaptureButtonShortPressed: appletCallHook(AppletHookType_OnCaptureButtonShortPressed); break; case AppletMessage_AlbumImageTaken: appletCallHook(AppletHookType_OnAlbumImageTaken); break; } return true; } bool appletMainLoop(void) { u32 msg = 0; if (R_FAILED(appletGetMessage(&msg))) return true; return appletProcessMessage(msg); }