From aa85b13a8c45d1633fc9b5957285cced6b7810d4 Mon Sep 17 00:00:00 2001 From: FGL82 Date: Fri, 23 Jul 2021 22:58:52 -0300 Subject: [PATCH] Experimental (#175) * Fixing OD BETA OC * Fixing indentation and using always the same dir function * Logic so pico8 roms display themselves as the preview * Adding bittboy Co-authored-by: bittboy --- simplemenu/Makefile | 1 + simplemenu/src/logic/logic.c | 1781 +++++++++++++++-------------- simplemenu/src/logic/screen.c | 32 +- simplemenu/src/logic/simplemenu.c | 4 +- 4 files changed, 927 insertions(+), 891 deletions(-) diff --git a/simplemenu/Makefile b/simplemenu/Makefile index 8a1fe2d8..a5103591 100644 --- a/simplemenu/Makefile +++ b/simplemenu/Makefile @@ -6,6 +6,7 @@ PLATFORM = PC #PLATFORM = OD-BETA #PLATFORM = NPG #PLATFORM = RFW +#PLATFORM = BITTBOY # Compiler ifeq ($(PLATFORM), BITTBOY) diff --git a/simplemenu/src/logic/logic.c b/simplemenu/src/logic/logic.c index c6f38033..62cb8f49 100644 --- a/simplemenu/src/logic/logic.c +++ b/simplemenu/src/logic/logic.c @@ -30,9 +30,7 @@ #include "../headers/doubly_linked_rom_list.h" #include "../headers/utils.h" - - -FILE *getCurrentSectionAliasFile() { +FILE* getCurrentSectionAliasFile() { FILE *aliasFile; aliasFile = fopen(CURRENT_SECTION.aliasFileName, "r"); return aliasFile; @@ -40,26 +38,25 @@ FILE *getCurrentSectionAliasFile() { struct Favorite findFavorite(char *name) { struct Favorite favorite; - for (int i=0; i1) { strcpy(alias, getRomRealName(romName)); } else { strcpy(alias, romName); } - if(strcmp(alias, romName)==0) { - if(currentSectionNumber==favoritesSectionNumber) { + if (strcmp(alias, romName) == 0) { + if (currentSectionNumber == favoritesSectionNumber) { struct Favorite favorite = findFavorite(alias); - if (strlen(favorite.alias)<2) { + if (strlen(favorite.alias) < 2) { } } else { char *ext = getExtension(romName); - if (strcmp(ext,".opk")==0) { - strcpy (alias, romName); + if (strcmp(ext, ".opk") == 0) { + strcpy(alias, romName); } } } return alias; } -char *getFileNameOrAlias(struct Rom *rom) { +char* getFileNameOrAlias(struct Rom *rom) { char *displayName = malloc(300); - if(rom->alias!=NULL&&strlen(rom->alias)>2) { + if (rom->alias != NULL && strlen(rom->alias) > 2) { //this is a favorite with no alias - if(strcmp(rom->alias, " ")!=0) { + if (strcmp(rom->alias, " ") != 0) { strcpy(displayName, rom->alias); } else { strcpy(displayName, rom->name); @@ -177,7 +174,7 @@ char *getFileNameOrAlias(struct Rom *rom) { } else { strcpy(displayName, rom->name); } - if(strcmp(displayName, rom->name)==0) { + if (strcmp(displayName, rom->name) == 0) { char *dup = strdup(displayName); free(displayName); stripGameName(dup); @@ -188,14 +185,14 @@ char *getFileNameOrAlias(struct Rom *rom) { } void generateError(char *pErrorMessage, int pThereIsACriticalError) { - itsStoppedBecauseOfAnError=1; - errorMessage=pErrorMessage; - thereIsACriticalError=pThereIsACriticalError; + itsStoppedBecauseOfAnError = 1; + errorMessage = pErrorMessage; + thereIsACriticalError = pThereIsACriticalError; } void quit() { // if(getLaunchAtBoot()==NULL) { - saveLastState(); + saveLastState(); // } saveFavorites(); clearTimer(); @@ -206,15 +203,15 @@ void quit() { #ifdef TARGET_PC exit(0); #endif - if (selectedShutDownOption==1) { + if (selectedShutDownOption == 1) { execlp("sh", "sh", "-c", "sync && reboot", NULL); } else { execlp("sh", "sh", "-c", "sync && poweroff", NULL); } } else { - if (selectedShutDownOption==1) { + if (selectedShutDownOption == 1) { execlp("sh", "sh", "-c", "sync && reboot", NULL); - } else if (selectedShutDownOption==2) { + } else if (selectedShutDownOption == 2) { execlp("sh", "sh", "-c", "sync && poweroff", NULL); } else { exit(0); @@ -223,8 +220,8 @@ void quit() { } int doesFavoriteExist(char *name) { - for(int i=0;idata); - if (currentSectionNumber == favoritesSectionNumber) { - setCPU(favorites[CURRENT_GAME_NUMBER].frequency); - } else { - setCPU(CURRENT_SECTION.currentGameNode->data->preferences.frequency); - } + if (currentSectionNumber == favoritesSectionNumber) { + setCPU(favorites[CURRENT_GAME_NUMBER].frequency); + } else { + setCPU(CURRENT_SECTION.currentGameNode->data->preferences.frequency); + } #endif #ifdef TARGET_RFW - // ipu modes (/proc/jz/ipu): - // 0: stretch - // 1: aspect - // 2: original (fallback to aspect when downscale is needed) - // 3: 4:3 - fp = fopen("/proc/jz/ipu","w"); - fprintf(fp,CURRENT_SECTION.scaling); - fclose(fp); + // ipu modes (/proc/jz/ipu): + // 0: stretch + // 1: aspect + // 2: original (fallback to aspect when downscale is needed) + // 3: 4:3 + fp = fopen("/proc/jz/ipu","w"); + fprintf(fp,CURRENT_SECTION.scaling); + fclose(fp); #endif #ifdef TARGET_OD_BETA - if (strcmp(CURRENT_SECTION.scaling,"0")==0) { //0: stretch - SDL_putenv("SDL_VIDEO_KMSDRM_SCALING_MODE=0"); - } else if (strcmp(CURRENT_SECTION.scaling,"1")==0) { //1: aspect - SDL_putenv("SDL_VIDEO_KMSDRM_SCALING_MODE=1"); - } else if (strcmp(CURRENT_SECTION.scaling,"2")==0) { - SDL_putenv("SDL_VIDEO_KMSDRM_SCALING_MODE=2"); //2: integer scaling - } else if (strcmp(CURRENT_SECTION.scaling,"3")==0) { - SDL_putenv("SDL_VIDEO_KMSDRM_SCALING_MODE"); //3: not set - } + if (strcmp(CURRENT_SECTION.scaling,"0")==0) { //0: stretch + SDL_putenv("SDL_VIDEO_KMSDRM_SCALING_MODE=0"); + } else if (strcmp(CURRENT_SECTION.scaling,"1")==0) { //1: aspect + SDL_putenv("SDL_VIDEO_KMSDRM_SCALING_MODE=1"); + } else if (strcmp(CURRENT_SECTION.scaling,"2")==0) { + SDL_putenv("SDL_VIDEO_KMSDRM_SCALING_MODE=2"); //2: integer scaling + } else if (strcmp(CURRENT_SECTION.scaling,"3")==0) { + SDL_putenv("SDL_VIDEO_KMSDRM_SCALING_MODE"); //3: not set + } #endif - fp = fopen("/sys/class/graphics/fb0/device/allow_downscaling","w"); - if (fp!=NULL) { - fprintf(fp, "%d" , 1); - fclose(fp); - } + fp = fopen("/sys/class/graphics/fb0/device/allow_downscaling", "w"); + if (fp != NULL) { + fprintf(fp, "%d", 1); + fclose(fp); + } #ifndef TARGET_PC - logMessage("INFO","executeCommand",emulatorFolder); - logMessage("INFO","executeCommand",exec); - logMessage("INFO","executeCommand",fileToBeExecutedWithFullPath); - SDL_SetVideoMode(320, 240, 32, SDL_HWSURFACE | SDL_DOUBLEBUF); - SDL_ShowCursor(1); - freeResources(); - SDL_ShowCursor(1); - #ifndef TARGET_OD_BETA - resetFrameBuffer1(); - #endif - //I NEED THIS PRINTF SO IT LAUNCHES ON RFW, WHY!!!! + logMessage("INFO", "executeCommand", emulatorFolder); + logMessage("INFO", "executeCommand", exec); + logMessage("INFO", "executeCommand", fileToBeExecutedWithFullPath); + SDL_SetVideoMode(320, 240, 32, SDL_HWSURFACE | SDL_DOUBLEBUF); + SDL_ShowCursor(1); + freeResources(); + SDL_ShowCursor(1); +#ifndef TARGET_OD_BETA + resetFrameBuffer1(); +#endif + //I NEED THIS PRINTF SO IT LAUNCHES ON RFW, WHY!!!! // #if defined TARGET_RFW - printf ("\n"); + printf("\n"); // #endif - if (consoleApp) { - #if defined(TARGET_NPG) || defined(TARGET_OD) || defined TARGET_OD_BETA + if (consoleApp) { +#if defined(TARGET_NPG) || defined(TARGET_OD) || defined TARGET_OD_BETA /* Enable the framebuffer console */ char c = '1'; int fd = open("/sys/devices/virtual/vtconsole/vtcon1/bind", O_WRONLY); @@ -373,20 +371,22 @@ void executeCommandPC (char *executable, char *fileToBeExecutedWithFullPath) { close(fd); } #endif - } - char states[] = {}; + } + char states[] = { }; // char menuDirectory[100] = ""; // char *directory="/home/retrofw/apps"; // getcwd(menuDirectory, sizeof(menuDirectory)); // int ret = chdir(directory); - //I NEED THIS PRINTF SO IT LAUNCHES ON RFW, WHY!!!! + //I NEED THIS PRINTF SO IT LAUNCHES ON RFW, WHY!!!! // #if defined TARGET_RFW - printf (" "); + printf(" "); // #endif // execlp("opkrun","invoker","-m","default.retrofw.desktop", exec,fileToBeExecutedWithFullPath,NULL); - execlp("./invoker.dge","invoker.dge", emulatorFolder, exec, fileToBeExecutedWithFullPath, states, pSectionNumber, pReturnTo, pPictureMode, NULL); + execlp("./invoker.dge", "invoker.dge", emulatorFolder, exec, + fileToBeExecutedWithFullPath, states, pSectionNumber, pReturnTo, + pPictureMode, NULL); #else strcat(exec, " \""); strcat(exec, fileToBeExecutedWithFullPath); @@ -404,47 +404,47 @@ void executeCommandPC (char *executable, char *fileToBeExecutedWithFullPath) { initializeFonts(); initializeSettingsFonts(); #endif - } +} - int isExtensionValid(char *extension, char *fileExtensions) { - char fileExtensionsCopy[200]; - strcpy(fileExtensionsCopy, fileExtensions); - char *ptr = strtok(fileExtensionsCopy, ","); - while(ptr != NULL) { - int areStringsDifferent = strcasecmp(extension,ptr); - if (!areStringsDifferent) { - return(1); - } - ptr = strtok(NULL, ","); +int isExtensionValid(char *extension, char *fileExtensions) { + char fileExtensionsCopy[200]; + strcpy(fileExtensionsCopy, fileExtensions); + char *ptr = strtok(fileExtensionsCopy, ","); + while (ptr != NULL) { + int areStringsDifferent = strcasecmp(extension, ptr); + if (!areStringsDifferent) { + return (1); } - return(0); + ptr = strtok(NULL, ","); } + return (0); +} - int countGamesInSection() { - int gamesCounter=0; - struct Node* currentGameNode = CURRENT_SECTION.head; - for (int i=0;i<=MAX_GAMES_IN_SECTION;i++) { - if (currentGameNode!=NULL) { - gamesCounter++; - currentGameNode = currentGameNode->next; - } else { - break; - } +int countGamesInSection() { + int gamesCounter = 0; + struct Node *currentGameNode = CURRENT_SECTION.head; + for (int i = 0; i <= MAX_GAMES_IN_SECTION; i++) { + if (currentGameNode != NULL) { + gamesCounter++; + currentGameNode = currentGameNode->next; + } else { + break; } - return gamesCounter; } + return gamesCounter; +} - struct Rom *findGame(char *game) { - for (int i=0;idata->name, game)==0) { - return (CURRENT_SECTION.currentGameNode->data); - } - if (CURRENT_SECTION.currentGameNode->next!=NULL) { - CURRENT_SECTION.currentGameNode = CURRENT_SECTION.currentGameNode->next; - } +struct Rom* findGame(char *game) { + for (int i = 0; i < CURRENT_SECTION.gameCount;i++) { + if (strcmp(CURRENT_SECTION.currentGameNode->data->name, game)==0) { + return (CURRENT_SECTION.currentGameNode->data); + } + if (CURRENT_SECTION.currentGameNode->next!=NULL) { + CURRENT_SECTION.currentGameNode = CURRENT_SECTION.currentGameNode->next; } - return NULL; } + return NULL; +} // struct Node *split(struct Node *head) // { // struct Node *fast = head,*slow = head; @@ -524,74 +524,75 @@ void executeCommandPC (char *executable, char *fileToBeExecutedWithFullPath) { // return merge(head,second); // } - int compareIgnoreCase(char * str1, char* str2) { - char* temp1 = strdup(str1); - char* temp2 = strdup(str2); - for(int i=0;temp1[i]; i++) { - temp1[i] = tolower(temp1[i]); - } - for(int i=0;temp2[i]; i++) { - temp2[i] = tolower(temp2[i]); - } - free(temp1); - free(temp2); - return strcmp(temp1, temp2); +int compareIgnoreCase(char *str1, char *str2) { + char *temp1 = strdup(str1); + char *temp2 = strdup(str2); + for (int i = 0; temp1[i]; i++) { + temp1[i] = tolower(temp1[i]); + } + for (int i = 0; temp2[i]; i++) { + temp2[i] = tolower(temp2[i]); } + free(temp1); + free(temp2); + return strcmp(temp1, temp2); +} - struct Node* SortedMerge(struct Node* a, struct Node* b) { - struct Node* result = NULL; +struct Node* SortedMerge(struct Node *a, struct Node *b) { + struct Node *result = NULL; - /* Base cases */ - if (a == NULL) - return (b); - else if (b == NULL) - return (a); + /* Base cases */ + if (a == NULL) + return (b); + else if (b == NULL) + return (a); - char *s1Alias = malloc(1000); - if(a->data->alias!=NULL&&strlen(a->data->alias)>2) { - strcpy(s1Alias,a->data->alias); - } else { - strcpy(s1Alias,a->data->name); - } - char *s2Alias=malloc(1000); - if(b->data->alias!=NULL&&strlen(b->data->alias)>2) { - strcpy(s2Alias,b->data->alias); - } else { - strcpy(s2Alias,b->data->name); - } + char *s1Alias = malloc(1000); + if (a->data->alias != NULL && strlen(a->data->alias) > 2) { + strcpy(s1Alias, a->data->alias); + } else { + strcpy(s1Alias, a->data->name); + } + char *s2Alias = malloc(1000); + if (b->data->alias != NULL && strlen(b->data->alias) > 2) { + strcpy(s2Alias, b->data->alias); + } else { + strcpy(s2Alias, b->data->name); + } - char * noPathS1Alias=strdup(s1Alias); - char * noPathS2Alias=strdup(s2Alias); - free(s1Alias); - free(s2Alias); + char *noPathS1Alias = strdup(s1Alias); + char *noPathS2Alias = strdup(s2Alias); + free(s1Alias); + free(s2Alias); - for(char *p = noPathS1Alias;*p;++p) *p=*p>0x40&&*p<0x5b?*p|0x60:*p; - for(char *p = noPathS2Alias;*p;++p) *p=*p>0x40&&*p<0x5b?*p|0x60:*p; + for (char *p = noPathS1Alias; *p; ++p) + *p = *p > 0x40 && *p < 0x5b ? *p | 0x60 : *p; + for (char *p = noPathS2Alias; *p; ++p) + *p = *p > 0x40 && *p < 0x5b ? *p | 0x60 : *p; - if(strlen(CURRENT_SECTION.aliasFileName)<2) { - stripGameName(noPathS1Alias); - stripGameName(noPathS2Alias); - } - if (strcasecmp(noPathS1Alias, noPathS2Alias)<=0) - { - free(noPathS1Alias); - free(noPathS2Alias); - a->next = SortedMerge(a->next,b); - a->next->prev = a; - a->prev = NULL; - result = a; + if (strlen(CURRENT_SECTION.aliasFileName)<2) { + stripGameName(noPathS1Alias); + stripGameName(noPathS2Alias); + } + if (strcasecmp(noPathS1Alias, noPathS2Alias) <= 0) { + free(noPathS1Alias); + free(noPathS2Alias); + a->next = SortedMerge(a->next, b); + a->next->prev = a; + a->prev = NULL; + result = a; // result->next = SortedMerge(a->next, b); - }else { - free(noPathS1Alias); - free(noPathS2Alias); - b->next = SortedMerge(a,b->next); - b->next->prev = b; - b->prev = NULL; - result = b; + } else { + free(noPathS1Alias); + free(noPathS2Alias); + b->next = SortedMerge(a, b->next); + b->next->prev = b; + b->prev = NULL; + result = b; // result->next = SortedMerge(a, b->next); - } + } - /* Pick either a or b, and recur */ + /* Pick either a or b, and recur */ // if (a->data <= b->data) { // result = a; // result->next = SortedMerge(a->next, b); @@ -599,764 +600,774 @@ void executeCommandPC (char *executable, char *fileToBeExecutedWithFullPath) { // result = b; // result->next = SortedMerge(a, b->next); // } - return (result); - + return (result); +} +void FrontBackSplit(struct Node *source, struct Node **frontRef, + struct Node **backRef) { + struct Node *fast; + struct Node *slow; + slow = source; + fast = source->next; + + /* Advance 'fast' two nodes, and advance 'slow' one node */ + while (fast != NULL) { + fast = fast->next; + if (fast != NULL) { + slow = slow->next; + fast = fast->next; + } } - void FrontBackSplit(struct Node* source, - struct Node** frontRef, struct Node** backRef) - { - struct Node* fast; - struct Node* slow; - slow = source; - fast = source->next; - - /* Advance 'fast' two nodes, and advance 'slow' one node */ - while (fast != NULL) { - fast = fast->next; - if (fast != NULL) { - slow = slow->next; - fast = fast->next; - } - } - - /* 'slow' is before the midpoint in the list, so split it in two - at that point. */ - *frontRef = source; - *backRef = slow->next; - slow->next = NULL; - } + /* 'slow' is before the midpoint in the list, so split it in two + at that point. */ + *frontRef = source; + *backRef = slow->next; + slow->next = NULL; +} - /* sorts the linked list by changing next pointers (not data) */ - void mergeSort(struct Node** headRef) - { - struct Node* head = *headRef; - struct Node* a; - struct Node* b; +/* sorts the linked list by changing next pointers (not data) */ +void mergeSort(struct Node **headRef) { + struct Node *head = *headRef; + struct Node *a; + struct Node *b; - /* Base case -- length 0 or 1 */ - if ((head == NULL) || (head->next == NULL)) { - return; - } + /* Base case -- length 0 or 1 */ + if ((head == NULL) || (head->next == NULL)) { + return; + } - /* Split head into 'a' and 'b' sublists */ - FrontBackSplit(head, &a, &b); + /* Split head into 'a' and 'b' sublists */ + FrontBackSplit(head, &a, &b); - /* Recursively sort the sublists */ - mergeSort(&a); - mergeSort(&b); + /* Recursively sort the sublists */ + mergeSort(&a); + mergeSort(&b); - /* answer = merge the two sorted lists together */ - *headRef = SortedMerge(a, b); - } + /* answer = merge the two sorted lists together */ + *headRef = SortedMerge(a, b); +} - void loadFavoritesSectionGameList() { - int gameInPage = 0; - int page = 0; - logMessage("ERROR","loadFavoritesSectionGameList", "Setting total pages"); - FAVORITES_SECTION.totalPages=0; - FAVORITES_SECTION.gameCount=0; - cleanListForSection(&FAVORITES_SECTION); - for (int i=0;iname=malloc(size); - rom->alias=malloc(aliasSize); - rom->directory=malloc(1); - strcpy(rom->alias, favorites[i].alias); - strcpy(rom->name, favorites[i].name); - rom->isConsoleApp = favorites[i].isConsoleApp; - loadRomPreferences(rom); - InsertAtTailInSection(&FAVORITES_SECTION, rom); - gameInPage++; - FAVORITES_SECTION.gameCount++; - } - FAVORITES_SECTION.tail=GetNthNode(FAVORITES_SECTION.gameCount-1); - if (favoritesSize>0) { - scrollToGame(FAVORITES_SECTION.realCurrentGameNumber); } - } - int scanDirectory(char *directory, char* files[], int i) - { - struct dirent **result; - int j=0; - int n = scandir(directory, &result, NULL, alphasort); - if (n < 0) { - logMessage("ERROR","scanDirectory", directory); - return 0; - } - while (jd_name); - if (strcmp(d_name, mediaFolder) != 0 && strcmp (d_name, "..") != 0 && strcmp (d_name, ".") != 0) { - if (result[j]->d_type & DT_DIR) { - char path[PATH_MAX]; - char *e = strrchr(d_name, '/'); - if (e==NULL) { - strcat(d_name, "/"); - } - free(e); - snprintf (path, PATH_MAX, "%s%s", directory, d_name); - CURRENT_SECTION.hasDirs = 1; - i=scanDirectory(path, files, i); - } else { - char path[PATH_MAX]; - snprintf (path, PATH_MAX, "%s%s", directory, d_name); - files[i]=malloc(sizeof(path)+1); - strcpy(files[i],path); - i++; - } - } - free (d_name); - free(result[j]); - j++; - } - free(result); - return i; + int size = strlen(favorites[i].name)+1; + int aliasSize = strlen(favorites[i].alias)+1; + + struct Rom *rom = malloc(sizeof(struct Rom)); + rom->name=malloc(size); + rom->alias=malloc(aliasSize); + rom->directory=malloc(1); + strcpy(rom->alias, favorites[i].alias); + strcpy(rom->name, favorites[i].name); + rom->isConsoleApp = favorites[i].isConsoleApp; + loadRomPreferences(rom); + InsertAtTailInSection(&FAVORITES_SECTION, rom); + gameInPage++; + FAVORITES_SECTION.gameCount++; + } + FAVORITES_SECTION.tail=GetNthNode(FAVORITES_SECTION.gameCount-1); + if (favoritesSize > 0) { + scrollToGame(FAVORITES_SECTION.realCurrentGameNumber); } +} - int recursivelyScanDirectory (char *directory, char* files[], int i) { - DIR * d; - d = opendir (directory); - if (d==NULL) { - return 0; - } - while (1) { - struct dirent *entry; - char * d_name; - entry = readdir (d); - if (!entry) { - break; - } - d_name = entry->d_name; - if (entry->d_type & DT_DIR) { - if (strcmp(d_name, mediaFolder) != 0 && strcmp (d_name, "..") != 0 && strcmp (d_name, ".") != 0) { - char path[PATH_MAX]; - char * e = strrchr(d_name, '/'); - if (e==NULL) { - strcat(d_name, "/"); - } - snprintf (path, PATH_MAX, "%s%s", directory, d_name); - i=recursivelyScanDirectory(path, files, i); +int scanDirectory(char *directory, char *files[], int i) { + struct dirent **result; + int j = 0; + int n = scandir(directory, &result, NULL, alphasort); + if (n < 0) { + logMessage("ERROR", "scanDirectory", directory); + return 0; + } + while (j < n) { + char *d_name = malloc(3000); + strcpy(d_name, result[j]->d_name); + if (strcmp(d_name, mediaFolder) != 0 && strcmp(d_name, "..") != 0 + && strcmp(d_name, ".") != 0) { + if (result[j]->d_type & DT_DIR) { + char path[PATH_MAX]; + char *e = strrchr(d_name, '/'); + if (e == NULL) { + strcat(d_name, "/"); } + free(e); + snprintf(path, PATH_MAX, "%s%s", directory, d_name); + CURRENT_SECTION.hasDirs = 1; + i = scanDirectory(path, files, i); } else { char path[PATH_MAX]; - snprintf (path, PATH_MAX, "%s%s", directory, d_name); - files[i]=strdup(path); + snprintf(path, PATH_MAX, "%s%s", directory, d_name); + files[i] = malloc(sizeof(path) + 1); + strcpy(files[i], path); i++; } } - free(d); - return i; + free(d_name); + free(result[j]); + j++; } + free(result); + return i; +} - int findDirectoriesInDirectory (char *directory, char* files[], int i) { - DIR * d; - d = opendir (directory); - if (d==NULL) { - return 0; +int recursivelyScanDirectory(char *directory, char *files[], int i) { + logMessage("INFO", "recursivelyScanDirectory","Entering"); + DIR *d; + logMessage("INFO", "recursivelyScanDirectory",directory); + d = opendir(directory); + if (d == NULL) { + logMessage("INFO", "recursivelyScanDirectory","0 files"); + return 0; + } + while (1) { + struct dirent *entry; + char *d_name; + logMessage("INFO", "recursivelyScanDirectory","Read dir"); + entry = readdir(d); + if (!entry) { + logMessage("INFO", "recursivelyScanDirectory","No entry"); + break; } - while (1) { - struct dirent *entry; - char * d_name; - entry = readdir (d); - if (!entry) { - break; - } - d_name = entry->d_name; - if (entry->d_type & DT_DIR) { - if (strcmp (d_name, "..") != 0 && strcmp (d_name, ".") != 0) { - char path[PATH_MAX]; - snprintf (path, PATH_MAX, "%s%s", directory, d_name); - files[i]=malloc(sizeof(path)); - strcpy(files[i],path); - i++; + d_name = entry->d_name; + if (entry->d_type & DT_DIR) { + logMessage("INFO", "recursivelyScanDirectory","It's a dir"); + if (strcmp(d_name, mediaFolder) != 0 && strcmp(d_name, "..") != 0 + && strcmp(d_name, ".") != 0) { + char path[PATH_MAX]; + char *e = strrchr(d_name, '/'); + if (e == NULL) { + strcat(d_name, "/"); } + free(e); + snprintf(path, PATH_MAX, "%s%s", directory, d_name); + logMessage("INFO", "recursivelyScanDirectory","Recursing to path"); + logMessage("INFO", "recursivelyScanDirectory",path); + i = recursivelyScanDirectory(path, files, i); } + } else { + logMessage("INFO", "recursivelyScanDirectory","It's a file"); + char path[PATH_MAX]; + snprintf(path, PATH_MAX, "%s%s", directory, d_name); + files[i] = strdup(path); + logMessage("INFO", "recursivelyScanDirectory",files[i]); + i++; } - free(d); - return i; } + free(d); + return i; +} - - int compareFavorites(const void *f1, const void *f2) - { - struct Favorite *e1 = (struct Favorite *)f1; - struct Favorite *e2 = (struct Favorite *)f2; - char temp1[300]=""; - char temp2[300]=""; - if (strlen(e1->alias)>1) { - strcpy(temp1,e1->alias); - } else { - strcpy(temp1,e1->name); - stripGameName(temp1); - } - if (strlen(e2->alias)>1) { - strcpy(temp2,e2->alias); - } else { - strcpy(temp2,e2->name); - stripGameName(temp2); - } - for(int i=0;temp1[i]; i++) { - temp1[i] = tolower(temp1[i]); +int findDirectoriesInDirectory(char *directory, char *files[], int i) { + DIR *d; + d = opendir(directory); + if (d == NULL) { + return 0; + } + while (1) { + struct dirent *entry; + char *d_name; + entry = readdir(d); + if (!entry) { + break; } - for(int i=0;temp2[i]; i++) { - temp2[i] = tolower(temp2[i]); + d_name = entry->d_name; + if (entry->d_type & DT_DIR) { + if (strcmp(d_name, "..") != 0 && strcmp(d_name, ".") != 0) { + char path[PATH_MAX]; + snprintf(path, PATH_MAX, "%s%s", directory, d_name); + files[i] = malloc(sizeof(path)); + strcpy(files[i], path); + i++; + } } - return strcmp(temp1, temp2); } + free(d); + return i; +} + +int compareFavorites(const void *f1, const void *f2) { + struct Favorite *e1 = (struct Favorite*) f1; + struct Favorite *e2 = (struct Favorite*) f2; + char temp1[300] = ""; + char temp2[300] = ""; + if (strlen(e1->alias) > 1) { + strcpy(temp1, e1->alias); + } else { + strcpy(temp1, e1->name); + stripGameName(temp1); + } + if (strlen(e2->alias) > 1) { + strcpy(temp2, e2->alias); + } else { + strcpy(temp2, e2->name); + stripGameName(temp2); + } + for (int i = 0; temp1[i]; i++) { + temp1[i] = tolower(temp1[i]); + } + for (int i = 0; temp2[i]; i++) { + temp2[i] = tolower(temp2[i]); + } + return strcmp(temp1, temp2); +} - void fillUpStolenGMenuFile(struct StolenGMenuFile* stolenFile, char* fileName) { - FILE * fp; - char * line = NULL; - size_t len = 0; - ssize_t read; - fp = fopen(fileName, "r"); - int paramsFlag = 0; - while ((read = getline(&line, &len, fp)) != -1) { - char *ptr; - if(strstr(line,"title")!=NULL) { - ptr = strtok(line, "="); - ptr = strtok(NULL, "="); - strcpy(stolenFile->title,ptr); - logMessage("INFO", "fillUpStolenGMenuFile","STOLEN FILE title"); - if (stolenFile->title[strlen(stolenFile->title)-1]=='\n') { - logMessage("INFO","fillUpStolenGMenuFile", "HAD ENTER"); - stolenFile->title[strlen(stolenFile->title)-1]='\0'; - } - logMessage("INFO","fillUpStolenGMenuFile", stolenFile->title); - } else if(strstr(line,"exec")!=NULL) { - ptr = strtok(line, "="); - ptr = strtok(NULL, "="); - strcpy(stolenFile->exec,ptr); - logMessage("INFO","fillUpStolenGMenuFile", "STOLEN FILE exec"); - if (stolenFile->exec[strlen(stolenFile->exec)-1]=='\n') { - logMessage("INFO", "fillUpStolenGMenuFile","HAD ENTER"); - stolenFile->exec[strlen(stolenFile->exec)-1]='\0'; - } else { - stolenFile->exec[strlen(stolenFile->exec)]='\0'; - } - logMessage("INFO","fillUpStolenGMenuFile", stolenFile->exec); - } else if(strstr(line,"params")!=NULL) { - ptr = strtok(line, "="); - ptr = strtok(NULL, "="); - strcpy(stolenFile->params,ptr); - logMessage("INFO","fillUpStolenGMenuFile", "STOLEN FILE params"); - if (stolenFile->params[strlen(stolenFile->params)-1]=='\n') { - logMessage("INFO","fillUpStolenGMenuFile", "HAD ENTER"); - stolenFile->params[strlen(stolenFile->params)-1]='\0'; - } - logMessage("INFO","fillUpStolenGMenuFile", stolenFile->params); - paramsFlag=1; - } else if(strstr(line,"consoleapp")!=NULL) { - ptr = strtok(line, "="); - ptr = strtok(NULL, "="); - logMessage("INFO","fillUpStolenGMenuFile", "STOLEN FILE isConsoleApp"); - stolenFile->isConsoleApp=strcmp("false",ptr); - logMessage("INFO","fillUpStolenGMenuFile", (stolenFile->isConsoleApp==0?"false":"true")); - break; +void fillUpStolenGMenuFile(struct StolenGMenuFile *stolenFile, char *fileName) { + FILE *fp; + char *line = NULL; + size_t len = 0; + ssize_t read; + fp = fopen(fileName, "r"); + int paramsFlag = 0; + while ((read = getline(&line, &len, fp)) != -1) { + char *ptr; + if (strstr(line, "title") != NULL) { + ptr = strtok(line, "="); + ptr = strtok(NULL, "="); + strcpy(stolenFile->title, ptr); + logMessage("INFO", "fillUpStolenGMenuFile", "STOLEN FILE title"); + if (stolenFile->title[strlen(stolenFile->title) - 1] == '\n') { + logMessage("INFO", "fillUpStolenGMenuFile", "HAD ENTER"); + stolenFile->title[strlen(stolenFile->title) - 1] = '\0'; } + logMessage("INFO", "fillUpStolenGMenuFile", stolenFile->title); + } else if (strstr(line, "exec") != NULL) { + ptr = strtok(line, "="); + ptr = strtok(NULL, "="); + strcpy(stolenFile->exec, ptr); + logMessage("INFO", "fillUpStolenGMenuFile", "STOLEN FILE exec"); + if (stolenFile->exec[strlen(stolenFile->exec) - 1] == '\n') { + logMessage("INFO", "fillUpStolenGMenuFile", "HAD ENTER"); + stolenFile->exec[strlen(stolenFile->exec) - 1] = '\0'; + } else { + stolenFile->exec[strlen(stolenFile->exec)] = '\0'; + } + logMessage("INFO", "fillUpStolenGMenuFile", stolenFile->exec); + } else if (strstr(line, "params") != NULL) { + ptr = strtok(line, "="); + ptr = strtok(NULL, "="); + strcpy(stolenFile->params, ptr); + logMessage("INFO", "fillUpStolenGMenuFile", "STOLEN FILE params"); + if (stolenFile->params[strlen(stolenFile->params) - 1] == '\n') { + logMessage("INFO", "fillUpStolenGMenuFile", "HAD ENTER"); + stolenFile->params[strlen(stolenFile->params) - 1] = '\0'; + } + logMessage("INFO", "fillUpStolenGMenuFile", stolenFile->params); + paramsFlag = 1; + } else if (strstr(line, "consoleapp") != NULL) { + ptr = strtok(line, "="); + ptr = strtok(NULL, "="); + logMessage("INFO", "fillUpStolenGMenuFile", + "STOLEN FILE isConsoleApp"); + stolenFile->isConsoleApp = strcmp("false", ptr); + logMessage("INFO", "fillUpStolenGMenuFile", + (stolenFile->isConsoleApp == 0 ? "false" : "true")); + break; } - if (paramsFlag==0) { - strcpy(stolenFile->params,"\0" ); - } - fclose(fp); - if(line) { - free(line); - } } + if (paramsFlag == 0) { + strcpy(stolenFile->params, "\0"); + } + fclose(fp); + if (line) { + free(line); + } +} - int theSectionHasGames(struct MenuSection *section) { - section->hidden=1; - int dirCounter=0; - char *dirs[10]; - char* ptr; - char dirsCopy[1000]; - char *filesDirectoriesCopy = strdup(section->filesDirectories); - char message[300]; - snprintf(message, 300, "Directories %s ", filesDirectoriesCopy); - logMessage("INFO","theSectionHasGames",message); - ptr = strtok(filesDirectoriesCopy, ","); +int theSectionHasGames(struct MenuSection *section) { + section->hidden = 1; + int dirCounter = 0; + char *dirs[10]; + char *ptr; + char dirsCopy[1000]; + char *filesDirectoriesCopy = strdup(section->filesDirectories); + char message[300]; + snprintf(message, 300, "Directories %s ", filesDirectoriesCopy); + logMessage("INFO", "theSectionHasGames", message); + ptr = strtok(filesDirectoriesCopy, ","); + snprintf(message, 300, "Looking at %s", ptr); + logMessage("INFO", "theSectionHasGames", message); + char *files[MAX_GAMES_IN_SECTION]; + int value = 0; + while (ptr != NULL) { + dirs[dirCounter] = strdup(ptr); + ptr = strtok(NULL, ","); snprintf(message, 300, "Looking at %s", ptr); - logMessage("INFO","theSectionHasGames", message); - char *files[MAX_GAMES_IN_SECTION]; - int value = 0; - while (ptr!=NULL) { - dirs[dirCounter]=strdup(ptr); - ptr = strtok(NULL, ","); - snprintf(message, 300, "Looking at %s", ptr); - logMessage("INFO","theSectionHasGames", message); - dirCounter++; - } - snprintf(message, 300, "DirCounter is %d", dirCounter); - logMessage("INFO","theSectionHasGames", message); - free(filesDirectoriesCopy); - for(int k=0;kfileExtensions)) { - if(strcmp(ext,".opk")==0) { - struct OPKDesktopFile desktopFiles[10]; - int desktopFilesCount=getOPK(files[i], desktopFiles); - int desktopCounter=0; - while(desktopCountercategory)==NULL&&strcmp(section->category,"all")!=0) { - snprintf(message, 300, "%s discarded", desktopFiles[desktopCounter].displayName); - logMessage("INFO","theSectionHasGames", message); - break; - } else { - snprintf(message, 300, "%s considered", desktopFiles[desktopCounter].displayName); - logMessage("INFO","theSectionHasGames", message); - value++; - } - desktopCounter++; + logMessage("INFO", "theSectionHasGames", message); + dirCounter++; + } + snprintf(message, 300, "DirCounter is %d", dirCounter); + logMessage("INFO", "theSectionHasGames", message); + free(filesDirectoriesCopy); + for (int k = 0; k < dirCounter; k++) { + snprintf(message, 300, "k is %d", k); + logMessage("INFO", "theSectionHasGames", message); + int n = recursivelyScanDirectory(dirs[k], files, 0); + snprintf(message, 300, "Directory %s has %d files", dirs[k], n); + logMessage("INFO", "theSectionHasGames", message); + for (int i = 0; i < n; i++) { + char *ext = getExtension(files[i]); + if (ext && strcmp((files[i]), "..") != 0 + && strcmp((files[i]), ".") != 0 + && isExtensionValid(ext, section->fileExtensions)) { + if (strcmp(ext, ".opk") == 0) { + struct OPKDesktopFile desktopFiles[10]; + int desktopFilesCount = getOPK(files[i], desktopFiles); + int desktopCounter = 0; + while (desktopCounter < desktopFilesCount) { + if (strstr(desktopFiles[desktopCounter].category, + section->category) == NULL + && strcmp(section->category, "all") != 0) { + snprintf(message, 300, "%s discarded", + desktopFiles[desktopCounter].displayName); + logMessage("INFO", "theSectionHasGames", message); + break; + } else { + snprintf(message, 300, "%s considered", + desktopFiles[desktopCounter].displayName); + logMessage("INFO", "theSectionHasGames", message); + value++; } - } else { - value++; + desktopCounter++; } + } else { + value++; } } - for (int i=0;i0) { - section->hidden=0; - } } - for (int i=0;i 0) { + section->hidden = 0; } - section->counted=1; - return value; } + for (int i = 0; i < dirCounter; i++) { + free(dirs[i]); + } + section->counted = 1; + return value; +} - char * readline(FILE *fp, char *buffer) - { - int ch; - int i = 0; - size_t buff_len = 0; - - buffer = malloc(buff_len + 1); - if (!buffer) return NULL; // Out of memory - - while ((ch = fgetc(fp)) != '\n' && ch != EOF) - { - buff_len++; - void *tmp = realloc(buffer, buff_len + 1); - if (tmp == NULL) - { - free(buffer); - return NULL; // Out of memory - } - buffer = tmp; +char* readline(FILE *fp, char *buffer) { + int ch; + int i = 0; + size_t buff_len = 0; - buffer[i] = (char) ch; - i++; - } - buffer[i] = '\0'; + buffer = malloc(buff_len + 1); + if (!buffer) + return NULL; // Out of memory - // Detect end - if (ch == EOF && (i == 0 || ferror(fp))) - { + while ((ch = fgetc(fp)) != '\n' && ch != EOF) { + buff_len++; + void *tmp = realloc(buffer, buff_len + 1); + if (tmp == NULL) { free(buffer); - return NULL; + return NULL; // Out of memory } - return buffer; + buffer = tmp; + + buffer[i] = (char) ch; + i++; + } + buffer[i] = '\0'; + + // Detect end + if (ch == EOF && (i == 0 || ferror(fp))) { + free(buffer); + return NULL; } + return buffer; +} - void loadGameList(int refresh) { +void loadGameList(int refresh) { // Uint32 startTime = SDL_GetTicks(); - logMessage("INFO","loadGameList",CURRENT_SECTION.sectionName); - int loadedFiles=0; - logMessage("INFO","loadGameList","Should we skip this?"); - FILE *fp=NULL; - if (CURRENT_SECTION.initialized==0||refresh) { - if(getLaunchAtBoot()==NULL) { - drawLoadingText(); - refreshScreen(); - } - logMessage("INFO","loadGameList","No, loading game list"); - CURRENT_SECTION.initialized=1; - //We don't need to reload the alias file if just refreshing - if (!refresh) { - loadAliasList(currentSectionNumber); - } - CURRENT_SECTION.totalPages=0; - CURRENT_SECTION.gameCount=0; - char *files[MAX_GAMES_IN_SECTION]; - int game = -1; - int dirCounter=0; - char *dirs[10]; - char *ptr=NULL; + logMessage("INFO", "loadGameList", CURRENT_SECTION.sectionName); + int loadedFiles=0; + logMessage("INFO","loadGameList","Should we skip this?"); + FILE *fp=NULL; + if (CURRENT_SECTION.initialized==0||refresh) { + if(getLaunchAtBoot()==NULL) { + drawLoadingText(); + refreshScreen(); + } + logMessage("INFO","loadGameList","No, loading game list"); + CURRENT_SECTION.initialized=1; + //We don't need to reload the alias file if just refreshing + if (!refresh) { + loadAliasList(currentSectionNumber); + } + CURRENT_SECTION.totalPages=0; + CURRENT_SECTION.gameCount=0; + char *files[MAX_GAMES_IN_SECTION]; + int game = -1; + int dirCounter=0; + char *dirs[10]; + char *ptr=NULL; - char sectionCacheName[PATH_MAX]; + char sectionCacheName[PATH_MAX]; + if (refresh) { + cleanListForSection(&CURRENT_SECTION); + logMessage("INFO","loadGameList","Cleaned section list"); + } + if (useCache==1) { if (refresh) { - cleanListForSection(&CURRENT_SECTION); - logMessage("INFO","loadGameList","Cleaned section list"); + remove(sectionCacheName); } - if (useCache==1) { - if (refresh) { - remove(sectionCacheName); - } - snprintf(sectionCacheName,sizeof(sectionCacheName),"%s/.simplemenu/tmp/%s.tmp",getenv("HOME"),CURRENT_SECTION.sectionName); - fp = fopen(sectionCacheName,"r"); - if (fp!=NULL) { - logMessage("INFO","loadGameList","Using cache file"); - char currentline[2000]; - while (fgets(currentline, sizeof(currentline), fp) != NULL) { - if(getLaunchAtBoot()==NULL) { - drawLoadingText(); - refreshScreen(); - } - - int size = strlen(currentline)+1; - - struct Rom *rom; - rom=malloc(sizeof(struct Rom)); - - char *ptr = strtok(currentline,";"); - size = strlen(ptr)+1; - rom->alias=malloc(size); - if (strcmp(ptr,"(null)")==0) { - free(rom->alias); - rom->alias = "\0"; - } else { - memcpy(rom->alias,ptr,size); - } + snprintf(sectionCacheName,sizeof(sectionCacheName),"%s/.simplemenu/tmp/%s.tmp",getenv("HOME"),CURRENT_SECTION.sectionName); + fp = fopen(sectionCacheName,"r"); + if (fp!=NULL) { + logMessage("INFO","loadGameList","Using cache file"); + char currentline[2000]; + while (fgets(currentline, sizeof(currentline), fp) != NULL) { + if(getLaunchAtBoot()==NULL) { + drawLoadingText(); + refreshScreen(); + } - ptr = strtok(NULL,";"); - size = strlen(ptr)+1; - rom->directory=strdup(ptr); + int size = strlen(currentline)+1; - ptr = strtok(NULL,";"); - size = strlen(ptr)+1; - rom->name=strdup(ptr); - rom->name[size-2] = '\0'; - if (game==ITEMS_PER_PAGE) { - CURRENT_SECTION.totalPages++; - game = 0; - } - loadRomPreferences(rom); - InsertAtTail(rom); - game++; - CURRENT_SECTION.gameCount++; + struct Rom *rom; + rom=malloc(sizeof(struct Rom)); + + char *ptr = strtok(currentline,";"); + size = strlen(ptr)+1; + rom->alias=malloc(size); + if (strcmp(ptr,"(null)")==0) { + free(rom->alias); + rom->alias = "\0"; + } else { + memcpy(rom->alias,ptr,size); } - logMessage("INFO","loadGameList","Finished reading cache"); - CURRENT_SECTION.tail=GetNthNode(CURRENT_SECTION.gameCount-1); - scrollToGame(CURRENT_SECTION.realCurrentGameNumber); - if (fp!=NULL) { - fclose(fp); + + ptr = strtok(NULL,";"); + size = strlen(ptr)+1; + rom->directory=strdup(ptr); + + ptr = strtok(NULL,";"); + size = strlen(ptr)+1; + rom->name=strdup(ptr); + rom->name[size-2] = '\0'; + if (game==ITEMS_PER_PAGE) { + CURRENT_SECTION.totalPages++; + game = 0; } - return; + loadRomPreferences(rom); + InsertAtTail(rom); + game++; + CURRENT_SECTION.gameCount++; } + logMessage("INFO","loadGameList","Finished reading cache"); + CURRENT_SECTION.tail=GetNthNode(CURRENT_SECTION.gameCount-1); + scrollToGame(CURRENT_SECTION.realCurrentGameNumber); if (fp!=NULL) { fclose(fp); } + return; } - char *filesDirectoriesCopy = strdup(CURRENT_SECTION.filesDirectories); - ptr = strtok(filesDirectoriesCopy, ","); - loading=1; - while (ptr!=NULL) { - dirs[dirCounter]=strdup(ptr); - ptr = strtok(NULL, ","); - dirCounter++; + if (fp!=NULL) { + fclose(fp); + } + } + char *filesDirectoriesCopy = strdup(CURRENT_SECTION.filesDirectories); + ptr = strtok(filesDirectoriesCopy, ","); + loading=1; + while (ptr!=NULL) { + dirs[dirCounter]=strdup(ptr); + ptr = strtok(NULL, ","); + dirCounter++; + } + free(filesDirectoriesCopy); + for(int k=0;kname=malloc(size); - rom->alias=malloc(aliasSize); - int directorySize = strlen(dirs[k])+1; - rom->directory=malloc(directorySize); - strcpy(rom->directory,dirs[k]); - - if (game==ITEMS_PER_PAGE) { - if(desktopCounter!=realItemCount) { - CURRENT_SECTION.totalPages++; - game = 0; - } + realItemCount++; + int size = strlen(desktopFiles[desktopCounter].parentOPK)+strlen("-m|")+strlen(desktopFiles[desktopCounter].name)+2; // " -m " + int aliasSize = strlen(desktopFiles[desktopCounter].displayName)+1; + struct Rom *rom; + rom=malloc(sizeof(struct Rom)); + rom->name=malloc(size); + rom->alias=malloc(aliasSize); + int directorySize = strlen(dirs[k])+1; + rom->directory=malloc(directorySize); + strcpy(rom->directory,dirs[k]); + + if (game==ITEMS_PER_PAGE) { + if(desktopCounter!=realItemCount) { + CURRENT_SECTION.totalPages++; + game = 0; } + } #ifdef TARGET_RFW - strcpy(rom->name,"-m|"); - strcat(rom->name,desktopFiles[desktopCounter].name); - strcat(rom->name,"|"); - strcat(rom->name,desktopFiles[desktopCounter].parentOPK); - strcpy(rom->alias,desktopFiles[desktopCounter].displayName); + strcpy(rom->name,"-m|"); + strcat(rom->name,desktopFiles[desktopCounter].name); + strcat(rom->name,"|"); + strcat(rom->name,desktopFiles[desktopCounter].parentOPK); + strcpy(rom->alias,desktopFiles[desktopCounter].displayName); #else - strcpy(rom->name,"-m|"); - strcat(rom->name,desktopFiles[desktopCounter].name); - strcat(rom->name,"|"); - strcat(rom->name,desktopFiles[desktopCounter].parentOPK); - strcpy(rom->alias,desktopFiles[desktopCounter].displayName); + strcpy(rom->name,"-m|"); + strcat(rom->name,desktopFiles[desktopCounter].name); + strcat(rom->name,"|"); + strcat(rom->name,desktopFiles[desktopCounter].parentOPK); + strcpy(rom->alias,desktopFiles[desktopCounter].displayName); #endif - loadRomPreferences(rom); - InsertAtTail(rom); - game++; - CURRENT_SECTION.gameCount++; - } - desktopCounter++; + loadRomPreferences(rom); + InsertAtTail(rom); + game++; + CURRENT_SECTION.gameCount++; } - loadedFiles++; + desktopCounter++; } - //it's not an opk - else { - int size = 2000; - struct Rom *rom; - rom=malloc(sizeof(struct Rom)); - rom->name=malloc(size); - - int directorySize = strlen(dirs[k])+1; - rom->directory=malloc(directorySize); - strcpy(rom->directory,dirs[k]); - rom->isConsoleApp=0; - //it's a custom link - if(strcmp(ext,".fgl")==0) { - struct StolenGMenuFile stolenFile; - logMessage("INFO","loadGameList", "STOLEN FILE!!!"); - fillUpStolenGMenuFile(&stolenFile, files[i]); - strcpy(rom->name,stolenFile.exec); - if(stolenFile.params[0]!='\0') { - strcat(rom->name," "); - strcat(rom->name,stolenFile.params); - } - logMessage("INFO","loadGameList", "FINAL NAME"); - logMessage("INFO","loadGameList", rom->name); - rom->alias=malloc(strlen(stolenFile.title)+1); - strcpy(rom->alias, stolenFile.title); - rom->isConsoleApp=stolenFile.isConsoleApp; + loadedFiles++; + } + //it's not an opk + else { + int size = 2000; + struct Rom *rom; + rom=malloc(sizeof(struct Rom)); + rom->name=malloc(size); + + int directorySize = strlen(dirs[k])+1; + rom->directory=malloc(directorySize); + strcpy(rom->directory,dirs[k]); + rom->isConsoleApp=0; + //it's a custom link + if(strcmp(ext,".fgl")==0) { + struct StolenGMenuFile stolenFile; + logMessage("INFO","loadGameList", "STOLEN FILE!!!"); + fillUpStolenGMenuFile(&stolenFile, files[i]); + strcpy(rom->name,stolenFile.exec); + if(stolenFile.params[0]!='\0') { + strcat(rom->name," "); + strcat(rom->name,stolenFile.params); + } + logMessage("INFO","loadGameList", "FINAL NAME"); + logMessage("INFO","loadGameList", rom->name); + rom->alias=malloc(strlen(stolenFile.title)+1); + strcpy(rom->alias, stolenFile.title); + rom->isConsoleApp=stolenFile.isConsoleApp; + } else { + //it's a rom + strcpy(rom->name,files[i]); + if(strlen(CURRENT_SECTION.aliasFileName)>2) { + char *temp=getAlias(rom->name); + rom->alias=malloc(strlen(temp)+1); + strcpy(rom->alias, temp); + free(temp); } else { - //it's a rom - strcpy(rom->name,files[i]); - if(strlen(CURRENT_SECTION.aliasFileName)>2) { - char *temp=getAlias(rom->name); - rom->alias=malloc(strlen(temp)+1); - strcpy(rom->alias, temp); - free(temp); - } else { - rom->alias=NULL; - } + rom->alias=NULL; } - CURRENT_SECTION.gameCount++; - if (game==ITEMS_PER_PAGE) { - if(i!=realItemCount) { - CURRENT_SECTION.totalPages++; - game = 0; - } + } + CURRENT_SECTION.gameCount++; + if (game==ITEMS_PER_PAGE) { + if(i!=realItemCount) { + CURRENT_SECTION.totalPages++; + game = 0; } - loadRomPreferences(rom); - InsertAtTail(rom); - loadedFiles++; - game++; } + loadRomPreferences(rom); + InsertAtTail(rom); + loadedFiles++; + game++; } } - logMessage("INFO","loadGameList","All files loaded"); - for (int i=0;i 0) { - logMessage("INFO","loadGameList","The list needs to be sorted"); - mergeSort(&CURRENT_SECTION.head); + logMessage("INFO","loadGameList","The list needs to be sorted"); + mergeSort(&CURRENT_SECTION.head); // } - if(useCache==1) { - fp = fopen(sectionCacheName,"w"); - for (int i=0;idata; - fprintf(fp,"%s;%s;%s\n",rom->alias, rom->directory, rom->name); - } - } - CURRENT_SECTION.tail=GetNthNode(CURRENT_SECTION.gameCount-1); - scrollToGame(CURRENT_SECTION.realCurrentGameNumber); - if (fp!=NULL) { - fclose(fp); + if(useCache==1) { + fp = fopen(sectionCacheName,"w"); + for (int i=0;idata; + fprintf(fp,"%s;%s;%s\n",rom->alias, rom->directory, rom->name); } } - loading=0; + CURRENT_SECTION.tail=GetNthNode(CURRENT_SECTION.gameCount-1); + scrollToGame(CURRENT_SECTION.realCurrentGameNumber); + if (fp!=NULL) { + fclose(fp); + } } + loading=0; +} - int countGamesInPage() { - int gamesCounter=0; - struct Node *node; - int number = CURRENT_GAME_NUMBER; - while(number%ITEMS_PER_PAGE!=0) { - number--; - } - node = CURRENT_SECTION.currentGameNode; - for (int i=number;idata->name!=NULL && strlen(node->data->name)>1) { - gamesCounter++; - } - if (node->next==NULL) { - break; - } - node=node->next; +int countGamesInPage() { + int gamesCounter = 0; + struct Node *node; + int number = CURRENT_GAME_NUMBER; + while (number % ITEMS_PER_PAGE != 0) { + number--; + } + node = CURRENT_SECTION.currentGameNode; + for (int i = number; i < number + ITEMS_PER_PAGE; i++) { + if (node != NULL) { + if (node->data->name != NULL && strlen(node->data->name) > 1) { + gamesCounter++; } + if (node->next == NULL) { + break; + } + node = node->next; } - return gamesCounter; } + return gamesCounter; +} - void selectRandomGame() { - int game = rand() % CURRENT_SECTION.gameCount; - scrollToGame(game); - } +void selectRandomGame() { + int game = rand() % CURRENT_SECTION.gameCount; + scrollToGame(game); +} - void determineStartingScreen(int sectionCount) { - if (sectionCount==-1) { - generateError("SECTIONS FILE NOT FOUND-SHUTTING DOWN",1); - return; +void determineStartingScreen(int sectionCount) { + if (sectionCount == -1) { + generateError("SECTIONS FILE NOT FOUND-SHUTTING DOWN", 1); + return; + } + if (sectionCount == 0 || currentSectionNumber == favoritesSectionNumber) { + logMessage("INFO", "determineStartingScreen", "No sections found or favorites was selected"); + favoritesSectionSelected = 1; + loadFavoritesSectionGameList(); + logMessage("INFO", "determineStartingScreen", "Favorites loaded"); + if (CURRENT_SECTION.backgroundSurface == NULL) { + logMessage("INFO","determineStartingScreen","Loading system background"); + CURRENT_SECTION.backgroundSurface = IMG_Load(CURRENT_SECTION.background); + resizeSectionBackground(&CURRENT_SECTION); + CURRENT_SECTION.systemPictureSurface = IMG_Load(CURRENT_SECTION.systemPicture); + resizeSectionSystemPicture(&CURRENT_SECTION); } - if(sectionCount==0||currentSectionNumber==favoritesSectionNumber) { - logMessage("INFO","determineStartingScreen","No sections found or favorites was selected"); - favoritesSectionSelected=1; - loadFavoritesSectionGameList(); - logMessage("INFO","determineStartingScreen","Favorites loaded"); - if (CURRENT_SECTION.backgroundSurface == NULL) { - logMessage("INFO","determineStartingScreen","Loading system background"); - CURRENT_SECTION.backgroundSurface = IMG_Load(CURRENT_SECTION.background); - resizeSectionBackground(&CURRENT_SECTION); - CURRENT_SECTION.systemPictureSurface = IMG_Load(CURRENT_SECTION.systemPicture); - resizeSectionSystemPicture(&CURRENT_SECTION); - } - logMessage("INFO","determineStartingScreen","Section background loaded and resized"); - int gamesInSection=CURRENT_SECTION.gameCount; - int pages = gamesInSection / ITEMS_PER_PAGE; - if (gamesInSection%ITEMS_PER_PAGE==0) { - pages--; - } - logMessage("INFO","determineStartingScreen","Pages set"); - CURRENT_SECTION.totalPages=pages; - if (favoritesSize==0&§ionCount>0) { - logMessage("INFO","determineStartingScreen","Favorites was selected but no favorites were found"); - favoritesSectionSelected=0; - currentSectionNumber=0; - logMessage("INFO","determineStartingScreen","Trying to determine starting section again"); - determineStartingScreen(sectionCount); - } - } else { - logMessage("INFO","determineStartingScreen","Loading game list"); + logMessage("INFO", "determineStartingScreen", "Section background loaded and resized"); + int gamesInSection = CURRENT_SECTION.gameCount; + int pages = gamesInSection / ITEMS_PER_PAGE; + if (gamesInSection % ITEMS_PER_PAGE == 0) { + pages--; + } + logMessage("INFO", "determineStartingScreen", "Pages set"); + CURRENT_SECTION.totalPages=pages; + if (favoritesSize == 0 && sectionCount > 0) { + logMessage("INFO", "determineStartingScreen", "Favorites was selected but no favorites were found"); + favoritesSectionSelected = 0; + currentSectionNumber = 0; + logMessage("INFO", "determineStartingScreen", "Trying to determine starting section again"); + determineStartingScreen(sectionCount); + } + } else { + logMessage("INFO", "determineStartingScreen", "Loading game list"); + loadGameList(0); + if (CURRENT_SECTION.gameCount==0) { + advanceSection(0); + logMessage("INFO","determineStartingScreen","Loading game list again"); loadGameList(0); - if (CURRENT_SECTION.gameCount==0){ - advanceSection(0); - logMessage("INFO","determineStartingScreen","Loading game list again"); - loadGameList(0); -// generateError("NO GAMES FOUND!", 0); - } - if (CURRENT_SECTION.backgroundSurface == NULL) { - logMessage("INFO","determineStartingScreen","Loading system background"); - CURRENT_SECTION.backgroundSurface = IMG_Load(CURRENT_SECTION.background); - resizeSectionBackground(&CURRENT_SECTION); - CURRENT_SECTION.systemPictureSurface = IMG_Load(CURRENT_SECTION.systemPicture); - resizeSectionSystemPicture(&CURRENT_SECTION); - } - int pages = CURRENT_SECTION.gameCount / ITEMS_PER_PAGE; - if (pages>0&&CURRENT_SECTION.gameCount%ITEMS_PER_PAGE==0) { - pages--; - } - CURRENT_SECTION.totalPages=pages; - +// generateError("NO GAMES FOUND!", 0); } - logMessage("INFO","determineStartingScreen","Found starting screen"); - } - - void deleteGame(struct Rom *rom) { - char command [300]; - snprintf(command,sizeof(command),"rm \"%s\";",rom->name); - int ret = system(command); - if (ret==-1) { - generateError("FATAL ERROR", 1); + if (CURRENT_SECTION.backgroundSurface == NULL) { + logMessage("INFO","determineStartingScreen","Loading system background"); + CURRENT_SECTION.backgroundSurface = IMG_Load(CURRENT_SECTION.background); + resizeSectionBackground(&CURRENT_SECTION); + CURRENT_SECTION.systemPictureSurface = IMG_Load(CURRENT_SECTION.systemPicture); + resizeSectionSystemPicture(&CURRENT_SECTION); } - char *pictureWithFullPath=malloc(600); - char *tempGameName; - char *tempGameName1; - strcpy(pictureWithFullPath, rom->directory); - tempGameName=getGameName(rom->name); - strcat(pictureWithFullPath,mediaFolder); - strcat(pictureWithFullPath,"/"); - tempGameName1=getNameWithoutExtension(tempGameName); - strcat(pictureWithFullPath,tempGameName); - strcat(pictureWithFullPath,".png"); - snprintf(command,sizeof(command),"rm \"%s\";",pictureWithFullPath); - free(tempGameName); - free(tempGameName1); - free(pictureWithFullPath); - ret = system(command); - if (ret==-1) { - generateError("FATAL ERROR", 1); + int pages = CURRENT_SECTION.gameCount / ITEMS_PER_PAGE; + if (pages > 0 && CURRENT_SECTION.gameCount%ITEMS_PER_PAGE==0) { + pages--; } + CURRENT_SECTION.totalPages=pages; + } + logMessage("INFO", "determineStartingScreen", "Found starting screen"); +} - int is43 () { - return SCREEN_RATIO<=1.34&&SCREEN_RATIO>=1.33; +void deleteGame(struct Rom *rom) { + char command[300]; + snprintf(command, sizeof(command), "rm \"%s\";", rom->name); + int ret = system(command); + if (ret == -1) { + generateError("FATAL ERROR", 1); } + char *pictureWithFullPath = malloc(600); + char *tempGameName; + char *tempGameName1; + strcpy(pictureWithFullPath, rom->directory); + tempGameName = getGameName(rom->name); + strcat(pictureWithFullPath, mediaFolder); + strcat(pictureWithFullPath, "/"); + tempGameName1 = getNameWithoutExtension(tempGameName); + strcat(pictureWithFullPath, tempGameName); + strcat(pictureWithFullPath, ".png"); + snprintf(command, sizeof(command), "rm \"%s\";", pictureWithFullPath); + free(tempGameName); + free(tempGameName1); + free(pictureWithFullPath); + ret = system(command); + if (ret == -1) { + generateError("FATAL ERROR", 1); + } +} + +int is43() { + return SCREEN_RATIO <= 1.34 && SCREEN_RATIO >= 1.33; +} diff --git a/simplemenu/src/logic/screen.c b/simplemenu/src/logic/screen.c index 171234b8..7027ff4d 100644 --- a/simplemenu/src/logic/screen.c +++ b/simplemenu/src/logic/screen.c @@ -306,6 +306,7 @@ void showConsole() { void displayGamePicture(struct Rom *rom) { char *pictureWithFullPath=malloc(600); char *tempGameName=malloc(300); + char *originalGameName = NULL; if (favoritesSectionSelected) { if (favoritesSize == 0) { return; @@ -313,19 +314,29 @@ void displayGamePicture(struct Rom *rom) { struct Favorite favorite = favorites[CURRENT_GAME_NUMBER]; strcpy(pictureWithFullPath, favorite.filesDirectory); tempGameName=getGameName(favorite.name); + originalGameName = favorite.name; } else { if (rom==NULL) { strcpy(pictureWithFullPath, "NO GAMES FOUND"); tempGameName=getGameName("NO GAMES FOUND"); + originalGameName = tempGameName; } else { strcpy(pictureWithFullPath, rom->directory); tempGameName=getGameName(rom->name); + originalGameName = rom->name; } } - strcat(pictureWithFullPath,mediaFolder); - strcat(pictureWithFullPath,"/"); + + int len = strlen(originalGameName); + const char *lastFour = &originalGameName[len-4]; + + if (strcmp(lastFour,".png") != 0) { + strcat(pictureWithFullPath,mediaFolder); + strcat(pictureWithFullPath,"/"); + } strcat(pictureWithFullPath,tempGameName); strcat(pictureWithFullPath,".png"); + displayBackgroundPicture(); if (rom==NULL) { displayCenteredImageOnScreen(pictureWithFullPath, tempGameName, 1,1); @@ -423,24 +434,37 @@ void displayGamePicture(struct Rom *rom) { void displayGamePictureInMenu(struct Rom *rom) { char *pictureWithFullPath=malloc(600); char *tempGameName=malloc(300); + char *originalGameName = NULL; if (favoritesSectionSelected) { if (favoritesSize == 0) { return; } struct Favorite favorite = favorites[CURRENT_GAME_NUMBER]; strcpy(pictureWithFullPath, favorite.filesDirectory); + originalGameName = favorite.name; tempGameName=getGameName(favorite.name); } else { if (rom==NULL) { + logMessage("INFO","displayGamePictureInMenu","No games!"); strcpy(pictureWithFullPath, "NO GAMES FOUND"); tempGameName=getGameName("NO GAMES FOUND"); + originalGameName = tempGameName; } else { + logMessage("INFO","displayGamePictureInMenu","Displaying..."); strcpy(pictureWithFullPath, rom->directory); + originalGameName = rom->name; tempGameName=getGameName(rom->name); } } - strcat(pictureWithFullPath,mediaFolder); - strcat(pictureWithFullPath,"/"); + + int len = strlen(originalGameName); + const char *lastFour = &originalGameName[len-4]; + + if (strcmp(lastFour,".png") != 0) { + strcat(pictureWithFullPath,mediaFolder); + strcat(pictureWithFullPath,"/"); + } + strcat(pictureWithFullPath,tempGameName); strcat(pictureWithFullPath,".png"); displayImageOnScreenCustom(pictureWithFullPath); diff --git a/simplemenu/src/logic/simplemenu.c b/simplemenu/src/logic/simplemenu.c index f7bac466..e9083385 100644 --- a/simplemenu/src/logic/simplemenu.c +++ b/simplemenu/src/logic/simplemenu.c @@ -88,10 +88,10 @@ void initialSetup() { loadLastState(); HW_Init(); currentCPU = OC_NO; -//#ifndef TARGET_OD_BETA +#ifndef TARGET_OD_BETA logMessage("INFO","initialSetup","Setting CPU to base"); setCPU(currentCPU); -//#endif +#endif setupKeys(); checkIfDefault(); }