-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathTweakSB.x
376 lines (332 loc) · 9.68 KB
/
TweakSB.x
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
#import <Foundation/Foundation.h>
#import <objc/runtime.h>
#import "SpringBoard.h"
#import "TPPrefsObserver.h"
#import "UIKitPrivate.h"
#include <assert.h>
#include <dlfcn.h>
#include <stdbool.h>
#include <unistd.h>
#include <dispatch/dispatch.h>
// #define DEBUG_LOG_IDIOM
#ifdef DEBUG_LOG_IDIOM
@interface NSThread(private)
+ (NSString *)ams_symbolicatedCallStackSymbols;
@end
#endif
static TPPrefsObserver* pref;
// Since some methods explicitly check for user interface idiom, I have no better way to fool them
// so I just hook them, set iPad idiom when necessary and set back to iPhone after calling original
static uint16_t forcePadIdiom = 0;
%hook UIDevice
- (UIUserInterfaceIdiom)userInterfaceIdiom {
// Ever wondered how I obtained those random functions to hook? This is my way
#ifdef DEBUG_LOG_IDIOM
{
static NSFileHandle *fileHandle;
static int left = 100;
static NSString *tmpOut = @"/var/mobile/Documents/stack.txt";
static NSString *realOut = @"/var/mobile/Documents/stack_out.txt";
if (!fileHandle) {
fileHandle = [NSFileHandle fileHandleForWritingAtPath:tmpOut];
if (!fileHandle) {
if (forcePadIdiom > 0) {
return UIUserInterfaceIdiomPad;
} else {
return %orig;
}
}
[fileHandle seekToEndOfFile];
}
// Log every single call stack to file
NSString *stackTrace = [NSThread ams_symbolicatedCallStackSymbols];
[fileHandle writeData:[stackTrace dataUsingEncoding:NSUTF8StringEncoding]];
if (left-- == 0) {
left = 100;
[fileHandle closeFile];
fileHandle = nil;
[NSFileManager.defaultManager removeItemAtPath:realOut error:nil];
[NSFileManager.defaultManager moveItemAtPath:tmpOut toPath:realOut error:nil];
}
return UIUserInterfaceIdiomPad;
}
#endif
if (forcePadIdiom > 0) {
return UIUserInterfaceIdiomPad;
} else {
return %orig;
}
}
%end
/*
%hook SBFloatingDockView
- (CGFloat)contentHeightForBounds:(CGRect)frame {
if (frame.size.width > frame.size.height) {
CGFloat width = frame.size.height;
frame.size.height = frame.size.width;
frame.size.width = width;
}
return %orig;
}
%end
*/
// Fix status bar for the external display
%hook _UIStatusBar
- (void)_prepareVisualProviderIfNeeded {
UIScreen *screen = self.targetScreen ?: self._effectiveTargetScreen;
if (screen._isExternal) {
// For performance reason, we're gonna overwrite userInterfaceIdiom directly
// userInterfaceIdiom: ldr x0, [x0, #0x8]
uint64_t *collection = (uint64_t *)(__bridge void *)screen.traitCollection;
if (collection) {
collection[1] = UIUserInterfaceIdiomPad;
}
}
%orig;
}
%end
%hook UIStatusBarWindow
- (void)setStatusBar:(UIStatusBar *)statusBar {
if (self.windowScene.screen._isExternal) {
statusBar.statusBar.targetScreen = self.windowScene.screen;
}
%orig;
}
%end
/*
%hook SBSystemShellExtendedDisplayControllerPolicy
-(void)displayController:(id)arg1 didBeginTransaction:(id)arg2 sceneManager:(id)arg3 displayConfiguration:(id)arg4 deactivationReasons:(unsigned long long)arg5 {
forcePadIdiom++;
%orig;
forcePadIdiom--;
}
%end
*/
// Enable Medusa multitasking (three-dots) button on top
%hook SBFullScreenSwitcherLiveContentOverlayCoordinator
-(void)layoutStateTransitionCoordinator:(id)arg1 transitionDidBeginWithTransitionContext:(id)arg2 {
forcePadIdiom++;
%orig;
forcePadIdiom--;
}
%end
/*
%hook SBShelfLiveContentOverlayCoordinator
-(void)layoutStateTransitionCoordinator:(id)arg1 transitionDidBeginWithTransitionContext:(id)arg2 {
forcePadIdiom++;
%orig;
forcePadIdiom--;
}
%end
*/
// Fix iOS 16 multitasking (split screen, slide over, stage manager)
%hook SBMainSwitcherControllerCoordinator
- (void)_loadContentViewControllerIfNecessaryForWindowScene:(id)scene {
forcePadIdiom++;
%orig;
forcePadIdiom--;
}
%end
// Min width and height are 150, smaller may crash the app
%hook SBSwitcherChamoisLayoutAttributes
- (void)setGridWidths:(NSArray<NSNumber *> *)values {
NSUInteger maxValue = values.lastObject.unsignedIntValue;
NSMutableArray *array = [NSMutableArray array];
for (int i = 150; i < maxValue; i += 20) {
[array addObject:@(i)];
}
[array addObject:@(maxValue)];
%orig(array);
}
- (void)setGridHeights:(NSArray<NSNumber *> *)values {
NSUInteger maxValue = values.lastObject.unsignedIntValue;
NSMutableArray *array = [NSMutableArray array];
for (int i = 150; i < maxValue; i += 20) {
[array addObject:@(i)];
}
[array addObject:@(maxValue)];
%orig(array);
}
%end
// Override app limit, I don't think this is healthy for battery, so I won't make it unlimited...
%hook SBSwitcherChamoisSettings
- (NSUInteger)maximumNumberOfAppsOnStage {
return 5;
}
%end
// FIXME: Is this needed?
%hook SBTraitsPipelineManager
-(id)defaultOrientationAnimationSettingsAnimatable:(BOOL)animatable {
forcePadIdiom++;
id result = %orig;
forcePadIdiom--;
return result;
}
%end
%hook SBTraitsSceneParticipantDelegate
// Allow upside down
- (BOOL)_isAllowedToHavePortraitUpsideDown {
return YES;
}
// Fix orientation issue for portrait-only apps
- (NSInteger)_orientationMode {
forcePadIdiom++;
NSInteger result = %orig;
forcePadIdiom--;
return result;
}
%end
// Workaround for iPhones with home button not being able to open Control Center
%hook CCSControlCenterDefaults
- (NSUInteger)_defaultPresentationGesture {
return 1;
}
%end
%hook SBHomeGestureSettings
- (BOOL)isHomeGestureEnabled {
return YES;
}
%end
%hook SBControlCenterController
-(NSUInteger)presentingEdge {
return 1;
}
%end
// Forcibly enable resizable as iOS somehow disabled it in the external display
%hook SBFluidSwitcherItemContainer
- (void)setAllowedTouchResizeCorners:(NSUInteger)cornerMask {
// !self.isResizingAllowed &&
if (self._screen != UIScreen.mainScreen) {
%orig(15);
// 12 (binary 1100): enable resizing for bottoms
// 15 (binary 1111): enable resizing for all corners
} else {
%orig;
}
}
%end
%hook SBAppResizeGrabberView
- (void)setAlpha:(CGFloat)alpha {
%orig;
self.hidden = pref.hideStageManagerResizeCorners;
}
%end
%hook SBFluidSwitcherViewController
// Use iPadOS app switching animation instead
- (BOOL)isDevicePad {
return pref.useiPadAppSwitchingAnimation;
}
// Restore number of grid to 1
/*
- (NSUInteger)numberOfRowsInGridSwitcher {
return 1;
}
*/
%end
// Fix truncated app name in app switcher
%hook SBAppSwitcherSettings
- (void)setDefaultValues {
%orig;
self.spacingBetweenLeadingEdgeAndIcon = 0;
self.spacingBetweenTrailingEdgeAndLabels = 0;
}
%end
%hook UIApplication
- (id)_defaultSupportedInterfaceOrientations {
forcePadIdiom++;
id result = %orig;
forcePadIdiom--;
return result;
}
%end
// Allow upside down Home Screen
%hook SBHomeScreenViewController
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
return %orig | UIInterfaceOrientationMaskPortraitUpsideDown;
}
%end
// Allow upside down Lock Screen
%hook SBCoverSheetPrimarySlidingViewController
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
return %orig | UIInterfaceOrientationMaskPortraitUpsideDown;
}
%end
// The following hooks are taken from various sources, please refer to tweaks that enable Slide Over.
%hook SpringBoard
- (NSInteger)homeScreenRotationStyle {
return pref.allowLandscapeHomeScreen ? 1 : %orig;
}
%end
%hook SBMedusaConfigurationUsageMetric
- (BOOL)_isFloatingActive {
return YES;
}
%end
%hook SBPlatformController
- (BOOL)isHomeGestureEnabled {
return YES;
}
- (NSInteger)medusaCapabilities {
return 2;
}
%end
%hook SBApplication
- (BOOL)isMedusaCapable {
return pref.forceEnableMedusaForLandscapeOnlyApps ||
(self.info.supportedInterfaceOrientations & UIInterfaceOrientationMaskPortrait) != 0;
}
- (BOOL)_supportsApplicationType:(int)arg1 {
return YES;
}
%end
%hook SBMainWorkspace
- (BOOL)isMedusaEnabled {
return YES;
}
%end
%hook SBFloatingDockController
+ (BOOL)isFloatingDockSupported {
return YES;
}
%end
// Force iPad app switcher, otherwise it will be broken
%hook SBAppSwitcherSettings
- (NSInteger)effectiveSwitcherStyle {
return 2;
}
%end
// Unlock external display support for MDC versions
int hookedExtDisplayEnabledFunc() {
// clang forgets to PAC this function, so we need this ugly line
int hack = 0; if (hack) { printf(""); }
return 1;
}
// Bypass Keyboard & Mouse requirement
%hook SBExternalDisplayRuntimeAvailabilitySettings
- (void)setDefaultValues {
self.requireHardwareKeyboard = NO;
self.requirePointer = NO;
}
%end
BOOL MGGetBoolAnswer(NSString* property);
%hookf(BOOL, MGGetBoolAnswer, NSString* property) {
// Hook ipad, DeviceSupportsEnhancedMultitasking
if ([property isEqualToString:@"DeviceSupportsEnhancedMultitasking"]) {
return YES;
}
return %orig;
}
%ctor {
// Unlock external display support for MDC versions
void *sbFoundationHandle = dlopen("/System/Library/PrivateFrameworks/SpringBoardFoundation.framework/SpringBoardFoundation", RTLD_GLOBAL);
// iOS 16.0
void *extDisplayEnabledFunc = dlsym(sbFoundationHandle, "SBChamoisExternalDisplayControllerIsEnabled");
if (!extDisplayEnabledFunc) {
// iOS 16.1.x
extDisplayEnabledFunc = dlsym(sbFoundationHandle, "SBFIsChamoisExternalDisplayControllerAvailable");
}
if (extDisplayEnabledFunc) {
MSHookFunction((void *)extDisplayEnabledFunc, (void *)hookedExtDisplayEnabledFunc, NULL);
}
pref = [TPPrefsObserver new];
}