| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #import <Cocoa/Cocoa.h> | 5 #import <Cocoa/Cocoa.h> |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "webkit/plugins/npapi/plugin_web_event_converter_mac.h" | 8 #include "webkit/plugins/npapi/plugin_web_event_converter_mac.h" |
| 9 #include "third_party/WebKit/Source/WebKit/chromium/public/WebInputEvent.h" | 9 #include "third_party/WebKit/Source/WebKit/chromium/public/WebInputEvent.h" |
| 10 | 10 |
| 11 using WebKit::WebInputEvent; | 11 using WebKit::WebInputEvent; |
| 12 using WebKit::WebKeyboardEvent; | 12 using WebKit::WebKeyboardEvent; |
| 13 using WebKit::WebMouseEvent; | 13 using WebKit::WebMouseEvent; |
| 14 using WebKit::WebMouseWheelEvent; | 14 using WebKit::WebMouseWheelEvent; |
| 15 | 15 |
| 16 namespace webkit { | 16 namespace webkit { |
| 17 namespace npapi { | 17 namespace npapi { |
| 18 | 18 |
| 19 namespace { | 19 namespace { |
| 20 | 20 |
| 21 // Returns true if the given key is a modifier key. |
| 22 bool KeyIsModifier(int native_key_code) { |
| 23 switch (native_key_code) { |
| 24 case 55: // Left command |
| 25 case 54: // Right command |
| 26 case 58: // Left option |
| 27 case 61: // Right option |
| 28 case 59: // Left control |
| 29 case 62: // Right control |
| 30 case 56: // Left shift |
| 31 case 60: // Right shift |
| 32 case 57: // Caps lock |
| 33 return true; |
| 34 default: |
| 35 return false; |
| 36 } |
| 37 } |
| 38 |
| 21 // Returns true if the caps lock flag should be set for the given event. | 39 // Returns true if the caps lock flag should be set for the given event. |
| 22 bool CapsLockIsActive(const WebInputEvent& event) { | 40 bool CapsLockIsActive(const WebInputEvent& event) { |
| 23 // Only key events have accurate information for the caps lock flag; see | 41 // Only key events have accurate information for the caps lock flag; see |
| 24 // <https://bugs.webkit.org/show_bug.cgi?id=46518>. | 42 // <https://bugs.webkit.org/show_bug.cgi?id=46518>. |
| 25 // For other types, use the live state. | 43 // For other types, use the live state. |
| 26 if (WebInputEvent::isKeyboardEventType(event.type)) | 44 if (WebInputEvent::isKeyboardEventType(event.type)) |
| 27 return (event.modifiers & WebInputEvent::CapsLockOn) != 0; | 45 return (event.modifiers & WebInputEvent::CapsLockOn) != 0; |
| 28 else | 46 else |
| 29 return ([[NSApp currentEvent] modifierFlags] & NSAlphaShiftKeyMask) != 0; | 47 return ([[NSApp currentEvent] modifierFlags] & NSAlphaShiftKeyMask) != 0; |
| 30 } | 48 } |
| 31 | 49 |
| 32 } // namespace | 50 } // namespace |
| 33 | 51 |
| 34 #pragma mark - | 52 #pragma mark - |
| 35 | 53 |
| 36 #ifndef NP_NO_CARBON | 54 PluginWebEventConverter::PluginWebEventConverter() { |
| 37 | |
| 38 // Converter implementation for the Carbon event model. | |
| 39 class CarbonPluginWebEventConverter : public PluginWebEventConverter { | |
| 40 public: | |
| 41 CarbonPluginWebEventConverter() {} | |
| 42 virtual ~CarbonPluginWebEventConverter() {} | |
| 43 | |
| 44 virtual bool InitWithEvent(const WebInputEvent& web_event); | |
| 45 | |
| 46 virtual void* plugin_event() { return &carbon_event_; } | |
| 47 | |
| 48 protected: | |
| 49 virtual bool ConvertKeyboardEvent(const WebKeyboardEvent& key_event); | |
| 50 virtual bool ConvertMouseEvent(const WebMouseEvent& mouse_event); | |
| 51 virtual bool ConvertMouseWheelEvent(const WebMouseWheelEvent& wheel_event); | |
| 52 | |
| 53 private: | |
| 54 // Returns the Carbon translation of web_event's modifiers. | |
| 55 static EventModifiers CarbonModifiers(const WebInputEvent& web_event); | |
| 56 | |
| 57 NPEvent carbon_event_; | |
| 58 | |
| 59 DISALLOW_COPY_AND_ASSIGN(CarbonPluginWebEventConverter); | |
| 60 }; | |
| 61 | |
| 62 bool CarbonPluginWebEventConverter::InitWithEvent( | |
| 63 const WebInputEvent& web_event) { | |
| 64 memset(&carbon_event_, 0, sizeof(carbon_event_)); | |
| 65 // Set the fields common to all event types. | |
| 66 carbon_event_.when = TickCount(); | |
| 67 carbon_event_.modifiers |= CarbonModifiers(web_event); | |
| 68 | |
| 69 return PluginWebEventConverter::InitWithEvent(web_event); | |
| 70 } | 55 } |
| 71 | 56 |
| 72 bool CarbonPluginWebEventConverter::ConvertKeyboardEvent( | 57 PluginWebEventConverter::~PluginWebEventConverter() { |
| 73 const WebKeyboardEvent& key_event) { | |
| 74 // TODO: Figure out how to handle Unicode input to plugins, if that's | |
| 75 // even possible in the NPAPI Carbon event model. | |
| 76 carbon_event_.message = (key_event.nativeKeyCode << 8) & keyCodeMask; | |
| 77 carbon_event_.message |= key_event.text[0] & charCodeMask; | |
| 78 carbon_event_.modifiers |= btnState; | |
| 79 | |
| 80 switch (key_event.type) { | |
| 81 case WebInputEvent::KeyDown: | |
| 82 if (key_event.modifiers & WebInputEvent::IsAutoRepeat) | |
| 83 carbon_event_.what = autoKey; | |
| 84 else | |
| 85 carbon_event_.what = keyDown; | |
| 86 return true; | |
| 87 case WebInputEvent::KeyUp: | |
| 88 carbon_event_.what = keyUp; | |
| 89 return true; | |
| 90 case WebInputEvent::RawKeyDown: | |
| 91 case WebInputEvent::Char: | |
| 92 // May be used eventually for IME, but currently not needed. | |
| 93 return false; | |
| 94 default: | |
| 95 NOTREACHED(); | |
| 96 return false; | |
| 97 } | |
| 98 } | 58 } |
| 99 | 59 |
| 100 bool CarbonPluginWebEventConverter::ConvertMouseEvent( | 60 bool PluginWebEventConverter::InitWithEvent(const WebInputEvent& web_event) { |
| 101 const WebMouseEvent& mouse_event) { | 61 memset(&cocoa_event_, 0, sizeof(cocoa_event_)); |
| 102 carbon_event_.where.h = mouse_event.globalX; | 62 if (web_event.type == WebInputEvent::MouseWheel) { |
| 103 carbon_event_.where.v = mouse_event.globalY; | 63 return ConvertMouseWheelEvent( |
| 104 | 64 *static_cast<const WebMouseWheelEvent*>(&web_event)); |
| 105 // Default to "button up"; override this for mouse down events below. | 65 } else if (WebInputEvent::isMouseEventType(web_event.type)) { |
| 106 carbon_event_.modifiers |= btnState; | 66 return ConvertMouseEvent(*static_cast<const WebMouseEvent*>(&web_event)); |
| 107 | 67 } else if (WebInputEvent::isKeyboardEventType(web_event.type)) { |
| 108 switch (mouse_event.button) { | 68 return ConvertKeyboardEvent( |
| 109 case WebMouseEvent::ButtonLeft: | 69 *static_cast<const WebKeyboardEvent*>(&web_event)); |
| 110 break; | |
| 111 case WebMouseEvent::ButtonMiddle: | |
| 112 carbon_event_.modifiers |= cmdKey; | |
| 113 break; | |
| 114 case WebMouseEvent::ButtonRight: | |
| 115 carbon_event_.modifiers |= controlKey; | |
| 116 break; | |
| 117 default: | |
| 118 NOTIMPLEMENTED(); | |
| 119 } | 70 } |
| 120 switch (mouse_event.type) { | 71 DLOG(WARNING) << "Unknown event type " << web_event.type; |
| 121 case WebInputEvent::MouseMove: | 72 return false; |
| 122 carbon_event_.what = nullEvent; | |
| 123 return true; | |
| 124 case WebInputEvent::MouseLeave: | |
| 125 case WebInputEvent::MouseEnter: | |
| 126 carbon_event_.what = NPEventType_AdjustCursorEvent; | |
| 127 return true; | |
| 128 case WebInputEvent::MouseDown: | |
| 129 carbon_event_.modifiers &= ~btnState; | |
| 130 carbon_event_.what = mouseDown; | |
| 131 return true; | |
| 132 case WebInputEvent::MouseUp: | |
| 133 carbon_event_.what = mouseUp; | |
| 134 return true; | |
| 135 default: | |
| 136 NOTREACHED(); | |
| 137 return false; | |
| 138 } | |
| 139 } | 73 } |
| 140 | 74 |
| 141 bool CarbonPluginWebEventConverter::ConvertMouseWheelEvent( | 75 bool PluginWebEventConverter::ConvertKeyboardEvent( |
| 142 const WebMouseWheelEvent& wheel_event) { | |
| 143 return false; // The Carbon NPAPI event model has no "mouse wheel" concept. | |
| 144 } | |
| 145 | |
| 146 EventModifiers CarbonPluginWebEventConverter::CarbonModifiers( | |
| 147 const WebInputEvent& web_event) { | |
| 148 NSInteger modifiers = 0; | |
| 149 if (web_event.modifiers & WebInputEvent::ControlKey) | |
| 150 modifiers |= controlKey; | |
| 151 if (web_event.modifiers & WebInputEvent::ShiftKey) | |
| 152 modifiers |= shiftKey; | |
| 153 if (web_event.modifiers & WebInputEvent::AltKey) | |
| 154 modifiers |= optionKey; | |
| 155 if (web_event.modifiers & WebInputEvent::MetaKey) | |
| 156 modifiers |= cmdKey; | |
| 157 if (CapsLockIsActive(web_event)) | |
| 158 modifiers |= alphaLock; | |
| 159 return modifiers; | |
| 160 } | |
| 161 | |
| 162 #endif // !NP_NO_CARBON | |
| 163 | |
| 164 #pragma mark - | |
| 165 | |
| 166 // Converter implementation for the Cocoa event model. | |
| 167 class CocoaPluginWebEventConverter : public PluginWebEventConverter { | |
| 168 public: | |
| 169 CocoaPluginWebEventConverter() {} | |
| 170 virtual ~CocoaPluginWebEventConverter() {} | |
| 171 | |
| 172 virtual bool InitWithEvent(const WebInputEvent& web_event); | |
| 173 | |
| 174 virtual void* plugin_event() { return &cocoa_event_; } | |
| 175 | |
| 176 protected: | |
| 177 virtual bool ConvertKeyboardEvent(const WebKeyboardEvent& key_event); | |
| 178 virtual bool ConvertMouseEvent(const WebMouseEvent& mouse_event); | |
| 179 virtual bool ConvertMouseWheelEvent(const WebMouseWheelEvent& wheel_event); | |
| 180 | |
| 181 private: | |
| 182 // Returns the Cocoa translation of web_event's modifiers. | |
| 183 static NSUInteger CocoaModifiers(const WebInputEvent& web_event); | |
| 184 | |
| 185 // Returns true if the given key is a modifier key. | |
| 186 static bool KeyIsModifier(int native_key_code); | |
| 187 | |
| 188 NPCocoaEvent cocoa_event_; | |
| 189 | |
| 190 DISALLOW_COPY_AND_ASSIGN(CocoaPluginWebEventConverter); | |
| 191 }; | |
| 192 | |
| 193 bool CocoaPluginWebEventConverter::InitWithEvent( | |
| 194 const WebInputEvent& web_event) { | |
| 195 memset(&cocoa_event_, 0, sizeof(cocoa_event_)); | |
| 196 return PluginWebEventConverter::InitWithEvent(web_event); | |
| 197 } | |
| 198 | |
| 199 bool CocoaPluginWebEventConverter::ConvertKeyboardEvent( | |
| 200 const WebKeyboardEvent& key_event) { | 76 const WebKeyboardEvent& key_event) { |
| 201 cocoa_event_.data.key.keyCode = key_event.nativeKeyCode; | 77 cocoa_event_.data.key.keyCode = key_event.nativeKeyCode; |
| 202 | 78 |
| 203 cocoa_event_.data.key.modifierFlags |= CocoaModifiers(key_event); | 79 cocoa_event_.data.key.modifierFlags |= CocoaModifiers(key_event); |
| 204 | 80 |
| 205 // Modifier keys have their own event type, and don't get character or | 81 // Modifier keys have their own event type, and don't get character or |
| 206 // repeat data. | 82 // repeat data. |
| 207 if (KeyIsModifier(key_event.nativeKeyCode)) { | 83 if (KeyIsModifier(key_event.nativeKeyCode)) { |
| 208 cocoa_event_.type = NPCocoaEventFlagsChanged; | 84 cocoa_event_.type = NPCocoaEventFlagsChanged; |
| 209 return true; | 85 return true; |
| (...skipping 18 matching lines...) Expand all Loading... |
| 228 case WebInputEvent::RawKeyDown: | 104 case WebInputEvent::RawKeyDown: |
| 229 case WebInputEvent::Char: | 105 case WebInputEvent::Char: |
| 230 // May be used eventually for IME, but currently not needed. | 106 // May be used eventually for IME, but currently not needed. |
| 231 return false; | 107 return false; |
| 232 default: | 108 default: |
| 233 NOTREACHED(); | 109 NOTREACHED(); |
| 234 return false; | 110 return false; |
| 235 } | 111 } |
| 236 } | 112 } |
| 237 | 113 |
| 238 bool CocoaPluginWebEventConverter::ConvertMouseEvent( | 114 bool PluginWebEventConverter::ConvertMouseEvent( |
| 239 const WebMouseEvent& mouse_event) { | 115 const WebMouseEvent& mouse_event) { |
| 240 cocoa_event_.data.mouse.pluginX = mouse_event.x; | 116 cocoa_event_.data.mouse.pluginX = mouse_event.x; |
| 241 cocoa_event_.data.mouse.pluginY = mouse_event.y; | 117 cocoa_event_.data.mouse.pluginY = mouse_event.y; |
| 242 cocoa_event_.data.mouse.modifierFlags |= CocoaModifiers(mouse_event); | 118 cocoa_event_.data.mouse.modifierFlags |= CocoaModifiers(mouse_event); |
| 243 cocoa_event_.data.mouse.clickCount = mouse_event.clickCount; | 119 cocoa_event_.data.mouse.clickCount = mouse_event.clickCount; |
| 244 switch (mouse_event.button) { | 120 switch (mouse_event.button) { |
| 245 case WebMouseEvent::ButtonLeft: | 121 case WebMouseEvent::ButtonLeft: |
| 246 cocoa_event_.data.mouse.buttonNumber = 0; | 122 cocoa_event_.data.mouse.buttonNumber = 0; |
| 247 break; | 123 break; |
| 248 case WebMouseEvent::ButtonMiddle: | 124 case WebMouseEvent::ButtonMiddle: |
| (...skipping 27 matching lines...) Expand all Loading... |
| 276 return true; | 152 return true; |
| 277 case WebInputEvent::MouseLeave: | 153 case WebInputEvent::MouseLeave: |
| 278 cocoa_event_.type = NPCocoaEventMouseExited; | 154 cocoa_event_.type = NPCocoaEventMouseExited; |
| 279 return true; | 155 return true; |
| 280 default: | 156 default: |
| 281 NOTREACHED(); | 157 NOTREACHED(); |
| 282 return false; | 158 return false; |
| 283 } | 159 } |
| 284 } | 160 } |
| 285 | 161 |
| 286 bool CocoaPluginWebEventConverter::ConvertMouseWheelEvent( | 162 bool PluginWebEventConverter::ConvertMouseWheelEvent( |
| 287 const WebMouseWheelEvent& wheel_event) { | 163 const WebMouseWheelEvent& wheel_event) { |
| 288 cocoa_event_.type = NPCocoaEventScrollWheel; | 164 cocoa_event_.type = NPCocoaEventScrollWheel; |
| 289 cocoa_event_.data.mouse.pluginX = wheel_event.x; | 165 cocoa_event_.data.mouse.pluginX = wheel_event.x; |
| 290 cocoa_event_.data.mouse.pluginY = wheel_event.y; | 166 cocoa_event_.data.mouse.pluginY = wheel_event.y; |
| 291 cocoa_event_.data.mouse.modifierFlags |= CocoaModifiers(wheel_event); | 167 cocoa_event_.data.mouse.modifierFlags |= CocoaModifiers(wheel_event); |
| 292 cocoa_event_.data.mouse.deltaX = wheel_event.deltaX; | 168 cocoa_event_.data.mouse.deltaX = wheel_event.deltaX; |
| 293 cocoa_event_.data.mouse.deltaY = wheel_event.deltaY; | 169 cocoa_event_.data.mouse.deltaY = wheel_event.deltaY; |
| 294 return true; | 170 return true; |
| 295 } | 171 } |
| 296 | 172 |
| 297 NSUInteger CocoaPluginWebEventConverter::CocoaModifiers( | 173 NSUInteger PluginWebEventConverter::CocoaModifiers( |
| 298 const WebInputEvent& web_event) { | 174 const WebInputEvent& web_event) { |
| 299 NSInteger modifiers = 0; | 175 NSInteger modifiers = 0; |
| 300 if (web_event.modifiers & WebInputEvent::ControlKey) | 176 if (web_event.modifiers & WebInputEvent::ControlKey) |
| 301 modifiers |= NSControlKeyMask; | 177 modifiers |= NSControlKeyMask; |
| 302 if (web_event.modifiers & WebInputEvent::ShiftKey) | 178 if (web_event.modifiers & WebInputEvent::ShiftKey) |
| 303 modifiers |= NSShiftKeyMask; | 179 modifiers |= NSShiftKeyMask; |
| 304 if (web_event.modifiers & WebInputEvent::AltKey) | 180 if (web_event.modifiers & WebInputEvent::AltKey) |
| 305 modifiers |= NSAlternateKeyMask; | 181 modifiers |= NSAlternateKeyMask; |
| 306 if (web_event.modifiers & WebInputEvent::MetaKey) | 182 if (web_event.modifiers & WebInputEvent::MetaKey) |
| 307 modifiers |= NSCommandKeyMask; | 183 modifiers |= NSCommandKeyMask; |
| 308 if (CapsLockIsActive(web_event)) | 184 if (CapsLockIsActive(web_event)) |
| 309 modifiers |= NSAlphaShiftKeyMask; | 185 modifiers |= NSAlphaShiftKeyMask; |
| 310 return modifiers; | 186 return modifiers; |
| 311 } | 187 } |
| 312 | 188 |
| 313 bool CocoaPluginWebEventConverter::KeyIsModifier(int native_key_code) { | |
| 314 switch (native_key_code) { | |
| 315 case 55: // Left command | |
| 316 case 54: // Right command | |
| 317 case 58: // Left option | |
| 318 case 61: // Right option | |
| 319 case 59: // Left control | |
| 320 case 62: // Right control | |
| 321 case 56: // Left shift | |
| 322 case 60: // Right shift | |
| 323 case 57: // Caps lock | |
| 324 return true; | |
| 325 default: | |
| 326 return false; | |
| 327 } | |
| 328 } | |
| 329 | |
| 330 #pragma mark - | |
| 331 | |
| 332 bool PluginWebEventConverter::InitWithEvent(const WebInputEvent& web_event) { | |
| 333 if (web_event.type == WebInputEvent::MouseWheel) { | |
| 334 return ConvertMouseWheelEvent( | |
| 335 *static_cast<const WebMouseWheelEvent*>(&web_event)); | |
| 336 } else if (WebInputEvent::isMouseEventType(web_event.type)) { | |
| 337 return ConvertMouseEvent(*static_cast<const WebMouseEvent*>(&web_event)); | |
| 338 } else if (WebInputEvent::isKeyboardEventType(web_event.type)) { | |
| 339 return ConvertKeyboardEvent( | |
| 340 *static_cast<const WebKeyboardEvent*>(&web_event)); | |
| 341 } | |
| 342 DLOG(WARNING) << "Unknown event type " << web_event.type; | |
| 343 return false; | |
| 344 } | |
| 345 | |
| 346 #pragma mark - | |
| 347 | |
| 348 PluginWebEventConverter* | |
| 349 PluginWebEventConverterFactory::CreateConverterForModel( | |
| 350 NPEventModel event_model) { | |
| 351 switch (event_model) { | |
| 352 case NPEventModelCocoa: | |
| 353 return new CocoaPluginWebEventConverter(); | |
| 354 #ifndef NP_NO_CARBON | |
| 355 case NPEventModelCarbon: | |
| 356 return new CarbonPluginWebEventConverter(); | |
| 357 #endif | |
| 358 default: | |
| 359 NOTIMPLEMENTED(); | |
| 360 return NULL; | |
| 361 } | |
| 362 } | |
| 363 | |
| 364 } // namespace npapi | 189 } // namespace npapi |
| 365 } // namespace webkit | 190 } // namespace webkit |
| OLD | NEW |