]> git.saurik.com Git - winterboard.git/blob - Library.mm
Added .artwork theming.
[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(@"_trace(%u)", __LINE__);
39
40 #include <objc/runtime.h>
41 #include <objc/message.h>
42
43 extern "C" {
44 #include <mach-o/nlist.h>
45 }
46
47 #import <Foundation/Foundation.h>
48 #import <CoreGraphics/CoreGraphics.h>
49
50 #import <UIKit/UIColor.h>
51 #import <UIKit/UIImage.h>
52 #import <UIKit/UIImageView.h>
53 #import <UIKit/UINavigationBarBackground.h>
54
55 #import <UIKit/UIImage-UIImageDeprecated.h>
56
57 #import <UIKit/UIView-Geometry.h>
58 #import <UIKit/UIView-Hierarchy.h>
59 #import <UIKit/UIView-Rendering.h>
60
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>
66
67 #import <CoreGraphics/CGGeometry.h>
68
69 @interface NSDictionary (WinterBoard)
70 - (UIColor *) colorForKey:(NSString *)key;
71 @end
72
73 @implementation NSDictionary (WinterBoard)
74
75 - (UIColor *) colorForKey:(NSString *)key {
76 NSString *value = [self objectForKey:key];
77 if (value == nil)
78 return nil;
79 /* XXX: incorrect */
80 return nil;
81 }
82
83 @end
84
85 /* WinterBoard Backend {{{ */
86 #define WBPrefix "wb_"
87
88 void WBInject(const char *classname, const char *oldname, IMP newimp, const char *type) {
89 Class _class = objc_getClass(classname);
90 if (_class == nil)
91 return;
92 if (!class_addMethod(_class, sel_registerName(oldname), newimp, type))
93 NSLog(@"WB:Error: failed to inject [%s %s]", classname, oldname);
94 }
95
96 void WBRename(const char *classname, const char *oldname, IMP newimp) {
97 Class _class = objc_getClass(classname);
98 if (_class == nil) {
99 NSLog(@"WB:Warning: cannot find class [%s]", classname);
100 return;
101 }
102 Method method = class_getInstanceMethod(_class, sel_getUid(oldname));
103 if (method == nil) {
104 NSLog(@"WB:Warning: cannot find method [%s %s]", classname, oldname);
105 return;
106 }
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);
114 unsigned int count;
115 Method *methods = class_copyMethodList(_class, &count);
116 for (unsigned int index(0); index != count; ++index)
117 if (methods[index] == method)
118 goto found;
119 if (newimp != NULL)
120 if (!class_addMethod(_class, sel_getUid(oldname), newimp, type))
121 NSLog(@"WB:Error: failed to rename [%s %s]", classname, oldname);
122 goto done;
123 found:
124 if (newimp != NULL)
125 method_setImplementation(method, newimp);
126 done:
127 free(methods);
128 }
129 /* }}} */
130
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;
142 @end
143
144 NSMutableDictionary **ImageMap_;
145
146 bool Debug_;
147
148 NSFileManager *Manager_;
149 NSDictionary *Info_;
150 NSString *theme_;
151 NSString *Wallpaper_;
152
153 NSString *SBApplication$pathForIcon(SBApplication<WinterBoard> *self, SEL sel) {
154 if (theme_ != nil) {
155 NSString *identifier = [self bundleIdentifier];
156
157 #define testForIcon(Name) \
158 if (NSString *name = Name) { \
159 NSString *path = [NSString stringWithFormat:@"%@/Icons/%@.png", theme_, name]; \
160 if ([Manager_ fileExistsAtPath:path]) \
161 return path; \
162 }
163
164 testForIcon([self displayName]);
165 testForIcon(identifier);
166
167 if (identifier != nil) {
168 NSString *path = [NSString stringWithFormat:@"%@/Bundles/%@/icon.png", theme_, identifier];
169 if ([Manager_ fileExistsAtPath:path])
170 return path;
171 }
172 }
173
174 return [self wb_pathForIcon];
175 }
176
177 NSString *NSBundle$pathForResource$ofType$(NSBundle<WinterBoard> *self, SEL sel, NSString *resource, NSString *type) {
178 if (Debug_)
179 NSLog(@"WB:Debug: [NSBundle(%@) pathForResource:\"%@.%@\"]", [self bundleIdentifier], resource, type);
180
181 if (theme_ != nil) {
182 NSString *identifier = [self bundleIdentifier];
183
184 if (identifier != nil) {
185 NSString *path = [NSString stringWithFormat:@"%@/Bundles/%@/%@.%@", theme_, identifier, resource, type];
186 if ([Manager_ fileExistsAtPath:path])
187 return path;
188 }
189
190 if ([resource isEqualToString:@"SBDockBG"] && [type isEqualToString:@"png"]) {
191 NSString *path = [NSString stringWithFormat:@"%@/Dock.png", theme_];
192 if ([Manager_ fileExistsAtPath:path])
193 return path;
194 }
195 }
196
197 return [self wb_pathForResource:resource ofType:type];
198 }
199
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];
204 }
205
206 bool UINavigationBar$setBarStyle$_(SBAppWindow<WinterBoard> *self) {
207 if (Info_ != nil) {
208 NSNumber *number = [Info_ objectForKey:@"NavigationBarStyle"];
209 if (number != nil) {
210 [self wb_setBarStyle:[number intValue]];
211 return true;
212 }
213 }
214
215 return false;
216 }
217
218 /*id UINavigationBarBackground$initWithFrame$withBarStyle$withTintColor$(UINavigationBarBackground<WinterBoard> *self, SEL sel, CGRect frame, int style, UIColor *tint) {
219 _trace();
220
221 if (Info_ != nil) {
222 NSNumber *number = [Info_ objectForKey:@"NavigationBarStyle"];
223 if (number != nil)
224 style = [number intValue];
225
226 UIColor *color = [Info_ colorForKey:@"NavigationBarTint"];
227 if (color != nil)
228 tint = color;
229 }
230
231 return [self wb_initWithFrame:frame withBarStyle:style withTintColor:tint];
232 }*/
233
234 /*id UINavigationBar$initWithCoder$(SBAppWindow<WinterBoard> *self, SEL sel, CGRect frame, NSCoder *coder) {
235 self = [self wb_initWithCoder:coder];
236 if (self == nil)
237 return nil;
238 UINavigationBar$setBarStyle$_(self);
239 return self;
240 }
241
242 id UINavigationBar$initWithFrame$(SBAppWindow<WinterBoard> *self, SEL sel, CGRect frame) {
243 self = [self wb_initWithFrame:frame];
244 if (self == nil)
245 return nil;
246 UINavigationBar$setBarStyle$_(self);
247 return self;
248 }*/
249
250 void UINavigationBar$setBarStyle$(SBAppWindow<WinterBoard> *self, SEL sel, int style) {
251 if (UINavigationBar$setBarStyle$_(self))
252 return;
253 return [self wb_setBarStyle:style];
254 }
255
256 /*id SBButtonBar$initWithFrame$(SBButtonBar<WinterBoard> *self, SEL sel, CGRect frame) {
257 self = [self wb_initWithFrame:frame];
258 if (self == nil)
259 return nil;
260 if (Wallpaper_ != nil)
261 [self setBackgroundColor:[UIColor clearColor]];
262 return self;
263 }*/
264
265 id SBContentLayer$initWithSize$(SBContentLayer<WinterBoard> *self, SEL sel, CGSize size) {
266 self = [self wb_initWithSize:size];
267 if (self == nil)
268 return nil;
269
270 if (Wallpaper_ != nil)
271 if (UIImage *image = [[UIImage alloc] initWithContentsOfFile:Wallpaper_])
272 [self addSubview:[[[UIImageView alloc] initWithImage:image] autorelease]];
273
274 return self;
275 }
276
277 extern "C" void FindMappedImages(void);
278 extern "C" NSData *UIImagePNGRepresentation(UIImage *);
279
280 extern "C" void WBInitialize() {
281 NSLog(@"WB:Notice: Installing WinterBoard...");
282
283 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
284
285 struct nlist nl[3];
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;
292
293 __UISharedImageInitialize(false);
294
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];
303 [pool release];
304 }*/
305
306 Manager_ = [[NSFileManager defaultManager] retain];
307
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$);
316
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"];
320 NSString *path;
321
322 if (theme_ == nil) {
323 path = [NSString stringWithFormat:@"%@/Library/SummerBoard/Themes/%@", NSHomeDirectory(), name];
324 if ([Manager_ fileExistsAtPath:path])
325 theme_ = [path retain];
326 }
327
328 if (theme_ == nil) {
329 path = [NSString stringWithFormat:@"/Library/Themes/%@", name];
330 if ([Manager_ fileExistsAtPath:path])
331 theme_ = [path retain];
332 }
333 }
334
335 if (theme_ != nil) {
336 NSString *path = [NSString stringWithFormat:@"%@/Wallpaper.png", theme_];
337 if ([Manager_ fileExistsAtPath:path])
338 Wallpaper_ = [path retain];
339
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"])
345 continue;
346 NSString *path = [NSString stringWithFormat:@"%@/%@", folder, name];
347 UIImage *image = [UIImage imageWithContentsOfFile:path];
348 [*ImageMap_ setObject:(id)[image imageRef] forKey:name];
349 }
350
351 Info_ = [[NSDictionary alloc] initWithContentsOfFile:[NSString stringWithFormat:@"%@/Info.plist", theme_]];
352 if (Info_ == nil) {
353 //LabelColor_ = [UIColor whiteColor];
354 } else {
355 //LabelColor_ = [Info_ colorForKey:@"LabelColor"];
356 }
357 }
358
359 [pool release];
360 }