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
3 // found in the LICENSE file.
5 #import <Cocoa/Cocoa.h>
7 #include "base/logging.h"
8 #include "content/child/npapi/plugin_web_event_converter_mac.h"
9 #include "third_party/WebKit/public/web/WebInputEvent.h"
11 using blink::WebInputEvent;
12 using blink::WebKeyboardEvent;
13 using blink::WebMouseEvent;
14 using blink::WebMouseWheelEvent;
20 // Returns true if the given key is a modifier key.
21 bool KeyIsModifier(int native_key_code) {
22 switch (native_key_code) {
23 case 55: // Left command
24 case 54: // Right command
25 case 58: // Left option
26 case 61: // Right option
27 case 59: // Left control
28 case 62: // Right control
29 case 56: // Left shift
30 case 60: // Right shift
38 // Returns true if the caps lock flag should be set for the given event.
39 bool CapsLockIsActive(const WebInputEvent& event) {
40 // Only key events have accurate information for the caps lock flag; see
41 // <https://bugs.webkit.org/show_bug.cgi?id=46518>.
42 // For other types, use the live state.
43 if (WebInputEvent::isKeyboardEventType(event.type))
44 return (event.modifiers & WebInputEvent::CapsLockOn) != 0;
46 return ([[NSApp currentEvent] modifierFlags] & NSAlphaShiftKeyMask) != 0;
53 PluginWebEventConverter::PluginWebEventConverter() {
56 PluginWebEventConverter::~PluginWebEventConverter() {
59 bool PluginWebEventConverter::InitWithEvent(const WebInputEvent& web_event) {
60 memset(&cocoa_event_, 0, sizeof(cocoa_event_));
61 if (web_event.type == WebInputEvent::MouseWheel) {
62 return ConvertMouseWheelEvent(
63 *static_cast<const WebMouseWheelEvent*>(&web_event));
64 } else if (WebInputEvent::isMouseEventType(web_event.type)) {
65 return ConvertMouseEvent(*static_cast<const WebMouseEvent*>(&web_event));
66 } else if (WebInputEvent::isKeyboardEventType(web_event.type)) {
67 return ConvertKeyboardEvent(
68 *static_cast<const WebKeyboardEvent*>(&web_event));
70 DLOG(WARNING) << "Unknown event type " << web_event.type;
74 bool PluginWebEventConverter::ConvertKeyboardEvent(
75 const WebKeyboardEvent& key_event) {
76 cocoa_event_.data.key.keyCode = key_event.nativeKeyCode;
78 cocoa_event_.data.key.modifierFlags |= CocoaModifiers(key_event);
80 // Modifier keys have their own event type, and don't get character or
82 if (KeyIsModifier(key_event.nativeKeyCode)) {
83 cocoa_event_.type = NPCocoaEventFlagsChanged;
87 cocoa_event_.data.key.characters = reinterpret_cast<NPNSString*>(
88 [NSString stringWithFormat:@"%S", key_event.text]);
89 cocoa_event_.data.key.charactersIgnoringModifiers =
90 reinterpret_cast<NPNSString*>(
91 [NSString stringWithFormat:@"%S", key_event.unmodifiedText]);
93 if (key_event.modifiers & WebInputEvent::IsAutoRepeat)
94 cocoa_event_.data.key.isARepeat = true;
96 switch (key_event.type) {
97 case WebInputEvent::KeyDown:
98 cocoa_event_.type = NPCocoaEventKeyDown;
100 case WebInputEvent::KeyUp:
101 cocoa_event_.type = NPCocoaEventKeyUp;
103 case WebInputEvent::RawKeyDown:
104 case WebInputEvent::Char:
105 // May be used eventually for IME, but currently not needed.
113 bool PluginWebEventConverter::ConvertMouseEvent(
114 const WebMouseEvent& mouse_event) {
115 cocoa_event_.data.mouse.pluginX = mouse_event.x;
116 cocoa_event_.data.mouse.pluginY = mouse_event.y;
117 cocoa_event_.data.mouse.modifierFlags |= CocoaModifiers(mouse_event);
118 cocoa_event_.data.mouse.clickCount = mouse_event.clickCount;
119 switch (mouse_event.button) {
120 case WebMouseEvent::ButtonLeft:
121 cocoa_event_.data.mouse.buttonNumber = 0;
123 case WebMouseEvent::ButtonMiddle:
124 cocoa_event_.data.mouse.buttonNumber = 2;
126 case WebMouseEvent::ButtonRight:
127 cocoa_event_.data.mouse.buttonNumber = 1;
130 cocoa_event_.data.mouse.buttonNumber = mouse_event.button;
133 switch (mouse_event.type) {
134 case WebInputEvent::MouseDown:
135 cocoa_event_.type = NPCocoaEventMouseDown;
137 case WebInputEvent::MouseUp:
138 cocoa_event_.type = NPCocoaEventMouseUp;
140 case WebInputEvent::MouseMove: {
142 (mouse_event.modifiers & WebInputEvent::LeftButtonDown) ||
143 (mouse_event.modifiers & WebInputEvent::RightButtonDown) ||
144 (mouse_event.modifiers & WebInputEvent::MiddleButtonDown);
145 cocoa_event_.type = mouse_is_down ? NPCocoaEventMouseDragged
146 : NPCocoaEventMouseMoved;
149 case WebInputEvent::MouseEnter:
150 cocoa_event_.type = NPCocoaEventMouseEntered;
152 case WebInputEvent::MouseLeave:
153 cocoa_event_.type = NPCocoaEventMouseExited;
161 bool PluginWebEventConverter::ConvertMouseWheelEvent(
162 const WebMouseWheelEvent& wheel_event) {
163 cocoa_event_.type = NPCocoaEventScrollWheel;
164 cocoa_event_.data.mouse.pluginX = wheel_event.x;
165 cocoa_event_.data.mouse.pluginY = wheel_event.y;
166 cocoa_event_.data.mouse.modifierFlags |= CocoaModifiers(wheel_event);
167 cocoa_event_.data.mouse.deltaX = wheel_event.deltaX;
168 cocoa_event_.data.mouse.deltaY = wheel_event.deltaY;
172 NSUInteger PluginWebEventConverter::CocoaModifiers(
173 const WebInputEvent& web_event) {
174 NSInteger modifiers = 0;
175 if (web_event.modifiers & WebInputEvent::ControlKey)
176 modifiers |= NSControlKeyMask;
177 if (web_event.modifiers & WebInputEvent::ShiftKey)
178 modifiers |= NSShiftKeyMask;
179 if (web_event.modifiers & WebInputEvent::AltKey)
180 modifiers |= NSAlternateKeyMask;
181 if (web_event.modifiers & WebInputEvent::MetaKey)
182 modifiers |= NSCommandKeyMask;
183 if (CapsLockIsActive(web_event))
184 modifiers |= NSAlphaShiftKeyMask;
188 } // namespace content