Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / ui / events / devices / x11 / touch_factory_x11.h
blob85e4a589d17d42086c0d5072105ded509591b715
1 // Copyright (c) 2012 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_TOUCH_FACTORY_X11_H_
6 #define UI_EVENTS_DEVICES_X11_TOUCH_FACTORY_X11_H_
8 #include <bitset>
9 #include <map>
10 #include <set>
11 #include <utility>
12 #include <vector>
14 #include "ui/events/devices/events_devices_export.h"
15 #include "ui/gfx/sequential_id_generator.h"
17 namespace base {
19 template <typename T> struct DefaultSingletonTraits;
22 typedef unsigned long Cursor;
23 typedef unsigned long Window;
24 typedef struct _XDisplay Display;
25 typedef union _XEvent XEvent;
27 namespace ui {
29 // Functions related to determining touch devices.
30 class EVENTS_DEVICES_EXPORT TouchFactory {
31 private:
32 TouchFactory();
33 ~TouchFactory();
35 public:
36 // Returns the TouchFactory singleton.
37 static TouchFactory* GetInstance();
39 // Sets the touch devices from the command line.
40 static void SetTouchDeviceListFromCommandLine();
42 // Updates the list of devices.
43 void UpdateDeviceList(Display* display);
45 // Checks whether an XI2 event should be processed or not (i.e. if the event
46 // originated from a device we are interested in).
47 bool ShouldProcessXI2Event(XEvent* xevent);
49 // Setup an X Window for XInput2 events.
50 void SetupXI2ForXWindow(::Window xid);
52 // Keeps a list of touch devices so that it is possible to determine if a
53 // pointer event is a touch-event or a mouse-event. The list is reset each
54 // time this is called.
55 void SetTouchDeviceList(const std::vector<int>& devices);
57 // Is the device ID valid?
58 bool IsValidDevice(int deviceid) const;
60 // Is the device a touch-device?
61 bool IsTouchDevice(int deviceid) const;
63 // Is the device a real multi-touch-device? (see doc. for |touch_device_list_|
64 // below for more explanation.)
65 bool IsMultiTouchDevice(int deviceid) const;
67 // Tries to find an existing slot ID mapping to tracking ID. Returns true
68 // if the slot is found and it is saved in |slot|, false if no such slot
69 // can be found.
70 bool QuerySlotForTrackingID(uint32 tracking_id, int* slot);
72 // Tries to find an existing slot ID mapping to tracking ID. If there
73 // isn't one already, allocates a new slot ID and sets up the mapping.
74 int GetSlotForTrackingID(uint32 tracking_id);
76 // Releases the slot ID mapping to tracking ID.
77 void ReleaseSlotForTrackingID(uint32 tracking_id);
79 // Whether any touch device is currently present and enabled.
80 bool IsTouchDevicePresent();
82 // Pairs of <vendor id, product id> of external touch screens.
83 const std::set<std::pair<int, int> >& GetTouchscreenIds() const {
84 return touchscreen_ids_;
87 // Resets the TouchFactory singleton.
88 void ResetForTest();
90 // Sets up the device id in the list |devices| as multi-touch capable
91 // devices and enables touch events processing. This function is only
92 // for test purpose, and it does not query from X server.
93 void SetTouchDeviceForTest(const std::vector<int>& devices);
95 // Sets up the device id in the list |devices| as pointer devices.
96 // This function is only for test purpose, and it does not query from
97 // X server.
98 void SetPointerDeviceForTest(const std::vector<int>& devices);
100 private:
101 // Requirement for Singleton
102 friend struct base::DefaultSingletonTraits<TouchFactory>;
104 void CacheTouchscreenIds(int id);
106 // NOTE: To keep track of touch devices, we currently maintain a lookup table
107 // to quickly decide if a device is a touch device or not. We also maintain a
108 // list of the touch devices. Ideally, there will be only one touch device,
109 // and instead of having the lookup table and the list, there will be a single
110 // identifier for the touch device. This can be completed after enough testing
111 // on real touch devices.
113 static const int kMaxDeviceNum = 128;
115 // A quick lookup table for determining if events from the pointer device
116 // should be processed.
117 std::bitset<kMaxDeviceNum> pointer_device_lookup_;
119 // A quick lookup table for determining if a device is a touch device.
120 std::bitset<kMaxDeviceNum> touch_device_lookup_;
122 // Indicates whether touch events are explicitly disabled.
123 bool touch_events_disabled_;
125 // The list of touch devices. For testing/debugging purposes, a single-pointer
126 // device (mouse or touch screen without sufficient X/driver support for MT)
127 // can sometimes be treated as a touch device. The key in the map represents
128 // the device id, and the value represents if the device is multi-touch
129 // capable.
130 std::map<int, bool> touch_device_list_;
132 // Touch screen <vid, pid>s.
133 std::set<std::pair<int, int> > touchscreen_ids_;
135 // Device ID of the virtual core keyboard.
136 int virtual_core_keyboard_device_;
138 SequentialIDGenerator id_generator_;
140 // Associate each device ID with its master device ID.
141 std::map<int, int> device_master_id_list_;
143 DISALLOW_COPY_AND_ASSIGN(TouchFactory);
146 } // namespace ui
148 #endif // UI_EVENTS_DEVICES_X11_TOUCH_FACTORY_X11_H_