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

Side by Side Diff: ui/events/x/touch_factory_x11.cc

Issue 685793002: Move all event related devices from ui/events/ to ui/events/devices/ (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@internal-touchscreens
Patch Set: fix ozone Created 6 years, 1 month 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/x/touch_factory_x11.h ('k') | ui/ozone/public/ozone_platform.cc » ('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/x/touch_factory_x11.h"
6
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>
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/x/device_data_manager_x11.h"
24 #include "ui/events/x/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 CommandLine* cmdline = CommandLine::ForCurrentProcess();
43 touch_events_disabled_ = cmdline->HasSwitch(switches::kTouchEvents) &&
44 cmdline->GetSwitchValueASCII(switches::kTouchEvents) ==
45 switches::kTouchEventsDisabled;
46 }
47
48 TouchFactory::~TouchFactory() {
49 }
50
51 // static
52 TouchFactory* TouchFactory::GetInstance() {
53 return Singleton<TouchFactory>::get();
54 }
55
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);
64
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;
76 }
77 ui::TouchFactory::GetInstance()->SetTouchDeviceList(device_ids);
78 }
79 }
80
81 void TouchFactory::UpdateDeviceList(Display* display) {
82 // Detect touch devices.
83 touch_device_lookup_.reset();
84 touch_device_list_.clear();
85 touchscreen_ids_.clear();
86 max_touch_points_ = -1;
87
88 #if !defined(USE_XI2_MT)
89 // NOTE: The new API for retrieving the list of devices (XIQueryDevice) does
90 // not provide enough information to detect a touch device. As a result, the
91 // old version of query function (XListInputDevices) is used instead.
92 // If XInput2 is not supported, this will return null (with count of -1) so
93 // we assume there cannot be any touch devices.
94 // With XI2.1 or older, we allow only single touch devices.
95 XDeviceList dev_list =
96 DeviceListCacheX::GetInstance()->GetXDeviceList(display);
97 Atom xi_touchscreen = XInternAtom(display, XI_TOUCHSCREEN, false);
98 for (int i = 0; i < dev_list.count; i++) {
99 if (dev_list[i].type == xi_touchscreen) {
100 touch_device_lookup_[dev_list[i].id] = true;
101 touch_device_list_[dev_list[i].id] = false;
102 }
103 }
104 #endif
105
106 if (!DeviceDataManagerX11::GetInstance()->IsXInput2Available())
107 return;
108
109 // Instead of asking X for the list of devices all the time, let's maintain a
110 // list of pointer devices we care about.
111 // It should not be necessary to select for slave devices. XInput2 provides
112 // enough information to the event callback to decide which slave device
113 // triggered the event, thus decide whether the 'pointer event' is a
114 // 'mouse event' or a 'touch event'.
115 // However, on some desktops, some events from a master pointer are
116 // not delivered to the client. So we select for slave devices instead.
117 // If the touch device has 'GrabDevice' set and 'SendCoreEvents' unset (which
118 // is possible), then the device is detected as a floating device, and a
119 // floating device is not connected to a master device. So it is necessary to
120 // also select on the floating devices.
121 pointer_device_lookup_.reset();
122 XIDeviceList xi_dev_list =
123 DeviceListCacheX::GetInstance()->GetXI2DeviceList(display);
124 for (int i = 0; i < xi_dev_list.count; i++) {
125 XIDeviceInfo* devinfo = xi_dev_list.devices + i;
126 if (devinfo->use == XIFloatingSlave || devinfo->use == XIMasterPointer) {
127 #if defined(USE_XI2_MT)
128 for (int k = 0; k < devinfo->num_classes; ++k) {
129 XIAnyClassInfo* xiclassinfo = devinfo->classes[k];
130 if (xiclassinfo->type == XITouchClass) {
131 XITouchClassInfo* tci =
132 reinterpret_cast<XITouchClassInfo*>(xiclassinfo);
133 // Only care direct touch device (such as touch screen) right now
134 if (tci->mode == XIDirectTouch) {
135 touch_device_lookup_[devinfo->deviceid] = true;
136 touch_device_list_[devinfo->deviceid] = true;
137 if (tci->num_touches > 0 && tci->num_touches > max_touch_points_)
138 max_touch_points_ = tci->num_touches;
139 }
140 }
141 }
142 #endif
143 pointer_device_lookup_[devinfo->deviceid] = true;
144 } else if (devinfo->use == XIMasterKeyboard) {
145 virtual_core_keyboard_device_ = devinfo->deviceid;
146 }
147
148 #if defined(USE_XI2_MT)
149 if (devinfo->use == XIFloatingSlave || devinfo->use == XISlavePointer) {
150 for (int k = 0; k < devinfo->num_classes; ++k) {
151 XIAnyClassInfo* xiclassinfo = devinfo->classes[k];
152 if (xiclassinfo->type == XITouchClass) {
153 XITouchClassInfo* tci =
154 reinterpret_cast<XITouchClassInfo*>(xiclassinfo);
155 // Only care direct touch device (such as touch screen) right now
156 if (tci->mode == XIDirectTouch)
157 CacheTouchscreenIds(display, devinfo->deviceid);
158 }
159 }
160 }
161 #endif
162 }
163 }
164
165 bool TouchFactory::ShouldProcessXI2Event(XEvent* xev) {
166 DCHECK_EQ(GenericEvent, xev->type);
167 XIEvent* event = static_cast<XIEvent*>(xev->xcookie.data);
168 XIDeviceEvent* xiev = reinterpret_cast<XIDeviceEvent*>(event);
169
170 #if defined(USE_XI2_MT)
171 if (event->evtype == XI_TouchBegin ||
172 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 &&
184 event->evtype != XI_ButtonRelease &&
185 event->evtype != XI_Motion)
186 return true;
187
188 if (!pointer_device_lookup_[xiev->deviceid])
189 return false;
190
191 return IsTouchDevice(xiev->deviceid) ? !touch_events_disabled_ : true;
192 }
193
194 void TouchFactory::SetupXI2ForXWindow(Window window) {
195 // Setup mask for mouse events. It is possible that a device is loaded/plugged
196 // in after we have setup XInput2 on a window. In such cases, we need to
197 // either resetup XInput2 for the window, so that we get events from the new
198 // device, or we need to listen to events from all devices, and then filter
199 // the events from uninteresting devices. We do the latter because that's
200 // simpler.
201
202 XDisplay* display = gfx::GetXDisplay();
203
204 unsigned char mask[XIMaskLen(XI_LASTEVENT)];
205 memset(mask, 0, sizeof(mask));
206
207 #if defined(USE_XI2_MT)
208 XISetMask(mask, XI_TouchBegin);
209 XISetMask(mask, XI_TouchUpdate);
210 XISetMask(mask, XI_TouchEnd);
211 #endif
212 XISetMask(mask, XI_ButtonPress);
213 XISetMask(mask, XI_ButtonRelease);
214 XISetMask(mask, XI_Motion);
215 #if defined(OS_CHROMEOS)
216 if (base::SysInfo::IsRunningOnChromeOS()) {
217 XISetMask(mask, XI_KeyPress);
218 XISetMask(mask, XI_KeyRelease);
219 }
220 #endif
221
222 XIEventMask evmask;
223 evmask.deviceid = XIAllDevices;
224 evmask.mask_len = sizeof(mask);
225 evmask.mask = mask;
226 XISelectEvents(display, window, &evmask, 1);
227 XFlush(display);
228 }
229
230 void TouchFactory::SetTouchDeviceList(
231 const std::vector<unsigned int>& devices) {
232 touch_device_lookup_.reset();
233 touch_device_list_.clear();
234 for (std::vector<unsigned int>::const_iterator iter = devices.begin();
235 iter != devices.end(); ++iter) {
236 DCHECK(*iter < touch_device_lookup_.size());
237 touch_device_lookup_[*iter] = true;
238 touch_device_list_[*iter] = false;
239 }
240 }
241
242 bool TouchFactory::IsTouchDevice(unsigned deviceid) const {
243 return deviceid < touch_device_lookup_.size() ?
244 touch_device_lookup_[deviceid] : false;
245 }
246
247 bool TouchFactory::IsMultiTouchDevice(unsigned int deviceid) const {
248 return (deviceid < touch_device_lookup_.size() &&
249 touch_device_lookup_[deviceid]) ?
250 touch_device_list_.find(deviceid)->second :
251 false;
252 }
253
254 bool TouchFactory::QuerySlotForTrackingID(uint32 tracking_id, int* slot) {
255 if (!id_generator_.HasGeneratedIDFor(tracking_id))
256 return false;
257 *slot = static_cast<int>(id_generator_.GetGeneratedID(tracking_id));
258 return true;
259 }
260
261 int TouchFactory::GetSlotForTrackingID(uint32 tracking_id) {
262 return id_generator_.GetGeneratedID(tracking_id);
263 }
264
265 void TouchFactory::AcquireSlotForTrackingID(uint32 tracking_id) {
266 tracking_id_refcounts_[tracking_id]++;
267 }
268
269 void TouchFactory::ReleaseSlotForTrackingID(uint32 tracking_id) {
270 tracking_id_refcounts_[tracking_id]--;
271 if (tracking_id_refcounts_[tracking_id] == 0)
272 id_generator_.ReleaseNumber(tracking_id);
273 }
274
275 bool TouchFactory::IsTouchDevicePresent() {
276 return !touch_events_disabled_ && touch_device_lookup_.any();
277 }
278
279 int TouchFactory::GetMaxTouchPoints() const {
280 return max_touch_points_;
281 }
282
283 void TouchFactory::ResetForTest() {
284 pointer_device_lookup_.reset();
285 touch_device_lookup_.reset();
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();
292 }
293
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;
303 }
304 touch_events_disabled_ = false;
305 }
306
307 void TouchFactory::SetPointerDeviceForTest(
308 const std::vector<unsigned int>& devices) {
309 pointer_device_lookup_.reset();
310 for (std::vector<unsigned int>::const_iterator iter = devices.begin();
311 iter != devices.end(); ++iter) {
312 pointer_device_lookup_[*iter] = true;
313 }
314 }
315
316 void TouchFactory::CacheTouchscreenIds(Display* display, int device_id) {
317 XDevice* device = XOpenDevice(display, device_id);
318 if (!device)
319 return;
320
321 Atom actual_type_return;
322 int actual_format_return;
323 unsigned long nitems_return;
324 unsigned long bytes_after_return;
325 unsigned char *prop_return;
326
327 const char kDeviceProductIdString[] = "Device Product ID";
328 Atom device_product_id_atom =
329 XInternAtom(display, kDeviceProductIdString, false);
330
331 if (device_product_id_atom != None &&
332 XGetDeviceProperty(display, device, device_product_id_atom, 0, 2,
333 False, XA_INTEGER, &actual_type_return,
334 &actual_format_return, &nitems_return,
335 &bytes_after_return, &prop_return) == Success) {
336 if (actual_type_return == XA_INTEGER &&
337 actual_format_return == 32 &&
338 nitems_return == 2) {
339 // An actual_format_return of 32 implies that the returned data is an
340 // array of longs. See the description of |prop_return| in `man
341 // XGetDeviceProperty` for details.
342 long* ptr = reinterpret_cast<long*>(prop_return);
343
344 // Internal displays will have a vid and pid of 0. Ignore them.
345 // ptr[0] is the vid, and ptr[1] is the pid.
346 if (ptr[0] || ptr[1])
347 touchscreen_ids_.insert(std::make_pair(ptr[0], ptr[1]));
348 }
349 XFree(prop_return);
350 }
351
352 XCloseDevice(display, device);
353 }
354
355 } // namespace ui
OLDNEW
« no previous file with comments | « ui/events/x/touch_factory_x11.h ('k') | ui/ozone/public/ozone_platform.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698