/* Cydia Substrate - Powerful Code Insertion Platform
- * Copyright (C) 2008-2010 Jay Freeman (saurik)
+ * Copyright (C) 2008-2012 Jay Freeman (saurik)
*/
/* GNU Lesser General Public License, Version 3 {{{ */
/*
- * Cycript is free software: you can redistribute it and/or modify it under
+ * Substrate is free software: you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the
* Free Software Foundation, either version 3 of the License, or (at your
* option) any later version.
*
- * Cycript is distributed in the hope that it will be useful, but WITHOUT
+ * Substrate is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
* License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
- * along with Cycript. If not, see <http://www.gnu.org/licenses/>.
+ * along with Substrate. If not, see <http://www.gnu.org/licenses/>.
**/
/* }}} */
+%apt Package: com.saurik.substrate.safemode
+%apt Author: Jay Freeman (saurik) <saurik@saurik.com>
+
+%apt Name: Substrate Safe Mode
+%apt Description: safe mode safety extension (safe)
+
+%apt Depends: mobilesubstrate (>= 0.9.3367+38)
+
+%fflag 1
+%fflag 2
+
+%bundle com.apple.springboard
+
+%flag -framework Foundation
+%flag -framework UIKit
+
#import <CoreFoundation/CoreFoundation.h>
#import <Foundation/Foundation.h>
#import <CoreGraphics/CGGeometry.h>
#import <UIKit/UIKit.h>
-#import <SpringBoard/SBAlertItem.h>
-#import <SpringBoard/SBAlertItemsController.h>
-#import <SpringBoard/SBButtonBar.h>
-#import <SpringBoard/SBStatusBarController.h>
-#import <SpringBoard/SBStatusBarDataManager.h>
-#import <SpringBoard/SBStatusBarTimeView.h>
-#import <SpringBoard/SBUIController.h>
+#include "CydiaSubstrate.h"
+
+MSClassHook(UIStatusBar)
-#include <substrate.h>
+MSClassHook(UIImage)
+MSMetaClassHook(UIImage)
+
+MSClassHook(AAAccountManager)
+MSMetaClassHook(AAAccountManager)
+
+MSClassHook(BBSectionInfo)
+MSClassHook(BKSApplicationLaunchSettings)
+
+MSClassHook(SBAlertItemsController)
+MSClassHook(SBButtonBar)
+MSClassHook(SBIconController)
+MSClassHook(SBStatusBar)
+MSClassHook(SBStatusBarDataManager)
+MSClassHook(SBStatusBarTimeView)
+MSClassHook(SBUIController)
Class $SafeModeAlertItem;
-Class $SBAlertItemsController;
+
+@interface SBAlertItem : NSObject {
+}
+- (UIAlertView *) alertSheet;
+- (void) dismiss;
+@end
+
+@interface SBAlertItemsController : NSObject {
+}
++ (SBAlertItemsController *) sharedInstance;
+- (void) activateAlertItem:(SBAlertItem *)item;
+@end
+
+@interface SBStatusBarTimeView : UIView {
+}
+- (id) textFont;
+@end
+
+@interface UIApplication (CydiaSubstrate)
+- (void) applicationOpenURL:(id)url;
+@end
+
+@interface UIAlertView (CydiaSubstrate)
+- (void) setForceHorizontalButtonsLayout:(BOOL)force;
+- (void) setBodyText:(NSString *)body;
+- (void) setNumberOfRows:(NSInteger)rows;
+@end
void SafeModeAlertItem$alertSheet$buttonClicked$(id self, SEL sel, id sheet, int button) {
switch (button) {
break;
case 2:
- exit(0);
+ if (kCFCoreFoundationVersionNumber > 700)
+ system("killall backboardd");
+ else
+ // XXX: there are better ways of restarting SpringBoard that would actually save state
+ exit(0);
break;
case 3:
- [UIApp applicationOpenURL:[NSURL URLWithString:@"http://cydia.saurik.com/safemode/"]];
+ [[UIApplication sharedApplication] applicationOpenURL:[NSURL URLWithString:@"http://cydia.saurik.com/safemode/"]];
break;
}
}
void SafeModeAlertItem$configure$requirePasscodeForActions$(id self, SEL sel, BOOL configure, BOOL require) {
- id sheet([self alertSheet]);
+ UIAlertView *sheet([self alertSheet]);
+
[sheet setDelegate:self];
- [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."];
+ [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.\n\nTap \"Help\" below for more tips."];
[sheet addButtonWithTitle:@"OK"];
[sheet addButtonWithTitle:@"Restart"];
[sheet addButtonWithTitle:@"Help"];
[sheet setNumberOfRows:1];
+
if ([sheet respondsToSelector:@selector(setForceHorizontalButtonsLayout:)])
[sheet setForceHorizontalButtonsLayout:YES];
}
[[$SBAlertItemsController sharedInstance] activateAlertItem:[[[$SafeModeAlertItem alloc] init] autorelease]];
}
-MSHook(void, SBStatusBar$touchesEnded$withEvent$, SBStatusBar *self, SEL sel, id touches, id event) {
+
+// XXX: on iOS 5.0, we really would prefer avoiding
+
+MSInstanceMessageHook2(void, SBStatusBar, touchesEnded,withEvent, id, touches, id, event) {
MSAlert();
- _SBStatusBar$touchesEnded$withEvent$(self, sel, touches, event);
+ MSOldCall(touches, event);
}
-MSHook(void, SBStatusBar$mouseDown$, SBStatusBar *self, SEL sel, GSEventRef event) {
+MSInstanceMessageHook1(void, SBStatusBar, mouseDown, void *, event) {
MSAlert();
- _SBStatusBar$mouseDown$(self, sel, event);
+ MSOldCall(event);
}
-MSHook(void, UIStatusBar$touchesBegan$withEvent$, id self, SEL sel, void *arg0, void *arg1) {
+MSInstanceMessageHook2(void, UIStatusBar, touchesBegan,withEvent, void *, touches, void *, event) {
MSAlert();
- _UIStatusBar$touchesBegan$withEvent$(self, sel, arg0, arg1);
+ MSOldCall(touches, event);
}
-MSHook(void, SBStatusBarDataManager$_updateTimeString, id self, SEL sel) {
- if (char *_data = &MSHookIvar<char>(self, "_data")) {
- char *timeString(_data + 20);
- strcpy(timeString, "Exit Safe Mode");
- }
+
+// this fairly complex code came from Grant, to solve the "it Safe Mode"-in-bar bug
+
+MSInstanceMessageHook0(void, SBStatusBarDataManager, _updateTimeString) {
+ char *_data(&MSHookIvar<char>(self, "_data"));
+ if (_data == NULL)
+ return;
+
+ Ivar _itemIsEnabled(object_getInstanceVariable(self, "_itemIsEnabled", NULL));
+ if (_itemIsEnabled == NULL)
+ return;
+
+ Ivar _itemIsCloaked(object_getInstanceVariable(self, "_itemIsCloaked", NULL));
+ if (_itemIsCloaked == NULL)
+ return;
+
+ size_t enabledOffset(ivar_getOffset(_itemIsEnabled));
+ size_t cloakedOffset(ivar_getOffset(_itemIsCloaked));
+ if (enabledOffset >= cloakedOffset)
+ return;
+
+ size_t offset(cloakedOffset - enabledOffset);
+ char *timeString(_data + offset);
+ strcpy(timeString, "Exit Safe Mode");
}
-static void SBIconController$showInfoAlertIfNeeded(id self, SEL sel) {
- static bool loaded = false;
- if (loaded)
+
+static bool alerted_;
+
+static void AlertIfNeeded() {
+ if (alerted_)
return;
- loaded = true;
+ alerted_ = true;
MSAlert();
}
-MSHook(int, SBButtonBar$maxIconColumns, SBButtonBar *self, SEL sel) {
+
+// on iOS 4.3 and above we can use this advertisement, which seems to check every time the user unlocks
+// XXX: verify that this still works on iOS 5.0
+
+MSClassMessageHook0(void, AAAccountManager, showMobileMeOfferIfNecessary) {
+ AlertIfNeeded();
+}
+
+
+// -[SBIconController showInfoAlertIfNeeded] explains how to drag icons around the iPhone home screen
+// it used to be shown to users when they unlocked their screen for the first time, and happened every unlock
+// however, as of iOS 4.3, it got relegated to only appearing once the user installed an app or web clip
+
+MSInstanceMessageHook0(void, SBIconController, showInfoAlertIfNeeded) {
+ AlertIfNeeded();
+}
+
+
+// the icon state, including crazy configurations like Five Icon Dock, is stored in SpringBoard's defaults
+// unfortunately, SpringBoard on iOS 2.0 and 2.1 (maybe 2.2 as well) buffer overrun with more than 4 icons
+// there is a third party package called IconSupport that remedies this, but not everyone is using it yet
+
+MSInstanceMessageHook0(int, SBButtonBar, maxIconColumns) {
static int max;
if (max == 0) {
- max = _SBButtonBar$maxIconColumns(self, sel);
+ max = MSOldCall();
if (NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults])
if (NSDictionary *iconState = [defaults objectForKey:@"iconState"])
if (NSDictionary *buttonBar = [iconState objectForKey:@"buttonBar"])
} return max;
}
-MSHook(id, SBUIController$init, SBUIController *self, SEL sel) {
- if ((self = _SBUIController$init(self, sel)) != nil) {
+
+MSInstanceMessageHook0(id, SBUIController, init) {
+ if ((self = MSOldCall()) != nil) {
UIView *&_contentLayer(MSHookIvar<UIView *>(self, "_contentLayer"));
UIView *&_contentView(MSHookIvar<UIView *>(self, "_contentView"));
} return self;
}
-#define Paper_ "/Library/MobileSubstrate/MobilePaper.png"
+#define Paper_ "/Library/MobileSubstrate/MobileSafety.png"
-MSHook(UIImage *, UIImage$defaultDesktopImage, UIImage *self, SEL sel) {
+MSClassMessageHook0(UIImage *, UIImage, defaultDesktopImage) {
return [UIImage imageWithContentsOfFile:@Paper_];
}
-MSHook(void, SBStatusBarTimeView$tile, SBStatusBarTimeView *self, SEL sel) {
+MSInstanceMessageHook0(void, SBStatusBarTimeView, tile) {
NSString *&_time(MSHookIvar<NSString *>(self, "_time"));
CGRect &_textRect(MSHookIvar<CGRect>(self, "_textRect"));
if (_time != nil)
[_time release];
_time = [@"Exit Safe Mode" retain];
- GSFontRef font([self textFont]);
- CGSize size([_time sizeWithFont:(id)font]);
+ id font([self textFont]);
+ CGSize size([_time sizeWithFont:font]);
CGRect frame([self frame]);
_textRect.size = size;
_textRect.origin.x = (frame.size.width - size.width) / 2;
_textRect.origin.y = (frame.size.height - size.height) / 2;
}
-#define Dylib_ "/Library/MobileSubstrate/MobileSubstrate.dylib"
-
-MSInitialize {
- NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
-
- NSLog(@"MS:Warning: Entering Safe Mode");
-
- _SBButtonBar$maxIconColumns = MSHookMessage(objc_getClass("SBButtonBar"), @selector(maxIconColumns), &$SBButtonBar$maxIconColumns);
- _SBUIController$init = MSHookMessage(objc_getClass("SBUIController"), @selector(init), &$SBUIController$init);
- _SBStatusBar$touchesEnded$withEvent$ = MSHookMessage(objc_getClass("SBStatusBar"), @selector(touchesEnded:withEvent:), &$SBStatusBar$touchesEnded$withEvent$);
- _SBStatusBar$mouseDown$ = MSHookMessage(objc_getClass("SBStatusBar"), @selector(mouseDown:), &$SBStatusBar$mouseDown$);
- _SBStatusBarDataManager$_updateTimeString = MSHookMessage(objc_getClass("SBStatusBarDataManager"), @selector(_updateTimeString), &$SBStatusBarDataManager$_updateTimeString);
- _SBStatusBarTimeView$tile = MSHookMessage(objc_getClass("SBStatusBarTimeView"), @selector(tile), &$SBStatusBarTimeView$tile);
-
- _UIStatusBar$touchesBegan$withEvent$ = MSHookMessage(objc_getClass("UIStatusBar"), @selector(touchesBegan:withEvent:), &$UIStatusBar$touchesBegan$withEvent$);
-
- _UIImage$defaultDesktopImage = MSHookMessage(object_getClass(objc_getClass("UIImage")), @selector(defaultDesktopImage), &$UIImage$defaultDesktopImage);
-
- char *dil = getenv("DYLD_INSERT_LIBRARIES");
- if (dil == NULL)
- NSLog(@"MS:Error: DYLD_INSERT_LIBRARIES is unset?");
- else {
- NSArray *dylibs([[NSString stringWithUTF8String:dil] componentsSeparatedByString:@":"]);
- NSUInteger index([dylibs indexOfObject:@ Dylib_]);
- if (index == NSNotFound)
- NSLog(@"MS:Error: dylib not in DYLD_INSERT_LIBRARIES?");
- else if ([dylibs count] == 1)
- unsetenv("DYLD_INSERT_LIBRARIES");
- else {
- NSMutableArray *value([[[NSMutableArray alloc] init] autorelease]);
- [value setArray:dylibs];
- [value removeObjectAtIndex:index];
- setenv("DYLD_INSERT_LIBRARIES", [[value componentsJoinedByString:@":"] UTF8String], !0);
- }
- }
- $SBAlertItemsController = objc_getClass("SBAlertItemsController");
+// notification widgets ("wee apps" or "bulletin board sections") are capable of crashing SpringBoard
+// unfortunately, which ones are in use are stored in SpringBoard's defaults, so we need to turn them off
- if (Class _class = objc_getClass("SBIconController")) {
- SEL sel(@selector(showInfoAlertIfNeeded));
- if (Method method = class_getInstanceMethod(_class, sel))
- method_setImplementation(method, (IMP) &SBIconController$showInfoAlertIfNeeded);
- }
+MSInstanceMessageHook0(BOOL, BBSectionInfo, showsInNotificationCenter) {
+ return NO;
+}
+
+
+// on iOS 6.0, Apple split parts of SpringBoard into a daemon called backboardd, including app launches
+// in order to allow safe mode to propogate into applications, we need to then tell backboardd here
+// XXX: (all of this should be replaced, however, with per-process launchd-mediated exception handling)
+
+MSInstanceMessageHook1(void, BKSApplicationLaunchSettings, setEnvironment, NSDictionary *, original) {
+ if (original == nil)
+ return MSOldCall(nil);
- [pool release];
+ NSMutableDictionary *modified([original mutableCopy]);
+ [modified setObject:@"1" forKey:@"_MSSafeMode"];
+ return MSOldCall(modified);
}