moved callback thunks to impl, makes classes readable
[staging/windowmanager.git] / src / wayland.hpp
1 #ifndef WM_WAYLAND_HPP
2 #define WM_WAYLAND_HPP
3
4 #include "ivi-controller-client-protocol.h"
5
6 #include "util.h"
7
8 #include <functional>
9 #include <map>
10 #include <memory>
11
12 //                      _                 _
13 // __      ____ _ _   _| | __ _ _ __   __| |     _ __  _ __ _____  ___   _
14 // \ \ /\ / / _` | | | | |/ _` | '_ \ / _` |    | '_ \| '__/ _ \ \/ / | | |
15 //  \ V  V / (_| | |_| | | (_| | | | | (_| |    | |_) | | | (_) >  <| |_| |
16 //   \_/\_/ \__,_|\__, |_|\__,_|_| |_|\__,_|____| .__/|_|  \___/_/\_\\__, |
17 //                |___/                   |_____|_|                  |___/
18 template <typename ProxyT>
19 struct wayland_proxy {
20    ProxyT *proxy;
21
22    wayland_proxy(void *p) : proxy(static_cast<ProxyT *>(p)) {}
23
24    virtual ~wayland_proxy() {
25       // If this is the nullptr, then it means it already was destroyed by a
26       // custom wayland dtor
27       if (this->proxy) {
28          logdebug("%s %p @ %p", __func__, this, this->proxy);
29          wl_proxy_destroy(reinterpret_cast<wl_proxy *>(proxy));
30       }
31    }
32 };
33
34 //                                                                  _
35 //  _ __   __ _ _ __ ___   ___  ___ _ __   __ _  ___ ___  __      _| |
36 // | '_ \ / _` | '_ ` _ \ / _ \/ __| '_ \ / _` |/ __/ _ \ \ \ /\ / / |
37 // | | | | (_| | | | | | |  __/\__ \ |_) | (_| | (_|  __/  \ V  V /| |
38 // |_| |_|\__,_|_| |_| |_|\___||___/ .__/ \__,_|\___\___|   \_/\_/ |_|
39 //                                 |_|
40 namespace wl {
41 struct registry;
42
43 //      _ _           _
44 //   __| (_)___ _ __ | | __ _ _   _
45 //  / _` | / __| '_ \| |/ _` | | | |
46 // | (_| | \__ \ |_) | | (_| | |_| |
47 //  \__,_|_|___/ .__/|_|\__,_|\__, |
48 //             |_|            |___/
49 struct display {
50    std::unique_ptr<wl_display, void (*)(wl_display *)> d;
51    std::unique_ptr<registry> r;
52
53    display();
54    bool ok() const;
55    void roundtrip();
56    int dispatch();
57 };
58
59 //                 _     _
60 //  _ __ ___  __ _(_)___| |_ _ __ _   _
61 // | '__/ _ \/ _` | / __| __| '__| | | |
62 // | | |  __/ (_| | \__ \ |_| |  | |_| |
63 // |_|  \___|\__, |_|___/\__|_|   \__, |
64 //           |___/                |___/
65 struct registry : public wayland_proxy<wl_registry> {
66    typedef std::function<void(wl_registry *, uint32_t, uint32_t)> binder;
67    std::map<std::string, binder> bindings;
68
69    registry(wl_display *d);
70    ~registry();
71
72    void add_global_handler(char const *iface, binder bind);
73
74    // Events
75    void global(uint32_t name, char const *iface, uint32_t v);
76    void global_remove(uint32_t name);
77 };
78
79 //              _               _
80 //   ___  _   _| |_ _ __  _   _| |_
81 //  / _ \| | | | __| '_ \| | | | __|
82 // | (_) | |_| | |_| |_) | |_| | |_
83 //  \___/ \__,_|\__| .__/ \__,_|\__|
84 //                 |_|
85 struct output : wayland_proxy<wl_output> {
86    output(wl_registry *registry, uint32_t name, uint32_t version);
87
88    // Events
89    void geometry(int32_t x, int32_t y, int32_t pw, int32_t ph, int32_t subpel,
90                  char const *make, char const *model, int32_t tx);
91    void mode(uint32_t flags, int32_t w, int32_t h, int32_t r);
92    void done();
93    void scale(int32_t factor);
94 };
95 }
96
97 //  _ __   __ _ _ __ ___   ___  ___ _ __   __ _  ___ ___
98 // | '_ \ / _` | '_ ` _ \ / _ \/ __| '_ \ / _` |/ __/ _ \
99 // | | | | (_| | | | | | |  __/\__ \ |_) | (_| | (_|  __/
100 // |_| |_|\__,_|_| |_| |_|\___||___/ .__/ \__,_|\___\___|
101 //                                 |_|
102 //                   _       _
103 //   __ _  ___ _ __ (_)_   _(_)
104 //  / _` |/ _ \ '_ \| \ \ / / |
105 // | (_| |  __/ | | | |\ V /| |
106 //  \__, |\___|_| |_|_| \_/ |_|
107 //  |___/
108 namespace genivi {
109
110 //                  __
111 //  ___ _   _ _ __ / _| __ _  ___ ___
112 // / __| | | | '__| |_ / _` |/ __/ _ \
113 // \__ \ |_| | |  |  _| (_| | (_|  __/
114 // |___/\__,_|_|  |_|  \__,_|\___\___|
115 //
116 struct surface : public wayland_proxy<ivi_controller_surface> {
117    uint32_t id;
118
119    surface(uint32_t i, ivi_controller *c);
120    ~surface() override;
121
122    // Events
123    void visibility(int32_t visibility);
124    void opacity(wl_fixed_t opacity);
125    void source_rectangle(int32_t x, int32_t y, int32_t width, int32_t height);
126    void destination_rectangle(int32_t x, int32_t y, int32_t width,
127                               int32_t height);
128    void configuration(int32_t width, int32_t height);
129    void orientation(int32_t orientation);
130    void pixelformat(int32_t pixelformat);
131    void layer(struct ivi_controller_layer *layer);
132    void stats(uint32_t redraw_count, uint32_t frame_count,
133               uint32_t update_count, uint32_t pid, const char *process_name);
134    void destroyed();
135    void content(int32_t content_state);
136 };
137
138 //  _
139 // | | __ _ _   _  ___ _ __
140 // | |/ _` | | | |/ _ \ '__|
141 // | | (_| | |_| |  __/ |
142 // |_|\__,_|\__, |\___|_|
143 //          |___/
144 struct layer : public wayland_proxy<ivi_controller_layer> {
145    uint32_t id;
146
147    layer(uint32_t i, ivi_controller *c);
148    ~layer() override;
149
150    // Events
151    void visibility(int32_t visibility);
152    void opacity(wl_fixed_t opacity);
153    void source_rectangle(int32_t x, int32_t y, int32_t width, int32_t height);
154    void destination_rectangle(int32_t x, int32_t y, int32_t width,
155                               int32_t height);
156    void configuration(int32_t width, int32_t height);
157    void orientation(int32_t orientation);
158    void screen(struct wl_output *screen);
159    void destroyed();
160 };
161
162 //
163 // ___  ___ _ __ ___  ___ _ __
164 /// __|/ __| '__/ _ \/ _ \ '_ \
165 //\__ \ (__| | |  __/  __/ | | |
166 //|___/\___|_|  \___|\___|_| |_|
167 //
168 struct screen : public wayland_proxy<ivi_controller_screen> {
169    uint32_t id;
170
171    screen(uint32_t i, ivi_controller_screen *p);
172 };
173
174 //                  _             _ _
175 //   ___ ___  _ __ | |_ _ __ ___ | | | ___ _ __
176 //  / __/ _ \| '_ \| __| '__/ _ \| | |/ _ \ '__|
177 // | (_| (_) | | | | |_| | | (_) | | |  __/ |
178 //  \___\___/|_| |_|\__|_|  \___/|_|_|\___|_|
179 //
180 struct controller : public wayland_proxy<ivi_controller> {
181    std::map<uint32_t, std::unique_ptr<surface>> surfaces;
182    std::map<uint32_t, std::unique_ptr<layer>> layers;
183    std::map<uint32_t, std::unique_ptr<screen>> screens;
184
185    controller(wl_registry *r, uint32_t name, uint32_t version);
186    ~controller() override;
187
188    // Events
189    void screen(uint32_t id, ivi_controller_screen *screen);
190    void layer(uint32_t id);
191    void surface(uint32_t id);
192    void error(int32_t oid, int32_t otype, int32_t code, char const *text);
193 };
194 }
195
196 #endif  // !WM_WAYLAND_HPP