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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "base/basictypes.h"
#include "ipc/IPCMessageUtils.h"
#include "ipc/IPCMessageUtilsSpecializations.h"
#include "mozilla/dom/UIEvent.h"
#include "mozilla/ArrayUtils.h"
#include "mozilla/Assertions.h"
#include "mozilla/ContentEvents.h"
#include "mozilla/EventStateManager.h"
#include "mozilla/PointerLockManager.h"
#include "mozilla/PresShell.h"
#include "mozilla/TextEvents.h"
#include "nsCOMPtr.h"
#include "nsContentUtils.h"
#include "nsIContent.h"
#include "nsIInterfaceRequestorUtils.h"
#include "nsIDocShell.h"
#include "nsIFrame.h"
#include "nsLayoutUtils.h"
#include "prtime.h"
namespace mozilla::dom {
UIEvent::UIEvent(EventTarget* aOwner, nsPresContext* aPresContext,
WidgetGUIEvent* aEvent)
: Event(aOwner, aPresContext,
aEvent ? aEvent : new InternalUIEvent(false, eVoidEvent, nullptr)),
mDefaultClientPoint(0, 0),
mLayerPoint(0, 0),
mPagePoint(0, 0),
mMovementPoint(0, 0) {
if (aEvent) {
mEventIsInternal = false;
} else {
mEventIsInternal = true;
}
// Fill mDetail and mView according to the mEvent (widget-generated
// event) we've got
switch (mEvent->mClass) {
case eUIEventClass: {
mDetail = mEvent->AsUIEvent()->mDetail;
break;
}
case eScrollPortEventClass: {
InternalScrollPortEvent* scrollEvent = mEvent->AsScrollPortEvent();
mDetail = static_cast<int32_t>(scrollEvent->mOrient);
break;
}
default:
mDetail = 0;
break;
}
mView = nullptr;
if (mPresContext) {
nsIDocShell* docShell = mPresContext->GetDocShell();
if (docShell) {
mView = docShell->GetWindow();
}
}
}
// static
already_AddRefed<UIEvent> UIEvent::Constructor(const GlobalObject& aGlobal,
const nsAString& aType,
const UIEventInit& aParam) {
nsCOMPtr<EventTarget> t = do_QueryInterface(aGlobal.GetAsSupports());
RefPtr<UIEvent> e = new UIEvent(t, nullptr, nullptr);
bool trusted = e->Init(t);
e->InitUIEvent(aType, aParam.mBubbles, aParam.mCancelable, aParam.mView,
aParam.mDetail);
e->SetTrusted(trusted);
e->SetComposed(aParam.mComposed);
return e.forget();
}
NS_IMPL_CYCLE_COLLECTION_INHERITED(UIEvent, Event, mView)
NS_IMPL_ADDREF_INHERITED(UIEvent, Event)
NS_IMPL_RELEASE_INHERITED(UIEvent, Event)
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(UIEvent)
NS_INTERFACE_MAP_END_INHERITING(Event)
static nsIntPoint DevPixelsToCSSPixels(const LayoutDeviceIntPoint& aPoint,
nsPresContext* aContext) {
return nsIntPoint(aContext->DevPixelsToIntCSSPixels(aPoint.x),
aContext->DevPixelsToIntCSSPixels(aPoint.y));
}
nsIntPoint UIEvent::GetMovementPoint() {
if (mEvent->mFlags.mIsPositionless) {
return nsIntPoint(0, 0);
}
if (mPrivateDataDuplicated || mEventIsInternal) {
return mMovementPoint;
}
if (!mEvent || !mEvent->AsGUIEvent()->mWidget ||
(mEvent->mMessage != eMouseMove && mEvent->mMessage != ePointerMove)) {
// Pointer Lock spec defines that movementX/Y must be zero for all mouse
// events except mousemove.
return nsIntPoint(0, 0);
}
// Calculate the delta between the last screen point and the current one.
nsIntPoint current = DevPixelsToCSSPixels(mEvent->mRefPoint, mPresContext);
nsIntPoint last = DevPixelsToCSSPixels(mEvent->mLastRefPoint, mPresContext);
return current - last;
}
void UIEvent::InitUIEvent(const nsAString& typeArg, bool canBubbleArg,
bool cancelableArg, nsGlobalWindowInner* viewArg,
int32_t detailArg) {
if (NS_WARN_IF(mEvent->mFlags.mIsBeingDispatched)) {
return;
}
Event::InitEvent(typeArg, canBubbleArg, cancelableArg);
mDetail = detailArg;
mView = viewArg ? viewArg->GetOuterWindow() : nullptr;
}
already_AddRefed<nsIContent> UIEvent::GetRangeParentContentAndOffset(
int32_t* aOffset) const {
if (NS_WARN_IF(!mPresContext)) {
return nullptr;
}
RefPtr<PresShell> presShell = mPresContext->GetPresShell();
if (NS_WARN_IF(!presShell)) {
return nullptr;
}
nsCOMPtr<nsIContent> container;
nsLayoutUtils::GetContainerAndOffsetAtEvent(
presShell, mEvent, getter_AddRefs(container), aOffset);
return container.forget();
}
int32_t UIEvent::RangeOffset() const {
if (NS_WARN_IF(!mPresContext)) {
return 0;
}
RefPtr<PresShell> presShell = mPresContext->GetPresShell();
if (NS_WARN_IF(!presShell)) {
return 0;
}
int32_t offset = 0;
nsLayoutUtils::GetContainerAndOffsetAtEvent(presShell, mEvent, nullptr,
&offset);
return offset;
}
nsIntPoint UIEvent::GetLayerPoint() const {
if (mEvent->mFlags.mIsPositionless) {
return nsIntPoint(0, 0);
}
if (!mEvent ||
(mEvent->mClass != eMouseEventClass &&
mEvent->mClass != eMouseScrollEventClass &&
mEvent->mClass != eWheelEventClass &&
mEvent->mClass != ePointerEventClass &&
mEvent->mClass != eTouchEventClass &&
mEvent->mClass != eDragEventClass &&
mEvent->mClass != eSimpleGestureEventClass) ||
!mPresContext || mEventIsInternal) {
return mLayerPoint;
}
// XXX I'm not really sure this is correct; it's my best shot, though
nsIFrame* targetFrame = mPresContext->EventStateManager()->GetEventTarget();
if (!targetFrame) return mLayerPoint;
nsIFrame* layer = nsLayoutUtils::GetClosestLayer(targetFrame);
nsPoint pt(
nsLayoutUtils::GetEventCoordinatesRelativeTo(mEvent, RelativeTo{layer}));
return nsIntPoint(nsPresContext::AppUnitsToIntCSSPixels(pt.x),
nsPresContext::AppUnitsToIntCSSPixels(pt.y));
}
void UIEvent::DuplicatePrivateData() {
mDefaultClientPoint = Event::GetClientCoords(
mPresContext, mEvent, mEvent->mRefPoint, mDefaultClientPoint);
mMovementPoint = GetMovementPoint();
mLayerPoint = GetLayerPoint();
mPagePoint = Event::GetPageCoords(mPresContext, mEvent, mEvent->mRefPoint,
mDefaultClientPoint);
// GetScreenPoint converts mEvent->mRefPoint to right coordinates.
CSSIntPoint screenPoint =
Event::GetScreenCoords(mPresContext, mEvent, mEvent->mRefPoint)
.valueOr(CSSIntPoint{0, 0});
Event::DuplicatePrivateData();
CSSToLayoutDeviceScale scale = mPresContext
? mPresContext->CSSToDevPixelScale()
: CSSToLayoutDeviceScale(1);
mEvent->mRefPoint = RoundedToInt(screenPoint * scale);
}
void UIEvent::Serialize(IPC::MessageWriter* aWriter,
bool aSerializeInterfaceType) {
if (aSerializeInterfaceType) {
IPC::WriteParam(aWriter, u"uievent"_ns);
}
Event::Serialize(aWriter, false);
IPC::WriteParam(aWriter, Detail());
}
bool UIEvent::Deserialize(IPC::MessageReader* aReader) {
NS_ENSURE_TRUE(Event::Deserialize(aReader), false);
NS_ENSURE_TRUE(IPC::ReadParam(aReader, &mDetail), false);
return true;
}
// XXX Following struct and array are used only in
// UIEvent::ComputeModifierState(), but if we define them in it,
// we fail to build on Mac at calling mozilla::ArrayLength().
struct ModifierPair {
Modifier modifier;
const char* name;
};
static const ModifierPair kPairs[] = {
// clang-format off
{ MODIFIER_ALT, NS_DOM_KEYNAME_ALT },
{ MODIFIER_ALTGRAPH, NS_DOM_KEYNAME_ALTGRAPH },
{ MODIFIER_CAPSLOCK, NS_DOM_KEYNAME_CAPSLOCK },
{ MODIFIER_CONTROL, NS_DOM_KEYNAME_CONTROL },
{ MODIFIER_FN, NS_DOM_KEYNAME_FN },
{ MODIFIER_FNLOCK, NS_DOM_KEYNAME_FNLOCK },
{ MODIFIER_META, NS_DOM_KEYNAME_META },
{ MODIFIER_NUMLOCK, NS_DOM_KEYNAME_NUMLOCK },
{ MODIFIER_SCROLLLOCK, NS_DOM_KEYNAME_SCROLLLOCK },
{ MODIFIER_SHIFT, NS_DOM_KEYNAME_SHIFT },
{ MODIFIER_SYMBOL, NS_DOM_KEYNAME_SYMBOL },
{ MODIFIER_SYMBOLLOCK, NS_DOM_KEYNAME_SYMBOLLOCK },
// clang-format on
};
// static
Modifiers UIEvent::ComputeModifierState(const nsAString& aModifiersList) {
if (aModifiersList.IsEmpty()) {
return 0;
}
// Be careful about the performance. If aModifiersList is too long,
// parsing it needs too long time.
// XXX Should we abort if aModifiersList is too long?
Modifiers modifiers = 0;
nsAString::const_iterator listStart, listEnd;
aModifiersList.BeginReading(listStart);
aModifiersList.EndReading(listEnd);
for (uint32_t i = 0; i < ArrayLength(kPairs); i++) {
nsAString::const_iterator start(listStart), end(listEnd);
if (!FindInReadable(NS_ConvertASCIItoUTF16(kPairs[i].name), start, end)) {
continue;
}
if ((start != listStart && !NS_IsAsciiWhitespace(*(--start))) ||
(end != listEnd && !NS_IsAsciiWhitespace(*(end)))) {
continue;
}
modifiers |= kPairs[i].modifier;
}
return modifiers;
}
bool UIEvent::GetModifierStateInternal(const nsAString& aKey) {
WidgetInputEvent* inputEvent = mEvent->AsInputEvent();
MOZ_ASSERT(inputEvent, "mEvent must be WidgetInputEvent or derived class");
return ((inputEvent->mModifiers & WidgetInputEvent::GetModifier(aKey)) != 0);
}
static Modifiers ConvertToModifiers(const EventModifierInit& aParam) {
Modifiers bits = MODIFIER_NONE;
#define SET_MODIFIER(aName, aValue) bits |= aParam.m##aName ? (aValue) : 0;
SET_MODIFIER(CtrlKey, MODIFIER_CONTROL)
SET_MODIFIER(ShiftKey, MODIFIER_SHIFT)
SET_MODIFIER(AltKey, MODIFIER_ALT)
SET_MODIFIER(MetaKey, MODIFIER_META)
SET_MODIFIER(ModifierAltGraph, MODIFIER_ALTGRAPH)
SET_MODIFIER(ModifierCapsLock, MODIFIER_CAPSLOCK)
SET_MODIFIER(ModifierFn, MODIFIER_FN)
SET_MODIFIER(ModifierFnLock, MODIFIER_FNLOCK)
SET_MODIFIER(ModifierNumLock, MODIFIER_NUMLOCK)
SET_MODIFIER(ModifierScrollLock, MODIFIER_SCROLLLOCK)
SET_MODIFIER(ModifierSymbol, MODIFIER_SYMBOL)
SET_MODIFIER(ModifierSymbolLock, MODIFIER_SYMBOLLOCK)
#undef SET_MODIFIER
return bits;
}
void UIEvent::InitModifiers(const EventModifierInit& aParam) {
if (NS_WARN_IF(!mEvent)) {
return;
}
WidgetInputEvent* inputEvent = mEvent->AsInputEvent();
MOZ_ASSERT(inputEvent,
"This method shouldn't be called if it doesn't have modifiers");
if (NS_WARN_IF(!inputEvent)) {
return;
}
inputEvent->mModifiers = ConvertToModifiers(aParam);
}
} // namespace mozilla::dom
using namespace mozilla;
using namespace mozilla::dom;
already_AddRefed<UIEvent> NS_NewDOMUIEvent(EventTarget* aOwner,
nsPresContext* aPresContext,
WidgetGUIEvent* aEvent) {
RefPtr<UIEvent> it = new UIEvent(aOwner, aPresContext, aEvent);
return it.forget();
}
|