Reload xml files when resolution gets changed
[platform/core/uifw/libscl-ui.git] / xml2binary / encode_key_coordinate_frame.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 "resource_storage_impl.h"
19 #include "encode_key_coordinate_frame.h"
20 #include <string>
21 #include "xmlresource.h"
22 #include "put_record.h"
23 #include "_auto_metadata.h"
24 using namespace xmlresource;
25 using namespace std;
26 static void
27 encode_key_coordinate_record(ResourceStorage& storage, const PSclLayoutKeyCoordinate cur, const Key_coordinate_record_width& record_width) {
28     if (cur == NULL) return;
29
30     //x
31     storage.put<sint_t>(cur->x, record_width.x);
32
33     //y
34     storage.put<sint_t>(cur->y, record_width.y);
35
36     //width
37     storage.put<sint_t>(cur->width, record_width.width);
38
39     //height
40     storage.put<sint_t>(cur->height, record_width.height);
41
42     //add_hit_left
43     storage.put<sint_t>(cur->add_hit_left, record_width.add_hit_left);
44
45     //add_hit_right
46     storage.put<sint_t>(cur->add_hit_right, record_width.add_hit_right);
47
48     //add_hit_top
49     storage.put<sint_t>(cur->add_hit_top, record_width.add_hit_top);
50
51     //add_hit_bottom
52     storage.put<sint_t>(cur->add_hit_bottom, record_width.add_hit_bottom);
53
54     //popup_relative_x
55     storage.put<sint_t>(cur->popup_relative_x, record_width.popup_relative_x);
56
57     //popup_relative_y
58     storage.put<sint_t>(cur->popup_relative_y, record_width.popup_relative_y);
59
60     //extract_offset_x
61     storage.put<sint_t>(cur->extract_offset_x, record_width.extract_offset_x);
62
63     //extract_offset_y
64     storage.put<sint_t>(cur->extract_offset_y, record_width.extract_offset_y);
65
66     storage.encode_string(cur->sub_layout, record_width.sub_layout);
67
68     //magnifier_offset_x
69     storage.put<sint_t>(cur->magnifier_offset_x, record_width.magnifier_offset_x);
70
71     //magnifier_offset_y
72     storage.put<sint_t>(cur->magnifier_offset_y, record_width.magnifier_offset_y);
73
74     storage.encode_string(cur->custom_id, record_width.custom_id);
75
76     //button_type
77     storage.put<sint_t>(cur->button_type, record_width.button_type);
78
79     //key_type
80     storage.put<sint_t>(cur->key_type, record_width.key_type);
81
82     //popup_type
83     storage.put<sint_t>(cur->popup_type, record_width.popup_type);
84
85     storage.put<sint_t>(cur->use_magnifier, record_width.use_magnifier);
86     storage.put<sint_t>(cur->use_long_key_magnifier, record_width.use_long_key_magnifier);
87
88     //popup_input_mode
89     for (int i = 0; i < SCL_DRAG_STATE_MAX; ++i) {
90         storage.encode_string(cur->popup_input_mode[i], record_width.popup_input_mode);
91     }
92
93     storage.encode_string(cur->sound_style, record_width.sound_style);
94     storage.encode_string(cur->vibe_style, record_width.vibe_style);
95     storage.put<sint_t>(cur->is_side_button, record_width.is_side_button);
96
97     //label_count
98     storage.put<sint_t>(cur->label_count, record_width.label_count);
99
100     //label
101     for (int i = 0; i < SCL_SHIFT_STATE_MAX; ++i) {
102         for (int j = 0; j < MAX_SIZE_OF_LABEL_FOR_ONE; ++j) {
103             storage.encode_string(cur->label[i][j], record_width.label);
104         }
105     }
106
107     storage.encode_string(cur->label_type, record_width.label_type);
108
109     //label_image_path
110     for (int i = 0; i < SCL_SHIFT_STATE_MAX; ++i) {
111         for (int j = 0; j < SCL_BUTTON_STATE_MAX; ++j) {
112             storage.encode_string(cur->image_label_path[i][j], record_width.image_label_path);
113         }
114     }
115
116     storage.encode_string(cur->image_label_type, record_width.image_label_type);
117
118     //bg_image_path
119     for (int i = 0; i < SCL_SHIFT_STATE_MAX; ++i) {
120         for (int j = 0; j < SCL_BUTTON_STATE_MAX; ++j) {
121             storage.encode_string(cur->bg_image_path[i][j], record_width.bg_image_path);
122         }
123     }
124
125     //key_value_count
126     storage.put<sint_t>(cur->key_value_count, record_width.key_value_count);
127
128     //key_value
129     for (int i = 0; i < SCL_SHIFT_STATE_MAX; ++i) {
130         for (int j = 0; j < MAX_SIZE_OF_MULTITAP_CHAR; ++j) {
131             storage.encode_string(cur->key_value[i][j], record_width.key_value);
132         }
133     }
134     //key_event
135     for (int i = 0; i < SCL_SHIFT_STATE_MAX; ++i) {
136         for (int j = 0; j < MAX_SIZE_OF_LABEL_FOR_ONE; ++j) {
137             storage.put<sint_t>(cur->key_event[i][j], record_width.key_event);
138         }
139     }
140
141     storage.put<sint_t>(cur->long_key_type, record_width.long_key_type);
142
143     //long_key_value
144     storage.encode_string(cur->long_key_value, record_width.long_key_value);
145
146     //long_key_event
147     storage.put<sint_t>(cur->long_key_event, record_width.long_key_event);
148
149     storage.put<sint_t>(cur->use_repeat_key, record_width.use_repeat_key);
150
151     //autopopup_keys
152     for (int i = 0; i < SCL_SHIFT_STATE_MAX; ++i) {
153         for (int j = 0; j < MAX_SIZE_OF_AUTOPOPUP_STRING; ++j) {
154             storage.encode_string(cur->autopopup_key_labels[i][j], record_width.autopopup_key_labels);
155             storage.encode_string(cur->autopopup_key_values[i][j], record_width.autopopup_key_values);
156             storage.put<sint_t>(cur->autopopup_key_events[i][j], record_width.autopopup_key_events);
157         }
158     }
159
160     storage.put<sint_t>(cur->dont_close_popup, record_width.dont_close_popup);
161
162     //extra_option
163     storage.put<sint_t>(cur->extra_option, record_width.extra_option);
164
165     //multitouch_type
166     storage.put<sint_t>(cur->multitouch_type, record_width.multitouch_type);
167
168     //modifier_decorator
169     storage.encode_string(cur->modifier_decorator, record_width.modifier_decorator);
170
171     //magnifier_label
172     for (int i = 0; i < SCL_SHIFT_STATE_MAX; ++i) {
173         for (int j = 0; j < MAX_SIZE_OF_LABEL_FOR_ONE; ++j) {
174             storage.encode_string(cur->magnifier_label[i][j], record_width.magnifier_label);
175         }
176     }
177
178     // hint_string
179     for (int i = 0; i < SCL_SHIFT_STATE_MAX; ++i) {
180         for (int j = 0; j < MAX_SIZE_OF_MULTITAP_CHAR; ++j) {
181             storage.encode_string(cur->hint_string[i][j], record_width.hint_string);
182         }
183     }
184 }
185
186 const int get_layout_num(const PSclLayoutKeyCoordinatePointerTable keyCoordinatePointerFrame) {
187     assert(keyCoordinatePointerFrame != NULL);
188     int layout_num = MAX_SCL_LAYOUT;
189     for ( int i = 0; i < MAX_SCL_LAYOUT; ++i ) {
190         // NULL is the end flag
191         if (keyCoordinatePointerFrame[i][0] == NULL) {
192             layout_num = i;
193             break;
194         }
195     }
196
197     return layout_num;
198 }
199
200 const int get_key_num(const PSclLayoutKeyCoordinatePointerTable keyCoordinatePointerFrame, const int layout_no) {
201     assert(keyCoordinatePointerFrame != NULL);
202     int key_num = MAX_KEY;
203
204     SclLayoutKeyCoordinatePointer* curLayoutRecordPointers = keyCoordinatePointerFrame[layout_no];
205     for (int i = 0; i < MAX_KEY; ++i) {
206         if (curLayoutRecordPointers[i] == NULL) {
207             key_num = i;
208             break;
209         }
210     }
211
212     return key_num;
213 }
214
215
216 int
217 encode_key_coordinate_frame_file(ResourceStorage& storage, IMetaData_Helper& md_helper) {
218     XMLResource *xmlresource = XMLResource::get_instance();
219
220     int layout_num = xmlresource->get_layout_size();
221     assert(layout_num >= 0);
222     SclLayoutKeyCoordinatePointer _key_coordinate_pointer_frame[MAX_SCL_LAYOUT][MAX_KEY];
223     memset(_key_coordinate_pointer_frame, 0x00, MAX_KEY * MAX_SCL_LAYOUT * sizeof(SclLayoutKeyCoordinatePointer));
224
225     for (int i = 0; i < layout_num; ++i) {
226         xmlresource->load(i);
227         for (int j = 0; j < MAX_KEY; ++j) {
228             _key_coordinate_pointer_frame[i][j] = xmlresource->get_key_coordinate_pointer_frame()[i][j];
229         }
230     }
231     // 4 byte (range[0-4,294,967,295))
232     const int DATA_SIZE_BYTES = 4;
233     // 1 byte (range[0-128))
234     const int REC_NUM_BYTES = 1;
235     const int KEY_NUM_BYTES = 1;
236     // 2 byte (range[0-65536))
237     const int KEY_COORDIANTE_REC_DATA_SIZE_BYTES = 2;
238
239     int init_size = storage.get_size();
240     // data size
241     storage.reserve(DATA_SIZE_BYTES);
242
243     //TODO: assert layout_num < REC_NUM_BYTES's range
244     storage.put<uint_t>(layout_num, REC_NUM_BYTES);
245
246     int *pKey_num_array = new int[layout_num];
247     memset(pKey_num_array, 0x00, layout_num * sizeof(int));
248     for (int i = 0; i < layout_num; ++i) {
249         pKey_num_array[i] = get_key_num(_key_coordinate_pointer_frame, i);
250         //TODO: assert key_num < KEY_NUM_BYTES's range
251         storage.put<uint_t>(pKey_num_array[i], KEY_NUM_BYTES);
252     }
253
254     // key_coordinate_rec_data_size
255     storage.reserve(KEY_COORDIANTE_REC_DATA_SIZE_BYTES);
256
257     Key_coordinate_record_width record_width;
258     set_key_coordinate_record_width(&md_helper, record_width);
259
260
261     int key_coordinate_rec_data_size = 0;
262     for ( int i = 0; i < layout_num; ++i ) {
263         for ( int j = 0; j < pKey_num_array[i]; ++j ) {
264             int pre_size = storage.get_size();
265
266             SclLayoutKeyCoordinatePointer cur = _key_coordinate_pointer_frame[i][j];
267             if (!cur) break;
268             encode_key_coordinate_record(storage, cur, record_width);
269
270             int aft_size = storage.get_size();
271             if (key_coordinate_rec_data_size == 0) {
272                 key_coordinate_rec_data_size = aft_size - pre_size;
273             }
274             assert(key_coordinate_rec_data_size == aft_size - pre_size);
275         }
276     }
277
278     delete[] pKey_num_array;
279
280     int advance_size = storage.get_size() - init_size;
281     storage.random_put<sint_t>(advance_size, DATA_SIZE_BYTES, init_size);
282
283     // random put key_coordinate_rec_data_size
284     int key_coordinate_rec_data_offset = init_size +
285                                         DATA_SIZE_BYTES +
286                                         REC_NUM_BYTES +
287                                         layout_num * KEY_NUM_BYTES;
288     storage.random_put<uint_t>(key_coordinate_rec_data_size,
289                                 KEY_COORDIANTE_REC_DATA_SIZE_BYTES,
290                                 key_coordinate_rec_data_offset);
291
292     return storage.get_size();
293 }
294
295 int
296 encode_key_coordinate_frame_file(const char* file, int& offset, IMetaData_Helper& md_helper) {
297     if (!file) return 0;
298
299     ResourceStorage storage;
300     encode_key_coordinate_frame_file(storage, md_helper);
301     storage.toFile(file, offset);
302
303     return storage.get_size();
304 }
305
306 int
307 encode_key_coordinate_frame_file(const char* file, IMetaData_Helper& md_helper) {
308     if (!file) return 0;
309
310     ResourceStorage storage;
311     encode_key_coordinate_frame_file(storage, md_helper);
312     storage.toFile(file);
313
314     return storage.get_size();
315 }