1 /* gnu_javax_sound_midi_alsa_AlsaPortDevice.c - Native support
3 Free Software Foundation, Inc.
5 This file is part of GNU Classpath.
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)
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.
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
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
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. */
41 #include <gnu_javax_sound_midi_alsa_AlsaPortDevice.h>
44 #include <alsa/asoundlib.h>
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
)
52 snd_seq_port_info_t
*pinfo
, *sinfo
;
53 snd_seq_port_subscribe_t
*subs
;
54 snd_seq_addr_t sender
, dest
;
57 snd_seq_port_info_alloca (&pinfo
);
58 snd_seq_port_info_alloca (&sinfo
);
59 snd_seq_port_subscribe_alloca (&subs
);
61 rc
= snd_seq_open (&seq
, "default", SND_SEQ_OPEN_DUPLEX
, SND_SEQ_NONBLOCK
);
63 snd_seq_port_info_set_capability (pinfo
, SND_SEQ_PORT_CAP_WRITE
);
64 snd_seq_port_info_set_type (pinfo
, SND_SEQ_PORT_TYPE_MIDI_GENERIC
);
66 rc
= snd_seq_create_port (seq
, pinfo
);
68 sender
.client
= (int) client
;
69 sender
.port
= (int) port
;
70 dest
.client
= snd_seq_port_info_get_client(pinfo
);
71 dest
.port
= snd_seq_port_info_get_port(pinfo
);
73 snd_seq_port_subscribe_set_sender (subs
, &sender
);
74 snd_seq_port_subscribe_set_dest (subs
, &dest
);
75 rc
= snd_seq_subscribe_port(seq
, subs
);
81 jmethodID sminit
, rsend
;
87 npfd
= snd_seq_poll_descriptors_count (seq
, POLLIN
);
88 pfd
= (struct pollfd
*) alloca (npfd
* sizeof (struct pollfd
));
89 snd_seq_poll_descriptors (seq
, pfd
, npfd
, POLLIN
);
91 smcls
= (*env
)->FindClass(env
, "javax/sound/midi/ShortMessage");
92 sminit
= (*env
)->GetMethodID(env
, smcls
, "<init>", "([B)V");
94 rcls
= (*env
)->FindClass(env
, "javax/sound/midi/Receiver");
95 rsend
= (*env
)->GetMethodID(env
, rcls
, "send", "(Ljavax/sound/midi/MidiMessage;J)V");
99 if (poll (pfd
, npfd
, 100000) > 0)
105 snd_seq_event_input (seq
, &ev
);
107 if ((ev
->flags
& SND_SEQ_TIME_STAMP_MASK
) == SND_SEQ_TIME_STAMP_TICK
)
108 jtimestamp
= (jlong
) ev
->time
.tick
;
110 jtimestamp
= (jlong
) ev
->time
.time
.tv_sec
* (jlong
) 1000000000
111 + (jlong
) ev
->time
.time
.tv_nsec
;
115 case SND_SEQ_EVENT_NOTEON
:
116 mba
= (*env
)->NewByteArray (env
, 3);
117 ba
= (*env
)->GetByteArrayElements (env
, mba
, 0);
118 ba
[0] = 0x90 + ev
->data
.control
.channel
;
119 ba
[1] = ev
->data
.note
.note
;
120 ba
[2] = ev
->data
.note
.velocity
;
121 (*env
)->ReleaseByteArrayElements (env
, mba
, ba
, 0);
122 msg
= (*env
)->NewObject(env
, smcls
, sminit
, mba
);
123 (*env
)->CallObjectMethod(env
, receiver
,
124 rsend
, msg
, jtimestamp
);
127 case SND_SEQ_EVENT_CONTROLLER
:
128 mba
= (*env
)->NewByteArray (env
, 3);
129 ba
= (*env
)->GetByteArrayElements (env
, mba
, 0);
130 ba
[0] = 0xB0 + ev
->data
.control
.channel
;
131 ba
[1] = ev
->data
.control
.param
;
132 ba
[2] = ev
->data
.control
.value
;
133 (*env
)->ReleaseByteArrayElements (env
, mba
, ba
, 0);
134 msg
= (*env
)->NewObject(env
, smcls
, sminit
, mba
);
135 (*env
)->CallObjectMethod(env
, receiver
,
136 rsend
, msg
, jtimestamp
);
140 printf ("UNKNOWN EVENT 0x%x\n", ev
->type
);
144 snd_seq_free_event(ev
);
146 while (snd_seq_event_input_pending (seq
, 0) > 0);