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>
46 #import <QuartzCore/CALayer.h>
47 // XXX: fix the minimum requirement
48 extern NSString * const kCAFilterNearest;
50 #include <WebKit/DOMCSSPrimitiveValue.h>
51 #include <WebKit/DOMCSSStyleDeclaration.h>
52 #include <WebKit/DOMDocument.h>
53 #include <WebKit/DOMHTMLBodyElement.h>
54 #include <WebKit/DOMNodeList.h>
55 #include <WebKit/DOMRGBColor.h>
57 #include <WebKit/WebFrame.h>
58 #include <WebKit/WebPolicyDelegate.h>
59 #include <WebKit/WebPreferences.h>
60 #include <WebKit/WebScriptObject.h>
62 #import <WebKit/WebView.h>
63 #import <WebKit/WebView-WebPrivate.h>
65 #include <WebCore/Page.h>
66 #include <WebCore/Settings.h>
68 #include <WebCore/WebCoreThread.h>
69 #include <WebKit/WebPreferences-WebPrivate.h>
71 @interface WebView (UICaboodle)
72 - (void) setScriptDebugDelegate:(id)delegate;
73 - (void) _setFormDelegate:(id)delegate;
74 - (void) _setUIKitDelegate:(id)delegate;
75 - (void) setWebMailDelegate:(id)delegate;
76 - (void) _setLayoutInterval:(float)interval;
80 #define _forever for (;;)
82 _disused static unsigned trace_;
84 #define _trace() do { \
85 NSLog(@"_trace(%u)@%s:%u[%s]\n", \
86 trace_++, __FILE__, __LINE__, __FUNCTION__\
90 @protocol CydgetController
91 - (NSDictionary *) currentConfiguration;
94 static Class $CydgetController(objc_getClass("CydgetController"));
96 @interface NSString (UIKit)
97 - (NSString *) stringByAddingPercentEscapes;
100 @implementation UIWebDocumentView (WebCycript)
102 - (void) _setScrollerOffset:(CGPoint)offset {
103 UIScroller *scroller([self _scroller]);
105 CGSize size([scroller contentSize]);
106 CGSize bounds([scroller bounds].size);
109 max.x = size.width - bounds.width;
110 max.y = size.height - bounds.height;
118 offset.x = offset.x < 0 ? 0 : offset.x > max.x ? max.x : offset.x;
119 offset.y = offset.y < 0 ? 0 : offset.y > max.y ? max.y : offset.y;
121 [scroller setOffset:offset];
126 /* WebCycript Delegate {{{ */
127 @interface WebCycriptDelegate : NSObject {
128 _transient volatile id delegate_;
131 - (void) setDelegate:(id)delegate;
132 - (id) initWithDelegate:(id)delegate;
135 @implementation WebCycriptDelegate
137 - (void) setDelegate:(id)delegate {
138 delegate_ = delegate;
141 - (id) initWithDelegate:(id)delegate {
142 delegate_ = delegate;
146 - (void) webView:(WebView *)sender didClearWindowObject:(WebScriptObject *)window forFrame:(WebFrame *)frame {
147 if (delegate_ != nil)
148 return [delegate_ webView:sender didClearWindowObject:window forFrame:frame];
151 - (void) webView:(WebView *)sender didCommitLoadForFrame:(WebFrame *)frame {
152 if (delegate_ != nil)
153 return [delegate_ webView:sender didCommitLoadForFrame:frame];
156 - (void) webView:(WebView *)sender didFailLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
157 if (delegate_ != nil)
158 return [delegate_ webView:sender didFailLoadWithError:error forFrame:frame];
161 - (void) webView:(WebView *)sender didFailProvisionalLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
162 if (delegate_ != nil)
163 return [delegate_ webView:sender didFailProvisionalLoadWithError:error forFrame:frame];
166 - (void) webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame {
167 if (delegate_ != nil)
168 return [delegate_ webView:sender didFinishLoadForFrame:frame];
171 /*- (void) webView:(WebView *)sender didReceiveTitle:(NSString *)title forFrame:(WebFrame *)frame {
172 if (delegate_ != nil)
173 return [delegate_ webView:sender didReceiveTitle:title forFrame:frame];
176 - (void) webView:(WebView *)sender didStartProvisionalLoadForFrame:(WebFrame *)frame {
177 if (delegate_ != nil)
178 return [delegate_ webView:sender didStartProvisionalLoadForFrame:frame];
181 /*- (void) webView:(WebView *)sender resource:(id)identifier didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge fromDataSource:(WebDataSource *)source {
182 if (delegate_ != nil)
183 return [delegate_ webView:sender resource:identifier didReceiveAuthenticationChallenge:challenge fromDataSource:source];
186 /*- (NSURLRequest *) webView:(WebView *)sender resource:(id)identifier willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)redirectResponse fromDataSource:(WebDataSource *)source {
187 if (delegate_ != nil)
188 return [delegate_ webView:sender resource:identifier willSendRequest:request redirectResponse:redirectResponse fromDataSource:source];
192 - (IMP) methodForSelector:(SEL)sel {
193 if (IMP method = [super methodForSelector:sel])
195 fprintf(stderr, "methodForSelector:[%s] == NULL\n", sel_getName(sel));
199 - (BOOL) respondsToSelector:(SEL)sel {
200 if ([super respondsToSelector:sel])
202 // XXX: WebThreadCreateNSInvocation returns nil
203 //fprintf(stderr, "[%s]R?%s\n", class_getName(self->isa), sel_getName(sel));
204 return delegate_ == nil ? NO : [delegate_ respondsToSelector:sel];
207 - (NSMethodSignature *) methodSignatureForSelector:(SEL)sel {
208 if (NSMethodSignature *method = [super methodSignatureForSelector:sel])
210 //fprintf(stderr, "[%s]S?%s\n", class_getName(self->isa), sel_getName(sel));
211 if (delegate_ != nil)
212 if (NSMethodSignature *sig = [delegate_ methodSignatureForSelector:sel])
214 // XXX: I fucking hate Apple so very very bad
215 return [NSMethodSignature signatureWithObjCTypes:"v@:"];
218 - (void) forwardInvocation:(NSInvocation *)inv {
219 SEL sel = [inv selector];
220 if (delegate_ != nil && [delegate_ respondsToSelector:sel])
221 [inv invokeWithTarget:delegate_];
227 @interface WebCydgetLockScreenView : UIView {
228 WebCycriptDelegate *indirect_;
229 UIProgressIndicator *indicator_;
230 UIScroller *scroller_;
231 UIWebDocumentView *document_;
239 NSMutableSet *loading_;
246 @implementation WebCydgetLockScreenView
248 //#include "UICaboodle/UCInternal.h"
254 WebView *webview([document_ webView]);
255 [webview setFrameLoadDelegate:nil];
256 [webview setResourceLoadDelegate:nil];
257 [webview setUIDelegate:nil];
258 [webview setScriptDebugDelegate:nil];
259 [webview setPolicyDelegate:nil];
261 /* XXX: these are set by UIWebDocumentView
262 [webview setDownloadDelegate:nil];
263 [webview _setFormDelegate:nil];
264 [webview _setUIKitDelegate:nil];
265 [webview setEditingDelegate:nil];*/
267 /* XXX: no one sets this, ever
268 [webview setWebMailDelegate:nil];*/
270 [document_ setDelegate:nil];
271 [document_ setGestureDelegate:nil];
272 [document_ setFormEditingDelegate:nil];
273 [document_ setInteractionDelegate:nil];
275 [indirect_ setDelegate:nil];
284 [scroller_ setDelegate:nil];
290 [indicator_ release];
294 + (float) defaultWidth {
298 - (void) _setTileDrawingEnabled:(BOOL)enabled {
299 //[document_ setTileDrawingEnabled:enabled];
302 - (void) willStartGesturesInView:(UIView *)view forEvent:(GSEventRef)event {
303 [self _setTileDrawingEnabled:NO];
306 - (void) didFinishGesturesInView:(UIView *)view forEvent:(GSEventRef)event {
307 [self _setTileDrawingEnabled:YES];
308 [document_ redrawScaledDocument];
311 - (void) setViewportWidth:(float)width {
312 width_ = width != 0 ? width : [[self class] defaultWidth];
313 [document_ setViewportSize:CGSizeMake(width_, UIWebViewGrowsAndShrinksToFitHeight) forDocumentTypes:0x10];
316 - (void) scrollerWillStartDragging:(UIScroller *)scroller {
317 [self _setTileDrawingEnabled:NO];
320 - (void) scrollerDidEndDragging:(UIScroller *)scroller willSmoothScroll:(BOOL)smooth {
321 [self _setTileDrawingEnabled:YES];
324 - (void) scrollerDidEndDragging:(UIScroller *)scroller {
325 [self _setTileDrawingEnabled:YES];
328 - (void) loadRequest:(NSURLRequest *)request {
332 [document_ loadRequest:request];
336 - (void) loadURL:(NSURL *)url cachePolicy:(NSURLRequestCachePolicy)policy {
337 [self loadRequest:[NSURLRequest
344 - (void) loadURL:(NSURL *)url {
345 [self loadURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy];
349 CGRect frame = {{0, 0}, {320, 480}};
350 frame.size.height -= GSDefaultStatusBarHeight();
352 if ((self = [super initWithFrame:frame]) != nil) {
353 struct CGRect bounds([self bounds]);
355 scroller_ = [[UIScroller alloc] initWithFrame:bounds];
356 [self addSubview:scroller_];
358 [scroller_ setFixedBackgroundPattern:YES];
359 [scroller_ setBackgroundColor:[UIColor blackColor]];
361 [scroller_ setScrollingEnabled:YES];
362 [scroller_ setClipsSubviews:YES];
363 [scroller_ setAllowsRubberBanding:YES];
365 [scroller_ setDelegate:self];
366 [scroller_ setBounces:YES];
367 [scroller_ setScrollHysteresis:8];
368 [scroller_ setThumbDetectionEnabled:NO];
369 [scroller_ setDirectionalScrolling:YES];
370 [scroller_ setScrollDecelerationFactor:0.99]; /* 0.989324 */
371 [scroller_ setEventMode:YES];
372 [scroller_ setShowBackgroundShadow:NO]; /* YES */
373 [scroller_ setAllowsRubberBanding:YES]; /* Vertical */
374 [scroller_ setAdjustForContentSizeChange:YES]; /* NO */
376 CGRect rect([scroller_ bounds]);
377 //rect.size.height = 0;
381 document_ = [[UIWebDocumentView alloc] initWithFrame:rect];
382 WebView *webview([document_ webView]);
384 [document_ setBackgroundColor:[UIColor blackColor]];
385 if ([document_ respondsToSelector:@selector(setDrawsBackground:)])
386 [document_ setDrawsBackground:NO];
387 [webview setDrawsBackground:NO];
389 [webview setPreferencesIdentifier:@"WebCycript"];
391 [document_ setTileSize:CGSizeMake(rect.size.width, 500)];
393 if ([document_ respondsToSelector:@selector(enableReachability)])
394 [document_ enableReachability];
396 [document_ setAllowsMessaging:YES];
398 if ([document_ respondsToSelector:@selector(useSelectionAssistantWithMode:)])
399 [document_ useSelectionAssistantWithMode:0];
401 [document_ setTilingEnabled:YES];
402 [document_ setDrawsGrid:NO];
403 [document_ setLogsTilingChanges:NO];
404 [document_ setTileMinificationFilter:kCAFilterNearest];
406 if ([document_ respondsToSelector:@selector(setDataDetectorTypes:)])
407 /* XXX: abstractify */
408 [document_ setDataDetectorTypes:0x80000000];
410 [document_ setDetectsPhoneNumbers:NO];
412 [document_ setAutoresizes:YES];
414 [document_ setMinimumScale:0.25f forDocumentTypes:0x10];
415 [document_ setMaximumScale:5.00f forDocumentTypes:0x10];
416 [document_ setInitialScale:UIWebViewScalesToFitScale forDocumentTypes:0x10];
417 //[document_ setViewportSize:CGSizeMake(980, UIWebViewGrowsAndShrinksToFitHeight) forDocumentTypes:0x10];
419 [document_ setViewportSize:CGSizeMake(320, UIWebViewGrowsAndShrinksToFitHeight) forDocumentTypes:0x2];
421 [document_ setMinimumScale:1.00f forDocumentTypes:0x8];
422 [document_ setInitialScale:UIWebViewScalesToFitScale forDocumentTypes:0x8];
423 [document_ setViewportSize:CGSizeMake(320, UIWebViewGrowsAndShrinksToFitHeight) forDocumentTypes:0x8];
425 [document_ _setDocumentType:0x4];
427 if ([document_ respondsToSelector:@selector(setZoomsFocusedFormControl:)])
428 [document_ setZoomsFocusedFormControl:YES];
429 [document_ setContentsPosition:7];
430 [document_ setEnabledGestures:0xa];
431 [document_ setValue:[NSNumber numberWithBool:YES] forGestureAttribute:UIGestureAttributeIsZoomRubberBandEnabled];
432 [document_ setValue:[NSNumber numberWithBool:YES] forGestureAttribute:UIGestureAttributeUpdatesScroller];
434 [document_ setSmoothsFonts:YES];
435 [document_ setAllowsImageSheet:YES];
436 [webview _setUsesLoaderCache:YES];
438 [webview setGroupName:@"CydgetGroup"];
440 WebPreferences *preferences([webview preferences]);
442 if ([webview respondsToSelector:@selector(_setLayoutInterval:)])
443 [webview _setLayoutInterval:0];
445 [preferences _setLayoutInterval:0];
447 [self setViewportWidth:0];
449 [document_ setDelegate:self];
450 [document_ setGestureDelegate:self];
451 [document_ setFormEditingDelegate:self];
452 [document_ setInteractionDelegate:self];
454 [scroller_ addSubview:document_];
456 //NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
458 indirect_ = [[WebCycriptDelegate alloc] initWithDelegate:self];
460 [webview setFrameLoadDelegate:indirect_];
461 [webview setPolicyDelegate:indirect_];
462 [webview setResourceLoadDelegate:indirect_];
463 [webview setUIDelegate:indirect_];
465 /* XXX: do not turn this on under penalty of extreme pain */
466 [webview setScriptDebugDelegate:nil];
470 CGSize indsize([UIProgressIndicator defaultSizeForStyle:UIProgressIndicatorStyleMediumWhite]);
471 indicator_ = [[UIProgressIndicator alloc] initWithFrame:CGRectMake(281, 12, indsize.width, indsize.height)];
472 [indicator_ setStyle:UIProgressIndicatorStyleMediumWhite];
474 [self setAutoresizingMask:UIViewAutoresizingFlexibleHeight];
475 [scroller_ setAutoresizingMask:UIViewAutoresizingFlexibleHeight];
477 NSDictionary *configuration([$CydgetController currentConfiguration]);
478 NSString *homepage([configuration objectForKey:@"Homepage"]);
479 [self loadURL:[NSURL URLWithString:homepage]];
483 - (void) webView:(WebView *)sender runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {
486 UIActionSheet *sheet = [[[UIActionSheet alloc]
488 buttons:[NSArray arrayWithObjects:@"OK", nil]
494 [sheet setBodyText:message];
495 [sheet popupAlertAnimated:YES];
498 - (BOOL) webView:(WebView *)sender runJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {
501 UIActionSheet *sheet = [[[UIActionSheet alloc]
503 buttons:[NSArray arrayWithObjects:@"OK", @"CANCEL", nil]
509 [sheet setNumberOfRows:1];
510 [sheet setBodyText:message];
511 [sheet popupAlertAnimated:YES];
513 NSRunLoop *loop([NSRunLoop currentRunLoop]);
514 NSDate *future([NSDate distantFuture]);
516 while (confirm_ == nil && [loop runMode:NSDefaultRunLoopMode beforeDate:future]);
518 NSNumber *confirm([confirm_ autorelease]);
522 return [confirm boolValue];
525 /* XXX: WebThreadLock? */
526 - (void) _fixScroller:(CGRect)bounds {
531 UIFormAssistant *assistant([UIFormAssistant sharedFormAssistant]);
532 CGRect peripheral([assistant peripheralFrame]);
533 extra = peripheral.size.height;
536 CGRect subrect([scroller_ frame]);
537 subrect.size.height -= extra;
538 [scroller_ setScrollerIndicatorSubrect:subrect];
540 NSSize visible(NSMakeSize(subrect.size.width, subrect.size.height));
541 [document_ setValue:[NSValue valueWithSize:visible] forGestureAttribute:UIGestureAttributeVisibleSize];
544 size.height += extra;
545 [scroller_ setContentSize:size];
547 [scroller_ releaseRubberBandIfNecessary];
550 - (void) fixScroller {
551 CGRect bounds([document_ documentBounds]);
552 [self _fixScroller:bounds];
555 - (void) view:(UIView *)sender didSetFrame:(CGRect)frame {
557 [self _fixScroller:frame];
560 - (void) view:(UIView *)sender didSetFrame:(CGRect)frame oldFrame:(CGRect)old {
561 [self view:sender didSetFrame:frame];
564 - (void) webView:(WebView *)sender willBeginEditingFormElement:(id)element {
568 - (void) webView:(WebView *)sender didBeginEditingFormElement:(id)element {
572 - (void) webViewDidEndEditingFormElements:(WebView *)sender {
577 - (void) webView:(WebView *)sender didClearWindowObject:(WebScriptObject *)window forFrame:(WebFrame *)frame {
581 return [loading_ count] != 0;
584 - (void) reloadButtons {
585 if ([self isLoading])
586 [indicator_ startAnimation];
588 [indicator_ stopAnimation];
591 - (void) _finishLoading {
592 size_t count([loading_ count]);
594 [self autorelease];*/
595 if (reloading_ || count != 0)
597 [self reloadButtons];
600 - (BOOL) webView:(WebView *)sender shouldScrollToPoint:(struct CGPoint)point forFrame:(WebFrame *)frame {
601 return [document_ webView:sender shouldScrollToPoint:point forFrame:frame];
604 - (void) webView:(WebView *)sender didReceiveViewportArguments:(id)arguments forFrame:(WebFrame *)frame {
605 return [document_ webView:sender didReceiveViewportArguments:arguments forFrame:frame];
608 - (void) webView:(WebView *)sender needsScrollNotifications:(id)notifications forFrame:(WebFrame *)frame {
609 return [document_ webView:sender needsScrollNotifications:notifications forFrame:frame];
612 - (void) webView:(WebView *)sender didCommitLoadForFrame:(WebFrame *)frame {
613 return [document_ webView:sender didCommitLoadForFrame:frame];
616 - (void) webView:(WebView *)sender didReceiveDocTypeForFrame:(WebFrame *)frame {
617 return [document_ webView:sender didReceiveDocTypeForFrame:frame];
620 - (void) webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame {
621 [loading_ removeObject:[NSValue valueWithNonretainedObject:frame]];
622 [self _finishLoading];
623 return [document_ webView:sender didFinishLoadForFrame:frame];
626 - (void) webView:(WebView *)sender didStartProvisionalLoadForFrame:(WebFrame *)frame {
627 /*if ([loading_ count] == 0)
629 [loading_ addObject:[NSValue valueWithNonretainedObject:frame]];
631 if ([frame parentFrame] == nil) {
632 [document_ resignFirstResponder];
636 [scroller_ scrollPointVisibleAtTopLeft:CGPointZero];
638 if ([scroller_ respondsToSelector:@selector(setZoomScale:duration:)])
639 [scroller_ setZoomScale:1 duration:0];
640 else if ([scroller_ respondsToSelector:@selector(_setZoomScale:duration:)])
641 [scroller_ _setZoomScale:1 duration:0];
642 /*else if ([scroller_ respondsToSelector:@selector(setZoomScale:animated:)])
643 [scroller_ setZoomScale:1 animated:NO];*/
645 CGRect rect([scroller_ bounds]);
646 //rect.size.height = 0;
647 [document_ setFrame:rect];
650 [self reloadButtons];
653 - (void) _didFailWithError:(NSError *)error forFrame:(WebFrame *)frame {
655 /*if ([frame parentFrame] == nil)
656 [self autorelease];*/
658 [loading_ removeObject:[NSValue valueWithNonretainedObject:frame]];
659 [self _finishLoading];
664 if ([frame parentFrame] == nil) {
665 [self loadURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@?%@",
666 [[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"error" ofType:@"html"]] absoluteString],
667 [[error localizedDescription] stringByAddingPercentEscapes]
674 - (void) webView:(WebView *)sender didFailLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
675 [self _didFailWithError:error forFrame:frame];
678 - (void) webView:(WebView *)sender didFailProvisionalLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
679 [self _didFailWithError:error forFrame:frame];
682 - (void) webView:(WebView *)sender addMessageToConsole:(NSDictionary *)dictionary {
683 fprintf(stderr, "Console:%s\n", [[dictionary description] UTF8String]);
688 @interface WebCycriptLockScreenController : SBAwayViewPluginController {
693 @implementation WebCycriptLockScreenController
695 + (id) rootViewController {
696 return [[[self alloc] init] autorelease];
700 [self setView:[[[WebCydgetLockScreenView alloc] init] autorelease]];