Fix Cydget Keyboard on 4.x.
[cydget.git] / Tweak.mm
1 /* Cydget - open-source AwayView plugin multiplexer
2  * Copyright (C) 2009-2011  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 MSClassHook(SBAwayController)
51 MSClassHook(SBAwayView)
52 MSClassHook(SBAwayWindow)
53
54 #define _trace() \
55     NSLog(@"_trace(%s:%u)@%s %d", __FILE__, __LINE__, __FUNCTION__, active_)
56 #define _not(type) \
57     static_cast<type>(~type())
58
59 static bool menu_;
60
61 static _H<NSArray> settings_;
62 static _H<NSArray> cydgets_;
63 static size_t active_;
64 static unsigned online_;
65
66 @interface CydgetController : NSObject {
67 }
68
69 + (NSDictionary *) currentConfiguration;
70
71 @end
72
73 @implementation CydgetController
74
75 + (NSDictionary *) currentConfiguration {
76     NSDictionary *cydget([cydgets_ objectAtIndex:active_]);
77     return [cydget objectForKey:@"CYConfiguration"] ?: [cydget objectForKey:@"Configuration"];
78 }
79
80 @end
81
82 @interface NSDictionary (Cydgets)
83 - (void) enableCydget:(SBAwayController *)away;
84 - (void) disableCydget:(SBAwayController *)away;
85 @end
86
87 @interface UIPeripheralHost : NSObject
88 + (UIPeripheralHost *) sharedInstance;
89 + (void) _releaseSharedInstance;
90 - (void) createAutomaticKeyboardIfNeeded;
91 @end
92
93 MSClassHook(UIPeripheralHost)
94
95 @interface UITextEffectsWindow : UIWindow
96 + (UIWindow *) sharedTextEffectsWindow;
97 @end
98
99 @implementation NSDictionary (Cydgets)
100
101 - (void) enableCydget:(SBAwayController *)away {
102     if (NSString *plugin = [self objectForKey:@"CYPlugin"] ?: [self objectForKey:@"Plugin"]) {
103         UIKeyboardEnableAutomaticAppearance();
104         [[$UIPeripheralHost sharedInstance] createAutomaticKeyboardIfNeeded];
105         [[UITextEffectsWindow sharedTextEffectsWindow] setWindowLevel:1000];
106
107         [away enableLockScreenBundleWithName:plugin];
108         ++online_;
109     }
110 }
111
112 - (void) disableCydget:(SBAwayController *)away {
113     if (NSString *plugin = [self objectForKey:@"CYPlugin"] ?: [self objectForKey:@"Plugin"]) {
114         --online_;
115         [away disableLockScreenBundleWithName:plugin];
116
117         [$UIPeripheralHost _releaseSharedInstance];
118         UIKeyboardDisableAutomaticAppearance();
119     }
120 }
121
122 @end
123
124 MSInstanceMessageHook1(void, SpringBoard, handleKeyEvent, GSEventRef, event) {
125     MSSuperCall(event);
126 }
127
128 MSInstanceMessageHook0(BOOL, SBAwayController, handleMenuButtonTap) {
129     if (!MSOldCall() && menu_) {
130         [[cydgets_ objectAtIndex:active_] disableCydget:self];
131         active_ = (active_ + 1) % [cydgets_ count];
132         [[cydgets_ objectAtIndex:active_] enableCydget:self];
133     }
134
135     return YES;
136 }
137
138 MSInstanceMessageHook0(void, SBAwayController, _undimScreen) {
139     menu_ = false;
140     [[cydgets_ objectAtIndex:active_] enableCydget:[$SBAwayController sharedAwayController]];
141     MSOldCall();
142 }
143
144 static void Deactivate_(SBAwayController *self) {
145     [[cydgets_ objectAtIndex:active_] disableCydget:self];
146     active_ = 0;
147 }
148
149 MSInstanceMessageHook1(void, SBAwayController, dimScreen, BOOL, dim) {
150     Deactivate_([$SBAwayController sharedAwayController]);
151     MSOldCall(dim);
152 }
153
154 MSInstanceMessageHook1(void, SpringBoard, menuButtonUp, GSEventRef, event) {
155     menu_ = true;
156     MSOldCall(event);
157 }
158
159 MSInstanceMessageHook1(void, SBAwayView, addGestureRecognizer, id, recognizer) {
160     // MSOldCall(recognizer);
161 }
162
163 MSInstanceMessageHook1(void, SBAwayWindow, sendGSEvent, GSEventRef, event) {
164     if (online_ != 0)
165         MSSuperCall(event);
166     else
167         MSOldCall(event);
168 }
169
170 #define Cydgets_ @"/System/Library/LockCydgets"
171
172 static void CydgetSetup() {
173     NSString *plist([NSString stringWithFormat:@"%@/Library/Preferences/com.saurik.Cydget.plist", NSHomeDirectory()]);
174     settings_ = [NSMutableDictionary dictionaryWithContentsOfFile:plist] ?: [NSMutableDictionary dictionary];
175
176     NSArray *cydgets([settings_ objectForKey:@"LockCydgets"] ?: [NSArray arrayWithObjects:[NSDictionary dictionaryWithObjectsAndKeys:
177         @"Welcome", @"Name", [NSNumber numberWithBool:YES], @"Active", nil
178     ], [NSDictionary dictionaryWithObjectsAndKeys:
179         @"AwayView", @"Name", [NSNumber numberWithBool:YES], @"Active", nil
180     ], nil]);
181
182     cydgets_ = [NSMutableArray arrayWithCapacity:4];
183     for (NSDictionary *cydget in cydgets)
184         if ([[cydget objectForKey:@"Active"] boolValue])
185             if (NSDictionary *info = [NSDictionary dictionaryWithContentsOfFile:[NSString stringWithFormat:@"%@/%@.cydget/Info.plist", Cydgets_, [cydget objectForKey:@"Name"]]])
186                 [cydgets_ addObject:info];
187
188     if ([cydgets_ count] == 0)
189         cydgets_ = nil;
190 }
191
192 // XXX: this could happen while it is unlocked
193 MSInstanceMessageHook1(id, SBAwayView, initWithFrame, CGRect, frame) {
194     self = MSOldCall(frame);
195     CydgetSetup();
196     [[cydgets_ objectAtIndex:active_] enableCydget:[$SBAwayController sharedAwayController]];
197     return self;
198 }
199
200 MSInstanceMessageHook1(void, SBAwayController, _finishedUnlockAttemptWithStatus, BOOL, status) {
201     if (status)
202         Deactivate_(self);
203     MSOldCall(status);
204 }
205
206 MSInstanceMessageHook0(void, SBAwayView, updateInterface) {
207     MSOldCall();
208
209     NSDictionary *cydget([cydgets_ objectAtIndex:active_]);
210
211     NSString *background([cydget objectForKey:@"CYBackground"]);
212     if ([background isEqualToString:@"Wallpaper"]) {
213         MSIvarHook(UIView *, _backgroundView);
214         [_backgroundView setAlpha:1.0f];
215     }
216
217     if ([[cydget objectForKey:@"CYShowDateTime"] boolValue])
218         [self addDateView];
219 }