00001
00017 #include "CoreMidiClient.h"
00018
00020
00022
00023
00024 MIDI_CALLBACK_PARAMETERS *g_callbackParameters;
00025
00027
00029
00042 void notifyCallback(const MIDINotification *message, void *notifyRefCon) {
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058 if ( message->messageID == kMIDIMsgSetupChanged ) {
00059
00060 JNIEnv *env;
00061
00062
00063 MIDI_CALLBACK_PARAMETERS *callbackParameters = (MIDI_CALLBACK_PARAMETERS *) notifyRefCon;
00064
00065
00066 int attachResult = callbackParameters->jvm->AttachCurrentThreadAsDaemon((void**) &env, NULL);
00067
00068
00069
00070
00071
00072 if ( attachResult == JNI_OK) {
00073
00074
00075 env->CallVoidMethod(callbackParameters->object, callbackParameters->methodID,127);
00076
00077
00078 if ( env->ExceptionCheck() ) {
00079
00080 env->ExceptionDescribe();
00081
00082 }
00083
00084 } else {
00085
00086 ThrowException(env,CFSTR("Notify Callback"),attachResult);
00087
00088 }
00089
00090 }
00091
00092 }
00093
00095
00097
00114 JNIEXPORT jint JNICALL Java_uk_co_xfactorylibrarians_coremidi4j_CoreMidiClient_createClient(JNIEnv *env, jobject obj, jstring clientName) {
00115
00116 __block MIDIClientRef client;
00117 __block OSStatus status;
00118 __block CFStringRef cfClientName = CFStringCreateWithCString(NULL,env->GetStringUTFChars(clientName,0),kCFStringEncodingMacRoman);
00119
00120
00121 g_callbackParameters = (MIDI_CALLBACK_PARAMETERS *) malloc(sizeof(MIDI_CALLBACK_PARAMETERS));
00122
00123
00124 if ( g_callbackParameters == NULL ) {
00125
00126 ThrowException(env,CFSTR("MIDIClientCreate"),-1);
00127
00128 }
00129
00130
00131 g_callbackParameters->object = env->NewGlobalRef(obj);
00132 g_callbackParameters->methodID = env->GetMethodID(env->GetObjectClass(obj), "notifyCallback", "()V");
00133 jint result = env->GetJavaVM(&g_callbackParameters->jvm);
00134
00135
00136
00137
00138
00139
00140
00141 assert (result == JNI_OK);
00142
00143
00144 if ( pthread_main_np() ) {
00145
00146
00147 status = MIDIClientCreate(cfClientName, ¬ifyCallback, g_callbackParameters, &client);
00148
00149 } else {
00150
00151
00152 dispatch_sync(dispatch_get_main_queue(), ^{
00153
00154 status = MIDIClientCreate(cfClientName, ¬ifyCallback, g_callbackParameters, &client);
00155
00156 });
00157
00158 }
00159
00160
00161 if ( status != 0) {
00162
00163 ThrowException(env,CFSTR("MIDIClientCreate"),status);
00164
00165 }
00166
00167
00168 return client;
00169
00170 }
00171
00187 JNIEXPORT void JNICALL Java_uk_co_xfactorylibrarians_coremidi4j_CoreMidiClient_disposeClient(JNIEnv *env, jobject obj, jint clientReference) {
00188
00189 std::cout << "** disposeClient: ENV : " << env << std::endl;
00190
00191 OSStatus status;
00192
00193
00194 status = MIDIClientDispose(clientReference);
00195
00196
00197 env->DeleteGlobalRef(g_callbackParameters->object);
00198
00199
00200 if ( g_callbackParameters != NULL ) {
00201
00202 free(g_callbackParameters);
00203
00204 }
00205
00206
00207 if ( status != 0) {
00208
00209 ThrowException(env,CFSTR("MIDIClientDispose"),status);
00210
00211 }
00212
00213 }
00214