| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef UI_EVENTS_PLATFORM_X11_DEVICE_DATA_MANAGER_X11_H_ | 5 #ifndef UI_EVENTS_DEVICES_X11_DEVICE_DATA_MANAGER_X11_H_ |
| 6 #define UI_EVENTS_PLATFORM_X11_DEVICE_DATA_MANAGER_X11_H_ | 6 #define UI_EVENTS_DEVICES_X11_DEVICE_DATA_MANAGER_X11_H_ |
| 7 | 7 |
| 8 // Generically-named #defines from Xlib is conflicting with symbols in GTest. | 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, | 9 // So many tests .cc file #undef Bool before including device_data_manager.h, |
| 10 // which makes Bool unrecognized in XInput2.h. | 10 // which makes Bool unrecognized in XInput2.h. |
| 11 #ifndef Bool | 11 #ifndef Bool |
| 12 #define Bool int | 12 #define Bool int |
| 13 #endif | 13 #endif |
| 14 | 14 |
| 15 #include <X11/extensions/XInput2.h> | 15 #include <X11/extensions/XInput2.h> |
| 16 | 16 |
| 17 #include <bitset> | 17 #include <bitset> |
| 18 #include <functional> |
| 18 #include <map> | 19 #include <map> |
| 19 #include <set> | 20 #include <set> |
| 20 #include <vector> | 21 #include <vector> |
| 21 | 22 |
| 22 #include "base/basictypes.h" | 23 #include "base/basictypes.h" |
| 23 #include "base/event_types.h" | 24 #include "base/event_types.h" |
| 24 #include "base/memory/scoped_ptr.h" | 25 #include "base/memory/scoped_ptr.h" |
| 25 #include "ui/events/device_data_manager.h" | 26 #include "ui/events/devices/device_data_manager.h" |
| 27 #include "ui/events/devices/events_devices_export.h" |
| 26 #include "ui/events/event_constants.h" | 28 #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/events/keycodes/keyboard_codes.h" |
| 29 #include "ui/gfx/geometry/rect.h" | 30 #include "ui/gfx/geometry/rect.h" |
| 30 #include "ui/gfx/x/x11_atom_cache.h" | 31 #include "ui/gfx/x/x11_atom_cache.h" |
| 31 | 32 |
| 32 typedef union _XEvent XEvent; | 33 typedef union _XEvent XEvent; |
| 33 | 34 |
| 34 namespace ui { | 35 namespace ui { |
| 35 | 36 |
| 36 // CrOS touchpad metrics gesture types | 37 // CrOS touchpad metrics gesture types |
| 37 enum GestureMetricsType { | 38 enum GestureMetricsType { |
| 38 kGestureMetricsTypeNoisyGround = 0, | 39 kGestureMetricsTypeNoisyGround = 0, |
| 39 kGestureMetricsTypeUnknown, | 40 kGestureMetricsTypeUnknown, |
| 40 }; | 41 }; |
| 41 | 42 |
| 42 // A class that extracts and tracks the input events data. It currently handles | 43 // A class that extracts and tracks the input events data. It currently handles |
| 43 // mouse, touchpad and touchscreen devices. | 44 // mouse, touchpad and touchscreen devices. |
| 44 class EVENTS_BASE_EXPORT DeviceDataManagerX11 : public DeviceDataManager { | 45 class EVENTS_DEVICES_EXPORT DeviceDataManagerX11 : public DeviceDataManager { |
| 45 public: | 46 public: |
| 46 // Enumerate additional data that one might be interested on an input event, | 47 // 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 // 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 // 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 // and the k*Type[Start/End] constants used by IsCMTDataType and |
| 50 // IsTouchDataType. | 51 // IsTouchDataType. |
| 51 enum DataType { | 52 enum DataType { |
| 52 // Define the valuators used the CrOS CMT driver. Used by mice and CrOS | 53 // Define the valuators used the CrOS CMT driver. Used by mice and CrOS |
| 53 // touchpads. | 54 // touchpads. |
| 54 DT_CMT_SCROLL_X = 0, // Scroll amount on the X (horizontal) direction. | 55 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_SCROLL_Y, // Scroll amount on the Y (vertical) direction. |
| 56 DT_CMT_ORDINAL_X, // Original (unaccelerated) value on the X direction. | 57 DT_CMT_ORDINAL_X, // Original (unaccelerated) value on the X direction. |
| 57 // Can be used both for scrolls and flings. | 58 // Can be used both for scrolls and flings. |
| 58 DT_CMT_ORDINAL_Y, // Original (unaccelerated) value on the Y direction. | 59 DT_CMT_ORDINAL_Y, // Original (unaccelerated) value on the Y direction. |
| 59 // Can be used both for scrolls and flings. | 60 // Can be used both for scrolls and flings. |
| 60 DT_CMT_START_TIME, // Gesture start time. | 61 DT_CMT_START_TIME, // Gesture start time. |
| 61 DT_CMT_END_TIME, // Gesture end time. | 62 DT_CMT_END_TIME, // Gesture end time. |
| 62 DT_CMT_FLING_X, // Fling amount on the X (horizontal) direction. | 63 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_Y, // Fling amount on the Y (vertical) direction. |
| 64 DT_CMT_FLING_STATE, // The state of fling gesture (whether the user just | 65 DT_CMT_FLING_STATE, // The state of fling gesture (whether the user just |
| 65 // start flinging or that he/she taps down). | 66 // started flinging or they tapped down). |
| 66 DT_CMT_METRICS_TYPE, // Metrics type of the metrics gesture, which are | 67 DT_CMT_METRICS_TYPE, // Metrics type of the metrics gesture, which are |
| 67 // used to wrap interesting patterns that we would | 68 // used to wrap interesting patterns that we would |
| 68 // like to track via the UMA system. | 69 // like to track via the UMA system. |
| 69 DT_CMT_METRICS_DATA1, // Complementary data 1 of the metrics gesture. | 70 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_METRICS_DATA2, // Complementary data 2 of the metrics gesture. |
| 71 DT_CMT_FINGER_COUNT, // Finger counts in the current gesture. A same type | 72 DT_CMT_FINGER_COUNT, // Finger counts in the current gesture. A same type |
| 72 // of gesture can have very different meanings based | 73 // of gesture can have very different meanings based |
| 73 // on that (e.g. 2f scroll v.s. 3f swipe). | 74 // on that (e.g. 2f scroll v.s. 3f swipe). |
| 74 | 75 |
| 75 // End of CMT data types. | 76 // End of CMT data types. |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 117 | 118 |
| 118 // Updates the list of devices. | 119 // Updates the list of devices. |
| 119 void UpdateDeviceList(Display* display); | 120 void UpdateDeviceList(Display* display); |
| 120 | 121 |
| 121 // For multitouch events we use slot number to distinguish touches from | 122 // For multitouch events we use slot number to distinguish touches from |
| 122 // different fingers. This function returns true if the associated slot | 123 // 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 // for |xiev| can be found and it is saved in |slot|, returns false if |
| 124 // no slot can be found. | 125 // no slot can be found. |
| 125 bool GetSlotNumber(const XIDeviceEvent* xiev, int* slot); | 126 bool GetSlotNumber(const XIDeviceEvent* xiev, int* slot); |
| 126 | 127 |
| 128 // Check if an XI event contains data of the specified type. |
| 129 bool HasEventData(const XIDeviceEvent* xiev, const DataType type) const; |
| 130 |
| 127 // Get all event data in one pass. We extract only data types that we know | 131 // 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 | 132 // about (defined in enum DataType). The data is not processed (e.g. not |
| 129 // filled in by cached values) as in GetEventData. | 133 // filled in by cached values) as in GetEventData. |
| 130 void GetEventRawData(const XEvent& xev, EventData* data); | 134 void GetEventRawData(const XEvent& xev, EventData* data); |
| 131 | 135 |
| 132 // Get a datum of the specified type. Return true and the value | 136 // 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 | 137 // 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 | 138 // 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_). | 139 // cached one (see the comment above last_seen_valuator_). |
| 136 bool GetEventData(const XEvent& xev, const DataType type, double* value); | 140 bool GetEventData(const XEvent& xev, const DataType type, double* value); |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 196 // Extract the start/end timestamps from CMT events. User must first verify | 200 // Extract the start/end timestamps from CMT events. User must first verify |
| 197 // the event with HasGestureTimes. Pointers shouldn't be NULL. | 201 // the event with HasGestureTimes. Pointers shouldn't be NULL. |
| 198 void GetGestureTimes(const base::NativeEvent& native_event, | 202 void GetGestureTimes(const base::NativeEvent& native_event, |
| 199 double* start_time, | 203 double* start_time, |
| 200 double* end_time); | 204 double* end_time); |
| 201 | 205 |
| 202 // Normalize the data value on deviceid to fall into [0, 1]. | 206 // 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) | 207 // *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 | 208 // Returns true and sets the normalized value in|value| if normalization is |
| 205 // successful. Returns false and |value| is unchanged otherwise. | 209 // successful. Returns false and |value| is unchanged otherwise. |
| 206 bool NormalizeData(unsigned int deviceid, const DataType type, double* value); | 210 bool NormalizeData(int deviceid, |
| 211 const DataType type, |
| 212 double* value); |
| 207 | 213 |
| 208 // Extract the range of the data type. Return true if the range is available | 214 // Extract the range of the data type. Return true if the range is available |
| 209 // and written into min & max, false if the range is not available. | 215 // and written into min & max, false if the range is not available. |
| 210 bool GetDataRange(unsigned int deviceid, | 216 bool GetDataRange(int deviceid, |
| 211 const DataType type, | 217 const DataType type, |
| 212 double* min, | 218 double* min, |
| 213 double* max); | 219 double* max); |
| 214 | 220 |
| 215 // Sets up relevant valuator informations for device ids in the device lists. | 221 // Sets up relevant valuator informations for device ids in the device lists. |
| 216 // This function is only for test purpose. It does not query the X server for | 222 // This function is only for test purpose. It does not query the X server for |
| 217 // the actual device info, but rather inits the relevant valuator structures | 223 // the actual device info, but rather inits the relevant valuator structures |
| 218 // to have safe default values for testing. | 224 // to have safe default values for testing. |
| 219 void SetDeviceListForTest(const std::vector<unsigned int>& touchscreen, | 225 void SetDeviceListForTest(const std::vector<int>& touchscreen, |
| 220 const std::vector<unsigned int>& cmt_devices); | 226 const std::vector<int>& cmt_devices, |
| 227 const std::vector<int>& other_devices); |
| 221 | 228 |
| 222 void SetValuatorDataForTest(XIDeviceEvent* xievent, | 229 void SetValuatorDataForTest(XIDeviceEvent* xievent, |
| 223 DataType type, | 230 DataType type, |
| 224 double value); | 231 double value); |
| 225 | 232 |
| 226 bool TouchEventNeedsCalibrate(int touch_device_id) const; | 233 bool TouchEventNeedsCalibrate(int touch_device_id) const; |
| 227 | 234 |
| 228 // Sets the keys which are still allowed on a disabled keyboard device. | 235 // Sets the keys which are still allowed on a disabled keyboard device. |
| 229 void SetDisabledKeyboardAllowedKeys( | 236 void SetDisabledKeyboardAllowedKeys( |
| 230 scoped_ptr<std::set<KeyboardCode>> excepted_keys); | 237 scoped_ptr<std::set<KeyboardCode> > excepted_keys); |
| 231 | 238 |
| 232 // Disables and enables events from devices by device id. | 239 // Disables and enables events from devices by device id. |
| 233 void DisableDevice(unsigned int deviceid); | 240 void DisableDevice(int deviceid); |
| 234 void EnableDevice(unsigned int deviceid); | 241 void EnableDevice(int deviceid); |
| 235 | 242 |
| 236 // Returns true if |native_event| should be blocked. | 243 // Returns true if |native_event| should be blocked. |
| 237 bool IsEventBlocked(const base::NativeEvent& native_event); | 244 bool IsEventBlocked(const base::NativeEvent& native_event); |
| 238 | 245 |
| 246 const std::vector<int>& master_pointers() const { |
| 247 return master_pointers_; |
| 248 } |
| 249 |
| 250 protected: |
| 251 // DeviceHotplugEventObserver: |
| 252 void OnKeyboardDevicesUpdated( |
| 253 const std::vector<KeyboardDevice>& devices) override; |
| 254 |
| 239 private: | 255 private: |
| 240 DeviceDataManagerX11(); | 256 DeviceDataManagerX11(); |
| 241 ~DeviceDataManagerX11() override; | 257 ~DeviceDataManagerX11() override; |
| 242 | 258 |
| 243 // Initialize the XInput related system information. | 259 // Initialize the XInput related system information. |
| 244 bool InitializeXInputInternal(); | 260 bool InitializeXInputInternal(); |
| 245 | 261 |
| 246 // Check if an XI event contains data of the specified type. | |
| 247 bool HasEventData(const XIDeviceEvent* xiev, const DataType type) const; | |
| 248 | |
| 249 void InitializeValuatorsForTest(int deviceid, | 262 void InitializeValuatorsForTest(int deviceid, |
| 250 int start_valuator, | 263 int start_valuator, |
| 251 int end_valuator, | 264 int end_valuator, |
| 252 double min_value, | 265 double min_value, |
| 253 double max_value); | 266 double max_value); |
| 254 | 267 |
| 255 static const int kMaxXIEventType = XI_LASTEVENT + 1; | 268 static const int kMaxXIEventType = XI_LASTEVENT + 1; |
| 256 static const int kMaxSlotNum = 10; | 269 static const int kMaxSlotNum = 10; |
| 257 | 270 |
| 258 // Major opcode for the XInput extension. Used to identify XInput events. | 271 // Major opcode for the XInput extension. Used to identify XInput events. |
| 259 int xi_opcode_; | 272 int xi_opcode_; |
| 260 | 273 |
| 261 // A quick lookup table for determining if the XI event is an XIDeviceEvent. | 274 // A quick lookup table for determining if the XI event is an XIDeviceEvent. |
| 262 std::bitset<kMaxXIEventType> xi_device_event_types_; | 275 std::bitset<kMaxXIEventType> xi_device_event_types_; |
| 263 | 276 |
| 264 // A quick lookup table for determining if events from the pointer device | 277 // A quick lookup table for determining if events from the pointer device |
| 265 // should be processed. | 278 // should be processed. |
| 266 std::bitset<kMaxDeviceNum> cmt_devices_; | 279 std::bitset<kMaxDeviceNum> cmt_devices_; |
| 267 std::bitset<kMaxDeviceNum> touchpads_; | 280 std::bitset<kMaxDeviceNum> touchpads_; |
| 268 | 281 |
| 282 // List of the master pointer devices. |
| 283 std::vector<int> master_pointers_; |
| 284 |
| 269 // A quick lookup table for determining if events from the XI device | 285 // A quick lookup table for determining if events from the XI device |
| 270 // should be blocked. | 286 // should be blocked. |
| 271 std::bitset<kMaxDeviceNum> blocked_devices_; | 287 std::bitset<kMaxDeviceNum> blocked_devices_; |
| 272 | 288 |
| 273 // The set of keys allowed while the keyboard is blocked. | 289 // The set of keys allowed while the keyboard is blocked. |
| 274 scoped_ptr<std::set<KeyboardCode>> blocked_keyboard_allowed_keys_; | 290 scoped_ptr<std::set<KeyboardCode> > blocked_keyboard_allowed_keys_; |
| 275 | 291 |
| 276 // Number of valuators on the specific device. | 292 // Number of valuators on the specific device. |
| 277 int valuator_count_[kMaxDeviceNum]; | 293 int valuator_count_[kMaxDeviceNum]; |
| 278 | 294 |
| 279 // Index table to find the valuator for DataType on the specific device | 295 // Index table to find the valuator for DataType on the specific device |
| 280 // by valuator_lookup_[device_id][data_type]. | 296 // by valuator_lookup_[device_id][data_type]. |
| 281 std::vector<int> valuator_lookup_[kMaxDeviceNum]; | 297 std::vector<int> valuator_lookup_[kMaxDeviceNum]; |
| 282 | 298 |
| 283 // Index table to find the DataType for valuator on the specific device | 299 // Index table to find the DataType for valuator on the specific device |
| 284 // by data_type_lookup_[device_id][valuator]. | 300 // by data_type_lookup_[device_id][valuator]. |
| 285 std::vector<int> data_type_lookup_[kMaxDeviceNum]; | 301 std::vector<int> data_type_lookup_[kMaxDeviceNum]; |
| 286 | 302 |
| 287 // Index table to find the min & max value of the Valuator on a specific | 303 // Index table to find the min & max value of the Valuator on a specific |
| 288 // device. | 304 // device. |
| 289 std::vector<double> valuator_min_[kMaxDeviceNum]; | 305 std::vector<double> valuator_min_[kMaxDeviceNum]; |
| 290 std::vector<double> valuator_max_[kMaxDeviceNum]; | 306 std::vector<double> valuator_max_[kMaxDeviceNum]; |
| 291 | 307 |
| 292 // Table to keep track of the last seen value for the specified valuator for | 308 // Table to keep track of the last seen value for the specified valuator for |
| 293 // a specified slot of a device. Defaults to 0 if the valuator for that slot | 309 // a specified slot of a device. Defaults to 0 if the valuator for that slot |
| 294 // was not specified in an earlier event. With MT-B/XI2.2, valuators in an | 310 // was not specified in an earlier event. With MT-B/XI2.2, valuators in an |
| 295 // XEvent are not reported if the values haven't changed from the previous | 311 // XEvent are not reported if the values haven't changed from the previous |
| 296 // event. So it is necessary to remember these valuators so that chrome | 312 // event. So it is necessary to remember these valuators so that chrome |
| 297 // doesn't think X/device doesn't know about the valuators. We currently | 313 // doesn't think X/device doesn't know about the valuators. We currently |
| 298 // use this only on touchscreen devices. | 314 // use this only on touchscreen devices. |
| 299 std::vector<double> last_seen_valuator_[kMaxDeviceNum][kMaxSlotNum]; | 315 std::vector<double> last_seen_valuator_[kMaxDeviceNum][kMaxSlotNum]; |
| 300 | 316 |
| 317 // Map that stores meta-data for blocked keyboards. This is needed to restore |
| 318 // devices when they are re-enabled. |
| 319 std::map<int, ui::KeyboardDevice> blocked_keyboards_; |
| 320 |
| 301 // X11 atoms cache. | 321 // X11 atoms cache. |
| 302 X11AtomCache atom_cache_; | 322 X11AtomCache atom_cache_; |
| 303 | 323 |
| 304 unsigned char button_map_[256]; | 324 unsigned char button_map_[256]; |
| 305 int button_map_count_; | 325 int button_map_count_; |
| 306 | 326 |
| 307 DISALLOW_COPY_AND_ASSIGN(DeviceDataManagerX11); | 327 DISALLOW_COPY_AND_ASSIGN(DeviceDataManagerX11); |
| 308 }; | 328 }; |
| 309 | 329 |
| 310 } // namespace ui | 330 } // namespace ui |
| 311 | 331 |
| 312 #endif // UI_EVENTS_PLATFORM_X11_DEVICE_DATA_MANAGER_X11_H_ | 332 #endif // UI_EVENTS_DEVICES_X11_DEVICE_DATA_MANAGER_X11_H_ |
| OLD | NEW |