Creating old package too
[apps/native/widget/widget.git] / dynamicbox / src / dynamicbox.c
1 /*
2  * Copyright 2013  Samsung Electronics Co., Ltd
3  *
4  * Licensed under the Flora License, Version 1.1 (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://floralicense.org/license/
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 <stdio.h>
18 #include <errno.h>
19 #include <stdlib.h> /* malloc */
20 #include <string.h> /* strdup */
21 #include <libgen.h>
22 #include <unistd.h> /* access */
23 #define __USE_GNU
24 #include <dlfcn.h>
25
26 #include <dlog.h>
27 #include <dynamicbox_errno.h>
28 #include <dynamicbox_service.h>
29 #include <dynamicbox_provider.h>
30 #include <dynamicbox_provider_buffer.h>
31 #include <dynamicbox_conf.h>
32 #include <dynamicbox_buffer.h>
33 #include <Evas.h>
34 #include <Ecore.h>
35
36 #include "debug.h"
37 #include "dlist.h"
38 #include "util.h"
39 #include "dynamicbox.h"
40 #include "internal/dynamicbox.h"
41
42 #define PUBLIC __attribute__((visibility("default")))
43
44 #define FILE_SCHEMA    "file://"
45
46 /**
47  * @note
48  * This value is configurable value.
49  */
50 #define FRAME_WAIT_INTERVAL (1.0f/6.0f)
51
52 struct block {
53     unsigned int idx;
54
55     char *type;
56     char *part;
57     char *data;
58     char *option;
59     char *id;
60     char *file;
61     char *target_id;
62 };
63
64 struct dynamicbox_desc {
65     FILE *fp;
66     int for_pd;
67
68     unsigned int last_idx;
69
70     struct dlist *block_list;
71 };
72
73 struct dynamicbox_buffer_data {
74     int is_gbar;
75     int accelerated;
76
77     /* for Buffer event wrapper */
78     int (*handler)(dynamicbox_buffer_h , dynamicbox_buffer_event_data_t, void *);
79     void *cbdata;
80
81     char *content;
82     char *title;
83     char *icon;
84     char *name;
85
86     Ecore_Timer *frame_wait_timer;
87 };
88
89 /**
90  * @brief These functions are defined in the data-provider-slave
91  */
92 static struct info {
93     const char *(*find_pkgname)(const char *filename);
94     int (*request_update_by_id)(const char *uri);
95     int (*trigger_update_monitor)(const char *id, int is_gbar);
96     int (*update_extra_info)(const char *id, const char *content, const char *title, const char *icon, const char *name);
97
98     enum load_type {
99         LOAD_TYPE_UNKNOWN = -1,
100         LOAD_TYPE_SLAVE   = 0,
101         LOAD_TYPE_APP     = 1
102     } type;
103
104     union _updated {
105         struct _slave {
106             int (*send)(const char *pkgname, const char *id, dynamicbox_buffer_h handle, int idx, int x, int y, int w, int h, int gbar, const char *descfile);
107         } slave;
108
109         struct _app {
110             int (*send)(dynamicbox_buffer_h handle, int idx, int x, int y, int w, int h, int gbar);
111         } app;
112     } updated;
113 } s_info = {
114     .find_pkgname = NULL,
115     .request_update_by_id = NULL,
116     .trigger_update_monitor = NULL,
117     .update_extra_info = NULL,
118     .type = LOAD_TYPE_UNKNOWN,    /* Not initialized */
119 };
120
121 #define FUNC_PREFIX                               "dynamicbox_"
122 #define FUNC_DYNAMICBOX_SEND_UPDATED              FUNC_PREFIX "send_buffer_updated"
123 #define FUNC_DYNAMICBOX_PROVIDER_APP_UPDATED      FUNC_PREFIX "provider_app_buffer_updated"
124 #define FUNC_DYNAMICBOX_FIND_PKGNAME              FUNC_PREFIX "find_pkgname"
125 #define FUNC_DYNAMICBOX_REQUEST_UPDATE_BY_ID      FUNC_PREFIX "request_update_by_id"
126 #define FUNC_DYNAMICBOX_TRIGGER_UPDATE_MONITOR    FUNC_PREFIX "trigger_update_monitor"
127 #define FUNC_DYNAMICBOX_UPDATE_EXTRA_INFO         FUNC_PREFIX "update_extra_info"
128
129 static inline void load_update_function(void)
130 {
131     /* Must to be checked the slave function first. */
132     s_info.updated.slave.send = dlsym(RTLD_DEFAULT, FUNC_DYNAMICBOX_SEND_UPDATED);
133     if (s_info.updated.slave.send) {
134         s_info.type = LOAD_TYPE_SLAVE;
135         DbgPrint("Slave detected\n");
136     } else {
137         s_info.updated.app.send = dlsym(RTLD_DEFAULT, FUNC_DYNAMICBOX_PROVIDER_APP_UPDATED);
138         if (s_info.updated.app.send) {
139             s_info.type = LOAD_TYPE_APP;
140             DbgPrint("App detected\n");
141         }
142     }
143 }
144
145 static int send_updated(const char *pkgname, const char *id, dynamicbox_buffer_h handle, int idx, int x, int y, int w, int h, int gbar, const char *descfile)
146 {
147     int ret = DBOX_STATUS_ERROR_INVALID_PARAMETER;
148
149     if (s_info.type == LOAD_TYPE_UNKNOWN) {
150         load_update_function();
151     }
152
153     if (s_info.type == LOAD_TYPE_APP) {
154         ret = s_info.updated.app.send(handle, idx, x, y, w, h, gbar);
155     } else if (s_info.type == LOAD_TYPE_SLAVE) {
156         /**
157          * pkgname, id are used for finding handle of direct connection.
158          */
159         ret = s_info.updated.slave.send(pkgname, id, handle, idx, x, y, w, h, gbar, descfile);
160     } else {
161         dynamicbox_damage_region_t region = {
162             .x = x,
163             .y = y,
164             .w = w,
165             .h = h,
166         };
167         ret = dynamicbox_provider_send_buffer_updated(handle, idx, &region, gbar, descfile);
168     }
169
170     return ret;
171 }
172
173 static char *id_to_uri(const char *id)
174 {
175     char *uri;
176     int uri_len;
177
178     uri_len = strlen(id) + strlen(FILE_SCHEMA) + 1;
179
180     uri = malloc(uri_len);
181     if (!uri) {
182         return NULL;
183     }
184
185     snprintf(uri, uri_len, FILE_SCHEMA "%s", id);
186     return uri;
187 }
188
189 static inline int event_handler_wrapper(dynamicbox_buffer_h buffer, dynamicbox_buffer_event_data_t event_info, void *data)
190 {
191     const char *pkgname;
192     const char *id;
193     struct dynamicbox_buffer_data *cbdata = data;
194     int ret;
195
196     pkgname = dynamicbox_provider_buffer_pkgname(buffer);
197     if (!pkgname) {
198         ErrPrint("pkgname is not valid\n");
199         return DBOX_STATUS_ERROR_INVALID_PARAMETER;
200     }
201
202     id = dynamicbox_provider_buffer_id(buffer);
203     if (!id) {
204         ErrPrint("id is not valid[%s]\n", pkgname);
205         return DBOX_STATUS_ERROR_INVALID_PARAMETER;
206     }
207
208     if (cbdata->handler) {
209         ret = cbdata->handler(buffer, event_info, cbdata->cbdata);
210     } else {
211         ret = DBOX_STATUS_ERROR_FAULT;
212     }
213
214     switch (event_info->type) {
215         case DBOX_BUFFER_EVENT_ACCESS_HIGHLIGHT:
216         case DBOX_BUFFER_EVENT_ACCESS_HIGHLIGHT_NEXT:
217         case DBOX_BUFFER_EVENT_ACCESS_HIGHLIGHT_PREV:
218         case DBOX_BUFFER_EVENT_ACCESS_ACTIVATE:
219         case DBOX_BUFFER_EVENT_ACCESS_ACTION_UP:
220         case DBOX_BUFFER_EVENT_ACCESS_ACTION_DOWN:
221         case DBOX_BUFFER_EVENT_ACCESS_SCROLL_UP:
222         case DBOX_BUFFER_EVENT_ACCESS_SCROLL_MOVE:
223         case DBOX_BUFFER_EVENT_ACCESS_SCROLL_DOWN:
224         case DBOX_BUFFER_EVENT_ACCESS_UNHIGHLIGHT:
225         case DBOX_BUFFER_EVENT_ACCESS_VALUE_CHANGE:
226         case DBOX_BUFFER_EVENT_ACCESS_MOUSE:
227         case DBOX_BUFFER_EVENT_ACCESS_BACK:
228         case DBOX_BUFFER_EVENT_ACCESS_OVER:
229         case DBOX_BUFFER_EVENT_ACCESS_READ:
230         case DBOX_BUFFER_EVENT_ACCESS_ENABLE:
231         case DBOX_BUFFER_EVENT_ACCESS_DISABLE:
232             DbgPrint("Accessibility event: %d\n", event_info->type);
233             if (ret < 0) {
234                 (void)dynamicbox_provider_send_access_status(pkgname, id, DBOX_ACCESS_STATUS_ERROR);
235             } else {
236                 (void)dynamicbox_provider_send_access_status(pkgname, id, ret);
237             }
238             break;
239         case DBOX_BUFFER_EVENT_KEY_UP:
240         case DBOX_BUFFER_EVENT_KEY_DOWN:
241         case DBOX_BUFFER_EVENT_KEY_FOCUS_IN:
242         case DBOX_BUFFER_EVENT_KEY_FOCUS_OUT:
243             DbgPrint("Key event: %d\n", event_info->type);
244             if (ret < 0) {
245                 (void)dynamicbox_provider_send_key_status(pkgname, id, DBOX_KEY_STATUS_ERROR);
246             } else {
247                 (void)dynamicbox_provider_send_key_status(pkgname, id, ret);
248             }
249             break;
250         default:
251             break;
252     }
253
254     return ret;
255 }
256
257 static inline int default_event_handler(dynamicbox_buffer_h buffer, dynamicbox_buffer_event_data_t event_info, void *data)
258 {
259     /* NOP */
260     return 0;
261 }
262
263 PUBLIC const int DBOX_DONE = 0x00;
264 PUBLIC const int DBOX_OUTPUT_UPDATED = 0x02;
265 PUBLIC const int DBOX_USE_NET = 0x04;
266
267 PUBLIC const int DBOX_NEED_TO_SCHEDULE = 0x01;
268 PUBLIC const int DBOX_NEED_TO_CREATE = 0x01;
269 PUBLIC const int DBOX_NEED_TO_DESTROY = 0x01;
270 PUBLIC const int DBOX_FORCE_TO_SCHEDULE = 0x08;
271
272 PUBLIC const int DBOX_SYS_EVENT_FONT_CHANGED = 0x01;
273 PUBLIC const int DBOX_SYS_EVENT_LANG_CHANGED = 0x02;
274 PUBLIC const int DBOX_SYS_EVENT_TIME_CHANGED = 0x04;
275 PUBLIC const int DBOX_SYS_EVENT_REGION_CHANGED = 0x08;
276 PUBLIC const int DBOX_SYS_EVENT_TTS_CHANGED = 0x10;
277 PUBLIC const int DBOX_SYS_EVENT_PAUSED = 0x0100;
278 PUBLIC const int DBOX_SYS_EVENT_RESUMED = 0x0200;
279 PUBLIC const int DBOX_SYS_EVENT_MMC_STATUS_CHANGED = 0x0400;
280 PUBLIC const int DBOX_SYS_EVENT_DELETED = 0x0800;
281
282 PUBLIC struct dynamicbox_desc *dynamicbox_desc_open(const char *filename, int for_pd)
283 {
284     struct dynamicbox_desc *handle;
285     char *new_fname;
286
287     handle = calloc(1, sizeof(*handle));
288     if (!handle) {
289         ErrPrint("Error: %s\n", strerror(errno));
290         return NULL;
291     }
292
293     if (for_pd) {
294         int len;
295         len = strlen(filename) + strlen(".desc") + 1;
296         new_fname = malloc(len);
297         if (!new_fname) {
298             ErrPrint("Error: %s\n", strerror(errno));
299             free(handle);
300             return NULL;
301         }
302         snprintf(new_fname, len, "%s.desc", filename);
303     } else {
304         new_fname = strdup(filename);
305         if (!new_fname) {
306             ErrPrint("Error: %s\n", strerror(errno));
307             free(handle);
308             return NULL;
309         }
310     }
311
312     DbgPrint("Open a file %s with merge mode %s\n",
313             new_fname,
314             access(new_fname, F_OK) == 0 ? "enabled" : "disabled");
315
316     handle->fp = fopen(new_fname, "at");
317     free(new_fname);
318     if (!handle->fp) {
319         ErrPrint("Failed to open a file: %s\n", strerror(errno));
320         free(handle);
321         return NULL;
322     }
323
324     return handle;
325 }
326
327 PUBLIC int dynamicbox_desc_close(struct dynamicbox_desc *handle)
328 {
329     struct dlist *l;
330     struct dlist *n;
331     struct block *block;
332
333     if (!handle) {
334         return DBOX_STATUS_ERROR_INVALID_PARAMETER;
335     }
336
337     dlist_foreach_safe(handle->block_list, l, n, block) {
338         handle->block_list = dlist_remove(handle->block_list, l);
339
340         fprintf(handle->fp, "{\n");
341         if (block->type) {
342             fprintf(handle->fp, "type=%s\n", block->type);
343         }
344
345         if (block->part) {
346             fprintf(handle->fp, "part=%s\n", block->part);
347         }
348
349         if (block->data) {
350             fprintf(handle->fp, "data=%s\n", block->data);
351         }
352
353         if (block->option) {
354             fprintf(handle->fp, "option=%s\n", block->option);
355         }
356
357         if (block->id) {
358             fprintf(handle->fp, "id=%s\n", block->id);
359         }
360
361         if (block->target_id) {
362             fprintf(handle->fp, "target=%s\n", block->target_id);
363         }
364         fprintf(handle->fp, "}\n");
365
366         free(block->type);
367         free(block->part);
368         free(block->data);
369         free(block->option);
370         free(block->id);
371         free(block->target_id);
372         free(block);
373     }
374
375     if (fclose(handle->fp) != 0) {
376         ErrPrint("fclose: %s\n", strerror(errno));
377     }
378     free(handle);
379     return DBOX_STATUS_ERROR_NONE;
380 }
381
382 PUBLIC int dynamicbox_desc_set_category(struct dynamicbox_desc *handle, const char *id, const char *category)
383 {
384     struct block *block;
385
386     if (!handle || !category) {
387         return DBOX_STATUS_ERROR_INVALID_PARAMETER;
388     }
389
390     block = calloc(1, sizeof(*block));
391     if (!block) {
392         return DBOX_STATUS_ERROR_OUT_OF_MEMORY;
393     }
394
395     block->type = strdup(DBOX_DESC_TYPE_INFO);
396     if (!block->type) {
397         free(block);
398         return DBOX_STATUS_ERROR_OUT_OF_MEMORY;
399     }
400
401     block->part = strdup("category");
402     if (!block->part) {
403         free(block->type);
404         free(block);
405         return DBOX_STATUS_ERROR_OUT_OF_MEMORY;
406     }
407
408     block->data = strdup(category);
409     if (!block->data) {
410         free(block->type);
411         free(block->part);
412         free(block);
413         return DBOX_STATUS_ERROR_OUT_OF_MEMORY;
414     }
415
416     if (id) {
417         block->id = strdup(id);
418         if (!block->id) {
419             free(block->data);
420             free(block->type);
421             free(block->part);
422             free(block);
423             return DBOX_STATUS_ERROR_OUT_OF_MEMORY;
424         }
425     }
426
427     block->idx = handle->last_idx++;
428     handle->block_list = dlist_append(handle->block_list, block);
429     return block->idx;
430 }
431
432 PUBLIC int dynamicbox_desc_set_size(struct dynamicbox_desc *handle, const char *id, int w, int h)
433 {
434     struct block *block;
435     char buffer[BUFSIZ];
436
437     if (!handle) {
438         return DBOX_STATUS_ERROR_INVALID_PARAMETER;
439     }
440
441     block = calloc(1, sizeof(*block));
442     if (!block) {
443         return DBOX_STATUS_ERROR_OUT_OF_MEMORY;
444     }
445
446     block->type = strdup(DBOX_DESC_TYPE_INFO);
447     if (!block->type) {
448         free(block);
449         return DBOX_STATUS_ERROR_OUT_OF_MEMORY;
450     }
451
452     block->part = strdup("size");
453     if (!block->part) {
454         free(block->type);
455         free(block);
456         return DBOX_STATUS_ERROR_OUT_OF_MEMORY;
457     }
458
459     if (id) {
460         block->id = strdup(id);
461         if (!block->id) {
462             free(block->part);
463             free(block->type);
464             free(block);
465             return DBOX_STATUS_ERROR_OUT_OF_MEMORY;
466         }
467     }
468
469     snprintf(buffer, sizeof(buffer), "%dx%d", w, h);
470     block->data = strdup(buffer);
471     if (!block->data) {
472         free(block->part);
473         free(block->type);
474         free(block);
475         return DBOX_STATUS_ERROR_OUT_OF_MEMORY;
476     }
477
478     block->idx = handle->last_idx++;
479     handle->block_list = dlist_append(handle->block_list, block);
480     return block->idx;
481 }
482
483 PUBLIC int dynamicbox_desc_set_id(struct dynamicbox_desc *handle, int idx, const char *id)
484 {
485     struct dlist *l;
486     struct block *block;
487
488     dlist_foreach(handle->block_list, l, block) {
489         if (block->idx == idx) {
490             if (strcasecmp(block->type, DBOX_DESC_TYPE_SCRIPT)) {
491                 ErrPrint("Invalid block is used\n");
492                 return DBOX_STATUS_ERROR_INVALID_PARAMETER;
493             }
494
495             free(block->target_id);
496             block->target_id = NULL;
497
498             if (!id || !strlen(id)) {
499                 return DBOX_STATUS_ERROR_NONE;
500             }
501
502             block->target_id = strdup(id);
503             if (!block->target_id) {
504                 ErrPrint("Heap: %s\n", strerror(errno));
505                 return DBOX_STATUS_ERROR_OUT_OF_MEMORY;
506             }
507
508             return DBOX_STATUS_ERROR_NONE;
509         }
510     }
511
512     return DBOX_STATUS_ERROR_NOT_EXIST;
513 }
514
515 /*!
516  * \return idx
517  */
518 PUBLIC int dynamicbox_desc_add_block(struct dynamicbox_desc *handle, const char *id, const char *type, const char *part, const char *data, const char *option)
519 {
520     struct block *block;
521
522     if (!handle || !type) {
523         return DBOX_STATUS_ERROR_INVALID_PARAMETER;
524     }
525
526     if (!part) {
527         part = "";
528     }
529
530     if (!data) {
531         data = "";
532     }
533
534     block = calloc(1, sizeof(*block));
535     if (!block) {
536         ErrPrint("Heap: %s\n", strerror(errno));
537         return DBOX_STATUS_ERROR_OUT_OF_MEMORY;
538     }
539
540     block->type = strdup(type);
541     if (!block->type) {
542         ErrPrint("Heap: %s\n", strerror(errno));
543         free(block);
544         return DBOX_STATUS_ERROR_OUT_OF_MEMORY;
545     }
546
547     block->part = strdup(part);
548     if (!block->part) {
549         ErrPrint("Heap: %s\n", strerror(errno));
550         free(block->type);
551         free(block);
552         return DBOX_STATUS_ERROR_OUT_OF_MEMORY;
553     }
554
555     block->data = strdup(data);
556     if (!block->data) {
557         ErrPrint("Heap: %s\n", strerror(errno));
558         free(block->type);
559         free(block->part);
560         free(block);
561         return DBOX_STATUS_ERROR_OUT_OF_MEMORY;
562     }
563
564     if (option) {
565         block->option = strdup(option);
566         if (!block->option) {
567             ErrPrint("Heap: %s\n", strerror(errno));
568             free(block->data);
569             free(block->type);
570             free(block->part);
571             free(block);
572             return DBOX_STATUS_ERROR_OUT_OF_MEMORY;
573         }
574     }
575
576     if (id) {
577         block->id = strdup(id);
578         if (!block->id) {
579             ErrPrint("Heap: %s\n", strerror(errno));
580             free(block->option);
581             free(block->data);
582             free(block->type);
583             free(block->part);
584             free(block);
585             return DBOX_STATUS_ERROR_OUT_OF_MEMORY;
586         }
587     }
588
589     block->idx = handle->last_idx++;
590     handle->block_list = dlist_append(handle->block_list, block);
591     return block->idx;
592 }
593
594 PUBLIC int dynamicbox_desc_del_block(struct dynamicbox_desc *handle, int idx)
595 {
596     struct dlist *l;
597     struct block *block;
598
599     if (!handle || idx < 0) {
600         return DBOX_STATUS_ERROR_INVALID_PARAMETER;
601     }
602
603     dlist_foreach(handle->block_list, l, block) {
604         if (block->idx == idx) {
605             handle->block_list = dlist_remove(handle->block_list, l);
606             free(block->type);
607             free(block->part);
608             free(block->data);
609             free(block->option);
610             free(block->id);
611             free(block->target_id);
612             free(block);
613             return DBOX_STATUS_ERROR_NONE;
614         }
615     }
616
617     return DBOX_STATUS_ERROR_NOT_EXIST;
618 }
619
620 PUBLIC int dynamicbox_acquire_buffer(dynamicbox_buffer_h handle, int idx, int width, int height, int pixel_size)
621 {
622     int ret;
623
624     if (!handle || width <= 0 || height <= 0 || pixel_size <= 0 || idx > DYNAMICBOX_CONF_EXTRA_BUFFER_COUNT) {
625         return DBOX_STATUS_ERROR_INVALID_PARAMETER;
626     }
627
628     if (idx >= 0) {
629         int _width = 0;
630         int _height = 0;
631         int _pixel_size = 0;
632         /* To validate the buffer */
633         if (dynamicbox_provider_buffer_get_size(handle, &_width, &_height, &_pixel_size) < 0) {
634             ErrPrint("Failed to get buffer size\n");
635         }
636
637         if (_width != width || _height != height || pixel_size != _pixel_size) {
638             DbgPrint("Extra buffer's geometry is not matched with primary one\n");
639         }
640
641         ret = dynamicbox_provider_buffer_extra_acquire(handle, idx, width, height, pixel_size);
642     } else {
643         ret = dynamicbox_provider_buffer_acquire(handle, width, height, pixel_size);
644     }
645
646     return ret;
647 }
648
649 PUBLIC dynamicbox_buffer_h dynamicbox_create_buffer(const char *filename, int is_gbar, int auto_align, int (*handler)(dynamicbox_buffer_h , dynamicbox_buffer_event_data_t, void *), void *data)
650 {
651     struct dynamicbox_buffer_data *user_data;
652     const char *pkgname;
653     dynamicbox_buffer_h handle;
654     char *uri;
655
656     if (!filename) {
657         ErrPrint("Invalid argument: %p(%dx%d)\n", filename);
658         return NULL;
659     }
660
661     user_data = calloc(1, sizeof(*user_data));
662     if (!user_data) {
663         ErrPrint("Heap: %s\n", strerror(errno));
664         return NULL;
665     }
666
667     user_data->is_gbar = is_gbar;
668     user_data->handler = handler ? handler : default_event_handler;
669     user_data->cbdata = data;
670
671     uri = id_to_uri(filename);
672     if (!uri) {
673         ErrPrint("Heap: %s\n", strerror(errno));
674         free(user_data);
675         return NULL;
676     }
677
678     if (!s_info.find_pkgname) {
679         s_info.find_pkgname = dlsym(RTLD_DEFAULT, FUNC_DYNAMICBOX_FIND_PKGNAME);
680         if (!s_info.find_pkgname) {
681             ErrPrint("Failed to find a \"dynamicbox_find_pkgname\"\n");
682             free(user_data);
683             free(uri);
684             return NULL;
685         }
686     }
687
688     pkgname = s_info.find_pkgname(uri);
689     if (!pkgname) {
690         ErrPrint("Invalid Request\n");
691         free(user_data);
692         free(uri);
693         return NULL;
694     }
695
696     handle = dynamicbox_provider_buffer_create((!!is_gbar) ? DBOX_TYPE_GBAR : DBOX_TYPE_DBOX, pkgname, uri, auto_align, event_handler_wrapper, user_data);
697     free(uri);
698     if (!handle) {
699         free(user_data);
700         return NULL;
701     }
702
703     (void)dynamicbox_provider_buffer_set_user_data(handle, user_data);
704     return handle;
705 }
706
707 PUBLIC int dynamicbox_request_update(const char *filename)
708 {
709     char *uri;
710     int ret;
711
712     if (!filename) {
713         ErrPrint("Invalid argument\n");
714         return DBOX_STATUS_ERROR_INVALID_PARAMETER;
715     }
716
717     uri = id_to_uri(filename);
718     if (!uri) {
719         ErrPrint("Heap: %s\n", strerror(errno));
720         return DBOX_STATUS_ERROR_OUT_OF_MEMORY;
721     }
722
723     if (!s_info.request_update_by_id) {
724         s_info.request_update_by_id = dlsym(RTLD_DEFAULT, FUNC_DYNAMICBOX_REQUEST_UPDATE_BY_ID);
725         if (!s_info.request_update_by_id) {
726             ErrPrint("\"dynamicbox_request_update_by_id\" is not exists\n");
727             free(uri);
728             return DBOX_STATUS_ERROR_FAULT;
729         }
730     }
731
732     ret = s_info.request_update_by_id(uri);
733     free(uri);
734     return ret;
735 }
736
737 PUBLIC unsigned int dynamicbox_resource_id(dynamicbox_buffer_h handle, int idx)
738 {
739     int ret;
740
741     if (!handle || idx > DYNAMICBOX_CONF_EXTRA_BUFFER_COUNT) {
742         return DBOX_STATUS_ERROR_INVALID_PARAMETER;
743     }
744
745     if (idx >= 0) {
746         ret = dynamicbox_provider_buffer_extra_resource_id(handle, idx);
747     } else {
748         ret = dynamicbox_provider_buffer_resource_id(handle);
749     }
750
751     return ret;
752 }
753
754 PUBLIC int dynamicbox_release_buffer(dynamicbox_buffer_h handle, int idx)
755 {
756     int ret;
757
758     if (!handle || idx > DYNAMICBOX_CONF_EXTRA_BUFFER_COUNT) {
759         return DBOX_STATUS_ERROR_INVALID_PARAMETER;
760     }
761
762     if (idx >= 0) {
763         ret = dynamicbox_provider_buffer_extra_release(handle, idx);
764     } else {
765         ret = dynamicbox_provider_buffer_release(handle);
766     }
767
768     DbgPrint("Release buffer: %d (%d)\n", idx, ret);
769     return ret;
770 }
771
772 PUBLIC int dynamicbox_destroy_buffer(dynamicbox_buffer_h handle)
773 {
774     struct dynamicbox_buffer_data *user_data;
775
776     if (!handle) {
777         return DBOX_STATUS_ERROR_INVALID_PARAMETER;
778     }
779
780     user_data = dynamicbox_provider_buffer_user_data(handle);
781     if (user_data) {
782         if (user_data->frame_wait_timer) {
783             ecore_timer_del(user_data->frame_wait_timer);
784             user_data->frame_wait_timer = NULL;
785         }
786         free(user_data->content);
787         free(user_data->title);
788         free(user_data->icon);
789         free(user_data->name);
790         free(user_data);
791         dynamicbox_provider_buffer_set_user_data(handle, NULL);
792     }
793
794     DbgPrint("Destroy buffer\n");
795     return dynamicbox_provider_buffer_destroy(handle);
796 }
797
798 PUBLIC void *dynamicbox_ref_buffer(dynamicbox_buffer_h handle)
799 {
800     struct dynamicbox_buffer_data *user_data;
801     void *data;
802     int w, h, size;
803     int ret;
804
805     if (!handle) {
806         return NULL;
807     }
808
809     user_data = dynamicbox_provider_buffer_user_data(handle);
810     if (!user_data) {
811         return NULL;
812     }
813
814     if (user_data->accelerated) {
815         DbgPrint("H/W accelerated buffer is allocated\n");
816         return NULL;
817     }
818
819     ret = dynamicbox_provider_buffer_get_size(handle, &w, &h, &size);
820
821     data = dynamicbox_provider_buffer_ref(handle);
822     if (data && !ret && w > 0 && h > 0 && size > 0) {
823         memset(data, 0, w * h * size);
824         (void)dynamicbox_provider_buffer_sync(handle);
825     }
826
827     DbgPrint("Ref buffer %ds%d(%d)\n", w, h, size);
828     return data;
829 }
830
831 PUBLIC int dynamicbox_unref_buffer(void *buffer)
832 {
833     if (!buffer) {
834         return DBOX_STATUS_ERROR_INVALID_PARAMETER;
835     }
836
837     DbgPrint("Unref buffer\n");
838     return dynamicbox_provider_buffer_unref(buffer);
839 }
840
841 PUBLIC int dynamicbox_sync_buffer(dynamicbox_buffer_h handle)
842 {
843     struct dynamicbox_buffer_data *user_data;
844     const char *pkgname;
845     const char *id;
846     int w;
847     int h;
848     int pixel_size;
849     int ret;
850
851     if (!handle) {
852         return DBOX_STATUS_ERROR_INVALID_PARAMETER;
853     }
854
855     user_data = dynamicbox_provider_buffer_user_data(handle);
856     if (!user_data) {
857         ErrPrint("Invalid buffer\n");
858         return DBOX_STATUS_ERROR_INVALID_PARAMETER;
859     }
860
861     if (user_data->accelerated) {
862         DbgPrint("H/W Buffer allocated. skip the sync buffer\n");
863         return DBOX_STATUS_ERROR_NONE;
864     }
865
866     pkgname = dynamicbox_provider_buffer_pkgname(handle);
867     if (!pkgname) {
868         ErrPrint("Invalid buffer handler\n");
869         return DBOX_STATUS_ERROR_INVALID_PARAMETER;
870     }
871
872     id = dynamicbox_provider_buffer_id(handle);
873     if (!id) {
874         ErrPrint("Invalid buffer handler\n");
875         return DBOX_STATUS_ERROR_INVALID_PARAMETER;
876     }
877
878     (void)dynamicbox_provider_buffer_sync(handle);
879
880     if (dynamicbox_provider_buffer_get_size(handle, &w, &h, &pixel_size) < 0) {
881         ErrPrint("Failed to get size (%s)\n", id);
882     }
883
884     /**
885      * @todo
886      * manipulate the damaged region, so send update event only for the damaged region.
887      * to make more efficient updates
888      */
889     ret = send_updated(pkgname, id, handle, DBOX_PRIMARY_BUFFER, 0, 0, w, h, user_data->is_gbar, NULL);
890     if (ret < 0) {
891         ErrPrint("Failed to send%supdated (%s)\n", user_data->is_gbar ? " GBAR " : " ", id);
892     }
893
894     return ret;
895 }
896
897 PUBLIC int dynamicbox_send_updated_by_idx(dynamicbox_buffer_h handle, int idx)
898 {
899     struct dynamicbox_buffer_data *user_data;
900     const char *pkgname;
901     const char *id;
902     int w;
903     int h;
904     int pixel_size;
905     int ret;
906
907     user_data = dynamicbox_provider_buffer_user_data(handle);
908     if (!user_data) {
909         ErrPrint("Invalid buffer\n");
910         return DBOX_STATUS_ERROR_INVALID_PARAMETER;
911     }
912
913     pkgname = dynamicbox_provider_buffer_pkgname(handle);
914     if (!pkgname) {
915         ErrPrint("Invalid buffer handler\n");
916         return DBOX_STATUS_ERROR_INVALID_PARAMETER;
917     }
918
919     id = dynamicbox_provider_buffer_id(handle);
920     if (!id) {
921         ErrPrint("Invalid buffer handler\n");
922         return DBOX_STATUS_ERROR_INVALID_PARAMETER;
923     }
924
925     if (dynamicbox_provider_buffer_get_size(handle, &w, &h, &pixel_size) < 0) {
926         ErrPrint("Failed to get size (%s)\n", id);
927     }
928
929     ret = send_updated(pkgname, id, handle, idx, 0, 0, w, h, user_data->is_gbar, NULL);
930     if (ret < 0) {
931         ErrPrint("Failed to send%supdated (%s) %d\n", user_data->is_gbar ? " GBAR " : " ", id, idx);
932     }
933
934     return ret;
935 }
936
937 PUBLIC int dynamicbox_support_hw_buffer(dynamicbox_buffer_h handle)
938 {
939     if (!handle) {
940         return DBOX_STATUS_ERROR_INVALID_PARAMETER;
941     }
942
943     return dynamicbox_provider_buffer_is_support_hw(handle);
944 }
945
946 PUBLIC int dynamicbox_create_hw_buffer(dynamicbox_buffer_h handle)
947 {
948     struct dynamicbox_buffer_data *user_data;
949     int ret;
950
951     if (!handle) {
952         return DBOX_STATUS_ERROR_INVALID_PARAMETER;
953     }
954
955     user_data = dynamicbox_provider_buffer_user_data(handle);
956     if (!user_data) {
957         return DBOX_STATUS_ERROR_INVALID_PARAMETER;
958     }
959
960     if (user_data->accelerated) {
961         return DBOX_STATUS_ERROR_ALREADY;
962     }
963
964     ret = dynamicbox_provider_buffer_create_hw(handle);
965     user_data->accelerated = (ret == 0);
966     return ret;
967 }
968
969 PUBLIC int dynamicbox_destroy_hw_buffer(dynamicbox_buffer_h handle)
970 {
971     struct dynamicbox_buffer_data *user_data;
972
973     if (!handle) {
974         LOGD("handle is NULL\n");
975         return DBOX_STATUS_ERROR_INVALID_PARAMETER;
976     }
977
978     user_data = dynamicbox_provider_buffer_user_data(handle);
979     if (!user_data || !user_data->accelerated) {
980         LOGD("user_data is NULL\n");
981         return DBOX_STATUS_ERROR_INVALID_PARAMETER;
982     }
983
984     user_data->accelerated = 0;
985
986     return dynamicbox_provider_buffer_destroy_hw(handle);
987 }
988
989 PUBLIC void *dynamicbox_buffer_hw_buffer(dynamicbox_buffer_h handle)
990 {
991     struct dynamicbox_buffer_data *user_data;
992
993     if (!handle) {
994         return NULL;
995     }
996
997     user_data = dynamicbox_provider_buffer_user_data(handle);
998     if (!user_data || !user_data->accelerated) {
999         return NULL;
1000     }
1001
1002     return dynamicbox_provider_buffer_hw_addr(handle);
1003 }
1004
1005 PUBLIC int dynamicbox_buffer_pre_render(dynamicbox_buffer_h handle)
1006 {
1007     struct dynamicbox_buffer_data *user_data;
1008
1009     if (!handle) {
1010         return DBOX_STATUS_ERROR_INVALID_PARAMETER;
1011     }
1012
1013     user_data = dynamicbox_provider_buffer_user_data(handle);
1014     if (!user_data) {
1015         return DBOX_STATUS_ERROR_INVALID_PARAMETER;
1016     }
1017
1018     if (!user_data->accelerated) {
1019         return DBOX_STATUS_ERROR_NONE;
1020     }
1021
1022     /*!
1023      * \note
1024      * Do preprocessing for accessing the H/W render buffer
1025      */
1026     return dynamicbox_provider_buffer_pre_render(handle);
1027 }
1028
1029 static Eina_Bool frame_wait_cb(void *data)
1030 {
1031     dynamicbox_buffer_h handle = data;
1032     struct dynamicbox_buffer_data *user_data;
1033     const char *pkgname;
1034     const char *id;
1035     int pixel_size;
1036     int ret;
1037     int w;
1038     int h;
1039
1040     user_data = dynamicbox_provider_buffer_user_data(handle);
1041     if (!user_data) {
1042         ErrPrint("Failed to get a user data\n");
1043         return ECORE_CALLBACK_CANCEL;
1044     }
1045
1046     pkgname = dynamicbox_provider_buffer_pkgname(handle);
1047     id = dynamicbox_provider_buffer_id(handle);
1048
1049     if (!pkgname || !id) {
1050         ErrPrint("Failed to get instance information (%s), (%s)\n", pkgname, id);
1051         user_data->frame_wait_timer = NULL;
1052         return ECORE_CALLBACK_CANCEL;
1053     }
1054
1055     if (dynamicbox_provider_buffer_get_size(handle, &w, &h, &pixel_size) < 0) {
1056         ErrPrint("Failed to get size (%s)\n", id);
1057         w = 0;
1058         h = 0;
1059         pixel_size = sizeof(int);
1060     }
1061
1062     DbgPrint("Frame skip waiting timer is fired (%s)\n", id);
1063
1064     /**
1065      * @todo
1066      * manipulate the damaged region, so send update event only for the damaged region.
1067      * to make more efficient updates
1068      */
1069     ret = send_updated(pkgname, id, handle, DBOX_PRIMARY_BUFFER, 0, 0, w, h, user_data->is_gbar, NULL);
1070     if (ret < 0) {
1071         ErrPrint("Failed to send%supdated (%s)\n", user_data->is_gbar ? " GBAR " : " ", id);
1072     }
1073
1074     (void)dynamicbox_provider_buffer_clear_frame_skip(handle);
1075
1076     user_data->frame_wait_timer = NULL;
1077     return ECORE_CALLBACK_CANCEL;
1078 }
1079
1080 PUBLIC int dynamicbox_buffer_post_render(dynamicbox_buffer_h handle)
1081 {
1082     int ret;
1083     const char *pkgname;
1084     const char *id;
1085     struct dynamicbox_buffer_data *user_data;
1086     int w;
1087     int h;
1088     int pixel_size;
1089     PERF_INIT();
1090     PERF_BEGIN();
1091
1092     if (!handle) {
1093         PERF_MARK("post_render");
1094         return DBOX_STATUS_ERROR_INVALID_PARAMETER;
1095     }
1096
1097     user_data = dynamicbox_provider_buffer_user_data(handle);
1098     if (!user_data) {
1099         PERF_MARK("post_render");
1100         return DBOX_STATUS_ERROR_INVALID_PARAMETER;
1101     }
1102
1103     if (!user_data->accelerated) {
1104         PERF_MARK("post_render");
1105         return DBOX_STATUS_ERROR_NONE;
1106     }
1107
1108     pkgname = dynamicbox_provider_buffer_pkgname(handle);
1109     if (!pkgname) {
1110         ErrPrint("Invalid buffer handle\n");
1111         PERF_MARK("post_render");
1112         return DBOX_STATUS_ERROR_INVALID_PARAMETER;
1113     }
1114
1115     id = dynamicbox_provider_buffer_id(handle);
1116     if (!id) {
1117         ErrPrint("Invalid buffer handler\n");
1118         PERF_MARK("post_render");
1119         return DBOX_STATUS_ERROR_INVALID_PARAMETER;
1120     }
1121
1122     ret = dynamicbox_provider_buffer_post_render(handle);
1123     if (ret < 0) {
1124         ErrPrint("Failed to post render processing\n");
1125         PERF_MARK("post_render");
1126         return ret;
1127     }
1128
1129     if (dynamicbox_provider_buffer_frame_skip(handle) == 0) {
1130         if (dynamicbox_provider_buffer_get_size(handle, &w, &h, &pixel_size) < 0) {
1131             ErrPrint("Failed to get size (%s)\n", id);
1132         }
1133
1134         /**
1135          * @todo
1136          * manipulate the damaged region, so send update event only for the damaged region.
1137          * to make more efficient updates
1138          */
1139         ret = send_updated(pkgname, id, handle, DBOX_PRIMARY_BUFFER, 0, 0, w, h, user_data->is_gbar, NULL);
1140         if (ret < 0) {
1141             ErrPrint("Failed to send%supdated (%s)\n", user_data->is_gbar ? " GBAR " : " ", id);
1142         }
1143
1144         if (user_data->frame_wait_timer) {
1145             ecore_timer_del(user_data->frame_wait_timer);
1146             user_data->frame_wait_timer = NULL;
1147         }
1148     } else {
1149         if (user_data->frame_wait_timer) {
1150             ecore_timer_reset(user_data->frame_wait_timer);
1151         } else {
1152             user_data->frame_wait_timer = ecore_timer_add(FRAME_WAIT_INTERVAL, frame_wait_cb, handle);
1153             if (!user_data->frame_wait_timer) {
1154                 ErrPrint("Failed to add waiting timer for next frame\n");
1155             }
1156         }
1157     }
1158
1159     PERF_MARK("post_render");
1160     return ret;
1161 }
1162
1163 PUBLIC int dynamicbox_buffer_stride(dynamicbox_buffer_h handle)
1164 {
1165     if (!handle) {
1166         return DBOX_STATUS_ERROR_INVALID_PARAMETER;
1167     }
1168
1169     return dynamicbox_provider_buffer_stride(handle);
1170 }
1171
1172 PUBLIC int dynamicbox_content_is_updated(const char *filename, int is_gbar)
1173 {
1174     if (!s_info.trigger_update_monitor) {
1175         s_info.trigger_update_monitor = dlsym(RTLD_DEFAULT, FUNC_DYNAMICBOX_TRIGGER_UPDATE_MONITOR);
1176         if (!s_info.trigger_update_monitor) {
1177             ErrPrint("Trigger update monitor function is not exists\n");
1178             return DBOX_STATUS_ERROR_FAULT;
1179         }
1180     }
1181
1182     return s_info.trigger_update_monitor(filename, is_gbar);
1183 }
1184
1185 PUBLIC int dynamicbox_request_close_glance_bar(const char *pkgname, const char *id, int reason)
1186 {
1187     char *uri;
1188     int schema_len = strlen(FILE_SCHEMA);
1189     int ret;
1190
1191     if (!pkgname || !id) {
1192         ErrPrint("Invalid parameters (%s) (%s)\n", pkgname, id);
1193         return DBOX_STATUS_ERROR_INVALID_PARAMETER;
1194     }
1195
1196     if (strncmp(id, FILE_SCHEMA, schema_len)) {
1197         uri = id_to_uri(id);
1198         if (!uri) {
1199             ErrPrint("Heap: %s\n", strerror(errno));
1200             return DBOX_STATUS_ERROR_OUT_OF_MEMORY;
1201         }
1202     } else {
1203         uri = strdup(id);
1204         if (!uri) {
1205             ErrPrint("Heap: %s\n", strerror(errno));
1206             return DBOX_STATUS_ERROR_OUT_OF_MEMORY;
1207         }
1208     }
1209
1210     ret = dynamicbox_provider_send_request_close_gbar(pkgname, uri, reason);
1211     free(uri);
1212     return ret;
1213 }
1214
1215 PUBLIC int dynamicbox_freeze_scroller(const char *pkgname, const char *id)
1216 {
1217     char *uri;
1218     int ret;
1219
1220     uri = id_to_uri(id);
1221     if (!uri) {
1222         ErrPrint("Heap: %s\n", strerror(errno));
1223         return DBOX_STATUS_ERROR_OUT_OF_MEMORY;
1224     }
1225
1226     ret = dynamicbox_provider_send_hold_scroll(pkgname, uri, 1);
1227     free(uri);
1228     return ret;
1229 }
1230
1231 PUBLIC int dynamicbox_thaw_scroller(const char *pkgname, const char *id)
1232 {
1233     char *uri;
1234     int ret;
1235
1236     uri = id_to_uri(id);
1237     if (!uri) {
1238         ErrPrint("Heap: %s\n", strerror(errno));
1239         return DBOX_STATUS_ERROR_OUT_OF_MEMORY;
1240     }
1241
1242     ret = dynamicbox_provider_send_hold_scroll(pkgname, uri, 0);
1243     free(uri);
1244     return ret;
1245 }
1246
1247 PUBLIC int dynamicbox_set_extra_info(const char *id, const char *content, const char *title, const char *icon, const char *name)
1248 {
1249     dynamicbox_buffer_h handle;
1250     const char *pkgname;
1251     char *uri;
1252
1253     uri = id_to_uri(id);
1254     if (!uri) {
1255         ErrPrint("Heap: %s\n", strerror(errno));
1256         return DBOX_STATUS_ERROR_OUT_OF_MEMORY;
1257     }
1258
1259     if (!s_info.find_pkgname) {
1260         s_info.find_pkgname = dlsym(RTLD_DEFAULT, FUNC_DYNAMICBOX_FIND_PKGNAME);
1261         if (!s_info.find_pkgname) {
1262             ErrPrint("Failed to find a \"dynamicbox_find_pkgname\"\n");
1263             free(uri);
1264             return DBOX_STATUS_ERROR_FAULT;
1265         }
1266     }
1267
1268     pkgname = s_info.find_pkgname(uri);
1269     if (!pkgname) {
1270         ErrPrint("Failed to find a package (%s)\n", uri);
1271         free(uri);
1272         return DBOX_STATUS_ERROR_INVALID_PARAMETER;
1273     }
1274
1275     handle = dynamicbox_provider_buffer_find_buffer(DBOX_TYPE_DBOX, pkgname, uri);
1276     if (handle) {
1277         struct dynamicbox_buffer_data *user_data;
1278
1279         user_data = dynamicbox_provider_buffer_user_data(handle);
1280         if (!user_data) {
1281             ErrPrint("User data is not available\n");
1282             free(uri);
1283             return DBOX_STATUS_ERROR_FAULT;
1284         }
1285
1286         if (content && strlen(content)) {
1287             char *_content;
1288
1289             _content = strdup(content);
1290             if (_content) {
1291                 if (user_data->content) {
1292                     free(user_data->content);
1293                 }
1294
1295                 user_data->content = _content;
1296             } else {
1297                 ErrPrint("Heap: %s\n", strerror(errno));
1298             }
1299         }
1300
1301         if (title && strlen(title)) {
1302             char *_title;
1303
1304             _title = strdup(title);
1305             if (_title) {
1306                 if (user_data->title) {
1307                     free(user_data->title);
1308                 }
1309
1310                 user_data->title = _title;
1311             } else {
1312                 ErrPrint("Heap: %s\n", strerror(errno));
1313             }
1314         }
1315
1316         if (icon && strlen(icon)) {
1317             char *_icon;
1318
1319             _icon = strdup(icon);
1320             if (_icon) {
1321                 if (user_data->icon) {
1322                     free(user_data->icon);
1323                 }
1324
1325                 user_data->icon = _icon;
1326             } else {
1327                 ErrPrint("Heap: %s\n", strerror(errno));
1328             }
1329         }
1330
1331         if (name && strlen(name)) {
1332             char *_name;
1333
1334             _name = strdup(name);
1335             if (_name) {
1336                 if (user_data->name) {
1337                     free(user_data->name);
1338                 }
1339
1340                 user_data->name = _name;
1341             } else {
1342                 ErrPrint("Heap: %s\n", strerror(errno));
1343             }
1344         }
1345
1346         if (dynamicbox_provider_send_extra_info(pkgname, uri, -1.0f, user_data->content, user_data->title, user_data->icon, user_data->name) < 0) {
1347             ErrPrint("Failed to send extra info (%s)\n", id);
1348         }
1349
1350         free(uri);
1351         return DBOX_STATUS_ERROR_NONE;
1352     }
1353     free(uri);
1354
1355     if (!s_info.update_extra_info) {
1356         s_info.update_extra_info = dlsym(RTLD_DEFAULT, FUNC_DYNAMICBOX_UPDATE_EXTRA_INFO);
1357         if (!s_info.update_extra_info) {
1358             ErrPrint("Failed to find a \"dynamicbox_update_extra_info\"\n");
1359             return DBOX_STATUS_ERROR_INVALID_PARAMETER;
1360         }
1361     }
1362
1363     return s_info.update_extra_info(id, content, title, icon, name);
1364 }
1365
1366 /* End of a file */