Chromium Code Reviews| OLD | NEW |
|---|---|
| (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/files/file_path.h" | |
| 10 #include "base/memory/scoped_ptr.h" | |
| 11 #include "base/message_loop/message_pump_ozone.h" | |
| 12 #include "base/strings/stringprintf.h" | |
| 13 #include "ui/events/ozone/evdev/event_factory.h" | |
| 14 #include "ui/events/ozone/evdev/scoped_udev.h" | |
| 15 | |
| 16 namespace ui { | |
| 17 | |
| 18 namespace { | |
| 19 | |
| 20 const char kSubsystemInput[] = "input"; | |
| 21 | |
| 22 // Severity levels from syslog.h. We can't include it directly as it | |
| 23 // conflicts with base/logging.h | |
| 24 enum { | |
| 25 SYS_LOG_EMERG = 0, | |
| 26 SYS_LOG_ALERT = 1, | |
| 27 SYS_LOG_CRIT = 2, | |
| 28 SYS_LOG_ERR = 3, | |
| 29 SYS_LOG_WARNING = 4, | |
| 30 SYS_LOG_NOTICE = 5, | |
| 31 SYS_LOG_INFO = 6, | |
| 32 SYS_LOG_DEBUG = 7, | |
| 33 }; | |
| 34 | |
| 35 // Log handler for messages generated from libudev. | |
| 36 void UdevLog(struct udev* udev, | |
| 37 int priority, | |
| 38 const char* file, | |
| 39 int line, | |
| 40 const char* fn, | |
| 41 const char* format, | |
| 42 va_list args) { | |
| 43 std::string message = base::StringPrintf("libudev: %s: ", fn); | |
| 44 base::StringAppendV(&message, format, args); | |
| 45 if (priority <= SYS_LOG_ERR) | |
| 46 LOG(ERROR) << message; | |
| 47 else if (priority <= SYS_LOG_INFO) | |
| 48 VLOG(1) << message; | |
| 49 else // SYS_LOG_DEBUG | |
|
rjkroege
2014/02/03 15:36:55
nit: i think that it might be more chrome-y to ret
spang
2014/02/03 16:43:32
If you have a less vague consistency argument I wi
| |
| 50 VLOG(2) << message; | |
| 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) { | |
|
rjkroege
2014/02/03 15:36:55
this is a macro?
spang
2014/02/03 16:43:32
Yes.
| |
| 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 // Found input device node; attach. | |
| 105 device_callback.Run(base::FilePath(path)); | |
| 106 } | |
| 107 | |
| 108 return true; | |
| 109 } | |
| 110 | |
| 111 // Device enumerator & monitor using udev. | |
| 112 // | |
| 113 // This class enumerates input devices attached to the system using udev. | |
| 114 // | |
| 115 // It also creates & monitors a udev netlink socket and issues callbacks for | |
|
rjkroege
2014/02/03 15:36:55
what thread does this class run on? hopefully not
spang
2014/02/03 16:43:32
UI thread. Would like to land the single-threaded
| |
| 116 // any changes to the set of attached devices. | |
| 117 class DeviceManagerUdev : public DeviceManagerEvdev, | |
|
rjkroege
2014/02/03 15:36:55
this class looks sufficiently rich as to require u
spang
2014/02/03 16:43:32
I will add a TODO for now and figure out how best
| |
| 118 base::MessagePumpLibevent::Watcher { | |
| 119 public: | |
| 120 DeviceManagerUdev() {} | |
| 121 virtual ~DeviceManagerUdev() {} | |
| 122 | |
| 123 // Enumerate existing devices & start watching for device changes. | |
| 124 virtual void ScanAndStartMonitoring(const EvdevDeviceCallback& device_added, | |
| 125 const EvdevDeviceCallback& device_removed) | |
| 126 OVERRIDE { | |
| 127 udev_ = UdevCreate(); | |
| 128 if (!udev_) { | |
| 129 LOG(ERROR) << "failed to initialize libudev"; | |
| 130 return; | |
| 131 } | |
| 132 | |
| 133 if (!StartMonitoring(device_added, device_removed)) | |
| 134 LOG(ERROR) << "failed to start monitoring device changes via udev"; | |
| 135 | |
| 136 if (!UdevEnumerateInputDevices(udev_.get(), device_added)) | |
| 137 LOG(ERROR) << "failed to enumerate input devices via udev"; | |
| 138 } | |
| 139 | |
| 140 virtual void OnFileCanReadWithoutBlocking(int fd) OVERRIDE { | |
| 141 // The netlink socket should never become disconnected. There's no need | |
| 142 // to handle broken connections here. | |
| 143 | |
| 144 scoped_udev_device device(udev_monitor_receive_device(udev_monitor_.get())); | |
| 145 if (!device) | |
| 146 return; | |
| 147 | |
| 148 const char* path = udev_device_get_devnode(device.get()); | |
| 149 const char* action = udev_device_get_action(device.get()); | |
| 150 if (!path || !action) | |
| 151 return; | |
| 152 | |
| 153 if (!strcmp(action, "add") || !strcmp(action, "change")) | |
| 154 device_added_.Run(base::FilePath(path)); | |
| 155 else if (!strcmp(action, "remove")) | |
| 156 device_removed_.Run(base::FilePath(path)); | |
| 157 } | |
| 158 | |
| 159 virtual void OnFileCanWriteWithoutBlocking(int fd) OVERRIDE { NOTREACHED(); } | |
| 160 | |
| 161 private: | |
| 162 bool StartMonitoring(const EvdevDeviceCallback& device_added, | |
| 163 const EvdevDeviceCallback& device_removed) { | |
| 164 udev_monitor_ = UdevCreateMonitor(udev_.get()); | |
| 165 if (!udev_monitor_) | |
| 166 return false; | |
| 167 | |
| 168 // Grab monitor socket. | |
| 169 int fd = udev_monitor_get_fd(udev_monitor_.get()); | |
| 170 if (fd < 0) | |
| 171 return false; | |
| 172 | |
| 173 // Save callbacks. | |
| 174 device_added_ = device_added; | |
| 175 device_removed_ = device_removed; | |
| 176 | |
| 177 // Watch for incoming events on monitor socket. | |
| 178 return base::MessagePumpOzone::Current()->WatchFileDescriptor( | |
|
rjkroege
2014/02/03 15:36:55
this is on the UI thread. I think putting it on an
spang
2014/02/03 16:43:32
TODO.
| |
| 179 fd, true, base::MessagePumpLibevent::WATCH_READ, &controller_, this); | |
| 180 } | |
| 181 | |
| 182 // Udev daemon connection. | |
| 183 scoped_udev udev_; | |
| 184 | |
| 185 // Udev device change monitor. | |
| 186 scoped_udev_monitor udev_monitor_; | |
| 187 | |
| 188 // Callbacks for device changes. | |
| 189 EvdevDeviceCallback device_added_; | |
| 190 EvdevDeviceCallback device_removed_; | |
| 191 | |
| 192 // Watcher for uevent netlink socket. | |
| 193 base::MessagePumpLibevent::FileDescriptorWatcher controller_; | |
| 194 | |
| 195 DISALLOW_COPY_AND_ASSIGN(DeviceManagerUdev); | |
| 196 }; | |
| 197 | |
| 198 } // namespace | |
| 199 | |
| 200 scoped_ptr<DeviceManagerEvdev> CreateDeviceManagerUdev() { | |
| 201 return scoped_ptr<DeviceManagerEvdev>(new DeviceManagerUdev); | |
| 202 } | |
| 203 | |
| 204 } // namespace ui | |
| OLD | NEW |