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