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"
15 // Table of USB codes (equivalent to DomCode values), native scan codes,
16 // and DOM Level 3 |code| strings.
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}
24 #define USB_KEYMAP(usb, xkb, win, mac, code, id) {usb, 0, code}
26 #define USB_KEYMAP_DECLARATION const KeycodeMapEntry usb_keycode_map[] =
27 #include "ui/events/keycodes/dom4/keycode_converter_data.h"
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
{
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"
43 #undef DOM_KEY_MAP_DECLARATION
45 const size_t kDomKeyMapEntries
= arraysize(dom_key_map
);
50 size_t KeycodeConverter::NumKeycodeMapEntriesForTest() {
51 return kKeycodeMapEntries
;
55 const KeycodeMapEntry
* KeycodeConverter::GetKeycodeMapForTest() {
56 return &usb_keycode_map
[0];
60 const char* KeycodeConverter::DomKeyStringForTest(size_t index
) {
61 if (index
>= kDomKeyMapEntries
)
63 return dom_key_map
[index
].string
;
67 int KeycodeConverter::InvalidNativeKeycode() {
68 return usb_keycode_map
[0].native_keycode
;
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
;
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
);
96 int KeycodeConverter::CodeToNativeKeycode(const char* 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();
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();
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
;
137 const char* KeycodeConverter::DomCodeToCodeString(DomCode dom_code
) {
138 return UsbKeycodeToCode(static_cast<uint32_t>(dom_code
));
142 DomKey
KeycodeConverter::KeyStringToDomKey(const char* key
) {
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
;
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
;
164 // Note that USB keycodes are not part of any web standard.
165 // Please don't use USB keycodes in new code.
168 uint32_t KeycodeConverter::InvalidUsbKeycode() {
169 return usb_keycode_map
[0].usb_keycode
;
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.
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();
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();
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
;
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
;
220 uint32_t KeycodeConverter::CodeToUsbKeycode(const char* 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();