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_
14 #include "ui/events/devices/events_devices_export.h"
15 #include "ui/gfx/sequential_id_generator.h"
17 template <typename T> struct DefaultSingletonTraits;
19 typedef unsigned long Cursor;
20 typedef unsigned long Window;
21 typedef struct _XDisplay Display;
22 typedef union _XEvent XEvent;
26 // Functions related to determining touch devices.
27 class EVENTS_DEVICES_EXPORT TouchFactory {
33 // Returns the TouchFactory singleton.
34 static TouchFactory* GetInstance();
36 // Sets the touch devices from the command line.
37 static void SetTouchDeviceListFromCommandLine();
39 // Updates the list of devices.
40 void UpdateDeviceList(Display* display);
42 // Checks whether an XI2 event should be processed or not (i.e. if the event
43 // originated from a device we are interested in).
44 bool ShouldProcessXI2Event(XEvent* xevent);
46 // Setup an X Window for XInput2 events.
47 void SetupXI2ForXWindow(::Window xid);
49 // Keeps a list of touch devices so that it is possible to determine if a
50 // pointer event is a touch-event or a mouse-event. The list is reset each
51 // time this is called.
52 void SetTouchDeviceList(const std::vector<unsigned int>& devices);
54 // Is the device a touch-device?
55 bool IsTouchDevice(unsigned int deviceid) const;
57 // Is the device a real multi-touch-device? (see doc. for |touch_device_list_|
58 // below for more explanation.)
59 bool IsMultiTouchDevice(unsigned int deviceid) const;
61 // Tries to find an existing slot ID mapping to tracking ID. Returns true
62 // if the slot is found and it is saved in |slot|, false if no such slot
64 bool QuerySlotForTrackingID(uint32 tracking_id, int* slot);
66 // Tries to find an existing slot ID mapping to tracking ID. If there
67 // isn't one already, allocates a new slot ID and sets up the mapping.
68 int GetSlotForTrackingID(uint32 tracking_id);
70 // Increases the number of times |ReleaseSlotForTrackingID| needs to be called
71 // on a given tracking id before it will actually be released.
72 void AcquireSlotForTrackingID(uint32 tracking_id);
74 // Releases the slot ID mapping to tracking ID.
75 void ReleaseSlotForTrackingID(uint32 tracking_id);
77 // Whether any touch device is currently present and enabled.
78 bool IsTouchDevicePresent();
80 // Pairs of <vendor id, product id> of external touch screens.
81 const std::set<std::pair<int, int> >& GetTouchscreenIds() const {
82 return touchscreen_ids_;
85 // Return maximum simultaneous touch points supported by device.
86 int GetMaxTouchPoints() const;
88 // Resets the TouchFactory singleton.
91 // Sets up the device id in the list |devices| as multi-touch capable
92 // devices and enables touch events processing. This function is only
93 // for test purpose, and it does not query from X server.
94 void SetTouchDeviceForTest(const std::vector<unsigned int>& devices);
96 // Sets up the device id in the list |devices| as pointer devices.
97 // This function is only for test purpose, and it does not query from
99 void SetPointerDeviceForTest(const std::vector<unsigned int>& devices);
102 // Requirement for Singleton
103 friend struct DefaultSingletonTraits<TouchFactory>;
105 void CacheTouchscreenIds(Display* display, int id);
107 // NOTE: To keep track of touch devices, we currently maintain a lookup table
108 // to quickly decide if a device is a touch device or not. We also maintain a
109 // list of the touch devices. Ideally, there will be only one touch device,
110 // and instead of having the lookup table and the list, there will be a single
111 // identifier for the touch device. This can be completed after enough testing
112 // on real touch devices.
114 static const int kMaxDeviceNum = 128;
116 // A quick lookup table for determining if events from the pointer device
117 // should be processed.
118 std::bitset<kMaxDeviceNum> pointer_device_lookup_;
120 // A quick lookup table for determining if a device is a touch device.
121 std::bitset<kMaxDeviceNum> touch_device_lookup_;
123 // Indicates whether touch events are explicitly disabled.
124 bool touch_events_disabled_;
126 // The list of touch devices. For testing/debugging purposes, a single-pointer
127 // device (mouse or touch screen without sufficient X/driver support for MT)
128 // can sometimes be treated as a touch device. The key in the map represents
129 // the device id, and the value represents if the device is multi-touch
131 std::map<int, bool> touch_device_list_;
133 // Touch screen <vid, pid>s.
134 std::set<std::pair<int, int> > touchscreen_ids_;
136 // Maps from a tracking id to the number of times |ReleaseSlotForTrackingID|
137 // must be called before the tracking id is released.
138 std::map<uint32, int> tracking_id_refcounts_;
140 // Maximum simultaneous touch points supported by device. In the case of
141 // devices with multiple digitizers (e.g. multiple touchscreens), the value
142 // is the maximum of the set of maximum supported contacts by each individual
144 int max_touch_points_;
146 // Device ID of the virtual core keyboard.
147 int virtual_core_keyboard_device_;
149 SequentialIDGenerator id_generator_;
151 DISALLOW_COPY_AND_ASSIGN(TouchFactory);
156 #endif // UI_EVENTS_DEVICES_X11_TOUCH_FACTORY_X11_H_