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