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