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 base::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
<int> device_ids
;
66 for (const base::StringPiece
& dev
:
67 base::SplitStringPiece(touch_devices
, ",", base::TRIM_WHITESPACE
,
68 base::SPLIT_WANT_ALL
)) {
70 if (base::StringToInt(dev
, &devid
))
71 device_ids
.push_back(devid
);
73 DLOG(WARNING
) << "Invalid touch-device id: " << dev
.as_string();
75 ui::TouchFactory::GetInstance()->SetTouchDeviceList(device_ids
);
79 void TouchFactory::UpdateDeviceList(Display
* display
) {
80 // Detect touch devices.
81 touch_device_lookup_
.reset();
82 touch_device_list_
.clear();
83 touchscreen_ids_
.clear();
85 if (!DeviceDataManagerX11::GetInstance()->IsXInput2Available())
88 // Instead of asking X for the list of devices all the time, let's maintain a
89 // list of pointer devices we care about.
90 // It should not be necessary to select for slave devices. XInput2 provides
91 // enough information to the event callback to decide which slave device
92 // triggered the event, thus decide whether the 'pointer event' is a
93 // 'mouse event' or a 'touch event'.
94 // However, on some desktops, some events from a master pointer are
95 // not delivered to the client. So we select for slave devices instead.
96 // If the touch device has 'GrabDevice' set and 'SendCoreEvents' unset (which
97 // is possible), then the device is detected as a floating device, and a
98 // floating device is not connected to a master device. So it is necessary to
99 // also select on the floating devices.
100 pointer_device_lookup_
.reset();
101 const XIDeviceList
& xi_dev_list
=
102 DeviceListCacheX11::GetInstance()->GetXI2DeviceList(display
);
103 for (int i
= 0; i
< xi_dev_list
.count
; i
++) {
104 const XIDeviceInfo
& devinfo
= xi_dev_list
[i
];
105 if (devinfo
.use
== XIFloatingSlave
|| devinfo
.use
== XIMasterPointer
) {
106 for (int k
= 0; k
< devinfo
.num_classes
; ++k
) {
107 XIAnyClassInfo
* xiclassinfo
= devinfo
.classes
[k
];
108 if (xiclassinfo
->type
== XITouchClass
) {
109 XITouchClassInfo
* tci
=
110 reinterpret_cast<XITouchClassInfo
*>(xiclassinfo
);
111 // Only care direct touch device (such as touch screen) right now
112 if (tci
->mode
== XIDirectTouch
) {
113 touch_device_lookup_
[devinfo
.deviceid
] = true;
114 touch_device_list_
[devinfo
.deviceid
] = true;
118 pointer_device_lookup_
[devinfo
.deviceid
] = true;
119 } else if (devinfo
.use
== XIMasterKeyboard
) {
120 virtual_core_keyboard_device_
= devinfo
.deviceid
;
123 if (devinfo
.use
== XIFloatingSlave
|| devinfo
.use
== XISlavePointer
) {
124 for (int k
= 0; k
< devinfo
.num_classes
; ++k
) {
125 XIAnyClassInfo
* xiclassinfo
= devinfo
.classes
[k
];
126 if (xiclassinfo
->type
== XITouchClass
) {
127 XITouchClassInfo
* tci
=
128 reinterpret_cast<XITouchClassInfo
*>(xiclassinfo
);
129 // Only care direct touch device (such as touch screen) right now
130 if (tci
->mode
== XIDirectTouch
) {
131 CacheTouchscreenIds(devinfo
.deviceid
);
132 if (devinfo
.use
== XISlavePointer
) {
133 device_master_id_list_
[devinfo
.deviceid
] = devinfo
.attachment
;
134 // If the slave device is direct touch device, we also set its
135 // master device to be touch device.
136 touch_device_lookup_
[devinfo
.attachment
] = true;
137 touch_device_list_
[devinfo
.attachment
] = true;
146 bool TouchFactory::ShouldProcessXI2Event(XEvent
* xev
) {
147 DCHECK_EQ(GenericEvent
, xev
->type
);
148 XIEvent
* event
= static_cast<XIEvent
*>(xev
->xcookie
.data
);
149 XIDeviceEvent
* xiev
= reinterpret_cast<XIDeviceEvent
*>(event
);
151 if (event
->evtype
== XI_TouchBegin
||
152 event
->evtype
== XI_TouchUpdate
||
153 event
->evtype
== XI_TouchEnd
) {
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
;
172 // Make sure only key-events from the virtual core keyboard are processed.
173 if (event
->evtype
== XI_KeyPress
|| event
->evtype
== XI_KeyRelease
) {
174 return (virtual_core_keyboard_device_
< 0) ||
175 (virtual_core_keyboard_device_
== xiev
->deviceid
);
178 if (event
->evtype
!= XI_ButtonPress
&&
179 event
->evtype
!= XI_ButtonRelease
&&
180 event
->evtype
!= XI_Motion
)
183 if (!pointer_device_lookup_
[xiev
->deviceid
])
186 return IsTouchDevice(xiev
->deviceid
) ? !touch_events_disabled_
: true;
189 void TouchFactory::SetupXI2ForXWindow(Window window
) {
190 // Setup mask for mouse events. It is possible that a device is loaded/plugged
191 // in after we have setup XInput2 on a window. In such cases, we need to
192 // either resetup XInput2 for the window, so that we get events from the new
193 // device, or we need to listen to events from all devices, and then filter
194 // the events from uninteresting devices. We do the latter because that's
197 XDisplay
* display
= gfx::GetXDisplay();
199 unsigned char mask
[XIMaskLen(XI_LASTEVENT
)];
200 memset(mask
, 0, sizeof(mask
));
202 XISetMask(mask
, XI_TouchBegin
);
203 XISetMask(mask
, XI_TouchUpdate
);
204 XISetMask(mask
, XI_TouchEnd
);
206 XISetMask(mask
, XI_ButtonPress
);
207 XISetMask(mask
, XI_ButtonRelease
);
208 XISetMask(mask
, XI_Motion
);
209 #if defined(OS_CHROMEOS)
210 // XGrabKey() must be replaced with XI2 keyboard grab if XI2 key events are
211 // enabled on desktop Linux.
212 if (base::SysInfo::IsRunningOnChromeOS()) {
213 XISetMask(mask
, XI_KeyPress
);
214 XISetMask(mask
, XI_KeyRelease
);
219 evmask
.deviceid
= XIAllDevices
;
220 evmask
.mask_len
= sizeof(mask
);
222 XISelectEvents(display
, window
, &evmask
, 1);
226 void TouchFactory::SetTouchDeviceList(const std::vector
<int>& devices
) {
227 touch_device_lookup_
.reset();
228 touch_device_list_
.clear();
229 for (int deviceid
: devices
) {
230 DCHECK(IsValidDevice(deviceid
));
231 touch_device_lookup_
[deviceid
] = true;
232 touch_device_list_
[deviceid
] = 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;
242 bool TouchFactory::IsValidDevice(int deviceid
) const {
243 return (deviceid
>= 0) &&
244 (static_cast<size_t>(deviceid
) < touch_device_lookup_
.size());
247 bool TouchFactory::IsTouchDevice(int deviceid
) const {
248 return IsValidDevice(deviceid
) ? touch_device_lookup_
[deviceid
] : false;
251 bool TouchFactory::IsMultiTouchDevice(int deviceid
) const {
252 return (IsValidDevice(deviceid
) && touch_device_lookup_
[deviceid
])
253 ? touch_device_list_
.find(deviceid
)->second
257 bool TouchFactory::QuerySlotForTrackingID(uint32 tracking_id
, int* slot
) {
258 if (!id_generator_
.HasGeneratedIDFor(tracking_id
))
260 *slot
= static_cast<int>(id_generator_
.GetGeneratedID(tracking_id
));
264 int TouchFactory::GetSlotForTrackingID(uint32 tracking_id
) {
265 return id_generator_
.GetGeneratedID(tracking_id
);
268 void TouchFactory::ReleaseSlotForTrackingID(uint32 tracking_id
) {
269 id_generator_
.ReleaseNumber(tracking_id
);
272 bool TouchFactory::IsTouchDevicePresent() {
273 return !touch_events_disabled_
&& touch_device_lookup_
.any();
276 void TouchFactory::ResetForTest() {
277 pointer_device_lookup_
.reset();
278 touch_device_lookup_
.reset();
279 touch_events_disabled_
= false;
280 touch_device_list_
.clear();
281 touchscreen_ids_
.clear();
282 id_generator_
.ResetForTest();
285 void TouchFactory::SetTouchDeviceForTest(
286 const std::vector
<int>& devices
) {
287 touch_device_lookup_
.reset();
288 touch_device_list_
.clear();
289 for (std::vector
<int>::const_iterator iter
= devices
.begin();
290 iter
!= devices
.end(); ++iter
) {
291 DCHECK(IsValidDevice(*iter
));
292 touch_device_lookup_
[*iter
] = true;
293 touch_device_list_
[*iter
] = true;
295 touch_events_disabled_
= false;
298 void TouchFactory::SetPointerDeviceForTest(
299 const std::vector
<int>& devices
) {
300 pointer_device_lookup_
.reset();
301 for (std::vector
<int>::const_iterator iter
= devices
.begin();
302 iter
!= devices
.end(); ++iter
) {
303 pointer_device_lookup_
[*iter
] = true;
307 void TouchFactory::CacheTouchscreenIds(int device_id
) {
308 if (!DeviceDataManager::HasInstance())
310 std::vector
<TouchscreenDevice
> touchscreens
=
311 DeviceDataManager::GetInstance()->touchscreen_devices();
313 std::find_if(touchscreens
.begin(), touchscreens
.end(),
314 [device_id
](const TouchscreenDevice
& touchscreen
) {
315 return touchscreen
.id
== device_id
;
317 // Internal displays will have a vid and pid of 0. Ignore them.
318 if (it
!= touchscreens
.end() && it
->vendor_id
&& it
->product_id
)
319 touchscreen_ids_
.insert(std::make_pair(it
->vendor_id
, it
->product_id
));