WebEventFactory.cpp [plain text]
#include "config.h"
#include "WebEventFactory.h"
#include <WebCore/PlatformKeyboardEvent.h>
#include <WebCore/Scrollbar.h>
#include <cmath>
#include <wpe/wpe.h>
namespace WebKit {
static inline bool isWPEKeyCodeFromKeyPad(unsigned keyCode)
{
return keyCode >= WPE_KEY_KP_Space && keyCode <= WPE_KEY_KP_9;
}
static OptionSet<WebEvent::Modifier> modifiersForEventModifiers(unsigned eventModifiers)
{
OptionSet<WebEvent::Modifier> modifiers;
if (eventModifiers & wpe_input_keyboard_modifier_control)
modifiers.add(WebEvent::Modifier::ControlKey);
if (eventModifiers & wpe_input_keyboard_modifier_shift)
modifiers.add(WebEvent::Modifier::ShiftKey);
if (eventModifiers & wpe_input_keyboard_modifier_alt)
modifiers.add(WebEvent::Modifier::AltKey);
if (eventModifiers & wpe_input_keyboard_modifier_meta)
modifiers.add(WebEvent::Modifier::MetaKey);
return modifiers;
}
WallTime wallTimeForEventTime(uint64_t timestamp)
{
return timestamp ? MonotonicTime::fromRawSeconds(timestamp / 1000.).approximateWallTime() : WallTime::now();
}
WebKeyboardEvent WebEventFactory::createWebKeyboardEvent(struct wpe_input_keyboard_event* event, const String& text, bool handledByInputMethod, Optional<Vector<WebCore::CompositionUnderline>>&& preeditUnderlines, Optional<EditingRange>&& preeditSelectionRange)
{
return WebKeyboardEvent(event->pressed ? WebEvent::KeyDown : WebEvent::KeyUp,
text.isNull() ? WebCore::PlatformKeyboardEvent::singleCharacterString(event->key_code) : text,
WebCore::PlatformKeyboardEvent::keyValueForWPEKeyCode(event->key_code),
WebCore::PlatformKeyboardEvent::keyCodeForHardwareKeyCode(event->hardware_key_code),
WebCore::PlatformKeyboardEvent::keyIdentifierForWPEKeyCode(event->key_code),
WebCore::PlatformKeyboardEvent::windowsKeyCodeForWPEKeyCode(event->key_code),
event->key_code,
handledByInputMethod,
WTFMove(preeditUnderlines),
WTFMove(preeditSelectionRange),
isWPEKeyCodeFromKeyPad(event->key_code),
modifiersForEventModifiers(event->modifiers),
wallTimeForEventTime(event->time));
}
static inline short pressedMouseButtons(uint32_t modifiers)
{
short buttons = 0;
if (modifiers & wpe_input_pointer_modifier_button1)
buttons |= 1;
if (modifiers & wpe_input_pointer_modifier_button2)
buttons |= 2;
if (modifiers & wpe_input_pointer_modifier_button3)
buttons |= 4;
return buttons;
}
WebMouseEvent WebEventFactory::createWebMouseEvent(struct wpe_input_pointer_event* event, float deviceScaleFactor)
{
WebEvent::Type type = WebEvent::NoType;
switch (event->type) {
case wpe_input_pointer_event_type_motion:
type = WebEvent::MouseMove;
break;
case wpe_input_pointer_event_type_button:
type = event->state ? WebEvent::MouseDown : WebEvent::MouseUp;
break;
case wpe_input_pointer_event_type_null:
ASSERT_NOT_REACHED();
}
WebMouseEvent::Button button = WebMouseEvent::NoButton;
switch (event->type) {
case wpe_input_pointer_event_type_motion:
case wpe_input_pointer_event_type_button:
if (event->button == 1)
button = WebMouseEvent::LeftButton;
else if (event->button == 2)
button = WebMouseEvent::RightButton;
else if (event->button == 3)
button = WebMouseEvent::MiddleButton;
break;
case wpe_input_pointer_event_type_null:
ASSERT_NOT_REACHED();
}
unsigned clickCount = (type == WebEvent::MouseDown) ? 1 : 0;
WebCore::IntPoint position(event->x, event->y);
position.scale(1 / deviceScaleFactor);
return WebMouseEvent(type, button, pressedMouseButtons(event->modifiers), position, position,
0, 0, 0, clickCount, modifiersForEventModifiers(event->modifiers), wallTimeForEventTime(event->time));
}
WebWheelEvent WebEventFactory::createWebWheelEvent(struct wpe_input_axis_event* event, float deviceScaleFactor)
{
enum Axis {
Vertical,
Horizontal,
Smooth
};
WebCore::FloatSize wheelTicks;
WebCore::FloatSize delta;
switch (event->axis) {
case Vertical:
wheelTicks = WebCore::FloatSize(0, std::copysign(1, event->value));
delta = wheelTicks;
delta.scale(WebCore::Scrollbar::pixelsPerLineStep());
break;
case Horizontal:
wheelTicks = WebCore::FloatSize(std::copysign(1, event->value), 0);
delta = wheelTicks;
delta.scale(WebCore::Scrollbar::pixelsPerLineStep());
break;
case Smooth:
wheelTicks = WebCore::FloatSize(0, event->value / deviceScaleFactor);
delta = wheelTicks;
break;
default:
ASSERT_NOT_REACHED();
};
WebCore::IntPoint position(event->x, event->y);
position.scale(1 / deviceScaleFactor);
return WebWheelEvent(WebEvent::Wheel, position, position,
delta, wheelTicks, WebWheelEvent::ScrollByPixelWheelEvent, OptionSet<WebEvent::Modifier> { }, wallTimeForEventTime(event->time));
}
#if ENABLE(TOUCH_EVENTS)
static WebKit::WebPlatformTouchPoint::TouchPointState stateForTouchPoint(int mainEventId, const struct wpe_input_touch_event_raw* point)
{
if (point->id != mainEventId)
return WebKit::WebPlatformTouchPoint::TouchStationary;
switch (point->type) {
case wpe_input_touch_event_type_down:
return WebKit::WebPlatformTouchPoint::TouchPressed;
case wpe_input_touch_event_type_motion:
return WebKit::WebPlatformTouchPoint::TouchMoved;
case wpe_input_touch_event_type_up:
return WebKit::WebPlatformTouchPoint::TouchReleased;
case wpe_input_touch_event_type_null:
ASSERT_NOT_REACHED();
break;
};
return WebKit::WebPlatformTouchPoint::TouchStationary;
}
WebTouchEvent WebEventFactory::createWebTouchEvent(struct wpe_input_touch_event* event, float deviceScaleFactor)
{
WebEvent::Type type = WebEvent::NoType;
switch (event->type) {
case wpe_input_touch_event_type_down:
type = WebEvent::TouchStart;
break;
case wpe_input_touch_event_type_motion:
type = WebEvent::TouchMove;
break;
case wpe_input_touch_event_type_up:
type = WebEvent::TouchEnd;
break;
case wpe_input_touch_event_type_null:
ASSERT_NOT_REACHED();
}
Vector<WebKit::WebPlatformTouchPoint> touchPoints;
touchPoints.reserveCapacity(event->touchpoints_length);
for (unsigned i = 0; i < event->touchpoints_length; ++i) {
auto& point = event->touchpoints[i];
if (point.type == wpe_input_touch_event_type_null)
continue;
WebCore::IntPoint pointCoordinates(point.x, point.y);
pointCoordinates.scale(1 / deviceScaleFactor);
touchPoints.uncheckedAppend(
WebKit::WebPlatformTouchPoint(point.id, stateForTouchPoint(event->id, &point),
pointCoordinates, pointCoordinates));
}
return WebTouchEvent(type, WTFMove(touchPoints), OptionSet<WebEvent::Modifier> { }, wallTimeForEventTime(event->time));
}
#endif // ENABLE(TOUCH_EVENTS)
}