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