1 /*
2  * Copyright (c) 1999, 2020, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.  Oracle designates this
8  * particular file as subject to the "Classpath" exception as provided
9  * by Oracle in the LICENSE file that accompanied this code.
10  *
11  * This code is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14  * version 2 for more details (a copy is included in the LICENSE file that
15  * accompanied this code).
16  *
17  * You should have received a copy of the GNU General Public License version
18  * 2 along with this work; if not, write to the Free Software Foundation,
19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22  * or visit www.oracle.com if you need additional information or have any
23  * questions.
24  */
25 
26 /*****************************************************************************/
27 /*
28 **      Native functions for interfacing Java with the native implementation
29 **      of PlatformMidi.h's functions.
30 */
31 /*****************************************************************************/
32 
33 #define USE_ERROR
34 #define USE_TRACE
35 
36 
37 #include <jni.h>
38 #include "SoundDefs.h"
39 #include "PlatformMidi.h"
40 #include "Utilities.h"
41 #include "com_sun_media_sound_MidiOutDevice.h"
42 
43 
44 // NATIVE METHODS
45 
46 
47 JNIEXPORT jlong JNICALL
Java_com_sun_media_sound_MidiOutDevice_nOpen(JNIEnv * e,jobject thisObj,jint index)48 Java_com_sun_media_sound_MidiOutDevice_nOpen(JNIEnv* e, jobject thisObj, jint index) {
49 
50     void* deviceHandle = NULL;
51     INT32 err = MIDI_NOT_SUPPORTED;
52 
53     TRACE1("Java_com_sun_media_sound_MidiOutDevice_nOpen: index: %d\n", index);
54 
55 #if USE_PLATFORM_MIDI_OUT == TRUE
56     err = MIDI_OUT_OpenDevice((INT32) index, (MidiDeviceHandle**) (&deviceHandle));
57 #endif
58 
59     // if we didn't get a valid handle, throw a MidiUnavailableException
60     if (!deviceHandle) {
61         ERROR0("Java_com_sun_media_sound_MidiOutDevice_nOpen:");
62         ThrowJavaMessageException(e, JAVA_MIDI_PACKAGE_NAME"/MidiUnavailableException",
63                                   MIDI_OUT_InternalGetErrorString(err));
64     } else {
65         TRACE0("Java_com_sun_media_sound_MidiOutDevice_nOpen succeeded\n");
66     }
67     return (jlong) (INT_PTR) deviceHandle;
68 }
69 
70 
71 JNIEXPORT void JNICALL
Java_com_sun_media_sound_MidiOutDevice_nClose(JNIEnv * e,jobject thisObj,jlong deviceHandle)72 Java_com_sun_media_sound_MidiOutDevice_nClose(JNIEnv* e, jobject thisObj, jlong deviceHandle) {
73 
74     TRACE0("Java_com_sun_media_sound_MidiOutDevice_nClose.\n");
75 
76 #if USE_PLATFORM_MIDI_OUT == TRUE
77     MIDI_OUT_CloseDevice((MidiDeviceHandle*) (UINT_PTR) deviceHandle);
78 #endif
79 
80     TRACE0("Java_com_sun_media_sound_MidiOutDevice_nClose succeeded\n");
81 }
82 
83 
84 JNIEXPORT jlong JNICALL
Java_com_sun_media_sound_MidiOutDevice_nGetTimeStamp(JNIEnv * e,jobject thisObj,jlong deviceHandle)85 Java_com_sun_media_sound_MidiOutDevice_nGetTimeStamp(JNIEnv* e, jobject thisObj, jlong deviceHandle) {
86 
87     jlong ret = -1;
88 
89     TRACE0("Java_com_sun_media_sound_MidiOutDevice_nGetTimeStamp.\n");
90 
91 #if USE_PLATFORM_MIDI_OUT == TRUE
92     ret = (jlong) MIDI_OUT_GetTimeStamp((MidiDeviceHandle*) (UINT_PTR) deviceHandle);
93 #endif
94 
95     /* Handle error codes. */
96     if (ret < -1) {
97         ERROR1("Java_com_sun_media_sound_MidiOutDevice_nGetTimeStamp: MIDI_IN_GetTimeStamp returned %lld\n", ret);
98         ret = -1;
99     }
100     return ret;
101 }
102 
103 
104 JNIEXPORT void JNICALL
Java_com_sun_media_sound_MidiOutDevice_nSendShortMessage(JNIEnv * e,jobject thisObj,jlong deviceHandle,jint packedMsg,jlong timeStamp)105 Java_com_sun_media_sound_MidiOutDevice_nSendShortMessage(JNIEnv* e, jobject thisObj, jlong deviceHandle,
106                                                          jint packedMsg, jlong timeStamp) {
107 
108     TRACE0("Java_com_sun_media_sound_MidiOutDevice_nSendShortMessage.\n");
109 
110 #if USE_PLATFORM_MIDI_OUT == TRUE
111     MIDI_OUT_SendShortMessage((MidiDeviceHandle*) (UINT_PTR) deviceHandle,
112                               (UINT32) packedMsg, (UINT32)timeStamp);
113 #endif
114 
115     TRACE0("Java_com_sun_media_sound_MidiOutDevice_nSendShortMessage succeeded\n");
116 }
117 
118 
119 JNIEXPORT void JNICALL
Java_com_sun_media_sound_MidiOutDevice_nSendLongMessage(JNIEnv * e,jobject thisObj,jlong deviceHandle,jbyteArray jData,jint size,jlong timeStamp)120 Java_com_sun_media_sound_MidiOutDevice_nSendLongMessage(JNIEnv* e, jobject thisObj, jlong deviceHandle,
121                                                         jbyteArray jData, jint size, jlong timeStamp) {
122 #if USE_PLATFORM_MIDI_OUT == TRUE
123     UBYTE* data;
124 #endif
125 
126     TRACE0("Java_com_sun_media_sound_MidiOutDevice_nSendLongMessage.\n");
127 
128 #if USE_PLATFORM_MIDI_OUT == TRUE
129     data = (UBYTE*) ((*e)->GetByteArrayElements(e, jData, NULL));
130     if (!data) {
131         ERROR0("MidiOutDevice: Java_com_sun_media_sound_MidiOutDevice_nSendLongMessage: could not get array elements\n");
132         return;
133     }
134     /* "continuation" sysex messages start with F7 (instead of F0), but
135        are sent without the F7. */
136     if (data[0] == 0xF7 && size > 1) {
137         data++;
138         size--;
139     }
140     MIDI_OUT_SendLongMessage((MidiDeviceHandle*) (UINT_PTR) deviceHandle, data,
141                              (UINT32) size, (UINT32)timeStamp);
142     // release the byte array
143     (*e)->ReleaseByteArrayElements(e, jData, (jbyte*) data, JNI_ABORT);
144 #endif
145 
146     TRACE0("Java_com_sun_media_sound_MidiOutDevice_nSendLongMessage succeeded\n");
147 }
148