diff --git a/nx/include/switch/result.h b/nx/include/switch/result.h index 2cf43824..f2108b16 100644 --- a/nx/include/switch/result.h +++ b/nx/include/switch/result.h @@ -75,6 +75,7 @@ enum { LibnxError_DomainMessageUnknownType, LibnxError_DomainMessageTooManyObjectIds, LibnxError_AppletFailedToInitialize, + LibnxError_ApmFailedToInitialize, }; /// libnx nvidia error codes diff --git a/nx/source/services/applet.c b/nx/source/services/applet.c index 599c29c3..a1f9eaae 100644 --- a/nx/source/services/applet.c +++ b/nx/source/services/applet.c @@ -82,6 +82,9 @@ Result appletInitialize(void) if (__nx_applet_type == AppletType_None) return 0; + if (R_FAILED(apmInitialize())) + return MAKERESULT(Module_Libnx, LibnxError_ApmFailedToInitialize); + Result rc = 0; g_appletResourceUserId = 0; @@ -224,9 +227,6 @@ Result appletInitialize(void) if (R_SUCCEEDED(rc)) rc = _appletSetPerformanceModeChangedNotification(1); - if (R_SUCCEEDED(rc)) - rc = apmInitialize(); - // Official apps aren't known to use apmSetPerformanceConfiguration with mode=1. if (R_SUCCEEDED(rc)) { // This is broken with the regular "apm" service. @@ -251,8 +251,6 @@ void appletExit(void) { if (atomicDecrement64(&g_refCnt) == 0) { - apmExit(); - //TODO: Enable this somehow later with more condition(s)? /*if (__nx_applet_type == AppletType_LibraryApplet) _appletExitProcessAndReturn();*/ @@ -286,6 +284,8 @@ void appletExit(void) serviceClose(&g_appletProxySession); serviceClose(&g_appletSrv); g_appletResourceUserId = 0; + + apmExit(); } } diff --git a/nx/source/services/hid.c b/nx/source/services/hid.c index 1ade0762..6a23df13 100644 --- a/nx/source/services/hid.c +++ b/nx/source/services/hid.c @@ -44,6 +44,9 @@ Result hidInitialize(void) if (serviceIsActive(&g_hidSrv)) return 0; + if (R_FAILED(appletInitialize())) + return MAKERESULT(Module_Libnx, LibnxError_AppletFailedToInitialize); + Result rc; Handle sharedmem_handle; u64 AppletResourceUserId; @@ -88,6 +91,8 @@ void hidExit(void) serviceClose(&g_hidIAppletResource); serviceClose(&g_hidSrv); shmemClose(&g_hidSharedmem); + + appletExit(); } } diff --git a/nx/source/services/irs.c b/nx/source/services/irs.c index 63817b35..a2101c25 100644 --- a/nx/source/services/irs.c +++ b/nx/source/services/irs.c @@ -32,6 +32,9 @@ Result irsInitialize(void) if (serviceIsActive(&g_irsSrv)) return 0; + if (R_FAILED(appletInitialize())) + return MAKERESULT(Module_Libnx, LibnxError_AppletFailedToInitialize); + Result rc; Handle sharedmem_handle; u64 AppletResourceUserId=0; @@ -80,6 +83,7 @@ void irsExit(void) serviceClose(&g_irsSrv); shmemClose(&g_irsSharedmem); + appletExit(); } } diff --git a/nx/source/services/vi.c b/nx/source/services/vi.c index 0a6a67c1..6c426b39 100644 --- a/nx/source/services/vi.c +++ b/nx/source/services/vi.c @@ -24,6 +24,9 @@ Result viInitialize(ViServiceType servicetype) if (serviceIsActive(&g_viSrv)) return MAKERESULT(Module_Libnx, LibnxError_AlreadyInitialized); + if (R_FAILED(appletInitialize())) + return MAKERESULT(Module_Libnx, LibnxError_AppletFailedToInitialize); + Result rc = 0; if (servicetype == ViServiceType_Default || servicetype == ViServiceType_Manager) { @@ -36,8 +39,7 @@ Result viInitialize(ViServiceType servicetype) g_viServiceType = 1; } - if ((servicetype == ViServiceType_Default && R_FAILED(rc)) || servicetype == ViServiceType_Application) - { + if ((servicetype == ViServiceType_Default && R_FAILED(rc)) || servicetype == ViServiceType_Application) { rc = smGetService(&g_viSrv, "vi:u"); g_viServiceType = 0; } @@ -85,6 +87,7 @@ void viExit(void) serviceClose(&g_viIHOSBinderDriverIndirect); serviceClose(&g_viSrv); + appletExit(); } Service* viGetSessionService(void) {