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

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: select mouse and touch events when XI2.1 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
« views/touchui/touch_factory.h ('K') | « views/touchui/touch_factory.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 XIEvent* event = static_cast<XIEvent*>(xev->xcookie.data);
257 XIDeviceEvent* xiev = reinterpret_cast<XIDeviceEvent*>(event);
234 258
235 XGenericEventCookie* cookie = &xev->xcookie; 259 #if defined(USE_XI2_1)
236 if (cookie->evtype != XI_ButtonPress && 260 if (event->evtype == XI_TouchBegin ||
237 cookie->evtype != XI_ButtonRelease && 261 event->evtype == XI_TouchUpdate ||
238 cookie->evtype != XI_Motion) 262 event->evtype == XI_TouchEnd) {
263 return touch_device_lookup_[xiev->sourceid];
264 }
265 #endif
266 if (event->evtype != XI_ButtonPress &&
267 event->evtype != XI_ButtonRelease &&
268 event->evtype != XI_Motion)
239 return true; 269 return true;
240 270
241 XIDeviceEvent* xiev = static_cast<XIDeviceEvent*>(cookie->data);
242 return pointer_device_lookup_[xiev->deviceid]; 271 return pointer_device_lookup_[xiev->deviceid];
243 } 272 }
244 273
245 void TouchFactory::SetupXI2ForXWindow(Window window) { 274 void TouchFactory::SetupXI2ForXWindow(Window window) {
246 // Setup mask for mouse events. It is possible that a device is loaded/plugged 275 // 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 276 // 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 277 // 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 278 // 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 279 // the events from uninteresting devices. We do the latter because that's
251 // simpler. 280 // simpler.
252 281
253 Display* display = ui::GetXDisplay(); 282 Display* display = ui::GetXDisplay();
254 283
255 unsigned char mask[XIMaskLen(XI_LASTEVENT)]; 284 unsigned char mask[XIMaskLen(XI_LASTEVENT)];
256 memset(mask, 0, sizeof(mask)); 285 memset(mask, 0, sizeof(mask));
257 286
287 #if defined(USE_XI2_1)
288 XISetMask(mask, XI_TouchBegin);
289 XISetMask(mask, XI_TouchUpdate);
290 XISetMask(mask, XI_TouchEnd);
291 #endif
258 XISetMask(mask, XI_ButtonPress); 292 XISetMask(mask, XI_ButtonPress);
259 XISetMask(mask, XI_ButtonRelease); 293 XISetMask(mask, XI_ButtonRelease);
260 XISetMask(mask, XI_Motion); 294 XISetMask(mask, XI_Motion);
261 295
262 XIEventMask evmask; 296 XIEventMask evmask;
263 evmask.deviceid = XIAllDevices; 297 evmask.deviceid = XIAllDevices;
264 evmask.mask_len = sizeof(mask); 298 evmask.mask_len = sizeof(mask);
265 evmask.mask = mask; 299 evmask.mask = mask;
266 XISelectEvents(display, window, &evmask, 1); 300 XISelectEvents(display, window, &evmask, 1);
267 XFlush(display); 301 XFlush(display);
(...skipping 11 matching lines...) Expand all
279 } 313 }
280 314
281 SetupValuator(); 315 SetupValuator();
282 } 316 }
283 317
284 bool TouchFactory::IsTouchDevice(unsigned deviceid) const { 318 bool TouchFactory::IsTouchDevice(unsigned deviceid) const {
285 return deviceid < touch_device_lookup_.size() ? 319 return deviceid < touch_device_lookup_.size() ?
286 touch_device_lookup_[deviceid] : false; 320 touch_device_lookup_[deviceid] : false;
287 } 321 }
288 322
323 #if !defined(USE_XI2_1)
289 bool TouchFactory::IsSlotUsed(int slot) const { 324 bool TouchFactory::IsSlotUsed(int slot) const {
290 CHECK_LT(slot, kMaxTouchPoints); 325 CHECK_LT(slot, kMaxTouchPoints);
291 return slots_used_[slot]; 326 return slots_used_[slot];
292 } 327 }
293 328
294 void TouchFactory::SetSlotUsed(int slot, bool used) { 329 void TouchFactory::SetSlotUsed(int slot, bool used) {
295 CHECK_LT(slot, kMaxTouchPoints); 330 CHECK_LT(slot, kMaxTouchPoints);
296 slots_used_[slot] = used; 331 slots_used_[slot] = used;
297 } 332 }
333 #endif
298 334
299 bool TouchFactory::GrabTouchDevices(Display* display, ::Window window) { 335 bool TouchFactory::GrabTouchDevices(Display* display, ::Window window) {
300 #if defined(TOUCH_UI) 336 #if defined(TOUCH_UI)
301 if (!base::MessagePumpForUI::HasXInput2() || 337 if (!base::MessagePumpForUI::HasXInput2() ||
302 touch_device_list_.empty()) 338 touch_device_list_.empty())
303 return true; 339 return true;
304 #endif 340 #endif
305 341
306 unsigned char mask[XIMaskLen(XI_LASTEVENT)]; 342 unsigned char mask[XIMaskLen(XI_LASTEVENT)];
307 bool success = true; 343 bool success = true;
308 344
309 memset(mask, 0, sizeof(mask)); 345 memset(mask, 0, sizeof(mask));
346 #if defined(USE_XI2_1)
347 XISetMask(mask, XI_TouchBegin);
348 XISetMask(mask, XI_TouchUpdate);
349 XISetMask(mask, XI_TouchEnd);
350 #endif
310 XISetMask(mask, XI_ButtonPress); 351 XISetMask(mask, XI_ButtonPress);
311 XISetMask(mask, XI_ButtonRelease); 352 XISetMask(mask, XI_ButtonRelease);
312 XISetMask(mask, XI_Motion); 353 XISetMask(mask, XI_Motion);
313 354
314 XIEventMask evmask; 355 XIEventMask evmask;
315 evmask.mask_len = sizeof(mask); 356 evmask.mask_len = sizeof(mask);
316 evmask.mask = mask; 357 evmask.mask = mask;
317 for (std::vector<int>::const_iterator iter = 358 for (std::vector<int>::const_iterator iter =
318 touch_device_list_.begin(); 359 touch_device_list_.begin();
319 iter != touch_device_list_.end(); ++iter) { 360 iter != touch_device_list_.end(); ++iter) {
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after
408 float* value) { 449 float* value) {
409 XIDeviceEvent* xiev = static_cast<XIDeviceEvent*>(xev.xcookie.data); 450 XIDeviceEvent* xiev = static_cast<XIDeviceEvent*>(xev.xcookie.data);
410 if (xiev->sourceid >= kMaxDeviceNum) 451 if (xiev->sourceid >= kMaxDeviceNum)
411 return false; 452 return false;
412 int v = valuator_lookup_[xiev->sourceid][tp]; 453 int v = valuator_lookup_[xiev->sourceid][tp];
413 if (v >= 0 && XIMaskIsSet(xiev->valuators.mask, v)) { 454 if (v >= 0 && XIMaskIsSet(xiev->valuators.mask, v)) {
414 *value = xiev->valuators.values[v]; 455 *value = xiev->valuators.values[v];
415 return true; 456 return true;
416 } 457 }
417 458
459 // With XInput 2.1, Tracking ID could be provided in the detail field
460 if (tp == TP_TRACKING_ID) {
461 *value = xiev->detail;
462 return true;
463 }
464
418 return false; 465 return false;
419 } 466 }
420 467
421 bool TouchFactory::NormalizeTouchParam(unsigned int deviceid, 468 bool TouchFactory::NormalizeTouchParam(unsigned int deviceid,
422 TouchParam tp, 469 TouchParam tp,
423 float* value) { 470 float* value) {
424 float max_value; 471 float max_value;
425 float min_value; 472 float min_value;
426 if (GetTouchParamRange(deviceid, tp, &min_value, &max_value)) { 473 if (GetTouchParamRange(deviceid, tp, &min_value, &max_value)) {
427 *value = (*value - min_value) / (max_value - min_value); 474 *value = (*value - min_value) / (max_value - min_value);
428 DCHECK(*value >= 0.0 && *value <= 1.0); 475 DCHECK(*value >= 0.0 && *value <= 1.0);
429 return true; 476 return true;
430 } 477 }
431 return false; 478 return false;
432 } 479 }
433 480
434 bool TouchFactory::GetTouchParamRange(unsigned int deviceid, 481 bool TouchFactory::GetTouchParamRange(unsigned int deviceid,
435 TouchParam tp, 482 TouchParam tp,
436 float* min, 483 float* min,
437 float* max) { 484 float* max) {
438 if (valuator_lookup_[deviceid][tp] >= 0) { 485 if (valuator_lookup_[deviceid][tp] >= 0) {
439 *min = touch_param_min_[deviceid][tp]; 486 *min = touch_param_min_[deviceid][tp];
440 *max = touch_param_max_[deviceid][tp]; 487 *max = touch_param_max_[deviceid][tp];
441 return true; 488 return true;
442 } 489 }
443 return false; 490 return false;
444 } 491 }
445 492
446 } // namespace views 493 } // namespace views
OLDNEW
« views/touchui/touch_factory.h ('K') | « views/touchui/touch_factory.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698