]> git.saurik.com Git - cydget.git/blob - Tweak.mm
Port Cydget to 4.2.
[cydget.git] / Tweak.mm
1 /* Cydget - open-source IntelliScreen replacement
2 * Copyright (C) 2009 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 @implementation NSDictionary (Cydgets)
88
89 - (void) enableCydget:(SBAwayController *)away {
90 if (NSString *plugin = [self objectForKey:@"CYPlugin"] ?: [self objectForKey:@"Plugin"]) {
91 [away enableLockScreenBundleWithName:plugin];
92 ++online_;
93 }
94 }
95
96 - (void) disableCydget:(SBAwayController *)away {
97 if (NSString *plugin = [self objectForKey:@"CYPlugin"] ?: [self objectForKey:@"Plugin"]) {
98 [away disableLockScreenBundleWithName:plugin];
99 --online_;
100 }
101 }
102
103 @end
104
105 MSInstanceMessageHook0(BOOL, SBAwayController, handleMenuButtonTap) {
106 if (!MSOldCall() && menu_) {
107 [[cydgets_ objectAtIndex:active_] disableCydget:self];
108 active_ = (active_ + 1) % [cydgets_ count];
109 [[cydgets_ objectAtIndex:active_] enableCydget:self];
110 }
111
112 return YES;
113 }
114
115 MSInstanceMessageHook0(void, SBAwayController, _undimScreen) {
116 menu_ = false;
117 [[cydgets_ objectAtIndex:active_] enableCydget:[$SBAwayController sharedAwayController]];
118 MSOldCall();
119 }
120
121 static void Deactivate_(SBAwayController *self) {
122 [[cydgets_ objectAtIndex:active_] disableCydget:self];
123 active_ = 0;
124 }
125
126 MSInstanceMessageHook1(void, SBAwayController, dimScreen, BOOL, dim) {
127 Deactivate_([$SBAwayController sharedAwayController]);
128 MSOldCall(dim);
129 }
130
131 MSInstanceMessageHook1(void, SpringBoard, menuButtonUp, GSEventRef, event) {
132 menu_ = true;
133 MSOldCall(event);
134 }
135
136 MSInstanceMessageHook1(void, SBAwayView, addGestureRecognizer, id, recognizer) {
137 // MSOldCall(recognizer);
138 }
139
140 MSInstanceMessageHook1(void, SBAwayWindow, sendGSEvent, GSEventRef, event) {
141 if (online_ != 0)
142 MSSuperCall(event);
143 else
144 MSOldCall(event);
145 }
146
147 #define Cydgets_ @"/System/Library/LockCydgets"
148
149 static void CydgetSetup() {
150 NSString *plist([NSString stringWithFormat:@"%@/Library/Preferences/com.saurik.Cydget.plist", NSHomeDirectory()]);
151 settings_ = [NSMutableDictionary dictionaryWithContentsOfFile:plist] ?: [NSMutableDictionary dictionary];
152
153 NSArray *cydgets([settings_ objectForKey:@"LockCydgets"] ?: [NSArray arrayWithObjects:[NSDictionary dictionaryWithObjectsAndKeys:
154 @"Welcome", @"Name", [NSNumber numberWithBool:YES], @"Active", nil
155 ], [NSDictionary dictionaryWithObjectsAndKeys:
156 @"AwayView", @"Name", [NSNumber numberWithBool:YES], @"Active", nil
157 ], nil]);
158
159 cydgets_ = [NSMutableArray arrayWithCapacity:4];
160 for (NSDictionary *cydget in cydgets)
161 if ([[cydget objectForKey:@"Active"] boolValue])
162 if (NSDictionary *info = [NSDictionary dictionaryWithContentsOfFile:[NSString stringWithFormat:@"%@/%@.cydget/Info.plist", Cydgets_, [cydget objectForKey:@"Name"]]])
163 [cydgets_ addObject:info];
164
165 if ([cydgets_ count] == 0)
166 cydgets_ = nil;
167 }
168
169 // XXX: this could happen while it is unlocked
170 MSInstanceMessageHook1(id, SBAwayView, initWithFrame, CGRect, frame) {
171 self = MSOldCall(frame);
172 CydgetSetup();
173 [[cydgets_ objectAtIndex:active_] enableCydget:[$SBAwayController sharedAwayController]];
174 return self;
175 }
176
177 MSInstanceMessageHook1(void, SBAwayController, _finishedUnlockAttemptWithStatus, BOOL, status) {
178 if (status)
179 Deactivate_(self);
180 MSOldCall(status);
181 }
182
183 MSInstanceMessageHook0(void, SBAwayView, updateInterface) {
184 MSOldCall();
185
186 NSDictionary *cydget([cydgets_ objectAtIndex:active_]);
187
188 NSString *background([cydget objectForKey:@"CYBackground"]);
189 if ([background isEqualToString:@"Wallpaper"]) {
190 MSIvarHook(UIView *, _backgroundView);
191 [_backgroundView setAlpha:1.0f];
192 }
193
194 if ([[cydget objectForKey:@"CYShowDateTime"] boolValue])
195 [self addDateView];
196 }