Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(13)

Side by Side Diff: ui/events/ozone/evdev/device_manager_udev.cc

Issue 250793005: Refactor Udev device support in Ozone and add a DRM hotplug monitor (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: . Created 6 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
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.
4
5 #include "ui/events/ozone/evdev/device_manager_udev.h"
6
7 #include <libudev.h>
8
9 #include "base/debug/trace_event.h"
10 #include "base/files/file_path.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/strings/string_util.h"
13 #include "base/strings/stringprintf.h"
14 #include "ui/events/ozone/evdev/device_manager_evdev.h"
15 #include "ui/events/ozone/evdev/event_factory_evdev.h"
16 #include "ui/events/ozone/evdev/scoped_udev.h"
17
18 namespace ui {
19
20 namespace {
21
22 const char kSubsystemInput[] = "input";
23
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.
123 class DeviceManagerUdev : public DeviceManagerEvdev,
124 base::MessagePumpLibevent::Watcher {
125 public:
126 DeviceManagerUdev() {}
127 virtual ~DeviceManagerUdev() { Stop(); }
128
129 // Enumerate existing devices & start watching for device changes.
130 virtual void ScanAndStartMonitoring(const EvdevDeviceCallback& device_added,
131 const EvdevDeviceCallback& device_removed)
132 OVERRIDE {
133 udev_ = UdevCreate();
134 if (!udev_) {
135 LOG(ERROR) << "failed to initialize libudev";
136 return;
137 }
138
139 if (!StartMonitoring(device_added, device_removed))
140 LOG(ERROR) << "failed to start monitoring device changes via udev";
141
142 if (!UdevEnumerateInputDevices(udev_.get(), device_added))
143 LOG(ERROR) << "failed to enumerate input devices via udev";
144 }
145
146 virtual void Stop() OVERRIDE {
147 controller_.StopWatchingFileDescriptor();
148 device_added_.Reset();
149 device_removed_.Reset();
150 }
151
152 virtual void OnFileCanReadWithoutBlocking(int fd) OVERRIDE {
153 // The netlink socket should never become disconnected. There's no need
154 // to handle broken connections here.
155 TRACE_EVENT1("ozone", "UdevDeviceChange", "socket", fd);
156
157 scoped_udev_device device(udev_monitor_receive_device(udev_monitor_.get()));
158 if (!device)
159 return;
160
161 const char* path = udev_device_get_devnode(device.get());
162 const char* action = udev_device_get_action(device.get());
163 if (!path || !action)
164 return;
165
166 if (!strcmp(action, "add") || !strcmp(action, "change"))
167 device_added_.Run(base::FilePath(path));
168 else if (!strcmp(action, "remove"))
169 device_removed_.Run(base::FilePath(path));
170 }
171
172 virtual void OnFileCanWriteWithoutBlocking(int fd) OVERRIDE { NOTREACHED(); }
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
201 // Callbacks for device changes.
202 EvdevDeviceCallback device_added_;
203 EvdevDeviceCallback device_removed_;
204
205 // Watcher for uevent netlink socket.
206 base::MessagePumpLibevent::FileDescriptorWatcher controller_;
207
208 DISALLOW_COPY_AND_ASSIGN(DeviceManagerUdev);
209 };
210
211 } // namespace
212
213 scoped_ptr<DeviceManagerEvdev> CreateDeviceManagerUdev() {
214 return make_scoped_ptr<DeviceManagerEvdev>(new DeviceManagerUdev());
215 }
216
217 } // namespace ui
OLDNEW
« no previous file with comments | « ui/events/ozone/evdev/device_manager_udev.h ('k') | ui/events/ozone/evdev/event_factory_evdev.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698