OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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 "ui/display/chromeos/x11/touchscreen_device_manager_x11.h" | 5 #include "ui/display/chromeos/x11/touchscreen_device_manager_x11.h" |
6 | 6 |
7 #include <X11/extensions/XInput.h> | 7 #include <X11/extensions/XInput.h> |
8 #include <X11/extensions/XInput2.h> | 8 #include <X11/extensions/XInput2.h> |
9 | 9 |
10 #include <cmath> | 10 #include <cmath> |
11 #include <set> | 11 #include <set> |
| 12 #include <string> |
| 13 #include <vector> |
12 | 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" |
13 #include "ui/gfx/x/x11_types.h" | 21 #include "ui/gfx/x/x11_types.h" |
14 | 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 |
15 namespace ui { | 94 namespace ui { |
16 | 95 |
17 TouchscreenDeviceManagerX11::TouchscreenDeviceManagerX11() | 96 TouchscreenDeviceManagerX11::TouchscreenDeviceManagerX11() |
18 : display_(gfx::GetXDisplay()) {} | 97 : display_(gfx::GetXDisplay()) {} |
19 | 98 |
20 TouchscreenDeviceManagerX11::~TouchscreenDeviceManagerX11() {} | 99 TouchscreenDeviceManagerX11::~TouchscreenDeviceManagerX11() {} |
21 | 100 |
22 std::vector<TouchscreenDevice> TouchscreenDeviceManagerX11::GetDevices() { | 101 std::vector<TouchscreenDevice> TouchscreenDeviceManagerX11::GetDevices() { |
23 std::vector<TouchscreenDevice> devices; | 102 std::vector<TouchscreenDevice> devices; |
24 int num_devices = 0; | 103 int num_devices = 0; |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
63 XITouchClassInfo* touch_info = | 142 XITouchClassInfo* touch_info = |
64 reinterpret_cast<XITouchClassInfo*>(class_info); | 143 reinterpret_cast<XITouchClassInfo*>(class_info); |
65 is_direct_touch = touch_info->mode == XIDirectTouch; | 144 is_direct_touch = touch_info->mode == XIDirectTouch; |
66 } | 145 } |
67 #endif | 146 #endif |
68 } | 147 } |
69 | 148 |
70 // Touchscreens should have absolute X and Y axes, and be direct touch | 149 // Touchscreens should have absolute X and Y axes, and be direct touch |
71 // devices. | 150 // devices. |
72 if (width > 0.0 && height > 0.0 && is_direct_touch) { | 151 if (width > 0.0 && height > 0.0 && is_direct_touch) { |
| 152 bool is_internal = IsTouchscreenInternal(display_, info[i].deviceid); |
73 devices.push_back(TouchscreenDevice(info[i].deviceid, | 153 devices.push_back(TouchscreenDevice(info[i].deviceid, |
74 gfx::Size(width, height))); | 154 gfx::Size(width, height), |
| 155 is_internal)); |
75 } | 156 } |
76 } | 157 } |
77 | 158 |
78 XIFreeDeviceInfo(info); | 159 XIFreeDeviceInfo(info); |
79 return devices; | 160 return devices; |
80 } | 161 } |
81 | 162 |
82 } // namespace ui | 163 } // namespace ui |
OLD | NEW |