Format source codes
[apps/agl-service-windowmanager-2017.git] / src / json_helper.cpp
1 /*
2  * Copyright (c) 2017 TOYOTA MOTOR CORPORATION
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include "json_helper.hpp"
18
19 #include <json.h>
20
21 json_object *to_json(compositor::surface_properties const &s)
22 {
23     // auto j = json::object({
24     auto j = json_object_new_object();
25
26     //    {"id", s.id},
27     json_object_object_add(j, "id", json_object_new_int(s.id));
28
29     //    {"size", {{"width", s.size.w}, {"height", s.size.h}}},
30     auto jsize = json_object_new_object();
31     json_object_object_add(jsize, "width", json_object_new_int(s.size.w));
32     json_object_object_add(jsize, "height", json_object_new_int(s.size.h));
33     json_object_object_add(j, "size", jsize);
34
35     //    {"dst",
36     //     {{"width", s.dst_rect.w},
37     //      {"height", s.dst_rect.h},
38     //      {"x", s.dst_rect.x},
39     //      {"y", s.dst_rect.y}}},
40     auto jdst = json_object_new_object();
41     json_object_object_add(jdst, "width", json_object_new_int(s.dst_rect.w));
42     json_object_object_add(jdst, "height", json_object_new_int(s.dst_rect.h));
43     json_object_object_add(jdst, "x", json_object_new_int(s.dst_rect.x));
44     json_object_object_add(jdst, "y", json_object_new_int(s.dst_rect.y));
45     json_object_object_add(j, "dst", jdst);
46
47     //    {"src",
48     //     {{"width", s.src_rect.w},
49     //      {"height", s.src_rect.h},
50     //      {"x", s.src_rect.x},
51     //      {"y", s.src_rect.y}}},
52     auto jsrc = json_object_new_object();
53     json_object_object_add(jsrc, "width", json_object_new_int(s.src_rect.w));
54     json_object_object_add(jsrc, "height", json_object_new_int(s.src_rect.h));
55     json_object_object_add(jsrc, "x", json_object_new_int(s.src_rect.x));
56     json_object_object_add(jsrc, "y", json_object_new_int(s.src_rect.y));
57     json_object_object_add(j, "src", jsrc);
58
59     //    {"visibility", s.visibility},
60     json_object_object_add(
61         j, "visibility",
62         json_object_new_boolean(static_cast<json_bool>(s.visibility == 1)));
63
64     //    {"opacity", s.opacity},
65     json_object_object_add(j, "opacity", json_object_new_double(s.opacity));
66
67     //    {"orientation", s.orientation},
68     json_object_object_add(j, "orientation", json_object_new_int(s.orientation));
69
70     // });
71     return j;
72 }
73
74 json_object *to_json(compositor::screen const *s)
75 {
76     auto o = json_object_new_object();
77     json_object_object_add(o, "id", json_object_new_int(s->id));
78     return o;
79 }
80
81 template <typename T>
82 json_object *to_json_(T const &s)
83 {
84     auto a = json_object_new_array();
85
86     if (!s.empty())
87     {
88         for (auto const &i : s)
89         {
90             json_object_array_add(a, to_json(i.second));
91         }
92     }
93
94     return a;
95 }
96
97 json_object *to_json(compositor::controller::props_map const &s)
98 {
99     return to_json_(s);
100 }
101
102 json_object *to_json(std::vector<uint32_t> const &v)
103 {
104     auto a = json_object_new_array();
105     for (const auto i : v)
106     {
107         json_object_array_add(a, json_object_new_int(i));
108     }
109     return a;
110 }