OLD | NEW |
| (Empty) |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef UI_EVENTS_X_DEVICE_DATA_MANAGER_H_ | |
6 #define UI_EVENTS_X_DEVICE_DATA_MANAGER_H_ | |
7 | |
8 // Generically-named #defines from Xlib is conflicting with symbols in GTest. | |
9 // So many tests .cc file #undef Bool before including device_data_manager.h, | |
10 // which makes Bool unrecognized in XInput2.h. | |
11 #ifndef Bool | |
12 #define Bool int | |
13 #endif | |
14 | |
15 #include <X11/extensions/XInput2.h> | |
16 | |
17 #include <bitset> | |
18 #include <map> | |
19 #include <vector> | |
20 | |
21 #include "base/basictypes.h" | |
22 #include "base/event_types.h" | |
23 #include "base/memory/scoped_ptr.h" | |
24 #include "ui/events/event.h" | |
25 #include "ui/events/event_constants.h" | |
26 #include "ui/events/events_base_export.h" | |
27 #include "ui/gfx/geometry/rect.h" | |
28 #include "ui/gfx/transform.h" | |
29 #include "ui/gfx/x/x11_atom_cache.h" | |
30 | |
31 template <typename T> struct DefaultSingletonTraits; | |
32 | |
33 typedef union _XEvent XEvent; | |
34 | |
35 namespace ui { | |
36 | |
37 // CrOS touchpad metrics gesture types | |
38 enum GestureMetricsType { | |
39 kGestureMetricsTypeNoisyGround = 0, | |
40 kGestureMetricsTypeUnknown, | |
41 }; | |
42 | |
43 // A class that extracts and tracks the input events data. It currently handles | |
44 // mouse, touchpad and touchscreen devices. | |
45 class EVENTS_BASE_EXPORT DeviceDataManager { | |
46 public: | |
47 // Enumerate additional data that one might be interested on an input event, | |
48 // which are usually wrapped in X valuators. If you modify any of this, | |
49 // make sure to update the kCachedAtoms data structure in the source file | |
50 // and the k*Type[Start/End] constants used by IsCMTDataType and | |
51 // IsTouchDataType. | |
52 enum DataType { | |
53 // Define the valuators used the CrOS CMT driver. Used by mice and CrOS | |
54 // touchpads. | |
55 DT_CMT_SCROLL_X = 0, // Scroll amount on the X (horizontal) direction. | |
56 DT_CMT_SCROLL_Y, // Scroll amount on the Y (vertical) direction. | |
57 DT_CMT_ORDINAL_X, // Original (unaccelerated) value on the X direction. | |
58 // Can be used both for scrolls and flings. | |
59 DT_CMT_ORDINAL_Y, // Original (unaccelerated) value on the Y direction. | |
60 // Can be used both for scrolls and flings. | |
61 DT_CMT_START_TIME, // Gesture start time. | |
62 DT_CMT_END_TIME, // Gesture end time. | |
63 DT_CMT_FLING_X, // Fling amount on the X (horizontal) direction. | |
64 DT_CMT_FLING_Y, // Fling amount on the Y (vertical) direction. | |
65 DT_CMT_FLING_STATE, // The state of fling gesture (whether the user just | |
66 // start flinging or that he/she taps down). | |
67 DT_CMT_METRICS_TYPE, // Metrics type of the metrics gesture, which are | |
68 // used to wrap interesting patterns that we would | |
69 // like to track via the UMA system. | |
70 DT_CMT_METRICS_DATA1, // Complementary data 1 of the metrics gesture. | |
71 DT_CMT_METRICS_DATA2, // Complementary data 2 of the metrics gesture. | |
72 DT_CMT_FINGER_COUNT, // Finger counts in the current gesture. A same type | |
73 // of gesture can have very different meanings based | |
74 // on that (e.g. 2f scroll v.s. 3f swipe). | |
75 | |
76 // End of CMT data types. | |
77 // Beginning of touch data types. | |
78 | |
79 // Define the valuators following the Multi-touch Protocol. Used by | |
80 // touchscreen devices. | |
81 DT_TOUCH_MAJOR, // Length of the touch area. | |
82 DT_TOUCH_MINOR, // Width of the touch area. | |
83 DT_TOUCH_ORIENTATION, // Angle between the X-axis and the major axis of the | |
84 // touch area. | |
85 DT_TOUCH_PRESSURE, // Pressure of the touch contact. | |
86 | |
87 // NOTE for XInput MT: 'Tracking ID' is provided in every touch event to | |
88 // track individual touch. 'Tracking ID' is an unsigned 32-bit value and | |
89 // is increased for each new touch. It will wrap back to 0 when reaching | |
90 // the numerical limit. | |
91 DT_TOUCH_TRACKING_ID, // ID of the touch point. | |
92 | |
93 // Kernel timestamp from touch screen (if available). | |
94 DT_TOUCH_RAW_TIMESTAMP, | |
95 | |
96 // End of touch data types. | |
97 | |
98 DT_LAST_ENTRY // This must come last. | |
99 }; | |
100 | |
101 // Data struct to store extracted data from an input event. | |
102 typedef std::map<int, double> EventData; | |
103 | |
104 // We use int because enums can be casted to ints but not vice versa. | |
105 static bool IsCMTDataType(const int type); | |
106 static bool IsTouchDataType(const int type); | |
107 | |
108 // Returns the DeviceDataManager singleton. | |
109 static DeviceDataManager* GetInstance(); | |
110 | |
111 // Returns if XInput2 is available on the system. | |
112 bool IsXInput2Available() const; | |
113 | |
114 // Updates the list of devices. | |
115 void UpdateDeviceList(Display* display); | |
116 | |
117 // For multitouch events we use slot number to distinguish touches from | |
118 // different fingers. This function returns true if the associated slot | |
119 // for |xiev| can be found and it is saved in |slot|, returns false if | |
120 // no slot can be found. | |
121 bool GetSlotNumber(const XIDeviceEvent* xiev, int* slot); | |
122 | |
123 // Get all event data in one pass. We extract only data types that we know | |
124 // about (defined in enum DataType). The data is not processed (e.g. not | |
125 // filled in by cached values) as in GetEventData. | |
126 void GetEventRawData(const XEvent& xev, EventData* data); | |
127 | |
128 // Get a datum of the specified type. Return true and the value | |
129 // is updated if the data is found, false and value unchanged if the data is | |
130 // not found. In the case of MT-B/XI2.2, the value can come from a previously | |
131 // cached one (see the comment above last_seen_valuator_). | |
132 bool GetEventData(const XEvent& xev, const DataType type, double* value); | |
133 | |
134 // Check if the event is an XI input event in the strict sense | |
135 // (i.e. XIDeviceEvent). This rules out things like hierarchy changes, | |
136 /// device changes, property changes and so on. | |
137 bool IsXIDeviceEvent(const base::NativeEvent& native_event) const; | |
138 | |
139 // Check if the event comes from touchpad devices. | |
140 bool IsTouchpadXInputEvent(const base::NativeEvent& native_event) const; | |
141 | |
142 // Check if the event comes from devices running CMT driver or using | |
143 // CMT valuators (e.g. mouses). Note that doesn't necessarily mean the event | |
144 // is a CMT event (e.g. it could be a mouse pointer move). | |
145 bool IsCMTDeviceEvent(const base::NativeEvent& native_event) const; | |
146 | |
147 // Check if the event is one of the CMT gesture events (scroll, fling, | |
148 // metrics etc.). | |
149 bool IsCMTGestureEvent(const base::NativeEvent& native_event) const; | |
150 | |
151 // Returns true if the event is of the specific type, false if not. | |
152 bool IsScrollEvent(const base::NativeEvent& native_event) const; | |
153 bool IsFlingEvent(const base::NativeEvent& native_event) const; | |
154 bool IsCMTMetricsEvent(const base::NativeEvent& native_event) const; | |
155 | |
156 // Returns true if the event has CMT start/end timestamps. | |
157 bool HasGestureTimes(const base::NativeEvent& native_event) const; | |
158 | |
159 // Extract data from a scroll event (a motion event with the necessary | |
160 // valuators). User must first verify the event type with IsScrollEvent. | |
161 // Pointers shouldn't be NULL. | |
162 void GetScrollOffsets(const base::NativeEvent& native_event, | |
163 float* x_offset, | |
164 float* y_offset, | |
165 float* x_offset_ordinal, | |
166 float* y_offset_ordinal, | |
167 int* finger_count); | |
168 | |
169 // Extract data from a fling event. User must first verify the event type | |
170 // with IsFlingEvent. Pointers shouldn't be NULL. | |
171 void GetFlingData(const base::NativeEvent& native_event, | |
172 float* vx, | |
173 float* vy, | |
174 float* vx_ordinal, | |
175 float* vy_ordinal, | |
176 bool* is_cancel); | |
177 | |
178 // Extract data from a CrOS metrics gesture event. User must first verify | |
179 // the event type with IsCMTMetricsEvent. Pointers shouldn't be NULL. | |
180 void GetMetricsData(const base::NativeEvent& native_event, | |
181 GestureMetricsType* type, | |
182 float* data1, | |
183 float* data2); | |
184 | |
185 // Returns the mapped button. | |
186 int GetMappedButton(int button); | |
187 | |
188 // Updates button mapping. This is usually called when a MappingNotify event | |
189 // is received. | |
190 void UpdateButtonMap(); | |
191 | |
192 // Extract the start/end timestamps from CMT events. User must first verify | |
193 // the event with HasGestureTimes. Pointers shouldn't be NULL. | |
194 void GetGestureTimes(const base::NativeEvent& native_event, | |
195 double* start_time, | |
196 double* end_time); | |
197 | |
198 // Normalize the data value on deviceid to fall into [0, 1]. | |
199 // *value = (*value - min_value_of_tp) / (max_value_of_tp - min_value_of_tp) | |
200 // Returns true and sets the normalized value in|value| if normalization is | |
201 // successful. Returns false and |value| is unchanged otherwise. | |
202 bool NormalizeData(unsigned int deviceid, | |
203 const DataType type, | |
204 double* value); | |
205 | |
206 // Extract the range of the data type. Return true if the range is available | |
207 // and written into min & max, false if the range is not available. | |
208 bool GetDataRange(unsigned int deviceid, | |
209 const DataType type, | |
210 double* min, | |
211 double* max); | |
212 | |
213 // Sets up relevant valuator informations for device ids in the device lists. | |
214 // This function is only for test purpose. It does not query the X server for | |
215 // the actual device info, but rather inits the relevant valuator structures | |
216 // to have safe default values for testing. | |
217 void SetDeviceListForTest(const std::vector<unsigned int>& touchscreen, | |
218 const std::vector<unsigned int>& cmt_devices); | |
219 | |
220 void SetValuatorDataForTest(XIDeviceEvent* xievent, | |
221 DataType type, | |
222 double value); | |
223 | |
224 void ClearTouchTransformerRecord(); | |
225 void UpdateTouchInfoForDisplay(int64 display_id, | |
226 int touch_device_id, | |
227 const gfx::Transform& touch_transformer); | |
228 void ApplyTouchTransformer(int touch_device_id, float* x, float* y); | |
229 int64 GetDisplayForTouchDevice(int touch_device_id) const; | |
230 bool TouchEventNeedsCalibrate(int touch_device_id) const; | |
231 | |
232 private: | |
233 // Requirement for Singleton. | |
234 friend struct DefaultSingletonTraits<DeviceDataManager>; | |
235 | |
236 DeviceDataManager(); | |
237 ~DeviceDataManager(); | |
238 | |
239 // Initialize the XInput related system information. | |
240 bool InitializeXInputInternal(); | |
241 | |
242 // Check if an XI event contains data of the specified type. | |
243 bool HasEventData(const XIDeviceEvent* xiev, const DataType type) const; | |
244 | |
245 void InitializeValuatorsForTest(int deviceid, | |
246 int start_valuator, | |
247 int end_valuator, | |
248 double min_value, | |
249 double max_value); | |
250 | |
251 bool IsTouchDeviceIdValid(int touch_device_id) const; | |
252 | |
253 static const int kMaxDeviceNum = 128; | |
254 static const int kMaxXIEventType = XI_LASTEVENT + 1; | |
255 static const int kMaxSlotNum = 10; | |
256 | |
257 // Major opcode for the XInput extension. Used to identify XInput events. | |
258 int xi_opcode_; | |
259 | |
260 // A quick lookup table for determining if the XI event is an XIDeviceEvent. | |
261 std::bitset<kMaxXIEventType> xi_device_event_types_; | |
262 | |
263 // A quick lookup table for determining if events from the pointer device | |
264 // should be processed. | |
265 std::bitset<kMaxDeviceNum> cmt_devices_; | |
266 std::bitset<kMaxDeviceNum> touchpads_; | |
267 | |
268 // Number of valuators on the specific device. | |
269 int valuator_count_[kMaxDeviceNum]; | |
270 | |
271 // Index table to find the valuator for DataType on the specific device | |
272 // by valuator_lookup_[device_id][data_type]. | |
273 std::vector<int> valuator_lookup_[kMaxDeviceNum]; | |
274 | |
275 // Index table to find the DataType for valuator on the specific device | |
276 // by data_type_lookup_[device_id][valuator]. | |
277 std::vector<int> data_type_lookup_[kMaxDeviceNum]; | |
278 | |
279 // Index table to find the min & max value of the Valuator on a specific | |
280 // device. | |
281 std::vector<double> valuator_min_[kMaxDeviceNum]; | |
282 std::vector<double> valuator_max_[kMaxDeviceNum]; | |
283 | |
284 // Table to keep track of the last seen value for the specified valuator for | |
285 // a specified slot of a device. Defaults to 0 if the valuator for that slot | |
286 // was not specified in an earlier event. With MT-B/XI2.2, valuators in an | |
287 // XEvent are not reported if the values haven't changed from the previous | |
288 // event. So it is necessary to remember these valuators so that chrome | |
289 // doesn't think X/device doesn't know about the valuators. We currently | |
290 // use this only on touchscreen devices. | |
291 std::vector<double> last_seen_valuator_[kMaxDeviceNum][kMaxSlotNum]; | |
292 | |
293 // X11 atoms cache. | |
294 X11AtomCache atom_cache_; | |
295 | |
296 unsigned char button_map_[256]; | |
297 int button_map_count_; | |
298 | |
299 // Table to keep track of which display id is mapped to which touch device. | |
300 int64 touch_device_to_display_map_[kMaxDeviceNum]; | |
301 // Index table to find the TouchTransformer for a touch device. | |
302 gfx::Transform touch_device_transformer_map_[kMaxDeviceNum]; | |
303 | |
304 DISALLOW_COPY_AND_ASSIGN(DeviceDataManager); | |
305 }; | |
306 | |
307 } // namespace ui | |
308 | |
309 #endif // UI_EVENTS_X_DEVICE_DATA_MANAGER_H_ | |
OLD | NEW |