src/java.desktop/linux/native/libjsound/PLATFORM_API_LinuxOS_ALSA_MidiUtils.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
/*
23668
98a5be057aad 8008114: [parfait] False positive buffer overrun in jdk/src/solaris/native/com/sun/media/sound/PLATFORM_API_LinuxOS_ALSA_MidiUtils.c
serb
parents: 7792
diff changeset
     2
 * Copyright (c) 2003, 2014, 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_MidiUtils.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
#include "PLATFORM_API_LinuxOS_ALSA_CommonUtils.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
#include <string.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
#include <sys/time.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
static INT64 getTimeInMicroseconds() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
    struct timeval tv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
    gettimeofday(&tv, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
    return (tv.tv_sec * 1000000UL) + tv.tv_usec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
const char* getErrorStr(INT32 err) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
        return snd_strerror((int) err);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
// callback for iteration through devices
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
// returns TRUE if iteration should continue
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
typedef int (*DeviceIteratorPtr)(UINT32 deviceID,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
                                 snd_rawmidi_info_t* rawmidi_info,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
                                 snd_ctl_card_info_t* cardinfo,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
                                 void *userData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
// for each ALSA device, call iterator. userData is passed to the iterator
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
// returns total number of iterations
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
static int iterateRawmidiDevices(snd_rawmidi_stream_t direction,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
                                 DeviceIteratorPtr iterator,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
                                 void* userData) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    int count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    int subdeviceCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    int card, dev, subDev;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    char devname[16];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    int err;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    snd_ctl_t *handle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    snd_rawmidi_t *rawmidi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    snd_rawmidi_info_t *rawmidi_info;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    snd_ctl_card_info_t *card_info, *defcardinfo = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    UINT32 deviceID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    int doContinue = TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    snd_rawmidi_info_malloc(&rawmidi_info);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    snd_ctl_card_info_malloc(&card_info);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    // 1st try "default" device
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    if (direction == SND_RAWMIDI_STREAM_INPUT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        err = snd_rawmidi_open(&rawmidi, NULL, ALSA_DEFAULT_DEVICE_NAME,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                               SND_RAWMIDI_NONBLOCK);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    } else if (direction == SND_RAWMIDI_STREAM_OUTPUT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        err = snd_rawmidi_open(NULL, &rawmidi, ALSA_DEFAULT_DEVICE_NAME,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                               SND_RAWMIDI_NONBLOCK);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        ERROR0("ERROR: iterateRawmidiDevices(): direction is neither"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
               " SND_RAWMIDI_STREAM_INPUT nor SND_RAWMIDI_STREAM_OUTPUT\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        err = MIDI_INVALID_ARGUMENT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    if (err < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        ERROR1("ERROR: snd_rawmidi_open (\"default\"): %s\n",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
               snd_strerror(err));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        err = snd_rawmidi_info(rawmidi, rawmidi_info);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        snd_rawmidi_close(rawmidi);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        if (err < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            ERROR1("ERROR: snd_rawmidi_info (\"default\"): %s\n",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                    snd_strerror(err));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            // try to get card info
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            card = snd_rawmidi_info_get_card(rawmidi_info);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            if (card >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                sprintf(devname, ALSA_HARDWARE_CARD, card);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                if (snd_ctl_open(&handle, devname, SND_CTL_NONBLOCK) >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                    if (snd_ctl_card_info(handle, card_info) >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                        defcardinfo = card_info;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                    snd_ctl_close(handle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            // call calback function for the device
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            if (iterator != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                doContinue = (*iterator)(ALSA_DEFAULT_DEVICE_ID, rawmidi_info,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                                         defcardinfo, userData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            count++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    // iterate cards
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    card = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    TRACE0("testing for cards...\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    if (snd_card_next(&card) >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        TRACE1("Found card %d\n", card);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        while (doContinue && (card >= 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            sprintf(devname, ALSA_HARDWARE_CARD, card);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            TRACE1("Opening control for alsa rawmidi device \"%s\"...\n", devname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            err = snd_ctl_open(&handle, devname, SND_CTL_NONBLOCK);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            if (err < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                ERROR2("ERROR: snd_ctl_open, card=%d: %s\n", card, snd_strerror(err));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                TRACE0("snd_ctl_open() SUCCESS\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                err = snd_ctl_card_info(handle, card_info);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                if (err < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                    ERROR2("ERROR: snd_ctl_card_info, card=%d: %s\n", card, snd_strerror(err));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                    TRACE0("snd_ctl_card_info() SUCCESS\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                    dev = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                    while (doContinue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                        if (snd_ctl_rawmidi_next_device(handle, &dev) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                            ERROR0("snd_ctl_rawmidi_next_device\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                        TRACE0("snd_ctl_rawmidi_next_device() SUCCESS\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                        if (dev < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                        snd_rawmidi_info_set_device(rawmidi_info, dev);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                        snd_rawmidi_info_set_subdevice(rawmidi_info, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                        snd_rawmidi_info_set_stream(rawmidi_info, direction);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                        err = snd_ctl_rawmidi_info(handle, rawmidi_info);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                        TRACE0("after snd_ctl_rawmidi_info()\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                        if (err < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                            if (err != -ENOENT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                                ERROR2("ERROR: snd_ctl_rawmidi_info, card=%d: %s", card, snd_strerror(err));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                            TRACE0("snd_ctl_rawmidi_info() SUCCESS\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                            subdeviceCount = needEnumerateSubdevices(ALSA_RAWMIDI)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                                ? snd_rawmidi_info_get_subdevices_count(rawmidi_info)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                                : 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                            if (iterator!=NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                                for (subDev = 0; subDev < subdeviceCount; subDev++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                                    TRACE3("  Iterating %d,%d,%d\n", card, dev, subDev);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                                    deviceID = encodeDeviceID(card, dev, subDev);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                                    doContinue = (*iterator)(deviceID, rawmidi_info,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                                                             card_info, userData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                                    count++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                                    TRACE0("returned from iterator\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                                    if (!doContinue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                                count += subdeviceCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                    } // of while(doContinue)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                snd_ctl_close(handle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            if (snd_card_next(&card) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        ERROR0("No cards found!\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    snd_ctl_card_info_free(card_info);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    snd_rawmidi_info_free(rawmidi_info);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    return count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
int getMidiDeviceCount(snd_rawmidi_stream_t direction) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    int deviceCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    TRACE0("> getMidiDeviceCount()\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    initAlsaSupport();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    deviceCount = iterateRawmidiDevices(direction, NULL, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    TRACE0("< getMidiDeviceCount()\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    return deviceCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
  userData is assumed to be a pointer to ALSA_MIDIDeviceDescription.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
  ALSA_MIDIDeviceDescription->index has to be set to the index of the device
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
  we want to get information of before this method is called the first time via
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
  iterateRawmidiDevices(). On each call of this method,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
  ALSA_MIDIDeviceDescription->index is decremented. If it is equal to zero,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
  we have reached the desired device, so action is taken.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
  So after successful completion of iterateRawmidiDevices(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
  ALSA_MIDIDeviceDescription->index is zero. If it isn't, this is an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
  indication of an error.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
static int deviceInfoIterator(UINT32 deviceID, snd_rawmidi_info_t *rawmidi_info,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                              snd_ctl_card_info_t *cardinfo, void *userData) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    char buffer[300];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    ALSA_MIDIDeviceDescription* desc = (ALSA_MIDIDeviceDescription*)userData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
#ifdef ALSA_MIDI_USE_PLUGHW
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    int usePlugHw = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
#else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    int usePlugHw = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    TRACE0("deviceInfoIterator\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    initAlsaSupport();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    if (desc->index == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        // we found the device with correct index
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        desc->deviceID = deviceID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        buffer[0]=' '; buffer[1]='[';
23668
98a5be057aad 8008114: [parfait] False positive buffer overrun in jdk/src/solaris/native/com/sun/media/sound/PLATFORM_API_LinuxOS_ALSA_MidiUtils.c
serb
parents: 7792
diff changeset
   232
        // buffer[300] is enough to store the actual device string w/o overrun
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        getDeviceStringFromDeviceID(&buffer[2], deviceID, usePlugHw, ALSA_RAWMIDI);
23668
98a5be057aad 8008114: [parfait] False positive buffer overrun in jdk/src/solaris/native/com/sun/media/sound/PLATFORM_API_LinuxOS_ALSA_MidiUtils.c
serb
parents: 7792
diff changeset
   234
        strncat(buffer, "]", sizeof(buffer) - strlen(buffer) - 1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        strncpy(desc->name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                (cardinfo != NULL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                    ? snd_ctl_card_info_get_id(cardinfo)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                    : snd_rawmidi_info_get_id(rawmidi_info),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                desc->strLen - strlen(buffer));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        strncat(desc->name, buffer, desc->strLen - strlen(desc->name));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        desc->description[0] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        if (cardinfo != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            strncpy(desc->description, snd_ctl_card_info_get_name(cardinfo),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                    desc->strLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            strncat(desc->description, ", ",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                    desc->strLen - strlen(desc->description));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        strncat(desc->description, snd_rawmidi_info_get_id(rawmidi_info),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                desc->strLen - strlen(desc->description));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        strncat(desc->description, ", ", desc->strLen - strlen(desc->description));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        strncat(desc->description, snd_rawmidi_info_get_name(rawmidi_info),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                desc->strLen - strlen(desc->description));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        TRACE2("Returning %s, %s\n", desc->name, desc->description);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        return FALSE; // do not continue iteration
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    desc->index--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    return TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
static int getMIDIDeviceDescriptionByIndex(snd_rawmidi_stream_t direction,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                                           ALSA_MIDIDeviceDescription* desc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    initAlsaSupport();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    TRACE1(" getMIDIDeviceDescriptionByIndex (index = %d)\n", desc->index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    iterateRawmidiDevices(direction, &deviceInfoIterator, desc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    return (desc->index == 0) ? MIDI_SUCCESS : MIDI_INVALID_DEVICEID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
int initMIDIDeviceDescription(ALSA_MIDIDeviceDescription* desc, int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    int ret = MIDI_SUCCESS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    desc->index = index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    desc->strLen = 200;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    desc->name = (char*) calloc(desc->strLen + 1, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    desc->description = (char*) calloc(desc->strLen + 1, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    if (! desc->name ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        ! desc->description) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        ret = MIDI_OUT_OF_MEMORY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
void freeMIDIDeviceDescription(ALSA_MIDIDeviceDescription* desc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    if (desc->name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        free(desc->name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    if (desc->description) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        free(desc->description);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
int getMidiDeviceName(snd_rawmidi_stream_t direction, int index, char *name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                      UINT32 nameLength) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    ALSA_MIDIDeviceDescription desc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    int ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
    TRACE1("getMidiDeviceName: nameLength: %d\n", (int) nameLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
    ret = initMIDIDeviceDescription(&desc, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    if (ret == MIDI_SUCCESS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        TRACE0("getMidiDeviceName: initMIDIDeviceDescription() SUCCESS\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        ret = getMIDIDeviceDescriptionByIndex(direction, &desc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        if (ret == MIDI_SUCCESS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            TRACE1("getMidiDeviceName: desc.name: %s\n", desc.name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            strncpy(name, desc.name, nameLength - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            name[nameLength - 1] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    freeMIDIDeviceDescription(&desc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
    return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
int getMidiDeviceVendor(int index, char *name, UINT32 nameLength) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
    strncpy(name, ALSA_VENDOR, nameLength - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    name[nameLength - 1] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    return MIDI_SUCCESS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
int getMidiDeviceDescription(snd_rawmidi_stream_t direction,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                             int index, char *name, UINT32 nameLength) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    ALSA_MIDIDeviceDescription desc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
    int ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    ret = initMIDIDeviceDescription(&desc, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
    if (ret == MIDI_SUCCESS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        ret = getMIDIDeviceDescriptionByIndex(direction, &desc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        if (ret == MIDI_SUCCESS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            strncpy(name, desc.description, nameLength - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
            name[nameLength - 1] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
    freeMIDIDeviceDescription(&desc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
int getMidiDeviceVersion(int index, char *name, UINT32 nameLength) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
    getALSAVersion(name, nameLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
    return MIDI_SUCCESS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
static int getMidiDeviceID(snd_rawmidi_stream_t direction, int index,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                           UINT32* deviceID) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
    ALSA_MIDIDeviceDescription desc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
    int ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
    ret = initMIDIDeviceDescription(&desc, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
    if (ret == MIDI_SUCCESS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        ret = getMIDIDeviceDescriptionByIndex(direction, &desc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        if (ret == MIDI_SUCCESS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
            // TRACE1("getMidiDeviceName: desc.name: %s\n", desc.name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
            *deviceID = desc.deviceID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
    freeMIDIDeviceDescription(&desc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
    return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
  direction has to be either SND_RAWMIDI_STREAM_INPUT or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
  SND_RAWMIDI_STREAM_OUTPUT.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
  Returns 0 on success. Otherwise, MIDI_OUT_OF_MEMORY, MIDI_INVALID_ARGUMENT
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
   or a negative ALSA error code is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
INT32 openMidiDevice(snd_rawmidi_stream_t direction, INT32 deviceIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
                     MidiDeviceHandle** handle) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
    snd_rawmidi_t* native_handle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
    snd_midi_event_t* event_parser = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
    int err;
7792
612315e0949d 6989702: sound native code compiler warnings
amenkov
parents: 5506
diff changeset
   376
    UINT32 deviceID = 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
    char devicename[100];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
#ifdef ALSA_MIDI_USE_PLUGHW
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
    int usePlugHw = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
#else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
    int usePlugHw = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
    TRACE0("> openMidiDevice()\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
    (*handle) = (MidiDeviceHandle*) calloc(sizeof(MidiDeviceHandle), 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
    if (!(*handle)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        ERROR0("ERROR: openDevice: out of memory\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        return MIDI_OUT_OF_MEMORY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
    // TODO: iterate to get dev ID from index
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
    err = getMidiDeviceID(direction, deviceIndex, &deviceID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
    TRACE1("  openMidiDevice(): deviceID: %d\n", (int) deviceID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
    getDeviceStringFromDeviceID(devicename, deviceID,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
                                usePlugHw, ALSA_RAWMIDI);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
    TRACE1("  openMidiDevice(): deviceString: %s\n", devicename);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
    // finally open the device
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
    if (direction == SND_RAWMIDI_STREAM_INPUT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        err = snd_rawmidi_open(&native_handle, NULL, devicename,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
                               SND_RAWMIDI_NONBLOCK);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
    } else if (direction == SND_RAWMIDI_STREAM_OUTPUT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        err = snd_rawmidi_open(NULL, &native_handle, devicename,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
                               SND_RAWMIDI_NONBLOCK);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        ERROR0("  ERROR: openMidiDevice(): direction is neither SND_RAWMIDI_STREAM_INPUT nor SND_RAWMIDI_STREAM_OUTPUT\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        err = MIDI_INVALID_ARGUMENT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
    if (err < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        ERROR1("<  ERROR: openMidiDevice(): snd_rawmidi_open() returned %d\n", err);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        free(*handle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        (*handle) = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        return err;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
    /* We opened with non-blocking behaviour to not get hung if the device
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
       is used by a different process. Writing, however, should
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
       be blocking. So we change it here. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
    if (direction == SND_RAWMIDI_STREAM_OUTPUT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
        err = snd_rawmidi_nonblock(native_handle, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
        if (err < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
            ERROR1("  ERROR: openMidiDevice(): snd_rawmidi_nonblock() returned %d\n", err);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
            snd_rawmidi_close(native_handle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
            free(*handle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
            (*handle) = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
            return err;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
    if (direction == SND_RAWMIDI_STREAM_INPUT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        err = snd_midi_event_new(EVENT_PARSER_BUFSIZE, &event_parser);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        if (err < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
            ERROR1("  ERROR: openMidiDevice(): snd_midi_event_new() returned %d\n", err);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
            snd_rawmidi_close(native_handle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
            free(*handle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
            (*handle) = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
            return err;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
    (*handle)->deviceHandle = (void*) native_handle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
    (*handle)->startTime = getTimeInMicroseconds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
    (*handle)->platformData = event_parser;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
    TRACE0("< openMidiDevice(): succeeded\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
    return err;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
INT32 closeMidiDevice(MidiDeviceHandle* handle) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
    int err;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
    TRACE0("> closeMidiDevice()\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
    if (!handle) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
        ERROR0("< ERROR: closeMidiDevice(): handle is NULL\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
        return MIDI_INVALID_HANDLE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
    if (!handle->deviceHandle) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
        ERROR0("< ERROR: closeMidiDevice(): native handle is NULL\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
        return MIDI_INVALID_HANDLE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
    err = snd_rawmidi_close((snd_rawmidi_t*) handle->deviceHandle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
    TRACE1("  snd_rawmidi_close() returns %d\n", err);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
    if (handle->platformData) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
        snd_midi_event_free((snd_midi_event_t*) handle->platformData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
    free(handle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
    TRACE0("< closeMidiDevice: succeeded\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
    return err;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
INT64 getMidiTimestamp(MidiDeviceHandle* handle) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
    if (!handle) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
        ERROR0("< ERROR: closeMidiDevice(): handle is NULL\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
        return MIDI_INVALID_HANDLE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
    return getTimeInMicroseconds() - handle->startTime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
/* end */