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/event.h" | 5 #include "ui/events/event.h" |
6 | 6 |
7 #if defined(USE_X11) | 7 #if defined(USE_X11) |
8 #include <X11/extensions/XInput2.h> | 8 #include <X11/extensions/XInput2.h> |
9 #include <X11/Xlib.h> | 9 #include <X11/Xlib.h> |
| 10 #include <X11/keysym.h> |
10 #endif | 11 #endif |
11 | 12 |
12 #include <cmath> | 13 #include <cmath> |
13 #include <cstring> | 14 #include <cstring> |
14 | 15 |
15 #include "base/metrics/histogram.h" | 16 #include "base/metrics/histogram.h" |
16 #include "base/strings/stringprintf.h" | 17 #include "base/strings/stringprintf.h" |
17 #include "ui/events/event_utils.h" | 18 #include "ui/events/event_utils.h" |
18 #include "ui/events/keycodes/keyboard_code_conversion.h" | 19 #include "ui/events/keycodes/keyboard_code_conversion.h" |
19 #include "ui/gfx/point3_f.h" | 20 #include "ui/gfx/point3_f.h" |
(...skipping 564 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
584 code_(CodeFromNative(native_event)), | 585 code_(CodeFromNative(native_event)), |
585 is_char_(IsCharFromNative(native_event)), | 586 is_char_(IsCharFromNative(native_event)), |
586 platform_keycode_(PlatformKeycodeFromNative(native_event)), | 587 platform_keycode_(PlatformKeycodeFromNative(native_event)), |
587 character_(0) { | 588 character_(0) { |
588 if (IsRepeated(*this)) | 589 if (IsRepeated(*this)) |
589 set_flags(flags() | ui::EF_IS_REPEAT); | 590 set_flags(flags() | ui::EF_IS_REPEAT); |
590 | 591 |
591 #if defined(USE_X11) | 592 #if defined(USE_X11) |
592 NormalizeFlags(); | 593 NormalizeFlags(); |
593 #endif | 594 #endif |
| 595 #if defined(OS_WIN) |
| 596 // Only Windows has native character events. |
| 597 if (is_char_) |
| 598 character_ = native_event.wParam; |
| 599 #endif |
594 } | 600 } |
595 | 601 |
596 KeyEvent::KeyEvent(EventType type, | 602 KeyEvent::KeyEvent(EventType type, |
597 KeyboardCode key_code, | 603 KeyboardCode key_code, |
598 int flags) | 604 int flags) |
599 : Event(type, EventTimeForNow(), flags), | 605 : Event(type, EventTimeForNow(), flags), |
600 key_code_(key_code), | 606 key_code_(key_code), |
601 is_char_(false), | 607 is_char_(false), |
602 platform_keycode_(0), | 608 platform_keycode_(0), |
603 character_(GetCharacterFromKeyCode(key_code, flags)) { | 609 character_() { |
604 } | 610 } |
605 | 611 |
606 KeyEvent::KeyEvent(EventType type, | 612 KeyEvent::KeyEvent(EventType type, |
607 KeyboardCode key_code, | 613 KeyboardCode key_code, |
608 const std::string& code, | 614 const std::string& code, |
609 int flags) | 615 int flags) |
610 : Event(type, EventTimeForNow(), flags), | 616 : Event(type, EventTimeForNow(), flags), |
611 key_code_(key_code), | 617 key_code_(key_code), |
612 code_(code), | 618 code_(code), |
613 is_char_(false), | 619 is_char_(false), |
614 platform_keycode_(0), | 620 platform_keycode_(0), |
615 character_(GetCharacterFromKeyCode(key_code, flags)) { | 621 character_(0) { |
616 } | 622 } |
617 | 623 |
618 KeyEvent::KeyEvent(base::char16 character, KeyboardCode key_code, int flags) | 624 KeyEvent::KeyEvent(base::char16 character, KeyboardCode key_code, int flags) |
619 : Event(ET_KEY_PRESSED, EventTimeForNow(), flags), | 625 : Event(ET_KEY_PRESSED, EventTimeForNow(), flags), |
620 key_code_(key_code), | 626 key_code_(key_code), |
621 code_(""), | 627 code_(""), |
622 is_char_(true), | 628 is_char_(true), |
623 platform_keycode_(0), | 629 platform_keycode_(0), |
624 character_(character) { | 630 character_(character) { |
625 } | 631 } |
(...skipping 24 matching lines...) Expand all Loading... |
650 return *this; | 656 return *this; |
651 } | 657 } |
652 | 658 |
653 KeyEvent::~KeyEvent() {} | 659 KeyEvent::~KeyEvent() {} |
654 | 660 |
655 void KeyEvent::SetExtendedKeyEventData(scoped_ptr<ExtendedKeyEventData> data) { | 661 void KeyEvent::SetExtendedKeyEventData(scoped_ptr<ExtendedKeyEventData> data) { |
656 extended_key_event_data_ = data.Pass(); | 662 extended_key_event_data_ = data.Pass(); |
657 } | 663 } |
658 | 664 |
659 base::char16 KeyEvent::GetCharacter() const { | 665 base::char16 KeyEvent::GetCharacter() const { |
660 if (character_) | 666 if (is_char_ || character_) |
661 return character_; | 667 return character_; |
662 | 668 |
| 669 // TODO(kpschoedel): streamline these cases after settling Ozone |
| 670 // positional coding. |
663 #if defined(OS_WIN) | 671 #if defined(OS_WIN) |
664 return (native_event().message == WM_CHAR) ? key_code_ : | 672 // Native Windows character events always have is_char_ == true, |
665 GetCharacterFromKeyCode(key_code_, flags()); | 673 // so this is a synthetic or native keystroke event. |
| 674 character_ = GetCharacterFromKeyCode(key_code_, flags()); |
| 675 return character_; |
666 #elif defined(USE_X11) | 676 #elif defined(USE_X11) |
667 if (!native_event()) | 677 if (!native_event()) { |
668 return GetCharacterFromKeyCode(key_code_, flags()); | 678 character_ = GetCharacterFromKeyCode(key_code_, flags()); |
| 679 return character_; |
| 680 } |
669 | 681 |
670 DCHECK(native_event()->type == KeyPress || | 682 DCHECK(native_event()->type == KeyPress || |
671 native_event()->type == KeyRelease || | 683 native_event()->type == KeyRelease || |
672 (native_event()->type == GenericEvent && | 684 (native_event()->type == GenericEvent && |
673 (native_event()->xgeneric.evtype == XI_KeyPress || | 685 (native_event()->xgeneric.evtype == XI_KeyPress || |
674 native_event()->xgeneric.evtype == XI_KeyRelease))); | 686 native_event()->xgeneric.evtype == XI_KeyRelease))); |
675 | 687 |
676 // When a control key is held, prefer ASCII characters to non ASCII | 688 // When a control key is held, prefer ASCII characters to non ASCII |
677 // characters in order to use it for shortcut keys. GetCharacterFromKeyCode | 689 // characters in order to use it for shortcut keys. GetCharacterFromKeyCode |
678 // returns 'a' for VKEY_A even if the key is actually bound to 'à' in X11. | 690 // returns 'a' for VKEY_A even if the key is actually bound to 'à' in X11. |
679 // GetCharacterFromXEvent returns 'à' in that case. | 691 // GetCharacterFromXEvent returns 'à' in that case. |
680 return IsControlDown() ? | 692 return IsControlDown() ? |
681 GetCharacterFromKeyCode(key_code_, flags()) : | 693 GetCharacterFromKeyCode(key_code_, flags()) : |
682 GetCharacterFromXEvent(native_event()); | 694 GetCharacterFromXEvent(native_event()); |
683 #else | 695 #else |
684 if (native_event()) { | 696 if (native_event()) { |
685 DCHECK(EventTypeFromNative(native_event()) == ET_KEY_PRESSED || | 697 DCHECK(EventTypeFromNative(native_event()) == ET_KEY_PRESSED || |
686 EventTypeFromNative(native_event()) == ET_KEY_RELEASED); | 698 EventTypeFromNative(native_event()) == ET_KEY_RELEASED); |
687 } | 699 } |
688 | 700 |
689 return GetCharacterFromKeyCode(key_code_, flags()); | 701 return GetCharacterFromKeyCode(key_code_, flags()); |
690 #endif | 702 #endif |
691 } | 703 } |
692 | 704 |
| 705 base::char16 KeyEvent::GetText() const { |
| 706 if ((flags() & EF_CONTROL_DOWN) != 0) { |
| 707 return GetControlCharacterForKeycode(key_code_, |
| 708 (flags() & EF_SHIFT_DOWN) != 0); |
| 709 } |
| 710 return GetUnmodifiedText(); |
| 711 } |
| 712 |
| 713 base::char16 KeyEvent::GetUnmodifiedText() const { |
| 714 if (!is_char_ && (key_code_ == VKEY_RETURN)) |
| 715 return '\r'; |
| 716 return GetCharacter(); |
| 717 } |
| 718 |
693 bool KeyEvent::IsUnicodeKeyCode() const { | 719 bool KeyEvent::IsUnicodeKeyCode() const { |
694 #if defined(OS_WIN) | 720 #if defined(OS_WIN) |
695 if (!IsAltDown()) | 721 if (!IsAltDown()) |
696 return false; | 722 return false; |
697 const int key = key_code(); | 723 const int key = key_code(); |
698 if (key >= VKEY_NUMPAD0 && key <= VKEY_NUMPAD9) | 724 if (key >= VKEY_NUMPAD0 && key <= VKEY_NUMPAD9) |
699 return true; | 725 return true; |
700 // Check whether the user is using the numeric keypad with num-lock off. | 726 // Check whether the user is using the numeric keypad with num-lock off. |
701 // In that case, EF_EXTENDED will not be set; if it is set, the key event | 727 // In that case, EF_EXTENDED will not be set; if it is set, the key event |
702 // originated from the relevant non-numpad dedicated key, e.g. [Insert]. | 728 // originated from the relevant non-numpad dedicated key, e.g. [Insert]. |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
756 break; | 782 break; |
757 case ET_KEY_RELEASED: | 783 case ET_KEY_RELEASED: |
758 case ET_TRANSLATED_KEY_RELEASE: | 784 case ET_TRANSLATED_KEY_RELEASE: |
759 SetType(translated ? ET_TRANSLATED_KEY_RELEASE : ET_KEY_RELEASED); | 785 SetType(translated ? ET_TRANSLATED_KEY_RELEASE : ET_KEY_RELEASED); |
760 break; | 786 break; |
761 default: | 787 default: |
762 NOTREACHED(); | 788 NOTREACHED(); |
763 } | 789 } |
764 } | 790 } |
765 | 791 |
| 792 bool KeyEvent::IsRightSideKey() const { |
| 793 switch (key_code_) { |
| 794 case VKEY_CONTROL: |
| 795 case VKEY_SHIFT: |
| 796 case VKEY_MENU: |
| 797 case VKEY_LWIN: |
| 798 #if defined(USE_X11) |
| 799 // Under X11, setting code_ requires platform-dependent information, and |
| 800 // currently assumes that X keycodes are based on Linux evdev keycodes. |
| 801 // In certain test environments this is not the case, and code_ is not |
| 802 // set accurately, so we need a different mechanism. Fortunately X11 key |
| 803 // mapping preserves the left-right distinction, so testing keysyms works |
| 804 // if the value is available (as it is for all X11 native-based events). |
| 805 if (platform_keycode_) { |
| 806 return (platform_keycode_ == XK_Shift_R) || |
| 807 (platform_keycode_ == XK_Control_R) || |
| 808 (platform_keycode_ == XK_Alt_R) || |
| 809 (platform_keycode_ == XK_Meta_R) || |
| 810 (platform_keycode_ == XK_Super_R) || |
| 811 (platform_keycode_ == XK_Hyper_R); |
| 812 } |
| 813 // Fall through to the generic code if we have no platform_keycode_. |
| 814 // Under X11, this must be a synthetic event, so we can require that |
| 815 // code_ be set correctly. |
| 816 #endif |
| 817 return ((code_.size() > 5) && |
| 818 (code_.compare(code_.size() - 5, 5, "Right", 5)) == 0); |
| 819 default: |
| 820 return false; |
| 821 } |
| 822 } |
| 823 |
| 824 KeyboardCode KeyEvent::GetLocatedWindowsKeyboardCode() const { |
| 825 switch (key_code_) { |
| 826 case VKEY_SHIFT: |
| 827 return IsRightSideKey() ? VKEY_RSHIFT : VKEY_LSHIFT; |
| 828 case VKEY_CONTROL: |
| 829 return IsRightSideKey() ? VKEY_RCONTROL : VKEY_LCONTROL; |
| 830 case VKEY_MENU: |
| 831 return IsRightSideKey() ? VKEY_RMENU : VKEY_LMENU; |
| 832 case VKEY_LWIN: |
| 833 return IsRightSideKey() ? VKEY_RWIN : VKEY_LWIN; |
| 834 // TODO(kpschoedel): EF_NUMPAD_KEY is present only on X11. Currently this |
| 835 // function is only called on X11. Likely the tests here will be replaced |
| 836 // with a DOM-based code enumeration test in the course of Ozone |
| 837 // platform-indpendent key event work. |
| 838 case VKEY_0: |
| 839 return (flags() & EF_NUMPAD_KEY) ? VKEY_NUMPAD0 : VKEY_0; |
| 840 case VKEY_1: |
| 841 return (flags() & EF_NUMPAD_KEY) ? VKEY_NUMPAD1 : VKEY_1; |
| 842 case VKEY_2: |
| 843 return (flags() & EF_NUMPAD_KEY) ? VKEY_NUMPAD2 : VKEY_2; |
| 844 case VKEY_3: |
| 845 return (flags() & EF_NUMPAD_KEY) ? VKEY_NUMPAD3 : VKEY_3; |
| 846 case VKEY_4: |
| 847 return (flags() & EF_NUMPAD_KEY) ? VKEY_NUMPAD4 : VKEY_4; |
| 848 case VKEY_5: |
| 849 return (flags() & EF_NUMPAD_KEY) ? VKEY_NUMPAD5 : VKEY_5; |
| 850 case VKEY_6: |
| 851 return (flags() & EF_NUMPAD_KEY) ? VKEY_NUMPAD6 : VKEY_6; |
| 852 case VKEY_7: |
| 853 return (flags() & EF_NUMPAD_KEY) ? VKEY_NUMPAD7 : VKEY_7; |
| 854 case VKEY_8: |
| 855 return (flags() & EF_NUMPAD_KEY) ? VKEY_NUMPAD8 : VKEY_8; |
| 856 case VKEY_9: |
| 857 return (flags() & EF_NUMPAD_KEY) ? VKEY_NUMPAD9 : VKEY_9; |
| 858 default: |
| 859 return key_code_; |
| 860 } |
| 861 } |
| 862 |
| 863 uint16 KeyEvent::GetConflatedWindowsKeyCode() const { |
| 864 if (is_char_) |
| 865 return character_; |
| 866 return key_code_; |
| 867 } |
| 868 |
766 //////////////////////////////////////////////////////////////////////////////// | 869 //////////////////////////////////////////////////////////////////////////////// |
767 // ScrollEvent | 870 // ScrollEvent |
768 | 871 |
769 ScrollEvent::ScrollEvent(const base::NativeEvent& native_event) | 872 ScrollEvent::ScrollEvent(const base::NativeEvent& native_event) |
770 : MouseEvent(native_event) { | 873 : MouseEvent(native_event) { |
771 if (type() == ET_SCROLL) { | 874 if (type() == ET_SCROLL) { |
772 GetScrollOffsets(native_event, | 875 GetScrollOffsets(native_event, |
773 &x_offset_, &y_offset_, | 876 &x_offset_, &y_offset_, |
774 &x_offset_ordinal_, &y_offset_ordinal_, | 877 &x_offset_ordinal_, &y_offset_ordinal_, |
775 &finger_count_); | 878 &finger_count_); |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
824 gfx::PointF(x, y), | 927 gfx::PointF(x, y), |
825 time_stamp, | 928 time_stamp, |
826 flags | EF_FROM_TOUCH), | 929 flags | EF_FROM_TOUCH), |
827 details_(details) { | 930 details_(details) { |
828 } | 931 } |
829 | 932 |
830 GestureEvent::~GestureEvent() { | 933 GestureEvent::~GestureEvent() { |
831 } | 934 } |
832 | 935 |
833 } // namespace ui | 936 } // namespace ui |
OLD | NEW |