diff --git a/libctru/include/3ds/services/apt.h b/libctru/include/3ds/services/apt.h index 3092f99..bec85f7 100644 --- a/libctru/include/3ds/services/apt.h +++ b/libctru/include/3ds/services/apt.h @@ -69,7 +69,7 @@ Result APT_PrepareToCloseApplication(Handle* handle, u8 a); Result APT_CloseApplication(Handle* handle, u32 a, u32 b, u32 c); Result APT_SetAppCpuTimeLimit(Handle* handle, u32 percent); Result APT_GetAppCpuTimeLimit(Handle* handle, u32 *percent); -Result APT_CheckNew3DS_Application(Handle* handle, u8 *out);//*Application and *System use APT commands 0x01010000 and 0x01020000. Using APT_CheckNew3DS() is recommended, this determines which of those two funcs to use automatically. +Result APT_CheckNew3DS_Application(Handle* handle, u8 *out);//*Application and *System use APT commands 0x01010000 and 0x01020000. Using APT_CheckNew3DS() is recommended, this determines which of those two funcs to use automatically. When this is first called(this calls aptOpenSession/aptCloseSession internally), this initializes an internal flag, which is then used for the out val for all future calls. Result APT_CheckNew3DS_System(Handle* handle, u8 *out); Result APT_CheckNew3DS(Handle* handle, u8 *out); diff --git a/libctru/source/services/apt.c b/libctru/source/services/apt.c index 91e1c17..9294f1a 100644 --- a/libctru/source/services/apt.c +++ b/libctru/source/services/apt.c @@ -17,6 +17,9 @@ NS_APPID currentAppId; static char *__apt_servicestr = NULL; static char *__apt_servicenames[3] = {"APT:U", "APT:S", "APT:A"}; +static u32 __apt_new3dsflag_initialized = 0; +static u8 __apt_new3dsflag = 0; + Handle aptLockHandle; Handle aptuHandle; Handle aptEvents[3]; @@ -838,7 +841,22 @@ Result APT_CheckNew3DS_System(Handle* handle, u8 *out) Result APT_CheckNew3DS(Handle* handle, u8 *out) { - if(currentAppId==APPID_APPLICATION)return APT_CheckNew3DS_Application(NULL, out); - return APT_CheckNew3DS_System(NULL, out); + Result ret=0; + + if(__apt_new3dsflag_initialized) + { + *out = __apt_new3dsflag; + return 0; + } + + aptOpenSession(); + if(currentAppId==APPID_APPLICATION)ret = APT_CheckNew3DS_Application(NULL, out); + ret = APT_CheckNew3DS_System(NULL, out); + aptCloseSession(); + + __apt_new3dsflag_initialized = 1; + __apt_new3dsflag = *out; + + return ret; } diff --git a/libctru/source/services/hid.c b/libctru/source/services/hid.c index cc17b68..d48f30c 100644 --- a/libctru/source/services/hid.c +++ b/libctru/source/services/hid.c @@ -21,6 +21,8 @@ static angularRate gRate; Result hidInit(u32* sharedMem) { + u8 val=0; + if(!sharedMem)sharedMem=(u32*)HID_SHAREDMEM_DEFAULT; Result ret=0; @@ -34,9 +36,16 @@ Result hidInit(u32* sharedMem) hidSharedMem=sharedMem; if((ret=svcMapMemoryBlock(hidMemHandle, (u32)hidSharedMem, MEMPERM_READ, 0x10000000)))goto cleanup2; + APT_CheckNew3DS(NULL, &val); + + if(val) + { + ret = irrstInit(NULL); + } + // Reset internal state. kOld = kHeld = kDown = kUp = 0; - return 0; + return ret; cleanup2: svcCloseHandle(hidMemHandle); @@ -48,10 +57,18 @@ cleanup1: void hidExit() { // Unmap HID sharedmem and close handles. + u8 val=0; int i; for(i=0; i<5; i++)svcCloseHandle(hidEvents[i]); svcUnmapMemoryBlock(hidMemHandle, (u32)hidSharedMem); svcCloseHandle(hidMemHandle); svcCloseHandle(hidHandle); + + APT_CheckNew3DS(NULL, &val); + + if(val) + { + irrstExit(); + } } void hidWaitForEvent(HID_Event id, bool nextEvent) diff --git a/libctru/source/services/irrst.c b/libctru/source/services/irrst.c index 87121ac..c7a54ff 100644 --- a/libctru/source/services/irrst.c +++ b/libctru/source/services/irrst.c @@ -17,6 +17,8 @@ static bool irrstUsed = false; Result irrstInit(u32* sharedMem) { + if(irrstUsed)return 0; + if(!sharedMem)sharedMem=(u32*)IRRST_SHAREDMEM_DEFAULT; Result ret=0; @@ -44,6 +46,8 @@ cleanup1: void irrstExit() { + if(!irrstUsed)return; + irrstUsed = false; svcCloseHandle(irrstEvent); // Unmap ir:rst sharedmem and close handles.