Segment process for control timer event
[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_helper.hpp"
19 #include "hmi-debug.h"
20
21
22 json_object *to_json(compositor::surface_properties const &s) {
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    auto o = json_object_new_object();
76    json_object_object_add(o, "id", json_object_new_int(s->id));
77    return o;
78 }
79
80 template <typename T>
81 json_object *to_json_(T const &s) {
82    auto a = json_object_new_array();
83
84    if (!s.empty()) {
85       for (auto const &i : s) {
86          json_object_array_add(a, to_json(i.second));
87       }
88    }
89
90    return a;
91 }
92
93 json_object *to_json(compositor::controller::props_map const &s) {
94    return to_json_(s);
95 }
96
97 json_object *to_json(std::vector<uint32_t> const &v) {
98    auto a = json_object_new_array();
99    for (const auto i : v) {
100       json_object_array_add(a, json_object_new_int(i));
101    }
102    return a;
103 }
104
105 namespace jh {
106
107 const char* getStringFromJson(json_object* obj, const char* key) {
108     if ((nullptr == obj) || (nullptr == key)) {
109         HMI_ERROR("wm:jh", "Argument is nullptr!!!");
110         return nullptr;
111     }
112
113     json_object* tmp;
114     if (!json_object_object_get_ex(obj, key, &tmp)) {
115         HMI_DEBUG("wm:jh", "Not found key \"%s\"", key);
116         return nullptr;
117     }
118
119     return json_object_get_string(tmp);
120 }
121
122 int getIntFromJson(json_object* obj, const char* key) {
123     if ((nullptr == obj) || (nullptr == key)) {
124         HMI_ERROR("wm:jh", "Argument is nullptr!!!");
125         return 0;
126     }
127
128     json_object* tmp;
129     if (!json_object_object_get_ex(obj, key, &tmp)) {
130         HMI_DEBUG("wm:jh", "Not found key \"%s\"", key);
131         return 0;
132     }
133
134     return json_object_get_int(tmp);
135 }
136
137 double getDoubleFromJson(json_object* obj, const char* key) {
138     if ((nullptr == obj) || (nullptr == key)) {
139         HMI_ERROR("wm:jh", "Argument is nullptr!!!");
140         return 0;
141     }
142
143     json_object* tmp;
144     if (!json_object_object_get_ex(obj, key, &tmp)) {
145         HMI_DEBUG("wm:jh", "Not found key \"%s\"", key);
146         return 0;
147     }
148
149     return json_object_get_double(tmp);
150 }
151
152 json_bool getBoolFromJson(json_object* obj, const char* key) {
153     if ((nullptr == obj) || (nullptr == key)) {
154         HMI_ERROR("wm:jh", "Argument is nullptr!!!");
155         return 0;
156     }
157
158     json_object* tmp;
159     if (!json_object_object_get_ex(obj, key, &tmp)) {
160         HMI_DEBUG("wm:jh", "Not found key \"%s\"", key);
161         return 0;
162     }
163
164     return json_object_get_boolean(tmp);
165 }
166
167 int inputJsonFilie(const char* file, json_object** obj) {
168     const int input_size = 128;
169     int ret = -1;
170
171     if ((nullptr == file) || (nullptr == obj)) {
172         HMI_ERROR("wm:jh", "Argument is nullptr!!!");
173         return ret;
174     }
175
176     HMI_DEBUG("wm:jh", "Input file: %s", file);
177
178     // Open json file
179     FILE *fp = fopen(file, "rb");
180     if (nullptr == fp) {
181         HMI_ERROR("wm:jh", "Could not open file");
182         return ret;
183     }
184
185     // Parse file data
186     struct json_tokener *tokener = json_tokener_new();
187     enum json_tokener_error json_error;
188     char buffer[input_size];
189     int block_cnt = 1;
190     while (1) {
191         size_t len = fread(buffer, sizeof(char), input_size, fp);
192         *obj = json_tokener_parse_ex(tokener, buffer, len);
193         if (nullptr != *obj) {
194             HMI_DEBUG("wm:jh", "File input is success");
195             ret = 0;
196             break;
197         }
198
199         json_error = json_tokener_get_error(tokener);
200         if ((json_tokener_continue != json_error)
201             || (input_size > len)) {
202             HMI_ERROR("wm:jh", "Failed to parse file (byte:%d err:%s)",
203                       (input_size * block_cnt), json_tokener_error_desc(json_error));
204             HMI_ERROR("wm:jh", "\n%s", buffer);
205             *obj = nullptr;
206             break;
207         }
208         block_cnt++;
209     }
210
211     // Close json file
212     fclose(fp);
213
214     // Free json_tokener
215     json_tokener_free(tokener);
216
217     return ret;
218 }
219
220 } // namespace jh