1 /* gnu_javax_sound_midi_alsa_AlsaMidiDeviceProvider.c
2    Copyright (C) 2005 Free Software Foundation, Inc.
3 
4    This file is part of GNU Classpath.
5 
6    GNU Classpath is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2, or (at your option)
9    any later version.
10 
11    GNU Classpath is distributed in the hope that it will be useful, but
12    WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14    General Public License for more details.
15 
16    You should have received a copy of the GNU General Public License
17    along with GNU Classpath; see the file COPYING.  If not, write to the
18    Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19    02110-1301 USA.
20 
21    Linking this library statically or dynamically with other modules is
22    making a combined work based on this library.  Thus, the terms and
23    conditions of the GNU General Public License cover the whole
24    combination.
25 
26    As a special exception, the copyright holders of this library give you
27    permission to link this library with independent modules to produce an
28    executable, regardless of the license terms of these independent
29    modules, and to copy and distribute the resulting executable under
30    terms of your choice, provided that you also meet, for each linked
31    independent module, the terms and conditions of the license of that
32    module.  An independent module is a module which is not derived from
33    or based on this library.  If you modify this library, you may extend
34    this exception to your version of the library, but you are not
35    obligated to do so.  If you do not wish to do so, delete this
36    exception statement from your version. */
37 
38 #include <config.h>
39 #include <gnu_javax_sound_midi_alsa_AlsaMidiDeviceProvider.h>
40 
41 #include <alsa/asoundlib.h>
42 
43 static snd_seq_t *seq;
44 
45 JNIEXPORT void JNICALL
Java_gnu_javax_sound_midi_alsa_AlsaMidiDeviceProvider_init_1(JNIEnv * env,jobject this)46 Java_gnu_javax_sound_midi_alsa_AlsaMidiDeviceProvider_init_1
47 (JNIEnv *env __attribute__((unused)), jobject this __attribute__((unused)))
48 {
49   int rc = snd_seq_open (&seq, "default", SND_SEQ_OPEN_DUPLEX, SND_SEQ_NONBLOCK);
50   if (rc < 0)
51     abort ();
52   snd_seq_set_client_name(seq, "gnu.javax.sound.midi.alsa");
53 }
54 
55 /**
56  * Return a CLAZZ[] filled with ports matching TYPE.
57  */
58 static jobjectArray
getPortDeviceInfo(JNIEnv * env,unsigned type,const char * clazz)59 getPortDeviceInfo (JNIEnv *env, unsigned type, const char *clazz)
60 {
61   jobjectArray rarray;
62   snd_seq_client_info_t *cinfo;
63   snd_seq_port_info_t *pinfo;
64   int client;
65   int count = 0;
66   jclass icls;
67   jmethodID mid;
68 
69   snd_seq_client_info_alloca(&cinfo);
70   snd_seq_port_info_alloca(&pinfo);
71 
72   /* First, count the number of input devices.  */
73   snd_seq_client_info_set_client (cinfo, -1);
74   while (snd_seq_query_next_client (seq, cinfo) >= 0)
75     {
76       client = snd_seq_client_info_get_client (cinfo);
77       if (client == 0)
78 	continue;
79       snd_seq_port_info_set_client (pinfo, client);
80       snd_seq_port_info_set_port (pinfo, -1);
81       while (snd_seq_query_next_port (seq, pinfo) >= 0)
82 	{
83 	  if ((snd_seq_port_info_get_capability (pinfo) & type) != type)
84 	    continue;
85 	  count++;
86 	}
87     }
88 
89   icls = (*env)->FindClass(env, clazz);
90   mid = (*env)->GetMethodID(env, icls, "<init>",
91 			    "(Ljava/lang/String;Ljava/lang/String;JJ)V");
92 
93   rarray = (jobjectArray) (*env)->NewObjectArray(env, count, icls, NULL);
94 
95   /* Now, populate our array.  */
96   count = 0;
97   snd_seq_client_info_set_client (cinfo, -1);
98   while (snd_seq_query_next_client (seq, cinfo) >= 0)
99     {
100       const char *client_name;
101       client = snd_seq_client_info_get_client (cinfo);
102       if (client == 0)
103 	continue;
104       snd_seq_port_info_set_client (pinfo, client);
105       snd_seq_port_info_set_port (pinfo, -1);
106 
107       client_name = snd_seq_client_info_get_name (cinfo);
108 
109       while (snd_seq_query_next_port (seq, pinfo) >= 0)
110 	{
111 	  const char *port_name;
112 
113 	  if ((snd_seq_port_info_get_capability (pinfo) & type) != type)
114 	    continue;
115 
116 	  port_name = snd_seq_port_info_get_name (pinfo);
117 
118 	  (*env)->SetObjectArrayElement(env, rarray, count,
119 					(*env)->NewObject (env, icls, mid,
120 							   (*env)->NewStringUTF (env, client_name),
121 							   (*env)->NewStringUTF (env, port_name),
122 							   (jlong) snd_seq_port_info_get_client(pinfo),
123 							   (jlong) snd_seq_port_info_get_port(pinfo)));
124 	  count++;
125 	}
126     }
127 
128   return rarray;
129 }
130 
131 JNIEXPORT jobjectArray JNICALL
Java_gnu_javax_sound_midi_alsa_AlsaMidiDeviceProvider_getInputDeviceInfo_1(JNIEnv * env,jobject this)132 Java_gnu_javax_sound_midi_alsa_AlsaMidiDeviceProvider_getInputDeviceInfo_1
133   (JNIEnv *env, jobject this __attribute__((unused)))
134 {
135   return getPortDeviceInfo (env,
136 			    (SND_SEQ_PORT_CAP_READ | SND_SEQ_PORT_CAP_SUBS_READ),
137 			    "gnu/javax/sound/midi/alsa/AlsaMidiDeviceProvider$AlsaInputPortInfo");
138 }
139 
140 JNIEXPORT jobjectArray JNICALL
Java_gnu_javax_sound_midi_alsa_AlsaMidiDeviceProvider_getOutputDeviceInfo_1(JNIEnv * env,jobject this)141 Java_gnu_javax_sound_midi_alsa_AlsaMidiDeviceProvider_getOutputDeviceInfo_1
142   (JNIEnv *env, jobject this __attribute__((unused)))
143 {
144   return getPortDeviceInfo (env,
145 			    (SND_SEQ_PORT_CAP_WRITE | SND_SEQ_PORT_CAP_SUBS_WRITE),
146 			    "gnu/javax/sound/midi/alsa/AlsaMidiDeviceProvider$AlsaOutputPortInfo");
147 }
148 
149