wayland: added notification prototype, be more explicit about struct types
[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 #include <vector>
12
13 //                      _                 _
14 // __      ____ _ _   _| | __ _ _ __   __| |     _ __  _ __ _____  ___   _
15 // \ \ /\ / / _` | | | | |/ _` | '_ \ / _` |    | '_ \| '__/ _ \ \/ / | | |
16 //  \ V  V / (_| | |_| | | (_| | | | | (_| |    | |_) | | | (_) >  <| |_| |
17 //   \_/\_/ \__,_|\__, |_|\__,_|_| |_|\__,_|____| .__/|_|  \___/_/\_\\__, |
18 //                |___/                   |_____|_|                  |___/
19 template <typename ProxyT>
20 struct wayland_proxy {
21    ProxyT *proxy;
22
23    wayland_proxy(void *p) : proxy(static_cast<ProxyT *>(p)) {}
24
25    virtual ~wayland_proxy() {
26       // If this is the nullptr, then it means it already was destroyed by a
27       // custom wayland dtor
28       if (this->proxy) {
29          logdebug("%s %p @ %p", __func__, this, this->proxy);
30          wl_proxy_destroy(reinterpret_cast<struct wl_proxy *>(proxy));
31       }
32    }
33 };
34
35 //                                                                  _
36 //  _ __   __ _ _ __ ___   ___  ___ _ __   __ _  ___ ___  __      _| |
37 // | '_ \ / _` | '_ ` _ \ / _ \/ __| '_ \ / _` |/ __/ _ \ \ \ /\ / / |
38 // | | | | (_| | | | | | |  __/\__ \ |_) | (_| | (_|  __/  \ V  V /| |
39 // |_| |_|\__,_|_| |_| |_|\___||___/ .__/ \__,_|\___\___|   \_/\_/ |_|
40 //                                 |_|
41 namespace wl {
42 struct registry;
43
44 //      _ _           _
45 //   __| (_)___ _ __ | | __ _ _   _
46 //  / _` | / __| '_ \| |/ _` | | | |
47 // | (_| | \__ \ |_) | | (_| | |_| |
48 //  \__,_|_|___/ .__/|_|\__,_|\__, |
49 //             |_|            |___/
50 struct display {
51    std::unique_ptr<struct wl_display, std::function<void(struct wl_display *)>>
52       d;
53    std::unique_ptr<struct registry> r;
54
55    display();
56    ~display();
57    bool ok() const;
58    void roundtrip();
59    int dispatch();
60 };
61
62 //                 _     _
63 //  _ __ ___  __ _(_)___| |_ _ __ _   _
64 // | '__/ _ \/ _` | / __| __| '__| | | |
65 // | | |  __/ (_| | \__ \ |_| |  | |_| |
66 // |_|  \___|\__, |_|___/\__|_|   \__, |
67 //           |___/                |___/
68 struct registry : public wayland_proxy<struct wl_registry> {
69    typedef std::function<void(struct wl_registry *, uint32_t, uint32_t)> binder;
70    std::map<std::string, binder> bindings;
71
72    registry(struct wl_display *d);
73    ~registry();
74
75    void add_global_handler(char const *iface, binder bind);
76
77    // Events
78    void global(uint32_t name, char const *iface, uint32_t v);
79    void global_remove(uint32_t name);
80 };
81
82 //              _               _
83 //   ___  _   _| |_ _ __  _   _| |_
84 //  / _ \| | | | __| '_ \| | | | __|
85 // | (_) | |_| | |_| |_) | |_| | |_
86 //  \___/ \__,_|\__| .__/ \__,_|\__|
87 //                 |_|
88 struct output : wayland_proxy<struct wl_output> {
89    output(struct wl_registry *registry, uint32_t name, uint32_t version);
90
91    // Events
92    void geometry(int32_t x, int32_t y, int32_t pw, int32_t ph, int32_t subpel,
93                  char const *make, char const *model, int32_t tx);
94    void mode(uint32_t flags, int32_t w, int32_t h, int32_t r);
95    void done();
96    void scale(int32_t factor);
97 };
98 }
99
100 //  _ __   __ _ _ __ ___   ___  ___ _ __   __ _  ___ ___
101 // | '_ \ / _` | '_ ` _ \ / _ \/ __| '_ \ / _` |/ __/ _ \
102 // | | | | (_| | | | | | |  __/\__ \ |_) | (_| | (_|  __/
103 // |_| |_|\__,_|_| |_| |_|\___||___/ .__/ \__,_|\___\___|
104 //                                 |_|
105 //                   _       _
106 //   __ _  ___ _ __ (_)_   _(_)
107 //  / _` |/ _ \ '_ \| \ \ / / |
108 // | (_| |  __/ | | | |\ V /| |
109 //  \__, |\___|_| |_|_| \_/ |_|
110 //  |___/
111 namespace genivi {
112
113 //
114 //  _ __   __ _ _ __ ___   ___  ___ _ __   __ _  ___ ___
115 // | '_ \ / _` | '_ ` _ \ / _ \/ __| '_ \ / _` |/ __/ _ \
116 // | | | | (_| | | | | | |  __/\__ \ |_) | (_| | (_|  __/
117 // |_| |_|\__,_|_| |_| |_|\___||___/ .__/ \__,_|\___\___|
118 //                                 |_|
119 //              _   _  __
120 //  _ __   ___ | |_(_)/ _|_   _
121 // | '_ \ / _ \| __| | |_| | | |
122 // | | | | (_) | |_| |  _| |_| |
123 // |_| |_|\___/ \__|_|_|  \__, |
124 //                        |___/
125 namespace notify {
126 enum property {
127    Dimensions,  // i.e. configure
128    DestRect,
129    SrcRect,
130    ZOrder,
131    Vibility,
132    Opacity,
133    Layer,
134    Surface,
135    Content,
136    Orientation,
137    Screen,
138 };
139
140 template <typename ObjectT>
141 struct notifier {
142    typedef std::function<void(enum property, ObjectT *)> ReceiverT;
143
144    std::vector<ReceiverT> receivers;
145
146    virtual ~notifier() {}
147
148    void add_receiver(ReceiverT);
149    void notify(enum property) const;
150 };
151 }
152
153 //                  __
154 //  ___ _   _ _ __ / _| __ _  ___ ___
155 // / __| | | | '__| |_ / _` |/ __/ _ \
156 // \__ \ |_| | |  |  _| (_| | (_|  __/
157 // |___/\__,_|_|  |_|  \__,_|\___\___|
158 //
159 struct surface : public wayland_proxy<struct ivi_controller_surface>,
160                  notify::notifier<struct surface> {
161    uint32_t id;
162
163    surface(uint32_t i, struct ivi_controller *c);
164    ~surface() override;
165
166    // Events
167    void visibility(int32_t visibility);
168    void opacity(float opacity);
169    void source_rectangle(int32_t x, int32_t y, int32_t width, int32_t height);
170    void destination_rectangle(int32_t x, int32_t y, int32_t width,
171                               int32_t height);
172    void configuration(int32_t width, int32_t height);
173    void orientation(int32_t orientation);
174    void pixelformat(int32_t pixelformat);
175    void layer(struct ivi_controller_layer *layer);
176    void stats(uint32_t redraw_count, uint32_t frame_count,
177               uint32_t update_count, uint32_t pid, const char *process_name);
178    void destroyed();
179    void content(int32_t content_state);
180
181    // Requests
182    inline void set_visibility(uint32_t visibility) {
183       ivi_controller_surface_set_visibility(this->proxy, visibility);
184    }
185
186    inline void set_opacity(wl_fixed_t opacity) {
187       ivi_controller_surface_set_opacity(this->proxy, opacity);
188    }
189
190    inline void set_source_rectangle(int32_t x, int32_t y, int32_t width,
191                                     int32_t height) {
192       ivi_controller_surface_set_source_rectangle(this->proxy, x, y, width,
193                                                   height);
194    }
195
196    inline void set_destination_rectangle(int32_t x, int32_t y, int32_t width,
197                                          int32_t height) {
198       ivi_controller_surface_set_destination_rectangle(this->proxy, x, y, width,
199                                                        height);
200    }
201
202    inline void set_configuration(int32_t width, int32_t height) {
203       ivi_controller_surface_set_configuration(this->proxy, width, height);
204    }
205
206    inline void set_orientation(int32_t orientation) {
207       ivi_controller_surface_set_orientation(this->proxy, orientation);
208    }
209
210    inline void screenshot(const char *filename) {
211       ivi_controller_surface_screenshot(this->proxy, filename);
212    }
213
214    inline void send_stats() { ivi_controller_surface_send_stats(this->proxy); }
215
216    inline void destroy(int32_t destroy_scene_object) {
217       ivi_controller_surface_destroy(this->proxy, destroy_scene_object);
218    }
219 };
220
221 //  _
222 // | | __ _ _   _  ___ _ __
223 // | |/ _` | | | |/ _ \ '__|
224 // | | (_| | |_| |  __/ |
225 // |_|\__,_|\__, |\___|_|
226 //          |___/
227 struct layer : public wayland_proxy<struct ivi_controller_layer>,
228                       notify::notifier<struct layer> {
229    uint32_t id;
230
231    layer(uint32_t i, struct ivi_controller *c);
232    ~layer() override;
233
234    // Events
235    void visibility(int32_t visibility);
236    void opacity(float opacity);
237    void source_rectangle(int32_t x, int32_t y, int32_t width, int32_t height);
238    void destination_rectangle(int32_t x, int32_t y, int32_t width,
239                               int32_t height);
240    void configuration(int32_t width, int32_t height);
241    void orientation(int32_t orientation);
242    void screen(struct wl_output *screen);
243    void destroyed();
244
245    // Requests
246    inline void set_visibility(uint32_t visibility) {
247       ivi_controller_layer_set_visibility(this->proxy, visibility);
248    }
249
250    inline void set_opacity(wl_fixed_t opacity) {
251       ivi_controller_layer_set_opacity(this->proxy, opacity);
252    }
253
254    inline void set_source_rectangle(int32_t x, int32_t y, int32_t width,
255                                     int32_t height) {
256       ivi_controller_layer_set_source_rectangle(this->proxy, x, y, width,
257                                                 height);
258    }
259
260    inline void set_destination_rectangle(int32_t x, int32_t y, int32_t width,
261                                          int32_t height) {
262       ivi_controller_layer_set_destination_rectangle(this->proxy, x, y, width,
263                                                      height);
264    }
265
266    inline void set_configuration(int32_t width, int32_t height) {
267       ivi_controller_layer_set_configuration(this->proxy, width, height);
268    }
269
270    inline void set_orientation(int32_t orientation) {
271       ivi_controller_layer_set_orientation(this->proxy, orientation);
272    }
273
274    inline void screenshot(const char *filename) {
275       ivi_controller_layer_screenshot(this->proxy, filename);
276    }
277
278    inline void clear_surfaces() {
279       ivi_controller_layer_clear_surfaces(this->proxy);
280    }
281
282    inline void add_surface(struct surface *surface) {
283       ivi_controller_layer_add_surface(this->proxy, surface->proxy);
284    }
285
286    inline void remove_surface(struct surface *surface) {
287       ivi_controller_layer_remove_surface(this->proxy, surface->proxy);
288    }
289
290    inline void set_render_order(struct wl_array *surfaces) {
291       ivi_controller_layer_set_render_order(this->proxy, surfaces);
292    }
293 };
294
295 //
296 //  ___  ___ _ __ ___  ___ _ __
297 // / __|/ __| '__/ _ \/ _ \ '_ \
298 // \__ \ (__| | |  __/  __/ | | |
299 // |___/\___|_|  \___|\___|_| |_|
300 //
301 struct screen : public wayland_proxy<struct ivi_controller_screen> {
302    uint32_t id;
303
304    screen(uint32_t i, struct ivi_controller_screen *p);
305 };
306
307 //                  _             _ _
308 //   ___ ___  _ __ | |_ _ __ ___ | | | ___ _ __
309 //  / __/ _ \| '_ \| __| '__/ _ \| | |/ _ \ '__|
310 // | (_| (_) | | | | |_| | | (_) | | |  __/ |
311 //  \___\___/|_| |_|\__|_|  \___/|_|_|\___|_|
312 //
313 struct controller : public wayland_proxy<struct ivi_controller> {
314    std::map<uint32_t, std::unique_ptr<struct surface>> surfaces;
315    std::map<uint32_t, std::unique_ptr<struct layer>> layers;
316    std::map<uint32_t, std::unique_ptr<struct screen>> screens;
317
318    controller(struct wl_registry *r, uint32_t name, uint32_t version);
319    ~controller() override;
320
321    // Events
322    void screen(uint32_t id, struct ivi_controller_screen *screen);
323    void layer(uint32_t id);
324    void surface(uint32_t id);
325    void error(int32_t oid, int32_t otype, int32_t code, char const *text);
326 };
327 }
328
329 #endif  // !WM_WAYLAND_HPP