[Session restore] Rename group name Enabled to Restore.
[chromium-blink-merge.git] / content / browser / renderer_host / input / web_input_event_util.cc
blob5de6ed44c9b97eb2fb3e3225f2a83e2a3b8a5365
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.
5 // MSVC++ requires this to be set before any other includes to get M_PI.
6 #define _USE_MATH_DEFINES
8 #include "content/browser/renderer_host/input/web_input_event_util.h"
10 #include <cmath>
12 #include "base/strings/string_util.h"
13 #include "content/common/input/web_touch_event_traits.h"
14 #include "ui/events/blink/blink_event_util.h"
15 #include "ui/events/event_constants.h"
16 #include "ui/events/gesture_detection/gesture_event_data.h"
17 #include "ui/events/gesture_detection/motion_event.h"
18 #include "ui/gfx/geometry/safe_integer_conversions.h"
20 using blink::WebGestureEvent;
21 using blink::WebInputEvent;
22 using blink::WebTouchEvent;
23 using blink::WebTouchPoint;
24 using ui::MotionEvent;
26 namespace {
28 const char* GetKeyIdentifier(ui::KeyboardCode key_code) {
29 switch (key_code) {
30 case ui::VKEY_MENU:
31 return "Alt";
32 case ui::VKEY_CONTROL:
33 return "Control";
34 case ui::VKEY_SHIFT:
35 return "Shift";
36 case ui::VKEY_CAPITAL:
37 return "CapsLock";
38 case ui::VKEY_LWIN:
39 case ui::VKEY_RWIN:
40 return "Win";
41 case ui::VKEY_CLEAR:
42 return "Clear";
43 case ui::VKEY_DOWN:
44 return "Down";
45 case ui::VKEY_END:
46 return "End";
47 case ui::VKEY_RETURN:
48 return "Enter";
49 case ui::VKEY_EXECUTE:
50 return "Execute";
51 case ui::VKEY_F1:
52 return "F1";
53 case ui::VKEY_F2:
54 return "F2";
55 case ui::VKEY_F3:
56 return "F3";
57 case ui::VKEY_F4:
58 return "F4";
59 case ui::VKEY_F5:
60 return "F5";
61 case ui::VKEY_F6:
62 return "F6";
63 case ui::VKEY_F7:
64 return "F7";
65 case ui::VKEY_F8:
66 return "F8";
67 case ui::VKEY_F9:
68 return "F9";
69 case ui::VKEY_F10:
70 return "F10";
71 case ui::VKEY_F11:
72 return "F11";
73 case ui::VKEY_F12:
74 return "F12";
75 case ui::VKEY_F13:
76 return "F13";
77 case ui::VKEY_F14:
78 return "F14";
79 case ui::VKEY_F15:
80 return "F15";
81 case ui::VKEY_F16:
82 return "F16";
83 case ui::VKEY_F17:
84 return "F17";
85 case ui::VKEY_F18:
86 return "F18";
87 case ui::VKEY_F19:
88 return "F19";
89 case ui::VKEY_F20:
90 return "F20";
91 case ui::VKEY_F21:
92 return "F21";
93 case ui::VKEY_F22:
94 return "F22";
95 case ui::VKEY_F23:
96 return "F23";
97 case ui::VKEY_F24:
98 return "F24";
99 case ui::VKEY_HELP:
100 return "Help";
101 case ui::VKEY_HOME:
102 return "Home";
103 case ui::VKEY_INSERT:
104 return "Insert";
105 case ui::VKEY_LEFT:
106 return "Left";
107 case ui::VKEY_NEXT:
108 return "PageDown";
109 case ui::VKEY_PRIOR:
110 return "PageUp";
111 case ui::VKEY_PAUSE:
112 return "Pause";
113 case ui::VKEY_SNAPSHOT:
114 return "PrintScreen";
115 case ui::VKEY_RIGHT:
116 return "Right";
117 case ui::VKEY_SCROLL:
118 return "Scroll";
119 case ui::VKEY_SELECT:
120 return "Select";
121 case ui::VKEY_UP:
122 return "Up";
123 case ui::VKEY_DELETE:
124 return "U+007F"; // Standard says that DEL becomes U+007F.
125 case ui::VKEY_MEDIA_NEXT_TRACK:
126 return "MediaNextTrack";
127 case ui::VKEY_MEDIA_PREV_TRACK:
128 return "MediaPreviousTrack";
129 case ui::VKEY_MEDIA_STOP:
130 return "MediaStop";
131 case ui::VKEY_MEDIA_PLAY_PAUSE:
132 return "MediaPlayPause";
133 case ui::VKEY_VOLUME_MUTE:
134 return "VolumeMute";
135 case ui::VKEY_VOLUME_DOWN:
136 return "VolumeDown";
137 case ui::VKEY_VOLUME_UP:
138 return "VolumeUp";
139 default:
140 return NULL;
144 } // namespace
146 namespace content {
148 void UpdateWindowsKeyCodeAndKeyIdentifier(blink::WebKeyboardEvent* event,
149 ui::KeyboardCode windows_key_code) {
150 event->windowsKeyCode = windows_key_code;
152 const char* id = GetKeyIdentifier(windows_key_code);
153 if (id) {
154 base::strlcpy(event->keyIdentifier, id, sizeof(event->keyIdentifier) - 1);
155 } else {
156 base::snprintf(event->keyIdentifier,
157 sizeof(event->keyIdentifier),
158 "U+%04X",
159 base::ToUpperASCII(static_cast<int>(windows_key_code)));
163 int WebEventModifiersToEventFlags(int modifiers) {
164 int flags = 0;
166 if (modifiers & blink::WebInputEvent::ShiftKey)
167 flags |= ui::EF_SHIFT_DOWN;
168 if (modifiers & blink::WebInputEvent::ControlKey)
169 flags |= ui::EF_CONTROL_DOWN;
170 if (modifiers & blink::WebInputEvent::AltKey)
171 flags |= ui::EF_ALT_DOWN;
172 if (modifiers & blink::WebInputEvent::MetaKey)
173 flags |= ui::EF_COMMAND_DOWN;
175 if (modifiers & blink::WebInputEvent::LeftButtonDown)
176 flags |= ui::EF_LEFT_MOUSE_BUTTON;
177 if (modifiers & blink::WebInputEvent::MiddleButtonDown)
178 flags |= ui::EF_MIDDLE_MOUSE_BUTTON;
179 if (modifiers & blink::WebInputEvent::RightButtonDown)
180 flags |= ui::EF_RIGHT_MOUSE_BUTTON;
181 if (modifiers & blink::WebInputEvent::CapsLockOn)
182 flags |= ui::EF_CAPS_LOCK_DOWN;
183 if (modifiers & blink::WebInputEvent::IsAutoRepeat)
184 flags |= ui::EF_IS_REPEAT;
185 if (modifiers & blink::WebInputEvent::IsKeyPad)
186 flags |= ui::EF_NUMPAD_KEY;
188 return flags;
191 } // namespace content