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

Side by Side Diff: ui/events/platform/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/platform/x11/touch_factory_x11.h ('k') | ui/events/platform/x11/x11_event_source.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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.
4
5 #include "ui/events/platform/x11/touch_factory_x11.h"
6
7 #include <X11/cursorfont.h>
8 #include <X11/extensions/XInput.h>
9 #include <X11/extensions/XInput2.h>
10 #include <X11/extensions/XIproto.h>
11 #include <X11/Xatom.h>
12
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/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"
26
27 namespace ui {
28
29 TouchFactory::TouchFactory()
30 : pointer_device_lookup_(),
31 touch_events_disabled_(false),
32 touch_device_list_(),
33 max_touch_points_(-1),
34 virtual_core_keyboard_device_(-1),
35 id_generator_(0) {
36 if (!DeviceDataManagerX11::GetInstance()->IsXInput2Available())
37 return;
38
39 XDisplay* display = gfx::GetXDisplay();
40 UpdateDeviceList(display);
41
42 base::CommandLine* cmdline = base::CommandLine::ForCurrentProcess();
43 touch_events_disabled_ =
44 cmdline->HasSwitch(switches::kTouchEvents) &&
45 cmdline->GetSwitchValueASCII(switches::kTouchEvents) ==
46 switches::kTouchEventsDisabled;
47 }
48
49 TouchFactory::~TouchFactory() {
50 }
51
52 // static
53 TouchFactory* TouchFactory::GetInstance() {
54 return Singleton<TouchFactory>::get();
55 }
56
57 // static
58 void TouchFactory::SetTouchDeviceListFromCommandLine() {
59 // 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
61 // touch-device isn't available.
62 std::string touch_devices =
63 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
64 switches::kTouchDevices);
65
66 if (!touch_devices.empty()) {
67 std::vector<std::string> devs;
68 std::vector<unsigned int> device_ids;
69 unsigned int devid;
70 base::SplitString(touch_devices, ',', &devs);
71 for (std::vector<std::string>::iterator iter = devs.begin();
72 iter != devs.end(); ++iter) {
73 if (base::StringToInt(*iter, reinterpret_cast<int*>(&devid)))
74 device_ids.push_back(devid);
75 else
76 DLOG(WARNING) << "Invalid touch-device id: " << *iter;
77 }
78 ui::TouchFactory::GetInstance()->SetTouchDeviceList(device_ids);
79 }
80 }
81
82 void TouchFactory::UpdateDeviceList(Display* display) {
83 // Detect touch devices.
84 touch_device_lookup_.reset();
85 touch_device_list_.clear();
86 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
107 if (!DeviceDataManagerX11::GetInstance()->IsXInput2Available())
108 return;
109
110 // Instead of asking X for the list of devices all the time, let's maintain a
111 // list of pointer devices we care about.
112 // It should not be necessary to select for slave devices. XInput2 provides
113 // enough information to the event callback to decide which slave device
114 // triggered the event, thus decide whether the 'pointer event' is a
115 // 'mouse event' or a 'touch event'.
116 // However, on some desktops, some events from a master pointer are
117 // not delivered to the client. So we select for slave devices instead.
118 // 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
120 // floating device is not connected to a master device. So it is necessary to
121 // also select on the floating devices.
122 pointer_device_lookup_.reset();
123 XIDeviceList xi_dev_list =
124 DeviceListCacheX::GetInstance()->GetXI2DeviceList(display);
125 for (int i = 0; i < xi_dev_list.count; i++) {
126 XIDeviceInfo* devinfo = xi_dev_list.devices + i;
127 if (devinfo->use == XIFloatingSlave || devinfo->use == XIMasterPointer) {
128 #if defined(USE_XI2_MT)
129 for (int k = 0; k < devinfo->num_classes; ++k) {
130 XIAnyClassInfo* xiclassinfo = devinfo->classes[k];
131 if (xiclassinfo->type == XITouchClass) {
132 XITouchClassInfo* tci =
133 reinterpret_cast<XITouchClassInfo*>(xiclassinfo);
134 // Only care direct touch device (such as touch screen) right now
135 if (tci->mode == XIDirectTouch) {
136 touch_device_lookup_[devinfo->deviceid] = true;
137 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 }
141 }
142 }
143 #endif
144 pointer_device_lookup_[devinfo->deviceid] = true;
145 } else if (devinfo->use == XIMasterKeyboard) {
146 virtual_core_keyboard_device_ = devinfo->deviceid;
147 }
148
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);
159 }
160 }
161 }
162 #endif
163 }
164 }
165
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);
170
171 #if defined(USE_XI2_MT)
172 if (event->evtype == XI_TouchBegin || event->evtype == XI_TouchUpdate ||
173 event->evtype == XI_TouchEnd) {
174 return !touch_events_disabled_ && IsTouchDevice(xiev->deviceid);
175 }
176 #endif
177 // Make sure only key-events from the virtual core keyboard are processed.
178 if (event->evtype == XI_KeyPress || event->evtype == XI_KeyRelease) {
179 return (virtual_core_keyboard_device_ < 0) ||
180 (virtual_core_keyboard_device_ == xiev->deviceid);
181 }
182
183 if (event->evtype != XI_ButtonPress && event->evtype != XI_ButtonRelease &&
184 event->evtype != XI_Motion)
185 return true;
186
187 if (!pointer_device_lookup_[xiev->deviceid])
188 return false;
189
190 return IsTouchDevice(xiev->deviceid) ? !touch_events_disabled_ : true;
191 }
192
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.
200
201 XDisplay* display = gfx::GetXDisplay();
202
203 unsigned char mask[XIMaskLen(XI_LASTEVENT)];
204 memset(mask, 0, sizeof(mask));
205
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);
218 }
219 #endif
220
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);
227 }
228
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;
238 }
239 }
240
241 bool TouchFactory::IsTouchDevice(unsigned deviceid) const {
242 return deviceid < touch_device_lookup_.size() ? touch_device_lookup_[deviceid]
243 : false;
244 }
245
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;
251 }
252
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;
258 }
259
260 int TouchFactory::GetSlotForTrackingID(uint32 tracking_id) {
261 return id_generator_.GetGeneratedID(tracking_id);
262 }
263
264 void TouchFactory::AcquireSlotForTrackingID(uint32 tracking_id) {
265 tracking_id_refcounts_[tracking_id]++;
266 }
267
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);
272 }
273
274 bool TouchFactory::IsTouchDevicePresent() {
275 return !touch_events_disabled_ && touch_device_lookup_.any();
276 }
277
278 int TouchFactory::GetMaxTouchPoints() const {
279 return max_touch_points_;
280 }
281
282 void TouchFactory::ResetForTest() {
283 pointer_device_lookup_.reset();
284 touch_device_lookup_.reset();
285 touch_events_disabled_ = false;
286 touch_device_list_.clear();
287 touchscreen_ids_.clear();
288 tracking_id_refcounts_.clear();
289 max_touch_points_ = -1;
290 id_generator_.ResetForTest();
291 }
292
293 void TouchFactory::SetTouchDeviceForTest(
294 const std::vector<unsigned int>& devices) {
295 touch_device_lookup_.reset();
296 touch_device_list_.clear();
297 for (std::vector<unsigned int>::const_iterator iter = devices.begin();
298 iter != devices.end(); ++iter) {
299 DCHECK(*iter < touch_device_lookup_.size());
300 touch_device_lookup_[*iter] = true;
301 touch_device_list_[*iter] = true;
302 }
303 touch_events_disabled_ = false;
304 }
305
306 void TouchFactory::SetPointerDeviceForTest(
307 const std::vector<unsigned int>& devices) {
308 pointer_device_lookup_.reset();
309 for (std::vector<unsigned int>::const_iterator iter = devices.begin();
310 iter != devices.end(); ++iter) {
311 pointer_device_lookup_[*iter] = true;
312 }
313 }
314
315 void TouchFactory::CacheTouchscreenIds(Display* display, int device_id) {
316 XDevice* device = XOpenDevice(display, device_id);
317 if (!device)
318 return;
319
320 Atom actual_type_return;
321 int actual_format_return;
322 unsigned long nitems_return;
323 unsigned long bytes_after_return;
324 unsigned char* prop_return;
325
326 const char kDeviceProductIdString[] = "Device Product ID";
327 Atom device_product_id_atom =
328 XInternAtom(display, kDeviceProductIdString, false);
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 }
352
353 } // namespace ui
OLDNEW
« no previous file with comments | « ui/events/platform/x11/touch_factory_x11.h ('k') | ui/events/platform/x11/x11_event_source.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698