src/audio/alsa/SDL_alsa_audio.c
author Ryan C. Gordon <icculus@icculus.org>
Sun, 28 Aug 2016 08:50:26 -0700
changeset 10284 bd207addc5ec
parent 10282 9bbe05f2612a
child 10285 9859cda24699
permissions -rw-r--r--
alsa: Implemented hotplug support, cleaned up device names.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
     1
/*
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5315
diff changeset
     2
  Simple DirectMedia Layer
9998
f67cf37e9cd4 Updated copyright to 2016
Sam Lantinga <slouken@libsdl.org>
parents: 9619
diff changeset
     3
  Copyright (C) 1997-2016 Sam Lantinga <slouken@libsdl.org>
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
     4
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5315
diff changeset
     5
  This software is provided 'as-is', without any express or implied
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5315
diff changeset
     6
  warranty.  In no event will the authors be held liable for any damages
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5315
diff changeset
     7
  arising from the use of this software.
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
     8
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5315
diff changeset
     9
  Permission is granted to anyone to use this software for any purpose,
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5315
diff changeset
    10
  including commercial applications, and to alter it and redistribute it
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5315
diff changeset
    11
  freely, subject to the following restrictions:
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    12
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5315
diff changeset
    13
  1. The origin of this software must not be misrepresented; you must not
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5315
diff changeset
    14
     claim that you wrote the original software. If you use this software
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5315
diff changeset
    15
     in a product, an acknowledgment in the product documentation would be
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5315
diff changeset
    16
     appreciated but is not required.
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5315
diff changeset
    17
  2. Altered source versions must be plainly marked as such, and must not be
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5315
diff changeset
    18
     misrepresented as being the original software.
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5315
diff changeset
    19
  3. This notice may not be removed or altered from any source distribution.
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    20
*/
8093
b43765095a6f Make internal SDL sources include SDL_internal.h instead of SDL_config.h
Ryan C. Gordon <icculus@icculus.org>
parents: 7860
diff changeset
    21
#include "../../SDL_internal.h"
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    22
6044
35448a5ea044 Lots of fixes importing SDL source wholesale into a new iOS project
Sam Lantinga <slouken@libsdl.org>
parents: 5622
diff changeset
    23
#if SDL_AUDIO_DRIVER_ALSA
35448a5ea044 Lots of fixes importing SDL source wholesale into a new iOS project
Sam Lantinga <slouken@libsdl.org>
parents: 5622
diff changeset
    24
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    25
/* Allow access to a raw mixing buffer */
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    26
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    27
#include <sys/types.h>
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
    28
#include <signal.h>             /* For kill() */
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
    29
#include <errno.h>
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
    30
#include <string.h>
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    31
10284
bd207addc5ec alsa: Implemented hotplug support, cleaned up device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10282
diff changeset
    32
#include "SDL_assert.h"
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents: 1358
diff changeset
    33
#include "SDL_timer.h"
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    34
#include "SDL_audio.h"
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents: 1358
diff changeset
    35
#include "../SDL_audiomem.h"
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents: 1358
diff changeset
    36
#include "../SDL_audio_c.h"
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    37
#include "SDL_alsa_audio.h"
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    38
6046
3b33b82cdbac Fixed some preprocessor mistakes introduced in iOS project cleanup.
Ryan C. Gordon <icculus@icculus.org>
parents: 6044
diff changeset
    39
#ifdef SDL_AUDIO_DRIVER_ALSA_DYNAMIC
3362
4e83cdb58134 Merged r4990:4991 from branches/SDL-1.2: ALSA 1.0 API and dlvsym() removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 3068
diff changeset
    40
#include "SDL_loadso.h"
4e83cdb58134 Merged r4990:4991 from branches/SDL-1.2: ALSA 1.0 API and dlvsym() removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 3068
diff changeset
    41
#endif
865
92615154bb68 Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
    42
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
    43
static int (*ALSA_snd_pcm_open)
2060
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2049
diff changeset
    44
  (snd_pcm_t **, const char *, snd_pcm_stream_t, int);
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2049
diff changeset
    45
static int (*ALSA_snd_pcm_close) (snd_pcm_t * pcm);
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
    46
static snd_pcm_sframes_t(*ALSA_snd_pcm_writei)
2060
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2049
diff changeset
    47
  (snd_pcm_t *, const void *, snd_pcm_uframes_t);
3627
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
    48
static int (*ALSA_snd_pcm_recover) (snd_pcm_t *, int, int);
2060
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2049
diff changeset
    49
static int (*ALSA_snd_pcm_prepare) (snd_pcm_t *);
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2049
diff changeset
    50
static int (*ALSA_snd_pcm_drain) (snd_pcm_t *);
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2049
diff changeset
    51
static const char *(*ALSA_snd_strerror) (int);
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2049
diff changeset
    52
static size_t(*ALSA_snd_pcm_hw_params_sizeof) (void);
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2049
diff changeset
    53
static size_t(*ALSA_snd_pcm_sw_params_sizeof) (void);
3627
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
    54
static void (*ALSA_snd_pcm_hw_params_copy)
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
    55
  (snd_pcm_hw_params_t *, const snd_pcm_hw_params_t *);
2060
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2049
diff changeset
    56
static int (*ALSA_snd_pcm_hw_params_any) (snd_pcm_t *, snd_pcm_hw_params_t *);
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
    57
static int (*ALSA_snd_pcm_hw_params_set_access)
2060
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2049
diff changeset
    58
  (snd_pcm_t *, snd_pcm_hw_params_t *, snd_pcm_access_t);
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
    59
static int (*ALSA_snd_pcm_hw_params_set_format)
2060
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2049
diff changeset
    60
  (snd_pcm_t *, snd_pcm_hw_params_t *, snd_pcm_format_t);
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
    61
static int (*ALSA_snd_pcm_hw_params_set_channels)
2060
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2049
diff changeset
    62
  (snd_pcm_t *, snd_pcm_hw_params_t *, unsigned int);
3627
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
    63
static int (*ALSA_snd_pcm_hw_params_get_channels)
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
    64
  (const snd_pcm_hw_params_t *, unsigned int *);
3362
4e83cdb58134 Merged r4990:4991 from branches/SDL-1.2: ALSA 1.0 API and dlvsym() removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 3068
diff changeset
    65
static int (*ALSA_snd_pcm_hw_params_set_rate_near)
4e83cdb58134 Merged r4990:4991 from branches/SDL-1.2: ALSA 1.0 API and dlvsym() removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 3068
diff changeset
    66
  (snd_pcm_t *, snd_pcm_hw_params_t *, unsigned int *, int *);
4e83cdb58134 Merged r4990:4991 from branches/SDL-1.2: ALSA 1.0 API and dlvsym() removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 3068
diff changeset
    67
static int (*ALSA_snd_pcm_hw_params_set_period_size_near)
4e83cdb58134 Merged r4990:4991 from branches/SDL-1.2: ALSA 1.0 API and dlvsym() removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 3068
diff changeset
    68
  (snd_pcm_t *, snd_pcm_hw_params_t *, snd_pcm_uframes_t *, int *);
3627
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
    69
static int (*ALSA_snd_pcm_hw_params_get_period_size)
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
    70
  (const snd_pcm_hw_params_t *, snd_pcm_uframes_t *, int *);
3362
4e83cdb58134 Merged r4990:4991 from branches/SDL-1.2: ALSA 1.0 API and dlvsym() removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 3068
diff changeset
    71
static int (*ALSA_snd_pcm_hw_params_set_periods_near)
4e83cdb58134 Merged r4990:4991 from branches/SDL-1.2: ALSA 1.0 API and dlvsym() removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 3068
diff changeset
    72
  (snd_pcm_t *, snd_pcm_hw_params_t *, unsigned int *, int *);
3627
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
    73
static int (*ALSA_snd_pcm_hw_params_get_periods)
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
    74
  (const snd_pcm_hw_params_t *, unsigned int *, int *);
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
    75
static int (*ALSA_snd_pcm_hw_params_set_buffer_size_near)
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
    76
  (snd_pcm_t *pcm, snd_pcm_hw_params_t *, snd_pcm_uframes_t *);
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
    77
static int (*ALSA_snd_pcm_hw_params_get_buffer_size)
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
    78
  (const snd_pcm_hw_params_t *, snd_pcm_uframes_t *);
2060
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2049
diff changeset
    79
static int (*ALSA_snd_pcm_hw_params) (snd_pcm_t *, snd_pcm_hw_params_t *);
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2049
diff changeset
    80
static int (*ALSA_snd_pcm_sw_params_current) (snd_pcm_t *,
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2049
diff changeset
    81
                                              snd_pcm_sw_params_t *);
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
    82
static int (*ALSA_snd_pcm_sw_params_set_start_threshold)
2060
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2049
diff changeset
    83
  (snd_pcm_t *, snd_pcm_sw_params_t *, snd_pcm_uframes_t);
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2049
diff changeset
    84
static int (*ALSA_snd_pcm_sw_params) (snd_pcm_t *, snd_pcm_sw_params_t *);
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2049
diff changeset
    85
static int (*ALSA_snd_pcm_nonblock) (snd_pcm_t *, int);
3627
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
    86
static int (*ALSA_snd_pcm_wait)(snd_pcm_t *, int);
5622
08e941dad8e9 Ported ALSA minimum-sample-count fix from 1.2 branch to 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents: 5594
diff changeset
    87
static int (*ALSA_snd_pcm_sw_params_set_avail_min)
08e941dad8e9 Ported ALSA minimum-sample-count fix from 1.2 branch to 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents: 5594
diff changeset
    88
  (snd_pcm_t *, snd_pcm_sw_params_t *, snd_pcm_uframes_t);
10104
4cd67316b308 alsa: Implemented basic device detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
    89
static int (*ALSA_snd_device_name_hint) (int, const char *, void ***);
4cd67316b308 alsa: Implemented basic device detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
    90
static char* (*ALSA_snd_device_name_get_hint) (const void *, const char *);
4cd67316b308 alsa: Implemented basic device detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
    91
