J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 1999-2007 Sun Microsystems, Inc. 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. Sun designates this |
| 8 | * particular file as subject to the "Classpath" exception as provided |
| 9 | * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 22 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 23 | * have any questions. |
| 24 | */ |
| 25 | |
| 26 | package com.sun.media.sound; |
| 27 | |
| 28 | import javax.sound.midi.MidiDevice; |
| 29 | import javax.sound.midi.spi.MidiDeviceProvider; |
| 30 | |
| 31 | |
| 32 | /** |
| 33 | * MIDI input device provider. |
| 34 | * |
| 35 | * @author Kara Kytle |
| 36 | * @author Florian Bomers |
| 37 | */ |
| 38 | public class MidiInDeviceProvider extends AbstractMidiDeviceProvider { |
| 39 | |
| 40 | /** Cache of info objects for all MIDI output devices on the system. */ |
| 41 | static Info[] infos = null; |
| 42 | |
| 43 | /** Cache of open MIDI input devices on the system. */ |
| 44 | static MidiDevice[] devices = null; |
| 45 | |
| 46 | private static boolean enabled; |
| 47 | |
| 48 | // STATIC |
| 49 | |
| 50 | static { |
| 51 | // initialize |
| 52 | Platform.initialize(); |
| 53 | enabled = Platform.isMidiIOEnabled(); |
| 54 | } |
| 55 | |
| 56 | // CONSTRUCTOR |
| 57 | |
| 58 | /** |
| 59 | * Required public no-arg constructor. |
| 60 | */ |
| 61 | public MidiInDeviceProvider() { |
| 62 | if (Printer.trace) Printer.trace("MidiInDeviceProvider: constructor"); |
| 63 | } |
| 64 | |
| 65 | // implementation of abstract methods in AbstractMidiDeviceProvider |
| 66 | |
| 67 | AbstractMidiDeviceProvider.Info createInfo(int index) { |
| 68 | if (!enabled) { |
| 69 | return null; |
| 70 | } |
| 71 | return new MidiInDeviceInfo(index, MidiInDeviceProvider.class); |
| 72 | } |
| 73 | |
| 74 | MidiDevice createDevice(AbstractMidiDeviceProvider.Info info) { |
| 75 | if (enabled && (info instanceof MidiInDeviceInfo)) { |
| 76 | return new MidiInDevice(info); |
| 77 | } |
| 78 | return null; |
| 79 | } |
| 80 | |
| 81 | int getNumDevices() { |
| 82 | if (!enabled) { |
| 83 | if (Printer.debug)Printer.debug("MidiInDevice not enabled, returning 0 devices"); |
| 84 | return 0; |
| 85 | } |
| 86 | int numDevices = nGetNumDevices(); |
| 87 | if (Printer.debug)Printer.debug("MidiInDeviceProvider.getNumDevices(): devices: " + numDevices); |
| 88 | return numDevices; |
| 89 | } |
| 90 | |
| 91 | MidiDevice[] getDeviceCache() { return devices; } |
| 92 | void setDeviceCache(MidiDevice[] devices) { this.devices = devices; } |
| 93 | Info[] getInfoCache() { return infos; } |
| 94 | void setInfoCache(Info[] infos) { this.infos = infos; } |
| 95 | |
| 96 | |
| 97 | // INNER CLASSES |
| 98 | |
| 99 | /** |
| 100 | * Info class for MidiInDevices. Adds the |
| 101 | * provider's Class to keep the provider class from being |
| 102 | * unloaded. Otherwise, at least on JDK1.1.7 and 1.1.8, |
| 103 | * the provider class can be unloaded. Then, then the provider |
| 104 | * is next invoked, the static block is executed again and a new |
| 105 | * instance of the device object is created. Even though the |
| 106 | * previous instance may still exist and be open / in use / etc., |
| 107 | * the new instance will not reflect that state... |
| 108 | */ |
| 109 | static class MidiInDeviceInfo extends AbstractMidiDeviceProvider.Info { |
| 110 | private Class providerClass; |
| 111 | |
| 112 | private MidiInDeviceInfo(int index, Class providerClass) { |
| 113 | super(nGetName(index), nGetVendor(index), nGetDescription(index), nGetVersion(index), index); |
| 114 | this.providerClass = providerClass; |
| 115 | } |
| 116 | |
| 117 | } // class MidiInDeviceInfo |
| 118 | |
| 119 | |
| 120 | // NATIVE METHODS |
| 121 | |
| 122 | private static native int nGetNumDevices(); |
| 123 | private static native String nGetName(int index); |
| 124 | private static native String nGetVendor(int index); |
| 125 | private static native String nGetDescription(int index); |
| 126 | private static native String nGetVersion(int index); |
| 127 | } |