summaryrefslogtreecommitdiffstatshomepage
path: root/3rdparty/portmidi/pm_java/pmjni/pmjni.c
blob: 95e51dda4c5f32248189bf509fc4c3b9d0ff7f12 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
#include "portmidi.h"
#include "porttime.h"
#include "jportmidi_JportMidiApi.h"
#include <stdio.h>

// these macros assume JNIEnv *env is declared and valid:
//
#define CLASS(c, obj) jclass c = (*env)->GetObjectClass(env, obj)
#define ADDRESS_FID(fid, c) \
    jfieldID fid = (*env)->GetFieldID(env, c, "address", "J")
// Uses Java Long (64-bit) to make sure there is room to store a 
// pointer. Cast this to a C long (either 32 or 64 bit) to match
// the size of a pointer. Finally cast int to pointer. All this
// is supposed to avoid C compiler warnings and (worse) losing
// address bits.
#define PMSTREAM(obj, fid) ((PmStream *) (long) (*env)->GetLongField(env, obj, fid))
// Cast stream to long to convert integer to pointer, then expand
// integer to 64-bit jlong. This avoids compiler warnings.
#define SET_PMSTREAM(obj, fid, stream) \
    (*env)->SetLongField(env, obj, fid, (jlong) (long) stream)


/*
 * Method:    Pm_Initialize
 */
JNIEXPORT jint JNICALL Java_jportmidi_JPortMidiApi_Pm_1Initialize
  (JNIEnv *env, jclass cl)
{
    return Pm_Initialize();
}


/*
 * Method:    Pm_Terminate
 */
JNIEXPORT jint JNICALL Java_jportmidi_JPortMidiApi_Pm_1Terminate
  (JNIEnv *env, jclass cl)
{
    return Pm_Terminate();
}


/*
 * Method:    Pm_HasHostError
 */
JNIEXPORT jint JNICALL Java_jportmidi_JPortMidiApi_Pm_1HasHostError
  (JNIEnv *env, jclass cl, jobject jstream)
{
    CLASS(c, jstream);
    ADDRESS_FID(fid, c);
    return Pm_HasHostError(PMSTREAM(jstream, fid));
}


/*
 * Method:    Pm_GetErrorText
 */
JNIEXPORT jstring JNICALL Java_jportmidi_JPortMidiApi_Pm_1GetErrorText
  (JNIEnv *env, jclass cl, jint i)
{
    return (*env)->NewStringUTF(env, Pm_GetErrorText(i));
}


/*
 * Method:    Pm_GetHostErrorText
 */
JNIEXPORT jstring JNICALL Java_jportmidi_JPortMidiApi_Pm_1GetHostErrorText
  (JNIEnv *env, jclass cl)
{
    char msg[PM_HOST_ERROR_MSG_LEN];
    Pm_GetHostErrorText(msg, PM_HOST_ERROR_MSG_LEN);
    return (*env)->NewStringUTF(env, msg);
}


/*
 * Method:    Pm_CountDevices
 */
JNIEXPORT jint JNICALL Java_jportmidi_JPortMidiApi_Pm_1CountDevices
  (JNIEnv *env, jclass cl)
{
    return Pm_CountDevices();
}


/*
 * Method:    Pm_GetDefaultInputDeviceID
 */
JNIEXPORT jint JNICALL Java_jportmidi_JPortMidiApi_Pm_1GetDefaultInputDeviceID
  (JNIEnv *env, jclass cl)
{
    return Pm_GetDefaultInputDeviceID();
}


/*
 * Method:    Pm_GetDefaultOutputDeviceID
 */
JNIEXPORT jint JNICALL Java_jportmidi_JPortMidiApi_Pm_1GetDefaultOutputDeviceID
  (JNIEnv *env, jclass cl)
{
    return Pm_GetDefaultOutputDeviceID();
}


/*
 * Method:    Pm_GetDeviceInterf
 */
JNIEXPORT jstring JNICALL Java_jportmidi_JPortMidiApi_Pm_1GetDeviceInterf
  (JNIEnv *env, jclass cl, jint i)
{
    const PmDeviceInfo *info = Pm_GetDeviceInfo(i);
    if (!info) return NULL;
    return (*env)->NewStringUTF(env, info->interf);
}


