Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(4)

Side by Side Diff: ui/events/devices/x11/touch_factory_x11.cc

Issue 1287103004: Sync ui/events to chromium @ https://codereview.chromium.org/1210203002 (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: rebased Created 5 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « ui/events/devices/x11/touch_factory_x11.h ('k') | ui/events/event.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 #include "ui/events/platform/x11/touch_factory_x11.h" 5 #include "ui/events/devices/x11/touch_factory_x11.h"
6 6
7 #include <X11/Xatom.h>
7 #include <X11/cursorfont.h> 8 #include <X11/cursorfont.h>
8 #include <X11/extensions/XInput.h> 9 #include <X11/extensions/XInput.h>
9 #include <X11/extensions/XInput2.h> 10 #include <X11/extensions/XInput2.h>
10 #include <X11/extensions/XIproto.h> 11 #include <X11/extensions/XIproto.h>
11 #include <X11/Xatom.h>
12 12
13 #include "base/basictypes.h" 13 #include "base/basictypes.h"
14 #include "base/command_line.h" 14 #include "base/command_line.h"
15 #include "base/compiler_specific.h" 15 #include "base/compiler_specific.h"
16 #include "base/logging.h" 16 #include "base/logging.h"
17 #include "base/memory/singleton.h" 17 #include "base/memory/singleton.h"
18 #include "base/message_loop/message_loop.h" 18 #include "base/message_loop/message_loop.h"
19 #include "base/strings/string_number_conversions.h" 19 #include "base/strings/string_number_conversions.h"
20 #include "base/strings/string_split.h" 20 #include "base/strings/string_split.h"
21 #include "base/sys_info.h" 21 #include "base/sys_info.h"
22 #include "ui/events/devices/x11/device_data_manager_x11.h"
23 #include "ui/events/devices/x11/device_list_cache_x11.h"
22 #include "ui/events/event_switches.h" 24 #include "ui/events/event_switches.h"
23 #include "ui/events/platform/x11/device_data_manager_x11.h"
24 #include "ui/events/platform/x11/device_list_cache_x.h"
25 #include "ui/gfx/x/x11_types.h" 25 #include "ui/gfx/x/x11_types.h"
26 26
27 namespace ui { 27 namespace ui {
28 28
29 TouchFactory::TouchFactory() 29 TouchFactory::TouchFactory()
30 : pointer_device_lookup_(), 30 : pointer_device_lookup_(),
31 touch_events_disabled_(false), 31 touch_events_disabled_(false),
32 touch_device_list_(), 32 touch_device_list_(),
33 max_touch_points_(-1),
34 virtual_core_keyboard_device_(-1), 33 virtual_core_keyboard_device_(-1),
35 id_generator_(0) { 34 id_generator_(0) {
36 if (!DeviceDataManagerX11::GetInstance()->IsXInput2Available()) 35 if (!DeviceDataManagerX11::GetInstance()->IsXInput2Available())
37 return; 36 return;
38 37
39 XDisplay* display = gfx::GetXDisplay(); 38 XDisplay* display = gfx::GetXDisplay();
40 UpdateDeviceList(display); 39 UpdateDeviceList(display);
41 40
42 base::CommandLine* cmdline = base::CommandLine::ForCurrentProcess(); 41 base::CommandLine* cmdline = base::CommandLine::ForCurrentProcess();
43 touch_events_disabled_ = 42 touch_events_disabled_ = cmdline->HasSwitch(switches::kTouchEvents) &&
44 cmdline->HasSwitch(switches::kTouchEvents) &&
45 cmdline->GetSwitchValueASCII(switches::kTouchEvents) == 43 cmdline->GetSwitchValueASCII(switches::kTouchEvents) ==
46 switches::kTouchEventsDisabled; 44 switches::kTouchEventsDisabled;
47 } 45 }
48 46
49 TouchFactory::~TouchFactory() { 47 TouchFactory::~TouchFactory() {
50 } 48 }
51 49
52 // static 50 // static
53 TouchFactory* TouchFactory::GetInstance() { 51 TouchFactory* TouchFactory::GetInstance() {
54 return Singleton<TouchFactory>::get(); 52 return Singleton<TouchFactory>::get();
55 } 53 }
56 54
57 // static 55 // static
58 void TouchFactory::SetTouchDeviceListFromCommandLine() { 56 void TouchFactory::SetTouchDeviceListFromCommandLine() {
59 // Get a list of pointer-devices that should be treated as touch-devices. 57 // Get a list of pointer-devices that should be treated as touch-devices.
60 // This is primarily used for testing/debugging touch-event processing when a 58 // This is primarily used for testing/debugging touch-event processing when a
61 // touch-device isn't available. 59 // touch-device isn't available.
62 std::string touch_devices = 60 std::string touch_devices =
63 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII( 61 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
64 switches::kTouchDevices); 62 switches::kTouchDevices);
65 63
66 if (!touch_devices.empty()) { 64 if (!touch_devices.empty()) {
67 std::vector<std::string> devs; 65 std::vector<std::string> devs;
68 std::vector<unsigned int> device_ids; 66 std::vector<int> device_ids;
69 unsigned int devid; 67 int devid;
70 base::SplitString(touch_devices, ',', &devs); 68 base::SplitString(touch_devices, ',', &devs);
71 for (std::vector<std::string>::iterator iter = devs.begin(); 69 for (std::vector<std::string>::iterator iter = devs.begin();
72 iter != devs.end(); ++iter) { 70 iter != devs.end(); ++iter) {
73 if (base::StringToInt(*iter, reinterpret_cast<int*>(&devid))) 71 if (base::StringToInt(*iter, reinterpret_cast<int*>(&devid)))
74 device_ids.push_back(devid); 72 device_ids.push_back(devid);
75 else 73 else
76 DLOG(WARNING) << "Invalid touch-device id: " << *iter; 74 DLOG(WARNING) << "Invalid touch-device id: " << *iter;
77 } 75 }
78 ui::TouchFactory::GetInstance()->SetTouchDeviceList(device_ids); 76 ui::TouchFactory::GetInstance()->SetTouchDeviceList(device_ids);
79 } 77 }
80 } 78 }
81 79
82 void TouchFactory::UpdateDeviceList(Display* display) { 80 void TouchFactory::UpdateDeviceList(Display* display) {
83 // Detect touch devices. 81 // Detect touch devices.
84 touch_device_lookup_.reset(); 82 touch_device_lookup_.reset();
85 touch_device_list_.clear(); 83 touch_device_list_.clear();
86 touchscreen_ids_.clear(); 84 touchscreen_ids_.clear();
87 max_touch_points_ = -1;
88
89 #if !defined(USE_XI2_MT)
90 // NOTE: The new API for retrieving the list of devices (XIQueryDevice) does
91 // not provide enough information to detect a touch device. As a result, the
92 // old version of query function (XListInputDevices) is used instead.
93 // If XInput2 is not supported, this will return null (with count of -1) so
94 // we assume there cannot be any touch devices.
95 // With XI2.1 or older, we allow only single touch devices.
96 XDeviceList dev_list =
97 DeviceListCacheX::GetInstance()->GetXDeviceList(display);
98 Atom xi_touchscreen = XInternAtom(display, XI_TOUCHSCREEN, false);
99 for (int i = 0; i < dev_list.count; i++) {
100 if (dev_list[i].type == xi_touchscreen) {
101 touch_device_lookup_[dev_list[i].id] = true;
102 touch_device_list_[dev_list[i].id] = false;
103 }
104 }
105 #endif
106 85
107 if (!DeviceDataManagerX11::GetInstance()->IsXInput2Available()) 86 if (!DeviceDataManagerX11::GetInstance()->IsXInput2Available())
108 return; 87 return;
109 88
110 // Instead of asking X for the list of devices all the time, let's maintain a 89 // Instead of asking X for the list of devices all the time, let's maintain a
111 // list of pointer devices we care about. 90 // list of pointer devices we care about.
112 // It should not be necessary to select for slave devices. XInput2 provides 91 // It should not be necessary to select for slave devices. XInput2 provides
113 // enough information to the event callback to decide which slave device 92 // enough information to the event callback to decide which slave device
114 // triggered the event, thus decide whether the 'pointer event' is a 93 // triggered the event, thus decide whether the 'pointer event' is a
115 // 'mouse event' or a 'touch event'. 94 // 'mouse event' or a 'touch event'.
116 // However, on some desktops, some events from a master pointer are 95 // However, on some desktops, some events from a master pointer are
117 // not delivered to the client. So we select for slave devices instead. 96 // not delivered to the client. So we select for slave devices instead.
118 // If the touch device has 'GrabDevice' set and 'SendCoreEvents' unset (which 97 // If the touch device has 'GrabDevice' set and 'SendCoreEvents' unset (which
119 // is possible), then the device is detected as a floating device, and a 98 // is possible), then the device is detected as a floating device, and a
120 // floating device is not connected to a master device. So it is necessary to 99 // floating device is not connected to a master device. So it is necessary to
121 // also select on the floating devices. 100 // also select on the floating devices.
122 pointer_device_lookup_.reset(); 101 pointer_device_lookup_.reset();
123 XIDeviceList xi_dev_list = 102 const XIDeviceList& xi_dev_list =
124 DeviceListCacheX::GetInstance()->GetXI2DeviceList(display); 103 DeviceListCacheX11::GetInstance()->GetXI2DeviceList(display);
125 for (int i = 0; i < xi_dev_list.count; i++) { 104 for (int i = 0; i < xi_dev_list.count; i++) {
126 XIDeviceInfo* devinfo = xi_dev_list.devices + i; 105 const XIDeviceInfo& devinfo = xi_dev_list[i];
127 if (devinfo->use == XIFloatingSlave || devinfo->use == XIMasterPointer) { 106 if (devinfo.use == XIFloatingSlave || devinfo.use == XIMasterPointer) {
128 #if defined(USE_XI2_MT) 107 for (int k = 0; k < devinfo.num_classes; ++k) {
129 for (int k = 0; k < devinfo->num_classes; ++k) { 108 XIAnyClassInfo* xiclassinfo = devinfo.classes[k];
130 XIAnyClassInfo* xiclassinfo = devinfo->classes[k];
131 if (xiclassinfo->type == XITouchClass) { 109 if (xiclassinfo->type == XITouchClass) {
132 XITouchClassInfo* tci = 110 XITouchClassInfo* tci =
133 reinterpret_cast<XITouchClassInfo*>(xiclassinfo); 111 reinterpret_cast<XITouchClassInfo*>(xiclassinfo);
134 // Only care direct touch device (such as touch screen) right now 112 // Only care direct touch device (such as touch screen) right now
135 if (tci->mode == XIDirectTouch) { 113 if (tci->mode == XIDirectTouch) {
136 touch_device_lookup_[devinfo->deviceid] = true; 114 touch_device_lookup_[devinfo.deviceid] = true;
137 touch_device_list_[devinfo->deviceid] = true; 115 touch_device_list_[devinfo.deviceid] = true;
138 if (tci->num_touches > 0 && tci->num_touches > max_touch_points_)
139 max_touch_points_ = tci->num_touches;
140 } 116 }
141 } 117 }
142 } 118 }
143 #endif 119 pointer_device_lookup_[devinfo.deviceid] = true;
144 pointer_device_lookup_[devinfo->deviceid] = true; 120 } else if (devinfo.use == XIMasterKeyboard) {
145 } else if (devinfo->use == XIMasterKeyboard) { 121 virtual_core_keyboard_device_ = devinfo.deviceid;
146 virtual_core_keyboard_device_ = devinfo->deviceid;
147 } 122 }
148 123
149 #if defined(USE_XI2_MT) 124 if (devinfo.use == XIFloatingSlave || devinfo.use == XISlavePointer) {
150 if (devinfo->use == XIFloatingSlave || devinfo->use == XISlavePointer) { 125 for (int k = 0; k < devinfo.num_classes; ++k) {
151 for (int k = 0; k < devinfo->num_classes; ++k) { 126 XIAnyClassInfo* xiclassinfo = devinfo.classes[k];
152 XIAnyClassInfo* xiclassinfo = devinfo->classes[k];
153 if (xiclassinfo->type == XITouchClass) { 127 if (xiclassinfo->type == XITouchClass) {
154 XITouchClassInfo* tci = 128 XITouchClassInfo* tci =
155 reinterpret_cast<XITouchClassInfo*>(xiclassinfo); 129 reinterpret_cast<XITouchClassInfo*>(xiclassinfo);
156 // Only care direct touch device (such as touch screen) right now 130 // Only care direct touch device (such as touch screen) right now
157 if (tci->mode == XIDirectTouch) 131 if (tci->mode == XIDirectTouch)
158 CacheTouchscreenIds(display, devinfo->deviceid); 132 CacheTouchscreenIds(devinfo.deviceid);
133 if (devinfo.use == XISlavePointer) {
134 device_master_id_list_[devinfo.deviceid] = devinfo.attachment;
135 // If the slave device is direct touch device, we also set its
136 // master device to be touch device.
137 touch_device_lookup_[devinfo.attachment] = true;
138 touch_device_list_[devinfo.attachment] = true;
139 }
159 } 140 }
160 } 141 }
161 } 142 }
162 #endif
163 } 143 }
164 } 144 }
165 145
166 bool TouchFactory::ShouldProcessXI2Event(XEvent* xev) { 146 bool TouchFactory::ShouldProcessXI2Event(XEvent* xev) {
167 DCHECK_EQ(GenericEvent, xev->type); 147 DCHECK_EQ(GenericEvent, xev->type);
168 XIEvent* event = static_cast<XIEvent*>(xev->xcookie.data); 148 XIEvent* event = static_cast<XIEvent*>(xev->xcookie.data);
169 XIDeviceEvent* xiev = reinterpret_cast<XIDeviceEvent*>(event); 149 XIDeviceEvent* xiev = reinterpret_cast<XIDeviceEvent*>(event);
170 150
171 #if defined(USE_XI2_MT) 151 if (event->evtype == XI_TouchBegin ||
172 if (event->evtype == XI_TouchBegin || event->evtype == XI_TouchUpdate || 152 event->evtype == XI_TouchUpdate ||
173 event->evtype == XI_TouchEnd) { 153 event->evtype == XI_TouchEnd) {
174 return !touch_events_disabled_ && IsTouchDevice(xiev->deviceid); 154 // Since SetupXI2ForXWindow() selects events from all devices, for a
155 // touchscreen attached to a master pointer device, X11 sends two
156 // events for each touch: one from the slave (deviceid == the id of
157 // the touchscreen device), and one from the master (deviceid == the
158 // id of the master pointer device). Instead of processing both
159 // events, discard the event that comes from the slave, and only
160 // allow processing the event coming from the master.
161 // For a 'floating' touchscreen device, X11 sends only one event for
162 // each touch, with both deviceid and sourceid set to the id of the
163 // touchscreen device.
164 bool is_from_master_or_float = touch_device_list_[xiev->deviceid];
165 bool is_from_slave_device = !is_from_master_or_float
166 && xiev->sourceid == xiev->deviceid;
167 return !touch_events_disabled_ &&
168 IsTouchDevice(xiev->deviceid) &&
169 !is_from_slave_device;
175 } 170 }
176 #endif 171
177 // Make sure only key-events from the virtual core keyboard are processed. 172 // Make sure only key-events from the virtual core keyboard are processed.
178 if (event->evtype == XI_KeyPress || event->evtype == XI_KeyRelease) { 173 if (event->evtype == XI_KeyPress || event->evtype == XI_KeyRelease) {
179 return (virtual_core_keyboard_device_ < 0) || 174 return (virtual_core_keyboard_device_ < 0) ||
180 (virtual_core_keyboard_device_ == xiev->deviceid); 175 (virtual_core_keyboard_device_ == xiev->deviceid);
181 } 176 }
182 177
183 if (event->evtype != XI_ButtonPress && event->evtype != XI_ButtonRelease && 178 if (event->evtype != XI_ButtonPress &&
179 event->evtype != XI_ButtonRelease &&
184 event->evtype != XI_Motion) 180 event->evtype != XI_Motion)
185 return true; 181 return true;
186 182
187 if (!pointer_device_lookup_[xiev->deviceid]) 183 if (!pointer_device_lookup_[xiev->deviceid])
188 return false; 184 return false;
189 185
190 return IsTouchDevice(xiev->deviceid) ? !touch_events_disabled_ : true; 186 return IsTouchDevice(xiev->deviceid) ? !touch_events_disabled_ : true;
191 } 187 }
192 188
193 void TouchFactory::SetupXI2ForXWindow(Window window) { 189 void TouchFactory::SetupXI2ForXWindow(Window window) {
194 // Setup mask for mouse events. It is possible that a device is loaded/plugged 190 // Setup mask for mouse events. It is possible that a device is loaded/plugged
195 // in after we have setup XInput2 on a window. In such cases, we need to 191 // in after we have setup XInput2 on a window. In such cases, we need to
196 // either resetup XInput2 for the window, so that we get events from the new 192 // either resetup XInput2 for the window, so that we get events from the new
197 // device, or we need to listen to events from all devices, and then filter 193 // device, or we need to listen to events from all devices, and then filter
198 // the events from uninteresting devices. We do the latter because that's 194 // the events from uninteresting devices. We do the latter because that's
199 // simpler. 195 // simpler.
200 196
201 XDisplay* display = gfx::GetXDisplay(); 197 XDisplay* display = gfx::GetXDisplay();
202 198
203 unsigned char mask[XIMaskLen(XI_LASTEVENT)]; 199 unsigned char mask[XIMaskLen(XI_LASTEVENT)];
204 memset(mask, 0, sizeof(mask)); 200 memset(mask, 0, sizeof(mask));
205 201
206 #if defined(USE_XI2_MT)
207 XISetMask(mask, XI_TouchBegin); 202 XISetMask(mask, XI_TouchBegin);
208 XISetMask(mask, XI_TouchUpdate); 203 XISetMask(mask, XI_TouchUpdate);
209 XISetMask(mask, XI_TouchEnd); 204 XISetMask(mask, XI_TouchEnd);
210 #endif 205
211 XISetMask(mask, XI_ButtonPress); 206 XISetMask(mask, XI_ButtonPress);
212 XISetMask(mask, XI_ButtonRelease); 207 XISetMask(mask, XI_ButtonRelease);
213 XISetMask(mask, XI_Motion); 208 XISetMask(mask, XI_Motion);
214 #if defined(OS_CHROMEOS) 209 #if defined(OS_CHROMEOS)
210 // XGrabKey() must be replaced with XI2 keyboard grab if XI2 key events are
211 // enabled on desktop Linux.
215 if (base::SysInfo::IsRunningOnChromeOS()) { 212 if (base::SysInfo::IsRunningOnChromeOS()) {
216 XISetMask(mask, XI_KeyPress); 213 XISetMask(mask, XI_KeyPress);
217 XISetMask(mask, XI_KeyRelease); 214 XISetMask(mask, XI_KeyRelease);
218 } 215 }
219 #endif 216 #endif
220 217
221 XIEventMask evmask; 218 XIEventMask evmask;
222 evmask.deviceid = XIAllDevices; 219 evmask.deviceid = XIAllDevices;
223 evmask.mask_len = sizeof(mask); 220 evmask.mask_len = sizeof(mask);
224 evmask.mask = mask; 221 evmask.mask = mask;
225 XISelectEvents(display, window, &evmask, 1); 222 XISelectEvents(display, window, &evmask, 1);
226 XFlush(display); 223 XFlush(display);
227 } 224 }
228 225
229 void TouchFactory::SetTouchDeviceList( 226 void TouchFactory::SetTouchDeviceList(const std::vector<int>& devices) {
230 const std::vector<unsigned int>& devices) {
231 touch_device_lookup_.reset(); 227 touch_device_lookup_.reset();
232 touch_device_list_.clear(); 228 touch_device_list_.clear();
233 for (std::vector<unsigned int>::const_iterator iter = devices.begin(); 229 for (int deviceid : devices) {
234 iter != devices.end(); ++iter) { 230 DCHECK(IsValidDevice(deviceid));
235 DCHECK(*iter < touch_device_lookup_.size()); 231 touch_device_lookup_[deviceid] = true;
236 touch_device_lookup_[*iter] = true; 232 touch_device_list_[deviceid] = false;
237 touch_device_list_[*iter] = false; 233 if (device_master_id_list_.find(deviceid) != device_master_id_list_.end()) {
234 // When we set the device through the "--touch-devices" flag to slave
235 // touch device, we also set its master device to be touch device.
236 touch_device_lookup_[device_master_id_list_[deviceid]] = true;
237 touch_device_list_[device_master_id_list_[deviceid]] = false;
238 }
238 } 239 }
239 } 240 }
240 241
241 bool TouchFactory::IsTouchDevice(unsigned deviceid) const { 242 bool TouchFactory::IsValidDevice(int deviceid) const {
242 return deviceid < touch_device_lookup_.size() ? touch_device_lookup_[deviceid] 243 return (deviceid >= 0) &&
243 : false; 244 (static_cast<size_t>(deviceid) < touch_device_lookup_.size());
244 } 245 }
245 246
246 bool TouchFactory::IsMultiTouchDevice(unsigned int deviceid) const { 247 bool TouchFactory::IsTouchDevice(int deviceid) const {
247 return (deviceid < touch_device_lookup_.size() && 248 return IsValidDevice(deviceid) ? touch_device_lookup_[deviceid] : false;
248 touch_device_lookup_[deviceid]) 249 }
250
251 bool TouchFactory::IsMultiTouchDevice(int deviceid) const {
252 return (IsValidDevice(deviceid) && touch_device_lookup_[deviceid])
249 ? touch_device_list_.find(deviceid)->second 253 ? touch_device_list_.find(deviceid)->second
250 : false; 254 : false;
251 } 255 }
252 256
253 bool TouchFactory::QuerySlotForTrackingID(uint32 tracking_id, int* slot) { 257 bool TouchFactory::QuerySlotForTrackingID(uint32 tracking_id, int* slot) {
254 if (!id_generator_.HasGeneratedIDFor(tracking_id)) 258 if (!id_generator_.HasGeneratedIDFor(tracking_id))
255 return false; 259 return false;
256 *slot = static_cast<int>(id_generator_.GetGeneratedID(tracking_id)); 260 *slot = static_cast<int>(id_generator_.GetGeneratedID(tracking_id));
257 return true; 261 return true;
258 } 262 }
259 263
260 int TouchFactory::GetSlotForTrackingID(uint32 tracking_id) { 264 int TouchFactory::GetSlotForTrackingID(uint32 tracking_id) {
261 return id_generator_.GetGeneratedID(tracking_id); 265 return id_generator_.GetGeneratedID(tracking_id);
262 } 266 }
263 267
264 void TouchFactory::AcquireSlotForTrackingID(uint32 tracking_id) {
265 tracking_id_refcounts_[tracking_id]++;
266 }
267
268 void TouchFactory::ReleaseSlotForTrackingID(uint32 tracking_id) { 268 void TouchFactory::ReleaseSlotForTrackingID(uint32 tracking_id) {
269 tracking_id_refcounts_[tracking_id]--; 269 id_generator_.ReleaseNumber(tracking_id);
270 if (tracking_id_refcounts_[tracking_id] == 0)
271 id_generator_.ReleaseNumber(tracking_id);
272 } 270 }
273 271
274 bool TouchFactory::IsTouchDevicePresent() { 272 bool TouchFactory::IsTouchDevicePresent() {
275 return !touch_events_disabled_ && touch_device_lookup_.any(); 273 return !touch_events_disabled_ && touch_device_lookup_.any();
276 } 274 }
277 275
278 int TouchFactory::GetMaxTouchPoints() const {
279 return max_touch_points_;
280 }
281
282 void TouchFactory::ResetForTest() { 276 void TouchFactory::ResetForTest() {
283 pointer_device_lookup_.reset(); 277 pointer_device_lookup_.reset();
284 touch_device_lookup_.reset(); 278 touch_device_lookup_.reset();
285 touch_events_disabled_ = false; 279 touch_events_disabled_ = false;
286 touch_device_list_.clear(); 280 touch_device_list_.clear();
287 touchscreen_ids_.clear(); 281 touchscreen_ids_.clear();
288 tracking_id_refcounts_.clear();
289 max_touch_points_ = -1;
290 id_generator_.ResetForTest(); 282 id_generator_.ResetForTest();
291 } 283 }
292 284
293 void TouchFactory::SetTouchDeviceForTest( 285 void TouchFactory::SetTouchDeviceForTest(
294 const std::vector<unsigned int>& devices) { 286 const std::vector<int>& devices) {
295 touch_device_lookup_.reset(); 287 touch_device_lookup_.reset();
296 touch_device_list_.clear(); 288 touch_device_list_.clear();
297 for (std::vector<unsigned int>::const_iterator iter = devices.begin(); 289 for (std::vector<int>::const_iterator iter = devices.begin();
298 iter != devices.end(); ++iter) { 290 iter != devices.end(); ++iter) {
299 DCHECK(*iter < touch_device_lookup_.size()); 291 DCHECK(IsValidDevice(*iter));
300 touch_device_lookup_[*iter] = true; 292 touch_device_lookup_[*iter] = true;
301 touch_device_list_[*iter] = true; 293 touch_device_list_[*iter] = true;
302 } 294 }
303 touch_events_disabled_ = false; 295 touch_events_disabled_ = false;
304 } 296 }
305 297
306 void TouchFactory::SetPointerDeviceForTest( 298 void TouchFactory::SetPointerDeviceForTest(
307 const std::vector<unsigned int>& devices) { 299 const std::vector<int>& devices) {
308 pointer_device_lookup_.reset(); 300 pointer_device_lookup_.reset();
309 for (std::vector<unsigned int>::const_iterator iter = devices.begin(); 301 for (std::vector<int>::const_iterator iter = devices.begin();
310 iter != devices.end(); ++iter) { 302 iter != devices.end(); ++iter) {
311 pointer_device_lookup_[*iter] = true; 303 pointer_device_lookup_[*iter] = true;
312 } 304 }
313 } 305 }
314 306
315 void TouchFactory::CacheTouchscreenIds(Display* display, int device_id) { 307 void TouchFactory::CacheTouchscreenIds(int device_id) {
316 XDevice* device = XOpenDevice(display, device_id); 308 if (!DeviceDataManager::HasInstance())
317 if (!device)
318 return; 309 return;
319 310 std::vector<TouchscreenDevice> touchscreens =
320 Atom actual_type_return; 311 DeviceDataManager::GetInstance()->touchscreen_devices();
321 int actual_format_return; 312 const auto it =
322 unsigned long nitems_return; 313 std::find_if(touchscreens.begin(), touchscreens.end(),
323 unsigned long bytes_after_return; 314 [device_id](const TouchscreenDevice& touchscreen) {
324 unsigned char* prop_return; 315 return touchscreen.id == device_id;
325 316 });
326 const char kDeviceProductIdString[] = "Device Product ID"; 317 // Internal displays will have a vid and pid of 0. Ignore them.
327 Atom device_product_id_atom = 318 if (it != touchscreens.end() && it->vendor_id && it->product_id)
328 XInternAtom(display, kDeviceProductIdString, false); 319 touchscreen_ids_.insert(std::make_pair(it->vendor_id, it->product_id));
329
330 if (device_product_id_atom != None &&
331 XGetDeviceProperty(display, device, device_product_id_atom, 0, 2, False,
332 XA_INTEGER, &actual_type_return, &actual_format_return,
333 &nitems_return, &bytes_after_return,
334 &prop_return) == Success) {
335 if (actual_type_return == XA_INTEGER && actual_format_return == 32 &&
336 nitems_return == 2) {
337 // An actual_format_return of 32 implies that the returned data is an
338 // array of longs. See the description of |prop_return| in `man
339 // XGetDeviceProperty` for details.
340 long* ptr = reinterpret_cast<long*>(prop_return);
341
342 // Internal displays will have a vid and pid of 0. Ignore them.
343 // ptr[0] is the vid, and ptr[1] is the pid.
344 if (ptr[0] || ptr[1])
345 touchscreen_ids_.insert(std::make_pair(ptr[0], ptr[1]));
346 }
347 XFree(prop_return);
348 }
349
350 XCloseDevice(display, device);
351 } 320 }
352 321
353 } // namespace ui 322 } // namespace ui
OLDNEW
« no previous file with comments | « ui/events/devices/x11/touch_factory_x11.h ('k') | ui/events/event.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698