Upstream version 10.38.222.0
[platform/framework/web/crosswalk.git] / src / ui / display / chromeos / x11 / touchscreen_device_manager_x11.cc
1 // Copyright 2014 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/display/chromeos/x11/touchscreen_device_manager_x11.h"
6
7 #include <X11/extensions/XInput.h>
8 #include <X11/extensions/XInput2.h>
9
10 #include <cmath>
11 #include <set>
12 #include <string>
13 #include <vector>
14
15 #include "base/command_line.h"
16 #include "base/files/file_enumerator.h"
17 #include "base/logging.h"
18 #include "base/process/launch.h"
19 #include "base/strings/string_util.h"
20 #include "base/sys_info.h"
21 #include "ui/gfx/x/x11_types.h"
22
23 namespace {
24
25 // We consider the touchscreen to be internal if it is an I2c device.
26 // With the device id, we can query X to get the device's dev input
27 // node eventXXX. Then we search all the dev input nodes registered
28 // by I2C devices to see if we can find eventXXX.
29 bool IsTouchscreenInternal(XDisplay* dpy, int device_id) {
30   using base::FileEnumerator;
31   using base::FilePath;
32
33   if (!base::SysInfo::IsRunningOnChromeOS())
34     return false;
35
36   // Input device has a property "Device Node" pointing to its dev input node,
37   // e.g.   Device Node (250): "/dev/input/event8"
38   Atom device_node = XInternAtom(dpy, "Device Node", False);
39   if (device_node == None)
40     return false;
41
42   Atom actual_type;
43   int actual_format;
44   unsigned long nitems, bytes_after;
45   unsigned char* data;
46   XDevice* dev = XOpenDevice(dpy, device_id);
47   if (!dev)
48     return false;
49
50   if (XGetDeviceProperty(dpy, dev, device_node, 0, 1000, False,
51                          AnyPropertyType, &actual_type, &actual_format,
52                          &nitems, &bytes_after, &data) != Success) {
53     XCloseDevice(dpy, dev);
54     return false;
55   }
56   base::FilePath dev_node_path(reinterpret_cast<char*>(data));
57   XFree(data);
58   XCloseDevice(dpy, dev);
59
60   std::string event_node = dev_node_path.BaseName().value();
61   if (event_node.empty() ||
62       !StartsWithASCII(event_node, "event", false)) {
63     return false;
64   }
65
66   // Extract id "XXX" from "eventXXX"
67   std::string event_node_id = event_node.substr(5);
68
69   // I2C input device registers its dev input node at
70   // /sys/bus/i2c/devices/*/input/inputXXX/eventXXX
71   FileEnumerator i2c_enum(FilePath(FILE_PATH_LITERAL("/sys/bus/i2c/devices/")),
72                           false,
73                           base::FileEnumerator::DIRECTORIES);
74   for (FilePath i2c_name = i2c_enum.Next();
75        !i2c_name.empty();
76        i2c_name = i2c_enum.Next()) {
77     FileEnumerator input_enum(i2c_name.Append(FILE_PATH_LITERAL("input")),
78                               false,
79                               base::FileEnumerator::DIRECTORIES,
80                               FILE_PATH_LITERAL("input*"));
81     for (base::FilePath input = input_enum.Next();
82          !input.empty();
83          input = input_enum.Next()) {
84       if (input.BaseName().value().substr(5) == event_node_id)
85         return true;
86     }
87   }
88
89   return false;
90 }
91
92 }  // namespace
93
94 namespace ui {
95
96 TouchscreenDeviceManagerX11::TouchscreenDeviceManagerX11()
97     : display_(gfx::GetXDisplay()) {}
98
99 TouchscreenDeviceManagerX11::~TouchscreenDeviceManagerX11() {}
100
101 std::vector<TouchscreenDevice> TouchscreenDeviceManagerX11::GetDevices() {
102   std::vector<TouchscreenDevice> devices;
103   int num_devices = 0;
104   Atom valuator_x = XInternAtom(display_, "Abs MT Position X", False);
105   Atom valuator_y = XInternAtom(display_, "Abs MT Position Y", False);
106   if (valuator_x == None || valuator_y == None)
107     return devices;
108
109   std::set<int> no_match_touchscreen;
110   XIDeviceInfo* info = XIQueryDevice(display_, XIAllDevices, &num_devices);
111   for (int i = 0; i < num_devices; i++) {
112     if (!info[i].enabled || info[i].use != XIFloatingSlave)
113       continue;  // Assume all touchscreens are floating slaves
114
115     double width = -1.0;
116     double height = -1.0;
117     bool is_direct_touch = false;
118
119     for (int j = 0; j < info[i].num_classes; j++) {
120       XIAnyClassInfo* class_info = info[i].classes[j];
121
122       if (class_info->type == XIValuatorClass) {
123         XIValuatorClassInfo* valuator_info =
124             reinterpret_cast<XIValuatorClassInfo*>(class_info);
125
126         if (valuator_x == valuator_info->label) {
127           // Ignore X axis valuator with unexpected properties
128           if (valuator_info->number == 0 && valuator_info->mode == Absolute &&
129               valuator_info->min == 0.0) {
130             width = valuator_info->max;
131           }
132         } else if (valuator_y == valuator_info->label) {
133           // Ignore Y axis valuator with unexpected properties
134           if (valuator_info->number == 1 && valuator_info->mode == Absolute &&
135               valuator_info->min == 0.0) {
136             height = valuator_info->max;
137           }
138         }
139       }
140 #if defined(USE_XI2_MT)
141       if (class_info->type == XITouchClass) {
142         XITouchClassInfo* touch_info =
143             reinterpret_cast<XITouchClassInfo*>(class_info);
144         is_direct_touch = touch_info->mode == XIDirectTouch;
145       }
146 #endif
147     }
148
149     // Touchscreens should have absolute X and Y axes, and be direct touch
150     // devices.
151     if (width > 0.0 && height > 0.0 && is_direct_touch) {
152       bool is_internal = IsTouchscreenInternal(display_, info[i].deviceid);
153       devices.push_back(TouchscreenDevice(info[i].deviceid,
154                                           gfx::Size(width, height),
155                                           is_internal));
156     }
157   }
158
159   XIFreeDeviceInfo(info);
160   return devices;
161 }
162
163 }  // namespace ui