7 #define AFB_BINDING_VERSION 2
10 #include <afb/afb-binding.h>
11 #include <systemd/sd-event.h>
18 std::unique_ptr<wl::display> display;
19 std::unique_ptr<genivi::controller> controller;
20 std::vector<std::unique_ptr<wl::output>> outputs;
22 wayland() : display(new wl::display), controller(nullptr), outputs() {}
27 struct wayland *g_wayland;
30 if (!this->display->ok()) {
34 this->display->r.add_global_handler("wl_output", [](wl_registry *r,
37 g_wayland->outputs.emplace_back(std::make_unique<wl::output>(r, name, v));
40 this->display->r.add_global_handler(
41 "ivi_controller", [](wl_registry *r, uint32_t name, uint32_t v) {
42 g_wayland->controller =
43 std::make_unique<genivi::controller>(r, name, v);
45 // XXX: This protocol needs the output, so lets just add our mapping
47 g_wayland->controller->add_proxy_to_id_mapping(
48 g_wayland->outputs.back()->proxy.get(),
49 wl_proxy_get_id(reinterpret_cast<struct wl_proxy *>(
50 g_wayland->outputs.back()->proxy.get())));
53 // First level objects
54 this->display->roundtrip();
55 // Second level objects
56 this->display->roundtrip();
57 // Third level objects
58 this->display->roundtrip();
64 // (_)_ __ (_) |_ | | __ _ _ _ ___ _ _| |_ / /\ \
65 // | | '_ \| | __| | |/ _` | | | |/ _ \| | | | __| | | |
66 // | | | | | | |_ | | (_| | |_| | (_) | |_| | |_| | | |
67 // |_|_| |_|_|\__|___|_|\__,_|\__, |\___/ \__,_|\__| | | |
68 // |_____| |___/ \_\/_/
69 char const *init_layout() {
70 if (!g_wayland->controller) {
71 return "ivi_controller global not available";
74 if (g_wayland->outputs.empty()) {
75 return "no output was set up!";
78 auto &c = g_wayland->controller;
80 auto &o = g_wayland->outputs.front();
81 auto &s = c->screens.begin()->second;
82 auto &layers = c->layers;
84 // XXX: Write output dimensions to ivi controller...
85 c->output_size = genivi::size{uint32_t(o->width), uint32_t(o->height)};
93 // Setup our dummy scene...
94 c->layer_create(100, 0, 0); // bottom layer, anything else
95 c->layer_create(1000, 0, 0); // top layer, mandelbrot
97 auto &l100 = c->layers[100];
98 auto &l1k = c->layers[1000];
100 // Set layers fullscreen
101 l100->set_destination_rectangle(0, 0, o->width, o->height);
102 l1k->set_destination_rectangle(0, 0, o->width, o->height);
103 l100->set_visibility(1);
104 l1k->set_visibility(1);
106 // Add layers to screen
107 s->set_render_order({100, 1000});
111 g_wayland->display->flush();
116 int display_event_callback(sd_event_source *evs, int fd, uint32_t events,
118 if ((events & EPOLLHUP) != 0) {
119 AFB_ERROR("The compositor hung up, dying now.");
125 if (events & EPOLLIN) {
126 int ret = g_wayland->display->dispatch();
128 AFB_ERROR("wl_display_dipatch() returned error %d",
129 g_wayland->display->get_error());
132 g_wayland->display->flush();
134 // execute pending tasks, that is layout changes etc.
135 g_wayland->controller->execute_pending();
136 g_wayland->display->roundtrip();
142 sd_event_source_unref(evs);
146 // _ _ _ _ _ _ _ ____
147 // | |__ (_)_ __ __| (_)_ __ __ _ (_)_ __ (_) |_ / /\ \
148 // | '_ \| | '_ \ / _` | | '_ \ / _` | | | '_ \| | __| | | |
149 // | |_) | | | | | (_| | | | | | (_| | | | | | | | |_| | | |
150 // |_.__/|_|_| |_|\__,_|_|_| |_|\__, |___|_|_| |_|_|\__| | | |
151 // |___/_____| \_\/_/
152 int binding_init_() {
153 lognotice("WinMan ver. %s", WINMAN_VERSION_STRING);
155 if (g_wayland != nullptr) {
156 AFB_ERROR("Wayland context already initialized?");
160 if (getenv("XDG_RUNTIME_DIR") == nullptr) {
161 AFB_ERROR("Environment variable XDG_RUNTIME_DIR not set");
165 g_wayland = new wayland;
166 if (g_wayland->init() == -1) {
167 AFB_ERROR("Could not connect to compositor");
172 if (char const *e = init_layout()) {
173 AFB_ERROR("Could not init layout: %s", e);
177 sd_event_add_io(afb_daemon_get_event_loop(), nullptr,
178 g_wayland->display->get_fd(), EPOLLIN,
179 display_event_callback, g_wayland);
181 atexit([] { delete g_wayland; });
186 int binding_init() noexcept {
188 return binding_init_();
189 } catch (std::exception &e) {
190 AFB_ERROR("Uncaught exception in binding_init(): %s", e.what());
195 #define CHECK_WAYLAND() \
197 if (g_wayland == nullptr) { \
198 afb_req_fail(req, "failed", \
199 "Binding not initialized, did the compositor die?"); \
205 // __| | ___| |__ _ _ __ _ ___| |_ __ _| |_ _ _ ___ / /\ \
206 // / _` |/ _ \ '_ \| | | |/ _` | / __| __/ _` | __| | | / __| | | |
207 // | (_| | __/ |_) | |_| | (_| | \__ \ || (_| | |_| |_| \__ \ | | |
208 // \__,_|\___|_.__/ \__,_|\__, |___|___/\__\__,_|\__|\__,_|___/ | | |
209 // |___/_____| \_\/_/
210 void debug_status(struct afb_req req) noexcept {
211 // Quick and dirty, dump current surfaces and layers
212 AFB_REQ_DEBUG(req, "status");
217 using json = nlohmann::json;
221 if (!g_wayland->controller->surfaces.empty()) {
222 auto a = json::array();
223 for (auto const &i : g_wayland->controller->surfaces) {
224 auto const &r = i.second->dst_rect;
225 auto const &s = i.second->size;
226 a.push_back({{"id", i.first},
227 {"size", {s.w, s.h}},
228 {"dst_rect", {r.w, r.h, r.x, r.y}}});
233 if (!g_wayland->controller->layers.empty()) {
234 auto a = json::array();
235 for (auto const &i : g_wayland->controller->layers) {
236 auto const &r = i.second->dst_rect;
237 auto const &s = i.second->size;
238 a.push_back({{"id", i.first},
239 {"size", {s.w, s.h}},
240 {"dst_rect", {r.w, r.h, r.x, r.y}}});
245 afb_req_success(req, json_tokener_parse(j.dump().c_str()), "status");
246 } catch (std::exception &e) {
247 afb_req_fail_f(req, "failed", "Uncaught exception: %s", e.what());
251 void debug_surfaces(afb_req req) {
254 auto a = json_object_new_array();
256 if (!g_wayland->controller->surfaces.empty()) {
257 for (auto const &i : g_wayland->controller->surfaces) {
258 json_object_array_add(a, json_object_new_int(i.first));
262 afb_req_success(req, a, "surfaces");
265 void debug_layers(afb_req req) {
268 auto a = json_object_new_array();
270 if (!g_wayland->controller->layers.empty()) {
271 for (auto const &i : g_wayland->controller->layers) {
272 json_object_array_add(a, json_object_new_int(i.first));
276 afb_req_success(req, a, "surfaces");
279 const struct afb_verb_v2 verbs[] = {
280 {"status", debug_status, NULL, NULL, AFB_SESSION_NONE_V2},
281 {"layers", debug_layers, NULL, NULL, AFB_SESSION_NONE_V2},
282 {"surfaces", debug_surfaces, NULL, NULL, AFB_SESSION_NONE_V2},
287 extern "C" const struct afb_binding_v2 afbBindingV2 = {
288 "winman", NULL, NULL, verbs, NULL, binding_init, NULL, 1};