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;
19 wayland() : display(new wl::display), controller(nullptr), outputs() {}
24 struct wayland *g_wayland;
27 if (!this->display->ok()) {
31 this->display->r.add_global_handler("wl_output", [](wl_registry *r,
34 g_wayland->outputs.emplace_back(std::make_unique<wl::output>(r, name, v));
37 this->display->r.add_global_handler(
38 "ivi_controller", [](wl_registry *r, uint32_t name, uint32_t v) {
39 g_wayland->controller =
40 std::make_unique<genivi::controller>(r, name, v);
42 // XXX: This protocol needs the output, so lets just add our mapping
44 g_wayland->controller->add_proxy_to_id_mapping(
45 g_wayland->outputs.back()->proxy.get(),
46 wl_proxy_get_id(reinterpret_cast<struct wl_proxy *>(
47 g_wayland->outputs.back()->proxy.get())));
50 // First level objects
51 this->display->roundtrip();
52 // Second level objects
53 this->display->roundtrip();
54 // Third level objects
55 this->display->roundtrip();
61 // (_)_ __ (_) |_ | | __ _ _ _ ___ _ _| |_ / /\ \
62 // | | '_ \| | __| | |/ _` | | | |/ _ \| | | | __| | | |
63 // | | | | | | |_ | | (_| | |_| | (_) | |_| | |_| | | |
64 // |_|_| |_|_|\__|___|_|\__,_|\__, |\___/ \__,_|\__| | | |
65 // |_____| |___/ \_\/_/
66 char const *init_layout() {
67 if (!g_wayland->controller) {
68 return "ivi_controller global not available";
71 if (g_wayland->outputs.empty()) {
72 return "no output was set up!";
75 auto &c = g_wayland->controller;
77 auto &o = g_wayland->outputs.front();
78 auto &s = c->screens.begin()->second;
79 auto &layers = c->layers;
81 // XXX: Write output dimensions to ivi controller...
82 c->output_size = genivi::size{uint32_t(o->width), uint32_t(o->height)};
90 // Setup our dummy scene...
91 c->layer_create(100, 0, 0); // bottom layer, anything else
92 c->layer_create(1000, 0, 0); // top layer, mandelbrot
94 auto &l100 = c->layers[100];
95 auto &l1k = c->layers[1000];
97 // Set layers fullscreen
98 l100->set_destination_rectangle(0, 0, o->width, o->height);
99 l1k->set_destination_rectangle(0, 0, o->width, o->height);
100 l100->set_visibility(1);
101 l1k->set_visibility(1);
103 // Add layers to screen
104 s->set_render_order({100, 1000});
108 g_wayland->display->flush();
113 int display_event_callback(sd_event_source *evs, int fd, uint32_t events,
115 if ((events & EPOLLHUP) != 0) {
116 logerror("The compositor hung up, dying now.");
122 if (events & EPOLLIN) {
123 int ret = g_wayland->display->dispatch();
125 logerror("wl_display_dipatch() returned error %d",
126 g_wayland->display->get_error());
129 g_wayland->display->flush();
131 // execute pending tasks, that is layout changes etc.
132 g_wayland->controller->execute_pending();
133 g_wayland->display->roundtrip();
139 sd_event_source_unref(evs);
143 // _ _ _ _ _ _ _ ____
144 // | |__ (_)_ __ __| (_)_ __ __ _ (_)_ __ (_) |_ / /\ \
145 // | '_ \| | '_ \ / _` | | '_ \ / _` | | | '_ \| | __| | | |
146 // | |_) | | | | | (_| | | | | | (_| | | | | | | | |_| | | |
147 // |_.__/|_|_| |_|\__,_|_|_| |_|\__, |___|_|_| |_|_|\__| | | |
148 // |___/_____| \_\/_/
149 int binding_init_() {
150 lognotice("WinMan ver. %s", WINMAN_VERSION_STRING);
152 if (g_wayland != nullptr) {
153 logerror("Wayland context already initialized?");
157 if (getenv("XDG_RUNTIME_DIR") == nullptr) {
158 logerror("Environment variable XDG_RUNTIME_DIR not set");
162 g_wayland = new wayland;
163 if (g_wayland->init() == -1) {
164 logerror("Could not connect to compositor");
168 if (char const *e = init_layout()) {
169 logerror("Could not init layout: %s", e);
174 int ret = sd_event_add_io(afb_daemon_get_event_loop(), nullptr,
175 g_wayland->display->get_fd(), EPOLLIN,
176 display_event_callback, g_wayland);
178 logerror("Could not initialize wayland event handler: %d", -ret);
183 atexit([] { delete g_wayland; });
193 int binding_init() noexcept {
195 return binding_init_();
196 } catch (std::exception &e) {
197 logerror("Uncaught exception in binding_init(): %s", e.what());
202 #define CHECK_WAYLAND() \
204 if (g_wayland == nullptr) { \
205 afb_req_fail(req, "failed", \
206 "Binding not initialized, did the compositor die?"); \
212 // __| | ___| |__ _ _ __ _ ___| |_ __ _| |_ _ _ ___ / /\ \
213 // / _` |/ _ \ '_ \| | | |/ _` | / __| __/ _` | __| | | / __| | | |
214 // | (_| | __/ |_) | |_| | (_| | \__ \ || (_| | |_| |_| \__ \ | | |
215 // \__,_|\___|_.__/ \__,_|\__, |___|___/\__\__,_|\__|\__,_|___/ | | |
216 // |___/_____| \_\/_/
217 void debug_status(struct afb_req req) {
218 // Quick and dirty, dump current surfaces and layers
219 AFB_REQ_DEBUG(req, "status");
221 auto o = json_object_new_object();
222 json_object_object_add(o, "surfaces",
223 to_json(g_wayland->controller->surfaces));
224 json_object_object_add(o, "layers", to_json(g_wayland->controller->layers));
225 json_object_object_add(o, "screens",
226 to_json(g_wayland->controller->screens));
228 afb_req_success(req, o, "status");
231 void debug_surfaces(afb_req req) {
232 afb_req_success(req, to_json(g_wayland->controller->surfaces), "surfaces");
235 void debug_layers(afb_req req) {
236 afb_req_success(req, to_json(g_wayland->controller->layers), "layers");
240 [](afb_req req) noexcept { \
244 } catch (std::exception & e) { \
245 afb_req_fail_f(req, "failed", "Uncaught exception: %s", e.what()); \
249 const struct afb_verb_v2 verbs[] = {
250 {"debug::status", WRAP(debug_status), NULL, NULL, AFB_SESSION_NONE_V2},
251 {"debug::layers", WRAP(debug_layers), NULL, NULL, AFB_SESSION_NONE_V2},
252 {"debug::surfaces", WRAP(debug_surfaces), NULL, NULL, AFB_SESSION_NONE_V2},
257 extern "C" const struct afb_binding_v2 afbBindingV2 = {
258 "winman", NULL, NULL, verbs, NULL, binding_init, NULL, 1};