Cache the preselected user in accountInitialize. Get/set the preselected userID in env, so that accountGetPreselectedUser() is usable multiple times. Renamed accountGetActiveUser to accountGetLastOpenedUser. Improved docs.

This commit is contained in:
yellows8 2019-07-29 19:00:52 -04:00
parent 4fa878f4b1
commit 773111b3f2
No known key found for this signature in database
GPG Key ID: 0AF90DA3F1E60E43
2 changed files with 34 additions and 6 deletions

View File

@ -10,6 +10,7 @@
#define ACC_USER_LIST_SIZE 8 #define ACC_USER_LIST_SIZE 8
/// Profile
typedef struct { typedef struct {
Service s; Service s;
} AccountProfile; } AccountProfile;
@ -46,9 +47,8 @@ Result accountGetUserCount(s32* user_count);
*/ */
Result accountListAllUsers(u128* userIDs, size_t max_userIDs, size_t *actual_total); 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. /// Get the userID for the last opened user. The output userID is only valid when the output account_selected==1.
/// An user is only selected when the user-account selection applet was used to select an user at least once before. Result accountGetLastOpenedUser(u128 *userID, bool *account_selected);
Result accountGetActiveUser(u128 *userID, bool *account_selected);
/// Get an AccountProfile for the specified userID. /// Get an AccountProfile for the specified userID.
Result accountGetProfile(AccountProfile* out, u128 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); 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); Result accountGetPreselectedUser(u128 *userID);

View File

@ -5,16 +5,23 @@
#include "services/acc.h" #include "services/acc.h"
#include "services/sm.h" #include "services/sm.h"
#include "services/applet.h" #include "services/applet.h"
#include "runtime/env.h"
#include "runtime/hosversion.h" #include "runtime/hosversion.h"
static Service g_accSrv; static Service g_accSrv;
static u64 g_refCnt; static u64 g_refCnt;
static u128 g_accPreselectedUserID;
static bool g_accPreselectedUserInitialized;
static Result _accountInitializeApplicationInfo(void); static Result _accountInitializeApplicationInfo(void);
static Result _accountGetPreselectedUser(u128 *userID);
Result accountInitialize(void) Result accountInitialize(void)
{ {
Result rc=0; Result rc=0;
Result rc2=0;
u128 *userIdEnv = envGetUserIdStorage();
atomicIncrement64(&g_refCnt); atomicIncrement64(&g_refCnt);
@ -27,6 +34,18 @@ Result accountInitialize(void)
if (R_SUCCEEDED(rc)) rc = _accountInitializeApplicationInfo(); 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(); if (R_FAILED(rc)) accountExit();
return rc; return rc;
@ -179,7 +198,7 @@ Result accountListAllUsers(u128* userIDs, size_t max_userIDs, size_t *actual_tot
return rc; return rc;
} }
Result accountGetActiveUser(u128 *userID, bool *account_selected) Result accountGetLastOpenedUser(u128 *userID, bool *account_selected)
{ {
IpcCommand c; IpcCommand c;
ipcInitialize(&c); ipcInitialize(&c);
@ -370,7 +389,7 @@ void accountProfileClose(AccountProfile* profile) {
serviceClose(&profile->s); serviceClose(&profile->s);
} }
Result accountGetPreselectedUser(u128 *userID) { static Result _accountGetPreselectedUser(u128 *userID) {
Result rc=0; Result rc=0;
AppletStorage storage; AppletStorage storage;
s64 tmpsize=0; s64 tmpsize=0;
@ -405,3 +424,11 @@ Result accountGetPreselectedUser(u128 *userID) {
return rc; return rc;
} }
Result accountGetPreselectedUser(u128 *userID) {
if (!g_accPreselectedUserInitialized) return MAKERESULT(Module_Libnx, LibnxError_NotInitialized);
*userID = g_accPreselectedUserID;
return 0;
}