jdk/src/share/classes/com/sun/media/sound/EventDispatcher.java
author darcy
Mon, 05 May 2014 23:19:00 -0700
changeset 24548 9c007a986347
parent 18215 b2afd66ce6db
permissions -rw-r--r--
8042256: Fix raw and unchecked lint warnings in com.sun.media.sound Reviewed-by: prr
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
18215
b2afd66ce6db 8006328: Improve robustness of sound classes
serb
parents: 5506
diff changeset
     2
 * Copyright (c) 1998, 2013, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package com.sun.media.sound;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.util.ArrayList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.util.List;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
18215
b2afd66ce6db 8006328: Improve robustness of sound classes
serb
parents: 5506
diff changeset
    31
import javax.sound.midi.ControllerEventListener;
b2afd66ce6db 8006328: Improve robustness of sound classes
serb
parents: 5506
diff changeset
    32
import javax.sound.midi.MetaEventListener;
b2afd66ce6db 8006328: Improve robustness of sound classes
serb
parents: 5506
diff changeset
    33
import javax.sound.midi.MetaMessage;
b2afd66ce6db 8006328: Improve robustness of sound classes
serb
parents: 5506
diff changeset
    34
import javax.sound.midi.ShortMessage;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import javax.sound.sampled.LineEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import javax.sound.sampled.LineListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * EventDispatcher.  Used by various classes in the Java Sound implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * to send events.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * @author David Rivas
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * @author Kara Kytle
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * @author Florian Bomers
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 */
18215
b2afd66ce6db 8006328: Improve robustness of sound classes
serb
parents: 5506
diff changeset
    48
