diff --git a/nx/include/switch/services/acc.h b/nx/include/switch/services/acc.h index 9d0fd556..560b5b7f 100644 --- a/nx/include/switch/services/acc.h +++ b/nx/include/switch/services/acc.h @@ -10,6 +10,7 @@ #define ACC_USER_LIST_SIZE 8 +/// Profile typedef struct { Service s; } AccountProfile; @@ -46,9 +47,8 @@ Result accountGetUserCount(s32* user_count); */ Result accountListAllUsers(u128* userIDs, size_t max_userIDs, size_t *actual_total); -/// Get the userID for the currently active user. The output userID is only valid when the output account_selected==1, otherwise no user is currently selected. -/// An user is only selected when the user-account selection applet was used to select an user at least once before. -Result accountGetActiveUser(u128 *userID, bool *account_selected); +/// Get the userID for the last opened user. The output userID is only valid when the output account_selected==1. +Result accountGetLastOpenedUser(u128 *userID, bool *account_selected); /// Get an AccountProfile for the specified userID. Result accountGetProfile(AccountProfile* out, u128 userID); @@ -64,6 +64,7 @@ Result accountProfileLoadImage(AccountProfile* profile, void* buf, size_t len, s void accountProfileClose(AccountProfile* profile); -/// Gets the userID which was selected by the profile-selector applet (if any), prior to launching the currently running Application title. This can only be used once under the current process, under an Application title. +/// Gets the userID which was selected by the profile-selector applet (if any), prior to launching the currently running Application title. +/// This gets the cached PreselectedUser loaded during accountInitialize, when PreselectedUser is available. Result accountGetPreselectedUser(u128 *userID); diff --git a/nx/source/services/acc.c b/nx/source/services/acc.c index 4e00d5c5..7c0c8d6d 100644 --- a/nx/source/services/acc.c +++ b/nx/source/services/acc.c @@ -5,16 +5,23 @@ #include "services/acc.h" #include "services/sm.h" #include "services/applet.h" +#include "runtime/env.h" #include "runtime/hosversion.h" static Service g_accSrv; static u64 g_refCnt; +static u128 g_accPreselectedUserID; +static bool g_accPreselectedUserInitialized; static Result _accountInitializeApplicationInfo(void); +static Result _accountGetPreselectedUser(u128 *userID); + Result accountInitialize(void) { Result rc=0; + Result rc2=0; + u128 *userIdEnv = envGetUserIdStorage(); atomicIncrement64(&g_refCnt); @@ -27,6 +34,18 @@ Result accountInitialize(void) if (R_SUCCEEDED(rc)) rc = _accountInitializeApplicationInfo(); } + if (R_SUCCEEDED(rc)) { + rc2 = _accountGetPreselectedUser(&g_accPreselectedUserID); + if (R_SUCCEEDED(rc2)) { + g_accPreselectedUserInitialized = true; + if (userIdEnv) *userIdEnv = g_accPreselectedUserID; + } + else if (userIdEnv) { + g_accPreselectedUserID = *userIdEnv; + if (g_accPreselectedUserID) g_accPreselectedUserInitialized = true; + } + } + if (R_FAILED(rc)) accountExit(); return rc; @@ -179,7 +198,7 @@ Result accountListAllUsers(u128* userIDs, size_t max_userIDs, size_t *actual_tot return rc; } -Result accountGetActiveUser(u128 *userID, bool *account_selected) +Result accountGetLastOpenedUser(u128 *userID, bool *account_selected) { IpcCommand c; ipcInitialize(&c); @@ -370,7 +389,7 @@ void accountProfileClose(AccountProfile* profile) { serviceClose(&profile->s); } -Result accountGetPreselectedUser(u128 *userID) { +static Result _accountGetPreselectedUser(u128 *userID) { Result rc=0; AppletStorage storage; s64 tmpsize=0; @@ -405,3 +424,11 @@ Result accountGetPreselectedUser(u128 *userID) { return rc; } +Result accountGetPreselectedUser(u128 *userID) { + if (!g_accPreselectedUserInitialized) return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); + + *userID = g_accPreselectedUserID; + + return 0; +} +