Reload xml files when resolution gets changed
[platform/core/uifw/libscl-ui.git] / xml2binary / xml2dat.cpp
1 /*
2  * Copyright (c) 2012 - 2014 Samsung Electronics Co., Ltd All Rights Reserved
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 <limits.h>
19 #include <unistd.h>
20 #include "string_encoder.h"
21 #include "metadata_handler.h"
22 #include "encode_layout.h"
23 #include "encode_input_mode_configure.h"
24 #include "encode_key_coordinate_frame.h"
25 #include "encode_label_properties_frame.h"
26 #include "encode_modifier_decoration.h"
27 #include "encode_default_configure.h"
28 #include "encode_magnifier_configure.h"
29 #include "encode_autopopup_configure.h"
30 #include "encode_nine_patch.h"
31 #include "xmlresource.h"
32 using namespace xmlresource;
33 using namespace std;
34
35 String_Encoder actual_string_encoder;
36 IString_Encoder& string_encoder = actual_string_encoder;
37
38 static inline void show_usage() {
39     static const char* message = {  "xml2binary: missing folder operand\n"
40                                     "-------------------------------------------------------\n"
41                                     "|  Usage: xml2binary operand1 operand2                |\n"
42                                     "|  operand1: the folder where xml text files located  |\n"
43                                     "|  operand2: the folder you want to place the bin file|\n"
44                                     "------------------------------------------------------- \n"};
45     printf("%s", message);
46 }
47
48 int main(const int argc, char* argv[]) {
49     if (argc < 2) {
50         show_usage();
51         return -1;
52     }
53
54     char* xml_text_dir = NULL;
55     if (argv[1])
56         xml_text_dir = strdup(argv[1]);
57
58     if (xml_text_dir) {
59         if (0 != access(xml_text_dir, R_OK)) {
60             perror(xml_text_dir);
61             free(xml_text_dir);
62             return -1;
63         } else {
64             free(xml_text_dir);
65             xml_text_dir = NULL;
66         }
67     }
68
69     char* xml_bin_dir = NULL;
70     if (argc < 3) {
71         if (argv[1])
72             xml_bin_dir = strdup(argv[1]);
73     } else {
74         if (argv[2])
75             xml_bin_dir = strdup(argv[2]);
76     }
77
78     if (xml_bin_dir) {
79         if (0 != access(xml_bin_dir, W_OK)) {
80             perror(xml_bin_dir);
81             free(xml_bin_dir);
82             return -1;
83         } else {
84             free(xml_bin_dir);
85             xml_bin_dir = NULL;
86         }
87     }
88
89     XMLResource *xmlresource = XMLResource::get_instance();
90     if (!xmlresource) {
91         printf("Failed build xmlresource instance.\n");
92         return -1;
93     }
94
95     xmlresource->set_resource_directory(xml_text_dir);
96     xmlresource->init("main_entry.xml");
97
98     static const char* metadata_path = "/usr/share/libscl-ui/metadata.xml";
99     if (0 != access(metadata_path, R_OK)) {
100         perror(metadata_path);
101         return -1;
102     }
103     MetaData_Handler md_handler(metadata_path);
104
105     char bin_file[_POSIX_PATH_MAX] = {0};
106     snprintf(bin_file, _POSIX_PATH_MAX, "%s/%s", xml_bin_dir, "sclresource.bin");
107
108     FILE *fp = fopen(bin_file, "wb");
109     if (!fp) {
110         perror(bin_file);
111         return -1;
112     }
113     fclose(fp);
114
115     enum{
116         ALLDATA = 0,
117         METADATA,
118         INPUT_MODE_CONFIGURE,
119         LAYOUT,
120         KEY_COORDINATE_FRAME,
121         LABEL_PROPERTIES_FRAME,
122         MODIFIER_DECORATION,
123         DEFAULT_CONFIGURE,
124         AUTOPOPUP_CONFIGURE,
125         MAGNIFIER_CONFIGURE,
126         NINE_PATCH,
127         STRING,
128         MAX_DATATYPE
129     };
130
131     int size_table[MAX_DATATYPE] = {0};
132     int offset_table[MAX_DATATYPE] = {0};
133     const int SIZE_WIDTH = 8;
134     const int OFFSET_WIDTH = 8;
135     int offset = 0;
136     {
137         //Reserve for size_table
138         ResourceStorage rs;
139         rs.reserve(MAX_DATATYPE * (SIZE_WIDTH + OFFSET_WIDTH));
140         int ret = rs.toFile(bin_file, offset);
141         if (ret < 0) {
142             printf("error\n");
143             return -1;
144         }
145     }
146
147     offset_table[METADATA] = offset;
148     size_table[METADATA] = md_handler.encode(bin_file, offset);
149     offset_table[INPUT_MODE_CONFIGURE] = offset;
150     size_table[INPUT_MODE_CONFIGURE] = encode_input_mode_configure_file(bin_file, offset, md_handler);
151
152     offset_table[LAYOUT] = offset;
153     size_table[LAYOUT] = encode_layout_file(bin_file, offset, md_handler);
154
155     offset_table[KEY_COORDINATE_FRAME] = offset;
156     size_table[KEY_COORDINATE_FRAME] = encode_key_coordinate_frame_file(bin_file, offset, md_handler);
157
158     offset_table[LABEL_PROPERTIES_FRAME] = offset;
159     size_table[LABEL_PROPERTIES_FRAME] = encode_label_properties_frame_file(bin_file, offset, md_handler);
160
161     offset_table[MODIFIER_DECORATION] = offset;
162     size_table[MODIFIER_DECORATION] = encode_modifier_decoration_file(bin_file, offset, md_handler);
163
164     offset_table[DEFAULT_CONFIGURE] = offset;
165     size_table[DEFAULT_CONFIGURE] = encode_default_configure_file(bin_file, offset, md_handler);
166
167     offset_table[AUTOPOPUP_CONFIGURE] = offset;
168     size_table[AUTOPOPUP_CONFIGURE] = encode_autopopup_configure_file(bin_file, offset, md_handler);
169
170     offset_table[MAGNIFIER_CONFIGURE] = offset;
171     size_table[MAGNIFIER_CONFIGURE] = encode_magnifier_configure_file(bin_file, offset, md_handler);
172
173     offset_table[NINE_PATCH] = offset;
174     size_table[NINE_PATCH] = encode_nine_patch_file(bin_file, offset, md_handler);
175
176     offset_table[STRING] = offset;
177     actual_string_encoder.set_path(bin_file);
178     size_table[STRING] = string_encoder.encode(offset);
179
180     offset_table[ALLDATA] = 0;
181     size_table[ALLDATA]= offset;
182
183     {
184         ResourceStorage rs;
185         for (int i = 0; i < MAX_DATATYPE; ++i) {
186             rs.put<sint_t>(size_table[i], SIZE_WIDTH);
187             rs.put<sint_t>(offset_table[i], OFFSET_WIDTH);
188         }
189
190         int bin_offset = 0;
191         int ret = rs.toFile(bin_file, bin_offset);
192         if (ret < 0) {
193             printf("error\n");
194             return -1;
195         }
196     }
197
198     printf("xml2binary succesfully.\n");
199
200     return 0;
201 }