|
|
1.1 root 1: #include <IOKit/audio/IOAudioManager.h>
2:
3: #include <libkern/c++/OSCollectionIterator.h>
4:
5: #define DEFAULT_INITIAL_VOLUME MASTER_VOLUME_MAX
6: #define DEFAULT_INITIAL_MUTE false
7: #define DEFAULT_INITIAL_INCREMENT 4096
8:
9: static IOAudioManager *amInstance = NULL;
10:
11: OSDefineMetaClassAndStructors(IOAudioManager, IOService);
12:
13: IOAudioManager *IOAudioManager::sharedInstance()
14: {
15: return amInstance;
16: }
17:
18: bool IOAudioManager::init(OSDictionary *properties)
19: {
20: if (!IOService::init(properties)) {
21: return false;
22: }
23:
24: masterVolumeLeft = DEFAULT_INITIAL_VOLUME;
25: masterVolumeRight = DEFAULT_INITIAL_VOLUME;
26: masterMute = DEFAULT_INITIAL_MUTE;
27: masterVolumeIncrement = DEFAULT_INITIAL_INCREMENT;
28:
29: setProperty(kMasterVolumeLeft, (unsigned long long)masterVolumeLeft, sizeof(masterVolumeLeft)*8);
30: setProperty(kMasterVolumeRight, (unsigned long long)masterVolumeRight, sizeof(masterVolumeRight)*8);
31: setProperty(kMasterMute, (unsigned long long)masterMute, sizeof(masterMute)*8);
32: setProperty(kMasterVolumeIncrement, (unsigned long long)masterVolumeIncrement, sizeof(masterVolumeIncrement)*8);
33:
34: audioControllers = NULL;
35: publishNotify = NULL;
36: driverLock = NULL;
37:
38: return true;
39: }
40:
41: void IOAudioManager::free()
42: {
43: if (audioControllers) {
44: audioControllers->release();
45: }
46:
47: if (publishNotify) {
48: publishNotify->release();
49: }
50:
51: if (driverLock) {
52: IOLockFree(driverLock);
53: }
54:
55: IOService::free();
56: }
57:
58: bool IOAudioManager::start(IOService *provider)
59: {
60: bool success = false;
61:
62: amInstance = this;
63:
64: do {
65: if (!IOService::start(provider)) {
66: break;
67: }
68:
69: audioControllers = OSArray::withCapacity(1);
70:
71: driverLock = IOLockAlloc();
72:
73: publishNotify = addNotification(gIOPublishNotification,
74: serviceMatching("IOAudioController"),
75: (IOServiceNotificationHandler) &audioPublishNotificationHandler,
76: this,
77: 0);
78:
79: if (!publishNotify || !driverLock) {
80: break;
81: }
82:
83: IOLockInit(driverLock);
84:
85: success = true;
86: } while (false);
87:
88: if (!success) {
89: amInstance = NULL;
90: }
91:
92: return success;
93: }
94:
95: IOReturn IOAudioManager::setProperties( OSObject * properties )
96: {
97: OSDictionary *props;
98: IOReturn result = kIOReturnSuccess;
99:
100: if (properties && (props = OSDynamicCast(OSDictionary, properties))) {
101: OSCollectionIterator *iterator;
102: OSObject *iteratorKey;
103:
104: iterator = OSCollectionIterator::withCollection(props);
105: while ((iteratorKey = iterator->getNextObject())) {
106: OSSymbol *key;
107:
108: key = OSDynamicCast(OSSymbol, iteratorKey);
109: if (key) {
110: OSNumber *value;
111: value = OSDynamicCast(OSNumber, props->getObject(key));
112:
113: if (value) {
114: if (key->isEqualTo(kMasterVolumeLeft)) {
115: setMasterVolumeLeft(value->unsigned16BitValue());
116: } else if (key->isEqualTo(kMasterVolumeRight)) {
117: setMasterVolumeRight(value->unsigned16BitValue());
118: } else if (key->isEqualTo(kMasterMute)) {
119: setMasterMute(value->unsigned8BitValue());
120: } else if (key->isEqualTo(kMasterVolumeIncrement)) {
121: setMasterVolumeIncrement(value->unsigned16BitValue());
122: }
123: } else {
124: result = kIOReturnBadArgument;
125: break;
126: }
127: } else {
128: result = kIOReturnBadArgument;
129: break;
130: }
131: }
132: } else {
133: result = kIOReturnBadArgument;
134: }
135:
136: return result;
137: }
138:
139: bool IOAudioManager::audioPublishNotificationHandler(IOAudioManager *self,
140: void *ref,
141: IOService *newService)
142: {
143: self->attach(newService);
144:
145: if (OSDynamicCast(IOAudioController, newService)) {
146: self->registerAudioController((IOAudioController *)newService);
147: }
148:
149: return true;
150: }
151:
152: bool IOAudioManager::registerAudioController(IOAudioController *controller)
153: {
154: IOTakeLock(driverLock);
155:
156: audioControllers->setObject(controller);
157:
158: controller->setMasterMute(masterMute);
159: controller->setMasterVolumeLeft(masterVolumeLeft);
160: controller->setMasterVolumeRight(masterVolumeRight);
161:
162: IOUnlock(driverLock);
163:
164: return true;
165: }
166:
167: UInt16 IOAudioManager::getMasterVolumeLeft()
168: {
169: return masterVolumeLeft;
170: }
171:
172: UInt16 IOAudioManager::getMasterVolumeRight()
173: {
174: return masterVolumeRight;
175: }
176:
177: UInt16 IOAudioManager::setMasterVolumeLeft(UInt16 newMasterVolumeLeft)
178: {
179: UInt16 oldMasterVolumeLeft;
180:
181: oldMasterVolumeLeft = masterVolumeLeft;
182: masterVolumeLeft = newMasterVolumeLeft;
183:
184: if (masterVolumeLeft != oldMasterVolumeLeft) {
185: OSCollectionIterator *iter;
186:
187: setProperty(kMasterVolumeLeft, (unsigned long long)masterVolumeLeft, sizeof(masterVolumeLeft)*8);
188:
189: IOTakeLock(driverLock);
190:
191: iter = OSCollectionIterator::withCollection(audioControllers);
192: if (iter) {
193: IOAudioController *controller;
194:
195: while ((controller = (IOAudioController *)iter->getNextObject()) != NULL) {
196: controller->setMasterVolumeLeft(masterVolumeLeft);
197: }
198:
199: iter->release();
200: }
201:
202: IOUnlock(driverLock);
203: }
204:
205: return oldMasterVolumeLeft;
206: }
207:
208: UInt16 IOAudioManager::setMasterVolumeRight(UInt16 newMasterVolumeRight)
209: {
210: UInt16 oldMasterVolumeRight;
211:
212: oldMasterVolumeRight = masterVolumeRight;
213: masterVolumeRight = newMasterVolumeRight;
214:
215: if (masterVolumeRight != oldMasterVolumeRight) {
216: OSCollectionIterator *iter;
217:
218: setProperty(kMasterVolumeRight, (unsigned long long)masterVolumeRight, sizeof(masterVolumeRight)*8);
219:
220: IOTakeLock(driverLock);
221:
222: iter = OSCollectionIterator::withCollection(audioControllers);
223: if (iter) {
224: IOAudioController *controller;
225:
226: while ((controller = (IOAudioController *)iter->getNextObject()) != NULL) {
227: controller->setMasterVolumeRight(masterVolumeRight);
228: }
229:
230: iter->release();
231: }
232:
233: IOUnlock(driverLock);
234: }
235:
236: return oldMasterVolumeRight;
237: }
238:
239: bool IOAudioManager::getMasterMute()
240: {
241: return masterMute;
242: }
243:
244: bool IOAudioManager::setMasterMute(bool newMasterMute)
245: {
246: bool oldMasterMute;
247:
248: oldMasterMute = masterMute;
249: masterMute = newMasterMute;
250:
251: if (masterMute != oldMasterMute) {
252: OSCollectionIterator *iter;
253:
254: setProperty(kMasterMute, (unsigned long long)masterMute, sizeof(masterMute)*8);
255:
256: IOTakeLock(driverLock);
257:
258: iter = OSCollectionIterator::withCollection(audioControllers);
259: if (iter) {
260: IOAudioController *controller;
261:
262: while ((controller = (IOAudioController *)iter->getNextObject()) != NULL) {
263: controller->setMasterMute(masterMute);
264: }
265:
266: iter->release();
267: }
268:
269: IOUnlock(driverLock);
270: }
271:
272: return oldMasterMute;
273: }
274:
275: UInt16 IOAudioManager::getMasterVolumeIncrement()
276: {
277: return masterVolumeIncrement;
278: }
279:
280: UInt16 IOAudioManager::setMasterVolumeIncrement(UInt16 newMasterVolumeIncrement)
281: {
282: UInt16 oldMasterVolumeIncrement;
283:
284: oldMasterVolumeIncrement = masterVolumeIncrement;
285: masterVolumeIncrement = newMasterVolumeIncrement;
286:
287: setProperty(kMasterVolumeIncrement, (unsigned long long)masterVolumeIncrement, sizeof(masterVolumeIncrement)*8);
288:
289: return oldMasterVolumeIncrement;
290: }
291:
292: UInt16 IOAudioManager::incrementMasterVolume()
293: {
294: SInt32 newMasterVolumeLeft;
295: SInt32 newMasterVolumeRight;
296:
297: newMasterVolumeLeft = masterVolumeLeft + masterVolumeIncrement;
298: newMasterVolumeRight = masterVolumeRight + masterVolumeIncrement;
299:
300: if (newMasterVolumeLeft > MASTER_VOLUME_MAX) {
301: newMasterVolumeRight = newMasterVolumeRight - (newMasterVolumeLeft - MASTER_VOLUME_MAX);
302: newMasterVolumeLeft = MASTER_VOLUME_MAX;
303: }
304:
305: if (newMasterVolumeRight > MASTER_VOLUME_MAX) {
306: newMasterVolumeLeft = newMasterVolumeLeft - (newMasterVolumeRight - MASTER_VOLUME_MAX);
307: newMasterVolumeRight = MASTER_VOLUME_MAX;
308: }
309:
310: setMasterVolumeLeft(newMasterVolumeLeft);
311: setMasterVolumeRight(newMasterVolumeRight);
312:
313: return masterVolumeLeft > masterVolumeRight ? masterVolumeLeft : masterVolumeRight;
314: }
315:
316: UInt16 IOAudioManager::decrementMasterVolume()
317: {
318: SInt32 newMasterVolumeLeft;
319: SInt32 newMasterVolumeRight;
320:
321: newMasterVolumeLeft = masterVolumeLeft - masterVolumeIncrement;
322: newMasterVolumeRight = masterVolumeRight - masterVolumeIncrement;
323:
324: if (newMasterVolumeLeft < MASTER_VOLUME_MIN) {
325: newMasterVolumeRight = newMasterVolumeRight + (MASTER_VOLUME_MIN - newMasterVolumeLeft);
326: newMasterVolumeLeft = MASTER_VOLUME_MIN;
327: }
328:
329: if (newMasterVolumeRight < MASTER_VOLUME_MIN) {
330: newMasterVolumeLeft = newMasterVolumeLeft + (MASTER_VOLUME_MIN - newMasterVolumeRight);
331: newMasterVolumeRight = MASTER_VOLUME_MIN;
332: }
333:
334: setMasterVolumeLeft(newMasterVolumeLeft);
335: setMasterVolumeRight(newMasterVolumeRight);
336:
337: return masterVolumeLeft > masterVolumeRight ? masterVolumeLeft : masterVolumeRight;
338: }
339:
340: bool IOAudioManager::toggleMasterMute()
341: {
342: setMasterMute(!masterMute);
343: return masterMute;
344: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.