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