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

Side by Side Diff: ui/events/ozone/device/udev/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/device/udev/device_manager_udev.h"
6
7 #include <libudev.h>
8
9 #include "base/debug/trace_event.h"
10 #include "base/strings/stringprintf.h"
11 #include "ui/events/ozone/device/device_event.h"
12 #include "ui/events/ozone/device/device_event_observer.h"
13
14 namespace ui {
15
16 namespace {
17
18 const char* kSubsystems[] = {
19 "input",
20 "drm",
21 };
22
23 // Severity levels from syslog.h. We can't include it directly as it
24 // conflicts with base/logging.h
25 enum {
26 SYS_LOG_EMERG = 0,
27 SYS_LOG_ALERT = 1,
28 SYS_LOG_CRIT = 2,
29 SYS_LOG_ERR = 3,
30 SYS_LOG_WARNING = 4,
31 SYS_LOG_NOTICE = 5,
32 SYS_LOG_INFO = 6,
33 SYS_LOG_DEBUG = 7,
34 };
35
36 // Log handler for messages generated from libudev.
37 void UdevLog(struct udev* udev,
38 int priority,
39 const char* file,
40 int line,
41 const char* fn,
42 const char* format,
43 va_list args) {
44 if (priority <= SYS_LOG_ERR)
45 LOG(ERROR) << "libudev: " << fn << ": " << base::StringPrintV(format, args);
46 else if (priority <= SYS_LOG_INFO)
47 VLOG(1) << "libudev: " << fn << ": " << base::StringPrintV(format, args);
48 else // SYS_LOG_DEBUG
49 VLOG(2) << "libudev: " << fn << ": " << base::StringPrintV(format, args);
50 }
51
52 // Create libudev context.
53 scoped_udev UdevCreate() {
54 struct udev* udev = udev_new();
55 if (udev) {
56 udev_set_log_fn(udev, UdevLog);
57 udev_set_log_priority(udev, SYS_LOG_DEBUG);
58 }
59 return scoped_udev(udev);
60 }
61
62 // Start monitoring input device changes.
63 scoped_udev_monitor UdevCreateMonitor(struct udev* udev) {
64 struct udev_monitor* monitor = udev_monitor_new_from_netlink(udev, "udev");
65 if (monitor) {
66 for (size_t i = 0; i < arraysize(kSubsystems); ++i)
67 udev_monitor_filter_add_match_subsystem_devtype(
68 monitor, kSubsystems[i], NULL);
69
70 if (udev_monitor_enable_receiving(monitor))
71 LOG(ERROR) << "Failed to start receiving events from udev";
72 } else {
73 LOG(ERROR) << "Failed to create udev monitor";
74 }
75
76 return scoped_udev_monitor(monitor);
77 }
78
79 } // namespace
80
81 DeviceManagerUdev::DeviceManagerUdev()
82 : udev_(UdevCreate()),
83 monitor_(UdevCreateMonitor(udev_.get())) {
84
85 if (monitor_) {
86 int fd = udev_monitor_get_fd(monitor_.get());
87 CHECK_GT(fd, 0);
88 base::MessageLoopForUI::current()->WatchFileDescriptor(
89 fd, true, base::MessagePumpLibevent::WATCH_READ, &controller_, this);
90 }
91 }
92
93 DeviceManagerUdev::~DeviceManagerUdev() {}
94
95 void DeviceManagerUdev::ScanDevices(DeviceEventObserver* observer) {
96 scoped_udev_enumerate enumerate(udev_enumerate_new(udev_.get()));
97 if (!enumerate)
98 return;
99
100 for (size_t i = 0; i < arraysize(kSubsystems); ++i)
101 udev_enumerate_add_match_subsystem(enumerate.get(), kSubsystems[i]);
102 udev_enumerate_scan_devices(enumerate.get());
103
104 struct udev_list_entry* devices =
105 udev_enumerate_get_list_entry(enumerate.get());
106 struct udev_list_entry* entry;
107
108 udev_list_entry_foreach(entry, devices) {
109 scoped_udev_device device(udev_device_new_from_syspath(
110 udev_.get(), udev_list_entry_get_name(entry)));
111 if (!device)
112 continue;
113
114 scoped_ptr<DeviceEvent> event = ProcessMessage(device.get());
115 if (event)
116 observer->OnDeviceEvent(*event.get());
117 }
118 }
119
120 void DeviceManagerUdev::AddObserver(DeviceEventObserver* observer) {
121 observers_.AddObserver(observer);
122 }
123
124 void DeviceManagerUdev::RemoveObserver(DeviceEventObserver* observer) {
125 observers_.RemoveObserver(observer);
126 }
127
128 void DeviceManagerUdev::OnFileCanReadWithoutBlocking(int fd) {
129 // The netlink socket should never become disconnected. There's no need
130 // to handle broken connections here.
131 TRACE_EVENT1("ozone", "UdevDeviceChange", "socket", fd);
132
133 scoped_udev_device device(udev_monitor_receive_device(monitor_.get()));
134 if (!device)
135 return;
136
137 scoped_ptr<DeviceEvent> event = ProcessMessage(device.get());
138 if (event)
139 FOR_EACH_OBSERVER(
140 DeviceEventObserver, observers_, OnDeviceEvent(*event.get()));
141 }
142
143 void DeviceManagerUdev::OnFileCanWriteWithoutBlocking(int fd) {
144 NOTREACHED();
145 }
146
147 scoped_ptr<DeviceEvent> DeviceManagerUdev::ProcessMessage(udev_device* device) {
148 const char* path = udev_device_get_devnode(device);
149 const char* action = udev_device_get_action(device);
150 const char* hotplug = udev_device_get_property_value(device, "HOTPLUG");
151 const char* subsystem = udev_device_get_property_value(device, "SUBSYSTEM");
152
153 if (!path || !subsystem)
154 return scoped_ptr<DeviceEvent>();
155
156 DeviceEvent::DeviceType device_type;
157 if (!strcmp(subsystem, "input") &&
158 StartsWithASCII(path, "/dev/input/event", true))
159 device_type = DeviceEvent::INPUT;
160 else if (!strcmp(subsystem, "drm") && hotplug && !strcmp(hotplug, "1"))
161 device_type = DeviceEvent::DISPLAY;
162 else
163 return scoped_ptr<DeviceEvent>();
164
165 DeviceEvent::ActionType action_type;
166 if (!action || !strcmp(action, "add"))
167 action_type = DeviceEvent::ADD;
168 else if (!strcmp(action, "remove"))
169 action_type = DeviceEvent::REMOVE;
170 else if (!strcmp(action, "change"))
171 action_type = DeviceEvent::CHANGE;
172 else
173 return scoped_ptr<DeviceEvent>();
174
175 return scoped_ptr<DeviceEvent>(
176 new DeviceEvent(device_type, action_type, base::FilePath(path)));
177 }
178
179 } // namespace ui
OLDNEW
« no previous file with comments | « ui/events/ozone/device/udev/device_manager_udev.h ('k') | ui/events/ozone/device/udev/scoped_udev.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698