1 /* CydgetScript - open-source IntelliDial replacement
2 * Copyright (C) 2009 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 #import <GraphicsServices/GraphicsServices.h>
41 #import <UIKit/UIKit.h>
42 #import <AddressBook/AddressBook.h>
44 #import <SpringBoardUI/SBAwayViewPluginController.h>
45 #import <TelephonyUI/TPBottomLockBar.h>
47 #import <QuartzCore/CALayer.h>
48 // XXX: fix the minimum requirement
49 extern NSString * const kCAFilterNearest;
51 #include <WebKit/DOMCSSPrimitiveValue.h>
52 #include <WebKit/DOMCSSStyleDeclaration.h>
53 #include <WebKit/DOMDocument.h>
54 #include <WebKit/DOMHTMLBodyElement.h>
55 #include <WebKit/DOMNodeList.h>
56 #include <WebKit/DOMRGBColor.h>
58 #include <WebKit/WebFrame.h>
59 #include <WebKit/WebPolicyDelegate.h>
60 #include <WebKit/WebPreferences.h>
61 #include <WebKit/WebScriptObject.h>
63 #import <WebKit/WebView.h>
64 #import <WebKit/WebView-WebPrivate.h>
66 #include <WebCore/Page.h>
67 #include <WebCore/Settings.h>
69 #include <WebCore/WebCoreThread.h>
70 #include <WebKit/WebPreferences-WebPrivate.h>
72 @interface WebView (UICaboodle)
73 - (void) setScriptDebugDelegate:(id)delegate;
74 - (void) _setFormDelegate:(id)delegate;
75 - (void) _setUIKitDelegate:(id)delegate;
76 - (void) setWebMailDelegate:(id)delegate;
77 - (void) _setLayoutInterval:(float)interval;
81 #define _forever for (;;)
83 _disused static unsigned trace_;
85 #define _trace() do { \
86 NSLog(@"_trace(%u)@%s:%u[%s]\n", \
87 trace_++, __FILE__, __LINE__, __FUNCTION__\
91 @protocol CydgetController
92 - (NSDictionary *) currentConfiguration;
95 static Class $CydgetController(objc_getClass("CydgetController"));
97 @interface NSString (UIKit)
98 - (NSString *) stringByAddingPercentEscapes;
101 @implementation UIWebDocumentView (WebCycript)
103 - (void) _setScrollerOffset:(CGPoint)offset {
104 UIScroller *scroller([self _scroller]);
106 CGSize size([scroller contentSize]);
107 CGSize bounds([scroller bounds].size);
110 max.x = size.width - bounds.width;
111 max.y = size.height - bounds.height;
119 offset.x = offset.x < 0 ? 0 : offset.x > max.x ? max.x : offset.x;
120 offset.y = offset.y < 0 ? 0 : offset.y > max.y ? max.y : offset.y;
122 [scroller setOffset:offset];
127 /* WebCycript Delegate {{{ */
128 @interface WebCycriptDelegate : NSObject {
129 _transient volatile id delegate_;
132 - (void) setDelegate:(id)delegate;
133 - (id) initWithDelegate:(id)delegate;
136 @implementation WebCycriptDelegate
138 - (void) setDelegate:(id)delegate {
139 delegate_ = delegate;
142 - (id) initWithDelegate:(id)delegate {
143 delegate_ = delegate;
147 - (void) webView:(WebView *)sender didClearWindowObject:(WebScriptObject *)window forFrame:(WebFrame *)frame {
148 if (delegate_ != nil)
149 return [delegate_ webView:sender didClearWindowObject:window forFrame:frame];
152 - (void) webView:(WebView *)sender didCommitLoadForFrame:(WebFrame *)frame {
153 if (delegate_ != nil)
154 return [delegate_ webView:sender didCommitLoadForFrame:frame];
157 - (void) webView:(WebView *)sender didFailLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
158 if (delegate_ != nil)
159 return [delegate_ webView:sender didFailLoadWithError:error forFrame:frame];
162 - (void) webView:(WebView *)sender didFailProvisionalLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
163 if (delegate_ != nil)
164 return [delegate_ webView:sender didFailProvisionalLoadWithError:error forFrame:frame];
167 - (void) webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame {
168 if (delegate_ != nil)
169 return [delegate_ webView:sender didFinishLoadForFrame:frame];
172 /*- (void) webView:(WebView *)sender didReceiveTitle:(NSString *)title forFrame:(WebFrame *)frame {
173 if (delegate_ != nil)
174 return [delegate_ webView:sender didReceiveTitle:title forFrame:frame];
177 - (void) webView:(WebView *)sender didStartProvisionalLoadForFrame:(WebFrame *)frame {
178 if (delegate_ != nil)
179 return [delegate_ webView:sender didStartProvisionalLoadForFrame:frame];
182 /*- (void) webView:(WebView *)sender resource:(id)identifier didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge fromDataSource:(WebDataSource *)source {
183 if (delegate_ != nil)
184 return [delegate_ webView:sender resource:identifier didReceiveAuthenticationChallenge:challenge fromDataSource:source];
187 /*- (NSURLRequest *) webView:(WebView *)sender resource:(id)identifier willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)redirectResponse fromDataSource:(WebDataSource *)source {
188 if (delegate_ != nil)
189 return [delegate_ webView:sender resource:identifier willSendRequest:request redirectResponse:redirectResponse fromDataSource:source];
193 - (IMP) methodForSelector:(SEL)sel {
194 if (IMP method = [super methodForSelector:sel])
196 fprintf(stderr, "methodForSelector:[%s] == NULL\n", sel_getName(sel));
200 - (BOOL) respondsToSelector:(SEL)sel {
201 if ([super respondsToSelector:sel])
203 // XXX: WebThreadCreateNSInvocation returns nil
204 //fprintf(stderr, "[%s]R?%s\n", class_getName(self->isa), sel_getName(sel));
205 return delegate_ == nil ? NO : [delegate_ respondsToSelector:sel];
208 - (NSMethodSignature *) methodSignatureForSelector:(SEL)sel {
209 if (NSMethodSignature *method = [super methodSignatureForSelector:sel])
211 //fprintf(stderr, "[%s]S?%s\n", class_getName(self->isa), sel_getName(sel));
212 if (delegate_ != nil)
213 if (NSMethodSignature *sig = [delegate_ methodSignatureForSelector:sel])
215 // XXX: I fucking hate Apple so very very bad
216 return [NSMethodSignature signatureWithObjCTypes:"v@:"];
219 - (void) forwardInvocation:(NSInvocation *)inv {
220 SEL sel = [inv selector];
221 if (delegate_ != nil && [delegate_ respondsToSelector:sel])
222 [inv invokeWithTarget:delegate_];
228 @interface WebCydgetLockScreenView : UIView {
229 WebCycriptDelegate *indirect_;
230 UIProgressIndicator *indicator_;
231 UIScroller *scroller_;
232 UIWebDocumentView *document_;
240 NSMutableSet *loading_;
247 @implementation WebCydgetLockScreenView
249 //#include "UICaboodle/UCInternal.h"
255 WebView *webview([document_ webView]);
256 [webview setFrameLoadDelegate:nil];
257 [webview setResourceLoadDelegate:nil];
258 [webview setUIDelegate:nil];
259 [webview setScriptDebugDelegate:nil];
260 [webview setPolicyDelegate:nil];
262 /* XXX: these are set by UIWebDocumentView
263 [webview setDownloadDelegate:nil];
264 [webview _setFormDelegate:nil];
265 [webview _setUIKitDelegate:nil];
266 [webview setEditingDelegate:nil];*/
268 /* XXX: no one sets this, ever
269 [webview setWebMailDelegate:nil];*/
271 [document_ setDelegate:nil];
272 [document_ setGestureDelegate:nil];
273 [document_ setFormEditingDelegate:nil];
274 [document_ setInteractionDelegate:nil];
276 [indirect_ setDelegate:nil];
285 [scroller_ setDelegate:nil];
291 [indicator_ release];
295 + (float) defaultWidth {
299 - (void) _setTileDrawingEnabled:(BOOL)enabled {
300 //[document_ setTileDrawingEnabled:enabled];
303 - (void) willStartGesturesInView:(UIView *)view forEvent:(GSEventRef)event {
304 [self _setTileDrawingEnabled:NO];
307 - (void) didFinishGesturesInView:(UIView *)view forEvent:(GSEventRef)event {
308 [self _setTileDrawingEnabled:YES];
309 [document_ redrawScaledDocument];
312 - (void) setViewportWidth:(float)width {
313 width_ = width != 0 ? width : [[self class] defaultWidth];
314 [document_ setViewportSize:CGSizeMake(width_, UIWebViewGrowsAndShrinksToFitHeight) forDocumentTypes:0x10];
317 - (void) scrollerWillStartDragging:(UIScroller *)scroller {
318 [self _setTileDrawingEnabled:NO];
321 - (void) scrollerDidEndDragging:(UIScroller *)scroller willSmoothScroll:(BOOL)smooth {
322 [self _setTileDrawingEnabled:YES];
325 - (void) scrollerDidEndDragging:(UIScroller *)scroller {
326 [self _setTileDrawingEnabled:YES];
329 - (void) loadRequest:(NSURLRequest *)request {
333 [document_ loadRequest:request];
337 - (void) loadURL:(NSURL *)url cachePolicy:(NSURLRequestCachePolicy)policy {
338 [self loadRequest:[NSURLRequest
345 - (void) loadURL:(NSURL *)url {
346 [self loadURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy];
350 CGRect frame = {{0, 0}, {320, 480}};
351 frame.size.height -= GSDefaultStatusBarHeight();
353 if ((self = [super initWithFrame:frame]) != nil) {
354 struct CGRect bounds([self bounds]);
356 scroller_ = [[UIScroller alloc] initWithFrame:bounds];
357 [self addSubview:scroller_];
359 [scroller_ setFixedBackgroundPattern:YES];
360 [scroller_ setBackgroundColor:[UIColor blackColor]];
362 [scroller_ setScrollingEnabled:YES];
363 [scroller_ setClipsSubviews:YES];
364 [scroller_ setAllowsRubberBanding:YES];
366 [scroller_ setDelegate:self];
367 [scroller_ setBounces:YES];
368 [scroller_ setScrollHysteresis:8];
369 [scroller_ setThumbDetectionEnabled:NO];
370 [scroller_ setDirectionalScrolling:YES];
371 [scroller_ setScrollDecelerationFactor:0.99]; /* 0.989324 */
372 [scroller_ setEventMode:YES];
373 [scroller_ setShowBackgroundShadow:NO]; /* YES */
374 [scroller_ setAllowsRubberBanding:YES]; /* Vertical */
375 [scroller_ setAdjustForContentSizeChange:YES]; /* NO */
377 CGRect rect([scroller_ bounds]);
378 //rect.size.height = 0;
382 document_ = [[UIWebDocumentView alloc] initWithFrame:rect];
383 WebView *webview([document_ webView]);
385 [document_ setBackgroundColor:[UIColor blackColor]];
386 if ([document_ respondsToSelector:@selector(setDrawsBackground:)])
387 [document_ setDrawsBackground:NO];
388 [webview setDrawsBackground:NO];
390 [webview setPreferencesIdentifier:@"WebCycript"];
392 [document_ setTileSize:CGSizeMake(rect.size.width, 500)];
394 if ([document_ respondsToSelector:@selector(enableReachability)])
395 [document_ enableReachability];
397 [document_ setAllowsMessaging:YES];
399 if ([document_ respondsToSelector:@selector(useSelectionAssistantWithMode:)])
400 [document_ useSelectionAssistantWithMode:0];
402 [document_ setTilingEnabled:YES];
403 [document_ setDrawsGrid:NO];
404 [document_ setLogsTilingChanges:NO];
405 [document_ setTileMinificationFilter:kCAFilterNearest];
407 if ([document_ respondsToSelector:@selector(setDataDetectorTypes:)])
408 /* XXX: abstractify */
409 [document_ setDataDetectorTypes:0x80000000];
411 [document_ setDetectsPhoneNumbers:NO];
413 [document_ setAutoresizes:YES];
415 [document_ setMinimumScale:0.25f forDocumentTypes:0x10];
416 [document_ setMaximumScale:5.00f forDocumentTypes:0x10];
417 [document_ setInitialScale:UIWebViewScalesToFitScale forDocumentTypes:0x10];
418 //[document_ setViewportSize:CGSizeMake(980, UIWebViewGrowsAndShrinksToFitHeight) forDocumentTypes:0x10];
420 [document_ setViewportSize:CGSizeMake(320, UIWebViewGrowsAndShrinksToFitHeight) forDocumentTypes:0x2];
422 [document_ setMinimumScale:1.00f forDocumentTypes:0x8];
423 [document_ setInitialScale:UIWebViewScalesToFitScale forDocumentTypes:0x8];
424 [document_ setViewportSize:CGSizeMake(320, UIWebViewGrowsAndShrinksToFitHeight) forDocumentTypes:0x8];
426 [document_ _setDocumentType:0x4];
428 if ([document_ respondsToSelector:@selector(setZoomsFocusedFormControl:)])
429 [document_ setZoomsFocusedFormControl:YES];
430 [document_ setContentsPosition:7];
431 [document_ setEnabledGestures:0xa];
432 [document_ setValue:[NSNumber numberWithBool:YES] forGestureAttribute:UIGestureAttributeIsZoomRubberBandEnabled];
433 [document_ setValue:[NSNumber numberWithBool:YES] forGestureAttribute:UIGestureAttributeUpdatesScroller];
435 [document_ setSmoothsFonts:YES];
436 [document_ setAllowsImageSheet:YES];
437 [webview _setUsesLoaderCache:YES];
439 [webview setGroupName:@"CydgetGroup"];
441 WebPreferences *preferences([webview preferences]);
443 if ([webview respondsToSelector:@selector(_setLayoutInterval:)])
444 [webview _setLayoutInterval:0];
446 [preferences _setLayoutInterval:0];
448 [self setViewportWidth:0];
450 [document_ setDelegate:self];
451 [document_ setGestureDelegate:self];
452 [document_ setFormEditingDelegate:self];
453 [document_ setInteractionDelegate:self];
455 [scroller_ addSubview:document_];
457 //NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
459 indirect_ = [[WebCycriptDelegate alloc] initWithDelegate:self];
461 [webview setFrameLoadDelegate:indirect_];
462 [webview setPolicyDelegate:indirect_];
463 [webview setResourceLoadDelegate:indirect_];
464 [webview setUIDelegate:indirect_];
466 /* XXX: do not turn this on under penalty of extreme pain */
467 [webview setScriptDebugDelegate:nil];
471 CGSize indsize([UIProgressIndicator defaultSizeForStyle:UIProgressIndicatorStyleMediumWhite]);
472 indicator_ = [[UIProgressIndicator alloc] initWithFrame:CGRectMake(281, 12, indsize.width, indsize.height)];
473 [indicator_ setStyle:UIProgressIndicatorStyleMediumWhite];
475 [self setAutoresizingMask:UIViewAutoresizingFlexibleHeight];
476 [scroller_ setAutoresizingMask:UIViewAutoresizingFlexibleHeight];
478 NSDictionary *configuration([$CydgetController currentConfiguration]);
479 NSString *homepage([configuration objectForKey:@"Homepage"]);
480 [self loadURL:[NSURL URLWithString:homepage]];
484 - (void) webView:(WebView *)sender runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {
487 UIActionSheet *sheet = [[[UIActionSheet alloc]
489 buttons:[NSArray arrayWithObjects:@"OK", nil]
495 [sheet setBodyText:message];
496 [sheet popupAlertAnimated:YES];
499 - (BOOL) webView:(WebView *)sender runJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {
502 UIActionSheet *sheet = [[[UIActionSheet alloc]
504 buttons:[NSArray arrayWithObjects:@"OK", @"CANCEL", nil]
510 [sheet setNumberOfRows:1];
511 [sheet setBodyText:message];
512 [sheet popupAlertAnimated:YES];
514 NSRunLoop *loop([NSRunLoop currentRunLoop]);
515 NSDate *future([NSDate distantFuture]);
517 while (confirm_ == nil && [loop runMode:NSDefaultRunLoopMode beforeDate:future]);
519 NSNumber *confirm([confirm_ autorelease]);
523 return [confirm boolValue];
526 /* XXX: WebThreadLock? */
527 - (void) _fixScroller:(CGRect)bounds {
532 UIFormAssistant *assistant([UIFormAssistant sharedFormAssistant]);
533 CGRect peripheral([assistant peripheralFrame]);
534 extra = peripheral.size.height;
537 CGRect subrect([scroller_ frame]);
538 subrect.size.height -= [TPBottomLockBar defaultHeight];
539 subrect.size.height -= extra;
540 [scroller_ setScrollerIndicatorSubrect:subrect];
542 NSSize visible(NSMakeSize(subrect.size.width, subrect.size.height));
543 [document_ setValue:[NSValue valueWithSize:visible] forGestureAttribute:UIGestureAttributeVisibleSize];
546 size.height += extra;
547 size.height += [TPBottomLockBar defaultHeight];
548 [scroller_ setContentSize:size];
550 [scroller_ releaseRubberBandIfNecessary];
553 - (void) fixScroller {
554 CGRect bounds([document_ documentBounds]);
555 [self _fixScroller:bounds];
558 - (void) view:(UIView *)sender didSetFrame:(CGRect)frame {
560 [self _fixScroller:frame];
563 - (void) view:(UIView *)sender didSetFrame:(CGRect)frame oldFrame:(CGRect)old {
564 [self view:sender didSetFrame:frame];
567 - (void) webView:(WebView *)sender willBeginEditingFormElement:(id)element {
571 - (void) webView:(WebView *)sender didBeginEditingFormElement:(id)element {
575 - (void) webViewDidEndEditingFormElements:(WebView *)sender {
580 - (void) webView:(WebView *)sender didClearWindowObject:(WebScriptObject *)window forFrame:(WebFrame *)frame {
584 return [loading_ count] != 0;
587 - (void) reloadButtons {
588 if ([self isLoading])
589 [indicator_ startAnimation];
591 [indicator_ stopAnimation];
594 - (void) _finishLoading {
595 size_t count([loading_ count]);
597 [self autorelease];*/
598 if (reloading_ || count != 0)
600 [self reloadButtons];
603 - (BOOL) webView:(WebView *)sender shouldScrollToPoint:(struct CGPoint)point forFrame:(WebFrame *)frame {
604 return [document_ webView:sender shouldScrollToPoint:point forFrame:frame];
607 - (void) webView:(WebView *)sender didReceiveViewportArguments:(id)arguments forFrame:(WebFrame *)frame {
608 return [document_ webView:sender didReceiveViewportArguments:arguments forFrame:frame];
611 - (void) webView:(WebView *)sender needsScrollNotifications:(id)notifications forFrame:(WebFrame *)frame {
612 return [document_ webView:sender needsScrollNotifications:notifications forFrame:frame];
615 - (void) webView:(WebView *)sender didCommitLoadForFrame:(WebFrame *)frame {
616 return [document_ webView:sender didCommitLoadForFrame:frame];
619 - (void) webView:(WebView *)sender didReceiveDocTypeForFrame:(WebFrame *)frame {
620 return [document_ webView:sender didReceiveDocTypeForFrame:frame];
623 - (void) webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame {
624 [loading_ removeObject:[NSValue valueWithNonretainedObject:frame]];
625 [self _finishLoading];
626 return [document_ webView:sender didFinishLoadForFrame:frame];
629 - (void) webView:(WebView *)sender didStartProvisionalLoadForFrame:(WebFrame *)frame {
630 /*if ([loading_ count] == 0)
632 [loading_ addObject:[NSValue valueWithNonretainedObject:frame]];
634 if ([frame parentFrame] == nil) {
635 [document_ resignFirstResponder];
639 [scroller_ scrollPointVisibleAtTopLeft:CGPointZero];
641 if ([scroller_ respondsToSelector:@selector(setZoomScale:duration:)])
642 [scroller_ setZoomScale:1 duration:0];
643 else if ([scroller_ respondsToSelector:@selector(_setZoomScale:duration:)])
644 [scroller_ _setZoomScale:1 duration:0];
645 /*else if ([scroller_ respondsToSelector:@selector(setZoomScale:animated:)])
646 [scroller_ setZoomScale:1 animated:NO];*/
648 CGRect rect([scroller_ bounds]);
649 //rect.size.height = 0;
650 [document_ setFrame:rect];
653 [self reloadButtons];
656 - (void) _didFailWithError:(NSError *)error forFrame:(WebFrame *)frame {
658 /*if ([frame parentFrame] == nil)
659 [self autorelease];*/
661 [loading_ removeObject:[NSValue valueWithNonretainedObject:frame]];
662 [self _finishLoading];
667 if ([frame parentFrame] == nil) {
668 [self loadURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@?%@",
669 [[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"error" ofType:@"html"]] absoluteString],
670 [[error localizedDescription] stringByAddingPercentEscapes]
677 - (void) webView:(WebView *)sender didFailLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
678 [self _didFailWithError:error forFrame:frame];
681 - (void) webView:(WebView *)sender didFailProvisionalLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
682 [self _didFailWithError:error forFrame:frame];
685 - (void) webView:(WebView *)sender addMessageToConsole:(NSDictionary *)dictionary {
686 fprintf(stderr, "Console:%s\n", [[dictionary description] UTF8String]);
691 @interface WebCycriptLockScreenController : SBAwayViewPluginController {
696 @implementation WebCycriptLockScreenController
698 + (id) rootViewController {
699 return [[[self alloc] init] autorelease];
703 [self setView:[[[WebCydgetLockScreenView alloc] init] autorelease]];