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