1 #include "json_helper.hpp"
10 #include <afb/afb-binding.h>
11 #include <systemd/sd-event.h>
16 std::unique_ptr<wl::display> display;
17 std::unique_ptr<genivi::controller> controller;
18 std::vector<std::unique_ptr<wl::output>> outputs;
22 afb_instance() : display{new wl::display}, controller{nullptr}, outputs{}, app{} {}
27 struct afb_instance *g_afb_instance;
29 int afb_instance::init() {
30 if (!this->display->ok()) {
34 this->display->r.add_global_handler("wl_output", [](wl_registry *r,
37 g_afb_instance->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_afb_instance->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_afb_instance->controller->add_proxy_to_id_mapping(
48 g_afb_instance->outputs.back()->proxy.get(),
49 wl_proxy_get_id(reinterpret_cast<struct wl_proxy *>(
50 g_afb_instance->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_afb_instance->controller) {
71 return "ivi_controller global not available";
74 if (g_afb_instance->outputs.empty()) {
75 return "no output was set up!";
78 auto &c = g_afb_instance->controller;
80 auto &o = g_afb_instance->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_afb_instance->display->flush();
116 int display_event_callback(sd_event_source *evs, int fd, uint32_t events,
118 if ((events & EPOLLHUP) != 0) {
119 logerror("The compositor hung up, dying now.");
120 delete g_afb_instance;
121 g_afb_instance = nullptr;
125 if (events & EPOLLIN) {
126 int ret = g_afb_instance->display->dispatch();
128 logerror("wl_display_dipatch() returned error %d",
129 g_afb_instance->display->get_error());
132 g_afb_instance->display->flush();
134 // execute pending tasks, that is layout changes etc.
135 g_afb_instance->controller->execute_pending();
136 g_afb_instance->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_afb_instance != nullptr) {
156 logerror("Wayland context already initialized?");
160 if (getenv("XDG_RUNTIME_DIR") == nullptr) {
161 logerror("Environment variable XDG_RUNTIME_DIR not set");
165 g_afb_instance = new afb_instance;
166 if (g_afb_instance->init() == -1) {
167 logerror("Could not connect to compositor");
171 if (char const *e = init_layout()) {
172 logerror("Could not init layout: %s", e);
177 int ret = sd_event_add_io(afb_daemon_get_event_loop(), nullptr,
178 g_afb_instance->display->get_fd(), EPOLLIN,
179 display_event_callback, g_afb_instance);
181 logerror("Could not initialize afb_instance event handler: %d", -ret);
186 atexit([] { delete g_afb_instance; });
191 delete g_afb_instance;
192 g_afb_instance = nullptr;
196 int binding_init() noexcept {
198 return binding_init_();
199 } catch (std::exception &e) {
200 logerror("Uncaught exception in binding_init(): %s", e.what());
206 // __| | ___| |__ _ _ __ _ ___| |_ __ _| |_ _ _ ___ / /\ \
207 // / _` |/ _ \ '_ \| | | |/ _` | / __| __/ _` | __| | | / __| | | |
208 // | (_| | __/ |_) | |_| | (_| | \__ \ || (_| | |_| |_| \__ \ | | |
209 // \__,_|\___|_.__/ \__,_|\__, |___|___/\__\__,_|\__|\__,_|___/ | | |
210 // |___/_____| \_\/_/
211 void debug_status(struct afb_req req) {
212 // Quick and dirty, dump current surfaces and layers
213 AFB_REQ_DEBUG(req, "status");
215 // auto r = g_afb_instance->app.api.debug_status();
217 // afb_req_fail(req, "failed", r.e.value());
221 auto o = json_object_new_object();
222 json_object_object_add(o, "surfaces",
223 to_json(g_afb_instance->controller->sprops));
224 json_object_object_add(o, "layers", to_json(g_afb_instance->controller->lprops));
225 // json_object_object_add(o, "screens",
226 // to_json(g_afb_instance->controller->screens));
228 afb_req_success(req, o, "status");
231 void debug_surfaces(afb_req req) {
232 afb_req_success(req, to_json(g_afb_instance->controller->sprops), "surfaces");
235 void debug_layers(afb_req req) {
236 afb_req_success(req, to_json(g_afb_instance->controller->lprops), "layers");
239 // Dummy register_surface implementation
240 void register_surface(afb_req req) {
241 AFB_DEBUG("register_surface");
243 auto jo = afb_req_json(req);
245 if (! json_object_object_get_ex(jo, "appid", &jappid)) {
246 afb_req_fail(req, "failed", "register_surface needs 'appid' integer argument");
250 json_object *jsurfid;
251 if (! json_object_object_get_ex(jo, "surfaceid", &jsurfid)) {
252 afb_req_fail(req, "failed", "register_surface needs 'surfaceid' integer argument");
256 uint32_t appid = json_object_get_int(jappid);
257 uint32_t surfid = json_object_get_int(jsurfid);
260 afb_req_fail(req, "failed", "invalid appid");
264 if (surfid > 0xffff) {
265 afb_req_fail(req, "failed", "invalid surfaceid");
269 lognotice("register_surface, got appid %d and surfaceid %d", appid, surfid);
271 afb_req_success(req, json_object_new_int((appid << 16) + surfid), "success");
275 [](afb_req req) noexcept { \
276 if (g_afb_instance == nullptr) { \
277 afb_req_fail(req, "failed", \
278 "Binding not initialized, did the compositor die?"); \
283 } catch (std::exception & e) { \
284 afb_req_fail_f(req, "failed", "Uncaught exception: %s", e.what()); \
288 const struct afb_verb_v2 verbs[] = {
289 {"debug::status", WRAP(debug_status), NULL, NULL, AFB_SESSION_NONE_V2},
290 {"debug::layers", WRAP(debug_layers), NULL, NULL, AFB_SESSION_NONE_V2},
291 {"debug::surfaces", WRAP(debug_surfaces), NULL, NULL, AFB_SESSION_NONE_V2},
293 {"register_surface", WRAP(register_surface), NULL, NULL, AFB_SESSION_NONE_V2},
298 extern "C" const struct afb_binding_v2 afbBindingV2 = {
299 "winman", NULL, NULL, verbs, NULL, binding_init, NULL, 1};