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.
5 #include "ui/display/chromeos/x11/touchscreen_device_manager_x11.h"
7 #include <X11/extensions/XInput.h>
8 #include <X11/extensions/XInput2.h>
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"
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;
33 if (!base::SysInfo::IsRunningOnChromeOS())
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)
44 unsigned long nitems, bytes_after;
46 XDevice* dev = XOpenDevice(dpy, device_id);
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);
56 base::FilePath dev_node_path(reinterpret_cast<char*>(data));
58 XCloseDevice(dpy, dev);
60 std::string event_node = dev_node_path.BaseName().value();
61 if (event_node.empty() ||
62 !StartsWithASCII(event_node, "event", false)) {
66 // Extract id "XXX" from "eventXXX"
67 std::string event_node_id = event_node.substr(5);
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/")),
73 base::FileEnumerator::DIRECTORIES);
74 for (FilePath i2c_name = i2c_enum.Next();
76 i2c_name = i2c_enum.Next()) {
77 FileEnumerator input_enum(i2c_name.Append(FILE_PATH_LITERAL("input")),
79 base::FileEnumerator::DIRECTORIES,
80 FILE_PATH_LITERAL("input*"));
81 for (base::FilePath input = input_enum.Next();
83 input = input_enum.Next()) {
84 if (input.BaseName().value().substr(5) == event_node_id)
96 TouchscreenDeviceManagerX11::TouchscreenDeviceManagerX11()
97 : display_(gfx::GetXDisplay()) {}
99 TouchscreenDeviceManagerX11::~TouchscreenDeviceManagerX11() {}
101 std::vector<TouchscreenDevice> TouchscreenDeviceManagerX11::GetDevices() {
102 std::vector<TouchscreenDevice> devices;
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)
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
116 double height = -1.0;
117 bool is_direct_touch = false;
119 for (int j = 0; j < info[i].num_classes; j++) {
120 XIAnyClassInfo* class_info = info[i].classes[j];
122 if (class_info->type == XIValuatorClass) {
123 XIValuatorClassInfo* valuator_info =
124 reinterpret_cast<XIValuatorClassInfo*>(class_info);
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;
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;
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;
149 // Touchscreens should have absolute X and Y axes, and be direct touch
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),
159 XIFreeDeviceInfo(info);