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/devices/x11/touch_factory_x11.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/devices/x11/device_data_manager_x11.h"
23 #include "ui/events/devices/x11/device_list_cache_x11.h"
24 #include "ui/events/event_switches.h"
25 #include "ui/gfx/x/x11_types.h"
29 TouchFactory::TouchFactory()
30 : pointer_device_lookup_(),
31 touch_events_disabled_(false),
33 virtual_core_keyboard_device_(-1),
35 if (!DeviceDataManagerX11::GetInstance()->IsXInput2Available())
38 XDisplay
* display
= gfx::GetXDisplay();
39 UpdateDeviceList(display
);
41 base::CommandLine
* cmdline
= base::CommandLine::ForCurrentProcess();
42 touch_events_disabled_
= cmdline
->HasSwitch(switches::kTouchEvents
) &&
43 cmdline
->GetSwitchValueASCII(switches::kTouchEvents
) ==
44 switches::kTouchEventsDisabled
;
47 TouchFactory::~TouchFactory() {
51 TouchFactory
* TouchFactory::GetInstance() {
52 return Singleton
<TouchFactory
>::get();
56 void TouchFactory::SetTouchDeviceListFromCommandLine() {
57 // Get a list of pointer-devices that should be treated as touch-devices.
58 // This is primarily used for testing/debugging touch-event processing when a
59 // touch-device isn't available.
60 std::string touch_devices
=
61 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
62 switches::kTouchDevices
);
64 if (!touch_devices
.empty()) {
65 std::vector
<std::string
> devs
;
66 std::vector
<unsigned int> device_ids
;
68 base::SplitString(touch_devices
, ',', &devs
);
69 for (std::vector
<std::string
>::iterator iter
= devs
.begin();
70 iter
!= devs
.end(); ++iter
) {
71 if (base::StringToInt(*iter
, reinterpret_cast<int*>(&devid
)))
72 device_ids
.push_back(devid
);
74 DLOG(WARNING
) << "Invalid touch-device id: " << *iter
;
76 ui::TouchFactory::GetInstance()->SetTouchDeviceList(device_ids
);
80 void TouchFactory::UpdateDeviceList(Display
* display
) {
81 // Detect touch devices.
82 touch_device_lookup_
.reset();
83 touch_device_list_
.clear();
84 touchscreen_ids_
.clear();
86 if (!DeviceDataManagerX11::GetInstance()->IsXInput2Available())
89 // Instead of asking X for the list of devices all the time, let's maintain a
90 // list of pointer devices we care about.
91 // It should not be necessary to select for slave devices. XInput2 provides
92 // enough information to the event callback to decide which slave device
93 // triggered the event, thus decide whether the 'pointer event' is a
94 // 'mouse event' or a 'touch event'.
95 // However, on some desktops, some events from a master pointer are
96 // not delivered to the client. So we select for slave devices instead.
97 // If the touch device has 'GrabDevice' set and 'SendCoreEvents' unset (which
98 // is possible), then the device is detected as a floating device, and a
99 // floating device is not connected to a master device. So it is necessary to
100 // also select on the floating devices.
101 pointer_device_lookup_
.reset();
102 const XIDeviceList
& xi_dev_list
=
103 DeviceListCacheX11::GetInstance()->GetXI2DeviceList(display
);
104 for (int i
= 0; i
< xi_dev_list
.count
; i
++) {
105 const XIDeviceInfo
& devinfo
= xi_dev_list
[i
];
106 if (devinfo
.use
== XIFloatingSlave
|| devinfo
.use
== XIMasterPointer
) {
107 for (int k
= 0; k
< devinfo
.num_classes
; ++k
) {
108 XIAnyClassInfo
* xiclassinfo
= devinfo
.classes
[k
];
109 if (xiclassinfo
->type
== XITouchClass
) {
110 XITouchClassInfo
* tci
=
111 reinterpret_cast<XITouchClassInfo
*>(xiclassinfo
);
112 // Only care direct touch device (such as touch screen) right now
113 if (tci
->mode
== XIDirectTouch
) {
114 touch_device_lookup_
[devinfo
.deviceid
] = true;
115 touch_device_list_
[devinfo
.deviceid
] = true;
119 pointer_device_lookup_
[devinfo
.deviceid
] = true;
120 } else if (devinfo
.use
== XIMasterKeyboard
) {
121 virtual_core_keyboard_device_
= devinfo
.deviceid
;
124 if (devinfo
.use
== XIFloatingSlave
|| devinfo
.use
== XISlavePointer
) {
125 for (int k
= 0; k
< devinfo
.num_classes
; ++k
) {
126 XIAnyClassInfo
* xiclassinfo
= devinfo
.classes
[k
];
127 if (xiclassinfo
->type
== XITouchClass
) {
128 XITouchClassInfo
* tci
=
129 reinterpret_cast<XITouchClassInfo
*>(xiclassinfo
);
130 // Only care direct touch device (such as touch screen) right now
131 if (tci
->mode
== XIDirectTouch
)
132 CacheTouchscreenIds(display
, devinfo
.deviceid
);
139 bool TouchFactory::ShouldProcessXI2Event(XEvent
* xev
) {
140 DCHECK_EQ(GenericEvent
, xev
->type
);
141 XIEvent
* event
= static_cast<XIEvent
*>(xev
->xcookie
.data
);
142 XIDeviceEvent
* xiev
= reinterpret_cast<XIDeviceEvent
*>(event
);
144 if (event
->evtype
== XI_TouchBegin
||
145 event
->evtype
== XI_TouchUpdate
||
146 event
->evtype
== XI_TouchEnd
) {
147 // Since SetupXI2ForXWindow() selects events from all devices, for a
148 // touchscreen attached to a master pointer device, X11 sends two
149 // events for each touch: one from the slave (deviceid == the id of
150 // the touchscreen device), and one from the master (deviceid == the
151 // id of the master pointer device). Instead of processing both
152 // events, discard the event that comes from the slave, and only
153 // allow processing the event coming from the master.
154 // For a 'floating' touchscreen device, X11 sends only one event for
155 // each touch, with both deviceid and sourceid set to the id of the
156 // touchscreen device.
157 bool is_from_master_or_float
= touch_device_list_
[xiev
->deviceid
];
158 bool is_from_slave_device
= !is_from_master_or_float
159 && xiev
->sourceid
== xiev
->deviceid
;
160 return !touch_events_disabled_
&&
161 IsTouchDevice(xiev
->deviceid
) &&
162 !is_from_slave_device
;
165 // Make sure only key-events from the virtual core keyboard are processed.
166 if (event
->evtype
== XI_KeyPress
|| event
->evtype
== XI_KeyRelease
) {
167 return (virtual_core_keyboard_device_
< 0) ||
168 (virtual_core_keyboard_device_
== xiev
->deviceid
);
171 if (event
->evtype
!= XI_ButtonPress
&&
172 event
->evtype
!= XI_ButtonRelease
&&
173 event
->evtype
!= XI_Motion
)
176 if (!pointer_device_lookup_
[xiev
->deviceid
])
179 return IsTouchDevice(xiev
->deviceid
) ? !touch_events_disabled_
: true;
182 void TouchFactory::SetupXI2ForXWindow(Window window
) {
183 // Setup mask for mouse events. It is possible that a device is loaded/plugged
184 // in after we have setup XInput2 on a window. In such cases, we need to
185 // either resetup XInput2 for the window, so that we get events from the new
186 // device, or we need to listen to events from all devices, and then filter
187 // the events from uninteresting devices. We do the latter because that's
190 XDisplay
* display
= gfx::GetXDisplay();
192 unsigned char mask
[XIMaskLen(XI_LASTEVENT
)];
193 memset(mask
, 0, sizeof(mask
));
195 XISetMask(mask
, XI_TouchBegin
);
196 XISetMask(mask
, XI_TouchUpdate
);
197 XISetMask(mask
, XI_TouchEnd
);
199 XISetMask(mask
, XI_ButtonPress
);
200 XISetMask(mask
, XI_ButtonRelease
);
201 XISetMask(mask
, XI_Motion
);
202 #if defined(OS_CHROMEOS)
203 // XGrabKey() must be replaced with XI2 keyboard grab if XI2 key events are
204 // enabled on desktop Linux.
205 if (base::SysInfo::IsRunningOnChromeOS()) {
206 XISetMask(mask
, XI_KeyPress
);
207 XISetMask(mask
, XI_KeyRelease
);
212 evmask
.deviceid
= XIAllDevices
;
213 evmask
.mask_len
= sizeof(mask
);
215 XISelectEvents(display
, window
, &evmask
, 1);
219 void TouchFactory::SetTouchDeviceList(
220 const std::vector
<unsigned int>& devices
) {
221 touch_device_lookup_
.reset();
222 touch_device_list_
.clear();
223 for (std::vector
<unsigned int>::const_iterator iter
= devices
.begin();
224 iter
!= devices
.end(); ++iter
) {
225 DCHECK(*iter
< touch_device_lookup_
.size());
226 touch_device_lookup_
[*iter
] = true;
227 touch_device_list_
[*iter
] = false;
231 bool TouchFactory::IsTouchDevice(unsigned deviceid
) const {
232 return deviceid
< touch_device_lookup_
.size() ?
233 touch_device_lookup_
[deviceid
] : false;
236 bool TouchFactory::IsMultiTouchDevice(unsigned int deviceid
) const {
237 return (deviceid
< touch_device_lookup_
.size() &&
238 touch_device_lookup_
[deviceid
]) ?
239 touch_device_list_
.find(deviceid
)->second
:
243 bool TouchFactory::QuerySlotForTrackingID(uint32 tracking_id
, int* slot
) {
244 if (!id_generator_
.HasGeneratedIDFor(tracking_id
))
246 *slot
= static_cast<int>(id_generator_
.GetGeneratedID(tracking_id
));
250 int TouchFactory::GetSlotForTrackingID(uint32 tracking_id
) {
251 return id_generator_
.GetGeneratedID(tracking_id
);
254 void TouchFactory::ReleaseSlotForTrackingID(uint32 tracking_id
) {
255 id_generator_
.ReleaseNumber(tracking_id
);
258 bool TouchFactory::IsTouchDevicePresent() {
259 return !touch_events_disabled_
&& touch_device_lookup_
.any();
262 void TouchFactory::ResetForTest() {
263 pointer_device_lookup_
.reset();
264 touch_device_lookup_
.reset();
265 touch_events_disabled_
= false;
266 touch_device_list_
.clear();
267 touchscreen_ids_
.clear();
268 id_generator_
.ResetForTest();
271 void TouchFactory::SetTouchDeviceForTest(
272 const std::vector
<unsigned int>& devices
) {
273 touch_device_lookup_
.reset();
274 touch_device_list_
.clear();
275 for (std::vector
<unsigned int>::const_iterator iter
= devices
.begin();
276 iter
!= devices
.end(); ++iter
) {
277 DCHECK(*iter
< touch_device_lookup_
.size());
278 touch_device_lookup_
[*iter
] = true;
279 touch_device_list_
[*iter
] = true;
281 touch_events_disabled_
= false;
284 void TouchFactory::SetPointerDeviceForTest(
285 const std::vector
<unsigned int>& devices
) {
286 pointer_device_lookup_
.reset();
287 for (std::vector
<unsigned int>::const_iterator iter
= devices
.begin();
288 iter
!= devices
.end(); ++iter
) {
289 pointer_device_lookup_
[*iter
] = true;
293 void TouchFactory::CacheTouchscreenIds(Display
* display
, int device_id
) {
294 XDevice
* device
= XOpenDevice(display
, device_id
);
298 Atom actual_type_return
;
299 int actual_format_return
;
300 unsigned long nitems_return
;
301 unsigned long bytes_after_return
;
302 unsigned char *prop_return
;
304 const char kDeviceProductIdString
[] = "Device Product ID";
305 Atom device_product_id_atom
=
306 XInternAtom(display
, kDeviceProductIdString
, false);
308 if (device_product_id_atom
!= None
&&
309 XGetDeviceProperty(display
, device
, device_product_id_atom
, 0, 2,
310 False
, XA_INTEGER
, &actual_type_return
,
311 &actual_format_return
, &nitems_return
,
312 &bytes_after_return
, &prop_return
) == Success
) {
313 if (actual_type_return
== XA_INTEGER
&&
314 actual_format_return
== 32 &&
315 nitems_return
== 2) {
316 // An actual_format_return of 32 implies that the returned data is an
317 // array of longs. See the description of |prop_return| in `man
318 // XGetDeviceProperty` for details.
319 long* ptr
= reinterpret_cast<long*>(prop_return
);
321 // Internal displays will have a vid and pid of 0. Ignore them.
322 // ptr[0] is the vid, and ptr[1] is the pid.
323 if (ptr
[0] || ptr
[1])
324 touchscreen_ids_
.insert(std::make_pair(ptr
[0], ptr
[1]));
329 XCloseDevice(display
, device
);