+
+error:
+ sd_event_source_unref(evs);
+ return -1;
+}
+
+// _ _ _ _ _ _ _ ____
+// | |__ (_)_ __ __| (_)_ __ __ _ (_)_ __ (_) |_ / /\ \
+// | '_ \| | '_ \ / _` | | '_ \ / _` | | | '_ \| | __| | | |
+// | |_) | | | | | (_| | | | | | (_| | | | | | | | |_| | | |
+// |_.__/|_|_| |_|\__,_|_|_| |_|\__, |___|_|_| |_|_|\__| | | |
+// |___/_____| \_\/_/
+int binding_init_() {
+ lognotice("WinMan ver. %s", WINMAN_VERSION_STRING);
+
+ if (g_wayland != nullptr) {
+ AFB_ERROR("Wayland context already initialized?");
+ return 0;
+ }
+
+ if (getenv("XDG_RUNTIME_DIR") == nullptr) {
+ AFB_ERROR("Environment variable XDG_RUNTIME_DIR not set");
+ goto error;
+ }
+
+ g_wayland = new wayland;
+ if (g_wayland->init() == -1) {
+ AFB_ERROR("Could not connect to compositor");
+ goto error;
+ }
+
+ if (char const *e = init_layout()) {
+ AFB_ERROR("Could not init layout: %s", e);
+ goto error;
+ }
+
+ {
+ int ret = sd_event_add_io(afb_daemon_get_event_loop(), nullptr,
+ g_wayland->display->get_fd(), EPOLLIN,
+ display_event_callback, g_wayland);
+ if (ret < 0) {
+ AFB_ERROR("Could not initialize wayland event handler: %d", -ret);
+ goto error;
+ }
+ }
+
+ atexit([] { delete g_wayland; });
+
+ return 0;
+
+error:
+ delete g_wayland;
+ g_wayland = nullptr;
+ return -1;
+}
+
+int binding_init() noexcept {
+ try {
+ return binding_init_();
+ } catch (std::exception &e) {
+ AFB_ERROR("Uncaught exception in binding_init(): %s", e.what());
+ }
+ return -1;
+}
+
+#define CHECK_WAYLAND() \
+ do { \
+ if (g_wayland == nullptr) { \
+ afb_req_fail(req, "failed", \
+ "Binding not initialized, did the compositor die?"); \
+ return; \
+ } \
+ } while (0)
+
+// _ _ _ _ ____
+// __| | ___| |__ _ _ __ _ ___| |_ __ _| |_ _ _ ___ / /\ \
+// / _` |/ _ \ '_ \| | | |/ _` | / __| __/ _` | __| | | / __| | | |
+// | (_| | __/ |_) | |_| | (_| | \__ \ || (_| | |_| |_| \__ \ | | |
+// \__,_|\___|_.__/ \__,_|\__, |___|___/\__\__,_|\__|\__,_|___/ | | |
+// |___/_____| \_\/_/
+void debug_status(struct afb_req req) {
+ // Quick and dirty, dump current surfaces and layers
+ AFB_REQ_DEBUG(req, "status");
+
+ auto o = json_object_new_object();
+ json_object_object_add(o, "surfaces",
+ to_json(g_wayland->controller->surfaces));
+ json_object_object_add(o, "layers", to_json(g_wayland->controller->layers));
+ json_object_object_add(o, "screens",
+ to_json(g_wayland->controller->screens));
+
+ afb_req_success(req, o, "status");
+}
+
+void debug_surfaces(afb_req req) {
+ afb_req_success(req, to_json(g_wayland->controller->surfaces), "surfaces");
+}
+
+void debug_layers(afb_req req) {
+ afb_req_success(req, to_json(g_wayland->controller->layers), "layers");