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