static int (*ALSA_snd_device_name_free_hint) (void **);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    92
6046
3b33b82cdbac Fixed some preprocessor mistakes introduced in iOS project cleanup.
Ryan C. Gordon <icculus@icculus.org>
parents: 6044
diff changeset
    93
#ifdef SDL_AUDIO_DRIVER_ALSA_DYNAMIC
5315
192f2d605339 Fixed bug #1085 (Jump to NULL function pointer on ALSA_OpenDevice)
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    94
#define snd_pcm_hw_params_sizeof ALSA_snd_pcm_hw_params_sizeof
192f2d605339 Fixed bug #1085 (Jump to NULL function pointer on ALSA_OpenDevice)
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    95
#define snd_pcm_sw_params_sizeof ALSA_snd_pcm_sw_params_sizeof
865
92615154bb68 Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
    96
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents: 1358
diff changeset
    97
static const char *alsa_library = SDL_AUDIO_DRIVER_ALSA_DYNAMIC;
865
92615154bb68 Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
    98
static void *alsa_handle = NULL;
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
    99
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   100
static int
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   101
load_alsa_sym(const char *fn, void **addr)
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   102
{
3362
4e83cdb58134 Merged r4990:4991 from branches/SDL-1.2: ALSA 1.0 API and dlvsym() removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 3068
diff changeset
   103
    *addr = SDL_LoadFunction(alsa_handle, fn);
4e83cdb58134 Merged r4990:4991 from branches/SDL-1.2: ALSA 1.0 API and dlvsym() removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 3068
diff changeset
   104
    if (*addr == NULL) {
4e83cdb58134 Merged r4990:4991 from branches/SDL-1.2: ALSA 1.0 API and dlvsym() removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 3068
diff changeset
   105
        /* Don't call SDL_SetError(): SDL_LoadFunction already did. */
4e83cdb58134 Merged r4990:4991 from branches/SDL-1.2: ALSA 1.0 API and dlvsym() removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 3068
diff changeset
   106
        return 0;
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   107
    }
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   108
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   109
    return 1;
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   110
}
865
92615154bb68 Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
   111
1161
05d4b93b911e Placate gcc's strict aliasing rules with an extra cast.
Ryan C. Gordon <icculus@icculus.org>
parents: 942
diff changeset
   112
