base: Change DCHECK_IS_ON to a macro DCHECK_IS_ON().
[chromium-blink-merge.git] / ui / events / keycodes / dom4 / keycode_converter.cc
blob7c81862278f1bf1f126f33f20fc32cc01af4d4a6
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 "ui/events/keycodes/dom3/dom_code.h"
8 #include "ui/events/keycodes/dom3/dom_key.h"
10 namespace ui {
12 namespace {
14 // Table of USB codes (equivalent to DomCode values), native scan codes,
15 // and DOM Level 3 |code| strings.
16 #if defined(OS_WIN)
17 #define USB_KEYMAP(usb, xkb, win, mac, code, id) {usb, win, code}
18 #elif defined(OS_LINUX)
19 #define USB_KEYMAP(usb, xkb, win, mac, code, id) {usb, xkb, code}
20 #elif defined(OS_MACOSX)
21 #define USB_KEYMAP(usb, xkb, win, mac, code, id) {usb, mac, code}
22 #else
23 #define USB_KEYMAP(usb, xkb, win, mac, code, id) {usb, 0, code}
24 #endif
25 #define USB_KEYMAP_DECLARATION const KeycodeMapEntry usb_keycode_map[] =
26 #include "ui/events/keycodes/dom4/keycode_converter_data.h"
27 #undef USB_KEYMAP
28 #undef USB_KEYMAP_DECLARATION
30 const size_t kKeycodeMapEntries = arraysize(usb_keycode_map);
32 // Table of DomKey enum values and DOM Level 3 |key| strings.
33 struct DomKeyMapEntry {
34 DomKey dom_key;
35 const char* string;
38 #define DOM_KEY_MAP(key, id) {DomKey::id, key}
39 #define DOM_KEY_MAP_DECLARATION const DomKeyMapEntry dom_key_map[] =
40 #include "ui/events/keycodes/dom3/dom_key_data.h"
41 #undef DOM_KEY_MAP
42 #undef DOM_KEY_MAP_DECLARATION
44 const size_t kDomKeyMapEntries = arraysize(dom_key_map);
46 } // namespace
48 // static
49 size_t KeycodeConverter::NumKeycodeMapEntriesForTest() {
50 return kKeycodeMapEntries;
53 // static
54 const KeycodeMapEntry* KeycodeConverter::GetKeycodeMapForTest() {
55 return &usb_keycode_map[0];
58 // static
59 const char* KeycodeConverter::DomKeyStringForTest(size_t index) {
60 if (index >= kDomKeyMapEntries)
61 return nullptr;
62 return dom_key_map[index].string;
65 // static
66 int KeycodeConverter::InvalidNativeKeycode() {
67 return usb_keycode_map[0].native_keycode;
70 // static
71 const char* KeycodeConverter::NativeKeycodeToCode(int native_keycode) {
72 for (size_t i = 0; i < kKeycodeMapEntries; ++i) {
73 if (usb_keycode_map[i].native_keycode == native_keycode) {
74 if (usb_keycode_map[i].code != NULL)
75 return usb_keycode_map[i].code;
76 break;
79 return "";
82 // static
83 DomCode KeycodeConverter::NativeKeycodeToDomCode(int native_keycode) {
84 for (size_t i = 0; i < kKeycodeMapEntries; ++i) {
85 if (usb_keycode_map[i].native_keycode == native_keycode) {
86 if (usb_keycode_map[i].code != NULL)
87 return static_cast<DomCode>(usb_keycode_map[i].usb_keycode);
88 break;
91 return DomCode::NONE;
94 // static
95 int KeycodeConverter::CodeToNativeKeycode(const char* code) {
96 if (!code || !*code)
97 return InvalidNativeKeycode();
99 for (size_t i = 0; i < kKeycodeMapEntries; ++i) {
100 if (usb_keycode_map[i].code &&
101 strcmp(usb_keycode_map[i].code, code) == 0) {
102 return usb_keycode_map[i].native_keycode;
105 return InvalidNativeKeycode();
108 // static
109 int KeycodeConverter::DomCodeToNativeKeycode(DomCode code) {
110 if (code == DomCode::NONE)
111 return InvalidNativeKeycode();
112 for (size_t i = 0; i < kKeycodeMapEntries; ++i) {
113 if (usb_keycode_map[i].usb_keycode == static_cast<uint32_t>(code))
114 return usb_keycode_map[i].native_keycode;
116 return InvalidNativeKeycode();
119 // static
120 DomCode KeycodeConverter::CodeStringToDomCode(const char* code) {
121 if (!code || !*code)
122 return DomCode::NONE;
123 for (size_t i = 0; i < kKeycodeMapEntries; ++i) {
124 if (usb_keycode_map[i].code &&
125 strcmp(usb_keycode_map[i].code, code) == 0) {
126 return static_cast<DomCode>(usb_keycode_map[i].usb_keycode);
129 return DomCode::NONE;
132 // static
133 const char* KeycodeConverter::DomCodeToCodeString(DomCode dom_code) {
134 return UsbKeycodeToCode(static_cast<uint32_t>(dom_code));
137 // static
138 DomKey KeycodeConverter::KeyStringToDomKey(const char* key) {
139 if (!key || !*key)
140 return DomKey::NONE;
141 for (size_t i = 0; i < kDomKeyMapEntries; ++i) {
142 if (dom_key_map[i].string &&
143 strcmp(dom_key_map[i].string, key) == 0) {
144 return dom_key_map[i].dom_key;
147 return DomKey::NONE;
150 // static
151 const char* KeycodeConverter::DomKeyToKeyString(DomKey dom_key) {
152 for (size_t i = 0; i < kDomKeyMapEntries; ++i) {
153 if (dom_key_map[i].dom_key == dom_key)
154 return dom_key_map[i].string;
156 return "";
159 // USB keycodes
160 // Note that USB keycodes are not part of any web standard.
161 // Please don't use USB keycodes in new code.
163 // static
164 uint32_t KeycodeConverter::InvalidUsbKeycode() {
165 return usb_keycode_map[0].usb_keycode;
168 // static
169 int KeycodeConverter::UsbKeycodeToNativeKeycode(uint32_t usb_keycode) {
170 // Deal with some special-cases that don't fit the 1:1 mapping.
171 if (usb_keycode == 0x070032) // non-US hash.
172 usb_keycode = 0x070031; // US backslash.
173 #if defined(OS_MACOSX)
174 if (usb_keycode == 0x070046) // PrintScreen.
175 usb_keycode = 0x070068; // F13.
176 #endif
178 for (size_t i = 0; i < kKeycodeMapEntries; ++i) {
179 if (usb_keycode_map[i].usb_keycode == usb_keycode)
180 return usb_keycode_map[i].native_keycode;
182 return InvalidNativeKeycode();
185 // static
186 uint32_t KeycodeConverter::NativeKeycodeToUsbKeycode(int native_keycode) {
187 for (size_t i = 0; i < kKeycodeMapEntries; ++i) {
188 if (usb_keycode_map[i].native_keycode == native_keycode)
189 return usb_keycode_map[i].usb_keycode;
191 return InvalidUsbKeycode();
194 // static
195 const char* KeycodeConverter::UsbKeycodeToCode(uint32_t usb_keycode) {
196 for (size_t i = 0; i < kKeycodeMapEntries; ++i) {
197 if (usb_keycode_map[i].usb_keycode == usb_keycode) {
198 if (usb_keycode_map[i].code)
199 return usb_keycode_map[i].code;
200 break;
203 return "";
206 // static
207 DomCode KeycodeConverter::UsbKeycodeToDomCode(uint32_t usb_keycode) {
208 for (size_t i = 0; i < kKeycodeMapEntries; ++i) {
209 if (usb_keycode_map[i].usb_keycode == usb_keycode)
210 return static_cast<DomCode>(usb_keycode);
212 return DomCode::NONE;
215 // static
216 uint32_t KeycodeConverter::CodeToUsbKeycode(const char* code) {
217 if (!code || !*code)
218 return InvalidUsbKeycode();
220 for (size_t i = 0; i < kKeycodeMapEntries; ++i) {
221 if (usb_keycode_map[i].code &&
222 strcmp(usb_keycode_map[i].code, code) == 0) {
223 return usb_keycode_map[i].usb_keycode;
226 return InvalidUsbKeycode();
229 } // namespace ui