-
Notifications
You must be signed in to change notification settings - Fork 0
/
TLImageCaptureManager.m
294 lines (245 loc) · 10 KB
/
TLImageCaptureManager.m
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
//
// TLImageCaptureManager.m
// Tagalog
//
// Created by Nathan Vander Wilt on 1/30/09.
// Copyright 2009 __MyCompanyName__. All rights reserved.
//
#import "TLImageCaptureManager.h"
#import "TLImageCapture.h"
#import "TLImageCapturePhotoSource.h"
NSString* const TLImageCaptureManagerSourcesDidUpdateNotification = @"TLImageCaptureManager_SourcesDidUpdate";
static NSString* const TLTagalogLaunchPathKey = @"LastLaunchPath";
static NSString* const TLTagalogPreviousLaunchApp = @"FormerLaunchedApp";
static CFStringRef const TLICAPreferencesSuiteName = CFSTR("com.apple.ImageCapture2");
static CFStringRef const TLICALaunchPath = CFSTR("HotPlugActionPath");
static CFStringRef const TLICALaunchOptions = CFSTR("HotPlugActionArray");
@interface TLImageCaptureManager ()
- (void)begin;
- (void)registerSource:(ICAObject)icao;
- (void)removeSource:(ICAObject)icao;
@end
static void TLICM_DeviceListCallback(ICAHeader* pb);
static TLImageCaptureManager* gTLImageCaptureManager_sharedInstance = nil;
@implementation TLImageCaptureManager
@synthesize sources = mutableSources;
#pragma mark Lifecycle
+ (void)initialize {
if (self != [TLImageCaptureManager class]) return;
gTLImageCaptureManager_sharedInstance = [TLImageCaptureManager new];
}
+ (id)sharedImageCaptureManager {
NSAssert([NSThread isMainThread], @"Image Capture Manager must be used from main thread");
[gTLImageCaptureManager_sharedInstance begin];
return gTLImageCaptureManager_sharedInstance;
}
- (id)init {
NSAssert(!gTLImageCaptureManager_sharedInstance, @"Singleton only!");
self = [super init];
if (self) {
mutableSources = [NSMutableSet new];
}
return self;
}
- (void)dealloc {
[mutableSources release];
[super dealloc];
}
- (void)addSourcesObject:(TLImageCapturePhotoSource*)newSource {
[mutableSources addObject:newSource];
[[NSNotificationCenter defaultCenter]
postNotificationName:TLImageCaptureManagerSourcesDidUpdateNotification object:self];
}
- (void)removeSourcesObject:(TLImageCapturePhotoSource*)oldSource {
[mutableSources removeObject:oldSource];
[[NSNotificationCenter defaultCenter]
postNotificationName:TLImageCaptureManagerSourcesDidUpdateNotification object:self];
}
- (void)begin {
if (begun) return;
begun = YES;
ICAGetDeviceListPB pb = {};
pb.header.refcon = (intptr_t)self;
ICAError err = ICAGetDeviceList(&pb, TLICM_DeviceListCallback);
if (err) {
NSLog(@"Could not load Image Capture photo sources."
@" Error %i calling ICAGetDeviceList.", (int)err);
}
}
- (void)backgroundRegisterSource:(NSDictionary*)info {
NSAutoreleasePool* pool = [NSAutoreleasePool new];
ICAObject icao = [[info valueForKey:(id)kTLICAObjectKey] intValue];
TLImageCapturePhotoSource* source = [[TLImageCapturePhotoSource alloc] initWithICAO:icao];
[self performSelectorOnMainThread:@selector(addSourcesObject:)
withObject:source
waitUntilDone:YES];
[source release];
[pool drain];
}
- (void)registerSource:(ICAObject)icao {
NSDictionary* info = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:icao], (id)kTLICAObjectKey, nil];
// add source in background, since ICA source initialization is synchronous
[self performSelectorInBackground:@selector(backgroundRegisterSource:) withObject:info];
}
- (void)removeSource:(ICAObject)icao {
TLImageCapturePhotoSource* oldSource = nil;
for (TLImageCapturePhotoSource* source in [self sources]) {
if ([source icao] == icao) {
oldSource = source;
break;
}
}
[self removeSourcesObject:oldSource];
}
#pragma mark Image Capture preferences handling
- (void)updateImageCaptureEntry {
// verify most recently launched Geotagalog is an Image Capture option
NSUserDefaults* ourDefaults = [NSUserDefaults standardUserDefaults];
NSString* formerLaunchPath = [ourDefaults stringForKey:TLTagalogLaunchPathKey];
NSString* currentLaunchPath = [[NSBundle mainBundle] bundlePath];
CFArrayRef cameraActions = CFPreferencesCopyValue(TLICALaunchOptions, TLICAPreferencesSuiteName,
kCFPreferencesCurrentUser, kCFPreferencesCurrentHost);
NSMutableArray* newCameraActions = nil;
if (cameraActions) {
newCameraActions = [[(NSArray*)cameraActions mutableCopy] autorelease];
CFRelease(cameraActions);
}
else {
newCameraActions = [NSMutableArray array];
}
[newCameraActions removeObject:formerLaunchPath];
[newCameraActions removeObject:currentLaunchPath]; // avoid duplicates
[newCameraActions addObject:currentLaunchPath];
//NSLog(@"Setting launch options: %@", newCameraActions);
CFPreferencesSetValue(TLICALaunchOptions, (CFArrayRef)newCameraActions, TLICAPreferencesSuiteName,
kCFPreferencesCurrentUser, kCFPreferencesCurrentHost);
[ourDefaults setObject:currentLaunchPath forKey:TLTagalogLaunchPathKey];
CFPreferencesSynchronize(TLICAPreferencesSuiteName,
kCFPreferencesCurrentUser, kCFPreferencesCurrentHost);
[ourDefaults synchronize];
}
- (BOOL)shouldAutoLaunch {
BOOL requestedExternally = NO;
CFStringRef currentApp = CFPreferencesCopyValue(TLICALaunchPath, TLICAPreferencesSuiteName,
kCFPreferencesCurrentUser, kCFPreferencesCurrentHost);
if (currentApp) {
NSString* ourLaunchPath = [[NSBundle mainBundle] bundlePath];
if ([ourLaunchPath isEqual:(id)currentApp]) {
requestedExternally = YES;
}
CFRelease(currentApp);
}
return requestedExternally;
}
- (void)setShouldAutoLaunch:(BOOL)newShouldAutoLaunch {
BOOL wasAutoLaunching = [self shouldAutoLaunch];
NSUserDefaults* ourDefaults = [NSUserDefaults standardUserDefaults];
if (!wasAutoLaunching) {
NSString* ourLaunchPath = [[NSBundle mainBundle] bundlePath];
// remember previous setting
CFStringRef formerApp = CFPreferencesCopyValue(TLICALaunchPath, TLICAPreferencesSuiteName,
kCFPreferencesCurrentUser, kCFPreferencesCurrentHost);
if (!formerApp || [ourLaunchPath isEqual:(id)formerApp]) {
if (formerApp) CFRelease(formerApp);
formerApp = CFSTR("");
}
//NSLog(@"Saving formerApp '%@'", (NSString*)formerApp);
[ourDefaults setObject:(NSString*)formerApp forKey:TLTagalogPreviousLaunchApp];
CFRelease(formerApp);
[ourDefaults synchronize];
// set ourself to launch
if (newShouldAutoLaunch) {
CFPreferencesSetValue(TLICALaunchPath, (CFStringRef)ourLaunchPath, TLICAPreferencesSuiteName,
kCFPreferencesCurrentUser, kCFPreferencesCurrentHost);
}
}
else if (wasAutoLaunching && !newShouldAutoLaunch) {
// restore previous setting
NSString* formerApp = [ourDefaults stringForKey:TLTagalogPreviousLaunchApp];
if (!formerApp) formerApp = @"";
CFPreferencesSetValue(TLICALaunchPath, (CFStringRef)formerApp, TLICAPreferencesSuiteName,
kCFPreferencesCurrentUser, kCFPreferencesCurrentHost);
//NSLog(@"Restoring formerApp '%@'", formerApp);
}
CFPreferencesSynchronize(TLICAPreferencesSuiteName,
kCFPreferencesCurrentUser, kCFPreferencesCurrentHost);
}
@end
static void TLICM_NotificationRegisteredCallback(ICAHeader* pb) {
if (pb->err) {
NSLog(@"Could not register for Image Capture notifications. Got back %i error.", (int)pb->err);
return;
}
// nothing more to do here
}
static void TLICM_NotificationCallback(CFStringRef type, CFDictionaryRef dict) {
//NSLog(@"%@ - %@\n", (id)type, (id)dict);
/* NOTE: until rdar://problem/6744557 is fixed, the kICARefconKey is invalid
on 64-bit: ICA truncates it to 32-bits. As a workaround, we take advantage of
the fact that the refcon points to a singleton stored as a global we can access. */
#ifdef __LP64__
intptr_t refcon = (intptr_t)gTLImageCaptureManager_sharedInstance;
#else
intptr_t refcon = [(id)CFDictionaryGetValue(dict, kICARefconKey) longValue];
#endif
NSCAssert([NSThread isMainThread], @"Image Capture notifications must happen on main thread.");
//NSLog(@"%@", (id)dict);
ICAObject icao = [(id)CFDictionaryGetValue(dict, kICANotificationDeviceICAObjectKey) intValue];
TLImageCaptureManager* manager = (TLImageCaptureManager*)refcon;
if ([(id)type isEqualToString:(id)kICANotificationTypeDeviceAdded]) {
[manager registerSource:icao];
}
else if ([(id)type isEqualToString:(id)kICANotificationTypeDeviceRemoved]) {
[manager removeSource:icao];
}
}
static void TLICM_DeviceListDictionaryCallback(ICAHeader* pb) {
if (pb->err) {
NSLog(@"Failed to get Image Capture device list dictionary. (Error %i)", (int)pb->err);
return;
}
TLImageCaptureManager* manager = (TLImageCaptureManager*)pb->refcon;
ICACopyObjectPropertyDictionaryPB* pb2 = (void*)pb;
CFDictionaryRef dict = *(pb2->theDict);
//NSLog(@"%@\n", (id)dict);
NSArray* devices = (NSArray*)CFDictionaryGetValue(dict, kICADevicesArrayKey);
for (NSDictionary* device in devices) {
// only add device if it is a camera
if ([[device objectForKey:(id)kTLICADeviceTypeKey]
isEqualToString:(NSString*)kICADeviceTypeCamera]) {
ICAObject object = [[device objectForKey:(id)kTLICAObjectKey] intValue];
[manager registerSource:object];
}
}
CFRelease(dict);
}
void TLICM_DeviceListCallback(ICAHeader* pb) {
if (pb->err) {
NSLog(@"Failed to get Image Capture device object. (Error %i)", (int)pb->err);
return;
}
// all we've gotten back is an object id, so we need to fetch its dictionary.
ICACopyObjectPropertyDictionaryPB pb2 = {};
pb2.header.refcon = pb->refcon;
ICAObject listObject = ((ICAGetDeviceListPB*)pb)->object;
pb2.object = listObject;
ICAError err = ICACopyObjectPropertyDictionary(&pb2, TLICM_DeviceListDictionaryCallback);
if (err) {
NSLog(@"Couldn't get Image Capture device list dictionary. "
@"Error %i calling ICACopyObjectPropertyDictionary.", (int)err);
}
// we can register for notifications with just the object id
ICARegisterForEventNotificationPB pb3 = {};
pb3.header.refcon = pb->refcon;
pb3.objectOfInterest = listObject;
pb3.eventsOfInterest = (CFArrayRef)[NSArray arrayWithObjects:
(id)kICANotificationTypeDeviceRemoved,
(id)kICANotificationTypeDeviceAdded, nil];
pb3.notificationProc = TLICM_NotificationCallback;
err = ICARegisterForEventNotification(&pb3, TLICM_NotificationRegisteredCallback);
if (err) {
NSLog(@"Couldn't register for Image Capture notifications. "
@"Error %i calling ICARegisterForEventNotification.", (int)err);
}
}