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