Fix reporting for connected or unconnected inputs
[pcsx_rearmed.git] / libpcsxcore / cdriso.c
index c8eacb8..c36c196 100644 (file)
@@ -19,6 +19,8 @@
  *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.           *
  ***************************************************************************/
 
+#include <compat/fopen_utf8.h>
+
 #include "psxcommon.h"
 #include "plugins.h"
 #include "cdrom.h"
@@ -334,16 +336,16 @@ static int parsetoc(const char *isofile) {
                return -1;
        }
 
-       if ((fi = fopen(tocname, "r")) == NULL) {
+       if ((fi = fopen_utf8(tocname, "r")) == NULL) {
                // try changing extension to .cue (to satisfy some stupid tutorials)
                strcpy(tocname + strlen(tocname) - 4, ".cue");
-               if ((fi = fopen(tocname, "r")) == NULL) {
+               if ((fi = fopen_utf8(tocname, "r")) == NULL) {
                        // if filename is image.toc.bin, try removing .bin (for Brasero)
                        strcpy(tocname, isofile);
                        t = strlen(tocname);
                        if (t >= 8 && strcmp(tocname + t - 8, ".toc.bin") == 0) {
                                tocname[t - 4] = '\0';
-                               if ((fi = fopen(tocname, "r")) == NULL) {
+                               if ((fi = fopen_utf8(tocname, "r")) == NULL) {
                                        return -1;
                                }
                        }
@@ -477,13 +479,16 @@ static int parsecue(const char *isofile) {
        strncpy(cuename, isofile, sizeof(cuename));
        cuename[MAXPATHLEN - 1] = '\0';
        if (strlen(cuename) >= 4) {
-               strcpy(cuename + strlen(cuename) - 4, ".cue");
+               // If 'isofile' is a '.cd<X>' file, use it as a .cue file
+               //  and don't try to search the additional .cue file
+               if (strncasecmp(cuename + strlen(cuename) - 4, ".cd", 3) != 0 )
+                       strcpy(cuename + strlen(cuename) - 4, ".cue");
        }
        else {
                return -1;
        }
 
-       if ((fi = fopen(cuename, "r")) == NULL) {
+       if ((fi = fopen_utf8(cuename, "r")) == NULL) {
                return -1;
        }
 
@@ -587,7 +592,7 @@ static int parsecue(const char *isofile) {
                        else
                                tmp = tmpb;
                        strncpy(incue_fname, tmp, incue_max_len);
-                       ti[numtracks + 1].handle = fopen(filepath, "rb");
+                       ti[numtracks + 1].handle = fopen_utf8(filepath, "rb");
 
                        // update global offset if this is not first file in this .cue
                        if (numtracks + 1 > 1) {
@@ -604,17 +609,21 @@ static int parsecue(const char *isofile) {
                        file_len = ftell(ti[numtracks + 1].handle) / 2352;
 
                        if (numtracks == 0 && strlen(isofile) >= 4 &&
-                               strcmp(isofile + strlen(isofile) - 4, ".cue") == 0)
-                       {
-                               // user selected .cue as image file, use it's data track instead
+                               (strcmp(isofile + strlen(isofile) - 4, ".cue") == 0 ||
+                               strncasecmp(isofile + strlen(isofile) - 4, ".cd", 3) == 0)) {
+                               // user selected .cue/.cdX as image file, use it's data track instead
                                fclose(cdHandle);
-                               cdHandle = fopen(filepath, "rb");
+                               cdHandle = fopen_utf8(filepath, "rb");
                        }
                }
        }
 
        fclose(fi);
 
+       // if there are no tracks detected, then it's not a cue file
+       if (!numtracks)
+               return -1;
+
        return 0;
 }
 
@@ -638,7 +647,7 @@ static int parseccd(const char *isofile) {
                return -1;
        }
 
-       if ((fi = fopen(ccdname, "r")) == NULL) {
+       if ((fi = fopen_utf8(ccdname, "r")) == NULL) {
                return -1;
        }
 
@@ -697,7 +706,7 @@ static int parsemds(const char *isofile) {
                return -1;
        }
 
-       if ((fi = fopen(mdsname, "rb")) == NULL) {
+       if ((fi = fopen_utf8(mdsname, "rb")) == NULL) {
                return -1;
        }
 
@@ -1050,7 +1059,10 @@ static int handlechd(const char *isofile) {
                goto fail_io;
 
        if(chd_open(isofile, CHD_OPEN_READ, NULL, &chd_img->chd) != CHDERR_NONE)
-      goto fail_io;
+               goto fail_io;
+
+       if (Config.CHD_Precache && (chd_precache(chd_img->chd) != CHDERR_NONE))
+               goto fail_io;
 
    chd_img->header = chd_get_header(chd_img->chd);
 
@@ -1064,7 +1076,8 @@ static int handlechd(const char *isofile) {
    cddaBigEndian = TRUE;
 
        numtracks = 0;
-       int frame_offset = 150;
+       int frame_offset = 0;
+       int file_offset = 0;
        memset(ti, 0, sizeof(ti));
 
    while (1)
@@ -1089,18 +1102,20 @@ static int handlechd(const char *isofile) {
       else
          break;
 
-               ti[md.track].type = !strncmp(md.type, "AUDIO", 5) ? CDDA : DATA;
+               if(md.track == 1)
+                       md.pregap = 150;
+               else
+                       sec2msf(msf2sec(ti[md.track-1].length) + md.pregap, ti[md.track-1].length);
 
-      sec2msf(frame_offset + md.pregap, ti[md.track].start);
-      sec2msf(md.frames - md.pregap, ti[md.track].length);
+               ti[md.track].type = !strncmp(md.type, "AUDIO", 5) ? CDDA : DATA;
 
-      if (!strcmp(md.type, md.pgtype))
-         frame_offset += md.pregap;
+               sec2msf(frame_offset + md.pregap, ti[md.track].start);
+               sec2msf(md.frames, ti[md.track].length);
 
-      ti[md.track].start_offset = frame_offset * CD_FRAMESIZE_RAW;
+               ti[md.track].start_offset = file_offset;
 
-               frame_offset += md.frames;
-               frame_offset += md.postgap;
+               frame_offset += md.pregap + md.frames + md.postgap;
+               file_offset += md.frames + md.postgap;
                numtracks++;
        }
 
@@ -1131,7 +1146,7 @@ static int opensubfile(const char *isoname) {
                return -1;
        }
 
-       subHandle = fopen(subname, "rb");
+       subHandle = fopen_utf8(subname, "rb");
        if (subHandle == NULL) {
                return -1;
        }
@@ -1260,14 +1275,19 @@ static void *readThreadMain(void *param) {
       last_read_sector = requested_sector_end;
     }
 
+    index = ra_sector % SECTOR_BUFFER_SIZE;
+
     // check for end of CD
     if (ra_count && ra_sector >= max_sector) {
       ra_count = 0;
+      pthread_mutex_lock(&sectorbuffer_lock);
+      sectorbuffer[index].ret = -1;
+      sectorbuffer[index].sector = ra_sector;
+      pthread_cond_signal(&sectorbuffer_cond);
+      pthread_mutex_unlock(&sectorbuffer_lock);
     }
 
     if (ra_count) {
-
-      index = ra_sector % SECTOR_BUFFER_SIZE;
       pthread_mutex_lock(&sectorbuffer_lock);
       if (sectorbuffer[index].sector != ra_sector) {
         pthread_mutex_unlock(&sectorbuffer_lock);
@@ -1477,19 +1497,17 @@ static int cdread_chd(FILE *f, unsigned int base, void *dest, int sector)
        int hunk;
 
        if (base)
-               sector += base / CD_FRAMESIZE_RAW;
+               sector += base;
 
        hunk = sector / chd_img->sectors_per_hunk;
        chd_img->sector_in_hunk = sector % chd_img->sectors_per_hunk;
 
-       if (hunk == chd_img->current_hunk)
-               goto finish;
-
-       chd_read(chd_img->chd, hunk, chd_img->buffer);
-
-       chd_img->current_hunk = hunk;
+       if (hunk != chd_img->current_hunk)
+       {
+               chd_read(chd_img->chd, hunk, chd_img->buffer);
+               chd_img->current_hunk = hunk;
+       }
 
-finish:
        if (dest != cdbuffer) // copy avoid HACK
                memcpy(dest, chd_img->buffer[chd_img->sector_in_hunk],
                        CD_FRAMESIZE_RAW);
@@ -1607,7 +1625,7 @@ static long CALLBACK ISOopen(void) {
                return 0; // it's already open
        }
 
-       cdHandle = fopen(GetIsoFile(), "rb");
+       cdHandle = fopen_utf8(GetIsoFile(), "rb");
        if (cdHandle == NULL) {
                SysPrintf(_("Could't open '%s' for reading: %s\n"),
                        GetIsoFile(), strerror(errno));
@@ -1679,7 +1697,7 @@ static long CALLBACK ISOopen(void) {
                        p = alt_bin_filename + strlen(alt_bin_filename) - 4;
                        for (i = 0; i < sizeof(exts) / sizeof(exts[0]); i++) {
                                strcpy(p, exts[i]);
-                               tmpf = fopen(alt_bin_filename, "rb");
+                               tmpf = fopen_utf8(alt_bin_filename, "rb");
                                if (tmpf != NULL)
                                        break;
                        }
@@ -1715,7 +1733,7 @@ static long CALLBACK ISOopen(void) {
 
        // make sure we have another handle open for cdda
        if (numtracks > 1 && ti[1].handle == NULL) {
-               ti[1].handle = fopen(bin_filename, "rb");
+               ti[1].handle = fopen_utf8(bin_filename, "rb");
        }
        cdda_cur_sector = 0;
        cdda_file_offset = 0;