From 9dfa6b9427115e7402ce25e40e4d78b20d559c93 Mon Sep 17 00:00:00 2001 From: Marcus Fritzsch Date: Thu, 6 Jul 2017 11:32:50 +0200 Subject: [PATCH] Move all nlohmann::json to json_helper.cpp Signed-off-by: Marcus Fritzsch --- src/CMakeLists.txt | 2 +- src/json_helper.cpp | 72 +++++++++++++++++++++++++++++++++++++++++++++++++++++ src/json_helper.hpp | 16 ++++++++++++ src/main.cpp | 65 ++++++++--------------------------------------- src/wayland.hpp | 20 ++++++++++----- 5 files changed, 114 insertions(+), 61 deletions(-) create mode 100644 src/json_helper.cpp create mode 100644 src/json_helper.hpp diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 4a0eafe..17796be 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -13,7 +13,7 @@ add_library(winman MODULE wayland.hpp util.cpp util.hpp - ${IVI_CON_PROTO}) + ${IVI_CON_PROTO} json_helper.cpp json_helper.hpp) target_include_directories(winman PRIVATE diff --git a/src/json_helper.cpp b/src/json_helper.cpp new file mode 100644 index 0000000..823c030 --- /dev/null +++ b/src/json_helper.cpp @@ -0,0 +1,72 @@ +#include "json_helper.hpp" + +#include + +#include + +using json = nlohmann::json; + +template +json_object *to_json_(T const *s) { + auto j = json::object({ + {"id", s->id}, + {"size", {{"width", s->size.w}, {"height", s->size.h}}}, + {"dst", + {{"width", s->dst_rect.w}, + {"height", s->dst_rect.h}, + {"x", s->dst_rect.x}, + {"y", s->dst_rect.y}}}, + {"src", + {{"width", s->src_rect.w}, + {"height", s->src_rect.h}, + {"x", s->src_rect.x}, + {"y", s->src_rect.y}}}, + {"visibility", s->visibility}, + {"opacity", s->opacity}, + {"orientation", s->orientation}, + }); + return json_tokener_parse(j.dump().c_str()); +} + +json_object *to_json(genivi::surface const *s) { return to_json_(s); } + +json_object *to_json(genivi::layer const *l) { return to_json_(l); } + +json_object *to_json(genivi::screen const *s) { + auto o = json_object_new_object(); + json_object_object_add(o, "id", json_object_new_int(s->id)); + return o; +} + +template +json_object *to_json_(T const &s) { + auto a = json_object_new_array(); + + if (!s.empty()) { + for (auto const &i : s) { + json_object_array_add(a, to_json(i.second.get())); + } + } + + return a; +} + +json_object *to_json(genivi::controller::surface_map_type const &s) { + return to_json_(s); +} + +json_object *to_json(genivi::controller::layer_map_type const &l) { + return to_json_(l); +} + +json_object *to_json(genivi::controller::screen_map_type const &s) { + return to_json_(s); +} + +json_object *to_json(std::vector const &v) { + auto a = json_object_new_array(); + for (const auto i : v) { + json_object_array_add(a, json_object_new_int(i)); + } + return a; +} diff --git a/src/json_helper.hpp b/src/json_helper.hpp new file mode 100644 index 0000000..4a536d3 --- /dev/null +++ b/src/json_helper.hpp @@ -0,0 +1,16 @@ +#ifndef TMCAGLWM_JSON_HELPER_HPP +#define TMCAGLWM_JSON_HELPER_HPP + +#include "wayland.hpp" + +struct json_object; + +json_object *to_json(genivi::surface const *s); +json_object *to_json(genivi::layer const *l); +json_object *to_json(genivi::screen const *s); +json_object *to_json(genivi::controller::surface_map_type const &s); +json_object *to_json(genivi::controller::layer_map_type const &l); +json_object *to_json(genivi::controller::screen_map_type const &s); +json_object *to_json(std::vector const &v); + +#endif // TMCAGLWM_JSON_HELPER_HPP diff --git a/src/main.cpp b/src/main.cpp index 1ac26f1..a2331c4 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,3 +1,4 @@ +#include "json_helper.hpp" #include "util.hpp" #include "wayland.hpp" @@ -11,8 +12,6 @@ extern "C" { #include } -#include - namespace { struct wayland { std::unique_ptr 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[] = { diff --git a/src/wayland.hpp b/src/wayland.hpp index 4865e42..7e005f1 100644 --- a/src/wayland.hpp +++ b/src/wayland.hpp @@ -228,6 +228,14 @@ struct screen : public wayland_proxy, // \___\___/|_| |_|\__|_| \___/|_|_|\___|_| // struct controller : public wayland_proxy { + typedef std::unordered_map proxy_to_id_map_type; + typedef std::unordered_map> + surface_map_type; + typedef std::unordered_map> + layer_map_type; + typedef std::unordered_map> + screen_map_type; + // HACK: // The order of these member is mandatory, as when objects are destroyed // they will call their parent (that's us right here!) and remove their @@ -235,13 +243,13 @@ struct controller : public wayland_proxy { // when the surfaces/layers/screens maps are destroyed. This sucks, but // I cannot see a better solution w/o globals or some other horrible // call-our-parent construct. - std::unordered_map surface_proxy_to_id; - std::unordered_map layer_proxy_to_id; - std::unordered_map screen_proxy_to_id; + proxy_to_id_map_type surface_proxy_to_id; + proxy_to_id_map_type layer_proxy_to_id; + proxy_to_id_map_type screen_proxy_to_id; - std::unordered_map> surfaces; - std::unordered_map> layers; - std::unordered_map> screens; + surface_map_type surfaces; + layer_map_type layers; + screen_map_type screens; typedef std::pair> name_task_pair; -- 2.16.6