Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / ui / events / devices / device_util_linux.cc
blob9f1f6329c7eb8381f6b58aa9e50fe638350d91bf
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/events/devices/device_util_linux.h"
7 #include <string>
9 #include "base/files/file_enumerator.h"
10 #include "base/files/file_path.h"
11 #include "base/files/file_util.h"
12 #include "base/message_loop/message_loop.h"
13 #include "base/strings/string_util.h"
15 namespace ui {
17 InputDeviceType GetInputDeviceTypeFromPath(const base::FilePath& path) {
18 DCHECK(!base::MessageLoopForUI::IsCurrent());
19 std::string event_node = path.BaseName().value();
20 if (event_node.empty() ||
21 !base::StartsWith(event_node, "event",
22 base::CompareCase::INSENSITIVE_ASCII))
23 return InputDeviceType::INPUT_DEVICE_UNKNOWN;
25 // Find sysfs device path for this device.
26 base::FilePath sysfs_path =
27 base::FilePath(FILE_PATH_LITERAL("/sys/class/input"));
28 sysfs_path = sysfs_path.Append(path.BaseName());
29 sysfs_path = base::MakeAbsoluteFilePath(sysfs_path);
31 // Device does not exist.
32 if (sysfs_path.empty())
33 return InputDeviceType::INPUT_DEVICE_UNKNOWN;
35 // Check ancestor devices for a known bus attachment.
36 for (base::FilePath path = sysfs_path; path != base::FilePath("/");
37 path = path.DirName()) {
38 // Bluetooth LE devices are virtual "uhid" devices.
39 if (path ==
40 base::FilePath(FILE_PATH_LITERAL("/sys/devices/virtual/misc/uhid")))
41 return InputDeviceType::INPUT_DEVICE_EXTERNAL;
43 std::string subsystem_path =
44 base::MakeAbsoluteFilePath(path.Append(FILE_PATH_LITERAL("subsystem")))
45 .value();
46 if (subsystem_path.empty())
47 continue;
49 // Internal bus attachments.
50 if (subsystem_path == "/sys/bus/pci")
51 return InputDeviceType::INPUT_DEVICE_INTERNAL;
52 if (subsystem_path == "/sys/bus/i2c")
53 return InputDeviceType::INPUT_DEVICE_INTERNAL;
54 if (subsystem_path == "/sys/bus/acpi")
55 return InputDeviceType::INPUT_DEVICE_INTERNAL;
56 if (subsystem_path == "/sys/bus/serio")
57 return InputDeviceType::INPUT_DEVICE_INTERNAL;
58 if (subsystem_path == "/sys/bus/platform")
59 return InputDeviceType::INPUT_DEVICE_INTERNAL;
61 // External bus attachments.
62 if (subsystem_path == "/sys/bus/usb")
63 return InputDeviceType::INPUT_DEVICE_EXTERNAL;
64 if (subsystem_path == "/sys/class/bluetooth")
65 return InputDeviceType::INPUT_DEVICE_EXTERNAL;
68 return InputDeviceType::INPUT_DEVICE_UNKNOWN;
71 } // namespace