| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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_EVENTS_PLATFORM_X11_TOUCH_FACTORY_X11_H_ | 5 #ifndef UI_EVENTS_DEVICES_X11_TOUCH_FACTORY_X11_H_ |
| 6 #define UI_EVENTS_PLATFORM_X11_TOUCH_FACTORY_X11_H_ | 6 #define UI_EVENTS_DEVICES_X11_TOUCH_FACTORY_X11_H_ |
| 7 | 7 |
| 8 #include <bitset> | 8 #include <bitset> |
| 9 #include <map> | 9 #include <map> |
| 10 #include <set> | 10 #include <set> |
| 11 #include <utility> | 11 #include <utility> |
| 12 #include <vector> | 12 #include <vector> |
| 13 | 13 |
| 14 #include "base/timer/timer.h" | 14 #include "ui/events/devices/events_devices_export.h" |
| 15 #include "ui/events/events_base_export.h" | |
| 16 #include "ui/gfx/sequential_id_generator.h" | 15 #include "ui/gfx/sequential_id_generator.h" |
| 17 | 16 |
| 18 template <typename T> | 17 template <typename T> struct DefaultSingletonTraits; |
| 19 struct DefaultSingletonTraits; | |
| 20 | 18 |
| 21 typedef unsigned long Cursor; | 19 typedef unsigned long Cursor; |
| 22 typedef unsigned long Window; | 20 typedef unsigned long Window; |
| 23 typedef struct _XDisplay Display; | 21 typedef struct _XDisplay Display; |
| 24 typedef union _XEvent XEvent; | 22 typedef union _XEvent XEvent; |
| 25 | 23 |
| 26 namespace ui { | 24 namespace ui { |
| 27 | 25 |
| 28 // Functions related to determining touch devices. | 26 // Functions related to determining touch devices. |
| 29 class EVENTS_BASE_EXPORT TouchFactory { | 27 class EVENTS_DEVICES_EXPORT TouchFactory { |
| 30 private: | 28 private: |
| 31 TouchFactory(); | 29 TouchFactory(); |
| 32 ~TouchFactory(); | 30 ~TouchFactory(); |
| 33 | 31 |
| 34 public: | 32 public: |
| 35 // Returns the TouchFactory singleton. | 33 // Returns the TouchFactory singleton. |
| 36 static TouchFactory* GetInstance(); | 34 static TouchFactory* GetInstance(); |
| 37 | 35 |
| 38 // Sets the touch devices from the command line. | 36 // Sets the touch devices from the command line. |
| 39 static void SetTouchDeviceListFromCommandLine(); | 37 static void SetTouchDeviceListFromCommandLine(); |
| 40 | 38 |
| 41 // Updates the list of devices. | 39 // Updates the list of devices. |
| 42 void UpdateDeviceList(Display* display); | 40 void UpdateDeviceList(Display* display); |
| 43 | 41 |
| 44 // Checks whether an XI2 event should be processed or not (i.e. if the event | 42 // Checks whether an XI2 event should be processed or not (i.e. if the event |
| 45 // originated from a device we are interested in). | 43 // originated from a device we are interested in). |
| 46 bool ShouldProcessXI2Event(XEvent* xevent); | 44 bool ShouldProcessXI2Event(XEvent* xevent); |
| 47 | 45 |
| 48 // Setup an X Window for XInput2 events. | 46 // Setup an X Window for XInput2 events. |
| 49 void SetupXI2ForXWindow(::Window xid); | 47 void SetupXI2ForXWindow(::Window xid); |
| 50 | 48 |
| 51 // Keeps a list of touch devices so that it is possible to determine if a | 49 // Keeps a list of touch devices so that it is possible to determine if a |
| 52 // pointer event is a touch-event or a mouse-event. The list is reset each | 50 // pointer event is a touch-event or a mouse-event. The list is reset each |
| 53 // time this is called. | 51 // time this is called. |
| 54 void SetTouchDeviceList(const std::vector<unsigned int>& devices); | 52 void SetTouchDeviceList(const std::vector<int>& devices); |
| 53 |
| 54 // Is the device ID valid? |
| 55 bool IsValidDevice(int deviceid) const; |
| 55 | 56 |
| 56 // Is the device a touch-device? | 57 // Is the device a touch-device? |
| 57 bool IsTouchDevice(unsigned int deviceid) const; | 58 bool IsTouchDevice(int deviceid) const; |
| 58 | 59 |
| 59 // Is the device a real multi-touch-device? (see doc. for |touch_device_list_| | 60 // Is the device a real multi-touch-device? (see doc. for |touch_device_list_| |
| 60 // below for more explanation.) | 61 // below for more explanation.) |
| 61 bool IsMultiTouchDevice(unsigned int deviceid) const; | 62 bool IsMultiTouchDevice(int deviceid) const; |
| 62 | 63 |
| 63 // Tries to find an existing slot ID mapping to tracking ID. Returns true | 64 // Tries to find an existing slot ID mapping to tracking ID. Returns true |
| 64 // if the slot is found and it is saved in |slot|, false if no such slot | 65 // if the slot is found and it is saved in |slot|, false if no such slot |
| 65 // can be found. | 66 // can be found. |
| 66 bool QuerySlotForTrackingID(uint32 tracking_id, int* slot); | 67 bool QuerySlotForTrackingID(uint32 tracking_id, int* slot); |
| 67 | 68 |
| 68 // Tries to find an existing slot ID mapping to tracking ID. If there | 69 // Tries to find an existing slot ID mapping to tracking ID. If there |
| 69 // isn't one already, allocates a new slot ID and sets up the mapping. | 70 // isn't one already, allocates a new slot ID and sets up the mapping. |
| 70 int GetSlotForTrackingID(uint32 tracking_id); | 71 int GetSlotForTrackingID(uint32 tracking_id); |
| 71 | 72 |
| 72 // Increases the number of times |ReleaseSlotForTrackingID| needs to be called | |
| 73 // on a given tracking id before it will actually be released. | |
| 74 void AcquireSlotForTrackingID(uint32 tracking_id); | |
| 75 | |
| 76 // Releases the slot ID mapping to tracking ID. | 73 // Releases the slot ID mapping to tracking ID. |
| 77 void ReleaseSlotForTrackingID(uint32 tracking_id); | 74 void ReleaseSlotForTrackingID(uint32 tracking_id); |
| 78 | 75 |
| 79 // Whether any touch device is currently present and enabled. | 76 // Whether any touch device is currently present and enabled. |
| 80 bool IsTouchDevicePresent(); | 77 bool IsTouchDevicePresent(); |
| 81 | 78 |
| 82 // Pairs of <vendor id, product id> of external touch screens. | 79 // Pairs of <vendor id, product id> of external touch screens. |
| 83 const std::set<std::pair<int, int>>& GetTouchscreenIds() const { | 80 const std::set<std::pair<int, int> >& GetTouchscreenIds() const { |
| 84 return touchscreen_ids_; | 81 return touchscreen_ids_; |
| 85 } | 82 } |
| 86 | 83 |
| 87 // Return maximum simultaneous touch points supported by device. | |
| 88 int GetMaxTouchPoints() const; | |
| 89 | |
| 90 // Resets the TouchFactory singleton. | 84 // Resets the TouchFactory singleton. |
| 91 void ResetForTest(); | 85 void ResetForTest(); |
| 92 | 86 |
| 93 // Sets up the device id in the list |devices| as multi-touch capable | 87 // Sets up the device id in the list |devices| as multi-touch capable |
| 94 // devices and enables touch events processing. This function is only | 88 // devices and enables touch events processing. This function is only |
| 95 // for test purpose, and it does not query from X server. | 89 // for test purpose, and it does not query from X server. |
| 96 void SetTouchDeviceForTest(const std::vector<unsigned int>& devices); | 90 void SetTouchDeviceForTest(const std::vector<int>& devices); |
| 97 | 91 |
| 98 // Sets up the device id in the list |devices| as pointer devices. | 92 // Sets up the device id in the list |devices| as pointer devices. |
| 99 // This function is only for test purpose, and it does not query from | 93 // This function is only for test purpose, and it does not query from |
| 100 // X server. | 94 // X server. |
| 101 void SetPointerDeviceForTest(const std::vector<unsigned int>& devices); | 95 void SetPointerDeviceForTest(const std::vector<int>& devices); |
| 102 | 96 |
| 103 private: | 97 private: |
| 104 // Requirement for Singleton | 98 // Requirement for Singleton |
| 105 friend struct DefaultSingletonTraits<TouchFactory>; | 99 friend struct DefaultSingletonTraits<TouchFactory>; |
| 106 | 100 |
| 107 void CacheTouchscreenIds(Display* display, int id); | 101 void CacheTouchscreenIds(int id); |
| 108 | 102 |
| 109 // NOTE: To keep track of touch devices, we currently maintain a lookup table | 103 // NOTE: To keep track of touch devices, we currently maintain a lookup table |
| 110 // to quickly decide if a device is a touch device or not. We also maintain a | 104 // to quickly decide if a device is a touch device or not. We also maintain a |
| 111 // list of the touch devices. Ideally, there will be only one touch device, | 105 // list of the touch devices. Ideally, there will be only one touch device, |
| 112 // and instead of having the lookup table and the list, there will be a single | 106 // and instead of having the lookup table and the list, there will be a single |
| 113 // identifier for the touch device. This can be completed after enough testing | 107 // identifier for the touch device. This can be completed after enough testing |
| 114 // on real touch devices. | 108 // on real touch devices. |
| 115 | 109 |
| 116 static const int kMaxDeviceNum = 128; | 110 static const int kMaxDeviceNum = 128; |
| 117 | 111 |
| 118 // A quick lookup table for determining if events from the pointer device | 112 // A quick lookup table for determining if events from the pointer device |
| 119 // should be processed. | 113 // should be processed. |
| 120 std::bitset<kMaxDeviceNum> pointer_device_lookup_; | 114 std::bitset<kMaxDeviceNum> pointer_device_lookup_; |
| 121 | 115 |
| 122 // A quick lookup table for determining if a device is a touch device. | 116 // A quick lookup table for determining if a device is a touch device. |
| 123 std::bitset<kMaxDeviceNum> touch_device_lookup_; | 117 std::bitset<kMaxDeviceNum> touch_device_lookup_; |
| 124 | 118 |
| 125 // Indicates whether touch events are explicitly disabled. | 119 // Indicates whether touch events are explicitly disabled. |
| 126 bool touch_events_disabled_; | 120 bool touch_events_disabled_; |
| 127 | 121 |
| 128 // The list of touch devices. For testing/debugging purposes, a single-pointer | 122 // The list of touch devices. For testing/debugging purposes, a single-pointer |
| 129 // device (mouse or touch screen without sufficient X/driver support for MT) | 123 // device (mouse or touch screen without sufficient X/driver support for MT) |
| 130 // can sometimes be treated as a touch device. The key in the map represents | 124 // can sometimes be treated as a touch device. The key in the map represents |
| 131 // the device id, and the value represents if the device is multi-touch | 125 // the device id, and the value represents if the device is multi-touch |
| 132 // capable. | 126 // capable. |
| 133 std::map<int, bool> touch_device_list_; | 127 std::map<int, bool> touch_device_list_; |
| 134 | 128 |
| 135 // Touch screen <vid, pid>s. | 129 // Touch screen <vid, pid>s. |
| 136 std::set<std::pair<int, int>> touchscreen_ids_; | 130 std::set<std::pair<int, int> > touchscreen_ids_; |
| 137 | |
| 138 // Maps from a tracking id to the number of times |ReleaseSlotForTrackingID| | |
| 139 // must be called before the tracking id is released. | |
| 140 std::map<uint32, int> tracking_id_refcounts_; | |
| 141 | |
| 142 // Maximum simultaneous touch points supported by device. In the case of | |
| 143 // devices with multiple digitizers (e.g. multiple touchscreens), the value | |
| 144 // is the maximum of the set of maximum supported contacts by each individual | |
| 145 // digitizer. | |
| 146 int max_touch_points_; | |
| 147 | 131 |
| 148 // Device ID of the virtual core keyboard. | 132 // Device ID of the virtual core keyboard. |
| 149 int virtual_core_keyboard_device_; | 133 int virtual_core_keyboard_device_; |
| 150 | 134 |
| 151 SequentialIDGenerator id_generator_; | 135 SequentialIDGenerator id_generator_; |
| 152 | 136 |
| 137 // Associate each device ID with its master device ID. |
| 138 std::map<int, int> device_master_id_list_; |
| 139 |
| 153 DISALLOW_COPY_AND_ASSIGN(TouchFactory); | 140 DISALLOW_COPY_AND_ASSIGN(TouchFactory); |
| 154 }; | 141 }; |
| 155 | 142 |
| 156 } // namespace ui | 143 } // namespace ui |
| 157 | 144 |
| 158 #endif // UI_EVENTS_PLATFORM_X11_TOUCH_FACTORY_X11_H_ | 145 #endif // UI_EVENTS_DEVICES_X11_TOUCH_FACTORY_X11_H_ |
| OLD | NEW |