wayland: add genivi::full_rect and operator== for genivi::rect
[staging/windowmanager.git] / src / wayland.hpp
1 #ifndef WM_WAYLAND_HPP
2 #define WM_WAYLAND_HPP
3
4 #include "controller_hooks.hpp"
5 #include "ivi-controller-client-protocol.h"
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    int get_error();
79
80    // Lets just proxy this for the registry
81    inline void add_global_handler(char const *iface, registry::binder bind) {
82       this->r.add_global_handler(iface, bind);
83    }
84 };
85
86 //              _               _
87 //   ___  _   _| |_ _ __  _   _| |_
88 //  / _ \| | | | __| '_ \| | | | __|
89 // | (_) | |_| | |_| |_) | |_| | |_
90 //  \___/ \__,_|\__| .__/ \__,_|\__|
91 //                 |_|
92 struct output : wayland_proxy<struct wl_output> {
93    int width{};
94    int height{};
95    int refresh{};
96
97    output(output const &) = delete;
98    output &operator=(output const &) = delete;
99    output(struct wl_registry *r, uint32_t name, uint32_t v);
100
101    // Events
102    void geometry(int32_t x, int32_t y, int32_t pw, int32_t ph, int32_t subpel,
103                  char const *make, char const *model, int32_t tx);
104    void mode(uint32_t flags, int32_t w, int32_t h, int32_t r);
105    void done();
106    void scale(int32_t factor);
107 };
108 }  // namespace wl
109
110 //  _ __   __ _ _ __ ___   ___  ___ _ __   __ _  ___ ___
111 // | '_ \ / _` | '_ ` _ \ / _ \/ __| '_ \ / _` |/ __/ _ \
112 // | | | | (_| | | | | | |  __/\__ \ |_) | (_| | (_|  __/
113 // |_| |_|\__,_|_| |_| |_|\___||___/ .__/ \__,_|\___\___|
114 //                                 |_|
115 //                   _       _
116 //   __ _  ___ _ __ (_)_   _(_)
117 //  / _` |/ _ \ '_ \| \ \ / / |
118 // | (_| |  __/ | | | |\ V /| |
119 //  \__, |\___|_| |_|_| \_/ |_|
120 //  |___/
121 namespace genivi {
122
123 struct size {
124    uint32_t w, h;
125 };
126
127 struct rect {
128    int32_t w, h;
129    int32_t x, y;
130 };
131
132 static const constexpr rect full_rect = rect{-1, -1, 0, 0};
133
134 inline bool operator == (struct rect a, struct rect b) {
135    return a.w == b.w && a.h == b.h && a.x == b.x && a.y == b.y;
136 }
137
138 struct controller;
139
140 struct controller_child {
141    struct controller *parent;
142    uint32_t id;
143
144    controller_child(controller_child const &) = delete;
145    controller_child &operator=(controller_child const &) = delete;
146    controller_child(struct controller *c, uint32_t i) : parent(c), id(i) {}
147    virtual ~controller_child() {}
148 };
149
150 struct surface_properties {
151    uint32_t id;  // let's just save an ID here too
152    struct rect dst_rect;
153    struct rect src_rect;
154    struct size size;
155    int32_t orientation;
156    int32_t visibility;
157    float opacity;
158 };
159
160 //                  __
161 //  ___ _   _ _ __ / _| __ _  ___ ___
162 // / __| | | | '__| |_ / _` |/ __/ _ \
163 // \__ \ |_| | |  |  _| (_| | (_|  __/
164 // |___/\__,_|_|  |_|  \__,_|\___\___|
165 //
166 struct surface : public wayland_proxy<struct ivi_controller_surface>,
167                  controller_child {
168    surface(surface const &) = delete;
169    surface &operator=(surface const &) = delete;
170    surface(uint32_t i, struct controller *c);
171
172    // Requests
173    void set_visibility(uint32_t visibility);
174    void set_opacity(wl_fixed_t opacity);
175    void set_source_rectangle(int32_t x, int32_t y, int32_t width,
176                              int32_t height);
177    void set_destination_rectangle(int32_t x, int32_t y, int32_t width,
178                                   int32_t height);
179    void set_configuration(int32_t width, int32_t height);
180    void set_orientation(int32_t orientation);
181    void screenshot(const char *filename);
182    void send_stats();
183    void destroy(int32_t destroy_scene_object);
184 };
185
186 //  _
187 // | | __ _ _   _  ___ _ __
188 // | |/ _` | | | |/ _ \ '__|
189 // | | (_| | |_| |  __/ |
190 // |_|\__,_|\__, |\___|_|
191 //          |___/
192 struct layer : public wayland_proxy<struct ivi_controller_layer>,
193                controller_child {
194    layer(layer const &) = delete;
195    layer &operator=(layer const &) = delete;
196    layer(uint32_t i, struct controller *c);
197    layer(uint32_t i, int32_t w, int32_t h, struct controller *c);
198
199    // Requests
200    void set_visibility(uint32_t visibility);
201    void set_opacity(wl_fixed_t opacity);
202    void set_source_rectangle(int32_t x, int32_t y, int32_t width,
203                              int32_t height);
204    void set_destination_rectangle(int32_t x, int32_t y, int32_t width,
205                                   int32_t height);
206    void set_configuration(int32_t width, int32_t height);
207    void set_orientation(int32_t orientation);
208    void screenshot(const char *filename);
209    void clear_surfaces();
210    void add_surface(struct surface *surface);
211    void remove_surface(struct surface *surface);
212    void set_render_order(std::vector<uint32_t> const &ro);
213 };
214
215 //
216 //  ___  ___ _ __ ___  ___ _ __
217 // / __|/ __| '__/ _ \/ _ \ '_ \
218 // \__ \ (__| | |  __/  __/ | | |
219 // |___/\___|_|  \___|\___|_| |_|
220 //
221 struct screen : public wayland_proxy<struct ivi_controller_screen>,
222                 controller_child {
223    screen(screen const &) = delete;
224    screen &operator=(screen const &) = delete;
225    screen(uint32_t i, struct controller *c, struct ivi_controller_screen *p);
226    void clear();
227    void add_layer(layer *l);
228    void set_render_order(std::vector<uint32_t> const &ro);
229 };
230
231 //                  _             _ _
232 //   ___ ___  _ __ | |_ _ __ ___ | | | ___ _ __
233 //  / __/ _ \| '_ \| __| '__/ _ \| | |/ _ \ '__|
234 // | (_| (_) | | | | |_| | | (_) | | |  __/ |
235 //  \___\___/|_| |_|\__|_|  \___/|_|_|\___|_|
236 //
237 struct controller : public wayland_proxy<struct ivi_controller> {
238    typedef std::unordered_map<uintptr_t, uint32_t> proxy_to_id_map_type;
239    typedef std::unordered_map<uint32_t, std::unique_ptr<struct surface>>
240       surface_map_type;
241    typedef std::unordered_map<uint32_t, std::unique_ptr<struct layer>>
242       layer_map_type;
243    typedef std::unordered_map<uint32_t, std::unique_ptr<struct screen>>
244       screen_map_type;
245
246    typedef std::unordered_map<uint32_t, struct surface_properties> props_map;
247
248    // HACK:
249    // The order of these member is mandatory, as when objects are destroyed
250    // they will call their parent (that's us right here!) and remove their
251    // proxy-to-id mapping. I.e. the *_proxy_to_id members need to be valid
252    // when the surfaces/layers/screens maps are destroyed. This sucks, but
253    // I cannot see a better solution w/o globals or some other horrible
254    // call-our-parent construct.
255    proxy_to_id_map_type surface_proxy_to_id;
256    proxy_to_id_map_type layer_proxy_to_id;
257    proxy_to_id_map_type screen_proxy_to_id;
258
259    props_map sprops;
260    props_map lprops;
261
262    surface_map_type surfaces;
263    layer_map_type layers;
264    screen_map_type screens;
265
266    typedef std::pair<char const *, std::function<void(struct controller *)>>
267       name_task_pair;
268    std::vector<name_task_pair> pending;
269
270    size output_size;
271
272    wm::controller_hooks *chooks;
273
274    void add_proxy_to_id_mapping(struct ivi_controller_surface *p, uint32_t id);
275    void remove_proxy_to_id_mapping(struct ivi_controller_surface *p);
276    void add_proxy_to_id_mapping(struct ivi_controller_layer *p, uint32_t id);
277    void remove_proxy_to_id_mapping(struct ivi_controller_layer *p);
278    void add_proxy_to_id_mapping(struct wl_output *p, uint32_t id);
279    void remove_proxy_to_id_mapping(struct wl_output *p);
280
281    void add_task(char const *name,
282                  std::function<void(struct controller *)> &&f);
283    void execute_pending();
284
285    controller(struct wl_registry *r, uint32_t name, uint32_t version);
286
287    // Requests
288    void commit_changes() const {
289       ivi_controller_commit_changes(this->proxy.get());
290    }
291    void layer_create(uint32_t id, int32_t w, int32_t h);
292    void surface_create(uint32_t id);
293
294    // Events
295    // controller
296    void controller_screen(uint32_t id, struct ivi_controller_screen *screen);
297    void controller_layer(uint32_t id);
298    void controller_surface(uint32_t id);
299    void controller_error(int32_t object_id, int32_t object_type,
300                          int32_t error_code, char const *error_text);
301
302    // surface
303    void surface_visibility(struct surface *s, int32_t visibility);
304    void surface_opacity(struct surface *s, float opacity);
305    void surface_source_rectangle(struct surface *s, int32_t x, int32_t y,
306                                  int32_t width, int32_t height);
307    void surface_destination_rectangle(struct surface *s, int32_t x, int32_t y,
308                                       int32_t width, int32_t height);
309    void surface_configuration(struct surface *s, int32_t width, int32_t height);
310    void surface_orientation(struct surface *s, int32_t orientation);
311    void surface_pixelformat(struct surface *s, int32_t pixelformat);
312    void surface_layer(struct surface *s, struct ivi_controller_layer *layer);
313    void surface_stats(struct surface *s, uint32_t redraw_count,
314                       uint32_t frame_count, uint32_t update_count, uint32_t pid,
315                       const char *process_name);
316    void surface_destroyed(struct surface *s);
317    void surface_content(struct surface *s, int32_t content_state);
318
319    // layer
320    void layer_visibility(struct layer *l, int32_t visibility);
321    void layer_opacity(struct layer *l, float opacity);
322    void layer_source_rectangle(struct layer *l, int32_t x, int32_t y,
323                                int32_t width, int32_t height);
324    void layer_destination_rectangle(struct layer *l, int32_t x, int32_t y,
325                                     int32_t width, int32_t height);
326    void layer_configuration(struct layer *l, int32_t width, int32_t height);
327    void layer_orientation(struct layer *l, int32_t orientation);
328    void layer_screen(struct layer *l, struct wl_output *screen);
329    void layer_destroyed(struct layer *l);
330 };
331 }  // namespace genivi
332
333 #endif  // !WM_WAYLAND_HPP