blob: 2e3517e3e74249615689fc276b2c2a2e5bf23dc4 [file] [log] [blame]
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ui/events/event_utils.h"
#include <vector>
#include "ui/events/event.h"
#include "ui/gfx/display.h"
#include "ui/gfx/screen.h"
namespace ui {
namespace {
int g_custom_event_types = ET_LAST;
} // namespace
scoped_ptr<Event> EventFromNative(const base::NativeEvent& native_event) {
scoped_ptr<Event> event;
EventType type = EventTypeFromNative(native_event);
switch(type) {
case ET_KEY_PRESSED:
case ET_KEY_RELEASED:
event.reset(new KeyEvent(native_event));
break;
case ET_TRANSLATED_KEY_PRESS:
case ET_TRANSLATED_KEY_RELEASE:
// These should not be generated by native events.
NOTREACHED();
break;
case ET_MOUSE_PRESSED:
case ET_MOUSE_DRAGGED:
case ET_MOUSE_RELEASED:
case ET_MOUSE_MOVED:
case ET_MOUSE_ENTERED:
case ET_MOUSE_EXITED:
event.reset(new MouseEvent(native_event));
break;
case ET_MOUSEWHEEL:
event.reset(new MouseWheelEvent(native_event));
break;
case ET_SCROLL_FLING_START:
case ET_SCROLL_FLING_CANCEL:
case ET_SCROLL:
event.reset(new ScrollEvent(native_event));
break;
case ET_TOUCH_RELEASED:
case ET_TOUCH_PRESSED:
case ET_TOUCH_MOVED:
case ET_TOUCH_CANCELLED:
event.reset(new TouchEvent(native_event));
break;
default:
break;
}
return event.Pass();
}
// From third_party/WebKit/Source/web/gtk/WebInputEventFactory.cpp:
base::char16 GetControlCharacterForKeycode(int windows_key_code, bool shift) {
if (windows_key_code >= ui::VKEY_A &&
windows_key_code <= ui::VKEY_Z) {
// ctrl-A ~ ctrl-Z map to \x01 ~ \x1A
return windows_key_code - ui::VKEY_A + 1;
}
if (shift) {
// following graphics chars require shift key to input.
switch (windows_key_code) {
// ctrl-@ maps to \x00 (Null byte)
case ui::VKEY_2:
return 0;
// ctrl-^ maps to \x1E (Record separator, Information separator two)
case ui::VKEY_6:
return 0x1E;
// ctrl-_ maps to \x1F (Unit separator, Information separator one)
case ui::VKEY_OEM_MINUS:
return 0x1F;
// Returns 0 for all other keys to avoid inputting unexpected chars.
default:
break;
}
} else {
switch (windows_key_code) {
// ctrl-[ maps to \x1B (Escape)
case ui::VKEY_OEM_4:
return 0x1B;
// ctrl-\ maps to \x1C (File separator, Information separator four)
case ui::VKEY_OEM_5:
return 0x1C;
// ctrl-] maps to \x1D (Group separator, Information separator three)
case ui::VKEY_OEM_6:
return 0x1D;
// ctrl-Enter maps to \x0A (Line feed)
case ui::VKEY_RETURN:
return 0x0A;
// Returns 0 for all other keys to avoid inputting unexpected chars.
default:
break;
}
}
return 0;
}
int RegisterCustomEventType() {
return ++g_custom_event_types;
}
base::TimeDelta EventTimeForNow() {
return base::TimeDelta::FromInternalValue(
base::TimeTicks::Now().ToInternalValue());
}
bool ShouldDefaultToNaturalScroll() {
return GetInternalDisplayTouchSupport() ==
gfx::Display::TOUCH_SUPPORT_AVAILABLE;
}
gfx::Display::TouchSupport GetInternalDisplayTouchSupport() {
gfx::Screen* screen = gfx::Screen::GetScreenByType(gfx::SCREEN_TYPE_NATIVE);
// No screen in some unit tests.
if (!screen)
return gfx::Display::TOUCH_SUPPORT_UNKNOWN;
const std::vector<gfx::Display>& displays = screen->GetAllDisplays();
for (std::vector<gfx::Display>::const_iterator it = displays.begin();
it != displays.end(); ++it) {
if (it->IsInternal())
return it->touch_support();
}
return gfx::Display::TOUCH_SUPPORT_UNAVAILABLE;
}
} // namespace ui