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"
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"
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() || !StartsWithASCII(event_node
, "event", false))
21 return InputDeviceType::INPUT_DEVICE_UNKNOWN
;
23 // Find sysfs device path for this device.
24 base::FilePath sysfs_path
=
25 base::FilePath(FILE_PATH_LITERAL("/sys/class/input"));
26 sysfs_path
= sysfs_path
.Append(path
.BaseName());
27 sysfs_path
= base::MakeAbsoluteFilePath(sysfs_path
);
29 // Device does not exist.
30 if (sysfs_path
.empty())
31 return InputDeviceType::INPUT_DEVICE_UNKNOWN
;
33 // Check ancestor devices for a known bus attachment.
34 for (base::FilePath path
= sysfs_path
; path
!= base::FilePath("/");
35 path
= path
.DirName()) {
36 // Bluetooth LE devices are virtual "uhid" devices.
38 base::FilePath(FILE_PATH_LITERAL("/sys/devices/virtual/misc/uhid")))
39 return InputDeviceType::INPUT_DEVICE_EXTERNAL
;
41 std::string subsystem_path
=
42 base::MakeAbsoluteFilePath(path
.Append(FILE_PATH_LITERAL("subsystem")))
44 if (subsystem_path
.empty())
47 // Internal bus attachments.
48 if (subsystem_path
== "/sys/bus/pci")
49 return InputDeviceType::INPUT_DEVICE_INTERNAL
;
50 if (subsystem_path
== "/sys/bus/i2c")
51 return InputDeviceType::INPUT_DEVICE_INTERNAL
;
52 if (subsystem_path
== "/sys/bus/acpi")
53 return InputDeviceType::INPUT_DEVICE_INTERNAL
;
54 if (subsystem_path
== "/sys/bus/serio")
55 return InputDeviceType::INPUT_DEVICE_INTERNAL
;
56 if (subsystem_path
== "/sys/bus/platform")
57 return InputDeviceType::INPUT_DEVICE_INTERNAL
;
59 // External bus attachments.
60 if (subsystem_path
== "/sys/bus/usb")
61 return InputDeviceType::INPUT_DEVICE_EXTERNAL
;
62 if (subsystem_path
== "/sys/class/bluetooth")
63 return InputDeviceType::INPUT_DEVICE_EXTERNAL
;
66 return InputDeviceType::INPUT_DEVICE_UNKNOWN
;