From 9c591299856d3817b5035ef78d49e1fb24061952 Mon Sep 17 00:00:00 2001 From: Sam Lantinga Date: Sat, 20 May 2006 04:35:58 +0000 Subject: [PATCH] Added API requested in bug #43: Added SDL_GetNumVideoDrivers() and SDL_GetVideoDriver(). Replaced SDL_VideoDriverName() with SDL_GetCurrentVideoDriver() Added SDL_GetNumAudioDrivers() and SDL_GetAudioDriver(). Replaced SDL_AudioDriverName() with SDL_GetCurrentAudioDriver() --- WhatsNew | 10 +++++----- include/SDL_audio.h | 13 +++++++++---- include/SDL_video.h | 13 +++++++++---- src/audio/SDL_audio.c | 20 ++++++++++++++++---- src/video/SDL_video.c | 19 +++++++++++++++---- test/loopwave.c | 19 +++++++++++++++++-- test/testvidinfo.c | 22 +++++++++++++++++++--- 7 files changed, 90 insertions(+), 26 deletions(-) diff --git a/WhatsNew b/WhatsNew index 5f6cea345..64f669284 100644 --- a/WhatsNew +++ b/WhatsNew @@ -1,7 +1,11 @@ This is a list of API changes in SDL's version history. -Version 1.0: +1.3.0: + Added SDL_GetNumVideoDrivers() and SDL_GetVideoDriver(). + Replaced SDL_VideoDriverName() with SDL_GetCurrentVideoDriver() + Added SDL_GetNumAudioDrivers() and SDL_GetAudioDriver(). + Replaced SDL_AudioDriverName() with SDL_GetCurrentAudioDriver() 1.2.10: If SDL_OpenAudio() is passed zero for the desired format @@ -416,8 +420,6 @@ Version 1.0: 1.0.0: New public release -Version 0.11: - 0.11.5: A new function SDL_GetVideoSurface() has been added, and returns a pointer to the current display surface. @@ -436,8 +438,6 @@ Version 0.11: installing fatal signal handlers on operating systems that support them. -Version 0.9: - 0.9.15: SDL_CreateColorCursor() has been removed. Color cursors should be implemented as sprites, blitted by the application when the diff --git a/include/SDL_audio.h b/include/SDL_audio.h index 68ec4759d..c079aa3b3 100644 --- a/include/SDL_audio.h +++ b/include/SDL_audio.h @@ -95,6 +95,12 @@ typedef struct SDL_AudioCVT { /* Function prototypes */ +/* These functions return the list of built in video drivers, in the + * order that they are normally initialized by default. + */ +extern DECLSPEC int SDLCALL SDL_GetNumAudioDrivers(void); +extern DECLSPEC const char * SDLCALL SDL_GetAudioDriver(int index); + /* These functions are used internally, and should not be used unless you * have a specific need to specify the audio driver you want to use. * You should normally use SDL_Init() or SDL_InitSubSystem(). @@ -102,11 +108,10 @@ typedef struct SDL_AudioCVT { extern DECLSPEC int SDLCALL SDL_AudioInit(const char *driver_name); extern DECLSPEC void SDLCALL SDL_AudioQuit(void); -/* This function fills the given character buffer with the name of the - * current audio driver, and returns a pointer to it if the audio driver has - * been initialized. It returns NULL if no driver has been initialized. +/* This function returns the name of the current audio driver, or NULL + * if no driver has been initialized. */ -extern DECLSPEC char * SDLCALL SDL_AudioDriverName(char *namebuf, int maxlen); +extern DECLSPEC const char * SDLCALL SDL_GetCurrentAudioDriver(void); /* * This function opens the audio device with the desired parameters, and diff --git a/include/SDL_video.h b/include/SDL_video.h index a39e30abd..6390867da 100644 --- a/include/SDL_video.h +++ b/include/SDL_video.h @@ -224,6 +224,12 @@ typedef enum { /* Function prototypes */ +/* These functions return the list of built in video drivers, in the + * order that they are normally initialized by default. + */ +extern DECLSPEC int SDLCALL SDL_GetNumVideoDrivers(void); +extern DECLSPEC const char * SDLCALL SDL_GetVideoDriver(int index); + /* These functions are used internally, and should not be used unless you * have a specific need to specify the video driver you want to use. * You should normally use SDL_Init() or SDL_InitSubSystem(). @@ -240,11 +246,10 @@ typedef enum { extern DECLSPEC int SDLCALL SDL_VideoInit(const char *driver_name, Uint32 flags); extern DECLSPEC void SDLCALL SDL_VideoQuit(void); -/* This function fills the given character buffer with the name of the - * video driver, and returns a pointer to it if the video driver has - * been initialized. It returns NULL if no driver has been initialized. +/* This function returns the name of the current video driver, or NULL + * if no driver has been initialized. */ -extern DECLSPEC char * SDLCALL SDL_VideoDriverName(char *namebuf, int maxlen); +extern DECLSPEC const char * SDLCALL SDL_GetCurrentVideoDriver(void); /* * This function returns a pointer to the current display surface. diff --git a/src/audio/SDL_audio.c b/src/audio/SDL_audio.c index 84a5e0742..111433582 100644 --- a/src/audio/SDL_audio.c +++ b/src/audio/SDL_audio.c @@ -330,6 +330,19 @@ static Uint16 SDL_ParseAudioFormat(const char *string) return format; } +int SDL_GetNumAudioDrivers(void) +{ + return(SDL_arraysize(bootstrap)-1); +} + +const char *SDL_GetAudioDriver(int index) +{ + if ( index >= 0 && index < SDL_GetNumAudioDrivers() ) { + return(bootstrap[index]->name); + } + return(NULL); +} + int SDL_AudioInit(const char *driver_name) { SDL_AudioDevice *audio; @@ -419,11 +432,10 @@ int SDL_AudioInit(const char *driver_name) return(0); } -char *SDL_AudioDriverName(char *namebuf, int maxlen) +const char *SDL_GetCurrentAudioDriver() { - if ( current_audio != NULL ) { - SDL_strlcpy(namebuf, current_audio->name, maxlen); - return(namebuf); + if ( current_audio ) { + return current_audio->name; } return(NULL); } diff --git a/src/video/SDL_video.c b/src/video/SDL_video.c index eae847a09..418cedd6f 100644 --- a/src/video/SDL_video.c +++ b/src/video/SDL_video.c @@ -137,6 +137,18 @@ void SDL_VideoQuit(void); static SDL_GrabMode SDL_WM_GrabInputOff(void); +int SDL_GetNumVideoDrivers(void) +{ + return(SDL_arraysize(bootstrap)-1); +} + +const char *SDL_GetVideoDriver(int index) +{ + if ( index >= 0 && index < SDL_GetNumVideoDrivers() ) { + return(bootstrap[index]->name); + } + return(NULL); +} /* * Initialize the video and event subsystems -- determine native pixel format @@ -278,11 +290,10 @@ int SDL_VideoInit (const char *driver_name, Uint32 flags) return(0); } -char *SDL_VideoDriverName(char *namebuf, int maxlen) +const char *SDL_GetCurrentVideoDriver() { - if ( current_video != NULL ) { - SDL_strlcpy(namebuf, current_video->name, maxlen); - return(namebuf); + if ( current_video ) { + return current_video->name; } return(NULL); } diff --git a/test/loopwave.c b/test/loopwave.c index e685aee65..cd255065b 100644 --- a/test/loopwave.c +++ b/test/loopwave.c @@ -62,7 +62,22 @@ void poked(int sig) int main(int argc, char *argv[]) { - char name[32]; + int i, n; + + /* Print available audio drivers */ + n = SDL_GetNumAudioDrivers(); + if ( n == 0 ) { + printf("No built-in audio drivers\n"); + } else { + printf("Built-in audio drivers:"); + for ( i = 0; i < n; ++i ) { + if ( i > 0 ) { + printf(","); + } + printf(" %s", SDL_GetAudioDriver(i)); + } + printf("\n"); + } /* Load the SDL library */ if ( SDL_Init(SDL_INIT_AUDIO) < 0 ) { @@ -102,7 +117,7 @@ int main(int argc, char *argv[]) SDL_PauseAudio(0); /* Let the audio run */ - printf("Using audio driver: %s\n", SDL_AudioDriverName(name, 32)); + printf("Using audio driver: %s\n", SDL_GetCurrentAudioDriver()); while ( ! done && (SDL_GetAudioStatus() == SDL_AUDIO_PLAYING) ) SDL_Delay(1000); diff --git a/test/testvidinfo.c b/test/testvidinfo.c index bbafeb8b3..8717bbab7 100644 --- a/test/testvidinfo.c +++ b/test/testvidinfo.c @@ -386,16 +386,32 @@ void RunVideoTests() int main(int argc, char *argv[]) { const SDL_VideoInfo *info; - int i; + int i, n; SDL_Rect **modes; - char driver[128]; + const char *driver; + + /* Print available video drivers */ + n = SDL_GetNumVideoDrivers(); + if ( n == 0 ) { + printf("No built-in video drivers\n"); + } else { + printf("Built-in video drivers:"); + for ( i = 0; i < n; ++i ) { + if ( i > 0 ) { + printf(","); + } + printf(" %s", SDL_GetVideoDriver(i)); + } + printf("\n"); + } if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) { fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError()); exit(1); } - if ( SDL_VideoDriverName(driver, sizeof(driver)) ) { + driver = SDL_GetCurrentVideoDriver(); + if ( driver ) { printf("Video driver: %s\n", driver); } info = SDL_GetVideoInfo();