OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/keyboard_code_conversion_x.h" | 5 #include "ui/events/keycodes/keyboard_code_conversion_x.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #define XK_3270 // for XK_3270_BackTab | 9 #define XK_3270 // for XK_3270_BackTab |
10 #include <X11/XF86keysym.h> | 10 #include <X11/XF86keysym.h> |
11 #include <X11/Xlib.h> | 11 #include <X11/Xlib.h> |
12 #include <X11/Xutil.h> | 12 #include <X11/Xutil.h> |
13 #include <X11/extensions/XInput2.h> | 13 #include <X11/extensions/XInput2.h> |
14 #include <X11/keysym.h> | 14 #include <X11/keysym.h> |
15 | 15 |
16 #include "base/basictypes.h" | 16 #include "base/basictypes.h" |
17 #include "base/logging.h" | 17 #include "base/logging.h" |
18 #include "base/strings/stringprintf.h" | 18 #include "base/strings/stringprintf.h" |
19 #include "base/strings/sys_string_conversions.h" | 19 #include "base/strings/sys_string_conversions.h" |
20 #include "base/strings/utf_string_conversions.h" | 20 #include "base/strings/utf_string_conversions.h" |
21 #include "ui/events/keycodes/dom4/keycode_converter.h" | 21 #include "ui/events/keycodes/dom4/keycode_converter.h" |
| 22 #include "ui/events/x/keysym_to_unicode.h" |
22 | 23 |
23 #define VKEY_UNSUPPORTED VKEY_UNKNOWN | 24 #define VKEY_UNSUPPORTED VKEY_UNKNOWN |
24 | 25 |
25 namespace ui { | 26 namespace ui { |
26 | 27 |
27 namespace { | 28 namespace { |
28 | 29 |
29 // MAP0 - MAP3: | 30 // MAP0 - MAP3: |
30 // These are the generated VKEY code maps for all possible Latin keyboard | 31 // These are the generated VKEY code maps for all possible Latin keyboard |
31 // layouts in Windows. And the maps are only for special letter keys excluding | 32 // layouts in Windows. And the maps are only for special letter keys excluding |
(...skipping 437 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
469 // 4. If not found, fallback to find keysym + hardware_code in map1. | 470 // 4. If not found, fallback to find keysym + hardware_code in map1. |
470 // 5. If not found, fallback to find keysym + keysym_shift + hardware_code | 471 // 5. If not found, fallback to find keysym + keysym_shift + hardware_code |
471 // in map2. | 472 // in map2. |
472 // 6. If not found, fallback to find keysym + keysym_shift + keysym_altgr + | 473 // 6. If not found, fallback to find keysym + keysym_shift + keysym_altgr + |
473 // hardware_code in map3. | 474 // hardware_code in map3. |
474 // 7. If not found, fallback to find in KeyboardCodeFromXKeysym(), which | 475 // 7. If not found, fallback to find in KeyboardCodeFromXKeysym(), which |
475 // mainly for non-letter keys. | 476 // mainly for non-letter keys. |
476 // 8. If not found, fallback to find with the hardware code in US layout. | 477 // 8. If not found, fallback to find with the hardware code in US layout. |
477 | 478 |
478 KeySym keysym = NoSymbol; | 479 KeySym keysym = NoSymbol; |
479 XEvent xkeyevent; | 480 XEvent xkeyevent = {0}; |
480 if (xev->type == GenericEvent) { | 481 if (xev->type == GenericEvent) { |
481 // Convert the XI2 key event into a core key event so that we can | 482 // Convert the XI2 key event into a core key event so that we can |
482 // continue to use XLookupString() until crbug.com/367732 is complete. | 483 // continue to use XLookupString() until crbug.com/367732 is complete. |
483 InitXKeyEventFromXIDeviceEvent(*xev, &xkeyevent); | 484 InitXKeyEventFromXIDeviceEvent(*xev, &xkeyevent); |
484 } else { | 485 } else { |
485 xkeyevent.xkey = xev->xkey; | 486 xkeyevent.xkey = xev->xkey; |
486 } | 487 } |
487 XKeyEvent* xkey = &xkeyevent.xkey; | 488 XKeyEvent* xkey = &xkeyevent.xkey; |
488 xkey->state &= (~0xFF | Mod2Mask); // Clears the xkey's state except numlock. | 489 xkey->state &= (~0xFF | Mod2Mask); // Clears the xkey's state except numlock. |
489 // XLookupKeysym does not take into consideration the state of the lock/shift | 490 // XLookupKeysym does not take into consideration the state of the lock/shift |
(...skipping 344 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
834 } | 835 } |
835 | 836 |
836 const char* CodeFromXEvent(const XEvent* xev) { | 837 const char* CodeFromXEvent(const XEvent* xev) { |
837 int keycode = (xev->type == GenericEvent) | 838 int keycode = (xev->type == GenericEvent) |
838 ? static_cast<XIDeviceEvent*>(xev->xcookie.data)->detail | 839 ? static_cast<XIDeviceEvent*>(xev->xcookie.data)->detail |
839 : xev->xkey.keycode; | 840 : xev->xkey.keycode; |
840 return KeycodeConverter::GetInstance()->NativeKeycodeToCode(keycode); | 841 return KeycodeConverter::GetInstance()->NativeKeycodeToCode(keycode); |
841 } | 842 } |
842 | 843 |
843 uint16 GetCharacterFromXEvent(const XEvent* xev) { | 844 uint16 GetCharacterFromXEvent(const XEvent* xev) { |
844 XEvent xkeyevent; | 845 XEvent xkeyevent = {0}; |
845 const XKeyEvent* xkey = NULL; | 846 const XKeyEvent* xkey = NULL; |
846 char buf[6]; | |
847 if (xev->type == GenericEvent) { | 847 if (xev->type == GenericEvent) { |
848 // Convert the XI2 key event into a core key event so that we can | 848 // Convert the XI2 key event into a core key event so that we can |
849 // continue to use XLookupString() until crbug.com/367732 is complete. | 849 // continue to use XLookupString() until crbug.com/367732 is complete. |
850 InitXKeyEventFromXIDeviceEvent(*xev, &xkeyevent); | 850 InitXKeyEventFromXIDeviceEvent(*xev, &xkeyevent); |
851 xkey = &xkeyevent.xkey; | 851 xkey = &xkeyevent.xkey; |
852 } else { | 852 } else { |
853 xkey = &xev->xkey; | 853 xkey = &xev->xkey; |
854 } | 854 } |
855 int bytes_written = | 855 KeySym keysym = XK_VoidSymbol; |
856 XLookupString(const_cast<XKeyEvent*>(xkey), buf, 6, NULL, NULL); | 856 XLookupString(const_cast<XKeyEvent*>(xkey), NULL, 0, &keysym, NULL); |
857 DCHECK_LE(bytes_written, 6); | 857 return GetUnicodeCharacterFromXKeySym(keysym); |
858 | |
859 if (bytes_written <= 0) | |
860 return 0; | |
861 const base::string16& result = base::WideToUTF16( | |
862 base::SysNativeMBToWide(base::StringPiece(buf, bytes_written))); | |
863 return result.length() == 1 ? result[0] : 0; | |
864 } | 858 } |
865 | 859 |
866 KeyboardCode DefaultKeyboardCodeFromHardwareKeycode( | 860 KeyboardCode DefaultKeyboardCodeFromHardwareKeycode( |
867 unsigned int hardware_code) { | 861 unsigned int hardware_code) { |
868 // This function assumes that X11 is using evdev-based keycodes. | 862 // This function assumes that X11 is using evdev-based keycodes. |
869 static const KeyboardCode kHardwareKeycodeMap[] = { | 863 static const KeyboardCode kHardwareKeycodeMap[] = { |
870 // Please refer to below links for the table content: | 864 // Please refer to below links for the table content: |
871 // http://www.w3.org/TR/DOM-Level-3-Events-code/#keyboard-101 | 865 // http://www.w3.org/TR/DOM-Level-3-Events-code/#keyboard-101 |
872 // https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent.keyCode | 866 // https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent.keyCode |
873 // http://download.microsoft.com/download/1/6/1/161ba512-40e2-4cc9-843a-92
3143f3456c/translate.pdf | 867 // http://download.microsoft.com/download/1/6/1/161ba512-40e2-4cc9-843a-92
3143f3456c/translate.pdf |
(...skipping 474 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1348 // alone does not map to XK_less; XKeysymToKeycode() returns KEY_102ND | 1342 // alone does not map to XK_less; XKeysymToKeycode() returns KEY_102ND |
1349 // (the '<>' key between Shift and Z on 105-key keyboards) which does. | 1343 // (the '<>' key between Shift and Z on 105-key keyboards) which does. |
1350 // | 1344 // |
1351 // crbug.com/386066 and crbug.com/390263 are examples of problems | 1345 // crbug.com/386066 and crbug.com/390263 are examples of problems |
1352 // associated with this. | 1346 // associated with this. |
1353 // | 1347 // |
1354 return XKeysymToKeycode(display, XKeysymForWindowsKeyCode(key_code, false)); | 1348 return XKeysymToKeycode(display, XKeysymForWindowsKeyCode(key_code, false)); |
1355 } | 1349 } |
1356 | 1350 |
1357 } // namespace ui | 1351 } // namespace ui |
OLD | NEW |