From 4b359be16b60ee3d6a58f87e55559d7ea68d3565 Mon Sep 17 00:00:00 2001 From: plutoo Date: Sat, 10 Mar 2018 15:13:10 +0100 Subject: [PATCH] Use reference counting across services --- nx/include/switch/result.h | 1 + nx/source/services/applet.c | 10 +++++----- nx/source/services/hid.c | 5 +++++ nx/source/services/irs.c | 4 ++++ nx/source/services/vi.c | 7 +++++-- 5 files changed, 20 insertions(+), 7 deletions(-) diff --git a/nx/include/switch/result.h b/nx/include/switch/result.h index 6ad0bced..d579b887 100644 --- a/nx/include/switch/result.h +++ b/nx/include/switch/result.h @@ -73,6 +73,7 @@ enum { LibnxError_InitFail_Time, LibnxError_TooManyDevOpTabs, LibnxError_AppletFailedToInitialize, + LibnxError_ApmFailedToInitialize, }; /// libnx nvidia error codes diff --git a/nx/source/services/applet.c b/nx/source/services/applet.c index ee69d0c2..3d3fc841 100644 --- a/nx/source/services/applet.c +++ b/nx/source/services/applet.c @@ -79,6 +79,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; @@ -213,9 +216,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. @@ -240,8 +240,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();*/ @@ -270,6 +268,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 24d5e77c..293b2562 100644 --- a/nx/source/services/hid.c +++ b/nx/source/services/hid.c @@ -43,6 +43,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; @@ -86,6 +89,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) {