--- /dev/null
+/* Cydget - open-source IntelliScreen replacement
+ * Copyright (C) 2009 Jay Freeman (saurik)
+*/
+
+/*
+ * Redistribution and use in source and binary
+ * forms, with or without modification, are permitted
+ * provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the
+ * above copyright notice, this list of conditions
+ * and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the
+ * above copyright notice, this list of conditions
+ * and the following disclaimer in the documentation
+ * and/or other materials provided with the
+ * distribution.
+ * 3. The name of the author may not be used to endorse
+ * or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS''
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
+ * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
+ * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
+ * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
+ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+*/
+
+#include <substrate.h>
+
+#include <Foundation/Foundation.h>
+#include <GraphicsServices/GraphicsServices.h>
+
+#import <SpringBoard/SBAwayController.h>
+#import <SpringBoard/SBAwayWindow.h>
+
+MSClassHook(SpringBoard)
+MSClassHook(SBAwayController)
+MSClassHook(SBAwayView)
+
+#define _trace() \
+ NSLog(@"_trace(%s:%u)@%s %d", __FILE__, __LINE__, __FUNCTION__, active_)
+#define _not(type) \
+ static_cast<type>(~type())
+
+static bool menu_;
+static unsigned lock_;
+
+static _H<NSArray> cydgets_;
+static size_t active_ = _not(size_t);
+
+@interface NSDictionary (Cydgets)
+- (void) enableCydget:(SBAwayController *)away;
+- (void) disableCydget:(SBAwayController *)away;
+@end
+
+@implementation NSDictionary (Cydgets)
+
+- (void) enableCydget:(SBAwayController *)away {
+ [away enableLockScreenBundleWithName:[self objectForKey:@"Plugin"]];
+}
+
+- (void) disableCydget:(SBAwayController *)away {
+ [away disableLockScreenBundleWithName:[self objectForKey:@"Plugin"]];
+}
+
+@end
+
+MSHook(BOOL, SBAwayController$handleMenuButtonTap, SBAwayController *self, SEL sel) {
+ unsigned lock(lock_);
+
+ if (!_SBAwayController$handleMenuButtonTap(self, sel) && lock != 2) {
+ if (active_ != _not(size_t))
+ [[cydgets_ objectAtIndex:active_] disableCydget:self];
+
+ size_t count([cydgets_ count]);
+ if ((++active_ %= count + 1) == count)
+ active_ = _not(size_t);
+ else
+ [[cydgets_ objectAtIndex:active_] enableCydget:self];
+ }
+
+ return YES;
+}
+
+MSHook(void, SBAwayController$_undimScreen, SBAwayController *self, SEL sel) {
+ if (lock_ == 1)
+ lock_ = 2;
+ _SBAwayController$_undimScreen(self, sel);
+}
+
+MSHook(void, SBAwayController$undimScreen, SBAwayController *self, SEL sel) {
+ lock_ = menu_ ? 1 : 0;
+ _SBAwayController$undimScreen(self, sel);
+}
+
+static void Deactivate_(SBAwayController *self) {
+ if (active_ != _not(size_t)) {
+ [[cydgets_ objectAtIndex:active_] disableCydget:self];
+ active_ = _not(size_t);
+ }
+}
+
+MSHook(void, SBAwayController$finishedDimmingScreen, SBAwayController *self, SEL sel) {
+ Deactivate_(self);
+ _SBAwayController$finishedDimmingScreen(self, sel);
+}
+
+MSHook(void, SpringBoard$menuButtonUp$, UIView *self, SEL sel, GSEventRef event) {
+ menu_ = true;
+ _SpringBoard$menuButtonUp$(self, sel, event);
+ menu_ = false;
+}
+
+MSHook(void, SBAwayView$addGestureRecognizer$, UIView *self, SEL sel, id gr) {
+ //_SBAwayView$addGestureRecognizer$(self, sel, gr);
+}
+
+MSInstanceMessageHook1(void, SBAwayController, _finishedUnlockAttemptWithStatus, BOOL, status) {
+ if (status)
+ Deactivate_(self);
+ MSOldCall(status);
+}
+
+#define Cydgets_ @"/System/Library/LockCydgets"
+
+MSInitialize { _pooled
+ cydgets_ = [NSMutableArray arrayWithCapacity:4];
+
+ for (NSString *plist in [[NSFileManager defaultManager] directoryContentsAtPath:Cydgets_])
+ if ([plist hasSuffix:@".plist"])
+ [cydgets_ addObject:[NSDictionary dictionaryWithContentsOfFile:[Cydgets_ stringByAppendingPathComponent:plist]]];
+
+ MSHookMessage1(SpringBoard, menuButtonUp);
+
+ MSHookMessage0(SBAwayController, handleMenuButtonTap);
+ MSHookMessage0(SBAwayController, _undimScreen);
+ MSHookMessage0(SBAwayController, undimScreen);
+ MSHookMessage0(SBAwayController, finishedDimmingScreen);
+
+ MSHookMessage1(SBAwayView, addGestureRecognizer);
+}