7 #define AFB_BINDING_VERSION 2
10 #include <afb/afb-binding.h>
11 #include <systemd/sd-event.h>
16 using json = nlohmann::json;
20 std::unique_ptr<wl::display> display;
21 std::unique_ptr<genivi::controller> controller;
22 std::vector<std::unique_ptr<wl::output>> outputs;
23 sd_event_source *event_source;
26 : display(new wl::display),
32 struct wayland *g_wayland;
35 // (_)_ __ (_) |_ | | __ _ _ _ ___ _ _| |_ / /\ \
36 // | | '_ \| | __| | |/ _` | | | |/ _ \| | | | __| | | |
37 // | | | | | | |_ | | (_| | |_| | (_) | |_| | |_| | | |
38 // |_|_| |_|_|\__|___|_|\__,_|\__, |\___/ \__,_|\__| | | |
39 // |_____| |___/ \_\/_/
40 char const *init_layout() {
41 if (!g_wayland->controller) {
42 return "ivi_controller global not available";
45 if (g_wayland->outputs.empty()) {
46 return "no output was set up!";
49 auto &c = g_wayland->controller;
51 auto &o = g_wayland->outputs.front();
52 auto &s = c->screens.begin()->second;
53 auto &layers = c->layers;
55 // XXX: Write output dimensions to ivi controller...
56 c->output_size = genivi::size{uint32_t(o->width), uint32_t(o->height)};
64 // Setup our dummy scene...
65 c->layer_create(100, 0, 0); // bottom layer, anything else
66 c->layer_create(1000, 0, 0); // top layer, mandelbrot
68 auto &l100 = c->layers[100];
69 auto &l1k = c->layers[1000];
71 // Set layers fullscreen
72 l100->set_destination_rectangle(0, 0, o->width, o->height);
73 l1k->set_destination_rectangle(0, 0, o->width, o->height);
74 l100->set_visibility(1);
75 l1k->set_visibility(1);
77 // Add layers to screen
78 s->set_render_order({100, 1000});
82 g_wayland->display->flush();
88 // | |__ (_)_ __ __| (_)_ __ __ _ (_)_ __ (_) |_ / /\ \
89 // | '_ \| | '_ \ / _` | | '_ \ / _` | | | '_ \| | __| | | |
90 // | |_) | | | | | (_| | | | | | (_| | | | | | | | |_| | | |
91 // |_.__/|_|_| |_|\__,_|_|_| |_|\__, |___|_|_| |_|_|\__| | | |
94 lognotice("WinMan ver. %s", WINMAN_VERSION_STRING);
96 if (g_wayland != nullptr) {
97 AFB_ERROR("Wayland context already initialized?");
101 if (getenv("XDG_RUNTIME_DIR") == nullptr) {
102 AFB_ERROR("Environment variable XDG_RUNTIME_DIR not set");
106 g_wayland = new wayland;
107 if (!g_wayland->display->ok()) {
108 AFB_ERROR("Could not connect to compositor");
112 auto &d = g_wayland->display;
113 d->r.add_global_handler("wl_output", [](wl_registry *r, uint32_t name,
115 g_wayland->outputs.emplace_back(std::make_unique<wl::output>(r, name, v));
118 d->r.add_global_handler("ivi_controller", [](wl_registry *r, uint32_t name,
120 g_wayland->controller = std::make_unique<genivi::controller>(r, name, v);
122 // XXX: This protocol needs the output, so lets just add our mapping
124 g_wayland->controller->add_proxy_to_id_mapping(
125 g_wayland->outputs.back()->proxy.get(),
126 wl_proxy_get_id(reinterpret_cast<struct wl_proxy *>(
127 g_wayland->outputs.back()->proxy.get())));
130 // First level objects
132 // Second level objects
134 // Third level objects
137 if (char const *e = init_layout()) {
138 AFB_ERROR("Could not init layout: %s", e);
142 sd_event *ev = afb_daemon_get_event_loop();
144 ev, &g_wayland->event_source, g_wayland->display->get_fd(),
146 [](sd_event_source *evs, int fd, uint32_t events, void *data) {
147 if ((events & EPOLLHUP) != 0) {
148 sd_event_source_unref(evs);
154 if (events & EPOLLIN) {
155 int ret = g_wayland->display->dispatch();
156 g_wayland->display->flush();
158 // execute pending tasks, that is layout changes etc.
159 g_wayland->controller->execute_pending();
160 g_wayland->display->roundtrip();
161 return ret == -1 ? -1 : 0;
168 atexit([] { delete g_wayland; });
173 int binding_init() noexcept {
175 return binding_init_();
176 } catch (std::exception &e) {
177 AFB_ERROR("Uncaught exception in binding_init(): %s", e.what());
183 // __| | ___| |__ _ _ __ _ ___| |_ __ _| |_ _ _ ___ / /\ \
184 // / _` |/ _ \ '_ \| | | |/ _` | / __| __/ _` | __| | | / __| | | |
185 // | (_| | __/ |_) | |_| | (_| | \__ \ || (_| | |_| |_| \__ \ | | |
186 // \__,_|\___|_.__/ \__,_|\__, |___|___/\__\__,_|\__|\__,_|___/ | | |
187 // |___/_____| \_\/_/
188 void debug_status(struct afb_req req) noexcept {
189 // Quick and dirty, dump current surfaces and layers
190 AFB_REQ_DEBUG(req, "status");
192 if (g_wayland == nullptr) {
193 afb_req_fail(req, "failed",
194 "Binding not initialized, did the compositor die?");
201 if (!g_wayland->controller->surfaces.empty()) {
202 auto js = json::array();
203 for (auto const &i : g_wayland->controller->surfaces) {
204 auto const &r = i.second->dst_rect;
205 auto const &s = i.second->size;
206 js.push_back({{"id", i.first},
207 {"size", {s.w, s.h}},
208 {"dst_rect", {r.w, r.h, r.x, r.y}}});
213 if (!g_wayland->controller->layers.empty()) {
214 auto js = json::array();
215 for (auto const &i : g_wayland->controller->layers) {
216 auto const &r = i.second->dst_rect;
217 auto const &s = i.second->size;
218 js.push_back({{"id", i.first},
219 {"size", {s.w, s.h}},
220 {"dst_rect", {r.w, r.h, r.x, r.y}}});
225 afb_req_success(req, json_tokener_parse(j.dump().c_str()), "status");
226 } catch (std::exception &e) {
227 afb_req_fail_f(req, "failed", "Uncaught exception: %s", e.what());
231 const struct afb_verb_v2 verbs[] = {
232 {"status", debug_status, NULL, NULL, AFB_SESSION_NONE_V2}, {},
236 extern "C" const struct afb_binding_v2 afbBindingV2 = {
237 "winman", NULL, NULL, verbs, NULL, binding_init, NULL, 1};