Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / ui / events / event_utils.cc
blobc775e10957a447c73b8668a9b67007686102a004
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
3 // found in the LICENSE file.
5 #include "ui/events/event_utils.h"
7 #include <vector>
9 #include "ui/events/event.h"
10 #include "ui/gfx/display.h"
11 #include "ui/gfx/screen.h"
13 namespace ui {
15 namespace {
16 int g_custom_event_types = ET_LAST;
17 } // namespace
19 scoped_ptr<Event> EventFromNative(const base::NativeEvent& native_event) {
20 scoped_ptr<Event> event;
21 EventType type = EventTypeFromNative(native_event);
22 switch(type) {
23 case ET_KEY_PRESSED:
24 case ET_KEY_RELEASED:
25 event.reset(new KeyEvent(native_event));
26 break;
28 case ET_TRANSLATED_KEY_PRESS:
29 case ET_TRANSLATED_KEY_RELEASE:
30 // These should not be generated by native events.
31 NOTREACHED();
32 break;
34 case ET_MOUSE_PRESSED:
35 case ET_MOUSE_DRAGGED:
36 case ET_MOUSE_RELEASED:
37 case ET_MOUSE_MOVED:
38 case ET_MOUSE_ENTERED:
39 case ET_MOUSE_EXITED:
40 event.reset(new MouseEvent(native_event));
41 break;
43 case ET_MOUSEWHEEL:
44 event.reset(new MouseWheelEvent(native_event));
45 break;
47 case ET_SCROLL_FLING_START:
48 case ET_SCROLL_FLING_CANCEL:
49 case ET_SCROLL:
50 event.reset(new ScrollEvent(native_event));
51 break;
53 case ET_TOUCH_RELEASED:
54 case ET_TOUCH_PRESSED:
55 case ET_TOUCH_MOVED:
56 case ET_TOUCH_CANCELLED:
57 event.reset(new TouchEvent(native_event));
58 break;
60 default:
61 break;
63 return event.Pass();
66 #if defined(OS_LINUX)
67 // From third_party/WebKit/Source/web/gtk/WebInputEventFactory.cpp:
68 uint16 GetControlCharacterForKeycode(int windows_key_code, bool shift) {
69 if (windows_key_code >= ui::VKEY_A &&
70 windows_key_code <= ui::VKEY_Z) {
71 // ctrl-A ~ ctrl-Z map to \x01 ~ \x1A
72 return windows_key_code - ui::VKEY_A + 1;
74 if (shift) {
75 // following graphics chars require shift key to input.
76 switch (windows_key_code) {
77 // ctrl-@ maps to \x00 (Null byte)
78 case ui::VKEY_2:
79 return 0;
80 // ctrl-^ maps to \x1E (Record separator, Information separator two)
81 case ui::VKEY_6:
82 return 0x1E;
83 // ctrl-_ maps to \x1F (Unit separator, Information separator one)
84 case ui::VKEY_OEM_MINUS:
85 return 0x1F;
86 // Returns 0 for all other keys to avoid inputting unexpected chars.
87 default:
88 break;
90 } else {
91 switch (windows_key_code) {
92 // ctrl-[ maps to \x1B (Escape)
93 case ui::VKEY_OEM_4:
94 return 0x1B;
95 // ctrl-\ maps to \x1C (File separator, Information separator four)
96 case ui::VKEY_OEM_5:
97 return 0x1C;
98 // ctrl-] maps to \x1D (Group separator, Information separator three)
99 case ui::VKEY_OEM_6:
100 return 0x1D;
101 // ctrl-Enter maps to \x0A (Line feed)
102 case ui::VKEY_RETURN:
103 return 0x0A;
104 // Returns 0 for all other keys to avoid inputting unexpected chars.
105 default:
106 break;
109 return 0;
111 #endif
113 int RegisterCustomEventType() {
114 return ++g_custom_event_types;
117 base::TimeDelta EventTimeForNow() {
118 return base::TimeDelta::FromInternalValue(
119 base::TimeTicks::Now().ToInternalValue());
122 bool ShouldDefaultToNaturalScroll() {
123 return GetInternalDisplayTouchSupport() ==
124 gfx::Display::TOUCH_SUPPORT_AVAILABLE;
127 gfx::Display::TouchSupport GetInternalDisplayTouchSupport() {
128 gfx::Screen* screen = gfx::Screen::GetScreenByType(gfx::SCREEN_TYPE_NATIVE);
129 // No screen in some unit tests.
130 if (!screen)
131 return gfx::Display::TOUCH_SUPPORT_UNKNOWN;
132 const std::vector<gfx::Display>& displays = screen->GetAllDisplays();
133 for (std::vector<gfx::Display>::const_iterator it = displays.begin();
134 it != displays.end(); ++it) {
135 if (it->IsInternal())
136 return it->touch_support();
138 return gfx::Display::TOUCH_SUPPORT_UNAVAILABLE;
141 } // namespace ui