This commit is contained in:
2025-12-06 22:42:06 -06:00
parent ab9b766be3
commit e491cddfcb
8 changed files with 615 additions and 255 deletions

View File

@@ -21,23 +21,31 @@
#include "main.h"
#include "metadata.h"
#include "playback.h"
#include "gui.h"
volatile bool runThreads = true;
/**
* Prints the current key mappings to stdio.
*/
static void showControls(void)
{
printf("Button mappings:\n"
"Pause: L+R or L+Up\n"
"Previous/Next Song: ZL/ZR or L/R\n"
"A: Open File\n"
"B: Go up folder\n"
"Start: Exit\n"
"Browse: Up, Down, Left or Right\n");
/* Log message buffer for GUI display */
#define MAX_LOG_MESSAGES 100
static char* logMessages[MAX_LOG_MESSAGES] = {0};
static int logMessageCount = 0;
static int logScroll = 0;
static void addLogMessage(const char* msg) {
if (logMessageCount >= MAX_LOG_MESSAGES) {
/* Remove oldest message */
free(logMessages[0]);
memmove(logMessages, logMessages + 1, (MAX_LOG_MESSAGES - 1) * sizeof(char*));
logMessageCount--;
}
logMessages[logMessageCount++] = strdup(msg);
}
/**
* Prints the current key mappings (removed - not needed for GUI)
*/
/* Controls are now implied by the GUI interface */
/**
* Allows the playback thread to return any error messages that it may
* encounter.
@@ -56,19 +64,17 @@ void playbackWatchdog(void* infoIn)
if(*info->errInfo->error > 0)
{
continue;
consoleSelect(info->screen);
printf("Error %d: %s\n", *info->errInfo->error,
mice_strerror(*info->errInfo->error));
char errorMsg[256];
snprintf(errorMsg, sizeof(errorMsg), "Error %d: %s",
*info->errInfo->error, mice_strerror(*info->errInfo->error));
addLogMessage(errorMsg);
}
else if (*info->errInfo->error == -1)
{
continue;
/* Used to signify that playback has stopped.
* Not technically an error.
* Not technically an error. Don't spam logs.
*/
consoleSelect(info->screen);
puts("Stopped");
/* addLogMessage("Stopped"); */
}
}
@@ -115,7 +121,7 @@ static int changeFile(const char* ep_file, struct playbackInfo_t* playbackInfo)
//playbackInfo->file = strdup(ep_file);
if (memccpy(playbackInfo->file, ep_file, '\0', sizeof(playbackInfo->file)) == NULL)
{
puts("Error: File path too long\n");
addLogMessage("Error: File path too long");
return -1;
}
@@ -164,7 +170,7 @@ static int getDir(struct dirList_t* dirList)
free(dirList->currentDir);
if((dirList->currentDir = strdup(wd)) == NULL)
puts("Failure");
addLogMessage("Memory allocation failure");
if((dp = opendir(wd)) == NULL)
goto out;
@@ -177,7 +183,7 @@ static int getDir(struct dirList_t* dirList)
dirList->directories = realloc(dirList->directories, (dirNum + 1) * sizeof(char*));
if((dirList->directories[dirNum] = strdup(ep->d_name)) == NULL)
puts("Failure");
addLogMessage("Memory allocation failure");
dirNum++;
continue;
@@ -187,7 +193,7 @@ static int getDir(struct dirList_t* dirList)
dirList->files = realloc(dirList->files, (fileNum + 1) * sizeof(char*));
if((dirList->files[fileNum] = strdup(ep->d_name)) == NULL)
puts("Failure");
addLogMessage("Memory allocation failure");
fileNum++;
}
@@ -207,59 +213,53 @@ out:
}
/**
* List current directory.
*
* \param from First entry in directory to list.
* \param max Maximum number of entries to list. Must be > 0.
* \param select File to show as selected. Must be > 0.
* \return Number of entries listed or negative on error.
* Build file list for GUI display.
* Creates a combined list of directories and files for rendering.
*/
static int listDir(int from, int max, int select, struct dirList_t dirList)
static void buildFileListForGUI(struct dirList_t dirList, const char*** outList, int* outCount, int from)
{
int fileNum = 0;
int listed = 0;
printf("\033[0;0H");
printf("Dir: %.33s\n", dirList.currentDir);
static const char* combinedList[512];
static char entryBuffer[512][256];
int index = 0;
/* Add parent directory option */
if(from == 0)
{
printf("\33[2K%c../\n", select == 0 ? '>' : ' ');
listed++;
max--;
snprintf(entryBuffer[index], sizeof(entryBuffer[index]), "../");
combinedList[index] = entryBuffer[index];
index++;
}
while(dirList.fileNum + dirList.dirNum > fileNum)
/* Add all directories */
for(int i = 0; i < dirList.dirNum && index < 512; i++)
{
fileNum++;
if(fileNum <= from)
continue;
listed++;
if(dirList.dirNum >= fileNum)
{
printf("\33[2K%c\x1b[34;1m%.37s/\x1b[0m\n",
select == fileNum ? '>' : ' ',
dirList.directories[fileNum - 1]);
}
/* fileNum must be referring to a file instead of a directory. */
if(dirList.dirNum < fileNum)
{
printf("\33[2K%c%.37s\n",
select == fileNum ? '>' : ' ',
dirList.files[fileNum - dirList.dirNum - 1]);
}
if(fileNum == max + from)
break;
snprintf(entryBuffer[index], sizeof(entryBuffer[index]), "%s/", dirList.directories[i]);
combinedList[index] = entryBuffer[index];
index++;
}
/* Add all files */
for(int i = 0; i < dirList.fileNum && index < 512; i++)
{
snprintf(entryBuffer[index], sizeof(entryBuffer[index]), "%s", dirList.files[i]);
combinedList[index] = entryBuffer[index];
index++;
}
*outList = combinedList;
*outCount = index;
}
return listed;
/**
* Dummy function kept for compatibility (no longer used with GUI)
*/
static int listDir(int from __attribute__((unused)),
int max __attribute__((unused)),
int select __attribute__((unused)),
struct dirList_t dirList __attribute__((unused)))
{
/* This function is no longer used with GUI rendering */
return 0;
}
/**
@@ -290,13 +290,12 @@ err:
goto out;
}
int main(int argc, char **argv)
int main(int argc __attribute__((unused)), char **argv __attribute__((unused)))
{
PrintConsole topScreenLog, topScreenInfo, bottomScreen;
int fileMax;
int fileNum = 0;
int from = 0;
Thread watchdogThread;
Thread watchdogThread __attribute__((unused));
Handle playbackFailEvent;
struct watchdogInfo watchdogInfoIn;
struct errInfo_t errInfo;
@@ -309,26 +308,17 @@ int main(int argc, char **argv)
bool keyLComboPressed = false;
bool keyRComboPressed = false;
gfxInitDefault();
consoleInit(GFX_TOP, &topScreenLog);
consoleInit(GFX_TOP, &topScreenInfo);
consoleInit(GFX_BOTTOM, &bottomScreen);
/* Set console sizes. */
// (y-1) + (height) <= 30 (top screen only fits 30 lines)
consoleSetWindow(&topScreenLog, 1, 4, 50, 27);
consoleSetWindow(&topScreenInfo, 1, 1, 50, 3);
consoleSelect(&bottomScreen);
/* Display version in bottom right corner */
printf("\033[28;30H%s", MICE_VERSION);
/* Initialize GUI system */
if(guiInit() != 0)
{
return -1;
}
svcCreateEvent(&playbackFailEvent, RESET_ONESHOT);
errInfo.error = &error;
errInfo.failEvent = &playbackFailEvent;
watchdogInfoIn.screen = &topScreenLog;
watchdogInfoIn.screen = NULL; /* No longer using console */
watchdogInfoIn.errInfo = &errInfo;
watchdogThread = threadCreate(playbackWatchdog,
&watchdogInfoIn, 4 * 1024, 0x20, -2, true);
@@ -346,13 +336,7 @@ int main(int argc, char **argv)
/* TODO: Not actually possible to get less than 0 */
if(getDir(&dirList) < 0)
{
puts("Unable to obtain directory information");
goto err;
}
if(listDir(from, MAX_LIST, 0, dirList) < 0)
{
err_print("Unable to list directory.");
addLogMessage("Unable to obtain directory information");
goto err;
}
@@ -371,25 +355,22 @@ int main(int argc, char **argv)
u32 kUp;
static u64 mill = 0;
gfxFlushBuffers();
gspWaitForVBlank();
gfxSwapBuffers();
/* Begin GUI frame */
guiBeginFrame();
guiClearTopScreen();
guiClearBottomScreen();
hidScanInput();
kDown = hidKeysDown();
kHeld = hidKeysHeld();
kUp = hidKeysUp();
consoleSelect(&bottomScreen);
/* Exit mice */
if(kDown & KEY_START)
break;
#ifdef DEBUG
consoleSelect(&topScreenLog);
printf("\rNum: %d, Max: %d, from: %d ", fileNum, fileMax, from);
consoleSelect(&bottomScreen);
/* Debug info logged if needed */
#endif
if(kDown)
mill = osGetTime();
@@ -402,11 +383,10 @@ int main(int argc, char **argv)
if(isPlaying() == false)
continue;
consoleSelect(&topScreenLog);
if(togglePlayback() == true)
puts("Paused");
addLogMessage("Paused");
else
puts("Playing");
addLogMessage("Playing");
keyLComboPressed = true;
// distinguish between L+R and L+Up
@@ -416,11 +396,9 @@ int main(int argc, char **argv)
continue;
}
/* Show controls */
/* Show controls - no longer needed with GUI */
if(kDown & KEY_LEFT)
{
consoleSelect(&topScreenLog);
showControls();
keyLComboPressed = true;
continue;
}
@@ -431,11 +409,10 @@ int main(int argc, char **argv)
if(isPlaying() == false)
continue;
consoleSelect(&topScreenLog);
if(togglePlayback() == true)
puts("Paused");
addLogMessage("Paused");
else
puts("Playing");
addLogMessage("Playing");
keyLComboPressed = true;
keyRComboPressed = true;
@@ -485,12 +462,9 @@ int main(int argc, char **argv)
if(fileMax - fileNum > MAX_LIST-2 && from != 0)
{
from -= skip;
if(from < 0)
if(from < 0)
from = 0;
}
if(listDir(from, MAX_LIST, fileNum, dirList) < 0)
err_print("Unable to list directory.");
}
if((kDown & KEY_RIGHT ||
@@ -511,9 +485,6 @@ int main(int argc, char **argv)
if(from > fileMax - MAX_LIST)
from = fileMax - MAX_LIST;
}
if(listDir(from, MAX_LIST, fileNum, dirList) < 0)
err_print("Unable to list directory.");
}
/*
@@ -524,7 +495,6 @@ int main(int argc, char **argv)
((kDown & KEY_A) && (from == 0 && fileNum == 0)))
{
chdir("..");
consoleClear();
fileMax = getDir(&dirList);
fileNum = prevPosition[0];
@@ -537,9 +507,6 @@ int main(int argc, char **argv)
prevPosition[MAX_DIRECTORIES-1] = 0;
prevFrom[MAX_DIRECTORIES-1] = 0;
if(listDir(from, MAX_LIST, fileNum, dirList) < 0)
err_print("Unable to list directory.");
continue;
}
@@ -548,7 +515,6 @@ int main(int argc, char **argv)
if(dirList.dirNum >= fileNum)
{
chdir(dirList.directories[fileNum - 1]);
consoleClear();
fileMax = getDir(&dirList);
oldFileNum = fileNum;
@@ -556,36 +522,23 @@ int main(int argc, char **argv)
fileNum = 0;
from = 0;
if(listDir(from, MAX_LIST, fileNum, dirList) < 0)
{
err_print("Unable to list directory.");
}
else
{
/* save old position in folder */
for (int i=MAX_DIRECTORIES-1; i>0; i--) {
prevPosition[i] = prevPosition[i-1];
prevFrom[i] = prevFrom[i-1];
}
prevPosition[0] = oldFileNum;
prevFrom[0] = oldFrom;
/* save old position in folder */
for (int i=MAX_DIRECTORIES-1; i>0; i--) {
prevPosition[i] = prevPosition[i-1];
prevFrom[i] = prevFrom[i-1];
}
prevPosition[0] = oldFileNum;
prevFrom[0] = oldFrom;
continue;
}
if(dirList.dirNum < fileNum)
{
consoleSelect(&topScreenInfo);
consoleClear();
/* Extract and display metadata */
char fullPath[512];
snprintf(fullPath, sizeof(fullPath), "%s", dirList.files[fileNum - dirList.dirNum - 1]);
extractMetadata(fullPath, &currentMetadata);
displayMetadata(&currentMetadata, dirList.files[fileNum - dirList.dirNum - 1]);
consoleSelect(&topScreenLog);
//consoleClear();
guiDisplayMetadata(&currentMetadata, dirList.files[fileNum - dirList.dirNum - 1]);
changeFile(dirList.files[fileNum - dirList.dirNum - 1], &playbackInfo);
error = 0;
@@ -608,21 +561,15 @@ int main(int argc, char **argv)
if(fileNum >= MAX_LIST && fileMax - fileNum >= 0 &&
from < fileMax - MAX_LIST)
from++;
consoleSelect(&topScreenInfo);
consoleClear();
/* Extract and display metadata */
char fullPath[512];
snprintf(fullPath, sizeof(fullPath), "%s", dirList.files[fileNum - dirList.dirNum - 1]);
extractMetadata(fullPath, &currentMetadata);
displayMetadata(&currentMetadata, dirList.files[fileNum - dirList.dirNum - 1]);
guiDisplayMetadata(&currentMetadata, dirList.files[fileNum - dirList.dirNum - 1]);
consoleSelect(&topScreenLog);
//consoleClear();
changeFile(dirList.files[fileNum - dirList.dirNum - 1], &playbackInfo);
error = 0;
consoleSelect(&bottomScreen);
if(listDir(from, MAX_LIST, fileNum, dirList) < 0) err_print("Unable to list directory.");
continue;
}
// ignore L release if key combo with L used
@@ -639,21 +586,15 @@ int main(int argc, char **argv)
fileNum -= 1;
if(fileMax - fileNum > MAX_LIST-2 && from != 0)
from--;
consoleSelect(&topScreenInfo);
consoleClear();
/* Extract and display metadata */
char fullPath[512];
snprintf(fullPath, sizeof(fullPath), "%s", dirList.files[fileNum - dirList.dirNum - 1]);
extractMetadata(fullPath, &currentMetadata);
displayMetadata(&currentMetadata, dirList.files[fileNum - dirList.dirNum - 1]);
guiDisplayMetadata(&currentMetadata, dirList.files[fileNum - dirList.dirNum - 1]);
consoleSelect(&topScreenLog);
//consoleClear();
changeFile(dirList.files[fileNum - dirList.dirNum - 1], &playbackInfo);
error = 0;
consoleSelect(&bottomScreen);
if(listDir(from, MAX_LIST, fileNum, dirList) < 0) err_print("Unable to list directory.");
continue;
}
@@ -665,78 +606,70 @@ int main(int argc, char **argv)
continue;
}
fileNum += 1;
consoleSelect(&topScreenInfo);
consoleClear();
/* Extract and display metadata */
char fullPath[512];
snprintf(fullPath, sizeof(fullPath), "%s", dirList.files[fileNum - dirList.dirNum - 1]);
extractMetadata(fullPath, &currentMetadata);
displayMetadata(&currentMetadata, dirList.files[fileNum - dirList.dirNum - 1]);
guiDisplayMetadata(&currentMetadata, dirList.files[fileNum - dirList.dirNum - 1]);
consoleSelect(&topScreenLog);
//consoleClear();
changeFile(dirList.files[fileNum - dirList.dirNum - 1], &playbackInfo);
error = 0;
consoleSelect(&bottomScreen);
if(listDir(from, MAX_LIST, fileNum, dirList) < 0) err_print("Unable to list directory.");
continue;
}
/* After 1000ms, update playback time. */
while(osGetTime() - mill > 1000)
/* Render GUI elements */
const char** fileList;
int fileListCount;
buildFileListForGUI(dirList, &fileList, &fileListCount, from);
/* Display metadata if we have any */
if(currentMetadata.title[0] || currentMetadata.artist[0] || currentMetadata.album[0])
{
consoleSelect(&topScreenLog);
/* Position cursor at bottom of log area for time display */
printf("\033[29;0H\033[K"); /* Move to line 29, clear line */
/* Avoid divide by zero. */
if(playbackInfo.samples_per_second == 0)
break;
{
unsigned hr, min, sec;
size_t seconds_played;
seconds_played = playbackInfo.samples_played / playbackInfo.samples_per_second;
hr = (seconds_played/3600);
min = (seconds_played - (3600*hr))/60;
sec = (seconds_played -(3600*hr)-(min*60));
printf("%02d:%02d:%02d", hr, min, sec);
}
if(playbackInfo.samples_total != 0)
{
unsigned hr, min, sec;
size_t seconds_total;
seconds_total = playbackInfo.samples_total / playbackInfo.samples_per_second;
hr = (seconds_total/3600);
min = (seconds_total - (3600*hr))/60;
sec = (seconds_total -(3600*hr)-(min*60));
printf(" %02d:%02d:%02d", hr, min, sec);
}
break;
const char* currentFile = (fileNum > 0 && fileNum <= dirList.dirNum + dirList.fileNum) ?
(fileNum > dirList.dirNum ? dirList.files[fileNum - dirList.dirNum - 1] : "..") : "";
guiDisplayMetadata(&currentMetadata, currentFile);
}
/* Display file list on bottom screen */
guiDisplayFileList(fileList, fileListCount, fileNum, from);
/* Display logs on top screen */
guiDisplayLog((const char**)logMessages, logMessageCount, logScroll);
/* Display playback status */
if(playbackInfo.samples_per_second > 0)
{
float position = (float)playbackInfo.samples_played / playbackInfo.samples_per_second;
float duration = (float)playbackInfo.samples_total / playbackInfo.samples_per_second;
guiDisplayPlaybackStatus(isPlaying(), isPaused(), position, duration);
}
/* Display version */
guiDisplayVersion(MICE_VERSION);
/* End GUI frame */
guiEndFrame();
}
out:
puts("Exiting...");
addLogMessage("Exiting...");
runThreads = false;
clearMetadata(&currentMetadata);
svcSignalEvent(playbackFailEvent);
changeFile(NULL, &playbackInfo);
gfxExit();
/* Cleanup GUI */
guiExit();
/* Cleanup log messages */
for(int i = 0; i < logMessageCount; i++)
free(logMessages[i]);
return 0;
err:
puts("A fatal error occurred. Press START to exit.");
addLogMessage("A fatal error occurred. Press START to exit.");
while(true)
{