From 12e1d924127a82e3aa718483e0c2ac3831170b2c Mon Sep 17 00:00:00 2001 From: plutoo Date: Sat, 10 Mar 2018 15:01:29 +0100 Subject: [PATCH] Simplify nv --- nx/include/switch/services/applet.h | 1 + nx/include/switch/services/nv.h | 10 +----- nx/source/display/gfx.c | 25 +++----------- nx/source/services/applet.c | 6 +++- nx/source/services/nv.c | 52 ++++++++++++----------------- 5 files changed, 33 insertions(+), 61 deletions(-) diff --git a/nx/include/switch/services/applet.h b/nx/include/switch/services/applet.h index 74d32f85..13d38bd6 100644 --- a/nx/include/switch/services/applet.h +++ b/nx/include/switch/services/applet.h @@ -47,6 +47,7 @@ struct AppletHookCookie Result appletInitialize(void); void appletExit(void); Result appletGetAppletResourceUserId(u64 *out); +AppletType appletGetAppletType(void); void appletNotifyRunning(u8 *out); Result appletCreateManagedDisplayLayer(u64 *out); diff --git a/nx/include/switch/services/nv.h b/nx/include/switch/services/nv.h index ea3769a2..32dcb2bb 100644 --- a/nx/include/switch/services/nv.h +++ b/nx/include/switch/services/nv.h @@ -7,15 +7,7 @@ #pragma once #include "../types.h" -typedef enum { - NVSERVTYPE_Default = -1, - NVSERVTYPE_Application = 0, - NVSERVTYPE_Applet = 1, - NVSERVTYPE_Sysmodule = 2, - NVSERVTYPE_T = 3, -} nvServiceType; - -Result nvInitialize(nvServiceType servicetype, size_t sharedmem_size); +Result nvInitialize(size_t sharedmem_size); void nvExit(void); Result nvOpen(u32 *fd, const char *devicepath); diff --git a/nx/source/display/gfx.c b/nx/source/display/gfx.c index 4c0e96bc..3570f3b3 100644 --- a/nx/source/display/gfx.c +++ b/nx/source/display/gfx.c @@ -166,7 +166,7 @@ static Result _gfxQueueBuffer(s32 buf) { return rc; } -static Result _gfxInit(ViServiceType servicetype, const char *DisplayName, u32 LayerFlags, u64 LayerId, nvServiceType nv_servicetype, size_t nv_transfermem_size) { +static Result _gfxInit(ViServiceType servicetype, const char *DisplayName, u32 LayerFlags, u64 LayerId, size_t nv_transfermem_size) { Result rc=0; u32 i=0; @@ -241,7 +241,7 @@ static Result _gfxInit(ViServiceType servicetype, const char *DisplayName, u32 L rc = binderInitSession(&g_gfxBinderSession, 0x0f); } - if (R_SUCCEEDED(rc)) rc = nvInitialize(nv_servicetype, nv_transfermem_size); + if (R_SUCCEEDED(rc)) rc = nvInitialize(nv_transfermem_size); if (R_SUCCEEDED(rc)) rc = bqInitialize(&g_gfxBinderSession); @@ -326,25 +326,8 @@ static Result _gfxInit(ViServiceType servicetype, const char *DisplayName, u32 L return rc; } -Result gfxInitDefault(void) -{ - nvServiceType nv_servicetype; - - switch(__nx_applet_type) { - case AppletType_Application: - case AppletType_SystemApplication: - nv_servicetype = NVSERVTYPE_Application; - break; - - case AppletType_SystemApplet: - case AppletType_LibraryApplet: - case AppletType_OverlayApplet: - default: - nv_servicetype = NVSERVTYPE_Applet; - break; - } - - return _gfxInit(ViServiceType_Default, "Default", ViLayerFlags_Default, 0, nv_servicetype, 0x300000); +Result gfxInitDefault(void) { + return _gfxInit(ViServiceType_Default, "Default", ViLayerFlags_Default, 0, 0x300000); } void gfxExit(void) diff --git a/nx/source/services/applet.c b/nx/source/services/applet.c index 94ec34a7..ee69d0c2 100644 --- a/nx/source/services/applet.c +++ b/nx/source/services/applet.c @@ -273,6 +273,10 @@ void appletExit(void) } } +AppletType appletGetAppletType(void) { + return __nx_applet_type; +} + static void appletCallHook(AppletHookType hookType) { AppletHookCookie* c; @@ -514,7 +518,7 @@ Result appletGetDesiredLanguage(u64 *LanguageCode) { IpcCommand c; ipcInitialize(&c); - if (!serviceIsActive(&g_appletSrv) || __nx_applet_type!=AppletType_Application) + if (!serviceIsActive(&g_appletSrv) || __nx_applet_type != AppletType_Application) return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); struct { diff --git a/nx/source/services/nv.c b/nx/source/services/nv.c index a056ecea..0cde75a0 100644 --- a/nx/source/services/nv.c +++ b/nx/source/services/nv.c @@ -10,55 +10,52 @@ static Service g_nvSrv; static size_t g_nvIpcBufferSize = 0; -static u32 g_nvServiceType = -1; static TransferMemory g_nvTransfermem; static Result _nvInitialize(Handle proc, Handle sharedmem, u32 transfermem_size); static Result _nvSetClientPID(u64 AppletResourceUserId); -Result nvInitialize(nvServiceType servicetype, size_t transfermem_size) +Result nvInitialize(size_t transfermem_size) { - if (g_nvServiceType != -1) + if (serviceIsActive(&g_nvSrv)) return MAKERESULT(Module_Libnx, LibnxError_AlreadyInitialized); Result rc = 0; u64 AppletResourceUserId = 0; - if (servicetype==NVSERVTYPE_Default || servicetype==NVSERVTYPE_Application) { + switch (appletGetAppletType()) { + case AppletType_Default: + case AppletType_Application: + case AppletType_SystemApplication: + default: rc = smGetService(&g_nvSrv, "nvdrv"); - g_nvServiceType = 0; - } + break; - if ((servicetype==NVSERVTYPE_Default && R_FAILED(rc)) || servicetype==NVSERVTYPE_Applet) { + case AppletType_SystemApplet: + case AppletType_LibraryApplet: + case AppletType_OverlayApplet: rc = smGetService(&g_nvSrv, "nvdrv:a"); - g_nvServiceType = 1; - } - - if ((servicetype==NVSERVTYPE_Default && R_FAILED(rc)) || servicetype==NVSERVTYPE_Sysmodule) - { - rc = smGetService(&g_nvSrv, "nvdrv:s"); - g_nvServiceType = 2; - } - - if ((servicetype==NVSERVTYPE_Default && R_FAILED(rc)) || servicetype==NVSERVTYPE_T) - { - rc = smGetService(&g_nvSrv, "nvdrv:t"); - g_nvServiceType = 3; + break; } if (R_SUCCEEDED(rc)) { g_nvIpcBufferSize = 0; rc = ipcQueryPointerBufferSize(g_nvSrv.handle, &g_nvIpcBufferSize); - if (R_SUCCEEDED(rc)) rc = tmemCreate(&g_nvTransfermem, transfermem_size, Perm_None); + if (R_SUCCEEDED(rc)) + rc = tmemCreate(&g_nvTransfermem, transfermem_size, Perm_None); - if (R_SUCCEEDED(rc)) rc = _nvInitialize(CUR_PROCESS_HANDLE, g_nvTransfermem.handle, transfermem_size); + if (R_SUCCEEDED(rc)) + rc = _nvInitialize(CUR_PROCESS_HANDLE, g_nvTransfermem.handle, transfermem_size); - //Officially ipc control DuplicateSessionEx would be used here. + // Officially ipc control DuplicateSessionEx would be used here. - if (R_SUCCEEDED(rc)) rc = appletGetAppletResourceUserId(&AppletResourceUserId);//TODO: How do sysmodules handle this? + // TODO: How do sysmodules handle this? + if (R_SUCCEEDED(rc)) + rc = appletGetAppletResourceUserId(&AppletResourceUserId); - if (R_SUCCEEDED(rc)) rc = _nvSetClientPID(AppletResourceUserId); + if (R_SUCCEEDED(rc)) + rc = _nvSetClientPID(AppletResourceUserId); } if (R_FAILED(rc)) { @@ -70,11 +67,6 @@ Result nvInitialize(nvServiceType servicetype, size_t transfermem_size) void nvExit(void) { - if (g_nvServiceType == -1) - return; - - g_nvServiceType = -1; - serviceClose(&g_nvSrv); tmemClose(&g_nvTransfermem); }