/*
 * Method:    Pm_GetDeviceName
 */
JNIEXPORT jstring JNICALL Java_jportmidi_JPortMidiApi_Pm_1GetDeviceName
  (JNIEnv *env, jclass cl, jint i)
{
    const PmDeviceInfo *info = Pm_GetDeviceInfo(i);
    if (!info) return NULL;
    return (*env)->NewStringUTF(env, info->name);
}


/*
 * Method:    Pm_GetDeviceInput
 */
JNIEXPORT jboolean JNICALL Java_jportmidi_JPortMidiApi_Pm_1GetDeviceInput
  (JNIEnv *env, jclass cl, jint i)
{
    const PmDeviceInfo *info = Pm_GetDeviceInfo(i);
    if (!info) return (jboolean) 0;
    return (jboolean) info->input;
}


/*
 * Method:    Pm_GetDeviceOutput
 */
JNIEXPORT jboolean JNICALL Java_jportmidi_JPortMidiApi_Pm_1GetDeviceOutput
  (JNIEnv *env, jclass cl, jint i)
{
    const PmDeviceInfo *info = Pm_GetDeviceInfo(i);
    if (!info) return (jboolean) 0;
    return (jboolean) info->output;
}


/*
 * Method:    Pm_OpenInput
 */
JNIEXPORT jint JNICALL Java_jportmidi_JPortMidiApi_Pm_1OpenInput
  (JNIEnv *env, jclass cl, 
   jobject jstream, jint index, jstring extras, jint bufsiz)
{
    PmError rslt;
    PortMidiStream *stream;
    CLASS(c, jstream);
    ADDRESS_FID(fid, c);
    rslt = Pm_OpenInput(&stream, index, NULL, bufsiz, NULL, NULL);
    SET_PMSTREAM(jstream, fid, stream);
    return rslt;
}


/*
 * Method:    Pm_OpenOutput
 */
JNIEXPORT jint JNICALL Java_jportmidi_JPortMidiApi_Pm_1OpenOutput
  (JNIEnv *env, jclass cl, jobject jstream, jint index, jstring extras,
   jint bufsiz, jint latency)
{
    PmError rslt;
    PortMidiStream *stream;
    CLASS(c, jstream);
    ADDRESS_FID(fid, c);
    rslt = Pm_OpenOutput(&stream, index, NULL, bufsiz, NULL, NULL, latency);
    SET_PMSTREAM(jstream, fid, stream);
    return rslt;
}


/*
 * Method:    Pm_SetFilter
 */
JNIEXPORT jint JNICALL Java_jportmidi_JPortMidiApi_Pm_1SetFilter
  (JNIEnv *env, jclass cl, jobject jstream, jint filters)
{
    CLASS(c, jstream);
    ADDRESS_FID(fid, c);
    return Pm_SetFilter(PMSTREAM(jstream, fid), filters);
}


/*
 * Method:    Pm_SetChannelMask
 */
JNIEXPORT jint JNICALL Java_jportmidi_JPortMidiApi_Pm_1SetChannelMask
  (JNIEnv *env, jclass cl, jobject jstream, jint mask)
{
    CLASS(c, jstream);
    ADDRESS_FID(fid, c);
    return Pm_SetChannelMask(PMSTREAM(jstream, fid), mask);
}


/*
 * Method:    Pm_Abort
 */
JNIEXPORT jint JNICALL Java_jportmidi_JPortMidiApi_Pm_1Abort
  (JNIEnv *env, jclass cl, jobject jstream)
{
    CLASS(c, jstream);
    ADDRESS_FID(fid, c);
    return Pm_Abort(PMSTREAM(jstream, fid));
}


/*
 * Method:    Pm_Close
 */
JNIEXPORT jint JNICALL Java_jportmidi_JPortMidiApi_Pm_1Close
  (JNIEnv *env, jclass cl, jobject jstream)
{
    CLASS(c, jstream);
    ADDRESS_FID(fid, c);
    return Pm_Close(PMSTREAM(jstream, fid));
}


/*
 * Method:    Pm_Read
 */
