/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/**
* Abstract AbstractMidiDevice class representing functionality shared by
* MidiInDevice and MidiOutDevice objects.
*
* @author David Rivas
* @author Kara Kytle
* @author Matthias Pfisterer
* @author Florian Bomers
*/
// STATIC VARIABLES
private static final boolean TRACE_TRANSMITTER = false;
// INSTANCE VARIABLES
// lock to protect receiverList and transmitterList
// from simultaneous creation and destruction
// reduces possibility of deadlock, compared to
// synchronizing to the class instance
// DEVICE ATTRIBUTES
// DEVICE STATE
private boolean open = false;
private int openRefCount;
/** List of Receivers and Transmitters that opened the device implicitely.
*/
/**
* This is the device handle returned from native code
*/
// CONSTRUCTOR
/**
* Constructs an AbstractMidiDevice with the specified info object.
* @param info the description of the device
*/
/*
* The initial mode and and only supported mode default to OMNI_ON_POLY.
*/
openRefCount = 0;
}
// MIDI DEVICE METHODS
return info;
}
/** Open the device from an aplication program.
* Setting the open reference count to -1 here prevents Transmitters and Receivers that
* opened the the device implicitly from closing it. The only way to close the device after
* this call is a call to close().
*/
synchronized(this) {
openRefCount = -1;
doOpen();
}
}
/** Open the device implicitly.
* This method is intended to be used by AbstractReceiver
* and BasicTransmitter. Actually, it is called by getReceiverReferenceCounting() and
* getTransmitterReferenceCounting(). These, in turn, are called by MidiSytem on calls to
* getReceiver() and getTransmitter(). The former methods should pass the Receiver or
* Transmitter just created as the object parameter to this method. Storing references to
* these objects is necessary to be able to decide later (when it comes to closing) if
* R/T's are ones that opened the device implicitly.
*
* @object The Receiver or Transmitter instance that triggered this implicit open.
*/
synchronized(this) {
if (openRefCount != -1) {
openRefCount++;
}
// double calls to doOpens() will be catched by the open flag.
doOpen();
}
}
synchronized(this) {
if (! isOpen()) {
implOpen();
open = true;
}
}
}
public final void close() {
synchronized (this) {
doClose();
openRefCount = 0;
}
}
/** Close the device for an object that implicitely opened it.
* This method is intended to be used by Transmitter.close() and Receiver.close().
* Those methods should pass this for the object parameter. Since Transmitters or Receivers
* do not know if their device has been opened implicitely because of them, they call this
* method in any case. This method now is able to seperate Receivers/Transmitters that opened
* the device implicitely from those that didn't by looking up the R/T in the
* openKeepingObjects list. Only if the R/T is contained there, the reference count is
* reduced.
*
* @param object The object that might have been opening the device implicitely (for now,
* this may be a Transmitter or receiver).
*/
synchronized(this) {
if (openRefCount > 0) {
openRefCount--;
if (openRefCount == 0) {
doClose();
}
}
}
}
}
public final void doClose() {
synchronized(this) {
if (isOpen()) {
implClose();
open = false;
}
}
}
public final boolean isOpen() {
return open;
}
protected void implClose() {
synchronized (traRecLock) {
if (receiverList != null) {
// close all receivers
}
}
if (transmitterList != null) {
// close all transmitters
}
}
}
/**
* This implementation always returns -1.
* Devices that actually provide this should over-ride
* this method.
*/
public long getMicrosecondPosition() {
return -1;
}
/** Return the maximum number of Receivers supported by this device.
Depending on the return value of hasReceivers(), this method returns either 0 or -1.
Subclasses should rather override hasReceivers() than override this method.
*/
public final int getMaxReceivers() {
if (hasReceivers()) {
return -1;
} else {
return 0;
}
}
/** Return the maximum number of Transmitters supported by this device.
Depending on the return value of hasTransmitters(), this method returns either 0 or -1.
Subclasses should override hasTransmitters().
*/
public final int getMaxTransmitters() {
if (hasTransmitters()) {
return -1;
} else {
return 0;
}
}
/** Retrieve a Receiver for this device.
This method returns the value returned by createReceiver(), if it doesn't throw
an exception. Subclasses should rather override createReceiver() than override
this method.
If createReceiver returns a Receiver, it is added to the internal list
of Receivers (see getReceiversList)
*/
synchronized (traRecLock) {
}
return receiver;
}
synchronized (traRecLock) {
if (receiverList == null) {
} else {
}
}
return recs;
}
/**
* This implementation uses createTransmitter, which may throw an exception.
* If a transmitter is returned in createTransmitter, it is added to the internal
* TransmitterList
*/
synchronized (traRecLock) {
}
return transmitter;
}
synchronized (traRecLock) {
if (transmitterList == null
} else {
}
}
return tras;
}
// HELPER METHODS
final long getId() {
return id;
}
// REFERENCE COUNTING
/** Retrieve a Receiver and open the device implicitly.
This method is called by MidiSystem.getReceiver().
*/
throws MidiUnavailableException {
/* Keep this order of commands! If getReceiver() throws an exception,
openInternal() should not be called!
*/
synchronized (traRecLock) {
receiver = getReceiver();
}
return receiver;
}
/** Retrieve a Transmitter and open the device implicitly.
This method is called by MidiSystem.getTransmitter().
*/
throws MidiUnavailableException {
/* Keep this order of commands! If getTransmitter() throws an exception,
openInternal() should not be called!
*/
synchronized (traRecLock) {
}
return transmitter;
}
/** Return the list of objects that have opened the device implicitely.
*/
if (openKeepingObjects == null) {
openKeepingObjects = new ArrayList();
}
return openKeepingObjects;
}
// RECEIVER HANDLING METHODS
/** Return the internal list of Receivers, possibly creating it first.
*/
synchronized (traRecLock) {
if (receiverList == null) {
}
}
return receiverList;
}
/** Returns if this device supports Receivers.
Subclasses that use Receivers should override this method to
return true. They also should override createReceiver().
@return true, if the device supports Receivers, false otherwise.
*/
protected boolean hasReceivers() {
return false;
}
/** Create a Receiver object.
throwing an exception here means that Receivers aren't enabled.
Subclasses that use Receivers should override this method with
one that returns objects implementing Receiver.
Classes overriding this method should also override hasReceivers()
to return true.
*/
throw new MidiUnavailableException("MIDI IN receiver not available");
}
// TRANSMITTER HANDLING
/** Return the internal list of Transmitters, possibly creating it first.
*/
synchronized (traRecLock) {
if (transmitterList == null) {
transmitterList = new TransmitterList();
}
}
return transmitterList;
}
/** Returns if this device supports Transmitters.
Subclasses that use Transmitters should override this method to
return true. They also should override createTransmitter().
@return true, if the device supports Transmitters, false otherwise.
*/
protected boolean hasTransmitters() {
return false;
}
/** Create a Transmitter object.
throwing an exception here means that Transmitters aren't enabled.
Subclasses that use Transmitters should override this method with
one that returns objects implementing Transmitters.
Classes overriding this method should also override hasTransmitters()
to return true.
*/
throw new MidiUnavailableException("MIDI OUT transmitter not available");
}
// ABSTRACT METHODS
/**
* close this device if discarded by the garbage collector
*/
protected final void finalize() {
close();
}
// INNER CLASSES
/** Base class for Receivers.
Subclasses that use Receivers must use this base class, since it
contains magic necessary to manage implicit closing the device.
This is necessary for Receivers retrieved via MidiSystem.getReceiver()
(which opens the device implicitely).
*/
private boolean open = true;
/** Deliver a MidiMessage.
This method contains magic related to the closed state of a
Receiver. Therefore, subclasses should not override this method.
Instead, they should implement implSend().
*/
final long timeStamp) {
if (!open) {
throw new IllegalStateException("Receiver is not open");
}
}
/** Close the Receiver.
* Here, the call to the magic method closeInternal() takes place.
* Therefore, subclasses that override this method must call
* 'super.close()'.
*/
public final void close() {
open = false;
synchronized (AbstractMidiDevice.this.traRecLock) {
}
AbstractMidiDevice.this.closeInternal(this);
}
return AbstractMidiDevice.this;
}
final boolean isOpen() {
return open;
}
//$$fb is that a good idea?
//protected void finalize() {
// close();
//}
} // class AbstractReceiver
/**
* Transmitter base class.
* This class especially makes sure the device is closed if it
* has been opened implicitly by a call to MidiSystem.getTransmitter().
* The logic of doing so is actually in closeInternal().
*
* Also, it has some optimizations regarding sending to the Receivers,
* for known Receivers, and managing itself in the TransmitterList.
*/
protected BasicTransmitter() {
}
}
}
}
return receiver;
}
/** Close the Transmitter.
* Here, the call to the magic method closeInternal() takes place.
* Therefore, subclasses that override this method must call
* 'super.close()'.
*/
public final void close() {
AbstractMidiDevice.this.closeInternal(this);
}
}
return AbstractMidiDevice.this;
}
} // class BasicTransmitter
/**
* a class to manage a list of transmitters
*/
final class TransmitterList {
// how many transmitters must be present for optimized
// handling
synchronized(transmitters) {
transmitters.add(t);
}
if (t instanceof BasicTransmitter) {
((BasicTransmitter) t).setTransmitterList(this);
}
}
synchronized(transmitters) {
if (index >= 0) {
}
}
}
synchronized(transmitters) {
// some optimization
if (midiOutReceiver == oldR) {
}
&& (midiOutReceiver == null)) {
}
}
}
// more potential for optimization here
}
/** closes all transmitters and empties the list */
void close() {
synchronized (transmitters) {
}
}
}
/**
* Send this message to all receivers
* status = packedMessage & 0xFF
* data1 = (packedMessage & 0xFF00) >> 8;
* data1 = (packedMessage & 0xFF0000) >> 16;
*/
try {
synchronized(transmitters) {
if (optimizedReceiverCount == size) {
if (midiOutReceiver != null) {
}
} else {
if (TRACE_TRANSMITTER) Printer.println("Sending packed message to "+size+" transmitter's receivers");
for (int i = 0; i < size; i++) {
if (optimizedReceiverCount > 0) {
} else {
}
} else {
}
}
}
}
}
} catch (InvalidMidiDataException e) {
// this happens when invalid data comes over the wire. Ignore it.
}
}
try {
synchronized(transmitters) {
for (int i = 0; i < size; i++) {
//$$fb 2002-04-02: SysexMessages are mutable, so
// an application could change the contents of this object,
// or try to use the object later. So we can't get around object creation
// But the array need not be unique for each FastSysexMessage object,
// because it cannot be modified.
}
}
}
} catch (InvalidMidiDataException e) {
// this happens when invalid data comes over the wire. Ignore it.
return;
}
}
/**
* Send this message to all transmitters
*/
if (message instanceof FastShortMessage) {
return;
}
synchronized(transmitters) {
if (optimizedReceiverCount == size) {
if (midiOutReceiver != null) {
}
} else {
for (int i = 0; i < size; i++) {
//$$fb 2002-04-02: ShortMessages are mutable, so
// an application could change the contents of this object,
// or try to use the object later.
// We violate this spec here, to avoid costly (and gc-intensive)
// object creation for potentially hundred of messages per second.
// The spec should be changed to allow Immutable MidiMessages
// (i.e. throws InvalidStateException or so in setMessage)
}
}
}
}
}
} // TransmitterList
}