Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / ui / events / x / touch_factory_x11.cc
index 58235da..7d6b844 100644 (file)
@@ -4,11 +4,11 @@
 
 #include "ui/events/x/touch_factory_x11.h"
 
+#include <X11/Xatom.h>
 #include <X11/cursorfont.h>
 #include <X11/extensions/XInput.h>
 #include <X11/extensions/XInput2.h>
 #include <X11/extensions/XIproto.h>
-#include <string>
 
 #include "base/basictypes.h"
 #include "base/command_line.h"
@@ -18,7 +18,9 @@
 #include "base/message_loop/message_loop.h"
 #include "base/strings/string_number_conversions.h"
 #include "base/strings/string_split.h"
+#include "base/sys_info.h"
 #include "ui/events/event_switches.h"
+#include "ui/events/x/device_data_manager_x11.h"
 #include "ui/events/x/device_list_cache_x.h"
 #include "ui/gfx/x/x11_types.h"
 
@@ -29,14 +31,10 @@ TouchFactory::TouchFactory()
       touch_device_available_(false),
       touch_events_disabled_(false),
       touch_device_list_(),
-#if defined(USE_XI2_MT)
-      id_generator_(0),
-#endif
-      slots_used_() {
-#if defined(USE_AURA)
-  if (!base::MessagePumpForUI::HasXInput2())
+      max_touch_points_(-1),
+      id_generator_(0) {
+  if (!DeviceDataManagerX11::GetInstance()->IsXInput2Available())
     return;
-#endif
 
   XDisplay* display = gfx::GetXDisplay();
   UpdateDeviceList(display);
@@ -57,7 +55,6 @@ TouchFactory* TouchFactory::GetInstance() {
 
 // static
 void TouchFactory::SetTouchDeviceListFromCommandLine() {
-#if defined(TOOLKIT_VIEWS)
   // Get a list of pointer-devices that should be treated as touch-devices.
   // This is primarily used for testing/debugging touch-event processing when a
   // touch-device isn't available.
@@ -79,7 +76,6 @@ void TouchFactory::SetTouchDeviceListFromCommandLine() {
     }
     ui::TouchFactory::GetInstance()->SetTouchDeviceList(device_ids);
   }
-#endif
 }
 
 void TouchFactory::UpdateDeviceList(Display* display) {
@@ -87,6 +83,8 @@ void TouchFactory::UpdateDeviceList(Display* display) {
   touch_device_available_ = false;
   touch_device_lookup_.reset();
   touch_device_list_.clear();
+  touchscreen_ids_.clear();
+  max_touch_points_ = -1;
 
 #if !defined(USE_XI2_MT)
   // NOTE: The new API for retrieving the list of devices (XIQueryDevice) does
@@ -94,17 +92,10 @@ void TouchFactory::UpdateDeviceList(Display* display) {
   // old version of query function (XListInputDevices) is used instead.
   // If XInput2 is not supported, this will return null (with count of -1) so
   // we assume there cannot be any touch devices.
-  // With XI2.1 or older, we allow only single touch devices unless
-  // |enable_xi2_mt| is turned on.
+  // With XI2.1 or older, we allow only single touch devices.
   XDeviceList dev_list =
       DeviceListCacheX::GetInstance()->GetXDeviceList(display);
-#if !defined(ENABLE_XI21_MT)
   Atom xi_touchscreen = XInternAtom(display, XI_TOUCHSCREEN, false);
-#else
-  // Certain Samsung and Intel devices support multi-touch with XInput2.1
-  // using the device type "MULTITOUCHSCREEN".
-  Atom xi_touchscreen = XInternAtom(display, "MULTITOUCHSCREEN", false);
-#endif
   for (int i = 0; i < dev_list.count; i++) {
     if (dev_list[i].type == xi_touchscreen) {
       touch_device_lookup_[dev_list[i].id] = true;
@@ -114,6 +105,9 @@ void TouchFactory::UpdateDeviceList(Display* display) {
   }
 #endif
 
+  if (!DeviceDataManagerX11::GetInstance()->IsXInput2Available())
+    return;
+
   // Instead of asking X for the list of devices all the time, let's maintain a
   // list of pointer devices we care about.
   // It should not be necessary to select for slave devices. XInput2 provides
@@ -137,18 +131,35 @@ void TouchFactory::UpdateDeviceList(Display* display) {
         XIAnyClassInfo* xiclassinfo = devinfo->classes[k];
         if (xiclassinfo->type == XITouchClass) {
           XITouchClassInfo* tci =
-              reinterpret_cast<XITouchClassInfo *>(xiclassinfo);
+              reinterpret_cast<XITouchClassInfo*>(xiclassinfo);
           // Only care direct touch device (such as touch screen) right now
           if (tci->mode == XIDirectTouch) {
             touch_device_lookup_[devinfo->deviceid] = true;
             touch_device_list_[devinfo->deviceid] = true;
             touch_device_available_ = true;
+            if (tci->num_touches > 0 && tci->num_touches > max_touch_points_)
+              max_touch_points_ = tci->num_touches;
           }
         }
       }
 #endif
       pointer_device_lookup_[devinfo->deviceid] = true;
     }
+
+#if defined(USE_XI2_MT)
+    if (devinfo->use == XIFloatingSlave || devinfo->use == XISlavePointer) {
+      for (int k = 0; k < devinfo->num_classes; ++k) {
+        XIAnyClassInfo* xiclassinfo = devinfo->classes[k];
+        if (xiclassinfo->type == XITouchClass) {
+          XITouchClassInfo* tci =
+              reinterpret_cast<XITouchClassInfo*>(xiclassinfo);
+          // Only care direct touch device (such as touch screen) right now
+          if (tci->mode == XIDirectTouch)
+            CacheTouchscreenIds(display, devinfo->deviceid);
+        }
+      }
+    }
+#endif
   }
 }
 
@@ -164,6 +175,10 @@ bool TouchFactory::ShouldProcessXI2Event(XEvent* xev) {
     return !touch_events_disabled_ && IsTouchDevice(xiev->deviceid);
   }
 #endif
+  // Make sure only key-events from the master device are processed.
+  if (event->evtype == XI_KeyPress || event->evtype == XI_KeyRelease)
+    return xiev->deviceid == xiev->sourceid;
+
   if (event->evtype != XI_ButtonPress &&
       event->evtype != XI_ButtonRelease &&
       event->evtype != XI_Motion)
@@ -196,6 +211,12 @@ void TouchFactory::SetupXI2ForXWindow(Window window) {
   XISetMask(mask, XI_ButtonPress);
   XISetMask(mask, XI_ButtonRelease);
   XISetMask(mask, XI_Motion);
+#if defined(OS_CHROMEOS)
+  if (base::SysInfo::IsRunningOnChromeOS()) {
+    XISetMask(mask, XI_KeyPress);
+    XISetMask(mask, XI_KeyRelease);
+  }
+#endif
 
   XIEventMask evmask;
   evmask.deviceid = XIAllDevices;
@@ -229,7 +250,6 @@ bool TouchFactory::IsMultiTouchDevice(unsigned int deviceid) const {
           false;
 }
 
-#if defined(USE_XI2_MT)
 bool TouchFactory::QuerySlotForTrackingID(uint32 tracking_id, int* slot) {
   if (!id_generator_.HasGeneratedIDFor(tracking_id))
     return false;
@@ -241,23 +261,34 @@ int TouchFactory::GetSlotForTrackingID(uint32 tracking_id) {
   return id_generator_.GetGeneratedID(tracking_id);
 }
 
+void TouchFactory::AcquireSlotForTrackingID(uint32 tracking_id) {
+  tracking_id_refcounts_[tracking_id]++;
+}
+
 void TouchFactory::ReleaseSlotForTrackingID(uint32 tracking_id) {
-  id_generator_.ReleaseNumber(tracking_id);
+  tracking_id_refcounts_[tracking_id]--;
+  if (tracking_id_refcounts_[tracking_id] == 0)
+    id_generator_.ReleaseNumber(tracking_id);
 }
-#endif
 
-bool TouchFactory::IsSlotUsed(int slot) const {
-  CHECK_LT(slot, kMaxTouchPoints);
-  return slots_used_[slot];
+bool TouchFactory::IsTouchDevicePresent() {
+  return !touch_events_disabled_ && touch_device_available_;
 }
 
-void TouchFactory::SetSlotUsed(int slot, bool used) {
-  CHECK_LT(slot, kMaxTouchPoints);
-  slots_used_[slot] = used;
+int TouchFactory::GetMaxTouchPoints() const {
+  return max_touch_points_;
 }
 
-bool TouchFactory::IsTouchDevicePresent() {
-  return !touch_events_disabled_ && touch_device_available_;
+void TouchFactory::ResetForTest() {
+  pointer_device_lookup_.reset();
+  touch_device_lookup_.reset();
+  touch_device_available_ = false;
+  touch_events_disabled_ = false;
+  touch_device_list_.clear();
+  touchscreen_ids_.clear();
+  tracking_id_refcounts_.clear();
+  max_touch_points_ = -1;
+  id_generator_.ResetForTest();
 }
 
 void TouchFactory::SetTouchDeviceForTest(
@@ -274,4 +305,52 @@ void TouchFactory::SetTouchDeviceForTest(
   touch_events_disabled_ = false;
 }
 
+void TouchFactory::SetPointerDeviceForTest(
+    const std::vector<unsigned int>& devices) {
+  pointer_device_lookup_.reset();
+  for (std::vector<unsigned int>::const_iterator iter = devices.begin();
+       iter != devices.end(); ++iter) {
+    pointer_device_lookup_[*iter] = true;
+  }
+}
+
+void TouchFactory::CacheTouchscreenIds(Display* display, int device_id) {
+  XDevice* device = XOpenDevice(display, device_id);
+  if (!device)
+    return;
+
+  Atom actual_type_return;
+  int actual_format_return;
+  unsigned long nitems_return;
+  unsigned long bytes_after_return;
+  unsigned char *prop_return;
+
+  const char kDeviceProductIdString[] = "Device Product ID";
+  Atom device_product_id_atom =
+      XInternAtom(display, kDeviceProductIdString, false);
+
+  if (device_product_id_atom != None &&
+      XGetDeviceProperty(display, device, device_product_id_atom, 0, 2,
+                         False, XA_INTEGER, &actual_type_return,
+                         &actual_format_return, &nitems_return,
+                         &bytes_after_return, &prop_return) == Success) {
+    if (actual_type_return == XA_INTEGER &&
+        actual_format_return == 32 &&
+        nitems_return == 2) {
+      // An actual_format_return of 32 implies that the returned data is an
+      // array of longs. See the description of |prop_return| in `man
+      // XGetDeviceProperty` for details.
+      long* ptr = reinterpret_cast<long*>(prop_return);
+
+      // Internal displays will have a vid and pid of 0. Ignore them.
+      // ptr[0] is the vid, and ptr[1] is the pid.
+      if (ptr[0] || ptr[1])
+        touchscreen_ids_.insert(std::make_pair(ptr[0], ptr[1]));
+    }
+    XFree(prop_return);
+  }
+
+  XCloseDevice(display, device);
+}
+
 }  // namespace ui