web_input_event_builders_android.cc revision d0247b1b59f9c528cb6df88b4f2b9afaf80d181e
1// Copyright 2013 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#include "content/browser/renderer_host/input/web_input_event_builders_android.h" 6 7#include "base/logging.h" 8#include "content/browser/renderer_host/input/web_input_event_util.h" 9#include "content/browser/renderer_host/input/web_input_event_util_posix.h" 10#include "ui/events/keycodes/keyboard_code_conversion_android.h" 11#include "ui/events/keycodes/keyboard_codes_posix.h" 12 13namespace content { 14 15using WebKit::WebInputEvent; 16using WebKit::WebKeyboardEvent; 17using WebKit::WebGestureEvent; 18using WebKit::WebMouseEvent; 19using WebKit::WebMouseWheelEvent; 20 21WebKeyboardEvent WebKeyboardEventBuilder::Build(WebInputEvent::Type type, 22 int modifiers, 23 double time_sec, 24 int keycode, 25 int unicode_character, 26 bool is_system_key) { 27 DCHECK(WebInputEvent::isKeyboardEventType(type)); 28 WebKeyboardEvent result; 29 30 result.type = type; 31 result.modifiers = modifiers; 32 result.timeStampSeconds = time_sec; 33 ui::KeyboardCode windows_key_code = 34 ui::KeyboardCodeFromAndroidKeyCode(keycode); 35 UpdateWindowsKeyCodeAndKeyIdentifier(&result, windows_key_code); 36 result.modifiers |= GetLocationModifiersFromWindowsKeyCode(windows_key_code); 37 result.nativeKeyCode = keycode; 38 result.unmodifiedText[0] = unicode_character; 39 if (result.windowsKeyCode == ui::VKEY_RETURN) { 40 // This is the same behavior as GTK: 41 // We need to treat the enter key as a key press of character \r. This 42 // is apparently just how webkit handles it and what it expects. 43 result.unmodifiedText[0] = '\r'; 44 } 45 result.text[0] = result.unmodifiedText[0]; 46 result.isSystemKey = is_system_key; 47 48 return result; 49} 50 51WebMouseEvent WebMouseEventBuilder::Build(WebKit::WebInputEvent::Type type, 52 WebMouseEvent::Button button, 53 double time_sec, 54 int window_x, 55 int window_y, 56 int modifiers, 57 int click_count) { 58 DCHECK(WebInputEvent::isMouseEventType(type)); 59 WebMouseEvent result; 60 61 result.type = type; 62 result.x = window_x; 63 result.y = window_y; 64 result.windowX = window_x; 65 result.windowY = window_y; 66 result.timeStampSeconds = time_sec; 67 result.clickCount = click_count; 68 result.modifiers = modifiers; 69 70 if (type == WebInputEvent::MouseDown || type == WebInputEvent::MouseUp) 71 result.button = button; 72 else 73 result.button = WebMouseEvent::ButtonNone; 74 75 return result; 76} 77 78WebMouseWheelEvent WebMouseWheelEventBuilder::Build(Direction direction, 79 double time_sec, 80 int window_x, 81 int window_y) { 82 WebMouseWheelEvent result; 83 84 result.type = WebInputEvent::MouseWheel; 85 result.x = window_x; 86 result.y = window_y; 87 result.windowX = window_x; 88 result.windowY = window_y; 89 result.timeStampSeconds = time_sec; 90 result.button = WebMouseEvent::ButtonNone; 91 92 // The below choices are matched from GTK. 93 const float scrollbar_pixels_per_tick = 160.0f / 3.0f; 94 95 switch (direction) { 96 case DIRECTION_UP: 97 result.deltaY = scrollbar_pixels_per_tick; 98 result.wheelTicksY = 1; 99 break; 100 case DIRECTION_DOWN: 101 result.deltaY = -scrollbar_pixels_per_tick; 102 result.wheelTicksY = -1; 103 break; 104 case DIRECTION_LEFT: 105 result.deltaX = scrollbar_pixels_per_tick; 106 result.wheelTicksX = 1; 107 break; 108 case DIRECTION_RIGHT: 109 result.deltaX = -scrollbar_pixels_per_tick; 110 result.wheelTicksX = -1; 111 break; 112 } 113 114 return result; 115} 116 117WebGestureEvent WebGestureEventBuilder::Build(WebInputEvent::Type type, 118 double time_sec, 119 int x, 120 int y) { 121 DCHECK(WebInputEvent::isGestureEventType(type)); 122 WebGestureEvent result; 123 124 result.type = type; 125 result.x = x; 126 result.y = y; 127 result.timeStampSeconds = time_sec; 128 result.sourceDevice = WebGestureEvent::Touchscreen; 129 130 return result; 131} 132 133} // namespace content 134