1 #include "json_helper.hpp"
8 #define AFB_BINDING_VERSION 2
11 #include <afb/afb-binding.h>
12 #include <systemd/sd-event.h>
17 std::unique_ptr<wl::display> display;
18 std::unique_ptr<genivi::controller> controller;
19 std::vector<std::unique_ptr<wl::output>> outputs;
21 wayland() : display(new wl::display), controller(nullptr), outputs() {}
26 struct wayland *g_wayland;
29 if (!this->display->ok()) {
33 this->display->r.add_global_handler("wl_output", [](wl_registry *r,
36 g_wayland->outputs.emplace_back(std::make_unique<wl::output>(r, name, v));
39 this->display->r.add_global_handler(
40 "ivi_controller", [](wl_registry *r, uint32_t name, uint32_t v) {
41 g_wayland->controller =
42 std::make_unique<genivi::controller>(r, name, v);
44 // XXX: This protocol needs the output, so lets just add our mapping
46 g_wayland->controller->add_proxy_to_id_mapping(
47 g_wayland->outputs.back()->proxy.get(),
48 wl_proxy_get_id(reinterpret_cast<struct wl_proxy *>(
49 g_wayland->outputs.back()->proxy.get())));
52 // First level objects
53 this->display->roundtrip();
54 // Second level objects
55 this->display->roundtrip();
56 // Third level objects
57 this->display->roundtrip();
63 // (_)_ __ (_) |_ | | __ _ _ _ ___ _ _| |_ / /\ \
64 // | | '_ \| | __| | |/ _` | | | |/ _ \| | | | __| | | |
65 // | | | | | | |_ | | (_| | |_| | (_) | |_| | |_| | | |
66 // |_|_| |_|_|\__|___|_|\__,_|\__, |\___/ \__,_|\__| | | |
67 // |_____| |___/ \_\/_/
68 char const *init_layout() {
69 if (!g_wayland->controller) {
70 return "ivi_controller global not available";
73 if (g_wayland->outputs.empty()) {
74 return "no output was set up!";
77 auto &c = g_wayland->controller;
79 auto &o = g_wayland->outputs.front();
80 auto &s = c->screens.begin()->second;
81 auto &layers = c->layers;
83 // XXX: Write output dimensions to ivi controller...
84 c->output_size = genivi::size{uint32_t(o->width), uint32_t(o->height)};
92 // Setup our dummy scene...
93 c->layer_create(100, 0, 0); // bottom layer, anything else
94 c->layer_create(1000, 0, 0); // top layer, mandelbrot
96 auto &l100 = c->layers[100];
97 auto &l1k = c->layers[1000];
99 // Set layers fullscreen
100 l100->set_destination_rectangle(0, 0, o->width, o->height);
101 l1k->set_destination_rectangle(0, 0, o->width, o->height);
102 l100->set_visibility(1);
103 l1k->set_visibility(1);
105 // Add layers to screen
106 s->set_render_order({100, 1000});
110 g_wayland->display->flush();
115 int display_event_callback(sd_event_source *evs, int fd, uint32_t events,
117 if ((events & EPOLLHUP) != 0) {
118 AFB_ERROR("The compositor hung up, dying now.");
124 if (events & EPOLLIN) {
125 int ret = g_wayland->display->dispatch();
127 AFB_ERROR("wl_display_dipatch() returned error %d",
128 g_wayland->display->get_error());
131 g_wayland->display->flush();
133 // execute pending tasks, that is layout changes etc.
134 g_wayland->controller->execute_pending();
135 g_wayland->display->roundtrip();
141 sd_event_source_unref(evs);
145 // _ _ _ _ _ _ _ ____
146 // | |__ (_)_ __ __| (_)_ __ __ _ (_)_ __ (_) |_ / /\ \
147 // | '_ \| | '_ \ / _` | | '_ \ / _` | | | '_ \| | __| | | |
148 // | |_) | | | | | (_| | | | | | (_| | | | | | | | |_| | | |
149 // |_.__/|_|_| |_|\__,_|_|_| |_|\__, |___|_|_| |_|_|\__| | | |
150 // |___/_____| \_\/_/
151 int binding_init_() {
152 lognotice("WinMan ver. %s", WINMAN_VERSION_STRING);
154 if (g_wayland != nullptr) {
155 AFB_ERROR("Wayland context already initialized?");
159 if (getenv("XDG_RUNTIME_DIR") == nullptr) {
160 AFB_ERROR("Environment variable XDG_RUNTIME_DIR not set");
164 g_wayland = new wayland;
165 if (g_wayland->init() == -1) {
166 AFB_ERROR("Could not connect to compositor");
170 if (char const *e = init_layout()) {
171 AFB_ERROR("Could not init layout: %s", e);
176 int ret = sd_event_add_io(afb_daemon_get_event_loop(), nullptr,
177 g_wayland->display->get_fd(), EPOLLIN,
178 display_event_callback, g_wayland);
180 AFB_ERROR("Could not initialize wayland event handler: %d", -ret);
185 atexit([] { delete g_wayland; });
195 int binding_init() noexcept {
197 return binding_init_();
198 } catch (std::exception &e) {
199 AFB_ERROR("Uncaught exception in binding_init(): %s", e.what());
204 #define CHECK_WAYLAND() \
206 if (g_wayland == nullptr) { \
207 afb_req_fail(req, "failed", \
208 "Binding not initialized, did the compositor die?"); \
214 // __| | ___| |__ _ _ __ _ ___| |_ __ _| |_ _ _ ___ / /\ \
215 // / _` |/ _ \ '_ \| | | |/ _` | / __| __/ _` | __| | | / __| | | |
216 // | (_| | __/ |_) | |_| | (_| | \__ \ || (_| | |_| |_| \__ \ | | |
217 // \__,_|\___|_.__/ \__,_|\__, |___|___/\__\__,_|\__|\__,_|___/ | | |
218 // |___/_____| \_\/_/
219 void debug_status(struct afb_req req) noexcept {
220 // Quick and dirty, dump current surfaces and layers
221 AFB_REQ_DEBUG(req, "status");
225 auto o = json_object_new_object();
226 json_object_object_add(o, "surfaces",
227 to_json(g_wayland->controller->surfaces));
228 json_object_object_add(o, "layers", to_json(g_wayland->controller->layers));
229 json_object_object_add(o, "screens",
230 to_json(g_wayland->controller->screens));
232 afb_req_success(req, o, "status");
235 void debug_surfaces(afb_req req) noexcept {
238 afb_req_success(req, to_json(g_wayland->controller->surfaces), "surfaces");
241 void debug_layers(afb_req req) noexcept {
244 afb_req_success(req, to_json(g_wayland->controller->layers), "layers");
247 const struct afb_verb_v2 verbs[] = {
248 {"status", debug_status, NULL, NULL, AFB_SESSION_NONE_V2},
249 {"layers", debug_layers, NULL, NULL, AFB_SESSION_NONE_V2},
250 {"surfaces", debug_surfaces, NULL, NULL, AFB_SESSION_NONE_V2},
255 extern "C" const struct afb_binding_v2 afbBindingV2 = {
256 "winman", NULL, NULL, verbs, NULL, binding_init, NULL, 1};