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