diff --git a/Makefile.common b/Makefile.common index f7f962214f..d2b8b88d65 100644 --- a/Makefile.common +++ b/Makefile.common @@ -1617,6 +1617,15 @@ ifeq ($(HAVE_ZLIB_COMMON), 1) endif endif +ifeq ($(HAVE_CDROM), 1) + ifeq ($(CDROM_DEBUG), 1) + DEFINES += -DCDROM_DEBUG + endif + + DEFINES += -DHAVE_CDROM + OBJ += $(LIBRETRO_COMM_DIR)/cdrom/cdrom.o +endif + ifeq ($(HAVE_RTGA), 1) DEFINES += -DHAVE_RTGA OBJ += $(LIBRETRO_COMM_DIR)/formats/tga/rtga.o diff --git a/core_type.h b/core_type.h index 40b2d8b16e..5c477f2396 100644 --- a/core_type.h +++ b/core_type.h @@ -1,5 +1,6 @@ /* RetroArch - A frontend for libretro. * Copyright (C) 2011-2017 - Daniel De Matteis + * Copyright (C) 2016-2019 - Brad Parker * * RetroArch is free software: you can redistribute it and/or modify it under the terms * of the GNU General Public License as published by the Free Software Found- diff --git a/dynamic.c b/dynamic.c index ce8f081f4f..8da15379a3 100644 --- a/dynamic.c +++ b/dynamic.c @@ -1,6 +1,7 @@ /* RetroArch - A frontend for libretro. * Copyright (C) 2010-2014 - Hans-Kristian Arntzen * Copyright (C) 2011-2017 - Daniel De Matteis + * Copyright (C) 2016-2019 - Brad Parker * * RetroArch is free software: you can redistribute it and/or modify it under the terms * of the GNU General Public License as published by the Free Software Found- diff --git a/dynamic.h b/dynamic.h index a879e38d71..04e3f81589 100644 --- a/dynamic.h +++ b/dynamic.h @@ -1,6 +1,7 @@ /* RetroArch - A frontend for libretro. * Copyright (C) 2010-2014 - Hans-Kristian Arntzen * Copyright (C) 2011-2017 - Daniel De Matteis + * Copyright (C) 2016-2019 - Brad Parker * * RetroArch is free software: you can redistribute it and/or modify it under the terms * of the GNU General Public License as published by the Free Software Found- diff --git a/file_path_special.c b/file_path_special.c index f1c3bcbbec..b3f6bffcd1 100644 --- a/file_path_special.c +++ b/file_path_special.c @@ -1,5 +1,6 @@ /* RetroArch - A frontend for libretro. * Copyright (C) 2011-2017 - Daniel De Matteis + * Copyright (C) 2016-2019 - Brad Parker * * RetroArch is free software: you can redistribute it and/or modify it under the terms * of the GNU General Public License as published by the Free Software Found- diff --git a/file_path_special.h b/file_path_special.h index f2db2208ee..a2eed197a2 100644 --- a/file_path_special.h +++ b/file_path_special.h @@ -1,6 +1,7 @@ /* RetroArch - A frontend for libretro. * Copyright (C) 2010-2014 - Hans-Kristian Arntzen * Copyright (C) 2011-2016 - Daniel De Matteis + * Copyright (C) 2016-2019 - Brad Parker * * RetroArch is free software: you can redistribute it and/or modify it under the terms * of the GNU General Public License as published by the Free Software Found- diff --git a/griffin/griffin.c b/griffin/griffin.c index 4767fe42f0..7af09ff1f6 100644 --- a/griffin/griffin.c +++ b/griffin/griffin.c @@ -1,6 +1,7 @@ /* RetroArch - A frontend for libretro. * Copyright (C) 2010-2014 - Hans-Kristian Arntzen * Copyright (C) 2011-2017 - Daniel De Matteis +* Copyright (C) 2016-2019 - Brad Parker * * RetroArch is free software: you can redistribute it and/or modify it under the terms * of the GNU General Public License as published by the Free Software Found- diff --git a/griffin/griffin_cpp.cpp b/griffin/griffin_cpp.cpp index 79bbfeff06..686c70b20c 100644 --- a/griffin/griffin_cpp.cpp +++ b/griffin/griffin_cpp.cpp @@ -1,5 +1,6 @@ /* RetroArch - A frontend for libretro. * Copyright (C) 2011-2017 - Daniel De Matteis +* Copyright (C) 2016-2019 - Brad Parker * * RetroArch is free software: you can redistribute it and/or modify it under the terms * of the GNU General Public License as published by the Free Software Found- diff --git a/libretro-common/cdrom/cdrom.c b/libretro-common/cdrom/cdrom.c new file mode 100644 index 0000000000..6af9a485f7 --- /dev/null +++ b/libretro-common/cdrom/cdrom.c @@ -0,0 +1,461 @@ +/* Copyright (C) 2010-2019 The RetroArch team +* +* --------------------------------------------------------------------------------------- +* The following license statement only applies to this file (cdrom.c). +* --------------------------------------------------------------------------------------- +* +* Permission is hereby granted, free of charge, +* to any person obtaining a copy of this software and associated documentation files (the "Software"), +* to deal in the Software without restriction, including without limitation the rights to +* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, +* and to permit persons to whom the Software is furnished to do so, subject to the following conditions: +* +* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. +* +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, +* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +*/ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +#ifdef __linux__ +#include +#include +#endif + +#define CDROM_CUE_TRACK_BYTES 78 + +typedef enum +{ + DIRECTION_NONE, + DIRECTION_IN, + DIRECTION_OUT +} CDROM_CMD_Direction; + +void lba_to_msf(unsigned lba, unsigned char *min, unsigned char *sec, unsigned char *frame) +{ + if (!min || !sec || !frame) + return; + + *frame = lba % 75; + lba /= 75; + *sec = lba % 60; + lba /= 60; + *min = lba; +} + +unsigned msf_to_lba(unsigned char min, unsigned char sec, unsigned char frame) +{ + return (min * 60 + sec) * 75 + frame; +} + +void increment_msf(unsigned char *min, unsigned char *sec, unsigned char *frame) +{ + if (!min || !sec || !frame) + return; + + *min = (*frame == 74) ? (*sec < 59 ? *min : *min + 1) : *min; + *sec = (*frame == 74) ? (*sec < 59 ? (*sec + 1) : 0) : *sec; + *frame = (*frame < 74) ? (*frame + 1) : 0; +} + +static int cdrom_send_command(int fd, CDROM_CMD_Direction dir, void *buf, size_t len, unsigned char *cmd, size_t cmd_len) +{ +#ifdef __linux__ + sg_io_hdr_t sgio = {0}; + unsigned char sense[SG_MAX_SENSE] = {0}; + int rv; + + if (!cmd || cmd_len == 0) + return 1; + + sgio.interface_id = 'S'; + + switch (dir) + { + case DIRECTION_IN: + sgio.dxfer_direction = SG_DXFER_FROM_DEV; + break; + case DIRECTION_OUT: + sgio.dxfer_direction = SG_DXFER_TO_DEV; + break; + case DIRECTION_NONE: + default: + sgio.dxfer_direction = SG_DXFER_NONE; + break; + } + + sgio.cmd_len = cmd_len; + sgio.cmdp = cmd; + + if (buf) + sgio.dxferp = buf; + + if (len) + sgio.dxfer_len = len; + + sgio.sbp = sense; + sgio.mx_sb_len = sizeof(sense); + sgio.timeout = 30000; + + rv = ioctl(fd, SG_IO, &sgio); + +#ifdef CDROM_DEBUG + if (sgio.info & SG_INFO_CHECK) + { + unsigned i; + + printf("CHECK CONDITION\n"); + + for (i = 0; i < SG_MAX_SENSE; i++) + { + printf("%02X ", sense[i]); + } + + printf("\n"); + + if (sense[0] == 0x70) + printf("CURRENT ERROR:\n"); + if (sense[0] == 0x71) + printf("DEFERRED ERROR:\n"); + + printf("Sense Key: %02X\n", sense[2] & 0xF); + printf("ASC: %02X\n", sense[12]); + printf("ASCQ: %02X\n", sense[13]); + } +#endif + + if (rv == -1) + return 1; + + return 0; +#else + (void)fd; + (void)dir; + (void)buf; + (void)len; + (void)cmd; + (void)cmd_len; + + return 1; +#endif +} + +int cdrom_read_subq(int fd, unsigned char *buf, size_t len) +{ + /* MMC Command: READ TOC/PMA/ATIP */ + unsigned char cdb[] = {0x43, 0x2, 0x2, 0, 0, 0, 0x1, 0x9, 0x30, 0}; +#ifdef CDROM_DEBUG + unsigned short data_len = 0; + unsigned char first_session = 0; + unsigned char last_session = 0; + int i; +#endif + int rv; + + if (!buf) + return 1; + + rv = cdrom_send_command(fd, DIRECTION_IN, buf, len, cdb, sizeof(cdb)); + + if (rv) + return 1; + +#ifdef CDROM_DEBUG + data_len = buf[0] << 8 | buf[1]; + first_session = buf[2]; + last_session = buf[3]; + + printf("Data Length: %d\n", data_len); + printf("First Session: %d\n", first_session); + printf("Last Session: %d\n", last_session); + + for (i = 0; i < (data_len - 2) / 11; i++) + { + unsigned char session_num = buf[4 + (i * 11) + 0]; + unsigned char adr = (buf[4 + (i * 11) + 1] >> 4) & 0xF; + /*unsigned char control = buf[4 + (i * 11) + 1] & 0xF;*/ + unsigned char tno = buf[4 + (i * 11) + 2]; + unsigned char point = buf[4 + (i * 11) + 3]; + unsigned char pmin = buf[4 + (i * 11) + 8]; + unsigned char psec = buf[4 + (i * 11) + 9]; + unsigned char pframe = buf[4 + (i * 11) + 10]; + + /*printf("i %d control %d adr %d tno %d point %d: ", i, control, adr, tno, point);*/ + /* why is control always 0? */ + + if (/*(control == 4 || control == 6) && */adr == 1 && tno == 0 && point >= 1 && point <= 99) + { + printf("- Session#: %d TNO %d POINT %d ", session_num, tno, point); + printf("Track start time: (MSF %02u:%02u:%02u) ", pmin, psec, pframe); + } + else if (/*(control == 4 || control == 6) && */adr == 1 && tno == 0 && point == 0xA0) + { + printf("- Session#: %d TNO %d POINT %d ", session_num, tno, point); + printf("First Track Number: %d ", pmin); + printf("Disc Type: %d ", psec); + } + else if (/*(control == 4 || control == 6) && */adr == 1 && tno == 0 && point == 0xA1) + { + printf("- Session#: %d TNO %d POINT %d ", session_num, tno, point); + printf("Last Track Number: %d ", pmin); + } + else if (/*(control == 4 || control == 6) && */adr == 1 && tno == 0 && point == 0xA2) + { + printf("- Session#: %d TNO %d POINT %d ", session_num, tno, point); + printf("Lead-out runtime: (MSF %02u:%02u:%02u) ", pmin, psec, pframe); + } + + printf("\n"); + } +#endif + return 0; +} + +static int cdrom_read_track_info(int fd, unsigned char track, cdrom_toc_t *toc) +{ + /* MMC Command: READ TRACK INFORMATION */ + unsigned char cdb[] = {0x52, 0x1, 0, 0, 0, track, 0, 0x1, 0x80, 0}; + unsigned char buf[384] = {0}; + unsigned lba = 0; + unsigned track_size = 0; + int rv = cdrom_send_command(fd, DIRECTION_IN, buf, sizeof(buf), cdb, sizeof(cdb)); + + if (rv) + return 1; + + memcpy(&lba, buf + 8, 4); + memcpy(&track_size, buf + 24, 4); + + lba = swap_if_little32(lba); + track_size = swap_if_little32(track_size); + + toc->track[track - 1].lba_start = lba; + toc->track[track - 1].track_size = track_size; + +#ifdef CDROM_DEBUG + printf("Track %d Info: ", track); + printf("Copy: %d ", (buf[5] & 0x10) > 0); + printf("Data Mode: %d ", buf[6] & 0xF); + printf("LBA Start: %d ", lba); + printf("Track Size: %d\n", track_size); +#endif + + return 0; +} + +int cdrom_write_cue(int fd, char **out_buf, size_t *out_len, char cdrom_drive, unsigned char *num_tracks, cdrom_toc_t *toc) +{ + unsigned char buf[2352] = {0}; + unsigned short data_len = 0; + size_t len = 0; + size_t pos = 0; + int rv = 0; + int i; + + if (!out_buf || !out_len || !num_tracks || !toc) + { +#ifdef CDROM_DEBUG + printf("Invalid buffer/length pointer for CDROM cue sheet\n"); +#endif + return 1; + } + + rv = cdrom_read_subq(fd, buf, sizeof(buf)); + + if (rv) + return rv; + + data_len = buf[0] << 8 | buf[1]; + + for (i = 0; i < (data_len - 2) / 11; i++) + { + unsigned char adr = (buf[4 + (i * 11) + 1] >> 4) & 0xF; + unsigned char tno = buf[4 + (i * 11) + 2]; + unsigned char point = buf[4 + (i * 11) + 3]; + unsigned char pmin = buf[4 + (i * 11) + 8]; + + if (/*(control == 4 || control == 6) && */adr == 1 && tno == 0 && point == 0xA1) + { + *num_tracks = pmin; +#ifdef CDROM_DEBUG + printf("Number of CDROM tracks: %d\n", *num_tracks); +#endif + break; + } + } + + if (!*num_tracks || *num_tracks > 99) + { +#ifdef CDROM_DEBUG + printf("Invalid number of CDROM tracks: %d\n", *num_tracks); +#endif + return 1; + } + + len = CDROM_CUE_TRACK_BYTES * (*num_tracks); + toc->num_tracks = *num_tracks; + *out_buf = (char*)calloc(1, len); + *out_len = len; + + for (i = 0; i < (data_len - 2) / 11; i++) + { + /*unsigned char session_num = buf[4 + (i * 11) + 0];*/ + unsigned char adr = (buf[4 + (i * 11) + 1] >> 4) & 0xF; + /*unsigned char control = buf[4 + (i * 11) + 1] & 0xF;*/ + unsigned char tno = buf[4 + (i * 11) + 2]; + unsigned char point = buf[4 + (i * 11) + 3]; + unsigned char pmin = buf[4 + (i * 11) + 8]; + unsigned char psec = buf[4 + (i * 11) + 9]; + unsigned char pframe = buf[4 + (i * 11) + 10]; + + /*printf("i %d control %d adr %d tno %d point %d: ", i, control, adr, tno, point);*/ + /* why is control always 0? */ + + if (/*(control == 4 || control == 6) && */adr == 1 && tno == 0 && point >= 1 && point <= 99) + { + unsigned char next_pmin = (pframe == 74) ? (psec < 59 ? pmin : pmin + 1) : pmin; + unsigned char next_psec = (pframe == 74) ? (psec < 59 ? (psec + 1) : 0) : psec; + unsigned char next_pframe = (pframe < 74) ? (pframe + 1) : 0; + /* MMC Command: READ CD MSF */ + unsigned char q_cdb[] = {0xB9, 0, 0, pmin, psec, pframe, next_pmin, next_psec, next_pframe, 0, 0x2, 0}; + unsigned char q_buf[3] = {0}; + unsigned char mode = 1; + bool audio = false; + const char *track_type = "MODE1/2352"; + + rv = cdrom_send_command(fd, DIRECTION_IN, q_buf, sizeof(q_buf), q_cdb, sizeof(q_cdb)); + + if (rv) + continue; + + mode = q_buf[0] & 0xF; + audio = (q_buf[0] & 0x40) == 0; + +#ifdef CDROM_DEBUG + printf("Track %02d CONTROL %02X %02X %02X MODE %d AUDIO? %d\n", point, q_buf[0], q_buf[1], q_buf[2], mode, audio); +#endif + + toc->track[point - 1].track_num = point; + toc->track[point - 1].min = pmin; + toc->track[point - 1].sec = psec; + toc->track[point - 1].frame = pframe; + toc->track[point - 1].mode = mode; + toc->track[point - 1].audio = audio; + + if (audio) + track_type = "AUDIO"; + else if (mode == 1) + track_type = "MODE1/2352"; + else if (mode == 2) + track_type = "MODE2/2352"; + + pos += snprintf(*out_buf + pos, len - pos, "FILE \"cdrom://drive%c.bin\" BINARY\n", cdrom_drive); + pos += snprintf(*out_buf + pos, len - pos, " TRACK %02d %s\n", point, track_type); + pos += snprintf(*out_buf + pos, len - pos, " INDEX 01 %02d:%02d:%02d\n", pmin, psec, pframe); + + cdrom_read_track_info(fd, point, toc); + } + } + + return 0; +} + +/* needs 32 bytes for full vendor, product and version */ +int cdrom_get_inquiry(int fd, char *model, int len) +{ + /* MMC Command: INQUIRY */ + unsigned char cdb[] = {0x12, 0, 0, 0, 0xff, 0}; + unsigned char buf[256] = {0}; + int rv = cdrom_send_command(fd, DIRECTION_IN, buf, sizeof(buf), cdb, sizeof(cdb)); + + if (rv) + return 1; + + if (model && len >= 32) + { + memset(model, 0, len); + + /* vendor */ + memcpy(model, buf + 8, 8); + + model[8] = ' '; + + /* product */ + memcpy(model + 9, buf + 16, 16); + + model[25] = ' '; + + /* version */ + memcpy(model + 26, buf + 32, 4); + } + + return 0; +} + +int cdrom_read(int fd, unsigned char min, unsigned char sec, unsigned char frame, void *s, size_t len) +{ + /* MMC Command: READ CD MSF */ + unsigned char cdb[] = {0xB9, 0, 0, min, sec, frame, 0, 0, 0, 0xF8, 0, 0}; + int rv; + + if (len <= 2352) + { + unsigned char next_min = (frame == 74) ? (sec < 59 ? min : min + 1) : min; + unsigned char next_sec = (frame == 74) ? (sec < 59 ? (sec + 1) : 0) : sec; + unsigned char next_frame = (frame < 74) ? (frame + 1) : 0; + + cdb[6] = next_min; + cdb[7] = next_sec; + cdb[8] = next_frame; + } + else + { + unsigned frames = round(len / 2352.0); + + cdb[6] = frames / 75 / 60; + cdb[7] = frames / 75; + cdb[8] = frames - ((cdb[6] * 75 * 60) + (cdb[7] * 75)); + + if (cdb[8] > 74) + { + cdb[8] = 0; + cdb[7]++; + + if (cdb[7] > 59) + { + cdb[7] = 0; + cdb[6]++; + } + } + } + + rv = cdrom_send_command(fd, DIRECTION_IN, s, len, cdb, sizeof(cdb)); + +#ifdef CDROM_DEBUG + printf("read status code %d\n", rv); +#endif + + if (rv) + return 1; + + return 0; +} + diff --git a/libretro-common/include/cdrom/cdrom.h b/libretro-common/include/cdrom/cdrom.h new file mode 100644 index 0000000000..414451ec7d --- /dev/null +++ b/libretro-common/include/cdrom/cdrom.h @@ -0,0 +1,77 @@ +/* Copyright (C) 2010-2019 The RetroArch team + * + * --------------------------------------------------------------------------------------- + * The following license statement only applies to this file (cdrom.h). + * --------------------------------------------------------------------------------------- + * + * Permission is hereby granted, free of charge, + * to any person obtaining a copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation the rights to + * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, + * and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, + * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, + * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +#ifndef __LIBRETRO_SDK_CDROM_H +#define __LIBRETRO_SDK_CDROM_H + +#include +#include +#include +#include + +#include +#include +#include + +#include + +RETRO_BEGIN_DECLS + +typedef struct +{ + unsigned lba_start; + unsigned track_size; + unsigned char track_num; + unsigned char min; + unsigned char sec; + unsigned char frame; + unsigned char mode; + bool audio; +} cdrom_track_t; + +typedef struct +{ + unsigned char cur_min; + unsigned char cur_sec; + unsigned char cur_frame; + unsigned char num_tracks; + cdrom_track_t track[99]; +} cdrom_toc_t; + +void lba_to_msf(unsigned lba, unsigned char *min, unsigned char *sec, unsigned char *frame); + +unsigned msf_to_lba(unsigned char min, unsigned char sec, unsigned char frame); + +void increment_msf(unsigned char *min, unsigned char *sec, unsigned char *frame); + +int cdrom_read_subq(int fd, unsigned char *buf, size_t len); + +int cdrom_write_cue(int fd, char **out_buf, size_t *out_len, char cdrom_drive, unsigned char *num_tracks, cdrom_toc_t *toc); + +/* needs 32 bytes for full vendor, product and version */ +int cdrom_get_inquiry(int fd, char *model, int len); + +int cdrom_read(int fd, unsigned char min, unsigned char sec, unsigned char frame, void *s, size_t len); + +RETRO_END_DECLS + +#endif diff --git a/libretro-common/vfs/vfs_implementation.c b/libretro-common/vfs/vfs_implementation.c index 245b3c78d3..a5387325b0 100644 --- a/libretro-common/vfs/vfs_implementation.c +++ b/libretro-common/vfs/vfs_implementation.c @@ -158,6 +158,12 @@ #define FIO_S_ISDIR SCE_S_ISDIR #endif +#if defined(__linux__) +#include +#include +#include +#endif + #if (defined(__CELLOS_LV2__) && !defined(__PSL1GHT__)) || defined(__QNX__) || defined(PSP) #include /* stat() is defined here */ #endif @@ -190,8 +196,18 @@ #include #include +#ifdef HAVE_CDROM +#include +#endif + #define RFILE_HINT_UNBUFFERED (1 << 8) +enum vfs_scheme +{ + VFS_SCHEME_NONE = 0, + VFS_SCHEME_CDROM +}; + #ifdef VFS_FRONTEND struct retro_vfs_file_handle #else @@ -208,9 +224,19 @@ struct libretro_vfs_implementation_file uint64_t mappos; uint64_t mapsize; uint8_t *mapped; +#endif + enum vfs_scheme scheme; +#ifdef HAVE_CDROM + char *cdrom_cue_buf; + size_t cdrom_cue_len; + size_t cdrom_cue_pos; + size_t cdrom_byte_pos; + char cdrom_drive; #endif }; +static cdrom_toc_t vfs_cdrom_toc = {0}; + int64_t retro_vfs_file_seek_internal(libretro_vfs_implementation_file *stream, int64_t offset, int whence) { if (!stream) @@ -235,7 +261,84 @@ int64_t retro_vfs_file_seek_internal(libretro_vfs_implementation_file *stream, i return -1; return 0; #else - return fseeko(stream->fp, (off_t)offset, whence); +#ifdef HAVE_CDROM + if (stream->scheme == VFS_SCHEME_CDROM) + { + const char *ext = path_get_extension(stream->orig_path); + + if (string_is_equal_noncase(ext, "cue")) + { + switch (whence) + { + case SEEK_SET: + stream->cdrom_cue_pos = offset; + break; + case SEEK_CUR: + stream->cdrom_cue_pos += offset; + break; + case SEEK_END: + stream->cdrom_cue_pos = (stream->cdrom_cue_len - 1) + offset; + break; + } + +#ifdef CDROM_DEBUG + printf("CDROM Seek: Path %s Offset %lu is now at %lu\n", stream->orig_path, offset, stream->cdrom_cue_pos); +#endif + } + else if (string_is_equal_noncase(ext, "bin")) + { + unsigned char min = offset / 75 / 60; + unsigned char sec = offset / 75; + unsigned char frame = offset - ((min * 75 * 60) + (sec * 75)); + + switch (whence) + { + case SEEK_CUR: + { + min += vfs_cdrom_toc.cur_min; + sec += vfs_cdrom_toc.cur_sec; + frame += vfs_cdrom_toc.cur_frame; + + stream->cdrom_byte_pos += offset; + + break; + } + case SEEK_END: + { + unsigned char end_min = 0; + unsigned char end_sec = 0; + unsigned char end_frame = 0; + size_t end_lba = vfs_cdrom_toc.track[vfs_cdrom_toc.num_tracks - 1].lba_start + vfs_cdrom_toc.track[vfs_cdrom_toc.num_tracks - 1].track_size; + + lba_to_msf(end_lba, &min, &sec, &frame); + + min += end_min; + sec += end_sec; + frame += end_frame; + + stream->cdrom_byte_pos = end_lba * 2352; + + break; + } + case SEEK_SET: + default: + stream->cdrom_byte_pos = offset; + break; + } + + vfs_cdrom_toc.cur_min = min; + vfs_cdrom_toc.cur_sec = sec; + vfs_cdrom_toc.cur_frame = frame; + +#ifdef CDROM_DEBUG + printf("CDROM Seek: Path %s Offset %lu is now at %lu\n", stream->orig_path, offset, stream->cdrom_byte_pos); +#endif + } + + } + else +#endif + return fseeko(stream->fp, (off_t)offset, whence); #endif } #ifdef HAVE_MMAP @@ -296,6 +399,7 @@ libretro_vfs_implementation_file *retro_vfs_file_open_impl( { int flags = 0; const char *mode_str = NULL; + int path_len = (int)strlen(path); libretro_vfs_implementation_file *stream = (libretro_vfs_implementation_file*) calloc(1, sizeof(*stream)); @@ -303,7 +407,6 @@ libretro_vfs_implementation_file *retro_vfs_file_open_impl( const char *dumb_prefix = "vfsonly://"; size_t dumb_prefix_siz = strlen(dumb_prefix); int dumb_prefix_len = (int)dumb_prefix_siz; - int path_len = (int)strlen(path); if (path_len >= dumb_prefix_len) { @@ -312,6 +415,23 @@ libretro_vfs_implementation_file *retro_vfs_file_open_impl( } #endif +#ifdef HAVE_CDROM + { + const char *cdrom_prefix = "cdrom://"; + size_t cdrom_prefix_siz = strlen(cdrom_prefix); + int cdrom_prefix_len = (int)cdrom_prefix_siz; + + if (path_len >= cdrom_prefix_len) + { + if (!memcmp(path, cdrom_prefix, cdrom_prefix_len)) + { + path += cdrom_prefix_siz; + stream->scheme = VFS_SCHEME_CDROM; + } + } + } +#endif + if (!stream) return NULL; @@ -398,7 +518,78 @@ libretro_vfs_implementation_file *retro_vfs_file_open_impl( } stream->fd = fd; #else - FILE *fp = (FILE*)fopen_utf8(path, mode_str); + FILE *fp; +#ifdef HAVE_CDROM + if (stream->scheme == VFS_SCHEME_CDROM) + { +#ifdef __linux__ + char model[32] = {0}; + char cdrom_path[] = "/dev/sg1"; + size_t path_len = strlen(path); + const char *ext = path_get_extension(path); + + if (path_len >= strlen("drive1.cue")) + { + if (!memcmp(path, "drive", strlen("drive"))) + { + if (path[5] >= '0' && path[5] <= '9') + { + cdrom_path[7] = path[5]; + stream->cdrom_drive = path[5]; + } + } + } + +#ifdef CDROM_DEBUG + printf("CDROM Open: Path %s URI %s\n", cdrom_path, path); +#endif + fp = (FILE*)fopen_utf8(cdrom_path, "r+b"); + + if (fp) + { + int cdrom_fd = fileno(fp); + + if (!cdrom_get_inquiry(cdrom_fd, model, sizeof(model))) + { + size_t len = 0; + +#ifdef CDROM_DEBUG + printf("CDROM Model: %s\n", model); +#endif + } + } + else + goto error; + + if (string_is_equal_noncase(ext, "cue")) + { + if (stream->cdrom_cue_buf) + { + free(stream->cdrom_cue_buf); + stream->cdrom_cue_buf = NULL; + } + + cdrom_write_cue(fileno(fp), &stream->cdrom_cue_buf, &stream->cdrom_cue_len, stream->cdrom_drive, &vfs_cdrom_toc.num_tracks, &vfs_cdrom_toc); + + if (vfs_cdrom_toc.num_tracks > 1) + { + vfs_cdrom_toc.cur_min = vfs_cdrom_toc.track[0].min; + vfs_cdrom_toc.cur_sec = vfs_cdrom_toc.track[0].sec; + vfs_cdrom_toc.cur_frame = vfs_cdrom_toc.track[0].frame; + } + + if (string_is_empty(stream->cdrom_cue_buf)) + printf("Error writing cue sheet.\n"); +#ifdef CDROM_DEBUG + else + printf("CDROM CUE Sheet:\n%s\n", stream->cdrom_cue_buf); +#endif + } +#endif + } + else +#endif + fp = (FILE*)fopen_utf8(path, mode_str); if (!fp) goto error; @@ -508,8 +699,21 @@ int retro_vfs_file_close_impl(libretro_vfs_implementation_file *stream) } if (stream->buf) free(stream->buf); + +#ifdef HAVE_CDROM +#ifdef CDROM_DEBUG + if (stream->scheme == VFS_SCHEME_CDROM) + printf("CDROM Close: Path %s\n", stream->orig_path); +#endif +#endif + if (stream->orig_path) free(stream->orig_path); + +#ifdef HAVE_CDROM + if (stream->cdrom_cue_buf) + free(stream->cdrom_cue_buf); +#endif free(stream); return 0; @@ -565,7 +769,35 @@ int64_t retro_vfs_file_tell_impl(libretro_vfs_implementation_file *stream) #ifdef ATLEAST_VC2005 return _ftelli64(stream->fp); #else - return ftell(stream->fp); +#ifdef HAVE_CDROM + if (stream->scheme == VFS_SCHEME_CDROM) + { + const char *ext = path_get_extension(stream->orig_path); + + if (string_is_equal_noncase(ext, "cue")) + { +#ifdef HAVE_CDROM +#ifdef CDROM_DEBUG + printf("CDROM (cue) Tell: Path %s Position %lu\n", stream->orig_path, stream->cdrom_cue_pos); +#endif +#endif + return stream->cdrom_cue_pos; + } + else if (string_is_equal_noncase(ext, "bin")) + { + unsigned lba = msf_to_lba(vfs_cdrom_toc.cur_min, vfs_cdrom_toc.cur_sec, vfs_cdrom_toc.cur_frame); + +#ifdef HAVE_CDROM +#ifdef CDROM_DEBUG + printf("CDROM (bin) Tell: Path %s Position %u\n", stream->orig_path, lba * 2352); +#endif +#endif + return lba * 2352; + } + } + else +#endif + return ftell(stream->fp); #endif #endif } @@ -614,7 +846,56 @@ int64_t retro_vfs_file_read_impl(libretro_vfs_implementation_file *stream, return -1; return 0; #else - return fread(s, 1, (size_t)len, stream->fp); +#ifdef HAVE_CDROM + if (stream->scheme == VFS_SCHEME_CDROM) + { + int rv; + const char *ext = path_get_extension(stream->orig_path); + + if (string_is_equal_noncase(ext, "cue")) + { +#ifdef CDROM_DEBUG + printf("CDROM Read: Reading %lu bytes from cuesheet starting at %lu...\n", len, stream->cdrom_cue_pos); +#endif + strlcpy(s, stream->cdrom_cue_buf + stream->cdrom_cue_pos, len); + stream->cdrom_cue_pos += len; + } + else if (string_is_equal_noncase(ext, "bin")) + { + unsigned frames = len / 2352; + unsigned i; + +#ifdef CDROM_DEBUG + printf("CDROM Read: Reading %lu bytes from CD starting at byte offset %lu (MSF %02d:%02d:%02d) (LBA %u)...\n", len, stream->cdrom_byte_pos, vfs_cdrom_toc.cur_min, vfs_cdrom_toc.cur_sec, vfs_cdrom_toc.cur_frame, msf_to_lba(vfs_cdrom_toc.cur_min, vfs_cdrom_toc.cur_sec, vfs_cdrom_toc.cur_frame)); +#endif + + rv = cdrom_read(fileno(stream->fp), vfs_cdrom_toc.cur_min, vfs_cdrom_toc.cur_sec, vfs_cdrom_toc.cur_frame, s, (size_t)len); + + if (rv) + { +#ifdef CDROM_DEBUG + printf("Failed to read %lu bytes from CD.\n", len); +#endif + return 0; + } + + stream->cdrom_byte_pos += len; + + for (i = 0; i < frames; i++) + { + increment_msf(&vfs_cdrom_toc.cur_min, &vfs_cdrom_toc.cur_sec, &vfs_cdrom_toc.cur_frame); + } + +#ifdef CDROM_DEBUG + printf("CDROM read %lu bytes, position is now: %lu (MSF %02d:%02d:%02d) (LBA %u)\n", len, stream->cdrom_byte_pos, vfs_cdrom_toc.cur_min, vfs_cdrom_toc.cur_sec, vfs_cdrom_toc.cur_frame, msf_to_lba(vfs_cdrom_toc.cur_min, vfs_cdrom_toc.cur_sec, vfs_cdrom_toc.cur_frame)); +#endif + } + + return len; + } + else +#endif + return fread(s, 1, (size_t)len, stream->fp); #endif } #ifdef HAVE_MMAP diff --git a/paths.c b/paths.c index e5e2b6ab06..b63eb07a2f 100644 --- a/paths.c +++ b/paths.c @@ -1,6 +1,7 @@ /* RetroArch - A frontend for libretro. * Copyright (C) 2011-2019 - Daniel De Matteis * Copyright (C) 2017-2019 - Andrés Suárez + * Copyright (C) 2016-2019 - Brad Parker * * RetroArch is free software: you can redistribute it and/or modify it under the terms * of the GNU General Public License as published by the Free Software Found- diff --git a/paths.h b/paths.h index fbb043630e..b734e7fe12 100644 --- a/paths.h +++ b/paths.h @@ -1,5 +1,6 @@ /* RetroArch - A frontend for libretro. * Copyright (C) 2011-2019 - Daniel De Matteis + * Copyright (C) 2016-2019 - Brad Parker * * RetroArch is free software: you can redistribute it and/or modify it under the terms * of the GNU General Public License as published by the Free Software Found- diff --git a/performance_counters.c b/performance_counters.c index 6ac89ac286..f453e67afe 100644 --- a/performance_counters.c +++ b/performance_counters.c @@ -1,6 +1,7 @@ /* RetroArch - A frontend for libretro. * Copyright (C) 2010-2014 - Hans-Kristian Arntzen * Copyright (C) 2011-2017 - Daniel De Matteis + * Copyright (C) 2016-2019 - Brad Parker * * RetroArch is free software: you can redistribute it and/or modify it under the terms * of the GNU General Public License as published by the Free Software Found- diff --git a/verbosity.c b/verbosity.c index c39dfd678c..435042eafd 100644 --- a/verbosity.c +++ b/verbosity.c @@ -1,5 +1,6 @@ /* RetroArch - A frontend for libretro. * Copyright (C) 2011-2017 - Daniel De Matteis + * Copyright (C) 2016-2019 - Brad Parker * * RetroArch is free software: you can redistribute it and/or modify it under the terms * of the GNU General Public License as published by the Free Software Found-