Mercurial > sdl-ios-xcode
comparison src/audio/SDL_audiodev.c @ 3810:2c5387c0a642 SDL-ryan-multiple-audio-device
Multiple audio device code is now working for dsp and dma targets.
author | Ryan C. Gordon <icculus@icculus.org> |
---|---|
date | Thu, 05 Oct 2006 04:47:13 +0000 |
parents | 7852b5b78af5 |
children |
comparison
equal
deleted
inserted
replaced
3809:7852b5b78af5 | 3810:2c5387c0a642 |
---|---|
44 #endif | 44 #endif |
45 #ifndef _PATH_DEV_AUDIO | 45 #ifndef _PATH_DEV_AUDIO |
46 #define _PATH_DEV_AUDIO "/dev/audio" | 46 #define _PATH_DEV_AUDIO "/dev/audio" |
47 #endif | 47 #endif |
48 | 48 |
49 static inline void | |
50 test_device(const char *fname, int flags, int (*test)(int fd), | |
51 char ***devices, int *devCount) | |
52 { | |
53 struct stat sb; | |
54 if ( (stat(fname, &sb) == 0) && (S_ISCHR(sb.st_mode)) ) { | |
55 int audio_fd = open(fname, flags, 0); | |
56 if ( (audio_fd >= 0) && (test(audio_fd)) ) { | |
57 void *p = SDL_realloc(*devices, ((*devCount)+1) * sizeof (char *)); | |
58 if (p != NULL) { | |
59 size_t len = strlen(fname) + 1; | |
60 char *str = (char *) SDL_malloc(len); | |
61 *devices = (char **) p; | |
62 if (str != NULL) { | |
63 SDL_strlcpy(str, fname, len); | |
64 (*devices)[(*devCount)++] = str; | |
65 } | |
66 } | |
67 close(audio_fd); | |
68 } | |
69 } | |
70 } | |
49 | 71 |
50 int | 72 void |
51 SDL_OpenAudioPath(char *path, int maxlen, int flags, int classic) | 73 SDL_FreeUnixAudioDevices(char ***devices, int *devCount) |
74 { | |
75 int i = *devCount; | |
76 if ((i > 0) && (*devices != NULL)) { | |
77 while (i--) { | |
78 SDL_free( (*devices)[*devCount] ); | |
79 } | |
80 } | |
81 | |
82 if (*devices != NULL) { | |
83 SDL_free(*devices); | |
84 } | |
85 | |
86 *devices = NULL; | |
87 *devCount = 0; | |
88 } | |
89 | |
90 static int | |
91 test_stub(int fd) | |
92 { | |
93 return 1; | |
94 } | |
95 | |
96 void | |
97 SDL_EnumUnixAudioDevices(int flags, int classic, int (*test)(int fd), | |
98 char ***devices, int *devCount) | |
52 { | 99 { |
53 const char *audiodev; | 100 const char *audiodev; |
54 int audio_fd; | |
55 char audiopath[1024]; | 101 char audiopath[1024]; |
102 | |
103 if (test == NULL) | |
104 test = test_stub; | |
56 | 105 |
57 /* Figure out what our audio device is */ | 106 /* Figure out what our audio device is */ |
58 if (((audiodev = SDL_getenv("SDL_PATH_DSP")) == NULL) && | 107 if (((audiodev = SDL_getenv("SDL_PATH_DSP")) == NULL) && |
59 ((audiodev = SDL_getenv("AUDIODEV")) == NULL)) { | 108 ((audiodev = SDL_getenv("AUDIODEV")) == NULL)) { |
60 if (classic) { | 109 if (classic) { |
70 } else { | 119 } else { |
71 audiodev = _PATH_DEV_DSP; | 120 audiodev = _PATH_DEV_DSP; |
72 } | 121 } |
73 } | 122 } |
74 } | 123 } |
75 audio_fd = open(audiodev, flags, 0); | 124 test_device(audiodev, flags, test, devices, devCount); |
76 | 125 |
77 /* If the first open fails, look for other devices */ | 126 if (SDL_strlen(audiodev) < (sizeof(audiopath) - 3)) { |
78 if ((audio_fd < 0) && (SDL_strlen(audiodev) < (sizeof(audiopath) - 3))) { | 127 int instance = 0; |
79 int exists, instance; | 128 while (instance++ <= 64) { |
80 struct stat sb; | |
81 | |
82 instance = 1; | |
83 do { /* Don't use errno ENOENT - it may not be thread-safe */ | |
84 SDL_snprintf(audiopath, SDL_arraysize(audiopath), | 129 SDL_snprintf(audiopath, SDL_arraysize(audiopath), |
85 "%s%d", audiodev, instance++); | 130 "%s%d", audiodev, instance); |
86 exists = 0; | 131 test_device(audiopath, flags, test, devices, devCount); |
87 if (stat(audiopath, &sb) == 0) { | |
88 exists = 1; | |
89 audio_fd = open(audiopath, flags, 0); | |
90 } | |
91 } | 132 } |
92 while (exists && (audio_fd < 0)); | |
93 audiodev = audiopath; | |
94 } | 133 } |
95 if (path != NULL) { | |
96 SDL_strlcpy(path, audiodev, maxlen); | |
97 path[maxlen - 1] = '\0'; | |
98 } | |
99 return (audio_fd); | |
100 } | 134 } |
101 | 135 |
102 #endif /* Audio driver selection */ | 136 #endif /* Audio driver selection */ |
103 /* vi: set ts=4 sw=4 expandtab: */ | 137 /* vi: set ts=4 sw=4 expandtab: */ |