2 * Copyright 2013 Samsung Electronics Co., Ltd
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
8 * http://floralicense.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 */
22 #include <unistd.h> /* access */
25 #include <livebox-service.h>
27 #include <provider_buffer.h>
28 #include <livebox-errno.h>
35 #define PUBLIC __attribute__((visibility("default")))
37 #define FILE_SCHEMA "file://"
40 * \brief This function is defined by the data-provider-slave
42 extern const char *livebox_find_pkgname(const char *filename);
43 extern int livebox_request_update_by_id(const char *uri);
44 extern int livebox_trigger_update_monitor(const char *id, int is_pd);
62 unsigned int last_idx;
64 struct dlist *block_list;
67 struct livebox_buffer_data {
71 /* for Buffer event wrapper */
72 int (*handler)(struct livebox_buffer *, enum buffer_event, double, double, double, void *);
76 PUBLIC const int DONE = 0x00;
77 PUBLIC const int OUTPUT_UPDATED = 0x02;
78 PUBLIC const int USE_NET = 0x04;
80 PUBLIC const int NEED_TO_SCHEDULE = 0x01;
81 PUBLIC const int NEED_TO_CREATE = 0x01;
82 PUBLIC const int NEED_TO_DESTROY = 0x01;
83 PUBLIC const int NEED_TO_UPDATE = 0x01;
85 PUBLIC const int LB_SYS_EVENT_FONT_CHANGED = 0x01;
86 PUBLIC const int LB_SYS_EVENT_LANG_CHANGED = 0x02;
87 PUBLIC const int LB_SYS_EVENT_TIME_CHANGED = 0x04;
88 PUBLIC const int LB_SYS_EVENT_REGION_CHANGED = 0x08;
89 PUBLIC const int LB_SYS_EVENT_PAUSED = 0x0100;
90 PUBLIC const int LB_SYS_EVENT_RESUMED = 0x0200;
91 PUBLIC const int LB_SYS_EVENT_MMC_STATUS_CHANGED = 0x0400;
93 PUBLIC struct livebox_desc *livebox_desc_open(const char *filename, int for_pd)
95 struct livebox_desc *handle;
98 handle = calloc(1, sizeof(*handle));
100 ErrPrint("Error: %s\n", strerror(errno));
106 len = strlen(filename) + strlen(".desc") + 1;
107 new_fname = malloc(len);
109 ErrPrint("Error: %s\n", strerror(errno));
113 snprintf(new_fname, len, "%s.desc", filename);
115 new_fname = strdup(filename);
117 ErrPrint("Error: %s\n", strerror(errno));
123 DbgPrint("Open a file %s with merge mode %s\n",
125 access(new_fname, F_OK) == 0 ? "enabled" : "disabled");
127 handle->fp = fopen(new_fname, "at");
130 ErrPrint("Failed to open a file: %s\n", strerror(errno));
138 PUBLIC int livebox_desc_close(struct livebox_desc *handle)
145 return LB_STATUS_ERROR_INVALID;
147 dlist_foreach_safe(handle->block_list, l, n, block) {
148 handle->block_list = dlist_remove(handle->block_list, l);
150 fprintf(handle->fp, "{\n");
152 fprintf(handle->fp, "type=%s\n", block->type);
155 fprintf(handle->fp, "part=%s\n", block->part);
158 fprintf(handle->fp, "data=%s\n", block->data);
161 fprintf(handle->fp, "option=%s\n", block->option);
164 fprintf(handle->fp, "id=%s\n", block->id);
166 if (block->target_id)
167 fprintf(handle->fp, "target=%s\n", block->target_id);
168 fprintf(handle->fp, "}\n");
175 free(block->target_id);
181 return LB_STATUS_SUCCESS;
184 PUBLIC int livebox_desc_set_category(struct livebox_desc *handle, const char *id, const char *category)
188 if (!handle || !category)
189 return LB_STATUS_ERROR_INVALID;
191 block = calloc(1, sizeof(*block));
193 return LB_STATUS_ERROR_MEMORY;
195 block->type = strdup(LB_DESC_TYPE_INFO);
198 return LB_STATUS_ERROR_MEMORY;
201 block->part = strdup("category");
205 return LB_STATUS_ERROR_MEMORY;
208 block->data = strdup(category);
213 return LB_STATUS_ERROR_MEMORY;
217 block->id = strdup(id);
223 return LB_STATUS_ERROR_MEMORY;
227 block->idx = handle->last_idx++;
228 handle->block_list = dlist_append(handle->block_list, block);
232 PUBLIC int livebox_desc_set_size(struct livebox_desc *handle, const char *id, int w, int h)
238 return LB_STATUS_ERROR_INVALID;
240 block = calloc(1, sizeof(*block));
242 return LB_STATUS_ERROR_MEMORY;
244 block->type = strdup(LB_DESC_TYPE_INFO);
247 return LB_STATUS_ERROR_MEMORY;
250 block->part = strdup("size");
254 return LB_STATUS_ERROR_MEMORY;
258 block->id = strdup(id);
263 return LB_STATUS_ERROR_MEMORY;
267 snprintf(buffer, sizeof(buffer), "%dx%d", w, h);
268 block->data = strdup(buffer);
273 return LB_STATUS_ERROR_MEMORY;
276 block->idx = handle->last_idx++;
277 handle->block_list = dlist_append(handle->block_list, block);
281 PUBLIC char *livebox_util_nl2br(const char *str)
295 ret = malloc(len + 1);
308 tmp = realloc(ret, len + 1);
335 PUBLIC int livebox_desc_set_id(struct livebox_desc *handle, int idx, const char *id)
340 dlist_foreach(handle->block_list, l, block) {
341 if (block->idx == idx) {
342 if (strcasecmp(block->type, LB_DESC_TYPE_SCRIPT)) {
343 ErrPrint("Invalid block is used\n");
344 return LB_STATUS_ERROR_INVALID;
347 free(block->target_id);
348 block->target_id = NULL;
350 if (!id || !strlen(id))
351 return LB_STATUS_SUCCESS;
353 block->target_id = strdup(id);
354 if (!block->target_id) {
355 ErrPrint("Heap: %s\n", strerror(errno));
356 return LB_STATUS_ERROR_MEMORY;
359 return LB_STATUS_SUCCESS;
363 return LB_STATUS_ERROR_NOT_EXIST;
369 PUBLIC int livebox_desc_add_block(struct livebox_desc *handle, const char *id, const char *type, const char *part, const char *data, const char *option)
373 if (!handle || !type)
374 return LB_STATUS_ERROR_INVALID;
382 block = calloc(1, sizeof(*block));
384 ErrPrint("Heap: %s\n", strerror(errno));
385 return LB_STATUS_ERROR_MEMORY;
388 block->type = strdup(type);
390 ErrPrint("Heap: %s\n", strerror(errno));
392 return LB_STATUS_ERROR_MEMORY;
395 block->part = strdup(part);
397 ErrPrint("Heap: %s\n", strerror(errno));
400 return LB_STATUS_ERROR_MEMORY;
403 block->data = strdup(data);
405 ErrPrint("Heap: %s\n", strerror(errno));
409 return LB_STATUS_ERROR_MEMORY;
413 block->option = strdup(option);
414 if (!block->option) {
415 ErrPrint("Heap: %s\n", strerror(errno));
420 return LB_STATUS_ERROR_MEMORY;
425 block->id = strdup(id);
427 ErrPrint("Heap: %s\n", strerror(errno));
433 return LB_STATUS_ERROR_MEMORY;
437 block->idx = handle->last_idx++;
438 handle->block_list = dlist_append(handle->block_list, block);
442 PUBLIC int livebox_desc_del_block(struct livebox_desc *handle, int idx)
447 dlist_foreach(handle->block_list, l, block) {
448 if (block->idx == idx) {
449 handle->block_list = dlist_remove(handle->block_list, l);
455 free(block->target_id);
457 return LB_STATUS_SUCCESS;
461 return LB_STATUS_ERROR_NOT_EXIST;
466 * The last "data" argument is same with "user_data" which is managed by "provider_set_user_data).
468 static inline int event_handler_wrapper(struct livebox_buffer *buffer, enum buffer_event event, double timestamp, double x, double y, void *data)
472 struct livebox_buffer_data *cbdata = data;
475 pkgname = provider_buffer_pkgname(buffer);
476 id = provider_buffer_pkgname(buffer);
478 ret = cbdata->handler(buffer, event, timestamp, x, y, cbdata->cbdata);
481 case BUFFER_EVENT_HIGHLIGHT:
482 case BUFFER_EVENT_HIGHLIGHT_NEXT:
483 case BUFFER_EVENT_HIGHLIGHT_PREV:
484 case BUFFER_EVENT_ACTIVATE:
485 case BUFFER_EVENT_ACTION_UP:
486 case BUFFER_EVENT_ACTION_DOWN:
487 case BUFFER_EVENT_SCROLL_UP:
488 case BUFFER_EVENT_SCROLL_MOVE:
489 case BUFFER_EVENT_SCROLL_DOWN:
490 case BUFFER_EVENT_UNHIGHLIGHT:
492 (void)provider_send_access_status(pkgname, id, LB_ACCESS_STATUS_ERROR);
494 (void)provider_send_access_status(pkgname, id, ret);
503 static inline int default_event_handler(struct livebox_buffer *buffer, enum buffer_event event, double timestamp, double x, double y, void *data)
509 PUBLIC struct livebox_buffer *livebox_acquire_buffer(const char *filename, int is_pd, int width, int height, int (*handler)(struct livebox_buffer *, enum buffer_event, double, double, double, void *), void *data)
511 struct livebox_buffer_data *user_data;
513 struct livebox_buffer *handle;
516 struct event_cbdata *cbdata;
518 if (!filename || !width || !height) {
519 ErrPrint("Invalid argument: %p(%dx%d)\n", filename, width, height);
523 user_data = calloc(1, sizeof(*user_data));
525 ErrPrint("Heap: %s\n", strerror(errno));
529 user_data->is_pd = is_pd;
530 user_data->handler = handler ? handler : default_event_handler;
531 user_data->cbdata = data;
533 uri_len = strlen(filename) + strlen(FILE_SCHEMA) + 1;
534 uri = malloc(uri_len);
536 ErrPrint("Heap: %s\n", strerror(errno));
541 snprintf(uri, uri_len, FILE_SCHEMA "%s", filename);
542 pkgname = livebox_find_pkgname(uri);
544 ErrPrint("Invalid Request\n");
550 handle = provider_buffer_acquire((!!is_pd) ? TYPE_PD : TYPE_LB, pkgname, uri, width, height, sizeof(int), event_handler_wrapper, user_data);
551 DbgPrint("Acquire buffer for PD(%s), %s, %p\n", pkgname, uri, handle);
558 (void)provider_buffer_set_user_data(handle, user_data);
562 PUBLIC int livebox_request_update(const char *filename)
569 ErrPrint("Invalid argument\n");
570 return LB_STATUS_ERROR_INVALID;
573 uri_len = strlen(filename) + strlen(FILE_SCHEMA) + 1;
574 uri = malloc(uri_len);
576 ErrPrint("Heap: %s\n", strerror(errno));
577 return LB_STATUS_ERROR_MEMORY;
580 snprintf(uri, uri_len, FILE_SCHEMA "%s", filename);
581 ret = livebox_request_update_by_id(uri);
586 PUBLIC unsigned long livebox_pixmap_id(struct livebox_buffer *handle)
588 return provider_buffer_pixmap_id(handle);
591 PUBLIC int livebox_release_buffer(struct livebox_buffer *handle)
593 struct livebox_buffer_data *user_data;
596 return LB_STATUS_ERROR_INVALID;
598 user_data = provider_buffer_user_data(handle);
601 provider_buffer_set_user_data(handle, NULL);
604 DbgPrint("Release buffer\n");
605 return provider_buffer_release(handle);
608 PUBLIC void *livebox_ref_buffer(struct livebox_buffer *handle)
610 struct livebox_buffer_data *user_data;
618 user_data = provider_buffer_user_data(handle);
622 if (user_data->accelerated) {
623 DbgPrint("H/W accelerated buffer is allocated\n");
627 ret = provider_buffer_get_size(handle, &w, &h, &size);
629 data = provider_buffer_ref(handle);
630 if (data && !ret && w > 0 && h > 0 && size > 0) {
631 memset(data, 0, w * h * size);
632 provider_buffer_sync(handle);
635 DbgPrint("Ref buffer %ds%d(%d)\n", w, h, size);
639 PUBLIC int livebox_unref_buffer(void *buffer)
642 return LB_STATUS_ERROR_INVALID;
644 DbgPrint("Unref buffer\n");
645 return provider_buffer_unref(buffer);
648 PUBLIC int livebox_sync_buffer(struct livebox_buffer *handle)
650 struct livebox_buffer_data *user_data;
655 return LB_STATUS_ERROR_INVALID;
657 user_data = provider_buffer_user_data(handle);
659 ErrPrint("Invalid buffer\n");
660 return LB_STATUS_ERROR_INVALID;
663 if (user_data->accelerated) {
664 DbgPrint("H/W Buffer allocated. skip the sync buffer\n");
665 return LB_STATUS_SUCCESS;
668 pkgname = provider_buffer_pkgname(handle);
670 ErrPrint("Invalid buffer handler\n");
671 return LB_STATUS_ERROR_INVALID;
674 id = provider_buffer_id(handle);
676 ErrPrint("Invalid buffer handler\n");
677 return LB_STATUS_ERROR_INVALID;
680 provider_buffer_sync(handle);
682 if (user_data->is_pd) {
683 if (provider_send_desc_updated(pkgname, id, NULL) < 0)
684 ErrPrint("Failed to send PD updated (%s)\n", id);
690 if (provider_buffer_get_size(handle, &w, &h, &pixel_size) < 0)
691 ErrPrint("Failed to get size (%s)\n", id);
693 if (provider_send_updated(pkgname, id, w, h, -1.0f, NULL, NULL) < 0)
694 ErrPrint("Failed to send updated (%s)\n", id);
697 return LB_STATUS_SUCCESS;
700 PUBLIC int livebox_support_hw_buffer(struct livebox_buffer *handle)
703 return LB_STATUS_ERROR_INVALID;
705 return provider_buffer_pixmap_is_support_hw(handle);
708 PUBLIC int livebox_create_hw_buffer(struct livebox_buffer *handle)
710 struct livebox_buffer_data *user_data;
714 return LB_STATUS_ERROR_INVALID;
716 user_data = provider_buffer_user_data(handle);
718 return LB_STATUS_ERROR_INVALID;
720 if (user_data->accelerated)
723 ret = provider_buffer_pixmap_create_hw(handle);
724 user_data->accelerated = (ret == 0);
728 PUBLIC int livebox_destroy_hw_buffer(struct livebox_buffer *handle)
730 struct livebox_buffer_data *user_data;
732 return LB_STATUS_ERROR_INVALID;
734 user_data = provider_buffer_user_data(handle);
735 if (!user_data || !user_data->accelerated)
736 return LB_STATUS_ERROR_INVALID;
738 user_data->accelerated = 0;
740 return provider_buffer_pixmap_destroy_hw(handle);
743 PUBLIC void *livebox_buffer_hw_buffer(struct livebox_buffer *handle)
745 struct livebox_buffer_data *user_data;
750 user_data = provider_buffer_user_data(handle);
751 if (!user_data || !user_data->accelerated)
754 return provider_buffer_pixmap_hw_addr(handle);
757 PUBLIC int livebox_buffer_pre_render(struct livebox_buffer *handle)
759 struct livebox_buffer_data *user_data;
762 return LB_STATUS_ERROR_INVALID;
764 user_data = provider_buffer_user_data(handle);
766 return LB_STATUS_ERROR_INVALID;
768 if (!user_data->accelerated)
769 return LB_STATUS_SUCCESS;
773 * Do preprocessing for accessing the H/W render buffer
775 return provider_buffer_pre_render(handle);
778 PUBLIC int livebox_buffer_post_render(struct livebox_buffer *handle)
783 struct livebox_buffer_data *user_data;
786 return LB_STATUS_ERROR_INVALID;
788 user_data = provider_buffer_user_data(handle);
790 return LB_STATUS_ERROR_INVALID;
792 if (!user_data->accelerated)
793 return LB_STATUS_SUCCESS;
795 pkgname = provider_buffer_pkgname(handle);
797 ErrPrint("Invalid buffer handle\n");
798 return LB_STATUS_ERROR_INVALID;
801 id = provider_buffer_id(handle);
803 ErrPrint("Invalid buffer handler\n");
804 return LB_STATUS_ERROR_INVALID;
807 ret = provider_buffer_post_render(handle);
809 ErrPrint("Failed to post render processing\n");
813 if (user_data->is_pd == 1) {
814 if (provider_send_desc_updated(pkgname, id, NULL) < 0)
815 ErrPrint("Failed to send PD updated (%s)\n", id);
821 if (provider_buffer_get_size(handle, &w, &h, &pixel_size) < 0)
822 ErrPrint("Failed to get size (%s)\n", id);
824 if (provider_send_updated(pkgname, id, w, h, -1.0f, NULL, NULL) < 0)
825 ErrPrint("Failed to send updated (%s)\n", id);
828 return LB_STATUS_SUCCESS;
831 PUBLIC int livebox_content_is_updated(const char *filename, int is_pd)
833 return livebox_trigger_update_monitor(filename, is_pd);