-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTweak.xm
219 lines (196 loc) · 6.64 KB
/
Tweak.xm
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
#import "resources.h"
#import "Oneko.h"
@interface UIScreen(Private)
- (CGRect)_referenceBounds;
@end
@interface UIWindow(Private)
- (UIInterfaceOrientation)interfaceOrientation;
- (void)_rotateWindowToOrientation:(UIInterfaceOrientation)orientation
updateStatusBar:(BOOL)updateStatusBar duration:(CGFloat)duration
skipCallbacks:(BOOL)skipCallbacks;
@end
@interface SpringBoard : UIApplication
- (BOOL)isLocked;
- (NSSet<UIWindowScene *> *)connectedScenes;
- (UIInterfaceOrientation)activeInterfaceOrientation;
@end
@interface OnekoWindow : UIWindow
@end
@implementation OnekoWindow
- (BOOL)autorotates {
return NO;
}
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event {
return NO;
}
@end
@interface OnekoViewController : UIViewController
@end
@implementation OnekoViewController
- (BOOL)shouldAutorotate {
return NO;
}
@end
//FIXME: There has to be a better way to do this
static CGPoint TranslatePoint(CGPoint point, CGSize bounds,
UIInterfaceOrientation from, UIInterfaceOrientation to)
{
CGPoint ret = point;
const CGFloat origX = point.x;
const CGFloat origY = point.y;
const CGFloat mirroredX = bounds.width - origX;
const CGFloat mirroredY = bounds.height - origY;
switch (from) {
case UIInterfaceOrientationPortraitUpsideDown: switch (to) {
case UIInterfaceOrientationPortraitUpsideDown:
// no change
break;
case UIInterfaceOrientationLandscapeRight:
ret = CGPointMake(mirroredY, origX);
break;
case UIInterfaceOrientationLandscapeLeft:
ret = CGPointMake(origY, mirroredX);
break;
case UIInterfaceOrientationPortrait:
ret = CGPointMake(mirroredX, mirroredY);
default:
break;
}
break;
case UIInterfaceOrientationLandscapeRight: switch (to) {
case UIInterfaceOrientationPortraitUpsideDown:
ret = CGPointMake(origY, mirroredX);
break;
case UIInterfaceOrientationLandscapeRight:
// no change
break;
case UIInterfaceOrientationLandscapeLeft:
ret = CGPointMake(mirroredX, mirroredY);
break;
case UIInterfaceOrientationPortrait:
ret = CGPointMake(mirroredY, origX);
default:
break;
}
break;
case UIInterfaceOrientationLandscapeLeft: switch (to) {
case UIInterfaceOrientationPortraitUpsideDown:
ret = CGPointMake(mirroredY, origX);
break;
case UIInterfaceOrientationLandscapeRight:
ret = CGPointMake(mirroredX, mirroredY);
break;
case UIInterfaceOrientationLandscapeLeft:
// no change
break;
case UIInterfaceOrientationPortrait:
ret = CGPointMake(origY, mirroredX);
default:
break;
}
break;
case UIInterfaceOrientationPortrait:
default: switch (to) {
case UIInterfaceOrientationPortraitUpsideDown:
ret = CGPointMake(mirroredX, mirroredY);
break;
case UIInterfaceOrientationLandscapeRight:
ret = CGPointMake(origY, mirroredX);
break;
case UIInterfaceOrientationLandscapeLeft:
ret = CGPointMake(mirroredY, origX);
break;
case UIInterfaceOrientationPortrait:
// no change
break;
default:
break;
}
break;
}
return ret;
}
static OnekoViewController *viewController;
static Oneko *neko;
static OnekoWindow *window;
static NSTimer *timer;
@interface UITouchesEvent : NSObject
@end
%hook UITouchesEvent
-(void)_setHIDEvent:(id)event {
%orig;
NSSet<UITouch *> *touches;
if (@available(iOS 11.0, *)) {
touches = [(NSMutableSet *)[self valueForKey:@"_allTouchesMutable"] copy];
} else {
touches = (NSSet *)[self valueForKey:@"_allTouches"];
}
if (touches.count == 0) {
return;
}
UITouch *touch = [touches anyObject];
CGPoint point = [touch locationInView:nil];
if ([[touch window] interfaceOrientation] == UIInterfaceOrientationPortrait) {
CGSize referenceBounds = [[UIScreen mainScreen] _referenceBounds].size;
CGPoint mouseLocation = TranslatePoint(point, referenceBounds,
UIInterfaceOrientationPortrait, [window interfaceOrientation]);
neko.mouseLocation = mouseLocation;
}
}
%end
static void onekoTimerTick() {
SpringBoard *springboard = (SpringBoard *)[UIApplication sharedApplication];
if ([springboard isLocked]) {
neko.hidden = YES;
return;
}
neko.hidden = NO;
UIInterfaceOrientation orientation = [springboard activeInterfaceOrientation];
if ([window interfaceOrientation] != orientation) {
CGSize vcSize = [viewController.view bounds].size;
UIInterfaceOrientation from = [window interfaceOrientation];
UIInterfaceOrientation to = orientation;
CGRect frame = neko.frame;
frame.origin.x += frame.size.width / 2;
frame.origin.y += frame.size.height / 2;
frame.origin = TranslatePoint(frame.origin, vcSize, from, to);
frame.origin.y -= frame.size.height / 2;
neko.mouseLocation = frame.origin;
frame.origin.x -= frame.size.width / 2;
neko.frame = frame;
[window _rotateWindowToOrientation:orientation updateStatusBar:NO
duration:0 skipCallbacks:NO];
}
[neko handleTimer:timer];
}
%hook SpringBoard
- (void)applicationDidFinishLaunching:(id)application {
%orig;
window = [[OnekoWindow alloc] initWithFrame:UIScreen.mainScreen.bounds];
if (@available(iOS 13.0, *)) {
window.windowScene = (id)[self connectedScenes].allObjects[0];
}
else {
window.screen = [UIScreen mainScreen];
}
viewController = [OnekoViewController new];
neko = [Oneko new];
neko.userInteractionEnabled = NO;
[viewController.view addSubview:neko];
window.rootViewController = viewController;
window.userInteractionEnabled = YES;
window.opaque = NO;
window.hidden = NO;
window.backgroundColor = [UIColor clearColor];
window.windowLevel = CGFLOAT_MAX / 2.0;
[window makeKeyAndVisible];
timer = [NSTimer
timerWithTimeInterval:0.125f
repeats:YES
block:^(NSTimer *timer) {
onekoTimerTick();
}
];
[[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
}
%end