Bumping gaia.json for 2 gaia revision(s) a=gaia-bump
[gecko.git] / widget / InputData.h
blob6080ba8f6d380d946711bde03b57f8e07ca55886
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 #ifndef InputData_h__
7 #define InputData_h__
9 #include "nsDebug.h"
10 #include "nsPoint.h"
11 #include "nsTArray.h"
12 #include "Units.h"
13 #include "mozilla/EventForwards.h"
14 #include "mozilla/TimeStamp.h"
16 template<class E> struct already_AddRefed;
17 class nsIWidget;
19 namespace mozilla {
21 namespace dom {
22 class Touch;
25 enum InputType
27 MULTITOUCH_INPUT,
28 PANGESTURE_INPUT,
29 PINCHGESTURE_INPUT,
30 TAPGESTURE_INPUT
33 class MultiTouchInput;
34 class PanGestureInput;
35 class PinchGestureInput;
36 class TapGestureInput;
38 // This looks unnecessary now, but as we add more and more classes that derive
39 // from InputType (eventually probably almost as many as *Events.h has), it
40 // will be more and more clear what's going on with a macro that shortens the
41 // definition of the RTTI functions.
42 #define INPUTDATA_AS_CHILD_TYPE(type, enumID) \
43 const type& As##type() const \
44 { \
45 NS_ABORT_IF_FALSE(mInputType == enumID, "Invalid cast of InputData."); \
46 return (const type&) *this; \
47 } \
48 type& As##type() \
49 { \
50 NS_ABORT_IF_FALSE(mInputType == enumID, "Invalid cast of InputData."); \
51 return (type&) *this; \
54 /** Base input data class. Should never be instantiated. */
55 class InputData
57 public:
58 InputType mInputType;
59 // Time in milliseconds that this data is relevant to. This only really
60 // matters when this data is used as an event. We use uint32_t instead of
61 // TimeStamp because it is easier to convert from WidgetInputEvent. The time
62 // is platform-specific but it in the case of B2G and Fennec it is since
63 // startup.
64 uint32_t mTime;
65 // Set in parallel to mTime until we determine it is safe to drop
66 // platform-specific event times (see bug 77992).
67 TimeStamp mTimeStamp;
69 Modifiers modifiers;
71 INPUTDATA_AS_CHILD_TYPE(MultiTouchInput, MULTITOUCH_INPUT)
72 INPUTDATA_AS_CHILD_TYPE(PanGestureInput, PANGESTURE_INPUT)
73 INPUTDATA_AS_CHILD_TYPE(PinchGestureInput, PINCHGESTURE_INPUT)
74 INPUTDATA_AS_CHILD_TYPE(TapGestureInput, TAPGESTURE_INPUT)
76 InputData()
80 protected:
81 InputData(InputType aInputType, uint32_t aTime, TimeStamp aTimeStamp,
82 Modifiers aModifiers)
83 : mInputType(aInputType),
84 mTime(aTime),
85 mTimeStamp(aTimeStamp),
86 modifiers(aModifiers)
93 /**
94 * Data container for a single touch input. Similar to dom::Touch, but used in
95 * off-main-thread situations. This is more for just storing touch data, whereas
96 * dom::Touch is more useful for dispatching through the DOM (which can only
97 * happen on the main thread). dom::Touch also bears the problem of storing
98 * pointers to nsIWidget instances which can only be used on the main thread,
99 * so if instead we used dom::Touch and ever set these pointers
100 * off-main-thread, Bad Things Can Happen(tm).
102 * Note that this doesn't inherit from InputData because this itself is not an
103 * event. It is only a container/struct that should have any number of instances
104 * within a MultiTouchInput.
106 * fixme/bug 775746: Make dom::Touch inherit from this class.
108 class SingleTouchData
110 public:
111 SingleTouchData(int32_t aIdentifier,
112 ScreenIntPoint aScreenPoint,
113 ScreenSize aRadius,
114 float aRotationAngle,
115 float aForce)
116 : mIdentifier(aIdentifier),
117 mScreenPoint(aScreenPoint),
118 mRadius(aRadius),
119 mRotationAngle(aRotationAngle),
120 mForce(aForce)
124 SingleTouchData()
128 already_AddRefed<dom::Touch> ToNewDOMTouch() const;
130 // A unique number assigned to each SingleTouchData within a MultiTouchInput so
131 // that they can be easily distinguished when handling a touch start/move/end.
132 int32_t mIdentifier;
134 // Point on the screen that the touch hit, in device pixels. They are
135 // coordinates on the screen.
136 ScreenIntPoint mScreenPoint;
138 // Radius that the touch covers, i.e. if you're using your thumb it will
139 // probably be larger than using your pinky, even with the same force.
140 // Radius can be different along x and y. For example, if you press down with
141 // your entire finger vertically, the y radius will be much larger than the x
142 // radius.
143 ScreenSize mRadius;
145 float mRotationAngle;
147 // How hard the screen is being pressed.
148 float mForce;
152 * Similar to WidgetTouchEvent, but for use off-main-thread. Also only stores a
153 * screen touch point instead of the many different coordinate spaces
154 * WidgetTouchEvent stores its touch point in. This includes a way to initialize
155 * itself from a WidgetTouchEvent by copying all relevant data over. Note that
156 * this copying from WidgetTouchEvent functionality can only be used on the main
157 * thread.
159 * Stores an array of SingleTouchData.
161 class MultiTouchInput : public InputData
163 public:
164 enum MultiTouchType
166 MULTITOUCH_START,
167 MULTITOUCH_MOVE,
168 MULTITOUCH_END,
169 MULTITOUCH_CANCEL
172 MultiTouchInput(MultiTouchType aType, uint32_t aTime, TimeStamp aTimeStamp,
173 Modifiers aModifiers)
174 : InputData(MULTITOUCH_INPUT, aTime, aTimeStamp, aModifiers),
175 mType(aType)
179 MultiTouchInput()
183 MultiTouchInput(const MultiTouchInput& aOther)
184 : InputData(MULTITOUCH_INPUT, aOther.mTime,
185 aOther.mTimeStamp, aOther.modifiers)
186 , mType(aOther.mType)
188 mTouches.AppendElements(aOther.mTouches);
191 explicit MultiTouchInput(const WidgetTouchEvent& aTouchEvent);
192 WidgetTouchEvent ToWidgetTouchEvent(nsIWidget* aWidget) const;
194 // This conversion from WidgetMouseEvent to MultiTouchInput is needed because
195 // on the B2G emulator we can only receive mouse events, but we need to be
196 // able to pan correctly. To do this, we convert the events into a format that
197 // the panning code can handle. This code is very limited and only supports
198 // SingleTouchData. It also sends garbage for the identifier, radius, force
199 // and rotation angle.
200 explicit MultiTouchInput(const WidgetMouseEvent& aMouseEvent);
202 MultiTouchType mType;
203 nsTArray<SingleTouchData> mTouches;
207 * Encapsulation class for pan events, can be used off-main-thread.
208 * These events are currently only used for scrolling on desktop.
210 class PanGestureInput : public InputData
212 public:
213 enum PanGestureType
215 // MayStart: Dispatched before any actual panning has occurred but when a
216 // pan gesture is probably about to start, for example when the user
217 // starts touching the touchpad. Should interrupt any ongoing APZ
218 // animation and can be used to trigger scrollability indicators (e.g.
219 // flashing overlay scrollbars).
220 PANGESTURE_MAYSTART,
222 // Cancelled: Dispatched after MayStart when no pan gesture is going to
223 // happen after all, for example when the user lifts their fingers from a
224 // touchpad without having done any scrolling.
225 PANGESTURE_CANCELLED,
227 // Start: A pan gesture is starting.
228 // For devices that do not support the MayStart event type, this event can
229 // be used to interrupt ongoing APZ animations.
230 PANGESTURE_START,
232 // Pan: The actual pan motion by mPanDisplacement.
233 PANGESTURE_PAN,
235 // End: The pan gesture has ended, for example because the user has lifted
236 // their fingers from a touchpad after scrolling.
237 // Any potential momentum events fire after this event.
238 PANGESTURE_END,
240 // The following momentum event types are used in order to control the pan
241 // momentum animation. Using these instead of our own animation ensures
242 // that the animation curve is OS native and that the animation stops
243 // reliably if it is cancelled by the user.
245 // MomentumStart: Dispatched between the End event of the actual
246 // user-controlled pan, and the first MomentumPan event of the momentum
247 // animation.
248 PANGESTURE_MOMENTUMSTART,
250 // MomentumPan: The actual momentum motion by mPanDisplacement.
251 PANGESTURE_MOMENTUMPAN,
253 // MomentumEnd: The momentum animation has ended, for example because the
254 // momentum velocity has gone below the stopping threshold, or because the
255 // user has stopped the animation by putting their fingers on a touchpad.
256 PANGESTURE_MOMENTUMEND
259 PanGestureInput(PanGestureType aType,
260 uint32_t aTime,
261 TimeStamp aTimeStamp,
262 const ScreenPoint& aPanStartPoint,
263 const ScreenPoint& aPanDisplacement,
264 Modifiers aModifiers)
265 : InputData(PANGESTURE_INPUT, aTime, aTimeStamp, aModifiers),
266 mType(aType),
267 mPanStartPoint(aPanStartPoint),
268 mPanDisplacement(aPanDisplacement)
272 PanGestureType mType;
273 ScreenPoint mPanStartPoint;
275 // Only non-zero if mType is PANGESTURE_PAN or PANGESTURE_MOMENTUMPAN.
276 ScreenPoint mPanDisplacement;
280 * Encapsulation class for pinch events. In general, these will be generated by
281 * a gesture listener by looking at SingleTouchData/MultiTouchInput instances and
282 * determining whether or not the user was trying to do a gesture.
284 class PinchGestureInput : public InputData
286 public:
287 enum PinchGestureType
289 PINCHGESTURE_START,
290 PINCHGESTURE_SCALE,
291 PINCHGESTURE_END
294 PinchGestureInput(PinchGestureType aType,
295 uint32_t aTime,
296 TimeStamp aTimeStamp,
297 const ScreenPoint& aFocusPoint,
298 float aCurrentSpan,
299 float aPreviousSpan,
300 Modifiers aModifiers)
301 : InputData(PINCHGESTURE_INPUT, aTime, aTimeStamp, aModifiers),
302 mType(aType),
303 mFocusPoint(aFocusPoint),
304 mCurrentSpan(aCurrentSpan),
305 mPreviousSpan(aPreviousSpan)
311 PinchGestureType mType;
313 // Center point of the pinch gesture. That is, if there are two fingers on the
314 // screen, it is their midpoint. In the case of more than two fingers, the
315 // point is implementation-specific, but can for example be the midpoint
316 // between the very first and very last touch. This is in device pixels and
317 // are the coordinates on the screen of this midpoint.
318 ScreenPoint mFocusPoint;
320 // The distance in device pixels (though as a float for increased precision
321 // and because it is the distance along both the x and y axis) between the
322 // touches responsible for the pinch gesture.
323 float mCurrentSpan;
325 // The previous |mCurrentSpan| in the PinchGestureInput preceding this one.
326 // This is only really relevant during a PINCHGESTURE_SCALE because when it is
327 // of this type then there must have been a history of spans.
328 float mPreviousSpan;
332 * Encapsulation class for tap events. In general, these will be generated by
333 * a gesture listener by looking at SingleTouchData/MultiTouchInput instances and
334 * determining whether or not the user was trying to do a gesture.
336 class TapGestureInput : public InputData
338 public:
339 enum TapGestureType
341 TAPGESTURE_LONG,
342 TAPGESTURE_LONG_UP,
343 TAPGESTURE_UP,
344 TAPGESTURE_CONFIRMED,
345 TAPGESTURE_DOUBLE,
346 TAPGESTURE_CANCEL
349 TapGestureInput(TapGestureType aType,
350 uint32_t aTime,
351 TimeStamp aTimeStamp,
352 const ScreenIntPoint& aPoint,
353 Modifiers aModifiers)
354 : InputData(TAPGESTURE_INPUT, aTime, aTimeStamp, aModifiers),
355 mType(aType),
356 mPoint(aPoint)
362 TapGestureType mType;
363 ScreenIntPoint mPoint;
368 #endif // InputData_h__