summaryrefslogtreecommitdiff
path: root/chromium/ui/events/keycodes/dom/keycode_converter.cc
blob: a8a1615e52ff5c48bacfecc75486c795a12883d4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ui/events/keycodes/dom/keycode_converter.h"

#include "base/logging.h"
#include "base/strings/utf_string_conversion_utils.h"
#include "build/build_config.h"
#include "ui/events/keycodes/dom/dom_code.h"
#include "ui/events/keycodes/dom/dom_key.h"

#if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
#include <linux/input.h>
#endif

namespace ui {

namespace {

// Table of USB codes (equivalent to DomCode values), native scan codes,
// and DOM Level 3 |code| strings.
#if BUILDFLAG(IS_WIN)
#define DOM_CODE(usb, evdev, xkb, win, mac, code, id) \
  { usb, win, code }
#elif BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
#define DOM_CODE(usb, evdev, xkb, win, mac, code, id) \
  { usb, xkb, code }
#elif BUILDFLAG(IS_APPLE)
#define DOM_CODE(usb, evdev, xkb, win, mac, code, id) \
  { usb, mac, code }
#elif BUILDFLAG(IS_ANDROID)
#define DOM_CODE(usb, evdev, xkb, win, mac, code, id) \
  { usb, evdev, code }
#elif BUILDFLAG(IS_FUCHSIA)
#define DOM_CODE(usb, evdev, xkb, win, mac, code, id) \
  { usb, usb, code }
#else
#error Unsupported platform
#endif
#define DOM_CODE_DECLARATION constexpr KeycodeMapEntry kDomCodeMappings[] =
#include "ui/events/keycodes/dom/dom_code_data.inc"
#undef DOM_CODE
#undef DOM_CODE_DECLARATION

// Table of DomKey enum values and DOM Level 3 |key| strings.
struct DomKeyMapEntry {
  DomKey dom_key;
  const char* string;
};

#define DOM_KEY_MAP_DECLARATION constexpr DomKeyMapEntry kDomKeyMappings[] =
#define DOM_KEY_UNI(key, id, value) {DomKey::id, key}
#define DOM_KEY_MAP(key, id, value) {DomKey::id, key}
#include "ui/events/keycodes/dom/dom_key_data.inc"
#undef DOM_KEY_MAP_DECLARATION
#undef DOM_KEY_MAP
#undef DOM_KEY_UNI

#if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)

// The offset between XKB Keycode and evdev code.
constexpr int kXkbKeycodeOffset = 8;

// TODO(crbug.com/1135034): After migrating native code for
// these platforms from XKB to evdev, use XKB_INVALID_KEYCODE
// (=0xFFFFFFFF) to represent invalid XKB keycode.
// Currently, 0 is returned for backward compatibility.

// Converts XKB keycode to evdev code, based on the mapping
// usually available at /usr/share/X11/xkb/keycodes/evdev.
// See also
// https://xkbcommon.org/doc/current/xkbcommon_8h.html#ac29aee92124c08d1953910ab28ee1997
// for the reference of the history of key mapping.
// Returns KEY_RESERVED for unknown XKB keycode mapping.
int XkbKeycodeToEvdevCode(uint32_t xkb_keycode) {
  // There's no mapping from XKB keycode in range [0-7] (inclusive)
  // to evdev. Return KEY_RESERVED as an error.
  if (xkb_keycode < kXkbKeycodeOffset)
    return KEY_RESERVED;
  return static_cast<int>(xkb_keycode - kXkbKeycodeOffset);
}

// Converts evdev code into XKB keycode.
// Returns KeycodeConverter::InvalidNativeKeycode() if the given code is in
// the invalid range or KEY_RESERVED.
uint32_t EvdevCodeToXkbKeycode(int evdev_code) {
  if (evdev_code < 0 || evdev_code > KEY_MAX || evdev_code == KEY_RESERVED)
    return KeycodeConverter::InvalidNativeKeycode();

  // TODO(crbug.com/1135034): Move this to EvdevCodeToDomCode on
  // migration.
  if (evdev_code == KEY_PLAYCD)
    evdev_code = KEY_PLAY;
  return static_cast<uint32_t>(evdev_code + kXkbKeycodeOffset);
}

#endif  // BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)

}  // namespace

// static
size_t KeycodeConverter::NumKeycodeMapEntriesForTest() {
  return std::size(kDomCodeMappings);
}

// static
const KeycodeMapEntry* KeycodeConverter::GetKeycodeMapForTest() {
  return &kDomCodeMappings[0];
}

// static
const char* KeycodeConverter::DomKeyStringForTest(size_t index) {
  if (index >= std::size(kDomKeyMappings))
    return nullptr;
  return kDomKeyMappings[index].string;
}

// static
int KeycodeConverter::InvalidNativeKeycode() {
  return kDomCodeMappings[0].native_keycode;
}

// TODO(zijiehe): Most of the following functions can be optimized by using
// either multiple arrays or unordered_map.

// static
DomCode KeycodeConverter::NativeKeycodeToDomCode(int native_keycode) {
  for (auto& mapping : kDomCodeMappings) {
    if (mapping.native_keycode == native_keycode)
      return static_cast<DomCode>(mapping.usb_keycode);
  }
  return DomCode::NONE;
}

// static
int KeycodeConverter::DomCodeToNativeKeycode(DomCode code) {
  return UsbKeycodeToNativeKeycode(static_cast<uint32_t>(code));
}

#if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
// static
DomCode KeycodeConverter::XkbKeycodeToDomCode(uint32_t xkb_keycode) {
  // Currently XKB keycode is the native keycode.
  // TODO(crbug.com/1135034): Replace with evdev.
  return NativeKeycodeToDomCode(static_cast<int>(xkb_keycode));
}

// static
uint32_t KeycodeConverter::DomCodeToXkbKeycode(DomCode code) {
  // Currently XKB keycode is the native keycode.
  // TODO(crbug.com/1135034): Replace with evdev.
  return static_cast<uint32_t>(DomCodeToNativeKeycode(code));
}

// static
DomCode KeycodeConverter::EvdevCodeToDomCode(int evdev_code) {
  // Currently XKB keycode is the native keycode.
  // TODO(crbug.com/1135034): Replace with evdev.
  return XkbKeycodeToDomCode(EvdevCodeToXkbKeycode(evdev_code));
}

// static
int KeycodeConverter::DomCodeToEvdevCode(DomCode code) {
  // Currently XKB keycode is the native keycode.
  // TODO(crbug.com/1135034): Replace with evdev.
  return XkbKeycodeToEvdevCode(DomCodeToXkbKeycode(code));
}
#endif

#if BUILDFLAG(IS_CHROMEOS)
// static
DomCode KeycodeConverter::MapUSPositionalShortcutKeyToDomCode(
    KeyboardCode key_code) {
  // VKEY Mapping: http://kbdlayout.info/kbdus/overview+virtualkeys
  // DomCode Mapping:
  //     https://www.w3.org/TR/DOM-Level-3-Events-code/#writing-system-keys
  switch (key_code) {
    case VKEY_OEM_MINUS:
      return DomCode::MINUS;
    case VKEY_OEM_PLUS:
      return DomCode::EQUAL;
    case VKEY_OEM_2:
      return DomCode::SLASH;
    case VKEY_OEM_4:
      return DomCode::BRACKET_LEFT;
    case VKEY_OEM_6:
      return DomCode::BRACKET_RIGHT;
    case VKEY_OEM_COMMA:
      return DomCode::COMMA;
    case VKEY_OEM_PERIOD:
      return DomCode::PERIOD;
    default:
      return DomCode::NONE;
  }
}

// static
KeyboardCode KeycodeConverter::MapPositionalDomCodeToUSShortcutKey(
    DomCode code) {
  // VKEY Mapping: http://kbdlayout.info/kbdus/overview+virtualkeys
  // DomCode Mapping:
  //     https://www.w3.org/TR/DOM-Level-3-Events-code/#writing-system-keys
  switch (code) {
    case DomCode::MINUS:
      return VKEY_OEM_MINUS;
    case DomCode::EQUAL:
      return VKEY_OEM_PLUS;
    case DomCode::SLASH:
      return VKEY_OEM_2;
    case DomCode::BRACKET_LEFT:
      return VKEY_OEM_4;
    case DomCode::BRACKET_RIGHT:
      return VKEY_OEM_6;
    case DomCode::COMMA:
      return VKEY_OEM_COMMA;
    case DomCode::PERIOD:
      return VKEY_OEM_PERIOD;
    default:
      return VKEY_UNKNOWN;
  }
}
#endif

// static
DomCode KeycodeConverter::CodeStringToDomCode(const std::string& code) {
  if (code.empty())
    return DomCode::NONE;
  for (auto& mapping : kDomCodeMappings) {
    if (mapping.code && code == mapping.code) {
      return static_cast<DomCode>(mapping.usb_keycode);
    }
  }
  LOG(WARNING) << "unrecognized code string '" << code << "'";
  return DomCode::NONE;
}

// static
const char* KeycodeConverter::DomCodeToCodeString(DomCode dom_code) {
  for (auto& mapping : kDomCodeMappings) {
    if (mapping.usb_keycode == static_cast<uint32_t>(dom_code)) {
      if (mapping.code)
        return mapping.code;
      break;
    }
  }
  return "";
}

// static
DomKeyLocation KeycodeConverter::DomCodeToLocation(DomCode dom_code) {
  static const struct {
    DomCode code;
    DomKeyLocation location;
  } kLocations[] = {{DomCode::CONTROL_LEFT, DomKeyLocation::LEFT},
                    {DomCode::SHIFT_LEFT, DomKeyLocation::LEFT},
                    {DomCode::ALT_LEFT, DomKeyLocation::LEFT},
                    {DomCode::META_LEFT, DomKeyLocation::LEFT},
                    {DomCode::CONTROL_RIGHT, DomKeyLocation::RIGHT},
                    {DomCode::SHIFT_RIGHT, DomKeyLocation::RIGHT},
                    {DomCode::ALT_RIGHT, DomKeyLocation::RIGHT},
                    {DomCode::META_RIGHT, DomKeyLocation::RIGHT},
                    {DomCode::NUMPAD_DIVIDE, DomKeyLocation::NUMPAD},
                    {DomCode::NUMPAD_MULTIPLY, DomKeyLocation::NUMPAD},
                    {DomCode::NUMPAD_SUBTRACT, DomKeyLocation::NUMPAD},
                    {DomCode::NUMPAD_ADD, DomKeyLocation::NUMPAD},
                    {DomCode::NUMPAD_ENTER, DomKeyLocation::NUMPAD},
                    {DomCode::NUMPAD1, DomKeyLocation::NUMPAD},
                    {DomCode::NUMPAD2, DomKeyLocation::NUMPAD},
                    {DomCode::NUMPAD3, DomKeyLocation::NUMPAD},
                    {DomCode::NUMPAD4, DomKeyLocation::NUMPAD},
                    {DomCode::NUMPAD5, DomKeyLocation::NUMPAD},
                    {DomCode::NUMPAD6, DomKeyLocation::NUMPAD},
                    {DomCode::NUMPAD7, DomKeyLocation::NUMPAD},
                    {DomCode::NUMPAD8, DomKeyLocation::NUMPAD},
                    {DomCode::NUMPAD9, DomKeyLocation::NUMPAD},
                    {DomCode::NUMPAD0, DomKeyLocation::NUMPAD},
                    {DomCode::NUMPAD_DECIMAL, DomKeyLocation::NUMPAD},
                    {DomCode::NUMPAD_EQUAL, DomKeyLocation::NUMPAD},
                    {DomCode::NUMPAD_COMMA, DomKeyLocation::NUMPAD},
                    {DomCode::NUMPAD_PAREN_LEFT, DomKeyLocation::NUMPAD},
                    {DomCode::NUMPAD_PAREN_RIGHT, DomKeyLocation::NUMPAD},
                    {DomCode::NUMPAD_BACKSPACE, DomKeyLocation::NUMPAD},
                    {DomCode::NUMPAD_MEMORY_STORE, DomKeyLocation::NUMPAD},
                    {DomCode::NUMPAD_MEMORY_RECALL, DomKeyLocation::NUMPAD},
                    {DomCode::NUMPAD_MEMORY_CLEAR, DomKeyLocation::NUMPAD},
                    {DomCode::NUMPAD_MEMORY_ADD, DomKeyLocation::NUMPAD},
                    {DomCode::NUMPAD_MEMORY_SUBTRACT, DomKeyLocation::NUMPAD},
                    {DomCode::NUMPAD_SIGN_CHANGE, DomKeyLocation::NUMPAD},
                    {DomCode::NUMPAD_CLEAR, DomKeyLocation::NUMPAD},
                    {DomCode::NUMPAD_CLEAR_ENTRY, DomKeyLocation::NUMPAD}};
  for (const auto& key : kLocations) {
    if (key.code == dom_code)
      return key.location;
  }
  return DomKeyLocation::STANDARD;
}

// static
DomKey KeycodeConverter::KeyStringToDomKey(const std::string& key) {
  if (key.empty())
    return DomKey::NONE;
  // Check for standard key names.
  for (auto& mapping : kDomKeyMappings) {
    if (mapping.string && key == mapping.string) {
      return mapping.dom_key;
    }
  }
  if (key == "Dead") {
    // The web KeyboardEvent string does not encode the combining character,
    // so we just set it to the Unicode designated non-character 0xFFFF.
    // This will round-trip convert back to 'Dead' but take no part in
    // character composition.
    return DomKey::DeadKeyFromCombiningCharacter(0xFFFF);
  }
  // Otherwise, if the string contains a single Unicode character,
  // the key value is that character.
  int32_t char_index = 0;
  uint32_t character;
  if (base::ReadUnicodeCharacter(key.c_str(),
                                 static_cast<int32_t>(key.length()),
                                 &char_index, &character) &&
      key[++char_index] == 0) {
    return DomKey::FromCharacter(character);
  }
  return DomKey::NONE;
}

// static
std::string KeycodeConverter::DomKeyToKeyString(DomKey dom_key) {
  if (dom_key.IsDeadKey()) {
    // All dead-key combining codes collapse to 'Dead', as UI Events
    // KeyboardEvent represents the combining character separately.
    return "Dead";
  }
  for (auto& mapping : kDomKeyMappings) {
    if (mapping.dom_key == dom_key) {
      if (mapping.string)
        return mapping.string;
      break;
    }
  }
  if (dom_key.IsCharacter()) {
    std::string s;
    base::WriteUnicodeCharacter(dom_key.ToCharacter(), &s);
    return s;
  }
  return std::string();
}

// static
bool KeycodeConverter::IsDomKeyForModifier(DomKey dom_key) {
  switch (dom_key) {
    case DomKey::ACCEL:
    case DomKey::ALT:
    case DomKey::ALT_GRAPH:
    case DomKey::CAPS_LOCK:
    case DomKey::CONTROL:
    case DomKey::FN:
    case DomKey::FN_LOCK:
    case DomKey::HYPER:
    case DomKey::META:
    case DomKey::NUM_LOCK:
    case DomKey::SCROLL_LOCK:
    case DomKey::SHIFT:
    case DomKey::SUPER:
    case DomKey::SYMBOL:
    case DomKey::SYMBOL_LOCK:
    case DomKey::SHIFT_LEVEL5:
      return true;
    default:
      return false;
  }
}

// static
bool KeycodeConverter::IsDomKeyNamed(DomKey dom_key) {
  if (dom_key.IsDeadKey()) {
    return true;
  }
  for (auto& mapping : kDomKeyMappings) {
    if (mapping.dom_key == dom_key) {
      return mapping.string != nullptr;
    }
  }
  return false;
}

// USB keycodes
// Note that USB keycodes are not part of any web standard.
// Please don't use USB keycodes in new code.

// static
uint32_t KeycodeConverter::InvalidUsbKeycode() {
  return kDomCodeMappings[0].usb_keycode;
}

// static
int KeycodeConverter::UsbKeycodeToNativeKeycode(uint32_t usb_keycode) {
  // Deal with some special-cases that don't fit the 1:1 mapping.
  if (usb_keycode == 0x070032)  // non-US hash.
    usb_keycode = 0x070031;     // US backslash.
#if BUILDFLAG(IS_APPLE)
  if (usb_keycode == 0x070046) // PrintScreen.
    usb_keycode = 0x070068; // F13.
#endif

  for (auto& mapping : kDomCodeMappings) {
    if (mapping.usb_keycode == usb_keycode)
      return mapping.native_keycode;
  }
  return InvalidNativeKeycode();
}

// static
uint32_t KeycodeConverter::NativeKeycodeToUsbKeycode(int native_keycode) {
  for (auto& mapping : kDomCodeMappings) {
    if (mapping.native_keycode == native_keycode)
      return mapping.usb_keycode;
  }
  return InvalidUsbKeycode();
}

// static
DomCode KeycodeConverter::UsbKeycodeToDomCode(uint32_t usb_keycode) {
  for (auto& mapping : kDomCodeMappings) {
    if (mapping.usb_keycode == usb_keycode)
      return static_cast<DomCode>(usb_keycode);
  }
  return DomCode::NONE;
}

// static
uint32_t KeycodeConverter::DomCodeToUsbKeycode(DomCode dom_code) {
  for (auto& mapping : kDomCodeMappings) {
    if (mapping.usb_keycode == static_cast<uint32_t>(dom_code))
      return mapping.usb_keycode;
  }
  return InvalidUsbKeycode();
}

// static
uint32_t KeycodeConverter::CodeStringToUsbKeycode(const std::string& code) {
  if (code.empty())
    return InvalidUsbKeycode();

  for (auto& mapping : kDomCodeMappings) {
    if (mapping.code && code == mapping.code) {
      return mapping.usb_keycode;
    }
  }
  return InvalidUsbKeycode();
}

// static
int KeycodeConverter::CodeStringToNativeKeycode(const std::string& code) {
  return UsbKeycodeToNativeKeycode(CodeStringToUsbKeycode(code));
}

}  // namespace ui