-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathTLDeviceManager.m
More file actions
414 lines (375 loc) · 16.4 KB
/
TLDeviceManager.m
File metadata and controls
414 lines (375 loc) · 16.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
#import "TLDeviceManager.h"
#import <dlfcn.h>
/**
* TLDeviceManager
*
* Manages the camera device and LED control for TrollLEDs.
* Handles initialization, stream setup, and property setting for flashlight LEDs.
*/
@implementation TLDeviceManager
@synthesize currentError = _currentError;
/**
* Ensures resources are cleaned up when the object is deallocated.
* Automatically releases the camera device stream if still initialized.
*/
- (void)dealloc {
// Ensure resources are cleaned up when the object is deallocated
if (initialized) {
[self releaseStream];
}
}
/**
* Initializes the camera device vendor.
* Loads the required framework (CMCapture or Celestial) and obtains the vendor instance.
* Sets currentError if initialization fails.
*/
- (void)initVendor {
void *cmCaptureHandle = dlopen("/System/Library/PrivateFrameworks/CMCapture.framework/CMCapture", RTLD_NOW);
if (!cmCaptureHandle) {
void *celestialHandle = dlopen("/System/Library/PrivateFrameworks/Celestial.framework/Celestial", RTLD_NOW);
if (!celestialHandle) {
_currentError = @"Failed to load camera framework. Your device may not be supported.";
return;
}
}
BWFigCaptureDeviceVendorClass = NSClassFromString(@"BWFigCaptureDeviceVendor");
if (!BWFigCaptureDeviceVendorClass) {
_currentError = @"Camera device vendor class not found. This iOS version may not be supported.";
return;
}
if ([BWFigCaptureDeviceVendorClass respondsToSelector:@selector(sharedCaptureDeviceVendor)])
vendor = [BWFigCaptureDeviceVendorClass sharedCaptureDeviceVendor];
else if ([BWFigCaptureDeviceVendorClass respondsToSelector:@selector(sharedInstance)])
vendor = [BWFigCaptureDeviceVendorClass sharedInstance];
if (!vendor) {
_currentError = @"Failed to obtain camera device vendor. The camera may be in use by another app.";
return;
}
pid = getpid();
}
/**
* Checks the device type to determine if it uses legacy LEDs or modern quad-LEDs.
* Legacy devices use AppleH6CamIn or AppleH9CamIn IOKit services.
* Sets currentError if IOKit framework cannot be loaded.
*/
- (void)checkType {
void *IOKit = dlopen("/System/Library/Frameworks/IOKit.framework/IOKit", RTLD_NOW);
if (!IOKit) {
_currentError = @"Failed to load IOKit framework.";
return;
}
mach_port_t *kIOMasterPortDefault = (mach_port_t *)dlsym(IOKit, "kIOMasterPortDefault");
CFMutableDictionaryRef (*IOServiceMatching)(const char *name) =
(CFMutableDictionaryRef(*)(const char *))dlsym(IOKit, "IOServiceMatching");
mach_port_t (*IOServiceGetMatchingService)(mach_port_t masterPort, CFDictionaryRef matching) =
(mach_port_t(*)(mach_port_t, CFDictionaryRef))dlsym(IOKit, "IOServiceGetMatchingService");
kern_return_t (*IOObjectRelease)(mach_port_t object) =
(kern_return_t(*)(mach_port_t))dlsym(IOKit, "IOObjectRelease");
if (!kIOMasterPortDefault || !IOServiceMatching || !IOServiceGetMatchingService || !IOObjectRelease) {
_currentError = @"Failed to load required IOKit symbols.";
return;
}
mach_port_t h9 = IOServiceGetMatchingService(*kIOMasterPortDefault, IOServiceMatching("AppleH9CamIn"));
mach_port_t h6 = IOServiceGetMatchingService(*kIOMasterPortDefault, IOServiceMatching("AppleH6CamIn"));
legacyLEDs = h9 || h6;
if (h9)
IOObjectRelease(h9);
if (h6)
if (h6)
IOObjectRelease(h6);
}
/**
* Sets up the camera device stream for LED control.
* This is the main initialization method that establishes connection to the camera device.
*
* @return YES if setup was successful, NO if it failed (with currentError set)
*
* The method handles multiple iOS versions and API variations, trying different
* selectors based on what's available. Properly cleans up resources on failure.
*/
- (BOOL)setupStream {
if (initialized)
return YES;
// Clear any previous errors
_currentError = nil;
if (!vendor && !BWFigCaptureDeviceVendorClass) {
_currentError = @"Device vendor not initialized. Please restart the app.";
return NO;
}
NSString *clientDescription = @"TrollLEDs application";
if ([BWFigCaptureDeviceVendorClass
respondsToSelector:@selector
(copyDefaultVideoDeviceWithStealingBehavior:forPID:clientIDOut:withDeviceAvailabilityChangedHandler:)]) {
deviceRef = [BWFigCaptureDeviceVendorClass copyDefaultVideoDeviceWithStealingBehavior:1
forPID:pid
clientIDOut:&client
withDeviceAvailabilityChangedHandler:NULL];
if (!deviceRef) {
_currentError = @"Failed to access camera device. The camera may be in use by another app.";
return NO;
}
if ([BWFigCaptureDeviceVendorClass
respondsToSelector:@selector(copyStreamForFlashlightWithPosition:deviceType:forDevice:)])
streamRef = [BWFigCaptureDeviceVendorClass copyStreamForFlashlightWithPosition:1
deviceType:2
forDevice:deviceRef];
else
streamRef = [BWFigCaptureDeviceVendorClass copyStreamWithPosition:1 deviceType:2 forDevice:deviceRef];
if (!streamRef) {
_currentError = @"Failed to create flashlight stream. Your device may not have controllable LEDs.";
// Clean up deviceRef before returning
if (deviceRef) {
CFRelease(deviceRef);
deviceRef = NULL;
}
return NO;
}
} else {
if ([vendor respondsToSelector:@selector
(registerClientWithPID:
clientDescription:clientPriority:canStealFromClientsWithSamePriority
:deviceSharingWithOtherClientsAllowed:deviceAvailabilityChangedHandler:)])
client = [vendor registerClientWithPID:pid
clientDescription:clientDescription
clientPriority:1
canStealFromClientsWithSamePriority:NO
deviceSharingWithOtherClientsAllowed:YES
deviceAvailabilityChangedHandler:NULL];
else if ([vendor respondsToSelector:@selector
(registerClientWithPID:
clientDescription:stealingBehavior:deviceSharingWithOtherClientsAllowed
:deviceAvailabilityChangedHandler:)])
client = [vendor registerClientWithPID:pid
clientDescription:clientDescription
stealingBehavior:1
deviceSharingWithOtherClientsAllowed:YES
deviceAvailabilityChangedHandler:NULL];
else if ([vendor respondsToSelector:@selector(registerClientWithPID:
stealingBehavior:deviceSharingWithOtherClientsAllowed
:deviceAvailabilityChangedHandler:)])
client = [vendor registerClientWithPID:pid
stealingBehavior:1
deviceSharingWithOtherClientsAllowed:YES
deviceAvailabilityChangedHandler:NULL];
else {
_currentError = @"Unsupported iOS version. Cannot register camera client.";
return NO;
}
if (client == 0) {
_currentError = @"Failed to register as camera client. Try closing other camera apps.";
return NO;
}
int error = 0;
if ([vendor respondsToSelector:@selector(copyDeviceForClient:informClientWhenDeviceAvailableAgain:error:)])
device = [vendor copyDeviceForClient:client informClientWhenDeviceAvailableAgain:NO error:&error];
else if ([vendor respondsToSelector:@selector(copyDeviceForClient:error:)])
device = [vendor copyDeviceForClient:client error:&error];
else if ([vendor respondsToSelector:@selector(copyDeviceForClient:)]) {
deviceRef = [vendor copyDeviceForClient:client];
if (!deviceRef) {
_currentError = @"Failed to obtain camera device. The camera may be locked by another app.";
return NO;
}
SEL selector = @selector(copyStreamForFlashlightWithPosition:deviceType:forDevice:);
NSInvocation *inv =
[NSInvocation invocationWithMethodSignature:[vendor methodSignatureForSelector:selector]];
inv.selector = selector;
inv.target = vendor;
int position = 1;
[inv setArgument:&position atIndex:2];
int deviceType = 2;
[inv setArgument:&deviceType atIndex:3];
[inv setArgument:&deviceRef atIndex:4];
[inv invoke];
[inv getReturnValue:&streamRef];
if (!streamRef) {
_currentError = @"Failed to create flashlight stream. Your device may not support LED control.";
if (deviceRef) {
CFRelease(deviceRef);
deviceRef = NULL;
}
return NO;
}
} else {
_currentError = @"Cannot access camera device on this iOS version.";
return NO;
}
// Check if device was obtained successfully for non-deviceRef path
if (!device && !deviceRef) {
_currentError =
error != 0
? [NSString
stringWithFormat:@"Failed to get camera device (error code: %d). Try restarting the app.",
error]
: @"Failed to get camera device. The camera may be in use.";
return NO;
}
}
if (streamRef) {
@try {
if (@available(iOS 11.0, *)) {
const CMBaseVTable *vtable = CMBaseObjectGetVTable((CMBaseObjectRef)streamRef);
if (vtable && vtable->baseClass) {
streamSetProperty = vtable->baseClass->setProperty;
}
} else {
const CMBaseVTable_iOS10 *vtable =
(const CMBaseVTable_iOS10 *)CMBaseObjectGetVTable((CMBaseObjectRef)streamRef);
if (vtable && vtable->baseClass) {
streamSetProperty = vtable->baseClass->setProperty;
}
}
if (!streamSetProperty) {
_currentError = @"Failed to obtain stream property setter. Device may not support this feature.";
[self releaseStream];
return NO;
}
} @catch (NSException *exception) {
_currentError = [NSString stringWithFormat:@"Exception while setting up stream: %@", exception.reason];
[self releaseStream];
return NO;
}
} else if (device) {
stream = [vendor copyStreamForFlashlightWithPosition:1 deviceType:2 forDevice:device];
if (!stream) {
_currentError = @"Failed to create flashlight stream. Your device may not support LED control.";
[self releaseStream];
return NO;
}
}
if (!streamSetProperty && !stream) {
_currentError = @"Could not initialize LED control. Your device may not be supported.";
[self releaseStream];
return NO;
}
initialized = YES;
return YES;
}
/**
* Releases the camera device stream and cleans up all resources.
* Should be called when the app no longer needs to control the LEDs,
* allowing other apps (like Camera) to use the device.
*
* Uses exception handling to ensure cleanup continues even if errors occur.
* Safely releases Core Foundation objects with NULL checks.
*/
- (void)releaseStream {
// Only attempt to take back the device if it was successfully initialized
if (initialized) {
@try {
if ([vendor respondsToSelector:@selector(takeBackDevice:forClient:informClientWhenDeviceAvailableAgain:)])
[vendor takeBackDevice:device forClient:client informClientWhenDeviceAvailableAgain:NO];
else if ([vendor respondsToSelector:@selector(takeBackFlashlightDevice:forPID:)] && deviceRef)
[vendor takeBackFlashlightDevice:deviceRef forPID:pid];
else if ([vendor respondsToSelector:@selector(takeBackDevice:forClient:)] && deviceRef)
[vendor takeBackDevice:deviceRef forClient:client];
else if ([BWFigCaptureDeviceVendorClass
respondsToSelector:@selector
(takeBackVideoDevice:forPID:requestDeviceWhenAvailableAgain:informOtherClients:)] &&
deviceRef)
[BWFigCaptureDeviceVendorClass takeBackVideoDevice:deviceRef
forPID:pid
requestDeviceWhenAvailableAgain:NO
informOtherClients:YES];
} @catch (NSException *exception) {
NSLog(@"TrollLEDs: Exception while releasing device: %@", exception.reason);
}
}
// Safely release Core Foundation objects
if (deviceRef) {
CFRelease(deviceRef);
deviceRef = NULL;
}
if (streamRef) {
CFRelease(streamRef);
streamRef = NULL;
}
// Clear all state
streamSetProperty = NULL;
device = nil;
stream = nil;
client = 0;
initialized = NO;
}
/**
* Returns whether the device uses legacy LED control (dual-LEDs).
*
* @return YES if device uses legacy LED API, NO for modern quad-LED API
*/
- (BOOL)isLegacyLEDs {
return legacyLEDs;
}
/**
* Returns whether the device stream is currently initialized.
* When initialized, the app has exclusive control over the LEDs.
*
* @return YES if stream is initialized, NO otherwise
*/
- (BOOL)isInitialized {
return initialized;
}
/**
* Gets a property value from the camera device.
* Can only retrieve properties when the device stream is active (initialized).
*
* @param property Core Foundation string key for the property to get
* @return The property value, or nil if not available or an error occurred
*
* Note: This only works when the device is locked (stream is initialized).
* If the device is not locked, returns nil.
*/
- (id)getProperty:(CFStringRef)property {
if (!property) {
NSLog(@"TrollLEDs: Attempted to get property with nil property key");
return nil;
}
if (!initialized) {
NSLog(@"TrollLEDs: Cannot get property - stream not initialized");
return nil;
}
@try {
if (device) {
int error = 0;
id value = [device getProperty:property error:&error];
if (error != 0) {
NSLog(@"TrollLEDs: Error getting property (error code: %d)", error);
return nil;
}
return value;
} else {
NSLog(@"TrollLEDs: Device object not available for property reading");
return nil;
}
} @catch (NSException *exception) {
NSLog(@"TrollLEDs: Exception while getting property: %@", exception.reason);
return nil;
}
}
/**
* Sets a property on the camera device stream.
* Used to control LED levels and behavior.
*
* @param property Core Foundation string key for the property to set
* @param value The value to set for the property
*
* Handles both object-based and function pointer-based stream property setting.
* Includes error handling to prevent crashes from invalid parameters.
*/
- (void)setProperty:(CFStringRef)property value:(id)value {
if (!property || !value) {
NSLog(@"TrollLEDs: Attempted to set property with nil property or value");
return;
}
@try {
if (stream) {
[stream setProperty:property value:value];
} else if (streamRef && streamSetProperty) {
streamSetProperty((CMBaseObjectRef)streamRef, property, (__bridge CFTypeRef)value);
} else {
NSLog(@"TrollLEDs: Cannot set property - stream not initialized");
}
} @catch (NSException *exception) {
NSLog(@"TrollLEDs: Exception while setting property: %@", exception.reason);
}
}
@end