-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathTweak.x
394 lines (341 loc) · 11.4 KB
/
Tweak.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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
#import <SpringBoard/SpringBoard.h>
#import <QuartzCore/QuartzCore.h>
#import <notify.h>
#import <CaptainHook/CaptainHook.h>
static CFMutableSetRef icons;
static CATransform3D currentTransform;
static CGFloat reflectionOpacity;
static int notify_token;
static UIInterfaceOrientation lastOrientation;
@interface SBIconView : UIView
@end
@interface SBNowPlayingBarView : UIView
@property (readonly, nonatomic) UIButton *toggleButton;
@property (readonly, nonatomic) UIButton *airPlayButton;
@end
@interface SBNowPlayingBarMediaControlsView : UIView
@property (readonly, nonatomic) UIButton *prevButton;
@property (readonly, nonatomic) UIButton *playButton;
@property (readonly, nonatomic) UIButton *nextButton;
@property (readonly, nonatomic) UIButton *airPlayButton;
@end
@interface UIView (Springtomize)
- (CGFloat)springtomizeScaleFactor;
@end
@interface SBOrientationLockManager : NSObject {
NSMutableSet *_lockOverrideReasons;
UIInterfaceOrientation _userLockedOrientation;
}
+ (SBOrientationLockManager *)sharedInstance;
- (void)restoreStateFromPrefs;
- (id)init;
- (void)dealloc;
- (void)lock;
- (void)lock:(UIInterfaceOrientation)lock;
- (void)unlock;
- (BOOL)isLocked;
- (UIInterfaceOrientation)userLockOrientation;
- (void)setLockOverrideEnabled:(BOOL)enabled forReason:(id)reason;
- (void)enableLockOverrideForReason:(id)reason suggestOrientation:(UIInterfaceOrientation)orientation;
- (void)enableLockOverrideForReason:(id)reason forceOrientation:(UIInterfaceOrientation)orientation;
- (BOOL)lockOverrideEnabled;
- (void)updateLockOverrideForCurrentDeviceOrientation;
- (void)_updateLockStateWithChanges:(id)changes;
- (void)_updateLockStateWithOrientation:(int)orientation changes:(id)changes;
- (void)_updateLockStateWithOrientation:(int)orientation forceUpdateHID:(BOOL)forceHID changes:(id)changes;
- (BOOL)_effectivelyLocked;
@end
@interface SpringBoard (iOS6)
- (void)setWantsOrientationEvents:(BOOL)wantsEvents;
- (void)updateOrientationAndAccelerometerSettings;
@end
static CATransform3D (*ScaledTransform)(UIView *, CATransform3D);
static CATransform3D ScaledTransformSpringtomize(UIView *iconView, CATransform3D transform)
{
CGFloat scale = [iconView springtomizeScaleFactor];
return CATransform3DScale(transform, scale, scale, 1.0f);
}
static CATransform3D ScaledTransformDefault(UIView *iconView, CATransform3D transform)
{
return transform;
}
%hook UIView
- (void)didMoveToWindow
{
if (!self.window)
CFSetRemoveValue(icons, self);
%orig;
}
- (void)dealloc
{
CFSetRemoveValue(icons, self);
%orig;
}
%end
typedef void (^RotatedViewUpdateBlock)(id view, UIInterfaceOrientation orientation, CATransform3D transform, NSTimeInterval duration);
static RotatedViewUpdateBlock standardUpdateBlock = ^(id view, UIInterfaceOrientation orientation, CATransform3D transform, NSTimeInterval duration) {
CALayer *layer = [view layer];
if (duration) {
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform"];
animation.toValue = [NSValue valueWithCATransform3D:transform];
animation.duration = duration;
animation.removedOnCompletion = YES;
animation.fromValue = [layer.presentationLayer valueForKeyPath:@"transform"];
[layer addAnimation:animation forKey:@"IconRotator"];
}
layer.transform = transform;
};
static RotatedViewUpdateBlock iconViewUpdateBlock = ^(id view, UIInterfaceOrientation orientation, CATransform3D transform, NSTimeInterval duration) {
transform = ScaledTransform(view, transform);
CALayer *layer = [view layer];
if (duration) {
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"sublayerTransform"];
animation.toValue = [NSValue valueWithCATransform3D:transform];
animation.duration = duration;
animation.removedOnCompletion = YES;
animation.fromValue = [layer.presentationLayer valueForKeyPath:@"sublayerTransform"];
[layer addAnimation:animation forKey:@"IconRotator"];
}
layer.sublayerTransform = transform;
UIImageView **imageView = CHIvarRef(view, _reflection, UIImageView *);
if (imageView)
(*imageView).alpha = reflectionOpacity;
};
static void ApplyRotatedViewTransform(UIView *view, RotatedViewUpdateBlock updateBlock)
{
if (view && updateBlock) {
[view.layer setValue:[[updateBlock copy] autorelease] forKey:@"IconRotatorBlock"];
CFSetSetValue(icons, view);
updateBlock(view, lastOrientation, currentTransform, 0.0);
}
}
%hook SBIconView
- (void)didMoveToWindow
{
%orig;
if (self.window) {
ApplyRotatedViewTransform(self, iconViewUpdateBlock);
}
}
- (void)didMoveToSuperview
{
%orig;
if (self.window) {
ApplyRotatedViewTransform(self, iconViewUpdateBlock);
}
}
%end
%hook SBNowPlayingBarView
- (void)didMoveToWindow
{
%orig;
if (self.window) {
ApplyRotatedViewTransform(self.toggleButton, standardUpdateBlock);
ApplyRotatedViewTransform(self.airPlayButton, standardUpdateBlock);
}
}
%end
%hook SBNowPlayingBarMediaControlsView
- (void)didMoveToWindow
{
%orig;
if (self.window) {
ApplyRotatedViewTransform(self.prevButton, standardUpdateBlock);
ApplyRotatedViewTransform(self.playButton, standardUpdateBlock);
ApplyRotatedViewTransform(self.nextButton, standardUpdateBlock);
ApplyRotatedViewTransform(self.airPlayButton, standardUpdateBlock);
}
}
%end
%hook SBSearchController
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *result = %orig;
if (result) {
NSArray *subviews = result.subviews;
if ([subviews count]) {
ApplyRotatedViewTransform([subviews objectAtIndex:0], standardUpdateBlock);
}
}
return result;
}
%end
static void SetAccelerometerEnabled(BOOL enabled)
{
if ([UIApp respondsToSelector:@selector(setWantsOrientationEvents:)] && [UIApp respondsToSelector:@selector(updateOrientationAndAccelerometerSettings)]) {
[(SpringBoard *)UIApp setWantsOrientationEvents:enabled];
[(SpringBoard *)UIApp updateOrientationAndAccelerometerSettings];
return;
}
// This code is quite evil
SBAccelerometerInterface *accelerometer = [%c(SBAccelerometerInterface) sharedInstance];
NSMutableArray **_clients = CHIvarRef(accelerometer, _clients, NSMutableArray *);
if (_clients) {
NSMutableArray *clients = *_clients;
if (!clients)
*_clients = clients = [[NSMutableArray alloc] init];
static SBAccelerometerClient *client;
if (!client) {
client = [[%c(SBAccelerometerClient) alloc] init];
[client setUpdateInterval:0.1];
}
if (client) {
if (enabled) {
if ([clients indexOfObjectIdenticalTo:client] == NSNotFound)
[clients addObject:client];
} else {
[clients removeObjectIdenticalTo:client];
}
}
}
[accelerometer updateSettings];
}
%hook SpringBoard
- (void)applicationDidFinishLaunching:(UIApplication *)application
{
%orig;
if ([UIView instancesRespondToSelector:@selector(springtomizeScaleFactor)])
ScaledTransform = ScaledTransformSpringtomize;
SetAccelerometerEnabled(YES);
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
}
%end
%hook SBAwayController
- (void)dimScreen:(BOOL)animated
{
%orig;
SetAccelerometerEnabled(NO);
}
- (void)undimScreen
{
%orig;
SBOrientationLockManager *olm = [%c(SBOrientationLockManager) sharedInstance];
if (![olm _effectivelyLocked])
SetAccelerometerEnabled(YES);
}
- (void)updateOrientationForUndim
{
%orig();
SetAccelerometerEnabled(YES);
}
%end;
%hook SBOrientationLockManager
- (void)setLockOverrideEnabled:(BOOL)enabled forReason:(NSString *)reason
{
if ([reason isEqualToString:@"SBOrientationLockForSwitcher"])
enabled = NO;
%orig();
}
%end
static void UpdateWithOrientation(UIInterfaceOrientation orientation)
{
switch (orientation) {
case UIInterfaceOrientationPortrait:
currentTransform = CATransform3DIdentity;
reflectionOpacity = 1.0f;
break;
case UIInterfaceOrientationPortraitUpsideDown:
currentTransform = CATransform3DMakeRotation(M_PI, 0.0f, 0.0f, 1.0f);
reflectionOpacity = 0.0f;
break;
case UIInterfaceOrientationLandscapeRight:
currentTransform = CATransform3DMakeRotation(0.5f * M_PI, 0.0f, 0.0f, 1.0f);
reflectionOpacity = 0.0f;
break;
case UIInterfaceOrientationLandscapeLeft:
currentTransform = CATransform3DMakeRotation(-0.5f * M_PI, 0.0f, 0.0f, 1.0f);
reflectionOpacity = 0.0f;
break;
default:
return;
}
[UIView animateWithDuration:0.2 animations:^{
for (UIView *view in (id)icons) {
RotatedViewUpdateBlock updateBlock = [view.layer valueForKey:@"IconRotatorBlock"];
updateBlock(view, orientation, currentTransform, 0.2);
}
}];
}
%hook SBOrientationLockManager
- (void)_updateLockStateWithChanges:(id)changes
{
%orig;
if ([self _effectivelyLocked]) {
SetAccelerometerEnabled(NO);
UpdateWithOrientation([self userLockOrientation]);
} else {
SetAccelerometerEnabled(YES);
}
}
- (void)_updateLockStateWithOrientation:(UIInterfaceOrientation)orientation forceUpdateHID:(BOOL)updateHID changes:(id)changes
{
%orig;
if ([self _effectivelyLocked]) {
SetAccelerometerEnabled(NO);
UpdateWithOrientation([self userLockOrientation]);
} else {
SetAccelerometerEnabled(YES);
}
}
- (void)_updateLockStateWithOrientation:(UIInterfaceOrientation)orientation changes:(id)changes
{
%orig;
if ([self _effectivelyLocked]) {
SetAccelerometerEnabled(NO);
UpdateWithOrientation([self userLockOrientation]);
} else {
SetAccelerometerEnabled(YES);
}
}
%end
static inline void UpdateWithDeviceOrientation(UIDeviceOrientation orientation)
{
if (UIDeviceOrientationIsValidInterfaceOrientation(orientation)) {
if (orientation != lastOrientation) {
lastOrientation = orientation;
switch (orientation) {
case UIDeviceOrientationPortrait:
UpdateWithOrientation(UIInterfaceOrientationPortrait);
break;
case UIDeviceOrientationPortraitUpsideDown:
UpdateWithOrientation(UIInterfaceOrientationPortraitUpsideDown);
break;
case UIDeviceOrientationLandscapeLeft:
UpdateWithOrientation(UIInterfaceOrientationLandscapeRight);
break;
case UIDeviceOrientationLandscapeRight:
UpdateWithOrientation(UIInterfaceOrientationLandscapeLeft);
break;
}
}
}
}
static void LegacyOrientationChangedCallback(CFNotificationCenterRef center, void *observer, CFStringRef name, const void *object, CFDictionaryRef userInfo)
{
SBOrientationLockManager *olm = [%c(SBOrientationLockManager) sharedInstance];
if ([olm _effectivelyLocked])
return;
uint64_t orientation = 0;
notify_get_state(notify_token, &orientation);
UpdateWithDeviceOrientation((UIDeviceOrientation)orientation);
}
static void UIDeviceOrientationChangedCallback(CFNotificationCenterRef center, void *observer, CFStringRef name, const void *object, CFDictionaryRef userInfo)
{
SBOrientationLockManager *olm = [%c(SBOrientationLockManager) sharedInstance];
if ([olm _effectivelyLocked])
return;
UpdateWithDeviceOrientation([UIDevice currentDevice].orientation);
}
%ctor
{
%init;
ScaledTransform = ScaledTransformDefault;
currentTransform = CATransform3DIdentity;
icons = CFSetCreateMutable(kCFAllocatorDefault, 0, NULL);
if (kCFCoreFoundationVersionNumber < 783.0) {
notify_register_check("com.apple.springboard.rawOrientation", ¬ify_token);
CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), NULL, LegacyOrientationChangedCallback, CFSTR("com.apple.springboard.rawOrientation"), NULL, CFNotificationSuspensionBehaviorCoalesce);
} else {
CFNotificationCenterAddObserver(CFNotificationCenterGetLocalCenter(), NULL, UIDeviceOrientationChangedCallback, (CFStringRef)UIDeviceOrientationDidChangeNotification, NULL, CFNotificationSuspensionBehaviorCoalesce);
}
}