This source file includes following definitions.
- shouldForgetPreviousClick
- resetClickCountState
- isKeyPadKeyval
- gdkEventTimeToWebEventTime
- gdkStateToWebEventModifiers
- gdkEventToWindowsKeyCode
- normalizeEventState
- getControlCharacter
- keyboardEvent
- isSystemKeyEvent
- keyboardEvent
- mouseEvent
- mouseEvent
- mouseEvent
- mouseWheelEvent
#include "config.h"
#include "WebInputEventFactory.h"
#include "platform/KeyCodeConversion.h"
#include "platform/KeyboardCodes.h"
#include "WebInputEvent.h"
#include <gdk/gdk.h>
#include <gdk/gdkkeysyms.h>
#include <gtk/gtk.h>
#include <stdlib.h>
#include "wtf/Assertions.h"
namespace {
static int gNumClicks = 0;
static GdkWindow* gLastClickEventWindow = 0;
static gint gLastClickTime = 0;
static gint gLastClickX = 0;
static gint gLastClickY = 0;
static blink::WebMouseEvent::Button gLastClickButton = blink::WebMouseEvent::ButtonNone;
bool shouldForgetPreviousClick(GdkWindow* window, gint time, gint x, gint y)
{
static GtkSettings* settings = gtk_settings_get_default();
if (window != gLastClickEventWindow)
return true;
gint doubleClickTime = 250;
gint doubleClickDistance = 5;
g_object_get(G_OBJECT(settings),
"gtk-double-click-time", &doubleClickTime,
"gtk-double-click-distance", &doubleClickDistance, NULL);
return (time - gLastClickTime) > doubleClickTime
|| abs(x - gLastClickX) > doubleClickDistance
|| abs(y - gLastClickY) > doubleClickDistance;
}
void resetClickCountState()
{
gNumClicks = 0;
gLastClickEventWindow = 0;
gLastClickTime = 0;
gLastClickX = 0;
gLastClickY = 0;
gLastClickButton = blink::WebMouseEvent::ButtonNone;
}
bool isKeyPadKeyval(guint keyval)
{
return keyval >= GDK_KP_Space && keyval <= GDK_KP_9;
}
}
namespace blink {
static double gdkEventTimeToWebEventTime(guint32 time)
{
return time / 1000.0;
}
static int gdkStateToWebEventModifiers(guint state)
{
int modifiers = 0;
if (state & GDK_SHIFT_MASK)
modifiers |= WebInputEvent::ShiftKey;
if (state & GDK_CONTROL_MASK)
modifiers |= WebInputEvent::ControlKey;
if (state & GDK_MOD1_MASK)
modifiers |= WebInputEvent::AltKey;
if (state & GDK_META_MASK)
modifiers |= WebInputEvent::MetaKey;
if (state & GDK_BUTTON1_MASK)
modifiers |= WebInputEvent::LeftButtonDown;
if (state & GDK_BUTTON2_MASK)
modifiers |= WebInputEvent::MiddleButtonDown;
if (state & GDK_BUTTON3_MASK)
modifiers |= WebInputEvent::RightButtonDown;
if (state & GDK_LOCK_MASK)
modifiers |= WebInputEvent::CapsLockOn;
if (state & GDK_MOD2_MASK)
modifiers |= WebInputEvent::NumLockOn;
return modifiers;
}
static int gdkEventToWindowsKeyCode(const GdkEventKey* event)
{
static const unsigned hardwareCodeToGDKKeyval[] = {
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
GDK_1,
GDK_2,
GDK_3,
GDK_4,
GDK_5,
GDK_6,
GDK_7,
GDK_8,
GDK_9,
GDK_0,
GDK_minus,
GDK_equal,
0,
0,
GDK_q,
GDK_w,
GDK_e,
GDK_r,
GDK_t,
GDK_y,
GDK_u,
GDK_i,
GDK_o,
GDK_p,
GDK_bracketleft,
GDK_bracketright,
0,
0,
GDK_a,
GDK_s,
GDK_d,
GDK_f,
GDK_g,
GDK_h,
GDK_j,
GDK_k,
GDK_l,
GDK_semicolon,
GDK_apostrophe,
GDK_grave,
0,
GDK_backslash,
GDK_z,
GDK_x,
GDK_c,
GDK_v,
GDK_b,
GDK_n,
GDK_m,
GDK_comma,
GDK_period,
GDK_slash,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
GDK_Super_L,
GDK_Super_R,
};
int windowsKeyCode = WebCore::windowsKeyCodeForKeyEvent(event->keyval);
if (windowsKeyCode)
return windowsKeyCode;
const int tableSize = sizeof(hardwareCodeToGDKKeyval) / sizeof(hardwareCodeToGDKKeyval[0]);
if (event->hardware_keycode < tableSize) {
int keyval = hardwareCodeToGDKKeyval[event->hardware_keycode];
if (keyval)
return WebCore::windowsKeyCodeForKeyEvent(keyval);
}
return WebCore::windowsKeyCodeForKeyEvent(event->keyval);
}
static guint normalizeEventState(const GdkEventKey* event)
{
guint mask = 0;
switch (gdkEventToWindowsKeyCode(event)) {
case WebCore::VKEY_CONTROL:
case WebCore::VKEY_LCONTROL:
case WebCore::VKEY_RCONTROL:
mask = GDK_CONTROL_MASK;
break;
case WebCore::VKEY_SHIFT:
case WebCore::VKEY_LSHIFT:
case WebCore::VKEY_RSHIFT:
mask = GDK_SHIFT_MASK;
break;
case WebCore::VKEY_MENU:
case WebCore::VKEY_LMENU:
case WebCore::VKEY_RMENU:
mask = GDK_MOD1_MASK;
break;
case WebCore::VKEY_CAPITAL:
mask = GDK_LOCK_MASK;
break;
default:
return event->state;
}
if (event->type == GDK_KEY_PRESS)
return event->state | mask;
return event->state & ~mask;
}
static WebUChar getControlCharacter(int windowsKeyCode, bool shift)
{
if (windowsKeyCode >= WebCore::VKEY_A && windowsKeyCode <= WebCore::VKEY_Z) {
return windowsKeyCode - WebCore::VKEY_A + 1;
}
if (shift) {
switch (windowsKeyCode) {
case WebCore::VKEY_2:
return 0;
case WebCore::VKEY_6:
return 0x1E;
case WebCore::VKEY_OEM_MINUS:
return 0x1F;
default:
return 0;
}
} else {
switch (windowsKeyCode) {
case WebCore::VKEY_OEM_4:
return 0x1B;
case WebCore::VKEY_OEM_5:
return 0x1C;
case WebCore::VKEY_OEM_6:
return 0x1D;
case WebCore::VKEY_RETURN:
return 0x0A;
default:
return 0;
}
}
}
WebKeyboardEvent WebInputEventFactory::keyboardEvent(const GdkEventKey* event)
{
WebKeyboardEvent result;
result.timeStampSeconds = gdkEventTimeToWebEventTime(event->time);
result.modifiers = gdkStateToWebEventModifiers(normalizeEventState(event));
switch (event->type) {
case GDK_KEY_RELEASE:
result.type = WebInputEvent::KeyUp;
break;
case GDK_KEY_PRESS:
result.type = WebInputEvent::RawKeyDown;
break;
default:
ASSERT_NOT_REACHED();
}
result.isSystemKey = WebInputEventFactory::isSystemKeyEvent(result);
int windowsKeyCode = gdkEventToWindowsKeyCode(event);
result.windowsKeyCode = WebKeyboardEvent::windowsKeyCodeWithoutLocation(windowsKeyCode);
result.modifiers |= WebKeyboardEvent::locationModifiersFromWindowsKeyCode(windowsKeyCode);
result.nativeKeyCode = event->hardware_keycode;
if (result.windowsKeyCode == WebCore::VKEY_RETURN)
result.unmodifiedText[0] = '\r';
else
result.unmodifiedText[0] =
static_cast<WebUChar>(gdk_keyval_to_unicode(event->keyval));
if (result.modifiers & WebInputEvent::ControlKey)
result.text[0] = getControlCharacter(
result.windowsKeyCode, result.modifiers & WebInputEvent::ShiftKey);
else
result.text[0] = result.unmodifiedText[0];
result.setKeyIdentifierFromWindowsKeyCode();
if (isKeyPadKeyval(event->keyval))
result.modifiers |= WebInputEvent::IsKeyPad;
return result;
}
bool WebInputEventFactory::isSystemKeyEvent(const WebKeyboardEvent& event)
{
return event.modifiers & WebInputEvent::AltKey;
}
WebKeyboardEvent WebInputEventFactory::keyboardEvent(wchar_t character, int state, double timeStampSeconds)
{
WebKeyboardEvent result;
result.type = blink::WebInputEvent::Char;
result.timeStampSeconds = timeStampSeconds;
result.modifiers = gdkStateToWebEventModifiers(state);
result.windowsKeyCode = character;
result.nativeKeyCode = character;
result.text[0] = character;
result.unmodifiedText[0] = character;
if (result.modifiers & WebInputEvent::AltKey)
result.isSystemKey = true;
return result;
}
WebMouseEvent WebInputEventFactory::mouseEvent(const GdkEventButton* event)
{
WebMouseEvent result;
result.timeStampSeconds = gdkEventTimeToWebEventTime(event->time);
result.modifiers = gdkStateToWebEventModifiers(event->state);
result.x = static_cast<int>(event->x);
result.y = static_cast<int>(event->y);
result.windowX = result.x;
result.windowY = result.y;
result.globalX = static_cast<int>(event->x_root);
result.globalY = static_cast<int>(event->y_root);
result.clickCount = 0;
switch (event->type) {
case GDK_BUTTON_PRESS:
result.type = WebInputEvent::MouseDown;
break;
case GDK_BUTTON_RELEASE:
result.type = WebInputEvent::MouseUp;
break;
case GDK_3BUTTON_PRESS:
case GDK_2BUTTON_PRESS:
default:
ASSERT_NOT_REACHED();
};
result.button = WebMouseEvent::ButtonNone;
if (event->button == 1)
result.button = WebMouseEvent::ButtonLeft;
else if (event->button == 2)
result.button = WebMouseEvent::ButtonMiddle;
else if (event->button == 3)
result.button = WebMouseEvent::ButtonRight;
if (result.type == WebInputEvent::MouseDown) {
bool forgetPreviousClick = shouldForgetPreviousClick(event->window, event->time, event->x, event->y);
if (!forgetPreviousClick && result.button == gLastClickButton)
++gNumClicks;
else {
gNumClicks = 1;
gLastClickEventWindow = event->window;
gLastClickX = event->x;
gLastClickY = event->y;
gLastClickButton = result.button;
}
gLastClickTime = event->time;
}
result.clickCount = gNumClicks;
return result;
}
WebMouseEvent WebInputEventFactory::mouseEvent(const GdkEventMotion* event)
{
WebMouseEvent result;
result.timeStampSeconds = gdkEventTimeToWebEventTime(event->time);
result.modifiers = gdkStateToWebEventModifiers(event->state);
result.x = static_cast<int>(event->x);
result.y = static_cast<int>(event->y);
result.windowX = result.x;
result.windowY = result.y;
result.globalX = static_cast<int>(event->x_root);
result.globalY = static_cast<int>(event->y_root);
switch (event->type) {
case GDK_MOTION_NOTIFY:
result.type = WebInputEvent::MouseMove;
break;
default:
ASSERT_NOT_REACHED();
}
result.button = WebMouseEvent::ButtonNone;
if (event->state & GDK_BUTTON1_MASK)
result.button = WebMouseEvent::ButtonLeft;
else if (event->state & GDK_BUTTON2_MASK)
result.button = WebMouseEvent::ButtonMiddle;
else if (event->state & GDK_BUTTON3_MASK)
result.button = WebMouseEvent::ButtonRight;
if (shouldForgetPreviousClick(event->window, event->time, event->x, event->y))
resetClickCountState();
return result;
}
WebMouseEvent WebInputEventFactory::mouseEvent(const GdkEventCrossing* event)
{
WebMouseEvent result;
result.timeStampSeconds = gdkEventTimeToWebEventTime(event->time);
result.modifiers = gdkStateToWebEventModifiers(event->state);
result.x = static_cast<int>(event->x);
result.y = static_cast<int>(event->y);
result.windowX = result.x;
result.windowY = result.y;
result.globalX = static_cast<int>(event->x_root);
result.globalY = static_cast<int>(event->y_root);
switch (event->type) {
case GDK_ENTER_NOTIFY:
case GDK_LEAVE_NOTIFY:
result.type = WebInputEvent::MouseMove;
break;
default:
ASSERT_NOT_REACHED();
}
result.button = WebMouseEvent::ButtonNone;
if (event->state & GDK_BUTTON1_MASK)
result.button = WebMouseEvent::ButtonLeft;
else if (event->state & GDK_BUTTON2_MASK)
result.button = WebMouseEvent::ButtonMiddle;
else if (event->state & GDK_BUTTON3_MASK)
result.button = WebMouseEvent::ButtonRight;
if (shouldForgetPreviousClick(event->window, event->time, event->x, event->y))
resetClickCountState();
return result;
}
WebMouseWheelEvent WebInputEventFactory::mouseWheelEvent(const GdkEventScroll* event)
{
WebMouseWheelEvent result;
result.type = WebInputEvent::MouseWheel;
result.button = WebMouseEvent::ButtonNone;
result.timeStampSeconds = gdkEventTimeToWebEventTime(event->time);
result.modifiers = gdkStateToWebEventModifiers(event->state);
result.x = static_cast<int>(event->x);
result.y = static_cast<int>(event->y);
result.windowX = result.x;
result.windowY = result.y;
result.globalX = static_cast<int>(event->x_root);
result.globalY = static_cast<int>(event->y_root);
static const float scrollbarPixelsPerTick = 160.0f / 3.0f;
switch (event->direction) {
case GDK_SCROLL_UP:
result.deltaY = scrollbarPixelsPerTick;
result.wheelTicksY = 1;
break;
case GDK_SCROLL_DOWN:
result.deltaY = -scrollbarPixelsPerTick;
result.wheelTicksY = -1;
break;
case GDK_SCROLL_LEFT:
result.deltaX = scrollbarPixelsPerTick;
result.wheelTicksX = 1;
break;
case GDK_SCROLL_RIGHT:
result.deltaX = -scrollbarPixelsPerTick;
result.wheelTicksX = -1;
break;
}
return result;
}
}