WebEventFactory.cpp [plain text]
#include "config.h"
#include "WebEventFactory.h"
#include "EflKeyboardUtilities.h"
#include <WebCore/AffineTransform.h>
#include <WebCore/Scrollbar.h>
#if ENABLE(TOUCH_EVENTS)
#include "APIArray.h"
#include "EwkTouchEvent.h"
#include "EwkTouchPoint.h"
#include "WKAPICast.h"
#endif
using namespace WebCore;
namespace WebKit {
enum {
VerticalScrollDirection = 0,
HorizontalScrollDirection = 1
};
enum {
LeftButton = 1,
MiddleButton = 2,
RightButton = 3
};
static const char keyPadPrefix[] = "KP_";
static inline WebEvent::Modifiers modifiersForEvent(const Evas_Modifier* modifiers)
{
unsigned result = 0;
if (evas_key_modifier_is_set(modifiers, "Shift"))
result |= WebEvent::ShiftKey;
if (evas_key_modifier_is_set(modifiers, "Control"))
result |= WebEvent::ControlKey;
if (evas_key_modifier_is_set(modifiers, "Alt"))
result |= WebEvent::AltKey;
if (evas_key_modifier_is_set(modifiers, "Meta"))
result |= WebEvent::MetaKey;
return static_cast<WebEvent::Modifiers>(result);
}
static inline WebMouseEvent::Button buttonForEvent(int button)
{
if (button == LeftButton)
return WebMouseEvent::LeftButton;
if (button == MiddleButton)
return WebMouseEvent::MiddleButton;
if (button == RightButton)
return WebMouseEvent::RightButton;
return WebMouseEvent::NoButton;
}
static inline int clickCountForEvent(const Evas_Button_Flags flags)
{
if (flags & EVAS_BUTTON_TRIPLE_CLICK)
return 3;
if (flags & EVAS_BUTTON_DOUBLE_CLICK)
return 2;
return 1;
}
static inline double convertMillisecondToSecond(unsigned timestamp)
{
return static_cast<double>(timestamp) / 1000;
}
WebMouseEvent WebEventFactory::createWebMouseEvent(const Evas_Event_Mouse_Down* event, const AffineTransform& toWebContent, const AffineTransform& toDeviceScreen)
{
IntPoint pos(event->canvas.x, event->canvas.y);
return WebMouseEvent(WebEvent::MouseDown,
buttonForEvent(event->button),
toWebContent.mapPoint(pos),
toDeviceScreen.mapPoint(pos),
0 ,
0 ,
0 ,
clickCountForEvent(event->flags),
modifiersForEvent(event->modifiers),
convertMillisecondToSecond(event->timestamp));
}
WebMouseEvent WebEventFactory::createWebMouseEvent(const Evas_Event_Mouse_Up* event, const AffineTransform& toWebContent, const AffineTransform& toDeviceScreen)
{
IntPoint pos(event->canvas.x, event->canvas.y);
return WebMouseEvent(WebEvent::MouseUp,
buttonForEvent(event->button),
toWebContent.mapPoint(pos),
toDeviceScreen.mapPoint(pos),
0 ,
0 ,
0 ,
clickCountForEvent(event->flags),
modifiersForEvent(event->modifiers),
convertMillisecondToSecond(event->timestamp));
}
WebMouseEvent WebEventFactory::createWebMouseEvent(const Evas_Event_Mouse_Move* event, const AffineTransform& toWebContent, const AffineTransform& toDeviceScreen)
{
IntPoint pos(event->cur.canvas.x, event->cur.canvas.y);
return WebMouseEvent(WebEvent::MouseMove,
buttonForEvent(event->buttons),
toWebContent.mapPoint(pos),
toDeviceScreen.mapPoint(pos),
(event->cur.canvas.x - event->prev.canvas.x) ,
(event->cur.canvas.y - event->prev.canvas.y) ,
0 ,
0 ,
modifiersForEvent(event->modifiers),
convertMillisecondToSecond(event->timestamp));
}
WebWheelEvent WebEventFactory::createWebWheelEvent(const Evas_Event_Mouse_Wheel* event, const AffineTransform& toWebContent, const AffineTransform& toDeviceScreen)
{
float deltaX = 0;
float deltaY = 0;
float wheelTicksX = 0;
float wheelTicksY = 0;
if (event->direction == VerticalScrollDirection) {
deltaX = 0;
deltaY = - event->z;
} else if (event->direction == HorizontalScrollDirection) {
deltaX = - event->z;
deltaY = 0;
}
wheelTicksX = deltaX;
wheelTicksY = deltaY;
deltaX *= static_cast<float>(Scrollbar::pixelsPerLineStep());
deltaY *= static_cast<float>(Scrollbar::pixelsPerLineStep());
IntPoint pos(event->canvas.x, event->canvas.y);
return WebWheelEvent(WebEvent::Wheel,
toWebContent.mapPoint(pos),
toDeviceScreen.mapPoint(pos),
FloatSize(deltaX, deltaY),
FloatSize(wheelTicksX, wheelTicksY),
WebWheelEvent::ScrollByPixelWheelEvent,
modifiersForEvent(event->modifiers),
convertMillisecondToSecond(event->timestamp));
}
WebKeyboardEvent WebEventFactory::createWebKeyboardEvent(const Evas_Event_Key_Down* event)
{
const String keyName(event->key);
return WebKeyboardEvent(WebEvent::KeyDown,
String::fromUTF8(event->string),
String::fromUTF8(event->string),
keyIdentifierForEvasKeyName(keyName),
windowsKeyCodeForEvasKeyName(keyName),
0 ,
0 ,
false ,
keyName.startsWith(keyPadPrefix),
false ,
modifiersForEvent(event->modifiers),
convertMillisecondToSecond(event->timestamp));
}
WebKeyboardEvent WebEventFactory::createWebKeyboardEvent(const Evas_Event_Key_Up* event)
{
const String keyName(event->key);
return WebKeyboardEvent(WebEvent::KeyUp,
String::fromUTF8(event->string),
String::fromUTF8(event->string),
keyIdentifierForEvasKeyName(keyName),
windowsKeyCodeForEvasKeyName(keyName),
0 ,
0 ,
false ,
keyName.startsWith(keyPadPrefix),
false ,
modifiersForEvent(event->modifiers),
convertMillisecondToSecond(event->timestamp));
}
#if ENABLE(TOUCH_EVENTS)
static inline WebPlatformTouchPoint::TouchPointState toWebPlatformTouchPointState(WKTouchPointState state)
{
switch (state) {
case kWKTouchPointStateTouchReleased:
return WebPlatformTouchPoint::TouchReleased;
case kWKTouchPointStateTouchMoved:
return WebPlatformTouchPoint::TouchMoved;
case kWKTouchPointStateTouchPressed:
return WebPlatformTouchPoint::TouchPressed;
case kWKTouchPointStateTouchStationary:
return WebPlatformTouchPoint::TouchStationary;
case kWKTouchPointStateTouchCancelled:
default:
return WebPlatformTouchPoint::TouchCancelled;
}
}
static inline WebEvent::Type toWebEventType(WKEventType type)
{
switch (type) {
case kWKEventTypeTouchStart:
return WebEvent::TouchStart;
case kWKEventTypeTouchMove:
return WebEvent::TouchMove;
case kWKEventTypeTouchEnd:
return WebEvent::TouchEnd;
case kWKEventTypeTouchCancel:
return WebEvent::TouchCancel;
default:
return WebEvent::NoType;
}
}
static inline WebEvent::Modifiers toWebEventModifiers(unsigned modifiers)
{
unsigned result = 0;
if (modifiers & kWKEventModifiersShiftKey)
result |= WebEvent::ShiftKey;
if (modifiers & kWKEventModifiersControlKey)
result |= WebEvent::ControlKey;
if (modifiers & kWKEventModifiersAltKey)
result |= WebEvent::AltKey;
if (modifiers & kWKEventModifiersMetaKey)
result |= WebEvent::MetaKey;
return static_cast<WebEvent::Modifiers>(result);
}
WebTouchEvent WebEventFactory::createWebTouchEvent(const EwkTouchEvent* event, const AffineTransform& toWebContent)
{
API::Array* touchPointsArray = toImpl(event->touchPoints());
size_t size = touchPointsArray->size();
Vector<WebPlatformTouchPoint> touchPoints;
touchPoints.reserveInitialCapacity(size);
for (size_t i = 0; i < size; ++i) {
if (EwkTouchPoint* point = touchPointsArray->at<EwkTouchPoint>(i))
touchPoints.uncheckedAppend(WebPlatformTouchPoint(point->id(), toWebPlatformTouchPointState(point->state()), toIntPoint(point->screenPosition()), toWebContent.mapPoint(toIntPoint(point->position())), toIntSize(point->radius()), point->rotationAngle(), point->forceFactor()));
}
return WebTouchEvent(toWebEventType(event->eventType()), touchPoints, toWebEventModifiers(event->modifiers()), event->timestamp());
}
#endif
}