void __attribute__((weak)) __system_allocateHeaps(void) { u32 tmp=0; if(envIsHomebrew()) { // Use launcher-provided heap information. __ctru_heap_size = envGetHeapSize(); __ctru_linear_heap_size = envGetLinearHeapSize(); } else { // Distribute available memory into halves, aligning to page size. u32 size = (osGetMemRegionFree(MEMREGION_APPLICATION) / 2) & 0xFFFFF000; __ctru_heap_size = size; __ctru_linear_heap_size = size; } // Allocate the application heap __ctru_heap = 0x08000000; svcControlMemory(&tmp, __ctru_heap, 0x0, __ctru_heap_size, MEMOP_ALLOC, MEMPERM_READ | MEMPERM_WRITE); // Allocate the linear heap svcControlMemory(&__ctru_linear_heap, 0x0, 0x0, __ctru_linear_heap_size, MEMOP_ALLOC_LINEAR, MEMPERM_READ | MEMPERM_WRITE); // Set up newlib heap fake_heap_start = (char*)__ctru_heap; fake_heap_end = fake_heap_start + __ctru_heap_size; }
void __attribute__((weak)) __libctru_init(void (*retAddr)(void)) { /* Store the return address */ __system_retAddr = NULL; if (envIsHomebrew()) { __system_retAddr = retAddr; } /* Initialize the synchronization subsystem */ __sync_init(); /* Initialize newlib support system calls */ __system_initSyscalls(); /* Allocate application and linear heaps */ __system_allocateHeaps(); }
void __attribute__((weak)) __libctru_init(void (*retAddr)(void)) { // Store the return address __system_retAddr = envIsHomebrew() ? retAddr : NULL; // Initialize the synchronization subsystem __sync_init(); // Initialize newlib support system calls __system_initSyscalls(); // Allocate application and linear heaps __system_allocateHeaps(); // Build argc/argv if present __system_initArgv(); }
int main(int argc, char **argv) { (void) argc, (void) argv; Result res = MAKERESULT(RL_SUCCESS, RS_SUCCESS, 0, RD_SUCCESS); gfxInitDefault(); PrintConsole top; consoleInit(GFX_TOP, &top); util_debug_init(); consoleSelect(&top); if (R_FAILED(res = acInit())) { util_presult("acInit failed", res); goto ac_failure; } u32 wifi = 0; if (R_FAILED(res = ACU_GetWifiStatus(&wifi))) { util_presult("ACU_GetWifiStatus failed", res); fprintf(stderr, "Did you enable Wifi?\n"); goto wifi_check_failure; } if (!wifi) { fprintf(stderr, "Wifi disabled.\n"); goto wifi_check_failure; } if ((sock_ctx = memalign(SOCU_BUFSZ, SOCU_ALIGN)) == NULL) { util_perror("Allocating SOC buffer"); res = MAKERESULT( RL_PERMANENT, RS_OUTOFRESOURCE, RM_SOC, RD_OUT_OF_MEMORY); goto soc_alloc_failure; } if (R_FAILED(res = socInit(sock_ctx, SOCU_BUFSZ))) { util_presult("socInit failed", res); goto soc_failure; } if (R_FAILED(res = hidInit())) { util_presult("hidInit failed", res); goto hid_failure; } if (R_FAILED(res = HIDUSER_EnableAccelerometer())) { util_presult("Failed to enable accelerometer", res); goto accel_failure; } if (R_FAILED(res = ctrollerInit())) { fprintf(stderr, "Do you have a valid IP in\n '" CFG_FILE "'?"); goto failure; } bool isHomebrew = envIsHomebrew(); printf("Press %s to exit.\n", isHomebrew ? EXIT_DESC : "HOME"); fflush(stdout); while (aptMainLoop()) { if (isHomebrew) { if (hidKeysHeld() == EXIT_KEYS) { res = RL_SUCCESS; break; } } if (ctrollerSendHIDInfo()) { util_perror("Sending HID info"); fflush(stderr); for (int i = 3; i > 0; i--) { util_debug_printf("\rRetrying in %ds... ", i); svcSleepThread(1000000000L); } util_debug_printf("\rRetrying now.\x1b[K\n"); } gspWaitForVBlank(); gfxFlushBuffers(); gfxSwapBuffers(); } puts("Exiting..."); failure: HIDUSER_DisableAccelerometer(); accel_failure: hidExit(); hid_failure: socExit(); soc_failure: free(sock_ctx); soc_alloc_failure: wifi_check_failure: acExit(); ac_failure: if (R_FAILED(res)) { util_hang(res); } gfxExit(); return res; }