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"
9 #include "ui/events/event.h"
10 #include "ui/gfx/display.h"
11 #include "ui/gfx/screen.h"
16 int g_custom_event_types = ET_LAST;
19 scoped_ptr<Event> EventFromNative(const base::NativeEvent& native_event) {
20 scoped_ptr<Event> event;
21 EventType type = EventTypeFromNative(native_event);
25 event.reset(new KeyEvent(native_event));
28 case ET_TRANSLATED_KEY_PRESS:
29 case ET_TRANSLATED_KEY_RELEASE:
30 // These should not be generated by native events.
34 case ET_MOUSE_PRESSED:
35 case ET_MOUSE_DRAGGED:
36 case ET_MOUSE_RELEASED:
38 case ET_MOUSE_ENTERED:
40 event.reset(new MouseEvent(native_event));
44 event.reset(new MouseWheelEvent(native_event));
47 case ET_SCROLL_FLING_START:
48 case ET_SCROLL_FLING_CANCEL:
50 event.reset(new ScrollEvent(native_event));
53 case ET_TOUCH_RELEASED:
54 case ET_TOUCH_PRESSED:
56 case ET_TOUCH_CANCELLED:
57 event.reset(new TouchEvent(native_event));
66 // From third_party/WebKit/Source/web/gtk/WebInputEventFactory.cpp:
67 base::char16 GetControlCharacterForKeycode(int windows_key_code, bool shift) {
68 if (windows_key_code >= ui::VKEY_A &&
69 windows_key_code <= ui::VKEY_Z) {
70 // ctrl-A ~ ctrl-Z map to \x01 ~ \x1A
71 return windows_key_code - ui::VKEY_A + 1;
74 // following graphics chars require shift key to input.
75 switch (windows_key_code) {
76 // ctrl-@ maps to \x00 (Null byte)
79 // ctrl-^ maps to \x1E (Record separator, Information separator two)
82 // ctrl-_ maps to \x1F (Unit separator, Information separator one)
83 case ui::VKEY_OEM_MINUS:
85 // Returns 0 for all other keys to avoid inputting unexpected chars.
90 switch (windows_key_code) {
91 // ctrl-[ maps to \x1B (Escape)
94 // ctrl-\ maps to \x1C (File separator, Information separator four)
97 // ctrl-] maps to \x1D (Group separator, Information separator three)
100 // ctrl-Enter maps to \x0A (Line feed)
101 case ui::VKEY_RETURN:
103 // Returns 0 for all other keys to avoid inputting unexpected chars.
111 int RegisterCustomEventType() {
112 return ++g_custom_event_types;
115 base::TimeDelta EventTimeForNow() {
116 return base::TimeDelta::FromInternalValue(
117 base::TimeTicks::Now().ToInternalValue());
120 bool ShouldDefaultToNaturalScroll() {
121 return GetInternalDisplayTouchSupport() ==
122 gfx::Display::TOUCH_SUPPORT_AVAILABLE;
125 gfx::Display::TouchSupport GetInternalDisplayTouchSupport() {
126 gfx::Screen* screen = gfx::Screen::GetScreenByType(gfx::SCREEN_TYPE_NATIVE);
127 // No screen in some unit tests.
129 return gfx::Display::TOUCH_SUPPORT_UNKNOWN;
130 const std::vector<gfx::Display>& displays = screen->GetAllDisplays();
131 for (std::vector<gfx::Display>::const_iterator it = displays.begin();
132 it != displays.end(); ++it) {
133 if (it->IsInternal())
134 return it->touch_support();
136 return gfx::Display::TOUCH_SUPPORT_UNAVAILABLE;