Chromium Code Reviews| 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 character_(character) { | 629 character_(character) { |
| 624 } | 630 } |
| 625 | 631 |
| 626 base::char16 KeyEvent::GetCharacter() const { | 632 base::char16 KeyEvent::GetCharacter() const { |
| 627 if (character_) | 633 if (is_char_ || character_) |
| 628 return character_; | 634 return character_; |
| 629 | 635 |
| 636 // TODO(kpschoedel): streamline these cases after settling Ozone | |
| 637 // positional coding. | |
| 630 #if defined(OS_WIN) | 638 #if defined(OS_WIN) |
| 631 return (native_event().message == WM_CHAR) ? key_code_ : | 639 // Native Windows character events always have is_char_ == true, |
| 632 GetCharacterFromKeyCode(key_code_, flags()); | 640 // so this is a synthetic or native keystroke event. |
| 641 character_ = GetCharacterFromKeyCode(key_code_, flags()); | |
| 642 return character_; | |
| 633 #elif defined(USE_X11) | 643 #elif defined(USE_X11) |
| 634 if (!native_event()) | 644 if (!native_event()) { |
| 635 return GetCharacterFromKeyCode(key_code_, flags()); | 645 character_ = GetCharacterFromKeyCode(key_code_, flags()); |
| 646 return character_; | |
| 647 } | |
| 636 | 648 |
| 637 DCHECK(native_event()->type == KeyPress || | 649 DCHECK(native_event()->type == KeyPress || |
| 638 native_event()->type == KeyRelease || | 650 native_event()->type == KeyRelease || |
| 639 (native_event()->type == GenericEvent && | 651 (native_event()->type == GenericEvent && |
| 640 (native_event()->xgeneric.evtype == XI_KeyPress || | 652 (native_event()->xgeneric.evtype == XI_KeyPress || |
| 641 native_event()->xgeneric.evtype == XI_KeyRelease))); | 653 native_event()->xgeneric.evtype == XI_KeyRelease))); |
| 642 | 654 |
| 643 // When a control key is held, prefer ASCII characters to non ASCII | 655 // When a control key is held, prefer ASCII characters to non ASCII |
| 644 // characters in order to use it for shortcut keys. GetCharacterFromKeyCode | 656 // characters in order to use it for shortcut keys. GetCharacterFromKeyCode |
| 645 // returns 'a' for VKEY_A even if the key is actually bound to 'à' in X11. | 657 // returns 'a' for VKEY_A even if the key is actually bound to 'à' in X11. |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 723 break; | 735 break; |
| 724 case ET_KEY_RELEASED: | 736 case ET_KEY_RELEASED: |
| 725 case ET_TRANSLATED_KEY_RELEASE: | 737 case ET_TRANSLATED_KEY_RELEASE: |
| 726 SetType(translated ? ET_TRANSLATED_KEY_RELEASE : ET_KEY_RELEASED); | 738 SetType(translated ? ET_TRANSLATED_KEY_RELEASE : ET_KEY_RELEASED); |
| 727 break; | 739 break; |
| 728 default: | 740 default: |
| 729 NOTREACHED(); | 741 NOTREACHED(); |
| 730 } | 742 } |
| 731 } | 743 } |
| 732 | 744 |
| 745 bool KeyEvent::IsRightSideKey() const { | |
| 746 switch (key_code_) { | |
| 747 case VKEY_CONTROL: | |
| 748 case VKEY_SHIFT: | |
| 749 case VKEY_MENU: | |
| 750 case VKEY_LWIN: | |
| 751 #if defined(USE_X11) | |
| 752 // Under X11, setting code_ requires platform-dependent information, and | |
| 753 // currently assumes that X keycodes are based on Linux evdev keycodes. | |
| 754 // In certain test environments this is not the case, and code_ is not | |
| 755 // set accurately, so we need a different mechanism. Fortunately X11 key | |
| 756 // mapping preserves the left-right distinction, so testing keysyms works | |
| 757 // if the value is available (as it is for all X11 native-based events). | |
| 758 if (platform_keycode_) { | |
| 759 return (platform_keycode_ == XK_Shift_R) || | |
| 760 (platform_keycode_ == XK_Control_R) || | |
| 761 (platform_keycode_ == XK_Alt_R) || | |
| 762 (platform_keycode_ == XK_Meta_R) || | |
| 763 (platform_keycode_ == XK_Super_R) || | |
| 764 (platform_keycode_ == XK_Hyper_R); | |
| 765 } | |
| 766 // Fall through to the generic code if we have no platform_keycode_. | |
| 767 // Under X11, this must be a synthetic event, so we can require that | |
| 768 // code_ be set correctly. | |
| 769 #endif | |
| 770 return ((code_.size() > 5) && | |
| 771 (code_.compare(code_.size() - 5, 5, "Right", 5)) == 0); | |
| 772 default: | |
| 773 return false; | |
| 774 } | |
| 775 } | |
| 776 | |
| 777 KeyboardCode KeyEvent::GetLocatedWindowsKeyboardCode() const { | |
| 778 switch (key_code_) { | |
| 779 case VKEY_SHIFT: | |
| 780 return IsRightSideKey() ? VKEY_RSHIFT : VKEY_LSHIFT; | |
| 781 case VKEY_CONTROL: | |
| 782 return IsRightSideKey() ? VKEY_RCONTROL : VKEY_LCONTROL; | |
| 783 case VKEY_MENU: | |
| 784 return IsRightSideKey() ? VKEY_RMENU : VKEY_LMENU; | |
| 785 case VKEY_LWIN: | |
| 786 return IsRightSideKey() ? VKEY_RWIN : VKEY_LWIN; | |
| 787 case VKEY_0: | |
| 788 return (flags() & EF_NUMPAD_KEY) ? VKEY_NUMPAD0 : VKEY_0; | |
|
sadrul
2014/08/19 03:46:19
EF_NUMPAD_KEY is set only on X11. Does that make a
kpschoedel
2014/08/19 21:58:10
It's only used for X11 / Ozone at present, and it'
| |
| 789 case VKEY_1: | |
| 790 return (flags() & EF_NUMPAD_KEY) ? VKEY_NUMPAD1 : VKEY_1; | |
| 791 case VKEY_2: | |
| 792 return (flags() & EF_NUMPAD_KEY) ? VKEY_NUMPAD2 : VKEY_2; | |
| 793 case VKEY_3: | |
| 794 return (flags() & EF_NUMPAD_KEY) ? VKEY_NUMPAD3 : VKEY_3; | |
| 795 case VKEY_4: | |
| 796 return (flags() & EF_NUMPAD_KEY) ? VKEY_NUMPAD4 : VKEY_4; | |
| 797 case VKEY_5: | |
| 798 return (flags() & EF_NUMPAD_KEY) ? VKEY_NUMPAD5 : VKEY_5; | |
| 799 case VKEY_6: | |
| 800 return (flags() & EF_NUMPAD_KEY) ? VKEY_NUMPAD6 : VKEY_6; | |
| 801 case VKEY_7: | |
| 802 return (flags() & EF_NUMPAD_KEY) ? VKEY_NUMPAD7 : VKEY_7; | |
| 803 case VKEY_8: | |
| 804 return (flags() & EF_NUMPAD_KEY) ? VKEY_NUMPAD8 : VKEY_8; | |
| 805 case VKEY_9: | |
| 806 return (flags() & EF_NUMPAD_KEY) ? VKEY_NUMPAD9 : VKEY_9; | |
| 807 // Locatable codes not currently distinguished: | |
|
sadrul
2014/08/19 03:46:19
Should be a TODO (maybe with an accompanying crbug
kpschoedel
2014/08/19 21:58:10
I'll just delete this. There will probably never b
| |
| 808 // case VKEY_RETURN: | |
| 809 // return (flags() & EF_NUMPAD_KEY) ? VKEY_RETURN : ** numpad enter **; | |
| 810 // case VKEY_OEM_PLUS: | |
| 811 // return (flags() & EF_NUMPAD_KEY) ? VKEY_OEM_PLUS : ** numpad equal **; | |
| 812 default: | |
| 813 return key_code_; | |
| 814 } | |
| 815 } | |
| 816 | |
| 733 //////////////////////////////////////////////////////////////////////////////// | 817 //////////////////////////////////////////////////////////////////////////////// |
| 734 // ScrollEvent | 818 // ScrollEvent |
| 735 | 819 |
| 736 ScrollEvent::ScrollEvent(const base::NativeEvent& native_event) | 820 ScrollEvent::ScrollEvent(const base::NativeEvent& native_event) |
| 737 : MouseEvent(native_event) { | 821 : MouseEvent(native_event) { |
| 738 if (type() == ET_SCROLL) { | 822 if (type() == ET_SCROLL) { |
| 739 GetScrollOffsets(native_event, | 823 GetScrollOffsets(native_event, |
| 740 &x_offset_, &y_offset_, | 824 &x_offset_, &y_offset_, |
| 741 &x_offset_ordinal_, &y_offset_ordinal_, | 825 &x_offset_ordinal_, &y_offset_ordinal_, |
| 742 &finger_count_); | 826 &finger_count_); |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 791 gfx::PointF(x, y), | 875 gfx::PointF(x, y), |
| 792 time_stamp, | 876 time_stamp, |
| 793 flags | EF_FROM_TOUCH), | 877 flags | EF_FROM_TOUCH), |
| 794 details_(details) { | 878 details_(details) { |
| 795 } | 879 } |
| 796 | 880 |
| 797 GestureEvent::~GestureEvent() { | 881 GestureEvent::~GestureEvent() { |
| 798 } | 882 } |
| 799 | 883 |
| 800 } // namespace ui | 884 } // namespace ui |
| OLD | NEW |