1 /*
   2  * Copyright (c) 1999, 2014, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.sun.media.sound;
  27 
  28 import javax.sound.midi.MidiDevice;
  29 
  30 /**
  31  * MIDI input device provider.
  32  *
  33  * @author Kara Kytle
  34  * @author Florian Bomers
  35  */
  36 public final class MidiInDeviceProvider extends AbstractMidiDeviceProvider {
  37 
  38     /** Cache of info objects for all MIDI output devices on the system. */
  39     private static Info[] infos = null;
  40 
  41     /** Cache of open MIDI input devices on the system. */
  42     private static MidiDevice[] devices = null;
  43 
  44     private static final boolean enabled;
  45 
  46     static {
  47         // initialize
  48         Platform.initialize();
  49         enabled = Platform.isMidiIOEnabled();
  50     }
  51 
  52     /**
  53      * Required public no-arg constructor.
  54      */
  55     public MidiInDeviceProvider() {
  56         if (Printer.trace) Printer.trace("MidiInDeviceProvider: constructor");
  57     }
  58 
  59     // implementation of abstract methods in AbstractMidiDeviceProvider
  60 
  61     @Override
  62     AbstractMidiDeviceProvider.Info createInfo(int index) {
  63         if (!enabled) {
  64             return null;
  65         }
  66         return new MidiInDeviceInfo(index, MidiInDeviceProvider.class);
  67     }
  68 
  69     @Override
  70     MidiDevice createDevice(AbstractMidiDeviceProvider.Info info) {
  71         if (enabled && (info instanceof MidiInDeviceInfo)) {
  72             return new MidiInDevice(info);
  73         }
  74         return null;
  75     }
  76 
  77     @Override
  78     int getNumDevices() {
  79         if (!enabled) {
  80             if (Printer.debug)Printer.debug("MidiInDevice not enabled, returning 0 devices");
  81             return 0;
  82         }
  83         int numDevices = nGetNumDevices();
  84         if (Printer.debug)Printer.debug("MidiInDeviceProvider.getNumDevices(): devices: " + numDevices);
  85         return numDevices;
  86     }
  87 
  88     @Override
  89     MidiDevice[] getDeviceCache() { return devices; }
  90     @Override
  91     void setDeviceCache(MidiDevice[] devices) { MidiInDeviceProvider.devices = devices; }
  92     @Override
  93     Info[] getInfoCache() { return infos; }
  94     @Override
  95     void setInfoCache(Info[] infos) { MidiInDeviceProvider.infos = infos; }
  96 
  97     /**
  98      * Info class for MidiInDevices.  Adds the
  99      * provider's Class to keep the provider class from being
 100      * unloaded.  Otherwise, at least on JDK1.1.7 and 1.1.8,
 101      * the provider class can be unloaded.  Then, then the provider
 102      * is next invoked, the static block is executed again and a new
 103      * instance of the device object is created.  Even though the
 104      * previous instance may still exist and be open / in use / etc.,
 105      * the new instance will not reflect that state...
 106      */
 107     static final class MidiInDeviceInfo extends AbstractMidiDeviceProvider.Info {
 108         private final Class<?> providerClass;
 109 
 110         private MidiInDeviceInfo(int index, Class<?> providerClass) {
 111             super(nGetName(index), nGetVendor(index), nGetDescription(index), nGetVersion(index), index);
 112             this.providerClass = providerClass;
 113         }
 114 
 115     } // class MidiInDeviceInfo
 116 
 117     private static native int nGetNumDevices();
 118     private static native String nGetName(int index);
 119     private static native String nGetVendor(int index);
 120     private static native String nGetDescription(int index);
 121     private static native String nGetVersion(int index);
 122 }