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

Unified Diff: ui/events/ozone/device_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, 8 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 side-by-side diff with in-line comments
Download patch
Index: ui/events/ozone/device_udev.cc
diff --git a/ui/events/ozone/device_udev.cc b/ui/events/ozone/device_udev.cc
new file mode 100644
index 0000000000000000000000000000000000000000..ea88244c53c19f77722a2a367852ae5efe59b1a7
--- /dev/null
+++ b/ui/events/ozone/device_udev.cc
@@ -0,0 +1,168 @@
+// Copyright 2014 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "ui/events/ozone/device_udev.h"
+
+#include <libudev.h>
+
+#include "base/debug/trace_event.h"
+#include "base/message_loop/message_pump_libevent.h"
+#include "base/stl_util.h"
+#include "base/strings/stringprintf.h"
+
+namespace ui {
+
+namespace {
+
+// Severity levels from syslog.h. We can't include it directly as it
+// conflicts with base/logging.h
+enum {
+ SYS_LOG_EMERG = 0,
+ SYS_LOG_ALERT = 1,
+ SYS_LOG_CRIT = 2,
+ SYS_LOG_ERR = 3,
+ SYS_LOG_WARNING = 4,
+ SYS_LOG_NOTICE = 5,
+ SYS_LOG_INFO = 6,
+ SYS_LOG_DEBUG = 7,
+};
+
+// Log handler for messages generated from libudev.
+void UdevLog(struct udev* udev,
+ int priority,
+ const char* file,
+ int line,
+ const char* fn,
+ const char* format,
+ va_list args) {
+ if (priority <= SYS_LOG_ERR)
+ LOG(ERROR) << "libudev: " << fn << ": " << base::StringPrintV(format, args);
+ else if (priority <= SYS_LOG_INFO)
+ VLOG(1) << "libudev: " << fn << ": " << base::StringPrintV(format, args);
+ else // SYS_LOG_DEBUG
+ VLOG(2) << "libudev: " << fn << ": " << base::StringPrintV(format, args);
+}
+
+// Create libudev context.
+scoped_udev UdevCreate() {
+ struct udev* udev = udev_new();
+ if (udev) {
+ udev_set_log_fn(udev, UdevLog);
+ udev_set_log_priority(udev, SYS_LOG_DEBUG);
+ }
+ return scoped_udev(udev);
+}
+
+// Start monitoring input device changes.
+scoped_udev_monitor UdevCreateMonitor(struct udev* udev,
+ const std::string& subsystem) {
+ struct udev_monitor* monitor = udev_monitor_new_from_netlink(udev, "udev");
+ if (monitor) {
+ udev_monitor_filter_add_match_subsystem_devtype(
+ monitor, subsystem.c_str(), NULL);
+
+ if (udev_monitor_enable_receiving(monitor))
+ LOG(ERROR) << "failed to start receiving events from udev";
+ }
+
+ return scoped_udev_monitor(monitor);
+}
+
+} // namespace
+
+class DeviceUdev::WatcherUdev : public base::MessagePumpLibevent::Watcher {
+ public:
+ WatcherUdev(MonitorUdev* monitor, scoped_udev_monitor udev_monitor);
+ virtual ~WatcherUdev();
+
+ virtual void OnFileCanReadWithoutBlocking(int fd) OVERRIDE;
+ virtual void OnFileCanWriteWithoutBlocking(int fd) OVERRIDE;
+ private:
+ base::MessagePumpLibevent::FileDescriptorWatcher controller_;
+
+ MonitorUdev* monitor_; // Not owned.
+ scoped_udev_monitor udev_monitor_;
+
+ DISALLOW_COPY_AND_ASSIGN(WatcherUdev);
+};
+
+DeviceUdev::WatcherUdev::WatcherUdev(MonitorUdev* monitor,
+ scoped_udev_monitor udev_monitor)
+ : monitor_(monitor),
+ udev_monitor_(udev_monitor.Pass()) {
+ int fd = udev_monitor_get_fd(udev_monitor_.get());
+ CHECK_GT(fd, 0);
+ base::MessageLoopForUI::current()->WatchFileDescriptor(
+ fd, true, base::MessagePumpLibevent::WATCH_READ, &controller_, this);
+}
+
+DeviceUdev::WatcherUdev::~WatcherUdev() {}
+
+void DeviceUdev::WatcherUdev::OnFileCanReadWithoutBlocking(int fd) {
+ // The netlink socket should never become disconnected. There's no need
+ // to handle broken connections here.
+ TRACE_EVENT1("ozone", "UdevDeviceChange", "socket", fd);
+
+ scoped_udev_device device(udev_monitor_receive_device(udev_monitor_.get()));
+ if (!device)
+ return;
+
+ monitor_->HandleUdevEvent(device.Pass());
+}
+
+void DeviceUdev::WatcherUdev::OnFileCanWriteWithoutBlocking(int fd) {
+ NOTREACHED();
+}
+
+DeviceUdev::DeviceUdev() : udev_(UdevCreate()) {}
+
+DeviceUdev::~DeviceUdev() {
+ STLDeleteContainerPairSecondPointers(monitors_.begin(), monitors_.end());
+}
+
+bool DeviceUdev::RegisterDeviceMonitor(MonitorUdev* monitor,
+ const std::string& subsystem) {
+ scoped_udev_monitor udev_monitor = UdevCreateMonitor(udev_.get(), subsystem);
+ if (!udev_monitor)
+ return false;
+
+ monitors_[monitor] = new WatcherUdev(monitor, udev_monitor.Pass());
+ return true;
+}
+
+void DeviceUdev::UnregisterDeviceMonitor(MonitorUdev* monitor) {
+ std::map<MonitorUdev*, WatcherUdev*>::iterator it = monitors_.find(monitor);
+ if (it == monitors_.end())
+ return;
+
+ delete it->second;
+ monitors_.erase(it);
+}
+
+bool DeviceUdev::ScanDevices(MonitorUdev* monitor,
+ const std::string& subsystem) {
+ scoped_udev_enumerate enumerate(udev_enumerate_new(udev_.get()));
+ if (!enumerate)
+ return false;;
+
+ udev_enumerate_add_match_subsystem(enumerate.get(), subsystem.c_str());
+ udev_enumerate_scan_devices(enumerate.get());
+
+ struct udev_list_entry* devices =
+ udev_enumerate_get_list_entry(enumerate.get());
+ struct udev_list_entry* entry;
+
+ udev_list_entry_foreach(entry, devices) {
+ scoped_udev_device device(udev_device_new_from_syspath(
+ udev_.get(), udev_list_entry_get_name(entry)));
+ if (!device)
+ continue;
+
+ monitor->HandleUdevEvent(device.Pass());
+ }
+
+ return true;
+}
+
+} // namespace ui

Powered by Google App Engine
This is Rietveld 408576698