src/java.desktop/linux/native/libjsound/PLATFORM_API_LinuxOS_ALSA_MidiIn.c
author neliasso
Mon, 01 Jul 2019 10:49:58 +0200
changeset 55536 8313c42345d5
parent 49289 148e29df1644
permissions -rw-r--r--
8226287: Make process_users_of_allocation handle gc barriers Reviewed-by: kvn, roland
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: 7790
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
#if USE_PLATFORM_MIDI_IN == TRUE
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
#include <alsa/asoundlib.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
#include "PlatformMidi.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
#include "PLATFORM_API_LinuxOS_ALSA_MidiUtils.h"
7790
a7b669e9b0f7 6842956: Assertion error in javax_sound on 64-bit RHEL 5.3 with 32-bit JDK
amenkov
parents: 5506
diff changeset
    35
#if defined(i586)
a7b669e9b0f7 6842956: Assertion error in javax_sound on 64-bit RHEL 5.3 with 32-bit JDK
amenkov
parents: 5506
diff changeset
    36
#include <sys/utsname.h>
a7b669e9b0f7 6842956: Assertion error in javax_sound on 64-bit RHEL 5.3 with 32-bit JDK
amenkov
parents: 5506
diff changeset
    37
#endif
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * Helper methods
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 inline UINT32 packMessage(int status, int data1, int data2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    return ((status & 0xFF) | ((data1 & 0xFF) << 8) | ((data2 & 0xFF) << 16));
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
static void setShortMessage(MidiMessage* message,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
                            int status, int data1, int data2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    message->type = SHORT_MESSAGE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    message->data.s.packedMsg = packMessage(status, data1, data2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
static void setRealtimeMessage(MidiMessage* message, int status) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    setShortMessage(message, status, 0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
static void set14bitMessage(MidiMessage* message, int status, int value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    TRACE3("14bit value: %d, lsb: %d, msb: %d\n", value, value & 0x7F, (value >> 7) & 0x7F);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    value &= 0x3FFF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    TRACE3("14bit value (2): %d, lsb: %d, msb: %d\n", value, value & 0x7F, (value >> 7) & 0x7F);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    setShortMessage(message, status,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
                    value & 0x7F,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
                    (value >> 7) & 0x7F);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * implementation of the platform-dependent
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * MIDI in functions declared in PlatformMidi.h
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
char* MIDI_IN_GetErrorStr(INT32 err) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    return (char*) getErrorStr(err);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
7790
a7b669e9b0f7 6842956: Assertion error in javax_sound on 64-bit RHEL 5.3 with 32-bit JDK
amenkov
parents: 5506
diff changeset
    79
INT32 MIDI_IN_GetNumDevices() {
a7b669e9b0f7 6842956: Assertion error in javax_sound on 64-bit RHEL 5.3 with 32-bit JDK
amenkov
parents: 5506
diff changeset
    80
/* Workaround for 6842956: 32bit app on 64bit linux
a7b669e9b0f7 6842956: Assertion error in javax_sound on 64-bit RHEL 5.3 with 32-bit JDK
amenkov
parents: 5506
diff changeset
    81
 * gets assertion failure trying to open midiIn ports.
a7b669e9b0f7 6842956: Assertion error in javax_sound on 64-bit RHEL 5.3 with 32-bit JDK
amenkov
parents: 5506
diff changeset
    82
 * Untill the issue is fixed in ALSA
a7b669e9b0f7 6842956: Assertion error in javax_sound on 64-bit RHEL 5.3 with 32-bit JDK
amenkov
parents: 5506
diff changeset
    83
 * (https://bugtrack.alsa-project.org/alsa-bug/view.php?id=4807)
a7b669e9b0f7 6842956: Assertion error in javax_sound on 64-bit RHEL 5.3 with 32-bit JDK
amenkov
parents: 5506
diff changeset
    84
 * report no midi in devices in the configuration.
a7b669e9b0f7 6842956: Assertion error in javax_sound on 64-bit RHEL 5.3 with 32-bit JDK
amenkov
parents: 5506
diff changeset
    85
 */
a7b669e9b0f7 6842956: Assertion error in javax_sound on 64-bit RHEL 5.3 with 32-bit JDK
amenkov
parents: 5506
diff changeset
    86
#if defined(i586)
a7b669e9b0f7 6842956: Assertion error in javax_sound on 64-bit RHEL 5.3 with 32-bit JDK
amenkov
parents: 5506
diff changeset
    87
    static int jre32onlinux64 = -1;
a7b669e9b0f7 6842956: Assertion error in javax_sound on 64-bit RHEL 5.3 with 32-bit JDK
amenkov
parents: 5506
diff changeset
    88
    if (jre32onlinux64 < 0) {
a7b669e9b0f7 6842956: Assertion error in javax_sound on 64-bit RHEL 5.3 with 32-bit JDK
amenkov
parents: 5506
diff changeset
    89
        jre32onlinux64 = 0;
a7b669e9b0f7 6842956: Assertion error in javax_sound on 64-bit RHEL 5.3 with 32-bit JDK
amenkov
parents: 5506
diff changeset
    90
        /* The workaround may be disabled setting "JAVASOUND_ENABLE_MIDIIN"
a7b669e9b0f7 6842956: Assertion error in javax_sound on 64-bit RHEL 5.3 with 32-bit JDK
amenkov
parents: 5506
diff changeset
    91
         * environment variable.
a7b669e9b0f7 6842956: Assertion error in javax_sound on 64-bit RHEL 5.3 with 32-bit JDK
amenkov
parents: 5506
diff changeset
    92
         */
a7b669e9b0f7 6842956: Assertion error in javax_sound on 64-bit RHEL 5.3 with 32-bit JDK
amenkov
parents: 5506
diff changeset
    93
        if (getenv("JAVASOUND_ENABLE_MIDIIN") == NULL) {
a7b669e9b0f7 6842956: Assertion error in javax_sound on 64-bit RHEL 5.3 with 32-bit JDK
amenkov
parents: 5506
diff changeset
    94
            struct utsname u;
a7b669e9b0f7 6842956: Assertion error in javax_sound on 64-bit RHEL 5.3 with 32-bit JDK
amenkov
parents: 5506
diff changeset
    95
            jre32onlinux64 = 0;
a7b669e9b0f7 6842956: Assertion error in javax_sound on 64-bit RHEL 5.3 with 32-bit JDK
amenkov
parents: 5506
diff changeset
    96
            if (uname(&u) == 0) {
a7b669e9b0f7 6842956: Assertion error in javax_sound on 64-bit RHEL 5.3 with 32-bit JDK
amenkov
parents: 5506
diff changeset
    97
                if (strstr(u.machine, "64") != NULL) {
a7b669e9b0f7 6842956: Assertion error in javax_sound on 64-bit RHEL 5.3 with 32-bit JDK
amenkov
parents: 5506
diff changeset
    98
                    TRACE0("jre32 on linux64 detected - report no midiIn devices\n");
a7b669e9b0f7 6842956: Assertion error in javax_sound on 64-bit RHEL 5.3 with 32-bit JDK
amenkov
parents: 5506
diff changeset
    99
                    jre32onlinux64 = 1;
a7b669e9b0f7 6842956: Assertion error in javax_sound on 64-bit RHEL 5.3 with 32-bit JDK
amenkov
parents: 5506
diff changeset
   100
                }
a7b669e9b0f7 6842956: Assertion error in javax_sound on 64-bit RHEL 5.3 with 32-bit JDK
amenkov
parents: 5506
diff changeset
   101
            }
a7b669e9b0f7 6842956: Assertion error in javax_sound on 64-bit RHEL 5.3 with 32-bit JDK
amenkov
parents: 5506
diff changeset
   102
        }
a7b669e9b0f7 6842956: Assertion error in javax_sound on 64-bit RHEL 5.3 with 32-bit JDK
amenkov
parents: 5506
diff changeset
   103
    }
a7b669e9b0f7 6842956: Assertion error in javax_sound on 64-bit RHEL 5.3 with 32-bit JDK
amenkov
parents: 5506
diff changeset
   104
    if (jre32onlinux64) {
a7b669e9b0f7 6842956: Assertion error in javax_sound on 64-bit RHEL 5.3 with 32-bit JDK
amenkov
parents: 5506
diff changeset
   105
        return 0;
a7b669e9b0f7 6842956: Assertion error in javax_sound on 64-bit RHEL 5.3 with 32-bit JDK
amenkov
parents: 5506
diff changeset
   106
    }
a7b669e9b0f7 6842956: Assertion error in javax_sound on 64-bit RHEL 5.3 with 32-bit JDK
amenkov
parents: 5506
diff changeset
   107
#endif
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    TRACE0("MIDI_IN_GetNumDevices()\n");
7790
a7b669e9b0f7 6842956: Assertion error in javax_sound on 64-bit RHEL 5.3 with 32-bit JDK
amenkov
parents: 5506
diff changeset
   110
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    return getMidiDeviceCount(SND_RAWMIDI_STREAM_INPUT);
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
INT32 MIDI_IN_GetDeviceName(INT32 deviceIndex, char *name, UINT32 nameLength) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    int ret = getMidiDeviceName(SND_RAWMIDI_STREAM_INPUT, deviceIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                                name, nameLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
INT32 MIDI_IN_GetDeviceVendor(INT32 deviceIndex, char *name, UINT32 nameLength) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    int ret = getMidiDeviceVendor(deviceIndex, name, nameLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    return ret;
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
INT32 MIDI_IN_GetDeviceDescription(INT32 deviceIndex, char *name, UINT32 nameLength) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    int ret = getMidiDeviceDescription(SND_RAWMIDI_STREAM_INPUT, deviceIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                                       name, nameLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
INT32 MIDI_IN_GetDeviceVersion(INT32 deviceIndex, char *name, UINT32 nameLength) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    int ret = getMidiDeviceVersion(deviceIndex, name, nameLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
/*************************************************************************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
INT32 MIDI_IN_OpenDevice(INT32 deviceIndex, MidiDeviceHandle** handle) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    INT32 ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    TRACE0("> MIDI_IN_OpenDevice\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    ret = openMidiDevice(SND_RAWMIDI_STREAM_INPUT, deviceIndex, handle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    TRACE1("< MIDI_IN_OpenDevice: returning %d\n", (int) ret);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
INT32 MIDI_IN_CloseDevice(MidiDeviceHandle* handle) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    INT32 ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    TRACE0("> MIDI_IN_CloseDevice\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    ret = closeMidiDevice(handle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    TRACE1("< MIDI_IN_CloseDevice: returning %d\n", (int) ret);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
INT32 MIDI_IN_StartDevice(MidiDeviceHandle* handle) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    TRACE0("MIDI_IN_StartDevice\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    return MIDI_SUCCESS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
INT32 MIDI_IN_StopDevice(MidiDeviceHandle* handle) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    TRACE0("MIDI_IN_StopDevice\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    return MIDI_SUCCESS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
INT64 MIDI_IN_GetTimeStamp(MidiDeviceHandle* handle) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    return getMidiTimestamp(handle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
/* read the next message from the queue */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
MidiMessage* MIDI_IN_GetMessage(MidiDeviceHandle* handle) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    snd_seq_event_t alsa_message;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    MidiMessage* jdk_message;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    int err;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    char buffer[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    int status;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    TRACE0("> MIDI_IN_GetMessage\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    if (!handle) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        ERROR0("< ERROR: MIDI_IN_GetMessage(): handle is NULL\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    if (!handle->deviceHandle) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        ERROR0("< ERROR: MIDI_IN_GetMessage(): native handle is NULL\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    if (!handle->platformData) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        ERROR0("< ERROR: MIDI_IN_GetMessage(): platformData is NULL\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    /* For MIDI In, the device is left in non blocking mode. So if there is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
       no data from the device, snd_rawmidi_read() returns with -11 (EAGAIN).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
       This results in jumping back to the Java layer. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    while (TRUE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        TRACE0("before snd_rawmidi_read()\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        err = snd_rawmidi_read((snd_rawmidi_t*) handle->deviceHandle, buffer, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        TRACE0("after snd_rawmidi_read()\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        if (err != 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            ERROR2("< ERROR: MIDI_IN_GetMessage(): snd_rawmidi_read() returned %d : %s\n", err, snd_strerror(err));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        // printf("received byte: %d\n", buffer[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        err = snd_midi_event_encode_byte((snd_midi_event_t*) handle->platformData,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                                         (int) buffer[0],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                                         &alsa_message);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        if (err == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        } else if (err < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            ERROR1("< ERROR: MIDI_IN_GetMessage(): snd_midi_event_encode_byte() returned %d\n", err);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    jdk_message = (MidiMessage*) calloc(sizeof(MidiMessage), 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    if (!jdk_message) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        ERROR0("< ERROR: MIDI_IN_GetMessage(): out of memory\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    // TODO: tra
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    switch (alsa_message.type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    case SND_SEQ_EVENT_NOTEON:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    case SND_SEQ_EVENT_NOTEOFF:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    case SND_SEQ_EVENT_KEYPRESS:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        status = (alsa_message.type == SND_SEQ_EVENT_KEYPRESS) ? 0xA0 :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            (alsa_message.type == SND_SEQ_EVENT_NOTEON) ? 0x90 : 0x80;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        status |= alsa_message.data.note.channel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        setShortMessage(jdk_message, status,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                        alsa_message.data.note.note,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                        alsa_message.data.note.velocity);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    case SND_SEQ_EVENT_CONTROLLER:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        status = 0xB0 | alsa_message.data.control.channel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        setShortMessage(jdk_message, status,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                        alsa_message.data.control.param,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                        alsa_message.data.control.value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    case SND_SEQ_EVENT_PGMCHANGE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    case SND_SEQ_EVENT_CHANPRESS:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        status = (alsa_message.type == SND_SEQ_EVENT_PGMCHANGE) ? 0xC0 : 0xD0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        status |= alsa_message.data.control.channel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        setShortMessage(jdk_message, status,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                        alsa_message.data.control.value, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    case SND_SEQ_EVENT_PITCHBEND:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        status = 0xE0 | alsa_message.data.control.channel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        // $$mp 2003-09-23:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        // possible hack to work around a bug in ALSA. Necessary for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        // ALSA 0.9.2. May be fixed in newer versions of ALSA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        // alsa_message.data.control.value ^= 0x2000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        // TRACE1("pitchbend value: %d\n", alsa_message.data.control.value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        set14bitMessage(jdk_message, status,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                        alsa_message.data.control.value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        /* System exclusive messages */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    case SND_SEQ_EVENT_SYSEX:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        jdk_message->type = LONG_MESSAGE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        jdk_message->data.l.size = alsa_message.data.ext.len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        jdk_message->data.l.data = malloc(alsa_message.data.ext.len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        if (jdk_message->data.l.data == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            ERROR0("< ERROR: MIDI_IN_GetMessage(): out of memory\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            free(jdk_message);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            jdk_message = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            memcpy(jdk_message->data.l.data, alsa_message.data.ext.ptr, alsa_message.data.ext.len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        /* System common messages */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    case SND_SEQ_EVENT_QFRAME:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        setShortMessage(jdk_message, 0xF1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                        alsa_message.data.control.value & 0x7F, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    case SND_SEQ_EVENT_SONGPOS:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        set14bitMessage(jdk_message, 0xF2,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                        alsa_message.data.control.value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    case SND_SEQ_EVENT_SONGSEL:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        setShortMessage(jdk_message, 0xF3,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                        alsa_message.data.control.value & 0x7F, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    case SND_SEQ_EVENT_TUNE_REQUEST:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        setRealtimeMessage(jdk_message, 0xF6);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        /* System realtime messages */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    case SND_SEQ_EVENT_CLOCK:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        setRealtimeMessage(jdk_message, 0xF8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    case SND_SEQ_EVENT_START:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        setRealtimeMessage(jdk_message, 0xFA);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    case SND_SEQ_EVENT_CONTINUE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        setRealtimeMessage(jdk_message, 0xFB);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    case SND_SEQ_EVENT_STOP:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        setRealtimeMessage(jdk_message, 0xFC);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    case SND_SEQ_EVENT_SENSING:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        setRealtimeMessage(jdk_message, 0xFE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    case SND_SEQ_EVENT_RESET:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        setRealtimeMessage(jdk_message, 0xFF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
    default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        ERROR0("< ERROR: MIDI_IN_GetMessage(): unhandled ALSA MIDI message type\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        free(jdk_message);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        jdk_message = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    // set timestamp
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
    if (jdk_message != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        jdk_message->timestamp = getMidiTimestamp(handle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    TRACE1("< MIDI_IN_GetMessage: returning %p\n", jdk_message);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    return jdk_message;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
void MIDI_IN_ReleaseMessage(MidiDeviceHandle* handle, MidiMessage* msg) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
    if (!msg) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        ERROR0("< ERROR: MIDI_IN_ReleaseMessage(): message is NULL\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
    if (msg->type == LONG_MESSAGE && msg->data.l.data) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        free(msg->data.l.data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
    free(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
#endif /* USE_PLATFORM_MIDI_IN */