PageRenderTime 37ms CodeModel.GetById 14ms app.highlight 19ms RepoModel.GetById 1ms app.codeStats 0ms

/libs/ObjectAL/OpenAL/ALDevice.m

http://github.com/kstenerud/ObjectAL-for-iPhone
Objective C | 230 lines | 151 code | 49 blank | 30 comment | 11 complexity | 202796a19f2e1edc7f05a50205b9568e MD5 | raw file
  1//
  2//  ALDevice.m
  3//  ObjectAL
  4//
  5//  Created by Karl Stenerud on 10-01-09.
  6//
  7// Copyright 2009 Karl Stenerud
  8//
  9// Licensed under the Apache License, Version 2.0 (the "License");
 10// you may not use this file except in compliance with the License.
 11// You may obtain a copy of the License at
 12//
 13// http://www.apache.org/licenses/LICENSE-2.0
 14//
 15// Unless required by applicable law or agreed to in writing, software
 16// distributed under the License is distributed on an "AS IS" BASIS,
 17// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 18// See the License for the specific language governing permissions and
 19// limitations under the License.
 20//
 21// Note: You are NOT required to make the license available from within your
 22// iOS application. Including it in your project is sufficient.
 23//
 24// Attribution is not required, but appreciated :)
 25//
 26
 27#import "ALDevice.h"
 28#import "NSMutableArray+WeakReferences.h"
 29#import "ObjectALMacros.h"
 30#import "ALWrapper.h"
 31#import "OpenALManager.h"
 32
 33
 34/**
 35 * (INTERNAL USE) Private methods for ALDevice.
 36 */
 37@interface ALDevice (Private)
 38
 39/** (INTERNAL USE) Close any resources belonging to the OS.
 40 */
 41- (void) closeOSResources;
 42
 43@end
 44
 45
 46@implementation ALDevice
 47
 48#pragma mark Object Management
 49
 50+ (id) deviceWithDeviceSpecifier:(NSString*) deviceSpecifier
 51{
 52	return [[[self alloc] initWithDeviceSpecifier:deviceSpecifier] autorelease];
 53}
 54
 55- (id) initWithDeviceSpecifier:(NSString*) deviceSpecifier
 56{
 57	if(nil != (self = [super init]))
 58	{
 59		OAL_LOG_DEBUG(@"%@: Init device %@", self, deviceSpecifier);
 60
 61		device = [ALWrapper openDevice:deviceSpecifier];
 62		if(nil == device)
 63		{
 64			OAL_LOG_ERROR(@"%@: Failed to init device %@. Returning nil", self, deviceSpecifier);
 65			[self release];
 66			return nil;
 67		}
 68
 69		suspendHandler = [[OALSuspendHandler alloc] initWithTarget:nil selector:nil];
 70		
 71		contexts = [NSMutableArray newMutableArrayUsingWeakReferencesWithCapacity:5];
 72			
 73		[[OpenALManager sharedInstance] notifyDeviceInitializing:self];
 74		[[OpenALManager sharedInstance] addSuspendListener:self];
 75	}
 76	return self;
 77}
 78
 79- (void) dealloc
 80{
 81	OAL_LOG_DEBUG(@"%@: Dealloc", self);
 82
 83	[[OpenALManager sharedInstance] removeSuspendListener:self];
 84	[[OpenALManager sharedInstance] notifyDeviceDeallocating:self];
 85
 86	[self closeOSResources];
 87	
 88	[contexts release];
 89	[suspendHandler release];
 90
 91	[super dealloc];
 92}
 93
 94- (void) closeOSResources
 95{
 96	OPTIONALLY_SYNCHRONIZED(self)
 97	{
 98		if(nil != device)
 99		{
100			[ALWrapper closeDevice:device];
101			device = nil;
102		}
103	}
104}
105
106- (void) close
107{
108	OPTIONALLY_SYNCHRONIZED(self)
109	{
110		if(nil != contexts)
111		{
112			[contexts makeObjectsPerformSelector:@selector(close)];
113			[contexts release];
114			contexts = nil;
115
116			[self closeOSResources];
117		}
118	}
119}
120
121
122#pragma mark Properties
123
124@synthesize contexts;
125
126@synthesize device;
127
128- (NSArray*) extensions
129{
130	return [ALWrapper getSpaceSeparatedStringList:device attribute:ALC_EXTENSIONS];
131}
132
133- (int) majorVersion
134{
135	return [ALWrapper getInteger:device attribute:ALC_MAJOR_VERSION];
136}
137
138- (int) minorVersion
139{
140	return [ALWrapper getInteger:device attribute:ALC_MINOR_VERSION];
141}
142
143#pragma mark Suspend Handler
144
145- (void) addSuspendListener:(id<OALSuspendListener>) listener
146{
147	[suspendHandler addSuspendListener:listener];
148}
149
150- (void) removeSuspendListener:(id<OALSuspendListener>) listener
151{
152	[suspendHandler removeSuspendListener:listener];
153}
154
155- (bool) manuallySuspended
156{
157	return suspendHandler.manuallySuspended;
158}
159
160- (void) setManuallySuspended:(bool) value
161{
162	suspendHandler.manuallySuspended = value;
163}
164
165- (bool) interrupted
166{
167	return suspendHandler.interrupted;
168}
169
170- (void) setInterrupted:(bool) value
171{
172	suspendHandler.interrupted = value;
173}
174
175- (bool) suspended
176{
177	return suspendHandler.suspended;
178}
179
180
181#pragma mark Extensions
182
183- (bool) isExtensionPresent:(NSString*) name
184{
185	return [ALWrapper isExtensionPresent:device name:name];
186}
187
188- (void*) getProcAddress:(NSString*) functionName
189{
190	return [ALWrapper getProcAddress:device name:functionName];
191}
192
193
194#pragma mark Utility
195
196- (void) clearBuffers
197{
198	OPTIONALLY_SYNCHRONIZED(self)
199	{
200		for(ALContext* context in contexts)
201		{
202			[context clearBuffers];
203		}
204	}
205}
206
207
208#pragma mark Internal Use
209
210- (void) notifyContextInitializing:(ALContext*) context
211{
212	OPTIONALLY_SYNCHRONIZED(self)
213	{
214		[contexts addObject:context];
215	}
216}
217
218- (void) notifyContextDeallocating:(ALContext*) context
219{
220	OPTIONALLY_SYNCHRONIZED(self)
221	{
222		if([OpenALManager sharedInstance].currentContext == context)
223		{
224			[OpenALManager sharedInstance].currentContext = nil;
225		}
226		[contexts removeObject:context];
227	}
228}
229
230@end