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;
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();
59 // Init the app's members
60 this->app.display = this->display.get();
61 this->app.controller = this->controller.get();
67 // (_)_ __ (_) |_ | | __ _ _ _ ___ _ _| |_ / /\ \
68 // | | '_ \| | __| | |/ _` | | | |/ _ \| | | | __| | | |
69 // | | | | | | |_ | | (_| | |_| | (_) | |_| | |_| | | |
70 // |_|_| |_|_|\__|___|_|\__,_|\__, |\___/ \__,_|\__| | | |
71 // |_____| |___/ \_\/_/
72 char const *init_layout() {
73 if (!g_afb_instance->controller) {
74 return "ivi_controller global not available";
77 if (g_afb_instance->outputs.empty()) {
78 return "no output was set up!";
81 auto &c = g_afb_instance->controller;
83 auto &o = g_afb_instance->outputs.front();
84 auto &s = c->screens.begin()->second;
85 auto &layers = c->layers;
87 // XXX: Write output dimensions to ivi controller...
88 c->output_size = genivi::size{uint32_t(o->width), uint32_t(o->height)};
96 // Setup our dummy scene...
97 c->layer_create(100, 0, 0); // bottom layer, anything else
98 c->layer_create(1000, 0, 0); // top layer, mandelbrot
100 auto &l100 = c->layers[100];
101 auto &l1k = c->layers[1000];
103 // Set layers fullscreen
104 l100->set_destination_rectangle(0, 0, o->width, o->height);
105 l1k->set_destination_rectangle(0, 0, o->width, o->height);
106 l100->set_visibility(1);
107 l1k->set_visibility(1);
109 // Add layers to screen
110 s->set_render_order({100, 1000});
114 g_afb_instance->display->flush();
119 int display_event_callback(sd_event_source *evs, int fd, uint32_t events,
121 if ((events & EPOLLHUP) != 0) {
122 logerror("The compositor hung up, dying now.");
123 delete g_afb_instance;
124 g_afb_instance = nullptr;
128 if (events & EPOLLIN) {
129 int ret = g_afb_instance->display->dispatch();
131 logerror("wl_display_dipatch() returned error %d",
132 g_afb_instance->display->get_error());
135 g_afb_instance->display->flush();
137 // execute pending tasks, that is layout changes etc.
138 g_afb_instance->controller->execute_pending();
139 g_afb_instance->display->roundtrip();
145 sd_event_source_unref(evs);
149 // _ _ _ _ _ _ _ ____
150 // | |__ (_)_ __ __| (_)_ __ __ _ (_)_ __ (_) |_ / /\ \
151 // | '_ \| | '_ \ / _` | | '_ \ / _` | | | '_ \| | __| | | |
152 // | |_) | | | | | (_| | | | | | (_| | | | | | | | |_| | | |
153 // |_.__/|_|_| |_|\__,_|_|_| |_|\__, |___|_|_| |_|_|\__| | | |
154 // |___/_____| \_\/_/
155 int binding_init_() {
156 lognotice("WinMan ver. %s", WINMAN_VERSION_STRING);
158 if (g_afb_instance != nullptr) {
159 logerror("Wayland context already initialized?");
163 if (getenv("XDG_RUNTIME_DIR") == nullptr) {
164 logerror("Environment variable XDG_RUNTIME_DIR not set");
168 g_afb_instance = new afb_instance;
169 if (g_afb_instance->init() == -1) {
170 logerror("Could not connect to compositor");
174 if (char const *e = init_layout()) {
175 logerror("Could not init layout: %s", e);
180 int ret = sd_event_add_io(afb_daemon_get_event_loop(), nullptr,
181 g_afb_instance->display->get_fd(), EPOLLIN,
182 display_event_callback, g_afb_instance);
184 logerror("Could not initialize afb_instance event handler: %d", -ret);
189 atexit([] { delete g_afb_instance; });
194 delete g_afb_instance;
195 g_afb_instance = nullptr;
199 int binding_init() noexcept {
201 return binding_init_();
202 } catch (std::exception &e) {
203 logerror("Uncaught exception in binding_init(): %s", e.what());
210 #include "afb_binding.inl"
212 extern "C" const struct afb_binding_v2 afbBindingV2 = {
213 "winman", NULL, NULL, winman_verbs, NULL, binding_init, NULL, 1};