all: 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.hpp"
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    std::unique_ptr<ProxyT, std::function<void(ProxyT *)>> proxy;
22    wayland_proxy(wayland_proxy const &) = delete;
23    wayland_proxy &operator=(wayland_proxy const &) = delete;
24    wayland_proxy(void *p)
25       : wayland_proxy(p,
26                       reinterpret_cast<void (*)(ProxyT *)>(wl_proxy_destroy)) {}
27    wayland_proxy(void *p, std::function<void(ProxyT *)> &&p_del)
28       : proxy(std::unique_ptr<ProxyT, std::function<void(ProxyT *)>>(
29            static_cast<ProxyT *>(p), p_del)) {}
30 };
31
32 //                                                                  _
33 //  _ __   __ _ _ __ ___   ___  ___ _ __   __ _  ___ ___  __      _| |
34 // | '_ \ / _` | '_ ` _ \ / _ \/ __| '_ \ / _` |/ __/ _ \ \ \ /\ / / |
35 // | | | | (_| | | | | | |  __/\__ \ |_) | (_| | (_|  __/  \ V  V /| |
36 // |_| |_|\__,_|_| |_| |_|\___||___/ .__/ \__,_|\___\___|   \_/\_/ |_|
37 //                                 |_|
38 namespace wl {
39 //                 _     _
40 //  _ __ ___  __ _(_)___| |_ _ __ _   _
41 // | '__/ _ \/ _` | / __| __| '__| | | |
42 // | | |  __/ (_| | \__ \ |_| |  | |_| |
43 // |_|  \___|\__, |_|___/\__|_|   \__, |
44 //           |___/                |___/
45 struct registry : public wayland_proxy<struct wl_registry> {
46    typedef std::function<void(struct wl_registry *, uint32_t, uint32_t)> binder;
47    std::unordered_map<std::string, binder> bindings;
48
49    registry(registry const &) = delete;
50    registry &operator=(registry const &) = delete;
51    registry(struct wl_display *d);
52
53    void add_global_handler(char const *iface, binder bind);
54
55    // Events
56    void global(uint32_t name, char const *iface, uint32_t v);
57    void global_remove(uint32_t name);
58 };
59
60 //      _ _           _
61 //   __| (_)___ _ __ | | __ _ _   _
62 //  / _` | / __| '_ \| |/ _` | | | |
63 // | (_| | \__ \ |_) | | (_| | |_| |
64 //  \__,_|_|___/ .__/|_|\__,_|\__, |
65 //             |_|            |___/
66 struct display {
67    std::unique_ptr<struct wl_display, void (*)(struct wl_display *)> d;
68    struct registry r;
69
70    display(display const &) = delete;
71    display &operator=(display const &) = delete;
72    display();
73    bool ok() const;
74    void roundtrip();
75    int dispatch();
76    void flush();
77    int get_fd() const;
78 };
79
80 //              _               _
81 //   ___  _   _| |_ _ __  _   _| |_
82 //  / _ \| | | | __| '_ \| | | | __|
83 // | (_) | |_| | |_| |_) | |_| | |_
84 //  \___/ \__,_|\__| .__/ \__,_|\__|
85 //                 |_|
86 struct output : wayland_proxy<struct wl_output> {
87    int width{};
88    int height{};
89    int refresh{};
90
91    output(output const &) = delete;
92    output &operator=(output const &) = delete;
93    output(struct wl_registry *r, uint32_t name, uint32_t v);
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 }  // namespace wl
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 controller_child {
129    struct controller *parent;
130    uint32_t id;
131
132    controller_child(controller_child const &) = delete;
133    controller_child &operator=(controller_child const &) = delete;
134    controller_child(struct controller *c, uint32_t i) : parent(c), id(i) {}
135    virtual ~controller_child() {}
136 };
137
138 //                  __
139 //  ___ _   _ _ __ / _| __ _  ___ ___
140 // / __| | | | '__| |_ / _` |/ __/ _ \
141 // \__ \ |_| | |  |  _| (_| | (_|  __/
142 // |___/\__,_|_|  |_|  \__,_|\___\___|
143 //
144 struct surface : public wayland_proxy<struct ivi_controller_surface>,
145                  controller_child {
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(surface const &) = delete;
154    surface &operator=(surface const &) = delete;
155    surface(uint32_t i, struct controller *c);
156
157    // Requests
158    void set_visibility(uint32_t visibility);
159    void set_opacity(wl_fixed_t opacity);
160    void set_source_rectangle(int32_t x, int32_t y, int32_t width,
161                              int32_t height);
162    void set_destination_rectangle(int32_t x, int32_t y, int32_t width,
163                                   int32_t height);
164    void set_configuration(int32_t width, int32_t height);
165    void set_orientation(int32_t orientation);
166    void screenshot(const char *filename);
167    void send_stats();
168    void destroy(int32_t destroy_scene_object);
169 };
170
171 //  _
172 // | | __ _ _   _  ___ _ __
173 // | |/ _` | | | |/ _ \ '__|
174 // | | (_| | |_| |  __/ |
175 // |_|\__,_|\__, |\___|_|
176 //          |___/
177 struct layer : public wayland_proxy<struct ivi_controller_layer>,
178                controller_child {
179    struct rect dst_rect;
180    struct rect src_rect;
181    struct size size;
182    int32_t orientation;
183    int32_t visibility;
184    float opacity;
185
186    layer(layer const &) = delete;
187    layer &operator=(layer const &) = delete;
188    layer(uint32_t i, struct controller *c);
189    layer(uint32_t i, int32_t w, int32_t h, struct controller *c);
190
191    // Requests
192    void set_visibility(uint32_t visibility);
193    void set_opacity(wl_fixed_t opacity);
194    void set_source_rectangle(int32_t x, int32_t y, int32_t width,
195                              int32_t height);
196    void set_destination_rectangle(int32_t x, int32_t y, int32_t width,
197                                   int32_t height);
198    void set_configuration(int32_t width, int32_t height);
199    void set_orientation(int32_t orientation);
200    void screenshot(const char *filename);
201    void clear_surfaces();
202    void add_surface(struct surface *surface);
203    void remove_surface(struct surface *surface);
204    void set_render_order(std::vector<uint32_t> const &ro);
205 };
206
207 //
208 //  ___  ___ _ __ ___  ___ _ __
209 // / __|/ __| '__/ _ \/ _ \ '_ \
210 // \__ \ (__| | |  __/  __/ | | |
211 // |___/\___|_|  \___|\___|_| |_|
212 //
213 struct screen : public wayland_proxy<struct ivi_controller_screen>,
214                 controller_child {
215    screen(screen const &) = delete;
216    screen &operator=(screen const &) = delete;
217    screen(uint32_t i, struct controller *c, struct ivi_controller_screen *p);
218    void clear();
219    void add_layer(layer *l);
220    void set_render_order(std::vector<uint32_t> const &ro);
221 };
222
223 //                  _             _ _
224 //   ___ ___  _ __ | |_ _ __ ___ | | | ___ _ __
225 //  / __/ _ \| '_ \| __| '__/ _ \| | |/ _ \ '__|
226 // | (_| (_) | | | | |_| | | (_) | | |  __/ |
227 //  \___\___/|_| |_|\__|_|  \___/|_|_|\___|_|
228 //
229 struct controller : public wayland_proxy<struct ivi_controller> {
230    // HACK:
231    // The order of these member is mandatory, as when objects are destroyed
232    // they will call their parent (that's us right here!) and remove their
233    // proxy-to-id mapping. I.e. the *_proxy_to_id members need to be valid
234    // when the surfaces/layers/screens maps are destroyed. This sucks, but
235    // I cannot see a better solution w/o globals or some other horrible
236    // call-our-parent construct.
237    std::unordered_map<uintptr_t, uint32_t> surface_proxy_to_id;
238    std::unordered_map<uintptr_t, uint32_t> layer_proxy_to_id;
239    std::unordered_map<uintptr_t, uint32_t> screen_proxy_to_id;
240
241    std::unordered_map<uint32_t, std::unique_ptr<struct surface>> surfaces;
242    std::unordered_map<uint32_t, std::unique_ptr<struct layer>> layers;
243    std::unordered_map<uint32_t, std::unique_ptr<struct screen>> screens;
244
245    typedef std::pair<char const *, std::function<void(struct controller *)>>
246       name_task_pair;
247    std::vector<name_task_pair> pending;
248
249    size output_size;
250
251    void debug_dump_current_status();
252
253    void add_proxy_to_id_mapping(struct ivi_controller_surface *p, uint32_t id);
254    void remove_proxy_to_id_mapping(struct ivi_controller_surface *p);
255    void add_proxy_to_id_mapping(struct ivi_controller_layer *p, uint32_t id);
256    void remove_proxy_to_id_mapping(struct ivi_controller_layer *p);
257    void add_proxy_to_id_mapping(struct wl_output *p, uint32_t id);
258    void remove_proxy_to_id_mapping(struct wl_output *p);
259
260    void add_task(char const *name,
261                  std::function<void(struct controller *)> &&f);
262    void execute_pending();
263
264    controller(struct wl_registry *r, uint32_t name, uint32_t version);
265
266    // Requests
267    void commit_changes() const {
268       ivi_controller_commit_changes(this->proxy.get());
269    }
270    void layer_create(uint32_t id, int32_t w, int32_t h);
271    void surface_create(uint32_t id);
272
273    // Events
274    // controller
275    void controller_screen(uint32_t id, struct ivi_controller_screen *screen);
276    void controller_layer(uint32_t id);
277    void controller_surface(uint32_t id);
278    void controller_error(int32_t object_id, int32_t object_type,
279                          int32_t error_code, char const *error_text);
280
281    // surface
282    void surface_visibility(struct surface *s, int32_t visibility);
283    void surface_opacity(struct surface *s, float opacity);
284    void surface_source_rectangle(struct surface *s, int32_t x, int32_t y,
285                                  int32_t width, int32_t height);
286    void surface_destination_rectangle(struct surface *s, int32_t x, int32_t y,
287                                       int32_t width, int32_t height);
288    void surface_configuration(struct surface *s, int32_t width, int32_t height);
289    void surface_orientation(struct surface *s, int32_t orientation);
290    void surface_pixelformat(struct surface *s, int32_t pixelformat);
291    void surface_layer(struct surface *s, struct ivi_controller_layer *layer);
292    void surface_stats(struct surface *s, uint32_t redraw_count,
293                       uint32_t frame_count, uint32_t update_count, uint32_t pid,
294                       const char *process_name);
295    void surface_destroyed(struct surface *s);
296    void surface_content(struct surface *s, int32_t content_state);
297
298    // layer
299    void layer_visibility(struct layer *l, int32_t visibility);
300    void layer_opacity(struct layer *l, float opacity);
301    void layer_source_rectangle(struct layer *l, int32_t x, int32_t y,
302                                int32_t width, int32_t height);
303    void layer_destination_rectangle(struct layer *l, int32_t x, int32_t y,
304                                     int32_t width, int32_t height);
305    void layer_configuration(struct layer *l, int32_t width, int32_t height);
306    void layer_orientation(struct layer *l, int32_t orientation);
307    void layer_screen(struct layer *l, struct wl_output *screen);
308    void layer_destroyed(struct layer *l);
309 };
310 }  // namespace genivi
311
312 #endif  // !WM_WAYLAND_HPP