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