forked from a3tweaks/Flipswitch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FSSwitchMainPanel.m
551 lines (515 loc) · 23.1 KB
/
FSSwitchMainPanel.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
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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
#import "FSSwitchMainPanel.h"
#import "FSSwitchPanel+Internal.h"
#import "FSSwitchService.h"
#import "FSSwitchDataSource.h"
#import "FSPreferenceSwitchDataSource.h"
#import "FSLazySwitch.h"
#import "FSCapability.h"
#import "FSLaunchURL.h"
#define ROCKETBOOTSTRAP_LOAD_DYNAMIC
#import "LightMessaging/LightMessaging.h"
#import "Internal.h"
#import <notify.h>
#import <sys/stat.h>
#import <libkern/OSAtomic.h>
#import <objc/runtime.h>
#define kSwitchesPath @"/Library/Switches/"
static volatile int32_t stateChangeCount;
@implementation FSSwitchMainPanel
- (void)_registerDataSourceForSwitchIdentifier:(NSArray *)args
{
[self registerDataSource:[args objectAtIndex:0] forSwitchIdentifier:[args objectAtIndex:1]];
}
- (void)registerDataSource:(id<FSSwitchDataSource>)dataSource forSwitchIdentifier:(NSString *)switchIdentifier;
{
if (!switchIdentifier) {
[NSException raise:NSInvalidArgumentException format:@"Switch identifier passed to -[FSSwitchPanel registerSwitch:forIdentifier:] must not be nil"];
}
if (!dataSource) {
[NSException raise:NSInvalidArgumentException format:@"Switch data source passed to -[FSSwitchPanel registerSwitch:forIdentifier:] must not be nil"];
}
if (![NSThread isMainThread]) {
[self performSelectorOnMainThread:@selector(_registerDataSourceForSwitchIdentifier:) withObject:[NSArray arrayWithObjects:dataSource, switchIdentifier, nil] waitUntilDone:YES];
return;
}
// Verify that switchImplementation is either a valid action-like switchImplementation or setting-like switchImplementation
if ([(NSObject *)dataSource methodForSelector:@selector(applyState:forSwitchIdentifier:)] == [NSObject instanceMethodForSelector:@selector(applyState:forSwitchIdentifier:)]) {
if ([(NSObject *)dataSource methodForSelector:@selector(applyActionForSwitchIdentifier:)] == [NSObject instanceMethodForSelector:@selector(applyActionForSwitchIdentifier:)]) {
[NSException raise:NSInvalidArgumentException format:@"Switch data source passed to -[FSSwitchPanel registerSwitch:forIdentifier] must override either applyState:forSwitchIdentifier: or applyActionForSwitchIdentifier:"];
}
} else {
if ([(NSObject *)dataSource methodForSelector:@selector(stateForSwitchIdentifier:)] == [NSObject instanceMethodForSelector:@selector(stateForSwitchIdentifier:)]) {
[NSException raise:NSInvalidArgumentException format:@"Switch data source passed to -[FSSwitchPanel registerSwitch:forIdentifier] must override stateForSwitchIdentifier:"];
}
}
id<FSSwitchDataSource> oldSwitch = [[_switchImplementations objectForKey:switchIdentifier] retain];
[_switchImplementations setObject:dataSource forKey:switchIdentifier];
[dataSource switchWasRegisteredForIdentifier:switchIdentifier];
[oldSwitch switchWasUnregisteredForIdentifier:switchIdentifier];
[oldSwitch release];
if (!hasUpdatedSwitches) {
hasUpdatedSwitches = YES;
[self performSelector:@selector(_sendSwitchesChanged) withObject:nil afterDelay:0.0];
}
}
- (void)unregisterSwitchIdentifier:(NSString *)switchIdentifier
{
if (!switchIdentifier) {
[NSException raise:NSInvalidArgumentException format:@"Switch identifier passed to -[FSSwitchPanel unregisterSwitchIdentifier:] must not be nil"];
}
if (![NSThread isMainThread]) {
[self performSelectorOnMainThread:@selector(unregisterSwitchIdentifier:) withObject:switchIdentifier waitUntilDone:YES];
return;
}
id<FSSwitchDataSource> oldSwitch = [[_switchImplementations objectForKey:switchIdentifier] retain];
[_switchImplementations removeObjectForKey:switchIdentifier];
[oldSwitch switchWasUnregisteredForIdentifier:switchIdentifier];
[oldSwitch release];
if (!hasUpdatedSwitches) {
hasUpdatedSwitches = YES;
[self performSelector:@selector(_sendSwitchesChanged) withObject:nil afterDelay:0.0];
}
}
- (void)_sendSwitchesChanged
{
hasUpdatedSwitches = NO;
notify_post([FSSwitchPanelSwitchesChangedNotification UTF8String]);
[self postNotificationName:FSSwitchPanelSwitchesChangedNotification userInfo:nil];
}
- (void)_postNotificationForStateDidChangeForSwitchIdentifier:(NSString *)switchIdentifier
{
NSDictionary *userInfo = switchIdentifier ? [NSDictionary dictionaryWithObject:switchIdentifier forKey:FSSwitchPanelSwitchIdentifierKey] : nil;
[self postNotificationName:FSSwitchPanelSwitchStateChangedNotification userInfo:userInfo];
}
- (void)stateDidChangeForSwitchIdentifier:(NSString *)switchIdentifier
{
OSAtomicIncrement32(&stateChangeCount);
if ([NSThread isMainThread])
[self _postNotificationForStateDidChangeForSwitchIdentifier:switchIdentifier];
else
[self performSelectorOnMainThread:@selector(_postNotificationForStateDidChangeForSwitchIdentifier:) withObject:switchIdentifier waitUntilDone:NO];
}
- (NSArray *)switchIdentifiers
{
if (![NSThread isMainThread]) {
return [super switchIdentifiers];
}
return [_switchImplementations allKeys];
}
- (NSString *)titleForSwitchIdentifier:(NSString *)switchIdentifier
{
if (![NSThread isMainThread]) {
return [super titleForSwitchIdentifier:switchIdentifier];
}
id<FSSwitchDataSource> switchImplementation = [_switchImplementations objectForKey:switchIdentifier];
return [switchImplementation titleForSwitchIdentifier:switchIdentifier];
}
- (FSSwitchState)stateForSwitchIdentifier:(NSString *)switchIdentifier
{
if (![NSThread isMainThread]) {
return [super stateForSwitchIdentifier:switchIdentifier];
}
id<FSSwitchDataSource> switchImplementation = [_switchImplementations objectForKey:switchIdentifier];
return [switchImplementation stateForSwitchIdentifier:switchIdentifier];
}
- (void)setState:(FSSwitchState)state forSwitchIdentifier:(NSString *)switchIdentifier
{
if (![NSThread isMainThread]) {
return [super setState:state forSwitchIdentifier:switchIdentifier];
}
id<FSSwitchDataSource> switchImplementation = [_switchImplementations objectForKey:switchIdentifier];
// Workaround switches that don't explicitly send state change notifications :(
FSSwitchState oldState = [switchImplementation stateForSwitchIdentifier:switchIdentifier];
int32_t oldStateChangeCount = stateChangeCount;
[switchImplementation applyState:state forSwitchIdentifier:switchIdentifier];
if (oldStateChangeCount == stateChangeCount && oldState != [switchImplementation stateForSwitchIdentifier:switchIdentifier]) {
[self stateDidChangeForSwitchIdentifier:switchIdentifier];
}
}
- (void)applyActionForSwitchIdentifier:(NSString *)switchIdentifier
{
if (![NSThread isMainThread]) {
return [super applyActionForSwitchIdentifier:switchIdentifier];
}
id<FSSwitchDataSource> switchImplementation = [_switchImplementations objectForKey:switchIdentifier];
// Workaround switches that don't explicitly send state change notifications :(
FSSwitchState oldState = [switchImplementation stateForSwitchIdentifier:switchIdentifier];
int32_t oldStateChangeCount = stateChangeCount;
[switchImplementation applyActionForSwitchIdentifier:switchIdentifier];
if (oldStateChangeCount == stateChangeCount && oldState != [switchImplementation stateForSwitchIdentifier:switchIdentifier]) {
[self stateDidChangeForSwitchIdentifier:switchIdentifier];
}
}
- (id)glyphImageDescriptorOfState:(FSSwitchState)switchState variant:(NSString *)variant size:(CGFloat)size scale:(CGFloat)scale forSwitchIdentifier:(NSString *)switchIdentifier;
{
if (![NSThread isMainThread]) {
return [super glyphImageDescriptorOfState:switchState variant:variant size:size scale:scale forSwitchIdentifier:switchIdentifier];
}
id<FSSwitchDataSource> switchImplementation = [_switchImplementations objectForKey:switchIdentifier];
if (switchImplementation) {
return [switchImplementation glyphImageDescriptorOfState:switchState variant:variant size:size scale:scale forSwitchIdentifier:switchIdentifier];
} else if ([[NSFileManager defaultManager] fileExistsAtPath:switchIdentifier]) {
return [switchIdentifier stringByResolvingSymlinksInPath];
} else {
return nil;
}
}
- (BOOL)hasAlternateActionForSwitchIdentifier:(NSString *)switchIdentifier
{
if (![NSThread isMainThread]) {
return [super hasAlternateActionForSwitchIdentifier:switchIdentifier];
}
id<FSSwitchDataSource> switchImplementation = [_switchImplementations objectForKey:switchIdentifier];
return [switchImplementation hasAlternateActionForSwitchIdentifier:switchIdentifier];
}
- (void)applyAlternateActionForSwitchIdentifier:(NSString *)switchIdentifier
{
if (![NSThread isMainThread]) {
return [super applyAlternateActionForSwitchIdentifier:switchIdentifier];
}
id<FSSwitchDataSource> switchImplementation = [_switchImplementations objectForKey:switchIdentifier];
[switchImplementation applyAlternateActionForSwitchIdentifier:switchIdentifier];
}
- (void)postNotificationName:(NSString *)notificationName userInfo:(NSDictionary *)userInfo
{
[userInfo retain];
[pendingNotificationUserInfo release];
pendingNotificationUserInfo = userInfo;
[[NSNotificationCenter defaultCenter] postNotificationName:notificationName object:self userInfo:userInfo];
notify_post([notificationName UTF8String]);
}
- (void)openURLAsAlternateAction:(NSURL *)url
{
if (![NSThread isMainThread]) {
[self performSelectorOnMainThread:_cmd withObject:url waitUntilDone:NO];
return;
}
NSDictionary *userInfo = url ? [NSDictionary dictionaryWithObject:[url absoluteString] forKey:@"url"] : nil;
[self postNotificationName:FSSwitchPanelSwitchWillOpenURLNotification userInfo:userInfo];
FSLaunchURL(url);
}
- (BOOL)switchWithIdentifierIsEnabled:(NSString *)switchIdentifier
{
if (![NSThread isMainThread]) {
return [super switchWithIdentifierIsEnabled:switchIdentifier];
}
id<FSSwitchDataSource> switchImplementation = [_switchImplementations objectForKey:switchIdentifier];
return [switchImplementation switchWithIdentifierIsEnabled:switchIdentifier];
}
- (void)beginPrewarmingForSwitchIdentifier:(NSString *)switchIdentifier
{
if (![NSThread isMainThread]) {
return [super beginPrewarmingForSwitchIdentifier:switchIdentifier];
}
id<FSSwitchDataSource> switchImplementation = [_switchImplementations objectForKey:switchIdentifier];
return [switchImplementation beginPrewarmingForSwitchIdentifier:switchIdentifier];
}
- (void)cancelPrewarmingForSwitchIdentifier:(NSString *)switchIdentifier;
{
if (![NSThread isMainThread]) {
return [super cancelPrewarmingForSwitchIdentifier:switchIdentifier];
}
id<FSSwitchDataSource> switchImplementation = [_switchImplementations objectForKey:switchIdentifier];
return [switchImplementation cancelPrewarmingForSwitchIdentifier:switchIdentifier];
}
- (NSString *)descriptionOfState:(FSSwitchState)state forSwitchIdentifier:(NSString *)switchIdentifier
{
if (![NSThread isMainThread]) {
return [super descriptionOfState:state forSwitchIdentifier:switchIdentifier];
}
id<FSSwitchDataSource> switchImplementation = [_switchImplementations objectForKey:switchIdentifier];
return [switchImplementation descriptionOfState:state forSwitchIdentifier:switchIdentifier];
}
- (Class <FSSwitchSettingsViewController>)settingsViewControllerClassForSwitchIdentifier:(NSString *)switchIdentifier
{
if (![NSThread isMainThread]) {
return [super settingsViewControllerClassForSwitchIdentifier:switchIdentifier];
}
id<FSSwitchDataSource> switchImplementation = [_switchImplementations objectForKey:switchIdentifier];
Class _class = [switchImplementation settingsViewControllerClassForSwitchIdentifier:switchIdentifier];
if (!_class)
return nil;
if (![_class isSubclassOfClass:[UIViewController class]]) {
NSLog(@"Flipswitch: %@ is not a UIViewController (for switch %@)", _class, switchIdentifier);
return nil;
}
if (![_class conformsToProtocol:@protocol(FSSwitchSettingsViewController)]) {
NSLog(@"Flipswitch: %@ does not conform to FSSwitchSettingsViewController (for switch %@)", _class, switchIdentifier);
return nil;
}
return _class;
}
- (BOOL)switchWithIdentifierIsSimpleAction:(NSString *)switchIdentifier
{
if (![NSThread isMainThread]) {
return [super switchWithIdentifierIsSimpleAction:switchIdentifier];
}
id<FSSwitchDataSource> switchImplementation = [_switchImplementations objectForKey:switchIdentifier];
return [switchImplementation switchWithIdentifierIsSimpleAction:switchIdentifier];
}
static void processMessage(FSSwitchMainPanel *self, SInt32 messageId, mach_port_t replyPort, CFDataRef data)
{
switch ((FSSwitchServiceMessage)messageId) {
case FSSwitchServiceMessageGetIdentifiers:
LMSendPropertyListReply(replyPort, self.switchIdentifiers);
return;
case FSSwitchServiceMessageGetTitleForIdentifier: {
NSString *identifier = [NSPropertyListSerialization propertyListFromData:(NSData *)data mutabilityOption:0 format:NULL errorDescription:NULL];
if ([identifier isKindOfClass:[NSString class]]) {
NSString *title = [self titleForSwitchIdentifier:identifier];
LMSendPropertyListReply(replyPort, title);
return;
}
break;
}
case FSSwitchServiceMessageGetStateForIdentifier: {
NSString *identifier = [NSPropertyListSerialization propertyListFromData:(NSData *)data mutabilityOption:0 format:NULL errorDescription:NULL];
if ([identifier isKindOfClass:[NSString class]]) {
LMSendIntegerReply(replyPort, [self stateForSwitchIdentifier:identifier]);
return;
}
break;
}
case FSSwitchServiceMessageSetStateForIdentifier: {
NSArray *args = [NSPropertyListSerialization propertyListFromData:(NSData *)data mutabilityOption:0 format:NULL errorDescription:NULL];
if ([args isKindOfClass:[NSArray class]] && [args count] == 2) {
NSNumber *state = [args objectAtIndex:0];
NSString *identifier = [args objectAtIndex:1];
if ([state isKindOfClass:[NSNumber class]] && [identifier isKindOfClass:[NSString class]]) {
[self setState:[state integerValue] forSwitchIdentifier:identifier];
}
}
break;
}
case FSSwitchServiceMessageGetImageDescriptorForSwitch: {
NSDictionary *args = [NSPropertyListSerialization propertyListFromData:(NSData *)data mutabilityOption:0 format:NULL errorDescription:NULL];
if ([args isKindOfClass:[NSDictionary class]]) {
NSString *switchIdentifier = [args objectForKey:@"switchIdentifier"];
CGFloat size = [[args objectForKey:@"size"] floatValue];
CGFloat scale = [[args objectForKey:@"scale"] floatValue];
FSSwitchState switchState = [[args objectForKey:@"switchState"] intValue];
NSString *variant = [args objectForKey:@"variant"];
id imageDescriptor = [self glyphImageDescriptorOfState:switchState variant:variant size:size scale:scale forSwitchIdentifier:switchIdentifier];
if (imageDescriptor) {
// TODO: Allow responding with a string representing file path, data containing image bytes, or UImage
LMSendPropertyListReply(replyPort, imageDescriptor);
return;
}
}
break;
}
case FSSwitchServiceMessageApplyActionForIdentifier: {
NSString *identifier = [NSPropertyListSerialization propertyListFromData:(NSData *)data mutabilityOption:0 format:NULL errorDescription:NULL];
if ([identifier isKindOfClass:[NSString class]]) {
[self applyActionForSwitchIdentifier:identifier];
}
break;
}
case FSSwitchServiceMessageHasAlternateActionForIdentifier: {
NSString *identifier = [NSPropertyListSerialization propertyListFromData:(NSData *)data mutabilityOption:0 format:NULL errorDescription:NULL];
if ([identifier isKindOfClass:[NSString class]]) {
LMSendIntegerReply(replyPort, [self hasAlternateActionForSwitchIdentifier:identifier]);
return;
}
break;
}
case FSSwitchServiceMessageApplyAlternateActionForIdentifier: {
NSString *identifier = [NSPropertyListSerialization propertyListFromData:(NSData *)data mutabilityOption:0 format:NULL errorDescription:NULL];
if ([identifier isKindOfClass:[NSString class]]) {
[self applyAlternateActionForSwitchIdentifier:identifier];
}
break;
}
case FSSwitchServiceMessageGetPendingNotificationUserInfo: {
LMSendPropertyListReply(replyPort, self->pendingNotificationUserInfo);
return;
}
case FSSwitchServiceMessageGetEnabledForIdentifier: {
NSString *identifier = [NSPropertyListSerialization propertyListFromData:(NSData *)data mutabilityOption:0 format:NULL errorDescription:NULL];
if ([identifier isKindOfClass:[NSString class]]) {
LMSendIntegerReply(replyPort, [self switchWithIdentifierIsEnabled:identifier]);
return;
}
break;
}
case FSSwitchServiceMessageBeginPrewarmingForIdentifier: {
NSString *identifier = [NSPropertyListSerialization propertyListFromData:(NSData *)data mutabilityOption:0 format:NULL errorDescription:NULL];
if ([identifier isKindOfClass:[NSString class]]) {
[self beginPrewarmingForSwitchIdentifier:identifier];
break;
}
break;
}
case FSSwitchServiceMessageCancelPrewarmingForIdentifier: {
NSString *identifier = [NSPropertyListSerialization propertyListFromData:(NSData *)data mutabilityOption:0 format:NULL errorDescription:NULL];
if ([identifier isKindOfClass:[NSString class]]) {
[self cancelPrewarmingForSwitchIdentifier:identifier];
break;
}
break;
}
case FSSwitchServiceMessageOpenURLAsAlternateAction: {
NSString *url = [NSPropertyListSerialization propertyListFromData:(NSData *)data mutabilityOption:0 format:NULL errorDescription:NULL];
if ([url isKindOfClass:[NSString class]]) {
[self openURLAsAlternateAction:[NSURL URLWithString:url]];
break;
}
break;
}
case FSSwitchServiceMessageSettingsViewControllerForIdentifier: {
NSString *identifier = [NSPropertyListSerialization propertyListFromData:(NSData *)data mutabilityOption:0 format:NULL errorDescription:NULL];
if ([identifier isKindOfClass:[NSString class]]) {
Class _class = [self settingsViewControllerClassForSwitchIdentifier:identifier];
if (_class) {
const char *imageName = class_getImageName(_class);
NSArray *response = [NSArray arrayWithObjects:NSStringFromClass(_class), imageName ? [NSString stringWithUTF8String:imageName] : nil, nil];
LMSendPropertyListReply(replyPort, response);
return;
}
}
break;
}
case FSSwitchServiceMessageDescriptionOfStateForIdentifier: {
NSArray *args = [NSPropertyListSerialization propertyListFromData:(NSData *)data mutabilityOption:0 format:NULL errorDescription:NULL];
if ([args isKindOfClass:[NSArray class]] && [args count] == 2) {
NSString *identifier = [args objectAtIndex:0];
if ([identifier isKindOfClass:[NSString class]]) {
NSNumber *state = [args objectAtIndex:1];
if ([state isKindOfClass:[NSNumber class]]) {
NSString *description = [self descriptionOfState:[state intValue] forSwitchIdentifier:identifier];
if (description) {
LMSendPropertyListReply(replyPort, description);
return;
}
}
}
}
break;
}
case FSSwitchServiceMessageGetIsSimpleActionForIdentifier: {
NSString *identifier = [NSPropertyListSerialization propertyListFromData:(NSData *)data mutabilityOption:0 format:NULL errorDescription:NULL];
if ([identifier isKindOfClass:[NSString class]]) {
LMSendIntegerReply(replyPort, [self switchWithIdentifierIsSimpleAction:identifier]);
return;
}
break;
}
}
LMSendReply(replyPort, NULL, 0);
}
static void machPortCallback(CFMachPortRef port, void *bytes, CFIndex size, void *info)
{
LMMessage *request = bytes;
if (!LMDataWithSizeIsValidMessage(bytes, size)) {
LMSendReply(request->head.msgh_remote_port, NULL, 0);
LMResponseBufferFree(bytes);
return;
}
// Send Response
const void *data = LMMessageGetData(request);
size_t length = LMMessageGetDataLength(request);
mach_port_t replyPort = request->head.msgh_remote_port;
CFDataRef cfdata = CFDataCreateWithBytesNoCopy(kCFAllocatorDefault, data ?: &data, length, kCFAllocatorNull);
processMessage((FSSwitchMainPanel *)[FSSwitchPanel sharedPanel], request->head.msgh_id, replyPort, cfdata);
if (cfdata)
CFRelease(cfdata);
LMResponseBufferFree(bytes);
}
- (void)_loadSwitchForBundle:(NSBundle *)bundle
{
Class arrayClass = [NSArray class];
NSArray *capabilities = [bundle objectForInfoDictionaryKey:@"required-capabilities"];
if ([capabilities isKindOfClass:arrayClass])
for (NSString *capability in capabilities)
if ([capability isKindOfClass:[NSString class]])
if (!FSSystemHasCapability(capability))
return;
NSArray *coreFoundationVersion = [bundle objectForInfoDictionaryKey:@"CoreFoundationVersion"];
if ([coreFoundationVersion isKindOfClass:arrayClass] && coreFoundationVersion.count > 0) {
NSNumber *lowerBound = [coreFoundationVersion objectAtIndex:0];
if (kCFCoreFoundationVersionNumber < lowerBound.doubleValue)
return;
if (coreFoundationVersion.count > 1) {
NSNumber *upperBound = [coreFoundationVersion objectAtIndex:1];
if (kCFCoreFoundationVersionNumber >= upperBound.doubleValue)
return;
}
}
Class switchClass = nil;
if ([[bundle objectForInfoDictionaryKey:@"lazy-load"] boolValue]) {
switchClass = [FSLazySwitch class];
} else if ([bundle objectForInfoDictionaryKey:@"CFBundleExecutable"]) {
NSError *error = nil;
if ([bundle loadAndReturnError:&error]) {
NSString *principalClass = [bundle objectForInfoDictionaryKey:@"NSPrincipalClass"];
if (principalClass) {
switchClass = NSClassFromString(principalClass);
}
} else {
NSLog(@"Flipswitch: Failed to load bundle with error: %@", error);
}
} else {
NSString *principalClass = [bundle objectForInfoDictionaryKey:@"NSPrincipalClass"];
if (principalClass) {
switchClass = NSClassFromString(principalClass);
}
}
if (switchClass) {
id<FSSwitchDataSource> switchImplementation = [switchClass instancesRespondToSelector:@selector(initWithBundle:)] ? [[switchClass alloc] initWithBundle:bundle] : [[switchClass alloc] init];
if (switchImplementation) {
[self registerDataSource:switchImplementation forSwitchIdentifier:bundle.bundleIdentifier];
[switchImplementation release];
}
}
}
- (void)_loadBuiltInSwitches
{
NSArray *switchDirectoryContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:kSwitchesPath error:nil];
for (NSString *folder in switchDirectoryContents) {
NSBundle *bundle = [NSBundle bundleWithPath:[kSwitchesPath stringByAppendingPathComponent:folder]];
if (bundle) {
[self _loadSwitchForBundle:bundle];
}
}
}
- (id)init
{
if ((self = [super init]))
{
_switchImplementations = [[NSMutableDictionary alloc] init];
}
return self;
}
- (void)dealloc
{
[_switchImplementations release];
[super dealloc];
}
@end
static struct timespec GetFileModifiedTime(const char *path)
{
struct stat temp;
if (stat(path, &temp) == 0)
return temp.st_mtimespec;
struct timespec distantPast;
distantPast.tv_sec = 0;
distantPast.tv_nsec = 0;
return distantPast;
}
__attribute__((constructor))
static void constructor(void)
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
// Clear the cache if a new WinterBoard theme has been applied
struct timespec cacheModified = GetFileModifiedTime("/tmp/FlipswitchCache");
if (cacheModified.tv_sec != 0) {
struct timespec winterboardModified = GetFileModifiedTime("/var/mobile/Library/Preferences/com.saurik.WinterBoard.plist");
if ((cacheModified.tv_sec < winterboardModified.tv_sec) || (cacheModified.tv_sec == winterboardModified.tv_sec && cacheModified.tv_nsec < winterboardModified.tv_nsec)) {
NSLog(@"Flipswitch: Clearing image cache!");
[[NSFileManager defaultManager] removeItemAtPath:@"/tmp/FlipswitchCache" error:NULL];
}
}
kern_return_t err = LMStartService(kFSSwitchServiceName, CFRunLoopGetCurrent(), machPortCallback);
if (err) NSLog(@"Flipswitch: Unable to bootstrap service with error: %x", err);
[pool drain];
}