Path: blob/master/src/java.desktop/share/classes/com/sun/media/sound/AbstractMidiDeviceProvider.java
41161 views
/*1* Copyright (c) 2002, 2019, 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 java.util.Objects;2829import javax.sound.midi.MidiDevice;30import javax.sound.midi.spi.MidiDeviceProvider;3132/**33* Super class for MIDI input or output device provider.34*35* @author Florian Bomers36*/37public abstract class AbstractMidiDeviceProvider extends MidiDeviceProvider {3839private static final boolean enabled;4041/**42* Create objects representing all MIDI output devices on the system.43*/44static {45Platform.initialize();46enabled = Platform.isMidiIOEnabled();47// $$fb number of MIDI devices may change with time48// also for memory's sake, do not initialize the arrays here49}5051final synchronized void readDeviceInfos() {52Info[] infos = getInfoCache();53MidiDevice[] devices = getDeviceCache();54if (!enabled) {55if (infos == null || infos.length != 0) {56setInfoCache(new Info[0]);57}58if (devices == null || devices.length != 0) {59setDeviceCache(new MidiDevice[0]);60}61return;62}6364int oldNumDevices = (infos==null)?-1:infos.length;65int newNumDevices = getNumDevices();66if (oldNumDevices != newNumDevices) {67// initialize the arrays68Info[] newInfos = new Info[newNumDevices];69MidiDevice[] newDevices = new MidiDevice[newNumDevices];7071for (int i = 0; i < newNumDevices; i++) {72Info newInfo = createInfo(i);7374// in case that we are re-reading devices, try to find75// the previous one and reuse it76if (infos != null) {77for (int ii = 0; ii < infos.length; ii++) {78Info info = infos[ii];79if (info != null && info.equalStrings(newInfo)) {80// new info matches the still existing info. Use old one81newInfos[i] = info;82info.setIndex(i);83infos[ii] = null; // prevent re-use84newDevices[i] = devices[ii];85devices[ii] = null;86break;87}88}89}90if (newInfos[i] == null) {91newInfos[i] = newInfo;92}93}94// the remaining MidiDevice.Info instances in the infos array95// have become obsolete.96if (infos != null) {97for (int i = 0; i < infos.length; i++) {98if (infos[i] != null) {99// disable this device info100infos[i].setIndex(-1);101}102// what to do with the MidiDevice instances that are left103// in the devices array ?? Close them ?104}105}106// commit new list of infos.107setInfoCache(newInfos);108setDeviceCache(newDevices);109}110}111112@Override113public final MidiDevice.Info[] getDeviceInfo() {114readDeviceInfos();115Info[] infos = getInfoCache();116MidiDevice.Info[] localArray = new MidiDevice.Info[infos.length];117System.arraycopy(infos, 0, localArray, 0, infos.length);118return localArray;119}120121@Override122public final MidiDevice getDevice(final MidiDevice.Info info) {123Objects.requireNonNull(info);124if (info instanceof Info) {125readDeviceInfos();126MidiDevice[] devices = getDeviceCache();127Info[] infos = getInfoCache();128Info thisInfo = (Info) info;129int index = thisInfo.getIndex();130if (index >= 0 && index < devices.length && infos[index] == info) {131if (devices[index] == null) {132devices[index] = createDevice(thisInfo);133}134if (devices[index] != null) {135return devices[index];136}137}138}139throw MidiUtils.unsupportedDevice(info);140}141142/**143* Info class for MidiDevices. Adds an index value for144* making native references to a particular device.145*/146static class Info extends MidiDevice.Info {147private int index;148149Info(String name, String vendor, String description, String version, int index) {150super(name, vendor, description, version);151this.index = index;152}153154final boolean equalStrings(Info info) {155return (info != null156&& getName().equals(info.getName())157&& getVendor().equals(info.getVendor())158&& getDescription().equals(info.getDescription())159&& getVersion().equals(info.getVersion()));160}161162final int getIndex() {163return index;164}165166final void setIndex(int index) {167this.index = index;168}169170} // class Info171172abstract int getNumDevices();173abstract MidiDevice[] getDeviceCache();174abstract void setDeviceCache(MidiDevice[] devices);175abstract Info[] getInfoCache();176abstract void setInfoCache(Info[] infos);177178abstract Info createInfo(int index);179abstract MidiDevice createDevice(Info info);180}181182183