/* cast funcs to char* first, to please GCC's strict aliasing rules. */
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   113
#define SDL_ALSA_SYM(x) \
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   114
    if (!load_alsa_sym(#x, (void **) (char *) &ALSA_##x)) return -1
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   115
#else
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   116
#define SDL_ALSA_SYM(x) ALSA_##x = x
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   117
#endif
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   118
2060
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2049
diff changeset
   119
static int
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2049
diff changeset
   120
load_alsa_syms(void)
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   121
{
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   122
    SDL_ALSA_SYM(snd_pcm_open);
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   123
    SDL_ALSA_SYM(snd_pcm_close);
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   124
    SDL_ALSA_SYM(snd_pcm_writei);
3627
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   125
    SDL_ALSA_SYM(snd_pcm_recover);
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   126
    SDL_ALSA_SYM(snd_pcm_prepare);
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   127
    SDL_ALSA_SYM(snd_pcm_drain);
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   128
    SDL_ALSA_SYM(snd_strerror);
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   129
    SDL_ALSA_SYM(snd_pcm_hw_params_sizeof);
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   130
    SDL_ALSA_SYM(snd_pcm_sw_params_sizeof);
3627
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   131
    SDL_ALSA_SYM(snd_pcm_hw_params_copy);
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   132
    SDL_ALSA_SYM(snd_pcm_hw_params_any);
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   133
    SDL_ALSA_SYM(snd_pcm_hw_params_set_access);
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   134
    SDL_ALSA_SYM(snd_pcm_hw_params_set_format);
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   135
    SDL_ALSA_SYM(snd_pcm_hw_params_set_channels);
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   136
    SDL_ALSA_SYM(snd_pcm_hw_params_get_channels);
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   137
    SDL_ALSA_SYM(snd_pcm_hw_params_set_rate_near);
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   138
    SDL_ALSA_SYM(snd_pcm_hw_params_set_period_size_near);
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   139
    SDL_ALSA_SYM(snd_pcm_hw_params_get_period_size);
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   140
    SDL_ALSA_SYM(snd_pcm_hw_params_set_periods_near);
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   141
    SDL_ALSA_SYM(snd_pcm_hw_params_get_periods);
3627
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   142
    SDL_ALSA_SYM(snd_pcm_hw_params_set_buffer_size_near);
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   143
    SDL_ALSA_SYM(snd_pcm_hw_params_get_buffer_size);
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   144
    SDL_ALSA_SYM(snd_pcm_hw_params);
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   145
    SDL_ALSA_SYM(snd_pcm_sw_params_current);
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   146
    SDL_ALSA_SYM(snd_pcm_sw_params_set_start_threshold);
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   147
    SDL_ALSA_SYM(snd_pcm_sw_params);
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   148
    SDL_ALSA_SYM(snd_pcm_nonblock);
3627
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   149
    SDL_ALSA_SYM(snd_pcm_wait);
5622
08e941dad8e9 Ported ALSA minimum-sample-count fix from 1.2 branch to 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents: 5594
diff changeset
   150
    SDL_ALSA_SYM(snd_pcm_sw_params_set_avail_min);
10104
4cd67316b308 alsa: Implemented basic device detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   151
    SDL_ALSA_SYM(snd_device_name_hint);
4cd67316b308 alsa: Implemented basic device detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   152
    SDL_ALSA_SYM(snd_device_name_get_hint);
4cd67316b308 alsa: Implemented basic device detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   153
    SDL_ALSA_SYM(snd_device_name_free_hint);
4cd67316b308 alsa: Implemented basic device detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   154
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   155
    return 0;
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   156
}
2060
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2049
diff changeset
   157
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   158
#undef SDL_ALSA_SYM
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   159
6046
3b33b82cdbac Fixed some preprocessor mistakes introduced in iOS project cleanup.
Ryan C. Gordon <icculus@icculus.org>
parents: 6044
diff changeset
   160
#ifdef SDL_AUDIO_DRIVER_ALSA_DYNAMIC
865
92615154bb68 Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
   161
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   162
static void
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   163
UnloadALSALibrary(void)
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   164
{
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   165
    if (alsa_handle != NULL) {
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7038
diff changeset
   166
        SDL_UnloadObject(alsa_handle);
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   167
        alsa_handle = NULL;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   168
    }
865
92615154bb68 Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
   169
}
92615154bb68 Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
   170
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   171
static int
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   172
LoadALSALibrary(void)
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   173
{
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   174
    int retval = 0;
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   175
    if (alsa_handle == NULL) {
3362
4e83cdb58134 Merged r4990:4991 from branches/SDL-1.2: ALSA 1.0 API and dlvsym() removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 3068
diff changeset
   176
        alsa_handle = SDL_LoadObject(alsa_library);
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   177
        if (alsa_handle == NULL) {
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   178
            retval = -1;
3362
4e83cdb58134 Merged r4990:4991 from branches/SDL-1.2: ALSA 1.0 API and dlvsym() removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 3068
diff changeset
   179
            /* Don't call SDL_SetError(): SDL_LoadObject already did. */
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   180
        } else {
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   181
            retval = load_alsa_syms();
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   182
            if (retval < 0) {
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   183
                UnloadALSALibrary();
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   184
            }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   185
        }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   186
    }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   187
    return retval;
865
92615154bb68 Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
   188
}
92615154bb68 Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
   189
92615154bb68 Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
   190
#else
92615154bb68 Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
   191
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   192
static void
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   193
UnloadALSALibrary(void)
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   194
{
865
92615154bb68 Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
   195
}
92615154bb68 Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
   196
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   197
static int
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   198
LoadALSALibrary(void)
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   199
{
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   200
    load_alsa_syms();
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   201
    return 0;
865
92615154bb68 Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
   202
}
92615154bb68 Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
   203
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents: 1358
diff changeset
   204
#endif /* SDL_AUDIO_DRIVER_ALSA_DYNAMIC */
865
92615154bb68 Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
   205
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   206
static const char *
10104
4cd67316b308 alsa: Implemented basic device detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   207
get_audio_device(void *handle, const int channels)
354
30935e76acb5 Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
   208
{
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   209
    const char *device;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   210
10104
4cd67316b308 alsa: Implemented basic device detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   211
    if (handle != NULL) {
4cd67316b308 alsa: Implemented basic device detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   212
        return (const char *) handle;
4cd67316b308 alsa: Implemented basic device detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   213
    }
4cd67316b308 alsa: Implemented basic device detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   214
4cd67316b308 alsa: Implemented basic device detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   215
    /* !!! FIXME: we also check "SDL_AUDIO_DEVICE_NAME" at the higher level. */
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   216
    device = SDL_getenv("AUDIODEV");    /* Is there a standard variable name? */
10104
4cd67316b308 alsa: Implemented basic device detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   217
    if (device != NULL) {
4cd67316b308 alsa: Implemented basic device detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   218
        return device;
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   219
    }
10104
4cd67316b308 alsa: Implemented basic device detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   220
4cd67316b308 alsa: Implemented basic device detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   221
    if (channels == 6) {
4cd67316b308 alsa: Implemented basic device detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   222
        return "plug:surround51";
4cd67316b308 alsa: Implemented basic device detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   223
    } else if (channels == 4) {
4cd67316b308 alsa: Implemented basic device detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   224
        return "plug:surround40";
4cd67316b308 alsa: Implemented basic device detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   225
    }
4cd67316b308 alsa: Implemented basic device detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   226
4cd67316b308 alsa: Implemented basic device detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   227
    return "default";
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   228
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   229
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   230
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   231
/* This function waits until it is possible to write a full sound buffer */
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   232
static void
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   233
ALSA_WaitDevice(_THIS)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   234
{
3627
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   235
    /* We're in blocking mode, so there's nothing to do here */
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   236
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   237
1878
d7c9d7f42881 Swizzle ALSA channels for 5.1 output to match DirectSound and CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 1553
diff changeset
   238
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   239
/* !!! FIXME: is there a channel swizzler in alsalib instead? */
1878
d7c9d7f42881 Swizzle ALSA channels for 5.1 output to match DirectSound and CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 1553
diff changeset
   240
/*
d7c9d7f42881 Swizzle ALSA channels for 5.1 output to match DirectSound and CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 1553
diff changeset
   241
 * http://bugzilla.libsdl.org/show_bug.cgi?id=110
d7c9d7f42881 Swizzle ALSA channels for 5.1 output to match DirectSound and CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 1553
diff changeset
   242
 * "For Linux ALSA, this is FL-FR-RL-RR-C-LFE
d7c9d7f42881 Swizzle ALSA channels for 5.1 output to match DirectSound and CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 1553
diff changeset
   243
 *  and for Windows DirectX [and CoreAudio], this is FL-FR-C-LFE-RL-RR"
d7c9d7f42881 Swizzle ALSA channels for 5.1 output to match DirectSound and CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 1553
diff changeset
   244
 */
d7c9d7f42881 Swizzle ALSA channels for 5.1 output to match DirectSound and CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 1553
diff changeset
   245
#define SWIZ6(T) \
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   246
    T *ptr = (T *) this->hidden->mixbuf; \
1878
d7c9d7f42881 Swizzle ALSA channels for 5.1 output to match DirectSound and CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 1553
diff changeset
   247
    Uint32 i; \
3704
9bc9ff36eb8f Merged r5549:5550 from branches/SDL-1.2: ALSA 6-channel swizzle fix.
Ryan C. Gordon <icculus@icculus.org>
parents: 3699
diff changeset
   248
    for (i = 0; i < this->spec.samples; i++, ptr += 6) { \
1878
d7c9d7f42881 Swizzle ALSA channels for 5.1 output to match DirectSound and CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 1553
diff changeset
   249
        T tmp; \
d7c9d7f42881 Swizzle ALSA channels for 5.1 output to match DirectSound and CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 1553
diff changeset
   250
        tmp = ptr[2]; ptr[2] = ptr[4]; ptr[4] = tmp; \
d7c9d7f42881 Swizzle ALSA channels for 5.1 output to match DirectSound and CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 1553
diff changeset
   251
        tmp = ptr[3]; ptr[3] = ptr[5]; ptr[5] = tmp; \
d7c9d7f42881 Swizzle ALSA channels for 5.1 output to match DirectSound and CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 1553
diff changeset
   252
    }
d7c9d7f42881 Swizzle ALSA channels for 5.1 output to match DirectSound and CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 1553
diff changeset
   253
7860
2b0bcdea3a79 Fixed bug 2129 - fix for bug 2121 breaks linking for mingw and throws multiple warnings
Sam Lantinga <slouken@libsdl.org>
parents: 7719
diff changeset
   254
static SDL_INLINE void
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   255
swizzle_alsa_channels_6_64bit(_THIS)
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   256
{
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   257
    SWIZ6(Uint64);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   258
}
2735
204be4fc2726 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents: 2060
diff changeset
   259
7860
2b0bcdea3a79 Fixed bug 2129 - fix for bug 2121 breaks linking for mingw and throws multiple warnings
Sam Lantinga <slouken@libsdl.org>
parents: 7719
diff changeset
   260
static SDL_INLINE void
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   261
swizzle_alsa_channels_6_32bit(_THIS)
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   262
{
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   263
    SWIZ6(Uint32);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   264
}
2735
204be4fc2726 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents: 2060
diff changeset
   265
7860
2b0bcdea3a79 Fixed bug 2129 - fix for bug 2121 breaks linking for mingw and throws multiple warnings
Sam Lantinga <slouken@libsdl.org>
parents: 7719
diff changeset
   266
static SDL_INLINE void
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   267
swizzle_alsa_channels_6_16bit(_THIS)
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   268
{
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   269
    SWIZ6(Uint16);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   270
}
2735
204be4fc2726 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents: 2060
diff changeset
   271
7860
2b0bcdea3a79 Fixed bug 2129 - fix for bug 2121 breaks linking for mingw and throws multiple warnings
Sam Lantinga <slouken@libsdl.org>
parents: 7719
diff changeset
   272
static SDL_INLINE void
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   273
swizzle_alsa_channels_6_8bit(_THIS)
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   274
{
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   275
    SWIZ6(Uint8);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   276
}
1878
d7c9d7f42881 Swizzle ALSA channels for 5.1 output to match DirectSound and CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 1553
diff changeset
   277
d7c9d7f42881 Swizzle ALSA channels for 5.1 output to match DirectSound and CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 1553
diff changeset
   278
#undef SWIZ6
d7c9d7f42881 Swizzle ALSA channels for 5.1 output to match DirectSound and CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 1553
diff changeset
   279
d7c9d7f42881 Swizzle ALSA channels for 5.1 output to match DirectSound and CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 1553
diff changeset
   280
d7c9d7f42881 Swizzle ALSA channels for 5.1 output to match DirectSound and CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 1553
diff changeset
   281
/*
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   282
 * Called right before feeding this->hidden->mixbuf to the hardware. Swizzle
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   283
 *  channels from Windows/Mac order to the format alsalib will want.
1878
d7c9d7f42881 Swizzle ALSA channels for 5.1 output to match DirectSound and CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 1553
diff changeset
   284
 */
7860
2b0bcdea3a79 Fixed bug 2129 - fix for bug 2121 breaks linking for mingw and throws multiple warnings
Sam Lantinga <slouken@libsdl.org>
parents: 7719
diff changeset
   285
static SDL_INLINE void
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   286
swizzle_alsa_channels(_THIS)
1878
d7c9d7f42881 Swizzle ALSA channels for 5.1 output to match DirectSound and CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 1553
diff changeset
   287
{
d7c9d7f42881 Swizzle ALSA channels for 5.1 output to match DirectSound and CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 1553
diff changeset
   288
    if (this->spec.channels == 6) {
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   289
        const Uint16 fmtsize = (this->spec.format & 0xFF);      /* bits/channel. */
1878
d7c9d7f42881 Swizzle ALSA channels for 5.1 output to match DirectSound and CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 1553
diff changeset
   290
        if (fmtsize == 16)
d7c9d7f42881 Swizzle ALSA channels for 5.1 output to match DirectSound and CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 1553
diff changeset
   291
            swizzle_alsa_channels_6_16bit(this);
d7c9d7f42881 Swizzle ALSA channels for 5.1 output to match DirectSound and CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 1553
diff changeset
   292
        else if (fmtsize == 8)
d7c9d7f42881 Swizzle ALSA channels for 5.1 output to match DirectSound and CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 1553
diff changeset
   293
            swizzle_alsa_channels_6_8bit(this);
d7c9d7f42881 Swizzle ALSA channels for 5.1 output to match DirectSound and CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 1553
diff changeset
   294
        else if (fmtsize == 32)
d7c9d7f42881 Swizzle ALSA channels for 5.1 output to match DirectSound and CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 1553
diff changeset
   295
            swizzle_alsa_channels_6_32bit(this);
d7c9d7f42881 Swizzle ALSA channels for 5.1 output to match DirectSound and CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 1553
diff changeset
   296
        else if (fmtsize == 64)
d7c9d7f42881 Swizzle ALSA channels for 5.1 output to match DirectSound and CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 1553
diff changeset
   297
            swizzle_alsa_channels_6_64bit(this);
d7c9d7f42881 Swizzle ALSA channels for 5.1 output to match DirectSound and CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 1553
diff changeset
   298
    }
d7c9d7f42881 Swizzle ALSA channels for 5.1 output to match DirectSound and CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 1553
diff changeset
   299
d7c9d7f42881 Swizzle ALSA channels for 5.1 output to match DirectSound and CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 1553
diff changeset
   300
    /* !!! FIXME: update this for 7.1 if needed, later. */
d7c9d7f42881 Swizzle ALSA channels for 5.1 output to match DirectSound and CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 1553
diff changeset
   301
}
d7c9d7f42881 Swizzle ALSA channels for 5.1 output to match DirectSound and CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 1553
diff changeset
   302
d7c9d7f42881 Swizzle ALSA channels for 5.1 output to match DirectSound and CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 1553
diff changeset
   303
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   304
static void
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   305
ALSA_PlayDevice(_THIS)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   306
{
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   307
    int status;
3627
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   308
    const Uint8 *sample_buf = (const Uint8 *) this->hidden->mixbuf;
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   309
    const int frame_size = (((int) (this->spec.format & 0xFF)) / 8) *
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   310
                                this->spec.channels;
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   311
    snd_pcm_uframes_t frames_left = ((snd_pcm_uframes_t) this->spec.samples);
765
4c2ba6161939 Editors Note: The original patch was modified to use SDL_Delay() instead of
Sam Lantinga <slouken@libsdl.org>
parents: 547
diff changeset
   312
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   313
    swizzle_alsa_channels(this);
1878
d7c9d7f42881 Swizzle ALSA channels for 5.1 output to match DirectSound and CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 1553
diff changeset
   314
3627
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   315
    while ( frames_left > 0 && this->enabled ) {
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   316
        /* !!! FIXME: This works, but needs more testing before going live */
7678
286c42d7c5ed OCD fixes: Adds a space after /* (glory to regular expressions!)
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7677
diff changeset
   317
        /* ALSA_snd_pcm_wait(this->hidden->pcm_handle, -1); */
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   318
        status = ALSA_snd_pcm_writei(this->hidden->pcm_handle,
3627
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   319
                                     sample_buf, frames_left);
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   320
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   321
        if (status < 0) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   322
            if (status == -EAGAIN) {
3627
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   323
                /* Apparently snd_pcm_recover() doesn't handle this case -
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   324
                   does it assume snd_pcm_wait() above? */
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   325
                SDL_Delay(1);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   326
                continue;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   327
            }
3627
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   328
            status = ALSA_snd_pcm_recover(this->hidden->pcm_handle, status, 0);
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   329
            if (status < 0) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   330
                /* Hmm, not much we can do - abort */
3627
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   331
                fprintf(stderr, "ALSA write failed (unrecoverable): %s\n",
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   332
                        ALSA_snd_strerror(status));
9394
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   333
                SDL_OpenedAudioDeviceDisconnected(this);
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   334
                return;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   335
            }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   336
            continue;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   337
        }
3627
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   338
        sample_buf += status * frame_size;
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   339
        frames_left -= status;
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   340
    }
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   341
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   342
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   343
static Uint8 *
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   344
ALSA_GetDeviceBuf(_THIS)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   345
{
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   346
    return (this->hidden->mixbuf);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   347
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   348
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   349
static void
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   350
ALSA_CloseDevice(_THIS)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   351
{
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   352
    if (this->hidden != NULL) {
7719
31b5f9ff36ca Christoph Mallon: Remove pointless if (x) before SDL_free(x)
Sam Lantinga <slouken@libsdl.org>
parents: 7678
diff changeset
   353
        SDL_FreeAudioMem(this->hidden->mixbuf);
31b5f9ff36ca Christoph Mallon: Remove pointless if (x) before SDL_free(x)
Sam Lantinga <slouken@libsdl.org>
parents: 7678
diff changeset
   354
        this->hidden->mixbuf = NULL;
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   355
        if (this->hidden->pcm_handle) {
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   356
            ALSA_snd_pcm_drain(this->hidden->pcm_handle);
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   357
            ALSA_snd_pcm_close(this->hidden->pcm_handle);
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   358
            this->hidden->pcm_handle = NULL;
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   359
        }
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   360
        SDL_free(this->hidden);
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   361
        this->hidden = NULL;
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   362
    }
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   363
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   364
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   365
static int
3627
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   366
ALSA_finalize_hardware(_THIS, snd_pcm_hw_params_t *hwparams, int override)
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   367
{
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   368
    int status;
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   369
    snd_pcm_uframes_t bufsize;
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   370
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   371
    /* "set" the hardware with the desired parameters */
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   372
    status = ALSA_snd_pcm_hw_params(this->hidden->pcm_handle, hwparams);
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   373
    if ( status < 0 ) {
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   374
        return(-1);
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   375
    }
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   376
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   377
    /* Get samples for the actual buffer size */
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   378
    status = ALSA_snd_pcm_hw_params_get_buffer_size(hwparams, &bufsize);
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   379
    if ( status < 0 ) {
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   380
        return(-1);
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   381
    }
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   382
    if ( !override && bufsize != this->spec.samples * 2 ) {
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   383
        return(-1);
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   384
    }
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   385
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   386
    /* !!! FIXME: Is this safe to do? */
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   387
    this->spec.samples = bufsize / 2;
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   388
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   389
    /* This is useful for debugging */
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   390
    if ( SDL_getenv("SDL_AUDIO_ALSA_DEBUG") ) {
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   391
        snd_pcm_uframes_t persize = 0;
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   392
        unsigned int periods = 0;
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   393
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   394
        ALSA_snd_pcm_hw_params_get_period_size(hwparams, &persize, NULL);
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   395
        ALSA_snd_pcm_hw_params_get_periods(hwparams, &periods, NULL);
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   396
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   397
        fprintf(stderr,
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   398
            "ALSA: period size = %ld, periods = %u, buffer size = %lu\n",
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   399
            persize, periods, bufsize);
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   400
    }
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   401
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   402
    return(0);
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   403
}
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   404
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   405
static int
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   406
ALSA_set_period_size(_THIS, snd_pcm_hw_params_t *params, int override)
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   407
{
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   408
    const char *env;
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   409
    int status;
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   410
    snd_pcm_hw_params_t *hwparams;
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   411
    snd_pcm_uframes_t frames;
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   412
    unsigned int periods;
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   413
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   414
    /* Copy the hardware parameters for this setup */
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   415
    snd_pcm_hw_params_alloca(&hwparams);
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   416
    ALSA_snd_pcm_hw_params_copy(hwparams, params);
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   417
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   418
    if ( !override ) {
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   419
        env = SDL_getenv("SDL_AUDIO_ALSA_SET_PERIOD_SIZE");
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   420
        if ( env ) {
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   421
            override = SDL_atoi(env);
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   422
            if ( override == 0 ) {
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   423
                return(-1);
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   424
            }
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   425
        }
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   426
    }
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   427
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   428
    frames = this->spec.samples;
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   429
    status = ALSA_snd_pcm_hw_params_set_period_size_near(
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   430
                this->hidden->pcm_handle, hwparams, &frames, NULL);
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   431
    if ( status < 0 ) {
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   432
        return(-1);
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   433
    }
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   434
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   435
    periods = 2;
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   436
    status = ALSA_snd_pcm_hw_params_set_periods_near(
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   437
                this->hidden->pcm_handle, hwparams, &periods, NULL);
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   438
    if ( status < 0 ) {
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   439
        return(-1);
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   440
    }
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   441
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   442
    return ALSA_finalize_hardware(this, hwparams, override);
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   443
}
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   444
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   445
static int
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   446
ALSA_set_buffer_size(_THIS, snd_pcm_hw_params_t *params, int override)
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   447
{
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   448
    const char *env;
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   449
    int status;
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   450
    snd_pcm_hw_params_t *hwparams;
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   451
    snd_pcm_uframes_t frames;
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   452
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   453
    /* Copy the hardware parameters for this setup */
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   454
    snd_pcm_hw_params_alloca(&hwparams);
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   455
    ALSA_snd_pcm_hw_params_copy(hwparams, params);
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   456
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   457
    if ( !override ) {
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   458
        env = SDL_getenv("SDL_AUDIO_ALSA_SET_BUFFER_SIZE");
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   459
        if ( env ) {
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   460
            override = SDL_atoi(env);
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   461
            if ( override == 0 ) {
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   462
                return(-1);
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   463
            }
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   464
        }
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   465
    }
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   466
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   467
    frames = this->spec.samples * 2;
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   468
    status = ALSA_snd_pcm_hw_params_set_buffer_size_near(
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   469
                    this->hidden->pcm_handle, hwparams, &frames);
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   470
    if ( status < 0 ) {
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   471
        return(-1);
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   472
    }
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   473
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   474
    return ALSA_finalize_hardware(this, hwparams, override);
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   475
}
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   476
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   477
static int
9394
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   478
ALSA_OpenDevice(_THIS, void *handle, const char *devname, int iscapture)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   479
{
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   480
    int status = 0;
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   481
    snd_pcm_t *pcm_handle = NULL;
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   482
    snd_pcm_hw_params_t *hwparams = NULL;
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   483
    snd_pcm_sw_params_t *swparams = NULL;
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   484
    snd_pcm_format_t format = 0;
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   485
    SDL_AudioFormat test_format = 0;
3362
4e83cdb58134 Merged r4990:4991 from branches/SDL-1.2: ALSA 1.0 API and dlvsym() removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 3068
diff changeset
   486
    unsigned int rate = 0;
4e83cdb58134 Merged r4990:4991 from branches/SDL-1.2: ALSA 1.0 API and dlvsym() removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 3068
diff changeset
   487
    unsigned int channels = 0;
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   488
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   489
    /* Initialize all variables that we clean on shutdown */
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   490
    this->hidden = (struct SDL_PrivateAudioData *)
2060
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2049
diff changeset
   491
        SDL_malloc((sizeof *this->hidden));
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   492
    if (this->hidden == NULL) {
7038
7f22b9ba218f Changed audio subsystem's OpenDevice interface to return -1 on error.
Ryan C. Gordon <icculus@icculus.org>
parents: 6885
diff changeset
   493
        return SDL_OutOfMemory();
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   494
    }
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   495
    SDL_memset(this->hidden, 0, (sizeof *this->hidden));
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   496
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   497
    /* Open the audio device */
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   498
    /* Name of device should depend on # channels in spec */
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   499
    status = ALSA_snd_pcm_open(&pcm_handle,
10104
4cd67316b308 alsa: Implemented basic device detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   500
                               get_audio_device(handle, this->spec.channels),
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   501
                               SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK);
942
41a59de7f2ed Here are patches for SDL12 and SDL_mixer for 4 or 6 channel
Sam Lantinga <slouken@libsdl.org>
parents: 939
diff changeset
   502
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   503
    if (status < 0) {
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   504
        ALSA_CloseDevice(this);
7038
7f22b9ba218f Changed audio subsystem's OpenDevice interface to return -1 on error.
Ryan C. Gordon <icculus@icculus.org>
parents: 6885
diff changeset
   505
        return SDL_SetError("ALSA: Couldn't open audio device: %s",
7f22b9ba218f Changed audio subsystem's OpenDevice interface to return -1 on error.
Ryan C. Gordon <icculus@icculus.org>
parents: 6885
diff changeset
   506
                            ALSA_snd_strerror(status));
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   507
    }
354
30935e76acb5 Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
   508
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   509
    this->hidden->pcm_handle = pcm_handle;
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   510
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   511
    /* Figure out what the hardware is capable of */
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   512
    snd_pcm_hw_params_alloca(&hwparams);
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   513
    status = ALSA_snd_pcm_hw_params_any(pcm_handle, hwparams);
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   514
    if (status < 0) {
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   515
        ALSA_CloseDevice(this);
7038
7f22b9ba218f Changed audio subsystem's OpenDevice interface to return -1 on error.
Ryan C. Gordon <icculus@icculus.org>
parents: 6885
diff changeset
   516
        return SDL_SetError("ALSA: Couldn't get hardware config: %s",
7f22b9ba218f Changed audio subsystem's OpenDevice interface to return -1 on error.
Ryan C. Gordon <icculus@icculus.org>
parents: 6885
diff changeset
   517
                            ALSA_snd_strerror(status));
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   518
    }
354
30935e76acb5 Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
   519
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   520
    /* SDL only uses interleaved sample output */
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   521
    status = ALSA_snd_pcm_hw_params_set_access(pcm_handle, hwparams,
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   522
                                               SND_PCM_ACCESS_RW_INTERLEAVED);
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   523
    if (status < 0) {
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   524
        ALSA_CloseDevice(this);
7038
7f22b9ba218f Changed audio subsystem's OpenDevice interface to return -1 on error.
Ryan C. Gordon <icculus@icculus.org>
parents: 6885
diff changeset
   525
        return SDL_SetError("ALSA: Couldn't set interleaved access: %s",
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   526
                     ALSA_snd_strerror(status));
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   527
    }
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   528
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   529
    /* Try for a closest match on audio format */
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   530
    status = -1;
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   531
    for (test_format = SDL_FirstAudioFormat(this->spec.format);
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   532
         test_format && (status < 0);) {
2060
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2049
diff changeset
   533
        status = 0;             /* if we can't support a format, it'll become -1. */
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   534
        switch (test_format) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   535
        case AUDIO_U8:
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   536
            format = SND_PCM_FORMAT_U8;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   537
            break;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   538
        case AUDIO_S8:
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   539
            format = SND_PCM_FORMAT_S8;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   540
            break;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   541
        case AUDIO_S16LSB:
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   542
            format = SND_PCM_FORMAT_S16_LE;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   543
            break;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   544
        case AUDIO_S16MSB:
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   545
            format = SND_PCM_FORMAT_S16_BE;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   546
            break;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   547
        case AUDIO_U16LSB:
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   548
            format = SND_PCM_FORMAT_U16_LE;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   549
            break;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   550
        case AUDIO_U16MSB:
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   551
            format = SND_PCM_FORMAT_U16_BE;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   552
            break;
1995
0ca6ba107642 ALSA backend can handle int32 and float32 data directly.
Ryan C. Gordon <icculus@icculus.org>
parents: 1982
diff changeset
   553
        case AUDIO_S32LSB:
2010
39897da56f63 Whoops, wrong tokens for int32 support in ALSA driver (specified unsigned
Ryan C. Gordon <icculus@icculus.org>
parents: 2009
diff changeset
   554
            format = SND_PCM_FORMAT_S32_LE;
1995
0ca6ba107642 ALSA backend can handle int32 and float32 data directly.
Ryan C. Gordon <icculus@icculus.org>
parents: 1982
diff changeset
   555
            break;
0ca6ba107642 ALSA backend can handle int32 and float32 data directly.
Ryan C. Gordon <icculus@icculus.org>
parents: 1982
diff changeset
   556
        case AUDIO_S32MSB:
2010
39897da56f63 Whoops, wrong tokens for int32 support in ALSA driver (specified unsigned
Ryan C. Gordon <icculus@icculus.org>
parents: 2009
diff changeset
   557
            format = SND_PCM_FORMAT_S32_BE;
1995
0ca6ba107642 ALSA backend can handle int32 and float32 data directly.
Ryan C. Gordon <icculus@icculus.org>
parents: 1982
diff changeset
   558
            break;
0ca6ba107642 ALSA backend can handle int32 and float32 data directly.
Ryan C. Gordon <icculus@icculus.org>
parents: 1982
diff changeset
   559
        case AUDIO_F32LSB:
0ca6ba107642 ALSA backend can handle int32 and float32 data directly.
Ryan C. Gordon <icculus@icculus.org>
parents: 1982
diff changeset
   560
            format = SND_PCM_FORMAT_FLOAT_LE;
0ca6ba107642 ALSA backend can handle int32 and float32 data directly.
Ryan C. Gordon <icculus@icculus.org>
parents: 1982
diff changeset
   561
            break;
0ca6ba107642 ALSA backend can handle int32 and float32 data directly.
Ryan C. Gordon <icculus@icculus.org>
parents: 1982
diff changeset
   562
        case AUDIO_F32MSB:
0ca6ba107642 ALSA backend can handle int32 and float32 data directly.
Ryan C. Gordon <icculus@icculus.org>
parents: 1982
diff changeset
   563
            format = SND_PCM_FORMAT_FLOAT_BE;
0ca6ba107642 ALSA backend can handle int32 and float32 data directly.
Ryan C. Gordon <icculus@icculus.org>
parents: 1982
diff changeset
   564
            break;
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   565
        default:
2009
f2058fb367e4 ALSA was testing if (format) was set to zero as an error condition, but
Ryan C. Gordon <icculus@icculus.org>
parents: 1995
diff changeset
   566
            status = -1;
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   567
            break;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   568
        }
2009
f2058fb367e4 ALSA was testing if (format) was set to zero as an error condition, but
Ryan C. Gordon <icculus@icculus.org>
parents: 1995
diff changeset
   569
        if (status >= 0) {
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   570
            status = ALSA_snd_pcm_hw_params_set_format(pcm_handle,
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   571
                                                       hwparams, format);
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   572
        }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   573
        if (status < 0) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   574
            test_format = SDL_NextAudioFormat();
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   575
        }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   576
    }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   577
    if (status < 0) {
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   578
        ALSA_CloseDevice(this);
7038
7f22b9ba218f Changed audio subsystem's OpenDevice interface to return -1 on error.
Ryan C. Gordon <icculus@icculus.org>
parents: 6885
diff changeset
   579
        return SDL_SetError("ALSA: Couldn't find any hardware audio formats");
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   580
    }
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   581
    this->spec.format = test_format;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   582
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   583
    /* Set the number of channels */
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   584
    status = ALSA_snd_pcm_hw_params_set_channels(pcm_handle, hwparams,
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   585
                                                 this->spec.channels);
3362
4e83cdb58134 Merged r4990:4991 from branches/SDL-1.2: ALSA 1.0 API and dlvsym() removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 3068
diff changeset
   586
    channels = this->spec.channels;
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   587
    if (status < 0) {
3362
4e83cdb58134 Merged r4990:4991 from branches/SDL-1.2: ALSA 1.0 API and dlvsym() removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 3068
diff changeset
   588
        status = ALSA_snd_pcm_hw_params_get_channels(hwparams, &channels);
4e83cdb58134 Merged r4990:4991 from branches/SDL-1.2: ALSA 1.0 API and dlvsym() removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 3068
diff changeset
   589
        if (status < 0) {
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   590
            ALSA_CloseDevice(this);
7038
7f22b9ba218f Changed audio subsystem's OpenDevice interface to return -1 on error.
Ryan C. Gordon <icculus@icculus.org>
parents: 6885
diff changeset
   591
            return SDL_SetError("ALSA: Couldn't set audio channels");
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   592
        }
3362
4e83cdb58134 Merged r4990:4991 from branches/SDL-1.2: ALSA 1.0 API and dlvsym() removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 3068
diff changeset
   593
        this->spec.channels = channels;
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   594
    }
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   595
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   596
    /* Set the audio rate */
3362
4e83cdb58134 Merged r4990:4991 from branches/SDL-1.2: ALSA 1.0 API and dlvsym() removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 3068
diff changeset
   597
    rate = this->spec.freq;
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   598
    status = ALSA_snd_pcm_hw_params_set_rate_near(pcm_handle, hwparams,
3362
4e83cdb58134 Merged r4990:4991 from branches/SDL-1.2: ALSA 1.0 API and dlvsym() removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 3068
diff changeset
   599
                                                  &rate, NULL);
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   600
    if (status < 0) {
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   601
        ALSA_CloseDevice(this);
7038
7f22b9ba218f Changed audio subsystem's OpenDevice interface to return -1 on error.
Ryan C. Gordon <icculus@icculus.org>
parents: 6885
diff changeset
   602
        return SDL_SetError("ALSA: Couldn't set audio frequency: %s",
7f22b9ba218f Changed audio subsystem's OpenDevice interface to return -1 on error.
Ryan C. Gordon <icculus@icculus.org>
parents: 6885
diff changeset
   603
                            ALSA_snd_strerror(status));
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   604
    }
3362
4e83cdb58134 Merged r4990:4991 from branches/SDL-1.2: ALSA 1.0 API and dlvsym() removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 3068
diff changeset
   605
    this->spec.freq = rate;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   606
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   607
    /* Set the buffer size, in samples */
3627
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   608
    if ( ALSA_set_period_size(this, hwparams, 0) < 0 &&
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   609
         ALSA_set_buffer_size(this, hwparams, 0) < 0 ) {
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   610
        /* Failed to set desired buffer size, do the best you can... */
10206
30ca8e64db6a ALSA: Fixed adding wrong status to error message (thanks, romain145!).
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 10142
diff changeset
   611
        status = ALSA_set_period_size(this, hwparams, 1);
30ca8e64db6a ALSA: Fixed adding wrong status to error message (thanks, romain145!).
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 10142
diff changeset
   612
        if (status < 0) {
3627
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   613
            ALSA_CloseDevice(this);
7038
7f22b9ba218f Changed audio subsystem's OpenDevice interface to return -1 on error.
Ryan C. Gordon <icculus@icculus.org>
parents: 6885
diff changeset
   614
            return SDL_SetError("Couldn't set hardware audio parameters: %s", ALSA_snd_strerror(status));
3627
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   615
        }
3362
4e83cdb58134 Merged r4990:4991 from branches/SDL-1.2: ALSA 1.0 API and dlvsym() removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 3068
diff changeset
   616
    }
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   617
    /* Set the software parameters */
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   618
    snd_pcm_sw_params_alloca(&swparams);
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   619
    status = ALSA_snd_pcm_sw_params_current(pcm_handle, swparams);
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   620
    if (status < 0) {
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   621
        ALSA_CloseDevice(this);
7038
7f22b9ba218f Changed audio subsystem's OpenDevice interface to return -1 on error.
Ryan C. Gordon <icculus@icculus.org>
parents: 6885
diff changeset
   622
        return SDL_SetError("ALSA: Couldn't get software config: %s",
7f22b9ba218f Changed audio subsystem's OpenDevice interface to return -1 on error.
Ryan C. Gordon <icculus@icculus.org>
parents: 6885
diff changeset
   623
                            ALSA_snd_strerror(status));
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   624
    }
5622
08e941dad8e9 Ported ALSA minimum-sample-count fix from 1.2 branch to 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents: 5594
diff changeset
   625
    status = ALSA_snd_pcm_sw_params_set_avail_min(pcm_handle, swparams, this->spec.samples);
08e941dad8e9 Ported ALSA minimum-sample-count fix from 1.2 branch to 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents: 5594
diff changeset
   626
    if (status < 0) {
08e941dad8e9 Ported ALSA minimum-sample-count fix from 1.2 branch to 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents: 5594
diff changeset
   627
        ALSA_CloseDevice(this);
7038
7f22b9ba218f Changed audio subsystem's OpenDevice interface to return -1 on error.
Ryan C. Gordon <icculus@icculus.org>
parents: 6885
diff changeset
   628
        return SDL_SetError("Couldn't set minimum available samples: %s",
7f22b9ba218f Changed audio subsystem's OpenDevice interface to return -1 on error.
Ryan C. Gordon <icculus@icculus.org>
parents: 6885
diff changeset
   629
                            ALSA_snd_strerror(status));
5622
08e941dad8e9 Ported ALSA minimum-sample-count fix from 1.2 branch to 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents: 5594
diff changeset
   630
    }
2060
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2049
diff changeset
   631
    status =
3627
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   632
        ALSA_snd_pcm_sw_params_set_start_threshold(pcm_handle, swparams, 1);
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   633
    if (status < 0) {
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   634
        ALSA_CloseDevice(this);
7038
7f22b9ba218f Changed audio subsystem's OpenDevice interface to return -1 on error.
Ryan C. Gordon <icculus@icculus.org>
parents: 6885
diff changeset
   635
        return SDL_SetError("ALSA: Couldn't set start threshold: %s",
7f22b9ba218f Changed audio subsystem's OpenDevice interface to return -1 on error.
Ryan C. Gordon <icculus@icculus.org>
parents: 6885
diff changeset
   636
                            ALSA_snd_strerror(status));
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   637
    }
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   638
    status = ALSA_snd_pcm_sw_params(pcm_handle, swparams);
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   639
    if (status < 0) {
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   640
        ALSA_CloseDevice(this);
7038
7f22b9ba218f Changed audio subsystem's OpenDevice interface to return -1 on error.
Ryan C. Gordon <icculus@icculus.org>
parents: 6885
diff changeset
   641
        return SDL_SetError("Couldn't set software audio parameters: %s",
7f22b9ba218f Changed audio subsystem's OpenDevice interface to return -1 on error.
Ryan C. Gordon <icculus@icculus.org>
parents: 6885
diff changeset
   642
                            ALSA_snd_strerror(status));
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   643
    }
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   644
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   645
    /* Calculate the final parameters for this audio specification */
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   646
    SDL_CalculateAudioSpec(&this->spec);
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   647
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   648
    /* Allocate mixing buffer */
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   649
    this->hidden->mixlen = this->spec.size;
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   650
    this->hidden->mixbuf = (Uint8 *) SDL_AllocAudioMem(this->hidden->mixlen);
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   651
    if (this->hidden->mixbuf == NULL) {
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   652
        ALSA_CloseDevice(this);
7038
7f22b9ba218f Changed audio subsystem's OpenDevice interface to return -1 on error.
Ryan C. Gordon <icculus@icculus.org>
parents: 6885
diff changeset
   653
        return SDL_OutOfMemory();
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   654
    }
7365
a3fc9175306d Minor ALSA tweaks (include-once macro name, len for memset() more clear).
Ryan C. Gordon <icculus@icculus.org>
parents: 7191
diff changeset
   655
    SDL_memset(this->hidden->mixbuf, this->spec.silence, this->hidden->mixlen);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   656
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   657
    /* Switch to blocking mode for playback */
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   658
    ALSA_snd_pcm_nonblock(pcm_handle, 0);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   659
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   660
    /* We're ready to rock and roll. :-) */
7038
7f22b9ba218f Changed audio subsystem's OpenDevice interface to return -1 on error.
Ryan C. Gordon <icculus@icculus.org>
parents: 6885
diff changeset
   661
    return 0;
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   662
}
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   663
10284
bd207addc5ec alsa: Implemented hotplug support, cleaned up device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10282
diff changeset
   664
typedef struct ALSA_Device
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   665
{
10284
bd207addc5ec alsa: Implemented hotplug support, cleaned up device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10282
diff changeset
   666
    char *name;
bd207addc5ec alsa: Implemented hotplug support, cleaned up device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10282
diff changeset
   667
    SDL_bool iscapture;
bd207addc5ec alsa: Implemented hotplug support, cleaned up device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10282
diff changeset
   668
    struct ALSA_Device *next;
bd207addc5ec alsa: Implemented hotplug support, cleaned up device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10282
diff changeset
   669
} ALSA_Device;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   670
10104
4cd67316b308 alsa: Implemented basic device detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   671
static void
10284
bd207addc5ec alsa: Implemented hotplug support, cleaned up device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10282
diff changeset
   672
add_device(const int iscapture, const char *name, void *hint, ALSA_Device **pSeen)
10104
4cd67316b308 alsa: Implemented basic device detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   673
{
10284
bd207addc5ec alsa: Implemented hotplug support, cleaned up device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10282
diff changeset
   674
    ALSA_Device *dev = SDL_malloc(sizeof (ALSA_Device));
bd207addc5ec alsa: Implemented hotplug support, cleaned up device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10282
diff changeset
   675
    char *desc = ALSA_snd_device_name_get_hint(hint, "DESC");
10104
4cd67316b308 alsa: Implemented basic device detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   676
    char *handle = NULL;
10284
bd207addc5ec alsa: Implemented hotplug support, cleaned up device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10282
diff changeset
   677
    char *ptr;
bd207addc5ec alsa: Implemented hotplug support, cleaned up device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10282
diff changeset
   678
bd207addc5ec alsa: Implemented hotplug support, cleaned up device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10282
diff changeset
   679
    if (!desc) {
bd207addc5ec alsa: Implemented hotplug support, cleaned up device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10282
diff changeset
   680
        SDL_free(dev);
bd207addc5ec alsa: Implemented hotplug support, cleaned up device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10282
diff changeset
   681
        return;
bd207addc5ec alsa: Implemented hotplug support, cleaned up device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10282
diff changeset
   682
    } else if (!dev) {
bd207addc5ec alsa: Implemented hotplug support, cleaned up device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10282
diff changeset
   683
        free(desc);
bd207addc5ec alsa: Implemented hotplug support, cleaned up device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10282
diff changeset
   684
        return;
bd207addc5ec alsa: Implemented hotplug support, cleaned up device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10282
diff changeset
   685
    }
bd207addc5ec alsa: Implemented hotplug support, cleaned up device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10282
diff changeset
   686
bd207addc5ec alsa: Implemented hotplug support, cleaned up device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10282
diff changeset
   687
    SDL_assert(name != NULL);
10104
4cd67316b308 alsa: Implemented basic device detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   688
10284
bd207addc5ec alsa: Implemented hotplug support, cleaned up device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10282
diff changeset
   689
    /* some strings have newlines, like "HDA NVidia, HDMI 0\nHDMI Audio Output".
bd207addc5ec alsa: Implemented hotplug support, cleaned up device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10282
diff changeset
   690
       just chop the extra lines off, this seems to get a reasonable device
bd207addc5ec alsa: Implemented hotplug support, cleaned up device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10282
diff changeset
   691
       name without extra details. */
bd207addc5ec alsa: Implemented hotplug support, cleaned up device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10282
diff changeset
   692
    if ((ptr = strchr(desc, '\n')) != NULL) {
bd207addc5ec alsa: Implemented hotplug support, cleaned up device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10282
diff changeset
   693
        *ptr = '\0';
bd207addc5ec alsa: Implemented hotplug support, cleaned up device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10282
diff changeset
   694
    }
bd207addc5ec alsa: Implemented hotplug support, cleaned up device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10282
diff changeset
   695
bd207addc5ec alsa: Implemented hotplug support, cleaned up device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10282
diff changeset
   696
    /*printf("ALSA: adding %s device '%s' (%s)\n", iscapture ? "capture" : "output", name, desc);*/
bd207addc5ec alsa: Implemented hotplug support, cleaned up device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10282
diff changeset
   697
bd207addc5ec alsa: Implemented hotplug support, cleaned up device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10282
diff changeset
   698
    handle = SDL_strdup(name);
bd207addc5ec alsa: Implemented hotplug support, cleaned up device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10282
diff changeset
   699
    if (!handle) {
bd207addc5ec alsa: Implemented hotplug support, cleaned up device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10282
diff changeset
   700
        free(desc);
bd207addc5ec alsa: Implemented hotplug support, cleaned up device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10282
diff changeset
   701
        SDL_free(dev);
bd207addc5ec alsa: Implemented hotplug support, cleaned up device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10282
diff changeset
   702
        return;
10142
bc8ac6c0cd98 alsa: Make device enumeration robust against weird results with NULL strings.
Ryan C. Gordon <icculus@icculus.org>
parents: 10104
diff changeset
   703
    }
bc8ac6c0cd98 alsa: Make device enumeration robust against weird results with NULL strings.
Ryan C. Gordon <icculus@icculus.org>
parents: 10104
diff changeset
   704
10284
bd207addc5ec alsa: Implemented hotplug support, cleaned up device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10282
diff changeset
   705
    SDL_AddAudioDevice(iscapture, desc, handle);
bd207addc5ec alsa: Implemented hotplug support, cleaned up device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10282
diff changeset
   706
    free(desc);
bd207addc5ec alsa: Implemented hotplug support, cleaned up device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10282
diff changeset
   707
bd207addc5ec alsa: Implemented hotplug support, cleaned up device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10282
diff changeset
   708
    dev->name = handle;
bd207addc5ec alsa: Implemented hotplug support, cleaned up device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10282
diff changeset
   709
    dev->iscapture = iscapture;
bd207addc5ec alsa: Implemented hotplug support, cleaned up device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10282
diff changeset
   710
    dev->next = *pSeen;
bd207addc5ec alsa: Implemented hotplug support, cleaned up device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10282
diff changeset
   711
    *pSeen = dev;
bd207addc5ec alsa: Implemented hotplug support, cleaned up device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10282
diff changeset
   712
}
bd207addc5ec alsa: Implemented hotplug support, cleaned up device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10282
diff changeset
   713
bd207addc5ec alsa: Implemented hotplug support, cleaned up device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10282
diff changeset
   714
bd207addc5ec alsa: Implemented hotplug support, cleaned up device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10282
diff changeset
   715
static SDL_atomic_t ALSA_hotplug_shutdown;
bd207addc5ec alsa: Implemented hotplug support, cleaned up device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10282
diff changeset
   716
static SDL_Thread *ALSA_hotplug_thread;
bd207addc5ec alsa: Implemented hotplug support, cleaned up device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10282
diff changeset
   717
bd207addc5ec alsa: Implemented hotplug support, cleaned up device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10282
diff changeset
   718
static int SDLCALL
bd207addc5ec alsa: Implemented hotplug support, cleaned up device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10282
diff changeset
   719
ALSA_HotplugThread(void *arg)
bd207addc5ec alsa: Implemented hotplug support, cleaned up device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10282
diff changeset
   720
{
bd207addc5ec alsa: Implemented hotplug support, cleaned up device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10282
diff changeset
   721
    SDL_sem *first_run_semaphore = (SDL_sem *) arg;
bd207addc5ec alsa: Implemented hotplug support, cleaned up device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10282
diff changeset
   722
    ALSA_Device *devices = NULL;
bd207addc5ec alsa: Implemented hotplug support, cleaned up device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10282
diff changeset
   723
    ALSA_Device *next;
bd207addc5ec alsa: Implemented hotplug support, cleaned up device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10282
diff changeset
   724
    ALSA_Device *dev;
bd207addc5ec alsa: Implemented hotplug support, cleaned up device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10282
diff changeset
   725
    Uint32 ticks;
bd207addc5ec alsa: Implemented hotplug support, cleaned up device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10282
diff changeset
   726
bd207addc5ec alsa: Implemented hotplug support, cleaned up device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10282
diff changeset
   727
    while (!SDL_AtomicGet(&ALSA_hotplug_shutdown)) {
bd207addc5ec alsa: Implemented hotplug support, cleaned up device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10282
diff changeset
   728
        void **hints = NULL;
bd207addc5ec alsa: Implemented hotplug support, cleaned up device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10282
diff changeset
   729
        if (ALSA_snd_device_name_hint(-1, "pcm", &hints) != -1) {
bd207addc5ec alsa: Implemented hotplug support, cleaned up device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10282
diff changeset
   730
            ALSA_Device *unseen = devices;
bd207addc5ec alsa: Implemented hotplug support, cleaned up device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10282
diff changeset
   731
            ALSA_Device *seen = NULL;
bd207addc5ec alsa: Implemented hotplug support, cleaned up device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10282
diff changeset
   732
            ALSA_Device *prev;
bd207addc5ec alsa: Implemented hotplug support, cleaned up device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10282
diff changeset
   733
            int i;
bd207addc5ec alsa: Implemented hotplug support, cleaned up device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10282
diff changeset
   734
bd207addc5ec alsa: Implemented hotplug support, cleaned up device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10282
diff changeset
   735
            for (i = 0; hints[i]; i++) {
bd207addc5ec alsa: Implemented hotplug support, cleaned up device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10282
diff changeset
   736
                char *name = ALSA_snd_device_name_get_hint(hints[i], "NAME");
bd207addc5ec alsa: Implemented hotplug support, cleaned up device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10282
diff changeset
   737
                if (!name) {
bd207addc5ec alsa: Implemented hotplug support, cleaned up device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10282
diff changeset
   738
                    continue;
bd207addc5ec alsa: Implemented hotplug support, cleaned up device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10282
diff changeset
   739
                }
bd207addc5ec alsa: Implemented hotplug support, cleaned up device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10282
diff changeset
   740
bd207addc5ec alsa: Implemented hotplug support, cleaned up device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10282
diff changeset
   741
                /* only want physical hardware interfaces */
bd207addc5ec alsa: Implemented hotplug support, cleaned up device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10282
diff changeset
   742
                if (SDL_strncmp(name, "hw:", 3) == 0) {
bd207addc5ec alsa: Implemented hotplug support, cleaned up device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10282
diff changeset
   743
                    char *ioid = ALSA_snd_device_name_get_hint(hints[i], "IOID");
bd207addc5ec alsa: Implemented hotplug support, cleaned up device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10282
diff changeset
   744
                    const SDL_bool isoutput = (ioid == NULL) || (SDL_strcmp(ioid, "Output") == 0);
bd207addc5ec alsa: Implemented hotplug support, cleaned up device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10282
diff changeset
   745
                    const SDL_bool isinput = (ioid == NULL) || (SDL_strcmp(ioid, "Input") == 0);
bd207addc5ec alsa: Implemented hotplug support, cleaned up device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10282
diff changeset
   746
                    SDL_bool have_output = SDL_FALSE;
bd207addc5ec alsa: Implemented hotplug support, cleaned up device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10282
diff changeset
   747
                    SDL_bool have_input = SDL_FALSE;
bd207addc5ec alsa: Implemented hotplug support, cleaned up device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10282
diff changeset
   748
bd207addc5ec alsa: Implemented hotplug support, cleaned up device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10282
diff changeset
   749
                    free(ioid);
bd207addc5ec alsa: Implemented hotplug support, cleaned up device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10282
diff changeset
   750
bd207addc5ec alsa: Implemented hotplug support, cleaned up device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10282
diff changeset
   751
                    if (!isoutput && !isinput) {
bd207addc5ec alsa: Implemented hotplug support, cleaned up device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10282
diff changeset
   752
                        free(name);
bd207addc5ec alsa: Implemented hotplug support, cleaned up device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10282
diff changeset
   753
                        continue;
bd207addc5ec alsa: Implemented hotplug support, cleaned up device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10282
diff changeset
   754
                    }
bd207addc5ec alsa: Implemented hotplug support, cleaned up device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10282
diff changeset
   755
bd207addc5ec alsa: Implemented hotplug support, cleaned up device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10282
diff changeset
   756
                    prev = NULL;
bd207addc5ec alsa: Implemented hotplug support, cleaned up device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10282
diff changeset
   757
                    for (dev = unseen; dev; dev = next) {
bd207addc5ec alsa: Implemented hotplug support, cleaned up device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10282
diff changeset
   758
                        next = dev->next;
bd207addc5ec alsa: Implemented hotplug support, cleaned up device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10282
diff changeset
   759
                        if ( (SDL_strcmp(dev->name, name) == 0) && (((isinput) && dev->iscapture) || ((isoutput) && !dev->iscapture)) ) {
bd207addc5ec alsa: Implemented hotplug support, cleaned up device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10282
diff changeset
   760
                            if (prev) {
bd207addc5ec alsa: Implemented hotplug support, cleaned up device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10282
diff changeset
   761
                                prev->next = next;
bd207addc5ec alsa: Implemented hotplug support, cleaned up device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10282
diff changeset
   762
                            } else {
bd207addc5ec alsa: Implemented hotplug support, cleaned up device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10282
diff changeset
   763
                                unseen = next;
bd207addc5ec alsa: Implemented hotplug support, cleaned up device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10282
diff changeset
   764
                            }
bd207addc5ec alsa: Implemented hotplug support, cleaned up device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10282
diff changeset
   765
                            dev->next = seen;
bd207addc5ec alsa: Implemented hotplug support, cleaned up device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10282
diff changeset
   766
                            seen = dev;
bd207addc5ec alsa: Implemented hotplug support, cleaned up device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10282
diff changeset
   767
                            if (isinput) have_input = SDL_TRUE;
bd207addc5ec alsa: Implemented hotplug support, cleaned up device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10282
diff changeset
   768
                            if (isoutput) have_output = SDL_TRUE;
bd207addc5ec alsa: Implemented hotplug support, cleaned up device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10282
diff changeset
   769
                        } else {
bd207addc5ec alsa: Implemented hotplug support, cleaned up device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10282
diff changeset
   770
                            prev = dev;
bd207addc5ec alsa: Implemented hotplug support, cleaned up device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10282
diff changeset
   771
                        }
bd207addc5ec alsa: Implemented hotplug support, cleaned up device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10282
diff changeset
   772
                    }
bd207addc5ec alsa: Implemented hotplug support, cleaned up device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10282
diff changeset
   773
bd207addc5ec alsa: Implemented hotplug support, cleaned up device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10282
diff changeset
   774
                    if (isinput && !have_input) {
bd207addc5ec alsa: Implemented hotplug support, cleaned up device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10282
diff changeset
   775
                        add_device(SDL_TRUE, name, hints[i], &seen);
bd207addc5ec alsa: Implemented hotplug support, cleaned up device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10282
diff changeset
   776
                    }
bd207addc5ec alsa: Implemented hotplug support, cleaned up device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10282
diff changeset
   777
                    if (isoutput && !have_output) {
bd207addc5ec alsa: Implemented hotplug support, cleaned up device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10282
diff changeset
   778
                        add_device(SDL_FALSE, name, hints[i], &seen);
bd207addc5ec alsa: Implemented hotplug support, cleaned up device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10282
diff changeset
   779
                    }
bd207addc5ec alsa: Implemented hotplug support, cleaned up device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10282
diff changeset
   780
                }
bd207addc5ec alsa: Implemented hotplug support, cleaned up device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10282
diff changeset
   781
bd207addc5ec alsa: Implemented hotplug support, cleaned up device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10282
diff changeset
   782
                free(name);
bd207addc5ec alsa: Implemented hotplug support, cleaned up device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10282
diff changeset
   783
            }
bd207addc5ec alsa: Implemented hotplug support, cleaned up device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10282
diff changeset
   784
bd207addc5ec alsa: Implemented hotplug support, cleaned up device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10282
diff changeset
   785
            ALSA_snd_device_name_free_hint(hints);
bd207addc5ec alsa: Implemented hotplug support, cleaned up device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10282
diff changeset
   786
bd207addc5ec alsa: Implemented hotplug support, cleaned up device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10282
diff changeset
   787
            devices = seen;   /* now we have a known-good list of attached devices. */
bd207addc5ec alsa: Implemented hotplug support, cleaned up device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10282
diff changeset
   788
bd207addc5ec alsa: Implemented hotplug support, cleaned up device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10282
diff changeset
   789
            /* report anything still in unseen as removed. */
bd207addc5ec alsa: Implemented hotplug support, cleaned up device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10282
diff changeset
   790
            for (dev = unseen; dev; dev = next) {
bd207addc5ec alsa: Implemented hotplug support, cleaned up device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10282
diff changeset
   791
                /*printf("ALSA: removing %s device '%s'\n", dev->iscapture ? "capture" : "output", dev->name);*/
bd207addc5ec alsa: Implemented hotplug support, cleaned up device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10282
diff changeset
   792
                next = dev->next;
bd207addc5ec alsa: Implemented hotplug support, cleaned up device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10282
diff changeset
   793
                SDL_RemoveAudioDevice(dev->iscapture, dev->name);
bd207addc5ec alsa: Implemented hotplug support, cleaned up device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10282
diff changeset
   794
                SDL_free(dev->name);
bd207addc5ec alsa: Implemented hotplug support, cleaned up device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10282
diff changeset
   795
                SDL_free(dev);
bd207addc5ec alsa: Implemented hotplug support, cleaned up device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10282
diff changeset
   796
            }
bd207addc5ec alsa: Implemented hotplug support, cleaned up device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10282
diff changeset
   797
        }
bd207addc5ec alsa: Implemented hotplug support, cleaned up device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10282
diff changeset
   798
bd207addc5ec alsa: Implemented hotplug support, cleaned up device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10282
diff changeset
   799
        /* On first run, tell ALSA_DetectDevices() that we have a complete device list so it can return. */
bd207addc5ec alsa: Implemented hotplug support, cleaned up device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10282
diff changeset
   800
        if (first_run_semaphore) {
bd207addc5ec alsa: Implemented hotplug support, cleaned up device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10282
diff changeset
   801
            SDL_SemPost(first_run_semaphore);
bd207addc5ec alsa: Implemented hotplug support, cleaned up device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10282
diff changeset
   802
            first_run_semaphore = NULL;  /* let other thread clean it up. */
bd207addc5ec alsa: Implemented hotplug support, cleaned up device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10282
diff changeset
   803
        }
bd207addc5ec alsa: Implemented hotplug support, cleaned up device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10282
diff changeset
   804
bd207addc5ec alsa: Implemented hotplug support, cleaned up device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10282
diff changeset
   805
        /* Block awhile before checking again, unless we're told to stop. */
bd207addc5ec alsa: Implemented hotplug support, cleaned up device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10282
diff changeset
   806
        ticks = SDL_GetTicks() + 5000;
bd207addc5ec alsa: Implemented hotplug support, cleaned up device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10282
diff changeset
   807
        while (!SDL_AtomicGet(&ALSA_hotplug_shutdown) && !SDL_TICKS_PASSED(SDL_GetTicks(), ticks))
bd207addc5ec alsa: Implemented hotplug support, cleaned up device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10282
diff changeset
   808
            SDL_Delay(100);
bd207addc5ec alsa: Implemented hotplug support, cleaned up device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10282
diff changeset
   809
        }
10104
4cd67316b308 alsa: Implemented basic device detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   810
    }
4cd67316b308 alsa: Implemented basic device detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   811
10284
bd207addc5ec alsa: Implemented hotplug support, cleaned up device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10282
diff changeset
   812
    /* Shutting down! Clean up any data we've gathered. */
bd207addc5ec alsa: Implemented hotplug support, cleaned up device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10282
diff changeset
   813
    for (dev = devices; dev; dev = next) {
bd207addc5ec alsa: Implemented hotplug support, cleaned up device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10282
diff changeset
   814
        /*printf("ALSA: at shutdown, removing %s device '%s'\n", dev->iscapture ? "capture" : "output", dev->name);*/
bd207addc5ec alsa: Implemented hotplug support, cleaned up device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10282
diff changeset
   815
        next = dev->next;
bd207addc5ec alsa: Implemented hotplug support, cleaned up device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10282
diff changeset
   816
        SDL_free(dev->name);
bd207addc5ec alsa: Implemented hotplug support, cleaned up device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10282
diff changeset
   817
        SDL_free(dev);
10104
4cd67316b308 alsa: Implemented basic device detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   818
    }
4cd67316b308 alsa: Implemented basic device detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   819
10284
bd207addc5ec alsa: Implemented hotplug support, cleaned up device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10282
diff changeset
   820
    return 0;
10104
4cd67316b308 alsa: Implemented basic device detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   821
}
4cd67316b308 alsa: Implemented basic device detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   822
4cd67316b308 alsa: Implemented basic device detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   823
static void
4cd67316b308 alsa: Implemented basic device detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   824
ALSA_DetectDevices(void)
4cd67316b308 alsa: Implemented basic device detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   825
{
10284
bd207addc5ec alsa: Implemented hotplug support, cleaned up device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10282
diff changeset
   826
    /* Start the device detection thread here, wait for an initial iteration to complete. */
bd207addc5ec alsa: Implemented hotplug support, cleaned up device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10282
diff changeset
   827
    SDL_sem *semaphore = SDL_CreateSemaphore(0);
bd207addc5ec alsa: Implemented hotplug support, cleaned up device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10282
diff changeset
   828
    if (!semaphore) {
10104
4cd67316b308 alsa: Implemented basic device detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   829
        return;  /* oh well. */
4cd67316b308 alsa: Implemented basic device detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   830
    }
4cd67316b308 alsa: Implemented basic device detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   831
10284
bd207addc5ec alsa: Implemented hotplug support, cleaned up device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10282
diff changeset
   832
    SDL_AtomicSet(&ALSA_hotplug_shutdown, 0);
10104
4cd67316b308 alsa: Implemented basic device detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   833
10284
bd207addc5ec alsa: Implemented hotplug support, cleaned up device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10282
diff changeset
   834
    ALSA_hotplug_thread = SDL_CreateThread(ALSA_HotplugThread, "SDLHotplugALSA", semaphore);
bd207addc5ec alsa: Implemented hotplug support, cleaned up device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10282
diff changeset
   835
    if (ALSA_hotplug_thread) {
bd207addc5ec alsa: Implemented hotplug support, cleaned up device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10282
diff changeset
   836
        SDL_SemWait(semaphore);  /* wait for the first iteration to finish. */
10104
4cd67316b308 alsa: Implemented basic device detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   837
    }
4cd67316b308 alsa: Implemented basic device detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   838
10284
bd207addc5ec alsa: Implemented hotplug support, cleaned up device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10282
diff changeset
   839
    SDL_DestroySemaphore(semaphore);
10104
4cd67316b308 alsa: Implemented basic device detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   840
}
4cd67316b308 alsa: Implemented basic device detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   841
4cd67316b308 alsa: Implemented basic device detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   842
static void
10284
bd207addc5ec alsa: Implemented hotplug support, cleaned up device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10282
diff changeset
   843
ALSA_Deinitialize(void)
10104
4cd67316b308 alsa: Implemented basic device detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   844
{
10284
bd207addc5ec alsa: Implemented hotplug support, cleaned up device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10282
diff changeset
   845
    if (ALSA_hotplug_thread != NULL) {
bd207addc5ec alsa: Implemented hotplug support, cleaned up device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10282
diff changeset
   846
        SDL_AtomicSet(&ALSA_hotplug_shutdown, 1);
bd207addc5ec alsa: Implemented hotplug support, cleaned up device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10282
diff changeset
   847
        SDL_WaitThread(ALSA_hotplug_thread, NULL);
bd207addc5ec alsa: Implemented hotplug support, cleaned up device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10282
diff changeset
   848
        ALSA_hotplug_thread = NULL;
bd207addc5ec alsa: Implemented hotplug support, cleaned up device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10282
diff changeset
   849
    }
bd207addc5ec alsa: Implemented hotplug support, cleaned up device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10282
diff changeset
   850
bd207addc5ec alsa: Implemented hotplug support, cleaned up device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10282
diff changeset
   851
    UnloadALSALibrary();
10104
4cd67316b308 alsa: Implemented basic device detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   852
}
4cd67316b308 alsa: Implemented basic device detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   853
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   854
static int
2060
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2049
diff changeset
   855
ALSA_Init(SDL_AudioDriverImpl * impl)
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   856
{
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   857
    if (LoadALSALibrary() < 0) {
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   858
        return 0;
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   859
    }
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   860
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   861
    /* Set the function pointers */
10104
4cd67316b308 alsa: Implemented basic device detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   862
    impl->DetectDevices = ALSA_DetectDevices;
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   863
    impl->OpenDevice = ALSA_OpenDevice;
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   864
    impl->WaitDevice = ALSA_WaitDevice;
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   865
    impl->GetDeviceBuf = ALSA_GetDeviceBuf;
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   866
    impl->PlayDevice = ALSA_PlayDevice;
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   867
    impl->CloseDevice = ALSA_CloseDevice;
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   868
    impl->Deinitialize = ALSA_Deinitialize;
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   869
3699
4160ba33b597 Removed test for "driver is valid, but doesn't see any audio devices."
Ryan C. Gordon <icculus@icculus.org>
parents: 3697
diff changeset
   870
    return 1;   /* this audio target is available. */
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   871
}
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   872
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   873
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   874
AudioBootStrap ALSA_bootstrap = {
5594
e741303e08a8 Removed needless macros in various audio targets.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   875
    "alsa", "ALSA PCM audio", ALSA_Init, 0
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   876
};
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   877
6044
35448a5ea044 Lots of fixes importing SDL source wholesale into a new iOS project
Sam Lantinga <slouken@libsdl.org>
parents: 5622
diff changeset
   878
#endif /* SDL_AUDIO_DRIVER_ALSA */
35448a5ea044 Lots of fixes importing SDL source wholesale into a new iOS project
Sam Lantinga <slouken@libsdl.org>
parents: 5622
diff changeset
   879
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   880
/* vi: set ts=4 sw=4 expandtab: */