src/java.desktop/linux/native/libjsound/PLATFORM_API_LinuxOS_ALSA_CommonUtils.c
author hannesw
Tue, 10 Sep 2019 15:30:23 +0200
changeset 58071 8a066d872553
parent 49289 148e29df1644
permissions -rw-r--r--
8230766: Changed message in IllegalMonitorStateException Reviewed-by: attila, jlaskey
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
33653
c1ee09fe3274 8136556: Add the ability to perform static builds of MacOSX x64 binaries
bobv
parents: 25859
diff changeset
     2
 * Copyright (c) 2003, 2015, 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
33653
c1ee09fe3274 8136556: Add the ability to perform static builds of MacOSX x64 binaries
bobv
parents: 25859
diff changeset
    47
/*
c1ee09fe3274 8136556: Add the ability to perform static builds of MacOSX x64 binaries
bobv
parents: 25859
diff changeset
    48
 * Declare library specific JNI_Onload entry if static build
c1ee09fe3274 8136556: Add the ability to perform static builds of MacOSX x64 binaries
bobv
parents: 25859
diff changeset
    49
 */
c1ee09fe3274 8136556: Add the ability to perform static builds of MacOSX x64 binaries
bobv
parents: 25859
diff changeset
    50
DEF_STATIC_JNI_OnLoad
c1ee09fe3274 8136556: Add the ability to perform static builds of MacOSX x64 binaries
bobv
parents: 25859
diff changeset
    51
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
void initAlsaSupport() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    char* enumerate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    if (!alsa_inited) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        alsa_inited = TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        snd_lib_error_set_handler(&alsaDebugOutput);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        enumerate = getenv(ENV_ENUMERATE_PCM_SUBDEVICES);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        if (enumerate != NULL && strlen(enumerate) > 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
            && (enumerate[0] != 'f')   // false
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
            && (enumerate[0] != 'F')   // False
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
            && (enumerate[0] != 'n')   // no
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
            && (enumerate[0] != 'N')) { // NO
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            alsa_enumerate_pcm_subdevices = TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
#ifdef ALSA_MIDI_ENUMERATE_SUBDEVICES
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        alsa_enumerate_midi_subdevices = TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
/* if true (non-zero), ALSA sub devices should be listed as separate devices
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
int needEnumerateSubdevices(int isMidi) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    initAlsaSupport();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    return isMidi ? alsa_enumerate_midi_subdevices
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                  : alsa_enumerate_pcm_subdevices;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * deviceID contains packed card, device and subdevice numbers
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * each number takes 10 bits
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * "default" device has id == ALSA_DEFAULT_DEVICE_ID
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
UINT32 encodeDeviceID(int card, int device, int subdevice) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    return (((card & 0x3FF) << 20) | ((device & 0x3FF) << 10)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
           | (subdevice & 0x3FF)) + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
void decodeDeviceID(UINT32 deviceID, int* card, int* device, int* subdevice,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                    int isMidi) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    deviceID--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    *card = (deviceID >> 20) & 0x3FF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    *device = (deviceID >> 10) & 0x3FF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    if (needEnumerateSubdevices(isMidi)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        *subdevice = deviceID  & 0x3FF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        *subdevice = -1; // ALSA will choose any subdevices
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
void getDeviceString(char* buffer, int card, int device, int subdevice,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                     int usePlugHw, int isMidi) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    if (needEnumerateSubdevices(isMidi)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        sprintf(buffer, "%s:%d,%d,%d",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                        usePlugHw ? ALSA_PLUGHARDWARE : ALSA_HARDWARE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                        card, device, subdevice);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        sprintf(buffer, "%s:%d,%d",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                        usePlugHw ? ALSA_PLUGHARDWARE : ALSA_HARDWARE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                        card, device);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
void getDeviceStringFromDeviceID(char* buffer, UINT32 deviceID,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                                 int usePlugHw, int isMidi) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    int card, device, subdevice;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    if (deviceID == ALSA_DEFAULT_DEVICE_ID) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        strcpy(buffer, ALSA_DEFAULT_DEVICE_NAME);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        decodeDeviceID(deviceID, &card, &device, &subdevice, isMidi);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        getDeviceString(buffer, card, device, subdevice, usePlugHw, isMidi);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
static int hasGottenALSAVersion = FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
#define ALSAVersionString_LENGTH 200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
static char ALSAVersionString[ALSAVersionString_LENGTH];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
void getALSAVersion(char* buffer, int len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    if (!hasGottenALSAVersion) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        // get alsa version from proc interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        FILE* file;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        int curr, len, totalLen, inVersionString;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        file = fopen(ALSA_VERSION_PROC_FILE, "r");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        ALSAVersionString[0] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        if (file) {
7792
612315e0949d 6989702: sound native code compiler warnings
amenkov
parents: 5506
diff changeset
   145
            if (NULL != fgets(ALSAVersionString, ALSAVersionString_LENGTH, file)) {
612315e0949d 6989702: sound native code compiler warnings
amenkov
parents: 5506
diff changeset
   146
                // parse for version number
612315e0949d 6989702: sound native code compiler warnings
amenkov
parents: 5506
diff changeset
   147
                totalLen = strlen(ALSAVersionString);
612315e0949d 6989702: sound native code compiler warnings
amenkov
parents: 5506
diff changeset
   148
                inVersionString = FALSE;
612315e0949d 6989702: sound native code compiler warnings
amenkov
parents: 5506
diff changeset
   149
                len = 0;
612315e0949d 6989702: sound native code compiler warnings
amenkov
parents: 5506
diff changeset
   150
                curr = 0;
612315e0949d 6989702: sound native code compiler warnings
amenkov
parents: 5506
diff changeset
   151
                while (curr < totalLen) {
612315e0949d 6989702: sound native code compiler warnings
amenkov
parents: 5506
diff changeset
   152
                    if (!inVersionString) {
612315e0949d 6989702: sound native code compiler warnings
amenkov
parents: 5506
diff changeset
   153
                        // is this char the beginning of a version string ?
612315e0949d 6989702: sound native code compiler warnings
amenkov
parents: 5506
diff changeset
   154
                        if (ALSAVersionString[curr] >= '0'
612315e0949d 6989702: sound native code compiler warnings
amenkov
parents: 5506
diff changeset
   155
                            && ALSAVersionString[curr] <= '9') {
612315e0949d 6989702: sound native code compiler warnings
amenkov
parents: 5506
diff changeset
   156
                            inVersionString = TRUE;
612315e0949d 6989702: sound native code compiler warnings
amenkov
parents: 5506
diff changeset
   157
                        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                    }
7792
612315e0949d 6989702: sound native code compiler warnings
amenkov
parents: 5506
diff changeset
   159
                    if (inVersionString) {
612315e0949d 6989702: sound native code compiler warnings
amenkov
parents: 5506
diff changeset
   160
                        // the version string ends with white space
612315e0949d 6989702: sound native code compiler warnings
amenkov
parents: 5506
diff changeset
   161
                        if (ALSAVersionString[curr] <= 32) {
612315e0949d 6989702: sound native code compiler warnings
amenkov
parents: 5506
diff changeset
   162
                            break;
612315e0949d 6989702: sound native code compiler warnings
amenkov
parents: 5506
diff changeset
   163
                        }
612315e0949d 6989702: sound native code compiler warnings
amenkov
parents: 5506
diff changeset
   164
                        if (curr != len) {
612315e0949d 6989702: sound native code compiler warnings
amenkov
parents: 5506
diff changeset
   165
                            // copy this char to the beginning of the string
612315e0949d 6989702: sound native code compiler warnings
amenkov
parents: 5506
diff changeset
   166
                            ALSAVersionString[len] = ALSAVersionString[curr];
612315e0949d 6989702: sound native code compiler warnings
amenkov
parents: 5506
diff changeset
   167
                        }
612315e0949d 6989702: sound native code compiler warnings
amenkov
parents: 5506
diff changeset
   168
                        len++;
612315e0949d 6989702: sound native code compiler warnings
amenkov
parents: 5506
diff changeset
   169
                    }
612315e0949d 6989702: sound native code compiler warnings
amenkov
parents: 5506
diff changeset
   170
                    curr++;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                }
7792
612315e0949d 6989702: sound native code compiler warnings
amenkov
parents: 5506
diff changeset
   172
                // remove trailing dots
612315e0949d 6989702: sound native code compiler warnings
amenkov
parents: 5506
diff changeset
   173
                while ((len > 0) && (ALSAVersionString[len - 1] == '.')) {
612315e0949d 6989702: sound native code compiler warnings
amenkov
parents: 5506
diff changeset
   174
                    len--;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                }
7792
612315e0949d 6989702: sound native code compiler warnings
amenkov
parents: 5506
diff changeset
   176
                // null terminate
612315e0949d 6989702: sound native code compiler warnings
amenkov
parents: 5506
diff changeset
   177
                ALSAVersionString[len] = 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            }
7792
612315e0949d 6989702: sound native code compiler warnings
amenkov
parents: 5506
diff changeset
   179
            fclose(file);
612315e0949d 6989702: sound native code compiler warnings
amenkov
parents: 5506
diff changeset
   180
            hasGottenALSAVersion = TRUE;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    strncpy(buffer, ALSAVersionString, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
/* end */