Revert a bunch of keyboard behavior for now: I hate Cydget.
[cydget.git] / Tweak.mm
1 /* Cydget - open-source AwayView plugin multiplexer
2  * Copyright (C) 2009-2012  Jay Freeman (saurik)
3 */
4
5 /*
6  *        Redistribution and use in source and binary
7  * forms, with or without modification, are permitted
8  * provided that the following conditions are met:
9  *
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
17  *    distribution.
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.
21  *
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.
36 */
37
38 #include <substrate.h>
39
40 #include <Foundation/Foundation.h>
41 #include <GraphicsServices/GraphicsServices.h>
42 #include <UIKit/UIKit.h>
43
44 #import <SpringBoard/SBAwayController.h>
45 #import <SpringBoard/SBAwayView.h>
46 #import <SpringBoard/SBAwayWindow.h>
47 #import <SpringBoard/SpringBoard.h>
48
49 MSClassHook(SpringBoard)
50
51 MSClassHook(SBAwayController)
52 MSClassHook(SBAwayView)
53 MSClassHook(SBAwayViewController)
54 MSClassHook(SBAwayWindow)
55
56 #define _trace() \
57     NSLog(@"_trace(%s:%u)@%s %d", __FILE__, __LINE__, __FUNCTION__, active_)
58 #define _not(type) \
59     static_cast<type>(~type())
60
61 static bool menu_;
62
63 static _H<NSArray> settings_;
64 static _H<NSArray> cydgets_;
65 static size_t active_;
66 static unsigned online_;
67
68 @interface CydgetController : NSObject {
69 }
70
71 + (NSDictionary *) currentConfiguration;
72
73 @end
74
75 @implementation CydgetController
76
77 + (NSDictionary *) currentConfiguration {
78     NSDictionary *cydget([cydgets_ objectAtIndex:active_]);
79     return [cydget objectForKey:@"CYConfiguration"] ?: [cydget objectForKey:@"Configuration"];
80 }
81
82 @end
83
84 @interface NSDictionary (Cydgets)
85 - (void) enableCydget:(SBAwayController *)away;
86 - (void) disableCydget:(SBAwayController *)away;
87 @end
88
89 @interface UIPeripheralHost : NSObject
90 + (UIPeripheralHost *) sharedInstance;
91 + (void) _releaseSharedInstance;
92 @end
93
94 MSClassHook(UIPeripheralHost)
95
96 @interface UITextEffectsWindow : UIWindow
97 + (UIWindow *) sharedTextEffectsWindow;
98 @end
99
100 @implementation NSDictionary (Cydgets)
101
102 - (void) enableCydget:(SBAwayController *)away {
103     if (NSString *plugin = [self objectForKey:@"CYPlugin"] ?: [self objectForKey:@"Plugin"]) {
104         ++online_;
105
106         if (kCFCoreFoundationVersionNumber < 600)
107             UIKeyboardEnableAutomaticAppearance();
108
109         [[UITextEffectsWindow sharedTextEffectsWindow] setWindowLevel:1000];
110         [away enableLockScreenBundleWithName:plugin];
111     }
112 }
113
114 - (void) disableCydget:(SBAwayController *)away {
115     if (NSString *plugin = [self objectForKey:@"CYPlugin"] ?: [self objectForKey:@"Plugin"]) {
116         [away disableLockScreenBundleWithName:plugin];
117         [$UIPeripheralHost _releaseSharedInstance];
118
119         if (kCFCoreFoundationVersionNumber < 600)
120             UIKeyboardDisableAutomaticAppearance();
121
122         --online_;
123     }
124 }
125
126 @end
127
128 // avoid rendering a keyboard onto the default SBAwayView while automatic keyboard is online
129 MSInstanceMessageHook0(UIView *, SBAwayView, inputView) {
130     // XXX: there is a conceptual error here
131     if (online_ == 0 && false || kCFCoreFoundationVersionNumber > 600)
132         return MSOldCall();
133
134     return [[[UIView alloc] init] autorelease];
135 }
136
137 // by default, keyboard actions are redirected to SBAwayController and press menu button
138 MSInstanceMessageHook1(void, SpringBoard, handleKeyEvent, GSEventRef, event) {
139     if (online_ == 0 || kCFCoreFoundationVersionNumber > 600)
140         return MSOldCall(event);
141
142     return MSSuperCall(event);
143 }
144
145 MSInstanceMessageHook0(BOOL, SBAwayController, handleMenuButtonTap) {
146     if (!MSOldCall() && menu_) {
147         [[cydgets_ objectAtIndex:active_] disableCydget:self];
148         active_ = (active_ + 1) % [cydgets_ count];
149         [[cydgets_ objectAtIndex:active_] enableCydget:self];
150     }
151
152     return YES;
153 }
154
155 MSInstanceMessageHook0(void, SBAwayController, undimScreen) {
156     if ([self isDimmed]) {
157         menu_ = false;
158         [[cydgets_ objectAtIndex:active_] enableCydget:self];
159         [[[self awayView] window] makeKeyWindow];
160     }
161
162     MSOldCall();
163 }
164
165 static void Deactivate_(SBAwayController *self) {
166     [[cydgets_ objectAtIndex:active_] disableCydget:self];
167     active_ = 0;
168 }
169
170 MSInstanceMessageHook1(void, SBAwayController, dimScreen, BOOL, dim) {
171     Deactivate_([$SBAwayController sharedAwayController]);
172     MSOldCall(dim);
173 }
174
175 MSInstanceMessageHook1(void, SpringBoard, menuButtonDown, GSEventRef, event) {
176     menu_ = true;
177     MSOldCall(event);
178 }
179
180 MSInstanceMessageHook1(void, SBAwayView, addGestureRecognizer, id, recognizer) {
181     if (online_ == 0 || kCFCoreFoundationVersionNumber > 600)
182         return MSOldCall(recognizer);
183 }
184
185 MSInstanceMessageHook1(void, SBAwayWindow, sendGSEvent, GSEventRef, event) {
186     if (online_ == 0 || kCFCoreFoundationVersionNumber > 600)
187         return MSOldCall(event);
188
189     return MSSuperCall(event);
190 }
191
192 #define Cydgets_ @"/System/Library/LockCydgets"
193
194 static void CydgetSetup() {
195     NSString *plist([NSString stringWithFormat:@"%@/Library/Preferences/com.saurik.Cydget.plist", NSHomeDirectory()]);
196     settings_ = [NSMutableDictionary dictionaryWithContentsOfFile:plist] ?: [NSMutableDictionary dictionary];
197
198     NSArray *cydgets([settings_ objectForKey:@"LockCydgets"] ?: [NSArray arrayWithObjects:[NSDictionary dictionaryWithObjectsAndKeys:
199         @"Welcome", @"Name", [NSNumber numberWithBool:YES], @"Active", nil
200     ], [NSDictionary dictionaryWithObjectsAndKeys:
201         @"AwayView", @"Name", [NSNumber numberWithBool:YES], @"Active", nil
202     ], nil]);
203
204     cydgets_ = [NSMutableArray arrayWithCapacity:4];
205     for (NSDictionary *cydget in cydgets)
206         if ([[cydget objectForKey:@"Active"] boolValue])
207             if (NSDictionary *info = [NSDictionary dictionaryWithContentsOfFile:[NSString stringWithFormat:@"%@/%@.cydget/Info.plist", Cydgets_, [cydget objectForKey:@"Name"]]])
208                 [cydgets_ addObject:info];
209
210     if ([cydgets_ count] == 0)
211         cydgets_ = nil;
212 }
213
214 // XXX: this could happen while it is unlocked
215 MSInstanceMessageHook1(id, SBAwayView, initWithFrame, CGRect, frame) {
216     self = MSOldCall(frame);
217     CydgetSetup();
218     [[cydgets_ objectAtIndex:active_] enableCydget:[$SBAwayController sharedAwayController]];
219     return self;
220 }
221
222 MSInstanceMessageHook1(void, SBAwayController, _finishedUnlockAttemptWithStatus, BOOL, status) {
223     if (status)
224         Deactivate_(self);
225     MSOldCall(status);
226 }
227
228 // this is called occasionally by -[SBAwayView updateInterface] and takes away our keyboard
229 MSInstanceMessageHook0(void, SBAwayView, _fixupFirstResponder) {
230     if (online_ == 0)
231         return MSOldCall();
232 }
233
234 MSInstanceMessageHook0(void, SBAwayView, updateInterface) {
235     MSOldCall();
236
237     NSDictionary *cydget([cydgets_ objectAtIndex:active_]);
238
239     NSString *background([cydget objectForKey:@"CYBackground"]);
240     if ([background isEqualToString:@"Wallpaper"]) {
241         MSIvarHook(UIView *, _backgroundView);
242         [_backgroundView setAlpha:1.0f];
243     }
244
245     if ([[cydget objectForKey:@"CYShowDateTime"] boolValue])
246         [self addDateView];
247 }