1 /* WinterBoard - Theme Manager for the iPhone
2 * Copyright (C) 2008 Jay Freeman (saurik)
6 * Redistribution and use in source and binary
7 * forms, with or without modification, are permitted
8 * provided that the following conditions are met:
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
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.
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.
38 #define _trace() NSLog(@"WB:_trace(%u)", __LINE__);
41 #include <objc/runtime.h>
42 #include <objc/message.h>
45 #include <mach-o/nlist.h>
48 #import <Foundation/Foundation.h>
49 #import <CoreGraphics/CoreGraphics.h>
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>
58 #import <UIKit/NSString-UIStringDrawing.h>
59 #import <UIKit/NSString-UIStringDrawingDeprecated.h>
61 #import <UIKit/UIImage-UIImageDeprecated.h>
63 #import <UIKit/UIView-Geometry.h>
64 #import <UIKit/UIView-Hierarchy.h>
65 #import <UIKit/UIView-Rendering.h>
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/SBStatusBarTimeView.h>
75 #import <SpringBoard/SBUIController.h>
77 #import <CoreGraphics/CGGeometry.h>
79 @interface NSDictionary (WinterBoard)
80 - (UIColor *) colorForKey:(NSString *)key;
81 - (BOOL) boolForKey:(NSString *)key;
84 @implementation NSDictionary (WinterBoard)
86 - (UIColor *) colorForKey:(NSString *)key {
87 NSString *value = [self objectForKey:key];
94 - (BOOL) boolForKey:(NSString *)key {
95 if (NSString *value = [self objectForKey:key])
96 return [value boolValue];
103 bool Engineer_ = false;
105 /* WinterBoard Backend {{{ */
106 #define WBPrefix "wb_"
108 void WBInject(const char *classname, const char *oldname, IMP newimp, const char *type) {
109 Class _class = objc_getClass(classname);
112 if (!class_addMethod(_class, sel_registerName(oldname), newimp, type))
113 NSLog(@"WB:Error: failed to inject [%s %s]", classname, oldname);
116 void WBRename(bool instance, const char *classname, const char *oldname, IMP newimp) {
117 Class _class = objc_getClass(classname);
120 NSLog(@"WB:Warning: cannot find class [%s]", classname);
124 _class = object_getClass(_class);
125 Method method = class_getInstanceMethod(_class, sel_getUid(oldname));
128 NSLog(@"WB:Warning: cannot find method [%s %s]", classname, oldname);
131 size_t namelen = strlen(oldname);
132 char newname[sizeof(WBPrefix) + namelen];
133 memcpy(newname, WBPrefix, sizeof(WBPrefix) - 1);
134 memcpy(newname + sizeof(WBPrefix) - 1, oldname, namelen + 1);
135 const char *type = method_getTypeEncoding(method);
136 if (!class_addMethod(_class, sel_registerName(newname), method_getImplementation(method), type))
137 NSLog(@"WB:Error: failed to rename [%s %s]", classname, oldname);
139 Method *methods = class_copyMethodList(_class, &count);
140 for (unsigned int index(0); index != count; ++index)
141 if (methods[index] == method)
144 if (!class_addMethod(_class, sel_getUid(oldname), newimp, type))
145 NSLog(@"WB:Error: failed to rename [%s %s]", classname, oldname);
149 method_setImplementation(method, newimp);
155 @protocol WinterBoard
156 - (NSString *) wb_pathForIcon;
157 - (NSString *) wb_pathForResource:(NSString *)resource ofType:(NSString *)type;
160 - (id) wb_initWithSize:(CGSize)size;
161 - (id) wb_initWithSize:(CGSize)size label:(NSString *)label;
162 - (id) wb_initWithFrame:(CGRect)frame;
163 - (id) wb_initWithCoder:(NSCoder *)coder;
164 - (void) wb_setFrame:(CGRect)frame;
165 - (void) wb_drawRect:(CGRect)rect;
166 - (void) wb_setBackgroundColor:(id)color;
167 - (void) wb_setAlpha:(float)value;
168 - (void) wb_setBarStyle:(int)style;
169 - (id) wb_initWithFrame:(CGRect)frame withBarStyle:(int)style withTintColor:(UIColor *)color;
170 - (void) wb_setOpaque:(BOOL)opaque;
171 - (void) wb_setInDock:(BOOL)docked;
172 - (void) wb_didMoveToSuperview;
173 + (UIImage *) wb_imageNamed:(NSString *)name inBundle:(NSBundle *)bundle;
174 - (NSDictionary *) wb_infoDictionary;
175 - (UIImage *) wb_icon;
178 NSMutableDictionary **ImageMap_;
180 NSFileManager *Manager_;
181 NSDictionary *English_;
185 NSString *$pathForIcon$(SBApplication<WinterBoard> *self) {
186 NSString *identifier = [self bundleIdentifier];
188 #define testForIcon(Name) \
189 if (NSString *name = Name) { \
190 NSString *path = [NSString stringWithFormat:@"%@/Icons/%@.png", theme_, name]; \
191 if ([Manager_ fileExistsAtPath:path]) \
195 if (identifier != nil) {
196 NSString *path = [NSString stringWithFormat:@"%@/Bundles/%@/icon.png", theme_, identifier];
197 if ([Manager_ fileExistsAtPath:path])
201 if (NSString *folder = [[self path] lastPathComponent]) {
202 NSString *path = [NSString stringWithFormat:@"%@/Folders/%@/icon.png", theme_, folder];
203 if ([Manager_ fileExistsAtPath:path])
207 testForIcon(identifier);
208 testForIcon([self displayName]);
210 if (NSString *display = [self displayIdentifier])
211 testForIcon([English_ objectForKey:display]);
216 static UIImage *SBApplicationIcon$icon(SBApplicationIcon<WinterBoard> *self, SEL sel) {
217 if (Info_ == nil || ![Info_ boolForKey:@"RenderIcons"])
218 if (NSString *path = $pathForIcon$([self application]))
219 return [UIImage imageWithContentsOfFile:path];
220 return [self wb_icon];
223 static NSString *SBApplication$pathForIcon(SBApplication<WinterBoard> *self, SEL sel) {
225 if (NSString *path = $pathForIcon$(self))
228 return [self wb_pathForIcon];
231 static NSString *$pathForFile$inBundle$(NSString *file, NSBundle *bundle) {
233 NSString *identifier = [bundle bundleIdentifier];
235 if (identifier != nil) {
236 NSString *path = [NSString stringWithFormat:@"%@/Bundles/%@/%@", theme_, identifier, file];
237 if ([Manager_ fileExistsAtPath:path])
241 if (NSString *folder = [[bundle bundlePath] lastPathComponent]) {
242 NSString *path = [NSString stringWithFormat:@"%@/Folders/%@/%@", theme_, folder, file];
243 if ([Manager_ fileExistsAtPath:path])
247 #define remapResourceName(oldname, newname) \
248 else if ([file isEqualToString:oldname]) { \
249 NSString *path = [NSString stringWithFormat:@"%@/%@.png", theme_, newname]; \
250 if ([Manager_ fileExistsAtPath:path]) \
254 if (identifier == nil || ![identifier isEqualToString:@"com.apple.springboard"]);
255 remapResourceName(@"FSO_BG.png", @"StatusBar")
256 remapResourceName(@"SBDockBG.png", @"Dock")
257 remapResourceName(@"SBWeatherCelsius.png", @"Icons/Weather")
263 static UIImage *UIImage$imageNamed$inBundle$(Class<WinterBoard> self, SEL sel, NSString *name, NSBundle *bundle) {
265 NSLog(@"WB:Debug: [UIImage(%@) imageNamed:\"%@\"]", [bundle bundleIdentifier], name);
266 if (NSString *path = $pathForFile$inBundle$(name, bundle))
267 return [UIImage imageWithContentsOfFile:path];
268 return [self wb_imageNamed:name inBundle:bundle];
271 static UIImage *UIImage$imageNamed$(Class<WinterBoard> self, SEL sel, NSString *name) {
272 return UIImage$imageNamed$inBundle$(self, sel, name, [NSBundle mainBundle]);
275 static NSString *NSBundle$pathForResource$ofType$(NSBundle<WinterBoard> *self, SEL sel, NSString *resource, NSString *type) {
276 NSString *file = type == nil ? resource : [NSString stringWithFormat:@"%@.%@", resource, type];
278 NSLog(@"WB:Debug: [NSBundle(%@) pathForResource:\"%@\"]", [self bundleIdentifier], file);
279 if (NSString *path = $pathForFile$inBundle$(file, self))
281 return [self wb_pathForResource:resource ofType:type];
284 bool UINavigationBar$setBarStyle$_(SBAppWindow<WinterBoard> *self) {
286 NSNumber *number = [Info_ objectForKey:@"NavigationBarStyle"];
288 [self wb_setBarStyle:[number intValue]];
296 /*id UINavigationBarBackground$initWithFrame$withBarStyle$withTintColor$(UINavigationBarBackground<WinterBoard> *self, SEL sel, CGRect frame, int style, UIColor *tint) {
300 NSNumber *number = [Info_ objectForKey:@"NavigationBarStyle"];
302 style = [number intValue];
304 UIColor *color = [Info_ colorForKey:@"NavigationBarTint"];
309 return [self wb_initWithFrame:frame withBarStyle:style withTintColor:tint];
312 /*id UINavigationBar$initWithCoder$(SBAppWindow<WinterBoard> *self, SEL sel, CGRect frame, NSCoder *coder) {
313 self = [self wb_initWithCoder:coder];
316 UINavigationBar$setBarStyle$_(self);
320 id UINavigationBar$initWithFrame$(SBAppWindow<WinterBoard> *self, SEL sel, CGRect frame) {
321 self = [self wb_initWithFrame:frame];
324 UINavigationBar$setBarStyle$_(self);
328 static void UINavigationBar$setBarStyle$(SBAppWindow<WinterBoard> *self, SEL sel, int style) {
329 if (UINavigationBar$setBarStyle$_(self))
331 return [self wb_setBarStyle:style];
334 static void $didMoveToSuperview(SBButtonBar<WinterBoard> *self, SEL sel) {
335 [[self superview] setBackgroundColor:[UIColor clearColor]];
336 [self wb_didMoveToSuperview];
339 static NSString *$getTheme$(NSString *file) {
340 NSString *path([NSString stringWithFormat:@"%@/%@", theme_, file]);
341 return [Manager_ fileExistsAtPath:path] ? path : nil;
344 static id SBContentLayer$initWithSize$(SBContentLayer<WinterBoard> *self, SEL sel, CGSize size) {
345 self = [self wb_initWithSize:size];
349 if (NSString *path = $getTheme$(@"Wallpaper.png"))
350 if (UIImage *image = [[[UIImage alloc] initWithContentsOfFile:path] autorelease])
351 [self addSubview:[[[UIImageView alloc] initWithImage:image] autorelease]];
352 if (NSString *path = $getTheme$(@"Wallpaper.html")) {
353 CGRect bounds = [self bounds];
355 UIWebDocumentView *view([[[UIWebDocumentView alloc] initWithFrame:bounds] autorelease]);
356 [view setAutoresizes:YES];
358 [view loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:path]]];
360 [[view webView] setDrawsBackground:NO];
361 [view setBackgroundColor:[UIColor clearColor]];
363 [self addSubview:view];
369 #define WBDelegate(delegate) \
370 - (NSMethodSignature*) methodSignatureForSelector:(SEL)sel { \
372 NSLog(@"WB:MS:%s:(%s)", class_getName([self class]), sel_getName(sel)); \
373 if (NSMethodSignature *sig = [delegate methodSignatureForSelector:sel]) \
375 NSLog(@"WB:Error: [%s methodSignatureForSelector:(%s)]", class_getName([self class]), sel_getName(sel)); \
379 - (void) forwardInvocation:(NSInvocation*)inv { \
380 SEL sel = [inv selector]; \
381 if ([delegate respondsToSelector:sel]) \
382 [inv invokeWithTarget:delegate]; \
384 NSLog(@"WB:Error: [%s forwardInvocation:(%s)]", class_getName([self class]), sel_getName(sel)); \
387 static unsigned *ContextCount_;
388 static void ***ContextStack_;
390 extern "C" CGColorRef CGGStateGetSystemColor(void *);
391 extern "C" CGColorRef CGGStateGetFillColor(void *);
392 extern "C" CGColorRef CGGStateGetStrokeColor(void *);
393 extern "C" NSString *UIStyleStringFromColor(CGColorRef);
395 @interface WBTime : NSProxy {
397 _transient SBStatusBarTimeView *view_;
400 - (id) initWithTime:(NSString *)time view:(SBStatusBarTimeView *)view;
404 @implementation WBTime
411 - (id) initWithTime:(NSString *)time view:(SBStatusBarTimeView *)view {
412 time_ = [time retain];
419 - (CGSize) drawAtPoint:(CGPoint)point forWidth:(float)width withFont:(UIFont *)font lineBreakMode:(int)mode {
421 if (NSString *custom = [Info_ objectForKey:@"TimeStyle"]) {
423 object_getInstanceVariable(view_, "_mode", (void **) &mode);
425 [time_ drawAtPoint:point withStyle:[NSString stringWithFormat:@""
426 "font-family: Helvetica; "
427 "font-weight: bold; "
430 "%@", mode ? @"white" : @"black", custom]];
435 return [time_ drawAtPoint:point forWidth:width withFont:font lineBreakMode:mode];
440 @interface WBIconLabel : NSProxy {
445 - (id) initWithString:(NSString *)string;
449 @implementation WBIconLabel
456 - (id) initWithString:(NSString *)string {
457 string_ = [string retain];
463 - (NSString *) _iconLabelStyle {
464 return Info_ == nil ? nil : [Info_ objectForKey:(docked_ ? @"DockedIconLabelStyle" : @"UndockedIconLabelStyle")];
467 - (CGSize) drawInRect:(CGRect)rect withFont:(UIFont *)font lineBreakMode:(int)mode alignment:(int)alignment {
468 if (NSString *custom = [self _iconLabelStyle]) {
469 [string_ drawInRect:rect withStyle:[NSString stringWithFormat:@""
470 "font-family: Helvetica; "
471 "font-weight: bold; "
473 "text-align: center; "
475 "%@", docked_ ? @"white" : @"#b3b3b3", custom]];
480 return [string_ drawInRect:rect withFont:font lineBreakMode:mode alignment:alignment];
483 - (void) drawInRect:(CGRect)rect withStyle:(NSString *)style {
484 if (NSString *custom = [self _iconLabelStyle])
485 return [string_ drawInRect:rect withStyle:[NSString stringWithFormat:@"%@; %@", style, custom]];
486 return [string_ drawInRect:rect withStyle:style];
489 - (BOOL) respondsToSelector:(SEL)sel {
491 sel == @selector(setInDock:)
492 ? YES : [super respondsToSelector:sel];
495 - (void) setInDock:(BOOL)docked {
501 static void SBStatusBarTimeView$drawRect$(SBStatusBarTimeView<WinterBoard> *self, SEL sel, CGRect rect) {
503 object_getInstanceVariable(self, "_time", (void **) &time);
504 if (time != nil && [time class] != [WBTime class])
505 object_setInstanceVariable(self, "_time", (void *) [[WBTime alloc] initWithTime:[time autorelease] view:self]);
506 return [self wb_drawRect:rect];
509 static void SBIconLabel$setInDock$(SBIconLabel<WinterBoard> *self, SEL sel, BOOL docked) {
511 object_getInstanceVariable(self, "_label", (void **) &label);
512 if (Info_ == nil || [Info_ boolForKey:@"IconLabelInDock"])
514 if (label != nil && [label respondsToSelector:@selector(setInDock:)])
515 [label setInDock:docked];
516 return [self wb_setInDock:docked];
519 static id SBIconLabel$initWithSize$label$(SBIconLabel<WinterBoard> *self, SEL sel, CGSize size, NSString *label) {
520 // XXX: technically I'm misusing self here
521 return [self wb_initWithSize:size label:[[[WBIconLabel alloc] initWithString:label] autorelease]];
522 //return [self wb_initWithSize:size label:label];
525 extern "C" void FindMappedImages(void);
526 extern "C" NSData *UIImagePNGRepresentation(UIImage *);
528 static void (*__UISharedImageInitialize)(bool);
530 extern "C" void WBInitialize() {
531 NSLog(@"WB:Notice: Installing WinterBoard...");
533 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
536 memset(nl, 0, sizeof(nl));
538 nl[0].n_un.n_name = (char *) "___mappedImages";
539 nl[1].n_un.n_name = (char *) "__UISharedImageInitialize";
540 nl[2].n_un.n_name = (char *) "___currentContextCount";
541 nl[3].n_un.n_name = (char *) "___currentContextStack";
543 nlist("/System/Library/Frameworks/UIKit.framework/UIKit", nl);
545 ImageMap_ = (id *) nl[0].n_value;
546 __UISharedImageInitialize = (void (*)(bool)) nl[1].n_value;
547 ContextCount_ = (unsigned *) nl[2].n_value;
548 ContextStack_ = (void ***) nl[3].n_value;
550 __UISharedImageInitialize(false);
552 English_ = [[NSDictionary alloc] initWithContentsOfFile:@"/System/Library/CoreServices/SpringBoard.app/English.lproj/LocalizedApplicationNames.strings"];
554 English_ = [English_ retain];
556 Manager_ = [[NSFileManager defaultManager] retain];
558 //WBRename("UINavigationBar", "initWithCoder:", (IMP) &UINavigationBar$initWithCoder$);
559 WBRename(true, "UINavigationBar", "setBarStyle:", (IMP) &UINavigationBar$setBarStyle$);
560 //WBRename("UINavigationBarBackground", "initWithFrame:withBarStyle:withTintColor:", (IMP) &UINavigationBarBackground$initWithFrame$withBarStyle$withTintColor$);
562 WBRename(false, "UIImage", "imageNamed:inBundle:", (IMP) &UIImage$imageNamed$inBundle$);
563 WBRename(false, "UIImage", "imageNamed:", (IMP) &UIImage$imageNamed$);
564 WBRename(true, "SBApplicationIcon", "icon", (IMP) &SBApplicationIcon$icon);
565 WBRename(true, "SBApplication", "pathForIcon", (IMP) &SBApplication$pathForIcon);
566 WBRename(true, "NSBundle", "pathForResource:ofType:", (IMP) &NSBundle$pathForResource$ofType$);
567 WBRename(true, "SBContentLayer", "initWithSize:", (IMP) &SBContentLayer$initWithSize$);
568 WBRename(true, "SBStatusBarContentsView", "didMoveToSuperview", (IMP) &$didMoveToSuperview);
569 WBRename(true, "SBButtonBar", "didMoveToSuperview", (IMP) &$didMoveToSuperview);
570 WBRename(true, "SBIconLabel", "setInDock:", (IMP) &SBIconLabel$setInDock$);
571 WBRename(true, "SBIconLabel", "initWithSize:label:", (IMP) &SBIconLabel$initWithSize$label$);
572 WBRename(true, "SBStatusBarTimeView", "drawRect:", (IMP) &SBStatusBarTimeView$drawRect$);
574 if (NSDictionary *settings = [[NSDictionary alloc] initWithContentsOfFile:[NSString stringWithFormat:@"%@/Library/Preferences/com.saurik.WinterBoard.plist", NSHomeDirectory()]]) {
575 [settings autorelease];
576 NSString *name = [settings objectForKey:@"Theme"];
580 path = [NSString stringWithFormat:@"%@/Library/SummerBoard/Themes/%@", NSHomeDirectory(), name];
581 if ([Manager_ fileExistsAtPath:path])
582 theme_ = [path retain];
586 path = [NSString stringWithFormat:@"/Library/Themes/%@", name];
587 if ([Manager_ fileExistsAtPath:path])
588 theme_ = [path retain];
593 NSString *folder = [NSString stringWithFormat:@"%@/UIImages", theme_];
594 if (NSArray *images = [Manager_ contentsOfDirectoryAtPath:folder error:NULL])
595 for (int i(0), e = [images count]; i != e; ++i) {
596 NSString *name = [images objectAtIndex:i];
597 if (![name hasSuffix:@".png"])
599 NSString *path = [NSString stringWithFormat:@"%@/%@", folder, name];
600 UIImage *image = [UIImage imageWithContentsOfFile:path];
601 [*ImageMap_ setObject:(id)[image imageRef] forKey:name];
604 Info_ = [[NSDictionary alloc] initWithContentsOfFile:[NSString stringWithFormat:@"%@/Info.plist", theme_]];