]> git.saurik.com Git - cydget.git/blob - CydgetLoader.mm
Re-implement iOS 6 media controls toggle on iOS 7.
[cydget.git] / CydgetLoader.mm
1 /* Cydget - open-source AwayView plugin multiplexer
2 * Copyright (C) 2009-2014 Jay Freeman (saurik)
3 */
4
5 /* GNU General Public License, Version 3 {{{ */
6 /*
7 * Cydia is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published
9 * by the Free Software Foundation, either version 3 of the License,
10 * or (at your option) any later version.
11 *
12 * Cydia is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with Cydia. If not, see <http://www.gnu.org/licenses/>.
19 **/
20 /* }}} */
21
22 #include <CydiaSubstrate/CydiaSubstrate.h>
23
24 #include <Foundation/Foundation.h>
25 #include <UIKit/UIKit.h>
26
27 #import <SpringBoard/SBAwayController.h>
28 #import <SpringBoard/SBAwayView.h>
29 #import <SpringBoard/SBAwayWindow.h>
30 #import <SpringBoard/SpringBoard.h>
31
32 #include "Handle.hpp"
33
34 #define _trace() \
35 NSLog(@"_trace(%s:%u)@%s %zd", __FILE__, __LINE__, __FUNCTION__, active_)
36 #define _not(type) \
37 static_cast<type>(~type())
38
39 typedef void *GSEventRef;
40
41 extern "C" void UIKeyboardEnableAutomaticAppearance();
42 extern "C" void UIKeyboardDisableAutomaticAppearance();
43
44 MSClassHook(SpringBoard)
45
46 MSClassHook(SBAwayController)
47 MSClassHook(SBAwayView)
48 MSClassHook(SBAwayWindow)
49 MSClassHook(SBLockScreenManager)
50 MSClassHook(SBLockScreenView)
51 MSClassHook(SBLockScreenViewController)
52 MSClassHook(SBScreenFadeAnimationController)
53 MSClassHook(SBUserAgent)
54
55 MSInitialize {
56 if (kCFCoreFoundationVersionNumber < 800) {
57 $SBUserAgent = $SBAwayController;
58 $SBLockScreenManager = $SBAwayController;
59 $SBLockScreenView = $SBAwayView;
60 }
61 }
62
63 @interface SBLockScreenViewController : UIViewController
64 - (void) _toggleMediaControls;
65 @end
66
67 static bool menu_;
68
69 static _H<NSDictionary> settings_;
70 static _H<NSMutableArray> cydgets_;
71 static size_t active_;
72 static unsigned online_;
73
74 @interface CydgetController : NSObject {
75 }
76
77 + (NSDictionary *) currentConfiguration;
78 + (NSString *) currentPath;
79
80 @end
81
82 @implementation CydgetController
83
84 + (NSDictionary *) currentConfiguration {
85 NSDictionary *cydget([cydgets_ objectAtIndex:active_]);
86 return [cydget objectForKey:@"CYConfiguration"] ?: [cydget objectForKey:@"Configuration"];
87 }
88
89 + (NSDictionary *) currentPath {
90 NSDictionary *cydget([cydgets_ objectAtIndex:active_]);
91 return [cydget objectForKey:@"CYPath"];
92 }
93
94 @end
95
96 @interface SBUserAgent : NSObject
97 + (SBUserAgent *) sharedUserAgent;
98 - (void) enableLockScreenBundleNamed:(NSString *)bundle activationContext:(id)context;
99 - (void) disableLockScreenBundleNamed:(NSString *)bundle deactivationContext:(id)context;
100 @end
101
102 @interface UIPeripheralHost : NSObject
103 + (UIPeripheralHost *) sharedInstance;
104 + (void) _releaseSharedInstance;
105 @end
106
107 MSClassHook(UIPeripheralHost)
108
109 @interface UITextEffectsWindow : UIWindow
110 + (UIWindow *) sharedTextEffectsWindow;
111 @end
112
113 @implementation NSDictionary (CydgetLoader)
114
115 - (NSString *) cydget {
116 return [self objectForKey:@"CYPlugin"] ?: [self objectForKey:@"Plugin"];
117 }
118
119 - (void) enableCydget {
120 if (NSString *plugin = [self cydget]) {
121 ++online_;
122
123 if (kCFCoreFoundationVersionNumber < 600)
124 UIKeyboardEnableAutomaticAppearance();
125
126 [[UITextEffectsWindow sharedTextEffectsWindow] setWindowLevel:1000];
127
128 if (kCFCoreFoundationVersionNumber < 800)
129 [[$SBAwayController sharedAwayController] enableLockScreenBundleWithName:plugin];
130 else
131 [[$SBUserAgent sharedUserAgent] enableLockScreenBundleNamed:plugin activationContext:nil];
132 }
133 }
134
135 - (void) disableCydget {
136 if (NSString *plugin = [self cydget]) {
137 if (kCFCoreFoundationVersionNumber < 800)
138 [[$SBAwayController sharedAwayController] disableLockScreenBundleWithName:plugin];
139 else
140 [[$SBUserAgent sharedUserAgent] disableLockScreenBundleNamed:plugin deactivationContext:nil];
141
142 [$UIPeripheralHost _releaseSharedInstance];
143
144 if (kCFCoreFoundationVersionNumber < 600)
145 UIKeyboardDisableAutomaticAppearance();
146
147 --online_;
148 }
149 }
150
151 @end
152
153 // avoid rendering a keyboard onto the default SBAwayView while automatic keyboard is online
154 MSInstanceMessageHook0(UIView *, SBAwayView, inputView) {
155 // XXX: there is a conceptual error here
156 if (online_ == 0 && false || kCFCoreFoundationVersionNumber > 600)
157 return MSOldCall();
158
159 return [[[UIView alloc] init] autorelease];
160 }
161
162 // by default, keyboard actions are redirected to SBAwayController and press menu button
163 MSInstanceMessageHook1(void, SpringBoard, handleKeyEvent, GSEventRef, event) {
164 if (online_ == 0 || kCFCoreFoundationVersionNumber > 600)
165 return MSOldCall(event);
166
167 return MSSuperCall(event);
168 }
169
170 bool media_;
171
172 MSInstanceMessageHook0(void, SBLockScreenViewController, _toggleMediaControls) {
173 if (!media_)
174 MSOldCall();
175 }
176
177 MSInstanceMessageHook0(BOOL, SBLockScreenViewController, handleMenuButtonDoubleTap) {
178 menu_ = false;
179 BOOL value(MSOldCall());
180 if (kCFCoreFoundationVersionNumber >= 800)
181 [self _toggleMediaControls];
182 return value;
183 }
184
185 MSInstanceMessageHook0(BOOL, SBLockScreenManager, handleMenuButtonTap) {
186 media_ = true;
187 BOOL value(MSOldCall());
188 media_ = false;
189
190 if (!value && menu_) {
191 [[cydgets_ objectAtIndex:active_] disableCydget];
192 active_ = (active_ + 1) % [cydgets_ count];
193 [[cydgets_ objectAtIndex:active_] enableCydget];
194 }
195
196 return value;
197 }
198
199 void Activate_() {
200 menu_ = false;
201 [[cydgets_ objectAtIndex:active_] enableCydget];
202 }
203
204 static void Undim_(SBAwayController *self) {
205 if ([self isDimmed]) {
206 Activate_();
207 [[[self awayView] window] makeKeyWindow];
208 }
209 }
210
211 MSInstanceMessageHook1(void, SBLockScreenViewController, startLockScreenFadeInAnimationForSource, int, source) {
212 Activate_();
213 MSOldCall(source);
214 }
215
216 MSInstanceMessageHook0(void, SBAwayController, undimScreen) {
217 Undim_(self);
218 MSOldCall();
219 }
220
221 MSInstanceMessageHook1(void, SBAwayController, undimScreen, BOOL, undim) {
222 Undim_(self);
223 MSOldCall(undim);
224 }
225
226 static void Deactivate_() {
227 [[cydgets_ objectAtIndex:active_] disableCydget];
228 active_ = 0;
229 }
230
231 MSHook(void, BKSDisplayServicesSetScreenBlanked, BOOL blanked) {
232 if (blanked)
233 Deactivate_();
234 _BKSDisplayServicesSetScreenBlanked(blanked);
235 }
236
237 MSInitialize {
238 MSHookFunction("_BKSDisplayServicesSetScreenBlanked", MSHake(BKSDisplayServicesSetScreenBlanked));
239 }
240
241 MSInstanceMessageHook1(void, SBUserAgent, dimScreen, BOOL, dim) {
242 Deactivate_();
243 MSOldCall(dim);
244 }
245
246 MSInstanceMessageHook1(void, SpringBoard, _menuButtonDown, GSEventRef, event) {
247 menu_ = true;
248 MSOldCall(event);
249 }
250
251 MSInstanceMessageHook1(void, SpringBoard, menuButtonDown, GSEventRef, event) {
252 menu_ = true;
253 MSOldCall(event);
254 }
255
256 MSInstanceMessageHook1(void, SBAwayView, addGestureRecognizer, id, recognizer) {
257 if (online_ == 0 || kCFCoreFoundationVersionNumber > 600)
258 return MSOldCall(recognizer);
259 }
260
261 MSInstanceMessageHook1(void, SBAwayWindow, sendGSEvent, GSEventRef, event) {
262 if (online_ == 0 || kCFCoreFoundationVersionNumber > 600)
263 return MSOldCall(event);
264
265 return MSSuperCall(event);
266 }
267
268 #define Cydgets_ @"/System/Library/LockCydgets"
269
270 static void CydgetSetup() {
271 NSString *plist([NSString stringWithFormat:@"%@/Library/Preferences/com.saurik.Cydget.plist", NSHomeDirectory()]);
272 settings_ = [NSMutableDictionary dictionaryWithContentsOfFile:plist] ?: [NSMutableDictionary dictionary];
273
274 NSArray *cydgets([settings_ objectForKey:@"LockCydgets"] ?: [NSArray arrayWithObjects:[NSDictionary dictionaryWithObjectsAndKeys:
275 @"Welcome", @"Name", [NSNumber numberWithBool:YES], @"Active", nil
276 ], [NSDictionary dictionaryWithObjectsAndKeys:
277 @"AwayView", @"Name", [NSNumber numberWithBool:YES], @"Active", nil
278 ], nil]);
279
280 cydgets_ = [NSMutableArray arrayWithCapacity:4];
281 for (NSDictionary *cydget in cydgets)
282 if ([[cydget objectForKey:@"Active"] boolValue]) {
283 NSString *path([NSString stringWithFormat:@"%@/%@.cydget/Info.plist", Cydgets_, [cydget objectForKey:@"Name"]]);
284 if (NSMutableDictionary *info = [NSMutableDictionary dictionaryWithContentsOfFile:path]) {
285 [info setObject:path forKey:@"CYPath"];
286 [cydgets_ addObject:info];
287 }
288 }
289
290 if ([cydgets_ count] == 0)
291 cydgets_ = nil;
292 }
293
294 // XXX: this could happen while it is unlocked
295 MSInstanceMessageHook1(id, SBLockScreenView, initWithFrame, CGRect, frame) {
296 self = MSOldCall(frame);
297 CydgetSetup();
298 [[cydgets_ objectAtIndex:active_] enableCydget];
299 return self;
300 }
301
302 MSInstanceMessageHook1(void, SBAwayController, _finishedUnlockAttemptWithStatus, BOOL, status) {
303 if (status)
304 Deactivate_();
305 MSOldCall(status);
306 }
307
308 // this is called occasionally by -[SBAwayView updateInterface] and takes away our keyboard
309 MSInstanceMessageHook0(void, SBAwayView, _fixupFirstResponder) {
310 if (online_ == 0)
311 return MSOldCall();
312 }
313
314 MSInstanceMessageHook0(void, SBAwayView, updateInterface) {
315 MSOldCall();
316
317 NSDictionary *cydget([cydgets_ objectAtIndex:active_]);
318
319 NSString *background([cydget objectForKey:@"CYBackground"]);
320 if ([background isEqualToString:@"Wallpaper"]) {
321 MSIvarHook(UIView *, _backgroundView);
322 [_backgroundView setAlpha:1.0f];
323 }
324
325 if ([[cydget objectForKey:@"CYShowDateTime"] boolValue])
326 [self addDateView];
327 }