EventSenderProxy.mm 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. /*
  2. * Copyright (C) 2011 Apple Inc. All rights reserved.
  3. * Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. *
  14. * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
  15. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  16. * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  17. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
  18. * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  19. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  20. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  21. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  22. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  23. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  24. * THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #import "config.h"
  27. #import "EventSenderProxy.h"
  28. #import "PlatformWebView.h"
  29. #import "StringFunctions.h"
  30. #import "TestController.h"
  31. #import <wtf/RetainPtr.h>
  32. #import <WebKit2/WKString.h>
  33. namespace WTR {
  34. enum MouseAction {
  35. MouseDown,
  36. MouseUp,
  37. MouseDragged
  38. };
  39. // Match the DOM spec (sadly the DOM spec does not provide an enum)
  40. enum MouseButton {
  41. LeftMouseButton = 0,
  42. MiddleMouseButton = 1,
  43. RightMouseButton = 2,
  44. NoMouseButton = -1
  45. };
  46. struct KeyMappingEntry {
  47. int macKeyCode;
  48. int macNumpadKeyCode;
  49. unichar character;
  50. NSString* characterName;
  51. };
  52. static NSEventType eventTypeForMouseButtonAndAction(int button, MouseAction action)
  53. {
  54. switch (button) {
  55. case LeftMouseButton:
  56. switch (action) {
  57. case MouseDown:
  58. return NSLeftMouseDown;
  59. case MouseUp:
  60. return NSLeftMouseUp;
  61. case MouseDragged:
  62. return NSLeftMouseDragged;
  63. }
  64. case RightMouseButton:
  65. switch (action) {
  66. case MouseDown:
  67. return NSRightMouseDown;
  68. case MouseUp:
  69. return NSRightMouseUp;
  70. case MouseDragged:
  71. return NSRightMouseDragged;
  72. }
  73. default:
  74. switch (action) {
  75. case MouseDown:
  76. return NSOtherMouseDown;
  77. case MouseUp:
  78. return NSOtherMouseUp;
  79. case MouseDragged:
  80. return NSOtherMouseDragged;
  81. }
  82. }
  83. assert(0);
  84. return static_cast<NSEventType>(0);
  85. }
  86. static int buildModifierFlags(WKEventModifiers modifiers)
  87. {
  88. int flags = 0;
  89. if (modifiers & kWKEventModifiersControlKey)
  90. flags |= NSControlKeyMask;
  91. if (modifiers & kWKEventModifiersShiftKey)
  92. flags |= NSShiftKeyMask;
  93. if (modifiers & kWKEventModifiersAltKey)
  94. flags |= NSAlternateKeyMask;
  95. if (modifiers & kWKEventModifiersMetaKey)
  96. flags |= NSCommandKeyMask;
  97. return flags;
  98. }
  99. EventSenderProxy::EventSenderProxy(TestController* testController)
  100. : m_testController(testController)
  101. , m_time(0)
  102. , m_position()
  103. , m_leftMouseButtonDown(false)
  104. , m_clickCount(0)
  105. , m_clickTime(0)
  106. , m_clickPosition()
  107. , m_clickButton(kWKEventMouseButtonNoButton)
  108. , eventNumber(0)
  109. {
  110. }
  111. EventSenderProxy::~EventSenderProxy()
  112. {
  113. }
  114. void EventSenderProxy::updateClickCountForButton(int button)
  115. {
  116. if (m_time - m_clickTime < 1 && m_position == m_clickPosition && button == m_clickButton) {
  117. ++m_clickCount;
  118. m_clickTime = m_time;
  119. return;
  120. }
  121. m_clickCount = 1;
  122. m_clickTime = m_time;
  123. m_clickPosition = m_position;
  124. m_clickButton = button;
  125. }
  126. void EventSenderProxy::mouseDown(unsigned buttonNumber, WKEventModifiers modifiers)
  127. {
  128. updateClickCountForButton(buttonNumber);
  129. NSEventType eventType = eventTypeForMouseButtonAndAction(buttonNumber, MouseDown);
  130. NSEvent *event = [NSEvent mouseEventWithType:eventType
  131. location:NSMakePoint(m_position.x, m_position.y)
  132. modifierFlags:buildModifierFlags(modifiers)
  133. timestamp:currentEventTime()
  134. windowNumber:[m_testController->mainWebView()->platformWindow() windowNumber]
  135. context:[NSGraphicsContext currentContext]
  136. eventNumber:++eventNumber
  137. clickCount:m_clickCount
  138. pressure:0.0];
  139. NSView *targetView = [m_testController->mainWebView()->platformView() hitTest:[event locationInWindow]];
  140. if (targetView) {
  141. [targetView mouseDown:event];
  142. if (buttonNumber == LeftMouseButton)
  143. m_leftMouseButtonDown = true;
  144. }
  145. }
  146. void EventSenderProxy::mouseUp(unsigned buttonNumber, WKEventModifiers modifiers)
  147. {
  148. NSEventType eventType = eventTypeForMouseButtonAndAction(buttonNumber, MouseUp);
  149. NSEvent *event = [NSEvent mouseEventWithType:eventType
  150. location:NSMakePoint(m_position.x, m_position.y)
  151. modifierFlags:buildModifierFlags(modifiers)
  152. timestamp:currentEventTime()
  153. windowNumber:[m_testController->mainWebView()->platformWindow() windowNumber]
  154. context:[NSGraphicsContext currentContext]
  155. eventNumber:++eventNumber
  156. clickCount:m_clickCount
  157. pressure:0.0];
  158. NSView *targetView = [m_testController->mainWebView()->platformView() hitTest:[event locationInWindow]];
  159. targetView = targetView ? targetView : m_testController->mainWebView()->platformView();
  160. assert(targetView);
  161. [targetView mouseUp:event];
  162. if (buttonNumber == LeftMouseButton)
  163. m_leftMouseButtonDown = false;
  164. m_clickTime = [event timestamp];
  165. m_clickPosition = m_position;
  166. }
  167. void EventSenderProxy::mouseMoveTo(double x, double y)
  168. {
  169. NSView *view = m_testController->mainWebView()->platformView();
  170. NSPoint position = [view convertPoint:NSMakePoint(x, y) toView:nil];
  171. m_position.x = position.x;
  172. m_position.y = position.y;
  173. NSEvent *event = [NSEvent mouseEventWithType:(m_leftMouseButtonDown ? NSLeftMouseDragged : NSMouseMoved)
  174. location:position
  175. modifierFlags:0
  176. timestamp:currentEventTime()
  177. windowNumber:[[view window] windowNumber]
  178. context:[NSGraphicsContext currentContext]
  179. eventNumber:++eventNumber
  180. clickCount:(m_leftMouseButtonDown ? m_clickCount : 0)
  181. pressure:0.0];
  182. NSView *targetView = [m_testController->mainWebView()->platformView() hitTest:[event locationInWindow]];
  183. if (targetView)
  184. [targetView mouseMoved:event];
  185. }
  186. void EventSenderProxy::leapForward(int milliseconds)
  187. {
  188. m_time += milliseconds / 1000.0;
  189. }
  190. void EventSenderProxy::keyDown(WKStringRef key, WKEventModifiers modifiers, unsigned keyLocation)
  191. {
  192. NSString* character = [NSString stringWithCString:toSTD(key).c_str()
  193. encoding:[NSString defaultCStringEncoding]];
  194. NSString *eventCharacter = character;
  195. unsigned short keyCode = 0;
  196. if ([character isEqualToString:@"leftArrow"]) {
  197. const unichar ch = NSLeftArrowFunctionKey;
  198. eventCharacter = [NSString stringWithCharacters:&ch length:1];
  199. keyCode = 0x7B;
  200. } else if ([character isEqualToString:@"rightArrow"]) {
  201. const unichar ch = NSRightArrowFunctionKey;
  202. eventCharacter = [NSString stringWithCharacters:&ch length:1];
  203. keyCode = 0x7C;
  204. } else if ([character isEqualToString:@"upArrow"]) {
  205. const unichar ch = NSUpArrowFunctionKey;
  206. eventCharacter = [NSString stringWithCharacters:&ch length:1];
  207. keyCode = 0x7E;
  208. } else if ([character isEqualToString:@"downArrow"]) {
  209. const unichar ch = NSDownArrowFunctionKey;
  210. eventCharacter = [NSString stringWithCharacters:&ch length:1];
  211. keyCode = 0x7D;
  212. } else if ([character isEqualToString:@"pageUp"]) {
  213. const unichar ch = NSPageUpFunctionKey;
  214. eventCharacter = [NSString stringWithCharacters:&ch length:1];
  215. keyCode = 0x74;
  216. } else if ([character isEqualToString:@"pageDown"]) {
  217. const unichar ch = NSPageDownFunctionKey;
  218. eventCharacter = [NSString stringWithCharacters:&ch length:1];
  219. keyCode = 0x79;
  220. } else if ([character isEqualToString:@"home"]) {
  221. const unichar ch = NSHomeFunctionKey;
  222. eventCharacter = [NSString stringWithCharacters:&ch length:1];
  223. keyCode = 0x73;
  224. } else if ([character isEqualToString:@"end"]) {
  225. const unichar ch = NSEndFunctionKey;
  226. eventCharacter = [NSString stringWithCharacters:&ch length:1];
  227. keyCode = 0x77;
  228. } else if ([character isEqualToString:@"insert"]) {
  229. const unichar ch = NSInsertFunctionKey;
  230. eventCharacter = [NSString stringWithCharacters:&ch length:1];
  231. keyCode = 0x72;
  232. } else if ([character isEqualToString:@"delete"]) {
  233. const unichar ch = NSDeleteFunctionKey;
  234. eventCharacter = [NSString stringWithCharacters:&ch length:1];
  235. keyCode = 0x75;
  236. } else if ([character isEqualToString:@"printScreen"]) {
  237. const unichar ch = NSPrintScreenFunctionKey;
  238. eventCharacter = [NSString stringWithCharacters:&ch length:1];
  239. keyCode = 0x0; // There is no known virtual key code for PrintScreen.
  240. } else if ([character isEqualToString:@"cyrillicSmallLetterA"]) {
  241. const unichar ch = 0x0430;
  242. eventCharacter = [NSString stringWithCharacters:&ch length:1];
  243. keyCode = 0x3; // Shares key with "F" on Russian layout.
  244. } else if ([character isEqualToString:@"leftControl"]) {
  245. const unichar ch = 0xFFE3;
  246. eventCharacter = [NSString stringWithCharacters:&ch length:1];
  247. keyCode = 0x3B;
  248. } else if ([character isEqualToString:@"leftShift"]) {
  249. const unichar ch = 0xFFE1;
  250. eventCharacter = [NSString stringWithCharacters:&ch length:1];
  251. keyCode = 0x38;
  252. } else if ([character isEqualToString:@"leftAlt"]) {
  253. const unichar ch = 0xFFE7;
  254. eventCharacter = [NSString stringWithCharacters:&ch length:1];
  255. keyCode = 0x3A;
  256. } else if ([character isEqualToString:@"rightControl"]) {
  257. const unichar ch = 0xFFE4;
  258. eventCharacter = [NSString stringWithCharacters:&ch length:1];
  259. keyCode = 0x3E;
  260. } else if ([character isEqualToString:@"rightShift"]) {
  261. const unichar ch = 0xFFE2;
  262. eventCharacter = [NSString stringWithCharacters:&ch length:1];
  263. keyCode = 0x3C;
  264. } else if ([character isEqualToString:@"rightAlt"]) {
  265. const unichar ch = 0xFFE8;
  266. eventCharacter = [NSString stringWithCharacters:&ch length:1];
  267. keyCode = 0x3D;
  268. }
  269. // Compare the input string with the function-key names defined by the DOM spec (i.e. "F1",...,"F24").
  270. // If the input string is a function-key name, set its key code.
  271. for (unsigned i = 1; i <= 24; i++) {
  272. if ([character isEqualToString:[NSString stringWithFormat:@"F%u", i]]) {
  273. const unichar ch = NSF1FunctionKey + (i - 1);
  274. eventCharacter = [NSString stringWithCharacters:&ch length:1];
  275. switch (i) {
  276. case 1: keyCode = 0x7A; break;
  277. case 2: keyCode = 0x78; break;
  278. case 3: keyCode = 0x63; break;
  279. case 4: keyCode = 0x76; break;
  280. case 5: keyCode = 0x60; break;
  281. case 6: keyCode = 0x61; break;
  282. case 7: keyCode = 0x62; break;
  283. case 8: keyCode = 0x64; break;
  284. case 9: keyCode = 0x65; break;
  285. case 10: keyCode = 0x6D; break;
  286. case 11: keyCode = 0x67; break;
  287. case 12: keyCode = 0x6F; break;
  288. case 13: keyCode = 0x69; break;
  289. case 14: keyCode = 0x6B; break;
  290. case 15: keyCode = 0x71; break;
  291. case 16: keyCode = 0x6A; break;
  292. case 17: keyCode = 0x40; break;
  293. case 18: keyCode = 0x4F; break;
  294. case 19: keyCode = 0x50; break;
  295. case 20: keyCode = 0x5A; break;
  296. }
  297. }
  298. }
  299. // FIXME: No keyCode is set for most keys.
  300. if ([character isEqualToString:@"\t"])
  301. keyCode = 0x30;
  302. else if ([character isEqualToString:@" "])
  303. keyCode = 0x31;
  304. else if ([character isEqualToString:@"\r"])
  305. keyCode = 0x24;
  306. else if ([character isEqualToString:@"\n"])
  307. keyCode = 0x4C;
  308. else if ([character isEqualToString:@"\x8"])
  309. keyCode = 0x33;
  310. else if ([character isEqualToString:@"a"])
  311. keyCode = 0x00;
  312. else if ([character isEqualToString:@"b"])
  313. keyCode = 0x0B;
  314. else if ([character isEqualToString:@"d"])
  315. keyCode = 0x02;
  316. else if ([character isEqualToString:@"e"])
  317. keyCode = 0x0E;
  318. KeyMappingEntry table[] = {
  319. {0x2F, 0x41, '.', nil},
  320. {0, 0x43, '*', nil},
  321. {0, 0x45, '+', nil},
  322. {0, 0x47, NSClearLineFunctionKey, @"clear"},
  323. {0x2C, 0x4B, '/', nil},
  324. {0, 0x4C, 3, @"enter" },
  325. {0x1B, 0x4E, '-', nil},
  326. {0x18, 0x51, '=', nil},
  327. {0x1D, 0x52, '0', nil},
  328. {0x12, 0x53, '1', nil},
  329. {0x13, 0x54, '2', nil},
  330. {0x14, 0x55, '3', nil},
  331. {0x15, 0x56, '4', nil},
  332. {0x17, 0x57, '5', nil},
  333. {0x16, 0x58, '6', nil},
  334. {0x1A, 0x59, '7', nil},
  335. {0x1C, 0x5B, '8', nil},
  336. {0x19, 0x5C, '9', nil},
  337. };
  338. for (unsigned i = 0; i < WTF_ARRAY_LENGTH(table); ++i) {
  339. NSString* currentCharacterString = [NSString stringWithCharacters:&table[i].character length:1];
  340. if ([character isEqualToString:currentCharacterString] || [character isEqualToString:table[i].characterName]) {
  341. if (keyLocation == 0x03 /*DOM_KEY_LOCATION_NUMPAD*/)
  342. keyCode = table[i].macNumpadKeyCode;
  343. else
  344. keyCode = table[i].macKeyCode;
  345. eventCharacter = currentCharacterString;
  346. break;
  347. }
  348. }
  349. NSString *charactersIgnoringModifiers = eventCharacter;
  350. int modifierFlags = 0;
  351. if ([character length] == 1 && [character characterAtIndex:0] >= 'A' && [character characterAtIndex:0] <= 'Z') {
  352. modifierFlags |= NSShiftKeyMask;
  353. charactersIgnoringModifiers = [character lowercaseString];
  354. }
  355. modifierFlags |= buildModifierFlags(modifiers);
  356. if (keyLocation == 0x03 /*DOM_KEY_LOCATION_NUMPAD*/)
  357. modifierFlags |= NSNumericPadKeyMask;
  358. // FIXME: [[[mainFrame frameView] documentView] layout];
  359. NSEvent *event = [NSEvent keyEventWithType:NSKeyDown
  360. location:NSMakePoint(5, 5)
  361. modifierFlags:modifierFlags
  362. timestamp:currentEventTime()
  363. windowNumber:[m_testController->mainWebView()->platformWindow() windowNumber]
  364. context:[NSGraphicsContext currentContext]
  365. characters:eventCharacter
  366. charactersIgnoringModifiers:charactersIgnoringModifiers
  367. isARepeat:NO
  368. keyCode:keyCode];
  369. [[m_testController->mainWebView()->platformWindow() firstResponder] keyDown:event];
  370. event = [NSEvent keyEventWithType:NSKeyUp
  371. location:NSMakePoint(5, 5)
  372. modifierFlags:modifierFlags
  373. timestamp:currentEventTime()
  374. windowNumber:[m_testController->mainWebView()->platformWindow() windowNumber]
  375. context:[NSGraphicsContext currentContext]
  376. characters:eventCharacter
  377. charactersIgnoringModifiers:charactersIgnoringModifiers
  378. isARepeat:NO
  379. keyCode:keyCode];
  380. [[m_testController->mainWebView()->platformWindow() firstResponder] keyUp:event];
  381. }
  382. void EventSenderProxy::mouseScrollBy(int x, int y)
  383. {
  384. RetainPtr<CGEventRef> cgScrollEvent = adoptCF(CGEventCreateScrollWheelEvent(0, kCGScrollEventUnitLine, 2, y, x));
  385. // CGEvent locations are in global display coordinates.
  386. CGPoint lastGlobalMousePosition = CGPointMake(m_position.x, [[NSScreen mainScreen] frame].size.height - m_position.y);
  387. CGEventSetLocation(cgScrollEvent.get(), lastGlobalMousePosition);
  388. NSEvent *event = [NSEvent eventWithCGEvent:cgScrollEvent.get()];
  389. if (NSView *targetView = [m_testController->mainWebView()->platformView() hitTest:[event locationInWindow]])
  390. [targetView scrollWheel:event];
  391. }
  392. void EventSenderProxy::continuousMouseScrollBy(int x, int y, bool paged)
  393. {
  394. // FIXME: Implement this.
  395. return;
  396. }
  397. } // namespace WTR