1 /* Cydia Substrate - Powerful Code Insertion Platform
2 * Copyright (C) 2008-2010 Jay Freeman (saurik)
5 /* GNU Lesser General Public License, Version 3 {{{ */
7 * Substrate is free software: you can redistribute it and/or modify it under
8 * the terms of the GNU Lesser General Public License as published by the
9 * Free Software Foundation, either version 3 of the License, or (at your
10 * option) any later version.
12 * Substrate is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
15 * License for more details.
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with Substrate. If not, see <http://www.gnu.org/licenses/>.
22 #import <CoreFoundation/CoreFoundation.h>
23 #import <Foundation/Foundation.h>
24 #import <CoreGraphics/CGGeometry.h>
25 #import <UIKit/UIKit.h>
27 #include "CydiaSubstrate.h"
31 @class SBUIController;
33 Class $SafeModeAlertItem;
34 Class $SBAlertItemsController;
36 @interface SBAlertItem : NSObject {
38 - (UIAlertView *) alertSheet;
42 @interface SBAlertItemsController : NSObject {
44 + (SBAlertItemsController *) sharedInstance;
45 - (void) activateAlertItem:(SBAlertItem *)item;
48 @interface SBStatusBarTimeView : UIView {
53 @interface UIApplication (CydiaSubstrate)
54 - (void) applicationOpenURL:(id)url;
57 @interface UIAlertView (CydiaSubstrate)
58 - (void) setForceHorizontalButtonsLayout:(BOOL)force;
59 - (void) setBodyText:(NSString *)body;
60 - (void) setNumberOfRows:(NSInteger)rows;
63 void SafeModeAlertItem$alertSheet$buttonClicked$(id self, SEL sel, id sheet, int button) {
73 [[UIApplication sharedApplication] applicationOpenURL:[NSURL URLWithString:@"http://cydia.saurik.com/safemode/"]];
80 void SafeModeAlertItem$configure$requirePasscodeForActions$(id self, SEL sel, BOOL configure, BOOL require) {
81 UIAlertView *sheet([self alertSheet]);
82 [sheet setDelegate:self];
83 [sheet setBodyText:@"We apologize for the inconvenience, but SpringBoard has just crashed.\n\nMobileSubstrate /did not/ cause this problem: it has protected you from it.\n\nYour device is now running in Safe Mode. All extensions that support this safety system are disabled.\n\nReboot (or restart SpringBoard) to return to the normal mode. To return to this dialog touch the status bar."];
84 [sheet addButtonWithTitle:@"OK"];
85 [sheet addButtonWithTitle:@"Restart"];
86 [sheet addButtonWithTitle:@"Help"];
87 [sheet setNumberOfRows:1];
88 if ([sheet respondsToSelector:@selector(setForceHorizontalButtonsLayout:)])
89 [sheet setForceHorizontalButtonsLayout:YES];
92 void SafeModeAlertItem$performUnlockAction(id self, SEL sel) {
93 [[$SBAlertItemsController sharedInstance] activateAlertItem:self];
96 static void MSAlert() {
97 if ($SafeModeAlertItem == nil)
98 $SafeModeAlertItem = objc_lookUpClass("SafeModeAlertItem");
99 if ($SafeModeAlertItem == nil) {
100 $SafeModeAlertItem = objc_allocateClassPair(objc_getClass("SBAlertItem"), "SafeModeAlertItem", 0);
101 if ($SafeModeAlertItem == nil)
104 class_addMethod($SafeModeAlertItem, @selector(alertSheet:buttonClicked:), (IMP) &SafeModeAlertItem$alertSheet$buttonClicked$, "v@:@i");
105 class_addMethod($SafeModeAlertItem, @selector(configure:requirePasscodeForActions:), (IMP) &SafeModeAlertItem$configure$requirePasscodeForActions$, "v@:cc");
106 class_addMethod($SafeModeAlertItem, @selector(performUnlockAction), (IMP) SafeModeAlertItem$performUnlockAction, "v@:");
107 objc_registerClassPair($SafeModeAlertItem);
110 if ($SBAlertItemsController != nil)
111 [[$SBAlertItemsController sharedInstance] activateAlertItem:[[[$SafeModeAlertItem alloc] init] autorelease]];
114 MSHook(void, SBStatusBar$touchesEnded$withEvent$, SBStatusBar *self, SEL sel, id touches, id event) {
116 _SBStatusBar$touchesEnded$withEvent$(self, sel, touches, event);
119 MSHook(void, SBStatusBar$mouseDown$, SBStatusBar *self, SEL sel, void *event) {
121 _SBStatusBar$mouseDown$(self, sel, event);
124 MSHook(void, UIStatusBar$touchesBegan$withEvent$, id self, SEL sel, void *arg0, void *arg1) {
126 _UIStatusBar$touchesBegan$withEvent$(self, sel, arg0, arg1);
129 MSHook(void, SBStatusBarDataManager$_updateTimeString, id self, SEL sel) {
130 if (char *_data = &MSHookIvar<char>(self, "_data")) {
131 char *timeString(_data + 20);
132 strcpy(timeString, "Exit Safe Mode");
136 static void SBIconController$showInfoAlertIfNeeded(id self, SEL sel) {
137 static bool loaded = false;
144 MSHook(int, SBButtonBar$maxIconColumns, SBButtonBar *self, SEL sel) {
147 max = _SBButtonBar$maxIconColumns(self, sel);
148 if (NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults])
149 if (NSDictionary *iconState = [defaults objectForKey:@"iconState"])
150 if (NSDictionary *buttonBar = [iconState objectForKey:@"buttonBar"])
151 if (NSArray *iconMatrix = [buttonBar objectForKey:@"iconMatrix"])
152 if ([iconMatrix count] != 0)
153 if (NSArray *row = [iconMatrix objectAtIndex:0]) {
154 int count([row count]);
161 MSHook(id, SBUIController$init, SBUIController *self, SEL sel) {
162 if ((self = _SBUIController$init(self, sel)) != nil) {
163 UIView *&_contentLayer(MSHookIvar<UIView *>(self, "_contentLayer"));
164 UIView *&_contentView(MSHookIvar<UIView *>(self, "_contentView"));
167 if (&_contentLayer != NULL)
168 layer = _contentLayer;
169 else if (&_contentView != NULL)
170 layer = _contentView;
175 [layer setBackgroundColor:[UIColor darkGrayColor]];
179 #define Paper_ "/Library/MobileSubstrate/MobileSafety.png"
181 MSHook(UIImage *, UIImage$defaultDesktopImage, UIImage *self, SEL sel) {
182 return [UIImage imageWithContentsOfFile:@Paper_];
185 MSHook(void, SBStatusBarTimeView$tile, SBStatusBarTimeView *self, SEL sel) {
186 NSString *&_time(MSHookIvar<NSString *>(self, "_time"));
187 CGRect &_textRect(MSHookIvar<CGRect>(self, "_textRect"));
190 _time = [@"Exit Safe Mode" retain];
191 id font([self textFont]);
192 CGSize size([_time sizeWithFont:font]);
193 CGRect frame([self frame]);
194 _textRect.size = size;
195 _textRect.origin.x = (frame.size.width - size.width) / 2;
196 _textRect.origin.y = (frame.size.height - size.height) / 2;
199 #define Dylib_ "/Library/MobileSubstrate/MobileSubstrate.dylib"
202 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
204 NSLog(@"MS:Warning: Entering Safe Mode");
206 _SBButtonBar$maxIconColumns = MSHookMessage(objc_getClass("SBButtonBar"), @selector(maxIconColumns), &$SBButtonBar$maxIconColumns);
207 _SBUIController$init = MSHookMessage(objc_getClass("SBUIController"), @selector(init), &$SBUIController$init);
208 _SBStatusBar$touchesEnded$withEvent$ = MSHookMessage(objc_getClass("SBStatusBar"), @selector(touchesEnded:withEvent:), &$SBStatusBar$touchesEnded$withEvent$);
209 _SBStatusBar$mouseDown$ = MSHookMessage(objc_getClass("SBStatusBar"), @selector(mouseDown:), &$SBStatusBar$mouseDown$);
210 _SBStatusBarDataManager$_updateTimeString = MSHookMessage(objc_getClass("SBStatusBarDataManager"), @selector(_updateTimeString), &$SBStatusBarDataManager$_updateTimeString);
211 _SBStatusBarTimeView$tile = MSHookMessage(objc_getClass("SBStatusBarTimeView"), @selector(tile), &$SBStatusBarTimeView$tile);
213 _UIStatusBar$touchesBegan$withEvent$ = MSHookMessage(objc_getClass("UIStatusBar"), @selector(touchesBegan:withEvent:), &$UIStatusBar$touchesBegan$withEvent$);
215 _UIImage$defaultDesktopImage = MSHookMessage(object_getClass(objc_getClass("UIImage")), @selector(defaultDesktopImage), &$UIImage$defaultDesktopImage);
217 char *dil = getenv("DYLD_INSERT_LIBRARIES");
219 NSLog(@"MS:Error: DYLD_INSERT_LIBRARIES is unset?");
221 NSArray *dylibs([[NSString stringWithUTF8String:dil] componentsSeparatedByString:@":"]);
222 NSUInteger index([dylibs indexOfObject:@ Dylib_]);
223 if (index == NSNotFound)
224 NSLog(@"MS:Error: dylib not in DYLD_INSERT_LIBRARIES?");
225 else if ([dylibs count] == 1)
226 unsetenv("DYLD_INSERT_LIBRARIES");
228 NSMutableArray *value([[[NSMutableArray alloc] init] autorelease]);
229 [value setArray:dylibs];
230 [value removeObjectAtIndex:index];
231 setenv("DYLD_INSERT_LIBRARIES", [[value componentsJoinedByString:@":"] UTF8String], !0);
235 $SBAlertItemsController = objc_getClass("SBAlertItemsController");
237 if (Class _class = objc_getClass("SBIconController")) {
238 SEL sel(@selector(showInfoAlertIfNeeded));
239 if (Method method = class_getInstanceMethod(_class, sel))
240 method_setImplementation(method, (IMP) &SBIconController$showInfoAlertIfNeeded);