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;
65 static _H<NSDictionary> settings_;
66 static _H<NSMutableArray> cydgets_;
67 static size_t active_;
68 static unsigned online_;
70 @interface CydgetController : NSObject {
73 + (NSDictionary *) currentConfiguration;
77 @implementation CydgetController
79 + (NSDictionary *) currentConfiguration {
80 NSDictionary *cydget([cydgets_ objectAtIndex:active_]);
81 return [cydget objectForKey:@"CYConfiguration"] ?: [cydget objectForKey:@"Configuration"];
86 @interface SBUserAgent : NSObject
87 + (SBUserAgent *) sharedUserAgent;
88 - (void) enableLockScreenBundleNamed:(NSString *)bundle activationContext:(id)context;
89 - (void) disableLockScreenBundleNamed:(NSString *)bundle deactivationContext:(id)context;
92 @interface UIPeripheralHost : NSObject
93 + (UIPeripheralHost *) sharedInstance;
94 + (void) _releaseSharedInstance;
97 MSClassHook(UIPeripheralHost)
99 @interface UITextEffectsWindow : UIWindow
100 + (UIWindow *) sharedTextEffectsWindow;
103 @implementation NSDictionary (CydgetLoader)
105 - (NSString *) cydget {
106 return [self objectForKey:@"CYPlugin"] ?: [self objectForKey:@"Plugin"];
109 - (void) enableCydget {
110 if (NSString *plugin = [self cydget]) {
113 if (kCFCoreFoundationVersionNumber < 600)
114 UIKeyboardEnableAutomaticAppearance();
116 [[UITextEffectsWindow sharedTextEffectsWindow] setWindowLevel:1000];
118 if (kCFCoreFoundationVersionNumber < 800)
119 [[$SBAwayController sharedAwayController] enableLockScreenBundleWithName:plugin];
121 [[$SBUserAgent sharedUserAgent] enableLockScreenBundleNamed:plugin activationContext:nil];
125 - (void) disableCydget {
126 if (NSString *plugin = [self cydget]) {
127 if (kCFCoreFoundationVersionNumber < 800)
128 [[$SBAwayController sharedAwayController] disableLockScreenBundleWithName:plugin];
130 [[$SBUserAgent sharedUserAgent] disableLockScreenBundleNamed:plugin deactivationContext:nil];
132 [$UIPeripheralHost _releaseSharedInstance];
134 if (kCFCoreFoundationVersionNumber < 600)
135 UIKeyboardDisableAutomaticAppearance();
143 // avoid rendering a keyboard onto the default SBAwayView while automatic keyboard is online
144 MSInstanceMessageHook0(UIView *, SBAwayView, inputView) {
145 // XXX: there is a conceptual error here
146 if (online_ == 0 && false || kCFCoreFoundationVersionNumber > 600)
149 return [[[UIView alloc] init] autorelease];
152 // by default, keyboard actions are redirected to SBAwayController and press menu button
153 MSInstanceMessageHook1(void, SpringBoard, handleKeyEvent, GSEventRef, event) {
154 if (online_ == 0 || kCFCoreFoundationVersionNumber > 600)
155 return MSOldCall(event);
157 return MSSuperCall(event);
160 MSInstanceMessageHook0(BOOL, SBLockScreenManager, handleMenuButtonTap) {
161 if (!MSOldCall() && menu_) {
162 [[cydgets_ objectAtIndex:active_] disableCydget];
163 active_ = (active_ + 1) % [cydgets_ count];
164 [[cydgets_ objectAtIndex:active_] enableCydget];
172 [[cydgets_ objectAtIndex:active_] enableCydget];
175 static void Undim_(SBAwayController *self) {
176 if ([self isDimmed]) {
178 [[[self awayView] window] makeKeyWindow];
182 MSInstanceMessageHook1(void, SBLockScreenViewController, startLockScreenFadeInAnimationForSource, int, source) {
187 MSInstanceMessageHook0(void, SBAwayController, undimScreen) {
192 MSInstanceMessageHook1(void, SBAwayController, undimScreen, BOOL, undim) {
197 static void Deactivate_() {
198 [[cydgets_ objectAtIndex:active_] disableCydget];
202 MSHook(void, BKSDisplayServicesSetScreenBlanked, BOOL blanked) {
205 _BKSDisplayServicesSetScreenBlanked(blanked);
209 MSHookFunction("_BKSDisplayServicesSetScreenBlanked", MSHake(BKSDisplayServicesSetScreenBlanked));
212 MSInstanceMessageHook1(void, SBUserAgent, dimScreen, BOOL, dim) {
217 MSInstanceMessageHook1(void, SpringBoard, _menuButtonDown, GSEventRef, event) {
222 MSInstanceMessageHook1(void, SpringBoard, menuButtonDown, GSEventRef, event) {
227 MSInstanceMessageHook1(void, SBAwayView, addGestureRecognizer, id, recognizer) {
228 if (online_ == 0 || kCFCoreFoundationVersionNumber > 600)
229 return MSOldCall(recognizer);
232 MSInstanceMessageHook1(void, SBAwayWindow, sendGSEvent, GSEventRef, event) {
233 if (online_ == 0 || kCFCoreFoundationVersionNumber > 600)
234 return MSOldCall(event);
236 return MSSuperCall(event);
239 #define Cydgets_ @"/System/Library/LockCydgets"
241 static void CydgetSetup() {
242 NSString *plist([NSString stringWithFormat:@"%@/Library/Preferences/com.saurik.Cydget.plist", NSHomeDirectory()]);
243 settings_ = [NSMutableDictionary dictionaryWithContentsOfFile:plist] ?: [NSMutableDictionary dictionary];
245 NSArray *cydgets([settings_ objectForKey:@"LockCydgets"] ?: [NSArray arrayWithObjects:[NSDictionary dictionaryWithObjectsAndKeys:
246 @"Welcome", @"Name", [NSNumber numberWithBool:YES], @"Active", nil
247 ], [NSDictionary dictionaryWithObjectsAndKeys:
248 @"AwayView", @"Name", [NSNumber numberWithBool:YES], @"Active", nil
251 cydgets_ = [NSMutableArray arrayWithCapacity:4];
252 for (NSDictionary *cydget in cydgets)
253 if ([[cydget objectForKey:@"Active"] boolValue])
254 if (NSDictionary *info = [NSDictionary dictionaryWithContentsOfFile:[NSString stringWithFormat:@"%@/%@.cydget/Info.plist", Cydgets_, [cydget objectForKey:@"Name"]]])
255 [cydgets_ addObject:info];
257 if ([cydgets_ count] == 0)
261 // XXX: this could happen while it is unlocked
262 MSInstanceMessageHook1(id, SBLockScreenView, initWithFrame, CGRect, frame) {
263 self = MSOldCall(frame);
265 [[cydgets_ objectAtIndex:active_] enableCydget];
269 MSInstanceMessageHook1(void, SBAwayController, _finishedUnlockAttemptWithStatus, BOOL, status) {
275 // this is called occasionally by -[SBAwayView updateInterface] and takes away our keyboard
276 MSInstanceMessageHook0(void, SBAwayView, _fixupFirstResponder) {
281 MSInstanceMessageHook0(void, SBAwayView, updateInterface) {
284 NSDictionary *cydget([cydgets_ objectAtIndex:active_]);
286 NSString *background([cydget objectForKey:@"CYBackground"]);
287 if ([background isEqualToString:@"Wallpaper"]) {
288 MSIvarHook(UIView *, _backgroundView);
289 [_backgroundView setAlpha:1.0f];
292 if ([[cydget objectForKey:@"CYShowDateTime"] boolValue])