JNIEXPORT jint JNICALL Java_jportmidi_JPortMidiApi_Pm_1Read
  (JNIEnv *env, jclass cl, jobject jstream, jobject jpmevent)
{
    CLASS(jstream_class, jstream);
    ADDRESS_FID(address_fid, jstream_class);
    jclass jpmevent_class = (*env)->GetObjectClass(env, jpmevent);
    jfieldID message_fid = 
            (*env)->GetFieldID(env, jpmevent_class, "message", "I");
    jfieldID timestamp_fid = 
            (*env)->GetFieldID(env, jpmevent_class, "timestamp", "I");
    PmEvent buffer;
    PmError rslt = Pm_Read(PMSTREAM(jstream, address_fid), &buffer, 1);
    (*env)->SetIntField(env, jpmevent, message_fid, buffer.message);
    (*env)->SetIntField(env, jpmevent, timestamp_fid, buffer.timestamp);
    return rslt;
}


/*
 * Method:    Pm_Poll
 */
JNIEXPORT jint JNICALL Java_jportmidi_JPortMidiApi_Pm_1Poll
        (JNIEnv *env, jclass cl, jobject jstream)
{
    CLASS(c, jstream);
    ADDRESS_FID(fid, c);
    return Pm_Poll(PMSTREAM(jstream, fid));
}


/*
 * Method:    Pm_Write
 */
JNIEXPORT jint JNICALL Java_jportmidi_JPortMidiApi_Pm_1Write
        (JNIEnv *env, jclass cl, jobject jstream, jobject jpmevent)
{
    CLASS(jstream_class, jstream);
    ADDRESS_FID(address_fid, jstream_class);
    jclass jpmevent_class = (*env)->GetObjectClass(env, jpmevent);
    jfieldID message_fid = 
            (*env)->GetFieldID(env, jpmevent_class, "message", "I");
    jfieldID timestamp_fid = 
            (*env)->GetFieldID(env, jpmevent_class, "timestamp", "I");
    // note that we call WriteShort because it's simpler than constructing
    // a buffer and passing it to Pm_Write
    return Pm_WriteShort(PMSTREAM(jstream, address_fid),
            (*env)->GetIntField(env, jpmevent, timestamp_fid),
            (*env)->GetIntField(env, jpmevent, message_fid));
}


/*
 * Method:    Pm_WriteShort
 */
JNIEXPORT jint JNICALL Java_jportmidi_JPortMidiApi_Pm_1WriteShort
  (JNIEnv *env, jclass cl, jobject jstream, jint when, jint msg)
{
    CLASS(c, jstream);
    ADDRESS_FID(fid, c);
    return Pm_WriteShort(PMSTREAM(jstream, fid), when, msg);
}


/*
 * Method:    Pm_WriteSysEx
 */
JNIEXPORT jint JNICALL Java_jportmidi_JPortMidiApi_Pm_1WriteSysEx
  (JNIEnv *env, jclass cl, jobject jstream, jint when, jbyteArray jmsg)
{
    CLASS(c, jstream);
    ADDRESS_FID(fid, c);
    jbyte *bytes = (*env)->GetByteArrayElements(env, jmsg, 0);
    PmError rslt = Pm_WriteSysEx(PMSTREAM(jstream, fid), when, 
                                 (unsigned char *) bytes);
    (*env)->ReleaseByteArrayElements(env, jmsg, bytes, 0);
    return rslt;
}

/*
 * Method:    Pt_TimeStart
 */
JNIEXPORT jint JNICALL Java_jportmidi_JPortMidiApi_Pt_1TimeStart
        (JNIEnv *env, jclass c, jint resolution)
{
    return Pt_Start(resolution, NULL, NULL);
}

/*
 * Method:    Pt_TimeStop
 */
JNIEXPORT jint JNICALL Java_jportmidi_JPortMidiApi_Pt_1TimeStop
        (JNIEnv *env, jclass c)
 {
     return Pt_Stop();
 }

/*
 * Method:    Pt_Time
 */
JNIEXPORT jint JNICALL Java_jportmidi_JPortMidiApi_Pt_1Time
        (JNIEnv *env, jclass c)
 {
     return Pt_Time();
 }

/*
 * Method:    Pt_TimeStarted
 */
JNIEXPORT jboolean JNICALL Java_jportmidi_JPortMidiApi_Pt_1TimeStarted
        (JNIEnv *env, jclass c)
{
    return Pt_Started();
}