Skip to content

Add userdata argument to Mix_ChannelFinished callback #689

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions examples/playwave.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ static void test_versions(void)
}

static int channel_is_done = 0;
static void SDLCALL channel_complete_callback (int chan)
static void SDLCALL channel_complete_callback (void *userdata, int chan)
{
if (verbose) {
Mix_Chunk *done_chunk = Mix_GetChunk(chan);
Expand All @@ -86,6 +86,8 @@ static void SDLCALL channel_complete_callback (int chan)
SDL_Log(" Which %s correct.\n", (g_wave == done_chunk) ? "is" : "is NOT");
}
channel_is_done = 1;

(void) userdata; /* rcg06192001 unused */
}

/* rcg06192001 abstract this out for testing purposes. */
Expand Down Expand Up @@ -415,7 +417,7 @@ int main(int argc, char *argv[])
flip_sample(g_wave);
}

Mix_ChannelFinished(channel_complete_callback);
Mix_ChannelFinished(NULL, channel_complete_callback);

if ((!Mix_SetReverseStereo(MIX_CHANNEL_POST, reverse_stereo)) &&
(reverse_stereo))
Expand Down
5 changes: 3 additions & 2 deletions include/SDL3_mixer/SDL_mixer.h
Original file line number Diff line number Diff line change
Expand Up @@ -1052,7 +1052,7 @@ extern SDL_DECLSPEC void SDLCALL Mix_HookMusicFinished(Mix_MusicFinishedCallback
*/
extern SDL_DECLSPEC void * SDLCALL Mix_GetMusicHookData(void);

typedef void (SDLCALL *Mix_ChannelFinishedCallback)(int channel);
typedef void (SDLCALL *Mix_ChannelFinishedCallback)(void *userdata, int channel);

/**
* Set a callback that runs when a channel has finished playing.
Expand All @@ -1065,12 +1065,13 @@ typedef void (SDLCALL *Mix_ChannelFinishedCallback)(int channel);
*
* A NULL pointer will disable the callback.
*
* \param userdata the user data to pass to the callback.
* \param channel_finished the callback function to become the new
* notification mechanism.
*
* \since This function is available since SDL_mixer 3.0.0.
*/
extern SDL_DECLSPEC void SDLCALL Mix_ChannelFinished(Mix_ChannelFinishedCallback channel_finished);
extern SDL_DECLSPEC void SDLCALL Mix_ChannelFinished(void *userdata, Mix_ChannelFinishedCallback channel_finished);

/**
* Magic number for effects to operate on the postmix instead of a channel.
Expand Down
6 changes: 4 additions & 2 deletions src/mixer.c
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ static void *mix_postmix_data = NULL;

/* rcg07062001 callback to alert when channels are done playing. */
static Mix_ChannelFinishedCallback channel_done_callback = NULL;
static void *channel_done_callback_userdata = NULL;

/* Support for user defined music functions */
static Mix_MixCallback mix_music = music_mixer;
Expand Down Expand Up @@ -291,7 +292,7 @@ static bool _Mix_remove_all_effects(int channel, effect_info **e);
static void _Mix_channel_done_playing(int channel)
{
if (channel_done_callback) {
channel_done_callback(channel);
channel_done_callback(channel_done_callback_userdata, channel);
}

/*
Expand Down Expand Up @@ -1023,10 +1024,11 @@ void *Mix_GetMusicHookData(void)
return music_data;
}

void Mix_ChannelFinished(Mix_ChannelFinishedCallback channel_finished)
void Mix_ChannelFinished(void *userdata, Mix_ChannelFinishedCallback channel_finished)
{
Mix_LockAudio();
channel_done_callback = channel_finished;
channel_done_callback_userdata = userdata;
Mix_UnlockAudio();
}

Expand Down
Loading