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 output device provider.
  32  *
  33  * @author Kara Kytle
  34  * @author Florian Bomers
  35  */
  36 public final class MidiOutDeviceProvider 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 output 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 MidiOutDeviceProvider() {
  56         if (Printer.trace) Printer.trace("MidiOutDeviceProvider: constructor");
  57     }
  58 
  59     @Override
  60     AbstractMidiDeviceProvider.Info createInfo(int index) {
  61         if (!enabled) {
  62             return null;
  63         }
  64         return new MidiOutDeviceInfo(index, MidiOutDeviceProvider.class);
  65     }
  66 
  67     @Override
  68     MidiDevice createDevice(AbstractMidiDeviceProvider.Info info) {
  69         if (enabled && (info instanceof MidiOutDeviceInfo)) {
  70             return new MidiOutDevice(info);
  71         }
  72         return null;
  73     }
  74 
  75     @Override
  76     int getNumDevices() {
  77         if (!enabled) {
  78             if (Printer.debug)Printer.debug("MidiOutDevice not enabled, returning 0 devices");
  79             return 0;
  80         }
  81         return nGetNumDevices();
  82     }
  83 
  84     @Override
  85     MidiDevice[] getDeviceCache() { return devices; }
  86     @Override
  87     void setDeviceCache(MidiDevice[] devices) { MidiOutDeviceProvider.devices = devices; }
  88     @Override
  89     Info[] getInfoCache() { return infos; }
  90     @Override
  91     void setInfoCache(Info[] infos) { MidiOutDeviceProvider.infos = infos; }
  92 
  93     /**
  94      * Info class for MidiOutDevices.  Adds the
  95      * provider's Class to keep the provider class from being
  96      * unloaded.  Otherwise, at least on JDK1.1.7 and 1.1.8,
  97      * the provider class can be unloaded.  Then, then the provider
  98      * is next invoked, the static block is executed again and a new
  99      * instance of the device object is created.  Even though the
 100      * previous instance may still exist and be open / in use / etc.,
 101      * the new instance will not reflect that state...
 102      */
 103     static final class MidiOutDeviceInfo extends AbstractMidiDeviceProvider.Info {
 104         private final Class<?> providerClass;
 105 
 106         private MidiOutDeviceInfo(int index, Class<?> providerClass) {
 107             super(nGetName(index), nGetVendor(index), nGetDescription(index), nGetVersion(index), index);
 108             this.providerClass = providerClass;
 109         }
 110 
 111     } // class MidiOutDeviceInfo
 112 
 113     private static native int nGetNumDevices();
 114     private static native String nGetName(int index);
 115     private static native String nGetVendor(int index);
 116     private static native String nGetDescription(int index);
 117     private static native String nGetVersion(int index);
 118 }