wayland: moved screen 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    void clear();
212    void add_layer(layer *l);
213    void set_render_order(std::vector<uint32_t> const &ro);
214 };
215
216 //                  _             _ _
217 //   ___ ___  _ __ | |_ _ __ ___ | | | ___ _ __
218 //  / __/ _ \| '_ \| __| '__/ _ \| | |/ _ \ '__|
219 // | (_| (_) | | | | |_| | | (_) | | |  __/ |
220 //  \___\___/|_| |_|\__|_|  \___/|_|_|\___|_|
221 //
222 struct controller : public wayland_proxy<struct ivi_controller> {
223    std::unordered_map<uintptr_t, uint32_t> surface_proxy_to_id;
224    std::unordered_map<uint32_t, std::unique_ptr<struct surface>> surfaces;
225    std::unordered_map<uintptr_t, uint32_t> layer_proxy_to_id;
226    std::unordered_map<uint32_t, std::unique_ptr<struct layer>> layers;
227    std::unordered_map<uintptr_t, uint32_t> screen_proxy_to_id;
228    std::unordered_map<uint32_t, std::unique_ptr<struct screen>> screens;
229
230    typedef std::pair<char const *, std::function<void(struct controller *)>>
231       name_task_pair;
232    std::vector<name_task_pair> pending;
233
234    size output_size;
235
236    void add_proxy_to_id_mapping(struct ivi_controller_surface *p, uint32_t id);
237    void remove_proxy_to_id_mapping(struct ivi_controller_surface *p);
238    void add_proxy_to_id_mapping(struct ivi_controller_layer *p, uint32_t id);
239    void remove_proxy_to_id_mapping(struct ivi_controller_layer *p);
240    void add_proxy_to_id_mapping(struct wl_output *p, uint32_t id);
241    void remove_proxy_to_id_mapping(struct wl_output *p);
242
243    void add_task(char const *name, std::function<void(struct controller *)> &&f);
244    void execute_pending();
245
246    controller(struct wl_registry *r, uint32_t name, uint32_t version);
247    ~controller() override;
248
249    // Requests
250    void commit_changes() const { ivi_controller_commit_changes(this->proxy); }
251    void layer_create(uint32_t id, int32_t w, int32_t h);
252    void surface_create(uint32_t id);
253
254    // Events
255    // controller
256    void controller_screen(uint32_t id, struct ivi_controller_screen *screen);
257    void controller_layer(uint32_t id);
258    void controller_surface(uint32_t id);
259    void controller_error(int32_t object_id, int32_t object_type,
260                          int32_t error_code, char const *error_text);
261
262    // surface
263    void surface_visibility(uint32_t id, int32_t visibility);
264    void surface_opacity(uint32_t id, float opacity);
265    void surface_source_rectangle(uint32_t id, int32_t x, int32_t y,
266                                  int32_t width, int32_t height);
267    void surface_destination_rectangle(uint32_t id, int32_t x, int32_t y,
268                                       int32_t width, int32_t height);
269    void surface_configuration(uint32_t id, int32_t width, int32_t height);
270    void surface_orientation(uint32_t id, int32_t orientation);
271    void surface_pixelformat(uint32_t id, int32_t pixelformat);
272    void surface_layer(uint32_t id, struct ivi_controller_layer *layer);
273    void surface_stats(uint32_t id, uint32_t redraw_count, uint32_t frame_count,
274                       uint32_t update_count, uint32_t pid,
275                       const char *process_name);
276    void surface_destroyed(uint32_t id);
277    void surface_content(uint32_t id, int32_t content_state);
278
279    // layer
280    void layer_visibility(uint32_t id, int32_t visibility);
281    void layer_opacity(uint32_t id, float opacity);
282    void layer_source_rectangle(uint32_t id, int32_t x, int32_t y, int32_t width,
283                                int32_t height);
284    void layer_destination_rectangle(uint32_t id, int32_t x, int32_t y,
285                                     int32_t width, int32_t height);
286    void layer_configuration(uint32_t id, int32_t width, int32_t height);
287    void layer_orientation(uint32_t id, int32_t orientation);
288    void layer_screen(uint32_t id, struct wl_output *screen);
289    void layer_destroyed(uint32_t id);
290 };
291 }
292
293 #endif  // !WM_WAYLAND_HPP