Track surface/layer properties with one struct
[staging/windowmanager.git] / src / json_helper.cpp
1 #include "json_helper.hpp"
2
3 #include <json.h>
4
5 #include <json.hpp>
6
7 using json = nlohmann::json;
8
9 json_object *to_json(genivi::surface_properties const &s) {
10    auto j = json::object({
11       {"id", s.id},
12       {"size", {{"width", s.size.w}, {"height", s.size.h}}},
13       {"dst",
14        {{"width", s.dst_rect.w},
15         {"height", s.dst_rect.h},
16         {"x", s.dst_rect.x},
17         {"y", s.dst_rect.y}}},
18       {"src",
19        {{"width", s.src_rect.w},
20         {"height", s.src_rect.h},
21         {"x", s.src_rect.x},
22         {"y", s.src_rect.y}}},
23       {"visibility", s.visibility},
24       {"opacity", s.opacity},
25       {"orientation", s.orientation},
26    });
27    return json_tokener_parse(j.dump().c_str());
28 }
29
30 json_object *to_json(genivi::screen const *s) {
31    auto o = json_object_new_object();
32    json_object_object_add(o, "id", json_object_new_int(s->id));
33    return o;
34 }
35
36 template <typename T>
37 json_object *to_json_(T const &s) {
38    auto a = json_object_new_array();
39
40    if (!s.empty()) {
41       for (auto const &i : s) {
42          json_object_array_add(a, to_json(i.second));
43       }
44    }
45
46    return a;
47 }
48
49 json_object *to_json(genivi::controller::props_map const &s) {
50    return to_json_(s);
51 }
52
53 json_object *to_json(std::vector<uint32_t> const &v) {
54    auto a = json_object_new_array();
55    for (const auto i : v) {
56       json_object_array_add(a, json_object_new_int(i));
57    }
58    return a;
59 }