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