Skip to content

Commit

Permalink
Add ability to set custom path open function
Browse files Browse the repository at this point in the history
  • Loading branch information
ds-sloth authored and Wohlstand committed Aug 16, 2024
1 parent dc3dca1 commit fc8b7f0
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 9 deletions.
13 changes: 13 additions & 0 deletions include/SDL_mixer.h
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,11 @@ typedef enum {
*/
typedef struct Mix_Music Mix_Music;

/**
* The type of a RWFromFile callback
*/
typedef SDL_RWops* (*Mix_RWFromFile_cb)(const char *file, const char *mode);

/**
* Open the default audio device for playback.
*
Expand Down Expand Up @@ -3716,6 +3721,14 @@ extern DECLSPEC int MIXCALL Mix_InitMixer(const SDL_AudioSpec *spec, SDL_bool sk
*/
extern DECLSPEC void MIXCALL Mix_FreeMixer(void);/*MixerX*/

/**
* Set a function that MixerX will use to open RWops handles from file paths,
* or pass NULL to use the default SDL_RWFromFile.
*
* This is the MixerX fork exclusive function.
*/
extern DECLSPEC void MIXCALL Mix_SetRWFromFile(Mix_RWFromFile_cb cb);/*MixerX*/

/**
* Close the mixer, halting all playing audio.
*
Expand Down
2 changes: 1 addition & 1 deletion src/codecs/music_fluidlite.c
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ static void FLUIDSYNTH_Delete(void *context);
#if 0
static int SDLCALL fluidsynth_check_soundfont(const char *path, void *data)
{
SDL_RWops *rw = SDL_RWFromFile(path, "rb");
SDL_RWops *rw = _Mix_RWFromFile(path, "rb");

(void)data;
if (rw) {
Expand Down
2 changes: 1 addition & 1 deletion src/codecs/music_fluidsynth.c
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ static void FLUIDSYNTH_Delete(void *context);

static int SDLCALL fluidsynth_check_soundfont(const char *path, void *data)
{
SDL_RWops *rw = SDL_RWFromFile(path, "rb");
SDL_RWops *rw = _Mix_RWFromFile(path, "rb");

(void)data;
if (rw) {
Expand Down
4 changes: 2 additions & 2 deletions src/codecs/music_wavpack.c
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ static void *WAVPACK_CreateFromFile(const char *file)
size_t len;
char *file2;

src1 = SDL_RWFromFile(file, "rb");
src1 = _Mix_RWFromFile(file, "rb");
if (!src1) {
Mix_SetError("Couldn't open '%s'", file);
return NULL;
Expand All @@ -335,7 +335,7 @@ static void *WAVPACK_CreateFromFile(const char *file)
SDL_memcpy(file2, file, len);
file2[len] = 'c';
file2[len + 1] = '\0';
src2 = SDL_RWFromFile(file2, "rb");
src2 = _Mix_RWFromFile(file2, "rb");
#if WAVPACK_DBG
if (src2) {
SDL_Log("Loaded WavPack correction file %s", file2);
Expand Down
4 changes: 2 additions & 2 deletions src/codecs/timidity/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ SDL_RWops *timi_openfile(const char *name)
/* First try the given name */

SNDDBG(("Trying to open %s\n", name));
if ((rw = SDL_RWFromFile(name, "rb")) != NULL)
if ((rw = _Mix_RWFromFile(name, "rb")) != NULL)
return rw;

if (!is_abspath(name))
Expand All @@ -69,7 +69,7 @@ SDL_RWops *timi_openfile(const char *name)
}
SDL_strlcpy(p, name, sizeof(current_filename) - l);
SNDDBG(("Trying to open %s\n", current_filename));
if ((rw = SDL_RWFromFile(current_filename, "rb")))
if ((rw = _Mix_RWFromFile(current_filename, "rb")))
return rw;
plp = plp->next;
}
Expand Down
20 changes: 19 additions & 1 deletion src/mixer.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ SDL_COMPILE_TIME_ASSERT(SDL_MIXER_PATCHLEVEL_min, SDL_MIXER_PATCHLEVEL >= 0);
SDL_COMPILE_TIME_ASSERT(SDL_MIXER_PATCHLEVEL_max, SDL_MIXER_PATCHLEVEL <= 99);
#endif

Mix_RWFromFile_cb _Mix_RWFromFile = SDL_RWFromFile;

static int audio_opened = 0;
static SDL_AudioSpec mixer;
static SDL_AudioDeviceID audio_device;
Expand Down Expand Up @@ -949,7 +951,7 @@ Mix_Chunk * MIXCALLCC Mix_LoadWAV_RW(SDL_RWops *src, int freesrc)

Mix_Chunk * MIXCALLCC Mix_LoadWAV(const char *file)
{
return Mix_LoadWAV_RW(SDL_RWFromFile(file, "rb"), 1);
return Mix_LoadWAV_RW(_Mix_RWFromFile(file, "rb"), 1);
}

/* Load a wave file of the mixer format from a memory buffer */
Expand Down Expand Up @@ -1509,6 +1511,22 @@ void MIXCALLCC Mix_FreeMixer(void)
}
}

/**
* Set a function that MixerX will use to open RWops handles from file paths,
* or pass NULL to use the default SDL_RWFromFile.
*
* This is the MixerX fork exclusive function.
*/
void MIXCALLCC Mix_SetRWFromFile(Mix_RWFromFile_cb cb)
{
if (cb) {
_Mix_RWFromFile = cb;
}
else {
_Mix_RWFromFile = SDL_RWFromFile;
}
}

/* Close the audio device, stop, and free all our mixer elements */
void MIXCALLCC Mix_CloseAudio(void)
{
Expand Down
4 changes: 4 additions & 0 deletions src/mixer.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,16 @@
#ifndef MIXER_H_
#define MIXER_H_

typedef SDL_RWops* (*Mix_RWFromFile_cb)(const char *file, const char *mode);

/* Locking wrapper functions */
extern void Mix_LockAudio(void);
extern void Mix_UnlockAudio(void);

extern void add_chunk_decoder(const char *decoder);

extern Mix_RWFromFile_cb _Mix_RWFromFile;

#endif /* MIXER_H_ */

/* vi: set ts=4 sw=4 expandtab: */
4 changes: 2 additions & 2 deletions src/music.c
Original file line number Diff line number Diff line change
Expand Up @@ -1887,7 +1887,7 @@ Mix_Music * MIXCALLCC Mix_LoadMUS(const char *file)
}
}

src = SDL_RWFromFile(file, "rb");
src = _Mix_RWFromFile(file, "rb");
if (src == NULL) {
Mix_SetError("Couldn't open '%s'", file);
SDL_free(music_file);
Expand Down Expand Up @@ -3435,7 +3435,7 @@ const char* MIXCALLCC Mix_GetSoundFonts(void)
unsigned i;

for (i = 0; i < SDL_arraysize(s_soundfont_paths); ++i) {
SDL_RWops *rwops = SDL_RWFromFile(s_soundfont_paths[i], "rb");
SDL_RWops *rwops = _Mix_RWFromFile(s_soundfont_paths[i], "rb");
if (rwops) {
SDL_RWclose(rwops);
return s_soundfont_paths[i];
Expand Down

0 comments on commit fc8b7f0

Please sign in to comment.