wayland: moved inline methods to .cpp
[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 *r, uint32_t name, uint32_t v);
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    void set_visibility(uint32_t visibility);
155    void set_opacity(wl_fixed_t opacity);
156    void set_source_rectangle(int32_t x, int32_t y, int32_t width,
157                                     int32_t height);
158    void set_destination_rectangle(int32_t x, int32_t y, int32_t width,
159                                          int32_t height);
160    void set_configuration(int32_t width, int32_t height);
161    void set_orientation(int32_t orientation);
162    void screenshot(const char *filename);
163    void send_stats();
164    void destroy(int32_t destroy_scene_object);
165 };
166
167 //  _
168 // | | __ _ _   _  ___ _ __
169 // | |/ _` | | | |/ _ \ '__|
170 // | | (_| | |_| |  __/ |
171 // |_|\__,_|\__, |\___|_|
172 //          |___/
173 struct layer : public wayland_proxy<struct ivi_controller_layer>,
174                controller_child {
175    struct rect dst_rect;
176    struct rect src_rect;
177    struct size size;
178    int32_t orientation;
179    int32_t visibility;
180    float opacity;
181
182    layer(uint32_t i, struct controller *c);
183    layer(uint32_t i, int32_t w, int32_t h, struct controller *c);
184    ~layer() override;
185
186    // Requests
187    void set_visibility(uint32_t visibility);
188    void set_opacity(wl_fixed_t opacity);
189    void set_source_rectangle(int32_t x, int32_t y, int32_t width,
190                              int32_t height);
191    void set_destination_rectangle(int32_t x, int32_t y, int32_t width,
192                                   int32_t height);
193    void set_configuration(int32_t width, int32_t height);
194    void set_orientation(int32_t orientation);
195    void screenshot(const char *filename);
196    void clear_surfaces();
197    void add_surface(struct surface *surface);
198    void remove_surface(struct surface *surface);
199    void set_render_order(std::vector<uint32_t> const &ro);
200 };
201
202 //
203 //  ___  ___ _ __ ___  ___ _ __
204 // / __|/ __| '__/ _ \/ _ \ '_ \
205 // \__ \ (__| | |  __/  __/ | | |
206 // |___/\___|_|  \___|\___|_| |_|
207 //
208 struct screen : public wayland_proxy<struct ivi_controller_screen>,
209                 controller_child {
210    screen(uint32_t i, struct controller *c, struct ivi_controller_screen *p);
211
212    void clear() { ivi_controller_screen_clear(this->proxy); }
213    void add_layer(layer *l) {
214       ivi_controller_screen_add_layer(this->proxy, l->proxy);
215    }
216    void set_render_order(std::vector<uint32_t> const &ro) {
217       struct wl_array wlro {
218          .size = ro.size() * sizeof(ro[0]),
219          .alloc = ro.capacity() * sizeof(ro[0]),
220          .data = const_cast<void *>(static_cast<void const *>(ro.data()))
221       };
222       ivi_controller_screen_set_render_order(this->proxy, &wlro);
223    }
224 };
225
226 //                  _             _ _
227 //   ___ ___  _ __ | |_ _ __ ___ | | | ___ _ __
228 //  / __/ _ \| '_ \| __| '__/ _ \| | |/ _ \ '__|
229 // | (_| (_) | | | | |_| | | (_) | | |  __/ |
230 //  \___\___/|_| |_|\__|_|  \___/|_|_|\___|_|
231 //
232 struct controller : public wayland_proxy<struct ivi_controller> {
233    std::unordered_map<uintptr_t, uint32_t> surface_proxy_to_id;
234    std::unordered_map<uint32_t, std::unique_ptr<struct surface>> surfaces;
235    std::unordered_map<uintptr_t, uint32_t> layer_proxy_to_id;
236    std::unordered_map<uint32_t, std::unique_ptr<struct layer>> layers;
237    std::unordered_map<uintptr_t, uint32_t> screen_proxy_to_id;
238    std::unordered_map<uint32_t, std::unique_ptr<struct screen>> screens;
239
240    typedef std::pair<char const *, std::function<void(struct controller *)>>
241       name_task_pair;
242    std::vector<name_task_pair> pending;
243
244    size output_size;
245
246    void add_proxy_to_id_mapping(struct ivi_controller_surface *p, uint32_t id);
247    void remove_proxy_to_id_mapping(struct ivi_controller_surface *p);
248    void add_proxy_to_id_mapping(struct ivi_controller_layer *p, uint32_t id);
249    void remove_proxy_to_id_mapping(struct ivi_controller_layer *p);
250    void add_proxy_to_id_mapping(struct wl_output *p, uint32_t id);
251    void remove_proxy_to_id_mapping(struct wl_output *p);
252
253    void add_task(char const *name, std::function<void(struct controller *)> &&f);
254    void execute_pending();
255
256    controller(struct wl_registry *r, uint32_t name, uint32_t version);
257    ~controller() override;
258
259    // Requests
260    void commit_changes() const { ivi_controller_commit_changes(this->proxy); }
261    void layer_create(uint32_t id, int32_t w, int32_t h);
262    void surface_create(uint32_t id);
263
264    // Events
265    // controller
266    void controller_screen(uint32_t id, struct ivi_controller_screen *screen);
267    void controller_layer(uint32_t id);
268    void controller_surface(uint32_t id);
269    void controller_error(int32_t object_id, int32_t object_type,
270                          int32_t error_code, char const *error_text);
271
272    // surface
273    void surface_visibility(uint32_t id, int32_t visibility);
274    void surface_opacity(uint32_t id, float opacity);
275    void surface_source_rectangle(uint32_t id, int32_t x, int32_t y,
276                                  int32_t width, int32_t height);
277    void surface_destination_rectangle(uint32_t id, int32_t x, int32_t y,
278                                       int32_t width, int32_t height);
279    void surface_configuration(uint32_t id, int32_t width, int32_t height);
280    void surface_orientation(uint32_t id, int32_t orientation);
281    void surface_pixelformat(uint32_t id, int32_t pixelformat);
282    void surface_layer(uint32_t id, struct ivi_controller_layer *layer);
283    void surface_stats(uint32_t id, uint32_t redraw_count, uint32_t frame_count,
284                       uint32_t update_count, uint32_t pid,
285                       const char *process_name);
286    void surface_destroyed(uint32_t id);
287    void surface_content(uint32_t id, int32_t content_state);
288
289    // layer
290    void layer_visibility(uint32_t id, int32_t visibility);
291    void layer_opacity(uint32_t id, float opacity);
292    void layer_source_rectangle(uint32_t id, int32_t x, int32_t y, int32_t width,
293                                int32_t height);
294    void layer_destination_rectangle(uint32_t id, int32_t x, int32_t y,
295                                     int32_t width, int32_t height);
296    void layer_configuration(uint32_t id, int32_t width, int32_t height);
297    void layer_orientation(uint32_t id, int32_t orientation);
298    void layer_screen(uint32_t id, struct wl_output *screen);
299    void layer_destroyed(uint32_t id);
300 };
301 }
302
303 #endif  // !WM_WAYLAND_HPP