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