1 /*
2  * Copyright (c) 2002-2008 LWJGL Project
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are
7  * met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  *   notice, this list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above copyright
13  *   notice, this list of conditions and the following disclaimer in the
14  *   documentation and/or other materials provided with the distribution.
15  *
16  * * Neither the name of 'LWJGL' nor the names of
17  *   its contributors may be used to endorse or promote products derived
18  *   from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 package org.lwjgl.openal;
33 
34 import java.util.HashMap;
35 import java.util.Iterator;
36 
37 /**
38  * The ALCdevice class represents a device opened in OpenAL space.
39  *
40  * ALC introduces the notion of a Device. A Device can be, depending on the
41  * implementation, a hardware device, or a daemon/OS service/actual server. This
42  * mechanism also permits different drivers (and hardware) to coexist within the same
43  * system, as well as allowing several applications to share system resources for audio,
44  * including a single hardware output device. The details are left to the implementation,
45  * which has to map the available backends to unique device specifiers.
46  *
47  * @author Brian Matzon <brian@matzon.dk>
48  * @version $Revision$
49  * $Id$
50  */
51 public final class ALCdevice {
52 
53     /** Address of actual device */
54     final long device;
55 
56     /** Whether this device is valid */
57     private boolean valid;
58 
59 	/** List of contexts belonging to the device */
60 	private final HashMap<Long, ALCcontext> contexts = new HashMap<Long, ALCcontext>();
61 
62     /**
63      * Creates a new instance of ALCdevice
64      *
65      * @param device address of actual device
66      */
ALCdevice(long device)67     ALCdevice(long device) {
68         this.device = device;
69         this.valid = true;
70     }
71 
72     /*
73      * @see java.lang.Object#equals(java.lang.Object)
74      */
equals(Object device)75 	public boolean equals(Object device) {
76 		if(device instanceof ALCdevice) {
77 			return ((ALCdevice)device).device == this.device;
78 		}
79 		return super.equals(device);
80 	}
81 
82 	/**
83 	 * Adds a context to the device
84 	 *
85 	 * @param context context to add to the list of contexts for this device
86 	 */
addContext(ALCcontext context)87 	void addContext(ALCcontext context) {
88 		synchronized (contexts) {
89 			contexts.put(context.context, context);
90 		}
91 	}
92 
93 	/**
94 	 * Remove context associated with device
95 	 *
96 	 * @param context Context to disassociate with device
97 	 */
removeContext(ALCcontext context)98 	void removeContext(ALCcontext context) {
99 		synchronized (contexts) {
100 			contexts.remove(context.context);
101 		}
102 	}
103 
104 	/**
105 	 * Marks this device and all of its contexts invalid
106 	 */
setInvalid()107 	void setInvalid() {
108 		valid = false;
109 		synchronized (contexts) {
110 			for ( ALCcontext context : contexts.values() )
111 				context.setInvalid();
112 		}
113 		contexts.clear();
114 	}
115 
116 	/**
117 	 * @return true if this device is still valid
118 	 */
isValid()119 	public boolean isValid() {
120 		return valid;
121 	}
122 }
123