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