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/events/ozone/evdev/device_manager_udev.h" | 5 #include "ui/events/ozone/evdev/device_manager_udev.h" |
6 | 6 |
7 #include <libudev.h> | |
8 | |
9 #include "base/debug/trace_event.h" | |
10 #include "base/files/file_path.h" | 7 #include "base/files/file_path.h" |
11 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
12 #include "base/strings/string_util.h" | 9 #include "base/strings/string_util.h" |
13 #include "base/strings/stringprintf.h" | 10 #include "ui/events/ozone/device_udev.h" |
14 #include "ui/events/ozone/evdev/device_manager_evdev.h" | 11 #include "ui/events/ozone/evdev/device_manager_evdev.h" |
15 #include "ui/events/ozone/evdev/event_factory_evdev.h" | 12 #include "ui/events/ozone/evdev/event_factory_evdev.h" |
16 #include "ui/events/ozone/evdev/scoped_udev.h" | 13 #include "ui/events/ozone/monitor_udev.h" |
| 14 #include "ui/events/ozone/scoped_udev.h" |
17 | 15 |
18 namespace ui { | 16 namespace ui { |
19 | 17 |
20 namespace { | 18 namespace { |
21 | 19 |
22 const char kSubsystemInput[] = "input"; | 20 const char kSubsystemInput[] = "input"; |
23 | 21 |
24 // Severity levels from syslog.h. We can't include it directly as it | |
25 // conflicts with base/logging.h | |
26 enum { | |
27 SYS_LOG_EMERG = 0, | |
28 SYS_LOG_ALERT = 1, | |
29 SYS_LOG_CRIT = 2, | |
30 SYS_LOG_ERR = 3, | |
31 SYS_LOG_WARNING = 4, | |
32 SYS_LOG_NOTICE = 5, | |
33 SYS_LOG_INFO = 6, | |
34 SYS_LOG_DEBUG = 7, | |
35 }; | |
36 | |
37 // Log handler for messages generated from libudev. | |
38 void UdevLog(struct udev* udev, | |
39 int priority, | |
40 const char* file, | |
41 int line, | |
42 const char* fn, | |
43 const char* format, | |
44 va_list args) { | |
45 if (priority <= SYS_LOG_ERR) | |
46 LOG(ERROR) << "libudev: " << fn << ": " << base::StringPrintV(format, args); | |
47 else if (priority <= SYS_LOG_INFO) | |
48 VLOG(1) << "libudev: " << fn << ": " << base::StringPrintV(format, args); | |
49 else // SYS_LOG_DEBUG | |
50 VLOG(2) << "libudev: " << fn << ": " << base::StringPrintV(format, args); | |
51 } | |
52 | |
53 // Create libudev context. | |
54 scoped_udev UdevCreate() { | |
55 struct udev* udev = udev_new(); | |
56 if (udev) { | |
57 udev_set_log_fn(udev, UdevLog); | |
58 udev_set_log_priority(udev, SYS_LOG_DEBUG); | |
59 } | |
60 return scoped_udev(udev); | |
61 } | |
62 | |
63 // Start monitoring input device changes. | |
64 scoped_udev_monitor UdevCreateMonitor(struct udev* udev) { | |
65 struct udev_monitor* monitor = udev_monitor_new_from_netlink(udev, "udev"); | |
66 if (monitor) { | |
67 udev_monitor_filter_add_match_subsystem_devtype( | |
68 monitor, kSubsystemInput, NULL); | |
69 | |
70 if (udev_monitor_enable_receiving(monitor)) | |
71 LOG(ERROR) << "failed to start receiving events from udev"; | |
72 } | |
73 | |
74 return scoped_udev_monitor(monitor); | |
75 } | |
76 | |
77 // Enumerate all input devices using udev. Calls device_callback per device. | |
78 bool UdevEnumerateInputDevices(struct udev* udev, | |
79 const EvdevDeviceCallback& device_callback) { | |
80 scoped_udev_enumerate enumerate(udev_enumerate_new(udev)); | |
81 if (!enumerate) | |
82 return false; | |
83 | |
84 // Build list of devices with subsystem "input". | |
85 udev_enumerate_add_match_subsystem(enumerate.get(), kSubsystemInput); | |
86 udev_enumerate_scan_devices(enumerate.get()); | |
87 | |
88 struct udev_list_entry* devices = | |
89 udev_enumerate_get_list_entry(enumerate.get()); | |
90 struct udev_list_entry* entry; | |
91 | |
92 // Run callback per device in the list. | |
93 udev_list_entry_foreach(entry, devices) { | |
94 const char* name = udev_list_entry_get_name(entry); | |
95 | |
96 scoped_udev_device device(udev_device_new_from_syspath(udev, name)); | |
97 if (!device) | |
98 continue; | |
99 | |
100 const char* path = udev_device_get_devnode(device.get()); | |
101 if (!path) | |
102 continue; | |
103 | |
104 // Filter non-evdev device notes. | |
105 if (!StartsWithASCII(path, "/dev/input/event", true)) | |
106 continue; | |
107 | |
108 // Found input device node; attach. | |
109 device_callback.Run(base::FilePath(path)); | |
110 } | |
111 | |
112 return true; | |
113 } | |
114 | |
115 // Device enumerator & monitor using udev. | |
116 // | |
117 // This class enumerates input devices attached to the system using udev. | |
118 // | |
119 // It also creates & monitors a udev netlink socket and issues callbacks for | |
120 // any changes to the set of attached devices. | |
121 // | |
122 // TODO(spang): Figure out how to test this. | 22 // TODO(spang): Figure out how to test this. |
123 class DeviceManagerUdev : public DeviceManagerEvdev, | 23 class DeviceManagerUdev : public DeviceManagerEvdev, MonitorUdev { |
124 base::MessagePumpLibevent::Watcher { | |
125 public: | 24 public: |
126 DeviceManagerUdev() {} | 25 DeviceManagerUdev(DeviceUdev* udev) : udev_(udev) {} |
127 virtual ~DeviceManagerUdev() { Stop(); } | 26 virtual ~DeviceManagerUdev() { Stop(); } |
128 | 27 |
| 28 private: |
| 29 // DeviceManagerEvdev overrides: |
| 30 // |
129 // Enumerate existing devices & start watching for device changes. | 31 // Enumerate existing devices & start watching for device changes. |
130 virtual void ScanAndStartMonitoring(const EvdevDeviceCallback& device_added, | 32 virtual void ScanAndStartMonitoring(const EvdevDeviceCallback& device_added, |
131 const EvdevDeviceCallback& device_removed) | 33 const EvdevDeviceCallback& device_removed) |
132 OVERRIDE { | 34 OVERRIDE { |
133 udev_ = UdevCreate(); | 35 // Save callbacks. |
134 if (!udev_) { | 36 device_added_ = device_added; |
135 LOG(ERROR) << "failed to initialize libudev"; | 37 device_removed_ = device_removed; |
136 return; | |
137 } | |
138 | 38 |
139 if (!StartMonitoring(device_added, device_removed)) | 39 if (!udev_->RegisterDeviceMonitor(this, kSubsystemInput)) |
140 LOG(ERROR) << "failed to start monitoring device changes via udev"; | 40 LOG(ERROR) << "failed to start monitoring device changes via udev"; |
141 | 41 |
142 if (!UdevEnumerateInputDevices(udev_.get(), device_added)) | 42 if (!udev_->ScanDevices(this, kSubsystemInput)) |
143 LOG(ERROR) << "failed to enumerate input devices via udev"; | 43 LOG(ERROR) << "failed to enumerate input devices via udev"; |
144 } | 44 } |
145 | 45 |
146 virtual void Stop() OVERRIDE { | 46 virtual void Stop() OVERRIDE { |
147 controller_.StopWatchingFileDescriptor(); | 47 udev_->UnregisterDeviceMonitor(this); |
148 device_added_.Reset(); | 48 device_added_.Reset(); |
149 device_removed_.Reset(); | 49 device_removed_.Reset(); |
150 } | 50 } |
151 | 51 |
152 virtual void OnFileCanReadWithoutBlocking(int fd) OVERRIDE { | 52 // MonitorUdev overrides: |
153 // The netlink socket should never become disconnected. There's no need | 53 virtual void HandleUdevEvent(scoped_udev_device device) OVERRIDE { |
154 // to handle broken connections here. | 54 const char* path = udev_device_get_devnode(device.get()); |
155 TRACE_EVENT1("ozone", "UdevDeviceChange", "socket", fd); | 55 const char* action = udev_device_get_action(device.get()); |
156 | 56 if (!path) |
157 scoped_udev_device device(udev_monitor_receive_device(udev_monitor_.get())); | |
158 if (!device) | |
159 return; | 57 return; |
160 | 58 |
161 const char* path = udev_device_get_devnode(device.get()); | 59 // Filter non-evdev device notes. |
162 const char* action = udev_device_get_action(device.get()); | 60 if (!StartsWithASCII(path, "/dev/input/event", true)) |
163 if (!path || !action) | |
164 return; | 61 return; |
165 | 62 |
166 if (!strcmp(action, "add") || !strcmp(action, "change")) | 63 if (!action || !strcmp(action, "add") || !strcmp(action, "change")) |
167 device_added_.Run(base::FilePath(path)); | 64 device_added_.Run(base::FilePath(path)); |
168 else if (!strcmp(action, "remove")) | 65 else if (!strcmp(action, "remove")) |
169 device_removed_.Run(base::FilePath(path)); | 66 device_removed_.Run(base::FilePath(path)); |
170 } | 67 } |
171 | 68 |
172 virtual void OnFileCanWriteWithoutBlocking(int fd) OVERRIDE { NOTREACHED(); } | 69 DeviceUdev* udev_; // Not owned. |
173 | |
174 private: | |
175 bool StartMonitoring(const EvdevDeviceCallback& device_added, | |
176 const EvdevDeviceCallback& device_removed) { | |
177 udev_monitor_ = UdevCreateMonitor(udev_.get()); | |
178 if (!udev_monitor_) | |
179 return false; | |
180 | |
181 // Grab monitor socket. | |
182 int fd = udev_monitor_get_fd(udev_monitor_.get()); | |
183 if (fd < 0) | |
184 return false; | |
185 | |
186 // Save callbacks. | |
187 device_added_ = device_added; | |
188 device_removed_ = device_removed; | |
189 | |
190 // Watch for incoming events on monitor socket. | |
191 return base::MessageLoopForUI::current()->WatchFileDescriptor( | |
192 fd, true, base::MessagePumpLibevent::WATCH_READ, &controller_, this); | |
193 } | |
194 | |
195 // Udev daemon connection. | |
196 scoped_udev udev_; | |
197 | |
198 // Udev device change monitor. | |
199 scoped_udev_monitor udev_monitor_; | |
200 | 70 |
201 // Callbacks for device changes. | 71 // Callbacks for device changes. |
202 EvdevDeviceCallback device_added_; | 72 EvdevDeviceCallback device_added_; |
203 EvdevDeviceCallback device_removed_; | 73 EvdevDeviceCallback device_removed_; |
204 | 74 |
205 // Watcher for uevent netlink socket. | |
206 base::MessagePumpLibevent::FileDescriptorWatcher controller_; | |
207 | |
208 DISALLOW_COPY_AND_ASSIGN(DeviceManagerUdev); | 75 DISALLOW_COPY_AND_ASSIGN(DeviceManagerUdev); |
209 }; | 76 }; |
210 | 77 |
211 } // namespace | 78 } // namespace |
212 | 79 |
213 scoped_ptr<DeviceManagerEvdev> CreateDeviceManagerUdev() { | 80 scoped_ptr<DeviceManagerEvdev> CreateDeviceManagerUdev(DeviceUdev* udev) { |
214 return make_scoped_ptr<DeviceManagerEvdev>(new DeviceManagerUdev()); | 81 return make_scoped_ptr<DeviceManagerEvdev>(new DeviceManagerUdev(udev)); |
215 } | 82 } |
216 | 83 |
217 } // namespace ui | 84 } // namespace ui |
OLD | NEW |