From 97a99f70e35491022e4a59891a4cc76117a86a95 Mon Sep 17 00:00:00 2001 From: cold-brewed Date: Sat, 1 Oct 2022 17:39:03 -0400 Subject: [PATCH] qt: Add floppy support to media history manager --- src/config.c | 17 +++++++++++++++++ src/floppy/fdd.c | 1 + src/include/86box/fdd.h | 2 ++ src/qt/qt_mediahistorymanager.cpp | 14 ++++++++++++-- src/qt/qt_mediahistorymanager.hpp | 5 +++-- src/qt/qt_mediamenu.cpp | 22 +++++++++++++++++++++- src/qt/qt_mediamenu.hpp | 1 + src/qt/qt_platform.cpp | 6 ++++++ 8 files changed, 63 insertions(+), 5 deletions(-) diff --git a/src/config.c b/src/config.c index 6212871be..702f41fe4 100644 --- a/src/config.c +++ b/src/config.c @@ -1291,6 +1291,14 @@ load_floppy_and_cdrom_drives(void) sprintf(temp, "fdd_%02i_check_bpb", c + 1); ini_section_delete_var(cat, temp); } + for (int i = 0; i < MAX_PREV_IMAGES; i++) { + fdd_image_history[c][i] = (char *) calloc(MAX_IMAGE_PATH_LEN + 1, sizeof(char)); + sprintf(temp, "fdd_%02i_image_history_%02i", c + 1, i + 1); + p = ini_section_get_string(cat, temp, NULL); + if (p) { + sprintf(fdd_image_history[c][i], "%s", p); + } + } } memset(temp, 0x00, sizeof(temp)); @@ -2680,6 +2688,15 @@ save_floppy_and_cdrom_drives(void) ini_section_delete_var(cat, temp); else ini_section_set_int(cat, temp, fdd_get_check_bpb(c)); + + for (int i = 0; i < MAX_PREV_IMAGES; i++) { + sprintf(temp, "fdd_%02i_image_history_%02i", c + 1, i + 1); + if ((fdd_image_history[c][i] == 0) || strlen(fdd_image_history[c][i]) == 0) { + ini_section_delete_var(cat, temp); + } else { + ini_section_set_string(cat, temp, fdd_image_history[c][i]); + } + } } for (c = 0; c < CDROM_NUM; c++) { diff --git a/src/floppy/fdd.c b/src/floppy/fdd.c index 65b95bb60..8ab8c315a 100644 --- a/src/floppy/fdd.c +++ b/src/floppy/fdd.c @@ -76,6 +76,7 @@ typedef struct { fdd_t fdd[FDD_NUM]; char floppyfns[FDD_NUM][512]; +char *fdd_image_history[FDD_NUM][FLOPPY_IMAGE_HISTORY]; pc_timer_t fdd_poll_time[FDD_NUM]; diff --git a/src/include/86box/fdd.h b/src/include/86box/fdd.h index 525c50d00..92efd9fd5 100644 --- a/src/include/86box/fdd.h +++ b/src/include/86box/fdd.h @@ -22,6 +22,7 @@ #define EMU_FDD_H #define FDD_NUM 4 +#define FLOPPY_IMAGE_HISTORY 4 #define SEEK_RECALIBRATE -999 #ifdef __cplusplus @@ -83,6 +84,7 @@ typedef struct { extern DRIVE drives[FDD_NUM]; extern char floppyfns[FDD_NUM][512]; +extern char *fdd_image_history[FDD_NUM][FLOPPY_IMAGE_HISTORY]; extern pc_timer_t fdd_poll_time[FDD_NUM]; extern int ui_writeprot[FDD_NUM]; diff --git a/src/qt/qt_mediahistorymanager.cpp b/src/qt/qt_mediahistorymanager.cpp index 884a13de5..19025d210 100644 --- a/src/qt/qt_mediahistorymanager.cpp +++ b/src/qt/qt_mediahistorymanager.cpp @@ -21,10 +21,15 @@ #include #include #include - -#include "86box/cdrom.h" #include "qt_mediahistorymanager.hpp" +extern "C" +{ +#include <86box/timer.h> +#include <86box/cdrom.h> +#include <86box/fdd.h> +} + namespace ui { MediaHistoryManager::MediaHistoryManager() { @@ -158,6 +163,9 @@ void MediaHistoryManager::initialDeduplication() case ui::MediaType::Optical: current_image = cdrom[device_index].image_path; break; + case ui::MediaType::Floppy: + current_image = floppyfns[device_index]; + break; default: continue; break; @@ -180,6 +188,8 @@ char ** MediaHistoryManager::getEmuHistoryVarForType(ui::MediaType type, int ind switch (type) { case ui::MediaType::Optical: return &cdrom[index].image_history[0]; + case ui::MediaType::Floppy: + return &fdd_image_history[index][0]; default: return nullptr; diff --git a/src/qt/qt_mediahistorymanager.hpp b/src/qt/qt_mediahistorymanager.hpp index 0a69aa100..c628ce793 100644 --- a/src/qt/qt_mediahistorymanager.hpp +++ b/src/qt/qt_mediahistorymanager.hpp @@ -59,7 +59,8 @@ namespace ui { // Used to iterate over all supported types when preparing data structures // Also useful to indicate which types support history static const MediaType AllSupportedMediaHistoryTypes[] = { - MediaType::Optical + MediaType::Optical, + MediaType::Floppy, }; class MediaHistoryManager { @@ -87,7 +88,7 @@ namespace ui { // Main hash of hash of vector of strings master_list_t master_list; - const master_list_t &getMasterList() const; + [[nodiscard]] const master_list_t &getMasterList() const; void setMasterList(const master_list_t &masterList); device_index_list_t index_list, empty_device_index_list; diff --git a/src/qt/qt_mediamenu.cpp b/src/qt/qt_mediamenu.cpp index ba52074cf..664cdaab2 100644 --- a/src/qt/qt_mediamenu.cpp +++ b/src/qt/qt_mediamenu.cpp @@ -105,6 +105,11 @@ void MediaMenu::refresh(QMenu *parentMenu) { menu->addAction(tr("&Existing image..."), [this, i]() { floppySelectImage(i, false); }); menu->addAction(tr("Existing image (&Write-protected)..."), [this, i]() { floppySelectImage(i, true); }); menu->addSeparator(); + for (int slot = 0; slot < MAX_PREV_IMAGES; slot++) { + floppyImageHistoryPos[slot] = menu->children().count(); + menu->addAction(QString::asprintf(tr("Image %i").toUtf8().constData(), slot), [this, i, slot]() { floppyMenuSelect(i, slot); })->setCheckable(false); + } + menu->addSeparator(); floppyExportPos = menu->children().count(); menu->addAction(tr("E&xport to 86F..."), [this, i]() { floppyExportTo86f(i); }); menu->addSeparator(); @@ -328,6 +333,7 @@ void MediaMenu::floppySelectImage(int i, bool wp) { } void MediaMenu::floppyMount(int i, const QString &filename, bool wp) { + auto previous_image = QFileInfo(floppyfns[i]); fdd_close(i); ui_writeprot[i] = wp ? 1 : 0; if (! filename.isEmpty()) { @@ -335,12 +341,14 @@ void MediaMenu::floppyMount(int i, const QString &filename, bool wp) { fdd_load(i, filenameBytes.data()); } ui_sb_update_icon_state(SB_FLOPPY | i, filename.isEmpty() ? 1 : 0); + mhm.addImageToHistory(i, ui::MediaType::Floppy, previous_image.filePath(), filename); floppyUpdateMenu(i); ui_sb_update_tip(SB_FLOPPY | i); config_save(); } void MediaMenu::floppyEject(int i) { + mhm.addImageToHistory(i, ui::MediaType::Floppy, floppyfns[i], QString()); fdd_close(i); ui_sb_update_icon_state(SB_FLOPPY | i, 1); floppyUpdateMenu(i); @@ -376,11 +384,22 @@ void MediaMenu::floppyUpdateMenu(int i) { ejectMenu->setText(QString::asprintf(tr("Eject %s").toUtf8().constData(), name.isEmpty() ? QString().toUtf8().constData() : fi.fileName().toUtf8().constData())); exportMenu->setEnabled(!name.isEmpty()); + for (int slot = 0; slot < MAX_PREV_IMAGES; slot++) { + updateImageHistory(i, slot, ui::MediaType::Floppy); + } + int type = fdd_get_type(i); //floppyMenus[i]->setTitle(tr("Floppy %1 (%2): %3").arg(QString::number(i+1), fdd_getname(type), name.isEmpty() ? tr("(empty)") : name)); floppyMenus[i]->setTitle(QString::asprintf(tr("Floppy %i (%s): %ls").toUtf8().constData(), i + 1, fdd_getname(type), name.isEmpty() ? tr("(empty)").toStdU16String().data() : name.toStdU16String().data())); } +void MediaMenu::floppyMenuSelect(int index, int slot) { + QString filename = mhm.getImageForSlot(index, slot, ui::MediaType::Floppy); + floppyMount(index, filename.toUtf8().constData(), false); + floppyUpdateMenu(index); + ui_sb_update_tip(SB_FLOPPY | index); +} + void MediaMenu::cdromMute(int i) { cdrom[i].sound_on ^= 1; config_save(); @@ -501,8 +520,9 @@ void MediaMenu::cdromUpdateMenu(int i) { imageMenu->setEnabled(!name.isEmpty()); imageMenu->setText(QString::asprintf(tr("Eject %s").toUtf8().constData(), name.isEmpty() ? QString().toUtf8().constData() : fi.fileName().toUtf8().constData())); - for (int slot = 0; slot < MAX_PREV_IMAGES; slot++) + for (int slot = 0; slot < MAX_PREV_IMAGES; slot++) { updateImageHistory(i, slot, ui::MediaType::Optical); + } QString busName = tr("Unknown Bus"); switch (cdrom[i].bus_type) { diff --git a/src/qt/qt_mediamenu.hpp b/src/qt/qt_mediamenu.hpp index 4503c1b93..870f57a95 100644 --- a/src/qt/qt_mediamenu.hpp +++ b/src/qt/qt_mediamenu.hpp @@ -37,6 +37,7 @@ public: void floppySelectImage(int i, bool wp); void floppyMount(int i, const QString& filename, bool wp); void floppyEject(int i); + void floppyMenuSelect(int index, int slot); void floppyExportTo86f(int i); void floppyUpdateMenu(int i); diff --git a/src/qt/qt_platform.cpp b/src/qt/qt_platform.cpp index 019d38cf4..f7048bee7 100644 --- a/src/qt/qt_platform.cpp +++ b/src/qt/qt_platform.cpp @@ -161,7 +161,13 @@ plat_timer_read(void) FILE * plat_fopen(const char *path, const char *mode) { +#if defined(Q_OS_MACOS) or defined(Q_OS_LINUX) + QFileInfo fi(path); + QString filename = fi.isRelative() ? usr_path + fi.filePath() : fi.filePath(); + return fopen(filename.toUtf8().constData(), mode); +#else return fopen(QString::fromUtf8(path).toLocal8Bit(), mode); +#endif } FILE *