File: WebEventFactoryWPE.cpp

package info (click to toggle)
wpewebkit 2.48.6-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 421,736 kB
  • sloc: cpp: 3,671,212; javascript: 194,411; ansic: 165,592; python: 46,476; asm: 19,295; ruby: 18,528; perl: 16,602; xml: 4,650; yacc: 2,360; java: 1,993; sh: 1,948; lex: 1,327; pascal: 366; makefile: 87
file content (240 lines) | stat: -rw-r--r-- 9,121 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/*
 * Copyright (C) 2023 Igalia S.L.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
 * THE POSSIBILITY OF SUCH DAMAGE.
 */

#include "config.h"
#include "WebEventFactory.h"

#if ENABLE(WPE_PLATFORM)
#include <WebCore/FloatPoint.h>
#include <WebCore/Scrollbar.h>
#include <wpe/wpe-platform.h>
#include <wtf/MonotonicTime.h>
#include <wtf/WallTime.h>

namespace WebKit {

using namespace WebCore;

static WallTime wallTimeForEvent(WPEEvent* event)
{
    auto time = wpe_event_get_time(event);
    if (!time)
        return WallTime::now();
    return MonotonicTime::fromRawSeconds(time / 1000.).approximateWallTime();
}

static OptionSet<WebEventModifier> modifiersFromWPEModifiers(WPEModifiers wpeModifiers)
{
    OptionSet<WebEventModifier> modifiers;
    if (wpeModifiers & WPE_MODIFIER_KEYBOARD_CONTROL)
        modifiers.add(WebEventModifier::ControlKey);
    if (wpeModifiers & WPE_MODIFIER_KEYBOARD_SHIFT)
        modifiers.add(WebEventModifier::ShiftKey);
    if (wpeModifiers & WPE_MODIFIER_KEYBOARD_ALT)
        modifiers.add(WebEventModifier::AltKey);
    if (wpeModifiers & WPE_MODIFIER_KEYBOARD_META)
        modifiers.add(WebEventModifier::MetaKey);
    if (wpeModifiers & WPE_MODIFIER_KEYBOARD_CAPS_LOCK)
        modifiers.add(WebEventModifier::CapsLockKey);
    return modifiers;
}

static WebMouseEventButton buttonForWPEButton(guint button)
{
    switch (button) {
    case 1:
        return WebMouseEventButton::Left;
    case 2:
        return WebMouseEventButton::Middle;
    case 3:
        return WebMouseEventButton::Right;
    }
    return WebMouseEventButton::None;
}

static WebMouseEventButton buttonFromWPEModifiers(WPEModifiers modifiers)
{
    if (modifiers & WPE_MODIFIER_POINTER_BUTTON1)
        return WebMouseEventButton::Left;
    if (modifiers & WPE_MODIFIER_POINTER_BUTTON2)
        return WebMouseEventButton::Middle;
    if (modifiers & WPE_MODIFIER_POINTER_BUTTON3)
        return WebMouseEventButton::Right;
    return WebMouseEventButton::None;
}

static FloatPoint movementDeltaFromEvent(WPEEvent* event)
{
    double x, y;
    wpe_event_pointer_move_get_delta(event, &x, &y);
    return FloatPoint(x, y);
}

static short pressedMouseButtons(WPEModifiers modifiers)
{
    // MouseEvent.buttons
    // https://www.w3.org/TR/uievents/#ref-for-dom-mouseevent-buttons-1

    // 0 MUST indicate no button is currently active.
    short buttons = 0;

    // 1 MUST indicate the primary button of the device (in general, the left button or the only button on
    // single-button devices, used to activate a user interface control or select text).
    if (modifiers & WPE_MODIFIER_POINTER_BUTTON1)
        buttons |= 1;

    // 4 MUST indicate the auxiliary button (in general, the middle button, often combined with a mouse wheel).
    if (modifiers & WPE_MODIFIER_POINTER_BUTTON2)
        buttons |= 4;

    // 2 MUST indicate the secondary button (in general, the right button, often used to display a context menu),
    // if present.
    if (modifiers & WPE_MODIFIER_POINTER_BUTTON3)
        buttons |= 2;

    return buttons;
}

static IntPoint positionFromEvent(WPEEvent* event)
{
    double x, y;
    if (wpe_event_get_position(event, &x, &y))
        return { clampToInteger(x), clampToInteger(y) };
    return { };
}

WebMouseEvent WebEventFactory::createWebMouseEvent(WPEEvent* event)
{
    auto modifiers = wpe_event_get_modifiers(event);
    FloatPoint movementDelta;
    std::optional<WebEventType> type;
    auto button = WebMouseEventButton::None;
    auto position = positionFromEvent(event);
    unsigned clickCount = 0;

    switch (wpe_event_get_event_type(event)) {
    case WPE_EVENT_POINTER_DOWN:
        type = WebEventType::MouseDown;
        button = buttonForWPEButton(wpe_event_pointer_button_get_button(event));
        clickCount = wpe_event_pointer_button_get_press_count(event);
        break;
    case WPE_EVENT_POINTER_UP:
        type = WebEventType::MouseUp;
        button = buttonForWPEButton(wpe_event_pointer_button_get_button(event));
        break;
    case WPE_EVENT_POINTER_MOVE:
    case WPE_EVENT_POINTER_ENTER:
    case WPE_EVENT_POINTER_LEAVE:
        type = WebEventType::MouseMove;
        button = buttonFromWPEModifiers(modifiers);
        movementDelta = movementDeltaFromEvent(event);
        break;
    default:
        RELEASE_ASSERT_NOT_REACHED();
    }

    return WebMouseEvent({ type.value(), modifiersFromWPEModifiers(modifiers), wallTimeForEvent(event) },
        button,
        pressedMouseButtons(modifiers),
        position,
        position,
        movementDelta.x(),
        movementDelta.y(),
        0 /* deltaZ */,
        clickCount);
}

WebWheelEvent WebEventFactory::createWebWheelEvent(WPEEvent* event)
{
    auto phase = wpe_event_scroll_is_stop(event) ? WebWheelEvent::Phase::PhaseEnded : WebWheelEvent::Phase::PhaseChanged;
    return createWebWheelEvent(event, phase);
}

WebWheelEvent WebEventFactory::createWebWheelEvent(WPEEvent* event, WebWheelEvent::Phase phase)
{
    double deltaX, deltaY;
    wpe_event_scroll_get_deltas(event, &deltaX, &deltaY);
    bool hasPreciseScrollingDeltas = wpe_event_scroll_has_precise_deltas(event);
    auto position = positionFromEvent(event);

    auto wheelTicks = FloatSize(deltaX, deltaY);
    FloatSize delta;
    if (hasPreciseScrollingDeltas) {
        static const double wpeScrollDeltaMultiplier = 2.5;
        delta = wheelTicks.scaled(wpeScrollDeltaMultiplier);
    } else {
        auto* view = wpe_event_get_view(event);
        float stepX = wheelTicks.width() ? static_cast<float>(Scrollbar::pixelsPerLineStep(wpe_view_get_width(view))) : 0;
        float stepY = wheelTicks.height() ? static_cast<float>(Scrollbar::pixelsPerLineStep(wpe_view_get_height(view))) : 0;
        delta = wheelTicks.scaled(stepX, stepY);
    }

    return WebWheelEvent({ WebEventType::Wheel, modifiersFromWPEModifiers(wpe_event_get_modifiers(event)), wallTimeForEvent(event) },
        position, position, delta, wheelTicks, WebWheelEvent::ScrollByPixelWheelEvent, phase, WebWheelEvent::Phase::PhaseNone, hasPreciseScrollingDeltas);
}

WebKeyboardEvent WebEventFactory::createWebKeyboardEvent(WPEEvent* event, const String& text, bool isAutoRepeat)
{
    auto type = wpe_event_get_event_type(event) == WPE_EVENT_KEYBOARD_KEY_DOWN ? WebEventType::KeyDown : WebEventType::KeyUp;
    auto keyval = wpe_event_keyboard_get_keyval(event);
    auto keycode = wpe_event_keyboard_get_keycode(event);
    return WebKeyboardEvent({ type, modifiersFromWPEModifiers(wpe_event_get_modifiers(event)), wallTimeForEvent(event) },
        text.isNull() ? WebKeyboardEvent::singleCharacterStringForWPEKeyval(keyval) : text,
        WebKeyboardEvent::keyValueStringForWPEKeyval(keyval),
        WebKeyboardEvent::keyCodeStringForWPEKeycode(keycode),
        WebKeyboardEvent::keyIdentifierForWPEKeyval(keyval),
        WebKeyboardEvent::windowsKeyCodeForWPEKeyval(keyval),
        keyval, false, std::nullopt, std::nullopt, isAutoRepeat,
        keyval >= WPE_KEY_KP_Space && keyval <= WPE_KEY_KP_9);
}

#if ENABLE(TOUCH_EVENTS)
WebTouchEvent WebEventFactory::createWebTouchEvent(WPEEvent* event, Vector<WebPlatformTouchPoint>&& touchPoints)
{
    std::optional<WebEventType> type;
    switch (wpe_event_get_event_type(event)) {
    case WPE_EVENT_TOUCH_DOWN:
        type = WebEventType::TouchStart;
        break;
    case WPE_EVENT_TOUCH_UP:
        type = WebEventType::TouchEnd;
        break;
    case WPE_EVENT_TOUCH_MOVE:
        type = WebEventType::TouchMove;
        break;
    case WPE_EVENT_TOUCH_CANCEL:
        type = WebEventType::TouchCancel;
        break;
    default:
        RELEASE_ASSERT_NOT_REACHED();
    }

    return WebTouchEvent({ type.value(), modifiersFromWPEModifiers(wpe_event_get_modifiers(event)), wallTimeForEvent(event) }, WTFMove(touchPoints), { }, { });
}
#endif // ENABLE(TOUCH_EVENTS)

} // namespace WebKit

#endif // ENABLE(WPE_PLATFORM)