main: wrap afb verb handlers, "namepace" debug verbs
[staging/windowmanager.git] / src / main.cpp
index 1ac26f1..bf78b52 100644 (file)
@@ -1,18 +1,16 @@
+#include "json_helper.hpp"
 #include "util.hpp"
 #include "wayland.hpp"
 
 #include <algorithm>
 #include <json.h>
 
-#define AFB_BINDING_VERSION 2
-
 extern "C" {
+#define AFB_BINDING_VERSION 2
 #include <afb/afb-binding.h>
 #include <systemd/sd-event.h>
 }
 
-#include <json.hpp>
-
 namespace {
 struct wayland {
    std::unique_ptr<wl::display> display;
@@ -178,8 +176,7 @@ int binding_init_() {
                                 g_wayland->display->get_fd(), EPOLLIN,
                                 display_event_callback, g_wayland);
       if (ret < 0) {
-                 AFB_ERROR("Could not initialize wayland event handler: %s",
-                           std::strerror(-ret));
+         AFB_ERROR("Could not initialize wayland event handler: %d", -ret);
          goto error;
       }
    }
@@ -218,79 +215,42 @@ int binding_init() noexcept {
 // | (_| |  __/ |_) | |_| | (_| |   \__ \ || (_| | |_| |_| \__ \ |  | |
 //  \__,_|\___|_.__/ \__,_|\__, |___|___/\__\__,_|\__|\__,_|___/ |  | |
 //                         |___/_____|                          \_\/_/
-void debug_status(struct afb_req req) noexcept {
+void debug_status(struct afb_req req) {
    // Quick and dirty, dump current surfaces and layers
    AFB_REQ_DEBUG(req, "status");
 
-   CHECK_WAYLAND();
+   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));
 
-   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());
-   }
+   afb_req_success(req, o, "status");
 }
 
-void debug_surfaces(afb_req req) noexcept {
-   CHECK_WAYLAND();
-
-   auto a = json_object_new_array();
-
-   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));
-      }
-   }
-
-   afb_req_success(req, a, "surfaces");
+void debug_surfaces(afb_req req) {
+   afb_req_success(req, to_json(g_wayland->controller->surfaces), "surfaces");
 }
 
-void debug_layers(afb_req req) noexcept {
-   CHECK_WAYLAND();
-
-   auto a = json_object_new_array();
+void debug_layers(afb_req req) {
+   afb_req_success(req, to_json(g_wayland->controller->layers), "layers");
+}
 
-   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));
-      }
+#define WRAP(F)                                                             \
+   [](afb_req req) noexcept {                                               \
+      CHECK_WAYLAND();                                                      \
+      try {                                                                 \
+         F(req);                                                            \
+      } catch (std::exception & e) {                                        \
+         afb_req_fail_f(req, "failed", "Uncaught exception: %s", e.what()); \
+      }                                                                     \
    }
 
-   afb_req_success(req, a, "surfaces");
-}
-
 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},
+   {"debug::status", WRAP(debug_status), NULL, NULL, AFB_SESSION_NONE_V2},
+   {"debug::layers", WRAP(debug_layers), NULL, NULL, AFB_SESSION_NONE_V2},
+   {"debug::surfaces", WRAP(debug_surfaces), NULL, NULL, AFB_SESSION_NONE_V2},
    {},
 };
 }  // namespace