jdk/src/solaris/native/com/sun/media/sound/PLATFORM_API_LinuxOS_ALSA_CommonUtils.c
author ohair
Wed, 06 Apr 2011 22:06:11 -0700
changeset 9035 1255eb81cc2f
parent 7792 612315e0949d
permissions -rw-r--r--
7033660: Update copyright year to 2011 on any files changed in 2011 Reviewed-by: dholmes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
9035
1255eb81cc2f 7033660: Update copyright year to 2011 on any files changed in 2011
ohair
parents: 7792
diff changeset
     2
 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
//#define USE_ERROR
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
//#define USE_TRACE
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
#include "PLATFORM_API_LinuxOS_ALSA_CommonUtils.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
static void alsaDebugOutput(const char *file, int line, const char *function, int err, const char *fmt, ...) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
#ifdef USE_ERROR
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
    va_list args;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
    va_start(args, fmt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
    printf("%s:%d function %s: error %d: %s\n", file, line, function, err, snd_strerror(err));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
    if (strlen(fmt) > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
        vprintf(fmt, args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    va_end(args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
static int alsa_inited = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
static int alsa_enumerate_pcm_subdevices = FALSE; // default: no
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
static int alsa_enumerate_midi_subdevices = FALSE; // default: no
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
void initAlsaSupport() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    char* enumerate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    if (!alsa_inited) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        alsa_inited = TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        snd_lib_error_set_handler(&alsaDebugOutput);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        enumerate = getenv(ENV_ENUMERATE_PCM_SUBDEVICES);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        if (enumerate != NULL && strlen(enumerate) > 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
            && (enumerate[0] != 'f')   // false
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
            && (enumerate[0] != 'F')   // False
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
            && (enumerate[0] != 'n')   // no
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
            && (enumerate[0] != 'N')) { // NO
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
            alsa_enumerate_pcm_subdevices = TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
#ifdef ALSA_MIDI_ENUMERATE_SUBDEVICES
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        alsa_enumerate_midi_subdevices = TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
/* if true (non-zero), ALSA sub devices should be listed as separate devices
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
int needEnumerateSubdevices(int isMidi) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    initAlsaSupport();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    return isMidi ? alsa_enumerate_midi_subdevices
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                  : alsa_enumerate_pcm_subdevices;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * deviceID contains packed card, device and subdevice numbers
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * each number takes 10 bits
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * "default" device has id == ALSA_DEFAULT_DEVICE_ID
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
UINT32 encodeDeviceID(int card, int device, int subdevice) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    return (((card & 0x3FF) << 20) | ((device & 0x3FF) << 10)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
           | (subdevice & 0x3FF)) + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
void decodeDeviceID(UINT32 deviceID, int* card, int* device, int* subdevice,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                    int isMidi) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    deviceID--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    *card = (deviceID >> 20) & 0x3FF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    *device = (deviceID >> 10) & 0x3FF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    if (needEnumerateSubdevices(isMidi)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        *subdevice = deviceID  & 0x3FF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        *subdevice = -1; // ALSA will choose any subdevices
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
void getDeviceString(char* buffer, int card, int device, int subdevice,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                     int usePlugHw, int isMidi) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    if (needEnumerateSubdevices(isMidi)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        sprintf(buffer, "%s:%d,%d,%d",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                        usePlugHw ? ALSA_PLUGHARDWARE : ALSA_HARDWARE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                        card, device, subdevice);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        sprintf(buffer, "%s:%d,%d",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                        usePlugHw ? ALSA_PLUGHARDWARE : ALSA_HARDWARE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                        card, device);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
void getDeviceStringFromDeviceID(char* buffer, UINT32 deviceID,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                                 int usePlugHw, int isMidi) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    int card, device, subdevice;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    if (deviceID == ALSA_DEFAULT_DEVICE_ID) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        strcpy(buffer, ALSA_DEFAULT_DEVICE_NAME);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        decodeDeviceID(deviceID, &card, &device, &subdevice, isMidi);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        getDeviceString(buffer, card, device, subdevice, usePlugHw, isMidi);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
static int hasGottenALSAVersion = FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
#define ALSAVersionString_LENGTH 200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
static char ALSAVersionString[ALSAVersionString_LENGTH];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
void getALSAVersion(char* buffer, int len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    if (!hasGottenALSAVersion) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        // get alsa version from proc interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        FILE* file;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        int curr, len, totalLen, inVersionString;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        file = fopen(ALSA_VERSION_PROC_FILE, "r");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        ALSAVersionString[0] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        if (file) {
7792
612315e0949d 6989702: sound native code compiler warnings
amenkov
parents: 5506
diff changeset
   140
            if (NULL != fgets(ALSAVersionString, ALSAVersionString_LENGTH, file)) {
612315e0949d 6989702: sound native code compiler warnings
amenkov
parents: 5506
diff changeset
   141
                // parse for version number
612315e0949d 6989702: sound native code compiler warnings
amenkov
parents: 5506
diff changeset
   142
                totalLen = strlen(ALSAVersionString);
612315e0949d 6989702: sound native code compiler warnings
amenkov
parents: 5506
diff changeset
   143
                inVersionString = FALSE;
612315e0949d 6989702: sound native code compiler warnings
amenkov
parents: 5506
diff changeset
   144
                len = 0;
612315e0949d 6989702: sound native code compiler warnings
amenkov
parents: 5506
diff changeset
   145
                curr = 0;
612315e0949d 6989702: sound native code compiler warnings
amenkov
parents: 5506
diff changeset
   146
                while (curr < totalLen) {
612315e0949d 6989702: sound native code compiler warnings
amenkov
parents: 5506
diff changeset
   147
                    if (!inVersionString) {
612315e0949d 6989702: sound native code compiler warnings
amenkov
parents: 5506
diff changeset
   148
                        // is this char the beginning of a version string ?
612315e0949d 6989702: sound native code compiler warnings
amenkov
parents: 5506
diff changeset
   149
                        if (ALSAVersionString[curr] >= '0'
612315e0949d 6989702: sound native code compiler warnings
amenkov
parents: 5506
diff changeset
   150
                            && ALSAVersionString[curr] <= '9') {
612315e0949d 6989702: sound native code compiler warnings
amenkov
parents: 5506
diff changeset
   151
                            inVersionString = TRUE;
612315e0949d 6989702: sound native code compiler warnings
amenkov
parents: 5506
diff changeset
   152
                        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                    }
7792
612315e0949d 6989702: sound native code compiler warnings
amenkov
parents: 5506
diff changeset
   154
                    if (inVersionString) {
612315e0949d 6989702: sound native code compiler warnings
amenkov
parents: 5506
diff changeset
   155
                        // the version string ends with white space
612315e0949d 6989702: sound native code compiler warnings
amenkov
parents: 5506
diff changeset
   156
                        if (ALSAVersionString[curr] <= 32) {
612315e0949d 6989702: sound native code compiler warnings
amenkov
parents: 5506
diff changeset
   157
                            break;
612315e0949d 6989702: sound native code compiler warnings
amenkov
parents: 5506
diff changeset
   158
                        }
612315e0949d 6989702: sound native code compiler warnings
amenkov
parents: 5506
diff changeset
   159
                        if (curr != len) {
612315e0949d 6989702: sound native code compiler warnings
amenkov
parents: 5506
diff changeset
   160
                            // copy this char to the beginning of the string
612315e0949d 6989702: sound native code compiler warnings
amenkov
parents: 5506
diff changeset
   161
                            ALSAVersionString[len] = ALSAVersionString[curr];
612315e0949d 6989702: sound native code compiler warnings
amenkov
parents: 5506
diff changeset
   162
                        }
612315e0949d 6989702: sound native code compiler warnings
amenkov
parents: 5506
diff changeset
   163
                        len++;
612315e0949d 6989702: sound native code compiler warnings
amenkov
parents: 5506
diff changeset
   164
                    }
612315e0949d 6989702: sound native code compiler warnings
amenkov
parents: 5506
diff changeset
   165
                    curr++;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                }
7792
612315e0949d 6989702: sound native code compiler warnings
amenkov
parents: 5506
diff changeset
   167
                // remove trailing dots
612315e0949d 6989702: sound native code compiler warnings
amenkov
parents: 5506
diff changeset
   168
                while ((len > 0) && (ALSAVersionString[len - 1] == '.')) {
612315e0949d 6989702: sound native code compiler warnings
amenkov
parents: 5506
diff changeset
   169
                    len--;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                }
7792
612315e0949d 6989702: sound native code compiler warnings
amenkov
parents: 5506
diff changeset
   171
                // null terminate
612315e0949d 6989702: sound native code compiler warnings
amenkov
parents: 5506
diff changeset
   172
                ALSAVersionString[len] = 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            }
7792
612315e0949d 6989702: sound native code compiler warnings
amenkov
parents: 5506
diff changeset
   174
            fclose(file);
612315e0949d 6989702: sound native code compiler warnings
amenkov
parents: 5506
diff changeset
   175
            hasGottenALSAVersion = TRUE;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    strncpy(buffer, ALSAVersionString, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
/* end */