Skip to content

Commit

Permalink
- Store sorted view separately.
Browse files Browse the repository at this point in the history
  • Loading branch information
Extrems committed Feb 13, 2024
1 parent 54e6e3e commit ca21f57
Show file tree
Hide file tree
Showing 8 changed files with 165 additions and 156 deletions.
4 changes: 3 additions & 1 deletion cube/swiss/include/files.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
#include <stdint.h>
#include "deviceHandler.h"

void sortFiles(file_handle* dir, int num_files);
file_handle** sortFiles(file_handle* dir, int num_files);
void freeFiles();
void scanFiles();
file_handle** getSortedDirEntries();
file_handle* getCurrentDirEntries();
#define getSortedDirEntryCount getCurrentDirEntryCount
int getCurrentDirEntryCount();
void concat_path(char *pathName, const char *dirName, const char *baseName);
void concatf_path(char *pathName, const char *dirName, const char *baseName, ...);
Expand Down
2 changes: 1 addition & 1 deletion cube/swiss/include/mp3.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@
#define PLAYER_NEXT 2
#define PLAYER_PREV 3

extern void mp3_player(file_handle* allFiles, int numFiles, file_handle* curFile);
extern void mp3_player(file_handle** allFiles, int numFiles, file_handle* curFile);

#endif
2 changes: 1 addition & 1 deletion cube/swiss/include/swiss.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ extern void drawFiles(file_handle** directory, int num_files, uiDrawObj_t *conta
extern void select_speed();
extern int select_slot();
extern void select_device(int type);
extern bool select_dest_dir(file_handle* directory, file_handle* selection);
extern bool select_dest_dir(file_handle* initial, file_handle* selection);

typedef struct {
int debugUSB; // Debug prints over USBGecko
Expand Down
30 changes: 12 additions & 18 deletions cube/swiss/source/devices/filemeta.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,15 @@ file_meta* meta_alloc() {

file_meta* meta = __lwp_heap_allocate(meta_cache, sizeof(file_meta));
// While there's no room to allocate, call release
file_handle* dirEntries = getCurrentDirEntries();
file_handle** dirEntries = getSortedDirEntries();
int dirEntryCount = getSortedDirEntryCount();
while(!meta) {
int i = 0;
for (i = 0; i < getCurrentDirEntryCount(); i++) {
for(int i = 0; i < dirEntryCount; i++) {
if(!in_range(i, current_view_start, current_view_end)) {
if(dirEntries[i].meta && trylockFile(&dirEntries[i])) {
meta_free(dirEntries[i].meta);
dirEntries[i].meta = NULL;
unlockFile(&dirEntries[i]);
if(dirEntries[i]->meta && trylockFile(dirEntries[i])) {
meta_free(dirEntries[i]->meta);
dirEntries[i]->meta = NULL;
unlockFile(dirEntries[i]);
break;
}
}
Expand Down Expand Up @@ -400,24 +400,18 @@ static void *meta_thread_func(void *arg) {
file_handle *dirEntries = getCurrentDirEntries();
int dirEntryCount = getCurrentDirEntryCount();
for (int i = 0; i < dirEntryCount; i++) {
for (int j = 0; j < dirEntryCount; j++) {
if (dirEntries[j].fileBase != i)
continue;
if (trylockFile(&dirEntries[j])) {
populate_meta(&dirEntries[j]);
unlockFile(&dirEntries[j]);
}
break;
if (meta_thread != LWP_GetSelf()) break;
if (trylockFile(&dirEntries[i])) {
populate_meta(&dirEntries[i]);
unlockFile(&dirEntries[i]);
}
if (meta_thread != LWP_GetSelf())
break;
}
return NULL;
}

void meta_thread_start() {
if (devices[DEVICE_CUR]->features & FEAT_THREAD_SAFE)
LWP_CreateThread(&meta_thread, meta_thread_func, NULL, NULL, 16*1024, LWP_PRIO_NORMAL);
LWP_CreateThread(&meta_thread, meta_thread_func, NULL, NULL, 32*1024, LWP_PRIO_NORMAL);
}

void meta_thread_stop() {
Expand Down
30 changes: 19 additions & 11 deletions cube/swiss/source/files.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,14 @@
#include "files.h"
#include "devices/filemeta.h"

static file_handle** sortedDirEntries;
static file_handle* curDirEntries; // all the files in the current dir
static int curDirEntryCount; // count of files in the current dir

int fileComparator(const void *a1, const void *b1)
{
const file_handle* a = a1;
const file_handle* b = b1;

if(!a && b) return 1;
if(a && !b) return -1;
if(!a && !b) return 0;
const file_handle* a = *(const file_handle **)a1;
const file_handle* b = *(const file_handle **)b1;

if((devices[DEVICE_CUR] == &__device_dvd) && ((dvdDiscTypeInt == ISO9660_GAMECUBE_DISC) || (dvdDiscTypeInt == GAMECUBE_DISC) || (dvdDiscTypeInt == MULTIDISC_DISC)))
{
Expand All @@ -34,14 +31,21 @@ int fileComparator(const void *a1, const void *b1)
return strcasecmp(a->name, b->name);
}

void sortFiles(file_handle* dir, int num_files)
file_handle** sortFiles(file_handle* dir, int num_files)
{
if(num_files > 0) {
qsort(&dir[0],num_files,sizeof(file_handle), fileComparator);
file_handle** sortedDir = calloc(num_files, sizeof(file_handle*));
if(sortedDir) {
for(int i = 0; i < num_files; i++) {
sortedDir[i] = &dir[i];
}
qsort(sortedDir, num_files, sizeof(file_handle*), fileComparator);
}
return sortedDir;
}

void freeFiles() {
free(sortedDirEntries);
sortedDirEntries = NULL;
if(curDirEntries) {
for(int i = 0; i < curDirEntryCount; i++) {
if(curDirEntries[i].meta) {
Expand Down Expand Up @@ -78,16 +82,20 @@ void scanFiles() {
}
}
print_gecko("Found %i entries\r\n",curDirEntryCount);
sortFiles(curDirEntries, curDirEntryCount);
sortedDirEntries = sortFiles(curDirEntries, curDirEntryCount);
for(int i = 0; i < curDirEntryCount; i++) {
if(!strcmp(curDirEntries[i].name, curFile.name)) {
if(!strcmp(sortedDirEntries[i]->name, curFile.name)) {
curSelection = i;
break;
}
}
memcpy(&curFile, &curDir, sizeof(file_handle));
}

file_handle** getSortedDirEntries() {
return sortedDirEntries;
}

file_handle* getCurrentDirEntries() {
return curDirEntries;
}
Expand Down
8 changes: 4 additions & 4 deletions cube/swiss/source/mp3.c
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ int play_mp3(file_handle *file, int numFiles, int curMP3) {
}

/* Plays a MP3 file */
void mp3_player(file_handle* allFiles, int numFiles, file_handle* curFile) {
void mp3_player(file_handle** allFiles, int numFiles, file_handle* curFile) {
// Initialise the audio subsystem
MP3Player_Init();
MP3Player_Volume(volume);
Expand All @@ -121,7 +121,7 @@ void mp3_player(file_handle* allFiles, int numFiles, file_handle* curFile) {
int curMP3 = 0, i = 0;

for(i = 0; i < numFiles; i++) {
if(!strcmp(allFiles[i].name,curFile->name)) {
if(!strcmp(allFiles[i]->name,curFile->name)) {
curMP3 = i;
break;
}
Expand All @@ -134,8 +134,8 @@ void mp3_player(file_handle* allFiles, int numFiles, file_handle* curFile) {
i = (rand() % numFiles);
}
// if it's .mp3
if((strlen(allFiles[i].name)>4) && endsWith(allFiles[i].name,".mp3")) {
ret = play_mp3(&allFiles[i], numFiles, i);
if(endsWith(allFiles[i]->name,".mp3")) {
ret = play_mp3(allFiles[i], numFiles, i);
}
if(ret == PLAYER_STOP) {
break;
Expand Down

0 comments on commit ca21f57

Please sign in to comment.