wayland: add missing add_layer and clear requests to genivi::screen
[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 *>(this->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    void flush();
61    int get_fd() const;
62 };
63
64 //                 _     _
65 //  _ __ ___  __ _(_)___| |_ _ __ _   _
66 // | '__/ _ \/ _` | / __| __| '__| | | |
67 // | | |  __/ (_| | \__ \ |_| |  | |_| |
68 // |_|  \___|\__, |_|___/\__|_|   \__, |
69 //           |___/                |___/
70 struct registry : public wayland_proxy<struct wl_registry> {
71    typedef std::function<void(struct wl_registry *, uint32_t, uint32_t)> binder;
72    std::map<std::string, binder> bindings;
73
74    registry(struct wl_display *d);
75    ~registry();
76
77    void add_global_handler(char const *iface, binder bind);
78
79    // Events
80    void global(uint32_t name, char const *iface, uint32_t v);
81    void global_remove(uint32_t name);
82 };
83
84 //              _               _
85 //   ___  _   _| |_ _ __  _   _| |_
86 //  / _ \| | | | __| '_ \| | | | __|
87 // | (_) | |_| | |_| |_) | |_| | |_
88 //  \___/ \__,_|\__| .__/ \__,_|\__|
89 //                 |_|
90 struct output : wayland_proxy<struct wl_output> {
91    int width;
92    int height;
93    int refresh;
94
95    output(struct wl_registry *registry, uint32_t name, uint32_t version);
96
97    // Events
98    void geometry(int32_t x, int32_t y, int32_t pw, int32_t ph, int32_t subpel,
99                  char const *make, char const *model, int32_t tx);
100    void mode(uint32_t flags, int32_t w, int32_t h, int32_t r);
101    void done();
102    void scale(int32_t factor);
103 };
104 }
105
106 //  _ __   __ _ _ __ ___   ___  ___ _ __   __ _  ___ ___
107 // | '_ \ / _` | '_ ` _ \ / _ \/ __| '_ \ / _` |/ __/ _ \
108 // | | | | (_| | | | | | |  __/\__ \ |_) | (_| | (_|  __/
109 // |_| |_|\__,_|_| |_| |_|\___||___/ .__/ \__,_|\___\___|
110 //                                 |_|
111 //                   _       _
112 //   __ _  ___ _ __ (_)_   _(_)
113 //  / _` |/ _ \ '_ \| \ \ / / |
114 // | (_| |  __/ | | | |\ V /| |
115 //  \__, |\___|_| |_|_| \_/ |_|
116 //  |___/
117 namespace genivi {
118
119 struct size {
120    uint32_t w, h;
121 };
122
123 struct rect {
124    uint32_t w, h;
125    int32_t x, y;
126 };
127
128 struct controller;
129
130 struct controlled_entity {
131    struct controller *parent;
132    uint32_t id;
133
134    controlled_entity(struct controller *c, uint32_t i) : parent(c), id(i) {}
135    virtual ~controlled_entity() {}
136 };
137
138 //                  __
139 //  ___ _   _ _ __ / _| __ _  ___ ___
140 // / __| | | | '__| |_ / _` |/ __/ _ \
141 // \__ \ |_| | |  |  _| (_| | (_|  __/
142 // |___/\__,_|_|  |_|  \__,_|\___\___|
143 //
144 struct surface : public wayland_proxy<struct ivi_controller_surface>,
145                  controlled_entity {
146    struct rect dst_rect;
147    struct rect src_rect;
148    struct size size;
149    int32_t orientation;
150    int32_t visibility;
151    float opacity;
152
153    surface(uint32_t i, struct controller *c);
154    ~surface() override;
155
156    // Requests
157    inline void set_visibility(uint32_t visibility) {
158       ivi_controller_surface_set_visibility(this->proxy, visibility);
159    }
160
161    inline void set_opacity(wl_fixed_t opacity) {
162       ivi_controller_surface_set_opacity(this->proxy, opacity);
163    }
164
165    inline void set_source_rectangle(int32_t x, int32_t y, int32_t width,
166                                     int32_t height) {
167       ivi_controller_surface_set_source_rectangle(this->proxy, x, y, width,
168                                                   height);
169    }
170
171    inline void set_destination_rectangle(int32_t x, int32_t y, int32_t width,
172                                          int32_t height) {
173       ivi_controller_surface_set_destination_rectangle(this->proxy, x, y, width,
174                                                        height);
175    }
176
177    inline void set_configuration(int32_t width, int32_t height) {
178       ivi_controller_surface_set_configuration(this->proxy, width, height);
179    }
180
181    inline void set_orientation(int32_t orientation) {
182       ivi_controller_surface_set_orientation(this->proxy, orientation);
183    }
184
185    inline void screenshot(const char *filename) {
186       ivi_controller_surface_screenshot(this->proxy, filename);
187    }
188
189    inline void send_stats() { ivi_controller_surface_send_stats(this->proxy); }
190
191    inline void destroy(int32_t destroy_scene_object) {
192       ivi_controller_surface_destroy(this->proxy, destroy_scene_object);
193    }
194 };
195
196 //  _
197 // | | __ _ _   _  ___ _ __
198 // | |/ _` | | | |/ _ \ '__|
199 // | | (_| | |_| |  __/ |
200 // |_|\__,_|\__, |\___|_|
201 //          |___/
202 struct layer : public wayland_proxy<struct ivi_controller_layer>,
203                controlled_entity {
204    layer(uint32_t i, struct controller *c);
205    layer(uint32_t i, int32_t w, int32_t h, struct controller *c);
206    ~layer() override;
207
208    // Requests
209    inline void set_visibility(uint32_t visibility) {
210       ivi_controller_layer_set_visibility(this->proxy, visibility);
211    }
212
213    inline void set_opacity(wl_fixed_t opacity) {
214       ivi_controller_layer_set_opacity(this->proxy, opacity);
215    }
216
217    inline void set_source_rectangle(int32_t x, int32_t y, int32_t width,
218                                     int32_t height) {
219       ivi_controller_layer_set_source_rectangle(this->proxy, x, y, width,
220                                                 height);
221    }
222
223    inline void set_destination_rectangle(int32_t x, int32_t y, int32_t width,
224                                          int32_t height) {
225       ivi_controller_layer_set_destination_rectangle(this->proxy, x, y, width,
226                                                      height);
227    }
228
229    inline void set_configuration(int32_t width, int32_t height) {
230       ivi_controller_layer_set_configuration(this->proxy, width, height);
231    }
232
233    inline void set_orientation(int32_t orientation) {
234       ivi_controller_layer_set_orientation(this->proxy, orientation);
235    }
236
237    inline void screenshot(const char *filename) {
238       ivi_controller_layer_screenshot(this->proxy, filename);
239    }
240
241    inline void clear_surfaces() {
242       ivi_controller_layer_clear_surfaces(this->proxy);
243    }
244
245    inline void add_surface(struct surface *surface) {
246       ivi_controller_layer_add_surface(this->proxy, surface->proxy);
247    }
248
249    inline void remove_surface(struct surface *surface) {
250       ivi_controller_layer_remove_surface(this->proxy, surface->proxy);
251    }
252
253    inline void set_render_order(struct wl_array *surfaces) {
254       ivi_controller_layer_set_render_order(this->proxy, surfaces);
255    }
256 };
257
258 //
259 //  ___  ___ _ __ ___  ___ _ __
260 // / __|/ __| '__/ _ \/ _ \ '_ \
261 // \__ \ (__| | |  __/  __/ | | |
262 // |___/\___|_|  \___|\___|_| |_|
263 //
264 struct screen : public wayland_proxy<struct ivi_controller_screen>,
265                 controlled_entity {
266    screen(uint32_t i, struct controller *c, struct ivi_controller_screen *p);
267
268    void clear() { ivi_controller_screen_clear(this->proxy); }
269    void add_layer(layer *l) { ivi_controller_screen_add_layer(this->proxy, l->proxy); }
270 };
271
272 //                  _             _ _
273 //   ___ ___  _ __ | |_ _ __ ___ | | | ___ _ __
274 //  / __/ _ \| '_ \| __| '__/ _ \| | |/ _ \ '__|
275 // | (_| (_) | | | | |_| | | (_) | | |  __/ |
276 //  \___\___/|_| |_|\__|_|  \___/|_|_|\___|_|
277 //
278 struct controller : public wayland_proxy<struct ivi_controller> {
279    std::map<uint32_t, std::unique_ptr<struct surface>> surfaces;
280    std::map<uint32_t, std::unique_ptr<struct layer>> layers;
281    std::map<uint32_t, std::unique_ptr<struct screen>> screens;
282
283    typedef std::pair<char const *, std::function<void(struct controller *)>>
284       name_task_pair;
285    std::vector<name_task_pair> pending;
286
287    void add_task(char const *name,
288                  std::function<void(struct controller *)> &&f) {
289       this->pending.emplace_back(std::make_pair(name, f));
290    }
291
292    void execute_pending() {
293       if (!this->pending.empty()) {
294          for (auto &t : this->pending) {
295             logdebug("executing task '%s'", t.first);
296             t.second(this);
297          }
298          this->pending.clear();
299          ivi_controller_commit_changes(this->proxy);
300       }
301    }
302
303    controller(struct wl_registry *r, uint32_t name, uint32_t version);
304    ~controller() override;
305
306    // Requests
307    void commit_changes() const { ivi_controller_commit_changes(this->proxy); }
308    void layer_create(uint32_t id, int32_t w, int32_t h);
309    void surface_create(uint32_t id);
310
311    // Events
312    // controller
313    void controller_screen(uint32_t id, struct ivi_controller_screen *screen);
314    void controller_layer(uint32_t id);
315    void controller_surface(uint32_t id);
316    void controller_error(int32_t oid, int32_t otype, int32_t code,
317                          char const *text);
318
319    // surface
320    void surface_visibility(uint32_t id, int32_t visibility);
321    void surface_opacity(uint32_t id, float opacity);
322    void surface_source_rectangle(uint32_t id, int32_t x, int32_t y,
323                                  int32_t width, int32_t height);
324    void surface_destination_rectangle(uint32_t id, int32_t x, int32_t y,
325                                       int32_t width, int32_t height);
326    void surface_configuration(uint32_t id, int32_t width, int32_t height);
327    void surface_orientation(uint32_t id, int32_t orientation);
328    void surface_pixelformat(uint32_t id, int32_t pixelformat);
329    void surface_layer(uint32_t id, struct ivi_controller_layer *layer);
330    void surface_stats(uint32_t id, uint32_t redraw_count, uint32_t frame_count,
331                       uint32_t update_count, uint32_t pid,
332                       const char *process_name);
333    void surface_destroyed(uint32_t id);
334    void surface_content(uint32_t id, int32_t content_state);
335
336    // layer
337    void layer_visibility(uint32_t id, int32_t visibility);
338    void layer_opacity(uint32_t id, float opacity);
339    void layer_source_rectangle(uint32_t id, int32_t x, int32_t y, int32_t width,
340                                int32_t height);
341    void layer_destination_rectangle(uint32_t id, int32_t x, int32_t y,
342                                     int32_t width, int32_t height);
343    void layer_configuration(uint32_t id, int32_t width, int32_t height);
344    void layer_orientation(uint32_t id, int32_t orientation);
345    void layer_screen(uint32_t id, struct wl_output *screen);
346    void layer_destroyed(uint32_t id);
347 };
348 }
349
350 #endif  // !WM_WAYLAND_HPP