final class EventDispatcher implements Runnable {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
     * time of inactivity until the auto closing clips
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     * are closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    private static final int AUTO_CLOSE_TIME = 5000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     * List of events
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     */
24548
9c007a986347 8042256: Fix raw and unchecked lint warnings in com.sun.media.sound
darcy
parents: 18215
diff changeset
    60
    private final ArrayList<EventInfo> eventQueue = new ArrayList<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * Thread object for this EventDispatcher instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    private Thread thread = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * support for auto-closing Clips
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     */
18215
b2afd66ce6db 8006328: Improve robustness of sound classes
serb
parents: 5506
diff changeset
    72
    private final ArrayList<ClipInfo> autoClosingClips = new ArrayList<ClipInfo>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * support for monitoring data lines
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     */
18215
b2afd66ce6db 8006328: Improve robustness of sound classes
serb
parents: 5506
diff changeset
    77
    private final ArrayList<LineMonitor> lineMonitors = new ArrayList<LineMonitor>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * Approximate interval between calls to LineMonitor.checkLine
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    static final int LINE_MONITOR_TIME = 400;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * This start() method starts an event thread if one is not already active.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    synchronized void start() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        if(thread == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            thread = JSSecurityManager.createThread(this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                                                    "Java Sound Event Dispatcher",   // name
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                                                    true,  // daemon
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                                                    -1,    // priority
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                                                    true); // doStart
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * Invoked when there is at least one event in the queue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * Implement this as a callback to process one event.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     */
18215
b2afd66ce6db 8006328: Improve robustness of sound classes
serb
parents: 5506
diff changeset
   104
    void processEvent(EventInfo eventInfo) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        int count = eventInfo.getListenerCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        // process an LineEvent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        if (eventInfo.getEvent() instanceof LineEvent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            LineEvent event = (LineEvent) eventInfo.getEvent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            if (Printer.debug) Printer.debug("Sending "+event+" to "+count+" listeners");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            for (int i = 0; i < count; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                    ((LineListener) eventInfo.getListener(i)).update(event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                } catch (Throwable t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                    if (Printer.err) t.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        // process a MetaMessage
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        if (eventInfo.getEvent() instanceof MetaMessage) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            MetaMessage event = (MetaMessage)eventInfo.getEvent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            for (int i = 0; i < count; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                    ((MetaEventListener) eventInfo.getListener(i)).meta(event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                } catch (Throwable t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                    if (Printer.err) t.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        // process a Controller or Mode Event
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        if (eventInfo.getEvent() instanceof ShortMessage) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            ShortMessage event = (ShortMessage)eventInfo.getEvent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            int status = event.getStatus();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            // Controller and Mode events have status byte 0xBc, where
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            // c is the channel they are sent on.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            if ((status & 0xF0) == 0xB0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                for (int i = 0; i < count; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                        ((ControllerEventListener) eventInfo.getListener(i)).controlChange(event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                    } catch (Throwable t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                        if (Printer.err) t.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        Printer.err("Unknown event type: " + eventInfo.getEvent());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * Wait until there is something in the event queue to process.  Then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * dispatch the event to the listeners.The entire method does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * need to be synchronized since this includes taking the event out
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * from the queue and processing the event. We only need to provide
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * exclusive access over the code where an event is removed from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     *queue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     */
18215
b2afd66ce6db 8006328: Improve robustness of sound classes
serb
parents: 5506
diff changeset
   165
    void dispatchEvents() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        EventInfo eventInfo = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            // Wait till there is an event in the event queue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                if (eventQueue.size() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                    if (autoClosingClips.size() > 0 || lineMonitors.size() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                        int waitTime = AUTO_CLOSE_TIME;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                        if (lineMonitors.size() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                            waitTime = LINE_MONITOR_TIME;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                        wait(waitTime);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                        wait();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            } catch (InterruptedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            if (eventQueue.size() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                // Remove the event from the queue and dispatch it to the listeners.
24548
9c007a986347 8042256: Fix raw and unchecked lint warnings in com.sun.media.sound
darcy
parents: 18215
diff changeset
   189
                eventInfo = eventQueue.remove(0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        } // end of synchronized
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        if (eventInfo != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            processEvent(eventInfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            if (autoClosingClips.size() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                closeAutoClosingClips();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            if (lineMonitors.size() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                monitorLines();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * Queue the given event in the event queue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    private synchronized void postEvent(EventInfo eventInfo) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        eventQueue.add(eventInfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        notifyAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * A loop to dispatch events.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        while (true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                dispatchEvents();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            } catch (Throwable t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                if (Printer.err) t.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * Send audio and MIDI events.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     */
24548
9c007a986347 8042256: Fix raw and unchecked lint warnings in com.sun.media.sound
darcy
parents: 18215
diff changeset
   233
    void sendAudioEvents(Object event, List<Object> listeners) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        if ((listeners == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            || (listeners.size() == 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            // nothing to do
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        EventInfo eventInfo = new EventInfo(event, listeners);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        postEvent(eventInfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * go through the list of registered auto-closing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * Clip instances and close them, if appropriate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * This method is called in regular intervals
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    private void closeAutoClosingClips() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        synchronized(autoClosingClips) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            if (Printer.debug)Printer.debug("> EventDispatcher.closeAutoClosingClips ("+autoClosingClips.size()+" clips)");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            long currTime = System.currentTimeMillis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            for (int i = autoClosingClips.size()-1; i >= 0 ; i--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                ClipInfo info = autoClosingClips.get(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                if (info.isExpired(currTime)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                    AutoClosingClip clip = info.getClip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                    // sanity check
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                    if (!clip.isOpen() || !clip.isAutoClosing()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                        if (Printer.debug)Printer.debug("EventDispatcher: removing clip "+clip+"  isOpen:"+clip.isOpen());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                        autoClosingClips.remove(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                    else if (!clip.isRunning() && !clip.isActive() && clip.isAutoClosing()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                        if (Printer.debug)Printer.debug("EventDispatcher: closing clip "+clip);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                        clip.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                        if (Printer.debug)Printer.debug("Doing nothing with clip "+clip+":");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                        if (Printer.debug)Printer.debug("  open="+clip.isOpen()+", autoclosing="+clip.isAutoClosing());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                        if (Printer.debug)Printer.debug("  isRunning="+clip.isRunning()+", isActive="+clip.isActive());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                    if (Printer.debug)Printer.debug("EventDispatcher: clip "+info.getClip()+" not yet expired");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        if (Printer.debug)Printer.debug("< EventDispatcher.closeAutoClosingClips ("+autoClosingClips.size()+" clips)");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    private int getAutoClosingClipIndex(AutoClosingClip clip) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        synchronized(autoClosingClips) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            for (int i = autoClosingClips.size()-1; i >= 0; i--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                if (clip.equals(autoClosingClips.get(i).getClip())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                    return i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * called from auto-closing clips when one of their open() method is called
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
    void autoClosingClipOpened(AutoClosingClip clip) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        if (Printer.debug)Printer.debug("> EventDispatcher.autoClosingClipOpened ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        int index = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        synchronized(autoClosingClips) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            index = getAutoClosingClipIndex(clip);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            if (index == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                if (Printer.debug)Printer.debug("EventDispatcher: adding auto-closing clip "+clip);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                autoClosingClips.add(new ClipInfo(clip));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        if (index == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                // this is only for the case that the first clip is set to autoclosing,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                // and it is already open, and nothing is done with it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                // EventDispatcher.process() method would block in wait() and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                // never close this first clip, keeping the device open.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                notifyAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        if (Printer.debug)Printer.debug("< EventDispatcher.autoClosingClipOpened finished("+autoClosingClips.size()+" clips)");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * called from auto-closing clips when their closed() method is called
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    void autoClosingClipClosed(AutoClosingClip clip) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        // nothing to do -- is removed from arraylist above
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
    // ////////////////////////// Line Monitoring Support /////////////////// //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     * go through the list of registered line monitors
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     * and call their checkLine method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     * This method is called in regular intervals
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
    private void monitorLines() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        synchronized(lineMonitors) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            if (Printer.debug)Printer.debug("> EventDispatcher.monitorLines ("+lineMonitors.size()+" monitors)");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
            for (int i = 0; i < lineMonitors.size(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                lineMonitors.get(i).checkLine();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        if (Printer.debug)Printer.debug("< EventDispatcher.monitorLines("+lineMonitors.size()+" monitors)");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     * Add this LineMonitor instance to the list of monitors
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    void addLineMonitor(LineMonitor lm) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        if (Printer.trace)Printer.trace("> EventDispatcher.addLineMonitor("+lm+")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        synchronized(lineMonitors) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
            if (lineMonitors.indexOf(lm) >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                if (Printer.trace)Printer.trace("< EventDispatcher.addLineMonitor finished -- this monitor already exists!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
            if (Printer.debug)Printer.debug("EventDispatcher: adding line monitor "+lm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
            lineMonitors.add(lm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
            // need to interrupt the infinite wait()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
            notifyAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        if (Printer.debug)Printer.debug("< EventDispatcher.addLineMonitor finished -- now ("+lineMonitors.size()+" monitors)");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     * Remove this LineMonitor instance from the list of monitors
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
    void removeLineMonitor(LineMonitor lm) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        if (Printer.trace)Printer.trace("> EventDispatcher.removeLineMonitor("+lm+")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        synchronized(lineMonitors) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
            if (lineMonitors.indexOf(lm) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
                if (Printer.trace)Printer.trace("< EventDispatcher.removeLineMonitor finished -- this monitor does not exist!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
            if (Printer.debug)Printer.debug("EventDispatcher: removing line monitor "+lm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
            lineMonitors.remove(lm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        if (Printer.debug)Printer.debug("< EventDispatcher.removeLineMonitor finished -- now ("+lineMonitors.size()+" monitors)");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
    // /////////////////////////////////// INNER CLASSES ////////////////////////////////////////// //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     * Container for an event and a set of listeners to deliver it to.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
    private class EventInfo {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
18215
b2afd66ce6db 8006328: Improve robustness of sound classes
serb
parents: 5506
diff changeset
   387
        private final Object event;
b2afd66ce6db 8006328: Improve robustness of sound classes
serb
parents: 5506
diff changeset
   388
        private final Object[] listeners;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
         * Create a new instance of this event Info class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
         * @param event the event to be dispatched
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
         * @param listeners listener list; will be copied
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
         */
24548
9c007a986347 8042256: Fix raw and unchecked lint warnings in com.sun.media.sound
darcy
parents: 18215
diff changeset
   395
        EventInfo(Object event, List<Object> listeners) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
            this.event = event;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
            this.listeners = listeners.toArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        Object getEvent() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
            return event;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        int getListenerCount() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
            return listeners.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        Object getListener(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
            return listeners[index];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
    } // class EventInfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     * Container for a clip with its expiration time
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
    private class ClipInfo {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
18215
b2afd66ce6db 8006328: Improve robustness of sound classes
serb
parents: 5506
diff changeset
   420
        private final AutoClosingClip clip;
b2afd66ce6db 8006328: Improve robustness of sound classes
serb
parents: 5506
diff changeset
   421
        private final long expiration;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
         * Create a new instance of this clip Info class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        ClipInfo(AutoClosingClip clip) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
            this.clip = clip;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
            this.expiration = System.currentTimeMillis() + AUTO_CLOSE_TIME;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        AutoClosingClip getClip() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
            return clip;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
        boolean isExpired(long currTime) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
            return currTime > expiration;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
    } // class ClipInfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
     * Interface that a class that wants to get regular
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
     * line monitor events implements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
    interface LineMonitor {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
         * Called by event dispatcher in regular intervals
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
        public void checkLine();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
} // class EventDispatcher