Small fix for touch screen on Linux with or without flag --touch-devices
[chromium-blink-merge.git] / ui / events / devices / x11 / touch_factory_x11.cc
blob322934447348528a0161496b8405bbd265d2cd6e
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"
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/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"
27 namespace ui {
29 TouchFactory::TouchFactory()
30 : pointer_device_lookup_(),
31 touch_events_disabled_(false),
32 touch_device_list_(),
33 virtual_core_keyboard_device_(-1),
34 id_generator_(0) {
35 if (!DeviceDataManagerX11::GetInstance()->IsXInput2Available())
36 return;
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() {
50 // static
51 TouchFactory* TouchFactory::GetInstance() {
52 return Singleton<TouchFactory>::get();
55 // static
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<int> device_ids;
67 int devid;
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);
73 else
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())
87 return;
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(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;
147 bool TouchFactory::ShouldProcessXI2Event(XEvent* xev) {
148 DCHECK_EQ(GenericEvent, xev->type);
149 XIEvent* event = static_cast<XIEvent*>(xev->xcookie.data);
150 XIDeviceEvent* xiev = reinterpret_cast<XIDeviceEvent*>(event);
152 if (event->evtype == XI_TouchBegin ||
153 event->evtype == XI_TouchUpdate ||
154 event->evtype == XI_TouchEnd) {
155 // Since SetupXI2ForXWindow() selects events from all devices, for a
156 // touchscreen attached to a master pointer device, X11 sends two
157 // events for each touch: one from the slave (deviceid == the id of
158 // the touchscreen device), and one from the master (deviceid == the
159 // id of the master pointer device). Instead of processing both
160 // events, discard the event that comes from the slave, and only
161 // allow processing the event coming from the master.
162 // For a 'floating' touchscreen device, X11 sends only one event for
163 // each touch, with both deviceid and sourceid set to the id of the
164 // touchscreen device.
165 bool is_from_master_or_float = touch_device_list_[xiev->deviceid];
166 bool is_from_slave_device = !is_from_master_or_float
167 && xiev->sourceid == xiev->deviceid;
168 return !touch_events_disabled_ &&
169 IsTouchDevice(xiev->deviceid) &&
170 !is_from_slave_device;
173 // Make sure only key-events from the virtual core keyboard are processed.
174 if (event->evtype == XI_KeyPress || event->evtype == XI_KeyRelease) {
175 return (virtual_core_keyboard_device_ < 0) ||
176 (virtual_core_keyboard_device_ == xiev->deviceid);
179 if (event->evtype != XI_ButtonPress &&
180 event->evtype != XI_ButtonRelease &&
181 event->evtype != XI_Motion)
182 return true;
184 if (!pointer_device_lookup_[xiev->deviceid])
185 return false;
187 return IsTouchDevice(xiev->deviceid) ? !touch_events_disabled_ : true;
190 void TouchFactory::SetupXI2ForXWindow(Window window) {
191 // Setup mask for mouse events. It is possible that a device is loaded/plugged
192 // in after we have setup XInput2 on a window. In such cases, we need to
193 // either resetup XInput2 for the window, so that we get events from the new
194 // device, or we need to listen to events from all devices, and then filter
195 // the events from uninteresting devices. We do the latter because that's
196 // simpler.
198 XDisplay* display = gfx::GetXDisplay();
200 unsigned char mask[XIMaskLen(XI_LASTEVENT)];
201 memset(mask, 0, sizeof(mask));
203 XISetMask(mask, XI_TouchBegin);
204 XISetMask(mask, XI_TouchUpdate);
205 XISetMask(mask, XI_TouchEnd);
207 XISetMask(mask, XI_ButtonPress);
208 XISetMask(mask, XI_ButtonRelease);
209 XISetMask(mask, XI_Motion);
210 #if defined(OS_CHROMEOS)
211 // XGrabKey() must be replaced with XI2 keyboard grab if XI2 key events are
212 // enabled on desktop Linux.
213 if (base::SysInfo::IsRunningOnChromeOS()) {
214 XISetMask(mask, XI_KeyPress);
215 XISetMask(mask, XI_KeyRelease);
217 #endif
219 XIEventMask evmask;
220 evmask.deviceid = XIAllDevices;
221 evmask.mask_len = sizeof(mask);
222 evmask.mask = mask;
223 XISelectEvents(display, window, &evmask, 1);
224 XFlush(display);
227 void TouchFactory::SetTouchDeviceList(const std::vector<int>& devices) {
228 touch_device_lookup_.reset();
229 touch_device_list_.clear();
230 for (int deviceid : devices) {
231 DCHECK(IsValidDevice(deviceid));
232 touch_device_lookup_[deviceid] = true;
233 touch_device_list_[deviceid] = false;
234 if (device_master_id_list_.find(deviceid) != device_master_id_list_.end()) {
235 // When we set the device through the "--touch-devices" flag to slave
236 // touch device, we also set its master device to be touch device.
237 touch_device_lookup_[device_master_id_list_[deviceid]] = true;
238 touch_device_list_[device_master_id_list_[deviceid]] = false;
243 bool TouchFactory::IsValidDevice(int deviceid) const {
244 return (deviceid >= 0) &&
245 (static_cast<size_t>(deviceid) < touch_device_lookup_.size());
248 bool TouchFactory::IsTouchDevice(int deviceid) const {
249 return IsValidDevice(deviceid) ? touch_device_lookup_[deviceid] : false;
252 bool TouchFactory::IsMultiTouchDevice(int deviceid) const {
253 return (IsValidDevice(deviceid) && touch_device_lookup_[deviceid])
254 ? touch_device_list_.find(deviceid)->second
255 : false;
258 bool TouchFactory::QuerySlotForTrackingID(uint32 tracking_id, int* slot) {
259 if (!id_generator_.HasGeneratedIDFor(tracking_id))
260 return false;
261 *slot = static_cast<int>(id_generator_.GetGeneratedID(tracking_id));
262 return true;
265 int TouchFactory::GetSlotForTrackingID(uint32 tracking_id) {
266 return id_generator_.GetGeneratedID(tracking_id);
269 void TouchFactory::ReleaseSlotForTrackingID(uint32 tracking_id) {
270 id_generator_.ReleaseNumber(tracking_id);
273 bool TouchFactory::IsTouchDevicePresent() {
274 return !touch_events_disabled_ && touch_device_lookup_.any();
277 void TouchFactory::ResetForTest() {
278 pointer_device_lookup_.reset();
279 touch_device_lookup_.reset();
280 touch_events_disabled_ = false;
281 touch_device_list_.clear();
282 touchscreen_ids_.clear();
283 id_generator_.ResetForTest();
286 void TouchFactory::SetTouchDeviceForTest(
287 const std::vector<int>& devices) {
288 touch_device_lookup_.reset();
289 touch_device_list_.clear();
290 for (std::vector<int>::const_iterator iter = devices.begin();
291 iter != devices.end(); ++iter) {
292 DCHECK(IsValidDevice(*iter));
293 touch_device_lookup_[*iter] = true;
294 touch_device_list_[*iter] = true;
296 touch_events_disabled_ = false;
299 void TouchFactory::SetPointerDeviceForTest(
300 const std::vector<int>& devices) {
301 pointer_device_lookup_.reset();
302 for (std::vector<int>::const_iterator iter = devices.begin();
303 iter != devices.end(); ++iter) {
304 pointer_device_lookup_[*iter] = true;
308 void TouchFactory::CacheTouchscreenIds(int device_id) {
309 if (!DeviceDataManager::HasInstance())
310 return;
311 std::vector<TouchscreenDevice> touchscreens =
312 DeviceDataManager::GetInstance()->touchscreen_devices();
313 const auto it =
314 std::find_if(touchscreens.begin(), touchscreens.end(),
315 [device_id](const TouchscreenDevice& touchscreen) {
316 return touchscreen.id == device_id;
318 // Internal displays will have a vid and pid of 0. Ignore them.
319 if (it != touchscreens.end() && it->vendor_id && it->product_id)
320 touchscreen_ids_.insert(std::make_pair(it->vendor_id, it->product_id));
323 } // namespace ui