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.
4
5 #include "ui/events/keycodes/dom4/keycode_converter.h"
6
7 namespace ui {
8
9 namespace {
10
11 #if defined(OS_WIN)
12 #define USB_KEYMAP(usb, xkb, win, mac, code) {usb, win, code}
13 #elif defined(OS_LINUX)
14 #define USB_KEYMAP(usb, xkb, win, mac, code) {usb, xkb, code}
15 #elif defined(OS_MACOSX)
16 #define USB_KEYMAP(usb, xkb, win, mac, code) {usb, mac, code}
17 #else
18 #define USB_KEYMAP(usb, xkb, win, mac, code) {usb, 0, code}
19 #endif
20 #include "ui/events/keycodes/dom4/keycode_converter_data.h"
21
22 const size_t kKeycodeMapEntries = arraysize(usb_keycode_map);
23
24 } // namespace
25
KeycodeConverter()26 KeycodeConverter::KeycodeConverter() {
27 }
28
GetInstance()29 KeycodeConverter* KeycodeConverter::GetInstance() {
30 return Singleton<KeycodeConverter>::get();
31 }
32
NumKeycodeMapEntriesForTest()33 size_t KeycodeConverter::NumKeycodeMapEntriesForTest() {
34 return kKeycodeMapEntries;
35 }
36
GetKeycodeMapForTest()37 const KeycodeMapEntry* KeycodeConverter::GetKeycodeMapForTest() {
38 return &usb_keycode_map[0];
39 }
40
InvalidNativeKeycode()41 uint16_t KeycodeConverter::InvalidNativeKeycode() {
42 return usb_keycode_map[0].native_keycode;
43 }
44
InvalidKeyboardEventCode()45 const char* KeycodeConverter::InvalidKeyboardEventCode() {
46 return "Unidentified";
47 }
48
NativeKeycodeToCode(uint16_t native_keycode)49 const char* KeycodeConverter::NativeKeycodeToCode(uint16_t native_keycode) {
50 for (size_t i = 0; i < kKeycodeMapEntries; ++i) {
51 if (usb_keycode_map[i].native_keycode == native_keycode) {
52 if (usb_keycode_map[i].code != NULL)
53 return usb_keycode_map[i].code;
54 break;
55 }
56 }
57 return InvalidKeyboardEventCode();
58 }
59
CodeToNativeKeycode(const char * code)60 uint16_t KeycodeConverter::CodeToNativeKeycode(const char* code) {
61 if (!code ||
62 strcmp(code, InvalidKeyboardEventCode()) == 0) {
63 return InvalidNativeKeycode();
64 }
65
66 for (size_t i = 0; i < kKeycodeMapEntries; ++i) {
67 if (usb_keycode_map[i].code &&
68 strcmp(usb_keycode_map[i].code, code) == 0) {
69 return usb_keycode_map[i].native_keycode;
70 }
71 }
72 return InvalidNativeKeycode();
73 }
74
75 // USB keycodes
76 // Note that USB keycodes are not part of any web standard.
77 // Please don't use USB keycodes in new code.
78
InvalidUsbKeycode()79 uint16_t KeycodeConverter::InvalidUsbKeycode() {
80 return usb_keycode_map[0].usb_keycode;
81 }
82
UsbKeycodeToNativeKeycode(uint32_t usb_keycode)83 uint16_t KeycodeConverter::UsbKeycodeToNativeKeycode(uint32_t usb_keycode) {
84 // Deal with some special-cases that don't fit the 1:1 mapping.
85 if (usb_keycode == 0x070032) // non-US hash.
86 usb_keycode = 0x070031; // US backslash.
87 #if defined(OS_MACOSX)
88 if (usb_keycode == 0x070046) // PrintScreen.
89 usb_keycode = 0x070068; // F13.
90 #endif
91
92 for (size_t i = 0; i < kKeycodeMapEntries; ++i) {
93 if (usb_keycode_map[i].usb_keycode == usb_keycode)
94 return usb_keycode_map[i].native_keycode;
95 }
96 return InvalidNativeKeycode();
97 }
98
NativeKeycodeToUsbKeycode(uint16_t native_keycode)99 uint32_t KeycodeConverter::NativeKeycodeToUsbKeycode(uint16_t native_keycode) {
100 for (size_t i = 0; i < kKeycodeMapEntries; ++i) {
101 if (usb_keycode_map[i].native_keycode == native_keycode)
102 return usb_keycode_map[i].usb_keycode;
103 }
104 return InvalidUsbKeycode();
105 }
106
UsbKeycodeToCode(uint32_t usb_keycode)107 const char* KeycodeConverter::UsbKeycodeToCode(uint32_t usb_keycode) {
108 for (size_t i = 0; i < kKeycodeMapEntries; ++i) {
109 if (usb_keycode_map[i].usb_keycode == usb_keycode)
110 return usb_keycode_map[i].code;
111 }
112 return InvalidKeyboardEventCode();
113 }
114
CodeToUsbKeycode(const char * code)115 uint32_t KeycodeConverter::CodeToUsbKeycode(const char* code) {
116 if (!code ||
117 strcmp(code, InvalidKeyboardEventCode()) == 0) {
118 return InvalidUsbKeycode();
119 }
120
121 for (size_t i = 0; i < kKeycodeMapEntries; ++i) {
122 if (usb_keycode_map[i].code &&
123 strcmp(usb_keycode_map[i].code, code) == 0) {
124 return usb_keycode_map[i].usb_keycode;
125 }
126 }
127 return InvalidUsbKeycode();
128 }
129
130 } // namespace ui
131