wayland: unique_ptr'd wayland_proxy
[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    std::unique_ptr<ProxyT, std::function<void(ProxyT *)>> proxy;
22    wayland_proxy(void *p)
23       : wayland_proxy(p, [](ProxyT *p) {
24            wl_proxy_destroy(reinterpret_cast<struct wl_proxy *>(p));
25         }) {}
26    wayland_proxy(void *p, std::function<void(ProxyT *)> p_del)
27       : proxy(std::unique_ptr<ProxyT, std::function<void(ProxyT *)>>(
28            static_cast<ProxyT *>(p), p_del)) {}
29 };
30
31 //                                                                  _
32 //  _ __   __ _ _ __ ___   ___  ___ _ __   __ _  ___ ___  __      _| |
33 // | '_ \ / _` | '_ ` _ \ / _ \/ __| '_ \ / _` |/ __/ _ \ \ \ /\ / / |
34 // | | | | (_| | | | | | |  __/\__ \ |_) | (_| | (_|  __/  \ V  V /| |
35 // |_| |_|\__,_|_| |_| |_|\___||___/ .__/ \__,_|\___\___|   \_/\_/ |_|
36 //                                 |_|
37 namespace wl {
38 //                 _     _
39 //  _ __ ___  __ _(_)___| |_ _ __ _   _
40 // | '__/ _ \/ _` | / __| __| '__| | | |
41 // | | |  __/ (_| | \__ \ |_| |  | |_| |
42 // |_|  \___|\__, |_|___/\__|_|   \__, |
43 //           |___/                |___/
44 struct registry : public wayland_proxy<struct wl_registry> {
45    typedef std::function<void(struct wl_registry *, uint32_t, uint32_t)> binder;
46    std::unordered_map<std::string, binder> bindings;
47
48    registry(struct wl_display *d);
49
50    void add_global_handler(char const *iface, binder bind);
51
52    // Events
53    void global(uint32_t name, char const *iface, uint32_t v);
54    void global_remove(uint32_t name);
55 };
56
57 //      _ _           _
58 //   __| (_)___ _ __ | | __ _ _   _
59 //  / _` | / __| '_ \| |/ _` | | | |
60 // | (_| | \__ \ |_) | | (_| | |_| |
61 //  \__,_|_|___/ .__/|_|\__,_|\__, |
62 //             |_|            |___/
63 struct display {
64    std::unique_ptr<struct wl_display, void (*)(struct wl_display *)> d;
65    struct registry r;
66
67    display();
68    bool ok() const;
69    void roundtrip();
70    int dispatch();
71    void flush();
72    int get_fd() const;
73 };
74
75 //              _               _
76 //   ___  _   _| |_ _ __  _   _| |_
77 //  / _ \| | | | __| '_ \| | | | __|
78 // | (_) | |_| | |_| |_) | |_| | |_
79 //  \___/ \__,_|\__| .__/ \__,_|\__|
80 //                 |_|
81 struct output : wayland_proxy<struct wl_output> {
82    int width{};
83    int height{};
84    int refresh{};
85
86    output(struct wl_registry *r, uint32_t name, uint32_t v);
87
88    // Events
89    void geometry(int32_t x, int32_t y, int32_t pw, int32_t ph, int32_t subpel,
90                  char const *make, char const *model, int32_t tx);
91    void mode(uint32_t flags, int32_t w, int32_t h, int32_t r);
92    void done();
93    void scale(int32_t factor);
94 };
95 }
96
97 //  _ __   __ _ _ __ ___   ___  ___ _ __   __ _  ___ ___
98 // | '_ \ / _` | '_ ` _ \ / _ \/ __| '_ \ / _` |/ __/ _ \
99 // | | | | (_| | | | | | |  __/\__ \ |_) | (_| | (_|  __/
100 // |_| |_|\__,_|_| |_| |_|\___||___/ .__/ \__,_|\___\___|
101 //                                 |_|
102 //                   _       _
103 //   __ _  ___ _ __ (_)_   _(_)
104 //  / _` |/ _ \ '_ \| \ \ / / |
105 // | (_| |  __/ | | | |\ V /| |
106 //  \__, |\___|_| |_|_| \_/ |_|
107 //  |___/
108 namespace genivi {
109
110 struct size {
111    uint32_t w, h;
112 };
113
114 struct rect {
115    uint32_t w, h;
116    int32_t x, y;
117 };
118
119 struct controller;
120
121 struct controller_child {
122    struct controller *parent;
123    uint32_t id;
124
125    controller_child(struct controller *c, uint32_t i) : parent(c), id(i) {}
126    virtual ~controller_child() {}
127 };
128
129 //                  __
130 //  ___ _   _ _ __ / _| __ _  ___ ___
131 // / __| | | | '__| |_ / _` |/ __/ _ \
132 // \__ \ |_| | |  |  _| (_| | (_|  __/
133 // |___/\__,_|_|  |_|  \__,_|\___\___|
134 //
135 struct surface : public wayland_proxy<struct ivi_controller_surface>,
136                  controller_child {
137    struct rect dst_rect;
138    struct rect src_rect;
139    struct size size;
140    int32_t orientation;
141    int32_t visibility;
142    float opacity;
143
144    surface(uint32_t i, struct controller *c);
145
146    // Requests
147    void set_visibility(uint32_t visibility);
148    void set_opacity(wl_fixed_t opacity);
149    void set_source_rectangle(int32_t x, int32_t y, int32_t width,
150                              int32_t height);
151    void set_destination_rectangle(int32_t x, int32_t y, int32_t width,
152                                   int32_t height);
153    void set_configuration(int32_t width, int32_t height);
154    void set_orientation(int32_t orientation);
155    void screenshot(const char *filename);
156    void send_stats();
157    void destroy(int32_t destroy_scene_object);
158 };
159
160 //  _
161 // | | __ _ _   _  ___ _ __
162 // | |/ _` | | | |/ _ \ '__|
163 // | | (_| | |_| |  __/ |
164 // |_|\__,_|\__, |\___|_|
165 //          |___/
166 struct layer : public wayland_proxy<struct ivi_controller_layer>,
167                controller_child {
168    struct rect dst_rect;
169    struct rect src_rect;
170    struct size size;
171    int32_t orientation;
172    int32_t visibility;
173    float opacity;
174
175    layer(uint32_t i, struct controller *c);
176    layer(uint32_t i, int32_t w, int32_t h, struct controller *c);
177
178    // Requests
179    void set_visibility(uint32_t visibility);
180    void set_opacity(wl_fixed_t opacity);
181    void set_source_rectangle(int32_t x, int32_t y, int32_t width,
182                              int32_t height);
183    void set_destination_rectangle(int32_t x, int32_t y, int32_t width,
184                                   int32_t height);
185    void set_configuration(int32_t width, int32_t height);
186    void set_orientation(int32_t orientation);
187    void screenshot(const char *filename);
188    void clear_surfaces();
189    void add_surface(struct surface *surface);
190    void remove_surface(struct surface *surface);
191    void set_render_order(std::vector<uint32_t> const &ro);
192 };
193
194 //
195 //  ___  ___ _ __ ___  ___ _ __
196 // / __|/ __| '__/ _ \/ _ \ '_ \
197 // \__ \ (__| | |  __/  __/ | | |
198 // |___/\___|_|  \___|\___|_| |_|
199 //
200 struct screen : public wayland_proxy<struct ivi_controller_screen>,
201                 controller_child {
202    screen(uint32_t i, struct controller *c, struct ivi_controller_screen *p);
203    void clear();
204    void add_layer(layer *l);
205    void set_render_order(std::vector<uint32_t> const &ro);
206 };
207
208 //                  _             _ _
209 //   ___ ___  _ __ | |_ _ __ ___ | | | ___ _ __
210 //  / __/ _ \| '_ \| __| '__/ _ \| | |/ _ \ '__|
211 // | (_| (_) | | | | |_| | | (_) | | |  __/ |
212 //  \___\___/|_| |_|\__|_|  \___/|_|_|\___|_|
213 //
214 struct controller : public wayland_proxy<struct ivi_controller> {
215    std::unordered_map<uintptr_t, uint32_t> surface_proxy_to_id;
216    std::unordered_map<uintptr_t, uint32_t> layer_proxy_to_id;
217    std::unordered_map<uintptr_t, uint32_t> screen_proxy_to_id;
218
219    std::unordered_map<uint32_t, std::unique_ptr<struct surface>> surfaces;
220    std::unordered_map<uint32_t, std::unique_ptr<struct layer>> layers;
221    std::unordered_map<uint32_t, std::unique_ptr<struct screen>> screens;
222
223    typedef std::pair<char const *, std::function<void(struct controller *)>>
224       name_task_pair;
225    std::vector<name_task_pair> pending;
226
227    size output_size;
228
229    void add_proxy_to_id_mapping(struct ivi_controller_surface *p, uint32_t id);
230    void remove_proxy_to_id_mapping(struct ivi_controller_surface *p);
231    void add_proxy_to_id_mapping(struct ivi_controller_layer *p, uint32_t id);
232    void remove_proxy_to_id_mapping(struct ivi_controller_layer *p);
233    void add_proxy_to_id_mapping(struct wl_output *p, uint32_t id);
234    void remove_proxy_to_id_mapping(struct wl_output *p);
235
236    void add_task(char const *name,
237                  std::function<void(struct controller *)> &&f);
238    void execute_pending();
239
240    controller(struct wl_registry *r, uint32_t name, uint32_t version);
241
242    // Requests
243    void commit_changes() const {
244       ivi_controller_commit_changes(this->proxy.get());
245    }
246    void layer_create(uint32_t id, int32_t w, int32_t h);
247    void surface_create(uint32_t id);
248
249    // Events
250    // controller
251    void controller_screen(uint32_t id, struct ivi_controller_screen *screen);
252    void controller_layer(uint32_t id);
253    void controller_surface(uint32_t id);
254    void controller_error(int32_t object_id, int32_t object_type,
255                          int32_t error_code, char const *error_text);
256
257    // surface
258    void surface_visibility(uint32_t id, int32_t visibility);
259    void surface_opacity(uint32_t id, float opacity);
260    void surface_source_rectangle(uint32_t id, int32_t x, int32_t y,
261                                  int32_t width, int32_t height);
262    void surface_destination_rectangle(uint32_t id, int32_t x, int32_t y,
263                                       int32_t width, int32_t height);
264    void surface_configuration(uint32_t id, int32_t width, int32_t height);
265    void surface_orientation(uint32_t id, int32_t orientation);
266    void surface_pixelformat(uint32_t id, int32_t pixelformat);
267    void surface_layer(uint32_t id, struct ivi_controller_layer *layer);
268    void surface_stats(uint32_t id, uint32_t redraw_count, uint32_t frame_count,
269                       uint32_t update_count, uint32_t pid,
270                       const char *process_name);
271    void surface_destroyed(uint32_t id);
272    void surface_content(uint32_t id, int32_t content_state);
273
274    // layer
275    void layer_visibility(uint32_t id, int32_t visibility);
276    void layer_opacity(uint32_t id, float opacity);
277    void layer_source_rectangle(uint32_t id, int32_t x, int32_t y, int32_t width,
278                                int32_t height);
279    void layer_destination_rectangle(uint32_t id, int32_t x, int32_t y,
280                                     int32_t width, int32_t height);
281    void layer_configuration(uint32_t id, int32_t width, int32_t height);
282    void layer_orientation(uint32_t id, int32_t orientation);
283    void layer_screen(uint32_t id, struct wl_output *screen);
284    void layer_destroyed(uint32_t id);
285 };
286 }
287
288 #endif  // !WM_WAYLAND_HPP