]> git.saurik.com Git - safemode-ios.git/blame - Tweak.xm
Fix the stupid alert popup on ALL the iOS versions.
[safemode-ios.git] / Tweak.xm
CommitLineData
df972f42 1/* Cydia Substrate - Powerful Code Insertion Platform
bef4d375 2 * Copyright (C) 2008-2013 Jay Freeman (saurik)
25f84761
JF
3*/
4
df972f42 5/* GNU Lesser General Public License, Version 3 {{{ */
25f84761 6/*
5bb6a76c 7 * Substrate is free software: you can redistribute it and/or modify it under
df972f42
JF
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.
25f84761 11 *
5bb6a76c 12 * Substrate is distributed in the hope that it will be useful, but WITHOUT
df972f42
JF
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.
25f84761 16 *
df972f42 17 * You should have received a copy of the GNU Lesser General Public License
5bb6a76c 18 * along with Substrate. If not, see <http://www.gnu.org/licenses/>.
df972f42
JF
19**/
20/* }}} */
25f84761
JF
21
22#import <CoreFoundation/CoreFoundation.h>
23#import <Foundation/Foundation.h>
24#import <CoreGraphics/CGGeometry.h>
39f17851
JF
25#import <UIKit/UIKit.h>
26
88e0e8ac 27#include <substrate.h>
2c75d26c 28
39f17851 29Class $SafeModeAlertItem;
39f17851 30
2c75d26c
JF
31@interface SBAlertItem : NSObject {
32}
33- (UIAlertView *) alertSheet;
34- (void) dismiss;
35@end
36
37@interface SBAlertItemsController : NSObject {
38}
39+ (SBAlertItemsController *) sharedInstance;
40- (void) activateAlertItem:(SBAlertItem *)item;
41@end
42
43@interface SBStatusBarTimeView : UIView {
44}
45- (id) textFont;
46@end
47
88d77501
JF
48@interface UIApplication (CydiaSubstrate)
49- (void) applicationOpenURL:(id)url;
50@end
51
52@interface UIAlertView (CydiaSubstrate)
53- (void) setForceHorizontalButtonsLayout:(BOOL)force;
54- (void) setBodyText:(NSString *)body;
55- (void) setNumberOfRows:(NSInteger)rows;
56@end
57
c7ea4f3c 58void SafeModeButtonClicked(int button) {
39f17851
JF
59 switch (button) {
60 case 1:
61 break;
62
63 case 2:
8d6b6bf4 64 if (kCFCoreFoundationVersionNumber >= 700)
d10105f2
JF
65 system("killall backboardd");
66 else
67 // XXX: there are better ways of restarting SpringBoard that would actually save state
68 exit(0);
39f17851
JF
69 break;
70
71 case 3:
88d77501 72 [[UIApplication sharedApplication] applicationOpenURL:[NSURL URLWithString:@"http://cydia.saurik.com/safemode/"]];
39f17851
JF
73 break;
74 }
c7ea4f3c
JF
75}
76
77void SafeModeAlertItem$alertSheet$buttonClicked$(id self, SEL sel, id sheet, int button) {
78 SafeModeButtonClicked(button);
79 [self dismiss];
80}
39f17851 81
c7ea4f3c
JF
82void SafeModeAlertItem$alertView$clickedButtonAtIndex$(id self, SEL sel, id sheet, NSInteger button) {
83 SafeModeButtonClicked(button + 1);
39f17851
JF
84 [self dismiss];
85}
86
87void SafeModeAlertItem$configure$requirePasscodeForActions$(id self, SEL sel, BOOL configure, BOOL require) {
2c75d26c 88 UIAlertView *sheet([self alertSheet]);
0625b5c3 89
39f17851 90 [sheet setDelegate:self];
a6712361 91 [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."];
39f17851
JF
92 [sheet addButtonWithTitle:@"OK"];
93 [sheet addButtonWithTitle:@"Restart"];
94 [sheet addButtonWithTitle:@"Help"];
95 [sheet setNumberOfRows:1];
0625b5c3 96
e5a54d69
JF
97 if ([sheet respondsToSelector:@selector(setForceHorizontalButtonsLayout:)])
98 [sheet setForceHorizontalButtonsLayout:YES];
39f17851
JF
99}
100
101void SafeModeAlertItem$performUnlockAction(id self, SEL sel) {
88e0e8ac 102 [[%c(SBAlertItemsController) sharedInstance] activateAlertItem:self];
39f17851
JF
103}
104
105static void MSAlert() {
106 if ($SafeModeAlertItem == nil)
107 $SafeModeAlertItem = objc_lookUpClass("SafeModeAlertItem");
108 if ($SafeModeAlertItem == nil) {
109 $SafeModeAlertItem = objc_allocateClassPair(objc_getClass("SBAlertItem"), "SafeModeAlertItem", 0);
110 if ($SafeModeAlertItem == nil)
111 return;
112
113 class_addMethod($SafeModeAlertItem, @selector(alertSheet:buttonClicked:), (IMP) &SafeModeAlertItem$alertSheet$buttonClicked$, "v@:@i");
c7ea4f3c 114 class_addMethod($SafeModeAlertItem, @selector(alertView:clickedButtonAtIndex:), (IMP) &SafeModeAlertItem$alertView$clickedButtonAtIndex$, "v@:@i");
39f17851
JF
115 class_addMethod($SafeModeAlertItem, @selector(configure:requirePasscodeForActions:), (IMP) &SafeModeAlertItem$configure$requirePasscodeForActions$, "v@:cc");
116 class_addMethod($SafeModeAlertItem, @selector(performUnlockAction), (IMP) SafeModeAlertItem$performUnlockAction, "v@:");
117 objc_registerClassPair($SafeModeAlertItem);
118 }
119
88e0e8ac
JF
120 if (%c(SBAlertItemsController) != nil)
121 [[%c(SBAlertItemsController) sharedInstance] activateAlertItem:[[[$SafeModeAlertItem alloc] init] autorelease]];
39f17851
JF
122}
123
0625b5c3
JF
124
125// XXX: on iOS 5.0, we really would prefer avoiding
126
88e0e8ac
JF
127%hook SBStatusBar
128- (void) touchesEnded:(id)touches withEvent:(id)event {
b4f9ed46 129 MSAlert();
88e0e8ac
JF
130 %orig(touches, event);
131} %end
b4f9ed46 132
88e0e8ac
JF
133%hook SBStatusBar
134- (void) mouseDown:(void *)event {
39f17851 135 MSAlert();
88e0e8ac
JF
136 %orig(event);
137} %end
39f17851 138
88e0e8ac
JF
139%hook UIStatusBar
140- (void) touchesBegan:(void *)touches withEvent:(void *)event {
e5a54d69 141 MSAlert();
88e0e8ac
JF
142 %orig(touches, event);
143} %end
e5a54d69 144
0625b5c3
JF
145
146// this fairly complex code came from Grant, to solve the "it Safe Mode"-in-bar bug
147
88e0e8ac
JF
148%hook SBStatusBarDataManager
149- (void) _updateTimeString {
22a7ee91
JF
150 char *_data(&MSHookIvar<char>(self, "_data"));
151 if (_data == NULL)
152 return;
153
154 Ivar _itemIsEnabled(object_getInstanceVariable(self, "_itemIsEnabled", NULL));
155 if (_itemIsEnabled == NULL)
156 return;
157
158 Ivar _itemIsCloaked(object_getInstanceVariable(self, "_itemIsCloaked", NULL));
159 if (_itemIsCloaked == NULL)
160 return;
161
162 size_t enabledOffset(ivar_getOffset(_itemIsEnabled));
163 size_t cloakedOffset(ivar_getOffset(_itemIsCloaked));
164 if (enabledOffset >= cloakedOffset)
165 return;
166
167 size_t offset(cloakedOffset - enabledOffset);
168 char *timeString(_data + offset);
169 strcpy(timeString, "Exit Safe Mode");
88e0e8ac 170} %end
e5a54d69 171
0625b5c3 172
601b245c
JF
173// this /insanely/ complex code came from that parrot guy... omg this is getting bad
174
175@interface SBStatusBarStateAggregator : NSObject
176- (void) _stopTimeItemTimer;
177@end
178
179%hook SBStatusBarStateAggregator
180
181- (void) _updateTimeItems {
182 if ([self respondsToSelector:@selector(_stopTimeItemTimer)])
183 [self _stopTimeItemTimer];
184 %orig;
185}
186
187- (void) _restartTimeItemTimer {
188}
189
190- (void) _resetTimeItemFormatter {
191 %orig;
192 if (NSDateFormatter *df = MSHookIvar<NSDateFormatter *>(self, "_timeItemDateFormatter"))
193 [df setDateFormat:@"'Exit' 'Safe' 'Mode'"];
194}
195
196%end
197
198
f7673201
JF
199static bool alerted_;
200
201static void AlertIfNeeded() {
202 if (alerted_)
39f17851 203 return;
f7673201 204 alerted_ = true;
39f17851 205 MSAlert();
25f84761
JF
206}
207
f037db0b 208// iOS 7
a4114048
JF
209%hook SBLockScreenManager
210- (void) _finishUIUnlockFromSource:(int)source withOptions:(id)options {
211 %orig;
212 AlertIfNeeded();
213} %end
214
f037db0b
JF
215// iOS 6
216%hook SBAwayController
217- (void) _finishUnlockWithSound:(BOOL)sound unlockSource:(int)source isAutoUnlock:(BOOL)is {
218 %orig;
219 AlertIfNeeded();
220} %end
0625b5c3 221
f037db0b
JF
222// iOS 5
223%hook SBAwayController
224- (void) _unlockWithSound:(BOOL)sound isAutoUnlock:(BOOL)is unlockSource:(int)source {
225 %orig;
f7673201 226 AlertIfNeeded();
88e0e8ac 227} %end
f7673201 228
f037db0b
JF
229// iOS 4.3 XXX: check lower versions
230%hook SBAwayController
231- (void) _unlockWithSound:(BOOL)sound isAutoUnlock:(BOOL)is unlockType:(int)type {
232 %orig;
233 AlertIfNeeded();
234} %end
0625b5c3
JF
235
236// -[SBIconController showInfoAlertIfNeeded] explains how to drag icons around the iPhone home screen
237// it used to be shown to users when they unlocked their screen for the first time, and happened every unlock
238// however, as of iOS 4.3, it got relegated to only appearing once the user installed an app or web clip
239
88e0e8ac
JF
240%hook SBIconController
241- (void) showInfoAlertIfNeeded {
f7673201 242 AlertIfNeeded();
88e0e8ac 243} %end
f7673201 244
0625b5c3
JF
245
246// the icon state, including crazy configurations like Five Icon Dock, is stored in SpringBoard's defaults
247// unfortunately, SpringBoard on iOS 2.0 and 2.1 (maybe 2.2 as well) buffer overrun with more than 4 icons
248// there is a third party package called IconSupport that remedies this, but not everyone is using it yet
249
88e0e8ac
JF
250%hook SBButtonBar
251- (int) maxIconColumns {
25f84761
JF
252 static int max;
253 if (max == 0) {
88e0e8ac 254 max = %orig();
25f84761
JF
255 if (NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults])
256 if (NSDictionary *iconState = [defaults objectForKey:@"iconState"])
257 if (NSDictionary *buttonBar = [iconState objectForKey:@"buttonBar"])
258 if (NSArray *iconMatrix = [buttonBar objectForKey:@"iconMatrix"])
259 if ([iconMatrix count] != 0)
260 if (NSArray *row = [iconMatrix objectAtIndex:0]) {
261 int count([row count]);
262 if (max < count)
263 max = count;
264 }
265 } return max;
88e0e8ac 266} %end
25f84761 267
0625b5c3 268
88e0e8ac
JF
269%hook SBUIController
270- (id) init {
271 if ((self = %orig()) != nil) {
b4f9ed46
JF
272 UIView *&_contentLayer(MSHookIvar<UIView *>(self, "_contentLayer"));
273 UIView *&_contentView(MSHookIvar<UIView *>(self, "_contentView"));
274
275 UIView *layer;
276 if (&_contentLayer != NULL)
277 layer = _contentLayer;
278 else if (&_contentView != NULL)
279 layer = _contentView;
280 else
281 layer = nil;
282
283 if (layer != nil)
284 [layer setBackgroundColor:[UIColor darkGrayColor]];
285 } return self;
88e0e8ac 286} %end
25f84761 287
1c74ea29 288#define Paper_ "/Library/MobileSubstrate/MobileSafety.png"
dbbe0f32 289
7ebd3397
JF
290%hook SBWallpaperImage
291+ (id) alloc {
292 return nil;
293} %end
294
88e0e8ac
JF
295%hook UIImage
296+ (UIImage *) defaultDesktopImage {
dbbe0f32 297 return [UIImage imageWithContentsOfFile:@Paper_];
88e0e8ac 298} %end
dbbe0f32 299
88e0e8ac
JF
300%hook SBStatusBarTimeView
301- (void) tile {
39f17851
JF
302 NSString *&_time(MSHookIvar<NSString *>(self, "_time"));
303 CGRect &_textRect(MSHookIvar<CGRect>(self, "_textRect"));
25f84761 304 if (_time != nil)
39f17851 305 [_time release];
dbbe0f32 306 _time = [@"Exit Safe Mode" retain];
2c75d26c 307 id font([self textFont]);
88d77501 308 CGSize size([_time sizeWithFont:font]);
39f17851 309 CGRect frame([self frame]);
39f17851
JF
310 _textRect.size = size;
311 _textRect.origin.x = (frame.size.width - size.width) / 2;
312 _textRect.origin.y = (frame.size.height - size.height) / 2;
88e0e8ac 313} %end
e9502538 314
0625b5c3
JF
315
316// notification widgets ("wee apps" or "bulletin board sections") are capable of crashing SpringBoard
317// unfortunately, which ones are in use are stored in SpringBoard's defaults, so we need to turn them off
318
88e0e8ac
JF
319%hook BBSectionInfo
320- (BOOL) showsInNotificationCenter {
e9502538 321 return NO;
88e0e8ac 322} %end
e06a3b1a
JF
323
324
c5182285
JF
325// we don't want this state persisted back to disk, however: that is just really really irritating
326
327%hook BBServer
328- (void) _writeBehaviorOverrides {}
329- (void) _writeSectionOrder {}
330- (void) _writeClearedSections {}
331- (void) _writeSectionInfo {}
332%end
333
334
e06a3b1a
JF
335// on iOS 6.0, Apple split parts of SpringBoard into a daemon called backboardd, including app launches
336// in order to allow safe mode to propogate into applications, we need to then tell backboardd here
337// XXX: (all of this should be replaced, however, with per-process launchd-mediated exception handling)
338
88e0e8ac
JF
339%hook BKSApplicationLaunchSettings
340- (void) setEnvironment:(NSDictionary *)original {
e06a3b1a 341 if (original == nil)
88e0e8ac 342 return %orig(nil);
e06a3b1a
JF
343
344 NSMutableDictionary *modified([original mutableCopy]);
345 [modified setObject:@"1" forKey:@"_MSSafeMode"];
88e0e8ac
JF
346 return %orig(modified);
347} %end
43b2fcfa 348
88e0e8ac 349%ctor {
43b2fcfa
JF
350 NSAutoreleasePool *pool([[NSAutoreleasePool alloc] init]);
351
352 // on iOS 6, backboardd is in charge of brightness, and freaks out when SpringBoard restarts :(
353 // the result is that the device is super dark until we attempt to update the brightness here.
354
355 if (kCFCoreFoundationVersionNumber >= 700) {
356 if (void (*GSEventSetBacklightLevel)(float) = reinterpret_cast<void (*)(float)>(dlsym(RTLD_DEFAULT, "GSEventSetBacklightLevel")))
357 if (NSMutableDictionary *defaults = [NSMutableDictionary dictionaryWithContentsOfFile:[NSString stringWithFormat:@"%@/Library/Preferences/com.apple.springboard.plist", NSHomeDirectory()]])
358 if (NSNumber *level = [defaults objectForKey:@"SBBacklightLevel2"])
359 GSEventSetBacklightLevel([level floatValue]);
360 }
361
362 [pool release];
363}