1 |
774 |
jeremybenn |
/* gnu_javax_sound_midi_alsa_AlsaPortDevice.c - Native support
|
2 |
|
|
Copyright (C) 2005, 2010
|
3 |
|
|
Free Software Foundation, Inc.
|
4 |
|
|
|
5 |
|
|
This file is part of GNU Classpath.
|
6 |
|
|
|
7 |
|
|
GNU Classpath is free software; you can redistribute it and/or modify
|
8 |
|
|
it under the terms of the GNU General Public License as published by
|
9 |
|
|
the Free Software Foundation; either version 2, or (at your option)
|
10 |
|
|
any later version.
|
11 |
|
|
|
12 |
|
|
GNU Classpath is distributed in the hope that it will be useful, but
|
13 |
|
|
WITHOUT ANY WARRANTY; without even the implied warranty of
|
14 |
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
15 |
|
|
General Public License for more details.
|
16 |
|
|
|
17 |
|
|
You should have received a copy of the GNU General Public License
|
18 |
|
|
along with GNU Classpath; see the file COPYING. If not, write to the
|
19 |
|
|
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
20 |
|
|
02110-1301 USA.
|
21 |
|
|
|
22 |
|
|
Linking this library statically or dynamically with other modules is
|
23 |
|
|
making a combined work based on this library. Thus, the terms and
|
24 |
|
|
conditions of the GNU General Public License cover the whole
|
25 |
|
|
combination.
|
26 |
|
|
|
27 |
|
|
As a special exception, the copyright holders of this library give you
|
28 |
|
|
permission to link this library with independent modules to produce an
|
29 |
|
|
executable, regardless of the license terms of these independent
|
30 |
|
|
modules, and to copy and distribute the resulting executable under
|
31 |
|
|
terms of your choice, provided that you also meet, for each linked
|
32 |
|
|
independent module, the terms and conditions of the license of that
|
33 |
|
|
module. An independent module is a module which is not derived from
|
34 |
|
|
or based on this library. If you modify this library, you may extend
|
35 |
|
|
this exception to your version of the library, but you are not
|
36 |
|
|
obligated to do so. If you do not wish to do so, delete this
|
37 |
|
|
exception statement from your version. */
|
38 |
|
|
|
39 |
|
|
|
40 |
|
|
#include <config.h>
|
41 |
|
|
#include <gnu_javax_sound_midi_alsa_AlsaPortDevice.h>
|
42 |
|
|
#include <unistd.h>
|
43 |
|
|
|
44 |
|
|
#include <alsa/asoundlib.h>
|
45 |
|
|
|
46 |
|
|
JNIEXPORT void JNICALL
|
47 |
|
|
Java_gnu_javax_sound_midi_alsa_AlsaPortDevice_run_1receiver_1thread_1
|
48 |
|
|
(JNIEnv *env, jobject this __attribute__((unused)),
|
49 |
|
|
jlong client, jlong port, jobject receiver)
|
50 |
|
|
{
|
51 |
|
|
snd_seq_port_info_t *pinfo, *sinfo;
|
52 |
|
|
snd_seq_port_subscribe_t *subs;
|
53 |
|
|
snd_seq_addr_t sender, dest;
|
54 |
|
|
snd_seq_t *seq;
|
55 |
|
|
|
56 |
|
|
snd_seq_port_info_alloca (&pinfo);
|
57 |
|
|
snd_seq_port_info_alloca (&sinfo);
|
58 |
|
|
snd_seq_port_subscribe_alloca (&subs);
|
59 |
|
|
|
60 |
|
|
snd_seq_open (&seq, "default", SND_SEQ_OPEN_DUPLEX, SND_SEQ_NONBLOCK);
|
61 |
|
|
|
62 |
|
|
snd_seq_port_info_set_capability (pinfo, SND_SEQ_PORT_CAP_WRITE);
|
63 |
|
|
snd_seq_port_info_set_type (pinfo, SND_SEQ_PORT_TYPE_MIDI_GENERIC);
|
64 |
|
|
|
65 |
|
|
snd_seq_create_port (seq, pinfo);
|
66 |
|
|
|
67 |
|
|
sender.client = (int) client;
|
68 |
|
|
sender.port = (int) port;
|
69 |
|
|
dest.client = snd_seq_port_info_get_client(pinfo);
|
70 |
|
|
dest.port = snd_seq_port_info_get_port(pinfo);
|
71 |
|
|
|
72 |
|
|
snd_seq_port_subscribe_set_sender (subs, &sender);
|
73 |
|
|
snd_seq_port_subscribe_set_dest (subs, &dest);
|
74 |
|
|
snd_seq_subscribe_port(seq, subs);
|
75 |
|
|
|
76 |
|
|
{
|
77 |
|
|
int npfd;
|
78 |
|
|
struct pollfd *pfd;
|
79 |
|
|
jclass smcls, rcls;
|
80 |
|
|
jmethodID sminit, rsend;
|
81 |
|
|
jbyteArray mba;
|
82 |
|
|
jbyte *ba;
|
83 |
|
|
jobject msg;
|
84 |
|
|
jlong jtimestamp;
|
85 |
|
|
|
86 |
|
|
npfd = snd_seq_poll_descriptors_count (seq, POLLIN);
|
87 |
|
|
pfd = (struct pollfd *) alloca (npfd * sizeof (struct pollfd));
|
88 |
|
|
snd_seq_poll_descriptors (seq, pfd, npfd, POLLIN);
|
89 |
|
|
|
90 |
|
|
smcls = (*env)->FindClass(env, "javax/sound/midi/ShortMessage");
|
91 |
|
|
sminit = (*env)->GetMethodID(env, smcls, "<init>", "([B)V");
|
92 |
|
|
|
93 |
|
|
rcls = (*env)->FindClass(env, "javax/sound/midi/Receiver");
|
94 |
|
|
rsend = (*env)->GetMethodID(env, rcls, "send", "(Ljavax/sound/midi/MidiMessage;J)V");
|
95 |
|
|
|
96 |
|
|
while (1)
|
97 |
|
|
{
|
98 |
|
|
if (poll (pfd, npfd, 100000) > 0)
|
99 |
|
|
{
|
100 |
|
|
snd_seq_event_t *ev;
|
101 |
|
|
|
102 |
|
|
do
|
103 |
|
|
{
|
104 |
|
|
snd_seq_event_input (seq, &ev);
|
105 |
|
|
|
106 |
|
|
if ((ev->flags & SND_SEQ_TIME_STAMP_MASK) == SND_SEQ_TIME_STAMP_TICK)
|
107 |
|
|
jtimestamp = (jlong) ev->time.tick;
|
108 |
|
|
else
|
109 |
|
|
jtimestamp = (jlong) ev->time.time.tv_sec * (jlong) 1000000000
|
110 |
|
|
+ (jlong) ev->time.time.tv_nsec;
|
111 |
|
|
|
112 |
|
|
switch (ev->type)
|
113 |
|
|
{
|
114 |
|
|
case SND_SEQ_EVENT_NOTEON:
|
115 |
|
|
mba = (*env)->NewByteArray (env, 3);
|
116 |
|
|
ba = (*env)->GetByteArrayElements (env, mba, 0);
|
117 |
|
|
ba[0] = 0x90 + ev->data.control.channel;
|
118 |
|
|
ba[1] = ev->data.note.note;
|
119 |
|
|
ba[2] = ev->data.note.velocity;
|
120 |
|
|
(*env)->ReleaseByteArrayElements (env, mba, ba, 0);
|
121 |
|
|
msg = (*env)->NewObject(env, smcls, sminit, mba);
|
122 |
|
|
(*env)->CallObjectMethod(env, receiver,
|
123 |
|
|
rsend, msg, jtimestamp);
|
124 |
|
|
break;
|
125 |
|
|
|
126 |
|
|
case SND_SEQ_EVENT_CONTROLLER:
|
127 |
|
|
mba = (*env)->NewByteArray (env, 3);
|
128 |
|
|
ba = (*env)->GetByteArrayElements (env, mba, 0);
|
129 |
|
|
ba[0] = 0xB0 + ev->data.control.channel;
|
130 |
|
|
ba[1] = ev->data.control.param;
|
131 |
|
|
ba[2] = ev->data.control.value;
|
132 |
|
|
(*env)->ReleaseByteArrayElements (env, mba, ba, 0);
|
133 |
|
|
msg = (*env)->NewObject(env, smcls, sminit, mba);
|
134 |
|
|
(*env)->CallObjectMethod(env, receiver,
|
135 |
|
|
rsend, msg, jtimestamp);
|
136 |
|
|
break;
|
137 |
|
|
|
138 |
|
|
default:
|
139 |
|
|
printf ("UNKNOWN EVENT 0x%x\n", ev->type);
|
140 |
|
|
break;
|
141 |
|
|
}
|
142 |
|
|
|
143 |
|
|
snd_seq_free_event(ev);
|
144 |
|
|
}
|
145 |
|
|
while (snd_seq_event_input_pending (seq, 0) > 0);
|
146 |
|
|
}
|
147 |
|
|
}
|
148 |
|
|
}
|
149 |
|
|
}
|