src/audio/SDL_audiodev.c
branchSDL-1.3
changeset 1668 4da1ee79c9af
parent 1662 782fd950bd46
equal deleted inserted replaced
1667:1fddae038bc8 1668:4da1ee79c9af
    46 #define _PATH_DEV_AUDIO	"/dev/audio"
    46 #define _PATH_DEV_AUDIO	"/dev/audio"
    47 #endif
    47 #endif
    48 
    48 
    49 
    49 
    50 int
    50 int
    51 SDL_OpenAudioPath (char *path, int maxlen, int flags, int classic)
    51 SDL_OpenAudioPath(char *path, int maxlen, int flags, int classic)
    52 {
    52 {
    53     const char *audiodev;
    53     const char *audiodev;
    54     int audio_fd;
    54     int audio_fd;
    55     char audiopath[1024];
    55     char audiopath[1024];
    56 
    56 
    57     /* Figure out what our audio device is */
    57     /* Figure out what our audio device is */
    58     if (((audiodev = SDL_getenv ("SDL_PATH_DSP")) == NULL) &&
    58     if (((audiodev = SDL_getenv("SDL_PATH_DSP")) == NULL) &&
    59         ((audiodev = SDL_getenv ("AUDIODEV")) == NULL)) {
    59         ((audiodev = SDL_getenv("AUDIODEV")) == NULL)) {
    60         if (classic) {
    60         if (classic) {
    61             audiodev = _PATH_DEV_AUDIO;
    61             audiodev = _PATH_DEV_AUDIO;
    62         } else {
    62         } else {
    63             struct stat sb;
    63             struct stat sb;
    64 
    64 
    65             /* Added support for /dev/sound/\* in Linux 2.4 */
    65             /* Added support for /dev/sound/\* in Linux 2.4 */
    66             if (((stat ("/dev/sound", &sb) == 0) && S_ISDIR (sb.st_mode))
    66             if (((stat("/dev/sound", &sb) == 0) && S_ISDIR(sb.st_mode))
    67                 && ((stat (_PATH_DEV_DSP24, &sb) == 0)
    67                 && ((stat(_PATH_DEV_DSP24, &sb) == 0)
    68                     && S_ISCHR (sb.st_mode))) {
    68                     && S_ISCHR(sb.st_mode))) {
    69                 audiodev = _PATH_DEV_DSP24;
    69                 audiodev = _PATH_DEV_DSP24;
    70             } else {
    70             } else {
    71                 audiodev = _PATH_DEV_DSP;
    71                 audiodev = _PATH_DEV_DSP;
    72             }
    72             }
    73         }
    73         }
    74     }
    74     }
    75     audio_fd = open (audiodev, flags, 0);
    75     audio_fd = open(audiodev, flags, 0);
    76 
    76 
    77     /* If the first open fails, look for other devices */
    77     /* If the first open fails, look for other devices */
    78     if ((audio_fd < 0) && (SDL_strlen (audiodev) < (sizeof (audiopath) - 3))) {
    78     if ((audio_fd < 0) && (SDL_strlen(audiodev) < (sizeof(audiopath) - 3))) {
    79         int exists, instance;
    79         int exists, instance;
    80         struct stat sb;
    80         struct stat sb;
    81 
    81 
    82         instance = 1;
    82         instance = 1;
    83         do {                    /* Don't use errno ENOENT - it may not be thread-safe */
    83         do {                    /* Don't use errno ENOENT - it may not be thread-safe */
    84             SDL_snprintf (audiopath, SDL_arraysize (audiopath),
    84             SDL_snprintf(audiopath, SDL_arraysize(audiopath),
    85                           "%s%d", audiodev, instance++);
    85                          "%s%d", audiodev, instance++);
    86             exists = 0;
    86             exists = 0;
    87             if (stat (audiopath, &sb) == 0) {
    87             if (stat(audiopath, &sb) == 0) {
    88                 exists = 1;
    88                 exists = 1;
    89                 audio_fd = open (audiopath, flags, 0);
    89                 audio_fd = open(audiopath, flags, 0);
    90             }
    90             }
    91         }
    91         }
    92         while (exists && (audio_fd < 0));
    92         while (exists && (audio_fd < 0));
    93         audiodev = audiopath;
    93         audiodev = audiopath;
    94     }
    94     }
    95     if (path != NULL) {
    95     if (path != NULL) {
    96         SDL_strlcpy (path, audiodev, maxlen);
    96         SDL_strlcpy(path, audiodev, maxlen);
    97         path[maxlen - 1] = '\0';
    97         path[maxlen - 1] = '\0';
    98     }
    98     }
    99     return (audio_fd);
    99     return (audio_fd);
   100 }
   100 }
   101 
   101 
   124     {'b', '3', '1'}, {'b', '3', '2'}, {'b', '3', '3'}, {'b', '3', '4'},
   124     {'b', '3', '1'}, {'b', '3', '2'}, {'b', '3', '3'}, {'b', '3', '4'},
   125     {'\0', '\0', '\0'}
   125     {'\0', '\0', '\0'}
   126 };
   126 };
   127 
   127 
   128 static int
   128 static int
   129 OpenUserDefinedDevice (char *path, int maxlen, int flags)
   129 OpenUserDefinedDevice(char *path, int maxlen, int flags)
   130 {
   130 {
   131     const char *audiodev;
   131     const char *audiodev;
   132     int audio_fd;
   132     int audio_fd;
   133 
   133 
   134     /* Figure out what our audio device is */
   134     /* Figure out what our audio device is */
   135     if ((audiodev = SDL_getenv ("SDL_PATH_DSP")) == NULL) {
   135     if ((audiodev = SDL_getenv("SDL_PATH_DSP")) == NULL) {
   136         audiodev = SDL_getenv ("AUDIODEV");
   136         audiodev = SDL_getenv("AUDIODEV");
   137     }
   137     }
   138     if (audiodev == NULL) {
   138     if (audiodev == NULL) {
   139         return -1;
   139         return -1;
   140     }
   140     }
   141     audio_fd = open (audiodev, flags, 0);
   141     audio_fd = open(audiodev, flags, 0);
   142     if (path != NULL) {
   142     if (path != NULL) {
   143         SDL_strlcpy (path, audiodev, maxlen);
   143         SDL_strlcpy(path, audiodev, maxlen);
   144         path[maxlen - 1] = '\0';
   144         path[maxlen - 1] = '\0';
   145     }
   145     }
   146     return audio_fd;
   146     return audio_fd;
   147 }
   147 }
   148 
   148 
   149 int
   149 int
   150 SDL_OpenAudioPath (char *path, int maxlen, int flags, int classic)
   150 SDL_OpenAudioPath(char *path, int maxlen, int flags, int classic)
   151 {
   151 {
   152     struct stat sb;
   152     struct stat sb;
   153     int audio_fd;
   153     int audio_fd;
   154     char audiopath[1024];
   154     char audiopath[1024];
   155     int cycle;
   155     int cycle;
   156 
   156 
   157     audio_fd = OpenUserDefinedDevice (path, maxlen, flags);
   157     audio_fd = OpenUserDefinedDevice(path, maxlen, flags);
   158     if (audio_fd != -1) {
   158     if (audio_fd != -1) {
   159         return audio_fd;
   159         return audio_fd;
   160     }
   160     }
   161 
   161 
   162     cycle = 0;
   162     cycle = 0;
   163     while (devsettings[cycle][0] != '\0') {
   163     while (devsettings[cycle][0] != '\0') {
   164         SDL_snprintf (audiopath, SDL_arraysize (audiopath),
   164         SDL_snprintf(audiopath, SDL_arraysize(audiopath),
   165                       _PATH_DEV_DSP,
   165                      _PATH_DEV_DSP,
   166                       devsettings[cycle][0],
   166                      devsettings[cycle][0],
   167                       devsettings[cycle][1], devsettings[cycle][2]);
   167                      devsettings[cycle][1], devsettings[cycle][2]);
   168 
   168 
   169         if (stat (audiopath, &sb) == 0) {
   169         if (stat(audiopath, &sb) == 0) {
   170             audio_fd = open (audiopath, flags, 0);
   170             audio_fd = open(audiopath, flags, 0);
   171             if (audio_fd > 0) {
   171             if (audio_fd > 0) {
   172                 if (path != NULL) {
   172                 if (path != NULL) {
   173                     SDL_strlcpy (path, audiopath, maxlen);
   173                     SDL_strlcpy(path, audiopath, maxlen);
   174                 }
   174                 }
   175                 return audio_fd;
   175                 return audio_fd;
   176             }
   176             }
   177         }
   177         }
   178     }
   178     }