1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
8 #include "mozilla/dom/Touch.h"
10 #include "nsThreadUtils.h"
11 #include "mozilla/MouseEvents.h"
12 #include "mozilla/TouchEvents.h"
18 already_AddRefed
<Touch
> SingleTouchData::ToNewDOMTouch() const
20 NS_ABORT_IF_FALSE(NS_IsMainThread(),
21 "Can only create dom::Touch instances on main thread");
22 nsRefPtr
<Touch
> touch
= new Touch(mIdentifier
,
23 nsIntPoint(mScreenPoint
.x
, mScreenPoint
.y
),
24 nsIntPoint(mRadius
.width
, mRadius
.height
),
27 return touch
.forget();
30 MultiTouchInput::MultiTouchInput(const WidgetTouchEvent
& aTouchEvent
)
31 : InputData(MULTITOUCH_INPUT
, aTouchEvent
.time
, aTouchEvent
.timeStamp
,
32 aTouchEvent
.modifiers
)
34 NS_ABORT_IF_FALSE(NS_IsMainThread(),
35 "Can only copy from WidgetTouchEvent on main thread");
37 switch (aTouchEvent
.message
) {
39 mType
= MULTITOUCH_START
;
42 mType
= MULTITOUCH_MOVE
;
45 mType
= MULTITOUCH_END
;
48 mType
= MULTITOUCH_CANCEL
;
51 MOZ_ASSERT_UNREACHABLE("Did not assign a type to a MultiTouchInput");
55 for (size_t i
= 0; i
< aTouchEvent
.touches
.Length(); i
++) {
56 const Touch
* domTouch
= aTouchEvent
.touches
[i
];
58 // Extract data from weird interfaces.
59 int32_t identifier
= domTouch
->Identifier();
60 int32_t radiusX
= domTouch
->RadiusX();
61 int32_t radiusY
= domTouch
->RadiusY();
62 float rotationAngle
= domTouch
->RotationAngle();
63 float force
= domTouch
->Force();
65 SingleTouchData
data(identifier
,
66 ScreenIntPoint::FromUnknownPoint(
67 gfx::IntPoint(domTouch
->mRefPoint
.x
,
68 domTouch
->mRefPoint
.y
)),
69 ScreenSize(radiusX
, radiusY
),
73 mTouches
.AppendElement(data
);
78 MultiTouchInput::ToWidgetTouchEvent(nsIWidget
* aWidget
) const
80 NS_ABORT_IF_FALSE(NS_IsMainThread(),
81 "Can only convert To WidgetTouchEvent on main thread");
83 uint32_t touchType
= NS_EVENT_NULL
;
85 case MULTITOUCH_START
:
86 touchType
= NS_TOUCH_START
;
89 touchType
= NS_TOUCH_MOVE
;
92 touchType
= NS_TOUCH_END
;
94 case MULTITOUCH_CANCEL
:
95 touchType
= NS_TOUCH_CANCEL
;
98 MOZ_ASSERT_UNREACHABLE("Did not assign a type to WidgetTouchEvent in MultiTouchInput");
102 WidgetTouchEvent
event(true, touchType
, aWidget
);
103 if (touchType
== NS_EVENT_NULL
) {
107 event
.modifiers
= this->modifiers
;
108 event
.time
= this->mTime
;
109 event
.timeStamp
= this->mTimeStamp
;
111 for (size_t i
= 0; i
< mTouches
.Length(); i
++) {
112 *event
.touches
.AppendElement() = mTouches
[i
].ToNewDOMTouch();
118 // This conversion from WidgetMouseEvent to MultiTouchInput is needed because on
119 // the B2G emulator we can only receive mouse events, but we need to be able
120 // to pan correctly. To do this, we convert the events into a format that the
121 // panning code can handle. This code is very limited and only supports
122 // SingleTouchData. It also sends garbage for the identifier, radius, force
123 // and rotation angle.
124 MultiTouchInput::MultiTouchInput(const WidgetMouseEvent
& aMouseEvent
)
125 : InputData(MULTITOUCH_INPUT
, aMouseEvent
.time
, aMouseEvent
.timeStamp
,
126 aMouseEvent
.modifiers
)
128 NS_ABORT_IF_FALSE(NS_IsMainThread(),
129 "Can only copy from WidgetMouseEvent on main thread");
130 switch (aMouseEvent
.message
) {
131 case NS_MOUSE_BUTTON_DOWN
:
132 mType
= MULTITOUCH_START
;
135 mType
= MULTITOUCH_MOVE
;
137 case NS_MOUSE_BUTTON_UP
:
138 mType
= MULTITOUCH_END
;
140 // The mouse pointer has been interrupted in an implementation-specific
141 // manner, such as a synchronous event or action cancelling the touch, or a
142 // touch point leaving the document window and going into a non-document
143 // area capable of handling user interactions.
145 mType
= MULTITOUCH_CANCEL
;
148 NS_WARNING("Did not assign a type to a MultiTouchInput");
152 mTouches
.AppendElement(SingleTouchData(0,
153 ScreenIntPoint::FromUnknownPoint(
154 gfx::IntPoint(aMouseEvent
.refPoint
.x
,
155 aMouseEvent
.refPoint
.y
)),