| 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 #if defined(USE_XI2_MT) |
| 14 #include "ui/base/touch/multi_touch_device.h" |
| 15 |
| 16 #include <utouch/frame.h> |
| 17 #include <utouch/frame_x11.h> |
| 18 #endif |
| 19 |
| 13 #include "base/memory/singleton.h" | 20 #include "base/memory/singleton.h" |
| 14 #include "base/hash_tables.h" | 21 #include "base/hash_tables.h" |
| 22 #include "base/synchronization/lock.h" |
| 23 #include "base/observer_list_threadsafe.h" |
| 15 #include "base/timer.h" | 24 #include "base/timer.h" |
| 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 struct DeviceObserver { |
| 44 virtual ~DeviceObserver() {} |
| 45 |
| 46 virtual void OnDevicesUpdated(TouchDeviceList deviceList) = 0; |
| 47 }; |
| 48 |
| 28 // Define the touch params following the Multi-touch Protocol. | 49 // Define the touch params following the Multi-touch Protocol. |
| 29 enum TouchParam { | 50 enum TouchParam { |
| 30 TP_TOUCH_MAJOR = 0, // Length of the touch area. | 51 TP_TOUCH_MAJOR = 0, // Length of the touch area. |
| 31 TP_TOUCH_MINOR, // Width of the touch area. | 52 TP_TOUCH_MINOR, // Width of the touch area. |
| 32 TP_ORIENTATION, // Angle between the X-axis and the major axis of the | 53 TP_ORIENTATION, // Angle between the X-axis and the major axis of the |
| 33 // touch area. | 54 // touch area. |
| 34 TP_PRESSURE, // Pressure of the touch contact. | 55 TP_PRESSURE, // Pressure of the touch contact. |
| 35 | 56 |
| 36 // NOTE: A touch event can have multiple touch points. So when we receive a | 57 // 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. | 58 // 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 | 75 // is increased for each new touch. It will wrap back to 0 when reaching |
| 55 // the numerical limit. | 76 // the numerical limit. |
| 56 TP_TRACKING_ID, // ID of the touch point. | 77 TP_TRACKING_ID, // ID of the touch point. |
| 57 | 78 |
| 58 TP_LAST_ENTRY | 79 TP_LAST_ENTRY |
| 59 }; | 80 }; |
| 60 | 81 |
| 61 // Returns the TouchFactory singleton. | 82 // Returns the TouchFactory singleton. |
| 62 static TouchFactory* GetInstance(); | 83 static TouchFactory* GetInstance(); |
| 63 | 84 |
| 85 // Add a new observer. |
| 86 // Can be called from any thread. |
| 87 // Must not be called from within a notification callback. |
| 88 void AddDeviceObserver(DeviceObserver * observer); |
| 89 |
| 90 // Remove an existing observer. |
| 91 // Can be called from any thread. |
| 92 // Must not be called from within a notification callback. |
| 93 void RemoveDeviceObserver(DeviceObserver * observer); |
| 94 |
| 64 // Updates the list of devices. | 95 // Updates the list of devices. |
| 65 void UpdateDeviceList(Display* display); | 96 void UpdateDeviceList(Display* display); |
| 66 | 97 |
| 67 // Checks whether an XI2 event should be processed or not (i.e. if the event | 98 // Checks whether an XI2 event should be processed or not (i.e. if the event |
| 68 // originated from a device we are interested in). | 99 // originated from a device we are interested in). |
| 69 bool ShouldProcessXI2Event(XEvent* xevent); | 100 bool ShouldProcessXI2Event(XEvent* xevent); |
| 70 | 101 |
| 102 // Preprocesses an XI2 event and feeds it to utouch frame. |
| 103 void ProcessXI2Event(XEvent* event); |
| 104 |
| 71 // Setup an X Window for XInput2 events. | 105 // Setup an X Window for XInput2 events. |
| 72 void SetupXI2ForXWindow(::Window xid); | 106 void SetupXI2ForXWindow(::Window xid); |
| 73 | 107 |
| 74 // Keeps a list of touch devices so that it is possible to determine if a | 108 // 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 | 109 // pointer event is a touch-event or a mouse-event. The list is reset each |
| 76 // time this is called. | 110 // time this is called. |
| 77 void SetTouchDeviceList(const std::vector<unsigned int>& devices); | 111 void SetTouchDeviceList(const std::vector<unsigned int>& devices); |
| 78 | 112 |
| 79 // Is the device a touch-device? | 113 // Is the device a touch-device? |
| 80 bool IsTouchDevice(unsigned int deviceid) const; | 114 bool IsTouchDevice(unsigned int deviceid) const; |
| 81 | 115 |
| 82 // Is the device a real multi-touch-device? (see doc. for |touch_device_list_| | 116 // Is the device a real multi-touch-device? (see doc. for |touch_device_list_| |
| 83 // below for more explanation.) | 117 // below for more explanation.) |
| 84 bool IsMultiTouchDevice(unsigned int deviceid) const; | 118 bool IsMultiTouchDevice(unsigned int deviceid) const; |
| 85 | 119 |
| 86 #if defined(USE_XI2_MT) | 120 #if defined(USE_XI2_MT) |
| 87 // Tries to find an existing slot ID mapping to tracking ID. If there | 121 // 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. | 122 // isn't one already, allocates a new slot ID and sets up the mapping. |
| 89 int GetSlotForTrackingID(uint32 tracking_id); | 123 int GetSlotForTrackingID(uint32 tracking_id); |
| 90 | 124 |
| 91 // Releases the slot ID mapping to tracking ID. | 125 // Releases the slot ID mapping to tracking ID. |
| 92 void ReleaseSlotForTrackingID(uint32 tracking_id); | 126 void ReleaseSlotForTrackingID(uint32 tracking_id); |
| 127 |
| 128 // Provides raw access to the utouch-frame instance. |
| 129 // TODO(tvoss): Come up with a more secure and clean |
| 130 // pattern to provide access to the frame instance. |
| 131 UFHandle handle() const { |
| 132 return utouch_frame_handle_; |
| 133 } |
| 134 |
| 135 #if defined(USE_AURA) |
| 136 // Provides raw access to the X handle of the Aura |
| 137 // root window. Defaults to ui::GetX11RootWindow() |
| 138 ::Window native_root_window_aura() const { |
| 139 return native_root_window_aura_; |
| 140 } |
| 141 #endif |
| 93 #endif | 142 #endif |
| 94 | 143 |
| 95 // Is the slot ID currently used? | 144 // Is the slot ID currently used? |
| 96 bool IsSlotUsed(int slot) const; | 145 bool IsSlotUsed(int slot) const; |
| 97 | 146 |
| 98 // Marks a slot as being used/unused. | 147 // Marks a slot as being used/unused. |
| 99 void SetSlotUsed(int slot, bool used); | 148 void SetSlotUsed(int slot, bool used); |
| 100 | 149 |
| 101 // Grabs the touch devices for the specified window on the specified display. | 150 // Grabs the touch devices for the specified window on the specified display. |
| 102 // Returns if grab was successful for all touch devices. | 151 // Returns if grab was successful for all touch devices. |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 143 SetCursorVisible(false, false); | 192 SetCursorVisible(false, false); |
| 144 } | 193 } |
| 145 | 194 |
| 146 // Setup the internal bookkeeping of the touch params valuator information for | 195 // Setup the internal bookkeeping of the touch params valuator information for |
| 147 // touch devices | 196 // touch devices |
| 148 void SetupValuator(); | 197 void SetupValuator(); |
| 149 | 198 |
| 150 // Requirement for Signleton | 199 // Requirement for Signleton |
| 151 friend struct DefaultSingletonTraits<TouchFactory>; | 200 friend struct DefaultSingletonTraits<TouchFactory>; |
| 152 | 201 |
| 202 // Observer management |
| 203 ObserverListThreadSafe<DeviceObserver>* device_observer_list_; |
| 204 |
| 153 // The default cursor is hidden after startup, and when the mouse pointer is | 205 // 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 | 206 // idle for a while. Once there is some event from a mouse device, the cursor |
| 155 // is immediately displayed. | 207 // is immediately displayed. |
| 156 bool is_cursor_visible_; | 208 bool is_cursor_visible_; |
| 157 | 209 |
| 158 // The cursor is hidden if it is idle for a certain amount time. This timer | 210 // The cursor is hidden if it is idle for a certain amount time. This timer |
| 159 // is used to keep track of the idleness. | 211 // is used to keep track of the idleness. |
| 160 base::OneShotTimer<TouchFactory> cursor_timer_; | 212 base::OneShotTimer<TouchFactory> cursor_timer_; |
| 161 | 213 |
| 162 // The default cursor. | 214 // The default cursor. |
| (...skipping 19 matching lines...) Expand all Loading... |
| 182 std::bitset<kMaxDeviceNum> touch_device_lookup_; | 234 std::bitset<kMaxDeviceNum> touch_device_lookup_; |
| 183 | 235 |
| 184 // Indicates whether a touch device is currently available or not. | 236 // Indicates whether a touch device is currently available or not. |
| 185 bool touch_device_available_; | 237 bool touch_device_available_; |
| 186 | 238 |
| 187 // The list of touch devices. For testing/debugging purposes, a single-pointer | 239 // 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) | 240 // 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 | 241 // 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 | 242 // the device id, and the value represents if the device is multi-touch |
| 191 // capable. | 243 // capable. |
| 192 std::map<int, bool> touch_device_list_; | 244 base::Lock touch_device_list_lock_; |
| 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_AURA) |
| 264 // The X handle of the aura root window |
| 265 ::Window native_root_window_aura_; |
| 266 #endif |
| 267 |
| 268 UFHandle utouch_frame_handle_; |
| 269 |
| 210 // Stores the minimum available slot ID which helps get slot ID from | 270 // Stores the minimum available slot ID which helps get slot ID from |
| 211 // tracking ID. When it equals to kMaxTouchPoints, there is no available | 271 // tracking ID. When it equals to kMaxTouchPoints, there is no available |
| 212 // slot. | 272 // slot. |
| 213 int min_available_slot_; | 273 int min_available_slot_; |
| 214 | 274 |
| 215 // A hash table to map tracking ID to slot. | 275 // A hash table to map tracking ID to slot. |
| 216 typedef base::hash_map<uint32, int> TrackingIdMap; | 276 typedef base::hash_map<uint32, int> TrackingIdMap; |
| 217 TrackingIdMap tracking_id_map_; | 277 TrackingIdMap tracking_id_map_; |
| 218 #endif | 278 #endif |
| 219 | 279 |
| 220 // A lookup table for slots in use for a touch event. | 280 // A lookup table for slots in use for a touch event. |
| 221 std::bitset<kMaxTouchPoints> slots_used_; | 281 std::bitset<kMaxTouchPoints> slots_used_; |
| 222 | 282 |
| 223 DISALLOW_COPY_AND_ASSIGN(TouchFactory); | 283 DISALLOW_COPY_AND_ASSIGN(TouchFactory); |
| 224 }; | 284 }; |
| 225 | 285 |
| 226 } // namespace ui | 286 } // namespace ui |
| 227 | 287 |
| 228 #endif // UI_BASE_TOUCH_TOUCH_FACTORY_H_ | 288 #endif // UI_BASE_TOUCH_TOUCH_FACTORY_H_ |
| OLD | NEW |