Path: blob/master/src/java.desktop/share/native/libjsound/MidiInDevice.c
41149 views
/*1* Copyright (c) 1999, 2007, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425/*****************************************************************************/26/*27** Native functions for interfacing Java with the native implementation28** of PlatformMidi.h's functions.29*/30/*****************************************************************************/3132#define USE_ERROR33#define USE_TRACE343536#include <jni.h>37/* for memcpy */38#include <string.h>39#include "SoundDefs.h"40#include "PlatformMidi.h"41#include "com_sun_media_sound_MidiInDevice.h"424344JNIEXPORT jlong JNICALL45Java_com_sun_media_sound_MidiInDevice_nOpen(JNIEnv* e, jobject thisObj, jint index) {4647MidiDeviceHandle* deviceHandle = NULL;48INT32 err = MIDI_NOT_SUPPORTED;4950TRACE1("> Java_com_sun_media_sound_MidiInDevice_nOpen: index: %d\n", index);5152#if USE_PLATFORM_MIDI_IN == TRUE53err = MIDI_IN_OpenDevice((INT32) index, &deviceHandle);54#endif5556/* $$mp 2003-08-28:57So far, the return value (err) hasn't been taken into account.58Now, it is also expected to be MIDI_SUCCESS (0).59This works for Linux, but has to be checked on other platforms.6061It would be better to settle on one method of signaling error:62either returned error codes or a NULL handle. If the latter is used,63the return value should be removed from the signature of64MIDI_IN_OpenDevice.65*/66// if we didn't get a valid handle, throw a MidiUnavailableException67if (!deviceHandle || err != MIDI_SUCCESS) {68deviceHandle = NULL;69ERROR0("Java_com_sun_media_sound_MidiInDevice_nOpen: ");70ThrowJavaMessageException(e, JAVA_MIDI_PACKAGE_NAME"/MidiUnavailableException",71MIDI_IN_InternalGetErrorString(err));72} else {73TRACE0("< Java_com_sun_media_sound_MidiInDevice_nOpen succeeded\n");74}75return (jlong) (UINT_PTR) deviceHandle;76}777879JNIEXPORT void JNICALL80Java_com_sun_media_sound_MidiInDevice_nClose(JNIEnv* e, jobject thisObj, jlong deviceHandle) {8182TRACE0("> Java_com_sun_media_sound_MidiInDevice_nClose.\n");8384#if USE_PLATFORM_MIDI_IN == TRUE85MIDI_IN_CloseDevice((MidiDeviceHandle*) (UINT_PTR) deviceHandle);86#endif8788TRACE0("< Java_com_sun_media_sound_MidiInDevice_nClose succeeded\n");89}909192JNIEXPORT void JNICALL93Java_com_sun_media_sound_MidiInDevice_nStart(JNIEnv* e, jobject thisObj, jlong deviceHandle) {9495INT32 err = MIDI_NOT_SUPPORTED;9697TRACE0("> Java_com_sun_media_sound_MidiInDevice_nStart.\n");9899#if USE_PLATFORM_MIDI_IN == TRUE100err = MIDI_IN_StartDevice((MidiDeviceHandle*) (UINT_PTR) deviceHandle);101#endif102103if (err != MIDI_SUCCESS) {104ERROR0("Java_com_sun_media_sound_MidiInDevice_nStart: ");105ThrowJavaMessageException(e, JAVA_MIDI_PACKAGE_NAME"/MidiUnavailableException",106MIDI_IN_InternalGetErrorString(err));107} else {108TRACE0("< Java_com_sun_media_sound_MidiInDevice_nStart succeeded\n");109}110}111112113JNIEXPORT void JNICALL114Java_com_sun_media_sound_MidiInDevice_nStop(JNIEnv* e, jobject thisObj, jlong deviceHandle) {115116TRACE0("> Java_com_sun_media_sound_MidiInDevice_nStop.\n");117118#if USE_PLATFORM_MIDI_IN == TRUE119// stop the device and remove all queued events for this device handle120MIDI_IN_StopDevice((MidiDeviceHandle*) (UINT_PTR) deviceHandle);121#endif122123TRACE0("< Java_com_sun_media_sound_MidiInDevice_nStop succeeded\n");124}125126JNIEXPORT jlong JNICALL127Java_com_sun_media_sound_MidiInDevice_nGetTimeStamp(JNIEnv* e, jobject thisObj, jlong deviceHandle) {128129jlong ret = -1;130131TRACE0("Java_com_sun_media_sound_MidiInDevice_nGetTimeStamp.\n");132133#if USE_PLATFORM_MIDI_IN == TRUE134ret = (jlong) MIDI_IN_GetTimeStamp((MidiDeviceHandle*) (UINT_PTR) deviceHandle);135#endif136137/* Handle error codes. */138if (ret < -1) {139ERROR1("Java_com_sun_media_sound_MidiInDevice_nGetTimeStamp: MIDI_IN_GetTimeStamp returned %lld\n", ret);140ret = -1;141}142return ret;143}144145146JNIEXPORT void JNICALL147Java_com_sun_media_sound_MidiInDevice_nGetMessages(JNIEnv* e, jobject thisObj, jlong deviceHandle) {148149#if USE_PLATFORM_MIDI_IN == TRUE150MidiMessage* pMessage;151jclass javaClass = NULL;152jmethodID callbackShortMessageMethodID = NULL;153jmethodID callbackLongMessageMethodID = NULL;154#endif155156TRACE0("> Java_com_sun_media_sound_MidiInDevice_nGetMessages\n");157158#if USE_PLATFORM_MIDI_IN == TRUE159while ((pMessage = MIDI_IN_GetMessage((MidiDeviceHandle*) (UINT_PTR) deviceHandle))) {160if ((javaClass == NULL) || (callbackShortMessageMethodID == NULL)) {161if (!thisObj) {162ERROR0("MidiInDevice: Java_com_sun_media_sound_MidiInDevice_nGetMessages: thisObj is NULL\n");163return;164}165166if (javaClass == NULL) {167javaClass = (*e)->GetObjectClass(e, thisObj);168if (javaClass == NULL) {169ERROR0("MidiInDevice: Java_com_sun_media_sound_MidiInDevice_nGetMessages: javaClass is NULL\n");170return;171}172}173174if (callbackShortMessageMethodID == NULL) {175// save the callbackShortMessage callback method id.176// this is valid as long as the class is not unloaded.177callbackShortMessageMethodID = (*e)->GetMethodID(e, javaClass, "callbackShortMessage", "(IJ)V");178if (callbackShortMessageMethodID == 0) {179ERROR0("MidiInDevice: Java_com_sun_media_sound_MidiInDevice_nGetMessages: callbackShortMessageMethodID is 0\n");180return;181}182}183if (callbackLongMessageMethodID == NULL) {184// save the callbackLongMessage callback method id.185// this is valid as long as the class is not unloaded.186callbackLongMessageMethodID = (*e)->GetMethodID(e, javaClass, "callbackLongMessage", "([BJ)V");187if (callbackLongMessageMethodID == 0) {188ERROR0("MidiInDevice: Java_com_sun_media_sound_MidiInDevice_nGetMessages: callbackLongMessageMethodID is 0\n");189return;190}191}192}193194switch ((int)pMessage->type) {195case SHORT_MESSAGE: {196jint msg = (jint)pMessage->data.s.packedMsg;197jlong ts = (jlong)pMessage->timestamp;198TRACE0("Java_com_sun_media_sound_MidiInDevice_nGetMessages: got SHORT_MESSAGE\n");199// now we can put this message object back in the queue200MIDI_IN_ReleaseMessage((MidiDeviceHandle*) (UINT_PTR) deviceHandle, pMessage);201// and notify Java space202(*e)->CallVoidMethod(e, thisObj, callbackShortMessageMethodID, msg, ts);203break;204}205206case LONG_MESSAGE: {207jlong ts = (jlong)pMessage->timestamp;208jbyteArray jData;209UBYTE* data;210int isSXCont = 0;211TRACE0("Java_com_sun_media_sound_MidiInDevice_nGetMessages: got LONG_MESSAGE\n");212if ((*(pMessage->data.l.data) != 0xF0)213&& (*(pMessage->data.l.data) != 0xF7)) {214// this is a continued sys ex message215// need to prepend 0xF7216isSXCont = 1;217}218jData = (*e)->NewByteArray(e, pMessage->data.l.size + isSXCont);219if (!jData) {220ERROR0("Java_com_sun_media_sound_MidiInDevice_nGetMessages: cannot create long byte array.\n");221break;222}223data = (UBYTE*) ((*e)->GetByteArrayElements(e, jData, NULL));224if (!data) {225ERROR0("MidiInDevice: Java_com_sun_media_sound_MidiInDevice_nGetMessages: array data is NULL\n");226break;227}228// finally copy the long message229memcpy(data + isSXCont, pMessage->data.l.data, pMessage->data.l.size);230231// now we can put this message object back in the queue232MIDI_IN_ReleaseMessage((MidiDeviceHandle*) (UINT_PTR) deviceHandle, pMessage);233234// if this is a patched continued sys ex message, prepend 0xF7235if (isSXCont) {236*data = 0xF7;237}238239// commit the byte array240(*e)->ReleaseByteArrayElements(e, jData, (jbyte*) data, (jint) 0);241242(*e)->CallVoidMethod(e, thisObj, callbackLongMessageMethodID, jData, ts);243// release local reference to array: not needed anymore.244(*e)->DeleteLocalRef(e, jData);245break;246}247248default:249// put this message object back in the queue250MIDI_IN_ReleaseMessage((MidiDeviceHandle*) (UINT_PTR) deviceHandle, pMessage);251ERROR1("Java_com_sun_media_sound_MidiInDevice_nGetMessages: got unsupported message, type %d\n", pMessage->type);252break;253} // switch254}255256#endif // USE_PLATFORM_MIDI_IN257258TRACE0("< Java_com_sun_media_sound_MidiInDevice_nGetMessages returning\n");259}260261262