1 #include "json_helper.hpp"
9 #include <afb/afb-binding.h>
10 #include <systemd/sd-event.h>
15 std::unique_ptr<wl::display> display;
16 std::unique_ptr<genivi::controller> controller;
17 std::vector<std::unique_ptr<wl::output>> outputs;
21 afb_instance() : display{new wl::display}, controller{nullptr}, outputs{}, app{} {}
26 struct afb_instance *g_afb_instance;
28 int afb_instance::init() {
29 if (!this->display->ok()) {
33 this->display->r.add_global_handler("wl_output", [](wl_registry *r,
36 g_afb_instance->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_afb_instance->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_afb_instance->controller->add_proxy_to_id_mapping(
47 g_afb_instance->outputs.back()->proxy.get(),
48 wl_proxy_get_id(reinterpret_cast<struct wl_proxy *>(
49 g_afb_instance->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_afb_instance->controller) {
70 return "ivi_controller global not available";
73 if (g_afb_instance->outputs.empty()) {
74 return "no output was set up!";
77 auto &c = g_afb_instance->controller;
79 auto &o = g_afb_instance->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_afb_instance->display->flush();
115 int display_event_callback(sd_event_source *evs, int fd, uint32_t events,
117 if ((events & EPOLLHUP) != 0) {
118 logerror("The compositor hung up, dying now.");
119 delete g_afb_instance;
120 g_afb_instance = nullptr;
124 if (events & EPOLLIN) {
125 int ret = g_afb_instance->display->dispatch();
127 logerror("wl_display_dipatch() returned error %d",
128 g_afb_instance->display->get_error());
131 g_afb_instance->display->flush();
133 // execute pending tasks, that is layout changes etc.
134 g_afb_instance->controller->execute_pending();
135 g_afb_instance->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_afb_instance != nullptr) {
155 logerror("Wayland context already initialized?");
159 if (getenv("XDG_RUNTIME_DIR") == nullptr) {
160 logerror("Environment variable XDG_RUNTIME_DIR not set");
164 g_afb_instance = new afb_instance;
165 if (g_afb_instance->init() == -1) {
166 logerror("Could not connect to compositor");
170 if (char const *e = init_layout()) {
171 logerror("Could not init layout: %s", e);
176 int ret = sd_event_add_io(afb_daemon_get_event_loop(), nullptr,
177 g_afb_instance->display->get_fd(), EPOLLIN,
178 display_event_callback, g_afb_instance);
180 logerror("Could not initialize afb_instance event handler: %d", -ret);
185 atexit([] { delete g_afb_instance; });
190 delete g_afb_instance;
191 g_afb_instance = nullptr;
195 int binding_init() noexcept {
197 return binding_init_();
198 } catch (std::exception &e) {
199 logerror("Uncaught exception in binding_init(): %s", e.what());
205 // __| | ___| |__ _ _ __ _ ___| |_ __ _| |_ _ _ ___ / /\ \
206 // / _` |/ _ \ '_ \| | | |/ _` | / __| __/ _` | __| | | / __| | | |
207 // | (_| | __/ |_) | |_| | (_| | \__ \ || (_| | |_| |_| \__ \ | | |
208 // \__,_|\___|_.__/ \__,_|\__, |___|___/\__\__,_|\__|\__,_|___/ | | |
209 // |___/_____| \_\/_/
210 void debug_status(struct afb_req req) {
211 // Quick and dirty, dump current surfaces and layers
212 AFB_REQ_DEBUG(req, "status");
214 auto o = json_object_new_object();
215 json_object_object_add(o, "surfaces",
216 to_json(g_afb_instance->controller->sprops));
217 json_object_object_add(o, "layers", to_json(g_afb_instance->controller->lprops));
218 // json_object_object_add(o, "screens",
219 // to_json(g_afb_instance->controller->screens));
221 afb_req_success(req, o, "status");
224 void debug_surfaces(afb_req req) {
225 afb_req_success(req, to_json(g_afb_instance->controller->sprops), "surfaces");
228 void debug_layers(afb_req req) {
229 afb_req_success(req, to_json(g_afb_instance->controller->lprops), "layers");
232 // Dummy register_surface implementation
233 void register_surface(afb_req req) {
234 AFB_DEBUG("register_surface");
236 auto jo = afb_req_json(req);
238 if (! json_object_object_get_ex(jo, "appid", &jappid)) {
239 afb_req_fail(req, "failed", "register_surface needs 'appid' integer argument");
243 json_object *jsurfid;
244 if (! json_object_object_get_ex(jo, "surfaceid", &jsurfid)) {
245 afb_req_fail(req, "failed", "register_surface needs 'surfaceid' integer argument");
249 uint32_t appid = json_object_get_int(jappid);
250 uint32_t surfid = json_object_get_int(jsurfid);
253 afb_req_fail(req, "failed", "invalid appid");
257 if (surfid > 0xffff) {
258 afb_req_fail(req, "failed", "invalid surfaceid");
262 lognotice("register_surface, got appid %d and surfaceid %d", appid, surfid);
264 afb_req_success(req, json_object_new_int((appid << 16) + surfid), "success");
268 [](afb_req req) noexcept { \
269 if (g_afb_instance == nullptr) { \
270 afb_req_fail(req, "failed", \
271 "Binding not initialized, did the compositor die?"); \
276 } catch (std::exception & e) { \
277 afb_req_fail_f(req, "failed", "Uncaught exception: %s", e.what()); \
281 const struct afb_verb_v2 verbs[] = {
282 {"debug::status", WRAP(debug_status), NULL, NULL, AFB_SESSION_NONE_V2},
283 {"debug::layers", WRAP(debug_layers), NULL, NULL, AFB_SESSION_NONE_V2},
284 {"debug::surfaces", WRAP(debug_surfaces), NULL, NULL, AFB_SESSION_NONE_V2},
286 {"register_surface", WRAP(register_surface), NULL, NULL, AFB_SESSION_NONE_V2},
291 extern "C" const struct afb_binding_v2 afbBindingV2 = {
292 "winman", NULL, NULL, verbs, NULL, binding_init, NULL, 1};