From 7cf1ffeb68a55ad271cd6208c1386a10583df3b0 Mon Sep 17 00:00:00 2001 From: TC1995 Date: Thu, 7 Dec 2023 20:30:48 +0100 Subject: [PATCH 1/7] Initial stub for CD-ROM passthrough (currently Windows only). --- src/cdrom/cdrom_ioctl.c | 221 ++++++++++++++++++++++++++++++ src/win/win_cdrom_ioctl.c | 275 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 496 insertions(+) create mode 100644 src/cdrom/cdrom_ioctl.c create mode 100644 src/win/win_cdrom_ioctl.c diff --git a/src/cdrom/cdrom_ioctl.c b/src/cdrom/cdrom_ioctl.c new file mode 100644 index 000000000..382465637 --- /dev/null +++ b/src/cdrom/cdrom_ioctl.c @@ -0,0 +1,221 @@ +/* + * 86Box A hypervisor and IBM PC system emulator that specializes in + * running old operating systems and software designed for IBM + * PC systems and compatibles from 1981 through fairly recent + * system designs based on the PCI bus. + * + * This file is part of the 86Box distribution. + * + * CD-ROM passthrough support. + * + * + * + * Authors: TheCollector1995, , + * Miran Grca, + * + * Copyright 2023 TheCollector1995. + * Copyright 2023 Miran Grca. + */ +#include +#include +#include +#include +#include +#include +#include +#define HAVE_STDARG_H +#include <86box/86box.h> +#include <86box/config.h> +#include <86box/path.h> +#include <86box/plat.h> +#include <86box/plat_cdrom.h> +#include <86box/scsi_device.h> +#include <86box/cdrom.h> + +#ifdef ENABLE_CDROM_IOCTL_LOG +int cdrom_ioctl_do_log = ENABLE_CDROM_IOCTL_LOG; + +void +cdrom_ioctl_log(const char *fmt, ...) +{ + va_list ap; + + if (cdrom_ioctl_do_log) { + va_start(ap, fmt); + pclog_ex(fmt, ap); + va_end(ap); + } +} +#else +# define cdrom_ioctl_log(fmt, ...) +#endif + +/* The addresses sent from the guest are absolute, ie. a LBA of 0 corresponds to a MSF of 00:00:00. Otherwise, the counter displayed by the guest is wrong: + there is a seeming 2 seconds in which audio plays but counter does not move, while a data track before audio jumps to 2 seconds before the actual start + of the audio while audio still plays. With an absolute conversion, the counter is fine. */ +#define MSFtoLBA(m, s, f) ((((m * 60) + s) * 75) + f) + +static void +cdrom_ioctl_get_tracks(UNUSED(cdrom_t *dev), int *first, int *last) +{ + TMSF tmsf; + + plat_cdrom_get_audio_tracks(first, last, &tmsf); +} + +static void +cdrom_ioctl_get_track_info(UNUSED(cdrom_t *dev), uint32_t track, int end, track_info_t *ti) +{ + TMSF tmsf; + + plat_cdrom_get_audio_track_info(end, track, &ti->number, &tmsf, &ti->attr); + + ti->m = tmsf.min; + ti->s = tmsf.sec; + ti->f = tmsf.fr; +} + +static void +cdrom_ioctl_get_subchannel(UNUSED(cdrom_t *dev), uint32_t lba, subchannel_t *subc) +{ + TMSF rel_pos; + TMSF abs_pos; + + plat_cdrom_get_audio_sub(lba, &subc->attr, &subc->track, &subc->index, + &rel_pos, &abs_pos); + + subc->abs_m = abs_pos.min; + subc->abs_s = abs_pos.sec; + subc->abs_f = abs_pos.fr; + + subc->rel_m = rel_pos.min; + subc->rel_s = rel_pos.sec; + subc->rel_f = rel_pos.fr; +} + +static int +cdrom_ioctl_is_track_audio(uint32_t pos, int ismsf) +{ + uint8_t attr; + int m; + int s; + int f; + int track; + + if (ismsf) { + m = (pos >> 16) & 0xff; + s = (pos >> 8) & 0xff; + f = pos & 0xff; + pos = MSFtoLBA(m, s, f) - 150; + } + + /* GetTrack requires LBA. */ + return plat_cdrom_get_audio_track(pos); +} + +static int +cdrom_ioctl_sector_size(UNUSED(cdrom_t *dev), UNUSED(uint32_t lba)) +{ + return plat_get_sector_size(); +} + +static int +cdrom_ioctl_read_sector(UNUSED(cdrom_t *dev), int type, uint8_t *b, uint32_t lba) +{ + switch (type) { + case CD_READ_DATA: + return plat_cdrom_read_sector(b, 0, lba); + case CD_READ_AUDIO: + return plat_cdrom_read_sector(b, 1, lba); + case CD_READ_RAW: + if (plat_get_sector_size() == 2352) + return plat_cdrom_read_sector(b, 1, lba); + else + return plat_cdrom_read_sector(b, 0, lba); + break; + default: + cdrom_ioctl_log("cdrom_ioctl_read_sector(): Unknown CD read type.\n"); + return 0; + } +} + +static int +cdrom_ioctl_track_type(UNUSED(cdrom_t *dev), uint32_t lba) +{ + if (cdrom_ioctl_is_track_audio(lba, 0)) + return CD_TRACK_AUDIO; + + return 0; +} + +static void +cdrom_ioctl_exit(cdrom_t *dev) +{ + dev->cd_status = CD_STATUS_EMPTY; + + plat_cdrom_exit(); + + dev->ops = NULL; +} + +static const cdrom_ops_t cdrom_ioctl_ops = { + cdrom_ioctl_get_tracks, + cdrom_ioctl_get_track_info, + cdrom_ioctl_get_subchannel, + NULL, + cdrom_ioctl_sector_size, + cdrom_ioctl_read_sector, + cdrom_ioctl_track_type, + cdrom_ioctl_exit +}; + +void +cdrom_ioctl_eject(void) +{ + plat_cdrom_eject(); +} + +void +cdrom_ioctl_load(void) +{ + plat_cdrom_load(); +} + +static int +cdrom_ioctl_open_abort(cdrom_t *dev) +{ + if (dev && dev->ops && dev->ops->exit) + dev->ops->exit(dev); + + dev->ops = NULL; + dev->host_drive = 0; + dev->ioctl_path[0] = 0; + return 1; +} + +int +cdrom_ioctl_open(cdrom_t *dev, const char *path) +{ + /* Open the drive. */ + if (plat_cdrom_open()) + return cdrom_ioctl_open_abort(dev); + + /* Make sure to not STRCPY if the two are pointing + at the same place. */ + if (path != dev->ioctl_path) + strcpy(dev->ioctl_path, path); + + /* All good, reset state. */ + dev->cd_status = CD_STATUS_STOPPED; + dev->is_dir = 0; + dev->seek_pos = 0; + dev->cd_buflen = 0; + plat_cdrom_reset(); + dev->cdrom_capacity = plat_cdrom_get_capacity(); + pclog("CD-ROM capacity: %i sectors (%" PRIi64 " bytes)\n", dev->cdrom_capacity, ((uint64_t) dev->cdrom_capacity) << 11ULL); + + /* Attach this handler to the drive. */ + dev->ops = &cdrom_ioctl_ops; + + return 0; +} diff --git a/src/win/win_cdrom_ioctl.c b/src/win/win_cdrom_ioctl.c new file mode 100644 index 000000000..9eee9c789 --- /dev/null +++ b/src/win/win_cdrom_ioctl.c @@ -0,0 +1,275 @@ +/* + * 86Box A hypervisor and IBM PC system emulator that specializes in + * running old operating systems and software designed for IBM + * PC systems and compatibles from 1981 through fairly recent + * system designs based on the PCI bus. + * + * This file is part of the 86Box distribution. + * + * Win32 CD-ROM support via IOCTL. + * + * + * + * Authors: TheCollector1995, , + * Miran Grca, + * + * Copyright 2023 TheCollector1995. + * Copyright 2023 Miran Grca. + */ +#define UNICODE +#define BITMAP WINDOWS_BITMAP +#include +#include +#undef BITMAP +#include +#include "ntddcdrm.h" +#include "ntddscsi.h" +#include +#include +#include <86box/86box.h> +#include <86box/scsi_device.h> +#include <86box/cdrom.h> +#include <86box/plat_unused.h> +#include <86box/plat_cdrom.h> + +static const char ioctl_path[8]; +static HANDLE hIOCTL; +static CDROM_TOC toc; + +/* The addresses sent from the guest are absolute, ie. a LBA of 0 corresponds to a MSF of 00:00:00. Otherwise, the counter displayed by the guest is wrong: + there is a seeming 2 seconds in which audio plays but counter does not move, while a data track before audio jumps to 2 seconds before the actual start + of the audio while audio still plays. With an absolute conversion, the counter is fine. */ +#define MSFtoLBA(m, s, f) ((((m * 60) + s) * 75) + f) + +static int +plat_cdrom_get_track(uint32_t sector) +{ + int track = 0; + uint32_t track_addr; + + for (int i = toc.FirstTrack; i < toc.LastTrack; i++) { + /* There must be at least two tracks - data and lead out. */ + track_addr = MSFtoLBA(toc.TrackData[i].Address[1], toc.TrackData[i].Address[2], toc.TrackData[i].Address[3]); + if (track_addr <= sector) { + track = i; + } + } + + pclog("GetTrack = %d.\n", track); + return track; +} + +int +plat_cdrom_get_audio_track(uint32_t sector) +{ + int control = 0; + uint32_t track_addr; + + for (int i = 0; toc.TrackData[i].TrackNumber != 0xaa; i++) { + /* There must be at least two tracks - data and lead out. */ + track_addr = MSFtoLBA(toc.TrackData[i].Address[1], toc.TrackData[i].Address[2], toc.TrackData[i].Address[3]); + if ((toc.TrackData[i].TrackNumber >= toc.FirstTrack) && (toc.TrackData[i].TrackNumber <= toc.LastTrack) && + (track_addr >= sector)) { + control = toc.TrackData[i].Control; + break; + } + } + + return (control & 4) ? DATA_TRACK : AUDIO_TRACK; +} + +int +plat_cdrom_get_audio_sub(uint32_t sector, uint8_t *attr, uint8_t *track, uint8_t *index, TMSF *rel_pos, TMSF *abs_pos) +{ + CDROM_SUB_Q_DATA_FORMAT insub; + SUB_Q_CHANNEL_DATA sub; + DWORD size; + int pos = 0; + int cur_track = plat_cdrom_get_track(sector); + + insub.Format = IOCTL_CDROM_CURRENT_POSITION; + if (plat_cdrom_open()) + return 0; + DeviceIoControl(hIOCTL,IOCTL_CDROM_READ_Q_CHANNEL, &insub, sizeof(insub), &sub, sizeof(sub), &size, NULL); + plat_cdrom_exit(); + + *attr = sub.CurrentPosition.Control; + *track = (uint8_t)(cur_track + 1); + *index = sub.CurrentPosition.IndexNumber; + + FRAMES_TO_MSF(sector + 150, &abs_pos->min, &abs_pos->sec, &abs_pos->fr); + + /* Absolute position should be adjusted by 150, not the relative ones. */ + FRAMES_TO_MSF(sector - toc.FirstTrack, &rel_pos->min, &rel_pos->sec, &rel_pos->fr); + + return 1; +} + +int +plat_cdrom_get_audio_tracks(int *st_track, int *end, TMSF *lead_out) +{ + CDROM_TOC toc; + DWORD byteCount; + + *st_track = toc.FirstTrack; + *end = toc.LastTrack; + FRAMES_TO_MSF(toc.TrackData[*end].TrackNumber + 150, &lead_out->min, &lead_out->sec, &lead_out->fr); + + return 1; +} + +/* This replaces both Info and EndInfo, they are specified by a variable. */ +int +plat_cdrom_get_audio_track_info(int end, int track, int *track_num, TMSF *start, uint8_t *attr) +{ + int pos; + DWORD byteCount; + + pclog("plat_cdrom_get_audio_track_info(): start track = %d, last track = %d.\n", track, end); + + if ((track < 1) || (track > end)) + return 0; + + pos = toc.FirstTrack + 150; + + FRAMES_TO_MSF(pos, &start->min, &start->sec, &start->fr); + + *track_num = toc.TrackData[track - 1].TrackNumber; + *attr = toc.TrackData[track - 1].Control; + + return 1; +} + +uint32_t +plat_get_sector_size(void) +{ + DISK_GEOMETRY dgCDROM; + DWORD size; + + if (plat_cdrom_open()) + return 0; + DeviceIoControl(hIOCTL, IOCTL_CDROM_GET_DRIVE_GEOMETRY, NULL, 0, &dgCDROM, sizeof(DISK_GEOMETRY), &size, NULL); + plat_cdrom_exit(); + + if (dgCDROM.MediaType != 11) // Removable Media Check + return 0; + + return dgCDROM.BytesPerSector; +} + +int +plat_cdrom_read_sector(uint8_t *buffer, int raw, uint32_t sector) +{ + int ret; + LARGE_INTEGER pos; + RAW_READ_INFO in; + DWORD byteCount; + pclog("plat_cdrom_read_sector(): raw? = %d, sector = %02x.\n", raw, sector); + + if (raw) { + in.TrackMode = CDDA; + in.SectorCount = 1; + in.DiskOffset.QuadPart = sector * RAW_SECTOR_SIZE; + if (plat_cdrom_open()) + return 0; + ret = DeviceIoControl(hIOCTL, IOCTL_CDROM_RAW_READ, &in, sizeof(RAW_READ_INFO), buffer, RAW_SECTOR_SIZE, &byteCount, NULL); + plat_cdrom_exit(); + return ret; + } else { + pos.QuadPart = sector * COOKED_SECTOR_SIZE; + if (plat_cdrom_open()) + return 0; + SetFilePointer(hIOCTL, pos.LowPart, &pos.HighPart, FILE_BEGIN); + ret = ReadFile(hIOCTL, buffer, COOKED_SECTOR_SIZE, &byteCount, NULL); + plat_cdrom_exit(); + pclog("plat_cdrom_read_sector(): ret = %x.\n", !ret); + return !ret; + } + return 0; +} + +uint32_t +plat_cdrom_get_capacity(void) +{ + DWORD size; + int c; + DISK_GEOMETRY dgCDROM; + uint32_t totals; + + if (plat_cdrom_open()) + return 0; + DeviceIoControl(hIOCTL, IOCTL_CDROM_GET_DRIVE_GEOMETRY, NULL, 0, &dgCDROM, sizeof(DISK_GEOMETRY), &size, NULL); + plat_cdrom_exit(); + + totals = dgCDROM.SectorsPerTrack * dgCDROM.TracksPerCylinder * dgCDROM.Cylinders.QuadPart; + + pclog("Total = %08x.\n", totals); + return totals; +} + +int +plat_cdrom_load(void) +{ + int ret; + DWORD size; + + if (plat_cdrom_open()) + return 0; + DeviceIoControl(hIOCTL, IOCTL_STORAGE_LOAD_MEDIA, NULL, 0, NULL, 0, &size, NULL); + plat_cdrom_exit(); + return 1; +} + +int +plat_cdrom_eject(void) +{ + int ret; + DWORD size; + + if (plat_cdrom_open()) + return 0; + ret = DeviceIoControl(hIOCTL, IOCTL_STORAGE_EJECT_MEDIA, NULL, 0, NULL, 0, &size, NULL); + plat_cdrom_exit(); + return ret; +} + +void +plat_cdrom_exit(void) +{ + if (hIOCTL) { + CloseHandle(hIOCTL); + hIOCTL = NULL; + } +} + +void +plat_cdrom_close(void) +{ + plat_cdrom_exit(); +} + +int +plat_cdrom_reset(void) +{ + CDROM_TOC ltoc; + DWORD size; + + if (plat_cdrom_open()) + return 0; + DeviceIoControl(hIOCTL, IOCTL_CDROM_READ_TOC, NULL, 0, <oc, sizeof(ltoc), &size, NULL); + plat_cdrom_exit(); + + toc = ltoc; + return 1; +} + +int +plat_cdrom_open(void) +{ + plat_cdrom_exit(); + hIOCTL = CreateFile((LPCWSTR)ioctl_path, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); + if (hIOCTL == NULL) + return 1; + + return 0; +} From 4fdb339407233905f12907147f2be024b8bc2128 Mon Sep 17 00:00:00 2001 From: TC1995 Date: Sat, 18 May 2024 19:42:00 +0200 Subject: [PATCH 2/7] Attempt #2 to implement CD-ROM passthrough. Still Windows only on the QT side. --- src/cdrom/CMakeLists.txt | 2 +- src/cdrom/cdrom.c | 28 +-- src/cdrom/cdrom_ioctl.c | 83 +++++---- src/config.c | 4 +- src/include/86box/cdrom.h | 4 +- src/include/86box/cdrom_ioctl.h | 32 ++++ src/include/86box/plat_cdrom.h | 66 +++++++ src/machine_status.c | 8 +- src/qt/qt_machinestatus.cpp | 2 +- src/qt/qt_mediamenu.cpp | 72 ++++++-- src/qt/qt_mediamenu.hpp | 2 + src/qt/win_cdrom_ioctl.c | 300 +++++++++++++------------------- 12 files changed, 356 insertions(+), 247 deletions(-) create mode 100644 src/include/86box/cdrom_ioctl.h create mode 100644 src/include/86box/plat_cdrom.h diff --git a/src/cdrom/CMakeLists.txt b/src/cdrom/CMakeLists.txt index 201cee7f6..d3b38095e 100644 --- a/src/cdrom/CMakeLists.txt +++ b/src/cdrom/CMakeLists.txt @@ -13,4 +13,4 @@ # Copyright 2020-2021 David Hrdlička. # -add_library(cdrom OBJECT cdrom.c cdrom_image_backend.c cdrom_image_viso.c cdrom_image.c cdrom_mitsumi.c) +add_library(cdrom OBJECT cdrom.c cdrom_image_backend.c cdrom_image_viso.c cdrom_image.c cdrom_ioctl.c cdrom_mitsumi.c) diff --git a/src/cdrom/cdrom.c b/src/cdrom/cdrom.c index e4e76ea66..06494e4a9 100644 --- a/src/cdrom/cdrom.c +++ b/src/cdrom/cdrom.c @@ -1972,7 +1972,8 @@ cdrom_hard_reset(void) #endif cdrom_image_open(dev, dev->image_path); - } + } else if (dev->host_drive == 201) + cdrom_ioctl_open(dev, dev->image_path, dev->letter); } } @@ -2028,7 +2029,7 @@ cdrom_eject(uint8_t id) if (dev->host_drive == 200) strcpy(dev->prev_image_path, dev->image_path); - dev->prev_host_drive = dev->host_drive; + dev->prev_host_drive = dev->host_drive + (dev->host ? 1 : 0); dev->host_drive = 0; dev->ops->exit(dev); @@ -2058,26 +2059,33 @@ cdrom_reload(uint8_t id) dev->ops = NULL; memset(dev->image_path, 0, sizeof(dev->image_path)); - if (dev->prev_host_drive == 200) { + if (dev->prev_host_drive >= 200) { /* Reload a previous image. */ strcpy(dev->image_path, dev->prev_image_path); #ifdef _WIN32 - if ((strlen(dev->image_path) >= 1) && (dev->image_path[strlen(dev->image_path) - 1] == '/')) - dev->image_path[strlen(dev->image_path) - 1] = '\\'; + if (dev->prev_host_drive == 200) { + if ((strlen(dev->image_path) >= 1) && (dev->image_path[strlen(dev->image_path) - 1] == '/')) + dev->image_path[strlen(dev->image_path) - 1] = '\\'; + } #else - if ((strlen(dev->image_path) >= 1) && (dev->image_path[strlen(dev->image_path) - 1] == '\\')) - dev->image_path[strlen(dev->image_path) - 1] = '/'; + if (dev->prev_host_drive == 200) { + if ((strlen(dev->image_path) >= 1) && (dev->image_path[strlen(dev->image_path) - 1] == '\\')) + dev->image_path[strlen(dev->image_path) - 1] = '/'; + } #endif - cdrom_image_open(dev, dev->image_path); + if (dev->prev_host_drive > 200) + cdrom_ioctl_open(dev, dev->image_path, dev->letter); + else + cdrom_image_open(dev, dev->image_path); cdrom_insert(id); - if (strlen(dev->image_path) == 0) + if ((strlen(dev->image_path) == 0) && !dev->letter) dev->host_drive = 0; else - dev->host_drive = 200; + dev->host_drive = 200 + (dev->host ? 1 : 0); } plat_cdrom_ui_update(id, 1); diff --git a/src/cdrom/cdrom_ioctl.c b/src/cdrom/cdrom_ioctl.c index 382465637..861c46eac 100644 --- a/src/cdrom/cdrom_ioctl.c +++ b/src/cdrom/cdrom_ioctl.c @@ -58,7 +58,7 @@ cdrom_ioctl_log(const char *fmt, ...) static void cdrom_ioctl_get_tracks(UNUSED(cdrom_t *dev), int *first, int *last) { - TMSF tmsf; + TMSF tmsf; plat_cdrom_get_audio_tracks(first, last, &tmsf); } @@ -94,14 +94,29 @@ cdrom_ioctl_get_subchannel(UNUSED(cdrom_t *dev), uint32_t lba, subchannel_t *sub } static int -cdrom_ioctl_is_track_audio(uint32_t pos, int ismsf) +cdrom_ioctl_get_capacity(UNUSED(cdrom_t *dev)) +{ + int ret; + + ret = plat_cdrom_get_last_block(); + pclog("GetCapacity=%x.\n", ret); + return ret; +} + +static int +cdrom_ioctl_is_track_audio(cdrom_t *dev, uint32_t pos, int ismsf) { uint8_t attr; + TMSF tmsf; int m; int s; int f; + int number; int track; + if (dev->cd_status == CD_STATUS_DATA_ONLY) + return 0; + if (ismsf) { m = (pos >> 16) & 0xff; s = (pos >> 8) & 0xff; @@ -110,13 +125,20 @@ cdrom_ioctl_is_track_audio(uint32_t pos, int ismsf) } /* GetTrack requires LBA. */ - return plat_cdrom_get_audio_track(pos); + return plat_cdrom_is_track_audio(pos); } static int -cdrom_ioctl_sector_size(UNUSED(cdrom_t *dev), UNUSED(uint32_t lba)) +cdrom_ioctl_is_track_pre(UNUSED(cdrom_t *dev), UNUSED(uint32_t lba)) { - return plat_get_sector_size(); + return 0; +} + +static int +cdrom_ioctl_sector_size(UNUSED(cdrom_t *dev), uint32_t lba) +{ + pclog("LBA=%x.\n", lba); + return plat_cdrom_get_sector_size(lba); } static int @@ -128,21 +150,18 @@ cdrom_ioctl_read_sector(UNUSED(cdrom_t *dev), int type, uint8_t *b, uint32_t lba case CD_READ_AUDIO: return plat_cdrom_read_sector(b, 1, lba); case CD_READ_RAW: - if (plat_get_sector_size() == 2352) - return plat_cdrom_read_sector(b, 1, lba); - else - return plat_cdrom_read_sector(b, 0, lba); - break; + return plat_cdrom_read_sector(b, 1, lba); default: cdrom_ioctl_log("cdrom_ioctl_read_sector(): Unknown CD read type.\n"); - return 0; + break; } + return 0; } static int -cdrom_ioctl_track_type(UNUSED(cdrom_t *dev), uint32_t lba) +cdrom_ioctl_track_type(cdrom_t *dev, uint32_t lba) { - if (cdrom_ioctl_is_track_audio(lba, 0)) + if (cdrom_ioctl_is_track_audio(dev, lba, 0)) return CD_TRACK_AUDIO; return 0; @@ -151,9 +170,10 @@ cdrom_ioctl_track_type(UNUSED(cdrom_t *dev), uint32_t lba) static void cdrom_ioctl_exit(cdrom_t *dev) { + cdrom_ioctl_log("CDROM: ioctl_exit(%s)\n", dev->image_path); dev->cd_status = CD_STATUS_EMPTY; - plat_cdrom_exit(); + plat_cdrom_close(); dev->ops = NULL; } @@ -162,7 +182,7 @@ static const cdrom_ops_t cdrom_ioctl_ops = { cdrom_ioctl_get_tracks, cdrom_ioctl_get_track_info, cdrom_ioctl_get_subchannel, - NULL, + cdrom_ioctl_is_track_pre, cdrom_ioctl_sector_size, cdrom_ioctl_read_sector, cdrom_ioctl_track_type, @@ -170,48 +190,38 @@ static const cdrom_ops_t cdrom_ioctl_ops = { }; void -cdrom_ioctl_eject(void) +cdrom_ioctl_close(cdrom_t *dev) { - plat_cdrom_eject(); -} + cdrom_ioctl_log("CDROM: ioctl_close(%s)\n", dev->image_path); -void -cdrom_ioctl_load(void) -{ - plat_cdrom_load(); + if (dev && dev->ops && dev->ops->exit) + dev->ops->exit(dev); } static int cdrom_ioctl_open_abort(cdrom_t *dev) { - if (dev && dev->ops && dev->ops->exit) - dev->ops->exit(dev); - + cdrom_ioctl_close(dev); dev->ops = NULL; dev->host_drive = 0; - dev->ioctl_path[0] = 0; + dev->image_path[0] = 0; return 1; } int -cdrom_ioctl_open(cdrom_t *dev, const char *path) +cdrom_ioctl_open(cdrom_t *dev, char *path, int letter) { - /* Open the drive. */ - if (plat_cdrom_open()) + /* Open the image. */ + int i = plat_cdrom_open(path, letter); + if (!i) return cdrom_ioctl_open_abort(dev); - /* Make sure to not STRCPY if the two are pointing - at the same place. */ - if (path != dev->ioctl_path) - strcpy(dev->ioctl_path, path); - /* All good, reset state. */ dev->cd_status = CD_STATUS_STOPPED; dev->is_dir = 0; dev->seek_pos = 0; dev->cd_buflen = 0; - plat_cdrom_reset(); - dev->cdrom_capacity = plat_cdrom_get_capacity(); + dev->cdrom_capacity = cdrom_ioctl_get_capacity(dev); pclog("CD-ROM capacity: %i sectors (%" PRIi64 " bytes)\n", dev->cdrom_capacity, ((uint64_t) dev->cdrom_capacity) << 11ULL); /* Attach this handler to the drive. */ @@ -219,3 +229,4 @@ cdrom_ioctl_open(cdrom_t *dev, const char *path) return 0; } + diff --git a/src/config.c b/src/config.c index 02c1d276a..fdecb6cd5 100644 --- a/src/config.c +++ b/src/config.c @@ -1263,7 +1263,7 @@ load_floppy_and_cdrom_drives(void) path_normalize(cdrom[c].image_path); } - if (cdrom[c].host_drive && (cdrom[c].host_drive != 200)) + if (cdrom[c].host_drive && (cdrom[c].host_drive != 200) && (cdrom[c].host_drive != 201)) cdrom[c].host_drive = 0; if ((cdrom[c].host_drive == 0x200) && (strlen(cdrom[c].image_path) == 0)) @@ -2575,7 +2575,7 @@ save_floppy_and_cdrom_drives(void) for (c = 0; c < CDROM_NUM; c++) { sprintf(temp, "cdrom_%02i_host_drive", c + 1); - if ((cdrom[c].bus_type == 0) || (cdrom[c].host_drive != 200)) + if ((cdrom[c].bus_type == 0) || ((cdrom[c].host_drive != 200) && (cdrom[c].host_drive != 201))) ini_section_delete_var(cat, temp); else ini_section_set_int(cat, temp, cdrom[c].host_drive); diff --git a/src/include/86box/cdrom.h b/src/include/86box/cdrom.h index 15127b06e..de72974d1 100644 --- a/src/include/86box/cdrom.h +++ b/src/include/86box/cdrom.h @@ -246,6 +246,8 @@ typedef struct cdrom { int audio_op; int audio_muted_soft; int sony_msf; + int host; + int letter; const cdrom_ops_t *ops; @@ -309,7 +311,7 @@ extern void cdrom_image_reset(cdrom_t *dev); extern void cdrom_ioctl_eject(void); extern void cdrom_ioctl_load(void); -extern int cdrom_ioctl_open(cdrom_t *dev, const char d); +extern int cdrom_ioctl_open(cdrom_t *dev, char *path, int letter); extern void cdrom_update_cdb(uint8_t *cdb, int lba_pos, int number_of_blocks); diff --git a/src/include/86box/cdrom_ioctl.h b/src/include/86box/cdrom_ioctl.h new file mode 100644 index 000000000..5fc157615 --- /dev/null +++ b/src/include/86box/cdrom_ioctl.h @@ -0,0 +1,32 @@ +/* + * 86Box A hypervisor and IBM PC system emulator that specializes in + * running old operating systems and software designed for IBM + * PC systems and compatibles from 1981 through fairly recent + * system designs based on the PCI bus. + * + * This file is part of the 86Box distribution. + * + * CD-ROM image file handling module header, translated to C + * from cdrom_dosbox.h. + * + * Authors: RichardG, + * Miran Grca, + * + * Copyright 2016-2022 RichardG. + * Copyright 2016-2022 Miran Grca. + */ +#ifndef CDROM_IOCTL_H +#define CDROM_IOCTL_H + +/* this header file lists the functions provided by + various platform specific cdrom-ioctl files */ + +#ifdef __cplusplus +extern "C" { +#endif + +#ifdef __cplusplus +} +#endif + +#endif /*CDROM_IOCTL_H*/ diff --git a/src/include/86box/plat_cdrom.h b/src/include/86box/plat_cdrom.h new file mode 100644 index 000000000..38246bd07 --- /dev/null +++ b/src/include/86box/plat_cdrom.h @@ -0,0 +1,66 @@ +/* + * 86Box A hypervisor and IBM PC system emulator that specializes in + * running old operating systems and software designed for IBM + * PC systems and compatibles from 1981 through fairly recent + * system designs based on the PCI bus. + * + * This file is part of the 86Box distribution. + * + * Definitions for platform specific serial to host passthrough. + * + * + * Authors: Andreas J. Reichel , + * Jasmine Iwanek + * + * Copyright 2021 Andreas J. Reichel. + * Copyright 2021-2022 Jasmine Iwanek. + */ + +#ifndef PLAT_CDROM_H +#define PLAT_CDROM_H + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +#define RAW_SECTOR_SIZE 2352 +#define COOKED_SECTOR_SIZE 2048 + +#define DATA_TRACK 0x14 +#define AUDIO_TRACK 0x10 + +#define CD_FPS 75 +#define FRAMES_TO_MSF(f, M, S, F) \ + { \ + uint64_t value = f; \ + *(F) = (value % CD_FPS) & 0xff; \ + value /= CD_FPS; \ + *(S) = (value % 60) & 0xff; \ + value /= 60; \ + *(M) = value & 0xff; \ + } +#define MSF_TO_FRAMES(M, S, F) ((M) *60 * CD_FPS + (S) *CD_FPS + (F)) + +typedef struct SMSF { + uint16_t min; + uint8_t sec; + uint8_t fr; +} TMSF; + +extern int plat_cdrom_is_track_audio(uint32_t sector); +extern int plat_cdrom_get_last_block(void); +extern void plat_cdrom_get_audio_tracks(int *st_track, int *end, TMSF *lead_out); +extern int plat_cdrom_get_audio_track_info(int end, int track, int *track_num, TMSF *start, uint8_t *attr); +extern int plat_cdrom_get_audio_sub(uint32_t sector, uint8_t *attr, uint8_t *track, uint8_t *index, TMSF *rel_pos, TMSF *abs_pos); +extern int plat_cdrom_get_sector_size(uint32_t sector); +extern int plat_cdrom_read_sector(uint8_t *buffer, int raw, uint32_t sector); +extern void plat_cdrom_close(void); +extern int plat_cdrom_open(char *path, int letter); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/src/machine_status.c b/src/machine_status.c index 65f4704b4..03c0ac092 100644 --- a/src/machine_status.c +++ b/src/machine_status.c @@ -33,7 +33,11 @@ machine_status_init(void) machine_status.fdd[i].active = false; } for (size_t i = 0; i < CDROM_NUM; ++i) { - machine_status.cdrom[i].empty = cdrom[i].host_drive != 200 || (strlen(cdrom[i].image_path) == 0); + if (cdrom[i].host) + machine_status.cdrom[i].empty = cdrom[i].host_drive != 201; + else + machine_status.cdrom[i].empty = cdrom[i].host_drive != 200 || (strlen(cdrom[i].image_path) == 0); + machine_status.cdrom[i].active = false; } for (size_t i = 0; i < ZIP_NUM; i++) { @@ -55,4 +59,4 @@ machine_status_init(void) machine_status.net[i].active = false; machine_status.net[i].empty = !network_is_connected(i); } -} \ No newline at end of file +} diff --git a/src/qt/qt_machinestatus.cpp b/src/qt/qt_machinestatus.cpp index 53e875e33..a36efd2cd 100644 --- a/src/qt/qt_machinestatus.cpp +++ b/src/qt/qt_machinestatus.cpp @@ -533,7 +533,7 @@ MachineStatus::refresh(QStatusBar *sbar) iterateCDROM([this, sbar](int i) { d->cdrom[i].label = std::make_unique(); - d->cdrom[i].setEmpty(cdrom[i].host_drive != 200 || QString(cdrom[i].image_path).isEmpty()); + d->cdrom[i].setEmpty(((cdrom[i].host_drive != 200) && (cdrom[i].host_drive != 201)) || QString(cdrom[i].image_path).isEmpty()); d->cdrom[i].setActive(false); d->cdrom[i].refresh(); connect((ClickableLabel *) d->cdrom[i].label.get(), &ClickableLabel::clicked, [i](QPoint pos) { diff --git a/src/qt/qt_mediamenu.cpp b/src/qt/qt_mediamenu.cpp index c4fd50567..e6d52a823 100644 --- a/src/qt/qt_mediamenu.cpp +++ b/src/qt/qt_mediamenu.cpp @@ -29,6 +29,21 @@ #include extern "C" { +#ifdef Q_OS_WINDOWS +#define BITMAP WINDOWS_BITMAP +#undef UNICODE +#include +#include +#undef BITMAP +#endif +#include +#include +#include +#include +#include +#include +#include +#define HAVE_STDARG_H #include <86box/86box.h> #include <86box/config.h> #include <86box/device.h> @@ -131,14 +146,25 @@ MediaMenu::refresh(QMenu *parentMenu) cdromMutePos = menu->children().count(); menu->addAction(QApplication::style()->standardIcon(QStyle::SP_MediaVolumeMuted), tr("&Mute"), [this, i]() { cdromMute(i); })->setCheckable(true); menu->addSeparator(); - menu->addAction(ProgSettings::loadIcon("/cdrom.ico"), tr("&Image..."), [this, i]() { cdromMount(i, 0); })->setCheckable(false); - menu->addAction(QApplication::style()->standardIcon(QStyle::SP_DirIcon), tr("&Folder..."), [this, i]() { cdromMount(i, 1); })->setCheckable(false); + menu->addAction(ProgSettings::loadIcon("/cdrom.ico"), tr("&Image..."), [this, i]() { cdrom[i].host = 0; cdrom[i].letter = 0; cdromMount(i, 0); })->setCheckable(false); + menu->addAction(QApplication::style()->standardIcon(QStyle::SP_DirIcon), tr("&Folder..."), [this, i]() { cdrom[i].host = 0; cdrom[i].letter = 0; cdromMount(i, 1); })->setCheckable(false); menu->addSeparator(); for (int slot = 0; slot < MAX_PREV_IMAGES; slot++) { cdromImageHistoryPos[slot] = menu->children().count(); menu->addAction(QString::asprintf(tr("Image %i").toUtf8().constData(), slot), [this, i, slot]() { cdromReload(i, slot); })->setCheckable(false); } menu->addSeparator(); +#ifdef Q_OS_WINDOWS + /* Loop through each Windows drive letter and test to see if + it's a CDROM */ + for (auto &letter : driveLetters) { + auto drive = QString::asprintf("%c:\\", letter).toUtf8().constData(); + if (GetDriveType(drive) == DRIVE_CDROM) { + menu->addAction(ProgSettings::loadIcon("/cdrom.ico"), tr("Host CD/DVD Drive (%1:)").arg(letter), [this, i, letter]() { cdrom[i].host = 1; cdrom[i].letter = letter; cdromMount(i, 0); })->setCheckable(false); + } + } + menu->addSeparator(); +#endif // _WIN32 cdromImagePos = menu->children().count(); cdromDirPos = menu->children().count(); menu->addAction(tr("E&ject"), [this, i]() { cdromEject(i); })->setCheckable(false); @@ -467,17 +493,24 @@ MediaMenu::cdromMount(int i, const QString &filename) if ((fn.data() != NULL) && (strlen(fn.data()) >= 1) && (fn.data()[strlen(fn.data()) - 1] == '\\')) fn.data()[strlen(fn.data()) - 1] = '/'; #endif - cdrom_image_open(&(cdrom[i]), fn.data()); + if (cdrom[i].host) + cdrom_ioctl_open(&(cdrom[i]), fn.data(), cdrom[i].letter); + else + cdrom_image_open(&(cdrom[i]), fn.data()); /* Signal media change to the emulated machine. */ if (cdrom[i].insert) cdrom[i].insert(cdrom[i].priv); - cdrom[i].host_drive = (strlen(cdrom[i].image_path) == 0) ? 0 : 200; - if (cdrom[i].host_drive == 200) { + cdrom[i].host_drive = (strlen(cdrom[i].image_path) == 0 && !cdrom[i].letter) ? 0 : (200 + (cdrom[i].host ? 1 : 0)); + + pclog("HostDrive=%d, letter=%d.\n", cdrom[i].host_drive, cdrom[i].letter); + if (cdrom[i].host_drive >= 200) { ui_sb_update_icon_state(SB_CDROM | i, 0); } else { ui_sb_update_icon_state(SB_CDROM | i, 1); } - mhm.addImageToHistory(i, ui::MediaType::Optical, cdrom[i].prev_image_path, cdrom[i].image_path); + if (!cdrom[i].host) + mhm.addImageToHistory(i, ui::MediaType::Optical, cdrom[i].prev_image_path, cdrom[i].image_path); + cdromUpdateMenu(i); ui_sb_update_tip(SB_CDROM | i); config_save(); @@ -489,19 +522,24 @@ MediaMenu::cdromMount(int i, int dir) QString filename; QFileInfo fi(cdrom[i].image_path); - if (dir) { - filename = QFileDialog::getExistingDirectory( - parentWidget); + pclog("IsHost?=%d.\n", cdrom[i].host); + if (cdrom[i].host) { + filename = QString(cdrom[i].letter); } else { - filename = QFileDialog::getOpenFileName( - parentWidget, - QString(), - QString(), - tr("CD-ROM images") % util::DlgFilter({ "iso", "cue" }) % tr("All files") % util::DlgFilter({ "*" }, true)); - } + if (dir) { + filename = QFileDialog::getExistingDirectory( + parentWidget); + } else { + filename = QFileDialog::getOpenFileName( + parentWidget, + QString(), + QString(), + tr("CD-ROM images") % util::DlgFilter({ "iso", "cue" }) % tr("All files") % util::DlgFilter({ "*" }, true)); + } - if (filename.isEmpty()) { - return; + if (filename.isEmpty()) { + return; + } } cdromMount(i, filename); diff --git a/src/qt/qt_mediamenu.hpp b/src/qt/qt_mediamenu.hpp index 7835077b3..2e448086f 100644 --- a/src/qt/qt_mediamenu.hpp +++ b/src/qt/qt_mediamenu.hpp @@ -87,6 +87,8 @@ private: QString getMediaOpenDirectory(); ui::MediaHistoryManager mhm; + const QByteArray driveLetters = QByteArrayLiteral("ABCDEFGHIJKLMNOPQRSTUVWXYZ"); + int cassetteRecordPos; int cassettePlayPos; int cassetteRewindPos; diff --git a/src/qt/win_cdrom_ioctl.c b/src/qt/win_cdrom_ioctl.c index 9eee9c789..1ba381692 100644 --- a/src/qt/win_cdrom_ioctl.c +++ b/src/qt/win_cdrom_ioctl.c @@ -32,107 +32,89 @@ #include <86box/plat_unused.h> #include <86box/plat_cdrom.h> -static const char ioctl_path[8]; -static HANDLE hIOCTL; -static CDROM_TOC toc; - /* The addresses sent from the guest are absolute, ie. a LBA of 0 corresponds to a MSF of 00:00:00. Otherwise, the counter displayed by the guest is wrong: there is a seeming 2 seconds in which audio plays but counter does not move, while a data track before audio jumps to 2 seconds before the actual start of the audio while audio still plays. With an absolute conversion, the counter is fine. */ #define MSFtoLBA(m, s, f) ((((m * 60) + s) * 75) + f) -static int -plat_cdrom_get_track(uint32_t sector) -{ - int track = 0; - uint32_t track_addr; - - for (int i = toc.FirstTrack; i < toc.LastTrack; i++) { - /* There must be at least two tracks - data and lead out. */ - track_addr = MSFtoLBA(toc.TrackData[i].Address[1], toc.TrackData[i].Address[2], toc.TrackData[i].Address[3]); - if (track_addr <= sector) { - track = i; - } - } - - pclog("GetTrack = %d.\n", track); - return track; -} +static HANDLE handle; +static char ioctl_path[8]; int -plat_cdrom_get_audio_track(uint32_t sector) +plat_cdrom_is_track_audio(uint32_t sector) { + CDROM_TOC toc; + long size = 0; + int ret; int control = 0; - uint32_t track_addr; + uint32_t track_addr = 0; - for (int i = 0; toc.TrackData[i].TrackNumber != 0xaa; i++) { - /* There must be at least two tracks - data and lead out. */ - track_addr = MSFtoLBA(toc.TrackData[i].Address[1], toc.TrackData[i].Address[2], toc.TrackData[i].Address[3]); - if ((toc.TrackData[i].TrackNumber >= toc.FirstTrack) && (toc.TrackData[i].TrackNumber <= toc.LastTrack) && - (track_addr >= sector)) { - control = toc.TrackData[i].Control; + DeviceIoControl(handle, IOCTL_CDROM_READ_TOC, NULL, 0, &toc, sizeof(toc), (LPDWORD)&size, NULL); + plat_cdrom_close(); + + for (int c = 0; toc.TrackData[c].TrackNumber != 0xaa; c++) { + track_addr = MSFtoLBA(toc.TrackData[c].Address[1], toc.TrackData[c].Address[2], toc.TrackData[c].Address[3]); + if ((toc.TrackData[c].TrackNumber >= toc.FirstTrack) && (toc.TrackData[c].TrackNumber <= toc.LastTrack) && + (track_addr >= sector)) + control = toc.TrackData[c].Control; break; - } } - - return (control & 4) ? DATA_TRACK : AUDIO_TRACK; + ret = (control & 0x04) ? 0 : 1; + return ret; } int -plat_cdrom_get_audio_sub(uint32_t sector, uint8_t *attr, uint8_t *track, uint8_t *index, TMSF *rel_pos, TMSF *abs_pos) +plat_cdrom_get_last_block(void) { - CDROM_SUB_Q_DATA_FORMAT insub; - SUB_Q_CHANNEL_DATA sub; - DWORD size; - int pos = 0; - int cur_track = plat_cdrom_get_track(sector); + CDROM_TOC toc; + int lb = 0; + long size = 0; + uint32_t address = 0; - insub.Format = IOCTL_CDROM_CURRENT_POSITION; - if (plat_cdrom_open()) - return 0; - DeviceIoControl(hIOCTL,IOCTL_CDROM_READ_Q_CHANNEL, &insub, sizeof(insub), &sub, sizeof(sub), &size, NULL); - plat_cdrom_exit(); + DeviceIoControl(handle, IOCTL_CDROM_READ_TOC, NULL, 0, &toc, sizeof(toc), (LPDWORD)&size, NULL); + plat_cdrom_close(); - *attr = sub.CurrentPosition.Control; - *track = (uint8_t)(cur_track + 1); - *index = sub.CurrentPosition.IndexNumber; - - FRAMES_TO_MSF(sector + 150, &abs_pos->min, &abs_pos->sec, &abs_pos->fr); - - /* Absolute position should be adjusted by 150, not the relative ones. */ - FRAMES_TO_MSF(sector - toc.FirstTrack, &rel_pos->min, &rel_pos->sec, &rel_pos->fr); - - return 1; + for (int c = 0; c <= toc.LastTrack; c++) { + address = MSFtoLBA(toc.TrackData[c].Address[1], toc.TrackData[c].Address[2], toc.TrackData[c].Address[3]); + if (address > lb) + lb = address; + } + pclog("LBCapacity=%x.\n", lb); + return lb; } -int +void plat_cdrom_get_audio_tracks(int *st_track, int *end, TMSF *lead_out) { - CDROM_TOC toc; - DWORD byteCount; + CDROM_TOC toc; + long size = 0; - *st_track = toc.FirstTrack; - *end = toc.LastTrack; - FRAMES_TO_MSF(toc.TrackData[*end].TrackNumber + 150, &lead_out->min, &lead_out->sec, &lead_out->fr); + DeviceIoControl(handle, IOCTL_CDROM_READ_TOC, NULL, 0, &toc, sizeof(toc), (LPDWORD)&size, NULL); + plat_cdrom_close(); - return 1; + *st_track = 1; + *end = toc.LastTrack; + lead_out->min = toc.TrackData[toc.LastTrack].Address[1]; + lead_out->sec = toc.TrackData[toc.LastTrack].Address[2]; + lead_out->fr = toc.TrackData[toc.LastTrack].Address[3]; } /* This replaces both Info and EndInfo, they are specified by a variable. */ int -plat_cdrom_get_audio_track_info(int end, int track, int *track_num, TMSF *start, uint8_t *attr) +plat_cdrom_get_audio_track_info(UNUSED(int end), int track, int *track_num, TMSF *start, uint8_t *attr) { - int pos; - DWORD byteCount; + CDROM_TOC toc; + long size = 0; - pclog("plat_cdrom_get_audio_track_info(): start track = %d, last track = %d.\n", track, end); + DeviceIoControl(handle, IOCTL_CDROM_READ_TOC, NULL, 0, &toc, sizeof(toc), (LPDWORD)&size, NULL); + plat_cdrom_close(); - if ((track < 1) || (track > end)) + if ((track < 1) || (track == 0xaa) || (track > (toc.LastTrack))) return 0; - pos = toc.FirstTrack + 150; - - FRAMES_TO_MSF(pos, &start->min, &start->sec, &start->fr); + start->min = toc.TrackData[track - 1].Address[1]; + start->sec = toc.TrackData[track - 1].Address[2]; + start->fr = toc.TrackData[track - 1].Address[3]; *track_num = toc.TrackData[track - 1].TrackNumber; *attr = toc.TrackData[track - 1].Control; @@ -140,136 +122,100 @@ plat_cdrom_get_audio_track_info(int end, int track, int *track_num, TMSF *start, return 1; } -uint32_t -plat_get_sector_size(void) +/* TODO: See if track start is adjusted by 150 or not. */ +int +plat_cdrom_get_audio_sub(UNUSED(uint32_t sector), uint8_t *attr, uint8_t *track, uint8_t *index, TMSF *rel_pos, TMSF *abs_pos) { - DISK_GEOMETRY dgCDROM; - DWORD size; + CDROM_SUB_Q_DATA_FORMAT insub; + SUB_Q_CHANNEL_DATA sub; + long size = 0; - if (plat_cdrom_open()) - return 0; - DeviceIoControl(hIOCTL, IOCTL_CDROM_GET_DRIVE_GEOMETRY, NULL, 0, &dgCDROM, sizeof(DISK_GEOMETRY), &size, NULL); - plat_cdrom_exit(); + insub.Format = IOCTL_CDROM_CURRENT_POSITION; - if (dgCDROM.MediaType != 11) // Removable Media Check + DeviceIoControl(handle, IOCTL_CDROM_READ_Q_CHANNEL, &insub, sizeof(insub), &sub, sizeof(sub), (LPDWORD)&size, NULL); + plat_cdrom_close(); + + if (sub.CurrentPosition.TrackNumber < 1) return 0; - return dgCDROM.BytesPerSector; + *track = sub.CurrentPosition.TrackNumber; + *attr = sub.CurrentPosition.Control; + *index = sub.CurrentPosition.IndexNumber; + + rel_pos->min = sub.CurrentPosition.TrackRelativeAddress[1]; + rel_pos->sec = sub.CurrentPosition.TrackRelativeAddress[2]; + rel_pos->fr = sub.CurrentPosition.TrackRelativeAddress[3]; + abs_pos->min = sub.CurrentPosition.AbsoluteAddress[1]; + abs_pos->sec = sub.CurrentPosition.AbsoluteAddress[2]; + abs_pos->fr = sub.CurrentPosition.AbsoluteAddress[3]; + + return 1; +} + +int +plat_cdrom_get_sector_size(UNUSED(uint32_t sector)) +{ + return COOKED_SECTOR_SIZE; } int plat_cdrom_read_sector(uint8_t *buffer, int raw, uint32_t sector) { - int ret; LARGE_INTEGER pos; - RAW_READ_INFO in; - DWORD byteCount; - pclog("plat_cdrom_read_sector(): raw? = %d, sector = %02x.\n", raw, sector); + BOOL status; + long size = 0; - if (raw) { - in.TrackMode = CDDA; - in.SectorCount = 1; - in.DiskOffset.QuadPart = sector * RAW_SECTOR_SIZE; - if (plat_cdrom_open()) - return 0; - ret = DeviceIoControl(hIOCTL, IOCTL_CDROM_RAW_READ, &in, sizeof(RAW_READ_INFO), buffer, RAW_SECTOR_SIZE, &byteCount, NULL); - plat_cdrom_exit(); - return ret; - } else { - pos.QuadPart = sector * COOKED_SECTOR_SIZE; - if (plat_cdrom_open()) - return 0; - SetFilePointer(hIOCTL, pos.LowPart, &pos.HighPart, FILE_BEGIN); - ret = ReadFile(hIOCTL, buffer, COOKED_SECTOR_SIZE, &byteCount, NULL); - plat_cdrom_exit(); - pclog("plat_cdrom_read_sector(): ret = %x.\n", !ret); - return !ret; - } - return 0; -} + int buflen = raw ? RAW_SECTOR_SIZE : COOKED_SECTOR_SIZE; -uint32_t -plat_cdrom_get_capacity(void) -{ - DWORD size; - int c; - DISK_GEOMETRY dgCDROM; - uint32_t totals; + if (!raw) { + pclog("Cooked.\n"); + // Cooked + int success = 0; + DWORD newPos = SetFilePointer(handle, sector * COOKED_SECTOR_SIZE, 0, FILE_BEGIN); + if (newPos != 0xFFFFFFFF) + success = ReadFile(handle, buffer, buflen, (LPDWORD)&size, NULL); + status = (success != 0); + } else { + pclog("Raw.\n"); + // Raw + RAW_READ_INFO in; + in.DiskOffset.LowPart = sector * COOKED_SECTOR_SIZE; + in.DiskOffset.HighPart = 0; + in.SectorCount = 1; + in.TrackMode = CDDA; + status = DeviceIoControl(handle, IOCTL_CDROM_RAW_READ, &in, sizeof(in), + buffer, buflen, (LPDWORD)&size, NULL); + } - if (plat_cdrom_open()) - return 0; - DeviceIoControl(hIOCTL, IOCTL_CDROM_GET_DRIVE_GEOMETRY, NULL, 0, &dgCDROM, sizeof(DISK_GEOMETRY), &size, NULL); - plat_cdrom_exit(); - - totals = dgCDROM.SectorsPerTrack * dgCDROM.TracksPerCylinder * dgCDROM.Cylinders.QuadPart; - - pclog("Total = %08x.\n", totals); - return totals; -} - -int -plat_cdrom_load(void) -{ - int ret; - DWORD size; - - if (plat_cdrom_open()) - return 0; - DeviceIoControl(hIOCTL, IOCTL_STORAGE_LOAD_MEDIA, NULL, 0, NULL, 0, &size, NULL); - plat_cdrom_exit(); - return 1; -} - -int -plat_cdrom_eject(void) -{ - int ret; - DWORD size; - - if (plat_cdrom_open()) - return 0; - ret = DeviceIoControl(hIOCTL, IOCTL_STORAGE_EJECT_MEDIA, NULL, 0, NULL, 0, &size, NULL); - plat_cdrom_exit(); - return ret; -} - -void -plat_cdrom_exit(void) -{ - if (hIOCTL) { - CloseHandle(hIOCTL); - hIOCTL = NULL; - } + plat_cdrom_close(); + pclog("ReadSector status=%d, sector=%d, size=%d.\n", status, sector, size); + return (size == buflen) && (status > 0); } void plat_cdrom_close(void) { - plat_cdrom_exit(); + if (handle != NULL) { + CloseHandle(handle); + handle = NULL; + } +} + +static int +plat_cdrom_load(char *path, int letter) +{ + plat_cdrom_close(); + strcpy(ioctl_path, "\\\\.\\"); + strcat(ioctl_path, path); + + /* Data track (shouldn't there be a lead in track?). */ + handle = CreateFileW((LPCWSTR)ioctl_path, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL); + pclog("handle=%p, error=%x, drive=%s.\n", handle, GetLastError(), ioctl_path); + return (handle != INVALID_HANDLE_VALUE); } int -plat_cdrom_reset(void) +plat_cdrom_open(char *path, int letter) { - CDROM_TOC ltoc; - DWORD size; - - if (plat_cdrom_open()) - return 0; - DeviceIoControl(hIOCTL, IOCTL_CDROM_READ_TOC, NULL, 0, <oc, sizeof(ltoc), &size, NULL); - plat_cdrom_exit(); - - toc = ltoc; - return 1; -} - -int -plat_cdrom_open(void) -{ - plat_cdrom_exit(); - hIOCTL = CreateFile((LPCWSTR)ioctl_path, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); - if (hIOCTL == NULL) - return 1; - - return 0; + return plat_cdrom_load(path, letter); } From 9af55412db2e013f91117c15c9ecff371c76a4d7 Mon Sep 17 00:00:00 2001 From: TC1995 Date: Sat, 18 May 2024 22:37:49 +0200 Subject: [PATCH 3/7] Make the CD/DVD-ROM passthrough actually work (WIP) Still WIP, eject/reload in the qt side is a bit broken. --- src/cdrom/cdrom.c | 13 ++++--- src/cdrom/cdrom_ioctl.c | 10 ++++-- src/include/86box/cdrom.h | 4 +-- src/include/86box/plat_cdrom.h | 2 +- src/qt/qt_mediamenu.cpp | 23 ++++++------ src/qt/win_cdrom_ioctl.c | 66 +++++++++++++++++++++++++--------- 6 files changed, 80 insertions(+), 38 deletions(-) diff --git a/src/cdrom/cdrom.c b/src/cdrom/cdrom.c index 06494e4a9..94d47ed06 100644 --- a/src/cdrom/cdrom.c +++ b/src/cdrom/cdrom.c @@ -1973,7 +1973,7 @@ cdrom_hard_reset(void) cdrom_image_open(dev, dev->image_path); } else if (dev->host_drive == 201) - cdrom_ioctl_open(dev, dev->image_path, dev->letter); + cdrom_ioctl_open(dev, dev->drive); } } @@ -2028,6 +2028,8 @@ cdrom_eject(uint8_t id) if (dev->host_drive == 200) strcpy(dev->prev_image_path, dev->image_path); + else if (dev->host_drive == 201) + cdrom_ioctl_eject(); dev->prev_host_drive = dev->host_drive + (dev->host ? 1 : 0); dev->host_drive = 0; @@ -2049,7 +2051,7 @@ cdrom_reload(uint8_t id) { cdrom_t *dev = &cdrom[id]; - if ((dev->host_drive == dev->prev_host_drive) || (dev->prev_host_drive == 0) || (dev->host_drive != 0)) { + if (!dev->host && ((dev->host_drive == dev->prev_host_drive) || (dev->prev_host_drive == 0) || (dev->host_drive != 0))) { /* Switch from empty to empty. Do nothing. */ return; } @@ -2061,7 +2063,8 @@ cdrom_reload(uint8_t id) if (dev->prev_host_drive >= 200) { /* Reload a previous image. */ - strcpy(dev->image_path, dev->prev_image_path); + if (dev->prev_host_drive == 200) + strcpy(dev->image_path, dev->prev_image_path); #ifdef _WIN32 if (dev->prev_host_drive == 200) { @@ -2076,13 +2079,13 @@ cdrom_reload(uint8_t id) #endif if (dev->prev_host_drive > 200) - cdrom_ioctl_open(dev, dev->image_path, dev->letter); + cdrom_ioctl_open(dev, dev->drive); else cdrom_image_open(dev, dev->image_path); cdrom_insert(id); - if ((strlen(dev->image_path) == 0) && !dev->letter) + if ((strlen(dev->image_path) == 0) && !dev->drive) dev->host_drive = 0; else dev->host_drive = 200 + (dev->host ? 1 : 0); diff --git a/src/cdrom/cdrom_ioctl.c b/src/cdrom/cdrom_ioctl.c index 861c46eac..8f5c5f099 100644 --- a/src/cdrom/cdrom_ioctl.c +++ b/src/cdrom/cdrom_ioctl.c @@ -208,11 +208,17 @@ cdrom_ioctl_open_abort(cdrom_t *dev) return 1; } +void +cdrom_ioctl_eject(void) +{ + plat_cdrom_eject(); +} + int -cdrom_ioctl_open(cdrom_t *dev, char *path, int letter) +cdrom_ioctl_open(cdrom_t *dev, int drive) { /* Open the image. */ - int i = plat_cdrom_open(path, letter); + int i = plat_cdrom_set_drive(drive); if (!i) return cdrom_ioctl_open_abort(dev); diff --git a/src/include/86box/cdrom.h b/src/include/86box/cdrom.h index de72974d1..ff0c77930 100644 --- a/src/include/86box/cdrom.h +++ b/src/include/86box/cdrom.h @@ -247,7 +247,7 @@ typedef struct cdrom { int audio_muted_soft; int sony_msf; int host; - int letter; + int drive; const cdrom_ops_t *ops; @@ -311,7 +311,7 @@ extern void cdrom_image_reset(cdrom_t *dev); extern void cdrom_ioctl_eject(void); extern void cdrom_ioctl_load(void); -extern int cdrom_ioctl_open(cdrom_t *dev, char *path, int letter); +extern int cdrom_ioctl_open(cdrom_t *dev, int drive); extern void cdrom_update_cdb(uint8_t *cdb, int lba_pos, int number_of_blocks); diff --git a/src/include/86box/plat_cdrom.h b/src/include/86box/plat_cdrom.h index 38246bd07..93f9befe5 100644 --- a/src/include/86box/plat_cdrom.h +++ b/src/include/86box/plat_cdrom.h @@ -57,7 +57,7 @@ extern int plat_cdrom_get_audio_sub(uint32_t sector, uint8_t *attr, uint8_t *tra extern int plat_cdrom_get_sector_size(uint32_t sector); extern int plat_cdrom_read_sector(uint8_t *buffer, int raw, uint32_t sector); extern void plat_cdrom_close(void); -extern int plat_cdrom_open(char *path, int letter); +extern int plat_cdrom_set_drive(int drive); #ifdef __cplusplus } diff --git a/src/qt/qt_mediamenu.cpp b/src/qt/qt_mediamenu.cpp index e6d52a823..8405156fb 100644 --- a/src/qt/qt_mediamenu.cpp +++ b/src/qt/qt_mediamenu.cpp @@ -146,8 +146,8 @@ MediaMenu::refresh(QMenu *parentMenu) cdromMutePos = menu->children().count(); menu->addAction(QApplication::style()->standardIcon(QStyle::SP_MediaVolumeMuted), tr("&Mute"), [this, i]() { cdromMute(i); })->setCheckable(true); menu->addSeparator(); - menu->addAction(ProgSettings::loadIcon("/cdrom.ico"), tr("&Image..."), [this, i]() { cdrom[i].host = 0; cdrom[i].letter = 0; cdromMount(i, 0); })->setCheckable(false); - menu->addAction(QApplication::style()->standardIcon(QStyle::SP_DirIcon), tr("&Folder..."), [this, i]() { cdrom[i].host = 0; cdrom[i].letter = 0; cdromMount(i, 1); })->setCheckable(false); + menu->addAction(ProgSettings::loadIcon("/cdrom.ico"), tr("&Image..."), [this, i]() { cdrom[i].host = 0; cdrom[i].drive = 0; cdromMount(i, 0); })->setCheckable(false); + menu->addAction(QApplication::style()->standardIcon(QStyle::SP_DirIcon), tr("&Folder..."), [this, i]() { cdrom[i].host = 0; cdrom[i].drive = 0; cdromMount(i, 1); })->setCheckable(false); menu->addSeparator(); for (int slot = 0; slot < MAX_PREV_IMAGES; slot++) { cdromImageHistoryPos[slot] = menu->children().count(); @@ -160,7 +160,7 @@ MediaMenu::refresh(QMenu *parentMenu) for (auto &letter : driveLetters) { auto drive = QString::asprintf("%c:\\", letter).toUtf8().constData(); if (GetDriveType(drive) == DRIVE_CDROM) { - menu->addAction(ProgSettings::loadIcon("/cdrom.ico"), tr("Host CD/DVD Drive (%1:)").arg(letter), [this, i, letter]() { cdrom[i].host = 1; cdrom[i].letter = letter; cdromMount(i, 0); })->setCheckable(false); + menu->addAction(ProgSettings::loadIcon("/cdrom.ico"), tr("Host CD/DVD Drive (%1:)").arg(letter), [this, i, letter]() { cdrom[i].host = 1; cdrom[i].drive = letter; cdromMount(i, 0); })->setCheckable(false); } } menu->addSeparator(); @@ -487,22 +487,22 @@ MediaMenu::cdromMount(int i, const QString &filename) cdrom[i].ops = nullptr; memset(cdrom[i].image_path, 0, sizeof(cdrom[i].image_path)); #ifdef _WIN32 - if ((fn.data() != NULL) && (strlen(fn.data()) >= 1) && (fn.data()[strlen(fn.data()) - 1] == '/')) + if (!cdrom[i].host && (fn.data() != NULL) && (strlen(fn.data()) >= 1) && (fn.data()[strlen(fn.data()) - 1] == '/')) fn.data()[strlen(fn.data()) - 1] = '\\'; #else if ((fn.data() != NULL) && (strlen(fn.data()) >= 1) && (fn.data()[strlen(fn.data()) - 1] == '\\')) fn.data()[strlen(fn.data()) - 1] = '/'; #endif if (cdrom[i].host) - cdrom_ioctl_open(&(cdrom[i]), fn.data(), cdrom[i].letter); + cdrom_ioctl_open(&(cdrom[i]), cdrom[i].drive); else cdrom_image_open(&(cdrom[i]), fn.data()); /* Signal media change to the emulated machine. */ if (cdrom[i].insert) cdrom[i].insert(cdrom[i].priv); - cdrom[i].host_drive = (strlen(cdrom[i].image_path) == 0 && !cdrom[i].letter) ? 0 : (200 + (cdrom[i].host ? 1 : 0)); + cdrom[i].host_drive = (strlen(cdrom[i].image_path) == 0 && !cdrom[i].drive) ? 0 : (200 + (cdrom[i].host ? 1 : 0)); - pclog("HostDrive=%d, letter=%d.\n", cdrom[i].host_drive, cdrom[i].letter); + pclog("HostDrive=%d, drive=%d.\n", cdrom[i].host_drive, cdrom[i].drive); if (cdrom[i].host_drive >= 200) { ui_sb_update_icon_state(SB_CDROM | i, 0); } else { @@ -524,7 +524,7 @@ MediaMenu::cdromMount(int i, int dir) pclog("IsHost?=%d.\n", cdrom[i].host); if (cdrom[i].host) { - filename = QString(cdrom[i].letter); + filename = QString(cdrom[i].drive); } else { if (dir) { filename = QFileDialog::getExistingDirectory( @@ -536,10 +536,11 @@ MediaMenu::cdromMount(int i, int dir) QString(), tr("CD-ROM images") % util::DlgFilter({ "iso", "cue" }) % tr("All files") % util::DlgFilter({ "*" }, true)); } + } - if (filename.isEmpty()) { - return; - } + if (filename.isEmpty()) { + pclog("File is empty.\n"); + return; } cdromMount(i, filename); diff --git a/src/qt/win_cdrom_ioctl.c b/src/qt/win_cdrom_ioctl.c index 1ba381692..9f02e8d27 100644 --- a/src/qt/win_cdrom_ioctl.c +++ b/src/qt/win_cdrom_ioctl.c @@ -38,7 +38,22 @@ #define MSFtoLBA(m, s, f) ((((m * 60) + s) * 75) + f) static HANDLE handle; -static char ioctl_path[8]; +static WCHAR ioctl_path[256]; + +static int +plat_cdrom_open(void) +{ + plat_cdrom_close(); + + handle = CreateFileW((LPCWSTR)ioctl_path, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL); + pclog("handle=%p, error=%x.\n", handle, GetLastError()); + if (handle != INVALID_HANDLE_VALUE) { + long size; + DeviceIoControl(handle, IOCTL_STORAGE_LOAD_MEDIA, NULL, 0, NULL, 0, (LPDWORD)&size, NULL); + return 1; + } + return 0; +} int plat_cdrom_is_track_audio(uint32_t sector) @@ -49,6 +64,7 @@ plat_cdrom_is_track_audio(uint32_t sector) int control = 0; uint32_t track_addr = 0; + plat_cdrom_open(); DeviceIoControl(handle, IOCTL_CDROM_READ_TOC, NULL, 0, &toc, sizeof(toc), (LPDWORD)&size, NULL); plat_cdrom_close(); @@ -71,6 +87,7 @@ plat_cdrom_get_last_block(void) long size = 0; uint32_t address = 0; + plat_cdrom_open(); DeviceIoControl(handle, IOCTL_CDROM_READ_TOC, NULL, 0, &toc, sizeof(toc), (LPDWORD)&size, NULL); plat_cdrom_close(); @@ -79,7 +96,7 @@ plat_cdrom_get_last_block(void) if (address > lb) lb = address; } - pclog("LBCapacity=%x.\n", lb); + pclog("LBCapacity=%d.\n", lb); return lb; } @@ -89,6 +106,7 @@ plat_cdrom_get_audio_tracks(int *st_track, int *end, TMSF *lead_out) CDROM_TOC toc; long size = 0; + plat_cdrom_open(); DeviceIoControl(handle, IOCTL_CDROM_READ_TOC, NULL, 0, &toc, sizeof(toc), (LPDWORD)&size, NULL); plat_cdrom_close(); @@ -106,6 +124,7 @@ plat_cdrom_get_audio_track_info(UNUSED(int end), int track, int *track_num, TMSF CDROM_TOC toc; long size = 0; + plat_cdrom_open(); DeviceIoControl(handle, IOCTL_CDROM_READ_TOC, NULL, 0, &toc, sizeof(toc), (LPDWORD)&size, NULL); plat_cdrom_close(); @@ -132,6 +151,7 @@ plat_cdrom_get_audio_sub(UNUSED(uint32_t sector), uint8_t *attr, uint8_t *track, insub.Format = IOCTL_CDROM_CURRENT_POSITION; + plat_cdrom_open(); DeviceIoControl(handle, IOCTL_CDROM_READ_Q_CHANNEL, &insub, sizeof(insub), &sub, sizeof(sub), (LPDWORD)&size, NULL); plat_cdrom_close(); @@ -155,7 +175,15 @@ plat_cdrom_get_audio_sub(UNUSED(uint32_t sector), uint8_t *attr, uint8_t *track, int plat_cdrom_get_sector_size(UNUSED(uint32_t sector)) { - return COOKED_SECTOR_SIZE; + long size; + DISK_GEOMETRY dgCDROM; + + plat_cdrom_open(); + DeviceIoControl(handle, IOCTL_CDROM_GET_DRIVE_GEOMETRY, NULL, 0, &dgCDROM, sizeof(dgCDROM), (LPDWORD)&size, NULL); + plat_cdrom_close(); + + pclog("BytesPerSector=%d.\n", dgCDROM.BytesPerSector); + return dgCDROM.BytesPerSector; } int @@ -167,6 +195,7 @@ plat_cdrom_read_sector(uint8_t *buffer, int raw, uint32_t sector) int buflen = raw ? RAW_SECTOR_SIZE : COOKED_SECTOR_SIZE; + plat_cdrom_open(); if (!raw) { pclog("Cooked.\n"); // Cooked @@ -186,12 +215,22 @@ plat_cdrom_read_sector(uint8_t *buffer, int raw, uint32_t sector) status = DeviceIoControl(handle, IOCTL_CDROM_RAW_READ, &in, sizeof(in), buffer, buflen, (LPDWORD)&size, NULL); } - plat_cdrom_close(); pclog("ReadSector status=%d, sector=%d, size=%d.\n", status, sector, size); return (size == buflen) && (status > 0); } +void +plat_cdrom_eject(void) +{ + long size; + int ret; + + plat_cdrom_open(); + DeviceIoControl(handle, IOCTL_STORAGE_EJECT_MEDIA, NULL, 0, NULL, 0, (LPDWORD)&size, NULL); + plat_cdrom_close(); +} + void plat_cdrom_close(void) { @@ -201,21 +240,14 @@ plat_cdrom_close(void) } } -static int -plat_cdrom_load(char *path, int letter) +int +plat_cdrom_set_drive(int drive) { plat_cdrom_close(); - strcpy(ioctl_path, "\\\\.\\"); - strcat(ioctl_path, path); - /* Data track (shouldn't there be a lead in track?). */ - handle = CreateFileW((LPCWSTR)ioctl_path, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL); - pclog("handle=%p, error=%x, drive=%s.\n", handle, GetLastError(), ioctl_path); - return (handle != INVALID_HANDLE_VALUE); -} + wsprintf(ioctl_path, L"\\\\.\\%c:", drive); + pclog("Path is %s\n", ioctl_path); -int -plat_cdrom_open(char *path, int letter) -{ - return plat_cdrom_load(path, letter); + plat_cdrom_open(); + return 1; } From 641da43f1527b90b1cc08dee57d245d2f5af5ffc Mon Sep 17 00:00:00 2001 From: OBattler Date: Sun, 19 May 2024 21:17:57 +0200 Subject: [PATCH 4/7] IOCTL and CD-ROM changes. --- src/cdrom/cdrom.c | 20 +- src/cdrom/cdrom_image.c | 7 + src/cdrom/cdrom_ioctl.c | 135 ++++++++----- src/include/86box/cdrom.h | 16 +- src/include/86box/plat_cdrom.h | 21 +- src/qt/CMakeLists.txt | 4 + src/qt/qt_machinestatus.cpp | 2 +- src/qt/qt_mediahistorymanager.cpp | 2 +- src/qt/qt_mediamenu.cpp | 116 ++++++----- src/qt/qt_mediamenu.hpp | 2 +- src/qt/qt_platform.cpp | 13 +- src/qt/win_cdrom_ioctl.c | 309 ++++++++++++++++++++---------- src/scsi/scsi_cdrom.c | 8 +- 13 files changed, 424 insertions(+), 231 deletions(-) diff --git a/src/cdrom/cdrom.c b/src/cdrom/cdrom.c index 94d47ed06..0da199b52 100644 --- a/src/cdrom/cdrom.c +++ b/src/cdrom/cdrom.c @@ -1971,9 +1971,11 @@ cdrom_hard_reset(void) dev->image_path[strlen(dev->image_path) - 1] = '/'; #endif - cdrom_image_open(dev, dev->image_path); - } else if (dev->host_drive == 201) - cdrom_ioctl_open(dev, dev->drive); + if ((strlen(dev->image_path) != 0) && (strstr(dev->image_path, "ioctl://") == dev->image_path)) + cdrom_ioctl_open(dev, dev->image_path); + else + cdrom_image_open(dev, dev->image_path); + } } } @@ -2026,10 +2028,8 @@ cdrom_eject(uint8_t id) return; } - if (dev->host_drive == 200) + if (dev->host_drive >= 200) strcpy(dev->prev_image_path, dev->image_path); - else if (dev->host_drive == 201) - cdrom_ioctl_eject(); dev->prev_host_drive = dev->host_drive + (dev->host ? 1 : 0); dev->host_drive = 0; @@ -2078,17 +2078,17 @@ cdrom_reload(uint8_t id) } #endif - if (dev->prev_host_drive > 200) - cdrom_ioctl_open(dev, dev->drive); + if ((strlen(dev->image_path) != 0) && (strstr(dev->image_path, "ioctl://") == dev->image_path)) + cdrom_ioctl_open(dev, dev->image_path); else cdrom_image_open(dev, dev->image_path); cdrom_insert(id); - if ((strlen(dev->image_path) == 0) && !dev->drive) + if (strlen(dev->image_path) == 0) dev->host_drive = 0; else - dev->host_drive = 200 + (dev->host ? 1 : 0); + dev->host_drive = 200; } plat_cdrom_ui_update(id, 1); diff --git a/src/cdrom/cdrom_image.c b/src/cdrom/cdrom_image.c index 0c7ee03a0..dff3a9105 100644 --- a/src/cdrom/cdrom_image.c +++ b/src/cdrom/cdrom_image.c @@ -217,6 +217,12 @@ image_track_type(cdrom_t *dev, uint32_t lba) return 0; } +static int +image_ext_medium_changed(cdrom_t *dev) +{ + return 0; +} + static void image_exit(cdrom_t *dev) { @@ -241,6 +247,7 @@ static const cdrom_ops_t cdrom_image_ops = { image_sector_size, image_read_sector, image_track_type, + image_ext_medium_changed, image_exit }; diff --git a/src/cdrom/cdrom_ioctl.c b/src/cdrom/cdrom_ioctl.c index 8f5c5f099..dae9abaad 100644 --- a/src/cdrom/cdrom_ioctl.c +++ b/src/cdrom/cdrom_ioctl.c @@ -56,7 +56,7 @@ cdrom_ioctl_log(const char *fmt, ...) #define MSFtoLBA(m, s, f) ((((m * 60) + s) * 75) + f) static void -cdrom_ioctl_get_tracks(UNUSED(cdrom_t *dev), int *first, int *last) +ioctl_get_tracks(UNUSED(cdrom_t *dev), int *first, int *last) { TMSF tmsf; @@ -64,7 +64,7 @@ cdrom_ioctl_get_tracks(UNUSED(cdrom_t *dev), int *first, int *last) } static void -cdrom_ioctl_get_track_info(UNUSED(cdrom_t *dev), uint32_t track, int end, track_info_t *ti) +ioctl_get_track_info(UNUSED(cdrom_t *dev), uint32_t track, int end, track_info_t *ti) { TMSF tmsf; @@ -76,13 +76,30 @@ cdrom_ioctl_get_track_info(UNUSED(cdrom_t *dev), uint32_t track, int end, track_ } static void -cdrom_ioctl_get_subchannel(UNUSED(cdrom_t *dev), uint32_t lba, subchannel_t *subc) +ioctl_get_subchannel(UNUSED(cdrom_t *dev), uint32_t lba, subchannel_t *subc) { TMSF rel_pos; TMSF abs_pos; - plat_cdrom_get_audio_sub(lba, &subc->attr, &subc->track, &subc->index, - &rel_pos, &abs_pos); + if ((dev->cd_status == CD_STATUS_PLAYING) || (dev->cd_status == CD_STATUS_PAUSED)) { + uint32_t dat = lba + 150; + abs_pos.fr = dat % 75; + dat /= 75; + abs_pos.sec = dat % 60; + dat /= 60; + abs_pos.min = dat; + + dat = lba - plat_cdrom_get_track_start(lba, &subc->attr, &subc->track); + rel_pos.fr = dat % 75; + dat /= 75; + rel_pos.sec = dat % 60; + dat /= 60; + rel_pos.min = dat; + + subc->index = 1; + } else + plat_cdrom_get_audio_sub(lba, &subc->attr, &subc->track, &subc->index, + &rel_pos, &abs_pos); subc->abs_m = abs_pos.min; subc->abs_s = abs_pos.sec; @@ -91,6 +108,9 @@ cdrom_ioctl_get_subchannel(UNUSED(cdrom_t *dev), uint32_t lba, subchannel_t *sub subc->rel_m = rel_pos.min; subc->rel_s = rel_pos.sec; subc->rel_f = rel_pos.fr; + + cdrom_ioctl_log("ioctl_get_subchannel(): %02i, %02X, %02i, %02i:%02i:%02i, %02i:%02i:%02i\n", + subc->attr, subc->track, subc->index, subc->abs_m, subc->abs_s, subc->abs_f, subc->rel_m, subc->rel_s, subc->rel_f); } static int @@ -99,20 +119,16 @@ cdrom_ioctl_get_capacity(UNUSED(cdrom_t *dev)) int ret; ret = plat_cdrom_get_last_block(); - pclog("GetCapacity=%x.\n", ret); + cdrom_ioctl_log("GetCapacity=%x.\n", ret); return ret; } static int -cdrom_ioctl_is_track_audio(cdrom_t *dev, uint32_t pos, int ismsf) +ioctl_is_track_audio(cdrom_t *dev, uint32_t pos, int ismsf) { - uint8_t attr; - TMSF tmsf; int m; int s; int f; - int number; - int track; if (dev->cd_status == CD_STATUS_DATA_ONLY) return 0; @@ -129,27 +145,30 @@ cdrom_ioctl_is_track_audio(cdrom_t *dev, uint32_t pos, int ismsf) } static int -cdrom_ioctl_is_track_pre(UNUSED(cdrom_t *dev), UNUSED(uint32_t lba)) +ioctl_is_track_pre(UNUSED(cdrom_t *dev), UNUSED(uint32_t lba)) { return 0; } static int -cdrom_ioctl_sector_size(UNUSED(cdrom_t *dev), uint32_t lba) +ioctl_sector_size(UNUSED(cdrom_t *dev), uint32_t lba) { - pclog("LBA=%x.\n", lba); + cdrom_ioctl_log("LBA=%x.\n", lba); return plat_cdrom_get_sector_size(lba); } static int -cdrom_ioctl_read_sector(UNUSED(cdrom_t *dev), int type, uint8_t *b, uint32_t lba) +ioctl_read_sector(UNUSED(cdrom_t *dev), int type, uint8_t *b, uint32_t lba) { switch (type) { case CD_READ_DATA: + cdrom_ioctl_log("cdrom_ioctl_read_sector(): Data.\n"); return plat_cdrom_read_sector(b, 0, lba); case CD_READ_AUDIO: + cdrom_ioctl_log("cdrom_ioctl_read_sector(): Audio.\n"); return plat_cdrom_read_sector(b, 1, lba); case CD_READ_RAW: + cdrom_ioctl_log("cdrom_ioctl_read_sector(): Raw.\n"); return plat_cdrom_read_sector(b, 1, lba); default: cdrom_ioctl_log("cdrom_ioctl_read_sector(): Unknown CD read type.\n"); @@ -159,16 +178,34 @@ cdrom_ioctl_read_sector(UNUSED(cdrom_t *dev), int type, uint8_t *b, uint32_t lba } static int -cdrom_ioctl_track_type(cdrom_t *dev, uint32_t lba) +ioctl_track_type(cdrom_t *dev, uint32_t lba) { - if (cdrom_ioctl_is_track_audio(dev, lba, 0)) - return CD_TRACK_AUDIO; + int ret = 0; - return 0; + if (ioctl_is_track_audio(dev, lba, 0)) + ret = CD_TRACK_AUDIO; + + cdrom_ioctl_log("cdrom_ioctl_track_type(): %i\n", ret); + + return ret; +} + +static int +ioctl_ext_medium_changed(cdrom_t *dev) +{ + const int ret = plat_cdrom_ext_medium_changed(); + + if (ret == 1) { + dev->cd_status = CD_STATUS_STOPPED; + dev->cdrom_capacity = cdrom_ioctl_get_capacity(dev); + } else if (ret == -1) + dev->cd_status = CD_STATUS_EMPTY; + + return ret; } static void -cdrom_ioctl_exit(cdrom_t *dev) +ioctl_exit(cdrom_t *dev) { cdrom_ioctl_log("CDROM: ioctl_exit(%s)\n", dev->image_path); dev->cd_status = CD_STATUS_EMPTY; @@ -179,25 +216,17 @@ cdrom_ioctl_exit(cdrom_t *dev) } static const cdrom_ops_t cdrom_ioctl_ops = { - cdrom_ioctl_get_tracks, - cdrom_ioctl_get_track_info, - cdrom_ioctl_get_subchannel, - cdrom_ioctl_is_track_pre, - cdrom_ioctl_sector_size, - cdrom_ioctl_read_sector, - cdrom_ioctl_track_type, - cdrom_ioctl_exit + ioctl_get_tracks, + ioctl_get_track_info, + ioctl_get_subchannel, + ioctl_is_track_pre, + ioctl_sector_size, + ioctl_read_sector, + ioctl_track_type, + ioctl_ext_medium_changed, + ioctl_exit }; -void -cdrom_ioctl_close(cdrom_t *dev) -{ - cdrom_ioctl_log("CDROM: ioctl_close(%s)\n", dev->image_path); - - if (dev && dev->ops && dev->ops->exit) - dev->ops->exit(dev); -} - static int cdrom_ioctl_open_abort(cdrom_t *dev) { @@ -208,17 +237,21 @@ cdrom_ioctl_open_abort(cdrom_t *dev) return 1; } -void -cdrom_ioctl_eject(void) -{ - plat_cdrom_eject(); -} - int -cdrom_ioctl_open(cdrom_t *dev, int drive) +cdrom_ioctl_open(cdrom_t *dev, const char *drv) { + const char *actual_drv = &(drv[8]); + + /* Make sure to not STRCPY if the two are pointing + at the same place. */ + if (drv != dev->image_path) + strcpy(dev->image_path, drv); + /* Open the image. */ - int i = plat_cdrom_set_drive(drive); + if (strstr(drv, "ioctl://") != drv) + return cdrom_ioctl_open_abort(dev); + cdrom_ioctl_log("actual_drv = %s\n", actual_drv); + int i = plat_cdrom_set_drive(actual_drv); if (!i) return cdrom_ioctl_open_abort(dev); @@ -228,7 +261,8 @@ cdrom_ioctl_open(cdrom_t *dev, int drive) dev->seek_pos = 0; dev->cd_buflen = 0; dev->cdrom_capacity = cdrom_ioctl_get_capacity(dev); - pclog("CD-ROM capacity: %i sectors (%" PRIi64 " bytes)\n", dev->cdrom_capacity, ((uint64_t) dev->cdrom_capacity) << 11ULL); + cdrom_ioctl_log("CD-ROM capacity: %i sectors (%" PRIi64 " bytes)\n", + dev->cdrom_capacity, ((uint64_t) dev->cdrom_capacity) << 11ULL); /* Attach this handler to the drive. */ dev->ops = &cdrom_ioctl_ops; @@ -236,3 +270,12 @@ cdrom_ioctl_open(cdrom_t *dev, int drive) return 0; } +void +cdrom_ioctl_close(cdrom_t *dev) +{ + cdrom_ioctl_log("CDROM: ioctl_close(%s)\n", dev->image_path); + + if (dev && dev->ops && dev->ops->exit) + dev->ops->exit(dev); +} + diff --git a/src/include/86box/cdrom.h b/src/include/86box/cdrom.h index ff0c77930..33f900806 100644 --- a/src/include/86box/cdrom.h +++ b/src/include/86box/cdrom.h @@ -199,10 +199,11 @@ typedef struct cdrom_ops_t { void (*get_tracks)(struct cdrom *dev, int *first, int *last); void (*get_track_info)(struct cdrom *dev, uint32_t track, int end, track_info_t *ti); void (*get_subchannel)(struct cdrom *dev, uint32_t lba, subchannel_t *subc); - int (*is_track_pre)(struct cdrom *dev, uint32_t lba); - int (*sector_size)(struct cdrom *dev, uint32_t lba); - int (*read_sector)(struct cdrom *dev, int type, uint8_t *b, uint32_t lba); - int (*track_type)(struct cdrom *dev, uint32_t lba); + int (*is_track_pre)(struct cdrom *dev, uint32_t lba); + int (*sector_size)(struct cdrom *dev, uint32_t lba); + int (*read_sector)(struct cdrom *dev, int type, uint8_t *b, uint32_t lba); + int (*track_type)(struct cdrom *dev, uint32_t lba); + int (*ext_medium_changed)(struct cdrom *dev); void (*exit)(struct cdrom *dev); } cdrom_ops_t; @@ -247,7 +248,6 @@ typedef struct cdrom { int audio_muted_soft; int sony_msf; int host; - int drive; const cdrom_ops_t *ops; @@ -307,11 +307,9 @@ extern void cdrom_reload(uint8_t id); extern int cdrom_image_open(cdrom_t *dev, const char *fn); extern void cdrom_image_close(cdrom_t *dev); -extern void cdrom_image_reset(cdrom_t *dev); -extern void cdrom_ioctl_eject(void); -extern void cdrom_ioctl_load(void); -extern int cdrom_ioctl_open(cdrom_t *dev, int drive); +extern int cdrom_ioctl_open(cdrom_t *dev, const char *drv); +extern void cdrom_ioctl_close(cdrom_t *dev); extern void cdrom_update_cdb(uint8_t *cdb, int lba_pos, int number_of_blocks); diff --git a/src/include/86box/plat_cdrom.h b/src/include/86box/plat_cdrom.h index 93f9befe5..a21dd357f 100644 --- a/src/include/86box/plat_cdrom.h +++ b/src/include/86box/plat_cdrom.h @@ -49,15 +49,18 @@ typedef struct SMSF { uint8_t fr; } TMSF; -extern int plat_cdrom_is_track_audio(uint32_t sector); -extern int plat_cdrom_get_last_block(void); -extern void plat_cdrom_get_audio_tracks(int *st_track, int *end, TMSF *lead_out); -extern int plat_cdrom_get_audio_track_info(int end, int track, int *track_num, TMSF *start, uint8_t *attr); -extern int plat_cdrom_get_audio_sub(uint32_t sector, uint8_t *attr, uint8_t *track, uint8_t *index, TMSF *rel_pos, TMSF *abs_pos); -extern int plat_cdrom_get_sector_size(uint32_t sector); -extern int plat_cdrom_read_sector(uint8_t *buffer, int raw, uint32_t sector); -extern void plat_cdrom_close(void); -extern int plat_cdrom_set_drive(int drive); +extern int plat_cdrom_is_track_audio(uint32_t sector); +extern uint32_t plat_cdrom_get_last_block(void); +extern void plat_cdrom_get_audio_tracks(int *st_track, int *end, TMSF *lead_out); +extern int plat_cdrom_get_audio_track_info(int end, int track, int *track_num, TMSF *start, uint8_t *attr); +extern int plat_cdrom_get_audio_sub(uint32_t sector, uint8_t *attr, uint8_t *track, uint8_t *index, TMSF *rel_pos, TMSF *abs_pos); +extern int plat_cdrom_get_sector_size(uint32_t sector); +extern int plat_cdrom_read_sector(uint8_t *buffer, int raw, uint32_t sector); +extern void plat_cdrom_eject(void); +extern void plat_cdrom_close(void); +extern int plat_cdrom_set_drive(const char *drv); +extern int plat_cdrom_ext_medium_changed(void); +extern uint32_t plat_cdrom_get_track_start(uint32_t sector, uint8_t *attr, uint8_t *track); #ifdef __cplusplus } diff --git a/src/qt/CMakeLists.txt b/src/qt/CMakeLists.txt index 422989a44..ebaa84be1 100644 --- a/src/qt/CMakeLists.txt +++ b/src/qt/CMakeLists.txt @@ -225,6 +225,10 @@ else() target_sources(plat PRIVATE ../unix/unix_serial_passthrough.c ../unix/unix_netsocket.c) endif() +if(WIN32) + target_sources(plat PRIVATE win_cdrom_ioctl.c) +endif() + if (APPLE) target_sources(ui PRIVATE macos_event_filter.mm) if(MOLTENVK) diff --git a/src/qt/qt_machinestatus.cpp b/src/qt/qt_machinestatus.cpp index a36efd2cd..88375354b 100644 --- a/src/qt/qt_machinestatus.cpp +++ b/src/qt/qt_machinestatus.cpp @@ -533,7 +533,7 @@ MachineStatus::refresh(QStatusBar *sbar) iterateCDROM([this, sbar](int i) { d->cdrom[i].label = std::make_unique(); - d->cdrom[i].setEmpty(((cdrom[i].host_drive != 200) && (cdrom[i].host_drive != 201)) || QString(cdrom[i].image_path).isEmpty()); + d->cdrom[i].setEmpty((cdrom[i].host_drive != 200) || QString(cdrom[i].image_path).isEmpty()); d->cdrom[i].setActive(false); d->cdrom[i].refresh(); connect((ClickableLabel *) d->cdrom[i].label.get(), &ClickableLabel::clicked, [i](QPoint pos) { diff --git a/src/qt/qt_mediahistorymanager.cpp b/src/qt/qt_mediahistorymanager.cpp index 9d003f464..672ccfa8d 100644 --- a/src/qt/qt_mediahistorymanager.cpp +++ b/src/qt/qt_mediahistorymanager.cpp @@ -306,7 +306,7 @@ MediaHistoryManager::removeMissingImages(device_index_list_t &device_history) } // For this check, explicitly prepend `usr_path` to relative paths to account for $CWD platform variances QFileInfo absolute_path = file_info.isRelative() ? QFileInfo(getUsrPath().append(file_info.filePath())) : file_info; - if (!absolute_path.exists()) { + if ((file_info.filePath().left(8) != "ioctl://") && !absolute_path.exists()) { qWarning("Image file %s does not exist - removing from history", qPrintable(file_info.filePath())); checked_path = ""; } diff --git a/src/qt/qt_mediamenu.cpp b/src/qt/qt_mediamenu.cpp index 8405156fb..1a2b0e831 100644 --- a/src/qt/qt_mediamenu.cpp +++ b/src/qt/qt_mediamenu.cpp @@ -146,8 +146,8 @@ MediaMenu::refresh(QMenu *parentMenu) cdromMutePos = menu->children().count(); menu->addAction(QApplication::style()->standardIcon(QStyle::SP_MediaVolumeMuted), tr("&Mute"), [this, i]() { cdromMute(i); })->setCheckable(true); menu->addSeparator(); - menu->addAction(ProgSettings::loadIcon("/cdrom.ico"), tr("&Image..."), [this, i]() { cdrom[i].host = 0; cdrom[i].drive = 0; cdromMount(i, 0); })->setCheckable(false); - menu->addAction(QApplication::style()->standardIcon(QStyle::SP_DirIcon), tr("&Folder..."), [this, i]() { cdrom[i].host = 0; cdrom[i].drive = 0; cdromMount(i, 1); })->setCheckable(false); + menu->addAction(ProgSettings::loadIcon("/cdrom.ico"), tr("&Image..."), [this, i]() { cdromMount(i, 0, nullptr); })->setCheckable(false); + menu->addAction(QApplication::style()->standardIcon(QStyle::SP_DirIcon), tr("&Folder..."), [this, i]() { cdromMount(i, 1, nullptr); })->setCheckable(false); menu->addSeparator(); for (int slot = 0; slot < MAX_PREV_IMAGES; slot++) { cdromImageHistoryPos[slot] = menu->children().count(); @@ -159,12 +159,11 @@ MediaMenu::refresh(QMenu *parentMenu) it's a CDROM */ for (auto &letter : driveLetters) { auto drive = QString::asprintf("%c:\\", letter).toUtf8().constData(); - if (GetDriveType(drive) == DRIVE_CDROM) { - menu->addAction(ProgSettings::loadIcon("/cdrom.ico"), tr("Host CD/DVD Drive (%1:)").arg(letter), [this, i, letter]() { cdrom[i].host = 1; cdrom[i].drive = letter; cdromMount(i, 0); })->setCheckable(false); - } + if (GetDriveType(drive) == DRIVE_CDROM) + menu->addAction(QApplication::style()->standardIcon(QStyle::SP_DriveCDIcon), tr("Host CD/DVD Drive (%1:)").arg(letter), [this, i, letter]() { cdromMount(i, 2, QString::asprintf(R"(\\.\%c:)", letter).toUtf8().constData()); })->setCheckable(false); } menu->addSeparator(); -#endif // _WIN32 +#endif // Q_OS_WINDOWS cdromImagePos = menu->children().count(); cdromDirPos = menu->children().count(); menu->addAction(tr("E&ject"), [this, i]() { cdromEject(i); })->setCheckable(false); @@ -486,30 +485,27 @@ MediaMenu::cdromMount(int i, const QString &filename) cdrom[i].ops = nullptr; memset(cdrom[i].image_path, 0, sizeof(cdrom[i].image_path)); -#ifdef _WIN32 - if (!cdrom[i].host && (fn.data() != NULL) && (strlen(fn.data()) >= 1) && (fn.data()[strlen(fn.data()) - 1] == '/')) +#ifdef Q_OS_WINDOWS + if (!cdrom[i].host && (fn.data() != nullptr) && (strlen(fn.data()) >= 1) && (fn.data()[strlen(fn.data()) - 1] == '/')) fn.data()[strlen(fn.data()) - 1] = '\\'; #else if ((fn.data() != NULL) && (strlen(fn.data()) >= 1) && (fn.data()[strlen(fn.data()) - 1] == '\\')) fn.data()[strlen(fn.data()) - 1] = '/'; #endif - if (cdrom[i].host) - cdrom_ioctl_open(&(cdrom[i]), cdrom[i].drive); + if ((fn.data() != nullptr) && fn.contains("ioctl://")) + cdrom_ioctl_open(&(cdrom[i]), fn.data()); else cdrom_image_open(&(cdrom[i]), fn.data()); /* Signal media change to the emulated machine. */ if (cdrom[i].insert) cdrom[i].insert(cdrom[i].priv); - cdrom[i].host_drive = (strlen(cdrom[i].image_path) == 0 && !cdrom[i].drive) ? 0 : (200 + (cdrom[i].host ? 1 : 0)); + cdrom[i].host_drive = (strlen(cdrom[i].image_path) == 0) ? 0 : 200; - pclog("HostDrive=%d, drive=%d.\n", cdrom[i].host_drive, cdrom[i].drive); - if (cdrom[i].host_drive >= 200) { + if (cdrom[i].host_drive >= 200) ui_sb_update_icon_state(SB_CDROM | i, 0); - } else { + else ui_sb_update_icon_state(SB_CDROM | i, 1); - } - if (!cdrom[i].host) - mhm.addImageToHistory(i, ui::MediaType::Optical, cdrom[i].prev_image_path, cdrom[i].image_path); + mhm.addImageToHistory(i, ui::MediaType::Optical, cdrom[i].prev_image_path, cdrom[i].image_path); cdromUpdateMenu(i); ui_sb_update_tip(SB_CDROM | i); @@ -517,31 +513,23 @@ MediaMenu::cdromMount(int i, const QString &filename) } void -MediaMenu::cdromMount(int i, int dir) +MediaMenu::cdromMount(int i, int dir, const QString &arg) { QString filename; QFileInfo fi(cdrom[i].image_path); - pclog("IsHost?=%d.\n", cdrom[i].host); - if (cdrom[i].host) { - filename = QString(cdrom[i].drive); - } else { - if (dir) { - filename = QFileDialog::getExistingDirectory( - parentWidget); - } else { - filename = QFileDialog::getOpenFileName( - parentWidget, - QString(), - QString(), - tr("CD-ROM images") % util::DlgFilter({ "iso", "cue" }) % tr("All files") % util::DlgFilter({ "*" }, true)); - } + if (dir > 1) + filename = QString::asprintf(R"(ioctl://%s)", arg.toStdString().c_str()); + else if (dir == 1) + filename = QFileDialog::getExistingDirectory(parentWidget); + else { + filename = QFileDialog::getOpenFileName(parentWidget, QString(), + QString(), + tr("CD-ROM images") % util::DlgFilter({ "iso", "cue" }) % tr("All files") % util::DlgFilter({ "*" }, true)); } - if (filename.isEmpty()) { - pclog("File is empty.\n"); + if (filename.isEmpty()) return; - } cdromMount(i, filename); } @@ -589,6 +577,9 @@ MediaMenu::updateImageHistory(int index, int slot, ui::MediaType type) QObjectList children; QFileInfo fi; QIcon menu_icon; + const auto fn = mhm.getImageForSlot(index, slot, type); + + QString menu_item_name; switch (type) { case ui::MediaType::Optical: @@ -597,8 +588,18 @@ MediaMenu::updateImageHistory(int index, int slot, ui::MediaType type) menu = cdromMenus[index]; children = menu->children(); imageHistoryUpdatePos = dynamic_cast(children[cdromImageHistoryPos[slot]]); - fi.setFile(mhm.getImageForSlot(index, slot, type)); - menu_icon = fi.isDir() ? QApplication::style()->standardIcon(QStyle::SP_DirIcon) : ProgSettings::loadIcon("/cdrom.ico"); + if (fn.left(8) == "ioctl://") { + menu_icon = QApplication::style()->standardIcon(QStyle::SP_DriveCDIcon); +#ifdef Q_OS_WINDOWS + menu_item_name = tr("Host CD/DVD Drive (%1)").arg(fn.right(2)).toUtf8().constData(); +#else + menu_item_name = tr("Host CD/DVD Drive (%1)").arg(fn.right(name.length() - 8)); +#endif + } else { + fi.setFile(fn); + menu_icon = fi.isDir() ? QApplication::style()->standardIcon(QStyle::SP_DirIcon) : ProgSettings::loadIcon("/cdrom.ico"); + menu_item_name = fn.isEmpty() ? tr("previous image").toUtf8().constData() : fn.toUtf8().constData(); + } imageHistoryUpdatePos->setIcon(menu_icon); break; case ui::MediaType::Floppy: @@ -607,17 +608,20 @@ MediaMenu::updateImageHistory(int index, int slot, ui::MediaType type) menu = floppyMenus[index]; children = menu->children(); imageHistoryUpdatePos = dynamic_cast(children[floppyImageHistoryPos[slot]]); - fi.setFile(mhm.getImageForSlot(index, slot, type)); + fi.setFile(fn); + menu_item_name = fi.fileName().isEmpty() ? tr("previous image").toUtf8().constData() : fi.fileName().toUtf8().constData(); break; default: - pclog("History not yet implemented for media type %s\n", qPrintable(mhm.mediaTypeToString(type))); + menu_item_name = fi.fileName().isEmpty() ? tr("previous image").toUtf8().constData() : fi.fileName().toUtf8().constData(); return; } - const QString menu_item_name = fi.fileName().isEmpty() ? tr("previous image").toUtf8().constData() : fi.fileName().toUtf8().constData(); imageHistoryUpdatePos->setText(QString::asprintf(tr("%s").toUtf8().constData(), menu_item_name.toUtf8().constData())); - imageHistoryUpdatePos->setVisible(!fi.fileName().isEmpty()); - imageHistoryUpdatePos->setVisible(fi.exists()); + + if (fn.left(8) == "ioctl://") + imageHistoryUpdatePos->setVisible(true); + else + imageHistoryUpdatePos->setVisible(!fn.isEmpty() && fi.exists()); } void @@ -630,8 +634,9 @@ MediaMenu::clearImageHistory() void MediaMenu::cdromUpdateMenu(int i) { - QString name = cdrom[i].image_path; - QFileInfo fi(cdrom[i].image_path); + QString name = cdrom[i].image_path; + QString name2; + QIcon menu_icon; if (!cdromMenus.contains(i)) return; @@ -644,14 +649,27 @@ MediaMenu::cdromUpdateMenu(int i) auto *imageMenu = dynamic_cast(childs[cdromImagePos]); imageMenu->setEnabled(!name.isEmpty()); - const QString menu_item_name = name.isEmpty() ? QString().toUtf8().constData() : fi.fileName().toUtf8().constData(); - const auto menu_icon = fi.isDir() ? QApplication::style()->standardIcon(QStyle::SP_DirIcon) : ProgSettings::loadIcon("/cdrom.ico"); + QString menu_item_name; + if (name.left(8) == "ioctl://") { +#ifdef Q_OS_WINDOWS + menu_item_name = tr("Host CD/DVD Drive (%1)").arg(name.right(2)).toUtf8().constData(); +#else + menu_item_name = tr("Host CD/DVD Drive (%1)").arg(name.right(name.length() - 8)); +#endif + name2 = menu_item_name; + menu_icon = QApplication::style()->standardIcon(QStyle::SP_DriveCDIcon); + } else { + QFileInfo fi(cdrom[i].image_path); + + menu_item_name = name.isEmpty() ? QString().toUtf8().constData() : name.toUtf8().constData(); + name2 = name; + menu_icon = fi.isDir() ? QApplication::style()->standardIcon(QStyle::SP_DirIcon) : ProgSettings::loadIcon("/cdrom.ico"); + } imageMenu->setIcon(menu_icon); imageMenu->setText(QString::asprintf(tr("Eject %s").toUtf8().constData(), menu_item_name.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) { @@ -669,7 +687,7 @@ MediaMenu::cdromUpdateMenu(int i) } // menu->setTitle(tr("CD-ROM %1 (%2): %3").arg(QString::number(i+1), busName, name.isEmpty() ? tr("(empty)") : name)); - menu->setTitle(QString::asprintf(tr("CD-ROM %i (%s): %s").toUtf8().constData(), i + 1, busName.toUtf8().data(), name.isEmpty() ? tr("(empty)").toUtf8().data() : name.toUtf8().data())); + menu->setTitle(QString::asprintf(tr("CD-ROM %i (%s): %s").toUtf8().constData(), i + 1, busName.toUtf8().data(), name.isEmpty() ? tr("(empty)").toUtf8().data() : name2.toUtf8().data())); } void diff --git a/src/qt/qt_mediamenu.hpp b/src/qt/qt_mediamenu.hpp index 2e448086f..725ce92b3 100644 --- a/src/qt/qt_mediamenu.hpp +++ b/src/qt/qt_mediamenu.hpp @@ -41,7 +41,7 @@ public: void floppyUpdateMenu(int i); void cdromMute(int i); - void cdromMount(int i, int dir); + void cdromMount(int i, int dir, const QString &arg); void cdromMount(int i, const QString &filename); void cdromEject(int i); void cdromReload(int index, int slot); diff --git a/src/qt/qt_platform.cpp b/src/qt/qt_platform.cpp index e0b11ad7b..91826ed83 100644 --- a/src/qt/qt_platform.cpp +++ b/src/qt/qt_platform.cpp @@ -278,7 +278,7 @@ int path_abs(char *path) { #ifdef Q_OS_WINDOWS - if ((path[1] == ':') || (path[0] == '\\') || (path[0] == '/')) + if ((path[1] == ':') || (path[0] == '\\') || (path[0] == '/') || (strstr(path, "ioctl://") == path)) return 1; return 0; @@ -291,10 +291,13 @@ void path_normalize(char *path) { #ifdef Q_OS_WINDOWS - while (*path++ != 0) { - if (*path == '\\') - *path = '/'; - } + if (strstr(path, "ioctl://") != path) { + while (*path++ != 0) { + if (*path == '\\') + *path = '/'; + } + } else + path[8] = path[9] = path[11] = '\\'; #endif } diff --git a/src/qt/win_cdrom_ioctl.c b/src/qt/win_cdrom_ioctl.c index 9f02e8d27..36838820a 100644 --- a/src/qt/win_cdrom_ioctl.c +++ b/src/qt/win_cdrom_ioctl.c @@ -21,11 +21,17 @@ #include #include #undef BITMAP +#include #include #include "ntddcdrm.h" #include "ntddscsi.h" -#include +#include #include +#include +#include +#include +#include +#define HAVE_STDARG_H #include <86box/86box.h> #include <86box/scsi_device.h> #include <86box/cdrom.h> @@ -37,8 +43,29 @@ of the audio while audio still plays. With an absolute conversion, the counter is fine. */ #define MSFtoLBA(m, s, f) ((((m * 60) + s) * 75) + f) -static HANDLE handle; -static WCHAR ioctl_path[256]; +static int toc_valid = 0; +static CDROM_TOC cur_toc = { 0 }; +static HANDLE handle = NULL; +static WCHAR ioctl_path[256] = { 0 }; +static WCHAR old_ioctl_path[256] = { 0 }; + +#ifdef ENABLE_WIN_CDROM_IOCTL_LOG +int win_cdrom_ioctl_do_log = ENABLE_WIN_CDROM_IOCTL_LOG; + +void +win_cdrom_ioctl_log(const char *fmt, ...) +{ + va_list ap; + + if (win_cdrom_ioctl_do_log) { + va_start(ap, fmt); + pclog_ex(fmt, ap); + va_end(ap); + } +} +#else +# define win_cdrom_ioctl_log(fmt, ...) +#endif static int plat_cdrom_open(void) @@ -46,7 +73,7 @@ plat_cdrom_open(void) plat_cdrom_close(); handle = CreateFileW((LPCWSTR)ioctl_path, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL); - pclog("handle=%p, error=%x.\n", handle, GetLastError()); + win_cdrom_ioctl_log("handle=%p, error=%x\n", handle, (unsigned int) GetLastError()); if (handle != INVALID_HANDLE_VALUE) { long size; DeviceIoControl(handle, IOCTL_STORAGE_LOAD_MEDIA, NULL, 0, NULL, 0, (LPDWORD)&size, NULL); @@ -55,88 +82,166 @@ plat_cdrom_open(void) return 0; } -int -plat_cdrom_is_track_audio(uint32_t sector) +static void +plat_cdrom_read_toc(void) { - CDROM_TOC toc; - long size = 0; - int ret; - int control = 0; - uint32_t track_addr = 0; + long size = 0; - plat_cdrom_open(); - DeviceIoControl(handle, IOCTL_CDROM_READ_TOC, NULL, 0, &toc, sizeof(toc), (LPDWORD)&size, NULL); - plat_cdrom_close(); - - for (int c = 0; toc.TrackData[c].TrackNumber != 0xaa; c++) { - track_addr = MSFtoLBA(toc.TrackData[c].Address[1], toc.TrackData[c].Address[2], toc.TrackData[c].Address[3]); - if ((toc.TrackData[c].TrackNumber >= toc.FirstTrack) && (toc.TrackData[c].TrackNumber <= toc.LastTrack) && - (track_addr >= sector)) - control = toc.TrackData[c].Control; - break; + if (!toc_valid) { + toc_valid = 1; + plat_cdrom_open(); + DeviceIoControl(handle, IOCTL_CDROM_READ_TOC, NULL, 0, &cur_toc, sizeof(cur_toc), (LPDWORD)&size, NULL); + plat_cdrom_close(); } - ret = (control & 0x04) ? 0 : 1; - return ret; } int +plat_cdrom_is_track_audio(uint32_t sector) +{ + int control = 0; + uint32_t track_addr = 0; + uint32_t next_track_addr = 0; + + plat_cdrom_read_toc(); + + for (int c = 0; cur_toc.TrackData[c].TrackNumber != 0xaa; c++) { + track_addr = MSFtoLBA(cur_toc.TrackData[c].Address[1], cur_toc.TrackData[c].Address[2], cur_toc.TrackData[c].Address[3]) - 150; + next_track_addr = MSFtoLBA(cur_toc.TrackData[c + 1].Address[1], cur_toc.TrackData[c + 1].Address[2], cur_toc.TrackData[c + 1].Address[3]) - 150; + win_cdrom_ioctl_log("F: %i, L: %i, C: %i (%i), c: %02X, A: %08X, S: %08X\n", + cur_toc.FirstTrack, cur_toc.LastTrack, + cur_toc.TrackData[c].TrackNumber, c, + cur_toc.TrackData[c].Control, track_addr, sector); + if ((cur_toc.TrackData[c].TrackNumber >= cur_toc.FirstTrack) && (cur_toc.TrackData[c].TrackNumber <= cur_toc.LastTrack) && + (sector >= track_addr) && (sector <= next_track_addr)) { + control = cur_toc.TrackData[c].Control; + break; + } + } + + const int ret = !(control & 0x04); + + win_cdrom_ioctl_log("plat_cdrom_is_track_audio(%08X): %i\n", sector, ret); + + return ret; +} + +uint32_t +plat_cdrom_get_track_start(uint32_t sector, uint8_t *attr, uint8_t *track) +{ + uint32_t track_addr = 0; + uint32_t next_track_addr = 0; + + plat_cdrom_read_toc(); + + for (int c = 0; cur_toc.TrackData[c].TrackNumber != 0xaa; c++) { + track_addr = MSFtoLBA(cur_toc.TrackData[c].Address[1], cur_toc.TrackData[c].Address[2], cur_toc.TrackData[c].Address[3]) - 150; + next_track_addr = MSFtoLBA(cur_toc.TrackData[c + 1].Address[1], cur_toc.TrackData[c + 1].Address[2], cur_toc.TrackData[c + 1].Address[3]) - 150; + win_cdrom_ioctl_log("F: %i, L: %i, C: %i (%i), c: %02X, A: %08X, S: %08X\n", + cur_toc.FirstTrack, cur_toc.LastTrack, + cur_toc.TrackData[c].TrackNumber, c, + cur_toc.TrackData[c].Control, track_addr, sector); + if ((cur_toc.TrackData[c].TrackNumber >= cur_toc.FirstTrack) && (cur_toc.TrackData[c].TrackNumber <= cur_toc.LastTrack) && + (sector >= track_addr) && (sector <= next_track_addr)) { + *track = cur_toc.TrackData[c].TrackNumber; + *attr = cur_toc.TrackData[c].Control; + break; + } + } + + win_cdrom_ioctl_log("plat_cdrom_get_track_start(%08X): %i\n", sector, track_addr); + + return track_addr; +} + +uint32_t plat_cdrom_get_last_block(void) { - CDROM_TOC toc; - int lb = 0; - long size = 0; - uint32_t address = 0; + uint32_t lb = 0; + uint32_t address = 0; - plat_cdrom_open(); - DeviceIoControl(handle, IOCTL_CDROM_READ_TOC, NULL, 0, &toc, sizeof(toc), (LPDWORD)&size, NULL); - plat_cdrom_close(); + plat_cdrom_read_toc(); - for (int c = 0; c <= toc.LastTrack; c++) { - address = MSFtoLBA(toc.TrackData[c].Address[1], toc.TrackData[c].Address[2], toc.TrackData[c].Address[3]); + for (int c = 0; c <= cur_toc.LastTrack; c++) { + address = MSFtoLBA(cur_toc.TrackData[c].Address[1], cur_toc.TrackData[c].Address[2], cur_toc.TrackData[c].Address[3]) - 150; if (address > lb) lb = address; } - pclog("LBCapacity=%d.\n", lb); + win_cdrom_ioctl_log("LBCapacity=%d\n", lb); return lb; } +int +plat_cdrom_ext_medium_changed(void) +{ + long size; + CDROM_TOC toc; + int ret = 0; + + plat_cdrom_open(); + int temp = DeviceIoControl(handle, IOCTL_CDROM_READ_TOC, + NULL, 0, &toc, sizeof(toc), + (LPDWORD)&size, NULL); + plat_cdrom_close(); + + if (!temp) + /* There has been some kind of error - not a medium change, but a not ready + condition. */ + ret = -1; + else if (!toc_valid || (memcmp(ioctl_path, old_ioctl_path, sizeof(ioctl_path)) != 0)) { + /* Changed to a different host drive - we already detect such medium changes. */ + toc_valid = 1; + cur_toc = toc; + if (memcmp(ioctl_path, old_ioctl_path, sizeof(ioctl_path)) != 0) + memcpy(old_ioctl_path, ioctl_path, sizeof(ioctl_path)); + } else if ((toc.TrackData[toc.LastTrack].Address[1] != + cur_toc.TrackData[cur_toc.LastTrack].Address[1]) || + (toc.TrackData[toc.LastTrack].Address[2] != + cur_toc.TrackData[cur_toc.LastTrack].Address[2]) || + (toc.TrackData[toc.LastTrack].Address[3] != + cur_toc.TrackData[cur_toc.LastTrack].Address[3])) + /* The TOC has changed. */ + ret = 1; + + win_cdrom_ioctl_log("plat_cdrom_ext_medium_changed(): %i\n", ret); + + return ret; +} + void plat_cdrom_get_audio_tracks(int *st_track, int *end, TMSF *lead_out) { - CDROM_TOC toc; - long size = 0; - - plat_cdrom_open(); - DeviceIoControl(handle, IOCTL_CDROM_READ_TOC, NULL, 0, &toc, sizeof(toc), (LPDWORD)&size, NULL); - plat_cdrom_close(); + plat_cdrom_read_toc(); *st_track = 1; - *end = toc.LastTrack; - lead_out->min = toc.TrackData[toc.LastTrack].Address[1]; - lead_out->sec = toc.TrackData[toc.LastTrack].Address[2]; - lead_out->fr = toc.TrackData[toc.LastTrack].Address[3]; + *end = cur_toc.LastTrack; + lead_out->min = cur_toc.TrackData[cur_toc.LastTrack].Address[1]; + lead_out->sec = cur_toc.TrackData[cur_toc.LastTrack].Address[2]; + lead_out->fr = cur_toc.TrackData[cur_toc.LastTrack].Address[3]; + + win_cdrom_ioctl_log("plat_cdrom_get_audio_tracks(): %02i, %02i, %02i:%02i:%02i\n", + *st_track, *end, lead_out->min, lead_out->sec, lead_out->fr); } /* This replaces both Info and EndInfo, they are specified by a variable. */ int plat_cdrom_get_audio_track_info(UNUSED(int end), int track, int *track_num, TMSF *start, uint8_t *attr) { - CDROM_TOC toc; - long size = 0; + plat_cdrom_read_toc(); - plat_cdrom_open(); - DeviceIoControl(handle, IOCTL_CDROM_READ_TOC, NULL, 0, &toc, sizeof(toc), (LPDWORD)&size, NULL); - plat_cdrom_close(); - - if ((track < 1) || (track == 0xaa) || (track > (toc.LastTrack))) + if ((track < 1) || (track == 0xaa) || (track > (cur_toc.LastTrack + 1))) { + win_cdrom_ioctl_log("plat_cdrom_get_audio_track_info(%02i)\n", track); return 0; + } - start->min = toc.TrackData[track - 1].Address[1]; - start->sec = toc.TrackData[track - 1].Address[2]; - start->fr = toc.TrackData[track - 1].Address[3]; + start->min = cur_toc.TrackData[track - 1].Address[1]; + start->sec = cur_toc.TrackData[track - 1].Address[2]; + start->fr = cur_toc.TrackData[track - 1].Address[3]; - *track_num = toc.TrackData[track - 1].TrackNumber; - *attr = toc.TrackData[track - 1].Control; + *track_num = cur_toc.TrackData[track - 1].TrackNumber; + *attr = cur_toc.TrackData[track - 1].Control; + + win_cdrom_ioctl_log("plat_cdrom_get_audio_track_info(%02i): %02i:%02i:%02i, %02i, %02X\n", + track, start->min, start->sec, start->fr, *track_num, *attr); return 1; } @@ -145,14 +250,14 @@ plat_cdrom_get_audio_track_info(UNUSED(int end), int track, int *track_num, TMSF int plat_cdrom_get_audio_sub(UNUSED(uint32_t sector), uint8_t *attr, uint8_t *track, uint8_t *index, TMSF *rel_pos, TMSF *abs_pos) { - CDROM_SUB_Q_DATA_FORMAT insub; - SUB_Q_CHANNEL_DATA sub; - long size = 0; + CDROM_SUB_Q_DATA_FORMAT insub; + SUB_Q_CHANNEL_DATA sub; + long size = 0; insub.Format = IOCTL_CDROM_CURRENT_POSITION; - plat_cdrom_open(); - DeviceIoControl(handle, IOCTL_CDROM_READ_Q_CHANNEL, &insub, sizeof(insub), &sub, sizeof(sub), (LPDWORD)&size, NULL); + plat_cdrom_open(); + DeviceIoControl(handle, IOCTL_CDROM_READ_Q_CHANNEL, &insub, sizeof(insub), &sub, sizeof(sub), (LPDWORD)&size, NULL); plat_cdrom_close(); if (sub.CurrentPosition.TrackNumber < 1) @@ -162,12 +267,15 @@ plat_cdrom_get_audio_sub(UNUSED(uint32_t sector), uint8_t *attr, uint8_t *track, *attr = sub.CurrentPosition.Control; *index = sub.CurrentPosition.IndexNumber; - rel_pos->min = sub.CurrentPosition.TrackRelativeAddress[1]; - rel_pos->sec = sub.CurrentPosition.TrackRelativeAddress[2]; - rel_pos->fr = sub.CurrentPosition.TrackRelativeAddress[3]; - abs_pos->min = sub.CurrentPosition.AbsoluteAddress[1]; - abs_pos->sec = sub.CurrentPosition.AbsoluteAddress[2]; - abs_pos->fr = sub.CurrentPosition.AbsoluteAddress[3]; + rel_pos->min = sub.CurrentPosition.TrackRelativeAddress[1]; + rel_pos->sec = sub.CurrentPosition.TrackRelativeAddress[2]; + rel_pos->fr = sub.CurrentPosition.TrackRelativeAddress[3]; + abs_pos->min = sub.CurrentPosition.AbsoluteAddress[1]; + abs_pos->sec = sub.CurrentPosition.AbsoluteAddress[2]; + abs_pos->fr = sub.CurrentPosition.AbsoluteAddress[3]; + + win_cdrom_ioctl_log("plat_cdrom_get_audio_sub(): %02i, %02X, %02i, %02i:%02i:%02i, %02i:%02i:%02i\n", + *track, *attr, *index, rel_pos->min, rel_pos->sec, rel_pos->fr, abs_pos->min, abs_pos->sec, abs_pos->fr); return 1; } @@ -179,55 +287,55 @@ plat_cdrom_get_sector_size(UNUSED(uint32_t sector)) DISK_GEOMETRY dgCDROM; plat_cdrom_open(); - DeviceIoControl(handle, IOCTL_CDROM_GET_DRIVE_GEOMETRY, NULL, 0, &dgCDROM, sizeof(dgCDROM), (LPDWORD)&size, NULL); - plat_cdrom_close(); + DeviceIoControl(handle, IOCTL_CDROM_GET_DRIVE_GEOMETRY, NULL, 0, &dgCDROM, sizeof(dgCDROM), (LPDWORD)&size, NULL); + plat_cdrom_close(); - pclog("BytesPerSector=%d.\n", dgCDROM.BytesPerSector); + win_cdrom_ioctl_log("BytesPerSector=%d\n", dgCDROM.BytesPerSector); return dgCDROM.BytesPerSector; } int plat_cdrom_read_sector(uint8_t *buffer, int raw, uint32_t sector) { - LARGE_INTEGER pos; BOOL status; - long size = 0; + long size = 0; + int buflen = raw ? RAW_SECTOR_SIZE : COOKED_SECTOR_SIZE; - int buflen = raw ? RAW_SECTOR_SIZE : COOKED_SECTOR_SIZE; + plat_cdrom_open(); - plat_cdrom_open(); - if (!raw) { - pclog("Cooked.\n"); - // Cooked - int success = 0; - DWORD newPos = SetFilePointer(handle, sector * COOKED_SECTOR_SIZE, 0, FILE_BEGIN); - if (newPos != 0xFFFFFFFF) + if (!raw) { + win_cdrom_ioctl_log("Cooked\n"); + /* Cooked */ + int success = 0; + DWORD newPos = SetFilePointer(handle, sector * COOKED_SECTOR_SIZE, 0, FILE_BEGIN); + if (newPos != 0xFFFFFFFF) success = ReadFile(handle, buffer, buflen, (LPDWORD)&size, NULL); - status = (success != 0); - } else { - pclog("Raw.\n"); - // Raw - RAW_READ_INFO in; - in.DiskOffset.LowPart = sector * COOKED_SECTOR_SIZE; - in.DiskOffset.HighPart = 0; - in.SectorCount = 1; - in.TrackMode = CDDA; - status = DeviceIoControl(handle, IOCTL_CDROM_RAW_READ, &in, sizeof(in), - buffer, buflen, (LPDWORD)&size, NULL); - } + status = (success != 0); + } else { + win_cdrom_ioctl_log("Raw\n"); + /* Raw */ + RAW_READ_INFO in; + in.DiskOffset.LowPart = sector * COOKED_SECTOR_SIZE; + in.DiskOffset.HighPart = 0; + in.SectorCount = 1; + in.TrackMode = CDDA; + status = DeviceIoControl(handle, IOCTL_CDROM_RAW_READ, &in, sizeof(in), + buffer, buflen, (LPDWORD)&size, NULL); + } plat_cdrom_close(); - pclog("ReadSector status=%d, sector=%d, size=%d.\n", status, sector, size); - return (size == buflen) && (status > 0); + win_cdrom_ioctl_log("ReadSector status=%d, sector=%d, size=%d.\n", status, sector, size); + + return (size == buflen) && (status > 0); } void plat_cdrom_eject(void) { - long size; - int ret; + long size; plat_cdrom_open(); DeviceIoControl(handle, IOCTL_STORAGE_EJECT_MEDIA, NULL, 0, NULL, 0, (LPDWORD)&size, NULL); + plat_cdrom_close(); } @@ -241,12 +349,17 @@ plat_cdrom_close(void) } int -plat_cdrom_set_drive(int drive) +plat_cdrom_set_drive(const char *drv) { plat_cdrom_close(); - wsprintf(ioctl_path, L"\\\\.\\%c:", drive); - pclog("Path is %s\n", ioctl_path); + memcpy(old_ioctl_path, ioctl_path, sizeof(ioctl_path)); + memset(ioctl_path, 0x00, sizeof(ioctl_path)); + + wsprintf(ioctl_path, L"%S", drv); + win_cdrom_ioctl_log("Path is %S\n", ioctl_path); + + toc_valid = 0; plat_cdrom_open(); return 1; diff --git a/src/scsi/scsi_cdrom.c b/src/scsi/scsi_cdrom.c index 47ba0535e..12840f624 100644 --- a/src/scsi/scsi_cdrom.c +++ b/src/scsi/scsi_cdrom.c @@ -1557,6 +1557,10 @@ static int scsi_cdrom_pre_execution_check(scsi_cdrom_t *dev, uint8_t *cdb) { int ready = 0; + int ext_medium_changed = 0; + + if (dev->drv && dev->drv->ops && dev->drv->ops->ext_medium_changed) + ext_medium_changed = dev->drv->ops->ext_medium_changed(dev->drv); if ((cdb[0] != GPCMD_REQUEST_SENSE) && (dev->cur_lun == SCSI_LUN_USE_CDB) && (cdb[1] & 0xe0)) { scsi_cdrom_log("CD-ROM %i: Attempting to execute a unknown command targeted at SCSI LUN %i\n", @@ -1590,10 +1594,10 @@ scsi_cdrom_pre_execution_check(scsi_cdrom_t *dev, uint8_t *cdb) goto skip_ready_check; } - if (dev->drv->cd_status & CD_STATUS_MEDIUM_CHANGED) + if ((dev->drv->cd_status & CD_STATUS_MEDIUM_CHANGED) || (ext_medium_changed == 1)) scsi_cdrom_insert((void *) dev); - ready = (dev->drv->cd_status != CD_STATUS_EMPTY); + ready = (dev->drv->cd_status != CD_STATUS_EMPTY) || (ext_medium_changed == -1); skip_ready_check: /* If the drive is not ready, there is no reason to keep the From 9dc92bc1d46129296ec5ec33bb719c0c428f2568 Mon Sep 17 00:00:00 2001 From: OBattler Date: Mon, 20 May 2024 01:57:15 +0200 Subject: [PATCH 5/7] More IOCTL fixes - audio now works correctly. --- src/cdrom/cdrom_image.c | 3 ++ src/cdrom/cdrom_ioctl.c | 36 ++++++++-------- src/include/86box/plat_cdrom.h | 1 + src/qt/win_cdrom_ioctl.c | 75 +++++++++++++++++++++++++++------- 4 files changed, 81 insertions(+), 34 deletions(-) diff --git a/src/cdrom/cdrom_image.c b/src/cdrom/cdrom_image.c index dff3a9105..be6779f9b 100644 --- a/src/cdrom/cdrom_image.c +++ b/src/cdrom/cdrom_image.c @@ -97,6 +97,9 @@ image_get_subchannel(cdrom_t *dev, uint32_t lba, subchannel_t *subc) subc->rel_m = rel_pos.min; subc->rel_s = rel_pos.sec; subc->rel_f = rel_pos.fr; + + cdrom_image_log("image_get_subchannel(): %02X, %02X, %02i, %02i:%02i:%02i, %02i:%02i:%02i\n", + subc->attr, subc->track, subc->index, subc->abs_m, subc->abs_s, subc->abs_f, subc->rel_m, subc->rel_s, subc->rel_f); } static int diff --git a/src/cdrom/cdrom_ioctl.c b/src/cdrom/cdrom_ioctl.c index dae9abaad..c1bc6569e 100644 --- a/src/cdrom/cdrom_ioctl.c +++ b/src/cdrom/cdrom_ioctl.c @@ -82,19 +82,12 @@ ioctl_get_subchannel(UNUSED(cdrom_t *dev), uint32_t lba, subchannel_t *subc) TMSF abs_pos; if ((dev->cd_status == CD_STATUS_PLAYING) || (dev->cd_status == CD_STATUS_PAUSED)) { - uint32_t dat = lba + 150; - abs_pos.fr = dat % 75; - dat /= 75; - abs_pos.sec = dat % 60; - dat /= 60; - abs_pos.min = dat; + const uint32_t trk = plat_cdrom_get_track_start(lba, &subc->attr, &subc->track); - dat = lba - plat_cdrom_get_track_start(lba, &subc->attr, &subc->track); - rel_pos.fr = dat % 75; - dat /= 75; - rel_pos.sec = dat % 60; - dat /= 60; - rel_pos.min = dat; + FRAMES_TO_MSF(lba + 150, &abs_pos.min, &abs_pos.sec, &abs_pos.fr); + + /* Absolute position should be adjusted by 150, not the relative ones. */ + FRAMES_TO_MSF(lba - trk, &rel_pos.min, &rel_pos.sec, &rel_pos.fr); subc->index = 1; } else @@ -109,12 +102,12 @@ ioctl_get_subchannel(UNUSED(cdrom_t *dev), uint32_t lba, subchannel_t *subc) subc->rel_s = rel_pos.sec; subc->rel_f = rel_pos.fr; - cdrom_ioctl_log("ioctl_get_subchannel(): %02i, %02X, %02i, %02i:%02i:%02i, %02i:%02i:%02i\n", + cdrom_ioctl_log("ioctl_get_subchannel(): %02X, %02X, %02i, %02i:%02i:%02i, %02i:%02i:%02i\n", subc->attr, subc->track, subc->index, subc->abs_m, subc->abs_s, subc->abs_f, subc->rel_m, subc->rel_s, subc->rel_f); } static int -cdrom_ioctl_get_capacity(UNUSED(cdrom_t *dev)) +ioctl_get_capacity(UNUSED(cdrom_t *dev)) { int ret; @@ -145,9 +138,9 @@ ioctl_is_track_audio(cdrom_t *dev, uint32_t pos, int ismsf) } static int -ioctl_is_track_pre(UNUSED(cdrom_t *dev), UNUSED(uint32_t lba)) +ioctl_is_track_pre(UNUSED(cdrom_t *dev), uint32_t lba) { - return 0; + return plat_cdrom_is_track_pre(lba); } static int @@ -193,11 +186,16 @@ ioctl_track_type(cdrom_t *dev, uint32_t lba) static int ioctl_ext_medium_changed(cdrom_t *dev) { - const int ret = plat_cdrom_ext_medium_changed(); + int ret; + + if ((dev->cd_status == CD_STATUS_PLAYING) || (dev->cd_status == CD_STATUS_PAUSED)) + ret = 0; + else + ret = plat_cdrom_ext_medium_changed(); if (ret == 1) { dev->cd_status = CD_STATUS_STOPPED; - dev->cdrom_capacity = cdrom_ioctl_get_capacity(dev); + dev->cdrom_capacity = ioctl_get_capacity(dev); } else if (ret == -1) dev->cd_status = CD_STATUS_EMPTY; @@ -260,7 +258,7 @@ cdrom_ioctl_open(cdrom_t *dev, const char *drv) dev->is_dir = 0; dev->seek_pos = 0; dev->cd_buflen = 0; - dev->cdrom_capacity = cdrom_ioctl_get_capacity(dev); + dev->cdrom_capacity = ioctl_get_capacity(dev); cdrom_ioctl_log("CD-ROM capacity: %i sectors (%" PRIi64 " bytes)\n", dev->cdrom_capacity, ((uint64_t) dev->cdrom_capacity) << 11ULL); diff --git a/src/include/86box/plat_cdrom.h b/src/include/86box/plat_cdrom.h index a21dd357f..8e81f9459 100644 --- a/src/include/86box/plat_cdrom.h +++ b/src/include/86box/plat_cdrom.h @@ -50,6 +50,7 @@ typedef struct SMSF { } TMSF; extern int plat_cdrom_is_track_audio(uint32_t sector); +extern int plat_cdrom_is_track_pre(uint32_t sector); extern uint32_t plat_cdrom_get_last_block(void); extern void plat_cdrom_get_audio_tracks(int *st_track, int *end, TMSF *lead_out); extern int plat_cdrom_get_audio_track_info(int end, int track, int *track_num, TMSF *start, uint8_t *attr); diff --git a/src/qt/win_cdrom_ioctl.c b/src/qt/win_cdrom_ioctl.c index 36838820a..ff422bcd2 100644 --- a/src/qt/win_cdrom_ioctl.c +++ b/src/qt/win_cdrom_ioctl.c @@ -72,6 +72,17 @@ plat_cdrom_open(void) { plat_cdrom_close(); + handle = CreateFileW((LPCWSTR)ioctl_path, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL); + win_cdrom_ioctl_log("handle=%p, error=%x\n", handle, (unsigned int) GetLastError()); + + return (handle != INVALID_HANDLE_VALUE); +} + +static int +plat_cdrom_load(void) +{ + plat_cdrom_close(); + handle = CreateFileW((LPCWSTR)ioctl_path, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL); win_cdrom_ioctl_log("handle=%p, error=%x\n", handle, (unsigned int) GetLastError()); if (handle != INVALID_HANDLE_VALUE) { @@ -112,7 +123,7 @@ plat_cdrom_is_track_audio(uint32_t sector) cur_toc.TrackData[c].TrackNumber, c, cur_toc.TrackData[c].Control, track_addr, sector); if ((cur_toc.TrackData[c].TrackNumber >= cur_toc.FirstTrack) && (cur_toc.TrackData[c].TrackNumber <= cur_toc.LastTrack) && - (sector >= track_addr) && (sector <= next_track_addr)) { + (sector >= track_addr) && (sector < next_track_addr)) { control = cur_toc.TrackData[c].Control; break; } @@ -125,6 +136,36 @@ plat_cdrom_is_track_audio(uint32_t sector) return ret; } +int +plat_cdrom_is_track_pre(uint32_t sector) +{ + int control = 0; + uint32_t track_addr = 0; + uint32_t next_track_addr = 0; + + plat_cdrom_read_toc(); + + for (int c = 0; cur_toc.TrackData[c].TrackNumber != 0xaa; c++) { + track_addr = MSFtoLBA(cur_toc.TrackData[c].Address[1], cur_toc.TrackData[c].Address[2], cur_toc.TrackData[c].Address[3]) - 150; + next_track_addr = MSFtoLBA(cur_toc.TrackData[c + 1].Address[1], cur_toc.TrackData[c + 1].Address[2], cur_toc.TrackData[c + 1].Address[3]) - 150; + win_cdrom_ioctl_log("F: %i, L: %i, C: %i (%i), c: %02X, A: %08X, S: %08X\n", + cur_toc.FirstTrack, cur_toc.LastTrack, + cur_toc.TrackData[c].TrackNumber, c, + cur_toc.TrackData[c].Control, track_addr, sector); + if ((cur_toc.TrackData[c].TrackNumber >= cur_toc.FirstTrack) && (cur_toc.TrackData[c].TrackNumber <= cur_toc.LastTrack) && + (sector >= track_addr) && (sector < next_track_addr)) { + control = cur_toc.TrackData[c].Control; + break; + } + } + + const int ret = (control & 0x01); + + win_cdrom_ioctl_log("plat_cdrom_is_track_audio(%08X): %i\n", sector, ret); + + return ret; +} + uint32_t plat_cdrom_get_track_start(uint32_t sector, uint8_t *attr, uint8_t *track) { @@ -136,14 +177,16 @@ plat_cdrom_get_track_start(uint32_t sector, uint8_t *attr, uint8_t *track) for (int c = 0; cur_toc.TrackData[c].TrackNumber != 0xaa; c++) { track_addr = MSFtoLBA(cur_toc.TrackData[c].Address[1], cur_toc.TrackData[c].Address[2], cur_toc.TrackData[c].Address[3]) - 150; next_track_addr = MSFtoLBA(cur_toc.TrackData[c + 1].Address[1], cur_toc.TrackData[c + 1].Address[2], cur_toc.TrackData[c + 1].Address[3]) - 150; - win_cdrom_ioctl_log("F: %i, L: %i, C: %i (%i), c: %02X, A: %08X, S: %08X\n", + win_cdrom_ioctl_log("F: %i, L: %i, C: %i (%i), c: %02X, a: %02X, A: %08X, S: %08X\n", cur_toc.FirstTrack, cur_toc.LastTrack, cur_toc.TrackData[c].TrackNumber, c, - cur_toc.TrackData[c].Control, track_addr, sector); + cur_toc.TrackData[c].Control, cur_toc.TrackData[c].Adr, + track_addr, sector); if ((cur_toc.TrackData[c].TrackNumber >= cur_toc.FirstTrack) && (cur_toc.TrackData[c].TrackNumber <= cur_toc.LastTrack) && - (sector >= track_addr) && (sector <= next_track_addr)) { + (sector >= track_addr) && (sector < next_track_addr)) { *track = cur_toc.TrackData[c].TrackNumber; *attr = cur_toc.TrackData[c].Control; + *attr |= ((cur_toc.TrackData[c].Adr << 4) & 0xf0); break; } } @@ -239,6 +282,7 @@ plat_cdrom_get_audio_track_info(UNUSED(int end), int track, int *track_num, TMSF *track_num = cur_toc.TrackData[track - 1].TrackNumber; *attr = cur_toc.TrackData[track - 1].Control; + *attr |= ((cur_toc.TrackData[track - 1].Adr << 4) & 0xf0); win_cdrom_ioctl_log("plat_cdrom_get_audio_track_info(%02i): %02i:%02i:%02i, %02i, %02X\n", track, start->min, start->sec, start->fr, *track_num, *attr); @@ -265,6 +309,7 @@ plat_cdrom_get_audio_sub(UNUSED(uint32_t sector), uint8_t *attr, uint8_t *track, *track = sub.CurrentPosition.TrackNumber; *attr = sub.CurrentPosition.Control; + *attr |= ((sub.CurrentPosition.ADR << 4) & 0xf0); *index = sub.CurrentPosition.IndexNumber; rel_pos->min = sub.CurrentPosition.TrackRelativeAddress[1]; @@ -303,15 +348,7 @@ plat_cdrom_read_sector(uint8_t *buffer, int raw, uint32_t sector) plat_cdrom_open(); - if (!raw) { - win_cdrom_ioctl_log("Cooked\n"); - /* Cooked */ - int success = 0; - DWORD newPos = SetFilePointer(handle, sector * COOKED_SECTOR_SIZE, 0, FILE_BEGIN); - if (newPos != 0xFFFFFFFF) - success = ReadFile(handle, buffer, buflen, (LPDWORD)&size, NULL); - status = (success != 0); - } else { + if (raw) { win_cdrom_ioctl_log("Raw\n"); /* Raw */ RAW_READ_INFO in; @@ -321,9 +358,17 @@ plat_cdrom_read_sector(uint8_t *buffer, int raw, uint32_t sector) in.TrackMode = CDDA; status = DeviceIoControl(handle, IOCTL_CDROM_RAW_READ, &in, sizeof(in), buffer, buflen, (LPDWORD)&size, NULL); + } else { + win_cdrom_ioctl_log("Cooked\n"); + /* Cooked */ + int success = 0; + DWORD newPos = SetFilePointer(handle, sector * COOKED_SECTOR_SIZE, 0, FILE_BEGIN); + if (newPos != 0xFFFFFFFF) + success = ReadFile(handle, buffer, buflen, (LPDWORD)&size, NULL); + status = (success != 0); } plat_cdrom_close(); - win_cdrom_ioctl_log("ReadSector status=%d, sector=%d, size=%d.\n", status, sector, size); + win_cdrom_ioctl_log("ReadSector status=%d, sector=%d, size=%" PRId64 ".\n", status, sector, (long long) size); return (size == buflen) && (status > 0); } @@ -361,6 +406,6 @@ plat_cdrom_set_drive(const char *drv) toc_valid = 0; - plat_cdrom_open(); + plat_cdrom_load(); return 1; } From 7c74a977fa965faa94429cde86d9c25733930500 Mon Sep 17 00:00:00 2001 From: OBattler Date: Mon, 20 May 2024 02:15:17 +0200 Subject: [PATCH 6/7] There goes the legacy host_drive. --- src/cdrom/cdrom.c | 25 ++++++++----------------- src/cdrom/cdrom_image.c | 1 - src/cdrom/cdrom_ioctl.c | 1 - src/cdrom/cdrom_mitsumi.c | 20 ++++++++++++-------- src/config.c | 23 ++--------------------- src/include/86box/cdrom.h | 2 -- src/machine_status.c | 6 +----- src/qt/qt_machinestatus.cpp | 2 +- src/qt/qt_mediamenu.cpp | 6 ++---- src/scsi/scsi_cdrom.c | 18 +++++++++--------- src/unix/unix_cdrom.c | 9 +++------ 11 files changed, 38 insertions(+), 75 deletions(-) diff --git a/src/cdrom/cdrom.c b/src/cdrom/cdrom.c index 0da199b52..88811c016 100644 --- a/src/cdrom/cdrom.c +++ b/src/cdrom/cdrom.c @@ -1961,7 +1961,7 @@ cdrom_hard_reset(void) dev->cd_status = CD_STATUS_EMPTY; - if (dev->host_drive == 200) { + if (strlen(dev->image_path) > 0) { #ifdef _WIN32 if ((strlen(dev->image_path) >= 1) && (dev->image_path[strlen(dev->image_path) - 1] == '/')) dev->image_path[strlen(dev->image_path) - 1] = '\\'; @@ -2023,16 +2023,12 @@ cdrom_eject(uint8_t id) cdrom_t *dev = &cdrom[id]; /* This entire block should be in cdrom.c/cdrom_eject(dev*) ... */ - if (dev->host_drive == 0) { + if (strlen(dev->image_path) == 0) { /* Switch from empty to empty. Do nothing. */ return; } - if (dev->host_drive >= 200) - strcpy(dev->prev_image_path, dev->image_path); - - dev->prev_host_drive = dev->host_drive + (dev->host ? 1 : 0); - dev->host_drive = 0; + strcpy(dev->prev_image_path, dev->image_path); dev->ops->exit(dev); dev->ops = NULL; @@ -2051,7 +2047,7 @@ cdrom_reload(uint8_t id) { cdrom_t *dev = &cdrom[id]; - if (!dev->host && ((dev->host_drive == dev->prev_host_drive) || (dev->prev_host_drive == 0) || (dev->host_drive != 0))) { + if ((strcmp(dev->image_path, dev->prev_image_path) == 0) || (strlen(dev->prev_image_path) == 0) || (strlen(dev->image_path) > 0)) { /* Switch from empty to empty. Do nothing. */ return; } @@ -2061,18 +2057,18 @@ cdrom_reload(uint8_t id) dev->ops = NULL; memset(dev->image_path, 0, sizeof(dev->image_path)); - if (dev->prev_host_drive >= 200) { + if (strlen(dev->image_path) > 0) { /* Reload a previous image. */ - if (dev->prev_host_drive == 200) + if (strlen(dev->prev_image_path) > 0) strcpy(dev->image_path, dev->prev_image_path); #ifdef _WIN32 - if (dev->prev_host_drive == 200) { + if (strlen(dev->prev_image_path) > 0) { if ((strlen(dev->image_path) >= 1) && (dev->image_path[strlen(dev->image_path) - 1] == '/')) dev->image_path[strlen(dev->image_path) - 1] = '\\'; } #else - if (dev->prev_host_drive == 200) { + if (strlen(dev->prev_image_path) > 0) { if ((strlen(dev->image_path) >= 1) && (dev->image_path[strlen(dev->image_path) - 1] == '\\')) dev->image_path[strlen(dev->image_path) - 1] = '/'; } @@ -2084,11 +2080,6 @@ cdrom_reload(uint8_t id) cdrom_image_open(dev, dev->image_path); cdrom_insert(id); - - if (strlen(dev->image_path) == 0) - dev->host_drive = 0; - else - dev->host_drive = 200; } plat_cdrom_ui_update(id, 1); diff --git a/src/cdrom/cdrom_image.c b/src/cdrom/cdrom_image.c index be6779f9b..2203674cd 100644 --- a/src/cdrom/cdrom_image.c +++ b/src/cdrom/cdrom_image.c @@ -259,7 +259,6 @@ image_open_abort(cdrom_t *dev) { cdrom_image_close(dev); dev->ops = NULL; - dev->host_drive = 0; dev->image_path[0] = 0; return 1; } diff --git a/src/cdrom/cdrom_ioctl.c b/src/cdrom/cdrom_ioctl.c index c1bc6569e..a204fad0f 100644 --- a/src/cdrom/cdrom_ioctl.c +++ b/src/cdrom/cdrom_ioctl.c @@ -230,7 +230,6 @@ cdrom_ioctl_open_abort(cdrom_t *dev) { cdrom_ioctl_close(dev); dev->ops = NULL; - dev->host_drive = 0; dev->image_path[0] = 0; return 1; } diff --git a/src/cdrom/cdrom_mitsumi.c b/src/cdrom/cdrom_mitsumi.c index 7f4d2645b..01a9cf047 100644 --- a/src/cdrom/cdrom_mitsumi.c +++ b/src/cdrom/cdrom_mitsumi.c @@ -147,14 +147,18 @@ mitsumi_cdrom_log(const char *fmt, ...) # define mitsumi_cdrom_log(fmt, ...) #endif +static int +mitsumi_cdrom_is_ready(const cdrom_t *dev) +{ + return (dev->image_path[0] != 0x00); +} + static void mitsumi_cdrom_reset(mcd_t *dev) { cdrom_t cdrom; - cdrom.host_drive = 0; - - dev->stat = cdrom.host_drive ? (STAT_READY | STAT_CHANGE) : 0; + dev->stat = mitsumi_cdrom_is_ready(&cdrom) ? (STAT_READY | STAT_CHANGE) : 0; dev->cmdrd_count = 0; dev->cmdbuf_count = 0; dev->buf_count = 0; @@ -344,18 +348,18 @@ mitsumi_cdrom_out(uint16_t port, uint8_t val, void *priv) break; } if (!dev->cmdrd_count) - dev->stat = cdrom.host_drive ? (STAT_READY | (dev->change ? STAT_CHANGE : 0)) : 0; + dev->stat = mitsumi_cdrom_is_ready(&cdrom) ? (STAT_READY | (dev->change ? STAT_CHANGE : 0)) : 0; return; } dev->cmd = val; dev->cmdbuf_idx = 0; dev->cmdrd_count = 0; dev->cmdbuf_count = 1; - dev->cmdbuf[0] = cdrom.host_drive ? (STAT_READY | (dev->change ? STAT_CHANGE : 0)) : 0; + dev->cmdbuf[0] = mitsumi_cdrom_is_ready(&cdrom) ? (STAT_READY | (dev->change ? STAT_CHANGE : 0)) : 0; dev->data = 0; switch (val) { case CMD_GET_INFO: - if (cdrom.host_drive) { + if (mitsumi_cdrom_is_ready(&cdrom)) { cdrom_get_track_buffer(&cdrom, &(dev->cmdbuf[1])); dev->cmdbuf_count = 10; dev->readcount = 0; @@ -365,7 +369,7 @@ mitsumi_cdrom_out(uint16_t port, uint8_t val, void *priv) } break; case CMD_GET_Q: - if (cdrom.host_drive) { + if (mitsumi_cdrom_is_ready(&cdrom)) { cdrom_get_q(&cdrom, &(dev->cmdbuf[1]), &dev->cur_toc_track, dev->mode & MODE_GET_TOC); dev->cmdbuf_count = 11; dev->readcount = 0; @@ -391,7 +395,7 @@ mitsumi_cdrom_out(uint16_t port, uint8_t val, void *priv) break; case CMD_READ1X: case CMD_READ2X: - if (cdrom.host_drive) { + if (mitsumi_cdrom_is_ready(&cdrom)) { dev->readcount = 0; dev->drvmode = (val == CMD_READ1X) ? DRV_MODE_CDDA : DRV_MODE_READ; dev->cmdrd_count = 6; diff --git a/src/config.c b/src/config.c index fdecb6cd5..77ec89125 100644 --- a/src/config.c +++ b/src/config.c @@ -1175,8 +1175,7 @@ load_floppy_and_cdrom_drives(void) memset(temp, 0x00, sizeof(temp)); for (c = 0; c < CDROM_NUM; c++) { sprintf(temp, "cdrom_%02i_host_drive", c + 1); - cdrom[c].host_drive = ini_section_get_int(cat, temp, 0); - cdrom[c].prev_host_drive = cdrom[c].host_drive; + ini_section_delete_var(cat, temp); sprintf(temp, "cdrom_%02i_parameters", c + 1); p = ini_section_get_string(cat, temp, NULL); @@ -1263,12 +1262,6 @@ load_floppy_and_cdrom_drives(void) path_normalize(cdrom[c].image_path); } - if (cdrom[c].host_drive && (cdrom[c].host_drive != 200) && (cdrom[c].host_drive != 201)) - cdrom[c].host_drive = 0; - - if ((cdrom[c].host_drive == 0x200) && (strlen(cdrom[c].image_path) == 0)) - cdrom[c].host_drive = 0; - for (int i = 0; i < MAX_PREV_IMAGES; i++) { cdrom[c].image_history[i] = (char *) calloc((MAX_IMAGE_PATH_LEN + 1) << 1, sizeof(char)); sprintf(temp, "cdrom_%02i_image_history_%02i", c + 1, i + 1); @@ -1289,9 +1282,6 @@ load_floppy_and_cdrom_drives(void) /* If the CD-ROM is disabled, delete all its variables. */ if (cdrom[c].bus_type == CDROM_BUS_DISABLED) { - sprintf(temp, "cdrom_%02i_host_drive", c + 1); - ini_section_delete_var(cat, temp); - sprintf(temp, "cdrom_%02i_parameters", c + 1); ini_section_delete_var(cat, temp); @@ -1422,9 +1412,6 @@ load_other_removable_devices(void) /* If the ZIP drive is disabled, delete all its variables. */ if (zip_drives[c].bus_type == ZIP_BUS_DISABLED) { - sprintf(temp, "zip_%02i_host_drive", c + 1); - ini_section_delete_var(cat, temp); - sprintf(temp, "zip_%02i_parameters", c + 1); ini_section_delete_var(cat, temp); @@ -1538,9 +1525,6 @@ load_other_removable_devices(void) /* If the MO drive is disabled, delete all its variables. */ if (mo_drives[c].bus_type == MO_BUS_DISABLED) { - sprintf(temp, "mo_%02i_host_drive", c + 1); - ini_section_delete_var(cat, temp); - sprintf(temp, "mo_%02i_parameters", c + 1); ini_section_delete_var(cat, temp); @@ -2575,10 +2559,7 @@ save_floppy_and_cdrom_drives(void) for (c = 0; c < CDROM_NUM; c++) { sprintf(temp, "cdrom_%02i_host_drive", c + 1); - if ((cdrom[c].bus_type == 0) || ((cdrom[c].host_drive != 200) && (cdrom[c].host_drive != 201))) - ini_section_delete_var(cat, temp); - else - ini_section_set_int(cat, temp, cdrom[c].host_drive); + ini_section_delete_var(cat, temp); sprintf(temp, "cdrom_%02i_speed", c + 1); if ((cdrom[c].bus_type == 0) || (cdrom[c].speed == 8)) diff --git a/src/include/86box/cdrom.h b/src/include/86box/cdrom.h index 33f900806..1702382a3 100644 --- a/src/include/86box/cdrom.h +++ b/src/include/86box/cdrom.h @@ -241,8 +241,6 @@ typedef struct cdrom { uint32_t cd_end; uint32_t type; - int host_drive; - int prev_host_drive; int cd_buflen; int audio_op; int audio_muted_soft; diff --git a/src/machine_status.c b/src/machine_status.c index 03c0ac092..16976fa83 100644 --- a/src/machine_status.c +++ b/src/machine_status.c @@ -33,11 +33,7 @@ machine_status_init(void) machine_status.fdd[i].active = false; } for (size_t i = 0; i < CDROM_NUM; ++i) { - if (cdrom[i].host) - machine_status.cdrom[i].empty = cdrom[i].host_drive != 201; - else - machine_status.cdrom[i].empty = cdrom[i].host_drive != 200 || (strlen(cdrom[i].image_path) == 0); - + machine_status.cdrom[i].empty = (strlen(cdrom[i].image_path) == 0); machine_status.cdrom[i].active = false; } for (size_t i = 0; i < ZIP_NUM; i++) { diff --git a/src/qt/qt_machinestatus.cpp b/src/qt/qt_machinestatus.cpp index 88375354b..a074f556e 100644 --- a/src/qt/qt_machinestatus.cpp +++ b/src/qt/qt_machinestatus.cpp @@ -533,7 +533,7 @@ MachineStatus::refresh(QStatusBar *sbar) iterateCDROM([this, sbar](int i) { d->cdrom[i].label = std::make_unique(); - d->cdrom[i].setEmpty((cdrom[i].host_drive != 200) || QString(cdrom[i].image_path).isEmpty()); + d->cdrom[i].setEmpty(QString(cdrom[i].image_path).isEmpty()); d->cdrom[i].setActive(false); d->cdrom[i].refresh(); connect((ClickableLabel *) d->cdrom[i].label.get(), &ClickableLabel::clicked, [i](QPoint pos) { diff --git a/src/qt/qt_mediamenu.cpp b/src/qt/qt_mediamenu.cpp index 1a2b0e831..de4f5a05e 100644 --- a/src/qt/qt_mediamenu.cpp +++ b/src/qt/qt_mediamenu.cpp @@ -478,7 +478,6 @@ MediaMenu::cdromMount(int i, const QString &filename) { QByteArray fn = filename.toUtf8().data(); - cdrom[i].prev_host_drive = cdrom[i].host_drive; strcpy(cdrom[i].prev_image_path, cdrom[i].image_path); if (cdrom[i].ops && cdrom[i].ops->exit) cdrom[i].ops->exit(&(cdrom[i])); @@ -499,9 +498,8 @@ MediaMenu::cdromMount(int i, const QString &filename) /* Signal media change to the emulated machine. */ if (cdrom[i].insert) cdrom[i].insert(cdrom[i].priv); - cdrom[i].host_drive = (strlen(cdrom[i].image_path) == 0) ? 0 : 200; - if (cdrom[i].host_drive >= 200) + if (strlen(cdrom[i].image_path) > 0) ui_sb_update_icon_state(SB_CDROM | i, 0); else ui_sb_update_icon_state(SB_CDROM | i, 1); @@ -557,7 +555,7 @@ MediaMenu::cdromUpdateUi(int i) { cdrom_t *drv = &cdrom[i]; - if (drv->host_drive == 0) { + if (strlen(cdrom[i].image_path) == 0) { mhm.addImageToHistory(i, ui::MediaType::Optical, drv->prev_image_path, QString()); ui_sb_update_icon_state(SB_CDROM | i, 1); } else { diff --git a/src/scsi/scsi_cdrom.c b/src/scsi/scsi_cdrom.c index 12840f624..262d9600c 100644 --- a/src/scsi/scsi_cdrom.c +++ b/src/scsi/scsi_cdrom.c @@ -1887,7 +1887,7 @@ begin: case GPCMD_AUDIO_SCAN: scsi_cdrom_set_phase(dev, SCSI_PHASE_STATUS); - if ((dev->drv->host_drive < 1) || (dev->drv->cd_status <= CD_STATUS_DATA_ONLY)) { + if ((dev->drv->image_path[0] == 0x00) || (dev->drv->cd_status <= CD_STATUS_DATA_ONLY)) { scsi_cdrom_illegal_mode(dev); break; } @@ -2563,7 +2563,7 @@ begin: case CDROM_TYPE_TOSHIBA_XM5701TA_3136: case CDROM_TYPE_TOSHIBA_SDM1401_1008: /*GPCMD_AUDIO_TRACK_SEARCH_TOSHIBA*/ scsi_cdrom_set_phase(dev, SCSI_PHASE_STATUS); - if ((dev->drv->host_drive < 1) || (dev->drv->cd_status <= CD_STATUS_DATA_ONLY)) { + if ((dev->drv->image_path[0] == 0x00) || (dev->drv->cd_status <= CD_STATUS_DATA_ONLY)) { scsi_cdrom_illegal_mode(dev); break; } @@ -2591,7 +2591,7 @@ begin: case CDROM_TYPE_NEC_211_100: case CDROM_TYPE_NEC_464_105: /*GPCMD_AUDIO_TRACK_SEARCH_NEC*/ scsi_cdrom_set_phase(dev, SCSI_PHASE_STATUS); - if ((dev->drv->host_drive < 1) || (dev->drv->cd_status <= CD_STATUS_DATA_ONLY)) { + if ((dev->drv->image_path[0] == 0x00) || (dev->drv->cd_status <= CD_STATUS_DATA_ONLY)) { scsi_cdrom_illegal_mode(dev); break; } @@ -2670,7 +2670,7 @@ begin: case CDROM_TYPE_TOSHIBA_XM5701TA_3136: case CDROM_TYPE_TOSHIBA_SDM1401_1008: /*GPCMD_PLAY_AUDIO_TOSHIBA*/ scsi_cdrom_set_phase(dev, SCSI_PHASE_STATUS); - if ((dev->drv->host_drive < 1) || (dev->drv->cd_status <= CD_STATUS_DATA_ONLY)) { + if ((dev->drv->image_path[0] == 0x00) || (dev->drv->cd_status <= CD_STATUS_DATA_ONLY)) { scsi_cdrom_illegal_mode(dev); break; } @@ -2697,7 +2697,7 @@ begin: case CDROM_TYPE_NEC_211_100: case CDROM_TYPE_NEC_464_105: /*GPCMD_PLAY_AUDIO_NEC*/ scsi_cdrom_set_phase(dev, SCSI_PHASE_STATUS); - if ((dev->drv->host_drive < 1) || (dev->drv->cd_status <= CD_STATUS_DATA_ONLY)) { + if ((dev->drv->image_path[0] == 0x00) || (dev->drv->cd_status <= CD_STATUS_DATA_ONLY)) { scsi_cdrom_illegal_mode(dev); break; } @@ -2765,7 +2765,7 @@ begin: break; } - if ((dev->drv->host_drive < 1) || (dev->drv->cd_status <= CD_STATUS_DATA_ONLY)) { + if ((dev->drv->image_path[0] == 0x00) || (dev->drv->cd_status <= CD_STATUS_DATA_ONLY)) { scsi_cdrom_illegal_mode(dev); break; } @@ -2882,7 +2882,7 @@ begin: } pos = cdb[4]; - if ((dev->drv->host_drive < 1) || (dev->drv->cd_status <= CD_STATUS_DATA_ONLY)) { + if ((dev->drv->image_path[0] == 0x00) || (dev->drv->cd_status <= CD_STATUS_DATA_ONLY)) { scsi_cdrom_illegal_mode(dev); break; } @@ -3596,7 +3596,7 @@ atapi_out: break; case CDROM_TYPE_PIONEER_DRM604X_2403: /*GPCMD_AUDIO_TRACK_SEARCH_PIONEER*/ scsi_cdrom_set_phase(dev, SCSI_PHASE_STATUS); - if ((dev->drv->host_drive < 1) || (dev->drv->cd_status <= CD_STATUS_DATA_ONLY)) { + if ((dev->drv->image_path[0] == 0x00) || (dev->drv->cd_status <= CD_STATUS_DATA_ONLY)) { scsi_cdrom_illegal_mode(dev); break; } @@ -3646,7 +3646,7 @@ atapi_out: break; case CDROM_TYPE_PIONEER_DRM604X_2403: /*GPCMD_PLAY_AUDIO_PIONEER*/ scsi_cdrom_set_phase(dev, SCSI_PHASE_STATUS); - if ((dev->drv->host_drive < 1) || (dev->drv->cd_status <= CD_STATUS_DATA_ONLY)) { + if ((dev->drv->image_path[0] == 0x00) || (dev->drv->cd_status <= CD_STATUS_DATA_ONLY)) { scsi_cdrom_illegal_mode(dev); break; } diff --git a/src/unix/unix_cdrom.c b/src/unix/unix_cdrom.c index 61813a754..186c7649b 100644 --- a/src/unix/unix_cdrom.c +++ b/src/unix/unix_cdrom.c @@ -124,7 +124,7 @@ plat_cdrom_ui_update(uint8_t id, uint8_t reload) { cdrom_t *drv = &cdrom[id]; - if (drv->host_drive == 0) { + if (drv->image_path[0] == 0x00) { ui_sb_update_icon_state(SB_CDROM | id, 1); } else { ui_sb_update_icon_state(SB_CDROM | id, 0); @@ -139,7 +139,6 @@ plat_cdrom_ui_update(uint8_t id, uint8_t reload) void cdrom_mount(uint8_t id, char *fn) { - cdrom[id].prev_host_drive = cdrom[id].host_drive; strcpy(cdrom[id].prev_image_path, cdrom[id].image_path); if (cdrom[id].ops && cdrom[id].ops->exit) cdrom[id].ops->exit(&(cdrom[id])); @@ -151,12 +150,10 @@ cdrom_mount(uint8_t id, char *fn) /* Signal media change to the emulated machine. */ if (cdrom[id].insert) cdrom[id].insert(cdrom[id].priv); - cdrom[id].host_drive = (strlen(cdrom[id].image_path) == 0) ? 0 : 200; - if (cdrom[id].host_drive == 200) { + if (cdrom[id].image_path[0] == 0x00) ui_sb_update_icon_state(SB_CDROM | id, 0); - } else { + else ui_sb_update_icon_state(SB_CDROM | id, 1); - } #if 0 media_menu_update_cdrom(id); #endif From f8b93dc24e64a89fec60491d5d796db47af133c6 Mon Sep 17 00:00:00 2001 From: OBattler Date: Mon, 20 May 2024 02:17:36 +0200 Subject: [PATCH 7/7] And .host as well. --- src/include/86box/cdrom.h | 1 - src/qt/qt_mediamenu.cpp | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/src/include/86box/cdrom.h b/src/include/86box/cdrom.h index 1702382a3..b3f5a5ea5 100644 --- a/src/include/86box/cdrom.h +++ b/src/include/86box/cdrom.h @@ -245,7 +245,6 @@ typedef struct cdrom { int audio_op; int audio_muted_soft; int sony_msf; - int host; const cdrom_ops_t *ops; diff --git a/src/qt/qt_mediamenu.cpp b/src/qt/qt_mediamenu.cpp index de4f5a05e..d9d6b6c31 100644 --- a/src/qt/qt_mediamenu.cpp +++ b/src/qt/qt_mediamenu.cpp @@ -485,7 +485,7 @@ MediaMenu::cdromMount(int i, const QString &filename) cdrom[i].ops = nullptr; memset(cdrom[i].image_path, 0, sizeof(cdrom[i].image_path)); #ifdef Q_OS_WINDOWS - if (!cdrom[i].host && (fn.data() != nullptr) && (strlen(fn.data()) >= 1) && (fn.data()[strlen(fn.data()) - 1] == '/')) + if ((fn.data() != nullptr) && (strlen(fn.data()) >= 1) && (fn.data()[strlen(fn.data()) - 1] == '/')) fn.data()[strlen(fn.data()) - 1] = '\\'; #else if ((fn.data() != NULL) && (strlen(fn.data()) >= 1) && (fn.data()[strlen(fn.data()) - 1] == '\\'))