[email protected] | 26958d1 | 2014-02-27 17:29:10 | [diff] [blame^] | 1 | // Copyright 2014 The Chromium Authors. All rights reserved. |
[email protected] | 125724f | 2014-01-31 20:13:23 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef DEVICE_HID_HID_SERVICE_LINUX_H_ |
| 6 | #define DEVICE_HID_HID_SERVICE_LINUX_H_ |
| 7 | |
| 8 | #include <libudev.h> |
| 9 | |
[email protected] | 26958d1 | 2014-02-27 17:29:10 | [diff] [blame^] | 10 | #include <map> |
| 11 | #include <string> |
| 12 | |
[email protected] | 125724f | 2014-01-31 20:13:23 | [diff] [blame] | 13 | #include "base/macros.h" |
| 14 | #include "base/memory/scoped_ptr.h" |
| 15 | #include "base/message_loop/message_pump_libevent.h" |
| 16 | #include "device/hid/hid_device_info.h" |
| 17 | #include "device/hid/hid_service.h" |
| 18 | |
| 19 | namespace device { |
| 20 | |
| 21 | class HidConnection; |
| 22 | |
| 23 | template<typename T, void func(T*)> |
[email protected] | d54d55c1 | 2014-02-03 07:02:29 | [diff] [blame] | 24 | void DiscardReturnType(T* arg) { func(arg); } |
| 25 | template<typename T, T* func(T*)> |
| 26 | void DiscardReturnType(T* arg) { func(arg); } |
| 27 | |
| 28 | template<typename T, void func(T*)> |
[email protected] | 125724f | 2014-01-31 20:13:23 | [diff] [blame] | 29 | struct Deleter { |
| 30 | void operator()(T* enumerate) const { |
| 31 | if (enumerate != NULL) |
| 32 | func(enumerate); |
| 33 | } |
| 34 | }; |
| 35 | |
[email protected] | d54d55c1 | 2014-02-03 07:02:29 | [diff] [blame] | 36 | typedef Deleter< |
| 37 | udev_enumerate, |
| 38 | DiscardReturnType<udev_enumerate, udev_enumerate_unref> > |
| 39 | UdevEnumerateDeleter; |
| 40 | typedef Deleter< |
| 41 | udev_device, |
| 42 | DiscardReturnType<udev_device, udev_device_unref> > UdevDeviceDeleter; |
| 43 | typedef Deleter<udev, DiscardReturnType<udev, udev_unref> > UdevDeleter; |
| 44 | typedef Deleter< |
| 45 | udev_monitor, |
| 46 | DiscardReturnType<udev_monitor, udev_monitor_unref> > UdevMonitorDeleter; |
[email protected] | 125724f | 2014-01-31 20:13:23 | [diff] [blame] | 47 | |
| 48 | typedef scoped_ptr<udev_device, UdevDeviceDeleter> ScopedUdevDevicePtr; |
| 49 | typedef scoped_ptr<udev_enumerate, UdevEnumerateDeleter> ScopedUdevEnumeratePtr; |
| 50 | |
| 51 | class HidServiceLinux : public HidService, |
| 52 | public base::MessagePumpLibevent::Watcher { |
| 53 | public: |
| 54 | HidServiceLinux(); |
| 55 | |
[email protected] | 26958d1 | 2014-02-27 17:29:10 | [diff] [blame^] | 56 | virtual scoped_refptr<HidConnection> Connect(const HidDeviceId& device_id) |
| 57 | OVERRIDE; |
[email protected] | 125724f | 2014-01-31 20:13:23 | [diff] [blame] | 58 | |
| 59 | // Implements base::MessagePumpLibevent::Watcher |
| 60 | virtual void OnFileCanReadWithoutBlocking(int fd) OVERRIDE; |
| 61 | virtual void OnFileCanWriteWithoutBlocking(int fd) OVERRIDE; |
| 62 | |
| 63 | private: |
| 64 | virtual ~HidServiceLinux(); |
| 65 | |
| 66 | void Enumerate(); |
[email protected] | 26958d1 | 2014-02-27 17:29:10 | [diff] [blame^] | 67 | void PlatformAddDevice(udev_device* device); |
| 68 | void PlatformRemoveDevice(udev_device* raw_dev); |
[email protected] | 125724f | 2014-01-31 20:13:23 | [diff] [blame] | 69 | |
| 70 | scoped_ptr<udev, UdevDeleter> udev_; |
| 71 | scoped_ptr<udev_monitor, UdevMonitorDeleter> monitor_; |
| 72 | int monitor_fd_; |
| 73 | base::MessagePumpLibevent::FileDescriptorWatcher monitor_watcher_; |
| 74 | |
| 75 | DISALLOW_COPY_AND_ASSIGN(HidServiceLinux); |
| 76 | }; |
| 77 | |
| 78 | } // namespace device |
| 79 | |
| 80 | #endif // DEVICE_HID_HID_SERVICE_LINUX_H_ |