Move all nlohmann::json to json_helper.cpp
[staging/windowmanager.git] / src / main.cpp
index 1ac26f1..a2331c4 100644 (file)
@@ -1,3 +1,4 @@
+#include "json_helper.hpp"
 #include "util.hpp"
 #include "wayland.hpp"
 
@@ -11,8 +12,6 @@ extern "C" {
 #include <systemd/sd-event.h>
 }
 
-#include <json.hpp>
-
 namespace {
 struct wayland {
    std::unique_ptr<wl::display> display;
@@ -178,8 +177,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;
       }
    }
@@ -224,67 +222,26 @@ void debug_status(struct afb_req req) noexcept {
 
    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;
-      }
+   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, 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");
+   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();
-
-   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));
-      }
-   }
-
-   afb_req_success(req, a, "surfaces");
+   afb_req_success(req, to_json(g_wayland->controller->layers), "layers");
 }
 
 const struct afb_verb_v2 verbs[] = {