]> git.saurik.com Git - safemode-ios.git/blobdiff - MobileSafety.mm
Force version.sh to output /something/, always.
[safemode-ios.git] / MobileSafety.mm
index e31e3274c55eeefb3a5bd7baa6264be777a3b834..3510a7fc75ba2d58caa547211d65d2b6b8d09bc2 100644 (file)
@@ -1,5 +1,5 @@
 /* Cydia Substrate - Powerful Code Insertion Platform
- * Copyright (C) 2008-2010  Jay Freeman (saurik)
+ * Copyright (C) 2008-2011  Jay Freeman (saurik)
 */
 
 /* GNU Lesser General Public License, Version 3 {{{ */
 **/
 /* }}} */
 
+%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>
 
 #include "CydiaSubstrate.h"
 
-@class SBButtonBar;
-@class SBStatusBar;
-@class SBUIController;
+MSClassHook(UIStatusBar)
+
+MSClassHook(UIImage)
+MSMetaClassHook(UIImage)
+
+MSClassHook(AAAccountManager)
+MSMetaClassHook(AAAccountManager)
+
+MSClassHook(BBSectionInfo)
+
+MSClassHook(SBAlertItemsController)
+MSClassHook(SBButtonBar)
+MSClassHook(SBIconController)
+MSClassHook(SBStatusBar)
+MSClassHook(SBStatusBarDataManager)
+MSClassHook(SBStatusBarTimeView)
+MSClassHook(SBUIController)
 
 Class $SafeModeAlertItem;
-Class $SBAlertItemsController;
 
 @interface SBAlertItem : NSObject {
 }
@@ -66,6 +95,7 @@ void SafeModeAlertItem$alertSheet$buttonClicked$(id self, SEL sel, id sheet, int
         break;
 
         case 2:
+            // XXX: there are better ways of restarting SpringBoard that would actually save state
             exit(0);
         break;
 
@@ -79,12 +109,14 @@ void SafeModeAlertItem$alertSheet$buttonClicked$(id self, SEL sel, id sheet, int
 
 void SafeModeAlertItem$configure$requirePasscodeForActions$(id self, SEL sel, BOOL configure, BOOL require) {
     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];
 }
@@ -111,40 +143,86 @@ static void MSAlert() {
         [[$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, void *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"])
@@ -158,8 +236,9 @@ MSHook(int, SBButtonBar$maxIconColumns, SBButtonBar *self, SEL sel) {
     } 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"));
 
@@ -176,13 +255,13 @@ MSHook(id, SBUIController$init, SBUIController *self, SEL sel) {
     } 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)
@@ -196,49 +275,10 @@ MSHook(void, SBStatusBarTimeView$tile, SBStatusBarTimeView *self, SEL sel) {
     _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");
-
-    if (Class _class = objc_getClass("SBIconController")) {
-        SEL sel(@selector(showInfoAlertIfNeeded));
-        if (Method method = class_getInstanceMethod(_class, sel))
-            method_setImplementation(method, (IMP) &SBIconController$showInfoAlertIfNeeded);
-    }
+// 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
 
-    [pool release];
+MSInstanceMessageHook0(BOOL, BBSectionInfo, showsInNotificationCenter) {
+    return NO;
 }