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