From 9051abec6d88d80cda7d8aa6cb4e69237169a054 Mon Sep 17 00:00:00 2001 From: Stephen White Date: Tue, 6 Nov 2018 19:23:48 +1100 Subject: [PATCH] FB and @CP can now be used to switch between SD and USB devices. --- src/FileBrowser.cpp | 88 ++++++++++------ src/FileBrowser.h | 4 + src/iec_commands.cpp | 235 ++++++++++++++++++++++++++++--------------- src/main.cpp | 6 +- 4 files changed, 221 insertions(+), 112 deletions(-) diff --git a/src/FileBrowser.cpp b/src/FileBrowser.cpp index c83096a..25829a5 100644 --- a/src/FileBrowser.cpp +++ b/src/FileBrowser.cpp @@ -21,6 +21,7 @@ #include #include #include +#include #include "debug.h" #include "options.h" #include "InputMappings.h" @@ -553,6 +554,53 @@ struct greater } }; +void FileBrowser::RefreshDevicesEntries(std::vector& entries, bool toLower) +{ + FileBrowser::BrowsableList::Entry entry; + char label[1024]; + DWORD vsn; + f_getlabel("SD:", label, &vsn); + + if (strlen(label) > 0) + sprintf(entry.filImage.fname, "SD: %s", label); + else + sprintf(entry.filImage.fname, "SD:"); + if (toLower) + { + for (int i = 0; entry.filImage.fname[i]; i++) + { + entry.filImage.fname[i] = tolower(entry.filImage.fname[i]); + } + } + entry.filImage.fattrib |= AM_DIR; + entry.filIcon.fname[0] = 0; + entries.push_back(entry); + + for (int USBDriveIndex = 0; USBDriveIndex < numberOfUSBMassStorageDevices; ++USBDriveIndex) + { + char USBDriveId[16]; + sprintf(USBDriveId, "USB%02d:", USBDriveIndex + 1); + + f_getlabel(USBDriveId, label, &vsn); + + if (strlen(label) > 0) + sprintf(entry.filImage.fname, "%s %s", USBDriveId, label); + else + strcpy(entry.filImage.fname, USBDriveId); + + if (toLower) + { + for (int i = 0; entry.filImage.fname[i]; i++) + { + entry.filImage.fname[i] = tolower(entry.filImage.fname[i]); + } + } + entry.filImage.fattrib |= AM_DIR; + entry.filIcon.fname[0] = 0; + entries.push_back(entry); + } +} + void FileBrowser::RefreshFolderEntries() { DIR dir; @@ -563,34 +611,7 @@ void FileBrowser::RefreshFolderEntries() folder.Clear(); if (displayingDevices) { - char label[1024]; - DWORD vsn; - f_getlabel("SD:", label, &vsn); - - if (strlen(label) > 0) - sprintf(entry.filImage.fname, "SD: %s", label); - else - sprintf(entry.filImage.fname, "SD:"); - entry.filImage.fattrib |= AM_DIR; - entry.filIcon.fname[0] = 0; - folder.entries.push_back(entry); - - for (int USBDriveIndex = 0; USBDriveIndex < numberOfUSBMassStorageDevices; ++USBDriveIndex) - { - char USBDriveId[16]; - sprintf(USBDriveId, "USB%02d:", USBDriveIndex + 1); - - f_getlabel(USBDriveId, label, &vsn); - - if (strlen(label) > 0) - sprintf(entry.filImage.fname, "%s %s", USBDriveId, label); - else - strcpy(entry.filImage.fname, USBDriveId); - - entry.filImage.fattrib |= AM_DIR; - entry.filIcon.fname[0] = 0; - folder.entries.push_back(entry); - } + FileBrowser::RefreshDevicesEntries(folder.entries, false); } else { @@ -663,6 +684,13 @@ void FileBrowser::DisplayRoot() FolderChanged(); } +void FileBrowser::DeviceSwitched() +{ + displayingDevices = false; + m_IEC_Commands.SetDisplayingDevices(displayingDevices); + FolderChanged(); +} + /* void FileBrowser::RefeshDisplayForBrowsableList(FileBrowser::BrowsableList* browsableList, int xOffset, bool showSelected) { @@ -867,9 +895,11 @@ void FileBrowser::PopFolder() if (f_getcwd(buffer, 1024) == FR_OK) { int deviceRoot = IsAtRootOfDevice(); + DEBUG_LOG("deviceRoot = %d\r\n", deviceRoot); if (deviceRoot >= 0) { displayingDevices = true; + m_IEC_Commands.SetDisplayingDevices(displayingDevices); RefreshFolderEntries(); folder.currentIndex = deviceRoot; folder.SetCurrent(); @@ -1058,6 +1088,7 @@ void FileBrowser::UpdateInputFolders() { SwitchDrive("SD:"); displayingDevices = false; + m_IEC_Commands.SetDisplayingDevices(displayingDevices); RefreshFolderEntries(); } else @@ -1071,6 +1102,7 @@ void FileBrowser::UpdateInputFolders() { SwitchDrive(USBDriveId); displayingDevices = false; + m_IEC_Commands.SetDisplayingDevices(displayingDevices); RefreshFolderEntries(); } } diff --git a/src/FileBrowser.h b/src/FileBrowser.h index 5d28372..97f44e6 100644 --- a/src/FileBrowser.h +++ b/src/FileBrowser.h @@ -198,11 +198,15 @@ public: static u32 Colour(int index); + static void RefreshDevicesEntries(std::vector& entries, bool toLower); + bool MakeLST(const char* filenameLST); bool SelectLST(const char* filenameLST); void SetScrollHighlightRate(float value) { scrollHighlightRate = value; } + void DeviceSwitched(); + private: void DisplayPNG(FILINFO& filIcon, int x, int y); void RefreshFolderEntries(); diff --git a/src/iec_commands.cpp b/src/iec_commands.cpp index c20a365..4a91d6c 100644 --- a/src/iec_commands.cpp +++ b/src/iec_commands.cpp @@ -770,102 +770,129 @@ void IEC_Commands::CD(int partition, char* filename) } else { - DIR dir; - FILINFO filInfo; - - char path[256] = { 0 }; - char* pattern = strrchr(filenameEdited, '\\'); - - if (pattern) + if (displayingDevices) { - // Now we look for a folder - int len = pattern - filenameEdited; - strncpy(path, filenameEdited, len); - - pattern++; - - if ((f_stat(path, &filInfo) != FR_OK) || !IsDirectory(filInfo)) + if (strncmp(filename, "SD", 2) == 0) { - Error(ERROR_62_FILE_NOT_FOUND); + SwitchDrive("SD:"); + displayingDevices = false; + updateAction = DECIVE_SWITCHED; } else { - char cwd[1024]; - if (f_getcwd(cwd, 1024) == FR_OK) + for (int USBDriveIndex = 0; USBDriveIndex < numberOfUSBMassStorageDevices; ++USBDriveIndex) { - f_chdir(path); + char USBDriveId[16]; + sprintf(USBDriveId, "USB%02d:", USBDriveIndex + 1); - char cwd2[1024]; - f_getcwd(cwd2, 1024); - - bool found = f_findfirst(&dir, &filInfo, ".", pattern) == FR_OK && filInfo.fname[0] != 0; - - //DEBUG_LOG("%s pattern = %s\r\n", filInfo.fname, pattern); - - if (found) + if (strncmp(filename, USBDriveId, 5) == 0) { - if (DiskImage::IsDiskImageExtention(filInfo.fname)) - { - if (f_stat(filInfo.fname, &filInfoSelectedImage) == FR_OK) - { - strcpy((char*)selectedImageName, filInfo.fname); - } - else - { - f_chdir(cwd); - Error(ERROR_62_FILE_NOT_FOUND); - } - } - else - { - //DEBUG_LOG("attemting changing dir %s\r\n", filInfo.fname); - if (f_chdir(filInfo.fname) != FR_OK) - { - Error(ERROR_62_FILE_NOT_FOUND); - f_chdir(cwd); - } - else - { - updateAction = DIR_PUSHED; - } - } + SwitchDrive(USBDriveId); + displayingDevices = false; + updateAction = DECIVE_SWITCHED; } - else - { - Error(ERROR_62_FILE_NOT_FOUND); - f_chdir(cwd); - } - } - //if (f_getcwd(cwd, 1024) == FR_OK) - // DEBUG_LOG("CWD on exit = %s\r\n", cwd); } } else { - bool found = FindFirst(dir, filenameEdited, filInfo); + DIR dir; + FILINFO filInfo; - if (found) + char path[256] = { 0 }; + char* pattern = strrchr(filenameEdited, '\\'); + + if (pattern) { - if (DiskImage::IsDiskImageExtention(filInfo.fname)) + // Now we look for a folder + int len = pattern - filenameEdited; + strncpy(path, filenameEdited, len); + + pattern++; + + if ((f_stat(path, &filInfo) != FR_OK) || !IsDirectory(filInfo)) { - if (f_stat(filInfo.fname, &filInfoSelectedImage) == FR_OK) - strcpy((char*)selectedImageName, filInfo.fname); - else - Error(ERROR_62_FILE_NOT_FOUND); + Error(ERROR_62_FILE_NOT_FOUND); } else { - //DEBUG_LOG("attemting changing dir %s\r\n", filInfo.fname); - if (f_chdir(filInfo.fname) != FR_OK) - Error(ERROR_62_FILE_NOT_FOUND); - else - updateAction = DIR_PUSHED; + char cwd[1024]; + if (f_getcwd(cwd, 1024) == FR_OK) + { + f_chdir(path); + + char cwd2[1024]; + f_getcwd(cwd2, 1024); + + bool found = f_findfirst(&dir, &filInfo, ".", pattern) == FR_OK && filInfo.fname[0] != 0; + + //DEBUG_LOG("%s pattern = %s\r\n", filInfo.fname, pattern); + + if (found) + { + if (DiskImage::IsDiskImageExtention(filInfo.fname)) + { + if (f_stat(filInfo.fname, &filInfoSelectedImage) == FR_OK) + { + strcpy((char*)selectedImageName, filInfo.fname); + } + else + { + f_chdir(cwd); + Error(ERROR_62_FILE_NOT_FOUND); + } + } + else + { + //DEBUG_LOG("attemting changing dir %s\r\n", filInfo.fname); + if (f_chdir(filInfo.fname) != FR_OK) + { + Error(ERROR_62_FILE_NOT_FOUND); + f_chdir(cwd); + } + else + { + updateAction = DIR_PUSHED; + } + } + } + else + { + Error(ERROR_62_FILE_NOT_FOUND); + f_chdir(cwd); + } + + } + //if (f_getcwd(cwd, 1024) == FR_OK) + // DEBUG_LOG("CWD on exit = %s\r\n", cwd); } } else { - Error(ERROR_62_FILE_NOT_FOUND); + bool found = FindFirst(dir, filenameEdited, filInfo); + + if (found) + { + if (DiskImage::IsDiskImageExtention(filInfo.fname)) + { + if (f_stat(filInfo.fname, &filInfoSelectedImage) == FR_OK) + strcpy((char*)selectedImageName, filInfo.fname); + else + Error(ERROR_62_FILE_NOT_FOUND); + } + else + { + //DEBUG_LOG("attemting changing dir %s\r\n", filInfo.fname); + if (f_chdir(filInfo.fname) != FR_OK) + Error(ERROR_62_FILE_NOT_FOUND); + else + updateAction = DIR_PUSHED; + } + } + else + { + Error(ERROR_62_FILE_NOT_FOUND); + } } } } @@ -1040,6 +1067,41 @@ void IEC_Commands::Copy(void) Error(ERROR_34_SYNTAX_ERROR); } } + +void IEC_Commands::ChangeDevice(void) +{ + Channel& channel = channels[15]; + const char* text = (char*)channel.buffer; + + if (strlen(text) > 2) + { + int deviceIndex = atoi(text + 2); + + if (deviceIndex == 0) + { + SwitchDrive("SD:"); + displayingDevices = false; + updateAction = DECIVE_SWITCHED; + } + else if ((deviceIndex - 1) < numberOfUSBMassStorageDevices) + { + char USBDriveId[16]; + sprintf(USBDriveId, "USB%02d:", deviceIndex); + SwitchDrive(USBDriveId); + displayingDevices = false; + updateAction = DECIVE_SWITCHED; + } + else + { + Error(ERROR_74_DRlVE_NOT_READY); + } + } + else + { + Error(ERROR_31_SYNTAX_ERROR); + } +} + void IEC_Commands::Memory(void) { Channel& channel = channels[15]; @@ -1286,7 +1348,7 @@ void IEC_Commands::ProcessCommand(void) break; case 'C': if (channel.buffer[1] == 'P') - Error(ERROR_31_SYNTAX_ERROR); // Change Partition not implemented yet + ChangeDevice(); else Copy(); break; @@ -1628,19 +1690,26 @@ void IEC_Commands::LoadDirectory() FileBrowser::BrowsableList::Entry entry; std::vector entries; - res = f_opendir(&dir, "."); - if (res == FR_OK) + if (displayingDevices) { - do + FileBrowser::RefreshDevicesEntries(entries, true); + } + else + { + res = f_opendir(&dir, "."); + if (res == FR_OK) { - res = f_readdir(&dir, &entry.filImage); - ext = strrchr(entry.filImage.fname, '.'); - if (res == FR_OK && entry.filImage.fname[0] != 0 && !(ext && strcasecmp(ext, ".png") == 0)) - entries.push_back(entry); - } while (res == FR_OK && entry.filImage.fname[0] != 0); - f_closedir(&dir); + do + { + res = f_readdir(&dir, &entry.filImage); + ext = strrchr(entry.filImage.fname, '.'); + if (res == FR_OK && entry.filImage.fname[0] != 0 && !(ext && strcasecmp(ext, ".png") == 0)) + entries.push_back(entry); + } while (res == FR_OK && entry.filImage.fname[0] != 0); + f_closedir(&dir); - std::sort(entries.begin(), entries.end(), greater()); + std::sort(entries.begin(), entries.end(), greater()); + } } for (u32 i = 0; i < entries.size(); ++i) diff --git a/src/main.cpp b/src/main.cpp index 3afd264..05f23e1 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -47,7 +47,7 @@ extern "C" #include "ssd_logo.h" unsigned versionMajor = 1; -unsigned versionMinor = 16; +unsigned versionMinor = 17; // When the emulated CPU starts we execute the first million odd cycles in non-real-time (ie as fast as possible so the emulated 1541 becomes responsive to CBM-Browser asap) // During these cycles the CPU is executing the ROM self test routines (these do not need to be cycle accurate) @@ -1114,6 +1114,10 @@ void emulator() GlobalSetDeviceID( m_IEC_Commands.GetDeviceId() ); fileBrowser->ShowDeviceAndROM(); break; + case IEC_Commands::DECIVE_SWITCHED: + DEBUG_LOG("DECIVE_SWITCHED\r\n"); + fileBrowser->DeviceSwitched(); + break; default: break; }