SDL_audiodev.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2024 Sam Lantinga <slouken@libsdl.org>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #include "SDL_internal.h"
  19. // Get the name of the audio device we use for output
  20. #if defined(SDL_AUDIO_DRIVER_NETBSD) || defined(SDL_AUDIO_DRIVER_OSS)
  21. #include <fcntl.h>
  22. #include <sys/types.h>
  23. #include <sys/stat.h>
  24. #include <unistd.h> // For close()
  25. #include "SDL_audiodev_c.h"
  26. #ifndef SDL_PATH_DEV_DSP
  27. #if defined(SDL_PLATFORM_NETBSD) || defined(SDL_PLATFORM_OPENBSD)
  28. #define SDL_PATH_DEV_DSP "/dev/audio"
  29. #else
  30. #define SDL_PATH_DEV_DSP "/dev/dsp"
  31. #endif
  32. #endif
  33. #ifndef SDL_PATH_DEV_DSP24
  34. #define SDL_PATH_DEV_DSP24 "/dev/sound/dsp"
  35. #endif
  36. #ifndef SDL_PATH_DEV_AUDIO
  37. #define SDL_PATH_DEV_AUDIO "/dev/audio"
  38. #endif
  39. static void test_device(const bool recording, const char *fname, int flags, bool (*test)(int fd))
  40. {
  41. struct stat sb;
  42. const int audio_fd = open(fname, flags | O_CLOEXEC, 0);
  43. if (audio_fd >= 0) {
  44. if ((fstat(audio_fd, &sb) == 0) && (S_ISCHR(sb.st_mode))) {
  45. const bool okay = test(audio_fd);
  46. close(audio_fd);
  47. if (okay) {
  48. static size_t dummyhandle = 0;
  49. dummyhandle++;
  50. SDL_assert(dummyhandle != 0);
  51. /* Note that spec is NULL; while we are opening the device
  52. * endpoint here, the endpoint does not provide any mix format
  53. * information, making this information inaccessible at
  54. * enumeration time
  55. */
  56. SDL_AddAudioDevice(recording, fname, NULL, (void *)(uintptr_t)dummyhandle);
  57. }
  58. } else {
  59. close(audio_fd);
  60. }
  61. }
  62. }
  63. static bool test_stub(int fd)
  64. {
  65. return true;
  66. }
  67. static void SDL_EnumUnixAudioDevices_Internal(const bool recording, const bool classic, bool (*test)(int))
  68. {
  69. const int flags = recording ? OPEN_FLAGS_INPUT : OPEN_FLAGS_OUTPUT;
  70. const char *audiodev;
  71. char audiopath[1024];
  72. if (!test) {
  73. test = test_stub;
  74. }
  75. // Figure out what our audio device is
  76. audiodev = SDL_getenv("AUDIODEV");
  77. if (!audiodev) {
  78. if (classic) {
  79. audiodev = SDL_PATH_DEV_AUDIO;
  80. } else {
  81. struct stat sb;
  82. // Added support for /dev/sound/\* in Linux 2.4
  83. if (((stat("/dev/sound", &sb) == 0) && S_ISDIR(sb.st_mode)) && ((stat(SDL_PATH_DEV_DSP24, &sb) == 0) && S_ISCHR(sb.st_mode))) {
  84. audiodev = SDL_PATH_DEV_DSP24;
  85. } else {
  86. audiodev = SDL_PATH_DEV_DSP;
  87. }
  88. }
  89. }
  90. test_device(recording, audiodev, flags, test);
  91. if (SDL_strlen(audiodev) < (sizeof(audiopath) - 3)) {
  92. int instance = 0;
  93. while (instance <= 64) {
  94. (void)SDL_snprintf(audiopath, SDL_arraysize(audiopath),
  95. "%s%d", audiodev, instance);
  96. instance++;
  97. test_device(recording, audiopath, flags, test);
  98. }
  99. }
  100. }
  101. void SDL_EnumUnixAudioDevices(const bool classic, bool (*test)(int))
  102. {
  103. SDL_EnumUnixAudioDevices_Internal(true, classic, test);
  104. SDL_EnumUnixAudioDevices_Internal(false, classic, test);
  105. }
  106. #endif // Audio device selection