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"
14 // Table of USB codes (equivalent to DomCode values), native scan codes,
15 // and DOM Level 3 |code| strings.
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}
23 #define USB_KEYMAP(usb, xkb, win, mac, code, id) {usb, 0, code}
25 #define USB_KEYMAP_DECLARATION const KeycodeMapEntry usb_keycode_map[] =
26 #include "ui/events/keycodes/dom4/keycode_converter_data.h"
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
{
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"
42 #undef DOM_KEY_MAP_DECLARATION
44 const size_t kDomKeyMapEntries
= arraysize(dom_key_map
);
49 size_t KeycodeConverter::NumKeycodeMapEntriesForTest() {
50 return kKeycodeMapEntries
;
54 const KeycodeMapEntry
* KeycodeConverter::GetKeycodeMapForTest() {
55 return &usb_keycode_map
[0];
59 const char* KeycodeConverter::DomKeyStringForTest(size_t index
) {
60 if (index
>= kDomKeyMapEntries
)
62 return dom_key_map
[index
].string
;
66 int KeycodeConverter::InvalidNativeKeycode() {
67 return usb_keycode_map
[0].native_keycode
;
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
;
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
);
95 int KeycodeConverter::CodeToNativeKeycode(const char* 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();
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();
120 DomCode
KeycodeConverter::CodeStringToDomCode(const char* 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
;
133 const char* KeycodeConverter::DomCodeToCodeString(DomCode dom_code
) {
134 return UsbKeycodeToCode(static_cast<uint32_t>(dom_code
));
138 DomKey
KeycodeConverter::KeyStringToDomKey(const char* key
) {
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
;
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
;
160 // Note that USB keycodes are not part of any web standard.
161 // Please don't use USB keycodes in new code.
164 uint32_t KeycodeConverter::InvalidUsbKeycode() {
165 return usb_keycode_map
[0].usb_keycode
;
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.
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();
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();
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
;
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
;
216 uint32_t KeycodeConverter::CodeToUsbKeycode(const char* 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();