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(@"_trace(%u)", __LINE__);
40 #include <objc/runtime.h>
41 #include <objc/message.h>
44 #include <mach-o/nlist.h>
47 #import <Foundation/Foundation.h>
48 #import <CoreGraphics/CoreGraphics.h>
50 #import <UIKit/UIColor.h>
51 #import <UIKit/UIImage.h>
52 #import <UIKit/UIImageView.h>
53 #import <UIKit/UINavigationBarBackground.h>
55 #import <UIKit/UIImage-UIImageDeprecated.h>
57 #import <UIKit/UIView-Geometry.h>
58 #import <UIKit/UIView-Hierarchy.h>
59 #import <UIKit/UIView-Rendering.h>
61 #import <SpringBoard/SBApplication.h>
62 #import <SpringBoard/SBAppWindow.h>
63 #import <SpringBoard/SBButtonBar.h>
64 #import <SpringBoard/SBContentLayer.h>
65 #import <SpringBoard/SBUIController.h>
67 #import <CoreGraphics/CGGeometry.h>
69 @interface NSDictionary (WinterBoard)
70 - (UIColor *) colorForKey:(NSString *)key;
73 @implementation NSDictionary (WinterBoard)
75 - (UIColor *) colorForKey:(NSString *)key {
76 NSString *value = [self objectForKey:key];
85 /* WinterBoard Backend {{{ */
86 #define WBPrefix "wb_"
88 void WBInject(const char *classname, const char *oldname, IMP newimp, const char *type) {
89 Class _class = objc_getClass(classname);
92 if (!class_addMethod(_class, sel_registerName(oldname), newimp, type))
93 NSLog(@"WB:Error: failed to inject [%s %s]", classname, oldname);
96 void WBRename(const char *classname, const char *oldname, IMP newimp) {
97 Class _class = objc_getClass(classname);
99 NSLog(@"WB:Warning: cannot find class [%s]", classname);
102 Method method = class_getInstanceMethod(_class, sel_getUid(oldname));
104 NSLog(@"WB:Warning: cannot find method [%s %s]", classname, oldname);
107 size_t namelen = strlen(oldname);
108 char newname[sizeof(WBPrefix) + namelen];
109 memcpy(newname, WBPrefix, sizeof(WBPrefix) - 1);
110 memcpy(newname + sizeof(WBPrefix) - 1, oldname, namelen + 1);
111 const char *type = method_getTypeEncoding(method);
112 if (!class_addMethod(_class, sel_registerName(newname), method_getImplementation(method), type))
113 NSLog(@"WB:Error: failed to rename [%s %s]", classname, oldname);
115 Method *methods = class_copyMethodList(_class, &count);
116 for (unsigned int index(0); index != count; ++index)
117 if (methods[index] == method)
120 if (!class_addMethod(_class, sel_getUid(oldname), newimp, type))
121 NSLog(@"WB:Error: failed to rename [%s %s]", classname, oldname);
125 method_setImplementation(method, newimp);
131 @protocol WinterBoard
132 - (NSString *) wb_pathForIcon;
133 - (NSString *) wb_pathForResource:(NSString *)resource ofType:(NSString *)type;
134 - (id) wb_initWithSize:(CGSize)size;
135 - (id) wb_initWithFrame:(CGRect)frame;
136 - (id) wb_initWithCoder:(NSCoder *)coder;
137 - (void) wb_setFrame:(CGRect)frame;
138 - (void) wb_setBackgroundColor:(id)color;
139 - (void) wb_setAlpha:(float)value;
140 - (void) wb_setBarStyle:(int)style;
141 - (id) wb_initWithFrame:(CGRect)frame withBarStyle:(int)style withTintColor:(UIColor *)color;
144 NSMutableDictionary **ImageMap_;
148 NSFileManager *Manager_;
151 NSString *Wallpaper_;
153 NSString *SBApplication$pathForIcon(SBApplication<WinterBoard> *self, SEL sel) {
155 NSString *identifier = [self bundleIdentifier];
157 #define testForIcon(Name) \
158 if (NSString *name = Name) { \
159 NSString *path = [NSString stringWithFormat:@"%@/Icons/%@.png", theme_, name]; \
160 if ([Manager_ fileExistsAtPath:path]) \
164 testForIcon([self displayName]);
165 testForIcon(identifier);
167 if (identifier != nil) {
168 NSString *path = [NSString stringWithFormat:@"%@/Bundles/%@/icon.png", theme_, identifier];
169 if ([Manager_ fileExistsAtPath:path])
174 return [self wb_pathForIcon];
177 NSString *NSBundle$pathForResource$ofType$(NSBundle<WinterBoard> *self, SEL sel, NSString *resource, NSString *type) {
179 NSLog(@"WB:Debug: [NSBundle(%@) pathForResource:\"%@.%@\"]", [self bundleIdentifier], resource, type);
182 NSString *identifier = [self bundleIdentifier];
184 if (identifier != nil) {
185 NSString *path = [NSString stringWithFormat:@"%@/Bundles/%@/%@.%@", theme_, identifier, resource, type];
186 if ([Manager_ fileExistsAtPath:path])
190 if ([resource isEqualToString:@"SBDockBG"] && [type isEqualToString:@"png"]) {
191 NSString *path = [NSString stringWithFormat:@"%@/Dock.png", theme_];
192 if ([Manager_ fileExistsAtPath:path])
197 return [self wb_pathForResource:resource ofType:type];
200 void SBAppWindow$setBackgroundColor$(SBAppWindow<WinterBoard> *self, SEL sel, UIColor *color) {
201 if (Wallpaper_ != nil)
202 return [self wb_setBackgroundColor:[UIColor clearColor]];
203 return [self wb_setBackgroundColor:color];
206 bool UINavigationBar$setBarStyle$_(SBAppWindow<WinterBoard> *self) {
208 NSNumber *number = [Info_ objectForKey:@"NavigationBarStyle"];
210 [self wb_setBarStyle:[number intValue]];
218 /*id UINavigationBarBackground$initWithFrame$withBarStyle$withTintColor$(UINavigationBarBackground<WinterBoard> *self, SEL sel, CGRect frame, int style, UIColor *tint) {
222 NSNumber *number = [Info_ objectForKey:@"NavigationBarStyle"];
224 style = [number intValue];
226 UIColor *color = [Info_ colorForKey:@"NavigationBarTint"];
231 return [self wb_initWithFrame:frame withBarStyle:style withTintColor:tint];
234 /*id UINavigationBar$initWithCoder$(SBAppWindow<WinterBoard> *self, SEL sel, CGRect frame, NSCoder *coder) {
235 self = [self wb_initWithCoder:coder];
238 UINavigationBar$setBarStyle$_(self);
242 id UINavigationBar$initWithFrame$(SBAppWindow<WinterBoard> *self, SEL sel, CGRect frame) {
243 self = [self wb_initWithFrame:frame];
246 UINavigationBar$setBarStyle$_(self);
250 void UINavigationBar$setBarStyle$(SBAppWindow<WinterBoard> *self, SEL sel, int style) {
251 if (UINavigationBar$setBarStyle$_(self))
253 return [self wb_setBarStyle:style];
256 /*id SBButtonBar$initWithFrame$(SBButtonBar<WinterBoard> *self, SEL sel, CGRect frame) {
257 self = [self wb_initWithFrame:frame];
260 if (Wallpaper_ != nil)
261 [self setBackgroundColor:[UIColor clearColor]];
265 id SBContentLayer$initWithSize$(SBContentLayer<WinterBoard> *self, SEL sel, CGSize size) {
266 self = [self wb_initWithSize:size];
270 if (Wallpaper_ != nil)
271 if (UIImage *image = [[UIImage alloc] initWithContentsOfFile:Wallpaper_])
272 [self addSubview:[[[UIImageView alloc] initWithImage:image] autorelease]];
277 extern "C" void FindMappedImages(void);
278 extern "C" NSData *UIImagePNGRepresentation(UIImage *);
280 extern "C" void WBInitialize() {
281 NSLog(@"WB:Notice: Installing WinterBoard...");
283 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
286 memset(nl, 0, sizeof(nl));
287 nl[0].n_un.n_name = (char *) "___mappedImages";
288 nl[1].n_un.n_name = (char *) "__UISharedImageInitialize";
289 nlist("/System/Library/Frameworks/UIKit.framework/UIKit", nl);
290 ImageMap_ = (id *) nl[0].n_value;
291 void (*__UISharedImageInitialize)(bool) = (void (*)(bool)) nl[1].n_value;
293 __UISharedImageInitialize(false);
295 /*NSArray *keys = [*ImageMap_ allKeys];
296 for (int i(0), e([keys count]); i != e; ++i) {
297 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
298 NSString *key = [keys objectAtIndex:i];
299 CGImageRef ref = (CGImageRef) [*ImageMap_ objectForKey:key];
300 UIImage *image = [UIImage imageWithCGImage:ref];
301 NSData *data = UIImagePNGRepresentation(image);
302 [data writeToFile:[NSString stringWithFormat:@"/tmp/pwnr/%@", key] atomically:YES];
306 Manager_ = [[NSFileManager defaultManager] retain];
308 WBRename("SBApplication", "pathForIcon", (IMP) &SBApplication$pathForIcon);
309 WBRename("NSBundle", "pathForResource:ofType:", (IMP) &NSBundle$pathForResource$ofType$);
310 WBRename("SBAppWindow", "setBackgroundColor:", (IMP) &SBAppWindow$setBackgroundColor$);
311 WBRename("SBContentLayer", "initWithSize:", (IMP) &SBContentLayer$initWithSize$);
312 //WBRename("UINavigationBar", "initWithFrame:", (IMP) &UINavigationBar$initWithFrame$);
313 //WBRename("UINavigationBar", "initWithCoder:", (IMP) &UINavigationBar$initWithCoder$);
314 WBRename("UINavigationBar", "setBarStyle:", (IMP) &UINavigationBar$setBarStyle$);
315 //WBRename("UINavigationBarBackground", "initWithFrame:withBarStyle:withTintColor:", (IMP) &UINavigationBarBackground$initWithFrame$withBarStyle$withTintColor$);
317 if (NSDictionary *settings = [[NSDictionary alloc] initWithContentsOfFile:[NSString stringWithFormat:@"%@/Library/Preferences/com.saurik.WinterBoard.plist", NSHomeDirectory()]]) {
318 [settings autorelease];
319 NSString *name = [settings objectForKey:@"Theme"];
323 path = [NSString stringWithFormat:@"%@/Library/SummerBoard/Themes/%@", NSHomeDirectory(), name];
324 if ([Manager_ fileExistsAtPath:path])
325 theme_ = [path retain];
329 path = [NSString stringWithFormat:@"/Library/Themes/%@", name];
330 if ([Manager_ fileExistsAtPath:path])
331 theme_ = [path retain];
336 NSString *path = [NSString stringWithFormat:@"%@/Wallpaper.png", theme_];
337 if ([Manager_ fileExistsAtPath:path])
338 Wallpaper_ = [path retain];
340 NSString *folder = [NSString stringWithFormat:@"%@/UIImages", theme_];
341 if (NSArray *images = [Manager_ contentsOfDirectoryAtPath:folder error:NULL])
342 for (int i(0), e = [images count]; i != e; ++i) {
343 NSString *name = [images objectAtIndex:i];
344 if (![name hasSuffix:@".png"])
346 NSString *path = [NSString stringWithFormat:@"%@/%@", folder, name];
347 UIImage *image = [UIImage imageWithContentsOfFile:path];
348 [*ImageMap_ setObject:(id)[image imageRef] forKey:name];
351 Info_ = [[NSDictionary alloc] initWithContentsOfFile:[NSString stringWithFormat:@"%@/Info.plist", theme_]];
353 //LabelColor_ = [UIColor whiteColor];
355 //LabelColor_ = [Info_ colorForKey:@"LabelColor"];