Bug 1073336 part 5 - Add AnimationPlayerCollection::PlayerUpdated; r=dbaron
[gecko.git] / widget / InputData.cpp
blob06798b5c3153c8b04258285b700495252df7fd2a
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/. */
6 #include "InputData.h"
8 #include "mozilla/dom/Touch.h"
9 #include "nsDebug.h"
10 #include "nsThreadUtils.h"
11 #include "mozilla/MouseEvents.h"
12 #include "mozilla/TouchEvents.h"
14 namespace mozilla {
16 using namespace dom;
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),
25 mRotationAngle,
26 mForce);
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) {
38 case NS_TOUCH_START:
39 mType = MULTITOUCH_START;
40 break;
41 case NS_TOUCH_MOVE:
42 mType = MULTITOUCH_MOVE;
43 break;
44 case NS_TOUCH_END:
45 mType = MULTITOUCH_END;
46 break;
47 case NS_TOUCH_CANCEL:
48 mType = MULTITOUCH_CANCEL;
49 break;
50 default:
51 MOZ_ASSERT_UNREACHABLE("Did not assign a type to a MultiTouchInput");
52 break;
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),
70 rotationAngle,
71 force);
73 mTouches.AppendElement(data);
77 WidgetTouchEvent
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;
84 switch (mType) {
85 case MULTITOUCH_START:
86 touchType = NS_TOUCH_START;
87 break;
88 case MULTITOUCH_MOVE:
89 touchType = NS_TOUCH_MOVE;
90 break;
91 case MULTITOUCH_END:
92 touchType = NS_TOUCH_END;
93 break;
94 case MULTITOUCH_CANCEL:
95 touchType = NS_TOUCH_CANCEL;
96 break;
97 default:
98 MOZ_ASSERT_UNREACHABLE("Did not assign a type to WidgetTouchEvent in MultiTouchInput");
99 break;
102 WidgetTouchEvent event(true, touchType, aWidget);
103 if (touchType == NS_EVENT_NULL) {
104 return event;
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();
115 return event;
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;
133 break;
134 case NS_MOUSE_MOVE:
135 mType = MULTITOUCH_MOVE;
136 break;
137 case NS_MOUSE_BUTTON_UP:
138 mType = MULTITOUCH_END;
139 break;
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.
144 case NS_MOUSE_EXIT:
145 mType = MULTITOUCH_CANCEL;
146 break;
147 default:
148 NS_WARNING("Did not assign a type to a MultiTouchInput");
149 break;
152 mTouches.AppendElement(SingleTouchData(0,
153 ScreenIntPoint::FromUnknownPoint(
154 gfx::IntPoint(aMouseEvent.refPoint.x,
155 aMouseEvent.refPoint.y)),
156 ScreenSize(1, 1),
157 180.0f,
158 1.0f));