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());
182 #define CHECK_WAYLAND() \
184 if (g_wayland == nullptr) { \
185 afb_req_fail(req, "failed", \
186 "Binding not initialized, did the compositor die?"); \
192 // __| | ___| |__ _ _ __ _ ___| |_ __ _| |_ _ _ ___ / /\ \
193 // / _` |/ _ \ '_ \| | | |/ _` | / __| __/ _` | __| | | / __| | | |
194 // | (_| | __/ |_) | |_| | (_| | \__ \ || (_| | |_| |_| \__ \ | | |
195 // \__,_|\___|_.__/ \__,_|\__, |___|___/\__\__,_|\__|\__,_|___/ | | |
196 // |___/_____| \_\/_/
197 void debug_status(struct afb_req req) noexcept {
198 // Quick and dirty, dump current surfaces and layers
199 AFB_REQ_DEBUG(req, "status");
206 if (!g_wayland->controller->surfaces.empty()) {
207 auto js = json::array();
208 for (auto const &i : g_wayland->controller->surfaces) {
209 auto const &r = i.second->dst_rect;
210 auto const &s = i.second->size;
211 js.push_back({{"id", i.first},
212 {"size", {s.w, s.h}},
213 {"dst_rect", {r.w, r.h, r.x, r.y}}});
218 if (!g_wayland->controller->layers.empty()) {
219 auto js = json::array();
220 for (auto const &i : g_wayland->controller->layers) {
221 auto const &r = i.second->dst_rect;
222 auto const &s = i.second->size;
223 js.push_back({{"id", i.first},
224 {"size", {s.w, s.h}},
225 {"dst_rect", {r.w, r.h, r.x, r.y}}});
230 afb_req_success(req, json_tokener_parse(j.dump().c_str()), "status");
231 } catch (std::exception &e) {
232 afb_req_fail_f(req, "failed", "Uncaught exception: %s", e.what());
236 void debug_surfaces(afb_req req) {
239 auto a = json_object_new_array();
241 if (!g_wayland->controller->surfaces.empty()) {
242 for (auto const &i : g_wayland->controller->surfaces) {
243 json_object_array_add(a, json_object_new_int(i.first));
247 afb_req_success(req, a, "surfaces");
250 void debug_layers(afb_req req) {
253 auto a = json_object_new_array();
255 if (!g_wayland->controller->layers.empty()) {
256 for (auto const &i : g_wayland->controller->layers) {
257 json_object_array_add(a, json_object_new_int(i.first));
261 afb_req_success(req, a, "surfaces");
264 const struct afb_verb_v2 verbs[] = {
265 {"status", debug_status, NULL, NULL, AFB_SESSION_NONE_V2},
266 {"layers", debug_layers, NULL, NULL, AFB_SESSION_NONE_V2},
267 {"surfaces", debug_surfaces, NULL, NULL, AFB_SESSION_NONE_V2},
272 extern "C" const struct afb_binding_v2 afbBindingV2 = {
273 "winman", NULL, NULL, verbs, NULL, binding_init, NULL, 1};