Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / ui / events / keycodes / dom4 / keycode_converter.cc
blobb7958dd0db19e93ed6785f721b16538445170cf6
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 namespace ui {
9 namespace {
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"
22 const size_t kKeycodeMapEntries = arraysize(usb_keycode_map);
24 } // namespace
26 // static
27 size_t KeycodeConverter::NumKeycodeMapEntriesForTest() {
28 return kKeycodeMapEntries;
31 // static
32 const KeycodeMapEntry* KeycodeConverter::GetKeycodeMapForTest() {
33 return &usb_keycode_map[0];
36 // static
37 uint16_t KeycodeConverter::InvalidNativeKeycode() {
38 return usb_keycode_map[0].native_keycode;
41 // static
42 const char* KeycodeConverter::InvalidKeyboardEventCode() {
43 return "Unidentified";
46 // static
47 const char* KeycodeConverter::NativeKeycodeToCode(uint16_t native_keycode) {
48 for (size_t i = 0; i < kKeycodeMapEntries; ++i) {
49 if (usb_keycode_map[i].native_keycode == native_keycode) {
50 if (usb_keycode_map[i].code != NULL)
51 return usb_keycode_map[i].code;
52 break;
55 return InvalidKeyboardEventCode();
58 // static
59 uint16_t KeycodeConverter::CodeToNativeKeycode(const char* code) {
60 if (!code ||
61 strcmp(code, InvalidKeyboardEventCode()) == 0) {
62 return InvalidNativeKeycode();
65 for (size_t i = 0; i < kKeycodeMapEntries; ++i) {
66 if (usb_keycode_map[i].code &&
67 strcmp(usb_keycode_map[i].code, code) == 0) {
68 return usb_keycode_map[i].native_keycode;
71 return InvalidNativeKeycode();
74 // USB keycodes
75 // Note that USB keycodes are not part of any web standard.
76 // Please don't use USB keycodes in new code.
78 // static
79 uint16_t KeycodeConverter::InvalidUsbKeycode() {
80 return usb_keycode_map[0].usb_keycode;
83 // static
84 uint16_t KeycodeConverter::UsbKeycodeToNativeKeycode(uint32_t usb_keycode) {
85 // Deal with some special-cases that don't fit the 1:1 mapping.
86 if (usb_keycode == 0x070032) // non-US hash.
87 usb_keycode = 0x070031; // US backslash.
88 #if defined(OS_MACOSX)
89 if (usb_keycode == 0x070046) // PrintScreen.
90 usb_keycode = 0x070068; // F13.
91 #endif
93 for (size_t i = 0; i < kKeycodeMapEntries; ++i) {
94 if (usb_keycode_map[i].usb_keycode == usb_keycode)
95 return usb_keycode_map[i].native_keycode;
97 return InvalidNativeKeycode();
100 // static
101 uint32_t KeycodeConverter::NativeKeycodeToUsbKeycode(uint16_t native_keycode) {
102 for (size_t i = 0; i < kKeycodeMapEntries; ++i) {
103 if (usb_keycode_map[i].native_keycode == native_keycode)
104 return usb_keycode_map[i].usb_keycode;
106 return InvalidUsbKeycode();
109 // static
110 const char* KeycodeConverter::UsbKeycodeToCode(uint32_t usb_keycode) {
111 for (size_t i = 0; i < kKeycodeMapEntries; ++i) {
112 if (usb_keycode_map[i].usb_keycode == usb_keycode)
113 return usb_keycode_map[i].code;
115 return InvalidKeyboardEventCode();
118 // static
119 uint32_t KeycodeConverter::CodeToUsbKeycode(const char* code) {
120 if (!code ||
121 strcmp(code, InvalidKeyboardEventCode()) == 0) {
122 return InvalidUsbKeycode();
125 for (size_t i = 0; i < kKeycodeMapEntries; ++i) {
126 if (usb_keycode_map[i].code &&
127 strcmp(usb_keycode_map[i].code, code) == 0) {
128 return usb_keycode_map[i].usb_keycode;
131 return InvalidUsbKeycode();
134 } // namespace ui