Upstream version 11.40.271.0
[platform/framework/web/crosswalk.git] / src / ui / events / devices / x11 / touch_factory_x11.h
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.
4
5 #ifndef UI_EVENTS_DEVICES_X11_TOUCH_FACTORY_X11_H_
6 #define UI_EVENTS_DEVICES_X11_TOUCH_FACTORY_X11_H_
7
8 #include <bitset>
9 #include <map>
10 #include <set>
11 #include <utility>
12 #include <vector>
13
14 #include "ui/events/devices/events_devices_export.h"
15 #include "ui/gfx/sequential_id_generator.h"
16
17 template <typename T> struct DefaultSingletonTraits;
18
19 typedef unsigned long Cursor;
20 typedef unsigned long Window;
21 typedef struct _XDisplay Display;
22 typedef union _XEvent XEvent;
23
24 namespace ui {
25
26 // Functions related to determining touch devices.
27 class EVENTS_DEVICES_EXPORT TouchFactory {
28  private:
29   TouchFactory();
30   ~TouchFactory();
31
32  public:
33   // Returns the TouchFactory singleton.
34   static TouchFactory* GetInstance();
35
36   // Sets the touch devices from the command line.
37   static void SetTouchDeviceListFromCommandLine();
38
39   // Updates the list of devices.
40   void UpdateDeviceList(Display* display);
41
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);
45
46   // Setup an X Window for XInput2 events.
47   void SetupXI2ForXWindow(::Window xid);
48
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);
53
54   // Is the device a touch-device?
55   bool IsTouchDevice(unsigned int deviceid) const;
56
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;
60
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
63   // can be found.
64   bool QuerySlotForTrackingID(uint32 tracking_id, int* slot);
65
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);
69
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);
73
74   // Releases the slot ID mapping to tracking ID.
75   void ReleaseSlotForTrackingID(uint32 tracking_id);
76
77   // Whether any touch device is currently present and enabled.
78   bool IsTouchDevicePresent();
79
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_;
83   }
84
85   // Return maximum simultaneous touch points supported by device.
86   int GetMaxTouchPoints() const;
87
88   // Resets the TouchFactory singleton.
89   void ResetForTest();
90
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);
95
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
98   // X server.
99   void SetPointerDeviceForTest(const std::vector<unsigned int>& devices);
100
101  private:
102   // Requirement for Singleton
103   friend struct DefaultSingletonTraits<TouchFactory>;
104
105   void CacheTouchscreenIds(Display* display, int id);
106
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.
113
114   static const int kMaxDeviceNum = 128;
115
116   // A quick lookup table for determining if events from the pointer device
117   // should be processed.
118   std::bitset<kMaxDeviceNum> pointer_device_lookup_;
119
120   // A quick lookup table for determining if a device is a touch device.
121   std::bitset<kMaxDeviceNum> touch_device_lookup_;
122
123   // Indicates whether touch events are explicitly disabled.
124   bool touch_events_disabled_;
125
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
130   // capable.
131   std::map<int, bool> touch_device_list_;
132
133   // Touch screen <vid, pid>s.
134   std::set<std::pair<int, int> > touchscreen_ids_;
135
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_;
139
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
143   // digitizer.
144   int max_touch_points_;
145
146   // Device ID of the virtual core keyboard.
147   int virtual_core_keyboard_device_;
148
149   SequentialIDGenerator id_generator_;
150
151   DISALLOW_COPY_AND_ASSIGN(TouchFactory);
152 };
153
154 }  // namespace ui
155
156 #endif  // UI_EVENTS_DEVICES_X11_TOUCH_FACTORY_X11_H_