872e287360decaa0817afcb73728bbb26275a76f
[apps/agl-service-windowmanager-2017.git] / src / wayland_ivi_wm.hpp
1 /*
2  * Copyright (c) 2017 TOYOTA MOTOR CORPORATION
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-wm-client-protocol.h"
22 #include "util.hpp"
23
24 #include <functional>
25 #include <memory>
26 #include <unordered_map>
27 #include <vector>
28
29 /**
30  * @struct wayland_proxy
31  */
32 template <typename ProxyT>
33 struct wayland_proxy {
34    std::unique_ptr<ProxyT, std::function<void(ProxyT *)>> proxy;
35    wayland_proxy(wayland_proxy const &) = delete;
36    wayland_proxy &operator=(wayland_proxy const &) = delete;
37    wayland_proxy(void *p)
38       : wayland_proxy(p,
39                       reinterpret_cast<void (*)(ProxyT *)>(wl_proxy_destroy)) {}
40    wayland_proxy(void *p, std::function<void(ProxyT *)> &&p_del)
41       : proxy(std::unique_ptr<ProxyT, std::function<void(ProxyT *)>>(
42            static_cast<ProxyT *>(p), p_del)) {}
43 };
44
45 /**
46  * namespace wl
47  */
48 namespace wl {
49
50 /**
51  * @struct registry
52  */
53 struct registry : public wayland_proxy<struct wl_registry> {
54    typedef std::function<void(struct wl_registry *, uint32_t, uint32_t)> binder;
55    std::unordered_map<std::string, binder> bindings;
56
57    registry(registry const &) = delete;
58    registry &operator=(registry const &) = delete;
59    registry(struct wl_display *d);
60
61    void add_global_handler(char const *iface, binder bind);
62
63    // Events
64    void global_created(uint32_t name, char const *iface, uint32_t v);
65    void global_removed(uint32_t name);
66 };
67
68 /**
69  * @struct display
70  */
71 struct display {
72    std::unique_ptr<struct wl_display, void (*)(struct wl_display *)> d;
73    struct registry r;
74
75    display(display const &) = delete;
76    display &operator=(display const &) = delete;
77    display();
78    bool ok() const;
79    void roundtrip();
80    int dispatch();
81    int dispatch_pending();
82    int read_events();
83    void flush();
84    int get_fd() const;
85    int get_error();
86
87    // Lets just proxy this for the registry
88    inline void add_global_handler(char const *iface, registry::binder bind) {
89       this->r.add_global_handler(iface, bind);
90    }
91 };
92
93 /**
94  * @struct output
95  */
96 struct output : public wayland_proxy<struct wl_output> {
97    int width{};
98    int height{};
99    int physical_width{};
100    int physical_height{};
101    int refresh{};
102    int transform{};
103
104    output(output const &) = delete;
105    output &operator=(output const &) = delete;
106    output(struct wl_registry *r, uint32_t name, uint32_t v);
107
108    // Events
109    void geometry(int32_t x, int32_t y, int32_t pw, int32_t ph, int32_t subpel,
110                  char const *make, char const *model, int32_t tx);
111    void mode(uint32_t flags, int32_t w, int32_t h, int32_t r);
112    void done();
113    void scale(int32_t factor);
114 };
115 }  // namespace wl
116
117 /**
118  * namespace compositor
119  */
120 namespace compositor {
121
122 struct size {
123    uint32_t w, h;
124 };
125
126 struct rect {
127    int32_t w, h;
128    int32_t x, y;
129 };
130
131 static const constexpr rect full_rect = rect{-1, -1, 0, 0};
132
133 inline bool operator==(struct rect a, struct rect b) {
134    return a.w == b.w && a.h == b.h && a.x == b.x && a.y == b.y;
135 }
136
137 struct controller;
138
139 struct controller_child {
140    struct controller *parent;
141    uint32_t id;
142
143    controller_child(controller_child const &) = delete;
144    controller_child &operator=(controller_child const &) = delete;
145    controller_child(struct controller *c, uint32_t i) : parent(c), id(i) {}
146    virtual ~controller_child() {}
147 };
148
149 struct surface_properties {
150    uint32_t id;  // let's just save an ID here too
151    struct rect dst_rect;
152    struct rect src_rect;
153    struct size size;
154    int32_t orientation;
155    int32_t visibility;
156    float opacity;
157 };
158
159 /**
160  * @struct surface
161  */
162 struct surface : public controller_child {
163    surface(surface const &) = delete;
164    surface &operator=(surface const &) = delete;
165    surface(uint32_t i, struct controller *c);
166
167    // Requests
168    void set_visibility(uint32_t visibility);
169    void set_source_rectangle(int32_t x, int32_t y,
170                              int32_t width, int32_t height);
171    void set_destination_rectangle(int32_t x, int32_t y,
172                                   int32_t width, int32_t height);
173 };
174
175 /**
176  * @struct layer
177  */
178 struct layer : public controller_child {
179    layer(layer const &) = delete;
180    layer &operator=(layer const &) = delete;
181    layer(uint32_t i, struct controller *c);
182    layer(uint32_t i, int32_t w, int32_t h, struct controller *c);
183
184    // Requests
185    void set_visibility(uint32_t visibility);
186    void set_destination_rectangle(int32_t x, int32_t y,
187                                   int32_t width, int32_t height);
188    void add_surface(uint32_t surface_id);
189    void remove_surface(uint32_t surface_id);
190 };
191
192 /**
193  * @struct screen
194  */
195 struct screen : public wayland_proxy<struct ivi_wm_screen>,
196                 public controller_child {
197    screen(screen const &) = delete;
198    screen &operator=(screen const &) = delete;
199    screen(uint32_t i, struct controller *c, struct wl_output *o);
200
201    void clear();
202    void screen_created(struct screen *screen, uint32_t id);
203    void set_render_order(std::vector<uint32_t> const &ro);
204 };
205
206 /**
207  * @struct controller
208  */
209 struct controller : public wayland_proxy<struct ivi_wm> {
210    // This controller is still missing ivi-input
211
212    typedef std::unordered_map<uintptr_t, uint32_t> proxy_to_id_map_type;
213    typedef std::unordered_map<uint32_t, std::unique_ptr<struct surface>>
214       surface_map_type;
215    typedef std::unordered_map<uint32_t, std::unique_ptr<struct layer>>
216       layer_map_type;
217    typedef std::unordered_map<uint32_t, struct screen *> screen_map_type;
218    typedef std::unordered_map<uint32_t, struct surface_properties> props_map;
219
220    // HACK:
221    // The order of these member is mandatory, as when objects are destroyed
222    // they will call their parent (that's us right here!) and remove their
223    // proxy-to-id mapping. I.e. the *_proxy_to_id members need to be valid
224    // when the surfaces/layers/screens maps are destroyed. This sucks, but
225    // I cannot see a better solution w/o globals or some other horrible
226    // call-our-parent construct.
227    proxy_to_id_map_type surface_proxy_to_id;
228    proxy_to_id_map_type layer_proxy_to_id;
229    proxy_to_id_map_type screen_proxy_to_id;
230
231    props_map sprops;
232    props_map lprops;
233
234    surface_map_type surfaces;
235    layer_map_type layers;
236    screen_map_type screens;
237
238    std::unique_ptr<struct screen> screen;
239
240    size output_size;   // Display size[pixel]
241    size physical_size; // Display size[mm]
242
243    wm::controller_hooks *chooks;
244
245    struct wl::display *display;
246
247    void add_proxy_to_sid_mapping(struct ivi_wm *p, uint32_t id);
248    void remove_proxy_to_sid_mapping(struct ivi_wm *p);
249
250    void add_proxy_to_lid_mapping(struct ivi_wm *p, uint32_t id);
251    void remove_proxy_to_lid_mapping(struct ivi_wm *p);
252
253    void add_proxy_to_id_mapping(struct wl_output *p, uint32_t id);
254    void remove_proxy_to_id_mapping(struct wl_output *p);
255
256    bool surface_exists(uint32_t id) const {
257       return this->surfaces.find(id) != this->surfaces.end();
258    }
259
260    bool layer_exists(uint32_t id) const {
261       return this->layers.find(id) != this->layers.end();
262    }
263
264    controller(struct wl_registry *r, uint32_t name, uint32_t version);
265
266    // Requests
267    void commit_changes() const {
268       ivi_wm_commit_changes(this->proxy.get());
269    }
270    void layer_create(uint32_t id, int32_t w, int32_t h);
271    void surface_create(uint32_t id);
272    void create_screen(struct wl_output *output);
273
274    // Events
275    void surface_visibility_changed(struct surface *s, int32_t visibility);
276    void surface_opacity_changed(struct surface *s, float opacity);
277    void surface_source_rectangle_changed(struct surface *s, int32_t x, int32_t y,
278                                  int32_t width, int32_t height);
279    void surface_destination_rectangle_changed(struct surface *s, int32_t x, int32_t y,
280                                       int32_t width, int32_t height);
281    void surface_created(uint32_t id);
282    void surface_destroyed(struct surface *s, uint32_t surface_id);
283    void surface_error_detected(uint32_t object_id,
284                       uint32_t error_code, char const *error_text);
285    void surface_size_changed(struct surface *s, int32_t width, int32_t height);
286    void surface_stats_received(struct surface *s, uint32_t surface_id,
287                       uint32_t frame_count, uint32_t pid);
288    void surface_added_to_layer(struct surface *s, uint32_t layer_id, uint32_t surface_id);
289
290    void layer_visibility_changed(struct layer *l, uint32_t layer_id,int32_t visibility);
291    void layer_opacity_changed(struct layer *l, uint32_t layer_id,float opacity);
292    void layer_source_rectangle_changed(struct layer *l, uint32_t layer_id,int32_t x, int32_t y,
293                                int32_t width, int32_t height);
294    void layer_destination_rectangle_changed(struct layer *l, uint32_t layer_id,int32_t x, int32_t y,
295                                     int32_t width, int32_t height);
296    void layer_created(uint32_t id);
297    void layer_destroyed(struct layer *l, uint32_t layer_id);
298    void layer_error_detected(uint32_t object_id,
299                     uint32_t error_code, char const *error_text);
300    void layer_configuration(struct layer *l, int32_t width, int32_t height);
301    void layer_orientation(struct layer *l, int32_t orientation);
302    void layer_screen(struct layer *l, struct wl_output *screen);
303
304 };
305 }  // namespace compositor
306
307 #endif  // !WM_WAYLAND_HPP