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_BASE_TOUCH_TOUCH_FACTORY_X11_H_ 6 #define UI_BASE_TOUCH_TOUCH_FACTORY_X11_H_ 7 8 #include <bitset> 9 #include <map> 10 #include <vector> 11 12 #include "base/containers/hash_tables.h" 13 #include "base/timer/timer.h" 14 #include "ui/base/ui_export.h" 15 16 template <typename T> struct DefaultSingletonTraits; 17 18 typedef unsigned long Cursor; 19 typedef unsigned long Window; 20 typedef struct _XDisplay Display; 21 typedef union _XEvent XEvent; 22 23 namespace ui { 24 25 // Functions related to determining touch devices. 26 class UI_EXPORT TouchFactory { 27 private: 28 TouchFactory(); 29 ~TouchFactory(); 30 31 public: 32 // Returns the TouchFactory singleton. 33 static TouchFactory* GetInstance(); 34 35 // Sets the touch devices from the command line. 36 static void SetTouchDeviceListFromCommandLine(); 37 38 // Updates the list of devices. 39 void UpdateDeviceList(Display* display); 40 41 // Checks whether an XI2 event should be processed or not (i.e. if the event 42 // originated from a device we are interested in). 43 bool ShouldProcessXI2Event(XEvent* xevent); 44 45 // Setup an X Window for XInput2 events. 46 void SetupXI2ForXWindow(::Window xid); 47 48 // Keeps a list of touch devices so that it is possible to determine if a 49 // pointer event is a touch-event or a mouse-event. The list is reset each 50 // time this is called. 51 void SetTouchDeviceList(const std::vector<unsigned int>& devices); 52 53 // Is the device a touch-device? 54 bool IsTouchDevice(unsigned int deviceid) const; 55 56 // Is the device a real multi-touch-device? (see doc. for |touch_device_list_| 57 // below for more explanation.) 58 bool IsMultiTouchDevice(unsigned int deviceid) const; 59 60 #if defined(USE_XI2_MT) 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 // Releases the slot ID mapping to tracking ID. 71 void ReleaseSlotForTrackingID(uint32 tracking_id); 72 #endif 73 74 // Is the slot ID currently used? 75 bool IsSlotUsed(int slot) const; 76 77 // Marks a slot as being used/unused. 78 void SetSlotUsed(int slot, bool used); 79 80 // Whether any touch device is currently present and enabled. 81 bool IsTouchDevicePresent(); 82 83 // Sets up the device id in the list |devices| as multi-touch capable 84 // devices and enables touch events processing. This function is only 85 // for test purpose, and it does not query from X server. 86 void SetTouchDeviceForTest(const std::vector<unsigned int>& devices); 87 88 private: 89 // Requirement for Singleton 90 friend struct DefaultSingletonTraits<TouchFactory>; 91 92 // NOTE: To keep track of touch devices, we currently maintain a lookup table 93 // to quickly decide if a device is a touch device or not. We also maintain a 94 // list of the touch devices. Ideally, there will be only one touch device, 95 // and instead of having the lookup table and the list, there will be a single 96 // identifier for the touch device. This can be completed after enough testing 97 // on real touch devices. 98 99 static const int kMaxDeviceNum = 128; 100 101 // A quick lookup table for determining if events from the pointer device 102 // should be processed. 103 std::bitset<kMaxDeviceNum> pointer_device_lookup_; 104 105 // A quick lookup table for determining if a device is a touch device. 106 std::bitset<kMaxDeviceNum> touch_device_lookup_; 107 108 // Indicates whether a touch device is currently available or not. 109 bool touch_device_available_; 110 111 // Indicates whether touch events are explicitly disabled. 112 bool touch_events_disabled_; 113 114 // The list of touch devices. For testing/debugging purposes, a single-pointer 115 // device (mouse or touch screen without sufficient X/driver support for MT) 116 // can sometimes be treated as a touch device. The key in the map represents 117 // the device id, and the value represents if the device is multi-touch 118 // capable. 119 std::map<int, bool> touch_device_list_; 120 121 // Maximum simultaneous touch points. 122 static const int kMaxTouchPoints = 32; 123 124 #if defined(USE_XI2_MT) 125 // Stores the minimum available slot ID which helps get slot ID from 126 // tracking ID. When it equals to kMaxTouchPoints, there is no available 127 // slot. 128 int min_available_slot_; 129 130 // A hash table to map tracking ID to slot. 131 typedef base::hash_map<uint32, int> TrackingIdMap; 132 TrackingIdMap tracking_id_map_; 133 #endif 134 135 // A lookup table for slots in use for a touch event. 136 std::bitset<kMaxTouchPoints> slots_used_; 137 138 DISALLOW_COPY_AND_ASSIGN(TouchFactory); 139 }; 140 141 } // namespace ui 142 143 #endif // UI_BASE_TOUCH_TOUCH_FACTORY_X11_H_ 144