~ubuntu-branches/ubuntu/vivid/mir/vivid

« back to all changes in this revision

Viewing changes to include/shared/mir/udev/wrapper.h

  • Committer: Package Import Robot
  • Author(s): Ubuntu daily release
  • Date: 2014-08-11 19:52:06 UTC
  • mto: This revision was merged to the branch mainline in revision 77.
  • Revision ID: package-import@ubuntu.com-20140811195206-05ee991qbzvdbmel
Tags: upstream-0.6.0+14.10.20140811
Import upstream version 0.6.0+14.10.20140811

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
protected:
 
63
    Device() = default;
 
64
};
 
65
 
 
66
bool operator==(Device const& lhs, Device const& rhs);
 
67
bool operator!=(Device const& lhs, Device const& rhs);
 
68
 
 
69
class Enumerator
 
70
{
 
71
public:
 
72
    Enumerator(std::shared_ptr<Context> const& ctx);
 
73
    ~Enumerator() noexcept;
 
74
 
 
75
    Enumerator(Enumerator const&) = delete;
 
76
    Enumerator& operator=(Enumerator const&) = delete;
 
77
 
 
78
    void scan_devices();
 
79
 
 
80
    void match_subsystem(std::string const& subsystem);
 
81
    void match_parent(Device const& parent);
 
82
    void match_sysname(std::string const& sysname);
 
83
 
 
84
    class iterator :
 
85
        public std::iterator<std::input_iterator_tag, Device>
 
86
    {
 
87
    public:
 
88
        iterator& operator++();
 
89
        iterator operator++(int);
 
90
 
 
91
        bool operator==(iterator const& rhs) const;
 
92
        bool operator!=(iterator const& rhs) const;
 
93
 
 
94
        Device const& operator*() const;
 
95
        Device const* operator->() const;
 
96
    private:
 
97
        friend class Enumerator;
 
98
 
 
99
        iterator ();
 
100
        iterator (std::shared_ptr<Context> const& ctx, udev_list_entry* entry);
 
101
 
 
102
        void increment();
 
103
 
 
104
        std::shared_ptr<Context> ctx;
 
105
        udev_list_entry* entry;
 
106
 
 
107
        std::shared_ptr<Device> current;
 
108
    };
 
109
 
 
110
    iterator begin();
 
111
    iterator end();
 
112
 
 
113
private:
 
114
    std::shared_ptr<Context> const ctx;
 
115
    udev_enumerate* const enumerator;
 
116
    bool scanned;
 
117
};
 
118
 
 
119
class Monitor
 
120
{
 
121
public:
 
122
    enum EventType {
 
123
        ADDED,
 
124
        REMOVED,
 
125
        CHANGED,
 
126
    };
 
127
 
 
128
    Monitor(const Context& ctx);
 
129
    ~Monitor() noexcept;
 
130
 
 
131
    Monitor(Monitor const&) = delete;
 
132
    Monitor& operator=(Monitor const&) = delete;
 
133
 
 
134
    void enable(void);
 
135
    int fd(void) const;
 
136
 
 
137
    void filter_by_subsystem(std::string const& subsystem);
 
138
    void filter_by_subsystem_and_type(std::string const& subsystem, std::string const& devtype);
 
139
 
 
140
    void process_events(std::function<void(EventType, Device const&)> const& handler) const;
 
141
 
 
142
private:
 
143
    udev_monitor* const monitor;
 
144
    bool enabled;
 
145
};
 
146
 
 
147
}
 
148
}
 
149
#endif // MIR_UDEV_WRAPPER_H_