Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/base/keycodes/keyboard_code_conversion_x.h" | 5 #include "ui/base/keycodes/keyboard_code_conversion_x.h" |
| 6 | 6 |
| 7 #include <X11/keysym.h> | 7 #include <X11/keysym.h> |
| 8 #include <X11/Xlib.h> | 8 #include <X11/Xlib.h> |
| 9 #include <X11/Xutil.h> | 9 #include <X11/Xutil.h> |
| 10 | 10 |
| 11 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
| 12 #include "base/logging.h" | 12 #include "base/logging.h" |
| 13 #include "base/utf_string_conversions.h" | 13 #include "base/utf_string_conversions.h" |
| 14 #include "ui/base/x/x11_util.h" | |
| 15 | |
| 16 namespace { | |
| 17 | |
| 18 // Converts ui::EventType to XKeyEvent state. | |
| 19 unsigned int XKeyEventState(int flags) { | |
| 20 return | |
| 21 ((flags & ui::EF_SHIFT_DOWN) ? ShiftMask : 0) | | |
| 22 ((flags & ui::EF_CONTROL_DOWN) ? ControlMask : 0) | | |
| 23 ((flags & ui::EF_CAPS_LOCK_DOWN) ? LockMask : 0); | |
| 24 } | |
| 25 | |
| 26 // Converts EventType to XKeyEvent type. | |
| 27 int XKeyEventType(ui::EventType type) { | |
| 28 switch (type) { | |
| 29 case ui::ET_KEY_PRESSED: | |
| 30 return KeyPress; | |
| 31 case ui::ET_KEY_RELEASED: | |
| 32 return KeyRelease; | |
| 33 default: | |
| 34 return 0; | |
| 35 } | |
| 36 } | |
| 37 | |
| 38 // Converts KeyboardCode to XKeyEvent keycode. | |
| 39 unsigned int XKeyEventKeyCode(ui::KeyboardCode key_code, | |
| 40 int flags, | |
| 41 Display* display) { | |
| 42 const int keysym = XKeysymForWindowsKeyCode(key_code, | |
| 43 flags & ui::EF_SHIFT_DOWN); | |
| 44 // Tests assume the keycode for XK_less is equal to the one of XK_comma, | |
| 45 // but XKeysymToKeycode returns 94 for XK_less while it returns 59 for | |
| 46 // XK_comma. Here we convert the value for XK_less to the value for XK_comma. | |
| 47 return (keysym == XK_less) ? 59 : XKeysymToKeycode(display, keysym); | |
| 48 } | |
| 49 | |
| 50 } // namespace | |
| 14 | 51 |
| 15 namespace ui { | 52 namespace ui { |
| 16 | 53 |
| 17 // Get an ui::KeyboardCode from an X keyevent | 54 // Get an ui::KeyboardCode from an X keyevent |
| 18 KeyboardCode KeyboardCodeFromXKeyEvent(XEvent* xev) { | 55 KeyboardCode KeyboardCodeFromXKeyEvent(XEvent* xev) { |
| 19 // XLookupKeysym does not take into consideration the state of the lock/shift | 56 // XLookupKeysym does not take into consideration the state of the lock/shift |
| 20 // etc. keys. So it is necessary to use XLookupString instead. | 57 // etc. keys. So it is necessary to use XLookupString instead. |
| 21 KeySym keysym; | 58 KeySym keysym; |
| 22 XLookupString(&xev->xkey, NULL, 0, &keysym, NULL); | 59 XLookupString(&xev->xkey, NULL, 0, &keysym, NULL); |
| 23 KeyboardCode keycode = KeyboardCodeFromXKeysym(keysym); | 60 KeyboardCode keycode = KeyboardCodeFromXKeysym(keysym); |
| (...skipping 589 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 613 case VKEY_F22: | 650 case VKEY_F22: |
| 614 case VKEY_F23: | 651 case VKEY_F23: |
| 615 case VKEY_F24: | 652 case VKEY_F24: |
| 616 return XK_F1 + (keycode - VKEY_F1); | 653 return XK_F1 + (keycode - VKEY_F1); |
| 617 | 654 |
| 618 default: | 655 default: |
| 619 return 0; | 656 return 0; |
| 620 } | 657 } |
| 621 } | 658 } |
| 622 | 659 |
| 660 XEvent* CreateXEventForTesting(EventType type, | |
|
Daniel Erat
2011/12/07 16:37:13
rename to CreateXKeyEventForTesting()?
mazda
2011/12/08 04:44:28
Renamed to InitXKeyEventForTesting.
| |
| 661 KeyboardCode key_code, | |
| 662 int flags) { | |
| 663 Display* display = GetXDisplay(); | |
| 664 XKeyEvent key_event; | |
| 665 key_event.type = XKeyEventType(type); | |
|
Daniel Erat
2011/12/07 16:37:13
add a CHECK() that type is non-zero?
mazda
2011/12/08 04:44:28
Done.
| |
| 666 key_event.serial = 0; | |
| 667 key_event.send_event = 0; | |
| 668 key_event.display = display; | |
| 669 key_event.time = 0; | |
| 670 key_event.window = 0; | |
| 671 key_event.root = 0; | |
| 672 key_event.subwindow = 0; | |
| 673 key_event.x = 0; | |
| 674 key_event.y = 0; | |
| 675 key_event.x_root = 0; | |
| 676 key_event.y_root = 0; | |
| 677 key_event.state = XKeyEventState(flags); | |
| 678 key_event.keycode = XKeyEventKeyCode(key_code, flags, display); | |
| 679 key_event.same_screen = 1; | |
| 680 XEvent* event = new XEvent; | |
|
Daniel Erat
2011/12/07 16:37:13
i'd prefer that you passed a pointer to an already
mazda
2011/12/08 04:44:28
Done.
| |
| 681 event->type = key_event.type; | |
| 682 event->xkey = key_event; | |
| 683 return event; | |
| 684 } | |
| 685 | |
| 623 } // namespace ui | 686 } // namespace ui |
| OLD | NEW |