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

Side by Side Diff: views/touchui/touch_factory.cc

Issue 7792094: touchui: support XInput2 MT (Closed) Base URL: http://git.chromium.org/git/chromium.git@trunk
Patch Set: Add gyp switch to support both XI2.0 and XI2.1. Correct the style issues also. Created 9 years, 3 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 | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "views/touchui/touch_factory.h" 5 #include "views/touchui/touch_factory.h"
6 6
7 #include <gtk/gtk.h> 7 #include <gtk/gtk.h>
8 #include <gdk/gdkx.h> 8 #include <gdk/gdkx.h>
9 #include <X11/cursorfont.h> 9 #include <X11/cursorfont.h>
10 #include <X11/extensions/XInput.h> 10 #include <X11/extensions/XInput.h>
(...skipping 11 matching lines...) Expand all
22 // The X cursor is hidden if it is idle for kCursorIdleSeconds seconds. 22 // The X cursor is hidden if it is idle for kCursorIdleSeconds seconds.
23 int kCursorIdleSeconds = 5; 23 int kCursorIdleSeconds = 5;
24 24
25 // Given the TouchParam, return the correspoding XIValuatorClassInfo using 25 // Given the TouchParam, return the correspoding XIValuatorClassInfo using
26 // the X device information through Atom name matching. 26 // the X device information through Atom name matching.
27 XIValuatorClassInfo* FindTPValuator(Display* display, 27 XIValuatorClassInfo* FindTPValuator(Display* display,
28 XIDeviceInfo* info, 28 XIDeviceInfo* info,
29 views::TouchFactory::TouchParam tp) { 29 views::TouchFactory::TouchParam tp) {
30 // Lookup table for mapping TouchParam to Atom string used in X. 30 // Lookup table for mapping TouchParam to Atom string used in X.
31 // A full set of Atom strings can be found at xserver-properties.h. 31 // A full set of Atom strings can be found at xserver-properties.h.
32 // For Slot ID, See this chromeos revision: http://git.chromium.org/gitweb/?
33 // p=chromiumos/overlays/chromiumos-overlay.git;
34 // a=commit;h=9164d0a75e48c4867e4ef4ab51f743ae231c059a
35 static struct { 32 static struct {
36 views::TouchFactory::TouchParam tp; 33 views::TouchFactory::TouchParam tp;
37 const char* atom; 34 const char* atom;
38 } kTouchParamAtom[] = { 35 } kTouchParamAtom[] = {
39 { views::TouchFactory::TP_TOUCH_MAJOR, "Abs MT Touch Major" }, 36 { views::TouchFactory::TP_TOUCH_MAJOR, "Abs MT Touch Major" },
40 { views::TouchFactory::TP_TOUCH_MINOR, "Abs MT Touch Minor" }, 37 { views::TouchFactory::TP_TOUCH_MINOR, "Abs MT Touch Minor" },
41 { views::TouchFactory::TP_ORIENTATION, "Abs MT Orientation" }, 38 { views::TouchFactory::TP_ORIENTATION, "Abs MT Orientation" },
42 { views::TouchFactory::TP_PRESSURE, "Abs MT Pressure" }, 39 { views::TouchFactory::TP_PRESSURE, "Abs MT Pressure" },
40 #if !defined(USE_XI2_1)
41 // For Slot ID, See this chromeos revision: http://git.chromium.org/gitweb/?
42 // p=chromiumos/overlays/chromiumos-overlay.git;
43 // a=commit;h=9164d0a75e48c4867e4ef4ab51f743ae231c059a
43 { views::TouchFactory::TP_SLOT_ID, "Abs MT Slot ID" }, 44 { views::TouchFactory::TP_SLOT_ID, "Abs MT Slot ID" },
45 #endif
44 { views::TouchFactory::TP_TRACKING_ID, "Abs MT Tracking ID" }, 46 { views::TouchFactory::TP_TRACKING_ID, "Abs MT Tracking ID" },
45 { views::TouchFactory::TP_LAST_ENTRY, NULL }, 47 { views::TouchFactory::TP_LAST_ENTRY, NULL },
46 }; 48 };
47 49
48 const char* atom_tp = NULL; 50 const char* atom_tp = NULL;
49 51
50 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kTouchParamAtom); i++) { 52 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kTouchParamAtom); i++) {
51 if (tp == kTouchParamAtom[i].tp) { 53 if (tp == kTouchParamAtom[i].tp) {
52 atom_tp = kTouchParamAtom[i].atom; 54 atom_tp = kTouchParamAtom[i].atom;
53 break; 55 break;
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
119 121
120 // static 122 // static
121 TouchFactory* TouchFactory::GetInstance() { 123 TouchFactory* TouchFactory::GetInstance() {
122 return Singleton<TouchFactory>::get(); 124 return Singleton<TouchFactory>::get();
123 } 125 }
124 126
125 TouchFactory::TouchFactory() 127 TouchFactory::TouchFactory()
126 : is_cursor_visible_(true), 128 : is_cursor_visible_(true),
127 keep_mouse_cursor_(false), 129 keep_mouse_cursor_(false),
128 cursor_timer_(), 130 cursor_timer_(),
131 #if !defined(USE_XI2_1)
132 slots_used_(),
133 #endif
129 pointer_device_lookup_(), 134 pointer_device_lookup_(),
130 touch_device_list_(), 135 touch_device_list_() {
131 slots_used_() {
132 #if defined(TOUCH_UI) 136 #if defined(TOUCH_UI)
133 if (!base::MessagePumpForUI::HasXInput2()) 137 if (!base::MessagePumpForUI::HasXInput2())
134 return; 138 return;
135 #endif 139 #endif
136 140
137 char nodata[] = { 0, 0, 0, 0, 0, 0, 0, 0 }; 141 char nodata[] = { 0, 0, 0, 0, 0, 0, 0, 0 };
138 XColor black; 142 XColor black;
139 black.red = black.green = black.blue = 0; 143 black.red = black.green = black.blue = 0;
140 Display* display = ui::GetXDisplay(); 144 Display* display = ui::GetXDisplay();
141 Pixmap blank = XCreateBitmapFromData(display, ui::GetX11RootWindow(), 145 Pixmap blank = XCreateBitmapFromData(display, ui::GetX11RootWindow(),
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
183 void TouchFactory::UpdateDeviceList(Display* display) { 187 void TouchFactory::UpdateDeviceList(Display* display) {
184 // Detect touch devices. 188 // Detect touch devices.
185 // NOTE: The new API for retrieving the list of devices (XIQueryDevice) does 189 // NOTE: The new API for retrieving the list of devices (XIQueryDevice) does
186 // not provide enough information to detect a touch device. As a result, the 190 // not provide enough information to detect a touch device. As a result, the
187 // old version of query function (XListInputDevices) is used instead. 191 // old version of query function (XListInputDevices) is used instead.
188 // If XInput2 is not supported, this will return null (with count of -1) so 192 // If XInput2 is not supported, this will return null (with count of -1) so
189 // we assume there cannot be any touch devices. 193 // we assume there cannot be any touch devices.
190 int count = 0; 194 int count = 0;
191 touch_device_lookup_.reset(); 195 touch_device_lookup_.reset();
192 touch_device_list_.clear(); 196 touch_device_list_.clear();
197 #if !defined(USE_XI2_1)
193 XDeviceInfo* devlist = XListInputDevices(display, &count); 198 XDeviceInfo* devlist = XListInputDevices(display, &count);
194 for (int i = 0; i < count; i++) { 199 for (int i = 0; i < count; i++) {
195 if (devlist[i].type) { 200 if (devlist[i].type) {
196 const char* devtype = XGetAtomName(display, devlist[i].type); 201 const char* devtype = XGetAtomName(display, devlist[i].type);
197 if (devtype && !strcmp(devtype, XI_TOUCHSCREEN)) { 202 if (devtype && !strcmp(devtype, XI_TOUCHSCREEN)) {
198 touch_device_lookup_[devlist[i].id] = true; 203 touch_device_lookup_[devlist[i].id] = true;
199 touch_device_list_.push_back(devlist[i].id); 204 touch_device_list_.push_back(devlist[i].id);
200 } 205 }
201 } 206 }
202 } 207 }
203 if (devlist) 208 if (devlist)
204 XFreeDeviceList(devlist); 209 XFreeDeviceList(devlist);
210 #endif
205 211
206 // Instead of asking X for the list of devices all the time, let's maintain a 212 // Instead of asking X for the list of devices all the time, let's maintain a
207 // list of pointer devices we care about. 213 // list of pointer devices we care about.
208 // It should not be necessary to select for slave devices. XInput2 provides 214 // It should not be necessary to select for slave devices. XInput2 provides
209 // enough information to the event callback to decide which slave device 215 // enough information to the event callback to decide which slave device
210 // triggered the event, thus decide whether the 'pointer event' is a 216 // triggered the event, thus decide whether the 'pointer event' is a
211 // 'mouse event' or a 'touch event'. 217 // 'mouse event' or a 'touch event'.
212 // However, on some desktops, some events from a master pointer are 218 // However, on some desktops, some events from a master pointer are
213 // not delivered to the client. So we select for slave devices instead. 219 // not delivered to the client. So we select for slave devices instead.
214 // If the touch device has 'GrabDevice' set and 'SendCoreEvents' unset (which 220 // If the touch device has 'GrabDevice' set and 'SendCoreEvents' unset (which
215 // is possible), then the device is detected as a floating device, and a 221 // is possible), then the device is detected as a floating device, and a
216 // floating device is not connected to a master device. So it is necessary to 222 // floating device is not connected to a master device. So it is necessary to
217 // also select on the floating devices. 223 // also select on the floating devices.
218 pointer_device_lookup_.reset(); 224 pointer_device_lookup_.reset();
219 XIDeviceInfo* devices = XIQueryDevice(display, XIAllDevices, &count); 225 XIDeviceInfo* devices = XIQueryDevice(display, XIAllDevices, &count);
220 for (int i = 0; i < count; i++) { 226 if (devices) {
221 XIDeviceInfo* devinfo = devices + i; 227 for (int i = 0; i < count; i++) {
222 if (devinfo->use == XIFloatingSlave || devinfo->use == XISlavePointer) { 228 if (!devices[i].enabled)
223 pointer_device_lookup_[devinfo->deviceid] = true; 229 continue;
230 XIDeviceInfo* devinfo = devices + i;
231 #if defined(USE_XI2_1)
232 for (int k = 0; k < devinfo->num_classes; ++k) {
233 XIAnyClassInfo* xiclassinfo = devinfo->classes[k];
234 if (xiclassinfo->type == XITouchClass) {
235 XITouchClassInfo* tci = (XITouchClassInfo *)xiclassinfo;
236 // Only care direct touch device (such as touch screen) right now
237 if (tci->mode == XIDirectTouch) {
238 touch_device_lookup_[devinfo->deviceid] = true;
239 touch_device_list_.push_back(devinfo->deviceid);
240 }
241 }
242 }
243 #endif
244 if (devinfo->use == XIFloatingSlave || devinfo->use == XISlavePointer)
245 pointer_device_lookup_[devinfo->deviceid] = true;
224 } 246 }
225 } 247 }
226 if (devices) 248 if (devices)
227 XIFreeDeviceInfo(devices); 249 XIFreeDeviceInfo(devices);
228 250
229 SetupValuator(); 251 SetupValuator();
230 } 252 }
231 253
232 bool TouchFactory::ShouldProcessXI2Event(XEvent* xev) { 254 bool TouchFactory::ShouldProcessXI2Event(XEvent* xev) {
233 DCHECK_EQ(GenericEvent, xev->type); 255 DCHECK_EQ(GenericEvent, xev->type);
256 #if defined(USE_XI2_1)
257 XIEvent* event = static_cast<XIEvent*>(xev->xcookie.data);
258 if (event->evtype != XI_TouchBegin &&
259 event->evtype != XI_TouchUpdate &&
260 event->evtype != XI_TouchEnd)
261 return true;
234 262
263 XIDeviceEvent* xiev = reinterpret_cast<XIDeviceEvent*>(event);
264 return touch_device_lookup_[xiev->sourceid];
265 #else
235 XGenericEventCookie* cookie = &xev->xcookie; 266 XGenericEventCookie* cookie = &xev->xcookie;
236 if (cookie->evtype != XI_ButtonPress && 267 if (cookie->evtype != XI_ButtonPress &&
237 cookie->evtype != XI_ButtonRelease && 268 cookie->evtype != XI_ButtonRelease &&
238 cookie->evtype != XI_Motion) 269 cookie->evtype != XI_Motion)
239 return true; 270 return true;
240 271
241 XIDeviceEvent* xiev = static_cast<XIDeviceEvent*>(cookie->data); 272 XIDeviceEvent* xiev = static_cast<XIDeviceEvent*>(cookie->data);
242 return pointer_device_lookup_[xiev->deviceid]; 273 return pointer_device_lookup_[xiev->deviceid];
274 #endif
243 } 275 }
244 276
245 void TouchFactory::SetupXI2ForXWindow(Window window) { 277 void TouchFactory::SetupXI2ForXWindow(Window window) {
246 // Setup mask for mouse events. It is possible that a device is loaded/plugged 278 // Setup mask for mouse events. It is possible that a device is loaded/plugged
247 // in after we have setup XInput2 on a window. In such cases, we need to 279 // in after we have setup XInput2 on a window. In such cases, we need to
248 // either resetup XInput2 for the window, so that we get events from the new 280 // either resetup XInput2 for the window, so that we get events from the new
249 // device, or we need to listen to events from all devices, and then filter 281 // device, or we need to listen to events from all devices, and then filter
250 // the events from uninteresting devices. We do the latter because that's 282 // the events from uninteresting devices. We do the latter because that's
251 // simpler. 283 // simpler.
252 284
253 Display* display = ui::GetXDisplay(); 285 Display* display = ui::GetXDisplay();
254 286
255 unsigned char mask[XIMaskLen(XI_LASTEVENT)]; 287 unsigned char mask[XIMaskLen(XI_LASTEVENT)];
256 memset(mask, 0, sizeof(mask)); 288 memset(mask, 0, sizeof(mask));
257 289
290 #if defined(USE_XI2_1)
291 XISetMask(mask, XI_TouchBegin);
292 XISetMask(mask, XI_TouchUpdate);
293 XISetMask(mask, XI_TouchEnd);
294 #else
sadrul 2011/09/07 14:56:37 I think it'd be a good idea to select for XI_Butto
ningxin.hu 2011/09/08 08:24:50 Yes. It is really a good idea. I am working on thi
258 XISetMask(mask, XI_ButtonPress); 295 XISetMask(mask, XI_ButtonPress);
259 XISetMask(mask, XI_ButtonRelease); 296 XISetMask(mask, XI_ButtonRelease);
260 XISetMask(mask, XI_Motion); 297 XISetMask(mask, XI_Motion);
298 #endif
261 299
262 XIEventMask evmask; 300 XIEventMask evmask;
263 evmask.deviceid = XIAllDevices; 301 evmask.deviceid = XIAllDevices;
264 evmask.mask_len = sizeof(mask); 302 evmask.mask_len = sizeof(mask);
265 evmask.mask = mask; 303 evmask.mask = mask;
266 XISelectEvents(display, window, &evmask, 1); 304 XISelectEvents(display, window, &evmask, 1);
267 XFlush(display); 305 XFlush(display);
268 } 306 }
269 307
270 void TouchFactory::SetTouchDeviceList( 308 void TouchFactory::SetTouchDeviceList(
271 const std::vector<unsigned int>& devices) { 309 const std::vector<unsigned int>& devices) {
272 touch_device_lookup_.reset(); 310 touch_device_lookup_.reset();
273 touch_device_list_.clear(); 311 touch_device_list_.clear();
274 for (std::vector<unsigned int>::const_iterator iter = devices.begin(); 312 for (std::vector<unsigned int>::const_iterator iter = devices.begin();
275 iter != devices.end(); ++iter) { 313 iter != devices.end(); ++iter) {
276 DCHECK(*iter < touch_device_lookup_.size()); 314 DCHECK(*iter < touch_device_lookup_.size());
277 touch_device_lookup_[*iter] = true; 315 touch_device_lookup_[*iter] = true;
278 touch_device_list_.push_back(*iter); 316 touch_device_list_.push_back(*iter);
279 } 317 }
280 318
281 SetupValuator(); 319 SetupValuator();
282 } 320 }
283 321
284 bool TouchFactory::IsTouchDevice(unsigned deviceid) const { 322 bool TouchFactory::IsTouchDevice(unsigned deviceid) const {
285 return deviceid < touch_device_lookup_.size() ? 323 return deviceid < touch_device_lookup_.size() ?
286 touch_device_lookup_[deviceid] : false; 324 touch_device_lookup_[deviceid] : false;
287 } 325 }
288 326
327 #if !defined(USE_XI2_1)
289 bool TouchFactory::IsSlotUsed(int slot) const { 328 bool TouchFactory::IsSlotUsed(int slot) const {
290 CHECK_LT(slot, kMaxTouchPoints); 329 CHECK_LT(slot, kMaxTouchPoints);
291 return slots_used_[slot]; 330 return slots_used_[slot];
292 } 331 }
293 332
294 void TouchFactory::SetSlotUsed(int slot, bool used) { 333 void TouchFactory::SetSlotUsed(int slot, bool used) {
295 CHECK_LT(slot, kMaxTouchPoints); 334 CHECK_LT(slot, kMaxTouchPoints);
296 slots_used_[slot] = used; 335 slots_used_[slot] = used;
297 } 336 }
337 #endif
298 338
299 bool TouchFactory::GrabTouchDevices(Display* display, ::Window window) { 339 bool TouchFactory::GrabTouchDevices(Display* display, ::Window window) {
300 #if defined(TOUCH_UI) 340 #if defined(TOUCH_UI)
301 if (!base::MessagePumpForUI::HasXInput2() || 341 if (!base::MessagePumpForUI::HasXInput2() ||
302 touch_device_list_.empty()) 342 touch_device_list_.empty())
303 return true; 343 return true;
304 #endif 344 #endif
305 345
306 unsigned char mask[XIMaskLen(XI_LASTEVENT)]; 346 unsigned char mask[XIMaskLen(XI_LASTEVENT)];
307 bool success = true; 347 bool success = true;
308 348
309 memset(mask, 0, sizeof(mask)); 349 memset(mask, 0, sizeof(mask));
350 #if defined(USE_XI2_1)
351 XISetMask(mask, XI_TouchBegin);
352 XISetMask(mask, XI_TouchUpdate);
353 XISetMask(mask, XI_TouchEnd);
354 #else
sadrul 2011/09/07 14:56:37 ditto
ningxin.hu 2011/09/08 08:24:50 Will select touch and mouse events for XI2.1
310 XISetMask(mask, XI_ButtonPress); 355 XISetMask(mask, XI_ButtonPress);
311 XISetMask(mask, XI_ButtonRelease); 356 XISetMask(mask, XI_ButtonRelease);
312 XISetMask(mask, XI_Motion); 357 XISetMask(mask, XI_Motion);
358 #endif
313 359
314 XIEventMask evmask; 360 XIEventMask evmask;
315 evmask.mask_len = sizeof(mask); 361 evmask.mask_len = sizeof(mask);
316 evmask.mask = mask; 362 evmask.mask = mask;
317 for (std::vector<int>::const_iterator iter = 363 for (std::vector<int>::const_iterator iter =
318 touch_device_list_.begin(); 364 touch_device_list_.begin();
319 iter != touch_device_list_.end(); ++iter) { 365 iter != touch_device_list_.end(); ++iter) {
320 evmask.deviceid = *iter; 366 evmask.deviceid = *iter;
321 Status status = XIGrabDevice(display, *iter, window, CurrentTime, None, 367 Status status = XIGrabDevice(display, *iter, window, CurrentTime, None,
322 GrabModeAsync, GrabModeAsync, False, &evmask); 368 GrabModeAsync, GrabModeAsync, False, &evmask);
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after
408 float* value) { 454 float* value) {
409 XIDeviceEvent* xiev = static_cast<XIDeviceEvent*>(xev.xcookie.data); 455 XIDeviceEvent* xiev = static_cast<XIDeviceEvent*>(xev.xcookie.data);
410 if (xiev->sourceid >= kMaxDeviceNum) 456 if (xiev->sourceid >= kMaxDeviceNum)
411 return false; 457 return false;
412 int v = valuator_lookup_[xiev->sourceid][tp]; 458 int v = valuator_lookup_[xiev->sourceid][tp];
413 if (v >= 0 && XIMaskIsSet(xiev->valuators.mask, v)) { 459 if (v >= 0 && XIMaskIsSet(xiev->valuators.mask, v)) {
414 *value = xiev->valuators.values[v]; 460 *value = xiev->valuators.values[v];
415 return true; 461 return true;
416 } 462 }
417 463
464 // With XInput 2.1, Tracking ID could be provided in the detail field
465 if (tp == TP_TRACKING_ID) {
466 *value = xiev->detail;
467 return true;
468 }
469
418 return false; 470 return false;
419 } 471 }
420 472
421 bool TouchFactory::NormalizeTouchParam(unsigned int deviceid, 473 bool TouchFactory::NormalizeTouchParam(unsigned int deviceid,
422 TouchParam tp, 474 TouchParam tp,
423 float* value) { 475 float* value) {
424 float max_value; 476 float max_value;
425 float min_value; 477 float min_value;
426 if (GetTouchParamRange(deviceid, tp, &min_value, &max_value)) { 478 if (GetTouchParamRange(deviceid, tp, &min_value, &max_value)) {
427 *value = (*value - min_value) / (max_value - min_value); 479 *value = (*value - min_value) / (max_value - min_value);
428 DCHECK(*value >= 0.0 && *value <= 1.0); 480 DCHECK(*value >= 0.0 && *value <= 1.0);
429 return true; 481 return true;
430 } 482 }
431 return false; 483 return false;
432 } 484 }
433 485
434 bool TouchFactory::GetTouchParamRange(unsigned int deviceid, 486 bool TouchFactory::GetTouchParamRange(unsigned int deviceid,
435 TouchParam tp, 487 TouchParam tp,
436 float* min, 488 float* min,
437 float* max) { 489 float* max) {
438 if (valuator_lookup_[deviceid][tp] >= 0) { 490 if (valuator_lookup_[deviceid][tp] >= 0) {
439 *min = touch_param_min_[deviceid][tp]; 491 *min = touch_param_min_[deviceid][tp];
440 *max = touch_param_max_[deviceid][tp]; 492 *max = touch_param_max_[deviceid][tp];
441 return true; 493 return true;
442 } 494 }
443 return false; 495 return false;
444 } 496 }
445 497
446 } // namespace views 498 } // namespace views
OLDNEW
« views/focus/accelerator_handler_touch.cc ('K') | « views/touchui/touch_factory.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698