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