APP_STATUS aptGetStatus() { APP_STATUS ret; svcWaitSynchronization(aptStatusMutex, U64_MAX); ret = aptStatus; svcReleaseMutex(aptStatusMutex); return ret; }
u32 aptGetStatusPower() { u32 ret; svcWaitSynchronization(aptStatusMutex, U64_MAX); ret = aptStatusPower; svcReleaseMutex(aptStatusMutex); return ret; }
void exitPhysicsThread(physicsThread_s* p) { if(!p)return; p->exit=true; svcWaitSynchronization(p->thread, U64_MAX); svcCloseHandle(p->requestMutex); svcCloseHandle(p->thread); }
bool hidTryWaitForEvent(HID_Event id, s64 nanoseconds){ if(R_DESCRIPTION(svcWaitSynchronization(hidEvents[id], nanoseconds))==RD_TIMEOUT){ return false; } else{ svcClearEvent(hidEvents[id]); return true; } }
void ui_pop() { svcWaitSynchronization(ui_stack_mutex, U64_MAX); if(ui_stack_top >= 0) { ui_stack[ui_stack_top--] = NULL; } svcReleaseMutex(ui_stack_mutex); }
static void extsavedata_update(ui_view* view, void* data, list_item** items, u32** itemCount, list_item* selected, bool selectedTouched) { extsavedata_data* listData = (extsavedata_data*) data; if(hidKeysDown() & KEY_B) { if(listData->cancelEvent != 0) { svcSignalEvent(listData->cancelEvent); while(svcWaitSynchronization(listData->cancelEvent, 0) == 0) { svcSleepThread(1000000); } listData->cancelEvent = 0; } ui_pop(); list_destroy(view); free(listData); return; } if(!listData->populated || (hidKeysDown() & KEY_X)) { if(listData->cancelEvent != 0) { svcSignalEvent(listData->cancelEvent); while(svcWaitSynchronization(listData->cancelEvent, 0) == 0) { svcSleepThread(1000000); } listData->cancelEvent = 0; } listData->cancelEvent = task_populate_ext_save_data(listData->items, &listData->count, EXTSAVEDATA_MAX); listData->populated = true; } if(selected != NULL && selected->data != NULL && (selectedTouched || (hidKeysDown() & KEY_A))) { extsavedata_action_open((ext_save_data_info*) selected->data, &listData->populated); return; } if(*itemCount != &listData->count || *items != listData->items) { *itemCount = &listData->count; *items = listData->items; } }
void hidWaitForEvent(HID_Event id, bool nextEvent) { if(id>=HIDEVENT_MAX)return; if (nextEvent) svcClearEvent(hidEvents[id]); svcWaitSynchronization(hidEvents[id], U64_MAX); if (!nextEvent) svcClearEvent(hidEvents[id]); }
void Thread::wait() { // TODO: check that this isn't current thread asking to wait for itself if (m_thread != 0) { svcWaitSynchronization(m_thread, U64_MAX); svcCloseHandle(m_thread); m_thread = 0; } }
void LuaBox_PushEvent(int type, int value) { svcWaitSynchronization(LuaBox_EventMutex, U64_MAX); // Attempt to lock event list struct LuaBox_Event * ev = malloc(sizeof(struct LuaBox_Event)); // Allocate event ev->type = type; ev->value = value; ev->next = LuaBox_EventList; LuaBox_EventList = ev; svcReleaseMutex(LuaBox_EventMutex); // release event mutex svcSignalEvent(LuaBox_EventSignal); // Signal Lua thread if it's listening; }
void threadMain(void *arg) { while(1) { svcWaitSynchronization(threadRequest, U64_MAX); svcClearEvent(threadRequest); if(threadExit) svcExitThread(); threadcount++; } }
void Ipaddr::View_c::runover(void* rundata) { if ((ipaddr_model == nullptr) || (console == nullptr)) return; Main::Rundata* rundata_r = static_cast<Main::Rundata*>(rundata); svcWaitSynchronization(rundata_r->console_mutex, UINT64_MAX); consoleSelect(console); printf("\x1b[%u;%uH%03u.%03u.%03u.%03u", consoleY, consoleX, ipaddr_model->out_ipaddr_byte[0], ipaddr_model->out_ipaddr_byte[1], ipaddr_model->out_ipaddr_byte[2], ipaddr_model->out_ipaddr_byte[3]); printf("\x1b[%u;%uH ", consoleY + 1, consoleX); printf("\x1b[%u;%uH^", consoleY + 1, (ipaddr_model->string_location == 11) ? 14 : (consoleX + ipaddr_model->string_location + (ipaddr_model->string_location / 3))); svcReleaseMutex(rundata_r->console_mutex); }
void aptExit() { if (!aptInitialised) return; if(!aptIsCrippled())aptAppletUtility_Exit_RetToApp(0); // This is only executed when application-termination was triggered via the home-menu power-off screen. if(aptGetStatusPower() == 1) { aptOpenSession(); APT_ReplySleepQuery(NULL, currentAppId, 0x0); aptCloseSession(); } if(!aptIsCrippled()) { bool isReinit = aptIsReinit(); if (aptGetStatus() == APP_EXITING || !isReinit) { aptOpenSession(); APT_PrepareToCloseApplication(NULL, 0x1); aptCloseSession(); aptOpenSession(); APT_CloseApplication(NULL, 0x0, 0x0, 0x0); aptCloseSession(); if (isReinit) { extern void (*__system_retAddr)(void); __system_retAddr = NULL; } } else if (isReinit) { aptOpenSession(); APT_Finalize(NULL, currentAppId); aptCloseSession(); } } svcSignalEvent(aptEvents[2]); svcWaitSynchronization(aptEventHandlerThread, U64_MAX); svcCloseHandle(aptEventHandlerThread); svcCloseHandle(aptEvents[2]); svcCloseHandle(aptSleepSync); svcCloseHandle(aptStatusMutex); svcCloseHandle(aptLockHandle); svcCloseHandle(aptStatusEvent); aptInitialised = false; }
void conn_main() { APP_STATUS status; u32 it = 0; int ret = 0; int first = 1; int exiting = 0; while((status = aptGetStatus()) != APP_EXITING) { hidScanInput(); consoleClear(&bot); print(&bot, "frame: %08x\n", it); print(&bot, "ret: %08x\n", ret); print(&bot, "last_cmd: %02x\n", last_cmd & 0xFF); if(!first) { u32 bytes_read = 0; while(1) { ret = recv(sock, &cmd, sizeof(cmd), 0); if(ret < 0) { if(ret == -EWOULDBLOCK) continue; break; } bytes_read += ret; if(bytes_read == sizeof(cmd)) { svcSignalEvent(new_cmd_event); svcWaitSynchronization(cmd_done_event, U64_MAX); svcClearEvent(cmd_done_event); send(sock, &resp, sizeof(resp), 0); if(last_cmd_result == 0xDEAD) exiting = 1; break; } } } first = 0; it++; if(enable_draw) renderFrame(); u32 keys = hidKeysUp(); if(keys & KEY_A || exiting) break; } }
void SPCThread(u32 blarg) { while (!exitemu) { if (!pause) SPC_Run(); svcWaitSynchronization(SPCSync, (s64)(17*1000*1000)); } svcExitThread(); }
static Result task_populate_pending_titles_from(populate_pending_titles_data* data, FS_MediaType mediaType) { Result res = 0; u32 pendingTitleCount = 0; if(R_SUCCEEDED(res = AM_GetPendingTitleCount(&pendingTitleCount, mediaType, AM_STATUS_MASK_INSTALLING | AM_STATUS_MASK_AWAITING_FINALIZATION))) { u64* pendingTitleIds = (u64*) calloc(pendingTitleCount, sizeof(u64)); if(pendingTitleIds != NULL) { if(R_SUCCEEDED(res = AM_GetPendingTitleList(&pendingTitleCount, pendingTitleCount, mediaType, AM_STATUS_MASK_INSTALLING | AM_STATUS_MASK_AWAITING_FINALIZATION, pendingTitleIds))) { qsort(pendingTitleIds, pendingTitleCount, sizeof(u64), util_compare_u64); AM_PendingTitleEntry* pendingTitleInfos = (AM_PendingTitleEntry*) calloc(pendingTitleCount, sizeof(AM_PendingTitleEntry)); if(pendingTitleInfos != NULL) { if(R_SUCCEEDED(res = AM_GetPendingTitleInfo(pendingTitleCount, mediaType, pendingTitleIds, pendingTitleInfos))) { for(u32 i = 0; i < pendingTitleCount && i < data->max; i++) { if(task_is_quit_all() || svcWaitSynchronization(data->cancelEvent, 0) == 0) { break; } pending_title_info* pendingTitleInfo = (pending_title_info*) calloc(1, sizeof(pending_title_info)); if(pendingTitleInfo != NULL) { pendingTitleInfo->mediaType = mediaType; pendingTitleInfo->titleId = pendingTitleIds[i]; pendingTitleInfo->version = pendingTitleInfos[i].version; list_item* item = &data->items[*data->count]; snprintf(item->name, NAME_MAX, "%016llX", pendingTitleIds[i]); if(mediaType == MEDIATYPE_NAND) { item->rgba = COLOR_NAND; } else if(mediaType == MEDIATYPE_SD) { item->rgba = COLOR_SD; } item->data = pendingTitleInfo; (*data->count)++; } } } free(pendingTitleInfos); } else { res = MAKERESULT(RL_PERMANENT, RS_INVALIDSTATE, 254, RD_OUT_OF_MEMORY); } } free(pendingTitleIds); } else { res = MAKERESULT(RL_PERMANENT, RS_INVALIDSTATE, 254, RD_OUT_OF_MEMORY); } } return res; }
ui_view* ui_top() { svcWaitSynchronization(ui_stack_mutex, U64_MAX); ui_view* ui = NULL; if(ui_stack_top >= 0) { ui = ui_stack[ui_stack_top]; } svcReleaseMutex(ui_stack_mutex); return ui; }
void cmd_thread_func() { while(1) { svcWaitSynchronization(new_cmd_event, U64_MAX); svcClearEvent(new_cmd_event); if(thread_exit) svcExitThread(); last_cmd = cmd.type; last_cmd_result = execute_cmd(sock, &cmd); svcSignalEvent(cmd_done_event); } }
void threadMain(void *arg) { ThreadArgs *const args = arg; while (1) { svcWaitSynchronization(*args->threadRequest, U64_MAX); svcClearEvent(*args->threadRequest); if (*args->shouldExit) svcExitThread(); (*args->count)++; } }
void Handler::run() { lock(); running = true; while (running) { // TODO: volatile bool for running state if (!handleMessageUnsafe()) { // If there is a message on the queue, find the reason why it didn't execute auto message = peek(); u64 time = osGetTime(); if (message && message->when > time) { u64 when = message->when; unlock(); svcWaitSynchronization(eventHandle, (when-time)*1000000LL); } else { unlock(); svcWaitSynchronization(eventHandle, UINT64_MAX); } lock(); } svcClearEvent(eventHandle); } unlock(); }
void aptSetStatus(APP_STATUS status) { svcWaitSynchronization(aptStatusMutex, U64_MAX); aptStatus = status; //if(prevstatus != APP_NOTINITIALIZED) //{ if(status == APP_RUNNING || status == APP_EXITING || status == APP_APPLETSTARTED || status == APP_APPLETCLOSED) svcSignalEvent(aptStatusEvent); //} svcReleaseMutex(aptStatusMutex); }
static bool action_paste_files_error(void* data, u32 index, Result res) { paste_files_data* pasteData = (paste_files_data*) data; if(res == R_FBI_CANCELLED) { prompt_display("Failure", "Paste cancelled.", COLOR_TEXT, false, NULL, NULL, NULL); return false; } else { ui_view* view = error_display_res(data, action_paste_files_draw_top, res, "Failed to paste content."); if(view != NULL) { svcWaitSynchronization(view->active, U64_MAX); } } return index < pasteData->pasteInfo.total - 1; }
bool udsWaitDataAvailable(const udsBindContext *bindcontext, bool nextEvent, bool wait) { bool ret = true; u64 delayvalue = U64_MAX; if(!wait)delayvalue = 0; if(nextEvent)svcClearEvent(bindcontext->event); if(svcWaitSynchronization(bindcontext->event, delayvalue)!=0 && !wait)ret = false; if(!nextEvent)svcClearEvent(bindcontext->event); return ret; }
bool udsWaitConnectionStatusEvent(bool nextEvent, bool wait) { bool ret = true; u64 delayvalue = U64_MAX; if(!wait)delayvalue = 0; if(nextEvent)svcClearEvent(__uds_connectionstatus_event); if(svcWaitSynchronization(__uds_connectionstatus_event, delayvalue)!=0 && !wait)ret = false; if(!nextEvent)svcClearEvent(__uds_connectionstatus_event); return ret; }
static void svchax_gspwn(u32 dst, u32 src, u32 size, u8* flush_buffer) { extern Handle gspEvents[GSPEVENT_MAX]; memcpy(flush_buffer, flush_buffer + 0x4000, 0x4000); GSPGPU_InvalidateDataCache(dst, size); GSPGPU_FlushDataCache(src, size); memcpy(flush_buffer, flush_buffer + 0x4000, 0x4000); svcClearEvent(gspEvents[GSPEVENT_PPF]); GX_TextureCopy(src, 0, dst, 0, size, 8); svcWaitSynchronization(gspEvents[GSPEVENT_PPF], U64_MAX); memcpy(flush_buffer, flush_buffer + 0x4000, 0x4000); }
bool ui_push(ui_view* view) { if(view == NULL) { return false; } svcWaitSynchronization(ui_stack_mutex, U64_MAX); bool space = ui_stack_top < MAX_UI_VIEWS - 1; if(space) { ui_stack[++ui_stack_top] = view; } svcReleaseMutex(ui_stack_mutex); return space; }
// Mic thread void threadMic(){ while(1) { svcWaitSynchronization(threadRequest, U64_MAX); svcClearEvent(threadRequest); if(threadExit) svcExitThread(); touchPosition touch; hidTouchRead(&touch); u32 kDown = hidKeysDown(); if((touchInCircle(touch, 85, 120, 35) || kDown & KEY_A) && recording == 0) { audiobuf_pos = 0; MIC_SetRecording(1); recording = 1; } if((recording == 1) && (audiobuf_pos < audiobuf_size)) { audiobuf_pos+= MIC_ReadAudioData(&audiobuf[audiobuf_pos], audiobuf_size-audiobuf_pos, 0); if(audiobuf_pos > audiobuf_size)audiobuf_pos = audiobuf_size; if(audiobuf_pos >= 32704 && print == 0){ print = 1; } } if((touchInCircle(touch, 165, 120, 35) || kDown & KEY_B) && recording == 1) { print = 0; MIC_SetRecording(0); recording = 2; //Prevent first mute second to be allocated in wav struct if(audiobuf_pos >= 32704){ nomute_audiobuf = (u8*)linearAlloc(audiobuf_pos - 32704); memcpy(nomute_audiobuf,&audiobuf[32704],audiobuf_pos - 32704); buf_size = (audiobuf_pos - 32704) / 2; write_wav("audio.wav", buf_size, (short int *)nomute_audiobuf, S_RATE); } GSPGPU_FlushDataCache(NULL, nomute_audiobuf, audiobuf_pos); recording = 0; } } }
static inline void initPXI(void) { Result res; Handle handles[2] = {0}; PXIReset(); if(PXISyncInterrupt != 0) svcBreak(USERBREAK_PANIC); //0xE0A0183B assertSuccess(svcCreateEvent(&PXISyncInterrupt, RESET_ONESHOT)); if(PXITransferMutex != 0) svcBreak(USERBREAK_PANIC); //0xE0A0183B assertSuccess(svcCreateMutex(&PXITransferMutex, false)); assertSuccess(svcCreateEvent(&handles[0], RESET_ONESHOT)); //receive FIFO not empty assertSuccess(svcCreateEvent(&handles[1], RESET_ONESHOT)); //send FIFO empty assertSuccess(bindPXIInterrupts(&PXISyncInterrupt, &handles[0], &handles[1])); s32 handleIndex; do { while(!PXIIsSendFIFOFull()) PXISendWord(0); res = assertSuccess(svcWaitSynchronization(handles[0], 0LL)); if(R_DESCRIPTION(res) == RD_TIMEOUT) assertSuccess(svcWaitSynchronizationN(&handleIndex, handles, 2, false, -1LL)); else handleIndex = 0; } while(handleIndex != 0); unbindPXIInterrupts(NULL, &handles[0], &handles[1]); PXISendByte(1); while(PXIReceiveByte() < 1); while (!PXIIsReceiveFIFOEmpty()) PXIReceiveWord(); PXISendByte(2); while(PXIReceiveByte() < 2); svcCloseHandle(handles[0]); svcCloseHandle(handles[1]); }
void OPL_WriteRegister(int reg, int value) { int i; svcWaitSynchronization(musMutex, U64_MAX); OPL_WritePort(OPL_REGISTER_PORT, reg); // For timing, read the register port six times after writing the // register number to cause the appropriate delay for (i = 0; i<6; ++i) { // An oddity of the Doom OPL code: at startup initialization, // the spacing here is performed by reading from the register // port; after initialization, the data port is read, instead. if (init_stage_reg_writes) { OPL_ReadPort(OPL_REGISTER_PORT); } else { OPL_ReadPort(OPL_DATA_PORT); } } OPL_WritePort(OPL_DATA_PORT, value); // Read the register port 24 times after writing the value to // cause the appropriate delay for (i = 0; i<24; ++i) { OPL_ReadStatus(); } svcReleaseMutex(musMutex); }
void ndspExit(void) { if (AtomicDecrement(&ndspRefCount)) return; if (!bDspReady) return; ndspThreadRun = false; if (bSleeping) svcSignalEvent(sleepEvent); svcWaitSynchronization(ndspThread, U64_MAX); svcCloseHandle(ndspThread); svcCloseHandle(sleepEvent); aptUnhook(&aptCookie); if (!bSleeping) ndspFinalize(false); bSleeping = false; bNeedsSync = false; dspExit(); if (componentFree) { free((void*)componentBin); componentBin = NULL; } }
/*** Take a picture and put it in a texture. @function takePicture @tparam number camera should be PORT_CAM1 if you have only 1 camera activated (`PORT_x`) @tparam number w width of the picture @tparam number h height of the picture @tparam[opt=PLACE_RAM] number place where to put the texture @treturn texture the texture object */ static int cam_takePicture(lua_State *L) { u8 cam = luaL_checkinteger(L, 1); s16 w = luaL_optinteger(L, 2, 640); s16 h = luaL_optinteger(L, 3, 480); u32 bufSize = 0; // Take the actual picture CAMU_GetMaxBytes(&bufSize, w, h); u8* buf = malloc(bufSize); CAMU_SetTransferBytes(cam, bufSize, w, h); Handle camReceiveEvent = 0; CAMU_ClearBuffer(cam); CAMU_StartCapture(cam); CAMU_SetReceiving(&camReceiveEvent, buf, cam, bufSize, (s16)bufSize); svcWaitSynchronization(camReceiveEvent, 300000000ULL); CAMU_StopCapture(cam); // Load it in a texture u8 place = luaL_optinteger(L, 4, SF2D_PLACE_RAM); texture_userdata *texture; texture = (texture_userdata *)lua_newuserdata(L, sizeof(*texture)); luaL_getmetatable(L, "LTexture"); lua_setmetatable(L, -2); texture->texture = sf2d_create_texture_mem_RGBA8(buf, w, h, TEXFMT_RGB565, place); sf2d_texture_tile32(texture->texture); texture->scaleX = 1.0f; texture->scaleY = 1.0f; texture->blendColor = 0xffffffff; return 1; }