usbloadergx/source/prompts/ProgressWindow.cpp

424 lines
14 KiB
C++
Raw Normal View History

/****************************************************************************
* ProgressWindow
* USB Loader GX 2009
*
* ProgressWindow.cpp
***************************************************************************/
#include <gccore.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "menu/menus.h"
#include "sys.h"
#include "language/gettext.h"
#include "libwiigui/gui.h"
#include "prompts/ProgressWindow.h"
#include "usbloader/wbfs.h"
#include "usbloader/utils.h"
#include "themes/CTheme.h"
#include "utils/timer.h"
/*** Variables used only in this file ***/
static lwp_t progressthread = LWP_THREAD_NULL;
static mutex_t ProgressMutex = LWP_MUTEX_NULL;
static ProgressAbortCallback AbortCallback = NULL;
static const char * progressTitle = NULL;
static const char * progressMsg1 = NULL;
static const char * progressMsg2 = NULL;
static Timer ProgressTimer;
static int showProgress = 0;
static s64 progressDone = -1;
static s64 progressTotal = -1;
static bool showTime = false;
static bool showSize = false;
2009-10-20 01:09:27 +02:00
static bool changed = true;
static bool changedMessages = true;
/****************************************************************************
* StartProgress
2010-09-24 02:48:03 +02:00
***************************************************************************/
extern "C" void StartProgress(const char * title, const char * msg1, const char * msg2, bool swSize, bool swTime)
{
progressTitle = title;
progressMsg1 = msg1;
progressMsg2 = msg2;
showSize = swSize;
showTime = swTime;
showProgress = 1;
ProgressTimer.reset();
LWP_ResumeThread(progressthread);
}
/****************************************************************************
* ShowProgress
*
* Callbackfunction for updating the progress values
* Use this function as standard callback
2010-09-24 02:48:03 +02:00
***************************************************************************/
extern "C" void ShowProgress(s64 done, s64 total)
{
LWP_MutexLock(ProgressMutex);
progressDone = done;
progressTotal = total;
if(!done)
{
ProgressTimer.reset();
LWP_ResumeThread(progressthread);
showProgress = 1;
}
changed = true;
LWP_MutexUnlock(ProgressMutex);
}
void ShowProgress(const char *title, const char *msg1, const char *msg2, s64 done, s64 total, bool swSize, bool swTime)
{
if (total <= 0)
return;
2009-10-20 01:09:27 +02:00
else if (done > total)
done = total;
LWP_MutexLock(ProgressMutex);
progressDone = done;
progressTotal = total;
progressTitle = title;
progressMsg1 = msg1;
progressMsg2 = msg2;
showSize = swSize;
showTime = swTime;
if(!done)
{
ProgressTimer.reset();
LWP_ResumeThread(progressthread);
showProgress = 1;
}
changedMessages = true;
changed = true;
LWP_MutexUnlock(ProgressMutex);
}
/****************************************************************************
* ProgressStop
***************************************************************************/
extern "C" void ProgressStop()
{
showProgress = 0;
progressTitle = NULL;
progressMsg1 = NULL;
progressMsg2 = NULL;
progressDone = -1;
progressTotal = -1;
showTime = false;
showSize = false;
// wait for thread to finish
while (!LWP_ThreadIsSuspended(progressthread))
usleep(100);
}
/****************************************************************************
* ProgressSetAbortCallback
*
* Set a callback for the cancel button
2010-09-24 02:48:03 +02:00
***************************************************************************/
extern "C" void ProgressSetAbortCallback(ProgressAbortCallback callback)
{
AbortCallback = callback;
}
/****************************************************************************
* UpdateProgressValues
***************************************************************************/
static void UpdateProgressValues(GuiImage *progressbarImg, GuiText *prTxt, GuiText *timeTxt, GuiText *speedTxt, GuiText *sizeTxt)
{
if(!changed)
return;
*Fixed display of partition size on WBFS partitions with a different wbfs sector size than 512bytes. *Made the ProgressWindow for game installation more accurate *Added displaying newly installed games (marked as new) on favorite list, so you don't have to change to full list when installing new games. (Thanks Cyan for the patch) *Lot's a small fixes *Added WDM Menu on game start. You can set it in the alternative DOL option (one new option there). The menu lists all DOLs on the disc and if a wdm file is provided in the WDM path (configurable in the settings) than the dol parameter and dol replacement name will be taken from the wdm. The DOLs that are not listed in the WDM but exist on the DISC will be listed at the end of the list. *Added avoid of multiple app cleanup when game fails to boot *Changed libfat to use FS info sector on FAT32 partitions. This speeds up the free space information getting to instant. For that the FS info sector has to have correct values. The values of all partitions where homebrews were writing to are currently incorrect because the official libfat does not support FS info sector (i submited a patch) (Windows does write it correct though). That is why there needs to be a synchronization of the FS info sector for partitions used with homebrews. For this purpose a new setting was added in the Loader Settings. You can synchronize all your FAT32 partitions on the USB with it once and you are done (if you don't write to that partition with current homebrews). After that you can enable free space display and it will be instant like on WBFS/NTFS/EXT partitions.
2011-01-16 14:12:07 +01:00
extern u64 gamesize;
LWP_MutexLock(ProgressMutex);
changed = false;
changedMessages = false;
s64 done = progressDone;
s64 total = progressTotal;
u32 speed = 0;
*Fixed display of partition size on WBFS partitions with a different wbfs sector size than 512bytes. *Made the ProgressWindow for game installation more accurate *Added displaying newly installed games (marked as new) on favorite list, so you don't have to change to full list when installing new games. (Thanks Cyan for the patch) *Lot's a small fixes *Added WDM Menu on game start. You can set it in the alternative DOL option (one new option there). The menu lists all DOLs on the disc and if a wdm file is provided in the WDM path (configurable in the settings) than the dol parameter and dol replacement name will be taken from the wdm. The DOLs that are not listed in the WDM but exist on the DISC will be listed at the end of the list. *Added avoid of multiple app cleanup when game fails to boot *Changed libfat to use FS info sector on FAT32 partitions. This speeds up the free space information getting to instant. For that the FS info sector has to have correct values. The values of all partitions where homebrews were writing to are currently incorrect because the official libfat does not support FS info sector (i submited a patch) (Windows does write it correct though). That is why there needs to be a synchronization of the FS info sector for partitions used with homebrews. For this purpose a new setting was added in the Loader Settings. You can synchronize all your FAT32 partitions on the USB with it once and you are done (if you don't write to that partition with current homebrews). After that you can enable free space display and it will be instant like on WBFS/NTFS/EXT partitions.
2011-01-16 14:12:07 +01:00
if(gamesize > 0)
{
*Fixed display of partition size on WBFS partitions with a different wbfs sector size than 512bytes. *Made the ProgressWindow for game installation more accurate *Added displaying newly installed games (marked as new) on favorite list, so you don't have to change to full list when installing new games. (Thanks Cyan for the patch) *Lot's a small fixes *Added WDM Menu on game start. You can set it in the alternative DOL option (one new option there). The menu lists all DOLs on the disc and if a wdm file is provided in the WDM path (configurable in the settings) than the dol parameter and dol replacement name will be taken from the wdm. The DOLs that are not listed in the WDM but exist on the DISC will be listed at the end of the list. *Added avoid of multiple app cleanup when game fails to boot *Changed libfat to use FS info sector on FAT32 partitions. This speeds up the free space information getting to instant. For that the FS info sector has to have correct values. The values of all partitions where homebrews were writing to are currently incorrect because the official libfat does not support FS info sector (i submited a patch) (Windows does write it correct though). That is why there needs to be a synchronization of the FS info sector for partitions used with homebrews. For this purpose a new setting was added in the Loader Settings. You can synchronize all your FAT32 partitions on the USB with it once and you are done (if you don't write to that partition with current homebrews). After that you can enable free space display and it will be instant like on WBFS/NTFS/EXT partitions.
2011-01-16 14:12:07 +01:00
done = (s64) ((double) done / (double) total * (double) gamesize);
total = (s64) gamesize;
}
//Calculate speed in KB/s
if (ProgressTimer.elapsed() > 0.0f)
speed = (u32) (done/ProgressTimer.elapsed());
LWP_MutexUnlock(ProgressMutex);
*Fixed display of partition size on WBFS partitions with a different wbfs sector size than 512bytes. *Made the ProgressWindow for game installation more accurate *Added displaying newly installed games (marked as new) on favorite list, so you don't have to change to full list when installing new games. (Thanks Cyan for the patch) *Lot's a small fixes *Added WDM Menu on game start. You can set it in the alternative DOL option (one new option there). The menu lists all DOLs on the disc and if a wdm file is provided in the WDM path (configurable in the settings) than the dol parameter and dol replacement name will be taken from the wdm. The DOLs that are not listed in the WDM but exist on the DISC will be listed at the end of the list. *Added avoid of multiple app cleanup when game fails to boot *Changed libfat to use FS info sector on FAT32 partitions. This speeds up the free space information getting to instant. For that the FS info sector has to have correct values. The values of all partitions where homebrews were writing to are currently incorrect because the official libfat does not support FS info sector (i submited a patch) (Windows does write it correct though). That is why there needs to be a synchronization of the FS info sector for partitions used with homebrews. For this purpose a new setting was added in the Loader Settings. You can synchronize all your FAT32 partitions on the USB with it once and you are done (if you don't write to that partition with current homebrews). After that you can enable free space display and it will be instant like on WBFS/NTFS/EXT partitions.
2011-01-16 14:12:07 +01:00
u32 TimeLeft = 0, h = 0, m = 0, s = 0;
if(speed > 0)
TimeLeft = (total-done)/speed;
*Fixed display of partition size on WBFS partitions with a different wbfs sector size than 512bytes. *Made the ProgressWindow for game installation more accurate *Added displaying newly installed games (marked as new) on favorite list, so you don't have to change to full list when installing new games. (Thanks Cyan for the patch) *Lot's a small fixes *Added WDM Menu on game start. You can set it in the alternative DOL option (one new option there). The menu lists all DOLs on the disc and if a wdm file is provided in the WDM path (configurable in the settings) than the dol parameter and dol replacement name will be taken from the wdm. The DOLs that are not listed in the WDM but exist on the DISC will be listed at the end of the list. *Added avoid of multiple app cleanup when game fails to boot *Changed libfat to use FS info sector on FAT32 partitions. This speeds up the free space information getting to instant. For that the FS info sector has to have correct values. The values of all partitions where homebrews were writing to are currently incorrect because the official libfat does not support FS info sector (i submited a patch) (Windows does write it correct though). That is why there needs to be a synchronization of the FS info sector for partitions used with homebrews. For this purpose a new setting was added in the Loader Settings. You can synchronize all your FAT32 partitions on the USB with it once and you are done (if you don't write to that partition with current homebrews). After that you can enable free space display and it will be instant like on WBFS/NTFS/EXT partitions.
2011-01-16 14:12:07 +01:00
if(TimeLeft > 0)
{
h = TimeLeft / 3600;
m = (TimeLeft / 60) % 60;
s = TimeLeft % 60;
}
float progressPercent = 100.0 * done / total;
prTxt->SetTextf("%.2f", progressPercent);
if (Settings.widescreen && Settings.wsprompt)
progressbarImg->SetSkew(0, 0, static_cast<int> (progressbarImg->GetWidth() * progressPercent * Settings.WSFactor)
- progressbarImg->GetWidth(), 0, static_cast<int> (progressbarImg->GetWidth() * progressPercent
* Settings.WSFactor) - progressbarImg->GetWidth(), 0, 0, 0);
else
progressbarImg->SetSkew(0, 0, static_cast<int> (progressbarImg->GetWidth() * progressPercent)
- progressbarImg->GetWidth(), 0, static_cast<int> (progressbarImg->GetWidth() * progressPercent)
- progressbarImg->GetWidth(), 0, 0, 0);
if (showTime == true)
{
timeTxt->SetTextf("%s %d:%02d:%02d", tr( "Time left:" ), h, m, s);
}
if (showSize == true)
{
if (total < MB_SIZE)
sizeTxt->SetTextf("%0.2fKB/%0.2fKB", done / KB_SIZE, total / KB_SIZE);
else if (total > MB_SIZE && total < GB_SIZE)
sizeTxt->SetTextf("%0.2fMB/%0.2fMB", done / MB_SIZE, total / MB_SIZE);
else
sizeTxt->SetTextf("%0.2fGB/%0.2fGB", done / GB_SIZE, total / GB_SIZE);
speedTxt->SetTextf("%dKB/s", (int) (speed/KB_SIZE));
}
}
/****************************************************************************
* ProgressWindow
*
* Opens a window, which displays progress to the user. Can either display a
* progress bar showing % completion, or a throbber that only shows that an
* action is in progress.
***************************************************************************/
2010-09-24 02:48:03 +02:00
static void ProgressWindow(const char *title, const char *msg1, const char *msg2)
{
2010-09-24 02:48:03 +02:00
GuiWindow promptWindow(472, 320);
promptWindow.SetAlignment(ALIGN_CENTRE, ALIGN_MIDDLE);
promptWindow.SetPosition(0, -10);
GuiImageData btnOutline(Resources::GetFile("button_dialogue_box.png"), Resources::GetFileSize("button_dialogue_box.png"));
GuiImageData dialogBox(Resources::GetFile("dialogue_box.png"), Resources::GetFileSize("dialogue_box.png"));
GuiTrigger trigA;
2010-09-24 02:48:03 +02:00
trigA.SetSimpleTrigger(-1, WPAD_BUTTON_A | WPAD_CLASSIC_BUTTON_A, PAD_BUTTON_A);
2010-09-24 02:48:03 +02:00
GuiImage dialogBoxImg(&dialogBox);
2010-10-27 16:45:27 +02:00
if (Settings.wsprompt)
{
2010-09-24 02:48:03 +02:00
dialogBoxImg.SetWidescreen(Settings.widescreen);
}
GuiImageData progressbarOutline(Resources::GetFile("progressbar_outline.png"), Resources::GetFileSize("progressbar_outline.png"));
2010-09-24 02:48:03 +02:00
GuiImage progressbarOutlineImg(&progressbarOutline);
2010-10-27 16:45:27 +02:00
if (Settings.wsprompt)
{
2010-09-24 02:48:03 +02:00
progressbarOutlineImg.SetWidescreen(Settings.widescreen);
}
2010-09-24 02:48:03 +02:00
progressbarOutlineImg.SetAlignment(ALIGN_LEFT, ALIGN_MIDDLE);
progressbarOutlineImg.SetPosition(35, 40);
2010-09-24 02:48:03 +02:00
GuiImageData progressbarEmpty(Resources::GetFile("progressbar_empty.png"), Resources::GetFileSize("button_dialogue_box.png"));
2010-09-24 02:48:03 +02:00
GuiImage progressbarEmptyImg(&progressbarEmpty);
progressbarEmptyImg.SetAlignment(ALIGN_LEFT, ALIGN_MIDDLE);
progressbarEmptyImg.SetPosition(35, 40);
2010-09-24 02:48:03 +02:00
progressbarEmptyImg.SetTile(100);
GuiImageData progressbar(Resources::GetFile("progressbar.png"), Resources::GetFileSize("progressbar.png"));
2010-09-24 02:48:03 +02:00
GuiImage progressbarImg(&progressbar);
progressbarImg.SetAlignment(ALIGN_LEFT, ALIGN_MIDDLE);
progressbarImg.SetPosition(35, 40);
2010-09-24 02:48:03 +02:00
GuiText titleTxt(title, 26, thColor("r=0 g=0 b=0 a=255 - prompt windows text color"));
2010-09-24 02:48:03 +02:00
titleTxt.SetAlignment(ALIGN_CENTRE, ALIGN_TOP);
titleTxt.SetPosition(0, 60);
GuiText msg1Txt(msg1, 22, thColor("r=0 g=0 b=0 a=255 - prompt windows text color"));
2010-09-24 02:48:03 +02:00
msg1Txt.SetAlignment(ALIGN_CENTRE, ALIGN_TOP);
if (msg2)
msg1Txt.SetPosition(0, 100);
else
2010-09-24 02:48:03 +02:00
msg1Txt.SetPosition(0, 120);
msg1Txt.SetMaxWidth(430, DOTTED);
GuiText msg2Txt(msg2, 22, thColor("r=0 g=0 b=0 a=255 - prompt windows text color"));
2010-09-24 02:48:03 +02:00
msg2Txt.SetAlignment(ALIGN_CENTRE, ALIGN_TOP);
msg2Txt.SetPosition(0, 125);
msg2Txt.SetMaxWidth(430, DOTTED);
GuiText prsTxt("%", 22, thColor("r=0 g=0 b=0 a=255 - prompt windows text color"));
2010-09-24 02:48:03 +02:00
prsTxt.SetAlignment(ALIGN_RIGHT, ALIGN_MIDDLE);
prsTxt.SetPosition(-178, 40);
2010-09-24 02:48:03 +02:00
GuiText timeTxt((char*) NULL, 22, thColor("r=0 g=0 b=0 a=255 - prompt windows text color"));
2010-09-24 02:48:03 +02:00
timeTxt.SetAlignment(ALIGN_LEFT, ALIGN_BOTTOM);
timeTxt.SetPosition(280, -50);
GuiText sizeTxt((char*) NULL, 22, thColor("r=0 g=0 b=0 a=255 - prompt windows text color"));
2010-09-24 02:48:03 +02:00
sizeTxt.SetAlignment(ALIGN_LEFT, ALIGN_BOTTOM);
sizeTxt.SetPosition(50, -50);
GuiText speedTxt((char*) NULL, 22, thColor("r=0 g=0 b=0 a=255 - prompt windows text color"));
2010-09-24 02:48:03 +02:00
speedTxt.SetAlignment(ALIGN_LEFT, ALIGN_BOTTOM);
speedTxt.SetPosition(50, -74);
GuiText prTxt((char*) NULL, 26, thColor("r=0 g=0 b=0 a=255 - prompt windows text color"));
2010-09-24 02:48:03 +02:00
prTxt.SetAlignment(ALIGN_LEFT, ALIGN_MIDDLE);
prTxt.SetPosition(210, 40);
2010-09-24 02:48:03 +02:00
2010-10-27 16:45:27 +02:00
if ((Settings.wsprompt) && (Settings.widescreen)) /////////////adjust for widescreen
{
2010-09-24 02:48:03 +02:00
progressbarOutlineImg.SetAlignment(ALIGN_CENTRE, ALIGN_MIDDLE);
progressbarOutlineImg.SetPosition(0, 40);
progressbarEmptyImg.SetPosition(80, 40);
progressbarEmptyImg.SetTile(78);
progressbarImg.SetPosition(80, 40);
msg1Txt.SetMaxWidth(380, DOTTED);
msg2Txt.SetMaxWidth(380, DOTTED);
timeTxt.SetPosition(250, -50);
timeTxt.SetFontSize(20);
speedTxt.SetPosition(90, -74);
speedTxt.SetFontSize(20);
sizeTxt.SetPosition(90, -50);
sizeTxt.SetFontSize(20);
}
GuiText cancelTxt(tr( "Cancel" ), 22, thColor("r=0 g=0 b=0 a=255 - prompt windows text color"));
GuiImage cancelImg(&btnOutline);
if (Settings.wsprompt)
{
cancelTxt.SetWidescreen(Settings.widescreen);
cancelImg.SetWidescreen(Settings.widescreen);
}
GuiButton cancelBtn(&cancelImg, &cancelImg, 2, 4, 0, -45, &trigA, btnSoundOver, btnSoundClick2, 1);
cancelBtn.SetLabel(&cancelTxt);
cancelBtn.SetState(STATE_SELECTED);
2010-09-24 02:48:03 +02:00
usleep(400000); // wait to see if progress flag changes soon
if (!showProgress) return;
promptWindow.Append(&dialogBoxImg);
promptWindow.Append(&progressbarEmptyImg);
promptWindow.Append(&progressbarImg);
promptWindow.Append(&progressbarOutlineImg);
promptWindow.Append(&prTxt);
promptWindow.Append(&prsTxt);
if (title) promptWindow.Append(&titleTxt);
promptWindow.Append(&msg1Txt);
promptWindow.Append(&msg2Txt);
2010-09-24 02:48:03 +02:00
if (showTime) promptWindow.Append(&timeTxt);
if (showSize)
{
2010-09-24 02:48:03 +02:00
promptWindow.Append(&sizeTxt);
promptWindow.Append(&speedTxt);
}
if(AbortCallback)
promptWindow.Append(&cancelBtn);
HaltGui();
2010-09-24 02:48:03 +02:00
promptWindow.SetEffect(EFFECT_SLIDE_TOP | EFFECT_SLIDE_IN, 50);
mainWindow->SetState(STATE_DISABLED);
mainWindow->Append(&promptWindow);
mainWindow->ChangeFocus(&promptWindow);
ResumeGui();
while (promptWindow.GetEffect() > 0) usleep(100);
2010-09-24 02:48:03 +02:00
while (showProgress)
{
usleep(50000);
if (shutdown)
Sys_Shutdown();
if (reset)
Sys_Reboot();
2010-09-24 02:48:03 +02:00
if (changed)
{
if (changedMessages && progressTitle) titleTxt.SetText(progressTitle);
if (changedMessages && progressMsg1) msg1Txt.SetText(progressMsg1);
if (changedMessages && progressMsg2) msg2Txt.SetText(progressMsg2);
UpdateProgressValues(&progressbarImg, &prTxt, &timeTxt, &speedTxt, &sizeTxt);
}
if(cancelBtn.GetState() == STATE_CLICKED)
{
if(AbortCallback) AbortCallback();
cancelBtn.ResetState();
2009-10-20 01:09:27 +02:00
}
}
2010-09-24 02:48:03 +02:00
promptWindow.SetEffect(EFFECT_SLIDE_TOP | EFFECT_SLIDE_OUT, 50);
while (promptWindow.GetEffect() > 0) usleep(100);
HaltGui();
2010-09-24 02:48:03 +02:00
mainWindow->Remove(&promptWindow);
mainWindow->SetState(STATE_DEFAULT);
ResumeGui();
}
/****************************************************************************
* ProgressThread
2010-09-24 02:48:03 +02:00
***************************************************************************/
static void * ProgressThread(void *arg)
{
2010-09-24 02:48:03 +02:00
while (1)
{
2010-09-24 02:48:03 +02:00
if (!showProgress) LWP_SuspendThread(progressthread);
2010-09-24 02:48:03 +02:00
ProgressWindow(progressTitle, progressMsg1, progressMsg2);
usleep(100);
}
return NULL;
}
/****************************************************************************
* InitProgressThread
*
* Startup Progressthread in idle prio
***************************************************************************/
void InitProgressThread()
{
LWP_MutexInit(&ProgressMutex, true);
LWP_CreateThread(&progressthread, ProgressThread, NULL, NULL, 16384, 70);
}
/****************************************************************************
* ExitProgressThread
*
* Shutdown Progressthread
***************************************************************************/
void ExitProgressThread()
{
2010-09-24 02:48:03 +02:00
LWP_JoinThread(progressthread, NULL);
progressthread = LWP_THREAD_NULL;
LWP_MutexUnlock(ProgressMutex);
LWP_MutexDestroy(ProgressMutex);
}