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 std::string subsystem_path
=
37 base::MakeAbsoluteFilePath(path
.Append(FILE_PATH_LITERAL("subsystem")))
39 if (subsystem_path
.empty())
42 // Internal bus attachments.
43 if (subsystem_path
== "/sys/bus/pci")
44 return InputDeviceType::INPUT_DEVICE_INTERNAL
;
45 if (subsystem_path
== "/sys/bus/i2c")
46 return InputDeviceType::INPUT_DEVICE_INTERNAL
;
47 if (subsystem_path
== "/sys/bus/acpi")
48 return InputDeviceType::INPUT_DEVICE_INTERNAL
;
49 if (subsystem_path
== "/sys/bus/serio")
50 return InputDeviceType::INPUT_DEVICE_INTERNAL
;
51 if (subsystem_path
== "/sys/bus/platform")
52 return InputDeviceType::INPUT_DEVICE_INTERNAL
;
54 // External bus attachments.
55 if (subsystem_path
== "/sys/bus/usb")
56 return InputDeviceType::INPUT_DEVICE_EXTERNAL
;
57 if (subsystem_path
== "/sys/class/bluetooth")
58 return InputDeviceType::INPUT_DEVICE_EXTERNAL
;
61 return InputDeviceType::INPUT_DEVICE_UNKNOWN
;