Fix search results being clipped in app list.
[chromium-blink-merge.git] / ui / events / devices / x11 / device_data_manager_x11.h
blobbb5fa15d6076c551ce99a61bcf1e4e734c27b7cb
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.
5 #ifndef UI_EVENTS_DEVICES_X11_DEVICE_DATA_MANAGER_X11_H_
6 #define UI_EVENTS_DEVICES_X11_DEVICE_DATA_MANAGER_X11_H_
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
15 #include <X11/extensions/XInput2.h>
17 #include <bitset>
18 #include <functional>
19 #include <map>
20 #include <set>
21 #include <vector>
23 #include "base/basictypes.h"
24 #include "base/event_types.h"
25 #include "base/memory/scoped_ptr.h"
26 #include "ui/events/devices/device_data_manager.h"
27 #include "ui/events/devices/events_devices_export.h"
28 #include "ui/events/event_constants.h"
29 #include "ui/events/keycodes/keyboard_codes.h"
30 #include "ui/gfx/geometry/rect.h"
31 #include "ui/gfx/x/x11_atom_cache.h"
33 typedef union _XEvent XEvent;
35 namespace ui {
37 // CrOS touchpad metrics gesture types
38 enum GestureMetricsType {
39 kGestureMetricsTypeNoisyGround = 0,
40 kGestureMetricsTypeUnknown,
43 // A class that extracts and tracks the input events data. It currently handles
44 // mouse, touchpad and touchscreen devices.
45 class EVENTS_DEVICES_EXPORT DeviceDataManagerX11 : public DeviceDataManager {
46 public:
47 // Enumerate additional data that one might be interested on an input event,
48 // which are usually wrapped in X valuators. If you modify any of this,
49 // make sure to update the kCachedAtoms data structure in the source file
50 // and the k*Type[Start/End] constants used by IsCMTDataType and
51 // IsTouchDataType.
52 enum DataType {
53 // Define the valuators used the CrOS CMT driver. Used by mice and CrOS
54 // touchpads.
55 DT_CMT_SCROLL_X = 0, // Scroll amount on the X (horizontal) direction.
56 DT_CMT_SCROLL_Y, // Scroll amount on the Y (vertical) direction.
57 DT_CMT_ORDINAL_X, // Original (unaccelerated) value on the X direction.
58 // Can be used both for scrolls and flings.
59 DT_CMT_ORDINAL_Y, // Original (unaccelerated) value on the Y direction.
60 // Can be used both for scrolls and flings.
61 DT_CMT_START_TIME, // Gesture start time.
62 DT_CMT_END_TIME, // Gesture end time.
63 DT_CMT_FLING_X, // Fling amount on the X (horizontal) direction.
64 DT_CMT_FLING_Y, // Fling amount on the Y (vertical) direction.
65 DT_CMT_FLING_STATE, // The state of fling gesture (whether the user just
66 // start flinging or that he/she taps down).
67 DT_CMT_METRICS_TYPE, // Metrics type of the metrics gesture, which are
68 // used to wrap interesting patterns that we would
69 // like to track via the UMA system.
70 DT_CMT_METRICS_DATA1, // Complementary data 1 of the metrics gesture.
71 DT_CMT_METRICS_DATA2, // Complementary data 2 of the metrics gesture.
72 DT_CMT_FINGER_COUNT, // Finger counts in the current gesture. A same type
73 // of gesture can have very different meanings based
74 // on that (e.g. 2f scroll v.s. 3f swipe).
76 // End of CMT data types.
77 // Beginning of touch data types.
79 // Define the valuators following the Multi-touch Protocol. Used by
80 // touchscreen devices.
81 DT_TOUCH_MAJOR, // Length of the touch area.
82 DT_TOUCH_MINOR, // Width of the touch area.
83 DT_TOUCH_ORIENTATION, // Angle between the X-axis and the major axis of the
84 // touch area.
85 DT_TOUCH_PRESSURE, // Pressure of the touch contact.
87 DT_TOUCH_POSITION_X, // Touch X position.
88 DT_TOUCH_POSITION_Y, // Touch Y position.
90 // NOTE for XInput MT: 'Tracking ID' is provided in every touch event to
91 // track individual touch. 'Tracking ID' is an unsigned 32-bit value and
92 // is increased for each new touch. It will wrap back to 0 when reaching
93 // the numerical limit.
94 DT_TOUCH_TRACKING_ID, // ID of the touch point.
96 // Kernel timestamp from touch screen (if available).
97 DT_TOUCH_RAW_TIMESTAMP,
99 // End of touch data types.
101 DT_LAST_ENTRY // This must come last.
104 // Data struct to store extracted data from an input event.
105 typedef std::map<int, double> EventData;
107 static void CreateInstance();
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);
113 // Returns the DeviceDataManagerX11 singleton.
114 static DeviceDataManagerX11* GetInstance();
116 // Returns if XInput2 is available on the system.
117 bool IsXInput2Available() const;
119 // Updates the list of devices.
120 void UpdateDeviceList(Display* display);
122 // For multitouch events we use slot number to distinguish touches from
123 // different fingers. This function returns true if the associated slot
124 // for |xiev| can be found and it is saved in |slot|, returns false if
125 // no slot can be found.
126 bool GetSlotNumber(const XIDeviceEvent* xiev, int* slot);
128 // Get all event data in one pass. We extract only data types that we know
129 // about (defined in enum DataType). The data is not processed (e.g. not
130 // filled in by cached values) as in GetEventData.
131 void GetEventRawData(const XEvent& xev, EventData* data);
133 // Get a datum of the specified type. Return true and the value
134 // is updated if the data is found, false and value unchanged if the data is
135 // not found. In the case of MT-B/XI2.2, the value can come from a previously
136 // cached one (see the comment above last_seen_valuator_).
137 bool GetEventData(const XEvent& xev, const DataType type, double* value);
139 // Check if the event is an XI input event in the strict sense
140 // (i.e. XIDeviceEvent). This rules out things like hierarchy changes,
141 /// device changes, property changes and so on.
142 bool IsXIDeviceEvent(const base::NativeEvent& native_event) const;
144 // Check if the event comes from touchpad devices.
145 bool IsTouchpadXInputEvent(const base::NativeEvent& native_event) const;
147 // Check if the event comes from devices running CMT driver or using
148 // CMT valuators (e.g. mouses). Note that doesn't necessarily mean the event
149 // is a CMT event (e.g. it could be a mouse pointer move).
150 bool IsCMTDeviceEvent(const base::NativeEvent& native_event) const;
152 // Check if the event is one of the CMT gesture events (scroll, fling,
153 // metrics etc.).
154 bool IsCMTGestureEvent(const base::NativeEvent& native_event) const;
156 // Returns true if the event is of the specific type, false if not.
157 bool IsScrollEvent(const base::NativeEvent& native_event) const;
158 bool IsFlingEvent(const base::NativeEvent& native_event) const;
159 bool IsCMTMetricsEvent(const base::NativeEvent& native_event) const;
161 // Returns true if the event has CMT start/end timestamps.
162 bool HasGestureTimes(const base::NativeEvent& native_event) const;
164 // Extract data from a scroll event (a motion event with the necessary
165 // valuators). User must first verify the event type with IsScrollEvent.
166 // Pointers shouldn't be NULL.
167 void GetScrollOffsets(const base::NativeEvent& native_event,
168 float* x_offset,
169 float* y_offset,
170 float* x_offset_ordinal,
171 float* y_offset_ordinal,
172 int* finger_count);
174 // Extract data from a fling event. User must first verify the event type
175 // with IsFlingEvent. Pointers shouldn't be NULL.
176 void GetFlingData(const base::NativeEvent& native_event,
177 float* vx,
178 float* vy,
179 float* vx_ordinal,
180 float* vy_ordinal,
181 bool* is_cancel);
183 // Extract data from a CrOS metrics gesture event. User must first verify
184 // the event type with IsCMTMetricsEvent. Pointers shouldn't be NULL.
185 void GetMetricsData(const base::NativeEvent& native_event,
186 GestureMetricsType* type,
187 float* data1,
188 float* data2);
190 // Returns the mapped button.
191 int GetMappedButton(int button);
193 // Updates button mapping. This is usually called when a MappingNotify event
194 // is received.
195 void UpdateButtonMap();
197 // Extract the start/end timestamps from CMT events. User must first verify
198 // the event with HasGestureTimes. Pointers shouldn't be NULL.
199 void GetGestureTimes(const base::NativeEvent& native_event,
200 double* start_time,
201 double* end_time);
203 // Normalize the data value on deviceid to fall into [0, 1].
204 // *value = (*value - min_value_of_tp) / (max_value_of_tp - min_value_of_tp)
205 // Returns true and sets the normalized value in|value| if normalization is
206 // successful. Returns false and |value| is unchanged otherwise.
207 bool NormalizeData(unsigned int deviceid,
208 const DataType type,
209 double* value);
211 // Extract the range of the data type. Return true if the range is available
212 // and written into min & max, false if the range is not available.
213 bool GetDataRange(unsigned int deviceid,
214 const DataType type,
215 double* min,
216 double* max);
218 // Sets up relevant valuator informations for device ids in the device lists.
219 // This function is only for test purpose. It does not query the X server for
220 // the actual device info, but rather inits the relevant valuator structures
221 // to have safe default values for testing.
222 void SetDeviceListForTest(const std::vector<unsigned int>& touchscreen,
223 const std::vector<unsigned int>& cmt_devices);
225 void SetValuatorDataForTest(XIDeviceEvent* xievent,
226 DataType type,
227 double value);
229 bool TouchEventNeedsCalibrate(unsigned int touch_device_id) const;
231 // Sets the keys which are still allowed on a disabled keyboard device.
232 void SetDisabledKeyboardAllowedKeys(
233 scoped_ptr<std::set<KeyboardCode> > excepted_keys);
235 // Disables and enables events from devices by device id.
236 void DisableDevice(unsigned int deviceid);
237 void EnableDevice(unsigned int deviceid);
239 // Returns true if |native_event| should be blocked.
240 bool IsEventBlocked(const base::NativeEvent& native_event);
242 const std::vector<int>& master_pointers() const {
243 return master_pointers_;
246 protected:
247 // DeviceHotplugEventObserver:
248 void OnKeyboardDevicesUpdated(
249 const std::vector<KeyboardDevice>& devices) override;
251 private:
252 DeviceDataManagerX11();
253 ~DeviceDataManagerX11() override;
255 // Initialize the XInput related system information.
256 bool InitializeXInputInternal();
258 // Check if an XI event contains data of the specified type.
259 bool HasEventData(const XIDeviceEvent* xiev, const DataType type) const;
261 void InitializeValuatorsForTest(int deviceid,
262 int start_valuator,
263 int end_valuator,
264 double min_value,
265 double max_value);
267 static const int kMaxXIEventType = XI_LASTEVENT + 1;
268 static const int kMaxSlotNum = 10;
270 // Major opcode for the XInput extension. Used to identify XInput events.
271 int xi_opcode_;
273 // A quick lookup table for determining if the XI event is an XIDeviceEvent.
274 std::bitset<kMaxXIEventType> xi_device_event_types_;
276 // A quick lookup table for determining if events from the pointer device
277 // should be processed.
278 std::bitset<kMaxDeviceNum> cmt_devices_;
279 std::bitset<kMaxDeviceNum> touchpads_;
281 // List of the master pointer devices.
282 std::vector<int> master_pointers_;
284 // A quick lookup table for determining if events from the XI device
285 // should be blocked.
286 std::bitset<kMaxDeviceNum> blocked_devices_;
288 // The set of keys allowed while the keyboard is blocked.
289 scoped_ptr<std::set<KeyboardCode> > blocked_keyboard_allowed_keys_;
291 // Number of valuators on the specific device.
292 int valuator_count_[kMaxDeviceNum];
294 // Index table to find the valuator for DataType on the specific device
295 // by valuator_lookup_[device_id][data_type].
296 std::vector<int> valuator_lookup_[kMaxDeviceNum];
298 // Index table to find the DataType for valuator on the specific device
299 // by data_type_lookup_[device_id][valuator].
300 std::vector<int> data_type_lookup_[kMaxDeviceNum];
302 // Index table to find the min & max value of the Valuator on a specific
303 // device.
304 std::vector<double> valuator_min_[kMaxDeviceNum];
305 std::vector<double> valuator_max_[kMaxDeviceNum];
307 // Table to keep track of the last seen value for the specified valuator for
308 // a specified slot of a device. Defaults to 0 if the valuator for that slot
309 // was not specified in an earlier event. With MT-B/XI2.2, valuators in an
310 // XEvent are not reported if the values haven't changed from the previous
311 // event. So it is necessary to remember these valuators so that chrome
312 // doesn't think X/device doesn't know about the valuators. We currently
313 // use this only on touchscreen devices.
314 std::vector<double> last_seen_valuator_[kMaxDeviceNum][kMaxSlotNum];
316 // Map that stores meta-data for blocked keyboards. This is needed to restore
317 // devices when they are re-enabled.
318 std::map<unsigned int, ui::KeyboardDevice> blocked_keyboards_;
320 // X11 atoms cache.
321 X11AtomCache atom_cache_;
323 unsigned char button_map_[256];
324 int button_map_count_;
326 DISALLOW_COPY_AND_ASSIGN(DeviceDataManagerX11);
329 } // namespace ui
331 #endif // UI_EVENTS_DEVICES_X11_DEVICE_DATA_MANAGER_X11_H_