]> git.saurik.com Git - cydia.git/blob - CyteKit/dispatchEvent.mm
9efe1cb4cd586ae379bd577a0150c76b6f0fdd7d
[cydia.git] / CyteKit / dispatchEvent.mm
1 /* Cydia - iPhone UIKit Front-End for Debian APT
2 * Copyright (C) 2008-2015 Jay Freeman (saurik)
3 */
4
5 /* GNU General Public License, Version 3 {{{ */
6 /*
7 * Cydia is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published
9 * by the Free Software Foundation, either version 3 of the License,
10 * or (at your option) any later version.
11 *
12 * Cydia is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with Cydia. If not, see <http://www.gnu.org/licenses/>.
19 **/
20 /* }}} */
21
22 #include "CyteKit/dispatchEvent.h"
23 #include "CyteKit/WebThreadLocked.hpp"
24
25 #include <WebCore/WebEvent.h>
26
27 #include <WebKit/WebFrame.h>
28 #include <WebKit/WebScriptObject.h>
29 #include <WebKit/WebView.h>
30
31 #include "Substrate.hpp"
32
33 @implementation UIWebDocumentView (CyteDispatchEvent)
34
35 - (void) dispatchEvent:(NSString *)event {
36 WebThreadLocked lock;
37
38 NSString *script([NSString stringWithFormat:@
39 "(function() {"
40 "var event = this.document.createEvent('Events');"
41 "event.initEvent('%@', false, false);"
42 "this.document.dispatchEvent(event);"
43 "})();"
44 , event]);
45
46 NSMutableArray *frames([NSMutableArray arrayWithObjects:
47 [[self webView] mainFrame]
48 , nil]);
49
50 while (WebFrame *frame = [frames lastObject]) {
51 WebScriptObject *object([frame windowObject]);
52 [object evaluateWebScript:script];
53 [frames removeLastObject];
54 [frames addObjectsFromArray:[frame childFrames]];
55 }
56 }
57
58 @end
59
60 MSHook(void, UIWebBrowserView$_webTouchEventsRecognized$, UIWebBrowserView *self, SEL _cmd, UIWebTouchEventsGestureRecognizer *recognizer) {
61 _UIWebBrowserView$_webTouchEventsRecognized$(self, _cmd, recognizer);
62
63 switch ([recognizer type]) {
64 case WebEventTouchEnd:
65 [self dispatchEvent:@"CydiaTouchEnd"];
66 break;
67
68 case WebEventTouchCancel:
69 [self dispatchEvent:@"CydiaTouchCancel"];
70 break;
71 }
72 }
73
74 __attribute__((__constructor__)) static void $() {
75 if (Class $UIWebBrowserView = objc_getClass("UIWebBrowserView")) {
76 if (Method method = class_getInstanceMethod($UIWebBrowserView, @selector(_webTouchEventsRecognized:))) {
77 _UIWebBrowserView$_webTouchEventsRecognized$ = reinterpret_cast<void (*)(UIWebBrowserView *, SEL, UIWebTouchEventsGestureRecognizer *)>(method_getImplementation(method));
78 method_setImplementation(method, reinterpret_cast<IMP>(&$UIWebBrowserView$_webTouchEventsRecognized$));
79 }
80 }
81 }