Supervised user whitelists: Cleanup
[chromium-blink-merge.git] / ui / events / keycodes / dom4 / keycode_converter.cc
blobd9a26a2c44461edf2c8c2deb9151f0afb755c23a
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 #include "ui/events/keycodes/dom4/keycode_converter.h"
7 #include "base/logging.h"
8 #include "ui/events/keycodes/dom3/dom_code.h"
9 #include "ui/events/keycodes/dom3/dom_key.h"
11 namespace ui {
13 namespace {
15 // Table of USB codes (equivalent to DomCode values), native scan codes,
16 // and DOM Level 3 |code| strings.
17 #if defined(OS_WIN)
18 #define USB_KEYMAP(usb, xkb, win, mac, code, id) {usb, win, code}
19 #elif defined(OS_LINUX)
20 #define USB_KEYMAP(usb, xkb, win, mac, code, id) {usb, xkb, code}
21 #elif defined(OS_MACOSX)
22 #define USB_KEYMAP(usb, xkb, win, mac, code, id) {usb, mac, code}
23 #else
24 #define USB_KEYMAP(usb, xkb, win, mac, code, id) {usb, 0, code}
25 #endif
26 #define USB_KEYMAP_DECLARATION const KeycodeMapEntry usb_keycode_map[] =
27 #include "ui/events/keycodes/dom4/keycode_converter_data.h"
28 #undef USB_KEYMAP
29 #undef USB_KEYMAP_DECLARATION
31 const size_t kKeycodeMapEntries = arraysize(usb_keycode_map);
33 // Table of DomKey enum values and DOM Level 3 |key| strings.
34 struct DomKeyMapEntry {
35 DomKey dom_key;
36 const char* string;
39 #define DOM_KEY_MAP(key, id) {DomKey::id, key}
40 #define DOM_KEY_MAP_DECLARATION const DomKeyMapEntry dom_key_map[] =
41 #include "ui/events/keycodes/dom3/dom_key_data.h"
42 #undef DOM_KEY_MAP
43 #undef DOM_KEY_MAP_DECLARATION
45 const size_t kDomKeyMapEntries = arraysize(dom_key_map);
47 } // namespace
49 // static
50 size_t KeycodeConverter::NumKeycodeMapEntriesForTest() {
51 return kKeycodeMapEntries;
54 // static
55 const KeycodeMapEntry* KeycodeConverter::GetKeycodeMapForTest() {
56 return &usb_keycode_map[0];
59 // static
60 const char* KeycodeConverter::DomKeyStringForTest(size_t index) {
61 if (index >= kDomKeyMapEntries)
62 return nullptr;
63 return dom_key_map[index].string;
66 // static
67 int KeycodeConverter::InvalidNativeKeycode() {
68 return usb_keycode_map[0].native_keycode;
71 // static
72 const char* KeycodeConverter::NativeKeycodeToCode(int native_keycode) {
73 for (size_t i = 0; i < kKeycodeMapEntries; ++i) {
74 if (usb_keycode_map[i].native_keycode == native_keycode) {
75 if (usb_keycode_map[i].code != NULL)
76 return usb_keycode_map[i].code;
77 break;
80 return "";
83 // static
84 DomCode KeycodeConverter::NativeKeycodeToDomCode(int native_keycode) {
85 for (size_t i = 0; i < kKeycodeMapEntries; ++i) {
86 if (usb_keycode_map[i].native_keycode == native_keycode) {
87 if (usb_keycode_map[i].code != NULL)
88 return static_cast<DomCode>(usb_keycode_map[i].usb_keycode);
89 break;
92 return DomCode::NONE;
95 // static
96 int KeycodeConverter::CodeToNativeKeycode(const char* code) {
97 if (!code || !*code)
98 return InvalidNativeKeycode();
100 for (size_t i = 0; i < kKeycodeMapEntries; ++i) {
101 if (usb_keycode_map[i].code &&
102 strcmp(usb_keycode_map[i].code, code) == 0) {
103 return usb_keycode_map[i].native_keycode;
106 return InvalidNativeKeycode();
109 // static
110 int KeycodeConverter::DomCodeToNativeKeycode(DomCode code) {
111 if (code == DomCode::NONE)
112 return InvalidNativeKeycode();
113 for (size_t i = 0; i < kKeycodeMapEntries; ++i) {
114 if (usb_keycode_map[i].usb_keycode == static_cast<uint32_t>(code))
115 return usb_keycode_map[i].native_keycode;
117 return InvalidNativeKeycode();
120 // static
121 DomCode KeycodeConverter::CodeStringToDomCode(const char* code) {
122 if (!code || !*code) {
123 LOG(WARNING) << "empty code string";
124 return DomCode::NONE;
126 for (size_t i = 0; i < kKeycodeMapEntries; ++i) {
127 if (usb_keycode_map[i].code &&
128 strcmp(usb_keycode_map[i].code, code) == 0) {
129 return static_cast<DomCode>(usb_keycode_map[i].usb_keycode);
132 LOG(WARNING) << "unrecognized code string '" << code << "'";
133 return DomCode::NONE;
136 // static
137 const char* KeycodeConverter::DomCodeToCodeString(DomCode dom_code) {
138 return UsbKeycodeToCode(static_cast<uint32_t>(dom_code));
141 // static
142 DomKey KeycodeConverter::KeyStringToDomKey(const char* key) {
143 if (!key || !*key)
144 return DomKey::NONE;
145 for (size_t i = 0; i < kDomKeyMapEntries; ++i) {
146 if (dom_key_map[i].string &&
147 strcmp(dom_key_map[i].string, key) == 0) {
148 return dom_key_map[i].dom_key;
151 return DomKey::NONE;
154 // static
155 const char* KeycodeConverter::DomKeyToKeyString(DomKey dom_key) {
156 for (size_t i = 0; i < kDomKeyMapEntries; ++i) {
157 if (dom_key_map[i].dom_key == dom_key)
158 return dom_key_map[i].string;
160 return "";
163 // USB keycodes
164 // Note that USB keycodes are not part of any web standard.
165 // Please don't use USB keycodes in new code.
167 // static
168 uint32_t KeycodeConverter::InvalidUsbKeycode() {
169 return usb_keycode_map[0].usb_keycode;
172 // static
173 int KeycodeConverter::UsbKeycodeToNativeKeycode(uint32_t usb_keycode) {
174 // Deal with some special-cases that don't fit the 1:1 mapping.
175 if (usb_keycode == 0x070032) // non-US hash.
176 usb_keycode = 0x070031; // US backslash.
177 #if defined(OS_MACOSX)
178 if (usb_keycode == 0x070046) // PrintScreen.
179 usb_keycode = 0x070068; // F13.
180 #endif
182 for (size_t i = 0; i < kKeycodeMapEntries; ++i) {
183 if (usb_keycode_map[i].usb_keycode == usb_keycode)
184 return usb_keycode_map[i].native_keycode;
186 return InvalidNativeKeycode();
189 // static
190 uint32_t KeycodeConverter::NativeKeycodeToUsbKeycode(int native_keycode) {
191 for (size_t i = 0; i < kKeycodeMapEntries; ++i) {
192 if (usb_keycode_map[i].native_keycode == native_keycode)
193 return usb_keycode_map[i].usb_keycode;
195 return InvalidUsbKeycode();
198 // static
199 const char* KeycodeConverter::UsbKeycodeToCode(uint32_t usb_keycode) {
200 for (size_t i = 0; i < kKeycodeMapEntries; ++i) {
201 if (usb_keycode_map[i].usb_keycode == usb_keycode) {
202 if (usb_keycode_map[i].code)
203 return usb_keycode_map[i].code;
204 break;
207 return "";
210 // static
211 DomCode KeycodeConverter::UsbKeycodeToDomCode(uint32_t usb_keycode) {
212 for (size_t i = 0; i < kKeycodeMapEntries; ++i) {
213 if (usb_keycode_map[i].usb_keycode == usb_keycode)
214 return static_cast<DomCode>(usb_keycode);
216 return DomCode::NONE;
219 // static
220 uint32_t KeycodeConverter::CodeToUsbKeycode(const char* code) {
221 if (!code || !*code)
222 return InvalidUsbKeycode();
224 for (size_t i = 0; i < kKeycodeMapEntries; ++i) {
225 if (usb_keycode_map[i].code &&
226 strcmp(usb_keycode_map[i].code, code) == 0) {
227 return usb_keycode_map[i].usb_keycode;
230 return InvalidUsbKeycode();
233 } // namespace ui