| 192 | } |
| 193 | |
| 194 | Result aptInit(void) |
| 195 | { |
| 196 | Result ret=0; |
| 197 | |
| 198 | if (AtomicPostIncrement(&aptRefCount)) return 0; |
| 199 | |
| 200 | // Retrieve APT lock |
| 201 | ret = APT_GetLockHandle(0x0, &aptLockHandle); |
| 202 | if (R_FAILED(ret)) goto _fail; |
| 203 | if (aptIsCrippled()) return 0; |
| 204 | |
| 205 | // Initialize APT |
| 206 | APT_AppletAttr attr = aptMakeAppletAttr(APTPOS_APP, false, false); |
| 207 | ret = APT_Initialize(envGetAptAppId(), attr, &aptEvents[0], &aptEvents[1]); |
| 208 | if (R_FAILED(ret)) goto _fail2; |
| 209 | |
| 210 | // Initialize light events |
| 211 | LightEvent_Init(&aptReceiveEvent, RESET_STICKY); |
| 212 | LightEvent_Init(&aptSleepEvent, RESET_ONESHOT); |
| 213 | |
| 214 | // Create APT event handler thread |
| 215 | aptEventHandlerThreadQuit = false; |
| 216 | aptEventHandlerThread = threadCreate(aptEventHandler, 0x0, APT_HANDLER_STACKSIZE, 0x31, -2, true); |
| 217 | if (!aptEventHandlerThread) goto _fail3; |
| 218 | |
| 219 | // By default allow sleep mode and home button presses |
| 220 | aptFlags = FLAG_ALLOWSLEEP; |
| 221 | if (osGetSystemCoreVersion() == 2) // ... except in safe mode, which doesn't have home menu running |
| 222 | aptFlags |= FLAG_ALLOWHOME; |
| 223 | |
| 224 | // Enable APT |
| 225 | ret = APT_Enable(attr); |
| 226 | if (R_FAILED(ret)) goto _fail3; |
| 227 | |
| 228 | // If the homebrew environment requires it, chainload-to-self by default |
| 229 | if (aptIsChainload()) |
| 230 | aptSetChainloaderToSelf(); |
| 231 | |
| 232 | // Wait for wakeup |
| 233 | aptWaitForWakeUp(TR_ENABLE); |
| 234 | |
| 235 | // Special handling for aptReinit (aka hax 2.x): |
| 236 | // In certain cases when running under hax 2.x, we may receive a spurious |
| 237 | // second wakeup command. Therefore we must silently drop it in order to |
| 238 | // avoid keeping stale commands in APT's internal buffer. |
| 239 | if (aptIsReinit()) |
| 240 | aptFlags |= FLAG_SPURIOUS; |
| 241 | return 0; |
| 242 | |
| 243 | _fail3: |
| 244 | svcCloseHandle(aptEvents[0]); |
| 245 | svcCloseHandle(aptEvents[1]); |
| 246 | _fail2: |
| 247 | svcCloseHandle(aptLockHandle); |
| 248 | _fail: |
| 249 | AtomicDecrement(&aptRefCount); |
| 250 | return ret; |
| 251 | } |
no test coverage detected