cpp, simple wrappers for most of what we need, ivi_* still incomplete
[staging/windowmanager.git] / src / wayland.cpp
1 #include "wayland.hpp"
2
3 //                                                                  _
4 //  _ __   __ _ _ __ ___   ___  ___ _ __   __ _  ___ ___  __      _| |
5 // | '_ \ / _` | '_ ` _ \ / _ \/ __| '_ \ / _` |/ __/ _ \ \ \ /\ / / |
6 // | | | | (_| | | | | | |  __/\__ \ |_) | (_| | (_|  __/  \ V  V /| |
7 // |_| |_|\__,_|_| |_| |_|\___||___/ .__/ \__,_|\___\___|   \_/\_/ |_|
8 //                                 |_|
9 namespace wl {
10
11 //      _ _           _
12 //   __| (_)___ _ __ | | __ _ _   _
13 //  / _` | / __| '_ \| |/ _` | | | |
14 // | (_| | \__ \ |_) | | (_| | |_| |
15 //  \__,_|_|___/ .__/|_|\__,_|\__, |
16 //             |_|            |___/
17 display::display()
18    : d(std::unique_ptr<wl_display, void (*)(wl_display *)>(
19         wl_display_connect(NULL), wl_display_disconnect)),
20      r(std::make_unique<registry>(d.get())) {}
21
22 void display::roundtrip() { wl_display_roundtrip(this->d.get()); }
23
24 //                 _     _
25 //  _ __ ___  __ _(_)___| |_ _ __ _   _
26 // | '__/ _ \/ _` | / __| __| '__| | | |
27 // | | |  __/ (_| | \__ \ |_| |  | |_| |
28 // |_|  \___|\__, |_|___/\__|_|   \__, |
29 //           |___/                |___/
30 registry::registry(wl_display *d) : wayland_proxy(wl_display_get_registry(d)) {
31    wl_registry_add_listener(this->proxy, &listener, this);
32 }
33
34 registry::~registry() {
35    wl_registry_destroy(this->proxy);
36    this->proxy = nullptr;
37 }
38
39 void registry::add_global_handler(char const *iface, binder bind) {
40    this->bindings[iface] = bind;
41 }
42
43 void registry::event_global(uint32_t name, char const *iface, uint32_t v) {
44    auto b = this->bindings.find(iface);
45    if (b != this->bindings.end())
46       b->second(this->proxy, name, v);
47    else
48       logdebug("registry @ %p global n %u i %s v %u", this->proxy, name, iface,
49                v);
50 }
51
52 void registry::event_global_remove(uint32_t name) {}
53
54 constexpr wl_registry_listener registry::listener;
55
56 void registry::s_global(void *data, struct wl_registry *r, uint32_t name,
57                         char const *iface, uint32_t v) {
58    static_cast<registry *>(data)->event_global(name, iface, v);
59 }
60
61 void registry::s_global_remove(void *data, struct wl_registry *r,
62                                uint32_t name) {
63    static_cast<registry *>(data)->event_global_remove(name);
64 }
65
66 //              _               _
67 //   ___  _   _| |_ _ __  _   _| |_
68 //  / _ \| | | | __| '_ \| | | | __|
69 // | (_) | |_| | |_| |_) | |_| | |_
70 //  \___/ \__,_|\__| .__/ \__,_|\__|
71 //                 |_|
72 output::output(wl_registry *r, uint32_t name, uint32_t v)
73    : wayland_proxy(wl_registry_bind(r, name, &wl_output_interface, v)) {
74    wl_output_add_listener(this->proxy, &listener, this);
75 }
76
77 void output::event_geometry(int32_t x, int32_t y, int32_t pw, int32_t ph,
78                             int32_t subpel, char const *make, char const *model,
79                             int32_t tx) {
80    logdebug(
81       "wl::output @ %p x %i y %i w %i h %i spel %x make %s model %s tx %i",
82       this->proxy, x, y, pw, ph, subpel, make, model, tx);
83 }
84
85 void output::event_mode(uint32_t flags, int32_t w, int32_t h, int32_t r) {
86    logdebug("wl::output @ %p mode f %x w %i h %i r %i", this->proxy, flags, w,
87             h, r);
88 }
89
90 void output::event_done() { logdebug("wl::output @ %p done", this->proxy); }
91
92 void output::event_scale(int32_t factor) {
93    logdebug("wl::output @ %p scale %i", this->proxy, factor);
94 }
95
96 void output::s_geometry(void *data, struct wl_output *wl_output, int32_t x,
97                         int32_t y, int32_t physical_width,
98                         int32_t physical_height, int32_t subpixel,
99                         const char *make, const char *model,
100                         int32_t transform) {
101    static_cast<output *>(data)->event_geometry(
102       x, y, physical_width, physical_height, subpixel, make, model, transform);
103 }
104
105 void output::s_mode(void *data, struct wl_output *wl_output, uint32_t flags,
106                     int32_t width, int32_t height, int32_t refresh) {
107    static_cast<output *>(data)->event_mode(flags, width, height, refresh);
108 }
109
110 void output::s_done(void *data, struct wl_output *wl_output) {
111    static_cast<output *>(data)->event_done();
112 }
113
114 void output::s_scale(void *data, struct wl_output *wl_output, int32_t factor) {
115    static_cast<output *>(data)->event_scale(factor);
116 }
117
118 constexpr wl_output_listener output::listener;
119 }
120
121 //  _ __   __ _ _ __ ___   ___  ___ _ __   __ _  ___ ___
122 // | '_ \ / _` | '_ ` _ \ / _ \/ __| '_ \ / _` |/ __/ _ \
123 // | | | | (_| | | | | | |  __/\__ \ |_) | (_| | (_|  __/
124 // |_| |_|\__,_|_| |_| |_|\___||___/ .__/ \__,_|\___\___|
125 //                                 |_|
126 //                   _       _
127 //   __ _  ___ _ __ (_)_   _(_)
128 //  / _` |/ _ \ '_ \| \ \ / / |
129 // | (_| |  __/ | | | |\ V /| |
130 //  \__, |\___|_| |_|_| \_/ |_|
131 //  |___/
132 namespace genivi {
133
134 //                  _             _ _
135 //   ___ ___  _ __ | |_ _ __ ___ | | | ___ _ __
136 //  / __/ _ \| '_ \| __| '__/ _ \| | |/ _ \ '__|
137 // | (_| (_) | | | | |_| | | (_) | | |  __/ |
138 //  \___\___/|_| |_|\__|_|  \___/|_|_|\___|_|
139 //
140 controller::controller(wl_registry *r, uint32_t name, uint32_t version)
141    : wayland_proxy(
142         wl_registry_bind(r, name, &ivi_controller_interface, version)) {
143    ivi_controller_add_listener(this->proxy, &listener, this);
144 }
145
146 controller::~controller() {}
147
148 void controller::event_screen(uint32_t id,
149                               struct ivi_controller_screen *screen) {
150    logdebug("genivi::controller @ %p screen %u (%x) @ %p", this->proxy, id, id,
151             screen);
152    this->screens[id] = std::make_unique<struct screen>(id, screen);
153 }
154
155 void controller::event_layer(uint32_t id) {
156    logdebug("genivi::controller @ %p layer %u (%x)", this->proxy, id, id);
157    this->layers[id] = std::make_unique<layer>(id, this->proxy);
158 }
159
160 void controller::event_surface(uint32_t id) {
161    logdebug("genivi::controller @ %p surface %u (%x)", this->proxy, id, id);
162    this->surfaces[id] = std::make_unique<surface>(id, this->proxy);
163 }
164
165 void controller::event_error(int32_t object_id, int32_t object_type,
166                              int32_t error_code, const char *error_text) {
167    logdebug("genivi::controller @ %p error o %i t %i c %i text %s", this->proxy,
168             object_id, object_type, error_code, error_text);
169 }
170
171 void controller::s_screen(void *data, struct ivi_controller *ivi_controller,
172                           uint32_t id_screen,
173                           struct ivi_controller_screen *screen) {
174    static_cast<controller *>(data)->event_screen(id_screen, screen);
175 }
176
177 void controller::s_layer(void *data, struct ivi_controller *ivi_controller,
178                          uint32_t id_layer) {
179    static_cast<controller *>(data)->event_layer(id_layer);
180 }
181
182 void controller::s_surface(void *data, struct ivi_controller *ivi_controller,
183                            uint32_t id_surface) {
184    static_cast<controller *>(data)->event_surface(id_surface);
185 }
186
187 void controller::s_error(void *data, struct ivi_controller *ivi_controller,
188                          int32_t object_id, int32_t object_type,
189                          int32_t error_code, const char *error_text) {
190    static_cast<controller *>(data)->event_error(object_id, object_type,
191                                                 error_code, error_text);
192 }
193
194 constexpr ivi_controller_listener controller::listener;
195
196 //  _
197 // | | __ _ _   _  ___ _ __
198 // | |/ _` | | | |/ _ \ '__|
199 // | | (_| | |_| |  __/ |
200 // |_|\__,_|\__, |\___|_|
201 //          |___/
202 layer::layer(uint32_t i, ivi_controller *c)
203    : wayland_proxy(ivi_controller_layer_create(c, i, 0, 0)), id(i) {
204    ivi_controller_layer_add_listener(this->proxy, &listener, this);
205 }
206
207 layer::~layer() {
208    logdebug("%s layer %i @ %p", __func__, this->id, this->proxy);
209    ivi_controller_layer_destroy(this->proxy, 1);
210    this->proxy = nullptr;
211 }
212
213 constexpr ivi_controller_layer_listener layer::listener;
214
215 //                  __
216 //  ___ _   _ _ __ / _| __ _  ___ ___
217 // / __| | | | '__| |_ / _` |/ __/ _ \
218 // \__ \ |_| | |  |  _| (_| | (_|  __/
219 // |___/\__,_|_|  |_|  \__,_|\___\___|
220 //
221 surface::surface(uint32_t i, ivi_controller *c)
222    : wayland_proxy(ivi_controller_surface_create(c, i)), id(i) {
223    ivi_controller_surface_add_listener(this->proxy, &listener, this);
224 }
225
226 surface::~surface() {
227    logdebug("%s surface %i @ %p", __func__, this->id, this->proxy);
228    ivi_controller_surface_destroy(this->proxy, 1);
229    this->proxy = nullptr;
230 }
231
232 constexpr ivi_controller_surface_listener surface::listener;
233
234 //
235 // ___  ___ _ __ ___  ___ _ __
236 /// __|/ __| '__/ _ \/ _ \ '_ \
237 //\__ \ (__| | |  __/  __/ | | |
238 //|___/\___|_|  \___|\___|_| |_|
239 //
240 screen::screen(uint32_t i, ivi_controller_screen *p)
241    : wayland_proxy(p), id(i) {}
242 }