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