Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/com/sun/media/sound/AbstractMidiDeviceProvider.java
38924 views
/*1* Copyright (c) 2002, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package com.sun.media.sound;2627import javax.sound.midi.MidiDevice;28import javax.sound.midi.spi.MidiDeviceProvider;293031/**32* Super class for MIDI input or output device provider.33*34* @author Florian Bomers35*/36public abstract class AbstractMidiDeviceProvider extends MidiDeviceProvider {3738private static final boolean enabled;3940/**41* Create objects representing all MIDI output devices on the system.42*/43static {44if (Printer.trace) Printer.trace("AbstractMidiDeviceProvider: static");45Platform.initialize();46enabled = Platform.isMidiIOEnabled();47if (Printer.trace) Printer.trace("AbstractMidiDeviceProvider: enabled: " + enabled);4849// $$fb number of MIDI devices may change with time50// also for memory's sake, do not initialize the arrays here51}525354final synchronized void readDeviceInfos() {55Info[] infos = getInfoCache();56MidiDevice[] devices = getDeviceCache();57if (!enabled) {58if (infos == null || infos.length != 0) {59setInfoCache(new Info[0]);60}61if (devices == null || devices.length != 0) {62setDeviceCache(new MidiDevice[0]);63}64return;65}6667int oldNumDevices = (infos==null)?-1:infos.length;68int newNumDevices = getNumDevices();69if (oldNumDevices != newNumDevices) {70if (Printer.trace) Printer.trace(getClass().toString()71+": readDeviceInfos: old numDevices: "+oldNumDevices72+" newNumDevices: "+ newNumDevices);7374// initialize the arrays75Info[] newInfos = new Info[newNumDevices];76MidiDevice[] newDevices = new MidiDevice[newNumDevices];7778for (int i = 0; i < newNumDevices; i++) {79Info newInfo = createInfo(i);8081// in case that we are re-reading devices, try to find82// the previous one and reuse it83if (infos != null) {84for (int ii = 0; ii < infos.length; ii++) {85Info info = infos[ii];86if (info != null && info.equalStrings(newInfo)) {87// new info matches the still existing info. Use old one88newInfos[i] = info;89info.setIndex(i);90infos[ii] = null; // prevent re-use91newDevices[i] = devices[ii];92devices[ii] = null;93break;94}95}96}97if (newInfos[i] == null) {98newInfos[i] = newInfo;99}100}101// the remaining MidiDevice.Info instances in the infos array102// have become obsolete.103if (infos != null) {104for (int i = 0; i < infos.length; i++) {105if (infos[i] != null) {106// disable this device info107infos[i].setIndex(-1);108}109// what to do with the MidiDevice instances that are left110// in the devices array ?? Close them ?111}112}113// commit new list of infos.114setInfoCache(newInfos);115setDeviceCache(newDevices);116}117}118119120public final MidiDevice.Info[] getDeviceInfo() {121readDeviceInfos();122Info[] infos = getInfoCache();123MidiDevice.Info[] localArray = new MidiDevice.Info[infos.length];124System.arraycopy(infos, 0, localArray, 0, infos.length);125return localArray;126}127128129public final MidiDevice getDevice(MidiDevice.Info info) {130if (info instanceof Info) {131readDeviceInfos();132MidiDevice[] devices = getDeviceCache();133Info[] infos = getInfoCache();134Info thisInfo = (Info) info;135int index = thisInfo.getIndex();136if (index >= 0 && index < devices.length && infos[index] == info) {137if (devices[index] == null) {138devices[index] = createDevice(thisInfo);139}140if (devices[index] != null) {141return devices[index];142}143}144}145146throw new IllegalArgumentException("MidiDevice " + info.toString()147+ " not supported by this provider.");148}149150151// INNER CLASSES152153154/**155* Info class for MidiDevices. Adds an index value for156* making native references to a particular device.157*/158static class Info extends MidiDevice.Info {159private int index;160161Info(String name, String vendor, String description, String version, int index) {162super(name, vendor, description, version);163this.index = index;164}165166final boolean equalStrings(Info info) {167return (info != null168&& getName().equals(info.getName())169&& getVendor().equals(info.getVendor())170&& getDescription().equals(info.getDescription())171&& getVersion().equals(info.getVersion()));172}173174final int getIndex() {175return index;176}177178final void setIndex(int index) {179this.index = index;180}181182} // class Info183184185// ABSTRACT METHODS186187abstract int getNumDevices();188abstract MidiDevice[] getDeviceCache();189abstract void setDeviceCache(MidiDevice[] devices);190abstract Info[] getInfoCache();191abstract void setInfoCache(Info[] infos);192193abstract Info createInfo(int index);194abstract MidiDevice createDevice(Info info);195}196197198