/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. #import "ALDevice.h"
  27. #import "NSMutableArray+WeakReferences.h"
  28. #import "ObjectALMacros.h"
  29. #import "ALWrapper.h"
  30. #import "OpenALManager.h"
  31. /**
  32. * (INTERNAL USE) Private methods for ALDevice.
  33. */
  34. @interface ALDevice (Private)
  35. /** (INTERNAL USE) Close any resources belonging to the OS.
  36. */
  37. - (void) closeOSResources;
  38. @end
  39. @implementation ALDevice
  40. #pragma mark Object Management
  41. + (id) deviceWithDeviceSpecifier:(NSString*) deviceSpecifier
  42. {
  43. return [[[self alloc] initWithDeviceSpecifier:deviceSpecifier] autorelease];
  44. }
  45. - (id) initWithDeviceSpecifier:(NSString*) deviceSpecifier
  46. {
  47. if(nil != (self = [super init]))
  48. {
  49. OAL_LOG_DEBUG(@"%@: Init device %@", self, deviceSpecifier);
  50. device = [ALWrapper openDevice:deviceSpecifier];
  51. if(nil == device)
  52. {
  53. OAL_LOG_ERROR(@"%@: Failed to init device %@. Returning nil", self, deviceSpecifier);
  54. [self release];
  55. return nil;
  56. }
  57. suspendHandler = [[OALSuspendHandler alloc] initWithTarget:nil selector:nil];
  58. contexts = [NSMutableArray newMutableArrayUsingWeakReferencesWithCapacity:5];
  59. [[OpenALManager sharedInstance] notifyDeviceInitializing:self];
  60. [[OpenALManager sharedInstance] addSuspendListener:self];
  61. }
  62. return self;
  63. }
  64. - (void) dealloc
  65. {
  66. OAL_LOG_DEBUG(@"%@: Dealloc", self);
  67. [[OpenALManager sharedInstance] removeSuspendListener:self];
  68. [[OpenALManager sharedInstance] notifyDeviceDeallocating:self];
  69. [self closeOSResources];
  70. [contexts release];
  71. [suspendHandler release];
  72. [super dealloc];
  73. }
  74. - (void) closeOSResources
  75. {
  76. OPTIONALLY_SYNCHRONIZED(self)
  77. {
  78. if(nil != device)
  79. {
  80. [ALWrapper closeDevice:device];
  81. device = nil;
  82. }
  83. }
  84. }
  85. - (void) close
  86. {
  87. OPTIONALLY_SYNCHRONIZED(self)
  88. {
  89. if(nil != contexts)
  90. {
  91. [contexts makeObjectsPerformSelector:@selector(close)];
  92. [contexts release];
  93. contexts = nil;
  94. [self closeOSResources];
  95. }
  96. }
  97. }
  98. #pragma mark Properties
  99. @synthesize contexts;
  100. @synthesize device;
  101. - (NSArray*) extensions
  102. {
  103. return [ALWrapper getSpaceSeparatedStringList:device attribute:ALC_EXTENSIONS];
  104. }
  105. - (int) majorVersion
  106. {
  107. return [ALWrapper getInteger:device attribute:ALC_MAJOR_VERSION];
  108. }
  109. - (int) minorVersion
  110. {
  111. return [ALWrapper getInteger:device attribute:ALC_MINOR_VERSION];
  112. }
  113. #pragma mark Suspend Handler
  114. - (void) addSuspendListener:(id<OALSuspendListener>) listener
  115. {
  116. [suspendHandler addSuspendListener:listener];
  117. }
  118. - (void) removeSuspendListener:(id<OALSuspendListener>) listener
  119. {
  120. [suspendHandler removeSuspendListener:listener];
  121. }
  122. - (bool) manuallySuspended
  123. {
  124. return suspendHandler.manuallySuspended;
  125. }
  126. - (void) setManuallySuspended:(bool) value
  127. {
  128. suspendHandler.manuallySuspended = value;
  129. }
  130. - (bool) interrupted
  131. {
  132. return suspendHandler.interrupted;
  133. }
  134. - (void) setInterrupted:(bool) value
  135. {
  136. suspendHandler.interrupted = value;
  137. }
  138. - (bool) suspended
  139. {
  140. return suspendHandler.suspended;
  141. }
  142. #pragma mark Extensions
  143. - (bool) isExtensionPresent:(NSString*) name
  144. {
  145. return [ALWrapper isExtensionPresent:device name:name];
  146. }
  147. - (void*) getProcAddress:(NSString*) functionName
  148. {
  149. return [ALWrapper getProcAddress:device name:functionName];
  150. }
  151. #pragma mark Utility
  152. - (void) clearBuffers
  153. {
  154. OPTIONALLY_SYNCHRONIZED(self)
  155. {
  156. for(ALContext* context in contexts)
  157. {
  158. [context clearBuffers];
  159. }
  160. }
  161. }
  162. #pragma mark Internal Use
  163. - (void) notifyContextInitializing:(ALContext*) context
  164. {
  165. OPTIONALLY_SYNCHRONIZED(self)
  166. {
  167. [contexts addObject:context];
  168. }
  169. }
  170. - (void) notifyContextDeallocating:(ALContext*) context
  171. {
  172. OPTIONALLY_SYNCHRONIZED(self)
  173. {
  174. if([OpenALManager sharedInstance].currentContext == context)
  175. {
  176. [OpenALManager sharedInstance].currentContext = nil;
  177. }
  178. [contexts removeObject:context];
  179. }
  180. }
  181. @end