Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(517)

Side by Side Diff: webkit/glue/plugins/plugin_web_event_converter_mac.mm

Issue 6012002: Move the NPAPI files from webkit/glue/plugins to webkit/plugins/npapi and put... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: Created 10 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2010 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.
4
5 #import <Cocoa/Cocoa.h>
6
7 #include "base/logging.h"
8 #include "webkit/glue/plugins/plugin_web_event_converter_mac.h"
9 #include "third_party/WebKit/WebKit/chromium/public/WebInputEvent.h"
10
11 using WebKit::WebInputEvent;
12 using WebKit::WebKeyboardEvent;
13 using WebKit::WebMouseEvent;
14 using WebKit::WebMouseWheelEvent;
15
16 namespace {
17
18 // Returns true if the caps lock flag should be set for the given event.
19 bool CapsLockIsActive(const WebInputEvent& event) {
20 // Only key events have accurate information for the caps lock flag; see
21 // <https://bugs.webkit.org/show_bug.cgi?id=46518>.
22 // For other types, use the live state.
23 if (WebInputEvent::isKeyboardEventType(event.type))
24 return (event.modifiers & WebInputEvent::CapsLockOn) != 0;
25 else
26 return ([[NSApp currentEvent] modifierFlags] & NSAlphaShiftKeyMask) != 0;
27 }
28
29 } // namespace
30
31 #pragma mark -
32
33 #ifndef NP_NO_CARBON
34
35 // Converter implementation for the Carbon event model.
36 class CarbonPluginWebEventConverter : public PluginWebEventConverter {
37 public:
38 CarbonPluginWebEventConverter() {}
39 virtual ~CarbonPluginWebEventConverter() {}
40
41 virtual bool InitWithEvent(const WebInputEvent& web_event);
42
43 virtual void* plugin_event() { return &carbon_event_; }
44
45 protected:
46 virtual bool ConvertKeyboardEvent(const WebKeyboardEvent& key_event);
47 virtual bool ConvertMouseEvent(const WebMouseEvent& mouse_event);
48 virtual bool ConvertMouseWheelEvent(const WebMouseWheelEvent& wheel_event);
49
50 private:
51 // Returns the Carbon translation of web_event's modifiers.
52 static EventModifiers CarbonModifiers(const WebInputEvent& web_event);
53
54 NPEvent carbon_event_;
55
56 DISALLOW_COPY_AND_ASSIGN(CarbonPluginWebEventConverter);
57 };
58
59 bool CarbonPluginWebEventConverter::InitWithEvent(
60 const WebInputEvent& web_event) {
61 memset(&carbon_event_, 0, sizeof(carbon_event_));
62 // Set the fields common to all event types.
63 carbon_event_.when = TickCount();
64 carbon_event_.modifiers |= CarbonModifiers(web_event);
65
66 return PluginWebEventConverter::InitWithEvent(web_event);
67 }
68
69 bool CarbonPluginWebEventConverter::ConvertKeyboardEvent(
70 const WebKeyboardEvent& key_event) {
71 // TODO: Figure out how to handle Unicode input to plugins, if that's
72 // even possible in the NPAPI Carbon event model.
73 carbon_event_.message = (key_event.nativeKeyCode << 8) & keyCodeMask;
74 carbon_event_.message |= key_event.text[0] & charCodeMask;
75 carbon_event_.modifiers |= btnState;
76
77 switch (key_event.type) {
78 case WebInputEvent::KeyDown:
79 if (key_event.modifiers & WebInputEvent::IsAutoRepeat)
80 carbon_event_.what = autoKey;
81 else
82 carbon_event_.what = keyDown;
83 return true;
84 case WebInputEvent::KeyUp:
85 carbon_event_.what = keyUp;
86 return true;
87 case WebInputEvent::RawKeyDown:
88 case WebInputEvent::Char:
89 // May be used eventually for IME, but currently not needed.
90 return false;
91 default:
92 NOTREACHED();
93 return false;
94 }
95 }
96
97 bool CarbonPluginWebEventConverter::ConvertMouseEvent(
98 const WebMouseEvent& mouse_event) {
99 carbon_event_.where.h = mouse_event.globalX;
100 carbon_event_.where.v = mouse_event.globalY;
101
102 // Default to "button up"; override this for mouse down events below.
103 carbon_event_.modifiers |= btnState;
104
105 switch (mouse_event.button) {
106 case WebMouseEvent::ButtonLeft:
107 break;
108 case WebMouseEvent::ButtonMiddle:
109 carbon_event_.modifiers |= cmdKey;
110 break;
111 case WebMouseEvent::ButtonRight:
112 carbon_event_.modifiers |= controlKey;
113 break;
114 default:
115 NOTIMPLEMENTED();
116 }
117 switch (mouse_event.type) {
118 case WebInputEvent::MouseMove:
119 carbon_event_.what = nullEvent;
120 return true;
121 case WebInputEvent::MouseLeave:
122 case WebInputEvent::MouseEnter:
123 carbon_event_.what = NPEventType_AdjustCursorEvent;
124 return true;
125 case WebInputEvent::MouseDown:
126 carbon_event_.modifiers &= ~btnState;
127 carbon_event_.what = mouseDown;
128 return true;
129 case WebInputEvent::MouseUp:
130 carbon_event_.what = mouseUp;
131 return true;
132 default:
133 NOTREACHED();
134 return false;
135 }
136 }
137
138 bool CarbonPluginWebEventConverter::ConvertMouseWheelEvent(
139 const WebMouseWheelEvent& wheel_event) {
140 return false; // The Carbon NPAPI event model has no "mouse wheel" concept.
141 }
142
143 EventModifiers CarbonPluginWebEventConverter::CarbonModifiers(
144 const WebInputEvent& web_event) {
145 NSInteger modifiers = 0;
146 if (web_event.modifiers & WebInputEvent::ControlKey)
147 modifiers |= controlKey;
148 if (web_event.modifiers & WebInputEvent::ShiftKey)
149 modifiers |= shiftKey;
150 if (web_event.modifiers & WebInputEvent::AltKey)
151 modifiers |= optionKey;
152 if (web_event.modifiers & WebInputEvent::MetaKey)
153 modifiers |= cmdKey;
154 if (CapsLockIsActive(web_event))
155 modifiers |= alphaLock;
156 return modifiers;
157 }
158
159 #endif // !NP_NO_CARBON
160
161 #pragma mark -
162
163 // Converter implementation for the Cocoa event model.
164 class CocoaPluginWebEventConverter : public PluginWebEventConverter {
165 public:
166 CocoaPluginWebEventConverter() {}
167 virtual ~CocoaPluginWebEventConverter() {}
168
169 virtual bool InitWithEvent(const WebInputEvent& web_event);
170
171 virtual void* plugin_event() { return &cocoa_event_; }
172
173 protected:
174 virtual bool ConvertKeyboardEvent(const WebKeyboardEvent& key_event);
175 virtual bool ConvertMouseEvent(const WebMouseEvent& mouse_event);
176 virtual bool ConvertMouseWheelEvent(const WebMouseWheelEvent& wheel_event);
177
178 private:
179 // Returns the Cocoa translation of web_event's modifiers.
180 static NSUInteger CocoaModifiers(const WebInputEvent& web_event);
181
182 // Returns true if the given key is a modifier key.
183 static bool KeyIsModifier(int native_key_code);
184
185 NPCocoaEvent cocoa_event_;
186
187 DISALLOW_COPY_AND_ASSIGN(CocoaPluginWebEventConverter);
188 };
189
190 bool CocoaPluginWebEventConverter::InitWithEvent(
191 const WebInputEvent& web_event) {
192 memset(&cocoa_event_, 0, sizeof(cocoa_event_));
193 return PluginWebEventConverter::InitWithEvent(web_event);
194 }
195
196 bool CocoaPluginWebEventConverter::ConvertKeyboardEvent(
197 const WebKeyboardEvent& key_event) {
198 cocoa_event_.data.key.keyCode = key_event.nativeKeyCode;
199
200 cocoa_event_.data.key.modifierFlags |= CocoaModifiers(key_event);
201
202 // Modifier keys have their own event type, and don't get character or
203 // repeat data.
204 if (KeyIsModifier(key_event.nativeKeyCode)) {
205 cocoa_event_.type = NPCocoaEventFlagsChanged;
206 return true;
207 }
208
209 cocoa_event_.data.key.characters = reinterpret_cast<NPNSString*>(
210 [NSString stringWithFormat:@"%S", key_event.text]);
211 cocoa_event_.data.key.charactersIgnoringModifiers =
212 reinterpret_cast<NPNSString*>(
213 [NSString stringWithFormat:@"%S", key_event.unmodifiedText]);
214
215 if (key_event.modifiers & WebInputEvent::IsAutoRepeat)
216 cocoa_event_.data.key.isARepeat = true;
217
218 switch (key_event.type) {
219 case WebInputEvent::KeyDown:
220 cocoa_event_.type = NPCocoaEventKeyDown;
221 return true;
222 case WebInputEvent::KeyUp:
223 cocoa_event_.type = NPCocoaEventKeyUp;
224 return true;
225 case WebInputEvent::RawKeyDown:
226 case WebInputEvent::Char:
227 // May be used eventually for IME, but currently not needed.
228 return false;
229 default:
230 NOTREACHED();
231 return false;
232 }
233 }
234
235 bool CocoaPluginWebEventConverter::ConvertMouseEvent(
236 const WebMouseEvent& mouse_event) {
237 cocoa_event_.data.mouse.pluginX = mouse_event.x;
238 cocoa_event_.data.mouse.pluginY = mouse_event.y;
239 cocoa_event_.data.mouse.modifierFlags |= CocoaModifiers(mouse_event);
240 cocoa_event_.data.mouse.clickCount = mouse_event.clickCount;
241 switch (mouse_event.button) {
242 case WebMouseEvent::ButtonLeft:
243 cocoa_event_.data.mouse.buttonNumber = 0;
244 break;
245 case WebMouseEvent::ButtonMiddle:
246 cocoa_event_.data.mouse.buttonNumber = 2;
247 break;
248 case WebMouseEvent::ButtonRight:
249 cocoa_event_.data.mouse.buttonNumber = 1;
250 break;
251 default:
252 cocoa_event_.data.mouse.buttonNumber = mouse_event.button;
253 break;
254 }
255 switch (mouse_event.type) {
256 case WebInputEvent::MouseDown:
257 cocoa_event_.type = NPCocoaEventMouseDown;
258 return true;
259 case WebInputEvent::MouseUp:
260 cocoa_event_.type = NPCocoaEventMouseUp;
261 return true;
262 case WebInputEvent::MouseMove: {
263 bool mouse_is_down =
264 (mouse_event.modifiers & WebInputEvent::LeftButtonDown) ||
265 (mouse_event.modifiers & WebInputEvent::RightButtonDown) ||
266 (mouse_event.modifiers & WebInputEvent::MiddleButtonDown);
267 cocoa_event_.type = mouse_is_down ? NPCocoaEventMouseDragged
268 : NPCocoaEventMouseMoved;
269 return true;
270 }
271 case WebInputEvent::MouseEnter:
272 cocoa_event_.type = NPCocoaEventMouseEntered;
273 return true;
274 case WebInputEvent::MouseLeave:
275 cocoa_event_.type = NPCocoaEventMouseExited;
276 return true;
277 default:
278 NOTREACHED();
279 return false;
280 }
281 }
282
283 bool CocoaPluginWebEventConverter::ConvertMouseWheelEvent(
284 const WebMouseWheelEvent& wheel_event) {
285 cocoa_event_.type = NPCocoaEventScrollWheel;
286 cocoa_event_.data.mouse.pluginX = wheel_event.x;
287 cocoa_event_.data.mouse.pluginY = wheel_event.y;
288 cocoa_event_.data.mouse.modifierFlags |= CocoaModifiers(wheel_event);
289 cocoa_event_.data.mouse.deltaX = wheel_event.deltaX;
290 cocoa_event_.data.mouse.deltaY = wheel_event.deltaY;
291 return true;
292 }
293
294 NSUInteger CocoaPluginWebEventConverter::CocoaModifiers(
295 const WebInputEvent& web_event) {
296 NSInteger modifiers = 0;
297 if (web_event.modifiers & WebInputEvent::ControlKey)
298 modifiers |= NSControlKeyMask;
299 if (web_event.modifiers & WebInputEvent::ShiftKey)
300 modifiers |= NSShiftKeyMask;
301 if (web_event.modifiers & WebInputEvent::AltKey)
302 modifiers |= NSAlternateKeyMask;
303 if (web_event.modifiers & WebInputEvent::MetaKey)
304 modifiers |= NSCommandKeyMask;
305 if (CapsLockIsActive(web_event))
306 modifiers |= NSAlphaShiftKeyMask;
307 return modifiers;
308 }
309
310 bool CocoaPluginWebEventConverter::KeyIsModifier(int native_key_code) {
311 switch (native_key_code) {
312 case 55: // Left command
313 case 54: // Right command
314 case 58: // Left option
315 case 61: // Right option
316 case 59: // Left control
317 case 62: // Right control
318 case 56: // Left shift
319 case 60: // Right shift
320 case 57: // Caps lock
321 return true;
322 default:
323 return false;
324 }
325 }
326
327 #pragma mark -
328
329 bool PluginWebEventConverter::InitWithEvent(const WebInputEvent& web_event) {
330 if (web_event.type == WebInputEvent::MouseWheel) {
331 return ConvertMouseWheelEvent(
332 *static_cast<const WebMouseWheelEvent*>(&web_event));
333 } else if (WebInputEvent::isMouseEventType(web_event.type)) {
334 return ConvertMouseEvent(*static_cast<const WebMouseEvent*>(&web_event));
335 } else if (WebInputEvent::isKeyboardEventType(web_event.type)) {
336 return ConvertKeyboardEvent(
337 *static_cast<const WebKeyboardEvent*>(&web_event));
338 }
339 DLOG(WARNING) << "Unknown event type " << web_event.type;
340 return false;
341 }
342
343 #pragma mark -
344
345 PluginWebEventConverter*
346 PluginWebEventConverterFactory::CreateConverterForModel(
347 NPEventModel event_model) {
348 switch (event_model) {
349 case NPEventModelCocoa:
350 return new CocoaPluginWebEventConverter();
351 #ifndef NP_NO_CARBON
352 case NPEventModelCarbon:
353 return new CarbonPluginWebEventConverter();
354 #endif
355 default:
356 NOTIMPLEMENTED();
357 return NULL;
358 }
359 }
OLDNEW
« no previous file with comments | « webkit/glue/plugins/plugin_web_event_converter_mac.h ('k') | webkit/glue/plugins/quickdraw_drawing_manager_mac.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698