main/wayland: added a simple main loop
[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 <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 struct registry;
43
44 //      _ _           _
45 //   __| (_)___ _ __ | | __ _ _   _
46 //  / _` | / __| '_ \| |/ _` | | | |
47 // | (_| | \__ \ |_) | | (_| | |_| |
48 //  \__,_|_|___/ .__/|_|\__,_|\__, |
49 //             |_|            |___/
50 struct display {
51    std::unique_ptr<struct wl_display, std::function<void(struct wl_display *)>>
52       d;
53    std::unique_ptr<struct registry> r;
54
55    display();
56    ~display();
57    bool ok() const;
58    void roundtrip();
59    int dispatch();
60    void flush();
61    int get_fd() const;
62 };
63
64 //                 _     _
65 //  _ __ ___  __ _(_)___| |_ _ __ _   _
66 // | '__/ _ \/ _` | / __| __| '__| | | |
67 // | | |  __/ (_| | \__ \ |_| |  | |_| |
68 // |_|  \___|\__, |_|___/\__|_|   \__, |
69 //           |___/                |___/
70 struct registry : public wayland_proxy<struct wl_registry> {
71    typedef std::function<void(struct wl_registry *, uint32_t, uint32_t)> binder;
72    std::map<std::string, binder> bindings;
73
74    registry(struct wl_display *d);
75    ~registry();
76
77    void add_global_handler(char const *iface, binder bind);
78
79    // Events
80    void global(uint32_t name, char const *iface, uint32_t v);
81    void global_remove(uint32_t name);
82 };
83
84 //              _               _
85 //   ___  _   _| |_ _ __  _   _| |_
86 //  / _ \| | | | __| '_ \| | | | __|
87 // | (_) | |_| | |_| |_) | |_| | |_
88 //  \___/ \__,_|\__| .__/ \__,_|\__|
89 //                 |_|
90 struct output : wayland_proxy<struct wl_output> {
91    output(struct wl_registry *registry, uint32_t name, uint32_t version);
92
93    // Events
94    void geometry(int32_t x, int32_t y, int32_t pw, int32_t ph, int32_t subpel,
95                  char const *make, char const *model, int32_t tx);
96    void mode(uint32_t flags, int32_t w, int32_t h, int32_t r);
97    void done();
98    void scale(int32_t factor);
99 };
100 }
101
102 //  _ __   __ _ _ __ ___   ___  ___ _ __   __ _  ___ ___
103 // | '_ \ / _` | '_ ` _ \ / _ \/ __| '_ \ / _` |/ __/ _ \
104 // | | | | (_| | | | | | |  __/\__ \ |_) | (_| | (_|  __/
105 // |_| |_|\__,_|_| |_| |_|\___||___/ .__/ \__,_|\___\___|
106 //                                 |_|
107 //                   _       _
108 //   __ _  ___ _ __ (_)_   _(_)
109 //  / _` |/ _ \ '_ \| \ \ / / |
110 // | (_| |  __/ | | | |\ V /| |
111 //  \__, |\___|_| |_|_| \_/ |_|
112 //  |___/
113 namespace genivi {
114
115 struct controller;
116
117 struct controlled_entity {
118    struct controller *parent;
119    uint32_t id;
120
121    controlled_entity(struct controller *c, uint32_t i) : parent(c), id(i) {}
122    virtual ~controlled_entity() {}
123 };
124
125 //                  __
126 //  ___ _   _ _ __ / _| __ _  ___ ___
127 // / __| | | | '__| |_ / _` |/ __/ _ \
128 // \__ \ |_| | |  |  _| (_| | (_|  __/
129 // |___/\__,_|_|  |_|  \__,_|\___\___|
130 //
131 struct surface : public wayland_proxy<struct ivi_controller_surface>,
132                  controlled_entity {
133    surface(uint32_t i, struct controller *c);
134    ~surface() override;
135
136    // Requests
137    inline void set_visibility(uint32_t visibility) {
138       ivi_controller_surface_set_visibility(this->proxy, visibility);
139    }
140
141    inline void set_opacity(wl_fixed_t opacity) {
142       ivi_controller_surface_set_opacity(this->proxy, opacity);
143    }
144
145    inline void set_source_rectangle(int32_t x, int32_t y, int32_t width,
146                                     int32_t height) {
147       ivi_controller_surface_set_source_rectangle(this->proxy, x, y, width,
148                                                   height);
149    }
150
151    inline void set_destination_rectangle(int32_t x, int32_t y, int32_t width,
152                                          int32_t height) {
153       ivi_controller_surface_set_destination_rectangle(this->proxy, x, y, width,
154                                                        height);
155    }
156
157    inline void set_configuration(int32_t width, int32_t height) {
158       ivi_controller_surface_set_configuration(this->proxy, width, height);
159    }
160
161    inline void set_orientation(int32_t orientation) {
162       ivi_controller_surface_set_orientation(this->proxy, orientation);
163    }
164
165    inline void screenshot(const char *filename) {
166       ivi_controller_surface_screenshot(this->proxy, filename);
167    }
168
169    inline void send_stats() { ivi_controller_surface_send_stats(this->proxy); }
170
171    inline void destroy(int32_t destroy_scene_object) {
172       ivi_controller_surface_destroy(this->proxy, destroy_scene_object);
173    }
174 };
175
176 //  _
177 // | | __ _ _   _  ___ _ __
178 // | |/ _` | | | |/ _ \ '__|
179 // | | (_| | |_| |  __/ |
180 // |_|\__,_|\__, |\___|_|
181 //          |___/
182 struct layer : public wayland_proxy<struct ivi_controller_layer>,
183                controlled_entity {
184    layer(uint32_t i, struct controller *c);
185    ~layer() override;
186
187    // Requests
188    inline void set_visibility(uint32_t visibility) {
189       ivi_controller_layer_set_visibility(this->proxy, visibility);
190    }
191
192    inline void set_opacity(wl_fixed_t opacity) {
193       ivi_controller_layer_set_opacity(this->proxy, opacity);
194    }
195
196    inline void set_source_rectangle(int32_t x, int32_t y, int32_t width,
197                                     int32_t height) {
198       ivi_controller_layer_set_source_rectangle(this->proxy, x, y, width,
199                                                 height);
200    }
201
202    inline void set_destination_rectangle(int32_t x, int32_t y, int32_t width,
203                                          int32_t height) {
204       ivi_controller_layer_set_destination_rectangle(this->proxy, x, y, width,
205                                                      height);
206    }
207
208    inline void set_configuration(int32_t width, int32_t height) {
209       ivi_controller_layer_set_configuration(this->proxy, width, height);
210    }
211
212    inline void set_orientation(int32_t orientation) {
213       ivi_controller_layer_set_orientation(this->proxy, orientation);
214    }
215
216    inline void screenshot(const char *filename) {
217       ivi_controller_layer_screenshot(this->proxy, filename);
218    }
219
220    inline void clear_surfaces() {
221       ivi_controller_layer_clear_surfaces(this->proxy);
222    }
223
224    inline void add_surface(struct surface *surface) {
225       ivi_controller_layer_add_surface(this->proxy, surface->proxy);
226    }
227
228    inline void remove_surface(struct surface *surface) {
229       ivi_controller_layer_remove_surface(this->proxy, surface->proxy);
230    }
231
232    inline void set_render_order(struct wl_array *surfaces) {
233       ivi_controller_layer_set_render_order(this->proxy, surfaces);
234    }
235 };
236
237 //
238 //  ___  ___ _ __ ___  ___ _ __
239 // / __|/ __| '__/ _ \/ _ \ '_ \
240 // \__ \ (__| | |  __/  __/ | | |
241 // |___/\___|_|  \___|\___|_| |_|
242 //
243 struct screen : public wayland_proxy<struct ivi_controller_screen>,
244                 controlled_entity {
245    screen(uint32_t i, struct controller *c, struct ivi_controller_screen *p);
246 };
247
248 //                  _             _ _
249 //   ___ ___  _ __ | |_ _ __ ___ | | | ___ _ __
250 //  / __/ _ \| '_ \| __| '__/ _ \| | |/ _ \ '__|
251 // | (_| (_) | | | | |_| | | (_) | | |  __/ |
252 //  \___\___/|_| |_|\__|_|  \___/|_|_|\___|_|
253 //
254 struct controller : public wayland_proxy<struct ivi_controller> {
255    std::map<uint32_t, std::unique_ptr<struct surface>> surfaces;
256    std::map<uint32_t, std::unique_ptr<struct layer>> layers;
257    std::map<uint32_t, std::unique_ptr<struct screen>> screens;
258
259    controller(struct wl_registry *r, uint32_t name, uint32_t version);
260    ~controller() override;
261
262    // Events
263    // controller
264    void controller_screen(uint32_t id, struct ivi_controller_screen *screen);
265    void controller_layer(uint32_t id);
266    void controller_surface(uint32_t id);
267    void controller_error(int32_t oid, int32_t otype, int32_t code,
268                          char const *text);
269
270    // surface
271    void surface_visibility(uint32_t id, int32_t visibility);
272    void surface_opacity(uint32_t id, float opacity);
273    void surface_source_rectangle(uint32_t id, int32_t x, int32_t y,
274                                  int32_t width, int32_t height);
275    void surface_destination_rectangle(uint32_t id, int32_t x, int32_t y,
276                                       int32_t width, int32_t height);
277    void surface_configuration(uint32_t id, int32_t width, int32_t height);
278    void surface_orientation(uint32_t id, int32_t orientation);
279    void surface_pixelformat(uint32_t id, int32_t pixelformat);
280    void surface_layer(uint32_t id, struct ivi_controller_layer *layer);
281    void surface_stats(uint32_t id, uint32_t redraw_count, uint32_t frame_count,
282                       uint32_t update_count, uint32_t pid,
283                       const char *process_name);
284    void surface_destroyed(uint32_t id);
285    void surface_content(uint32_t id, int32_t content_state);
286
287    // layer
288    void layer_visibility(uint32_t id, int32_t visibility);
289    void layer_opacity(uint32_t id, float opacity);
290    void layer_source_rectangle(uint32_t id, int32_t x, int32_t y, int32_t width,
291                                int32_t height);
292    void layer_destination_rectangle(uint32_t id, int32_t x, int32_t y,
293                                     int32_t width, int32_t height);
294    void layer_configuration(uint32_t id, int32_t width, int32_t height);
295    void layer_orientation(uint32_t id, int32_t orientation);
296    void layer_screen(uint32_t id, struct wl_output *screen);
297    void layer_destroyed(uint32_t id);
298 };
299 }
300
301 #endif  // !WM_WAYLAND_HPP