OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 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 #include "content/browser/renderer_host/input/web_input_event_util.h" | 5 #include "content/browser/renderer_host/input/web_input_event_util.h" |
6 | 6 |
7 #include "base/strings/string_util.h" | 7 #include "base/strings/string_util.h" |
8 #include "third_party/WebKit/public/web/WebInputEvent.h" | 8 #include "ui/events/gesture_detection/motion_event.h" |
| 9 |
| 10 using blink::WebInputEvent; |
| 11 using blink::WebTouchEvent; |
| 12 using blink::WebTouchPoint; |
| 13 using ui::MotionEvent; |
9 | 14 |
10 namespace { | 15 namespace { |
11 | 16 |
12 const char* GetKeyIdentifier(ui::KeyboardCode key_code) { | 17 const char* GetKeyIdentifier(ui::KeyboardCode key_code) { |
13 switch (key_code) { | 18 switch (key_code) { |
14 case ui::VKEY_MENU: | 19 case ui::VKEY_MENU: |
15 return "Alt"; | 20 return "Alt"; |
16 case ui::VKEY_CONTROL: | 21 case ui::VKEY_CONTROL: |
17 return "Control"; | 22 return "Control"; |
18 case ui::VKEY_SHIFT: | 23 case ui::VKEY_SHIFT: |
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
118 return "VolumeMute"; | 123 return "VolumeMute"; |
119 case ui::VKEY_VOLUME_DOWN: | 124 case ui::VKEY_VOLUME_DOWN: |
120 return "VolumeDown"; | 125 return "VolumeDown"; |
121 case ui::VKEY_VOLUME_UP: | 126 case ui::VKEY_VOLUME_UP: |
122 return "VolumeUp"; | 127 return "VolumeUp"; |
123 default: | 128 default: |
124 return NULL; | 129 return NULL; |
125 }; | 130 }; |
126 } | 131 } |
127 | 132 |
| 133 WebInputEvent::Type ToWebInputEventType(MotionEvent::Action action) { |
| 134 switch (action) { |
| 135 case MotionEvent::ACTION_DOWN: |
| 136 return WebInputEvent::TouchStart; |
| 137 case MotionEvent::ACTION_MOVE: |
| 138 return WebInputEvent::TouchMove; |
| 139 case MotionEvent::ACTION_UP: |
| 140 return WebInputEvent::TouchEnd; |
| 141 case MotionEvent::ACTION_CANCEL: |
| 142 return WebInputEvent::TouchCancel; |
| 143 case MotionEvent::ACTION_POINTER_DOWN: |
| 144 return WebInputEvent::TouchStart; |
| 145 case MotionEvent::ACTION_POINTER_UP: |
| 146 return WebInputEvent::TouchEnd; |
| 147 } |
| 148 NOTREACHED() << "Invalid MotionEvent::Action."; |
| 149 return WebInputEvent::Undefined; |
| 150 } |
| 151 |
| 152 // Note that |is_action_pointer| is meaningful only in the context of |
| 153 // |ACTION_POINTER_UP| and |ACTION_POINTER_DOWN|; other actions map directly to |
| 154 // WebTouchPoint::State. |
| 155 WebTouchPoint::State ToWebTouchPointState(MotionEvent::Action action, |
| 156 bool is_action_pointer) { |
| 157 switch (action) { |
| 158 case MotionEvent::ACTION_DOWN: |
| 159 return WebTouchPoint::StatePressed; |
| 160 case MotionEvent::ACTION_MOVE: |
| 161 return WebTouchPoint::StateMoved; |
| 162 case MotionEvent::ACTION_UP: |
| 163 return WebTouchPoint::StateReleased; |
| 164 case MotionEvent::ACTION_CANCEL: |
| 165 return WebTouchPoint::StateCancelled; |
| 166 case MotionEvent::ACTION_POINTER_DOWN: |
| 167 return is_action_pointer ? WebTouchPoint::StatePressed |
| 168 : WebTouchPoint::StateStationary; |
| 169 case MotionEvent::ACTION_POINTER_UP: |
| 170 return is_action_pointer ? WebTouchPoint::StateReleased |
| 171 : WebTouchPoint::StateStationary; |
| 172 } |
| 173 NOTREACHED() << "Invalid MotionEvent::Action."; |
| 174 return WebTouchPoint::StateUndefined; |
| 175 } |
| 176 |
| 177 WebTouchPoint CreateWebTouchPoint(const MotionEvent& event, |
| 178 size_t pointer_index, |
| 179 float scale) { |
| 180 WebTouchPoint touch; |
| 181 touch.id = event.GetPointerId(pointer_index); |
| 182 touch.state = ToWebTouchPointState( |
| 183 event.GetAction(), |
| 184 static_cast<int>(pointer_index) == event.GetActionIndex()); |
| 185 touch.position.x = event.GetX(pointer_index) * scale; |
| 186 touch.position.y = event.GetY(pointer_index) * scale; |
| 187 // TODO(joth): Raw event co-ordinates. |
| 188 touch.screenPosition = touch.position; |
| 189 touch.radiusX = touch.radiusY = |
| 190 event.GetTouchMajor(pointer_index) * 0.5f * scale; |
| 191 touch.force = event.GetPressure(pointer_index); |
| 192 |
| 193 return touch; |
| 194 } |
| 195 |
128 } // namespace | 196 } // namespace |
129 | 197 |
130 namespace content { | 198 namespace content { |
131 | 199 |
132 void UpdateWindowsKeyCodeAndKeyIdentifier(blink::WebKeyboardEvent* event, | 200 void UpdateWindowsKeyCodeAndKeyIdentifier(blink::WebKeyboardEvent* event, |
133 ui::KeyboardCode windows_key_code) { | 201 ui::KeyboardCode windows_key_code) { |
134 event->windowsKeyCode = windows_key_code; | 202 event->windowsKeyCode = windows_key_code; |
135 | 203 |
136 const char* id = GetKeyIdentifier(windows_key_code); | 204 const char* id = GetKeyIdentifier(windows_key_code); |
137 if (id) { | 205 if (id) { |
138 base::strlcpy(event->keyIdentifier, id, sizeof(event->keyIdentifier) - 1); | 206 base::strlcpy(event->keyIdentifier, id, sizeof(event->keyIdentifier) - 1); |
139 } else { | 207 } else { |
140 base::snprintf(event->keyIdentifier, sizeof(event->keyIdentifier), "U+%04X", | 208 base::snprintf(event->keyIdentifier, sizeof(event->keyIdentifier), "U+%04X", |
141 base::ToUpperASCII(static_cast<int>(windows_key_code))); | 209 base::ToUpperASCII(static_cast<int>(windows_key_code))); |
142 } | 210 } |
143 } | 211 } |
144 | 212 |
| 213 blink::WebTouchEvent CreateWebTouchEventFromMotionEvent( |
| 214 const ui::MotionEvent& event, |
| 215 float scale) { |
| 216 blink::WebTouchEvent result; |
| 217 |
| 218 result.type = ToWebInputEventType(event.GetAction()); |
| 219 DCHECK(WebInputEvent::isTouchEventType(result.type)); |
| 220 |
| 221 result.timeStampSeconds = |
| 222 (event.GetEventTime() - base::TimeTicks()).InSecondsF(); |
| 223 |
| 224 result.touchesLength = |
| 225 std::min(event.GetPointerCount(), |
| 226 static_cast<size_t>(WebTouchEvent::touchesLengthCap)); |
| 227 DCHECK_GT(result.touchesLength, 0U); |
| 228 |
| 229 for (size_t i = 0; i < result.touchesLength; ++i) |
| 230 result.touches[i] = CreateWebTouchPoint(event, i, scale); |
| 231 |
| 232 return result; |
| 233 } |
| 234 |
145 } // namespace content | 235 } // namespace content |
OLD | NEW |