2 * Copyright (c) 2017 TOYOTA MOTOR CORPORATION
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #ifndef WM_WAYLAND_HPP
18 #define WM_WAYLAND_HPP
20 #include "controller_hooks.hpp"
21 #include "ivi-controller-client-protocol.h"
26 #include <unordered_map>
30 * @struct wayland_proxy
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)
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)) {}
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;
57 registry(registry const &) = delete;
58 registry &operator=(registry const &) = delete;
59 registry(struct wl_display *d);
61 void add_global_handler(char const *iface, binder bind);
64 void global(uint32_t name, char const *iface, uint32_t v);
65 void global_remove(uint32_t name);
72 std::unique_ptr<struct wl_display, void (*)(struct wl_display *)> d;
75 display(display const &) = delete;
76 display &operator=(display const &) = delete;
81 int dispatch_pending();
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);
96 struct output : wayland_proxy<struct wl_output> {
100 int physical_height{};
104 output(output const &) = delete;
105 output &operator=(output const &) = delete;
106 output(struct wl_registry *r, uint32_t name, uint32_t v);
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);
113 void scale(int32_t factor);
118 * namespace compositor
120 namespace compositor {
131 static const constexpr rect full_rect = rect{-1, -1, 0, 0};
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;
139 struct controller_child {
140 struct controller *parent;
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() {}
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;
162 struct surface : public wayland_proxy<struct ivi_controller_surface>,
164 surface(surface const &) = delete;
165 surface &operator=(surface const &) = delete;
166 surface(uint32_t i, struct controller *c);
169 void set_visibility(uint32_t visibility);
170 void set_opacity(wl_fixed_t opacity);
171 void set_source_rectangle(int32_t x, int32_t y, int32_t width,
173 void set_destination_rectangle(int32_t x, int32_t y, int32_t width,
175 void set_configuration(int32_t width, int32_t height);
176 void set_orientation(int32_t orientation);
177 void screenshot(const char *filename);
179 void destroy(int32_t destroy_scene_object);
185 struct layer : public wayland_proxy<struct ivi_controller_layer>,
187 layer(layer const &) = delete;
188 layer &operator=(layer const &) = delete;
189 layer(uint32_t i, struct controller *c);
190 layer(uint32_t i, int32_t w, int32_t h, struct controller *c);
193 void set_visibility(uint32_t visibility);
194 void set_opacity(wl_fixed_t opacity);
195 void set_source_rectangle(int32_t x, int32_t y, int32_t width,
197 void set_destination_rectangle(int32_t x, int32_t y, int32_t width,
199 void set_configuration(int32_t width, int32_t height);
200 void set_orientation(int32_t orientation);
201 void screenshot(const char *filename);
202 void clear_surfaces();
203 void add_surface(struct surface *surface);
204 void remove_surface(struct surface *surface);
205 void set_render_order(std::vector<uint32_t> const &ro);
211 struct screen : public wayland_proxy<struct ivi_controller_screen>,
213 screen(screen const &) = delete;
214 screen &operator=(screen const &) = delete;
215 screen(uint32_t i, struct controller *c, struct ivi_controller_screen *p);
217 void add_layer(layer *l);
218 void set_render_order(std::vector<uint32_t> const &ro);
224 struct controller : public wayland_proxy<struct ivi_controller> {
225 // This controller is still missing ivi-input
227 typedef std::unordered_map<uintptr_t, uint32_t> proxy_to_id_map_type;
228 typedef std::unordered_map<uint32_t, std::unique_ptr<struct surface>>
230 typedef std::unordered_map<uint32_t, std::unique_ptr<struct layer>>
232 typedef std::unordered_map<uint32_t, std::unique_ptr<struct screen>>
235 typedef std::unordered_map<uint32_t, struct surface_properties> props_map;
238 // The order of these member is mandatory, as when objects are destroyed
239 // they will call their parent (that's us right here!) and remove their
240 // proxy-to-id mapping. I.e. the *_proxy_to_id members need to be valid
241 // when the surfaces/layers/screens maps are destroyed. This sucks, but
242 // I cannot see a better solution w/o globals or some other horrible
243 // call-our-parent construct.
244 proxy_to_id_map_type surface_proxy_to_id;
245 proxy_to_id_map_type layer_proxy_to_id;
246 proxy_to_id_map_type screen_proxy_to_id;
251 surface_map_type surfaces;
252 layer_map_type layers;
253 screen_map_type screens;
255 size output_size; // Display size[pixel]
256 size physical_size; // Display size[mm]
258 wm::controller_hooks *chooks;
262 void add_proxy_to_id_mapping(struct ivi_controller_surface *p, uint32_t id);
263 void remove_proxy_to_id_mapping(struct ivi_controller_surface *p);
264 void add_proxy_to_id_mapping(struct ivi_controller_layer *p, uint32_t id);
265 void remove_proxy_to_id_mapping(struct ivi_controller_layer *p);
266 void add_proxy_to_id_mapping(struct wl_output *p, uint32_t id);
267 void remove_proxy_to_id_mapping(struct wl_output *p);
269 bool surface_exists(uint32_t id) const {
270 return this->surfaces.find(id) != this->surfaces.end();
273 bool layer_exists(uint32_t id) const {
274 return this->layers.find(id) != this->layers.end();
277 controller(struct wl_registry *r, uint32_t name, uint32_t version);
280 void commit_changes() const {
281 ivi_controller_commit_changes(this->proxy.get());
283 void layer_create(uint32_t id, int32_t w, int32_t h);
284 void surface_create(uint32_t id);
288 void controller_screen(uint32_t id, struct ivi_controller_screen *screen);
289 void controller_layer(uint32_t id);
290 void controller_surface(uint32_t id);
291 void controller_error(int32_t object_id, int32_t object_type,
292 int32_t error_code, char const *error_text);
295 void surface_visibility(struct surface *s, int32_t visibility);
296 void surface_opacity(struct surface *s, float opacity);
297 void surface_source_rectangle(struct surface *s, int32_t x, int32_t y,
298 int32_t width, int32_t height);
299 void surface_destination_rectangle(struct surface *s, int32_t x, int32_t y,
300 int32_t width, int32_t height);
301 void surface_configuration(struct surface *s, int32_t width, int32_t height);
302 void surface_orientation(struct surface *s, int32_t orientation);
303 void surface_pixelformat(struct surface *s, int32_t pixelformat);
304 void surface_layer(struct surface *s, struct ivi_controller_layer *layer);
305 void surface_stats(struct surface *s, uint32_t redraw_count,
306 uint32_t frame_count, uint32_t update_count, uint32_t pid,
307 const char *process_name);
308 void surface_destroyed(struct surface *s);
309 void surface_content(struct surface *s, int32_t content_state);
312 void layer_visibility(struct layer *l, int32_t visibility);
313 void layer_opacity(struct layer *l, float opacity);
314 void layer_source_rectangle(struct layer *l, int32_t x, int32_t y,
315 int32_t width, int32_t height);
316 void layer_destination_rectangle(struct layer *l, int32_t x, int32_t y,
317 int32_t width, int32_t height);
318 void layer_configuration(struct layer *l, int32_t width, int32_t height);
319 void layer_orientation(struct layer *l, int32_t orientation);
320 void layer_screen(struct layer *l, struct wl_output *screen);
321 void layer_destroyed(struct layer *l);
323 } // namespace compositor
325 #endif // !WM_WAYLAND_HPP