1 /* Cydget - open-source AwayView plugin multiplexer
2 * Copyright (C) 2009-2011 Jay Freeman (saurik)
6 * Redistribution and use in source and binary
7 * forms, with or without modification, are permitted
8 * provided that the following conditions are met:
10 * 1. Redistributions of source code must retain the
11 * above copyright notice, this list of conditions
12 * and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the
14 * above copyright notice, this list of conditions
15 * and the following disclaimer in the documentation
16 * and/or other materials provided with the
18 * 3. The name of the author may not be used to endorse
19 * or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS''
23 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
24 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
25 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE
27 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
28 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
29 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
30 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
32 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
33 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
34 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
35 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 #include <substrate.h>
40 #include <Foundation/Foundation.h>
41 #include <GraphicsServices/GraphicsServices.h>
42 #include <UIKit/UIKit.h>
44 #import <SpringBoard/SBAwayController.h>
45 #import <SpringBoard/SBAwayView.h>
46 #import <SpringBoard/SBAwayWindow.h>
47 #import <SpringBoard/SpringBoard.h>
49 MSClassHook(SpringBoard)
51 MSClassHook(SBAwayController)
52 MSClassHook(SBAwayView)
53 MSClassHook(SBAwayViewController)
54 MSClassHook(SBAwayWindow)
57 NSLog(@"_trace(%s:%u)@%s %d", __FILE__, __LINE__, __FUNCTION__, active_)
59 static_cast<type>(~type())
63 static _H<NSArray> settings_;
64 static _H<NSArray> cydgets_;
65 static size_t active_;
66 static unsigned online_;
68 @interface CydgetController : NSObject {
71 + (NSDictionary *) currentConfiguration;
75 @implementation CydgetController
77 + (NSDictionary *) currentConfiguration {
78 NSDictionary *cydget([cydgets_ objectAtIndex:active_]);
79 return [cydget objectForKey:@"CYConfiguration"] ?: [cydget objectForKey:@"Configuration"];
84 @interface NSDictionary (Cydgets)
85 - (void) enableCydget:(SBAwayController *)away;
86 - (void) disableCydget:(SBAwayController *)away;
89 @interface UIPeripheralHost : NSObject
90 + (UIPeripheralHost *) sharedInstance;
91 + (void) _releaseSharedInstance;
92 - (void) createAutomaticKeyboardIfNeeded;
95 MSClassHook(UIPeripheralHost)
97 @interface UITextEffectsWindow : UIWindow
98 + (UIWindow *) sharedTextEffectsWindow;
101 @implementation NSDictionary (Cydgets)
103 - (void) enableCydget:(SBAwayController *)away {
104 if (NSString *plugin = [self objectForKey:@"CYPlugin"] ?: [self objectForKey:@"Plugin"]) {
106 UIKeyboardEnableAutomaticAppearance();
108 [[$UIPeripheralHost sharedInstance] createAutomaticKeyboardIfNeeded];
109 [[UITextEffectsWindow sharedTextEffectsWindow] setWindowLevel:1000];
111 [away enableLockScreenBundleWithName:plugin];
115 - (void) disableCydget:(SBAwayController *)away {
116 if (NSString *plugin = [self objectForKey:@"CYPlugin"] ?: [self objectForKey:@"Plugin"]) {
117 [away disableLockScreenBundleWithName:plugin];
119 [$UIPeripheralHost _releaseSharedInstance];
121 UIKeyboardDisableAutomaticAppearance();
128 // avoid rendering a keyboard onto the default SBAwayView while automatic keyboard is online
129 MSInstanceMessageHook0(UIView *, SBAwayView, inputView) {
133 return [[[UIView alloc] init] autorelease];
136 // by default, keyboard actions are redirected to SBAwayController and press menu button
137 MSInstanceMessageHook1(void, SpringBoard, handleKeyEvent, GSEventRef, event) {
139 return MSOldCall(event);
141 return MSSuperCall(event);
144 MSInstanceMessageHook0(BOOL, SBAwayController, handleMenuButtonTap) {
145 if (!MSOldCall() && menu_) {
146 [[cydgets_ objectAtIndex:active_] disableCydget:self];
147 active_ = (active_ + 1) % [cydgets_ count];
148 [[cydgets_ objectAtIndex:active_] enableCydget:self];
154 MSInstanceMessageHook0(void, SBAwayController, _undimScreen) {
156 [[cydgets_ objectAtIndex:active_] enableCydget:self];
157 [[[self awayView] window] makeKeyWindow];
161 static void Deactivate_(SBAwayController *self) {
162 [[cydgets_ objectAtIndex:active_] disableCydget:self];
166 MSInstanceMessageHook1(void, SBAwayController, dimScreen, BOOL, dim) {
167 Deactivate_([$SBAwayController sharedAwayController]);
171 MSInstanceMessageHook1(void, SpringBoard, menuButtonUp, GSEventRef, event) {
176 MSInstanceMessageHook1(void, SBAwayView, addGestureRecognizer, id, recognizer) {
178 return MSOldCall(recognizer);
181 MSInstanceMessageHook1(void, SBAwayWindow, sendGSEvent, GSEventRef, event) {
182 NSLog(@"sendGSEvent");
184 return MSOldCall(event);
186 return MSSuperCall(event);
189 #define Cydgets_ @"/System/Library/LockCydgets"
191 static void CydgetSetup() {
192 NSString *plist([NSString stringWithFormat:@"%@/Library/Preferences/com.saurik.Cydget.plist", NSHomeDirectory()]);
193 settings_ = [NSMutableDictionary dictionaryWithContentsOfFile:plist] ?: [NSMutableDictionary dictionary];
195 NSArray *cydgets([settings_ objectForKey:@"LockCydgets"] ?: [NSArray arrayWithObjects:[NSDictionary dictionaryWithObjectsAndKeys:
196 @"Welcome", @"Name", [NSNumber numberWithBool:YES], @"Active", nil
197 ], [NSDictionary dictionaryWithObjectsAndKeys:
198 @"AwayView", @"Name", [NSNumber numberWithBool:YES], @"Active", nil
201 cydgets_ = [NSMutableArray arrayWithCapacity:4];
202 for (NSDictionary *cydget in cydgets)
203 if ([[cydget objectForKey:@"Active"] boolValue])
204 if (NSDictionary *info = [NSDictionary dictionaryWithContentsOfFile:[NSString stringWithFormat:@"%@/%@.cydget/Info.plist", Cydgets_, [cydget objectForKey:@"Name"]]])
205 [cydgets_ addObject:info];
207 if ([cydgets_ count] == 0)
211 // XXX: this could happen while it is unlocked
212 MSInstanceMessageHook1(id, SBAwayView, initWithFrame, CGRect, frame) {
213 self = MSOldCall(frame);
215 [[cydgets_ objectAtIndex:active_] enableCydget:[$SBAwayController sharedAwayController]];
219 MSInstanceMessageHook1(void, SBAwayController, _finishedUnlockAttemptWithStatus, BOOL, status) {
225 MSInstanceMessageHook0(void, SBAwayView, updateInterface) {
228 NSDictionary *cydget([cydgets_ objectAtIndex:active_]);
230 NSString *background([cydget objectForKey:@"CYBackground"]);
231 if ([background isEqualToString:@"Wallpaper"]) {
232 MSIvarHook(UIView *, _backgroundView);
233 [_backgroundView setAlpha:1.0f];
236 if ([[cydget objectForKey:@"CYShowDateTime"] boolValue])