Bug 867104 - Add a crashtest. r=ehsan
[gecko.git] / widget / InputData.h
blobd103f2a187e3060d4f2b902d2538da1c2ba59c4c
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"
13 class nsTouchEvent;
14 class nsMouseEvent;
15 namespace mozilla {
18 enum InputType
20 MULTITOUCH_INPUT,
21 PINCHGESTURE_INPUT,
22 TAPGESTURE_INPUT
25 class MultiTouchInput;
26 class PinchGestureInput;
27 class TapGestureInput;
29 // This looks unnecessary now, but as we add more and more classes that derive
30 // from InputType (eventually probably almost as many as nsGUIEvent.h has), it
31 // will be more and more clear what's going on with a macro that shortens the
32 // definition of the RTTI functions.
33 #define INPUTDATA_AS_CHILD_TYPE(type, enumID) \
34 const type& As##type() const \
35 { \
36 NS_ABORT_IF_FALSE(mInputType == enumID, "Invalid cast of InputData."); \
37 return (const type&) *this; \
40 /** Base input data class. Should never be instantiated. */
41 class InputData
43 public:
44 InputType mInputType;
45 // Time in milliseconds that this data is relevant to. This only really
46 // matters when this data is used as an event. We use uint32_t instead of
47 // TimeStamp because it is easier to convert from nsInputEvent. The time is
48 // platform-specific but it in the case of B2G and Fennec it is since startup.
49 uint32_t mTime;
51 INPUTDATA_AS_CHILD_TYPE(MultiTouchInput, MULTITOUCH_INPUT)
52 INPUTDATA_AS_CHILD_TYPE(PinchGestureInput, PINCHGESTURE_INPUT)
53 INPUTDATA_AS_CHILD_TYPE(TapGestureInput, TAPGESTURE_INPUT)
55 InputData()
59 protected:
60 InputData(InputType aInputType, uint32_t aTime)
61 : mInputType(aInputType),
62 mTime(aTime)
69 /**
70 * Data container for a single touch input. Similar to dom::Touch, but used in
71 * off-main-thread situations. This is more for just storing touch data, whereas
72 * dom::Touch derives from nsIDOMTouch so it is more useful for dispatching
73 * through the DOM (which can only happen on the main thread). dom::Touch also
74 * bears the problem of storing pointers to nsIWidget instances which can only
75 * be used on the main thread, so if instead we used dom::Touch and ever set
76 * these pointers off-main-thread, Bad Things Can Happen(tm).
78 * Note that this doesn't inherit from InputData because this itself is not an
79 * event. It is only a container/struct that should have any number of instances
80 * within a MultiTouchInput.
82 * fixme/bug 775746: Make dom::Touch inherit from this class.
84 class SingleTouchData
86 public:
87 SingleTouchData(int32_t aIdentifier,
88 nsIntPoint aScreenPoint,
89 nsIntPoint aRadius,
90 float aRotationAngle,
91 float aForce)
92 : mIdentifier(aIdentifier),
93 mScreenPoint(aScreenPoint),
94 mRadius(aRadius),
95 mRotationAngle(aRotationAngle),
96 mForce(aForce)
102 SingleTouchData()
106 // A unique number assigned to each SingleTouchData within a MultiTouchInput so
107 // that they can be easily distinguished when handling a touch start/move/end.
108 int32_t mIdentifier;
110 // Point on the screen that the touch hit, in device pixels. They are
111 // coordinates on the screen.
112 nsIntPoint mScreenPoint;
114 // Radius that the touch covers, i.e. if you're using your thumb it will
115 // probably be larger than using your pinky, even with the same force.
116 // Radius can be different along x and y. For example, if you press down with
117 // your entire finger vertically, the y radius will be much larger than the x
118 // radius.
119 nsIntPoint mRadius;
121 float mRotationAngle;
123 // How hard the screen is being pressed.
124 float mForce;
128 * Similar to nsTouchEvent, but for use off-main-thread. Also only stores a
129 * screen touch point instead of the many different coordinate spaces nsTouchEvent
130 * stores its touch point in. This includes a way to initialize itself from an
131 * nsTouchEvent by copying all relevant data over. Note that this copying from
132 * nsTouchEvent functionality can only be used on the main thread.
134 * Stores an array of SingleTouchData.
136 class MultiTouchInput : public InputData
138 public:
139 enum MultiTouchType
141 MULTITOUCH_START,
142 MULTITOUCH_MOVE,
143 MULTITOUCH_END,
144 MULTITOUCH_ENTER,
145 MULTITOUCH_LEAVE,
146 MULTITOUCH_CANCEL
149 MultiTouchInput(MultiTouchType aType, uint32_t aTime)
150 : InputData(MULTITOUCH_INPUT, aTime),
151 mType(aType)
157 MultiTouchInput()
161 MultiTouchInput(const nsTouchEvent& aTouchEvent);
163 // This conversion from nsMouseEvent to MultiTouchInput is needed because on
164 // the B2G emulator we can only receive mouse events, but we need to be able
165 // to pan correctly. To do this, we convert the events into a format that the
166 // panning code can handle. This code is very limited and only supports
167 // SingleTouchData. It also sends garbage for the identifier, radius, force
168 // and rotation angle.
169 MultiTouchInput(const nsMouseEvent& aMouseEvent);
171 MultiTouchType mType;
172 nsTArray<SingleTouchData> mTouches;
176 * Encapsulation class for pinch events. In general, these will be generated by
177 * a gesture listener by looking at SingleTouchData/MultiTouchInput instances and
178 * determining whether or not the user was trying to do a gesture.
180 class PinchGestureInput : public InputData
182 public:
183 enum PinchGestureType
185 PINCHGESTURE_START,
186 PINCHGESTURE_SCALE,
187 PINCHGESTURE_END
190 PinchGestureInput(PinchGestureType aType,
191 uint32_t aTime,
192 const nsIntPoint& aFocusPoint,
193 float aCurrentSpan,
194 float aPreviousSpan)
195 : InputData(PINCHGESTURE_INPUT, aTime),
196 mType(aType),
197 mFocusPoint(aFocusPoint),
198 mCurrentSpan(aCurrentSpan),
199 mPreviousSpan(aPreviousSpan)
205 PinchGestureType mType;
207 // Center point of the pinch gesture. That is, if there are two fingers on the
208 // screen, it is their midpoint. In the case of more than two fingers, the
209 // point is implementation-specific, but can for example be the midpoint
210 // between the very first and very last touch. This is in device pixels and
211 // are the coordinates on the screen of this midpoint.
212 nsIntPoint mFocusPoint;
214 // The distance in device pixels (though as a float for increased precision
215 // and because it is the distance along both the x and y axis) between the
216 // touches responsible for the pinch gesture.
217 float mCurrentSpan;
219 // The previous |mCurrentSpan| in the PinchGestureInput preceding this one.
220 // This is only really relevant during a PINCHGESTURE_SCALE because when it is
221 // of this type then there must have been a history of spans.
222 float mPreviousSpan;
226 * Encapsulation class for tap events. In general, these will be generated by
227 * a gesture listener by looking at SingleTouchData/MultiTouchInput instances and
228 * determining whether or not the user was trying to do a gesture.
230 class TapGestureInput : public InputData
232 public:
233 enum TapGestureType
235 TAPGESTURE_LONG,
236 TAPGESTURE_UP,
237 TAPGESTURE_CONFIRMED,
238 TAPGESTURE_DOUBLE,
239 TAPGESTURE_CANCEL
242 TapGestureInput(TapGestureType aType, uint32_t aTime, const nsIntPoint& aPoint)
243 : InputData(TAPGESTURE_INPUT, aTime),
244 mType(aType),
245 mPoint(aPoint)
251 TapGestureType mType;
252 nsIntPoint mPoint;
257 #endif // InputData_h__