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> | |
11 #endif | 10 #endif |
12 | 11 |
13 #include <cmath> | 12 #include <cmath> |
14 #include <cstring> | 13 #include <cstring> |
15 | 14 |
16 #include "base/metrics/histogram.h" | 15 #include "base/metrics/histogram.h" |
17 #include "base/strings/stringprintf.h" | 16 #include "base/strings/stringprintf.h" |
18 #include "ui/events/event_utils.h" | 17 #include "ui/events/event_utils.h" |
19 #include "ui/events/keycodes/keyboard_code_conversion.h" | 18 #include "ui/events/keycodes/keyboard_code_conversion.h" |
20 #include "ui/gfx/point3_f.h" | 19 #include "ui/gfx/point3_f.h" |
(...skipping 564 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
585 code_(CodeFromNative(native_event)), | 584 code_(CodeFromNative(native_event)), |
586 is_char_(IsCharFromNative(native_event)), | 585 is_char_(IsCharFromNative(native_event)), |
587 platform_keycode_(PlatformKeycodeFromNative(native_event)), | 586 platform_keycode_(PlatformKeycodeFromNative(native_event)), |
588 character_(0) { | 587 character_(0) { |
589 if (IsRepeated(*this)) | 588 if (IsRepeated(*this)) |
590 set_flags(flags() | ui::EF_IS_REPEAT); | 589 set_flags(flags() | ui::EF_IS_REPEAT); |
591 | 590 |
592 #if defined(USE_X11) | 591 #if defined(USE_X11) |
593 NormalizeFlags(); | 592 NormalizeFlags(); |
594 #endif | 593 #endif |
595 #if defined(OS_WIN) | |
596 // Only Windows has native character events. | |
597 if (is_char_) | |
598 character_ = native_event.wParam; | |
599 #endif | |
600 } | 594 } |
601 | 595 |
602 KeyEvent::KeyEvent(EventType type, | 596 KeyEvent::KeyEvent(EventType type, |
603 KeyboardCode key_code, | 597 KeyboardCode key_code, |
604 int flags) | 598 int flags) |
605 : Event(type, EventTimeForNow(), flags), | 599 : Event(type, EventTimeForNow(), flags), |
606 key_code_(key_code), | 600 key_code_(key_code), |
607 is_char_(false), | 601 is_char_(false), |
608 platform_keycode_(0), | 602 platform_keycode_(0), |
609 character_() { | 603 character_(GetCharacterFromKeyCode(key_code, flags)) { |
610 } | 604 } |
611 | 605 |
612 KeyEvent::KeyEvent(EventType type, | 606 KeyEvent::KeyEvent(EventType type, |
613 KeyboardCode key_code, | 607 KeyboardCode key_code, |
614 const std::string& code, | 608 const std::string& code, |
615 int flags) | 609 int flags) |
616 : Event(type, EventTimeForNow(), flags), | 610 : Event(type, EventTimeForNow(), flags), |
617 key_code_(key_code), | 611 key_code_(key_code), |
618 code_(code), | 612 code_(code), |
619 is_char_(false), | 613 is_char_(false), |
620 platform_keycode_(0), | 614 platform_keycode_(0), |
621 character_(0) { | 615 character_(GetCharacterFromKeyCode(key_code, flags)) { |
622 } | 616 } |
623 | 617 |
624 KeyEvent::KeyEvent(base::char16 character, KeyboardCode key_code, int flags) | 618 KeyEvent::KeyEvent(base::char16 character, KeyboardCode key_code, int flags) |
625 : Event(ET_KEY_PRESSED, EventTimeForNow(), flags), | 619 : Event(ET_KEY_PRESSED, EventTimeForNow(), flags), |
626 key_code_(key_code), | 620 key_code_(key_code), |
627 code_(""), | 621 code_(""), |
628 is_char_(true), | 622 is_char_(true), |
629 platform_keycode_(0), | 623 platform_keycode_(0), |
630 character_(character) { | 624 character_(character) { |
631 } | 625 } |
(...skipping 24 matching lines...) Expand all Loading... |
656 return *this; | 650 return *this; |
657 } | 651 } |
658 | 652 |
659 KeyEvent::~KeyEvent() {} | 653 KeyEvent::~KeyEvent() {} |
660 | 654 |
661 void KeyEvent::SetExtendedKeyEventData(scoped_ptr<ExtendedKeyEventData> data) { | 655 void KeyEvent::SetExtendedKeyEventData(scoped_ptr<ExtendedKeyEventData> data) { |
662 extended_key_event_data_ = data.Pass(); | 656 extended_key_event_data_ = data.Pass(); |
663 } | 657 } |
664 | 658 |
665 base::char16 KeyEvent::GetCharacter() const { | 659 base::char16 KeyEvent::GetCharacter() const { |
666 if (is_char_ || character_) | 660 if (character_) |
667 return character_; | 661 return character_; |
668 | 662 |
669 // TODO(kpschoedel): streamline these cases after settling Ozone | |
670 // positional coding. | |
671 #if defined(OS_WIN) | 663 #if defined(OS_WIN) |
672 // Native Windows character events always have is_char_ == true, | 664 return (native_event().message == WM_CHAR) ? key_code_ : |
673 // so this is a synthetic or native keystroke event. | 665 GetCharacterFromKeyCode(key_code_, flags()); |
674 character_ = GetCharacterFromKeyCode(key_code_, flags()); | |
675 return character_; | |
676 #elif defined(USE_X11) | 666 #elif defined(USE_X11) |
677 if (!native_event()) { | 667 if (!native_event()) |
678 character_ = GetCharacterFromKeyCode(key_code_, flags()); | 668 return GetCharacterFromKeyCode(key_code_, flags()); |
679 return character_; | |
680 } | |
681 | 669 |
682 DCHECK(native_event()->type == KeyPress || | 670 DCHECK(native_event()->type == KeyPress || |
683 native_event()->type == KeyRelease || | 671 native_event()->type == KeyRelease || |
684 (native_event()->type == GenericEvent && | 672 (native_event()->type == GenericEvent && |
685 (native_event()->xgeneric.evtype == XI_KeyPress || | 673 (native_event()->xgeneric.evtype == XI_KeyPress || |
686 native_event()->xgeneric.evtype == XI_KeyRelease))); | 674 native_event()->xgeneric.evtype == XI_KeyRelease))); |
687 | 675 |
688 // When a control key is held, prefer ASCII characters to non ASCII | 676 // When a control key is held, prefer ASCII characters to non ASCII |
689 // characters in order to use it for shortcut keys. GetCharacterFromKeyCode | 677 // characters in order to use it for shortcut keys. GetCharacterFromKeyCode |
690 // returns 'a' for VKEY_A even if the key is actually bound to 'à' in X11. | 678 // returns 'a' for VKEY_A even if the key is actually bound to 'à' in X11. |
691 // GetCharacterFromXEvent returns 'à' in that case. | 679 // GetCharacterFromXEvent returns 'à' in that case. |
692 return IsControlDown() ? | 680 return IsControlDown() ? |
693 GetCharacterFromKeyCode(key_code_, flags()) : | 681 GetCharacterFromKeyCode(key_code_, flags()) : |
694 GetCharacterFromXEvent(native_event()); | 682 GetCharacterFromXEvent(native_event()); |
695 #else | 683 #else |
696 if (native_event()) { | 684 if (native_event()) { |
697 DCHECK(EventTypeFromNative(native_event()) == ET_KEY_PRESSED || | 685 DCHECK(EventTypeFromNative(native_event()) == ET_KEY_PRESSED || |
698 EventTypeFromNative(native_event()) == ET_KEY_RELEASED); | 686 EventTypeFromNative(native_event()) == ET_KEY_RELEASED); |
699 } | 687 } |
700 | 688 |
701 return GetCharacterFromKeyCode(key_code_, flags()); | 689 return GetCharacterFromKeyCode(key_code_, flags()); |
702 #endif | 690 #endif |
703 } | 691 } |
704 | 692 |
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 | |
719 bool KeyEvent::IsUnicodeKeyCode() const { | 693 bool KeyEvent::IsUnicodeKeyCode() const { |
720 #if defined(OS_WIN) | 694 #if defined(OS_WIN) |
721 if (!IsAltDown()) | 695 if (!IsAltDown()) |
722 return false; | 696 return false; |
723 const int key = key_code(); | 697 const int key = key_code(); |
724 if (key >= VKEY_NUMPAD0 && key <= VKEY_NUMPAD9) | 698 if (key >= VKEY_NUMPAD0 && key <= VKEY_NUMPAD9) |
725 return true; | 699 return true; |
726 // Check whether the user is using the numeric keypad with num-lock off. | 700 // Check whether the user is using the numeric keypad with num-lock off. |
727 // In that case, EF_EXTENDED will not be set; if it is set, the key event | 701 // In that case, EF_EXTENDED will not be set; if it is set, the key event |
728 // originated from the relevant non-numpad dedicated key, e.g. [Insert]. | 702 // originated from the relevant non-numpad dedicated key, e.g. [Insert]. |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
782 break; | 756 break; |
783 case ET_KEY_RELEASED: | 757 case ET_KEY_RELEASED: |
784 case ET_TRANSLATED_KEY_RELEASE: | 758 case ET_TRANSLATED_KEY_RELEASE: |
785 SetType(translated ? ET_TRANSLATED_KEY_RELEASE : ET_KEY_RELEASED); | 759 SetType(translated ? ET_TRANSLATED_KEY_RELEASE : ET_KEY_RELEASED); |
786 break; | 760 break; |
787 default: | 761 default: |
788 NOTREACHED(); | 762 NOTREACHED(); |
789 } | 763 } |
790 } | 764 } |
791 | 765 |
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 | |
869 //////////////////////////////////////////////////////////////////////////////// | 766 //////////////////////////////////////////////////////////////////////////////// |
870 // ScrollEvent | 767 // ScrollEvent |
871 | 768 |
872 ScrollEvent::ScrollEvent(const base::NativeEvent& native_event) | 769 ScrollEvent::ScrollEvent(const base::NativeEvent& native_event) |
873 : MouseEvent(native_event) { | 770 : MouseEvent(native_event) { |
874 if (type() == ET_SCROLL) { | 771 if (type() == ET_SCROLL) { |
875 GetScrollOffsets(native_event, | 772 GetScrollOffsets(native_event, |
876 &x_offset_, &y_offset_, | 773 &x_offset_, &y_offset_, |
877 &x_offset_ordinal_, &y_offset_ordinal_, | 774 &x_offset_ordinal_, &y_offset_ordinal_, |
878 &finger_count_); | 775 &finger_count_); |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
927 gfx::PointF(x, y), | 824 gfx::PointF(x, y), |
928 time_stamp, | 825 time_stamp, |
929 flags | EF_FROM_TOUCH), | 826 flags | EF_FROM_TOUCH), |
930 details_(details) { | 827 details_(details) { |
931 } | 828 } |
932 | 829 |
933 GestureEvent::~GestureEvent() { | 830 GestureEvent::~GestureEvent() { |
934 } | 831 } |
935 | 832 |
936 } // namespace ui | 833 } // namespace ui |
OLD | NEW |