-
Notifications
You must be signed in to change notification settings - Fork 5
/
Tweak.x
190 lines (150 loc) · 4.86 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
#import <UIKit/UIKit.h>
#import <objc/runtime.h>
#import <UIKit/UIWindow+Private.h>
#import "Tweak.h"
#import "TVLock.h"
#import "Globals.h"
// =============================== Globals ===============================
#define TWEAK_NAME @"TVLock"
#define BUNDLE [NSString stringWithFormat:@"com.wrp1002.%@", [TWEAK_NAME lowercaseString]]
#define BUNDLE_NOTIFY (CFStringRef)[NSString stringWithFormat:@"%@/ReloadPrefs", BUNDLE]
static TVLock *__strong tvLock;
double totalTime = 1.0;
double lockTime = 0.25;
// For @available to work. needs commented out for github to compile
//int __isOSVersionAtLeast(int major, int minor, int patch) { NSOperatingSystemVersion version; version.majorVersion = major; version.minorVersion = minor; version.patchVersion = patch; return [[NSProcessInfo processInfo] isOperatingSystemAtLeastVersion:version]; }
// =========================== Preference vars ===========================
BOOL enabled;
BOOL disableInLPM;
BOOL glowEffect;
BOOL smoothAnim;
BOOL landscapeEnabled;
NSInteger lineThickness;
NSInteger dotSize;
double animTime1;
double pauseTime1;
double animTime2;
double animTime3;
double totalTime;
double lockTime;
NSUserDefaults *prefs = nil;
static void InitPrefs(void) {
if (!prefs) {
NSDictionary *defaultPrefs = @{
@"kEnabled": @YES,
@"kLPM": @NO,
@"kGlow": @YES,
@"kSmooth": @NO,
@"kLandscape": @NO,
@"kLine": @4,
@"kDot": @3,
@"kAnim1": @0.15,
@"kPause1": @0.05,
@"kAnim2": @0.15,
@"kAnim3": @0.40,
};
prefs = [[NSUserDefaults alloc] initWithSuiteName:BUNDLE];
[prefs registerDefaults:defaultPrefs];
}
}
static void UpdatePrefs() {
enabled = [prefs boolForKey: @"kEnabled"];
disableInLPM = [prefs boolForKey: @"kLPM"];
glowEffect = [prefs boolForKey: @"kGlow"];
smoothAnim = [prefs boolForKey: @"kSmooth"];
landscapeEnabled = [prefs boolForKey: @"kLandscape"];
animTime1 = [prefs floatForKey:@"kAnim1"];
pauseTime1 = [prefs floatForKey:@"kPause1"];
animTime2 = [prefs floatForKey:@"kAnim2"];
animTime3 = [prefs floatForKey:@"kAnim3"];
lineThickness = [prefs integerForKey: @"kLine"];
dotSize = [prefs integerForKey: @"kDot"];
totalTime = animTime1 + pauseTime1 + animTime2 + animTime3;
if (!landscapeEnabled)
[tvLock resetToPortrait];
}
static void PrefsChangeCallback(CFNotificationCenterRef center, void *observer, CFNotificationName name, const void *object, CFDictionaryRef userInfo) {
UpdatePrefs();
}
// =========================== Hooks ===========================
%group allVersionHooks
%hook SpringBoard
// Called when springboard is finished launching
-(void)applicationDidFinishLaunching:(id)application {
%orig;
tvLock = [[TVLock alloc] init];
}
%end
%end
%group under16hooks
%hook SBBacklightController
-(void)_animateBacklightToFactor:(float)arg1 duration:(double)arg2 source:(long long)arg3 silently:(BOOL)arg4 completion:(id)arg5 {
if(enabled &&
(!disableInLPM || (![[NSProcessInfo processInfo] isLowPowerModeEnabled])) &&
(arg1==0 && [self screenIsOn])) {
arg2 = totalTime;
[tvLock showLockAnimation:arg2];
}
%orig(arg1, arg2, arg3, arg4, arg5);
}
%end
%end
%group iOS16hooks
// Temporary fix to avoid safe mode issues. Animation only triggers when lock button is used
%hook SBSleepWakeHardwareButtonInteraction
-(void)_performSleep {
if (enabled && (!disableInLPM || (![[NSProcessInfo processInfo] isLowPowerModeEnabled]))) {
[tvLock showLockAnimation:totalTime];
// Play start sound at beginning of animation
// otherwise it'll play very late
[self _playLockSound];
// Wait for animation to complete and then lock screen
double delayInSeconds = totalTime - lockTime;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
%orig;
});
}
else {
%orig;
}
}
%end
// Also trigger animation for auto lock
%hook SBIdleTimerPolicyAggregator
-(void)idleTimerDidExpire:(id)arg1 {
if (enabled && (!disableInLPM || (![[NSProcessInfo processInfo] isLowPowerModeEnabled]))) {
[tvLock showLockAnimation:totalTime];
// Wait for animation to complete and then lock screen
double delayInSeconds = totalTime - lockTime;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC)), dispatch_get_main_queue(), ^(void){
%orig;
});
}
else {
%orig;
}
}
%end
%end
// =========================== Constructor ===========================
%ctor {
InitPrefs();
UpdatePrefs();
CFNotificationCenterAddObserver(
CFNotificationCenterGetDarwinNotifyCenter(),
NULL,
&PrefsChangeCallback,
BUNDLE_NOTIFY,
NULL,
0
);
%init(allVersionHooks);
NSOperatingSystemVersion systemVersion = [NSProcessInfo processInfo].operatingSystemVersion;
if (systemVersion.majorVersion >= 16) {
%init(iOS16hooks);
}
else {
%init(under16hooks);
}
}