2009-07-12 16:02:48 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* USB Loader GX Team
|
|
|
|
* buffer.cpp
|
|
|
|
*
|
|
|
|
* Loading covers in a background thread
|
|
|
|
***************************************************************************/
|
|
|
|
|
|
|
|
#include <gccore.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "libwiigui/gui.h"
|
|
|
|
#include "usbloader/disc.h"
|
|
|
|
#include "settings/cfg.h"
|
|
|
|
#include "buffer.h"
|
|
|
|
#include "main.h"
|
|
|
|
|
|
|
|
#define BUFFERSIZE 9
|
|
|
|
|
|
|
|
extern struct discHdr * gameList;
|
|
|
|
extern u32 gameCnt;
|
|
|
|
|
|
|
|
static lwp_t bufferthread = LWP_THREAD_NULL;
|
|
|
|
static bool BufferHalt = true;
|
|
|
|
static int loading = 0;
|
|
|
|
static int offset = 0;
|
|
|
|
static int direction = 1;
|
|
|
|
static bool changed = false;
|
|
|
|
static bool firstime = true;
|
|
|
|
|
|
|
|
static GuiImageData NoCoverData(nocover_png);
|
|
|
|
static GuiImageData * cover[BUFFERSIZE];
|
|
|
|
static GuiImage * coverImg[BUFFERSIZE];
|
|
|
|
static GuiImage * NoCover[BUFFERSIZE];
|
|
|
|
|
2009-07-30 07:41:12 +02:00
|
|
|
GuiImage * ImageBuffer(int imagenumber) {
|
|
|
|
if ((BUFFERSIZE-1 > imagenumber) && direction >= 0) {
|
2009-07-12 16:02:48 +02:00
|
|
|
return coverImg[imagenumber];
|
|
|
|
}
|
|
|
|
|
2009-07-30 07:41:12 +02:00
|
|
|
if ((0 < imagenumber) && direction < 0) {
|
2009-07-12 16:02:48 +02:00
|
|
|
return coverImg[imagenumber];
|
|
|
|
}
|
|
|
|
|
2009-07-30 07:41:12 +02:00
|
|
|
if (loading == BUFFERSIZE) {
|
2009-07-12 16:02:48 +02:00
|
|
|
return coverImg[imagenumber];
|
|
|
|
}
|
|
|
|
|
|
|
|
return NoCover[imagenumber];
|
|
|
|
}
|
|
|
|
|
2009-07-30 07:41:12 +02:00
|
|
|
void LoadImages() {
|
|
|
|
if (!changed || BufferHalt)
|
2009-07-12 16:02:48 +02:00
|
|
|
return;
|
|
|
|
|
2009-07-30 07:41:12 +02:00
|
|
|
char ID[4];
|
|
|
|
char IDfull[7];
|
2009-07-12 16:02:48 +02:00
|
|
|
char imgPath[200];
|
|
|
|
|
2009-07-30 07:41:12 +02:00
|
|
|
for (u32 i = offset; (int) i < offset+BUFFERSIZE; i++) {
|
2009-07-12 16:02:48 +02:00
|
|
|
|
2009-07-30 07:41:12 +02:00
|
|
|
struct discHdr *header;
|
|
|
|
if (i > gameCnt-1)
|
|
|
|
header = &gameList[i-gameCnt];
|
|
|
|
else
|
|
|
|
header = &gameList[i];
|
2009-07-12 16:02:48 +02:00
|
|
|
|
2009-07-30 07:41:12 +02:00
|
|
|
snprintf (ID,sizeof(ID),"%c%c%c", header->id[0], header->id[1], header->id[2]);
|
|
|
|
snprintf (IDfull,sizeof(IDfull),"%c%c%c%c%c%c", header->id[0], header->id[1], header->id[2],header->id[3], header->id[4], header->id[5]);
|
2009-07-12 16:02:48 +02:00
|
|
|
|
|
|
|
|
2009-07-30 07:41:12 +02:00
|
|
|
snprintf(imgPath, sizeof(imgPath), "%s%s.png", Settings.covers_path, IDfull); //Load full id image
|
2009-07-12 16:02:48 +02:00
|
|
|
|
2009-07-30 07:41:12 +02:00
|
|
|
//firsttime loading images into memory
|
|
|
|
if (firstime) {
|
2009-07-12 16:02:48 +02:00
|
|
|
|
2009-07-30 07:41:12 +02:00
|
|
|
if (coverImg[loading]) {
|
|
|
|
delete coverImg[loading];
|
|
|
|
coverImg[loading] = NULL;
|
|
|
|
}
|
|
|
|
if (cover[loading]) {
|
|
|
|
delete cover[loading];
|
|
|
|
cover[loading] = NULL;
|
|
|
|
}
|
2009-07-12 16:02:48 +02:00
|
|
|
|
2009-07-30 07:41:12 +02:00
|
|
|
cover[loading] = new GuiImageData(imgPath,0);
|
2009-07-12 16:02:48 +02:00
|
|
|
if (!cover[loading]->GetImage()) {
|
|
|
|
delete cover[loading];
|
|
|
|
cover[loading] = NULL;
|
|
|
|
snprintf(imgPath, sizeof(imgPath), "%s%s.png", Settings.covers_path, ID); //Load short id image
|
|
|
|
cover[loading] = new GuiImageData(imgPath, 0);
|
|
|
|
if (!cover[loading]->GetImage()) {
|
|
|
|
delete cover[loading];
|
|
|
|
cover[loading] = NULL;
|
2009-07-29 03:31:11 +02:00
|
|
|
snprintf(imgPath, sizeof(imgPath), "%snoimage.png", CFG.theme_path); //Load no image
|
2009-07-12 16:02:48 +02:00
|
|
|
cover[loading] = new GuiImageData(imgPath, nocover_png);
|
|
|
|
}
|
|
|
|
}
|
2009-07-30 07:41:12 +02:00
|
|
|
coverImg[loading] = new GuiImage(cover[loading]);
|
2009-07-12 16:02:48 +02:00
|
|
|
|
2009-07-30 07:41:12 +02:00
|
|
|
} else {
|
2009-07-12 16:02:48 +02:00
|
|
|
|
2009-07-30 07:41:12 +02:00
|
|
|
if (direction >= 0) {
|
2009-07-12 16:02:48 +02:00
|
|
|
|
2009-07-30 07:41:12 +02:00
|
|
|
if (loading < BUFFERSIZE-1) {
|
2009-07-12 16:02:48 +02:00
|
|
|
|
2009-07-30 07:41:12 +02:00
|
|
|
if (coverImg[loading]) {
|
|
|
|
delete coverImg[loading];
|
|
|
|
coverImg[loading] = NULL;
|
|
|
|
}
|
2009-07-12 16:02:48 +02:00
|
|
|
|
2009-07-30 07:41:12 +02:00
|
|
|
if (loading == 0) {
|
|
|
|
if (cover[loading]) {
|
|
|
|
delete cover[loading];
|
|
|
|
cover[loading] = NULL;
|
|
|
|
}
|
|
|
|
cover[loading] = new GuiImageData(NULL, 0);
|
|
|
|
cover[loading] = cover[loading+1];
|
|
|
|
} else {
|
|
|
|
cover[loading] = cover[loading+1];
|
2009-07-12 16:02:48 +02:00
|
|
|
}
|
|
|
|
|
2009-07-30 07:41:12 +02:00
|
|
|
coverImg[loading] = new GuiImage(cover[loading]);
|
2009-07-12 16:02:48 +02:00
|
|
|
|
2009-07-30 07:41:12 +02:00
|
|
|
} else {
|
2009-07-12 16:02:48 +02:00
|
|
|
|
2009-07-30 07:41:12 +02:00
|
|
|
cover[loading] = new GuiImageData(imgPath,0);
|
2009-07-12 16:02:48 +02:00
|
|
|
if (!cover[loading]->GetImage()) {
|
|
|
|
delete cover[loading];
|
|
|
|
cover[loading] = NULL;
|
2009-07-30 07:41:12 +02:00
|
|
|
snprintf(imgPath, sizeof(imgPath), "%s%s.png", Settings.covers_path, ID); //Load short id image
|
|
|
|
cover[loading] = new GuiImageData(imgPath, 0);
|
|
|
|
if (!cover[loading]->GetImage()) {
|
|
|
|
delete cover[loading];
|
|
|
|
cover[loading] = NULL;
|
|
|
|
snprintf(imgPath, sizeof(imgPath), "%snoimage.png", Settings.covers_path); //Load no image
|
|
|
|
cover[loading] = new GuiImageData(imgPath, nocover_png);
|
|
|
|
}
|
2009-07-12 16:02:48 +02:00
|
|
|
}
|
2009-07-30 07:41:12 +02:00
|
|
|
if (coverImg[loading]) {
|
|
|
|
delete coverImg[loading];
|
|
|
|
coverImg[loading] = NULL;
|
|
|
|
}
|
|
|
|
coverImg[loading] = new GuiImage(cover[loading]);
|
2009-07-12 16:02:48 +02:00
|
|
|
}
|
2009-07-30 07:41:12 +02:00
|
|
|
} else if (direction < 0) {
|
2009-07-12 16:02:48 +02:00
|
|
|
|
2009-07-30 07:41:12 +02:00
|
|
|
if (BUFFERSIZE-loading-1 > 0) {
|
2009-07-12 16:02:48 +02:00
|
|
|
|
2009-07-30 07:41:12 +02:00
|
|
|
if (coverImg[BUFFERSIZE-loading-1]) {
|
|
|
|
delete coverImg[BUFFERSIZE-loading-1];
|
|
|
|
coverImg[BUFFERSIZE-loading-1] = NULL;
|
|
|
|
}
|
2009-07-12 16:02:48 +02:00
|
|
|
|
2009-07-30 07:41:12 +02:00
|
|
|
if (BUFFERSIZE-loading-1 == BUFFERSIZE-1) {
|
|
|
|
if (cover[BUFFERSIZE-loading-1]) {
|
|
|
|
delete cover[BUFFERSIZE-loading-1];
|
|
|
|
cover[BUFFERSIZE-loading-1] = NULL;
|
|
|
|
}
|
|
|
|
cover[BUFFERSIZE-loading-1] = new GuiImageData(NULL, 0);
|
|
|
|
cover[BUFFERSIZE-loading-1] = cover[BUFFERSIZE-loading-1-1];
|
|
|
|
} else {
|
|
|
|
cover[BUFFERSIZE-loading-1] = cover[BUFFERSIZE-loading-1-1];
|
|
|
|
coverImg[BUFFERSIZE-loading-1] = new GuiImage(cover[BUFFERSIZE-loading-1]);
|
2009-07-12 16:02:48 +02:00
|
|
|
}
|
|
|
|
|
2009-07-30 07:41:12 +02:00
|
|
|
} else {
|
2009-07-12 16:02:48 +02:00
|
|
|
|
2009-07-30 07:41:12 +02:00
|
|
|
header = &gameList[offset];
|
2009-07-12 16:02:48 +02:00
|
|
|
|
2009-07-30 07:41:12 +02:00
|
|
|
snprintf (ID,sizeof(ID),"%c%c%c", header->id[0], header->id[1], header->id[2]);
|
|
|
|
snprintf (IDfull,sizeof(IDfull),"%c%c%c%c%c%c", header->id[0], header->id[1], header->id[2],header->id[3], header->id[4], header->id[5]);
|
2009-07-12 16:02:48 +02:00
|
|
|
|
2009-07-30 07:41:12 +02:00
|
|
|
snprintf(imgPath, sizeof(imgPath), "%s%s.png", Settings.covers_path, IDfull); //Load full id image
|
2009-07-12 16:02:48 +02:00
|
|
|
|
2009-07-30 07:41:12 +02:00
|
|
|
cover[0] = new GuiImageData(imgPath,0);
|
2009-07-12 16:02:48 +02:00
|
|
|
if (!cover[0]->GetImage()) {
|
|
|
|
delete cover[0];
|
|
|
|
cover[0] = NULL;
|
2009-07-30 07:41:12 +02:00
|
|
|
snprintf(imgPath, sizeof(imgPath), "%s%s.png", Settings.covers_path, ID); //Load short id image
|
|
|
|
cover[0] = new GuiImageData(imgPath, 0);
|
|
|
|
if (!cover[0]->GetImage()) {
|
|
|
|
delete cover[0];
|
|
|
|
cover[0] = NULL;
|
|
|
|
snprintf(imgPath, sizeof(imgPath), "%snoimage.png", Settings.covers_path); //Load no image
|
|
|
|
cover[0] = new GuiImageData(imgPath, nocover_png);
|
|
|
|
}
|
2009-07-12 16:02:48 +02:00
|
|
|
}
|
2009-07-30 07:41:12 +02:00
|
|
|
if (coverImg[0]) {
|
|
|
|
delete coverImg[0];
|
|
|
|
coverImg[0] = NULL;
|
|
|
|
}
|
|
|
|
coverImg[0] = new GuiImage(cover[0]);
|
2009-07-12 16:02:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-07-30 07:41:12 +02:00
|
|
|
loading++;
|
2009-07-12 16:02:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
loading = BUFFERSIZE;
|
|
|
|
changed = false;
|
|
|
|
firstime = false;
|
|
|
|
}
|
|
|
|
|
2009-07-30 07:41:12 +02:00
|
|
|
void NewOffset(int off, int d) {
|
|
|
|
if (offset == off || loading < BUFFERSIZE)
|
2009-07-12 16:02:48 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
direction = d;
|
|
|
|
|
|
|
|
offset = off;
|
|
|
|
|
|
|
|
loading = 0;
|
|
|
|
changed = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* HaltBuffer
|
|
|
|
***************************************************************************/
|
2009-07-30 07:41:12 +02:00
|
|
|
void HaltBufferThread() {
|
2009-07-12 16:02:48 +02:00
|
|
|
BufferHalt = true;
|
|
|
|
firstime = true;
|
|
|
|
changed = true;
|
|
|
|
loading = 0;
|
|
|
|
offset = 0;
|
|
|
|
direction = 0;
|
|
|
|
|
2009-07-30 07:41:12 +02:00
|
|
|
// wait for thread to finish
|
|
|
|
while (!LWP_ThreadIsSuspended(bufferthread))
|
|
|
|
usleep(100);
|
2009-07-12 16:02:48 +02:00
|
|
|
|
2009-07-30 07:41:12 +02:00
|
|
|
for (int i = 0; i < BUFFERSIZE; i++) {
|
2009-07-12 16:02:48 +02:00
|
|
|
delete cover[i];
|
|
|
|
cover[i] = NULL;
|
|
|
|
delete coverImg[i];
|
|
|
|
coverImg[i] = NULL;
|
|
|
|
delete NoCover[i];
|
|
|
|
NoCover[i] = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* ResumeBufferThread
|
|
|
|
***************************************************************************/
|
2009-07-30 07:41:12 +02:00
|
|
|
void ResumeBufferThread(int offset) {
|
|
|
|
BufferHalt = false;
|
2009-07-12 16:02:48 +02:00
|
|
|
firstime = true;
|
|
|
|
changed = true;
|
|
|
|
loading = 0;
|
|
|
|
offset = offset;
|
|
|
|
direction = 0;
|
|
|
|
|
2009-07-30 07:41:12 +02:00
|
|
|
for (u8 i = 0; i < BUFFERSIZE; i++) {
|
|
|
|
if (NoCover[i] != NULL) {
|
2009-07-12 16:02:48 +02:00
|
|
|
delete NoCover[i];
|
|
|
|
NoCover[i] = NULL;
|
|
|
|
}
|
|
|
|
NoCover[i] = new GuiImage(&NoCoverData);
|
|
|
|
}
|
|
|
|
|
2009-07-30 07:41:12 +02:00
|
|
|
LWP_ResumeThread(bufferthread);
|
2009-07-12 16:02:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************************
|
|
|
|
* Bufferthread
|
|
|
|
*********************************************************************************/
|
2009-07-30 07:41:12 +02:00
|
|
|
static void * bufferinitcallback(void *arg) {
|
|
|
|
while (1) {
|
|
|
|
if (BufferHalt)
|
2009-07-12 16:02:48 +02:00
|
|
|
LWP_SuspendThread(bufferthread);
|
|
|
|
|
|
|
|
LoadImages();
|
|
|
|
}
|
2009-07-30 07:41:12 +02:00
|
|
|
return NULL;
|
2009-07-12 16:02:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* InitBufferThread with priority 50
|
|
|
|
***************************************************************************/
|
2009-07-30 07:41:12 +02:00
|
|
|
void InitBufferThread() {
|
|
|
|
LWP_CreateThread(&bufferthread, bufferinitcallback, NULL, NULL, 0, 50);
|
2009-07-12 16:02:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* ShutdownThread
|
|
|
|
***************************************************************************/
|
2009-07-30 07:41:12 +02:00
|
|
|
void ShutdownBufferThread() {
|
|
|
|
LWP_JoinThread (bufferthread, NULL);
|
|
|
|
bufferthread = LWP_THREAD_NULL;
|
2009-07-12 16:02:48 +02:00
|
|
|
}
|