Mir
wrapper.h
Go to the documentation of this file.
1 /*
2  * Copyright © 2013 Canonical Ltd.
3  *
4  * This program is free software: you can redistribute it and/or modify it
5  * under the terms of the GNU Lesser General Public License version 3,
6  * as published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU Lesser General Public License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public License
14  * along with this program. If not, see <http://www.gnu.org/licenses/>.
15  *
16  * Authored by: Christopher James Halse Rogers <christopher.halse.rogers@canonical.com>
17  */
18 
19 #ifndef MIR_UDEV_WRAPPER_H_
20 #define MIR_UDEV_WRAPPER_H_
21 
22 #include <memory>
23 #include <libudev.h>
24 
25 namespace mir
26 {
27 namespace udev
28 {
29 
30 class Device;
31 class Enumerator;
32 
33 class Context
34 {
35 public:
36  Context();
37  ~Context() noexcept;
38 
39  Context(Context const&) = delete;
40  Context& operator=(Context const&) = delete;
41 
42  std::shared_ptr<Device> device_from_syspath(std::string const& syspath);
43 
44  ::udev* ctx() const;
45 
46 private:
47  ::udev* const context;
48 };
49 
50 class Device
51 {
52 public:
53  virtual ~Device() = default;
54 
55  Device(Device const&) = delete;
56  Device& operator=(Device const&) = delete;
57 
58  virtual char const* subsystem() const = 0;
59  virtual char const* devtype() const = 0;
60  virtual char const* devpath() const = 0;
61  virtual char const* devnode() const = 0;
62  virtual char const* property(char const *name) const = 0;
63 protected:
64  Device() = default;
65 };
66 
67 bool operator==(Device const& lhs, Device const& rhs);
68 bool operator!=(Device const& lhs, Device const& rhs);
69 
71 {
72 public:
73  Enumerator(std::shared_ptr<Context> const& ctx);
74  ~Enumerator() noexcept;
75 
76  Enumerator(Enumerator const&) = delete;
77  Enumerator& operator=(Enumerator const&) = delete;
78 
79  void scan_devices();
80 
81  void match_subsystem(std::string const& subsystem);
82  void match_parent(Device const& parent);
83  void match_sysname(std::string const& sysname);
84 
85  class iterator :
86  public std::iterator<std::input_iterator_tag, Device>
87  {
88  public:
89  iterator& operator++();
90  iterator operator++(int);
91 
92  bool operator==(iterator const& rhs) const;
93  bool operator!=(iterator const& rhs) const;
94 
95  Device const& operator*() const;
96  Device const* operator->() const;
97  private:
98  friend class Enumerator;
99 
100  iterator ();
101  iterator (std::shared_ptr<Context> const& ctx, udev_list_entry* entry);
102 
103  void increment();
104 
105  std::shared_ptr<Context> ctx;
106  udev_list_entry* entry;
107 
108  std::shared_ptr<Device> current;
109  };
110 
111  iterator begin();
112  iterator end();
113 
114 private:
115  std::shared_ptr<Context> const ctx;
116  udev_enumerate* const enumerator;
117  bool scanned;
118 };
119 
120 class Monitor
121 {
122 public:
123  enum EventType {
127  };
128 
129  Monitor(const Context& ctx);
130  ~Monitor() noexcept;
131 
132  Monitor(Monitor const&) = delete;
133  Monitor& operator=(Monitor const&) = delete;
134 
135  void enable(void);
136  int fd(void) const;
137 
138  void filter_by_subsystem(std::string const& subsystem);
139  void filter_by_subsystem_and_type(std::string const& subsystem, std::string const& devtype);
140 
141  void process_events(std::function<void(EventType, Device const&)> const& handler) const;
142 
143 private:
144  udev_monitor* const monitor;
145  bool enabled;
146 };
147 
148 }
149 }
150 #endif // MIR_UDEV_WRAPPER_H_
All things Mir.
Definition: atomic_callback.h:25
pixel_iterator end(MirGraphicsRegion const &region)
Definition: multi_stream.cpp:156
Definition: wrapper.h:120
Definition: wrapper.h:70
STL namespace.
EventType
Definition: wrapper.h:123
Definition: wrapper.h:124
pixel_iterator begin(MirGraphicsRegion const &region)
Definition: multi_stream.cpp:151
Definition: wrapper.h:50
Definition: wrapper.h:125
bool operator==(Device const &lhs, Device const &rhs)
Definition: udev_wrapper.cpp:88
::udev * ctx() const
Definition: udev_wrapper.cpp:241
Context()
Definition: udev_wrapper.cpp:224
std::shared_ptr< Device > device_from_syspath(std::string const &syspath)
Definition: udev_wrapper.cpp:236
Context & operator=(Context const &)=delete
Definition: wrapper.h:126
constexpr Width operator*(Scalar scale, Width const &w)
Definition: dimensions.h:148
~Context() noexcept
Definition: udev_wrapper.cpp:231
bool operator!=(Device const &lhs, Device const &rhs)
Definition: udev_wrapper.cpp:94
Definition: wrapper.h:85
Definition: wrapper.h:33

Copyright © 2012-2015 Canonical Ltd.
Generated on Thu Sep 8 14:50:19 UTC 2016