1 /* Cydget - open-source AwayView plugin multiplexer
2 * Copyright (C) 2009-2015 Jay Freeman (saurik)
5 /* GNU General Public License, Version 3 {{{ */
7 * Cydget 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 * Cydget 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 Cydget. 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(SBLockScreenViewControllerBase)
53 MSClassHook(SBScreenFadeAnimationController)
54 MSClassHook(SBUserAgent)
57 if (kCFCoreFoundationVersionNumber < 800) {
58 $SBUserAgent = $SBAwayController;
59 $SBLockScreenManager = $SBAwayController;
60 $SBLockScreenView = $SBAwayView;
61 $SBLockScreenViewControllerBase = $SBAwayController;
65 @interface SBLockScreenNowPlayingPluginController : NSObject
66 - (BOOL) isNowPlayingPluginActive;
69 @interface SBLockScreenViewController : UIViewController
70 - (SBLockScreenView *) lockScreenView;
71 - (BOOL) isShowingMediaControls;
72 - (void) _toggleMediaControls;
75 @interface SBLockScreenManager : NSObject
76 + (SBLockScreenManager *) sharedInstance;
77 - (SBLockScreenViewController *) lockScreenViewController;
82 static _H<NSDictionary> settings_;
83 static _H<NSMutableArray> cydgets_;
84 static size_t active_;
85 static unsigned online_;
86 static bool nowplaying_;
87 static bool remember_;
89 static bool NowPlaying() {
90 if (kCFCoreFoundationVersionNumber < 800)
95 SBLockScreenViewController *controller([[$SBLockScreenManager sharedInstance] lockScreenViewController]);
96 if (controller != nil) {
97 SBLockScreenNowPlayingPluginController *now(MSHookIvar<SBLockScreenNowPlayingPluginController *>(controller, "_nowPlayingController"));
98 if (now != nil && [now isNowPlayingPluginActive])
105 @interface CydgetController : NSObject {
108 + (NSDictionary *) currentConfiguration;
109 + (NSString *) currentPath;
113 @implementation CydgetController
115 + (NSDictionary *) currentConfiguration {
116 NSDictionary *cydget([cydgets_ objectAtIndex:active_]);
117 return [cydget objectForKey:@"CYConfiguration"] ?: [cydget objectForKey:@"Configuration"];
120 + (NSDictionary *) currentPath {
121 NSDictionary *cydget([cydgets_ objectAtIndex:active_]);
122 return [cydget objectForKey:@"CYPath"];
127 @interface SBUserAgent : NSObject
128 + (SBUserAgent *) sharedUserAgent;
129 - (void) enableLockScreenBundleNamed:(NSString *)bundle activationContext:(id)context;
130 - (void) disableLockScreenBundleNamed:(NSString *)bundle deactivationContext:(id)context;
133 @interface UIPeripheralHost : NSObject
134 + (UIPeripheralHost *) sharedInstance;
135 + (void) _releaseSharedInstance;
138 MSClassHook(UIPeripheralHost)
140 @interface UITextEffectsWindow : UIWindow
141 + (UIWindow *) sharedTextEffectsWindow;
144 @implementation NSDictionary (CydgetLoader)
146 - (NSString *) cydget {
147 return [self objectForKey:@"CYPlugin"] ?: [self objectForKey:@"Plugin"];
150 - (void) enableCydget {
151 if (NSString *plugin = [self cydget]) {
154 if (kCFCoreFoundationVersionNumber < 600)
155 UIKeyboardEnableAutomaticAppearance();
157 [[UITextEffectsWindow sharedTextEffectsWindow] setWindowLevel:1000];
159 if (kCFCoreFoundationVersionNumber < 800)
160 [[$SBAwayController sharedAwayController] enableLockScreenBundleWithName:plugin];
162 [[$SBUserAgent sharedUserAgent] enableLockScreenBundleNamed:plugin activationContext:nil];
166 - (void) disableCydget {
167 if (NSString *plugin = [self cydget]) {
168 if (kCFCoreFoundationVersionNumber < 800)
169 [[$SBAwayController sharedAwayController] disableLockScreenBundleWithName:plugin];
171 [[$SBUserAgent sharedUserAgent] disableLockScreenBundleNamed:plugin deactivationContext:nil];
173 [$UIPeripheralHost _releaseSharedInstance];
175 if (kCFCoreFoundationVersionNumber < 600)
176 UIKeyboardDisableAutomaticAppearance();
184 // avoid rendering a keyboard onto the default SBAwayView while automatic keyboard is online
185 MSInstanceMessageHook0(UIView *, SBAwayView, inputView) {
186 // XXX: there is a conceptual error here
187 if (online_ == 0 && false || kCFCoreFoundationVersionNumber > 600)
190 return [[[UIView alloc] init] autorelease];
193 // by default, keyboard actions are redirected to SBAwayController and press menu button
194 MSInstanceMessageHook1(void, SpringBoard, handleKeyEvent, GSEventRef, event) {
195 if (online_ == 0 || kCFCoreFoundationVersionNumber > 600)
196 return MSOldCall(event);
198 return MSSuperCall(event);
203 MSInstanceMessageHook0(void, SBLockScreenViewController, _toggleMediaControls) {
208 MSInstanceMessageHook0(BOOL, SBLockScreenViewController, handleMenuButtonDoubleTap) {
210 BOOL value(MSOldCall());
211 if (kCFCoreFoundationVersionNumber >= 800)
212 [self _toggleMediaControls];
216 MSInstanceMessageHook0(BOOL, SBLockScreenViewControllerBase, handleMenuButtonTap) {
218 BOOL value(MSOldCall());
221 if (!value && menu_) {
222 if (active_ != _not(size_t))
223 [[cydgets_ objectAtIndex:active_] disableCydget];
224 active_ = (active_ + 1) % [cydgets_ count];
225 [[cydgets_ objectAtIndex:active_] enableCydget];
226 // XXX: or siri doesn't disappear correctly
227 return kCFCoreFoundationVersionNumber >= 800 ? value : YES;
236 for (active_ = 0; active_ != [cydgets_ count]; ++active_)
237 if ([[[cydgets_ objectAtIndex:active_] objectForKey:@"CYName"] isEqualToString:@"AwayView"])
239 if (active_ == [cydgets_ count])
240 active_ = _not(size_t);
242 if (active_ != _not(size_t))
243 [[cydgets_ objectAtIndex:active_] enableCydget];
246 static void Undim_(SBAwayController *self) {
247 if ([self isDimmed]) {
249 [[[self awayView] window] makeKeyWindow];
253 MSInstanceMessageHook1(void, SBLockScreenViewController, startLockScreenFadeInAnimationForSource, int, source) {
258 MSInstanceMessageHook0(void, SBAwayController, undimScreen) {
263 MSInstanceMessageHook1(void, SBAwayController, undimScreen, BOOL, undim) {
268 static void Deactivate_() {
269 if (active_ != _not(size_t))
270 [[cydgets_ objectAtIndex:active_] disableCydget];
275 MSHook(void, BKSDisplayServicesSetScreenBlanked, BOOL blanked) {
278 _BKSDisplayServicesSetScreenBlanked(blanked);
282 MSHookFunction("_BKSDisplayServicesSetScreenBlanked", MSHake(BKSDisplayServicesSetScreenBlanked));
285 MSInstanceMessageHook1(void, SBUserAgent, dimScreen, BOOL, dim) {
290 MSInstanceMessageHook0(void, SpringBoard, _menuButtonWasHeld) {
295 MSInstanceMessageHook1(void, SpringBoard, _menuButtonDown, GSEventRef, event) {
300 MSInstanceMessageHook1(void, SpringBoard, menuButtonDown, GSEventRef, event) {
305 MSInstanceMessageHook1(void, SBAwayView, addGestureRecognizer, id, recognizer) {
306 if (online_ == 0 || kCFCoreFoundationVersionNumber > 600)
307 return MSOldCall(recognizer);
310 MSInstanceMessageHook1(void, SBAwayWindow, sendGSEvent, GSEventRef, event) {
311 if (online_ == 0 || kCFCoreFoundationVersionNumber > 600)
312 return MSOldCall(event);
314 return MSSuperCall(event);
317 MSInstanceMessageHook2(void, SBLockScreenView, setMediaControlsHidden,forRequester, BOOL, hidden, id, requester) {
318 MSOldCall(hidden, requester);
319 UIView *&_mediaControlsContainerView(MSHookIvar<UIView *>(self, "_mediaControlsContainerView"));
320 UIView *&_mediaControlsView(MSHookIvar<UIView *>(self, "_mediaControlsView"));
321 if (&_mediaControlsContainerView != NULL && &_mediaControlsView != NULL)
322 [_mediaControlsContainerView setUserInteractionEnabled:([_mediaControlsView alpha] != 0)];
325 #define Cydgets_ @"/System/Library/LockCydgets"
327 static void CydgetSetup() {
328 NSString *plist([NSString stringWithFormat:@"%@/Library/Preferences/com.saurik.Cydget.plist", NSHomeDirectory()]);
329 settings_ = [NSMutableDictionary dictionaryWithContentsOfFile:plist] ?: [NSMutableDictionary dictionary];
331 nowplaying_ = [[settings_ objectForKey:@"NowPlaying"] boolValue];
332 remember_ = [[settings_ objectForKey:@"RememberPosition"] boolValue];
334 NSArray *cydgets([settings_ objectForKey:@"LockCydgets"] ?: [NSArray arrayWithObjects:[NSDictionary dictionaryWithObjectsAndKeys:
335 @"Welcome", @"Name", [NSNumber numberWithBool:YES], @"Active", nil
336 ], [NSDictionary dictionaryWithObjectsAndKeys:
337 @"AwayView", @"Name", [NSNumber numberWithBool:YES], @"Active", nil
340 cydgets_ = [NSMutableArray arrayWithCapacity:4];
341 for (NSDictionary *cydget in cydgets)
342 if ([[cydget objectForKey:@"Active"] boolValue]) {
343 NSString *name([cydget objectForKey:@"Name"]);
344 NSString *path([NSString stringWithFormat:@"%@/%@.cydget/Info.plist", Cydgets_, name]);
345 if (NSMutableDictionary *info = [NSMutableDictionary dictionaryWithContentsOfFile:path]) {
346 [info setObject:name forKey:@"CYName"];
347 [info setObject:path forKey:@"CYPath"];
348 [cydgets_ addObject:info];
352 if ([cydgets_ count] == 0)
356 // XXX: this could happen while it is unlocked
357 MSInstanceMessageHook1(id, SBLockScreenView, initWithFrame, CGRect, frame) {
358 self = MSOldCall(frame);
360 static bool loaded(false);
367 // XXX: this is just wrong, I don't know what to do :(
368 else if (kCFCoreFoundationVersionNumber < 800)
374 MSInstanceMessageHook1(void, SBAwayController, _finishedUnlockAttemptWithStatus, BOOL, status) {
380 // this is called occasionally by -[SBAwayView updateInterface] and takes away our keyboard
381 MSInstanceMessageHook0(void, SBAwayView, _fixupFirstResponder) {
386 MSInstanceMessageHook0(void, SBAwayView, updateInterface) {
389 NSDictionary *cydget([cydgets_ objectAtIndex:active_]);
391 NSString *background([cydget objectForKey:@"CYBackground"]);
392 if ([background isEqualToString:@"Wallpaper"]) {
393 MSIvarHook(UIView *, _backgroundView);
394 [_backgroundView setAlpha:1.0f];
397 if ([[cydget objectForKey:@"CYShowDateTime"] boolValue])