Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / ui / events / x / touch_factory_x11.cc
blobe016ea642a197d887fe3591619d8465db37dab92
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.
5 #include "ui/events/x/touch_factory_x11.h"
7 #include <X11/Xatom.h>
8 #include <X11/cursorfont.h>
9 #include <X11/extensions/XInput.h>
10 #include <X11/extensions/XInput2.h>
11 #include <X11/extensions/XIproto.h>
13 #include "base/basictypes.h"
14 #include "base/command_line.h"
15 #include "base/compiler_specific.h"
16 #include "base/logging.h"
17 #include "base/memory/singleton.h"
18 #include "base/message_loop/message_loop.h"
19 #include "base/strings/string_number_conversions.h"
20 #include "base/strings/string_split.h"
21 #include "base/sys_info.h"
22 #include "ui/events/event_switches.h"
23 #include "ui/events/x/device_data_manager_x11.h"
24 #include "ui/events/x/device_list_cache_x.h"
25 #include "ui/gfx/x/x11_types.h"
27 namespace ui {
29 TouchFactory::TouchFactory()
30 : pointer_device_lookup_(),
31 touch_device_available_(false),
32 touch_events_disabled_(false),
33 touch_device_list_(),
34 max_touch_points_(-1),
35 id_generator_(0) {
36 if (!DeviceDataManagerX11::GetInstance()->IsXInput2Available())
37 return;
39 XDisplay* display = gfx::GetXDisplay();
40 UpdateDeviceList(display);
42 CommandLine* cmdline = CommandLine::ForCurrentProcess();
43 touch_events_disabled_ = cmdline->HasSwitch(switches::kTouchEvents) &&
44 cmdline->GetSwitchValueASCII(switches::kTouchEvents) ==
45 switches::kTouchEventsDisabled;
48 TouchFactory::~TouchFactory() {
51 // static
52 TouchFactory* TouchFactory::GetInstance() {
53 return Singleton<TouchFactory>::get();
56 // static
57 void TouchFactory::SetTouchDeviceListFromCommandLine() {
58 // Get a list of pointer-devices that should be treated as touch-devices.
59 // This is primarily used for testing/debugging touch-event processing when a
60 // touch-device isn't available.
61 std::string touch_devices =
62 CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
63 switches::kTouchDevices);
65 if (!touch_devices.empty()) {
66 std::vector<std::string> devs;
67 std::vector<unsigned int> device_ids;
68 unsigned int devid;
69 base::SplitString(touch_devices, ',', &devs);
70 for (std::vector<std::string>::iterator iter = devs.begin();
71 iter != devs.end(); ++iter) {
72 if (base::StringToInt(*iter, reinterpret_cast<int*>(&devid)))
73 device_ids.push_back(devid);
74 else
75 DLOG(WARNING) << "Invalid touch-device id: " << *iter;
77 ui::TouchFactory::GetInstance()->SetTouchDeviceList(device_ids);
81 void TouchFactory::UpdateDeviceList(Display* display) {
82 // Detect touch devices.
83 touch_device_available_ = false;
84 touch_device_lookup_.reset();
85 touch_device_list_.clear();
86 touchscreen_ids_.clear();
87 max_touch_points_ = -1;
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 touch_device_available_ = true;
106 #endif
108 if (!DeviceDataManagerX11::GetInstance()->IsXInput2Available())
109 return;
111 // Instead of asking X for the list of devices all the time, let's maintain a
112 // list of pointer devices we care about.
113 // It should not be necessary to select for slave devices. XInput2 provides
114 // enough information to the event callback to decide which slave device
115 // triggered the event, thus decide whether the 'pointer event' is a
116 // 'mouse event' or a 'touch event'.
117 // However, on some desktops, some events from a master pointer are
118 // not delivered to the client. So we select for slave devices instead.
119 // If the touch device has 'GrabDevice' set and 'SendCoreEvents' unset (which
120 // is possible), then the device is detected as a floating device, and a
121 // floating device is not connected to a master device. So it is necessary to
122 // also select on the floating devices.
123 pointer_device_lookup_.reset();
124 XIDeviceList xi_dev_list =
125 DeviceListCacheX::GetInstance()->GetXI2DeviceList(display);
126 for (int i = 0; i < xi_dev_list.count; i++) {
127 XIDeviceInfo* devinfo = xi_dev_list.devices + i;
128 if (devinfo->use == XIFloatingSlave || devinfo->use == XIMasterPointer) {
129 #if defined(USE_XI2_MT)
130 for (int k = 0; k < devinfo->num_classes; ++k) {
131 XIAnyClassInfo* xiclassinfo = devinfo->classes[k];
132 if (xiclassinfo->type == XITouchClass) {
133 XITouchClassInfo* tci =
134 reinterpret_cast<XITouchClassInfo*>(xiclassinfo);
135 // Only care direct touch device (such as touch screen) right now
136 if (tci->mode == XIDirectTouch) {
137 touch_device_lookup_[devinfo->deviceid] = true;
138 touch_device_list_[devinfo->deviceid] = true;
139 touch_device_available_ = true;
140 if (tci->num_touches > 0 && tci->num_touches > max_touch_points_)
141 max_touch_points_ = tci->num_touches;
145 #endif
146 pointer_device_lookup_[devinfo->deviceid] = true;
149 #if defined(USE_XI2_MT)
150 if (devinfo->use == XIFloatingSlave || devinfo->use == XISlavePointer) {
151 for (int k = 0; k < devinfo->num_classes; ++k) {
152 XIAnyClassInfo* xiclassinfo = devinfo->classes[k];
153 if (xiclassinfo->type == XITouchClass) {
154 XITouchClassInfo* tci =
155 reinterpret_cast<XITouchClassInfo*>(xiclassinfo);
156 // Only care direct touch device (such as touch screen) right now
157 if (tci->mode == XIDirectTouch)
158 CacheTouchscreenIds(display, devinfo->deviceid);
162 #endif
166 bool TouchFactory::ShouldProcessXI2Event(XEvent* xev) {
167 DCHECK_EQ(GenericEvent, xev->type);
168 XIEvent* event = static_cast<XIEvent*>(xev->xcookie.data);
169 XIDeviceEvent* xiev = reinterpret_cast<XIDeviceEvent*>(event);
171 #if defined(USE_XI2_MT)
172 if (event->evtype == XI_TouchBegin ||
173 event->evtype == XI_TouchUpdate ||
174 event->evtype == XI_TouchEnd) {
175 return !touch_events_disabled_ && IsTouchDevice(xiev->deviceid);
177 #endif
178 // Make sure only key-events from the master device are processed.
179 if (event->evtype == XI_KeyPress || event->evtype == XI_KeyRelease)
180 return xiev->deviceid == xiev->sourceid;
182 if (event->evtype != XI_ButtonPress &&
183 event->evtype != XI_ButtonRelease &&
184 event->evtype != XI_Motion)
185 return true;
187 if (!pointer_device_lookup_[xiev->deviceid])
188 return false;
190 return IsTouchDevice(xiev->deviceid) ? !touch_events_disabled_ : true;
193 void TouchFactory::SetupXI2ForXWindow(Window window) {
194 // 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
196 // 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
198 // the events from uninteresting devices. We do the latter because that's
199 // simpler.
201 XDisplay* display = gfx::GetXDisplay();
203 unsigned char mask[XIMaskLen(XI_LASTEVENT)];
204 memset(mask, 0, sizeof(mask));
206 #if defined(USE_XI2_MT)
207 XISetMask(mask, XI_TouchBegin);
208 XISetMask(mask, XI_TouchUpdate);
209 XISetMask(mask, XI_TouchEnd);
210 #endif
211 XISetMask(mask, XI_ButtonPress);
212 XISetMask(mask, XI_ButtonRelease);
213 XISetMask(mask, XI_Motion);
214 #if defined(OS_CHROMEOS)
215 if (base::SysInfo::IsRunningOnChromeOS()) {
216 XISetMask(mask, XI_KeyPress);
217 XISetMask(mask, XI_KeyRelease);
219 #endif
221 XIEventMask evmask;
222 evmask.deviceid = XIAllDevices;
223 evmask.mask_len = sizeof(mask);
224 evmask.mask = mask;
225 XISelectEvents(display, window, &evmask, 1);
226 XFlush(display);
229 void TouchFactory::SetTouchDeviceList(
230 const std::vector<unsigned int>& devices) {
231 touch_device_lookup_.reset();
232 touch_device_list_.clear();
233 for (std::vector<unsigned int>::const_iterator iter = devices.begin();
234 iter != devices.end(); ++iter) {
235 DCHECK(*iter < touch_device_lookup_.size());
236 touch_device_lookup_[*iter] = true;
237 touch_device_list_[*iter] = false;
241 bool TouchFactory::IsTouchDevice(unsigned deviceid) const {
242 return deviceid < touch_device_lookup_.size() ?
243 touch_device_lookup_[deviceid] : false;
246 bool TouchFactory::IsMultiTouchDevice(unsigned int deviceid) const {
247 return (deviceid < touch_device_lookup_.size() &&
248 touch_device_lookup_[deviceid]) ?
249 touch_device_list_.find(deviceid)->second :
250 false;
253 bool TouchFactory::QuerySlotForTrackingID(uint32 tracking_id, int* slot) {
254 if (!id_generator_.HasGeneratedIDFor(tracking_id))
255 return false;
256 *slot = static_cast<int>(id_generator_.GetGeneratedID(tracking_id));
257 return true;
260 int TouchFactory::GetSlotForTrackingID(uint32 tracking_id) {
261 return id_generator_.GetGeneratedID(tracking_id);
264 void TouchFactory::AcquireSlotForTrackingID(uint32 tracking_id) {
265 tracking_id_refcounts_[tracking_id]++;
268 void TouchFactory::ReleaseSlotForTrackingID(uint32 tracking_id) {
269 tracking_id_refcounts_[tracking_id]--;
270 if (tracking_id_refcounts_[tracking_id] == 0)
271 id_generator_.ReleaseNumber(tracking_id);
274 bool TouchFactory::IsTouchDevicePresent() {
275 return !touch_events_disabled_ && touch_device_available_;
278 int TouchFactory::GetMaxTouchPoints() const {
279 return max_touch_points_;
282 void TouchFactory::ResetForTest() {
283 pointer_device_lookup_.reset();
284 touch_device_lookup_.reset();
285 touch_device_available_ = false;
286 touch_events_disabled_ = false;
287 touch_device_list_.clear();
288 touchscreen_ids_.clear();
289 tracking_id_refcounts_.clear();
290 max_touch_points_ = -1;
291 id_generator_.ResetForTest();
294 void TouchFactory::SetTouchDeviceForTest(
295 const std::vector<unsigned int>& devices) {
296 touch_device_lookup_.reset();
297 touch_device_list_.clear();
298 for (std::vector<unsigned int>::const_iterator iter = devices.begin();
299 iter != devices.end(); ++iter) {
300 DCHECK(*iter < touch_device_lookup_.size());
301 touch_device_lookup_[*iter] = true;
302 touch_device_list_[*iter] = true;
304 touch_device_available_ = true;
305 touch_events_disabled_ = false;
308 void TouchFactory::SetPointerDeviceForTest(
309 const std::vector<unsigned int>& devices) {
310 pointer_device_lookup_.reset();
311 for (std::vector<unsigned int>::const_iterator iter = devices.begin();
312 iter != devices.end(); ++iter) {
313 pointer_device_lookup_[*iter] = true;
317 void TouchFactory::CacheTouchscreenIds(Display* display, int device_id) {
318 XDevice* device = XOpenDevice(display, device_id);
319 if (!device)
320 return;
322 Atom actual_type_return;
323 int actual_format_return;
324 unsigned long nitems_return;
325 unsigned long bytes_after_return;
326 unsigned char *prop_return;
328 const char kDeviceProductIdString[] = "Device Product ID";
329 Atom device_product_id_atom =
330 XInternAtom(display, kDeviceProductIdString, false);
332 if (device_product_id_atom != None &&
333 XGetDeviceProperty(display, device, device_product_id_atom, 0, 2,
334 False, XA_INTEGER, &actual_type_return,
335 &actual_format_return, &nitems_return,
336 &bytes_after_return, &prop_return) == Success) {
337 if (actual_type_return == XA_INTEGER &&
338 actual_format_return == 32 &&
339 nitems_return == 2) {
340 // An actual_format_return of 32 implies that the returned data is an
341 // array of longs. See the description of |prop_return| in `man
342 // XGetDeviceProperty` for details.
343 long* ptr = reinterpret_cast<long*>(prop_return);
345 // Internal displays will have a vid and pid of 0. Ignore them.
346 // ptr[0] is the vid, and ptr[1] is the pid.
347 if (ptr[0] || ptr[1])
348 touchscreen_ids_.insert(std::make_pair(ptr[0], ptr[1]));
350 XFree(prop_return);
353 XCloseDevice(display, device);
356 } // namespace ui