From ba017944faafaf1e71845688dc57d3d8ebd85b84 Mon Sep 17 00:00:00 2001 From: dborth Date: Wed, 6 May 2009 06:36:59 +0000 Subject: [PATCH] more stable threading, file IO corrections, inline video functions --- source/ngc/fileop.cpp | 60 ++++++++++++++++++++++++++----------------- source/ngc/menu.cpp | 45 +++++++++++++++++--------------- source/ngc/video.cpp | 12 ++++----- 3 files changed, 67 insertions(+), 50 deletions(-) diff --git a/source/ngc/fileop.cpp b/source/ngc/fileop.cpp index 3e73885..5171a45 100644 --- a/source/ngc/fileop.cpp +++ b/source/ngc/fileop.cpp @@ -60,6 +60,7 @@ lwp_t devicethread = LWP_THREAD_NULL; static void * devicecallback (void *arg) { + sleep(1); while (1) { #ifdef HW_RVL @@ -74,7 +75,7 @@ devicecallback (void *arg) if(isMounted[METHOD_USB]) { - if(!usb->isInserted()) // check if the device was removed - doesn't work on USB! + if(!usb->isInserted()) // check if the device was removed { unmountRequired[METHOD_USB] = true; isMounted[METHOD_USB] = false; @@ -101,7 +102,7 @@ devicecallback (void *arg) } } #endif - usleep(500000); // suspend thread for 1/2 sec + sleep(1); // suspend thread for 1 sec } return NULL; } @@ -478,30 +479,32 @@ LoadFile (char * rbuffer, char *filepath, u32 length, int method, bool silent) else { struct stat fileinfo; - fstat(file->_file, &fileinfo); - size = fileinfo.st_size; - - memcpy (rbuffer, zipbuffer, readsize); // copy what we already read - - u32 offset = readsize; - u32 nextread = 0; - while(offset < size) + if(fstat(file->_file, &fileinfo) == 0) { - if(size - offset > 1024*512) nextread = 1024*512; - else nextread = size-offset; - ShowProgress ("Loading...", offset, size); - readsize = fread (rbuffer + offset, 1, nextread, file); // read in next chunk + size = fileinfo.st_size; - if(readsize <= 0 || readsize > nextread) - break; // read failure + memcpy (rbuffer, zipbuffer, readsize); // copy what we already read - if(readsize > 0) - offset += readsize; + u32 offset = readsize; + u32 nextread = 0; + while(offset < size) + { + if(size - offset > 1024*512) nextread = 1024*512; + else nextread = size-offset; + ShowProgress ("Loading...", offset, size); + readsize = fread (rbuffer + offset, 1, nextread, file); // read in next chunk + + if(readsize <= 0 || readsize > nextread) + break; // read failure + + if(readsize > 0) + offset += readsize; + } + CancelAction(); + + if(offset != size) // # bytes read doesn't match # expected + size = 0; } - CancelAction(); - - if(offset != size) // # bytes read doesn't match # expected - size = 0; } } } @@ -570,8 +573,17 @@ SaveFile (char * buffer, char *filepath, u32 datasize, int method, bool silent) if (file > 0) { - written = fwrite (savebuffer, 1, datasize, file); - if(written < datasize) written = 0; + u32 writesize, nextwrite; + while(written < datasize) + { + if(datasize - written > 16*1024) nextwrite=16*1024; + else nextwrite = datasize-written; + writesize = fwrite (buffer+written, 1, nextwrite, file); + if(writesize != nextwrite) break; // write failure + written += writesize; + } + + if(written != datasize) written = 0; fclose (file); } } diff --git a/source/ngc/menu.cpp b/source/ngc/menu.cpp index 1a7781d..e202ed7 100644 --- a/source/ngc/menu.cpp +++ b/source/ngc/menu.cpp @@ -40,6 +40,8 @@ extern "C" { #include "menu.h" #include "wiiusbsupport.h" +#define THREAD_SLEEP 100 + #ifdef HW_RVL GuiImageData * pointer[4]; #endif @@ -99,7 +101,7 @@ HaltGui() // wait for thread to finish while(!LWP_ThreadIsSuspended(guithread)) - usleep(50); + usleep(THREAD_SLEEP); } /**************************************************************************** @@ -194,7 +196,7 @@ WindowPrompt(const char *title, const char *msg, const char *btn1Label, const ch while(choice == -1) { - VIDEO_WaitVSync(); + usleep(THREAD_SLEEP); if(btn1.GetState() == STATE_CLICKED) choice = 1; @@ -203,7 +205,7 @@ WindowPrompt(const char *title, const char *msg, const char *btn1Label, const ch } promptWindow.SetEffect(EFFECT_SLIDE_TOP | EFFECT_SLIDE_OUT, 50); - while(promptWindow.GetEffect() > 0) usleep(50); + while(promptWindow.GetEffect() > 0) usleep(THREAD_SLEEP); HaltGui(); mainWindow->Remove(&promptWindow); mainWindow->SetState(STATE_DEFAULT); @@ -291,6 +293,7 @@ UpdateGUI (void *arg) #endif } } + usleep(THREAD_SLEEP); } return NULL; } @@ -380,7 +383,7 @@ ProgressWindow(char *title, char *msg) while(showProgress) { - VIDEO_WaitVSync(); + usleep(20000); if(showProgress == 1) { @@ -443,7 +446,7 @@ CancelAction() // wait for thread to finish while(!LWP_ThreadIsSuspended(progressthread)) - usleep(50); + usleep(THREAD_SLEEP); } /**************************************************************************** @@ -572,7 +575,7 @@ static void OnScreenKeyboard(char * var, u32 maxlen) while(save == -1) { - VIDEO_WaitVSync(); + usleep(THREAD_SLEEP); if(okBtn.GetState() == STATE_CLICKED) save = 1; @@ -665,7 +668,7 @@ SettingWindow(const char * title, GuiWindow * w) while(save == -1) { - VIDEO_WaitVSync(); + usleep(THREAD_SLEEP); if(okBtn.GetState() == STATE_CLICKED) save = 1; @@ -800,6 +803,7 @@ static void WindowCredits(void * ptr) if(userInput[i].wpad.btns_d || userInput[i].pad.btns_d) exit = true; } + usleep(THREAD_SLEEP); } // clear buttons pressed @@ -915,7 +919,7 @@ static int MenuGameSelection() while(menu == MENU_NONE) { - VIDEO_WaitVSync(); + usleep(THREAD_SLEEP); // update gameWindow based on arrow buttons // set MENU_EXIT if A button pressed on a game @@ -1254,7 +1258,7 @@ static int MenuGame() while(menu == MENU_NONE) { - VIDEO_WaitVSync(); + usleep(THREAD_SLEEP); #ifdef HW_RVL int level; @@ -1559,7 +1563,7 @@ static int MenuGameSaves(int action) while(menu == MENU_NONE) { - VIDEO_WaitVSync (); + usleep(THREAD_SLEEP); ret = saveBrowser.GetClickedSave(); @@ -1826,7 +1830,7 @@ static int MenuGameSettings() while(menu == MENU_NONE) { - VIDEO_WaitVSync (); + usleep(THREAD_SLEEP); if(mappingBtn.GetState() == STATE_CLICKED) { @@ -1947,7 +1951,7 @@ static int MenuGameSettings() while(menu == MENU_NONE) { - VIDEO_WaitVSync (); + usleep(THREAD_SLEEP); for(i=0; i < Cheat.num_cheats; i++) sprintf (options.value[i], "%s", Cheat.c[i].enabled == true ? "On" : "Off"); @@ -2127,7 +2131,7 @@ static int MenuSettingsMappings() while(menu == MENU_NONE) { - VIDEO_WaitVSync (); + usleep(THREAD_SLEEP); if(keyboardBtn.GetState() == STATE_CLICKED) { @@ -2234,7 +2238,7 @@ ButtonMappingWindow() while(pressed == 0) { - VIDEO_WaitVSync(); + usleep(THREAD_SLEEP); if(mapMenuCtrl == CTRLR_GCPAD) { @@ -2379,7 +2383,7 @@ static int MenuSettingsMappingsMap() while(menu == MENU_NONE) { - VIDEO_WaitVSync (); + usleep(THREAD_SLEEP); for(i=0; i < options.length; i++) { @@ -2685,7 +2689,7 @@ static int MenuSettingsVideo() while(menu == MENU_NONE) { - VIDEO_WaitVSync (); + usleep(THREAD_SLEEP); if (GCSettings.render == 0) sprintf (options.value[0], "Original"); @@ -2909,7 +2913,7 @@ static int MenuSettings() while(menu == MENU_NONE) { - VIDEO_WaitVSync (); + usleep(THREAD_SLEEP); if(savingBtn.GetState() == STATE_CLICKED) { @@ -3014,7 +3018,7 @@ static int MenuSettingsFile() while(menu == MENU_NONE) { - VIDEO_WaitVSync (); + usleep(THREAD_SLEEP); // some load/save methods are not implemented - here's where we skip them // they need to be skipped in the order they were enumerated in snes9xGX.h @@ -3200,7 +3204,7 @@ static int MenuSettingsMenu() while(menu == MENU_NONE) { - VIDEO_WaitVSync (); + usleep(THREAD_SLEEP); #ifdef HW_RVL if (GCSettings.ExitAction == 1) @@ -3333,7 +3337,7 @@ static int MenuSettingsNetwork() while(menu == MENU_NONE) { - VIDEO_WaitVSync (); + usleep(THREAD_SLEEP); strncpy (options.value[0], GCSettings.smbip, 15); strncpy (options.value[1], GCSettings.smbshare, 19); @@ -3499,6 +3503,7 @@ MainMenu (int menu) break; } lastMenu = currentMenu; + usleep(THREAD_SLEEP); } #ifdef HW_RVL diff --git a/source/ngc/video.cpp b/source/ngc/video.cpp index 50a6970..489b589 100644 --- a/source/ngc/video.cpp +++ b/source/ngc/video.cpp @@ -136,7 +136,7 @@ InitVideoThread () * Stock code to copy the GX buffer to the current display mode. * Also increments the frameticker, as it's called for each vb. ***************************************************************************/ -static void +static inline void copy_to_xfb (u32 arg) { if (copynow == GX_TRUE) @@ -153,7 +153,7 @@ copy_to_xfb (u32 arg) /**************************************************************************** * Scaler Support Functions ****************************************************************************/ -static void draw_init(void) +static inline void draw_init(void) { GX_ClearVtxDesc (); GX_SetVtxDesc (GX_VA_POS, GX_INDEX8); @@ -186,14 +186,14 @@ static void draw_init(void) GX_InitTexObjLOD(&texobj,GX_NEAR,GX_NEAR_MIP_NEAR,2.5,9.0,0.0,GX_FALSE,GX_FALSE,GX_ANISO_1); // original/unfiltered video mode: force texture filtering OFF } -static void draw_vert(u8 pos, u8 c, f32 s, f32 t) +static inline void draw_vert(u8 pos, u8 c, f32 s, f32 t) { GX_Position1x8(pos); GX_Color1x8(c); GX_TexCoord2f32(s, t); } -static void draw_square(Mtx v) +static inline void draw_square(Mtx v) { Mtx m; // model matrix. Mtx mv; // modelview matrix. @@ -221,7 +221,7 @@ static void draw_square(Mtx v) } #ifdef HW_RVL -static void draw_cursor(Mtx v) +static inline void draw_cursor(Mtx v) { if (!CursorVisible || !CursorValid) return; @@ -474,7 +474,7 @@ InitializeVideo () } -static void UpdateScaling() +static inline void UpdateScaling() { int xscale; int yscale;