Fix directory browsing

Only the first page of files can be browsed in the directory though.
The top screen is where the playlist will be displayed, but is currently
used for music playback info.

Signed-off-by: Mahyar Koshkouei <deltabeard@users.noreply.github.com>
This commit is contained in:
Mahyar Koshkouei
2016-12-31 19:16:37 +00:00
parent 91b55285f2
commit 9920fd1ac3
2 changed files with 115 additions and 30 deletions

View File

@@ -13,6 +13,7 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <unistd.h>
#include "all.h" #include "all.h"
#include "flac.h" #include "flac.h"
@@ -21,7 +22,7 @@
#include "wav.h" #include "wav.h"
/* Default folder */ /* Default folder */
#define TOP_FOLDER "sdmc:/" #define DEFAULT_DIR "sdmc:/"
/* Maximum number of lines that can be displayed */ /* Maximum number of lines that can be displayed */
#define MAX_LIST 28 #define MAX_LIST 28
@@ -35,26 +36,28 @@ enum file_types {
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
DIR *dp;
struct dirent *ep;
PrintConsole topScreen; PrintConsole topScreen;
PrintConsole bottomScreen; PrintConsole bottomScreen;
int fileMax; int fileMax;
int fileNum = 0; int fileNum = 0;
int from = 0;
gfxInitDefault(); gfxInitDefault();
consoleInit(GFX_TOP, &topScreen); consoleInit(GFX_TOP, &topScreen);
consoleInit(GFX_BOTTOM, &bottomScreen); consoleInit(GFX_BOTTOM, &bottomScreen);
consoleSelect(&bottomScreen); consoleSelect(&bottomScreen);
if((fileMax = listDir(TOP_FOLDER, 0, MAX_LIST)) < 0) chdir(DEFAULT_DIR);
chdir("MUSIC");
if((fileMax = listDir(from, MAX_LIST, 0)) < 0)
{ {
err_print("Unable to list directory."); err_print("Unable to list directory.");
goto out; goto out;
} }
consoleSelect(&topScreen); consoleSelect(&topScreen);
puts("Queue"); puts("Playlist");
consoleSelect(&bottomScreen);
/** /**
* This allows for music to continue playing through the headphones whilst * This allows for music to continue playing through the headphones whilst
@@ -78,45 +81,93 @@ int main(int argc, char **argv)
break; break;
if(kDown & KEY_UP) if(kDown & KEY_UP)
{
if(fileNum < fileMax)
fileNum++;
}
if(kDown & KEY_DOWN)
{ {
if(fileNum > 0) if(fileNum > 0)
fileNum--; fileNum--;
} }
if(kDown & KEY_DOWN)
{
if(fileNum < fileMax)
fileNum++;
}
if(kDown & (KEY_DOWN | KEY_UP)) if(kDown & (KEY_DOWN | KEY_UP))
{ {
printf("\33[2K\rSelected file %d", fileNum); consoleClear();
if((fileMax = listDir(from, MAX_LIST, fileNum)) < 0)
{
err_print("Unable to list directory.");
}
}
if((kDown & (KEY_A | KEY_R)) && (from == 0 && fileNum == 0))
{
if(chdir("..") != 0)
err_print("chdir");
consoleClear();
if((fileMax = listDir(from, MAX_LIST, fileNum)) < 0)
{
err_print("Unable to list directory.");
}
continue;
} }
if(kDown & (KEY_A | KEY_R)) if(kDown & (KEY_A | KEY_R))
{ {
int audioFileNum = 0; int audioFileNum = 0;
dp = opendir(TOP_FOLDER); DIR *dp;
char* file = NULL; struct dirent *ep;
char* wd = getcwd(NULL, 0);
/* TODO: Error out properly */
if(wd == NULL)
{
err_print("wd");
goto out;
}
dp = opendir(wd);
if (dp != NULL) if (dp != NULL)
{ {
char* file = NULL;
while((ep = readdir(dp)) != NULL) while((ep = readdir(dp)) != NULL)
{ {
if(audioFileNum == fileNum) if(audioFileNum == fileNum - 1)
break; break;
audioFileNum++; audioFileNum++;
} }
if(asprintf(&file, "%s%s", AUDIO_FOLDER, ep->d_name) == -1) if(ep->d_type == DT_DIR)
{
/* file not allocated yet, so no need to clear it */
if(chdir(ep->d_name) != 0)
err_print("chdir");
fileNum = 0;
consoleClear();
if((fileMax = listDir(from, MAX_LIST, fileNum)) < 0)
{
err_print("Unable to list directory.");
}
free(wd);
continue;
}
if(asprintf(&file, "%s%s", wd, ep->d_name) == -1)
{ {
err_print("Constructing file name failed."); err_print("Constructing file name failed.");
file = NULL; file = NULL;
} }
else else
{ {
consoleSelect(&topScreen);
switch(getFileType(file)) switch(getFileType(file))
{ {
case FILE_TYPE_WAV: case FILE_TYPE_WAV:
@@ -132,50 +183,75 @@ int main(int argc, char **argv)
break; break;
default: default:
consoleSelect(&bottomScreen);
printf("Unsupported File type.\n"); printf("Unsupported File type.\n");
} }
} }
consoleSelect(&bottomScreen);
free(file);
free(wd);
if(closedir(dp) != 0) if(closedir(dp) != 0)
err_print("Closing directory failed."); err_print("Closing directory failed.");
} }
else else
err_print("Unable to open directory."); err_print("Unable to open directory.");
free(file);
} }
} }
out: out:
puts("Exiting..."); puts("Exiting...");
/* Catch */
while(true)
{
u32 kDown;
hidScanInput();
kDown = hidKeysDown();
if(kDown & KEY_START)
break;
}
gfxExit(); gfxExit();
return 0; return 0;
} }
/** /**
* List directory. * List current directory.
* *
* \param dir Path of directory.
* \param from First entry in directory to list. * \param from First entry in directory to list.
* \param max Maximum number of entries to list. Must be > 0. * \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. * \return Number of entries listed or negative on error.
*/ */
int listDir(const char *dir, int from, int max) int listDir(int from, int max, int select)
{ {
DIR *dp; DIR *dp;
struct dirent *ep; struct dirent *ep;
int fileNum = 0; int fileNum = 0;
int countChr = 0; int countChr = 0;
int listed = 0; int listed = 0;
char* wd = getcwd(NULL, 0);
if((dp = opendir(dir)) == NULL) if(wd == NULL)
goto err; goto err;
/* TODO: Remove debug */
printf("Dir: %.30s\n", wd);
if((dp = opendir(wd)) == NULL)
goto err;
#if 0
/* Count number of occurrences of character in string. */ /* Count number of occurrences of character in string. */
for(int i = 0; i < strlen(dir); i++) for(int i = 0; wd[i] != '\0'; i++)
{ {
if(dir[i] == '/') if(wd[i] == '/')
countChr++; countChr++;
} }
@@ -187,7 +263,9 @@ int listDir(const char *dir, int from, int max)
} }
if(countChr > 1 && from == 0) if(countChr > 1 && from == 0)
puts("../"); printf("%c../\n", select == 0 ? '>' : ' ');
#endif
printf("%c../\n", select == 0 ? '>' : ' ');
while((ep = readdir(dp)) != NULL) while((ep = readdir(dp)) != NULL)
{ {
@@ -200,16 +278,23 @@ int listDir(const char *dir, int from, int max)
break; break;
listed++; listed++;
printf(" %.48s%s\n", ep->d_name, ep->d_type == DT_DIR ? "/" : "");
printf("%c%.38s%s\n",
select == fileNum ? '>' : ' ',
ep->d_name,
ep->d_type == DT_DIR ? "/" : "");
} }
if(closedir(dp) != 0) if(closedir(dp) != 0)
goto err; goto err;
out:
free(wd);
return listed; return listed;
err: err:
return -1; listed = -1;
goto out;
} }
/** /**

View File

@@ -8,14 +8,14 @@
*/ */
/** /**
* List directory. * List current directory.
* *
* \param dir Path of directory.
* \param from First entry in directory to list. * \param from First entry in directory to list.
* \param max Maximum number of entries to list. Must be > 0. * \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. * \return Number of entries listed or negative on error.
*/ */
int listDir(const char *dir, int from, int max); int listDir(int from, int max, int select);
/** /**
* Obtains file type. * Obtains file type.