1 /* Cydget - open-source AwayView plugin multiplexer
2 * Copyright (C) 2009-2014 Jay Freeman (saurik)
5 /* GNU General Public License, Version 3 {{{ */
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.
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.
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/>.
22 #include <CydiaSubstrate/CydiaSubstrate.h>
24 #include <Foundation/Foundation.h>
25 #include <UIKit/UIKit.h>
27 #import <SpringBoard/SBAwayController.h>
28 #import <SpringBoard/SBAwayView.h>
29 #import <SpringBoard/SBAwayWindow.h>
30 #import <SpringBoard/SpringBoard.h>
35 NSLog(@"_trace(%s:%u)@%s %zd", __FILE__, __LINE__, __FUNCTION__, active_)
37 static_cast<type>(~type())
39 typedef void *GSEventRef;
41 extern "C" void UIKeyboardEnableAutomaticAppearance();
42 extern "C" void UIKeyboardDisableAutomaticAppearance();
44 MSClassHook(SpringBoard)
46 MSClassHook(SBAwayController)
47 MSClassHook(SBAwayView)
48 MSClassHook(SBAwayWindow)
49 MSClassHook(SBLockScreenManager)
50 MSClassHook(SBLockScreenView)
51 MSClassHook(SBLockScreenViewController)
52 MSClassHook(SBScreenFadeAnimationController)
53 MSClassHook(SBUserAgent)
56 if (kCFCoreFoundationVersionNumber < 800) {
57 $SBUserAgent = $SBAwayController;
58 $SBLockScreenManager = $SBAwayController;
59 $SBLockScreenView = $SBAwayView;
63 @interface SBLockScreenViewController : UIViewController
64 - (void) _toggleMediaControls;
69 static _H<NSDictionary> settings_;
70 static _H<NSMutableArray> cydgets_;
71 static size_t active_;
72 static unsigned online_;
74 @interface CydgetController : NSObject {
77 + (NSDictionary *) currentConfiguration;
78 + (NSString *) currentPath;
82 @implementation CydgetController
84 + (NSDictionary *) currentConfiguration {
85 NSDictionary *cydget([cydgets_ objectAtIndex:active_]);
86 return [cydget objectForKey:@"CYConfiguration"] ?: [cydget objectForKey:@"Configuration"];
89 + (NSDictionary *) currentPath {
90 NSDictionary *cydget([cydgets_ objectAtIndex:active_]);
91 return [cydget objectForKey:@"CYPath"];
96 @interface SBUserAgent : NSObject
97 + (SBUserAgent *) sharedUserAgent;
98 - (void) enableLockScreenBundleNamed:(NSString *)bundle activationContext:(id)context;
99 - (void) disableLockScreenBundleNamed:(NSString *)bundle deactivationContext:(id)context;
102 @interface UIPeripheralHost : NSObject
103 + (UIPeripheralHost *) sharedInstance;
104 + (void) _releaseSharedInstance;
107 MSClassHook(UIPeripheralHost)
109 @interface UITextEffectsWindow : UIWindow
110 + (UIWindow *) sharedTextEffectsWindow;
113 @implementation NSDictionary (CydgetLoader)
115 - (NSString *) cydget {
116 return [self objectForKey:@"CYPlugin"] ?: [self objectForKey:@"Plugin"];
119 - (void) enableCydget {
120 if (NSString *plugin = [self cydget]) {
123 if (kCFCoreFoundationVersionNumber < 600)
124 UIKeyboardEnableAutomaticAppearance();
126 [[UITextEffectsWindow sharedTextEffectsWindow] setWindowLevel:1000];
128 if (kCFCoreFoundationVersionNumber < 800)
129 [[$SBAwayController sharedAwayController] enableLockScreenBundleWithName:plugin];
131 [[$SBUserAgent sharedUserAgent] enableLockScreenBundleNamed:plugin activationContext:nil];
135 - (void) disableCydget {
136 if (NSString *plugin = [self cydget]) {
137 if (kCFCoreFoundationVersionNumber < 800)
138 [[$SBAwayController sharedAwayController] disableLockScreenBundleWithName:plugin];
140 [[$SBUserAgent sharedUserAgent] disableLockScreenBundleNamed:plugin deactivationContext:nil];
142 [$UIPeripheralHost _releaseSharedInstance];
144 if (kCFCoreFoundationVersionNumber < 600)
145 UIKeyboardDisableAutomaticAppearance();
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)
159 return [[[UIView alloc] init] autorelease];
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);
167 return MSSuperCall(event);
172 MSInstanceMessageHook0(void, SBLockScreenViewController, _toggleMediaControls) {
177 MSInstanceMessageHook0(BOOL, SBLockScreenViewController, handleMenuButtonDoubleTap) {
179 BOOL value(MSOldCall());
180 if (kCFCoreFoundationVersionNumber >= 800)
181 [self _toggleMediaControls];
185 MSInstanceMessageHook0(BOOL, SBLockScreenManager, handleMenuButtonTap) {
187 BOOL value(MSOldCall());
190 if (!value && menu_) {
191 [[cydgets_ objectAtIndex:active_] disableCydget];
192 active_ = (active_ + 1) % [cydgets_ count];
193 [[cydgets_ objectAtIndex:active_] enableCydget];
194 // XXX: or siri doesn't disappear correctly
195 return kCFCoreFoundationVersionNumber >= 800 ? value : YES;
203 [[cydgets_ objectAtIndex:active_] enableCydget];
206 static void Undim_(SBAwayController *self) {
207 if ([self isDimmed]) {
209 [[[self awayView] window] makeKeyWindow];
213 MSInstanceMessageHook1(void, SBLockScreenViewController, startLockScreenFadeInAnimationForSource, int, source) {
218 MSInstanceMessageHook0(void, SBAwayController, undimScreen) {
223 MSInstanceMessageHook1(void, SBAwayController, undimScreen, BOOL, undim) {
228 static void Deactivate_() {
229 [[cydgets_ objectAtIndex:active_] disableCydget];
233 MSHook(void, BKSDisplayServicesSetScreenBlanked, BOOL blanked) {
236 _BKSDisplayServicesSetScreenBlanked(blanked);
240 MSHookFunction("_BKSDisplayServicesSetScreenBlanked", MSHake(BKSDisplayServicesSetScreenBlanked));
243 MSInstanceMessageHook1(void, SBUserAgent, dimScreen, BOOL, dim) {
248 MSInstanceMessageHook0(void, SpringBoard, _menuButtonWasHeld) {
253 MSInstanceMessageHook1(void, SpringBoard, _menuButtonDown, GSEventRef, event) {
258 MSInstanceMessageHook1(void, SpringBoard, menuButtonDown, GSEventRef, event) {
263 MSInstanceMessageHook1(void, SBAwayView, addGestureRecognizer, id, recognizer) {
264 if (online_ == 0 || kCFCoreFoundationVersionNumber > 600)
265 return MSOldCall(recognizer);
268 MSInstanceMessageHook1(void, SBAwayWindow, sendGSEvent, GSEventRef, event) {
269 if (online_ == 0 || kCFCoreFoundationVersionNumber > 600)
270 return MSOldCall(event);
272 return MSSuperCall(event);
275 #define Cydgets_ @"/System/Library/LockCydgets"
277 static void CydgetSetup() {
278 NSString *plist([NSString stringWithFormat:@"%@/Library/Preferences/com.saurik.Cydget.plist", NSHomeDirectory()]);
279 settings_ = [NSMutableDictionary dictionaryWithContentsOfFile:plist] ?: [NSMutableDictionary dictionary];
281 NSArray *cydgets([settings_ objectForKey:@"LockCydgets"] ?: [NSArray arrayWithObjects:[NSDictionary dictionaryWithObjectsAndKeys:
282 @"Welcome", @"Name", [NSNumber numberWithBool:YES], @"Active", nil
283 ], [NSDictionary dictionaryWithObjectsAndKeys:
284 @"AwayView", @"Name", [NSNumber numberWithBool:YES], @"Active", nil
287 cydgets_ = [NSMutableArray arrayWithCapacity:4];
288 for (NSDictionary *cydget in cydgets)
289 if ([[cydget objectForKey:@"Active"] boolValue]) {
290 NSString *path([NSString stringWithFormat:@"%@/%@.cydget/Info.plist", Cydgets_, [cydget objectForKey:@"Name"]]);
291 if (NSMutableDictionary *info = [NSMutableDictionary dictionaryWithContentsOfFile:path]) {
292 [info setObject:path forKey:@"CYPath"];
293 [cydgets_ addObject:info];
297 if ([cydgets_ count] == 0)
301 // XXX: this could happen while it is unlocked
302 MSInstanceMessageHook1(id, SBLockScreenView, initWithFrame, CGRect, frame) {
303 self = MSOldCall(frame);
305 [[cydgets_ objectAtIndex:active_] enableCydget];
309 MSInstanceMessageHook1(void, SBAwayController, _finishedUnlockAttemptWithStatus, BOOL, status) {
315 // this is called occasionally by -[SBAwayView updateInterface] and takes away our keyboard
316 MSInstanceMessageHook0(void, SBAwayView, _fixupFirstResponder) {
321 MSInstanceMessageHook0(void, SBAwayView, updateInterface) {
324 NSDictionary *cydget([cydgets_ objectAtIndex:active_]);
326 NSString *background([cydget objectForKey:@"CYBackground"]);
327 if ([background isEqualToString:@"Wallpaper"]) {
328 MSIvarHook(UIView *, _backgroundView);
329 [_backgroundView setAlpha:1.0f];
332 if ([[cydget objectForKey:@"CYShowDateTime"] boolValue])