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