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 SBLockScreenNowPlayingPluginController : NSObject
64 - (BOOL) isNowPlayingPluginActive;
67 @interface SBLockScreenViewController : UIViewController
68 - (SBLockScreenView *) lockScreenView;
69 - (BOOL) isShowingMediaControls;
70 - (void) _toggleMediaControls;
73 @interface SBLockScreenManager : NSObject
74 + (SBLockScreenManager *) sharedInstance;
75 - (SBLockScreenViewController *) lockScreenViewController;
80 static _H<NSDictionary> settings_;
81 static _H<NSMutableArray> cydgets_;
82 static size_t active_;
83 static unsigned online_;
84 static bool nowplaying_;
86 static bool NowPlaying() {
87 if (kCFCoreFoundationVersionNumber < 800)
92 SBLockScreenViewController *controller([[$SBLockScreenManager sharedInstance] lockScreenViewController]);
93 if (controller != nil) {
94 SBLockScreenNowPlayingPluginController *now(MSHookIvar<SBLockScreenNowPlayingPluginController *>(controller, "_nowPlayingController"));
95 if (now != nil && [now isNowPlayingPluginActive])
102 @interface CydgetController : NSObject {
105 + (NSDictionary *) currentConfiguration;
106 + (NSString *) currentPath;
110 @implementation CydgetController
112 + (NSDictionary *) currentConfiguration {
113 NSDictionary *cydget([cydgets_ objectAtIndex:active_]);
114 return [cydget objectForKey:@"CYConfiguration"] ?: [cydget objectForKey:@"Configuration"];
117 + (NSDictionary *) currentPath {
118 NSDictionary *cydget([cydgets_ objectAtIndex:active_]);
119 return [cydget objectForKey:@"CYPath"];
124 @interface SBUserAgent : NSObject
125 + (SBUserAgent *) sharedUserAgent;
126 - (void) enableLockScreenBundleNamed:(NSString *)bundle activationContext:(id)context;
127 - (void) disableLockScreenBundleNamed:(NSString *)bundle deactivationContext:(id)context;
130 @interface UIPeripheralHost : NSObject
131 + (UIPeripheralHost *) sharedInstance;
132 + (void) _releaseSharedInstance;
135 MSClassHook(UIPeripheralHost)
137 @interface UITextEffectsWindow : UIWindow
138 + (UIWindow *) sharedTextEffectsWindow;
141 @implementation NSDictionary (CydgetLoader)
143 - (NSString *) cydget {
144 return [self objectForKey:@"CYPlugin"] ?: [self objectForKey:@"Plugin"];
147 - (void) enableCydget {
148 if (NSString *plugin = [self cydget]) {
151 if (kCFCoreFoundationVersionNumber < 600)
152 UIKeyboardEnableAutomaticAppearance();
154 [[UITextEffectsWindow sharedTextEffectsWindow] setWindowLevel:1000];
156 if (kCFCoreFoundationVersionNumber < 800)
157 [[$SBAwayController sharedAwayController] enableLockScreenBundleWithName:plugin];
159 [[$SBUserAgent sharedUserAgent] enableLockScreenBundleNamed:plugin activationContext:nil];
163 - (void) disableCydget {
164 if (NSString *plugin = [self cydget]) {
165 if (kCFCoreFoundationVersionNumber < 800)
166 [[$SBAwayController sharedAwayController] disableLockScreenBundleWithName:plugin];
168 [[$SBUserAgent sharedUserAgent] disableLockScreenBundleNamed:plugin deactivationContext:nil];
170 [$UIPeripheralHost _releaseSharedInstance];
172 if (kCFCoreFoundationVersionNumber < 600)
173 UIKeyboardDisableAutomaticAppearance();
181 // avoid rendering a keyboard onto the default SBAwayView while automatic keyboard is online
182 MSInstanceMessageHook0(UIView *, SBAwayView, inputView) {
183 // XXX: there is a conceptual error here
184 if (online_ == 0 && false || kCFCoreFoundationVersionNumber > 600)
187 return [[[UIView alloc] init] autorelease];
190 // by default, keyboard actions are redirected to SBAwayController and press menu button
191 MSInstanceMessageHook1(void, SpringBoard, handleKeyEvent, GSEventRef, event) {
192 if (online_ == 0 || kCFCoreFoundationVersionNumber > 600)
193 return MSOldCall(event);
195 return MSSuperCall(event);
200 MSInstanceMessageHook0(void, SBLockScreenViewController, _toggleMediaControls) {
205 MSInstanceMessageHook0(BOOL, SBLockScreenViewController, handleMenuButtonDoubleTap) {
207 BOOL value(MSOldCall());
208 if (kCFCoreFoundationVersionNumber >= 800)
209 [self _toggleMediaControls];
213 MSInstanceMessageHook0(BOOL, SBLockScreenManager, handleMenuButtonTap) {
215 BOOL value(MSOldCall());
218 if (!value && menu_) {
219 if (active_ != _not(size_t))
220 [[cydgets_ objectAtIndex:active_] disableCydget];
221 active_ = (active_ + 1) % [cydgets_ count];
222 [[cydgets_ objectAtIndex:active_] enableCydget];
223 // XXX: or siri doesn't disappear correctly
224 return kCFCoreFoundationVersionNumber >= 800 ? value : YES;
233 for (active_ = 0; active_ != [cydgets_ count]; ++active_)
234 if ([[[cydgets_ objectAtIndex:active_] objectForKey:@"CYName"] isEqualToString:@"AwayView"])
236 if (active_ == [cydgets_ count])
237 active_ = _not(size_t);
239 if (active_ != _not(size_t))
240 [[cydgets_ objectAtIndex:active_] enableCydget];
243 static void Undim_(SBAwayController *self) {
244 if ([self isDimmed]) {
246 [[[self awayView] window] makeKeyWindow];
250 MSInstanceMessageHook1(void, SBLockScreenViewController, startLockScreenFadeInAnimationForSource, int, source) {
255 MSInstanceMessageHook0(void, SBAwayController, undimScreen) {
260 MSInstanceMessageHook1(void, SBAwayController, undimScreen, BOOL, undim) {
265 static void Deactivate_() {
266 if (active_ != _not(size_t))
267 [[cydgets_ objectAtIndex:active_] disableCydget];
271 MSHook(void, BKSDisplayServicesSetScreenBlanked, BOOL blanked) {
274 _BKSDisplayServicesSetScreenBlanked(blanked);
278 MSHookFunction("_BKSDisplayServicesSetScreenBlanked", MSHake(BKSDisplayServicesSetScreenBlanked));
281 MSInstanceMessageHook1(void, SBUserAgent, dimScreen, BOOL, dim) {
286 MSInstanceMessageHook0(void, SpringBoard, _menuButtonWasHeld) {
291 MSInstanceMessageHook1(void, SpringBoard, _menuButtonDown, GSEventRef, event) {
296 MSInstanceMessageHook1(void, SpringBoard, menuButtonDown, GSEventRef, event) {
301 MSInstanceMessageHook1(void, SBAwayView, addGestureRecognizer, id, recognizer) {
302 if (online_ == 0 || kCFCoreFoundationVersionNumber > 600)
303 return MSOldCall(recognizer);
306 MSInstanceMessageHook1(void, SBAwayWindow, sendGSEvent, GSEventRef, event) {
307 if (online_ == 0 || kCFCoreFoundationVersionNumber > 600)
308 return MSOldCall(event);
310 return MSSuperCall(event);
313 #define Cydgets_ @"/System/Library/LockCydgets"
315 static void CydgetSetup() {
316 NSString *plist([NSString stringWithFormat:@"%@/Library/Preferences/com.saurik.Cydget.plist", NSHomeDirectory()]);
317 settings_ = [NSMutableDictionary dictionaryWithContentsOfFile:plist] ?: [NSMutableDictionary dictionary];
319 nowplaying_ = [[settings_ objectForKey:@"NowPlaying"] boolValue];
321 NSArray *cydgets([settings_ objectForKey:@"LockCydgets"] ?: [NSArray arrayWithObjects:[NSDictionary dictionaryWithObjectsAndKeys:
322 @"Welcome", @"Name", [NSNumber numberWithBool:YES], @"Active", nil
323 ], [NSDictionary dictionaryWithObjectsAndKeys:
324 @"AwayView", @"Name", [NSNumber numberWithBool:YES], @"Active", nil
327 cydgets_ = [NSMutableArray arrayWithCapacity:4];
328 for (NSDictionary *cydget in cydgets)
329 if ([[cydget objectForKey:@"Active"] boolValue]) {
330 NSString *name([cydget objectForKey:@"Name"]);
331 NSString *path([NSString stringWithFormat:@"%@/%@.cydget/Info.plist", Cydgets_, name]);
332 if (NSMutableDictionary *info = [NSMutableDictionary dictionaryWithContentsOfFile:path]) {
333 [info setObject:name forKey:@"CYName"];
334 [info setObject:path forKey:@"CYPath"];
335 [cydgets_ addObject:info];
339 if ([cydgets_ count] == 0)
343 // XXX: this could happen while it is unlocked
344 MSInstanceMessageHook1(id, SBLockScreenView, initWithFrame, CGRect, frame) {
345 self = MSOldCall(frame);
351 MSInstanceMessageHook1(void, SBAwayController, _finishedUnlockAttemptWithStatus, BOOL, status) {
357 // this is called occasionally by -[SBAwayView updateInterface] and takes away our keyboard
358 MSInstanceMessageHook0(void, SBAwayView, _fixupFirstResponder) {
363 MSInstanceMessageHook0(void, SBAwayView, updateInterface) {
366 NSDictionary *cydget([cydgets_ objectAtIndex:active_]);
368 NSString *background([cydget objectForKey:@"CYBackground"]);
369 if ([background isEqualToString:@"Wallpaper"]) {
370 MSIvarHook(UIView *, _backgroundView);
371 [_backgroundView setAlpha:1.0f];
374 if ([[cydget objectForKey:@"CYShowDateTime"] boolValue])