OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ui/events/keycodes/dom/keycode_converter.h" | 5 #include "ui/events/keycodes/dom/keycode_converter.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "ui/events/keycodes/dom/dom_code.h" | 8 #include "ui/events/keycodes/dom/dom_code.h" |
9 #include "ui/events/keycodes/dom/dom_key.h" | 9 #include "ui/events/keycodes/dom/dom_key.h" |
10 | 10 |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
62 return nullptr; | 62 return nullptr; |
63 return dom_key_map[index].string; | 63 return dom_key_map[index].string; |
64 } | 64 } |
65 | 65 |
66 // static | 66 // static |
67 int KeycodeConverter::InvalidNativeKeycode() { | 67 int KeycodeConverter::InvalidNativeKeycode() { |
68 return usb_keycode_map[0].native_keycode; | 68 return usb_keycode_map[0].native_keycode; |
69 } | 69 } |
70 | 70 |
71 // static | 71 // static |
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; | |
77 break; | |
78 } | |
79 } | |
80 return ""; | |
81 } | |
82 | |
83 // static | |
84 DomCode KeycodeConverter::NativeKeycodeToDomCode(int native_keycode) { | 72 DomCode KeycodeConverter::NativeKeycodeToDomCode(int native_keycode) { |
85 for (size_t i = 0; i < kKeycodeMapEntries; ++i) { | 73 for (size_t i = 0; i < kKeycodeMapEntries; ++i) { |
86 if (usb_keycode_map[i].native_keycode == native_keycode) { | 74 if (usb_keycode_map[i].native_keycode == native_keycode) { |
87 if (usb_keycode_map[i].code != NULL) | 75 if (usb_keycode_map[i].code != NULL) |
88 return static_cast<DomCode>(usb_keycode_map[i].usb_keycode); | 76 return static_cast<DomCode>(usb_keycode_map[i].usb_keycode); |
89 break; | 77 break; |
90 } | 78 } |
91 } | 79 } |
92 return DomCode::NONE; | 80 return DomCode::NONE; |
93 } | 81 } |
94 | 82 |
95 // static | 83 // static |
96 int KeycodeConverter::CodeToNativeKeycode(const char* code) { | |
97 if (!code || !*code) | |
98 return InvalidNativeKeycode(); | |
99 | |
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; | |
104 } | |
105 } | |
106 return InvalidNativeKeycode(); | |
107 } | |
108 | |
109 // static | |
110 int KeycodeConverter::DomCodeToNativeKeycode(DomCode code) { | 84 int KeycodeConverter::DomCodeToNativeKeycode(DomCode code) { |
111 return UsbKeycodeToNativeKeycode(static_cast<uint32_t>(code)); | 85 return UsbKeycodeToNativeKeycode(static_cast<uint32_t>(code)); |
112 } | 86 } |
113 | 87 |
114 // static | 88 // static |
115 DomCode KeycodeConverter::CodeStringToDomCode(const char* code) { | 89 DomCode KeycodeConverter::CodeStringToDomCode(const char* code) { |
116 if (!code || !*code) { | 90 if (!code || !*code) { |
117 LOG(WARNING) << "empty code string"; | 91 LOG(WARNING) << "empty code string"; |
118 return DomCode::NONE; | 92 return DomCode::NONE; |
119 } | 93 } |
120 for (size_t i = 0; i < kKeycodeMapEntries; ++i) { | 94 for (size_t i = 0; i < kKeycodeMapEntries; ++i) { |
121 if (usb_keycode_map[i].code && | 95 if (usb_keycode_map[i].code && |
122 strcmp(usb_keycode_map[i].code, code) == 0) { | 96 strcmp(usb_keycode_map[i].code, code) == 0) { |
123 return static_cast<DomCode>(usb_keycode_map[i].usb_keycode); | 97 return static_cast<DomCode>(usb_keycode_map[i].usb_keycode); |
124 } | 98 } |
125 } | 99 } |
126 LOG(WARNING) << "unrecognized code string '" << code << "'"; | 100 LOG(WARNING) << "unrecognized code string '" << code << "'"; |
127 return DomCode::NONE; | 101 return DomCode::NONE; |
128 } | 102 } |
129 | 103 |
130 // static | 104 // static |
131 const char* KeycodeConverter::DomCodeToCodeString(DomCode dom_code) { | 105 const char* KeycodeConverter::DomCodeToCodeString(DomCode dom_code) { |
132 return UsbKeycodeToCode(static_cast<uint32_t>(dom_code)); | 106 for (size_t i = 0; i < kKeycodeMapEntries; ++i) { |
| 107 if (usb_keycode_map[i].usb_keycode == static_cast<uint32_t>(dom_code)) { |
| 108 if (usb_keycode_map[i].code) |
| 109 return usb_keycode_map[i].code; |
| 110 break; |
| 111 } |
| 112 } |
| 113 return ""; |
133 } | 114 } |
134 | 115 |
135 // static | 116 // static |
136 DomKeyLocation KeycodeConverter::DomCodeToLocation(DomCode dom_code) { | 117 DomKeyLocation KeycodeConverter::DomCodeToLocation(DomCode dom_code) { |
137 static const struct { | 118 static const struct { |
138 DomCode code; | 119 DomCode code; |
139 DomKeyLocation location; | 120 DomKeyLocation location; |
140 } kLocations[] = {{DomCode::CONTROL_LEFT, DomKeyLocation::LEFT}, | 121 } kLocations[] = {{DomCode::CONTROL_LEFT, DomKeyLocation::LEFT}, |
141 {DomCode::SHIFT_LEFT, DomKeyLocation::LEFT}, | 122 {DomCode::SHIFT_LEFT, DomKeyLocation::LEFT}, |
142 {DomCode::ALT_LEFT, DomKeyLocation::LEFT}, | 123 {DomCode::ALT_LEFT, DomKeyLocation::LEFT}, |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
232 // static | 213 // static |
233 uint32_t KeycodeConverter::NativeKeycodeToUsbKeycode(int native_keycode) { | 214 uint32_t KeycodeConverter::NativeKeycodeToUsbKeycode(int native_keycode) { |
234 for (size_t i = 0; i < kKeycodeMapEntries; ++i) { | 215 for (size_t i = 0; i < kKeycodeMapEntries; ++i) { |
235 if (usb_keycode_map[i].native_keycode == native_keycode) | 216 if (usb_keycode_map[i].native_keycode == native_keycode) |
236 return usb_keycode_map[i].usb_keycode; | 217 return usb_keycode_map[i].usb_keycode; |
237 } | 218 } |
238 return InvalidUsbKeycode(); | 219 return InvalidUsbKeycode(); |
239 } | 220 } |
240 | 221 |
241 // static | 222 // static |
242 const char* KeycodeConverter::UsbKeycodeToCode(uint32_t usb_keycode) { | |
243 for (size_t i = 0; i < kKeycodeMapEntries; ++i) { | |
244 if (usb_keycode_map[i].usb_keycode == usb_keycode) { | |
245 if (usb_keycode_map[i].code) | |
246 return usb_keycode_map[i].code; | |
247 break; | |
248 } | |
249 } | |
250 return ""; | |
251 } | |
252 | |
253 // static | |
254 DomCode KeycodeConverter::UsbKeycodeToDomCode(uint32_t usb_keycode) { | 223 DomCode KeycodeConverter::UsbKeycodeToDomCode(uint32_t usb_keycode) { |
255 for (size_t i = 0; i < kKeycodeMapEntries; ++i) { | 224 for (size_t i = 0; i < kKeycodeMapEntries; ++i) { |
256 if (usb_keycode_map[i].usb_keycode == usb_keycode) | 225 if (usb_keycode_map[i].usb_keycode == usb_keycode) |
257 return static_cast<DomCode>(usb_keycode); | 226 return static_cast<DomCode>(usb_keycode); |
258 } | 227 } |
259 return DomCode::NONE; | 228 return DomCode::NONE; |
260 } | 229 } |
261 | 230 |
262 // static | 231 // static |
| 232 uint32_t KeycodeConverter::DomCodeToUsbKeycode(DomCode dom_code) { |
| 233 for (size_t i = 0; i < kKeycodeMapEntries; ++i) { |
| 234 if (usb_keycode_map[i].usb_keycode == static_cast<uint32_t>(dom_code)) |
| 235 return usb_keycode_map[i].usb_keycode; |
| 236 } |
| 237 return InvalidUsbKeycode(); |
| 238 } |
| 239 |
| 240 // static |
263 uint32_t KeycodeConverter::CodeToUsbKeycode(const char* code) { | 241 uint32_t KeycodeConverter::CodeToUsbKeycode(const char* code) { |
264 if (!code || !*code) | 242 if (!code || !*code) |
265 return InvalidUsbKeycode(); | 243 return InvalidUsbKeycode(); |
266 | 244 |
267 for (size_t i = 0; i < kKeycodeMapEntries; ++i) { | 245 for (size_t i = 0; i < kKeycodeMapEntries; ++i) { |
268 if (usb_keycode_map[i].code && | 246 if (usb_keycode_map[i].code && |
269 strcmp(usb_keycode_map[i].code, code) == 0) { | 247 strcmp(usb_keycode_map[i].code, code) == 0) { |
270 return usb_keycode_map[i].usb_keycode; | 248 return usb_keycode_map[i].usb_keycode; |
271 } | 249 } |
272 } | 250 } |
273 return InvalidUsbKeycode(); | 251 return InvalidUsbKeycode(); |
274 } | 252 } |
275 | 253 |
276 } // namespace ui | 254 } // namespace ui |
OLD | NEW |