OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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_BASE_TOUCH_TOUCH_FACTORY_H_ | 5 #ifndef UI_BASE_TOUCH_TOUCH_FACTORY_H_ |
6 #define UI_BASE_TOUCH_TOUCH_FACTORY_H_ | 6 #define UI_BASE_TOUCH_TOUCH_FACTORY_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include <bitset> | 9 #include <bitset> |
10 #include <map> | 10 #include <map> |
11 #include <vector> | 11 #include <vector> |
12 | 12 |
| 13 #include "base/hash_tables.h" |
13 #include "base/memory/singleton.h" | 14 #include "base/memory/singleton.h" |
14 #include "base/hash_tables.h" | 15 #include "base/observer_list_threadsafe.h" |
| 16 #include "base/synchronization/lock.h" |
15 #include "base/timer.h" | 17 #include "base/timer.h" |
| 18 #if defined(USE_XI2_MT) |
| 19 #if defined(USE_UTOUCH) |
| 20 #include "third_party/utouch-frame/include/utouch/frame.h" |
| 21 #include "third_party/utouch-frame/include/utouch/frame_x11.h" |
| 22 #endif // USE_UTOUCH |
| 23 #include "ui/base/touch/multi_touch_device.h" |
| 24 #endif // USE_XI2_MT |
16 #include "ui/base/ui_export.h" | 25 #include "ui/base/ui_export.h" |
17 | 26 |
18 typedef unsigned long Cursor; | 27 typedef unsigned long Cursor; |
19 typedef unsigned long Window; | 28 typedef unsigned long Window; |
20 typedef struct _XDisplay Display; | 29 typedef struct _XDisplay Display; |
21 typedef union _XEvent XEvent; | 30 typedef union _XEvent XEvent; |
22 | 31 |
23 namespace ui { | 32 namespace ui { |
24 | 33 |
25 // Functions related to determining touch devices. | 34 // Functions related to determining touch devices. |
26 class UI_EXPORT TouchFactory { | 35 class UI_EXPORT TouchFactory { |
27 public: | 36 public: |
| 37 #if !defined(USE_XI2_MT) |
| 38 typedef std::map<int, bool> TouchDeviceList; |
| 39 #else |
| 40 typedef std::map<int, MultiTouchDevice> TouchDeviceList; |
| 41 #endif |
| 42 |
| 43 // Allow components interested in multi-touch hierarch events |
| 44 // to be notified if a device has been added or removed. |
| 45 struct DeviceObserver { |
| 46 virtual ~DeviceObserver() {} |
| 47 |
| 48 // Called when the list of known devices changes. |
| 49 virtual void OnDevicesUpdated(TouchDeviceList deviceList) = 0; |
| 50 }; |
| 51 |
28 // Define the touch params following the Multi-touch Protocol. | 52 // Define the touch params following the Multi-touch Protocol. |
29 enum TouchParam { | 53 enum TouchParam { |
30 TP_TOUCH_MAJOR = 0, // Length of the touch area. | 54 TP_TOUCH_MAJOR = 0, // Length of the touch area. |
31 TP_TOUCH_MINOR, // Width of the touch area. | 55 TP_TOUCH_MINOR, // Width of the touch area. |
32 TP_ORIENTATION, // Angle between the X-axis and the major axis of the | 56 TP_ORIENTATION, // Angle between the X-axis and the major axis of the |
33 // touch area. | 57 // touch area. |
34 TP_PRESSURE, // Pressure of the touch contact. | 58 TP_PRESSURE, // Pressure of the touch contact. |
35 | 59 |
36 // NOTE: A touch event can have multiple touch points. So when we receive a | 60 // NOTE: A touch event can have multiple touch points. So when we receive a |
37 // touch event, we need to determine which point triggered the event. | 61 // touch event, we need to determine which point triggered the event. |
(...skipping 16 matching lines...) Expand all Loading... |
54 // is increased for each new touch. It will wrap back to 0 when reaching | 78 // is increased for each new touch. It will wrap back to 0 when reaching |
55 // the numerical limit. | 79 // the numerical limit. |
56 TP_TRACKING_ID, // ID of the touch point. | 80 TP_TRACKING_ID, // ID of the touch point. |
57 | 81 |
58 TP_LAST_ENTRY | 82 TP_LAST_ENTRY |
59 }; | 83 }; |
60 | 84 |
61 // Returns the TouchFactory singleton. | 85 // Returns the TouchFactory singleton. |
62 static TouchFactory* GetInstance(); | 86 static TouchFactory* GetInstance(); |
63 | 87 |
| 88 // Add a new observer. |
| 89 // Can be called from any thread. |
| 90 // Must not be called from within a notification callback. |
| 91 void AddDeviceObserver(DeviceObserver * observer); |
| 92 |
| 93 // Remove an existing observer. |
| 94 // Can be called from any thread. |
| 95 // Must not be called from within a notification callback. |
| 96 void RemoveDeviceObserver(DeviceObserver * observer); |
| 97 |
64 // Updates the list of devices. | 98 // Updates the list of devices. |
65 void UpdateDeviceList(Display* display); | 99 void UpdateDeviceList(Display* display); |
66 | 100 |
67 // Checks whether an XI2 event should be processed or not (i.e. if the event | 101 // Checks whether an XI2 event should be processed or not (i.e. if the event |
68 // originated from a device we are interested in). | 102 // originated from a device we are interested in). |
69 bool ShouldProcessXI2Event(XEvent* xevent); | 103 bool ShouldProcessXI2Event(XEvent* xevent); |
70 | 104 |
| 105 #if defined(USE_UTOUCH) |
| 106 // Preprocesses an XI2 event and feeds it to utouch frame. |
| 107 void ProcessXI2Event(XEvent* event); |
| 108 #endif // USE_UTOUCH |
| 109 |
71 // Setup an X Window for XInput2 events. | 110 // Setup an X Window for XInput2 events. |
72 void SetupXI2ForXWindow(::Window xid); | 111 void SetupXI2ForXWindow(::Window xid); |
73 | 112 |
74 // Keeps a list of touch devices so that it is possible to determine if a | 113 // Keeps a list of touch devices so that it is possible to determine if a |
75 // pointer event is a touch-event or a mouse-event. The list is reset each | 114 // pointer event is a touch-event or a mouse-event. The list is reset each |
76 // time this is called. | 115 // time this is called. |
77 void SetTouchDeviceList(const std::vector<unsigned int>& devices); | 116 void SetTouchDeviceList(const std::vector<unsigned int>& devices); |
78 | 117 |
79 // Is the device a touch-device? | 118 // Is the device a touch-device? |
80 bool IsTouchDevice(unsigned int deviceid) const; | 119 bool IsTouchDevice(unsigned int deviceid) const; |
81 | 120 |
82 // Is the device a real multi-touch-device? (see doc. for |touch_device_list_| | 121 // Is the device a real multi-touch-device? (see doc. for |touch_device_list_| |
83 // below for more explanation.) | 122 // below for more explanation.) |
84 bool IsMultiTouchDevice(unsigned int deviceid) const; | 123 bool IsMultiTouchDevice(unsigned int deviceid) const; |
85 | 124 |
86 #if defined(USE_XI2_MT) | 125 #if defined(USE_XI2_MT) |
87 // Tries to find an existing slot ID mapping to tracking ID. If there | 126 // Tries to find an existing slot ID mapping to tracking ID. If there |
88 // isn't one already, allocates a new slot ID and sets up the mapping. | 127 // isn't one already, allocates a new slot ID and sets up the mapping. |
89 int GetSlotForTrackingID(uint32 tracking_id); | 128 int GetSlotForTrackingID(uint32 tracking_id); |
90 | 129 |
91 // Releases the slot ID mapping to tracking ID. | 130 // Releases the slot ID mapping to tracking ID. |
92 void ReleaseSlotForTrackingID(uint32 tracking_id); | 131 void ReleaseSlotForTrackingID(uint32 tracking_id); |
93 #endif | 132 |
| 133 #if defined(USE_UTOUCH) |
| 134 // Provides raw access to the utouch-frame instance. |
| 135 // TODO(tvoss): Come up with a more secure and clean |
| 136 // pattern to provide access to the frame instance. |
| 137 UFHandle handle() const { |
| 138 return utouch_frame_handle_; |
| 139 } |
| 140 #endif // USE_UTOUCH |
| 141 #endif // USE_XI2_MT |
94 | 142 |
95 // Is the slot ID currently used? | 143 // Is the slot ID currently used? |
96 bool IsSlotUsed(int slot) const; | 144 bool IsSlotUsed(int slot) const; |
97 | 145 |
98 // Marks a slot as being used/unused. | 146 // Marks a slot as being used/unused. |
99 void SetSlotUsed(int slot, bool used); | 147 void SetSlotUsed(int slot, bool used); |
100 | 148 |
101 // Grabs the touch devices for the specified window on the specified display. | 149 // Grabs the touch devices for the specified window on the specified display. |
102 // Returns if grab was successful for all touch devices. | 150 // Returns if grab was successful for all touch devices. |
103 bool GrabTouchDevices(Display* display, ::Window window); | 151 bool GrabTouchDevices(Display* display, ::Window window); |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
143 SetCursorVisible(false, false); | 191 SetCursorVisible(false, false); |
144 } | 192 } |
145 | 193 |
146 // Setup the internal bookkeeping of the touch params valuator information for | 194 // Setup the internal bookkeeping of the touch params valuator information for |
147 // touch devices | 195 // touch devices |
148 void SetupValuator(); | 196 void SetupValuator(); |
149 | 197 |
150 // Requirement for Signleton | 198 // Requirement for Signleton |
151 friend struct DefaultSingletonTraits<TouchFactory>; | 199 friend struct DefaultSingletonTraits<TouchFactory>; |
152 | 200 |
| 201 // Observer management |
| 202 ObserverListThreadSafe<DeviceObserver>* device_observer_list_; |
| 203 |
153 // The default cursor is hidden after startup, and when the mouse pointer is | 204 // The default cursor is hidden after startup, and when the mouse pointer is |
154 // idle for a while. Once there is some event from a mouse device, the cursor | 205 // idle for a while. Once there is some event from a mouse device, the cursor |
155 // is immediately displayed. | 206 // is immediately displayed. |
156 bool is_cursor_visible_; | 207 bool is_cursor_visible_; |
157 | 208 |
158 // The cursor is hidden if it is idle for a certain amount time. This timer | 209 // The cursor is hidden if it is idle for a certain amount time. This timer |
159 // is used to keep track of the idleness. | 210 // is used to keep track of the idleness. |
160 base::OneShotTimer<TouchFactory> cursor_timer_; | 211 base::OneShotTimer<TouchFactory> cursor_timer_; |
161 | 212 |
162 // The default cursor. | 213 // The default cursor. |
(...skipping 18 matching lines...) Expand all Loading... |
181 // A quick lookup table for determining if a device is a touch device. | 232 // A quick lookup table for determining if a device is a touch device. |
182 std::bitset<kMaxDeviceNum> touch_device_lookup_; | 233 std::bitset<kMaxDeviceNum> touch_device_lookup_; |
183 | 234 |
184 // Indicates whether a touch device is currently available or not. | 235 // Indicates whether a touch device is currently available or not. |
185 bool touch_device_available_; | 236 bool touch_device_available_; |
186 | 237 |
187 // The list of touch devices. For testing/debugging purposes, a single-pointer | 238 // The list of touch devices. For testing/debugging purposes, a single-pointer |
188 // device (mouse or touch screen without sufficient X/driver support for MT) | 239 // device (mouse or touch screen without sufficient X/driver support for MT) |
189 // can sometimes be treated as a touch device. The key in the map represents | 240 // can sometimes be treated as a touch device. The key in the map represents |
190 // the device id, and the value represents if the device is multi-touch | 241 // the device id, and the value represents if the device is multi-touch |
191 // capable. | 242 // capable. If USE_XI2_MT is defined, the list contains not only boolean |
192 std::map<int, bool> touch_device_list_; | 243 // values indicating multi-touch capabilites of the respective device but an |
| 244 // instance of class ui::MultiTouchDevice. |
| 245 TouchDeviceList touch_device_list_; |
193 | 246 |
194 // Index table to find the valuator for the TouchParam on the specific device | 247 // Index table to find the valuator for the TouchParam on the specific device |
195 // by valuator_lookup_[device_id][touch_params]. Use 2-D array to get fast | 248 // by valuator_lookup_[device_id][touch_params]. Use 2-D array to get fast |
196 // index at the expense of space. If the kMaxDeviceNum grows larger that the | 249 // index at the expense of space. If the kMaxDeviceNum grows larger that the |
197 // space waste becomes a concern, the 2D lookup table can be replaced by a | 250 // space waste becomes a concern, the 2D lookup table can be replaced by a |
198 // hash map. | 251 // hash map. |
199 signed char valuator_lookup_[kMaxDeviceNum][TP_LAST_ENTRY]; | 252 signed char valuator_lookup_[kMaxDeviceNum][TP_LAST_ENTRY]; |
200 | 253 |
201 // Index table to find the min & max value of the TouchParam on a specific | 254 // Index table to find the min & max value of the TouchParam on a specific |
202 // device. | 255 // device. |
203 int touch_param_min_[kMaxDeviceNum][TP_LAST_ENTRY]; | 256 int touch_param_min_[kMaxDeviceNum][TP_LAST_ENTRY]; |
204 int touch_param_max_[kMaxDeviceNum][TP_LAST_ENTRY]; | 257 int touch_param_max_[kMaxDeviceNum][TP_LAST_ENTRY]; |
205 | 258 |
206 // Maximum simultaneous touch points. | 259 // Maximum simultaneous touch points. |
207 static const int kMaxTouchPoints = 32; | 260 static const int kMaxTouchPoints = 32; |
208 | 261 |
209 #if defined(USE_XI2_MT) | 262 #if defined(USE_XI2_MT) |
| 263 #if defined(USE_UTOUCH) |
| 264 UFHandle utouch_frame_handle_; |
| 265 #endif // USE_UTOUCH |
| 266 |
210 // Stores the minimum available slot ID which helps get slot ID from | 267 // Stores the minimum available slot ID which helps get slot ID from |
211 // tracking ID. When it equals to kMaxTouchPoints, there is no available | 268 // tracking ID. When it equals to kMaxTouchPoints, there is no available |
212 // slot. | 269 // slot. |
213 int min_available_slot_; | 270 int min_available_slot_; |
214 | 271 |
215 // A hash table to map tracking ID to slot. | 272 // A hash table to map tracking ID to slot. |
216 typedef base::hash_map<uint32, int> TrackingIdMap; | 273 typedef base::hash_map<uint32, int> TrackingIdMap; |
217 TrackingIdMap tracking_id_map_; | 274 TrackingIdMap tracking_id_map_; |
218 #endif | 275 #endif // USE_XI2_MT |
219 | 276 |
220 // A lookup table for slots in use for a touch event. | 277 // A lookup table for slots in use for a touch event. |
221 std::bitset<kMaxTouchPoints> slots_used_; | 278 std::bitset<kMaxTouchPoints> slots_used_; |
222 | 279 |
223 DISALLOW_COPY_AND_ASSIGN(TouchFactory); | 280 DISALLOW_COPY_AND_ASSIGN(TouchFactory); |
224 }; | 281 }; |
225 | 282 |
226 } // namespace ui | 283 } // namespace ui |
227 | 284 |
228 #endif // UI_BASE_TOUCH_TOUCH_FACTORY_H_ | 285 #endif // UI_BASE_TOUCH_TOUCH_FACTORY_H_ |
OLD | NEW |