blob: ae730cea7ef4f0404fc65076d77a6759df1fb130 [file] [log] [blame]
[email protected]26958d12014-02-27 17:29:101// Copyright 2014 The Chromium Authors. All rights reserved.
[email protected]125724f2014-01-31 20:13:232// 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]26958d12014-02-27 17:29:1010#include <map>
11#include <string>
12
[email protected]125724f2014-01-31 20:13:2313#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
19namespace device {
20
21class HidConnection;
22
23template<typename T, void func(T*)>
[email protected]d54d55c12014-02-03 07:02:2924void DiscardReturnType(T* arg) { func(arg); }
25template<typename T, T* func(T*)>
26void DiscardReturnType(T* arg) { func(arg); }
27
28template<typename T, void func(T*)>
[email protected]125724f2014-01-31 20:13:2329struct Deleter {
30 void operator()(T* enumerate) const {
31 if (enumerate != NULL)
32 func(enumerate);
33 }
34};
35
[email protected]d54d55c12014-02-03 07:02:2936typedef Deleter<
37 udev_enumerate,
38 DiscardReturnType<udev_enumerate, udev_enumerate_unref> >
39 UdevEnumerateDeleter;
40typedef Deleter<
41 udev_device,
42 DiscardReturnType<udev_device, udev_device_unref> > UdevDeviceDeleter;
43typedef Deleter<udev, DiscardReturnType<udev, udev_unref> > UdevDeleter;
44typedef Deleter<
45 udev_monitor,
46 DiscardReturnType<udev_monitor, udev_monitor_unref> > UdevMonitorDeleter;
[email protected]125724f2014-01-31 20:13:2347
48typedef scoped_ptr<udev_device, UdevDeviceDeleter> ScopedUdevDevicePtr;
49typedef scoped_ptr<udev_enumerate, UdevEnumerateDeleter> ScopedUdevEnumeratePtr;
50
51class HidServiceLinux : public HidService,
52 public base::MessagePumpLibevent::Watcher {
53 public:
54 HidServiceLinux();
55
[email protected]26958d12014-02-27 17:29:1056 virtual scoped_refptr<HidConnection> Connect(const HidDeviceId& device_id)
57 OVERRIDE;
[email protected]125724f2014-01-31 20:13:2358
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]26958d12014-02-27 17:29:1067 void PlatformAddDevice(udev_device* device);
68 void PlatformRemoveDevice(udev_device* raw_dev);
[email protected]125724f2014-01-31 20:13:2369
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_