Commit 1124e169 authored by Vladimir Serbinenko's avatar Vladimir Serbinenko
Browse files

Migrate to VFS interface

parent 1d4dc49f
...@@ -34,3 +34,17 @@ SOURCES_C := \ ...@@ -34,3 +34,17 @@ SOURCES_C := \
$(GUI)/retro/SDL_gfxPrimitives.c \ $(GUI)/retro/SDL_gfxPrimitives.c \
$(GUI)/retro/retro_surface.c \ $(GUI)/retro/retro_surface.c \
$(GUI)/app.c $(GUI)/app.c
ifneq ($(STATIC_LINKING), 1)
SOURCES_C += \
$(CORE_DIR)/libretro-common/compat/compat_strl.c \
$(CORE_DIR)/libretro-common/compat/fopen_utf8.c \
$(CORE_DIR)/libretro-common/encodings/encoding_utf.c \
$(CORE_DIR)/libretro-common/file/file_path.c \
$(CORE_DIR)/libretro-common/file/retro_dirent.c \
$(CORE_DIR)/libretro-common/time/rtime.c \
$(CORE_DIR)/libretro-common/streams/file_stream.c \
$(CORE_DIR)/libretro-common/string/stdstring.c \
$(CORE_DIR)/libretro-common/vfs/vfs_implementation.c
endif
...@@ -8,7 +8,9 @@ ...@@ -8,7 +8,9 @@
#include <features_cpu.h> #include <features_cpu.h>
#endif #endif
#include "libretro.h" #include <libretro.h>
#include <streams/file_stream.h>
#include <retro_dirent.h>
#include "libretro-core.h" #include "libretro-core.h"
#include "MACkeymap.h" #include "MACkeymap.h"
#include "vkbd.i" #include "vkbd.i"
...@@ -45,6 +47,7 @@ char slash = '/'; ...@@ -45,6 +47,7 @@ char slash = '/';
#endif #endif
bool retro_load_ok = false; bool retro_load_ok = false;
struct retro_vfs_interface *vfs_interface;
char RETRO_DIR[512]; char RETRO_DIR[512];
...@@ -117,15 +120,22 @@ static char CMDFILE[512]; ...@@ -117,15 +120,22 @@ static char CMDFILE[512];
int loadcmdfile(char *argv) int loadcmdfile(char *argv)
{ {
int res = 0; int res = 0;
FILE *fp = fopen(argv,"r"); memset(CMDFILE, 0, sizeof(CMDFILE));
RFILE *h = filestream_open(argv, RETRO_VFS_FILE_ACCESS_READ, RETRO_VFS_FILE_ACCESS_HINT_NONE);
if (fp) char *p;
{ if (h) {
if ( fgets (CMDFILE , 512 , fp) != NULL ) filestream_read(h, CMDFILE, sizeof(CMDFILE) - 1);
res=1; filestream_close(h);
fclose (fp); res = 1;
} }
p = strchr(CMDFILE, '\n');
if (p)
*p = '\0';
p = strchr(CMDFILE, '\r');
if (p)
*p = '\0';
return res; return res;
} }
...@@ -354,6 +364,15 @@ void retro_set_environment(retro_environment_t cb) ...@@ -354,6 +364,15 @@ void retro_set_environment(retro_environment_t cb)
cb(RETRO_ENVIRONMENT_SET_SUPPORT_NO_GAME, &no_content); cb(RETRO_ENVIRONMENT_SET_SUPPORT_NO_GAME, &no_content);
cb(RETRO_ENVIRONMENT_SET_VARIABLES, variables); cb(RETRO_ENVIRONMENT_SET_VARIABLES, variables);
struct retro_vfs_interface_info vfs_interface_info;
vfs_interface_info.required_interface_version = 3;
vfs_interface_info.iface = NULL;
if(cb(RETRO_ENVIRONMENT_GET_VFS_INTERFACE, &vfs_interface_info)) {
vfs_interface = vfs_interface_info.iface;
}
dirent_vfs_init(&vfs_interface_info);
filestream_vfs_init(&vfs_interface_info);
} }
static void update_variables(void) static void update_variables(void)
......
...@@ -105,6 +105,7 @@ extern int VIRTUAL_WIDTH; ...@@ -105,6 +105,7 @@ extern int VIRTUAL_WIDTH;
extern int retrow ; extern int retrow ;
extern int retroh ; extern int retroh ;
extern int minivmac_statusbar; extern int minivmac_statusbar;
extern struct retro_vfs_interface *vfs_interface;
//FUNCS //FUNCS
extern void mainloop_retro(void); extern void mainloop_retro(void);
......
...@@ -36,7 +36,6 @@ int LOADCONTENT=-1; ...@@ -36,7 +36,6 @@ int LOADCONTENT=-1;
int LDRIVE=8; int LDRIVE=8;
#define NK_INCLUDE_FIXED_TYPES #define NK_INCLUDE_FIXED_TYPES
#define NK_INCLUDE_STANDARD_IO
#define NK_INCLUDE_STANDARD_VARARGS #define NK_INCLUDE_STANDARD_VARARGS
#define NK_INCLUDE_DEFAULT_ALLOCATOR #define NK_INCLUDE_DEFAULT_ALLOCATOR
#define NK_IMPLEMENTATION #define NK_IMPLEMENTATION
......
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <dirent.h> #ifndef _MSC_VER
#include <unistd.h>
#endif
#include <retro_dirent.h>
/* =============================================================== /* ===============================================================
* *
...@@ -45,6 +48,7 @@ die(const char *fmt, ...) ...@@ -45,6 +48,7 @@ die(const char *fmt, ...)
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
#ifndef __LIBRETRO__
static char* static char*
file_load(const char* path, size_t* siz) file_load(const char* path, size_t* siz)
{ {
...@@ -59,6 +63,7 @@ file_load(const char* path, size_t* siz) ...@@ -59,6 +63,7 @@ file_load(const char* path, size_t* siz)
fclose(fd); fclose(fd);
return buf; return buf;
} }
#endif
static char* static char*
str_duplicate(const char *src) str_duplicate(const char *src)
...@@ -88,10 +93,9 @@ dir_list(const char *dir, int return_subdirs, size_t *count) ...@@ -88,10 +93,9 @@ dir_list(const char *dir, int return_subdirs, size_t *count)
size_t n = 0; size_t n = 0;
char buffer[MAX_PATH_LEN]; char buffer[MAX_PATH_LEN];
char **results = NULL; char **results = NULL;
const DIR *none = NULL;
size_t capacity = 32; size_t capacity = 32;
size_t size; size_t size;
DIR *z; struct RDIR *zh = NULL;
assert(dir); assert(dir);
assert(count); assert(count);
...@@ -103,24 +107,21 @@ dir_list(const char *dir, int return_subdirs, size_t *count) ...@@ -103,24 +107,21 @@ dir_list(const char *dir, int return_subdirs, size_t *count)
size = 0; size = 0;
z = opendir(dir); zh = retro_opendir(dir);
if (z != none) { if (zh != NULL) {
int nonempty = 1; int nonempty = 1;
struct dirent *data = readdir(z); nonempty = retro_readdir(zh);
nonempty = (data != NULL);
if (!nonempty) return NULL; if (!nonempty) return NULL;
do { do {
DIR *y;
char *p; char *p;
int is_subdir; int is_subdir = 0;
if (data->d_name[0] == '.') const char *name = NULL;
name = retro_dirent_get_name(zh);
if (name[0] == '.')
continue; continue;
strncpy(buffer + n, data->d_name, MAX_PATH_LEN-n); is_subdir = retro_dirent_is_dir(zh, NULL);
y = opendir(buffer);
is_subdir = (y != NULL);
if (y != NULL) closedir(y);
if ((return_subdirs && is_subdir) || (!is_subdir && !return_subdirs)){ if ((return_subdirs && is_subdir) || (!is_subdir && !return_subdirs)){
if (!size) { if (!size) {
...@@ -132,13 +133,12 @@ dir_list(const char *dir, int return_subdirs, size_t *count) ...@@ -132,13 +133,12 @@ dir_list(const char *dir, int return_subdirs, size_t *count)
assert(results); assert(results);
if (!results) free(old); if (!results) free(old);
} }
p = str_duplicate(data->d_name); p = str_duplicate(name);
results[size++] = p; results[size++] = p;
} }
} while ((data = readdir(z)) != NULL); } while (zh != NULL && retro_readdir(zh));
} }
retro_closedir(zh);
if (z) closedir(z);
*count = size; *count = size;
return results; return results;
} }
......
...@@ -8,7 +8,6 @@ ...@@ -8,7 +8,6 @@
//#warning just an SDL wrapper for use SDL surface/maprgba in the core. //#warning just an SDL wrapper for use SDL surface/maprgba in the core.
#include <stdlib.h> #include <stdlib.h>
#include <unistd.h>
#include <time.h> #include <time.h>
/* The number of elements in a table */ /* The number of elements in a table */
......
...@@ -29,6 +29,10 @@ ...@@ -29,6 +29,10 @@
#include <time.h> #include <time.h>
#include <stdlib.h> #include <stdlib.h>
#include <retro_inline.h>
#include <libretro.h>
#include <streams/file_stream.h>
#include "libretro-core.h"
#include "CNFGRAPI.h" #include "CNFGRAPI.h"
#include "SYSDEPNS.h" #include "SYSDEPNS.h"
...@@ -238,7 +242,7 @@ LOCALVAR const ui3b Native2MacRomanTab[] = { ...@@ -238,7 +242,7 @@ LOCALVAR const ui3b Native2MacRomanTab[] = {
#endif #endif
#if IncludePbufs #if IncludePbufs
LOCALFUNC tMacErr NativeTextToMacRomanPbuf(char *x, tPbuf *r) LOCALFUNC tMacErr NativeTextToMacRomanPbuf(const char *x, tPbuf *r)
{ {
if (NULL == x) { if (NULL == x) {
return mnvm_miscErr; return mnvm_miscErr;
...@@ -250,7 +254,7 @@ LOCALFUNC tMacErr NativeTextToMacRomanPbuf(char *x, tPbuf *r) ...@@ -250,7 +254,7 @@ LOCALFUNC tMacErr NativeTextToMacRomanPbuf(char *x, tPbuf *r)
if (NULL == p) { if (NULL == p) {
return mnvm_miscErr; return mnvm_miscErr;
} else { } else {
ui3b *p0 = (ui3b *)x; const ui3b *p0 = (const ui3b *)x;
ui3b *p1 = (ui3b *)p; ui3b *p1 = (ui3b *)p;
int i; int i;
...@@ -369,10 +373,7 @@ LOCALPROC NativeStrFromCStr(char *r, char *s) ...@@ -369,10 +373,7 @@ LOCALPROC NativeStrFromCStr(char *r, char *s)
#define NotAfileRef NULL #define NotAfileRef NULL
LOCALVAR FILE *Drives[NumDrives]; /* open disk image files */ LOCALVAR RFILE *Drives[NumDrives]; /* open disk image files */
#if IncludeSonyGetName || IncludeSonyNew
LOCALVAR char *DriveNames[NumDrives];
#endif
LOCALPROC InitDrives(void) LOCALPROC InitDrives(void)
{ {
...@@ -384,9 +385,6 @@ LOCALPROC InitDrives(void) ...@@ -384,9 +385,6 @@ LOCALPROC InitDrives(void)
for (i = 0; i < NumDrives; ++i) { for (i = 0; i < NumDrives; ++i) {
Drives[i] = NotAfileRef; Drives[i] = NotAfileRef;
#if IncludeSonyGetName || IncludeSonyNew
DriveNames[i] = NULL;
#endif
} }
} }
...@@ -395,14 +393,14 @@ GLOBALFUNC tMacErr vSonyTransfer(blnr IsWrite, ui3p Buffer, ...@@ -395,14 +393,14 @@ GLOBALFUNC tMacErr vSonyTransfer(blnr IsWrite, ui3p Buffer,
ui5r *Sony_ActCount) ui5r *Sony_ActCount)
{ {
tMacErr err = mnvm_miscErr; tMacErr err = mnvm_miscErr;
FILE *refnum = Drives[Drive_No]; RFILE *refnum = Drives[Drive_No];
ui5r NewSony_Count = 0; ui5r NewSony_Count = 0;
if (0 == fseek(refnum, Sony_Start, SEEK_SET)) { if (filestream_seek(refnum, Sony_Start, RETRO_VFS_SEEK_POSITION_START) >= 0) {
if (IsWrite) { if (IsWrite) {
NewSony_Count = fwrite(Buffer, 1, Sony_Count, refnum); NewSony_Count = filestream_write(refnum, Buffer, Sony_Count);
} else { } else {
NewSony_Count = fread(Buffer, 1, Sony_Count, refnum); NewSony_Count = filestream_read(refnum, Buffer, Sony_Count);
} }
if (NewSony_Count == Sony_Count) { if (NewSony_Count == Sony_Count) {
...@@ -420,42 +418,34 @@ GLOBALFUNC tMacErr vSonyTransfer(blnr IsWrite, ui3p Buffer, ...@@ -420,42 +418,34 @@ GLOBALFUNC tMacErr vSonyTransfer(blnr IsWrite, ui3p Buffer,
GLOBALFUNC tMacErr vSonyGetSize(tDrive Drive_No, ui5r *Sony_Count) GLOBALFUNC tMacErr vSonyGetSize(tDrive Drive_No, ui5r *Sony_Count)
{ {
tMacErr err = mnvm_miscErr; tMacErr err = mnvm_miscErr;
FILE *refnum = Drives[Drive_No]; RFILE *refnum = Drives[Drive_No];
long v; int64_t sz = filestream_get_size(refnum);
if (sz < 0)
if (0 == fseek(refnum, 0, SEEK_END)) { return mnvm_miscErr;
v = ftell(refnum);
if (v >= 0) {
*Sony_Count = v;
err = mnvm_noErr;
}
}
return err; /*& figure out what really to return &*/ *Sony_Count = sz;
return mnvm_noErr;
} }
LOCALFUNC tMacErr vSonyEject0(tDrive Drive_No, blnr deleteit) LOCALFUNC tMacErr vSonyEject0(tDrive Drive_No, blnr deleteit)
{ {
FILE *refnum = Drives[Drive_No]; RFILE *refnum = Drives[Drive_No];
char *s = deleteit ? strdup(filestream_get_path(refnum)) : NULL;
DiskEjectedNotify(Drive_No); DiskEjectedNotify(Drive_No);
fclose(refnum); filestream_close(refnum);
Drives[Drive_No] = NotAfileRef; /* not really needed */ Drives[Drive_No] = NotAfileRef; /* not really needed */
#if IncludeSonyGetName || IncludeSonyNew if (NULL != s) {
{ if (deleteit) {
char *s = DriveNames[Drive_No]; if (vfs_interface)
if (NULL != s) { vfs_interface->remove(s);
if (deleteit) { else
remove(s); remove(s);
}
free(s);
DriveNames[Drive_No] = NULL; /* not really needed */
} }
free(s);
} }
#endif
return mnvm_noErr; return mnvm_noErr;
} }
...@@ -486,11 +476,11 @@ LOCALPROC UnInitDrives(void) ...@@ -486,11 +476,11 @@ LOCALPROC UnInitDrives(void)
#if IncludeSonyGetName #if IncludeSonyGetName
GLOBALFUNC tMacErr vSonyGetName(tDrive Drive_No, tPbuf *r) GLOBALFUNC tMacErr vSonyGetName(tDrive Drive_No, tPbuf *r)
{ {
char *drivepath = DriveNames[Drive_No]; const char *drivepath = filestream_get_path(Drives[Drive_No]);
if (NULL == drivepath) { if (NULL == drivepath) {
return mnvm_miscErr; return mnvm_miscErr;
} else { } else {
char *s = strrchr(drivepath, '/'); const char *s = strrchr(drivepath, '/');
if (NULL == s) { if (NULL == s) {
s = drivepath; s = drivepath;
} else { } else {
...@@ -501,7 +491,7 @@ GLOBALFUNC tMacErr vSonyGetName(tDrive Drive_No, tPbuf *r) ...@@ -501,7 +491,7 @@ GLOBALFUNC tMacErr vSonyGetName(tDrive Drive_No, tPbuf *r)
} }
#endif #endif
LOCALFUNC blnr Sony_Insert0(FILE *refnum, blnr locked, LOCALFUNC blnr Sony_Insert0(RFILE *refnum, blnr locked,
char *drivepath) char *drivepath)
{ {
tDrive Drive_No; tDrive Drive_No;
...@@ -517,24 +507,12 @@ LOCALFUNC blnr Sony_Insert0(FILE *refnum, blnr locked, ...@@ -517,24 +507,12 @@ LOCALFUNC blnr Sony_Insert0(FILE *refnum, blnr locked,
{ {
Drives[Drive_No] = refnum; Drives[Drive_No] = refnum;
DiskInsertNotify(Drive_No, locked); DiskInsertNotify(Drive_No, locked);
#if IncludeSonyGetName || IncludeSonyNew
{
ui5b L = strlen(drivepath);
char *p = malloc(L + 1);
if (p != NULL) {
(void) memcpy(p, drivepath, L + 1);
}
DriveNames[Drive_No] = p;
}
#endif
IsOk = trueblnr; IsOk = trueblnr;
} }
} }
if (! IsOk) { if (! IsOk) {
fclose(refnum); filestream_close(refnum);
} }
return IsOk; return IsOk;
...@@ -544,10 +522,10 @@ LOCALFUNC blnr Sony_Insert0(FILE *refnum, blnr locked, ...@@ -544,10 +522,10 @@ LOCALFUNC blnr Sony_Insert0(FILE *refnum, blnr locked,
{ {
blnr locked = falseblnr; blnr locked = falseblnr;
/* printf("Sony_Insert1 %s\n", drivepath); */ /* printf("Sony_Insert1 %s\n", drivepath); */
FILE *refnum = fopen(drivepath, "rb+"); RFILE *refnum = filestream_open(drivepath, RETRO_VFS_FILE_ACCESS_READ_WRITE | RETRO_VFS_FILE_ACCESS_UPDATE_EXISTING, RETRO_VFS_FILE_ACCESS_HINT_NONE);
if (NULL == refnum) { if (NULL == refnum) {
locked = trueblnr; locked = trueblnr;
refnum = fopen(drivepath, "rb"); refnum = filestream_open(drivepath, RETRO_VFS_FILE_ACCESS_READ, RETRO_VFS_FILE_ACCESS_HINT_NONE);
} }
if (NULL == refnum) { if (NULL == refnum) {
if (! silentfail) { if (! silentfail) {
...@@ -602,7 +580,7 @@ LOCALFUNC blnr LoadInitialImages(void) ...@@ -602,7 +580,7 @@ LOCALFUNC blnr LoadInitialImages(void)
} }
#if IncludeSonyNew #if IncludeSonyNew
LOCALFUNC blnr WriteZero(FILE *refnum, ui5b L) LOCALFUNC blnr WriteZero(RFILE *refnum, ui5b L)
{ {
#define ZeroBufferSize 2048 #define ZeroBufferSize 2048
ui5b i; ui5b i;
...@@ -612,7 +590,7 @@ LOCALFUNC blnr WriteZero(FILE *refnum, ui5b L) ...@@ -612,7 +590,7 @@ LOCALFUNC blnr WriteZero(FILE *refnum, ui5b L)
while (L > 0) { while (L > 0) {
i = (L > ZeroBufferSize) ? ZeroBufferSize : L; i = (L > ZeroBufferSize) ? ZeroBufferSize : L;
if (fwrite(buffer, 1, i, refnum) != i) { if (filestream_write(refnum, buffer, i) != i) {
return falseblnr; return falseblnr;
} }
L -= i; L -= i;
...@@ -625,7 +603,7 @@ LOCALFUNC blnr WriteZero(FILE *refnum, ui5b L) ...@@ -625,7 +603,7 @@ LOCALFUNC blnr WriteZero(FILE *refnum, ui5b L)
LOCALPROC MakeNewDisk0(ui5b L, char *drivepath) LOCALPROC MakeNewDisk0(ui5b L, char *drivepath)
{ {
blnr IsOk = falseblnr; blnr IsOk = falseblnr;
FILE *refnum = fopen(drivepath, "wb+"); RFILE *refnum = filestream_open(drivepath, RETRO_VFS_FILE_ACCESS_READ_WRITE, RETRO_VFS_FILE_ACCESS_HINT_NONE);
if (NULL == refnum) { if (NULL == refnum) {
MacMsg(kStrOpenFailTitle, kStrOpenFailMessage, falseblnr); MacMsg(kStrOpenFailTitle, kStrOpenFailMessage, falseblnr);
} else { } else {
...@@ -634,10 +612,13 @@ LOCALPROC MakeNewDisk0(ui5b L, char *drivepath) ...@@ -634,10 +612,13 @@ LOCALPROC MakeNewDisk0(ui5b L, char *drivepath)
refnum = NULL; refnum = NULL;
} }
if (refnum != NULL) { if (refnum != NULL) {
fclose(refnum); filestream_close(refnum);
} }
if (! IsOk) { if (! IsOk) {
(void) remove(drivepath); if (vfs_interface)
vfs_interface->remove(drivepath);
else
(void) remove(drivepath);
} }
} }
} }
...@@ -691,16 +672,16 @@ LOCALVAR char *rom_path = NULL; ...@@ -691,16 +672,16 @@ LOCALVAR char *rom_path = NULL;
LOCALFUNC tMacErr LoadMacRomFrom(char *path) LOCALFUNC tMacErr LoadMacRomFrom(char *path)
{ {
tMacErr err; tMacErr err;
FILE *ROM_File; RFILE *ROM_File;
int File_Size; int File_Size;
ROM_File = fopen(path, "rb"); ROM_File = filestream_open(path, RETRO_VFS_FILE_ACCESS_READ, RETRO_VFS_FILE_ACCESS_HINT_NONE);
if (NULL == ROM_File) { if (NULL == ROM_File) {
err = mnvm_fnfErr; err = mnvm_fnfErr;
} else { } else {
File_Size = fread(ROM, 1, kROM_Size, ROM_File); File_Size = filestream_read(ROM_File, ROM, kROM_Size);
if (File_Size != kROM_Size) { if (File_Size != kROM_Size) {
if (feof(ROM_File)) { if (filestream_eof(ROM_File)) {
err = mnvm_eofErr; err = mnvm_eofErr;
} else { } else {
err = mnvm_miscErr; err = mnvm_miscErr;
...@@ -708,7 +689,7 @@ LOCALFUNC tMacErr LoadMacRomFrom(char *path) ...@@ -708,7 +689,7 @@ LOCALFUNC tMacErr LoadMacRomFrom(char *path)
} else { } else {
err = mnvm_noErr; err = mnvm_noErr;
} }
fclose(ROM_File); filestream_close(ROM_File);