doc: shuffled around some sections, fixes.
[staging/windowmanager.git] / src / wayland.hpp
1 /*
2  * Copyright (C) 2017 Mentor Graphics Development (Deutschland) GmbH
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #ifndef WM_WAYLAND_HPP
18 #define WM_WAYLAND_HPP
19
20 #include "controller_hooks.hpp"
21 #include "ivi-controller-client-protocol.h"
22 #include "util.hpp"
23
24 #include <functional>
25 #include <memory>
26 #include <unordered_map>
27 #include <vector>
28
29 //                      _                 _
30 // __      ____ _ _   _| | __ _ _ __   __| |     _ __  _ __ _____  ___   _
31 // \ \ /\ / / _` | | | | |/ _` | '_ \ / _` |    | '_ \| '__/ _ \ \/ / | | |
32 //  \ V  V / (_| | |_| | | (_| | | | | (_| |    | |_) | | | (_) >  <| |_| |
33 //   \_/\_/ \__,_|\__, |_|\__,_|_| |_|\__,_|____| .__/|_|  \___/_/\_\\__, |
34 //                |___/                   |_____|_|                  |___/
35 template <typename ProxyT>
36 struct wayland_proxy {
37    std::unique_ptr<ProxyT, std::function<void(ProxyT *)>> proxy;
38    wayland_proxy(wayland_proxy const &) = delete;
39    wayland_proxy &operator=(wayland_proxy const &) = delete;
40    wayland_proxy(void *p)
41       : wayland_proxy(p,
42                       reinterpret_cast<void (*)(ProxyT *)>(wl_proxy_destroy)) {}
43    wayland_proxy(void *p, std::function<void(ProxyT *)> &&p_del)
44       : proxy(std::unique_ptr<ProxyT, std::function<void(ProxyT *)>>(
45            static_cast<ProxyT *>(p), p_del)) {}
46 };
47
48 //                                                                  _
49 //  _ __   __ _ _ __ ___   ___  ___ _ __   __ _  ___ ___  __      _| |
50 // | '_ \ / _` | '_ ` _ \ / _ \/ __| '_ \ / _` |/ __/ _ \ \ \ /\ / / |
51 // | | | | (_| | | | | | |  __/\__ \ |_) | (_| | (_|  __/  \ V  V /| |
52 // |_| |_|\__,_|_| |_| |_|\___||___/ .__/ \__,_|\___\___|   \_/\_/ |_|
53 //                                 |_|
54 namespace wl {
55 //                 _     _
56 //  _ __ ___  __ _(_)___| |_ _ __ _   _
57 // | '__/ _ \/ _` | / __| __| '__| | | |
58 // | | |  __/ (_| | \__ \ |_| |  | |_| |
59 // |_|  \___|\__, |_|___/\__|_|   \__, |
60 //           |___/                |___/
61 struct registry : public wayland_proxy<struct wl_registry> {
62    typedef std::function<void(struct wl_registry *, uint32_t, uint32_t)> binder;
63    std::unordered_map<std::string, binder> bindings;
64
65    registry(registry const &) = delete;
66    registry &operator=(registry const &) = delete;
67    registry(struct wl_display *d);
68
69    void add_global_handler(char const *iface, binder bind);
70
71    // Events
72    void global(uint32_t name, char const *iface, uint32_t v);
73    void global_remove(uint32_t name);
74 };
75
76 //      _ _           _
77 //   __| (_)___ _ __ | | __ _ _   _
78 //  / _` | / __| '_ \| |/ _` | | | |
79 // | (_| | \__ \ |_) | | (_| | |_| |
80 //  \__,_|_|___/ .__/|_|\__,_|\__, |
81 //             |_|            |___/
82 struct display {
83    std::unique_ptr<struct wl_display, void (*)(struct wl_display *)> d;
84    struct registry r;
85
86    display(display const &) = delete;
87    display &operator=(display const &) = delete;
88    display();
89    bool ok() const;
90    void roundtrip();
91    int dispatch();
92    int dispatch_pending();
93    int read_events();
94    void flush();
95    int get_fd() const;
96    int get_error();
97
98    // Lets just proxy this for the registry
99    inline void add_global_handler(char const *iface, registry::binder bind) {
100       this->r.add_global_handler(iface, bind);
101    }
102 };
103
104 //              _               _
105 //   ___  _   _| |_ _ __  _   _| |_
106 //  / _ \| | | | __| '_ \| | | | __|
107 // | (_) | |_| | |_| |_) | |_| | |_
108 //  \___/ \__,_|\__| .__/ \__,_|\__|
109 //                 |_|
110 struct output : wayland_proxy<struct wl_output> {
111    int width{};
112    int height{};
113    int refresh{};
114    int transform{};
115
116    output(output const &) = delete;
117    output &operator=(output const &) = delete;
118    output(struct wl_registry *r, uint32_t name, uint32_t v);
119
120    // Events
121    void geometry(int32_t x, int32_t y, int32_t pw, int32_t ph, int32_t subpel,
122                  char const *make, char const *model, int32_t tx);
123    void mode(uint32_t flags, int32_t w, int32_t h, int32_t r);
124    void done();
125    void scale(int32_t factor);
126 };
127 }  // namespace wl
128
129 //  _ __   __ _ _ __ ___   ___  ___ _ __   __ _  ___ ___
130 // | '_ \ / _` | '_ ` _ \ / _ \/ __| '_ \ / _` |/ __/ _ \
131 // | | | | (_| | | | | | |  __/\__ \ |_) | (_| | (_|  __/
132 // |_| |_|\__,_|_| |_| |_|\___||___/ .__/ \__,_|\___\___|
133 //                                 |_|
134 //                   _       _
135 //   __ _  ___ _ __ (_)_   _(_)
136 //  / _` |/ _ \ '_ \| \ \ / / |
137 // | (_| |  __/ | | | |\ V /| |
138 //  \__, |\___|_| |_|_| \_/ |_|
139 //  |___/
140 namespace genivi {
141
142 struct size {
143    uint32_t w, h;
144 };
145
146 struct rect {
147    int32_t w, h;
148    int32_t x, y;
149 };
150
151 static const constexpr rect full_rect = rect{-1, -1, 0, 0};
152
153 inline bool operator==(struct rect a, struct rect b) {
154    return a.w == b.w && a.h == b.h && a.x == b.x && a.y == b.y;
155 }
156
157 struct controller;
158
159 struct controller_child {
160    struct controller *parent;
161    uint32_t id;
162
163    controller_child(controller_child const &) = delete;
164    controller_child &operator=(controller_child const &) = delete;
165    controller_child(struct controller *c, uint32_t i) : parent(c), id(i) {}
166    virtual ~controller_child() {}
167 };
168
169 struct surface_properties {
170    uint32_t id;  // let's just save an ID here too
171    struct rect dst_rect;
172    struct rect src_rect;
173    struct size size;
174    int32_t orientation;
175    int32_t visibility;
176    float opacity;
177 };
178
179 //                  __
180 //  ___ _   _ _ __ / _| __ _  ___ ___
181 // / __| | | | '__| |_ / _` |/ __/ _ \
182 // \__ \ |_| | |  |  _| (_| | (_|  __/
183 // |___/\__,_|_|  |_|  \__,_|\___\___|
184 //
185 struct surface : public wayland_proxy<struct ivi_controller_surface>,
186                  controller_child {
187    surface(surface const &) = delete;
188    surface &operator=(surface const &) = delete;
189    surface(uint32_t i, 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 send_stats();
202    void destroy(int32_t destroy_scene_object);
203 };
204
205 //  _
206 // | | __ _ _   _  ___ _ __
207 // | |/ _` | | | |/ _ \ '__|
208 // | | (_| | |_| |  __/ |
209 // |_|\__,_|\__, |\___|_|
210 //          |___/
211 struct layer : public wayland_proxy<struct ivi_controller_layer>,
212                controller_child {
213    layer(layer const &) = delete;
214    layer &operator=(layer const &) = delete;
215    layer(uint32_t i, struct controller *c);
216    layer(uint32_t i, int32_t w, int32_t h, struct controller *c);
217
218    // Requests
219    void set_visibility(uint32_t visibility);
220    void set_opacity(wl_fixed_t opacity);
221    void set_source_rectangle(int32_t x, int32_t y, int32_t width,
222                              int32_t height);
223    void set_destination_rectangle(int32_t x, int32_t y, int32_t width,
224                                   int32_t height);
225    void set_configuration(int32_t width, int32_t height);
226    void set_orientation(int32_t orientation);
227    void screenshot(const char *filename);
228    void clear_surfaces();
229    void add_surface(struct surface *surface);
230    void remove_surface(struct surface *surface);
231    void set_render_order(std::vector<uint32_t> const &ro);
232 };
233
234 //
235 //  ___  ___ _ __ ___  ___ _ __
236 // / __|/ __| '__/ _ \/ _ \ '_ \
237 // \__ \ (__| | |  __/  __/ | | |
238 // |___/\___|_|  \___|\___|_| |_|
239 //
240 struct screen : public wayland_proxy<struct ivi_controller_screen>,
241                 controller_child {
242    screen(screen const &) = delete;
243    screen &operator=(screen const &) = delete;
244    screen(uint32_t i, struct controller *c, struct ivi_controller_screen *p);
245    void clear();
246    void add_layer(layer *l);
247    void set_render_order(std::vector<uint32_t> const &ro);
248 };
249
250 //                  _             _ _
251 //   ___ ___  _ __ | |_ _ __ ___ | | | ___ _ __
252 //  / __/ _ \| '_ \| __| '__/ _ \| | |/ _ \ '__|
253 // | (_| (_) | | | | |_| | | (_) | | |  __/ |
254 //  \___\___/|_| |_|\__|_|  \___/|_|_|\___|_|
255 //
256 struct controller : public wayland_proxy<struct ivi_controller> {
257    // This controller is still missing ivi-input
258
259    typedef std::unordered_map<uintptr_t, uint32_t> proxy_to_id_map_type;
260    typedef std::unordered_map<uint32_t, std::unique_ptr<struct surface>>
261       surface_map_type;
262    typedef std::unordered_map<uint32_t, std::unique_ptr<struct layer>>
263       layer_map_type;
264    typedef std::unordered_map<uint32_t, std::unique_ptr<struct screen>>
265       screen_map_type;
266
267    typedef std::unordered_map<uint32_t, struct surface_properties> props_map;
268
269    // HACK:
270    // The order of these member is mandatory, as when objects are destroyed
271    // they will call their parent (that's us right here!) and remove their
272    // proxy-to-id mapping. I.e. the *_proxy_to_id members need to be valid
273    // when the surfaces/layers/screens maps are destroyed. This sucks, but
274    // I cannot see a better solution w/o globals or some other horrible
275    // call-our-parent construct.
276    proxy_to_id_map_type surface_proxy_to_id;
277    proxy_to_id_map_type layer_proxy_to_id;
278    proxy_to_id_map_type screen_proxy_to_id;
279
280    props_map sprops;
281    props_map lprops;
282
283    surface_map_type surfaces;
284    layer_map_type layers;
285    screen_map_type screens;
286
287    size output_size;
288
289    wm::controller_hooks *chooks;
290
291    void add_proxy_to_id_mapping(struct ivi_controller_surface *p, uint32_t id);
292    void remove_proxy_to_id_mapping(struct ivi_controller_surface *p);
293    void add_proxy_to_id_mapping(struct ivi_controller_layer *p, uint32_t id);
294    void remove_proxy_to_id_mapping(struct ivi_controller_layer *p);
295    void add_proxy_to_id_mapping(struct wl_output *p, uint32_t id);
296    void remove_proxy_to_id_mapping(struct wl_output *p);
297
298    bool surface_exists(uint32_t id) const {
299       return this->surfaces.find(id) != this->surfaces.end();
300    }
301
302    bool layer_exists(uint32_t id) const {
303       return this->layers.find(id) != this->layers.end();
304    }
305
306    controller(struct wl_registry *r, uint32_t name, uint32_t version);
307
308    // Requests
309    void commit_changes() const {
310       ivi_controller_commit_changes(this->proxy.get());
311    }
312    void layer_create(uint32_t id, int32_t w, int32_t h);
313    void surface_create(uint32_t id);
314
315    // Events
316    // controller
317    void controller_screen(uint32_t id, struct ivi_controller_screen *screen);
318    void controller_layer(uint32_t id);
319    void controller_surface(uint32_t id);
320    void controller_error(int32_t object_id, int32_t object_type,
321                          int32_t error_code, char const *error_text);
322
323    // surface
324    void surface_visibility(struct surface *s, int32_t visibility);
325    void surface_opacity(struct surface *s, float opacity);
326    void surface_source_rectangle(struct surface *s, int32_t x, int32_t y,
327                                  int32_t width, int32_t height);
328    void surface_destination_rectangle(struct surface *s, int32_t x, int32_t y,
329                                       int32_t width, int32_t height);
330    void surface_configuration(struct surface *s, int32_t width, int32_t height);
331    void surface_orientation(struct surface *s, int32_t orientation);
332    void surface_pixelformat(struct surface *s, int32_t pixelformat);
333    void surface_layer(struct surface *s, struct ivi_controller_layer *layer);
334    void surface_stats(struct surface *s, uint32_t redraw_count,
335                       uint32_t frame_count, uint32_t update_count, uint32_t pid,
336                       const char *process_name);
337    void surface_destroyed(struct surface *s);
338    void surface_content(struct surface *s, int32_t content_state);
339
340    // layer
341    void layer_visibility(struct layer *l, int32_t visibility);
342    void layer_opacity(struct layer *l, float opacity);
343    void layer_source_rectangle(struct layer *l, int32_t x, int32_t y,
344                                int32_t width, int32_t height);
345    void layer_destination_rectangle(struct layer *l, int32_t x, int32_t y,
346                                     int32_t width, int32_t height);
347    void layer_configuration(struct layer *l, int32_t width, int32_t height);
348    void layer_orientation(struct layer *l, int32_t orientation);
349    void layer_screen(struct layer *l, struct wl_output *screen);
350    void layer_destroyed(struct layer *l);
351 };
352 }  // namespace genivi
353
354 #endif  // !WM_WAYLAND_HPP