2 * Copyright 2012 Samsung Electronics Co., Ltd
4 * Licensed under the Flora License, Version 1.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
8 * http://www.tizenopensource.org/license
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.
19 #include <stdlib.h> /* malloc */
20 #include <string.h> /* strdup */
27 #include "livebox_internal.h"
28 #include "desc_parser.h"
31 #include "critical_log.h"
33 #define TYPE_TEXT "text"
34 #define TYPE_IMAGE "image"
35 #define TYPE_EDJE "edje"
36 #define TYPE_SIGNAL "signal"
37 #define TYPE_INFO "info"
38 #define TYPE_DRAG "drag"
40 #define INFO_SIZE "size"
41 #define INFO_CATEGORY "category"
63 static int update_text(struct livebox *handle, struct block *block, int is_pd)
65 struct livebox_script_operators *ops;
67 if (!block || !block->part || !block->data) {
68 ErrPrint("Invalid argument\n");
72 ops = is_pd ? &handle->pd.data.ops : &handle->lb.data.ops;
74 ops->update_text(handle, (const char *)block->id, (const char *)block->part, (const char *)block->data);
79 static int update_image(struct livebox *handle, struct block *block, int is_pd)
81 struct livebox_script_operators *ops;
82 if (!block || !block->part) {
83 ErrPrint("Invalid argument\n");
87 ops = is_pd ? &handle->pd.data.ops : &handle->lb.data.ops;
88 if (ops->update_image)
89 ops->update_image(handle, block->id, block->part, block->data);
94 static int update_script(struct livebox *handle, struct block *block, int is_pd)
96 struct livebox_script_operators *ops;
97 if (!block || !block->part) {
98 ErrPrint("Invalid argument\n");
102 ops = is_pd ? &handle->pd.data.ops : &handle->lb.data.ops;
103 if (ops->update_script)
104 ops->update_script(handle, block->id, block->part, block->data, block->group);
109 static int update_signal(struct livebox *handle, struct block *block, int is_pd)
111 struct livebox_script_operators *ops;
114 ErrPrint("Invalid argument\n");
118 ops = is_pd ? &handle->pd.data.ops : &handle->lb.data.ops;
119 if (ops->update_signal)
120 ops->update_signal(handle, block->id, block->data, block->part);
125 static int update_drag(struct livebox *handle, struct block *block, int is_pd)
128 struct livebox_script_operators *ops;
130 if (!block || !block->data || !block->part) {
131 ErrPrint("Invalid argument\n");
135 ops = is_pd ? &handle->pd.data.ops : &handle->lb.data.ops;
137 if (sscanf(block->data, "%lfx%lf", &dx, &dy) != 2) {
138 ErrPrint("Invalid format of data\n");
142 if (ops->update_drag)
143 ops->update_drag(handle, block->id, block->part, dx, dy);
148 static int update_info(struct livebox *handle, struct block *block, int is_pd)
150 struct livebox_script_operators *ops;
152 if (!block || !block->part || !block->data) {
153 ErrPrint("Invalid argument\n");
157 ops = is_pd ? &handle->pd.data.ops : &handle->lb.data.ops;
159 if (!strcasecmp(block->part, INFO_SIZE)) {
162 if (sscanf(block->data, "%dx%d", &w, &h) != 2) {
163 ErrPrint("Invalid format (%s)\n", block->data);
167 if (ops->update_info_size)
168 ops->update_info_size(handle, block->id, w, h);
170 } else if (!strcasecmp(block->part, INFO_CATEGORY)) {
171 if (ops->update_info_category)
172 ops->update_info_category(handle, block->id, block->data);
178 static inline int update_begin(struct livebox *handle, int is_pd)
180 struct livebox_script_operators *ops;
182 ops = is_pd ? &handle->pd.data.ops : &handle->lb.data.ops;
184 if (ops->update_begin)
185 ops->update_begin(handle);
190 static inline int update_end(struct livebox *handle, int is_pd)
192 struct livebox_script_operators *ops;
194 ops = is_pd ? &handle->pd.data.ops : &handle->lb.data.ops;
197 ops->update_end(handle);
202 int parse_desc(struct livebox *handle, const char *descfile, int is_pd)
220 const char *field_name[] = {
230 register int field_idx;
231 register int idx = 0;
236 int (*handler)(struct livebox *handle, struct block *block, int is_pd);
240 .handler = update_text,
244 .handler = update_image,
248 .handler = update_script,
252 .handler = update_signal,
256 .handler = update_drag,
260 .handler = update_info,
268 fp = fopen(descfile, "rt");
270 ErrPrint("Error: %s\n", strerror(errno));
274 update_begin(handle, is_pd);
291 update_end(handle, is_pd);
304 block = calloc(1, sizeof(*block));
306 CRITICAL_LOG("Heap: %s\n", strerror(errno));
307 update_end(handle, is_pd);
327 if (field_name[field_idx][idx] != '\0')
372 block->group_len = 0;
395 if (field_name[field_idx][idx] != ch) {
398 ungetc(field_name[field_idx][idx], fp);
401 if (field_name[field_idx] == NULL)
412 if (idx == block->type_len) {
413 block->type_len += 256;
414 block->type = realloc(block->type, block->type_len);
416 CRITICAL_LOG("Heap: %s\n", strerror(errno));
422 block->type[idx] = '\0';
429 block->type[idx] = ch;
434 if (idx == block->part_len) {
435 block->part_len += 256;
436 block->part = realloc(block->part, block->part_len);
438 CRITICAL_LOG("Heap: %s\n", strerror(errno));
444 block->part[idx] = '\0';
451 block->part[idx] = ch;
456 if (idx == block->data_len) {
457 block->data_len += 256;
458 block->data = realloc(block->data, block->data_len);
460 CRITICAL_LOG("Heap: %s\n", strerror(errno));
466 block->data[idx] = '\0';
473 block->data[idx] = ch;
478 if (idx == block->file_len) {
479 block->file_len += 256;
480 block->file = realloc(block->file, block->file_len);
482 CRITICAL_LOG("Heap: %s\n", strerror(errno));
488 block->file[idx] = '\0';
495 block->file[idx] = ch;
500 if (idx == block->group_len) {
501 block->group_len += 256;
502 block->group = realloc(block->group, block->group_len);
504 CRITICAL_LOG("Heap: %s\n", strerror(errno));
510 block->group[idx] = '\0';
517 block->group[idx] = ch;
521 if (idx == block->id_len) {
522 block->id_len += 256;
523 block->id = realloc(block->id, block->id_len);
525 CRITICAL_LOG("Heap: %s\n", strerror(errno));
531 block->id[idx] = '\0';
543 block->file = strdup(util_uri_to_path(handle->id));
549 while (handlers[i].type) {
550 if (!strcasecmp(handlers[i].type, block->type)) {
551 handlers[i].handler(handle, block, is_pd);
557 if (!handlers[i].type)
558 ErrPrint("Unknown block type: %s\n", block->type);
577 if (state != UNKNOWN)
580 update_end(handle, is_pd);
586 ErrPrint("Parse error\n");
597 update_end(handle, is_pd);