]> git.saurik.com Git - winterboard.git/blame - Library.mm
Positioning adjustment.
[winterboard.git] / Library.mm
CommitLineData
d5168fd6 1/* WinterBoard - Theme Manager for the iPhone
9e42420f 2 * Copyright (C) 2008-2010 Jay Freeman (saurik)
d5168fd6
JF
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
69550bfb
JF
38#include <sys/time.h>
39
40struct timeval _ltv;
41bool _itv;
42
43#define _trace() do { \
44 struct timeval _ctv; \
45 gettimeofday(&_ctv, NULL); \
46 if (!_itv) { \
47 _itv = true; \
48 _ltv = _ctv; \
49 } \
50 fprintf(stderr, "%lu.%.6u[%f]:_trace()@%s:%u[%s]\n", \
51 _ctv.tv_sec, _ctv.tv_usec, \
52 (_ctv.tv_sec - _ltv.tv_sec) + (_ctv.tv_usec - _ltv.tv_usec) / 1000000.0, \
53 __FILE__, __LINE__, __FUNCTION__\
54 ); \
55 _ltv = _ctv; \
56} while (false)
57
ca13798d 58#define _transient
62b2dbad 59
e6f0817b 60#import <CoreFoundation/CoreFoundation.h>
d5168fd6 61#import <Foundation/Foundation.h>
2435118f 62#import <CoreGraphics/CoreGraphics.h>
d5168fd6 63
d806256e
JF
64#import <Celestial/AVController.h>
65#import <Celestial/AVItem.h>
66#import <Celestial/AVQueue.h>
67
2acbe5b8
JF
68#include <substrate.h>
69
4668cd8e 70#import <UIKit/UIKit.h>
d5168fd6
JF
71
72#import <SpringBoard/SBApplication.h>
394d1eb5 73#import <SpringBoard/SBApplicationIcon.h>
d5168fd6 74#import <SpringBoard/SBAppWindow.h>
9cfe3924 75#import <SpringBoard/SBAwayView.h>
502d350e 76#import <SpringBoard/SBBookmarkIcon.h>
62b2dbad 77#import <SpringBoard/SBButtonBar.h>
e6f0817b 78#import <SpringBoard/SBCalendarIconContentsView.h>
95a5777b 79#import <SpringBoard/SBIconController.h>
889cb4f2 80#import <SpringBoard/SBIconLabel.h>
a8d008e4 81#import <SpringBoard/SBIconList.h>
98fe8d52 82#import <SpringBoard/SBIconModel.h>
82b4a0bd 83#import <SpringBoard/SBImageCache.h>
1a4ffdf8 84// XXX: #import <SpringBoard/SBSearchView.h>
9c64bab2 85#import <SpringBoard/SBSearchTableViewCell.h>
08454e3a 86#import <SpringBoard/SBStatusBarContentsView.h>
d5fb6e01 87#import <SpringBoard/SBStatusBarController.h>
4668cd8e 88#import <SpringBoard/SBStatusBarOperatorNameView.h>
08454e3a 89#import <SpringBoard/SBStatusBarTimeView.h>
d5168fd6 90#import <SpringBoard/SBUIController.h>
4f1b7acd 91#import <SpringBoard/SBWidgetApplicationIcon.h>
d5168fd6 92
4668cd8e
JF
93#import <MobileSMS/mSMSMessageTranscriptController.h>
94
d806256e 95#import <MediaPlayer/MPMoviePlayerController.h>
d5fb6e01
JF
96#import <MediaPlayer/MPVideoView.h>
97#import <MediaPlayer/MPVideoView-PlaybackControl.h>
98
d5168fd6
JF
99#import <CoreGraphics/CGGeometry.h>
100
198eb03b
JF
101#import <ChatKit/CKMessageCell.h>
102
95a5777b
JF
103extern "C" void __clear_cache (char *beg, char *end);
104
0316ebc4
JF
105@protocol WinterBoard
106- (void *) _node;
107@end
108
d806256e 109Class $MPMoviePlayerController;
95a5777b 110Class $MPVideoView;
0316ebc4
JF
111Class $WebCoreFrameBridge;
112
113Class $NSBundle;
114
115Class $UIImage;
116Class $UINavigationBar;
117Class $UIToolbar;
118
198eb03b
JF
119Class $CKMessageCell;
120Class $CKTimestampView;
8b94f6b0 121Class $CKTranscriptController;
198eb03b 122Class $CKTranscriptTableView;
8b94f6b0 123
0316ebc4
JF
124Class $SBApplication;
125Class $SBApplicationIcon;
9cfe3924 126Class $SBAwayView;
0316ebc4
JF
127Class $SBBookmarkIcon;
128Class $SBButtonBar;
129Class $SBCalendarIconContentsView;
b0d0c73f 130Class $SBIcon;
0316ebc4
JF
131Class $SBIconBadge;
132Class $SBIconController;
133Class $SBIconLabel;
a8d008e4 134Class $SBIconList;
0316ebc4 135Class $SBIconModel;
82b4a0bd 136//Class $SBImageCache;
1a4ffdf8 137Class $SBSearchView;
9c64bab2 138Class $SBSearchTableViewCell;
0316ebc4
JF
139Class $SBStatusBarContentsView;
140Class $SBStatusBarController;
141Class $SBStatusBarOperatorNameView;
142Class $SBStatusBarTimeView;
69550bfb 143Class $SBUIController;
4f1b7acd 144Class $SBWidgetApplicationIcon;
95a5777b 145
1d3b613f
JF
146static bool IsWild_;
147
26c43b47 148@interface NSDictionary (WinterBoard)
37351a17
JF
149- (UIColor *) wb$colorForKey:(NSString *)key;
150- (BOOL) wb$boolForKey:(NSString *)key;
26c43b47
JF
151@end
152
153@implementation NSDictionary (WinterBoard)
154
37351a17 155- (UIColor *) wb$colorForKey:(NSString *)key {
26c43b47
JF
156 NSString *value = [self objectForKey:key];
157 if (value == nil)
158 return nil;
159 /* XXX: incorrect */
160 return nil;
161}
162
37351a17 163- (BOOL) wb$boolForKey:(NSString *)key {
889cb4f2
JF
164 if (NSString *value = [self objectForKey:key])
165 return [value boolValue];
95a5777b 166 return false;
889cb4f2
JF
167}
168
26c43b47
JF
169@end
170
81decb42
JF
171static BOOL (*_GSFontGetUseLegacyFontMetrics)();
172#define $GSFontGetUseLegacyFontMetrics() \
173 (_GSFontGetUseLegacyFontMetrics == NULL ? YES : _GSFontGetUseLegacyFontMetrics())
174
f22a8989 175bool Debug_ = false;
e6f0817b 176bool Engineer_ = false;
265e19b2 177bool SummerBoard_ = true;
889cb4f2 178
4df9352e
JF
179static UIImage *(*_UIApplicationImageWithName)(NSString *name);
180static UIImage *(*_UIImageAtPath)(NSString *name, NSBundle *path);
181static CGImageRef (*_UIImageRefAtPath)(NSString *name, bool cache, UIImageOrientation *orientation);
182static UIImage *(*_UIImageWithNameInDomain)(NSString *name, NSString *domain);
183static NSBundle *(*_UIKitBundle)();
4df9352e
JF
184static int (*_UISharedImageNameGetIdentifier)(NSString *);
185static UIImage *(*_UISharedImageWithIdentifier)(int);
186
502d350e
JF
187static NSMutableDictionary *UIImages_;
188static NSMutableDictionary *PathImages_;
4668cd8e
JF
189static NSMutableDictionary *Cache_;
190static NSMutableDictionary *Strings_;
0316ebc4 191static NSMutableDictionary *Themed_;
4df9352e 192static NSMutableDictionary *Bundles_;
2435118f 193
502d350e
JF
194static NSFileManager *Manager_;
195static NSDictionary *English_;
196static NSMutableDictionary *Info_;
197static NSMutableArray *themes_;
d5168fd6 198
0316ebc4 199static NSString *$getTheme$(NSArray *files, bool parent = false) {
e839c3fe
JF
200 if (!parent)
201 if (NSString *path = [Themed_ objectForKey:files])
202 return reinterpret_cast<id>(path) == [NSNull null] ? nil : path;
203
2acbe5b8
JF
204 if (Debug_)
205 NSLog(@"WB:Debug: %@", [files description]);
206
e839c3fe
JF
207 NSString *path;
208
502d350e
JF
209 for (NSString *theme in themes_)
210 for (NSString *file in files) {
e839c3fe
JF
211 path = [NSString stringWithFormat:@"%@/%@", theme, file];
212 if ([Manager_ fileExistsAtPath:path]) {
213 path = parent ? theme : path;
214 goto set;
215 }
502d350e
JF
216 }
217
e839c3fe
JF
218 path = nil;
219 set:
220 if (!parent)
221 [Themed_ setObject:(path == nil ? [NSNull null] : reinterpret_cast<id>(path)) forKey:files];
222 return path;
502d350e
JF
223}
224
0316ebc4 225static NSString *$pathForFile$inBundle$(NSString *file, NSBundle *bundle, bool ui) {
4df9352e
JF
226 NSString *identifier = [bundle bundleIdentifier];
227 NSMutableArray *names = [NSMutableArray arrayWithCapacity:8];
228
229 if (identifier != nil)
230 [names addObject:[NSString stringWithFormat:@"Bundles/%@/%@", identifier, file]];
231 if (NSString *folder = [[bundle bundlePath] lastPathComponent])
232 [names addObject:[NSString stringWithFormat:@"Folders/%@/%@", folder, file]];
233 if (ui)
234 [names addObject:[NSString stringWithFormat:@"UIImages/%@", file]];
235
236 #define remapResourceName(oldname, newname) \
237 else if ([file isEqualToString:oldname]) \
238 [names addObject:[NSString stringWithFormat:@"%@.png", newname]]; \
239
240 if (identifier == nil);
198eb03b
JF
241 else if ([identifier isEqualToString:@"com.apple.chatkit"])
242 [names addObject:[NSString stringWithFormat:@"Bundles/com.apple.MobileSMS/%@", file]];
4df9352e
JF
243 else if ([identifier isEqualToString:@"com.apple.calculator"])
244 [names addObject:[NSString stringWithFormat:@"Files/Applications/Calculator.app/%@", file]];
265e19b2 245 else if (![identifier isEqualToString:@"com.apple.springboard"] || !SummerBoard_);
4df9352e
JF
246 remapResourceName(@"FSO_BG.png", @"StatusBar")
247 remapResourceName(@"SBDockBG.png", @"Dock")
248 remapResourceName(@"SBWeatherCelsius.png", @"Icons/Weather")
249
250 if (NSString *path = $getTheme$(names))
251 return path;
252 return nil;
253}
254
0316ebc4 255static NSString *$pathForIcon$(SBApplication *self) {
2acbe5b8
JF
256 NSString *identifier = [self bundleIdentifier];
257 NSString *path = [self path];
258 NSString *folder = [path lastPathComponent];
259 NSString *dname = [self displayName];
260 NSString *didentifier = [self displayIdentifier];
d5fb6e01 261
2acbe5b8
JF
262 if (Debug_)
263 NSLog(@"WB:Debug: [SBApplication(%@:%@:%@:%@) pathForIcon]", identifier, folder, dname, didentifier);
62b2dbad 264
2acbe5b8 265 NSMutableArray *names = [NSMutableArray arrayWithCapacity:8];
d5fb6e01 266
265e19b2 267 /* XXX: I might need to keep this for backwards compatibility
2acbe5b8
JF
268 if (identifier != nil)
269 [names addObject:[NSString stringWithFormat:@"Bundles/%@/icon.png", identifier]];
270 if (folder != nil)
265e19b2 271 [names addObject:[NSString stringWithFormat:@"Folders/%@/icon.png", folder]]; */
889cb4f2 272
2acbe5b8
JF
273 #define testForIcon(Name) \
274 if (NSString *name = Name) \
275 [names addObject:[NSString stringWithFormat:@"Icons/%@.png", name]];
ca13798d 276
9e42420f
JF
277 if (![didentifier isEqualToString:identifier])
278 testForIcon(didentifier);
24ba43ee 279
2acbe5b8
JF
280 testForIcon(identifier);
281 testForIcon(dname);
889cb4f2 282
24ba43ee
JF
283 if ([identifier isEqualToString:@"com.apple.MobileSMS"])
284 testForIcon(@"SMS");
285
2acbe5b8
JF
286 if (didentifier != nil) {
287 testForIcon([English_ objectForKey:didentifier]);
889cb4f2 288
2acbe5b8
JF
289 NSArray *parts = [didentifier componentsSeparatedByString:@"-"];
290 if ([parts count] != 1)
291 if (NSDictionary *english = [[[NSDictionary alloc] initWithContentsOfFile:[path stringByAppendingString:@"/English.lproj/UIRoleDisplayNames.strings"]] autorelease])
292 testForIcon([english objectForKey:[parts lastObject]]);
d5fb6e01 293 }
889cb4f2 294
2acbe5b8
JF
295 if (NSString *path = $getTheme$(names))
296 return path;
24ba43ee 297
394d1eb5
JF
298 return nil;
299}
300
4df9352e
JF
301@interface NSBundle (WinterBoard)
302+ (NSBundle *) wb$bundleWithFile:(NSString *)path;
303@end
304
305@implementation NSBundle (WinterBoard)
306
307+ (NSBundle *) wb$bundleWithFile:(NSString *)path {
308 path = [path stringByDeletingLastPathComponent];
309 if (path == nil || [path length] == 0 || [path isEqualToString:@"/"])
310 return nil;
311
312 NSBundle *bundle([Bundles_ objectForKey:path]);
313 if (reinterpret_cast<id>(bundle) == [NSNull null])
314 return nil;
315 else if (bundle == nil) {
0316ebc4
JF
316 if ([Manager_ fileExistsAtPath:[path stringByAppendingPathComponent:@"Info.plist"]])
317 bundle = [NSBundle bundleWithPath:path];
4df9352e
JF
318 if (bundle == nil)
319 bundle = [NSBundle wb$bundleWithFile:path];
320 if (Debug_)
321 NSLog(@"WB:Debug:PathBundle(%@, %@)", path, bundle);
44aa4549 322 [Bundles_ setObject:(bundle == nil ? [NSNull null] : reinterpret_cast<id>(bundle)) forKey:path];
4df9352e
JF
323 }
324
325 return bundle;
326}
327
328@end
329
330@interface NSString (WinterBoard)
331- (NSString *) wb$themedPath;
332@end
333
334@implementation NSString (WinterBoard)
335
336- (NSString *) wb$themedPath {
337 if (Debug_)
338 NSLog(@"WB:Debug:Bypass(\"%@\")", self);
339
340 if (NSBundle *bundle = [NSBundle wb$bundleWithFile:self]) {
341 NSString *file([self stringByResolvingSymlinksInPath]);
342 NSString *prefix([[bundle bundlePath] stringByResolvingSymlinksInPath]);
343 if ([file hasPrefix:prefix]) {
344 NSUInteger length([prefix length]);
345 if (length != [file length])
346 if (NSString *path = $pathForFile$inBundle$([file substringFromIndex:(length + 1)], bundle, false))
347 return path;
348 }
349 }
350
351 return self;
352}
353
354@end
355
9c64bab2
JF
356void WBLogRect(const char *tag, struct CGRect rect) {
357 NSLog(@"%s:{%f,%f+%f,%f}", tag, rect.origin.x, rect.origin.y, rect.size.width, rect.size.height);
358}
359
360void WBLogHierarchy(UIView *view, unsigned index = 0, unsigned indent = 0) {
361 CGRect frame([view frame]);
198eb03b 362 NSLog(@"%*s|%2d:%p:%s : {%f,%f+%f,%f} (%@)", indent * 3, "", index, view, class_getName([view class]), frame.origin.x, frame.origin.y, frame.size.width, frame.size.height, [view backgroundColor]);
1a4ffdf8
JF
363 index = 0;
364 for (UIView *child in [view subviews])
9c64bab2 365 WBLogHierarchy(child, index++, indent + 1);
1a4ffdf8
JF
366}
367
82b4a0bd
JF
368UIImage *$cacheForImage$(UIImage *image) {
369 CGColorSpaceRef space(CGColorSpaceCreateDeviceRGB());
370 CGRect rect = {CGPointMake(1, 1), [image size]};
371 CGSize size = {rect.size.width + 2, rect.size.height + 2};
2acbe5b8 372
82b4a0bd
JF
373 CGContextRef context(CGBitmapContextCreate(NULL, size.width, size.height, 8, 4 * size.width, space, kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedFirst));
374 CGColorSpaceRelease(space);
2acbe5b8 375
82b4a0bd
JF
376 CGContextDrawImage(context, rect, [image CGImage]);
377 CGImageRef ref(CGBitmapContextCreateImage(context));
378 CGContextRelease(context);
2acbe5b8 379
82b4a0bd
JF
380 UIImage *cache([UIImage imageWithCGImage:ref]);
381 CGImageRelease(ref);
2acbe5b8 382
82b4a0bd
JF
383 return cache;
384}
385
386/*MSHook(id, SBImageCache$initWithName$forImageWidth$imageHeight$initialCapacity$, SBImageCache *self, SEL sel, NSString *name, unsigned width, unsigned height, unsigned capacity) {
387 //if ([name isEqualToString:@"icons"]) return nil;
388 return _SBImageCache$initWithName$forImageWidth$imageHeight$initialCapacity$(self, sel, name, width, height, capacity);
389}*/
390
391MSHook(void, SBIconModel$cacheImageForIcon$, SBIconModel *self, SEL sel, SBIcon *icon) {
392 NSString *key([icon displayIdentifier]);
2acbe5b8 393
82b4a0bd
JF
394 if (UIImage *image = [icon icon]) {
395 CGSize size = [image size];
b0d0c73f 396 if (size.width != 59 || size.height != 60) {
82b4a0bd
JF
397 UIImage *cache($cacheForImage$(image));
398 [Cache_ setObject:cache forKey:key];
399 return;
400 }
2acbe5b8 401 }
82b4a0bd
JF
402
403 _SBIconModel$cacheImageForIcon$(self, sel, icon);
4e0efa01
JF
404}
405
1a4ffdf8
JF
406MSHook(void, SBIconModel$cacheImagesForIcon$, SBIconModel *self, SEL sel, SBIcon *icon) {
407 /* XXX: do I /really/ have to do this? figure out how to cache the small icon! */
408 _SBIconModel$cacheImagesForIcon$(self, sel, icon);
409
410 NSString *key([icon displayIdentifier]);
411
412 if (UIImage *image = [icon icon]) {
413 CGSize size = [image size];
414 if (size.width != 59 || size.height != 60) {
415 UIImage *cache($cacheForImage$(image));
416 [Cache_ setObject:cache forKey:key];
417 return;
418 }
419 }
420}
421
0316ebc4 422MSHook(UIImage *, SBIconModel$getCachedImagedForIcon$, SBIconModel *self, SEL sel, SBIcon *icon) {
4e0efa01
JF
423 NSString *key([icon displayIdentifier]);
424 if (UIImage *image = [Cache_ objectForKey:key])
425 return image;
426 else
0316ebc4 427 return _SBIconModel$getCachedImagedForIcon$(self, sel, icon);
4e0efa01
JF
428}
429
1a4ffdf8
JF
430MSHook(UIImage *, SBIconModel$getCachedImagedForIcon$smallIcon$, SBIconModel *self, SEL sel, SBIcon *icon, BOOL small) {
431 if (small)
432 return _SBIconModel$getCachedImagedForIcon$smallIcon$(self, sel, icon, small);
433 NSString *key([icon displayIdentifier]);
434 if (UIImage *image = [Cache_ objectForKey:key])
435 return image;
436 else
437 return _SBIconModel$getCachedImagedForIcon$smallIcon$(self, sel, icon, small);
438}
439
440MSHook(id, SBSearchView$initWithFrame$, id /* XXX: SBSearchView */ self, SEL sel, struct CGRect frame) {
441 if ((self = _SBSearchView$initWithFrame$(self, sel, frame)) != nil) {
442 [self setBackgroundColor:[UIColor clearColor]];
443 for (UIView *child in [self subviews])
444 [child setBackgroundColor:[UIColor clearColor]];
445 } return self;
446}
447
9c64bab2
JF
448MSHook(id, SBSearchTableViewCell$initWithStyle$reuseIdentifier$, SBSearchTableViewCell *self, SEL sel, int style, NSString *reuse) {
449 if ((self = _SBSearchTableViewCell$initWithStyle$reuseIdentifier$(self, sel, style, reuse)) != nil) {
450 [self setBackgroundColor:[UIColor clearColor]];
451 } return self;
452}
453
454MSHook(void, SBSearchTableViewCell$drawRect$, SBSearchTableViewCell *self, SEL sel, struct CGRect rect, BOOL selected) {
455 _SBSearchTableViewCell$drawRect$(self, sel, rect, selected);
456 float inset([self edgeInset]);
457 [[UIColor clearColor] set];
458 UIRectFill(CGRectMake(0, 0, inset, rect.size.height));
459 UIRectFill(CGRectMake(rect.size.width - inset, 0, inset, rect.size.height));
460}
461
0316ebc4 462MSHook(UIImage *, SBApplicationIcon$icon, SBApplicationIcon *self, SEL sel) {
37351a17 463 if (![Info_ wb$boolForKey:@"ComposeStoreIcons"])
56539693 464 if (NSString *path = $pathForIcon$([self application]))
2acbe5b8 465 return [UIImage imageWithContentsOfFile:path];
0316ebc4 466 return _SBApplicationIcon$icon(self, sel);
394d1eb5
JF
467}
468
1d3b613f
JF
469MSHook(UIImage *, SBApplicationIcon$generateIconImage$, SBApplicationIcon *self, SEL sel, int type) {
470 if (type == 2)
471 if (![Info_ wb$boolForKey:@"ComposeStoreIcons"])
472 if (NSString *path = $pathForIcon$([self application]))
473 if (UIImage *image = [UIImage imageWithContentsOfFile:path])
474 return [image _imageScaledToProportion:1.2 interpolationQuality:5];
475 return _SBApplicationIcon$generateIconImage$(self, sel, type);
476}
477
4f1b7acd 478MSHook(UIImage *, SBWidgetApplicationIcon$icon, SBWidgetApplicationIcon *self, SEL sel) {
d806256e
JF
479 if (Debug_)
480 NSLog(@"WB:Debug:Widget(%@:%@)", [self displayIdentifier], [self displayName]);
481 if (NSString *path = $getTheme$([NSArray arrayWithObject:[NSString stringWithFormat:@"Icons/%@.png", [self displayName]]]))
4f1b7acd 482 return [UIImage imageWithContentsOfFile:path];
d806256e 483 return _SBWidgetApplicationIcon$icon(self, sel);
4f1b7acd
JF
484}
485
0316ebc4 486MSHook(UIImage *, SBBookmarkIcon$icon, SBBookmarkIcon *self, SEL sel) {
502d350e
JF
487 if (Debug_)
488 NSLog(@"WB:Debug:Bookmark(%@:%@)", [self displayIdentifier], [self displayName]);
489 if (NSString *path = $getTheme$([NSArray arrayWithObject:[NSString stringWithFormat:@"Icons/%@.png", [self displayName]]]))
2acbe5b8 490 return [UIImage imageWithContentsOfFile:path];
0316ebc4 491 return _SBBookmarkIcon$icon(self, sel);
502d350e
JF
492}
493
0316ebc4 494MSHook(NSString *, SBApplication$pathForIcon, SBApplication *self, SEL sel) {
d5fb6e01
JF
495 if (NSString *path = $pathForIcon$(self))
496 return path;
0316ebc4 497 return _SBApplication$pathForIcon(self, sel);
d5168fd6
JF
498}
499
95a5777b 500static UIImage *CachedImageAtPath(NSString *path) {
4df9352e 501 path = [path stringByResolvingSymlinksInPath];
95a5777b
JF
502 UIImage *image = [PathImages_ objectForKey:path];
503 if (image != nil)
504 return reinterpret_cast<id>(image) == [NSNull null] ? nil : image;
4df9352e 505 image = [[UIImage alloc] initWithContentsOfFile:path cache:true];
95a5777b
JF
506 if (image != nil)
507 image = [image autorelease];
508 [PathImages_ setObject:(image == nil ? [NSNull null] : reinterpret_cast<id>(image)) forKey:path];
509 return image;
510}
511
4df9352e 512MSHook(CGImageRef, _UIImageRefAtPath, NSString *name, bool cache, UIImageOrientation *orientation) {
889cb4f2 513 if (Debug_)
4df9352e 514 NSLog(@"WB:Debug: _UIImageRefAtPath(\"%@\", %s)", name, cache ? "true" : "false");
44aa4549 515 return __UIImageRefAtPath([name wb$themedPath], cache, orientation);
889cb4f2
JF
516}
517
4df9352e
JF
518/*MSHook(UIImage *, _UIImageAtPath, NSString *name, NSBundle *bundle) {
519 if (bundle == nil)
520 return __UIImageAtPath(name, nil);
521 else {
522 NSString *key = [NSString stringWithFormat:@"B:%@/%@", [bundle bundleIdentifier], name];
523 UIImage *image = [PathImages_ objectForKey:key];
524 if (image != nil)
525 return reinterpret_cast<id>(image) == [NSNull null] ? nil : image;
526 if (Debug_)
527 NSLog(@"WB:Debug: _UIImageAtPath(\"%@\", %@)", name, bundle);
528 if (NSString *path = $pathForFile$inBundle$(name, bundle, false))
529 image = CachedImageAtPath(path);
530 if (image == nil)
531 image = __UIImageAtPath(name, bundle);
532 [PathImages_ setObject:(image == nil ? [NSNull null] : reinterpret_cast<id>(image)) forKey:key];
533 return image;
534 }
535}*/
889cb4f2 536
4df9352e 537MSHook(UIImage *, _UIApplicationImageWithName, NSString *name) {
d5fb6e01
JF
538 NSBundle *bundle = [NSBundle mainBundle];
539 if (Debug_)
4df9352e 540 NSLog(@"WB:Debug: _UIApplicationImageWithName(\"%@\", %@)", name, bundle);
95a5777b
JF
541 if (NSString *path = $pathForFile$inBundle$(name, bundle, false))
542 return CachedImageAtPath(path);
4df9352e 543 return __UIApplicationImageWithName(name);
502d350e
JF
544}
545
502d350e
JF
546#define WBDelegate(delegate) \
547 - (NSMethodSignature*) methodSignatureForSelector:(SEL)sel { \
548 if (Engineer_) \
549 NSLog(@"WB:MS:%s:(%s)", class_getName([self class]), sel_getName(sel)); \
550 if (NSMethodSignature *sig = [delegate methodSignatureForSelector:sel]) \
551 return sig; \
552 NSLog(@"WB:Error: [%s methodSignatureForSelector:(%s)]", class_getName([self class]), sel_getName(sel)); \
553 return nil; \
554 } \
555\
556 - (void) forwardInvocation:(NSInvocation*)inv { \
557 SEL sel = [inv selector]; \
558 if ([delegate respondsToSelector:sel]) \
559 [inv invokeWithTarget:delegate]; \
560 else \
561 NSLog(@"WB:Error: [%s forwardInvocation:(%s)]", class_getName([self class]), sel_getName(sel)); \
562 }
563
0316ebc4 564MSHook(NSString *, NSBundle$pathForResource$ofType$, NSBundle *self, SEL sel, NSString *resource, NSString *type) {
889cb4f2
JF
565 NSString *file = type == nil ? resource : [NSString stringWithFormat:@"%@.%@", resource, type];
566 if (Debug_)
567 NSLog(@"WB:Debug: [NSBundle(%@) pathForResource:\"%@\"]", [self bundleIdentifier], file);
95a5777b 568 if (NSString *path = $pathForFile$inBundle$(file, self, false))
ca13798d 569 return path;
0316ebc4 570 return _NSBundle$pathForResource$ofType$(self, sel, resource, type);
d5168fd6
JF
571}
572
0316ebc4 573void $setBarStyle$_(NSString *name, int &style) {
95a5777b
JF
574 if (Debug_)
575 NSLog(@"WB:Debug:%@Style:%d", name, style);
576 NSNumber *number = nil;
577 if (number == nil)
578 number = [Info_ objectForKey:[NSString stringWithFormat:@"%@Style-%d", name, style]];
579 if (number == nil)
580 number = [Info_ objectForKey:[NSString stringWithFormat:@"%@Style", name]];
0316ebc4 581 if (number != nil) {
95a5777b
JF
582 style = [number intValue];
583 if (Debug_)
584 NSLog(@"WB:Debug:%@Style=%d", name, style);
26c43b47 585 }
26c43b47
JF
586}
587
0316ebc4 588MSHook(void, SBCalendarIconContentsView$drawRect$, SBCalendarIconContentsView *self, SEL sel, CGRect rect) {
4668cd8e
JF
589 NSBundle *bundle([NSBundle mainBundle]);
590
e6f0817b
JF
591 CFLocaleRef locale(CFLocaleCopyCurrent());
592 CFDateFormatterRef formatter(CFDateFormatterCreate(NULL, locale, kCFDateFormatterNoStyle, kCFDateFormatterNoStyle));
593 CFRelease(locale);
594
595 CFDateRef now(CFDateCreate(NULL, CFAbsoluteTimeGetCurrent()));
596
4668cd8e 597 CFDateFormatterSetFormat(formatter, (CFStringRef) [bundle localizedStringForKey:@"CALENDAR_ICON_DAY_NUMBER_FORMAT" value:@"" table:@"SpringBoard"]);
e6f0817b 598 CFStringRef date(CFDateFormatterCreateStringWithDate(NULL, formatter, now));
44aa4549 599 CFDateFormatterSetFormat(formatter, (CFStringRef) [bundle localizedStringForKey:@"CALENDAR_ICON_DAY_NAME_FORMAT" value:@"cccc" table:@"SpringBoard"]);
e6f0817b
JF
600 CFStringRef day(CFDateFormatterCreateStringWithDate(NULL, formatter, now));
601
602 CFRelease(now);
603
604 CFRelease(formatter);
605
c79980f5 606 NSString *datestyle([@""
e6f0817b
JF
607 "font-family: Helvetica; "
608 "font-weight: bold; "
e6f0817b
JF
609 "color: #333333; "
610 "alpha: 1.0; "
1d3b613f 611 "" stringByAppendingString:(IsWild_
c79980f5
JF
612 ? @"font-size: 54px; "
613 : @"font-size: 39px; "
614 )]);
e6f0817b 615
c79980f5 616 NSString *daystyle([@""
e6f0817b
JF
617 "font-family: Helvetica; "
618 "font-weight: bold; "
e6f0817b
JF
619 "color: white; "
620 "text-shadow: rgba(0, 0, 0, 0.2) -1px -1px 2px; "
1d3b613f 621 "" stringByAppendingString:(IsWild_
c79980f5
JF
622 ? @"font-size: 11px; "
623 : @"font-size: 9px; "
624 )]);
e6f0817b
JF
625
626 if (NSString *style = [Info_ objectForKey:@"CalendarIconDateStyle"])
627 datestyle = [datestyle stringByAppendingString:style];
628 if (NSString *style = [Info_ objectForKey:@"CalendarIconDayStyle"])
629 daystyle = [daystyle stringByAppendingString:style];
630
cf31d246 631 float width([self bounds].size.width);
4668cd8e
JF
632 float leeway(10);
633 CGSize datesize = [(NSString *)date sizeWithStyle:datestyle forWidth:(width + leeway)];
634 CGSize daysize = [(NSString *)day sizeWithStyle:daystyle forWidth:(width + leeway)];
e6f0817b 635
1d3b613f 636 unsigned base(IsWild_ ? 89 : 70);
c79980f5
JF
637 if ($GSFontGetUseLegacyFontMetrics())
638 base = base + 1;
81decb42 639
e6f0817b 640 [(NSString *)date drawAtPoint:CGPointMake(
81decb42 641 (width + 1 - datesize.width) / 2, (base - datesize.height) / 2
e6f0817b
JF
642 ) withStyle:datestyle];
643
644 [(NSString *)day drawAtPoint:CGPointMake(
1d3b613f 645 (width + 1 - daysize.width) / 2, ((IsWild_ ? 18 : 16) - daysize.height) / 2
e6f0817b
JF
646 ) withStyle:daystyle];
647
648 CFRelease(date);
649 CFRelease(day);
650}
651
502d350e 652/*static id UINavigationBarBackground$initWithFrame$withBarStyle$withTintColor$(UINavigationBarBackground<WinterBoard> *self, SEL sel, CGRect frame, int style, UIColor *tint) {
69550bfb 653_trace();
26c43b47 654
d5fb6e01
JF
655 if (NSNumber *number = [Info_ objectForKey:@"NavigationBarStyle"])
656 style = [number intValue];
26c43b47 657
37351a17 658 if (UIColor *color = [Info_ wb$colorForKey:@"NavigationBarTint"])
d5fb6e01 659 tint = color;
26c43b47 660
2acbe5b8 661 return [self wb$initWithFrame:frame withBarStyle:style withTintColor:tint];
26c43b47
JF
662}*/
663
502d350e 664/*static id UINavigationBar$initWithCoder$(SBAppWindow<WinterBoard> *self, SEL sel, CGRect frame, NSCoder *coder) {
2acbe5b8 665 self = [self wb$initWithCoder:coder];
26c43b47
JF
666 if (self == nil)
667 return nil;
668 UINavigationBar$setBarStyle$_(self);
669 return self;
670}
671
502d350e 672static id UINavigationBar$initWithFrame$(SBAppWindow<WinterBoard> *self, SEL sel, CGRect frame) {
2acbe5b8 673 self = [self wb$initWithFrame:frame];
26c43b47
JF
674 if (self == nil)
675 return nil;
676 UINavigationBar$setBarStyle$_(self);
677 return self;
678}*/
679
0316ebc4
JF
680MSHook(void, UIToolbar$setBarStyle$, UIToolbar *self, SEL sel, int style) {
681 $setBarStyle$_(@"Toolbar", style);
682 return _UIToolbar$setBarStyle$(self, sel, style);
95a5777b
JF
683}
684
0316ebc4
JF
685MSHook(void, UINavigationBar$setBarStyle$, UINavigationBar *self, SEL sel, int style) {
686 $setBarStyle$_(@"NavigationBar", style);
687 return _UINavigationBar$setBarStyle$(self, sel, style);
26c43b47
JF
688}
689
0316ebc4 690MSHook(void, SBButtonBar$didMoveToSuperview, UIView *self, SEL sel) {
2acbe5b8 691 [[self superview] setBackgroundColor:[UIColor clearColor]];
0316ebc4 692 _SBButtonBar$didMoveToSuperview(self, sel);
08454e3a 693}
62b2dbad 694
0316ebc4
JF
695MSHook(void, SBStatusBarContentsView$didMoveToSuperview, UIView *self, SEL sel) {
696 [[self superview] setBackgroundColor:[UIColor clearColor]];
697 _SBStatusBarContentsView$didMoveToSuperview(self, sel);
698}
699
700MSHook(UIImage *, UIImage$defaultDesktopImage, UIImage *self, SEL sel) {
502d350e
JF
701 if (Debug_)
702 NSLog(@"WB:Debug:DefaultDesktopImage");
703 if (NSString *path = $getTheme$([NSArray arrayWithObjects:@"LockBackground.png", @"LockBackground.jpg", nil]))
2acbe5b8 704 return [UIImage imageWithContentsOfFile:path];
0316ebc4 705 return _UIImage$defaultDesktopImage(self, sel);
502d350e
JF
706}
707
4668cd8e 708static NSArray *Wallpapers_;
1d3b613f 709static bool Papered_;
4668cd8e 710static NSString *WallpaperFile_;
502d350e
JF
711static UIImageView *WallpaperImage_;
712static UIWebDocumentView *WallpaperPage_;
713static NSURL *WallpaperURL_;
95a5777b 714
4668cd8e
JF
715#define _release(object) \
716 do if (object != nil) { \
717 [object release]; \
718 object = nil; \
719 } while (false)
720
69550bfb
JF
721MSHook(id, SBUIController$init, SBUIController *self, SEL sel) {
722 self = _SBUIController$init(self, sel);
d5168fd6
JF
723 if (self == nil)
724 return nil;
725
1d3b613f
JF
726 if (Papered_) {
727 UIWindow *&_wallpaperView(MSHookIvar<UIWindow *>(self, "_wallpaperView"));
728 if (&_wallpaperView != NULL) {
729 [_wallpaperView removeFromSuperview];
730 [_wallpaperView release];
731 _wallpaperView = nil;
732 }
733 }
734
69550bfb 735 UIWindow *&_window(MSHookIvar<UIWindow *>(self, "_window"));
44aa4549 736 UIView *&_contentLayer(MSHookIvar<UIView *>(self, "_contentLayer"));
69550bfb
JF
737 UIView *&_contentView(MSHookIvar<UIView *>(self, "_contentView"));
738
44aa4549
JF
739 UIView *layer;
740 if (&_contentLayer != NULL)
741 layer = _contentLayer;
742 else if (&_contentView != NULL)
743 layer = _contentView;
744 else
745 layer = nil;
746
747 UIView *content([[[UIView alloc] initWithFrame:[layer frame]] autorelease]);
1d3b613f 748
44aa4549
JF
749 [content setBackgroundColor:[layer backgroundColor]];
750 [layer setBackgroundColor:[UIColor clearColor]];
751 [layer setFrame:[content bounds]];
69550bfb
JF
752 [_window setContentView:content];
753
1d3b613f
JF
754 UIView *indirect;
755 if (!SummerBoard_ || !IsWild_)
756 indirect = content;
757 else {
758 CGRect bounds([content bounds]);
c8fbf228 759 bounds.origin.y = -30;
1d3b613f
JF
760 indirect = [[[UIView alloc] initWithFrame:bounds] autorelease];
761 [content addSubview:indirect];
762 [indirect zoomToScale:2.4];
763 }
764
4668cd8e
JF
765 _release(WallpaperFile_);
766 _release(WallpaperImage_);
767 _release(WallpaperPage_);
768 _release(WallpaperURL_);
769
770 if (NSString *theme = $getTheme$(Wallpapers_, true)) {
771 NSString *mp4 = [theme stringByAppendingPathComponent:@"Wallpaper.mp4"];
772 if ([Manager_ fileExistsAtPath:mp4]) {
d806256e
JF
773#if UseAVController
774 NSError *error;
775
776 static AVController *controller_(nil);
777 if (controller_ == nil) {
778 AVQueue *queue([AVQueue avQueue]);
779 controller_ = [[AVController avControllerWithQueue:queue error:&error] retain];
780 }
781
782 AVQueue *queue([controller_ queue]);
783
1d3b613f 784 UIView *video([[[UIView alloc] initWithFrame:[indirect bounds]] autorelease]);
d806256e
JF
785 [controller_ setLayer:[video _layer]];
786
787 AVItem *item([[[AVItem alloc] initWithPath:mp4 error:&error] autorelease]);
788 [queue appendItem:item error:&error];
789
790 [controller_ play:&error];
791#elif UseMPMoviePlayerController
792 NSURL *url([NSURL fileURLWithPath:mp4]);
265e19b2 793 MPMoviePlayerController *controller = [[$MPMoviePlayerController alloc] initWithContentURL:url];
d806256e
JF
794 controller.movieControlMode = MPMovieControlModeHidden;
795 [controller play];
796#else
1d3b613f 797 MPVideoView *video = [[[$MPVideoView alloc] initWithFrame:[indirect bounds]] autorelease];
4668cd8e
JF
798 [video setMovieWithPath:mp4];
799 [video setRepeatMode:1];
d806256e 800 [video setRepeatGap:-1];
4668cd8e 801 [video playFromBeginning];;
d806256e
JF
802#endif
803
1d3b613f 804 [indirect addSubview:video];
4668cd8e 805 }
d5fb6e01 806
4668cd8e
JF
807 NSString *png = [theme stringByAppendingPathComponent:@"Wallpaper.png"];
808 NSString *jpg = [theme stringByAppendingPathComponent:@"Wallpaper.jpg"];
95a5777b 809
4668cd8e
JF
810 NSString *path;
811 if ([Manager_ fileExistsAtPath:png])
812 path = png;
813 else if ([Manager_ fileExistsAtPath:jpg])
814 path = jpg;
815 else path = nil;
d5fb6e01 816
4668cd8e
JF
817 UIImage *image;
818 if (path != nil) {
4df9352e 819 image = [[UIImage alloc] initWithContentsOfFile:path];
4668cd8e
JF
820 if (image != nil)
821 image = [image autorelease];
822 } else image = nil;
394d1eb5 823
4668cd8e
JF
824 if (image != nil) {
825 WallpaperFile_ = [path retain];
826 WallpaperImage_ = [[UIImageView alloc] initWithImage:image];
9c64bab2
JF
827 if (NSNumber *number = [Info_ objectForKey:@"WallpaperAlpha"])
828 [WallpaperImage_ setAlpha:[number floatValue]];
1d3b613f 829 [indirect addSubview:WallpaperImage_];
4668cd8e 830 }
394d1eb5 831
4668cd8e
JF
832 NSString *html = [theme stringByAppendingPathComponent:@"Wallpaper.html"];
833 if ([Manager_ fileExistsAtPath:html]) {
1d3b613f 834 CGRect bounds = [indirect bounds];
502d350e 835
4668cd8e
JF
836 UIWebDocumentView *view([[[UIWebDocumentView alloc] initWithFrame:bounds] autorelease]);
837 [view setAutoresizes:true];
502d350e 838
4668cd8e
JF
839 WallpaperPage_ = [view retain];
840 WallpaperURL_ = [[NSURL fileURLWithPath:html] retain];
394d1eb5 841
4668cd8e
JF
842 [WallpaperPage_ loadRequest:[NSURLRequest requestWithURL:WallpaperURL_]];
843
4668cd8e 844 [view setBackgroundColor:[UIColor clearColor]];
1a4ffdf8
JF
845 if ([view respondsToSelector:@selector(setDrawsBackground:)])
846 [view setDrawsBackground:NO];
847 [[view webView] setDrawsBackground:NO];
394d1eb5 848
1d3b613f 849 [indirect addSubview:view];
4668cd8e 850 }
08454e3a 851 }
d5168fd6 852
5e5c7190
JF
853 for (size_t i(0), e([themes_ count]); i != e; ++i) {
854 NSString *theme = [themes_ objectAtIndex:(e - i - 1)];
855 NSString *html = [theme stringByAppendingPathComponent:@"Widget.html"];
856 if ([Manager_ fileExistsAtPath:html]) {
1d3b613f 857 CGRect bounds = [indirect bounds];
5e5c7190
JF
858
859 UIWebDocumentView *view([[[UIWebDocumentView alloc] initWithFrame:bounds] autorelease]);
860 [view setAutoresizes:true];
861
862 NSURL *url = [NSURL fileURLWithPath:html];
863 [view loadRequest:[NSURLRequest requestWithURL:url]];
864
5e5c7190 865 [view setBackgroundColor:[UIColor clearColor]];
1a4ffdf8
JF
866 if ([view respondsToSelector:@selector(setDrawsBackground:)])
867 [view setDrawsBackground:NO];
868 [[view webView] setDrawsBackground:NO];
5e5c7190 869
1d3b613f 870 [indirect addSubview:view];
5e5c7190
JF
871 }
872 }
873
44aa4549 874 [content addSubview:layer];
69550bfb 875
d5168fd6
JF
876 return self;
877}
878
9cfe3924 879MSHook(void, SBAwayView$updateDesktopImage$, SBAwayView *self, SEL sel, UIImage *image) {
502d350e 880 NSString *path = $getTheme$([NSArray arrayWithObject:@"LockBackground.html"]);
2acbe5b8 881 UIView *&_backgroundView(MSHookIvar<UIView *>(self, "_backgroundView"));
502d350e 882
2acbe5b8
JF
883 if (path != nil && _backgroundView != nil)
884 path = nil;
502d350e 885
9cfe3924 886 _SBAwayView$updateDesktopImage$(self, sel, image);
502d350e
JF
887
888 if (path != nil) {
889 CGRect bounds = [self bounds];
890
2acbe5b8 891 UIWebDocumentView *view([[[UIWebDocumentView alloc] initWithFrame:bounds] autorelease]);
502d350e
JF
892 [view setAutoresizes:true];
893
894 if (WallpaperPage_ != nil)
895 [WallpaperPage_ release];
896 WallpaperPage_ = [view retain];
897
898 if (WallpaperURL_ != nil)
899 [WallpaperURL_ release];
900 WallpaperURL_ = [[NSURL fileURLWithPath:path] retain];
901
902 [WallpaperPage_ loadRequest:[NSURLRequest requestWithURL:WallpaperURL_]];
903
904 [[view webView] setDrawsBackground:false];
2acbe5b8 905 [view setBackgroundColor:[UIColor clearColor]];
502d350e 906
2acbe5b8 907 [self insertSubview:view aboveSubview:_backgroundView];
ca13798d 908 }
502d350e 909}
ca13798d 910
95a5777b 911/*extern "C" CGColorRef CGGStateGetSystemColor(void *);
ca13798d
JF
912extern "C" CGColorRef CGGStateGetFillColor(void *);
913extern "C" CGColorRef CGGStateGetStrokeColor(void *);
95a5777b 914extern "C" NSString *UIStyleStringFromColor(CGColorRef);*/
ca13798d 915
cd6ad593
JF
916/* WBTimeLabel {{{ */
917@interface WBTimeLabel : NSProxy {
ca13798d
JF
918 NSString *time_;
919 _transient SBStatusBarTimeView *view_;
889cb4f2
JF
920}
921
ca13798d 922- (id) initWithTime:(NSString *)time view:(SBStatusBarTimeView *)view;
889cb4f2
JF
923
924@end
925
cd6ad593 926@implementation WBTimeLabel
889cb4f2
JF
927
928- (void) dealloc {
ca13798d 929 [time_ release];
889cb4f2
JF
930 [super dealloc];
931}
932
ca13798d
JF
933- (id) initWithTime:(NSString *)time view:(SBStatusBarTimeView *)view {
934 time_ = [time retain];
935 view_ = view;
889cb4f2
JF
936 return self;
937}
938
44aa4549
JF
939- (NSString *) description {
940 return time_;
941}
942
ca13798d
JF
943WBDelegate(time_)
944
945- (CGSize) drawAtPoint:(CGPoint)point forWidth:(float)width withFont:(UIFont *)font lineBreakMode:(int)mode {
d5fb6e01 946 if (NSString *custom = [Info_ objectForKey:@"TimeStyle"]) {
2acbe5b8 947 BOOL &_mode(MSHookIvar<BOOL>(view_, "_mode"));;
d5fb6e01
JF
948
949 [time_ drawAtPoint:point withStyle:[NSString stringWithFormat:@""
950 "font-family: Helvetica; "
951 "font-weight: bold; "
952 "font-size: 14px; "
953 "color: %@; "
2acbe5b8 954 "%@", _mode ? @"white" : @"black", custom]];
d5fb6e01
JF
955
956 return CGSizeZero;
957 }
ca13798d
JF
958
959 return [time_ drawAtPoint:point forWidth:width withFont:font lineBreakMode:mode];
889cb4f2
JF
960}
961
cd6ad593
JF
962@end
963/* }}} */
964/* WBBadgeLabel {{{ */
965@interface WBBadgeLabel : NSProxy {
966 NSString *badge_;
967}
968
969- (id) initWithBadge:(NSString *)badge;
29155933 970- (NSString *) description;
cd6ad593 971
ca13798d
JF
972@end
973
cd6ad593
JF
974@implementation WBBadgeLabel
975
976- (void) dealloc {
977 [badge_ release];
978 [super dealloc];
889cb4f2
JF
979}
980
cd6ad593
JF
981- (id) initWithBadge:(NSString *)badge {
982 badge_ = [badge retain];
983 return self;
984}
985
29155933
JF
986- (NSString *) description {
987 return [badge_ description];
988}
989
cd6ad593
JF
990WBDelegate(badge_)
991
992- (CGSize) drawAtPoint:(CGPoint)point forWidth:(float)width withFont:(UIFont *)font lineBreakMode:(int)mode {
993 if (NSString *custom = [Info_ objectForKey:@"BadgeStyle"]) {
994 [badge_ drawAtPoint:point withStyle:[NSString stringWithFormat:@""
995 "font-family: Helvetica; "
996 "font-weight: bold; "
997 "font-size: 17px; "
998 "color: white; "
999 "%@", custom]];
1000
1001 return CGSizeZero;
1002 }
1003
1004 return [badge_ drawAtPoint:point forWidth:width withFont:font lineBreakMode:mode];
1005}
ca13798d
JF
1006
1007@end
cd6ad593
JF
1008/* }}} */
1009
b0d0c73f
JF
1010MSHook(void, SBIcon$setAlpha$, SBIcon *self, SEL sel, float alpha) {
1011 if (NSNumber *number = [Info_ objectForKey:@"IconAlpha"])
1012 alpha = [number floatValue];
1013 return _SBIcon$setAlpha$(self, sel, alpha);
1014}
1015
0316ebc4
JF
1016MSHook(id, SBIconBadge$initWithBadge$, SBIconBadge *self, SEL sel, NSString *badge) {
1017 if ((self = _SBIconBadge$initWithBadge$(self, sel, badge)) != nil) {
2acbe5b8
JF
1018 id &_badge(MSHookIvar<id>(self, "_badge"));
1019 if (_badge != nil)
1020 if (id label = [[WBBadgeLabel alloc] initWithBadge:[_badge autorelease]])
1021 _badge = label;
cd6ad593
JF
1022 } return self;
1023}
ca13798d 1024
69550bfb 1025void SBStatusBarController$setStatusBarMode(int &mode) {
95a5777b
JF
1026 if (Debug_)
1027 NSLog(@"WB:Debug:setStatusBarMode:%d", mode);
502d350e 1028 if (mode < 100) // 104:hidden 105:glowing
95a5777b
JF
1029 if (NSNumber *number = [Info_ objectForKey:@"StatusBarMode"])
1030 mode = [number intValue];
d5fb6e01
JF
1031}
1032
69550bfb
JF
1033/*MSHook(void, SBStatusBarController$setStatusBarMode$orientation$duration$animation$, SBStatusBarController *self, SEL sel, int mode, int orientation, double duration, int animation) {
1034 NSLog(@"mode:%d orientation:%d duration:%f animation:%d", mode, orientation, duration, animation);
1035 SBStatusBarController$setStatusBarMode(mode);
1036 return _SBStatusBarController$setStatusBarMode$orientation$duration$animation$(self, sel, mode, orientation, duration, animation);
1037}*/
1038
1039MSHook(void, SBStatusBarController$setStatusBarMode$orientation$duration$fenceID$animation$, SBStatusBarController *self, SEL sel, int mode, int orientation, float duration, int fenceID, int animation) {
8b94f6b0 1040 //NSLog(@"mode:%d orientation:%d duration:%f fenceID:%d animation:%d", mode, orientation, duration, fenceID, animation);
69550bfb
JF
1041 SBStatusBarController$setStatusBarMode(mode);
1042 return _SBStatusBarController$setStatusBarMode$orientation$duration$fenceID$animation$(self, sel, mode, orientation, duration, fenceID, animation);
1043}
1044
1045MSHook(void, SBStatusBarController$setStatusBarMode$orientation$duration$fenceID$animation$startTime$, SBStatusBarController *self, SEL sel, int mode, int orientation, double duration, int fenceID, int animation, double startTime) {
8b94f6b0 1046 //NSLog(@"mode:%d orientation:%d duration:%f fenceID:%d animation:%d startTime:%f", mode, orientation, duration, fenceID, animation, startTime);
69550bfb 1047 SBStatusBarController$setStatusBarMode(mode);
8b94f6b0 1048 //NSLog(@"mode=%u", mode);
69550bfb
JF
1049 return _SBStatusBarController$setStatusBarMode$orientation$duration$fenceID$animation$startTime$(self, sel, mode, orientation, duration, fenceID, animation, startTime);
1050}
1051
1052/*MSHook(id, SBStatusBarContentsView$initWithStatusBar$mode$, SBStatusBarContentsView *self, SEL sel, id bar, int mode) {
d5fb6e01
JF
1053 if (NSNumber *number = [Info_ objectForKey:@"StatusBarContentsMode"])
1054 mode = [number intValue];
0316ebc4 1055 return _SBStatusBarContentsView$initWithStatusBar$mode$(self, sel, bar, mode);
69550bfb 1056}*/
d5fb6e01 1057
0316ebc4
JF
1058MSHook(NSString *, SBStatusBarOperatorNameView$operatorNameStyle, SBStatusBarOperatorNameView *self, SEL sel) {
1059 NSString *style(_SBStatusBarOperatorNameView$operatorNameStyle(self, sel));
4668cd8e
JF
1060 if (Debug_)
1061 NSLog(@"operatorNameStyle= %@", style);
1062 if (NSString *custom = [Info_ objectForKey:@"OperatorNameStyle"])
1063 style = [NSString stringWithFormat:@"%@; %@", style, custom];
1064 return style;
1065}
1066
0316ebc4 1067MSHook(void, SBStatusBarOperatorNameView$setOperatorName$fullSize$, SBStatusBarOperatorNameView *self, SEL sel, NSString *name, BOOL full) {
4668cd8e
JF
1068 if (Debug_)
1069 NSLog(@"setOperatorName:\"%@\" fullSize:%u", name, full);
0316ebc4 1070 return _SBStatusBarOperatorNameView$setOperatorName$fullSize$(self, sel, name, NO);
4668cd8e
JF
1071}
1072
0316ebc4
JF
1073// XXX: replace this with [SBStatusBarTimeView tile]
1074MSHook(void, SBStatusBarTimeView$drawRect$, SBStatusBarTimeView *self, SEL sel, CGRect rect) {
2acbe5b8
JF
1075 id &_time(MSHookIvar<id>(self, "_time"));
1076 if (_time != nil && [_time class] != [WBTimeLabel class])
1077 object_setInstanceVariable(self, "_time", reinterpret_cast<void *>([[WBTimeLabel alloc] initWithTime:[_time autorelease] view:self]));
0316ebc4 1078 return _SBStatusBarTimeView$drawRect$(self, sel, rect);
ca13798d
JF
1079}
1080
98fe8d52 1081@interface UIView (WinterBoard)
a8d008e4 1082- (bool) wb$isWBImageView;
198eb03b 1083- (void) wb$logHierarchy;
98fe8d52
JF
1084@end
1085
1086@implementation UIView (WinterBoard)
1087
a8d008e4 1088- (bool) wb$isWBImageView {
98fe8d52
JF
1089 return false;
1090}
1091
198eb03b
JF
1092- (void) wb$logHierarchy {
1093 WBLogHierarchy(self);
1094}
1095
98fe8d52
JF
1096@end
1097
a8d008e4
JF
1098@interface WBImageView : UIImageView {
1099}
1100
1101- (bool) wb$isWBImageView;
1102- (void) wb$updateFrame;
98fe8d52
JF
1103@end
1104
a8d008e4 1105@implementation WBImageView
98fe8d52 1106
a8d008e4 1107- (bool) wb$isWBImageView {
98fe8d52
JF
1108 return true;
1109}
1110
a8d008e4
JF
1111- (void) wb$updateFrame {
1112 CGRect frame([self frame]);
1113 frame.origin.y = 0;
1114
1115 for (UIView *view(self); ; ) {
1116 view = [view superview];
1117 if (view == nil)
1118 break;
1119 frame.origin.y -= [view frame].origin.y;
1120 }
1121
1122 [self setFrame:frame];
1123}
1124
98fe8d52
JF
1125@end
1126
a8d008e4
JF
1127MSHook(void, SBIconList$setFrame$, SBIconList *self, SEL sel, CGRect frame) {
1128 NSArray *subviews([self subviews]);
1129 WBImageView *view([subviews count] == 0 ? nil : [subviews objectAtIndex:0]);
1130 if (view != nil && [view wb$isWBImageView])
1131 [view wb$updateFrame];
1132 _SBIconList$setFrame$(self, sel, frame);
1133}
1134
98fe8d52
JF
1135MSHook(void, SBIconController$noteNumberOfIconListsChanged, SBIconController *self, SEL sel) {
1136 SBIconModel *&_iconModel(MSHookIvar<SBIconModel *>(self, "_iconModel"));
1137 NSArray *lists([_iconModel iconLists]);
1138
1139 for (unsigned i(0), e([lists count]); i != e; ++i)
1140 if (NSString *path = $getTheme$([NSArray arrayWithObject:[NSString stringWithFormat:@"Page%u.png", i]])) {
1141 SBIconList *list([lists objectAtIndex:i]);
1142 NSArray *subviews([list subviews]);
a8d008e4
JF
1143
1144 WBImageView *view([subviews count] == 0 ? nil : [subviews objectAtIndex:0]);
1145 if (view == nil || ![view wb$isWBImageView]) {
1146 view = [[[WBImageView alloc] init] autorelease];
98fe8d52
JF
1147 [list insertSubview:view atIndex:0];
1148 }
a8d008e4 1149
98fe8d52 1150 UIImage *image([UIImage imageWithContentsOfFile:path]);
1a4ffdf8
JF
1151
1152 CGRect frame([view frame]);
1153 frame.size = [image size];
1154 [view setFrame:frame];
1155
98fe8d52 1156 [view setImage:image];
a8d008e4 1157 [view wb$updateFrame];
98fe8d52
JF
1158 }
1159
1160 return _SBIconController$noteNumberOfIconListsChanged(self, sel);
95a5777b
JF
1161}
1162
e542b0ca
JF
1163MSHook(id, SBIconLabel$initWithSize$label$, SBIconLabel *self, SEL sel, CGSize size, NSString *label) {
1164 self = _SBIconLabel$initWithSize$label$(self, sel, size, label);
cf31d246
JF
1165 if (self != nil)
1166 [self setClipsToBounds:NO];
1167 return self;
1168}
1169
e542b0ca 1170MSHook(void, SBIconLabel$setInDock$, SBIconLabel *self, SEL sel, BOOL docked) {
2acbe5b8 1171 id &_label(MSHookIvar<id>(self, "_label"));
37351a17 1172 if (![Info_ wb$boolForKey:@"UndockedIconLabels"])
95a5777b 1173 docked = true;
2acbe5b8
JF
1174 if (_label != nil && [_label respondsToSelector:@selector(setInDock:)])
1175 [_label setInDock:docked];
e542b0ca 1176 return _SBIconLabel$setInDock$(self, sel, docked);
889cb4f2
JF
1177}
1178
0316ebc4 1179MSHook(NSString *, NSBundle$localizedStringForKey$value$table$, NSBundle *self, SEL sel, NSString *key, NSString *value, NSString *table) {
4fa950df
JF
1180 NSString *identifier = [self bundleIdentifier];
1181 NSLocale *locale = [NSLocale currentLocale];
1182 NSString *language = [locale objectForKey:NSLocaleLanguageCode];
2acbe5b8 1183 if (Debug_)
4fa950df
JF
1184 NSLog(@"WB:Debug:[NSBundle(%@) localizedStringForKey:\"%@\" value:\"%@\" table:\"%@\"] (%@)", identifier, key, value, table, language);
1185 NSString *file = table == nil ? @"Localizable" : table;
1186 NSString *name = [NSString stringWithFormat:@"%@:%@", identifier, file];
1187 NSDictionary *strings;
1188 if ((strings = [Strings_ objectForKey:name]) != nil) {
1189 if (static_cast<id>(strings) != [NSNull null]) strings:
1190 if (NSString *value = [strings objectForKey:key])
1191 return value;
1192 } else if (NSString *path = $pathForFile$inBundle$([NSString stringWithFormat:@"%@.lproj/%@.strings",
1193 language, file
1194 ], self, false)) {
1195 if ((strings = [[NSDictionary alloc] initWithContentsOfFile:path]) != nil) {
1196 [Strings_ setObject:[strings autorelease] forKey:name];
1197 goto strings;
1198 } else goto null;
1199 } else null:
1200 [Strings_ setObject:[NSNull null] forKey:name];
0316ebc4 1201 return _NSBundle$localizedStringForKey$value$table$(self, sel, key, value, table);
4fa950df
JF
1202}
1203
7ff778ee 1204@class WebCoreFrameBridge;
0316ebc4 1205MSHook(CGSize, WebCoreFrameBridge$renderedSizeOfNode$constrainedToWidth$, WebCoreFrameBridge *self, SEL sel, id node, float width) {
7ff778ee
JF
1206 if (node == nil)
1207 return CGSizeZero;
1208 void **core(reinterpret_cast<void **>([node _node]));
1209 if (core == NULL || core[6] == NULL)
1210 return CGSizeZero;
0316ebc4 1211 return _WebCoreFrameBridge$renderedSizeOfNode$constrainedToWidth$(self, sel, node, width);
7ff778ee
JF
1212}
1213
e542b0ca 1214MSHook(void, SBIconLabel$drawRect$, SBIconLabel *self, SEL sel, CGRect rect) {
cf31d246 1215 CGRect bounds = [self bounds];
e6f0817b 1216
a3fa54a5
JF
1217 static Ivar drawMoreLegibly = object_getInstanceVariable(self, "_drawMoreLegibly", NULL);
1218
cf31d246 1219 BOOL docked;
a3fa54a5
JF
1220 Ivar ivar = object_getInstanceVariable(self, "_inDock", reinterpret_cast<void **>(&docked));
1221 docked = (docked & (ivar_getOffset(ivar) == ivar_getOffset(drawMoreLegibly) ? 0x2 : 0x1)) != 0;
cf31d246 1222
6134e674 1223 NSString *label(MSHookIvar<NSString *>(self, "_label"));
cf31d246
JF
1224
1225 NSString *style = [NSString stringWithFormat:@""
1226 "font-family: Helvetica; "
1227 "font-weight: bold; "
ac1be56e 1228 "color: %@; %@"
1d3b613f 1229 "", (docked || !SummerBoard_ ? @"white" : @"#b3b3b3"), (IsWild_
ac1be56e
JF
1230 ? @"font-size: 12px; "
1231 : @"font-size: 11px; "
1232 )];
1233
1d3b613f 1234 if (IsWild_)
ac1be56e
JF
1235 style = [style stringByAppendingString:@"text-shadow: rgba(0, 0, 0, 0.5) 0px 1px 0px; "];
1236 else if (docked)
cf31d246 1237 style = [style stringByAppendingString:@"text-shadow: rgba(0, 0, 0, 0.5) 0px -1px 0px; "];
d806256e
JF
1238
1239 bool ellipsis(false);
1240 float max = 75, width;
1241 width:
1242 width = [(ellipsis ? [label stringByAppendingString:@"..."] : label) sizeWithStyle:style forWidth:320].width;
1243
1244 if (width > max) {
1245 size_t length([label length]);
1246 float spacing((width - max) / (length - 1));
1247
1248 if (spacing > 1.25) {
1249 ellipsis = true;
1250 label = [label substringToIndex:(length - 1)];
1251 goto width;
1252 }
1253
1254 style = [style stringByAppendingString:[NSString stringWithFormat:@"letter-spacing: -%f; ", spacing]];
1255 }
1256
1257 if (ellipsis)
1258 label = [label stringByAppendingString:@"..."];
1259
cf31d246
JF
1260 if (NSString *custom = [Info_ objectForKey:(docked ? @"DockedIconLabelStyle" : @"UndockedIconLabelStyle")])
1261 style = [style stringByAppendingString:custom];
1262
1263 CGSize size = [label sizeWithStyle:style forWidth:bounds.size.width];
1264 [label drawAtPoint:CGPointMake((bounds.size.width - size.width) / 2, 0) withStyle:style];
889cb4f2
JF
1265}
1266
198eb03b
JF
1267MSHook(void, CKMessageCell$addBalloonView$, id self, SEL sel, CKBalloonView *balloon) {
1268 _CKMessageCell$addBalloonView$(self, sel, balloon);
1269 [balloon setBackgroundColor:[UIColor clearColor]];
1270}
1271
1272MSHook(id, CKMessageCell$initWithStyle$reuseIdentifier$, id self, SEL sel, int style, NSString *reuse) {
1273 if ((self = _CKMessageCell$initWithStyle$reuseIdentifier$(self, sel, style, reuse)) != nil) {
1274 [[self contentView] setBackgroundColor:[UIColor clearColor]];
1275 } return self;
1276}
1277
1278MSHook(id, CKTimestampView$initWithStyle$reuseIdentifier$, id self, SEL sel, int style, NSString *reuse) {
1279 if ((self = _CKTimestampView$initWithStyle$reuseIdentifier$(self, sel, style, reuse)) != nil) {
1280 UILabel *&_label(MSHookIvar<UILabel *>(self, "_label"));
1281 [_label setBackgroundColor:[UIColor clearColor]];
1282 } return self;
1283}
1284
1285MSHook(void, CKTranscriptTableView$setSeparatorStyle$, id self, SEL sel, int style) {
1286 _CKTranscriptTableView$setSeparatorStyle$(self, sel, UITableViewCellSeparatorStyleNone);
1287}
1288
1289MSHook(id, CKTranscriptTableView$initWithFrame$style$, id self, SEL sel, CGRect frame, int style) {
1290 _trace();
1291 if ((self = _CKTranscriptTableView$initWithFrame$style$(self, sel, frame, style)) != nil) {
1292 [self setSeparatorStyle:UITableViewCellSeparatorStyleNone];
1293 } return self;
1294}
1295
8b94f6b0
JF
1296MSHook(void, TranscriptController$loadView, mSMSMessageTranscriptController *self, SEL sel) {
1297 _TranscriptController$loadView(self, sel);
e542b0ca 1298
4668cd8e 1299 if (NSString *path = $getTheme$([NSArray arrayWithObjects:@"SMSBackground.png", @"SMSBackground.jpg", nil]))
4df9352e 1300 if (UIImage *image = [[UIImage alloc] initWithContentsOfFile:path]) {
4668cd8e 1301 [image autorelease];
8b94f6b0
JF
1302
1303 UIView *&_transcriptTable(MSHookIvar<UIView *>(self, "_transcriptTable"));
4668cd8e 1304 UIView *&_transcriptLayer(MSHookIvar<UIView *>(self, "_transcriptLayer"));
8b94f6b0
JF
1305 UIView *table;
1306 if (&_transcriptTable != NULL)
1307 table = _transcriptTable;
1308 else if (&_transcriptLayer != NULL)
1309 table = _transcriptLayer;
1310 else
1311 table = nil;
1312
1313 UIView *placard(table != nil ? [table superview] : MSHookIvar<UIView *>(self, "_backPlacard"));
4668cd8e 1314 UIImageView *background([[[UIImageView alloc] initWithImage:image] autorelease]);
8b94f6b0
JF
1315
1316 if (table == nil)
1317 [placard insertSubview:background atIndex:0];
1318 else {
1319 [table setBackgroundColor:[UIColor clearColor]];
1320 [placard insertSubview:background belowSubview:table];
1321 }
4668cd8e
JF
1322 }
1323}
1324
4df9352e 1325MSHook(UIImage *, _UIImageWithName, NSString *name) {
95a5777b
JF
1326 int id(_UISharedImageNameGetIdentifier(name));
1327 if (Debug_)
9c64bab2 1328 NSLog(@"WB:Debug: _UIImageWithName(\"%@\": %d)", name, id);
95a5777b
JF
1329
1330 if (id == -1)
1331 return _UIImageAtPath(name, _UIKitBundle());
1332 else {
1333 NSNumber *key([NSNumber numberWithInt:id]);
1334 UIImage *image = [UIImages_ objectForKey:key];
1335 if (image != nil)
96442a01 1336 return reinterpret_cast<id>(image) == [NSNull null] ? _UISharedImageWithIdentifier(id) : image;
95a5777b 1337 if (NSString *path = $pathForFile$inBundle$(name, _UIKitBundle(), true)) {
4f1b7acd 1338 image = [[UIImage alloc] initWithContentsOfFile:path cache:true];
95a5777b
JF
1339 if (image != nil)
1340 [image autorelease];
1341 }
95a5777b 1342 [UIImages_ setObject:(image == nil ? [NSNull null] : reinterpret_cast<id>(image)) forKey:key];
96442a01 1343 return image == nil ? _UISharedImageWithIdentifier(id) : image;
95a5777b
JF
1344 }
1345}
1346
4df9352e 1347MSHook(UIImage *, _UIImageWithNameInDomain, NSString *name, NSString *domain) {
96442a01
JF
1348 NSString *key([NSString stringWithFormat:@"D:%zu%@%@", [domain length], domain, name]);
1349 UIImage *image([PathImages_ objectForKey:key]);
e6f0817b 1350 if (image != nil)
96442a01 1351 return reinterpret_cast<id>(image) == [NSNull null] ? __UIImageWithNameInDomain(name, domain) : image;
95a5777b 1352 if (Debug_)
e6f0817b
JF
1353 NSLog(@"WB:Debug: UIImageWithNameInDomain(\"%@\", \"%@\")", name, domain);
1354 if (NSString *path = $getTheme$([NSArray arrayWithObject:[NSString stringWithFormat:@"Domains/%@/%@", domain, name]])) {
4df9352e 1355 image = [[UIImage alloc] initWithContentsOfFile:path];
e6f0817b
JF
1356 if (image != nil)
1357 [image autorelease];
95a5777b 1358 }
e6f0817b 1359 [PathImages_ setObject:(image == nil ? [NSNull null] : reinterpret_cast<id>(image)) forKey:key];
96442a01 1360 return image == nil ? __UIImageWithNameInDomain(name, domain) : image;
95a5777b
JF
1361}
1362
d806256e 1363MSHook(GSFontRef, GSFontCreateWithName, const char *name, GSFontSymbolicTraits traits, float size) {
9c64bab2
JF
1364 if (Debug_)
1365 NSLog(@"WB:Debug: GSFontCreateWithName(\"%s\", %f)", name, size);
d806256e
JF
1366 if (NSString *font = [Info_ objectForKey:[NSString stringWithFormat:@"FontName-%s", name]])
1367 name = [font UTF8String];
1368 return _GSFontCreateWithName(name, traits, size);
1369}
1370
95a5777b
JF
1371#define AudioToolbox "/System/Library/Frameworks/AudioToolbox.framework/AudioToolbox"
1372#define UIKit "/System/Library/Frameworks/UIKit.framework/UIKit"
1373
e542b0ca 1374bool (*_Z24GetFileNameForThisActionmPcRb)(unsigned long a0, char *a1, bool &a2);
95a5777b 1375
e542b0ca 1376MSHook(bool, _Z24GetFileNameForThisActionmPcRb, unsigned long a0, char *a1, bool &a2) {
9c64bab2
JF
1377 if (Debug_)
1378 NSLog(@"WB:Debug:GetFileNameForThisAction(%u, %p, %u)", a0, a1, a2);
e542b0ca 1379 bool value = __Z24GetFileNameForThisActionmPcRb(a0, a1, a2);
95a5777b
JF
1380 if (Debug_)
1381 NSLog(@"WB:Debug:GetFileNameForThisAction(%u, %s, %u) = %u", a0, value ? a1 : NULL, a2, value);
1382
1383 if (value) {
1384 NSString *path = [NSString stringWithUTF8String:a1];
1385 if ([path hasPrefix:@"/System/Library/Audio/UISounds/"]) {
1386 NSString *file = [path substringFromIndex:31];
95a5777b
JF
1387 for (NSString *theme in themes_) {
1388 NSString *path([NSString stringWithFormat:@"%@/UISounds/%@", theme, file]);
1389 if ([Manager_ fileExistsAtPath:path]) {
1390 strcpy(a1, [path UTF8String]);
1391 continue;
1392 }
1393 }
1394 }
1395 }
1396 return value;
1397}
1398
1399static void ChangeWallpaper(
1400 CFNotificationCenterRef center,
1401 void *observer,
1402 CFStringRef name,
1403 const void *object,
1404 CFDictionaryRef info
1405) {
1406 if (Debug_)
1407 NSLog(@"WB:Debug:ChangeWallpaper!");
502d350e 1408
95a5777b 1409 UIImage *image;
4668cd8e 1410 if (WallpaperFile_ != nil) {
4df9352e 1411 image = [[UIImage alloc] initWithContentsOfFile:WallpaperFile_];
95a5777b
JF
1412 if (image != nil)
1413 image = [image autorelease];
1414 } else image = nil;
502d350e
JF
1415
1416 if (WallpaperImage_ != nil)
1417 [WallpaperImage_ setImage:image];
1418 if (WallpaperPage_ != nil)
1419 [WallpaperPage_ loadRequest:[NSURLRequest requestWithURL:WallpaperURL_]];
1420
95a5777b 1421}
394d1eb5 1422
0316ebc4
JF
1423#define WBRename(name, sel, imp) \
1424 _ ## name ## $ ## imp = MSHookMessage($ ## name, @selector(sel), &$ ## name ## $ ## imp)
e6f0817b 1425
9c64bab2
JF
1426template <typename Type_>
1427static void nlset(Type_ &function, struct nlist *nl, size_t index) {
1428 struct nlist &name(nl[index]);
1429 uintptr_t value(name.n_value);
1430 if ((name.n_desc & N_ARM_THUMB_DEF) != 0)
1431 value |= 0x00000001;
1432 function = reinterpret_cast<Type_>(value);
1433}
1434
1435template <typename Type_>
1436static void dlset(Type_ &function, const char *name) {
8b94f6b0 1437 function = reinterpret_cast<Type_>(dlsym(RTLD_DEFAULT, name));
9c64bab2
JF
1438}
1439
d5168fd6 1440extern "C" void WBInitialize() {
265e19b2 1441 NSAutoreleasePool *pool([[NSAutoreleasePool alloc] init]);
d5168fd6 1442
2acbe5b8 1443 NSString *identifier([[NSBundle mainBundle] bundleIdentifier]);
394d1eb5 1444
2acbe5b8 1445 NSLog(@"WB:Notice: WinterBoard");
95a5777b 1446
9c64bab2 1447 dlset(_GSFontGetUseLegacyFontMetrics, "GSFontGetUseLegacyFontMetrics");
81decb42 1448
69550bfb 1449 struct nlist nl[8];
394d1eb5 1450
9c64bab2 1451 memset(nl, 0, sizeof(nl));
4df9352e
JF
1452 nl[0].n_un.n_name = (char *) "__UIApplicationImageWithName";
1453 nl[1].n_un.n_name = (char *) "__UIImageAtPath";
1454 nl[2].n_un.n_name = (char *) "__UIImageRefAtPath";
1455 nl[3].n_un.n_name = (char *) "__UIImageWithNameInDomain";
1456 nl[4].n_un.n_name = (char *) "__UIKitBundle";
69550bfb
JF
1457 nl[5].n_un.n_name = (char *) "__UISharedImageNameGetIdentifier";
1458 nl[6].n_un.n_name = (char *) "__UISharedImageWithIdentifier";
95a5777b
JF
1459 nlist(UIKit, nl);
1460
9c64bab2
JF
1461 nlset(_UIApplicationImageWithName, nl, 0);
1462 nlset(_UIImageAtPath, nl, 1);
1463 nlset(_UIImageRefAtPath, nl, 2);
1464 nlset(_UIImageWithNameInDomain, nl, 3);
1465 nlset(_UIKitBundle, nl, 4);
1466 nlset(_UISharedImageNameGetIdentifier, nl, 5);
1467 nlset(_UISharedImageWithIdentifier, nl, 6);
95a5777b 1468
4df9352e
JF
1469 MSHookFunction(_UIApplicationImageWithName, &$_UIApplicationImageWithName, &__UIApplicationImageWithName);
1470 MSHookFunction(_UIImageRefAtPath, &$_UIImageRefAtPath, &__UIImageRefAtPath);
1471 MSHookFunction(_UIImageWithName, &$_UIImageWithName, &__UIImageWithName);
1472 MSHookFunction(_UIImageWithNameInDomain, &$_UIImageWithNameInDomain, &__UIImageWithNameInDomain);
95a5777b 1473
d806256e
JF
1474 MSHookFunction(&GSFontCreateWithName, &$GSFontCreateWithName, &_GSFontCreateWithName);
1475
95a5777b
JF
1476 if (dlopen(AudioToolbox, RTLD_LAZY | RTLD_NOLOAD) != NULL) {
1477 struct nlist nl[2];
1478 memset(nl, 0, sizeof(nl));
1479 nl[0].n_un.n_name = (char *) "__Z24GetFileNameForThisActionmPcRb";
1480 nlist(AudioToolbox, nl);
9c64bab2 1481 nlset(_Z24GetFileNameForThisActionmPcRb, nl, 0);
e542b0ca 1482 MSHookFunction(_Z24GetFileNameForThisActionmPcRb, &$_Z24GetFileNameForThisActionmPcRb, &__Z24GetFileNameForThisActionmPcRb);
95a5777b 1483 }
2435118f 1484
0316ebc4
JF
1485 $NSBundle = objc_getClass("NSBundle");
1486
1487 _NSBundle$localizedStringForKey$value$table$ = MSHookMessage($NSBundle, @selector(localizedStringForKey:value:table:), &$NSBundle$localizedStringForKey$value$table$);
1488 _NSBundle$pathForResource$ofType$ = MSHookMessage($NSBundle, @selector(pathForResource:ofType:), &$NSBundle$pathForResource$ofType$);
1489
1490 $UIImage = objc_getClass("UIImage");
1491 $UINavigationBar = objc_getClass("UINavigationBar");
1492 $UIToolbar = objc_getClass("UIToolbar");
1493
1494 _UIImage$defaultDesktopImage = MSHookMessage(object_getClass($UIImage), @selector(defaultDesktopImage), &$UIImage$defaultDesktopImage);
e6f0817b 1495
265e19b2
JF
1496 //WBRename("UINavigationBar", @selector(initWithCoder:), (IMP) &UINavigationBar$initWithCoder$);
1497 //WBRename("UINavigationBarBackground", @selector(initWithFrame:withBarStyle:withTintColor:), (IMP) &UINavigationBarBackground$initWithFrame$withBarStyle$withTintColor$);
e6f0817b 1498
0316ebc4
JF
1499 _UINavigationBar$setBarStyle$ = MSHookMessage($UINavigationBar, @selector(setBarStyle:), &$UINavigationBar$setBarStyle$);
1500 _UIToolbar$setBarStyle$ = MSHookMessage($UIToolbar, @selector(setBarStyle:), &$UIToolbar$setBarStyle$);
e6f0817b 1501
502d350e
JF
1502 Manager_ = [[NSFileManager defaultManager] retain];
1503 UIImages_ = [[NSMutableDictionary alloc] initWithCapacity:16];
1504 PathImages_ = [[NSMutableDictionary alloc] initWithCapacity:16];
4fa950df 1505 Strings_ = [[NSMutableDictionary alloc] initWithCapacity:0];
4df9352e 1506 Bundles_ = [[NSMutableDictionary alloc] initWithCapacity:2];
e839c3fe 1507 Themed_ = [[NSMutableDictionary alloc] initWithCapacity:128];
502d350e 1508
d5fb6e01 1509 themes_ = [[NSMutableArray alloc] initWithCapacity:8];
26c43b47 1510
265e19b2
JF
1511 if (NSDictionary *settings = [NSDictionary dictionaryWithContentsOfFile:[NSString stringWithFormat:@"/User/Library/Preferences/com.saurik.WinterBoard.plist"]]) {
1512 if (NSNumber *value = [settings objectForKey:@"SummerBoard"])
1513 SummerBoard_ = [value boolValue];
1514 if (NSNumber *value = [settings objectForKey:@"Debug"])
1515 Debug_ = [value boolValue];
62b2dbad 1516
265e19b2 1517 NSArray *themes([settings objectForKey:@"Themes"]);
d5fb6e01
JF
1518 if (themes == nil)
1519 if (NSString *theme = [settings objectForKey:@"Theme"])
1520 themes = [NSArray arrayWithObject:[NSDictionary dictionaryWithObjectsAndKeys:
1521 theme, @"Name",
95a5777b 1522 [NSNumber numberWithBool:true], @"Active",
d5fb6e01 1523 nil]];
265e19b2 1524
d5fb6e01
JF
1525 if (themes != nil)
1526 for (NSDictionary *theme in themes) {
265e19b2 1527 NSNumber *active([theme objectForKey:@"Active"]);
d5fb6e01
JF
1528 if (![active boolValue])
1529 continue;
26c43b47 1530
265e19b2 1531 NSString *name([theme objectForKey:@"Name"]);
d5fb6e01
JF
1532 if (name == nil)
1533 continue;
1534
265e19b2 1535 NSString *theme(nil);
d5fb6e01 1536
95a5777b
JF
1537 #define testForTheme(format...) \
1538 if (theme == nil) { \
1539 NSString *path = [NSString stringWithFormat:format]; \
1540 if ([Manager_ fileExistsAtPath:path]) { \
1541 [themes_ addObject:path]; \
1542 continue; \
1543 } \
d5fb6e01 1544 }
d5fb6e01 1545
95a5777b
JF
1546 testForTheme(@"/Library/Themes/%@.theme", name)
1547 testForTheme(@"/Library/Themes/%@", name)
1548 testForTheme(@"%@/Library/SummerBoard/Themes/%@", NSHomeDirectory(), name)
265e19b2 1549
d5fb6e01 1550 }
26c43b47
JF
1551 }
1552
d5fb6e01
JF
1553 Info_ = [[NSMutableDictionary dictionaryWithCapacity:16] retain];
1554
95a5777b 1555 for (NSString *theme in themes_)
265e19b2 1556 if (NSDictionary *info = [NSDictionary dictionaryWithContentsOfFile:[NSString stringWithFormat:@"%@/Info.plist", theme]])
d5fb6e01
JF
1557 for (NSString *key in [info allKeys])
1558 if ([Info_ objectForKey:key] == nil)
1559 [Info_ setObject:[info objectForKey:key] forKey:key];
95a5777b 1560
198eb03b
JF
1561 bool sms($getTheme$([NSArray arrayWithObjects:@"SMSBackground.png", @"SMSBackground.jpg", nil]) != nil);
1562
1563 if ([NSBundle bundleWithIdentifier:@"com.apple.chatkit"])
1564 if (sms) {
1565 $CKMessageCell = objc_getClass("CKMessageCell");
1566 _CKMessageCell$addBalloonView$ = MSHookMessage($CKMessageCell, @selector(addBalloonView:), &$CKMessageCell$addBalloonView$);
1567 _CKMessageCell$initWithStyle$reuseIdentifier$ = MSHookMessage($CKMessageCell, @selector(initWithStyle:reuseIdentifier:), &$CKMessageCell$initWithStyle$reuseIdentifier$);
1568
1569 $CKTranscriptTableView = objc_getClass("CKTranscriptTableView");
1570 _CKTranscriptTableView$setSeparatorStyle$ = MSHookMessage($CKTranscriptTableView, @selector(setSeparatorStyle:), &$CKTranscriptTableView$setSeparatorStyle$);
1571 _CKTranscriptTableView$initWithFrame$style$ = MSHookMessage($CKTranscriptTableView, @selector(initWithFrame:style:), &$CKTranscriptTableView$initWithFrame$style$);
1572
1573 $CKTimestampView = objc_getClass("CKTimestampView");
1574 _CKTimestampView$initWithStyle$reuseIdentifier$ = MSHookMessage($CKTimestampView, @selector(initWithStyle:reuseIdentifier:), &$CKTimestampView$initWithStyle$reuseIdentifier$);
1575
1576 $CKTranscriptController = objc_getClass("CKTranscriptController");
1577 _TranscriptController$loadView = MSHookMessage($CKTranscriptController, @selector(loadView), &$TranscriptController$loadView);
1578 }
8b94f6b0 1579
4668cd8e 1580 if ([identifier isEqualToString:@"com.apple.MobileSMS"]) {
198eb03b
JF
1581 if (sms) {
1582 if (_TranscriptController$loadView == NULL) {
1583 Class mSMSMessageTranscriptController = objc_getClass("mSMSMessageTranscriptController");
1584 _TranscriptController$loadView = MSHookMessage(mSMSMessageTranscriptController, @selector(loadView), &$TranscriptController$loadView);
1585 }
8b94f6b0 1586 }
4668cd8e 1587 } else if ([identifier isEqualToString:@"com.apple.springboard"]) {
95a5777b
JF
1588 CFNotificationCenterAddObserver(
1589 CFNotificationCenterGetDarwinNotifyCenter(),
1590 NULL, &ChangeWallpaper, (CFStringRef) @"com.saurik.winterboard.lockbackground", NULL, 0
1591 );
62b2dbad 1592
cc46954c 1593 if ($getTheme$([NSArray arrayWithObject:@"Wallpaper.mp4"]) != nil) {
265e19b2
JF
1594 NSBundle *MediaPlayer([NSBundle bundleWithPath:@"/System/Library/Frameworks/MediaPlayer.framework"]);
1595 if (MediaPlayer != nil)
1596 [MediaPlayer load];
1597
1598 $MPMoviePlayerController = objc_getClass("MPMoviePlayerController");
1599 $MPVideoView = objc_getClass("MPVideoView");
cc46954c 1600 }
2acbe5b8 1601
0316ebc4
JF
1602 $WebCoreFrameBridge = objc_getClass("WebCoreFrameBridge");
1603
1604 $SBApplication = objc_getClass("SBApplication");
1605 $SBApplicationIcon = objc_getClass("SBApplicationIcon");
9cfe3924 1606 $SBAwayView = objc_getClass("SBAwayView");
0316ebc4
JF
1607 $SBBookmarkIcon = objc_getClass("SBBookmarkIcon");
1608 $SBButtonBar = objc_getClass("SBButtonBar");
1609 $SBCalendarIconContentsView = objc_getClass("SBCalendarIconContentsView");
b0d0c73f 1610 $SBIcon = objc_getClass("SBIcon");
0316ebc4
JF
1611 $SBIconBadge = objc_getClass("SBIconBadge");
1612 $SBIconController = objc_getClass("SBIconController");
1613 $SBIconLabel = objc_getClass("SBIconLabel");
a8d008e4 1614 $SBIconList = objc_getClass("SBIconList");
0316ebc4 1615 $SBIconModel = objc_getClass("SBIconModel");
82b4a0bd 1616 //$SBImageCache = objc_getClass("SBImageCache");
1a4ffdf8 1617 $SBSearchView = objc_getClass("SBSearchView");
9c64bab2 1618 $SBSearchTableViewCell = objc_getClass("SBSearchTableViewCell");
0316ebc4
JF
1619 $SBStatusBarContentsView = objc_getClass("SBStatusBarContentsView");
1620 $SBStatusBarController = objc_getClass("SBStatusBarController");
1621 $SBStatusBarOperatorNameView = objc_getClass("SBStatusBarOperatorNameView");
1622 $SBStatusBarTimeView = objc_getClass("SBStatusBarTimeView");
69550bfb 1623 $SBUIController = objc_getClass("SBUIController");
4f1b7acd 1624 $SBWidgetApplicationIcon = objc_getClass("SBWidgetApplicationIcon");
0316ebc4
JF
1625
1626 WBRename(WebCoreFrameBridge, renderedSizeOfNode:constrainedToWidth:, renderedSizeOfNode$constrainedToWidth$);
1627
265e19b2
JF
1628 if (SummerBoard_) {
1629 WBRename(SBApplication, pathForIcon, pathForIcon);
1630 WBRename(SBApplicationIcon, icon, icon);
1d3b613f 1631 WBRename(SBApplicationIcon, generateIconImage:, generateIconImage$);
265e19b2
JF
1632 }
1633
0316ebc4
JF
1634 WBRename(SBBookmarkIcon, icon, icon);
1635 WBRename(SBButtonBar, didMoveToSuperview, didMoveToSuperview);
1636 WBRename(SBCalendarIconContentsView, drawRect:, drawRect$);
b0d0c73f 1637 WBRename(SBIcon, setAlpha:, setAlpha$);
0316ebc4 1638 WBRename(SBIconBadge, initWithBadge:, initWithBadge$);
98fe8d52 1639 WBRename(SBIconController, noteNumberOfIconListsChanged, noteNumberOfIconListsChanged);
69550bfb 1640 WBRename(SBUIController, init, init);
4f1b7acd 1641 WBRename(SBWidgetApplicationIcon, icon, icon);
0316ebc4
JF
1642
1643 WBRename(SBIconLabel, drawRect:, drawRect$);
1644 WBRename(SBIconLabel, initWithSize:label:, initWithSize$label$);
1645 WBRename(SBIconLabel, setInDock:, setInDock$);
1646
a8d008e4
JF
1647 WBRename(SBIconList, setFrame:, setFrame$);
1648
0316ebc4 1649 WBRename(SBIconModel, cacheImageForIcon:, cacheImageForIcon$);
1a4ffdf8 1650 WBRename(SBIconModel, cacheImagesForIcon:, cacheImagesForIcon$);
0316ebc4 1651 WBRename(SBIconModel, getCachedImagedForIcon:, getCachedImagedForIcon$);
1a4ffdf8
JF
1652 WBRename(SBIconModel, getCachedImagedForIcon:smallIcon:, getCachedImagedForIcon$smallIcon$);
1653
1654 WBRename(SBSearchView, initWithFrame:, initWithFrame$);
9c64bab2
JF
1655 WBRename(SBSearchTableViewCell, drawRect:, drawRect$);
1656 WBRename(SBSearchTableViewCell, initWithStyle:reuseIdentifier:, initWithStyle$reuseIdentifier$);
0316ebc4 1657
82b4a0bd
JF
1658 //WBRename(SBImageCache, initWithName:forImageWidth:imageHeight:initialCapacity:, initWithName$forImageWidth$imageHeight$initialCapacity$);
1659
9cfe3924 1660 WBRename(SBAwayView, updateDesktopImage:, updateDesktopImage$);
0316ebc4 1661 WBRename(SBStatusBarContentsView, didMoveToSuperview, didMoveToSuperview);
69550bfb
JF
1662 //WBRename(SBStatusBarContentsView, initWithStatusBar:mode:, initWithStatusBar$mode$);
1663 //WBRename(SBStatusBarController, setStatusBarMode:orientation:duration:animation:, setStatusBarMode$orientation$duration$animation$);
0316ebc4 1664 WBRename(SBStatusBarController, setStatusBarMode:orientation:duration:fenceID:animation:, setStatusBarMode$orientation$duration$fenceID$animation$);
69550bfb 1665 WBRename(SBStatusBarController, setStatusBarMode:orientation:duration:fenceID:animation:startTime:, setStatusBarMode$orientation$duration$fenceID$animation$startTime$);
0316ebc4
JF
1666 WBRename(SBStatusBarOperatorNameView, operatorNameStyle, operatorNameStyle);
1667 WBRename(SBStatusBarOperatorNameView, setOperatorName:fullSize:, setOperatorName$fullSize$);
1668 WBRename(SBStatusBarTimeView, drawRect:, drawRect$);
2acbe5b8 1669
24ba43ee
JF
1670 if (SummerBoard_)
1671 English_ = [[NSDictionary alloc] initWithContentsOfFile:@"/System/Library/CoreServices/SpringBoard.app/English.lproj/LocalizedApplicationNames.strings"];
1672
2acbe5b8
JF
1673 Cache_ = [[NSMutableDictionary alloc] initWithCapacity:64];
1674 }
1675
4668cd8e 1676 Wallpapers_ = [[NSArray arrayWithObjects:@"Wallpaper.mp4", @"Wallpaper.png", @"Wallpaper.jpg", @"Wallpaper.html", nil] retain];
1d3b613f
JF
1677 Papered_ = $getTheme$(Wallpapers_) != nil;
1678
1679 UIDevice *device([UIDevice currentDevice]);
1680 IsWild_ = [device respondsToSelector:@selector(isWildcat)] && [device isWildcat];
4668cd8e 1681
f22a8989
JF
1682 if ([Info_ objectForKey:@"UndockedIconLabels"] == nil)
1683 [Info_ setObject:[NSNumber numberWithBool:(
1d3b613f 1684 Papered_ ||
f22a8989
JF
1685 [Info_ objectForKey:@"DockedIconLabelStyle"] != nil ||
1686 [Info_ objectForKey:@"UndockedIconLabelStyle"] != nil
1687 )] forKey:@"UndockedIconLabels"];
1688
d5fb6e01
JF
1689 if (Debug_)
1690 NSLog(@"WB:Debug:Info = %@", [Info_ description]);
1691
d5168fd6
JF
1692 [pool release];
1693}