src/audio/SDL_audiodev.c
author Philipp Wiesemann <philipp.wiesemann@arcor.de>
Sun, 22 Feb 2015 23:21:32 +0100
changeset 9373 679eb3986e37
parent 8149 681eb46b8ac4
child 9394 bb28e5281770
permissions -rw-r--r--
Emscripten: Fixed out of range joystick device index after joystick disconnect.

After disconnecting a joystick the remaining kept their original device index.
This was not correct because the device index must be a number between 0 and
SDL_NumJoysticks(). It was fixed with ideas from SDL's joystick implementation
for Android. Some range checks were removed as the caller already checks them.
     1 /*
     2   Simple DirectMedia Layer
     3   Copyright (C) 1997-2014 Sam Lantinga <slouken@libsdl.org>
     4 
     5   This software is provided 'as-is', without any express or implied
     6   warranty.  In no event will the authors be held liable for any damages
     7   arising from the use of this software.
     8 
     9   Permission is granted to anyone to use this software for any purpose,
    10   including commercial applications, and to alter it and redistribute it
    11   freely, subject to the following restrictions:
    12 
    13   1. The origin of this software must not be misrepresented; you must not
    14      claim that you wrote the original software. If you use this software
    15      in a product, an acknowledgment in the product documentation would be
    16      appreciated but is not required.
    17   2. Altered source versions must be plainly marked as such, and must not be
    18      misrepresented as being the original software.
    19   3. This notice may not be removed or altered from any source distribution.
    20 */
    21 #include "../SDL_internal.h"
    22 
    23 /* Get the name of the audio device we use for output */
    24 
    25 #if SDL_AUDIO_DRIVER_BSD || SDL_AUDIO_DRIVER_OSS || SDL_AUDIO_DRIVER_SUNAUDIO
    26 
    27 #include <fcntl.h>
    28 #include <sys/types.h>
    29 #include <sys/stat.h>
    30 #include <unistd.h> /* For close() */
    31 
    32 #include "SDL_stdinc.h"
    33 #include "SDL_audiodev_c.h"
    34 
    35 #ifndef _PATH_DEV_DSP
    36 #if defined(__NETBSD__) || defined(__OPENBSD__)
    37 #define _PATH_DEV_DSP  "/dev/audio"
    38 #else
    39 #define _PATH_DEV_DSP  "/dev/dsp"
    40 #endif
    41 #endif
    42 #ifndef _PATH_DEV_DSP24
    43 #define _PATH_DEV_DSP24 "/dev/sound/dsp"
    44 #endif
    45 #ifndef _PATH_DEV_AUDIO
    46 #define _PATH_DEV_AUDIO "/dev/audio"
    47 #endif
    48 
    49 static SDL_INLINE void
    50 test_device(const char *fname, int flags, int (*test) (int fd),
    51             SDL_AddAudioDevice addfn)
    52 {
    53     struct stat sb;
    54     if ((stat(fname, &sb) == 0) && (S_ISCHR(sb.st_mode))) {
    55         const int audio_fd = open(fname, flags, 0);
    56         if (audio_fd >= 0) {
    57             if (test(audio_fd)) {
    58                 addfn(fname);
    59             }
    60             close(audio_fd);
    61         }
    62     }
    63 }
    64 
    65 static int
    66 test_stub(int fd)
    67 {
    68     return 1;
    69 }
    70 
    71 void
    72 SDL_EnumUnixAudioDevices(int iscapture, int classic, int (*test)(int fd),
    73                          SDL_AddAudioDevice addfn)
    74 {
    75     const int flags = ((iscapture) ? OPEN_FLAGS_INPUT : OPEN_FLAGS_OUTPUT);
    76     const char *audiodev;
    77     char audiopath[1024];
    78 
    79     if (test == NULL)
    80         test = test_stub;
    81 
    82     /* Figure out what our audio device is */
    83     if (((audiodev = SDL_getenv("SDL_PATH_DSP")) == NULL) &&
    84         ((audiodev = SDL_getenv("AUDIODEV")) == NULL)) {
    85         if (classic) {
    86             audiodev = _PATH_DEV_AUDIO;
    87         } else {
    88             struct stat sb;
    89 
    90             /* Added support for /dev/sound/\* in Linux 2.4 */
    91             if (((stat("/dev/sound", &sb) == 0) && S_ISDIR(sb.st_mode))
    92                 && ((stat(_PATH_DEV_DSP24, &sb) == 0)
    93                     && S_ISCHR(sb.st_mode))) {
    94                 audiodev = _PATH_DEV_DSP24;
    95             } else {
    96                 audiodev = _PATH_DEV_DSP;
    97             }
    98         }
    99     }
   100     test_device(audiodev, flags, test, addfn);
   101 
   102     if (SDL_strlen(audiodev) < (sizeof(audiopath) - 3)) {
   103         int instance = 0;
   104         while (instance++ <= 64) {
   105             SDL_snprintf(audiopath, SDL_arraysize(audiopath),
   106                          "%s%d", audiodev, instance);
   107             test_device(audiopath, flags, test, addfn);
   108         }
   109     }
   110 }
   111 
   112 #endif /* Audio driver selection */
   113 /* vi: set ts=4 sw=4 expandtab: */