Skip to content

Commit

Permalink
refactor: improve IOCTL device detection
Browse files Browse the repository at this point in the history
  • Loading branch information
midwan committed Nov 16, 2024
1 parent e420a8a commit 22d02f0
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 39 deletions.
81 changes: 42 additions & 39 deletions src/osdep/blkdev_ioctl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@
struct dev_info_ioctl {
int fd;
uae_u8* tempbuffer;
TCHAR drvletter;
TCHAR drvlettername[10];
TCHAR drvletter[30];
TCHAR drvlettername[30];
TCHAR devname[30];
int type;
struct cdrom_tocentry cdromtoc;
Expand Down Expand Up @@ -884,7 +884,7 @@ static void update_device_info(int unitnum)
di->lun = 0;
di->media_inserted = 0;
di->bytespersector = 2048;
_stprintf(di->mediapath, _T("\\\\.\\%c:"), ciw->drvletter);
strncpy(di->mediapath, ciw->drvletter, sizeof(ciw->drvletter) - 1);
if (fetch_geometry(ciw, unitnum, di)) { // || ioctl_command_toc (unitnum))
di->media_inserted = 1;
}
Expand Down Expand Up @@ -930,7 +930,7 @@ static int sys_cddev_open(struct dev_info_ioctl* ciw, int unitnum)
}

struct hd_driveid id;
if (ioctl(ciw->h, HDIO_GET_IDENTITY, &id) == 0) {
if (ioctl(ciw->fd, HDIO_GET_IDENTITY, &id) == 0) {
strncpy(ciw->di.vendorid, (const char*)id.model, sizeof(ciw->di.vendorid) - 1);
strncpy(ciw->di.productid, (const char*)id.serial_no, sizeof(ciw->di.productid) - 1);
strncpy(ciw->di.revision, (const char*)id.fw_rev, sizeof(ciw->di.revision) - 1);
Expand Down Expand Up @@ -1044,22 +1044,25 @@ static int open_bus(int flags)
return 1;
}
total_devices = 0;
for (int drive = 0; drive <= 9 && total_devices < MAX_TOTAL_SCSI_DEVICES; drive++) {
char tmp[10];
snprintf(tmp, sizeof(tmp), "/dev/sr%d", drive);
int fd = open(tmp, O_RDONLY | O_NONBLOCK);
if (fd != -1) {
struct stat st;
if (fstat(fd, &st) == 0 && S_ISBLK(st.st_mode)) {
ciw32[total_devices].drvletter = drive;
strcpy(ciw32[total_devices].drvlettername, tmp);
ciw32[total_devices].type = st.st_rdev;
ciw32[total_devices].di.bytespersector = 2048;
strcpy(ciw32[total_devices].devname, tmp);
ciw32[total_devices].fd = fd;
total_devices++;
auto cd_drives = get_cd_drives();
if (!cd_drives.empty())
{
for (const auto& drive: cd_drives)
{
int fd = open(drive.c_str(), O_RDONLY | O_NONBLOCK);
if (fd != -1) {
struct stat st;
if (fstat(fd, &st) == 0 && S_ISBLK(st.st_mode)) {
strncpy(ciw32[total_devices].drvletter, drive.c_str(), sizeof(ciw32[total_devices].drvletter));
strcpy(ciw32[total_devices].drvlettername, drive.c_str());
ciw32[total_devices].type = st.st_rdev;
ciw32[total_devices].di.bytespersector = 2048;
strcpy(ciw32[total_devices].devname, drive.c_str());
ciw32[total_devices].fd = fd;
total_devices++;
}
close(fd);
}
close(fd);
}
}
bus_open = 1;
Expand Down Expand Up @@ -1092,26 +1095,26 @@ static struct device_info* info_device(int unitnum, struct device_info* di, int
return di;
}

bool win32_ioctl_media_change(TCHAR driveletter, int insert)
{
for (int i = 0; i < total_devices; i++) {
struct dev_info_ioctl* ciw = &ciw32[i];
if (ciw->drvletter == driveletter && ciw->di.media_inserted != insert) {
write_log(_T("IOCTL: media change %s %d\n"), ciw->drvlettername, insert);
ciw->di.media_inserted = insert;
ciw->changed = true;
int unitnum = getunitnum(ciw);
if (unitnum >= 0) {
update_device_info(unitnum);
scsi_do_disk_change(unitnum, insert, NULL);
filesys_do_disk_change(unitnum, insert != 0);
blkdev_cd_change(unitnum, ciw->drvlettername);
return true;
}
}
}
return false;
}
// bool win32_ioctl_media_change(TCHAR driveletter, int insert)
// {
// for (int i = 0; i < total_devices; i++) {
// struct dev_info_ioctl* ciw = &ciw32[i];
// if (ciw->drvletter == driveletter && ciw->di.media_inserted != insert) {
// write_log(_T("IOCTL: media change %s %d\n"), ciw->drvlettername, insert);
// ciw->di.media_inserted = insert;
// ciw->changed = true;
// int unitnum = getunitnum(ciw);
// if (unitnum >= 0) {
// update_device_info(unitnum);
// scsi_do_disk_change(unitnum, insert, NULL);
// filesys_do_disk_change(unitnum, insert != 0);
// blkdev_cd_change(unitnum, ciw->drvlettername);
// return true;
// }
// }
// }
// return false;
// }

static int ioctl_scsiemu(int unitnum, uae_u8* cmd)
{
Expand Down
1 change: 1 addition & 0 deletions src/osdep/gui/PanelHD.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,7 @@ void InitPanelHD(const config_category& category)

category.panel->add(chkCDTurbo, DISTANCE_BORDER, posY);

cboCDFile->clearSelected();
RefreshPanelHD();
}

Expand Down
1 change: 1 addition & 0 deletions src/osdep/gui/PanelQuickstart.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -940,6 +940,7 @@ void InitPanelQuickstart(const config_category& category)
cmdCDEject->setVisible(false);
cmdCDSelect->setVisible(false);
cboCDFile->setVisible(false);
cboCDFile->clearSelected();

bIgnoreListChange = false;

Expand Down

0 comments on commit 22d02f0

Please sign in to comment.