-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tweak.xm
209 lines (163 loc) · 5.37 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
#include "Tweak.h"
#import <UIKit/UIKit.h>
#import "substrate.h"
static int sixOff = 0;
static int tenOff = 0;
static SBLockScreenManager *sharedRef;
static bool deviceUpsideDown = false;
static float cachedScreenBrightness = 1.0;
//Cant figure out how to enable and disable AutoScreenDimm.
//static bool cacheedScreenAutoDimm = true;
static bool isDimmed;
static float kLockDelay = 5;
static double kSensitivity = -0.436332;
static bool kEnabled = true;
extern "C" void IOHIDEventQueueEnqueue(IOHIDEventQueueRef queue, IOHIDEventRef event);
void (*old_Enqueue)(IOHIDEventQueueRef queue, IOHIDEventRef event);
#pragma mark - Enqueue
void newEnqueue(IOHIDEventQueueRef queue, IOHIDEventRef event){
if(kEnabled){
if(IOHIDEventGetType(event) == (unsigned)14){
if(sixOff == 0){
sixOff = 5;
if(PROX_VALUE(event) < (unsigned)300){
POST_NOTIF("dev.squiddy.PP/StartM")
}else{
POST_NOTIF("dev.squiddy.PP/StopM")
}
}else{
sixOff--;
}
}
old_Enqueue(queue, event);
}else{
old_Enqueue(queue, event);
}
}
@implementation UX
UIWindow *__strong dimWindow;
+(void)restoreScreenBrightness{
dispatch_async (dispatch_get_main_queue(), ^{
dimWindow.hidden = YES;
dimWindow = nil;
isDimmed = false;
[[%c(SBBrightnessController) sharedBrightnessController] setBrightnessLevel:cachedScreenBrightness];
});
}
+(void)dimmScreenBrightness{
dispatch_async (dispatch_get_main_queue(), ^{
//Cache the screen brightness
cachedScreenBrightness = [UIScreen mainScreen].brightness;
//Dimm the screen
[[%c(SBBrightnessController) sharedBrightnessController] setBrightnessLevel:0];
dimWindow = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
dimWindow.windowLevel = UIWindowLevelAlert +1;
UIViewController *controller = [[UIViewController alloc] init];
dimWindow.rootViewController = controller;
UIView *backgroundView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
backgroundView.backgroundColor = [UIColor colorWithRed:0.1 green:.1 blue:.1 alpha:1];
//TODO PREFS here
UITapGestureRecognizer *singleFingerTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(restoreScreenBrightness)];
[backgroundView addGestureRecognizer:singleFingerTap];
backgroundView.alpha = 0.8;
[controller.view addSubview:backgroundView];
[dimWindow setHidden:NO];
[dimWindow makeKeyAndVisible];
//Tell console what happened
//Remember we've dimmed it
isDimmed = true;
//TODO PREFS here
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, kLockDelay * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
lockScreen();
});
});
return;
}
@end
#pragma mark - Screen Brightness
%hook SpringBoard
-(void)_ringerChanged:(struct __IOHIDEvent *)arg1 {
HBLogDebug(@" isDimmed: %d", isDimmed);
HBLogDebug(@" kDelay: %f", kLockDelay);
HBLogDebug(@" deviceUpsideDow: %d", deviceUpsideDown);
HBLogDebug(@" sensitivity %f", kSensitivity);
%orig;
}
%end
static void lockScreen(){
if(isDimmed){
[[[%c(SpringBoard) sharedApplication] pluginUserAgent] lockAndDimDevice];
[UX restoreScreenBrightness];
}
}
#pragma mark - Start/Stop Motion & hook
%hook SBLockScreenManager
%property (nonatomic, retain) CMMotionManager *motionManager;
-(id)init {
self.motionManager = [[%c(CMMotionManager) alloc] init];
self.motionManager.deviceMotionUpdateInterval = 1.0;
sharedRef = %orig;
return sharedRef;
}
%new
-(void)startMotion {
cachedScreenBrightness = [UIScreen mainScreen].brightness;
[self.motionManager startDeviceMotionUpdatesUsingReferenceFrame:CMAttitudeReferenceFrameXMagneticNorthZVertical
toQueue:[NSOperationQueue mainQueue]
withHandler:^(CMDeviceMotion *accelerometerData, NSError *error) {
dispatch_async(dispatch_get_main_queue(), ^{
if ((double)[[NSNumber numberWithDouble:accelerometerData.attitude.pitch] doubleValue] < (double)TRIGGERANGLE ){
if(!deviceUpsideDown){
deviceUpsideDown = true;
[UX dimmScreenBrightness];
}
}else{
if(deviceUpsideDown){
deviceUpsideDown = false;
[UX restoreScreenBrightness];
}else {
tenOff++;
if(tenOff == 10){
[self stopMotion];
tenOff = 0;
}
}
}
});
return;
}];
}
%new
-(void)stopMotion{
[self.motionManager stopDeviceMotionUpdates];
isDimmed = 0;
deviceUpsideDown = 0;
}
%end
static void startMotion(){
[sharedRef startMotion];
}
static void stopMotion(){
[sharedRef stopMotion];
[UX restoreScreenBrightness];
}
static void loadPrefs(){
NSDictionary *prefs = [[NSUserDefaults standardUserDefaults] persistentDomainForName:@"dev.squiddy.PP"];
if (prefs) {
kLockDelay = [prefs objectForKey:@"delay"] ? [[prefs objectForKey:@"delay"] floatValue] : 5.f;
kEnabled = [prefs objectForKey:@"enabled"] ? [[prefs objectForKey:@"enabled"] boolValue] : YES;
float temp = 0;
temp = [prefs objectForKey:@"sensitivity"] ? [[prefs objectForKey:@"sensitivity"] floatValue] : -20.f;
//Convert Degrees to radian.
kSensitivity = (temp * M_PI)/180;
}
}
%ctor {
LISTEN_NOTIF(loadPrefs, "dev.squiddy.PP/prefs")
if ([[[NSProcessInfo processInfo].processName uppercaseString] isEqualToString:@"SPRINGBOARD"]) {
LISTEN_NOTIF(startMotion, "dev.squiddy.PP/StartM")
LISTEN_NOTIF(stopMotion, "dev.squiddy.PP/StopM")
}
loadPrefs();
MSHookFunction(&IOHIDEventQueueEnqueue,&newEnqueue,&old_Enqueue);
}