]> git.saurik.com Git - winterboard.git/blob - Library.mm
Various release engineeirng issues.
[winterboard.git] / Library.mm
1 /* WinterBoard - Theme Manager for the iPhone
2 * Copyright (C) 2008 Jay Freeman (saurik)
3 */
4
5 /*
6 * Redistribution and use in source and binary
7 * forms, with or without modification, are permitted
8 * provided that the following conditions are met:
9 *
10 * 1. Redistributions of source code must retain the
11 * above copyright notice, this list of conditions
12 * and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the
14 * above copyright notice, this list of conditions
15 * and the following disclaimer in the documentation
16 * and/or other materials provided with the
17 * distribution.
18 * 3. The name of the author may not be used to endorse
19 * or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS''
23 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
24 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
25 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE
27 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
28 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
29 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
30 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
32 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
33 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
34 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
35 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 */
37
38 #define _trace() NSLog(@"WB:_trace(%u)", __LINE__);
39 #define _transient
40
41 #include <objc/runtime.h>
42 #include <objc/message.h>
43
44 extern "C" {
45 #include <mach-o/nlist.h>
46 }
47
48 #import <Foundation/Foundation.h>
49 #import <CoreGraphics/CoreGraphics.h>
50
51 #import <UIKit/UIColor.h>
52 #import <UIKit/UIFont.h>
53 #import <UIKit/UIImage.h>
54 #import <UIKit/UIImageView.h>
55 #import <UIKit/UINavigationBarBackground.h>
56 #import <UIKit/UIWebDocumentView.h>
57
58 #import <UIKit/NSString-UIStringDrawing.h>
59 #import <UIKit/NSString-UIStringDrawingDeprecated.h>
60
61 #import <UIKit/UIImage-UIImageDeprecated.h>
62
63 #import <UIKit/UIView-Geometry.h>
64 #import <UIKit/UIView-Hierarchy.h>
65 #import <UIKit/UIView-Rendering.h>
66
67 #import <SpringBoard/SBApplication.h>
68 #import <SpringBoard/SBApplicationIcon.h>
69 #import <SpringBoard/SBAppWindow.h>
70 #import <SpringBoard/SBButtonBar.h>
71 #import <SpringBoard/SBContentLayer.h>
72 #import <SpringBoard/SBIconLabel.h>
73 #import <SpringBoard/SBStatusBarContentsView.h>
74 #import <SpringBoard/SBStatusBarController.h>
75 #import <SpringBoard/SBStatusBarTimeView.h>
76 #import <SpringBoard/SBUIController.h>
77
78 #import <MediaPlayer/MPVideoView.h>
79 #import <MediaPlayer/MPVideoView-PlaybackControl.h>
80
81 #import <CoreGraphics/CGGeometry.h>
82
83 @interface NSDictionary (WinterBoard)
84 - (UIColor *) colorForKey:(NSString *)key;
85 - (BOOL) boolForKey:(NSString *)key;
86 @end
87
88 @implementation NSDictionary (WinterBoard)
89
90 - (UIColor *) colorForKey:(NSString *)key {
91 NSString *value = [self objectForKey:key];
92 if (value == nil)
93 return nil;
94 /* XXX: incorrect */
95 return nil;
96 }
97
98 - (BOOL) boolForKey:(NSString *)key {
99 if (NSString *value = [self objectForKey:key])
100 return [value boolValue];
101 return NO;
102 }
103
104 @end
105
106 bool Debug_ = false;
107 bool Engineer_ = false;
108
109 /* WinterBoard Backend {{{ */
110 #define WBPrefix "wb_"
111
112 void WBInject(const char *classname, const char *oldname, IMP newimp, const char *type) {
113 Class _class = objc_getClass(classname);
114 if (_class == nil)
115 return;
116 if (!class_addMethod(_class, sel_registerName(oldname), newimp, type))
117 NSLog(@"WB:Error: failed to inject [%s %s]", classname, oldname);
118 }
119
120 void WBRename(bool instance, const char *classname, const char *oldname, IMP newimp) {
121 Class _class = objc_getClass(classname);
122 if (_class == nil) {
123 if (Debug_)
124 NSLog(@"WB:Warning: cannot find class [%s]", classname);
125 return;
126 }
127 if (!instance)
128 _class = object_getClass(_class);
129 Method method = class_getInstanceMethod(_class, sel_getUid(oldname));
130 if (method == nil) {
131 if (Debug_)
132 NSLog(@"WB:Warning: cannot find method [%s %s]", classname, oldname);
133 return;
134 }
135 size_t namelen = strlen(oldname);
136 char newname[sizeof(WBPrefix) + namelen];
137 memcpy(newname, WBPrefix, sizeof(WBPrefix) - 1);
138 memcpy(newname + sizeof(WBPrefix) - 1, oldname, namelen + 1);
139 const char *type = method_getTypeEncoding(method);
140 if (!class_addMethod(_class, sel_registerName(newname), method_getImplementation(method), type))
141 NSLog(@"WB:Error: failed to rename [%s %s]", classname, oldname);
142 unsigned int count;
143 Method *methods = class_copyMethodList(_class, &count);
144 for (unsigned int index(0); index != count; ++index)
145 if (methods[index] == method)
146 goto found;
147 if (newimp != NULL)
148 if (!class_addMethod(_class, sel_getUid(oldname), newimp, type))
149 NSLog(@"WB:Error: failed to rename [%s %s]", classname, oldname);
150 goto done;
151 found:
152 if (newimp != NULL)
153 method_setImplementation(method, newimp);
154 done:
155 free(methods);
156 }
157 /* }}} */
158
159 @protocol WinterBoard
160 - (NSString *) wb_pathForIcon;
161 - (NSString *) wb_pathForResource:(NSString *)resource ofType:(NSString *)type;
162 - (id) wb_init;
163 - (id) wb_layer;
164 - (id) wb_initWithSize:(CGSize)size;
165 - (id) wb_initWithSize:(CGSize)size label:(NSString *)label;
166 - (id) wb_initWithFrame:(CGRect)frame;
167 - (id) wb_initWithCoder:(NSCoder *)coder;
168 - (void) wb_setFrame:(CGRect)frame;
169 - (void) wb_drawRect:(CGRect)rect;
170 - (void) wb_setBackgroundColor:(id)color;
171 - (void) wb_setAlpha:(float)value;
172 - (void) wb_setBarStyle:(int)style;
173 - (id) wb_initWithFrame:(CGRect)frame withBarStyle:(int)style withTintColor:(UIColor *)color;
174 - (void) wb_setOpaque:(BOOL)opaque;
175 - (void) wb_setInDock:(BOOL)docked;
176 - (void) wb_didMoveToSuperview;
177 + (UIImage *) wb_imageNamed:(NSString *)name inBundle:(NSBundle *)bundle;
178 + (UIImage *) wb_applicationImageNamed:(NSString *)name;
179 - (NSDictionary *) wb_infoDictionary;
180 - (UIImage *) wb_icon;
181 - (id) wb_initWithStatusBar:(id)bar mode:(int)mode;
182 - (id) wb_initWithMode:(int)mode orientation:(int)orientation;
183 - (void) wb_setStatusBarMode:(int)mode orientation:(int)orientation duration:(float)duration fenceID:(int)id animation:(int)animation;
184 @end
185
186 NSMutableDictionary **ImageMap_;
187 NSMutableSet *UIImages_;
188
189 NSFileManager *Manager_;
190 NSDictionary *English_;
191 NSMutableDictionary *Info_;
192 NSMutableArray *themes_;
193
194 NSString *$pathForIcon$(SBApplication<WinterBoard> *self) {
195 for (NSString *theme in themes_) {
196 NSString *identifier = [self bundleIdentifier];
197 NSString *folder = [[self path] lastPathComponent];
198 NSString *dname = [self displayName];
199 NSString *didentifier = [self displayIdentifier];
200
201 if (Debug_) {
202 NSLog(@"WB:Debug: [SBApplication(%@:%@:%@:%@) pathForIcon]", identifier, folder, dname, didentifier);
203 }
204
205 #define testForIcon(Name) \
206 if (NSString *name = Name) { \
207 NSString *path = [NSString stringWithFormat:@"%@/Icons/%@.png", theme, name]; \
208 if ([Manager_ fileExistsAtPath:path]) \
209 return path; \
210 }
211
212 if (identifier != nil) {
213 NSString *path = [NSString stringWithFormat:@"%@/Bundles/%@/icon.png", theme, identifier];
214 if ([Manager_ fileExistsAtPath:path])
215 return path;
216 }
217
218 if (folder != nil) {
219 NSString *path = [NSString stringWithFormat:@"%@/Folders/%@/icon.png", theme, folder];
220 if ([Manager_ fileExistsAtPath:path])
221 return path;
222 }
223
224 testForIcon(identifier);
225 testForIcon(dname);
226
227 if (didentifier != nil) {
228 testForIcon([English_ objectForKey:didentifier]);
229
230 NSArray *parts = [didentifier componentsSeparatedByString:@"-"];
231 if ([parts count] != 1)
232 if (NSDictionary *english = [[[NSDictionary alloc] initWithContentsOfFile:[[self path] stringByAppendingString:@"/English.lproj/UIRoleDisplayNames.strings"]] autorelease])
233 testForIcon([english objectForKey:[parts lastObject]]);
234 }
235 }
236
237 return nil;
238 }
239
240 static UIImage *SBApplicationIcon$icon(SBApplicationIcon<WinterBoard> *self, SEL sel) {
241 if (![Info_ boolForKey:@"ComposeStoreIcons"])
242 if (NSString *path = $pathForIcon$([self application]))
243 return [UIImage imageWithContentsOfFile:path];
244 return [self wb_icon];
245 }
246
247 static NSString *SBApplication$pathForIcon(SBApplication<WinterBoard> *self, SEL sel) {
248 if (NSString *path = $pathForIcon$(self))
249 return path;
250 return [self wb_pathForIcon];
251 }
252
253 static NSString *$pathForFile$inBundle$(NSString *file, NSBundle *bundle) {
254 for (NSString *theme in themes_) {
255 NSString *identifier = [bundle bundleIdentifier];
256
257 if (identifier != nil) {
258 NSString *path = [NSString stringWithFormat:@"%@/Bundles/%@/%@", theme, identifier, file];
259 if (Debug_)
260 NSLog(@"WB:Debug:%@", path);
261 if ([Manager_ fileExistsAtPath:path])
262 return path;
263 }
264
265 if (NSString *folder = [[bundle bundlePath] lastPathComponent]) {
266 NSString *path = [NSString stringWithFormat:@"%@/Folders/%@/%@", theme, folder, file];
267 if (Debug_)
268 NSLog(@"WB:Debug:%@", path);
269 if ([Manager_ fileExistsAtPath:path])
270 return path;
271 }
272
273 #define remapResourceName(oldname, newname) \
274 else if ([file isEqualToString:oldname]) { \
275 NSString *path = [NSString stringWithFormat:@"%@/%@.png", theme, newname]; \
276 if (Debug_) \
277 NSLog(@"WB:Debug:%@", path); \
278 if ([Manager_ fileExistsAtPath:path]) \
279 return path; \
280 }
281
282 if (identifier == nil || ![identifier isEqualToString:@"com.apple.springboard"]);
283 remapResourceName(@"FSO_BG.png", @"StatusBar")
284 remapResourceName(@"SBDockBG.png", @"Dock")
285 remapResourceName(@"SBWeatherCelsius.png", @"Icons/Weather")
286 }
287
288 return nil;
289 }
290
291 static UIImage *UIImage$imageNamed$inBundle$(Class<WinterBoard> self, SEL sel, NSString *name, NSBundle *bundle) {
292 if (Debug_)
293 NSLog(@"WB:Debug: [UIImage(%@) imageNamed:\"%@\"]", [bundle bundleIdentifier], name);
294 if (NSString *path = $pathForFile$inBundle$(name, bundle))
295 return [UIImage imageWithContentsOfFile:path];
296 return [self wb_imageNamed:name inBundle:bundle];
297 }
298
299 static UIImage *UIImage$imageNamed$(Class<WinterBoard> self, SEL sel, NSString *name) {
300 return UIImage$imageNamed$inBundle$(self, sel, name, [NSBundle mainBundle]);
301 }
302
303 static UIImage *UIImage$applicationImageNamed$(Class<WinterBoard> self, SEL sel, NSString *name) {
304 NSBundle *bundle = [NSBundle mainBundle];
305 if (Debug_)
306 NSLog(@"WB:Debug: [UIImage(%@) applicationImageNamed:\"%@\"]", [bundle bundleIdentifier], name);
307 if (NSString *path = $pathForFile$inBundle$(name, bundle))
308 return [UIImage imageWithContentsOfFile:path];
309 return [self wb_applicationImageNamed:name];
310 }
311
312 static NSString *NSBundle$pathForResource$ofType$(NSBundle<WinterBoard> *self, SEL sel, NSString *resource, NSString *type) {
313 NSString *file = type == nil ? resource : [NSString stringWithFormat:@"%@.%@", resource, type];
314 if (Debug_)
315 NSLog(@"WB:Debug: [NSBundle(%@) pathForResource:\"%@\"]", [self bundleIdentifier], file);
316 if (NSString *path = $pathForFile$inBundle$(file, self))
317 return path;
318 return [self wb_pathForResource:resource ofType:type];
319 }
320
321 bool UINavigationBar$setBarStyle$_(SBAppWindow<WinterBoard> *self) {
322 if (NSNumber *number = [Info_ objectForKey:@"NavigationBarStyle"]) {
323 [self wb_setBarStyle:[number intValue]];
324 return true;
325 }
326
327 return false;
328 }
329
330 /*id UINavigationBarBackground$initWithFrame$withBarStyle$withTintColor$(UINavigationBarBackground<WinterBoard> *self, SEL sel, CGRect frame, int style, UIColor *tint) {
331 _trace();
332
333 if (NSNumber *number = [Info_ objectForKey:@"NavigationBarStyle"])
334 style = [number intValue];
335
336 if (UIColor *color = [Info_ colorForKey:@"NavigationBarTint"])
337 tint = color;
338
339 return [self wb_initWithFrame:frame withBarStyle:style withTintColor:tint];
340 }*/
341
342 /*id UINavigationBar$initWithCoder$(SBAppWindow<WinterBoard> *self, SEL sel, CGRect frame, NSCoder *coder) {
343 self = [self wb_initWithCoder:coder];
344 if (self == nil)
345 return nil;
346 UINavigationBar$setBarStyle$_(self);
347 return self;
348 }
349
350 id UINavigationBar$initWithFrame$(SBAppWindow<WinterBoard> *self, SEL sel, CGRect frame) {
351 self = [self wb_initWithFrame:frame];
352 if (self == nil)
353 return nil;
354 UINavigationBar$setBarStyle$_(self);
355 return self;
356 }*/
357
358 static void UINavigationBar$setBarStyle$(SBAppWindow<WinterBoard> *self, SEL sel, int style) {
359 if (UINavigationBar$setBarStyle$_(self))
360 return;
361 return [self wb_setBarStyle:style];
362 }
363
364 static void $didMoveToSuperview(SBButtonBar<WinterBoard> *self, SEL sel) {
365 [[self superview] setBackgroundColor:[UIColor clearColor]];
366 [self wb_didMoveToSuperview];
367 }
368
369 static NSString *$getTheme$(NSArray *files) {
370 for (NSString *theme in themes_)
371 for (NSString *file in files) {
372 NSString *path([NSString stringWithFormat:@"%@/%@", theme, file]);
373 if ([Manager_ fileExistsAtPath:path])
374 return path;
375 }
376
377 return nil;
378 }
379
380 static id SBContentLayer$initWithSize$(SBContentLayer<WinterBoard> *self, SEL sel, CGSize size) {
381 self = [self wb_initWithSize:size];
382 if (self == nil)
383 return nil;
384
385 if (NSString *path = $getTheme$([NSArray arrayWithObject:@"Wallpaper.mp4"])) {
386 MPVideoView *video = [[[MPVideoView alloc] initWithFrame:[self bounds]] autorelease];
387 [video setMovieWithPath:path];
388 [video setRepeatMode:1];
389 [video setRepeatGap:0];
390 [self addSubview:video];
391 [video playFromBeginning];;
392 }
393
394 if (NSString *path = $getTheme$([NSArray arrayWithObjects:@"Wallpaper.png", @"Wallpaper.jpg", nil]))
395 if (UIImage *image = [[[UIImage alloc] initWithContentsOfFile:path] autorelease])
396 [self addSubview:[[[UIImageView alloc] initWithImage:image] autorelease]];
397
398 if (NSString *path = $getTheme$([NSArray arrayWithObject:@"Wallpaper.html"])) {
399 CGRect bounds = [self bounds];
400
401 UIWebDocumentView *view([[[UIWebDocumentView alloc] initWithFrame:bounds] autorelease]);
402 [view setAutoresizes:YES];
403
404 [view loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:path]]];
405
406 [[view webView] setDrawsBackground:NO];
407 [view setBackgroundColor:[UIColor clearColor]];
408
409 [self addSubview:view];
410 }
411
412 return self;
413 }
414
415 #define WBDelegate(delegate) \
416 - (NSMethodSignature*) methodSignatureForSelector:(SEL)sel { \
417 if (Engineer_) \
418 NSLog(@"WB:MS:%s:(%s)", class_getName([self class]), sel_getName(sel)); \
419 if (NSMethodSignature *sig = [delegate methodSignatureForSelector:sel]) \
420 return sig; \
421 NSLog(@"WB:Error: [%s methodSignatureForSelector:(%s)]", class_getName([self class]), sel_getName(sel)); \
422 return nil; \
423 } \
424 \
425 - (void) forwardInvocation:(NSInvocation*)inv { \
426 SEL sel = [inv selector]; \
427 if ([delegate respondsToSelector:sel]) \
428 [inv invokeWithTarget:delegate]; \
429 else \
430 NSLog(@"WB:Error: [%s forwardInvocation:(%s)]", class_getName([self class]), sel_getName(sel)); \
431 }
432
433 static unsigned *ContextCount_;
434 static void ***ContextStack_;
435
436 extern "C" CGColorRef CGGStateGetSystemColor(void *);
437 extern "C" CGColorRef CGGStateGetFillColor(void *);
438 extern "C" CGColorRef CGGStateGetStrokeColor(void *);
439 extern "C" NSString *UIStyleStringFromColor(CGColorRef);
440
441 @interface WBTime : NSProxy {
442 NSString *time_;
443 _transient SBStatusBarTimeView *view_;
444 }
445
446 - (id) initWithTime:(NSString *)time view:(SBStatusBarTimeView *)view;
447
448 @end
449
450 @implementation WBTime
451
452 - (void) dealloc {
453 [time_ release];
454 [super dealloc];
455 }
456
457 - (id) initWithTime:(NSString *)time view:(SBStatusBarTimeView *)view {
458 time_ = [time retain];
459 view_ = view;
460 return self;
461 }
462
463 WBDelegate(time_)
464
465 - (CGSize) drawAtPoint:(CGPoint)point forWidth:(float)width withFont:(UIFont *)font lineBreakMode:(int)mode {
466 if (NSString *custom = [Info_ objectForKey:@"TimeStyle"]) {
467 BOOL mode;
468 object_getInstanceVariable(view_, "_mode", (void **) &mode);
469
470 [time_ drawAtPoint:point withStyle:[NSString stringWithFormat:@""
471 "font-family: Helvetica; "
472 "font-weight: bold; "
473 "font-size: 14px; "
474 "color: %@; "
475 "%@", mode ? @"white" : @"black", custom]];
476
477 return CGSizeZero;
478 }
479
480 return [time_ drawAtPoint:point forWidth:width withFont:font lineBreakMode:mode];
481 }
482
483 @end
484
485 @interface WBIconLabel : NSProxy {
486 NSString *string_;
487 BOOL docked_;
488 }
489
490 - (id) initWithString:(NSString *)string;
491
492 @end
493
494 @implementation WBIconLabel
495
496 - (void) dealloc {
497 [string_ release];
498 [super dealloc];
499 }
500
501 - (id) initWithString:(NSString *)string {
502 string_ = [string retain];
503 return self;
504 }
505
506 WBDelegate(string_)
507
508 - (NSString *) _iconLabelStyle {
509 NSString *key = docked_ ? @"DockedIconLabelStyle" : @"UndockedIconLabelStyle";
510 NSString *style = [Info_ objectForKey:key];
511 return style;
512 }
513
514 - (CGSize) drawInRect:(CGRect)rect withFont:(UIFont *)font lineBreakMode:(int)mode alignment:(int)alignment {
515 if (NSString *custom = [self _iconLabelStyle]) {
516 NSString *style = [NSString stringWithFormat:@""
517 "font-family: Helvetica; "
518 "font-weight: bold; "
519 "font-size: 11px; "
520 "text-align: center; "
521 "color: %@; "
522 "%@", docked_ ? @"white" : @"#b3b3b3", custom];
523
524 if (Debug_)
525 NSLog(@"WB:Debug:style = %@", style);
526 [string_ drawInRect:rect withStyle:style];
527 return CGSizeZero;
528 }
529
530 return [string_ drawInRect:rect withFont:font lineBreakMode:mode alignment:alignment];
531 }
532
533 - (void) drawInRect:(CGRect)rect withStyle:(NSString *)style {
534 if (NSString *custom = [self _iconLabelStyle]) {
535 NSString *combined = [NSString stringWithFormat:@"%@; %@", style, custom];
536 if (Debug_)
537 NSLog(@"WB:Debug:combined = %@", combined);
538 return [string_ drawInRect:rect withStyle:combined];
539 }
540 return [string_ drawInRect:rect withStyle:style];
541 }
542
543 - (BOOL) respondsToSelector:(SEL)sel {
544 return
545 sel == @selector(setInDock:)
546 ? YES : [super respondsToSelector:sel];
547 }
548
549 - (void) setInDock:(BOOL)docked {
550 docked_ = docked;
551 }
552
553 @end
554
555 static void SBStatusBarController$setStatusBarMode$orientation$duration$fenceID$animation$(SBStatusBarController<WinterBoard> *self, SEL sel, int mode, int orientation, float duration, int id, int animation) {
556 if (NSNumber *number = [Info_ objectForKey:@"StatusBarMode"])
557 mode = [number intValue];
558 return [self wb_setStatusBarMode:mode orientation:orientation duration:duration fenceID:id animation:animation];
559 }
560
561 /*static id SBStatusBar$initWithMode$orientation$(SBStatusBar<WinterBoard> *self, SEL sel, int mode, int orientation) {
562 return [self wb_initWithMode:mode orientation:orientation];
563 }*/
564
565 static id SBStatusBarContentsView$initWithStatusBar$mode$(SBStatusBarContentsView<WinterBoard> *self, SEL sel, id bar, int mode) {
566 if (NSNumber *number = [Info_ objectForKey:@"StatusBarContentsMode"])
567 mode = [number intValue];
568 return [self wb_initWithStatusBar:bar mode:mode];
569 }
570
571 static void SBStatusBarTimeView$drawRect$(SBStatusBarTimeView<WinterBoard> *self, SEL sel, CGRect rect) {
572 id time;
573 object_getInstanceVariable(self, "_time", (void **) &time);
574 if (time != nil && [time class] != [WBTime class])
575 object_setInstanceVariable(self, "_time", (void *) [[WBTime alloc] initWithTime:[time autorelease] view:self]);
576 return [self wb_drawRect:rect];
577 }
578
579 static void SBIconLabel$setInDock$(SBIconLabel<WinterBoard> *self, SEL sel, BOOL docked) {
580 id label;
581 object_getInstanceVariable(self, "_label", (void **) &label);
582 if (![Info_ boolForKey:@"UndockedIconLabels"])
583 docked = YES;
584 if (label != nil && [label respondsToSelector:@selector(setInDock:)])
585 [label setInDock:docked];
586 return [self wb_setInDock:docked];
587 }
588
589 static id SBIconLabel$initWithSize$label$(SBIconLabel<WinterBoard> *self, SEL sel, CGSize size, NSString *label) {
590 // XXX: technically I'm misusing self here
591 return [self wb_initWithSize:size label:[[[WBIconLabel alloc] initWithString:label] autorelease]];
592 //return [self wb_initWithSize:size label:label];
593 }
594
595 extern "C" void FindMappedImages(void);
596 extern "C" NSData *UIImagePNGRepresentation(UIImage *);
597
598 static void (*__UISharedImageInitialize)(bool);
599
600 extern "C" void WBInitialize() {
601 NSLog(@"WB:Notice: Installing WinterBoard...");
602
603 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
604
605 struct nlist nl[5];
606 memset(nl, 0, sizeof(nl));
607
608 nl[0].n_un.n_name = (char *) "___mappedImages";
609 nl[1].n_un.n_name = (char *) "__UISharedImageInitialize";
610 nl[2].n_un.n_name = (char *) "___currentContextCount";
611 nl[3].n_un.n_name = (char *) "___currentContextStack";
612
613 nlist("/System/Library/Frameworks/UIKit.framework/UIKit", nl);
614
615 ImageMap_ = (id *) nl[0].n_value;
616 __UISharedImageInitialize = (void (*)(bool)) nl[1].n_value;
617 ContextCount_ = (unsigned *) nl[2].n_value;
618 ContextStack_ = (void ***) nl[3].n_value;
619
620 __UISharedImageInitialize(false);
621
622 English_ = [[NSDictionary alloc] initWithContentsOfFile:@"/System/Library/CoreServices/SpringBoard.app/English.lproj/LocalizedApplicationNames.strings"];
623 if (English_ != nil)
624 English_ = [English_ retain];
625
626 Manager_ = [[NSFileManager defaultManager] retain];
627 UIImages_ = [[NSMutableSet alloc] initWithCapacity:16];
628
629 //WBRename("UINavigationBar", "initWithCoder:", (IMP) &UINavigationBar$initWithCoder$);
630 WBRename(true, "UINavigationBar", "setBarStyle:", (IMP) &UINavigationBar$setBarStyle$);
631 //WBRename("UINavigationBarBackground", "initWithFrame:withBarStyle:withTintColor:", (IMP) &UINavigationBarBackground$initWithFrame$withBarStyle$withTintColor$);
632
633 WBRename(false, "UIImage", "imageNamed:inBundle:", (IMP) &UIImage$imageNamed$inBundle$);
634 WBRename(false, "UIImage", "imageNamed:", (IMP) &UIImage$imageNamed$);
635 WBRename(false, "UIImage", "applicationImageNamed:", (IMP) &UIImage$applicationImageNamed$);
636 WBRename(true, "SBApplicationIcon", "icon", (IMP) &SBApplicationIcon$icon);
637 WBRename(true, "SBApplication", "pathForIcon", (IMP) &SBApplication$pathForIcon);
638 WBRename(true, "NSBundle", "pathForResource:ofType:", (IMP) &NSBundle$pathForResource$ofType$);
639 WBRename(true, "SBContentLayer", "initWithSize:", (IMP) &SBContentLayer$initWithSize$);
640 WBRename(true, "SBStatusBarContentsView", "initWithStatusBar:mode:", (IMP) &SBStatusBarContentsView$initWithStatusBar$mode$);
641 //WBRename(true, "SBStatusBar", "initWithMode:orientation:", (IMP) &SBStatusBar$initWithMode$orientation$);
642 WBRename(true, "SBStatusBarContentsView", "didMoveToSuperview", (IMP) &$didMoveToSuperview);
643 WBRename(true, "SBButtonBar", "didMoveToSuperview", (IMP) &$didMoveToSuperview);
644 WBRename(true, "SBIconLabel", "setInDock:", (IMP) &SBIconLabel$setInDock$);
645 WBRename(true, "SBIconLabel", "initWithSize:label:", (IMP) &SBIconLabel$initWithSize$label$);
646 WBRename(true, "SBStatusBarTimeView", "drawRect:", (IMP) &SBStatusBarTimeView$drawRect$);
647 WBRename(true, "SBStatusBarController", "setStatusBarMode:orientation:duration:fenceID:animation:", (IMP) &SBStatusBarController$setStatusBarMode$orientation$duration$fenceID$animation$);
648
649 themes_ = [[NSMutableArray alloc] initWithCapacity:8];
650
651 if (NSDictionary *settings = [[NSDictionary alloc] initWithContentsOfFile:[NSString stringWithFormat:@"/User/Library/Preferences/com.saurik.WinterBoard.plist"]]) {
652 [settings autorelease];
653
654 NSArray *themes = [settings objectForKey:@"Themes"];
655 if (themes == nil)
656 if (NSString *theme = [settings objectForKey:@"Theme"])
657 themes = [NSArray arrayWithObject:[NSDictionary dictionaryWithObjectsAndKeys:
658 theme, @"Name",
659 [NSNumber numberWithBool:YES], @"Active",
660 nil]];
661 if (themes != nil)
662 for (NSDictionary *theme in themes) {
663 NSNumber *active = [theme objectForKey:@"Active"];
664 if (![active boolValue])
665 continue;
666
667 NSString *name = [theme objectForKey:@"Name"];
668 if (name == nil)
669 continue;
670
671 NSString *theme = nil;
672
673 if (theme == nil) {
674 NSString *path = [NSString stringWithFormat:@"/Library/Themes/%@.theme", name];
675 if ([Manager_ fileExistsAtPath:path]) {
676 [themes_ addObject:path];
677 continue;
678 }
679 }
680
681 if (theme == nil) {
682 NSString *path = [NSString stringWithFormat:@"/Library/Themes/%@", name];
683 if ([Manager_ fileExistsAtPath:path]) {
684 [themes_ addObject:path];
685 continue;
686 }
687 }
688
689 if (theme == nil) {
690 NSString *path = [NSString stringWithFormat:@"%@/Library/SummerBoard/Themes/%@", NSHomeDirectory(), name];
691 if ([Manager_ fileExistsAtPath:path]) {
692 [themes_ addObject:path];
693 continue;
694 }
695 }
696 }
697 }
698
699 Info_ = [[NSMutableDictionary dictionaryWithCapacity:16] retain];
700
701 for (NSString *theme in themes_) {
702 NSString *folder = [NSString stringWithFormat:@"%@/UIImages", theme];
703 if (NSArray *images = [Manager_ contentsOfDirectoryAtPath:folder error:NULL])
704 for (int i(0), e = [images count]; i != e; ++i) {
705 NSString *name = [images objectAtIndex:i];
706 if (![name hasSuffix:@".png"])
707 continue;
708 if ([UIImages_ containsObject:name])
709 continue;
710 NSString *path = [NSString stringWithFormat:@"%@/%@", folder, name];
711 UIImage *image = [UIImage imageWithContentsOfFile:path];
712 [*ImageMap_ setObject:(id)[image imageRef] forKey:name];
713 [UIImages_ addObject:name];
714 }
715
716 if (NSDictionary *info = [[NSDictionary alloc] initWithContentsOfFile:[NSString stringWithFormat:@"%@/Info.plist", theme]])
717 for (NSString *key in [info allKeys])
718 if ([Info_ objectForKey:key] == nil)
719 [Info_ setObject:[info objectForKey:key] forKey:key];
720 }
721
722 if ([Info_ objectForKey:@"UndockedIconLabels"] == nil)
723 [Info_ setObject:[NSNumber numberWithBool:(
724 [Info_ objectForKey:@"DockedIconLabelStyle"] != nil ||
725 [Info_ objectForKey:@"UndockedIconLabelStyle"] != nil
726 )] forKey:@"UndockedIconLabels"];
727
728 if (![Info_ boolForKey:@"UndockedIconLabels"])
729 if (Debug_)
730 NSLog(@"WB:Debug:Info = %@", [Info_ description]);
731
732 [pool release];
733 }