Readd Policy Manager as plugin
[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 #include "hmi-debug.h"
19
20 #include <json.h>
21
22 json_object *to_json(compositor::surface_properties const &s)
23 {
24     // auto j = json::object({
25     auto j = json_object_new_object();
26
27     //    {"id", s.id},
28     json_object_object_add(j, "id", json_object_new_int(s.id));
29
30     //    {"size", {{"width", s.size.w}, {"height", s.size.h}}},
31     auto jsize = json_object_new_object();
32     json_object_object_add(jsize, "width", json_object_new_int(s.size.w));
33     json_object_object_add(jsize, "height", json_object_new_int(s.size.h));
34     json_object_object_add(j, "size", jsize);
35
36     //    {"dst",
37     //     {{"width", s.dst_rect.w},
38     //      {"height", s.dst_rect.h},
39     //      {"x", s.dst_rect.x},
40     //      {"y", s.dst_rect.y}}},
41     auto jdst = json_object_new_object();
42     json_object_object_add(jdst, "width", json_object_new_int(s.dst_rect.w));
43     json_object_object_add(jdst, "height", json_object_new_int(s.dst_rect.h));
44     json_object_object_add(jdst, "x", json_object_new_int(s.dst_rect.x));
45     json_object_object_add(jdst, "y", json_object_new_int(s.dst_rect.y));
46     json_object_object_add(j, "dst", jdst);
47
48     //    {"src",
49     //     {{"width", s.src_rect.w},
50     //      {"height", s.src_rect.h},
51     //      {"x", s.src_rect.x},
52     //      {"y", s.src_rect.y}}},
53     auto jsrc = json_object_new_object();
54     json_object_object_add(jsrc, "width", json_object_new_int(s.src_rect.w));
55     json_object_object_add(jsrc, "height", json_object_new_int(s.src_rect.h));
56     json_object_object_add(jsrc, "x", json_object_new_int(s.src_rect.x));
57     json_object_object_add(jsrc, "y", json_object_new_int(s.src_rect.y));
58     json_object_object_add(j, "src", jsrc);
59
60     //    {"visibility", s.visibility},
61     json_object_object_add(
62         j, "visibility",
63         json_object_new_boolean(static_cast<json_bool>(s.visibility == 1)));
64
65     //    {"opacity", s.opacity},
66     json_object_object_add(j, "opacity", json_object_new_double(s.opacity));
67
68     //    {"orientation", s.orientation},
69     json_object_object_add(j, "orientation", json_object_new_int(s.orientation));
70
71     // });
72     return j;
73 }
74
75 json_object *to_json(compositor::screen const *s)
76 {
77     auto o = json_object_new_object();
78     json_object_object_add(o, "id", json_object_new_int(s->id));
79     return o;
80 }
81
82 template <typename T>
83 json_object *to_json_(T const &s)
84 {
85     auto a = json_object_new_array();
86
87     if (!s.empty())
88     {
89         for (auto const &i : s)
90         {
91             json_object_array_add(a, to_json(i.second));
92         }
93     }
94
95     return a;
96 }
97
98 json_object *to_json(compositor::controller::props_map const &s)
99 {
100     return to_json_(s);
101 }
102
103 json_object *to_json(std::vector<uint32_t> const &v)
104 {
105     auto a = json_object_new_array();
106     for (const auto i : v)
107     {
108         json_object_array_add(a, json_object_new_int(i));
109     }
110     return a;
111 }
112
113 namespace jh {
114
115 const char* getStringFromJson(json_object* obj, const char* key)
116 {
117     json_object* tmp;
118     if (!json_object_object_get_ex(obj, key, &tmp))
119     {
120         HMI_DEBUG("wm:jh", "Not found key \"%s\"", key);
121         return nullptr;
122     }
123
124     return json_object_get_string(tmp);
125 }
126
127 int getIntFromJson(json_object *obj, const char *key)
128 {
129     json_object *tmp;
130     if (!json_object_object_get_ex(obj, key, &tmp))
131     {
132         HMI_DEBUG("wm:jh", "Not found key \"%s\"", key);
133         return 0;
134     }
135
136     return json_object_get_int(tmp);
137 }
138
139 json_bool getBoolFromJson(json_object *obj, const char *key)
140 {
141     json_object *tmp;
142     if (!json_object_object_get_ex(obj, key, &tmp))
143     {
144         HMI_DEBUG("wm:jh", "Not found key \"%s\"", key);
145         return FALSE;
146     }
147
148     return json_object_get_boolean(tmp);
149 }
150
151 int inputJsonFilie(const char* file, json_object** obj)
152 {
153     const int input_size = 128;
154     int ret = -1;
155
156     HMI_DEBUG("wm:jh", "Input file: %s", file);
157
158     // Open json file
159     FILE *fp = fopen(file, "rb");
160     if (nullptr == fp)
161     {
162         HMI_ERROR("wm:jh", "Could not open file");
163         return ret;
164     }
165
166     // Parse file data
167     struct json_tokener *tokener = json_tokener_new();
168     enum json_tokener_error json_error;
169     char buffer[input_size];
170     int block_cnt = 1;
171     while (1)
172     {
173         size_t len = fread(buffer, sizeof(char), input_size, fp);
174         *obj = json_tokener_parse_ex(tokener, buffer, len);
175         if (nullptr != *obj)
176         {
177             HMI_DEBUG("wm:jh", "File input is success");
178             ret = 0;
179             break;
180         }
181
182         json_error = json_tokener_get_error(tokener);
183         if ((json_tokener_continue != json_error)
184             || (input_size > len))
185         {
186             HMI_ERROR("wm:jh", "Failed to parse file (byte:%d err:%s)",
187                       (input_size * block_cnt), json_tokener_error_desc(json_error));
188             HMI_ERROR("wm:jh", "\n%s", buffer);
189             *obj = nullptr;
190             break;
191         }
192         block_cnt++;
193     }
194
195     // Close json file
196     fclose(fp);
197
198     // Free json_tokener
199     json_tokener_free(tokener);
200
201     return ret;
202 }
203
204 } // namespace jh