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