fixed display dtor message, added layer request defs
[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
12 //                      _                 _
13 // __      ____ _ _   _| | __ _ _ __   __| |     _ __  _ __ _____  ___   _
14 // \ \ /\ / / _` | | | | |/ _` | '_ \ / _` |    | '_ \| '__/ _ \ \/ / | | |
15 //  \ V  V / (_| | |_| | | (_| | | | | (_| |    | |_) | | | (_) >  <| |_| |
16 //   \_/\_/ \__,_|\__, |_|\__,_|_| |_|\__,_|____| .__/|_|  \___/_/\_\\__, |
17 //                |___/                   |_____|_|                  |___/
18 template <typename ProxyT>
19 struct wayland_proxy {
20    ProxyT *proxy;
21
22    wayland_proxy(void *p) : proxy(static_cast<ProxyT *>(p)) {}
23
24    virtual ~wayland_proxy() {
25       // If this is the nullptr, then it means it already was destroyed by a
26       // custom wayland dtor
27       if (this->proxy) {
28          logdebug("%s %p @ %p", __func__, this, this->proxy);
29          wl_proxy_destroy(reinterpret_cast<wl_proxy *>(proxy));
30       }
31    }
32 };
33
34 //                                                                  _
35 //  _ __   __ _ _ __ ___   ___  ___ _ __   __ _  ___ ___  __      _| |
36 // | '_ \ / _` | '_ ` _ \ / _ \/ __| '_ \ / _` |/ __/ _ \ \ \ /\ / / |
37 // | | | | (_| | | | | | |  __/\__ \ |_) | (_| | (_|  __/  \ V  V /| |
38 // |_| |_|\__,_|_| |_| |_|\___||___/ .__/ \__,_|\___\___|   \_/\_/ |_|
39 //                                 |_|
40 namespace wl {
41 struct registry;
42
43 //      _ _           _
44 //   __| (_)___ _ __ | | __ _ _   _
45 //  / _` | / __| '_ \| |/ _` | | | |
46 // | (_| | \__ \ |_) | | (_| | |_| |
47 //  \__,_|_|___/ .__/|_|\__,_|\__, |
48 //             |_|            |___/
49 struct display {
50    std::unique_ptr<wl_display, void (*)(wl_display *)> d;
51    std::unique_ptr<registry> r;
52
53    display();
54    ~display();
55    bool ok() const;
56    void roundtrip();
57    int dispatch();
58 };
59
60 //                 _     _
61 //  _ __ ___  __ _(_)___| |_ _ __ _   _
62 // | '__/ _ \/ _` | / __| __| '__| | | |
63 // | | |  __/ (_| | \__ \ |_| |  | |_| |
64 // |_|  \___|\__, |_|___/\__|_|   \__, |
65 //           |___/                |___/
66 struct registry : public wayland_proxy<wl_registry> {
67    typedef std::function<void(wl_registry *, uint32_t, uint32_t)> binder;
68    std::map<std::string, binder> bindings;
69
70    registry(wl_display *d);
71    ~registry();
72
73    void add_global_handler(char const *iface, binder bind);
74
75    // Events
76    void global(uint32_t name, char const *iface, uint32_t v);
77    void global_remove(uint32_t name);
78 };
79
80 //              _               _
81 //   ___  _   _| |_ _ __  _   _| |_
82 //  / _ \| | | | __| '_ \| | | | __|
83 // | (_) | |_| | |_| |_) | |_| | |_
84 //  \___/ \__,_|\__| .__/ \__,_|\__|
85 //                 |_|
86 struct output : wayland_proxy<wl_output> {
87    output(wl_registry *registry, uint32_t name, uint32_t version);
88
89    // Events
90    void geometry(int32_t x, int32_t y, int32_t pw, int32_t ph, int32_t subpel,
91                  char const *make, char const *model, int32_t tx);
92    void mode(uint32_t flags, int32_t w, int32_t h, int32_t r);
93    void done();
94    void scale(int32_t factor);
95 };
96 }
97
98 //  _ __   __ _ _ __ ___   ___  ___ _ __   __ _  ___ ___
99 // | '_ \ / _` | '_ ` _ \ / _ \/ __| '_ \ / _` |/ __/ _ \
100 // | | | | (_| | | | | | |  __/\__ \ |_) | (_| | (_|  __/
101 // |_| |_|\__,_|_| |_| |_|\___||___/ .__/ \__,_|\___\___|
102 //                                 |_|
103 //                   _       _
104 //   __ _  ___ _ __ (_)_   _(_)
105 //  / _` |/ _ \ '_ \| \ \ / / |
106 // | (_| |  __/ | | | |\ V /| |
107 //  \__, |\___|_| |_|_| \_/ |_|
108 //  |___/
109 namespace genivi {
110
111 //                  __
112 //  ___ _   _ _ __ / _| __ _  ___ ___
113 // / __| | | | '__| |_ / _` |/ __/ _ \
114 // \__ \ |_| | |  |  _| (_| | (_|  __/
115 // |___/\__,_|_|  |_|  \__,_|\___\___|
116 //
117 struct surface : public wayland_proxy<ivi_controller_surface> {
118    uint32_t id;
119
120    surface(uint32_t i, ivi_controller *c);
121    ~surface() override;
122
123    // Events
124    void visibility(int32_t visibility);
125    void opacity(float opacity);
126    void source_rectangle(int32_t x, int32_t y, int32_t width, int32_t height);
127    void destination_rectangle(int32_t x, int32_t y, int32_t width,
128                               int32_t height);
129    void configuration(int32_t width, int32_t height);
130    void orientation(int32_t orientation);
131    void pixelformat(int32_t pixelformat);
132    void layer(struct ivi_controller_layer *layer);
133    void stats(uint32_t redraw_count, uint32_t frame_count,
134               uint32_t update_count, uint32_t pid, const char *process_name);
135    void destroyed();
136    void content(int32_t content_state);
137 };
138
139 //  _
140 // | | __ _ _   _  ___ _ __
141 // | |/ _` | | | |/ _ \ '__|
142 // | | (_| | |_| |  __/ |
143 // |_|\__,_|\__, |\___|_|
144 //          |___/
145 struct layer : public wayland_proxy<ivi_controller_layer> {
146    uint32_t id;
147
148    layer(uint32_t i, ivi_controller *c);
149    ~layer() override;
150
151    // Events
152    void visibility(int32_t visibility);
153    void opacity(float opacity);
154    void source_rectangle(int32_t x, int32_t y, int32_t width, int32_t height);
155    void destination_rectangle(int32_t x, int32_t y, int32_t width,
156                               int32_t height);
157    void configuration(int32_t width, int32_t height);
158    void orientation(int32_t orientation);
159    void screen(struct wl_output *screen);
160    void destroyed();
161
162    // Requests
163    void set_visibility(uint32_t visibility);
164    void set_opacity(wl_fixed_t opacity);
165    void set_source_rectangle(int32_t x, int32_t y, int32_t width, int32_t height);
166    void set_destination_rectangle(int32_t x, int32_t y, int32_t width, int32_t height);
167    void set_configuration(int32_t width, int32_t height);
168    void set_orientation(int32_t orientation);
169    void screenshot(const char *filename);
170    void clear_surfaces();
171    void add_surface(struct surface *surface);
172    void remove_surface(struct surface *surface);
173    void set_render_order(struct wl_array *id_surfaces);
174 };
175
176 //
177 // ___  ___ _ __ ___  ___ _ __
178 /// __|/ __| '__/ _ \/ _ \ '_ \
179 //\__ \ (__| | |  __/  __/ | | |
180 //|___/\___|_|  \___|\___|_| |_|
181 //
182 struct screen : public wayland_proxy<ivi_controller_screen> {
183    uint32_t id;
184
185    screen(uint32_t i, ivi_controller_screen *p);
186 };
187
188 //                  _             _ _
189 //   ___ ___  _ __ | |_ _ __ ___ | | | ___ _ __
190 //  / __/ _ \| '_ \| __| '__/ _ \| | |/ _ \ '__|
191 // | (_| (_) | | | | |_| | | (_) | | |  __/ |
192 //  \___\___/|_| |_|\__|_|  \___/|_|_|\___|_|
193 //
194 struct controller : public wayland_proxy<ivi_controller> {
195    std::map<uint32_t, std::unique_ptr<surface>> surfaces;
196    std::map<uint32_t, std::unique_ptr<layer>> layers;
197    std::map<uint32_t, std::unique_ptr<screen>> screens;
198
199    controller(wl_registry *r, uint32_t name, uint32_t version);
200    ~controller() override;
201
202    // Events
203    void screen(uint32_t id, ivi_controller_screen *screen);
204    void layer(uint32_t id);
205    void surface(uint32_t id);
206    void error(int32_t oid, int32_t otype, int32_t code, char const *text);
207 };
208 }
209
210 #endif  // !WM_WAYLAND_HPP