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);
61 unsigned int last_idx;
63 struct dlist *block_list;
66 struct livebox_buffer_data {
70 /* for Buffer event wrapper */
71 int (*handler)(struct livebox_buffer *, enum buffer_event, double, double, double, void *);
75 PUBLIC const int DONE = 0x00;
76 PUBLIC const int OUTPUT_UPDATED = 0x02;
77 PUBLIC const int USE_NET = 0x04;
79 PUBLIC const int NEED_TO_SCHEDULE = 0x01;
80 PUBLIC const int NEED_TO_CREATE = 0x01;
81 PUBLIC const int NEED_TO_DESTROY = 0x01;
82 PUBLIC const int NEED_TO_UPDATE = 0x01;
84 PUBLIC const int LB_SYS_EVENT_FONT_CHANGED = 0x01;
85 PUBLIC const int LB_SYS_EVENT_LANG_CHANGED = 0x02;
86 PUBLIC const int LB_SYS_EVENT_TIME_CHANGED = 0x04;
87 PUBLIC const int LB_SYS_EVENT_REGION_CHANGED = 0x08;
88 PUBLIC const int LB_SYS_EVENT_PAUSED = 0x0100;
89 PUBLIC const int LB_SYS_EVENT_RESUMED = 0x0200;
90 PUBLIC const int LB_SYS_EVENT_MMC_STATUS_CHANGED = 0x0400;
92 PUBLIC struct livebox_desc *livebox_desc_open(const char *filename, int for_pd)
94 struct livebox_desc *handle;
97 handle = calloc(1, sizeof(*handle));
99 ErrPrint("Error: %s\n", strerror(errno));
105 len = strlen(filename) + strlen(".desc") + 1;
106 new_fname = malloc(len);
108 ErrPrint("Error: %s\n", strerror(errno));
112 snprintf(new_fname, len, "%s.desc", filename);
114 new_fname = strdup(filename);
116 ErrPrint("Error: %s\n", strerror(errno));
122 DbgPrint("Open a file %s with merge mode %s\n",
124 access(new_fname, F_OK) == 0 ? "enabled" : "disabled");
126 handle->fp = fopen(new_fname, "at");
129 ErrPrint("Failed to open a file: %s\n", strerror(errno));
137 PUBLIC int livebox_desc_close(struct livebox_desc *handle)
144 return LB_STATUS_ERROR_INVALID;
146 dlist_foreach_safe(handle->block_list, l, n, block) {
147 handle->block_list = dlist_remove(handle->block_list, l);
149 fprintf(handle->fp, "{\n");
151 fprintf(handle->fp, "type=%s\n", block->type);
154 fprintf(handle->fp, "part=%s\n", block->part);
157 fprintf(handle->fp, "data=%s\n", block->data);
160 fprintf(handle->fp, "option=%s\n", block->option);
163 fprintf(handle->fp, "id=%s\n", block->id);
165 if (block->target_id)
166 fprintf(handle->fp, "target=%s\n", block->target_id);
167 fprintf(handle->fp, "}\n");
174 free(block->target_id);
180 return LB_STATUS_SUCCESS;
183 PUBLIC int livebox_desc_set_category(struct livebox_desc *handle, const char *id, const char *category)
187 if (!handle || !category)
188 return LB_STATUS_ERROR_INVALID;
190 block = calloc(1, sizeof(*block));
192 return LB_STATUS_ERROR_MEMORY;
194 block->type = strdup(LB_DESC_TYPE_INFO);
197 return LB_STATUS_ERROR_MEMORY;
200 block->part = strdup("category");
204 return LB_STATUS_ERROR_MEMORY;
207 block->data = strdup(category);
212 return LB_STATUS_ERROR_MEMORY;
216 block->id = strdup(id);
222 return LB_STATUS_ERROR_MEMORY;
226 block->idx = handle->last_idx++;
227 handle->block_list = dlist_append(handle->block_list, block);
231 PUBLIC int livebox_desc_set_size(struct livebox_desc *handle, const char *id, int w, int h)
237 return LB_STATUS_ERROR_INVALID;
239 block = calloc(1, sizeof(*block));
241 return LB_STATUS_ERROR_MEMORY;
243 block->type = strdup(LB_DESC_TYPE_INFO);
246 return LB_STATUS_ERROR_MEMORY;
249 block->part = strdup("size");
253 return LB_STATUS_ERROR_MEMORY;
257 block->id = strdup(id);
262 return LB_STATUS_ERROR_MEMORY;
266 snprintf(buffer, sizeof(buffer), "%dx%d", w, h);
267 block->data = strdup(buffer);
272 return LB_STATUS_ERROR_MEMORY;
275 block->idx = handle->last_idx++;
276 handle->block_list = dlist_append(handle->block_list, block);
280 PUBLIC char *livebox_util_nl2br(const char *str)
294 ret = malloc(len + 1);
307 tmp = realloc(ret, len + 1);
334 PUBLIC int livebox_desc_set_id(struct livebox_desc *handle, int idx, const char *id)
339 dlist_foreach(handle->block_list, l, block) {
340 if (block->idx == idx) {
341 if (strcasecmp(block->type, LB_DESC_TYPE_SCRIPT)) {
342 ErrPrint("Invalid block is used\n");
343 return LB_STATUS_ERROR_INVALID;
346 free(block->target_id);
347 block->target_id = NULL;
349 if (!id || !strlen(id))
350 return LB_STATUS_SUCCESS;
352 block->target_id = strdup(id);
353 if (!block->target_id) {
354 ErrPrint("Heap: %s\n", strerror(errno));
355 return LB_STATUS_ERROR_MEMORY;
358 return LB_STATUS_SUCCESS;
362 return LB_STATUS_ERROR_NOT_EXIST;
368 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)
372 if (!handle || !type)
373 return LB_STATUS_ERROR_INVALID;
381 block = calloc(1, sizeof(*block));
383 ErrPrint("Heap: %s\n", strerror(errno));
384 return LB_STATUS_ERROR_MEMORY;
387 block->type = strdup(type);
389 ErrPrint("Heap: %s\n", strerror(errno));
391 return LB_STATUS_ERROR_MEMORY;
394 block->part = strdup(part);
396 ErrPrint("Heap: %s\n", strerror(errno));
399 return LB_STATUS_ERROR_MEMORY;
402 block->data = strdup(data);
404 ErrPrint("Heap: %s\n", strerror(errno));
408 return LB_STATUS_ERROR_MEMORY;
412 block->option = strdup(option);
413 if (!block->option) {
414 ErrPrint("Heap: %s\n", strerror(errno));
419 return LB_STATUS_ERROR_MEMORY;
424 block->id = strdup(id);
426 ErrPrint("Heap: %s\n", strerror(errno));
432 return LB_STATUS_ERROR_MEMORY;
436 block->idx = handle->last_idx++;
437 handle->block_list = dlist_append(handle->block_list, block);
441 PUBLIC int livebox_desc_del_block(struct livebox_desc *handle, int idx)
446 dlist_foreach(handle->block_list, l, block) {
447 if (block->idx == idx) {
448 handle->block_list = dlist_remove(handle->block_list, l);
454 free(block->target_id);
456 return LB_STATUS_SUCCESS;
460 return LB_STATUS_ERROR_NOT_EXIST;
465 * The last "data" argument is same with "user_data" which is managed by "provider_set_user_data).
467 static inline int event_handler_wrapper(struct livebox_buffer *buffer, enum buffer_event event, double timestamp, double x, double y, void *data)
471 struct livebox_buffer_data *cbdata = data;
474 pkgname = provider_buffer_pkgname(buffer);
475 id = provider_buffer_pkgname(buffer);
477 ret = cbdata->handler(buffer, event, timestamp, x, y, cbdata->cbdata);
480 case BUFFER_EVENT_HIGHLIGHT:
481 case BUFFER_EVENT_HIGHLIGHT_NEXT:
482 case BUFFER_EVENT_HIGHLIGHT_PREV:
483 case BUFFER_EVENT_ACTIVATE:
484 case BUFFER_EVENT_ACTION_UP:
485 case BUFFER_EVENT_ACTION_DOWN:
486 case BUFFER_EVENT_SCROLL_UP:
487 case BUFFER_EVENT_SCROLL_MOVE:
488 case BUFFER_EVENT_SCROLL_DOWN:
489 case BUFFER_EVENT_UNHIGHLIGHT:
491 (void)provider_send_access_status(pkgname, id, LB_ACCESS_STATUS_ERROR);
493 (void)provider_send_access_status(pkgname, id, ret);
502 static inline int default_event_handler(struct livebox_buffer *buffer, enum buffer_event event, double timestamp, double x, double y, void *data)
508 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)
510 struct livebox_buffer_data *user_data;
512 struct livebox_buffer *handle;
515 struct event_cbdata *cbdata;
517 if (!filename || !width || !height) {
518 ErrPrint("Invalid argument: %p(%dx%d)\n", filename, width, height);
522 user_data = calloc(1, sizeof(*user_data));
524 ErrPrint("Heap: %s\n", strerror(errno));
528 user_data->is_pd = is_pd;
529 user_data->handler = handler ? handler : default_event_handler;
530 user_data->cbdata = data;
532 uri_len = strlen(filename) + strlen(FILE_SCHEMA) + 1;
533 uri = malloc(uri_len);
535 ErrPrint("Heap: %s\n", strerror(errno));
540 snprintf(uri, uri_len, FILE_SCHEMA "%s", filename);
541 pkgname = livebox_find_pkgname(uri);
543 ErrPrint("Invalid Request\n");
549 handle = provider_buffer_acquire((!!is_pd) ? TYPE_PD : TYPE_LB, pkgname, uri, width, height, sizeof(int), event_handler_wrapper, user_data);
550 DbgPrint("Acquire buffer for PD(%s), %s, %p\n", pkgname, uri, handle);
557 (void)provider_buffer_set_user_data(handle, user_data);
561 PUBLIC int livebox_request_update(const char *filename)
568 ErrPrint("Invalid argument\n");
569 return LB_STATUS_ERROR_INVALID;
572 uri_len = strlen(filename) + strlen(FILE_SCHEMA) + 1;
573 uri = malloc(uri_len);
575 ErrPrint("Heap: %s\n", strerror(errno));
576 return LB_STATUS_ERROR_MEMORY;
579 snprintf(uri, uri_len, FILE_SCHEMA "%s", filename);
580 ret = livebox_request_update_by_id(uri);
585 PUBLIC unsigned long livebox_pixmap_id(struct livebox_buffer *handle)
587 return provider_buffer_pixmap_id(handle);
590 PUBLIC int livebox_release_buffer(struct livebox_buffer *handle)
592 struct livebox_buffer_data *user_data;
595 return LB_STATUS_ERROR_INVALID;
597 user_data = provider_buffer_user_data(handle);
600 provider_buffer_set_user_data(handle, NULL);
603 DbgPrint("Release buffer\n");
604 return provider_buffer_release(handle);
607 PUBLIC void *livebox_ref_buffer(struct livebox_buffer *handle)
609 struct livebox_buffer_data *user_data;
617 user_data = provider_buffer_user_data(handle);
621 if (user_data->accelerated) {
622 DbgPrint("H/W accelerated buffer is allocated\n");
626 ret = provider_buffer_get_size(handle, &w, &h, &size);
628 data = provider_buffer_ref(handle);
629 if (data && !ret && w > 0 && h > 0 && size > 0) {
630 memset(data, 0, w * h * size);
631 provider_buffer_sync(handle);
634 DbgPrint("Ref buffer %ds%d(%d)\n", w, h, size);
638 PUBLIC int livebox_unref_buffer(void *buffer)
641 return LB_STATUS_ERROR_INVALID;
643 DbgPrint("Unref buffer\n");
644 return provider_buffer_unref(buffer);
647 PUBLIC int livebox_sync_buffer(struct livebox_buffer *handle)
649 struct livebox_buffer_data *user_data;
654 return LB_STATUS_ERROR_INVALID;
656 user_data = provider_buffer_user_data(handle);
658 ErrPrint("Invalid buffer\n");
659 return LB_STATUS_ERROR_INVALID;
662 if (user_data->accelerated) {
663 DbgPrint("H/W Buffer allocated. skip the sync buffer\n");
664 return LB_STATUS_SUCCESS;
667 pkgname = provider_buffer_pkgname(handle);
669 ErrPrint("Invalid buffer handler\n");
670 return LB_STATUS_ERROR_INVALID;
673 id = provider_buffer_id(handle);
675 ErrPrint("Invalid buffer handler\n");
676 return LB_STATUS_ERROR_INVALID;
679 provider_buffer_sync(handle);
681 if (user_data->is_pd) {
682 if (provider_send_desc_updated(pkgname, id, NULL) < 0)
683 ErrPrint("Failed to send PD updated (%s)\n", id);
689 if (provider_buffer_get_size(handle, &w, &h, &pixel_size) < 0)
690 ErrPrint("Failed to get size (%s)\n", id);
692 if (provider_send_updated(pkgname, id, w, h, -1.0f, NULL, NULL) < 0)
693 ErrPrint("Failed to send updated (%s)\n", id);
696 return LB_STATUS_SUCCESS;
699 PUBLIC int livebox_support_hw_buffer(struct livebox_buffer *handle)
702 return LB_STATUS_ERROR_INVALID;
704 return provider_buffer_pixmap_is_support_hw(handle);
707 PUBLIC int livebox_create_hw_buffer(struct livebox_buffer *handle)
709 struct livebox_buffer_data *user_data;
713 return LB_STATUS_ERROR_INVALID;
715 user_data = provider_buffer_user_data(handle);
717 return LB_STATUS_ERROR_INVALID;
719 if (user_data->accelerated)
722 ret = provider_buffer_pixmap_create_hw(handle);
723 user_data->accelerated = (ret == 0);
727 PUBLIC int livebox_destroy_hw_buffer(struct livebox_buffer *handle)
729 struct livebox_buffer_data *user_data;
731 return LB_STATUS_ERROR_INVALID;
733 user_data = provider_buffer_user_data(handle);
734 if (!user_data || !user_data->accelerated)
735 return LB_STATUS_ERROR_INVALID;
737 user_data->accelerated = 0;
739 return provider_buffer_pixmap_destroy_hw(handle);
742 PUBLIC void *livebox_buffer_hw_buffer(struct livebox_buffer *handle)
744 struct livebox_buffer_data *user_data;
749 user_data = provider_buffer_user_data(handle);
750 if (!user_data || !user_data->accelerated)
753 return provider_buffer_pixmap_hw_addr(handle);
756 PUBLIC int livebox_buffer_pre_render(struct livebox_buffer *handle)
758 struct livebox_buffer_data *user_data;
761 return LB_STATUS_ERROR_INVALID;
763 user_data = provider_buffer_user_data(handle);
765 return LB_STATUS_ERROR_INVALID;
767 if (!user_data->accelerated)
768 return LB_STATUS_SUCCESS;
772 * Do preprocessing for accessing the H/W render buffer
774 return provider_buffer_pre_render(handle);
777 PUBLIC int livebox_buffer_post_render(struct livebox_buffer *handle)
782 struct livebox_buffer_data *user_data;
785 return LB_STATUS_ERROR_INVALID;
787 user_data = provider_buffer_user_data(handle);
789 return LB_STATUS_ERROR_INVALID;
791 if (!user_data->accelerated)
792 return LB_STATUS_SUCCESS;
794 pkgname = provider_buffer_pkgname(handle);
796 ErrPrint("Invalid buffer handle\n");
797 return LB_STATUS_ERROR_INVALID;
800 id = provider_buffer_id(handle);
802 ErrPrint("Invalid buffer handler\n");
803 return LB_STATUS_ERROR_INVALID;
806 ret = provider_buffer_post_render(handle);
808 ErrPrint("Failed to post render processing\n");
812 if (user_data->is_pd == 1) {
813 if (provider_send_desc_updated(pkgname, id, NULL) < 0)
814 ErrPrint("Failed to send PD updated (%s)\n", id);
820 if (provider_buffer_get_size(handle, &w, &h, &pixel_size) < 0)
821 ErrPrint("Failed to get size (%s)\n", id);
823 if (provider_send_updated(pkgname, id, w, h, -1.0f, NULL, NULL) < 0)
824 ErrPrint("Failed to send updated (%s)\n", id);
827 return LB_STATUS_SUCCESS;