main: reorganize and simplify
[staging/windowmanager.git] / src / main.cpp
index 794d9af..03aa202 100644 (file)
-#include "util.h"
+#include "util.hpp"
+#include "wayland.hpp"
 
-#include "ivi-controller-client-protocol.h"
+#include <algorithm>
+#include <json.h>
 
-#include <stdlib.h>
-#include <string.h>
+#define AFB_BINDING_VERSION 2
 
-#include <wayland-client.h>
+extern "C" {
+#include <afb/afb-binding.h>
+#include <systemd/sd-event.h>
+}
 
-#include <map>
-#include <memory>
-#include <string>
-#include <vector>
+#include <json.hpp>
 
-struct ivi_surface;
-struct ivi_layer;
+namespace {
+struct wayland {
+   std::unique_ptr<wl::display> display;
+   std::unique_ptr<genivi::controller> controller;
+   std::vector<std::unique_ptr<wl::output>> outputs;
 
-struct conn {
-   wl_display *d;
-   wl_registry *r;
-   ivi_controller *c;
-   std::vector<std::unique_ptr<wl_output, void (*)(wl_output *)>> outputs;
-   std::map<uint32_t, std::unique_ptr<ivi_layer>> layers;
-   std::map<uint32_t, std::unique_ptr<ivi_surface>> surfaces;
+   wayland() : display(new wl::display), controller(nullptr), outputs() {}
 
-   ~conn();
+   int init();
 };
 
-struct ivi_surface {
-   ivi_controller_surface *controller;
-   uint32_t id;
-   conn *con;
-   ivi_surface(ivi_controller_surface *c, uint32_t i, conn *co)
-      : controller(c), id(i), con(co) {}
-   ~ivi_surface() { ivi_controller_surface_destroy(this->controller, 1); }
-};
+struct wayland *g_wayland;
 
-struct ivi_layer {
-   ivi_controller_layer *controller;
-   uint32_t id;
-   conn *con;
-   ivi_layer(ivi_controller_layer *c, uint32_t i, conn *co)
-      : controller(c), id(i), con(co) {}
-   ~ivi_layer() { ivi_controller_layer_destroy(this->controller, 1); }
-};
+int wayland::init() {
+   if (!this->display->ok()) {
+      return -1;
+   }
 
-conn::~conn() {
-   this->layers.clear();
-   this->surfaces.clear();
-   ivi_controller_destroy(this->c);
-   this->outputs.clear();
-   wl_registry_destroy(this->r);
-   wl_display_disconnect(this->d);
+   this->display->r.add_global_handler("wl_output", [](wl_registry *r,
+                                                       uint32_t name,
+                                                       uint32_t v) {
+      g_wayland->outputs.emplace_back(std::make_unique<wl::output>(r, name, v));
+   });
+
+   this->display->r.add_global_handler(
+      "ivi_controller", [](wl_registry *r, uint32_t name, uint32_t v) {
+         g_wayland->controller =
+            std::make_unique<genivi::controller>(r, name, v);
+
+         // XXX: This protocol needs the output, so lets just add our mapping
+         // here...
+         g_wayland->controller->add_proxy_to_id_mapping(
+            g_wayland->outputs.back()->proxy.get(),
+            wl_proxy_get_id(reinterpret_cast<struct wl_proxy *>(
+               g_wayland->outputs.back()->proxy.get())));
+      });
+
+   // First level objects
+   this->display->roundtrip();
+   // Second level objects
+   this->display->roundtrip();
+   // Third level objects
+   this->display->roundtrip();
+
+   return 0;
 }
 
-static ivi_controller_surface_listener cs_listener = {};
+//  _       _ _       _                         _    ____
+// (_)_ __ (_) |_    | | __ _ _   _  ___  _   _| |_ / /\ \
+// | | '_ \| | __|   | |/ _` | | | |/ _ \| | | | __| |  | |
+// | | | | | | |_    | | (_| | |_| | (_) | |_| | |_| |  | |
+// |_|_| |_|_|\__|___|_|\__,_|\__, |\___/ \__,_|\__| |  | |
+//              |_____|       |___/                 \_\/_/
+char const *init_layout() {
+   if (!g_wayland->controller) {
+      return "ivi_controller global not available";
+   }
 
-static ivi_controller_layer_listener cl_listener = {};
+   if (g_wayland->outputs.empty()) {
+      return "no output was set up!";
+   }
 
-static void c_screen(void *data, struct ivi_controller *ivi_controller,
-                     uint32_t id_screen, struct ivi_controller_screen *screen) {
-   lognotice("ivi_controller @ %p screen %u (%x) @ %p", ivi_controller,
-             id_screen, id_screen, screen);
-}
+   auto &c = g_wayland->controller;
 
-static void c_layer(void *data, struct ivi_controller *ivi_controller,
-                    uint32_t id_layer) {
-   lognotice("ivi_controller @ %p layer %u (%x)", ivi_controller, id_layer,
-             id_layer);
-   auto c = static_cast<conn *>(data);
-   auto i = std::make_unique<ivi_layer>(
-      ivi_controller_layer_create(c->c, id_layer, 0, 0), id_layer, c);
-   ivi_controller_layer_add_listener(i->controller, &cl_listener, i.get());
-   c->layers[id_layer] = std::move(i);
-}
+   auto &o = g_wayland->outputs.front();
+   auto &s = c->screens.begin()->second;
+   auto &layers = c->layers;
 
-static void c_surface(void *data, struct ivi_controller *ivi_controller,
-                      uint32_t id_surface) {
-   lognotice("ivi_controller @ %p surface %u (%x)", ivi_controller, id_surface,
-             id_surface);
-   auto c = static_cast<conn *>(data);
-   auto i = std::make_unique<ivi_surface>(
-      ivi_controller_surface_create(c->c, id_surface), id_surface, c);
-   ivi_controller_surface_add_listener(i->controller, &cs_listener, i.get());
-   c->surfaces[id_surface] = std::move(i);
-}
+   // XXX: Write output dimensions to ivi controller...
+   c->output_size = genivi::size{uint32_t(o->width), uint32_t(o->height)};
 
-static void c_error(void *data, struct ivi_controller *ivi_controller,
-                    int32_t object_id, int32_t object_type, int32_t error_code,
-                    const char *error_text) {
-   lognotice("ivi_controller @ %p error o %i t %i c %i text %s", ivi_controller,
-             object_id, object_type, error_code, error_text);
-}
+   // Clear scene
+   layers.clear();
+
+   // Clear screen
+   s->clear();
+
+   // Setup our dummy scene...
+   c->layer_create(100, 0, 0);   // bottom layer, anything else
+   c->layer_create(1000, 0, 0);  // top layer, mandelbrot
+
+   auto &l100 = c->layers[100];
+   auto &l1k = c->layers[1000];
+
+   // Set layers fullscreen
+   l100->set_destination_rectangle(0, 0, o->width, o->height);
+   l1k->set_destination_rectangle(0, 0, o->width, o->height);
+   l100->set_visibility(1);
+   l1k->set_visibility(1);
+
+   // Add layers to screen
+   s->set_render_order({100, 1000});
 
-static struct ivi_controller_listener c_listener = {c_screen, c_layer,
-                                                    c_surface, c_error};
+   c->commit_changes();
 
-static void o_geometry(void *data, struct wl_output *wl_output, int32_t x,
-                       int32_t y, int32_t physical_width,
-                       int32_t physical_height, int32_t subpixel,
-                       const char *make, const char *model, int32_t transform) {
-   lognotice("output @ %p x %i y %i w %i h %i spel %x make %s model %s tx %i",
-             wl_output, x, y, physical_width, physical_height, subpixel, make,
-             model, transform);
+   g_wayland->display->flush();
+
+   return nullptr;
 }
 
-static void o_mode(void *data, struct wl_output *wl_output, uint32_t flags,
-                   int32_t width, int32_t height, int32_t refresh) {
-   lognotice("output @ %p mode f %x w %i h %i r %i", wl_output, flags, width,
-             height, refresh);
+int display_event_callback(sd_event_source *evs, int fd, uint32_t events,
+                           void *data) {
+   if ((events & EPOLLHUP) != 0) {
+      AFB_ERROR("The compositor hung up, dying now.");
+      delete g_wayland;
+      g_wayland = nullptr;
+      goto error;
+   }
+
+   if (events & EPOLLIN) {
+      int ret = g_wayland->display->dispatch();
+      if (ret == -1) {
+         AFB_ERROR("wl_display_dipatch() returned error %d",
+                   g_wayland->display->get_error());
+         goto error;
+      }
+      g_wayland->display->flush();
+
+      // execute pending tasks, that is layout changes etc.
+      g_wayland->controller->execute_pending();
+      g_wayland->display->roundtrip();
+   }
+
+   return 0;
+
+error:
+   sd_event_source_unref(evs);
+   return -1;
 }
 
-static void o_done(void *data, struct wl_output *wl_output) {
-   lognotice("output @ %p done");
+//  _     _           _ _                 _       _ _    ____
+// | |__ (_)_ __   __| (_)_ __   __ _    (_)_ __ (_) |_ / /\ \
+// | '_ \| | '_ \ / _` | | '_ \ / _` |   | | '_ \| | __| |  | |
+// | |_) | | | | | (_| | | | | | (_| |   | | | | | | |_| |  | |
+// |_.__/|_|_| |_|\__,_|_|_| |_|\__, |___|_|_| |_|_|\__| |  | |
+//                              |___/_____|             \_\/_/
+int binding_init_() {
+   lognotice("WinMan ver. %s", WINMAN_VERSION_STRING);
+
+   if (g_wayland != nullptr) {
+      AFB_ERROR("Wayland context already initialized?");
+      return -1;
+   }
+
+   if (getenv("XDG_RUNTIME_DIR") == nullptr) {
+      AFB_ERROR("Environment variable XDG_RUNTIME_DIR not set");
+      return -1;
+   }
+
+   g_wayland = new wayland;
+   if (g_wayland->init() == -1) {
+      AFB_ERROR("Could not connect to compositor");
+      delete g_wayland;
+      return -1;
+   }
+
+   if (char const *e = init_layout()) {
+      AFB_ERROR("Could not init layout: %s", e);
+      return -1;
+   }
+
+   sd_event_add_io(afb_daemon_get_event_loop(), nullptr,
+                   g_wayland->display->get_fd(), EPOLLIN,
+                   display_event_callback, g_wayland);
+
+   atexit([] { delete g_wayland; });
+
+   return 0;
 }
 
-static void o_scale(void *data, struct wl_output *wl_output, int32_t factor) {
-   lognotice("output @ %p scale %i", wl_output, factor);
+int binding_init() noexcept {
+   try {
+      return binding_init_();
+   } catch (std::exception &e) {
+      AFB_ERROR("Uncaught exception in binding_init(): %s", e.what());
+   }
+   return -1;
 }
 
-static struct wl_output_listener o_listener = {o_geometry, o_mode, o_done,
-                                               o_scale};
-
-static void r_global(void *data, struct wl_registry *r, uint32_t name,
-                     char const *iface, uint32_t v) {
-   struct conn *c = static_cast<conn *>(data);
-
-   if (strcmp(iface, "ivi_controller") == 0) {
-      c->c = static_cast<ivi_controller *>(
-         wl_registry_bind(r, name, &ivi_controller_interface, v));
-      ivi_controller_add_listener(c->c, &c_listener, c);
-   } else if (strcmp(iface, "wl_output") == 0) {
-      auto o = static_cast<wl_output *>(
-         wl_registry_bind(r, name, &wl_output_interface, v));
-      c->outputs.emplace_back(std::unique_ptr<wl_output, void (*)(wl_output *)>(
-         o, wl_output_destroy));
-      wl_output_add_listener(o, &o_listener, c);
-   } else {
-      lognotice("registry @ %p global n %u i %s v %u", r, name, iface, v);
+#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) noexcept {
+   // Quick and dirty, dump current surfaces and layers
+   AFB_REQ_DEBUG(req, "status");
+
+   CHECK_WAYLAND();
+
+   try {
+      using json = nlohmann::json;
+
+      json j;
+
+      if (!g_wayland->controller->surfaces.empty()) {
+         auto a = json::array();
+         for (auto const &i : g_wayland->controller->surfaces) {
+            auto const &r = i.second->dst_rect;
+            auto const &s = i.second->size;
+            a.push_back({{"id", i.first},
+                         {"size", {s.w, s.h}},
+                         {"dst_rect", {r.w, r.h, r.x, r.y}}});
+         }
+         j["surfaces"] = a;
+      }
+
+      if (!g_wayland->controller->layers.empty()) {
+         auto a = json::array();
+         for (auto const &i : g_wayland->controller->layers) {
+            auto const &r = i.second->dst_rect;
+            auto const &s = i.second->size;
+            a.push_back({{"id", i.first},
+                          {"size", {s.w, s.h}},
+                          {"dst_rect", {r.w, r.h, r.x, r.y}}});
+         }
+         j["layers"] = a;
+      }
+
+      afb_req_success(req, json_tokener_parse(j.dump().c_str()), "status");
+   } catch (std::exception &e) {
+      afb_req_fail_f(req, "failed", "Uncaught exception: %s", e.what());
    }
 }
 
-static void r_global_remove(void *data, struct wl_registry *r, uint32_t name) {}
+void debug_surfaces(afb_req req) {
+   CHECK_WAYLAND();
 
-static struct wl_registry_listener r_listener = {r_global, r_global_remove};
+   auto a = json_object_new_array();
 
-int main(int argc, char **argv) {
-   lognotice("WinMan ver. %s", WINMAN_VERSION_STRING);
+   if (!g_wayland->controller->surfaces.empty()) {
+      for (auto const &i : g_wayland->controller->surfaces) {
+         json_object_array_add(a, json_object_new_int(i.first));
+      }
+   }
 
-   if (!getenv("XDG_RUNTIME_DIR"))
-      fatal("Environment variable XDG_RUNTIME_DIR not set");
+   afb_req_success(req, a, "surfaces");
+}
 
-   struct conn c = {};
+void debug_layers(afb_req req) {
+   CHECK_WAYLAND();
 
-   c.d = wl_display_connect(NULL);
-   if (!c.d)
-      fatal("Could not connect to compositor");
-   c.r = wl_display_get_registry(c.d);
-   wl_registry_add_listener(c.r, &r_listener, &c);
+   auto a = json_object_new_array();
 
-   // First level objects
-   wl_display_roundtrip(c.d);
-   // Second level objects
-   wl_display_roundtrip(c.d);
+   if (!g_wayland->controller->layers.empty()) {
+      for (auto const &i : g_wayland->controller->layers) {
+         json_object_array_add(a, json_object_new_int(i.first));
+      }
+   }
 
-   if (!c.c)
-      fatal("ivi_controller global not available");
+   afb_req_success(req, a, "surfaces");
+}
 
-   // main loop
+const struct afb_verb_v2 verbs[] = {
+   {"status", debug_status, NULL, NULL, AFB_SESSION_NONE_V2},
+   {"layers", debug_layers, NULL, NULL, AFB_SESSION_NONE_V2},
+   {"surfaces", debug_surfaces, NULL, NULL, AFB_SESSION_NONE_V2},
+   {},
+};
+}  // namespace
 
-   return 0;
-}
+extern "C" const struct afb_binding_v2 afbBindingV2 = {
+   "winman", NULL, NULL, verbs, NULL, binding_init, NULL, 1};