Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remember last rom and automatically show that location. #179

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 47 additions & 1 deletion source/arm11/filebrowser.c
Original file line number Diff line number Diff line change
Expand Up @@ -144,16 +144,62 @@ Result browseFiles(const char *const basePath, char selected[512])
if(curDir == NULL) return RES_OUT_OF_MEM;
safeStrcpy(curDir, basePath, 512);

// Check if full rom path was passed in (ends in .gba)
bool isRomPath = strstr(basePath, ".gba") == basePath + strlen(basePath) - 4;
char* preselectedRomName = NULL;
// Convert rom path to base path
if (isRomPath)
{
size_t cmpLen = strrchr(basePath, '/') - basePath;
cmpLen++;
if (cmpLen < 512)
{
if (cmpLen < strlen(basePath))
{
// Double null as insecure code on KEY_A
// replaces null terminator with '/'
// and does not null terminate it.
curDir[cmpLen] = '\0';
// Remove trailing / as it causes KEY_B
// to navigate to the same directory initially
curDir[cmpLen-1] = '\0';
}
}
preselectedRomName = basePath + cmpLen;
}

DirList *const dList = (DirList*)malloc(sizeof(DirList));
if(dList == NULL) return RES_OUT_OF_MEM;

Result res;
if((res = scanDir(curDir, dList, ".gba")) != RES_OK) goto end;
showDirList(dList, 0);

s32 cursorPos = 0; // Within the entire list.
u32 windowPos = 0; // Window start position within the list.
s32 oldCursorPos = 0;

// Look for preselected rom and set cursor and window position to it
if (preselectedRomName != NULL)
{
for (int i = 0; i < dList->num; i++)
{

char* romName = &dList->ptrs[i][1];

if (strncmp(preselectedRomName, romName, 512) == 0)
{
// Found preselected rom
windowPos = i;
cursorPos = i;
break;
}
}

}

// Show dir list with preselected rom or default location
showDirList(dList, windowPos);

while(1)
{
ee_printf("\x1b[%lu;H ", oldCursorPos - windowPos); // Clear old cursor.
Expand Down
14 changes: 3 additions & 11 deletions source/arm11/open_agb_firm.c
Original file line number Diff line number Diff line change
Expand Up @@ -342,17 +342,9 @@ static Result showFileBrowser(char romAndSavePath[512])
}
else if(res != RES_OK) break;

size_t cmpLen = strrchr(romAndSavePath, '/') - romAndSavePath;
if((size_t)(strchr(romAndSavePath, '/') - romAndSavePath) == cmpLen) cmpLen++; // Keep the first '/'.
if(cmpLen < 512)
{
if(cmpLen < strlen(lastDir) || strncmp(lastDir, romAndSavePath, cmpLen) != 0)
{
strncpy(lastDir, romAndSavePath, cmpLen);
lastDir[cmpLen] = '\0';
res = fsQuickWrite("lastdir.txt", lastDir, cmpLen + 1);
}
}
// Save selected path if any rom was selected
if (romAndSavePath[0] != '\0')
res = fsQuickWrite("lastdir.txt", romAndSavePath, 512);
} while(0);

free(lastDir);
Expand Down