Source for gnu.javax.sound.midi.alsa.AlsaMidiDeviceProvider

   1: /* AlsaMidiDeviceProvider.java -- The ALSA MIDI Device Provider
   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: 
  39: package gnu.javax.sound.midi.alsa;
  40: 
  41: import gnu.classpath.Configuration;
  42: 
  43: import javax.sound.midi.MidiDevice;
  44: import javax.sound.midi.MidiDevice.Info;
  45: import javax.sound.midi.spi.MidiDeviceProvider;
  46: 
  47: /**
  48:  * Provide ALSA MIDI devices.
  49:  *
  50:  * @author Anthony Green (green@redhat.com)
  51:  *
  52:  */
  53: public class AlsaMidiDeviceProvider extends MidiDeviceProvider
  54: {
  55:   /**
  56:    * Abstract base for ALSA specific MIDI device info.
  57:    *
  58:    * @author Anthony Green (green@redhat.com)
  59:    *
  60:    */
  61:   private static abstract class AlsaInfo extends Info
  62:   {
  63:     /**
  64:      * Create an ALSA specific MIDI device info object.
  65:      *
  66:      * @param name the device name
  67:      * @param description the device description
  68:      */
  69:     public AlsaInfo(String name, String description)
  70:     {
  71:       super(name, "Alsa", description, "0.0");
  72:     }
  73: 
  74:     abstract MidiDevice getDevice ();
  75:   }
  76: 
  77:   /**
  78:    * ALSA MIDI Port.
  79:    *
  80:    * @author Anthony Green (green@redhat.com)
  81:    *
  82:    */
  83:   public static abstract class AlsaPortInfo extends AlsaInfo
  84:   {
  85:     long client;
  86:     long port;
  87: 
  88:     /**
  89:      * Create ALSA MIDI In Port.
  90:      *
  91:      * @param name the device name
  92:      * @param description the device description
  93:      * @param client the client ID
  94:      * @param port the port ID
  95:      */
  96:     public AlsaPortInfo(String name, String description, long client, long port)
  97:     {
  98:       super(name, description);
  99:       this.client = client;
 100:       this.port = port;
 101:     }
 102:   }
 103: 
 104:   /**
 105:    * ALSA Sequencer specific info.
 106:    *
 107:    * @author Anthony Green (green@redhat.com)
 108:    *
 109:    */
 110:   private static class AlsaSequencerInfo extends AlsaInfo
 111:   {
 112:     public AlsaSequencerInfo(String name, String description)
 113:     {
 114:       super(name, description);
 115:     }
 116: 
 117:     MidiDevice getDevice()
 118:     {
 119:       return AlsaMidiSequencerDevice.getInstance();
 120:     }
 121:   }
 122: 
 123:   /**
 124:    * ALSA MIDI In Port.
 125:    *
 126:    * @author Anthony Green (green@redhat.com)
 127:    *
 128:    */
 129:   private static class AlsaInputPortInfo extends AlsaPortInfo
 130:   {
 131:     public AlsaInputPortInfo(String name, String description, long client, long port)
 132:     {
 133:       super(name, description, client, port);
 134:     }
 135: 
 136:     MidiDevice getDevice()
 137:     {
 138:       return new AlsaInputPortDevice(this);
 139:     }
 140:   }
 141: 
 142:   /**
 143:    * ALSA MIDI Out Port.
 144:    *
 145:    * @author Anthony Green (green@redhat.com)
 146:    *
 147:    */
 148:   private static class AlsaOutputPortInfo extends AlsaPortInfo
 149:   {
 150:     public AlsaOutputPortInfo(String name, String description, long client, long port)
 151:     {
 152:       super(name, description, client, port);
 153:     }
 154: 
 155:     MidiDevice getDevice()
 156:     {
 157:       return new AlsaOutputPortDevice(this);
 158:     }
 159:   }
 160: 
 161:   private static AlsaInfo[] infos;
 162: 
 163:   private static native AlsaInfo[] getInputDeviceInfo_();
 164:   private static native AlsaInfo[] getOutputDeviceInfo_();
 165: 
 166:   /**
 167:    * Initialize the ALSA system
 168:    */
 169:   private static native void init_();
 170: 
 171:   static
 172:   {
 173:     if (Configuration.INIT_LOAD_LIBRARY)
 174:       {
 175:         System.loadLibrary("gjsmalsa");
 176:       }
 177: 
 178:     init_();
 179: 
 180:     AlsaInfo inputs[] = getInputDeviceInfo_();
 181:     AlsaInfo outputs[] = getOutputDeviceInfo_();
 182: 
 183:     infos = new AlsaInfo[inputs.length + outputs.length + 1];
 184:     infos[0] = new AlsaSequencerInfo ("/dev/snd/seq", "ALSA Sequencer");
 185:     System.arraycopy(inputs, 0, infos, 1, inputs.length);
 186:     System.arraycopy(outputs, 0, infos, 1 + inputs.length, outputs.length);
 187:   }
 188: 
 189:   public AlsaMidiDeviceProvider()
 190:   {
 191:     // Nothing.
 192:   }
 193: 
 194:   /* (non-Javadoc)
 195:    * @see javax.sound.midi.spi.MidiDeviceProvider#getDeviceInfo()
 196:    */
 197:   public Info[] getDeviceInfo()
 198:   {
 199:     return infos;
 200:   }
 201: 
 202:   /* (non-Javadoc)
 203:    * @see javax.sound.midi.spi.MidiDeviceProvider#getDevice(javax.sound.midi.MidiDevice.Info)
 204:    */
 205:   public MidiDevice getDevice(Info info)
 206:   {
 207:     for (int i = 0; i < infos.length; i++)
 208:     {
 209:       if (info.equals(infos[i]))
 210:       {
 211:         return infos[i].getDevice();
 212:       }
 213:     }
 214:     throw new IllegalArgumentException("Don't recognize MIDI device " + info);
 215:   }
 216: }