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 */
24 #include <livebox-service.h>
26 #include <provider_buffer.h>
33 #define EAPI __attribute__((visibility("default")))
35 #define FILE_SCHEMA "file://"
38 * \brief This function is defined by the data-provider-slave
40 extern const char *livebox_find_pkgname(const char *filename);
41 extern int livebox_request_update_by_id(const char *uri);
59 unsigned int last_idx;
61 struct dlist *block_list;
64 struct livebox_buffer_data {
69 EAPI const int DONE = 0x00;
70 EAPI const int OUTPUT_UPDATED = 0x02;
71 EAPI const int USE_NET = 0x04;
73 EAPI const int NEED_TO_SCHEDULE = 0x01;
74 EAPI const int NEED_TO_CREATE = 0x01;
75 EAPI const int NEED_TO_DESTROY = 0x01;
76 EAPI const int NEED_TO_UPDATE = 0x01;
78 EAPI const int LB_SYS_EVENT_FONT_CHANGED = 0x01;
79 EAPI const int LB_SYS_EVENT_LANG_CHANGED = 0x02;
80 EAPI const int LB_SYS_EVENT_TIME_CHANGED = 0x04;
81 EAPI const int LB_SYS_EVENT_REGION_CHANGED = 0x08;
82 EAPI const int LB_SYS_EVENT_PAUSED = 0x0100;
83 EAPI const int LB_SYS_EVENT_RESUMED = 0x0200;
85 EAPI struct livebox_desc *livebox_desc_open(const char *filename, int for_pd)
87 struct livebox_desc *handle;
90 handle = calloc(1, sizeof(*handle));
92 ErrPrint("Error: %s\n", strerror(errno));
98 len = strlen(filename) + strlen(".desc") + 1;
99 new_fname = malloc(len);
101 ErrPrint("Error: %s\n", strerror(errno));
105 snprintf(new_fname, len, "%s.desc", filename);
107 new_fname = strdup(filename);
109 ErrPrint("Error: %s\n", strerror(errno));
115 DbgPrint("Open a new file: %s\n", new_fname);
116 handle->fp = fopen(new_fname, "w+t");
119 ErrPrint("Failed to open a file: %s\n", strerror(errno));
127 EAPI int livebox_desc_close(struct livebox_desc *handle)
136 DbgPrint("Close and flush\n");
137 dlist_foreach_safe(handle->block_list, l, n, block) {
138 handle->block_list = dlist_remove(handle->block_list, l);
141 fprintf(handle->fp, "{\n");
143 fprintf(handle->fp, "type=%s\n", block->type);
144 DbgPrint("type=%s\n", block->type);
148 fprintf(handle->fp, "part=%s\n", block->part);
149 DbgPrint("part=%s\n", block->part);
153 fprintf(handle->fp, "data=%s\n", block->data);
154 DbgPrint("data=%s\n", block->data);
158 fprintf(handle->fp, "option=%s\n", block->option);
159 DbgPrint("option=%s\n", block->option);
163 fprintf(handle->fp, "id=%s\n", block->id);
164 DbgPrint("id=%s\n", block->id);
167 if (block->target_id) {
168 fprintf(handle->fp, "target=%s\n", block->target_id);
169 DbgPrint("target=%s\n", block->target_id);
172 fprintf(handle->fp, "}\n");
180 free(block->target_id);
189 EAPI int livebox_desc_set_category(struct livebox_desc *handle, const char *id, const char *category)
193 if (!handle || !category)
196 block = calloc(1, sizeof(*block));
200 block->type = strdup(LB_DESC_TYPE_INFO);
206 block->part = strdup("category");
213 block->data = strdup(category);
222 block->id = strdup(id);
232 block->idx = handle->last_idx++;
233 handle->block_list = dlist_append(handle->block_list, block);
237 EAPI int livebox_desc_set_size(struct livebox_desc *handle, const char *id, int w, int h)
245 block = calloc(1, sizeof(*block));
249 block->type = strdup(LB_DESC_TYPE_INFO);
255 block->part = strdup("size");
263 block->id = strdup(id);
272 snprintf(buffer, sizeof(buffer), "%dx%d", w, h);
273 block->data = strdup(buffer);
281 block->idx = handle->last_idx++;
282 handle->block_list = dlist_append(handle->block_list, block);
286 EAPI char *livebox_util_nl2br(const char *str)
300 ret = malloc(len + 1);
313 tmp = realloc(ret, len + 1);
340 EAPI int livebox_desc_set_id(struct livebox_desc *handle, int idx, const char *id)
345 dlist_foreach(handle->block_list, l, block) {
346 if (block->idx == idx) {
347 if (strcasecmp(block->type, LB_DESC_TYPE_SCRIPT)) {
348 ErrPrint("Invalid block is used\n");
352 free(block->target_id);
353 block->target_id = NULL;
355 if (!id || !strlen(id))
358 block->target_id = strdup(id);
359 if (!block->target_id) {
360 ErrPrint("Heap: %s\n", strerror(errno));
374 EAPI int livebox_desc_add_block(struct livebox_desc *handle, const char *id, const char *type, const char *part, const char *data, const char *option)
378 if (!handle || !type)
387 block = calloc(1, sizeof(*block));
391 block->type = strdup(type);
397 block->part = strdup(part);
404 block->data = strdup(data);
413 block->option = strdup(option);
414 if (!block->option) {
424 block->id = strdup(id);
435 block->idx = handle->last_idx++;
436 handle->block_list = dlist_append(handle->block_list, block);
440 EAPI int livebox_desc_del_block(struct livebox_desc *handle, int idx)
445 dlist_foreach(handle->block_list, l, block) {
446 if (block->idx == idx) {
447 handle->block_list = dlist_remove(handle->block_list, l);
453 free(block->target_id);
462 EAPI 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)
464 struct livebox_buffer_data *user_data;
466 struct livebox_buffer *handle;
470 if (!filename || !width || !height) {
471 ErrPrint("Invalid argument: %p(%dx%d)\n", filename, width, height);
475 user_data = calloc(1, sizeof(*user_data));
477 ErrPrint("Heap: %s\n", strerror(errno));
481 user_data->is_pd = is_pd;
483 uri_len = strlen(filename) + strlen(FILE_SCHEMA) + 1;
484 uri = malloc(uri_len);
486 ErrPrint("Heap: %s\n", strerror(errno));
491 snprintf(uri, uri_len, FILE_SCHEMA "%s", filename);
492 pkgname = livebox_find_pkgname(uri);
494 ErrPrint("Invalid Request\n");
500 handle = provider_buffer_acquire((!!is_pd) ? TYPE_PD : TYPE_LB, pkgname, uri, width, height, sizeof(int), handler, data);
501 DbgPrint("Acquire buffer for PD(%s), %s, %p\n", pkgname, uri, handle);
504 (void)provider_buffer_set_user_data(handle, user_data);
508 EAPI int livebox_request_update(const char *filename)
515 ErrPrint("Invalid argument\n");
519 uri_len = strlen(filename) + strlen(FILE_SCHEMA) + 1;
520 uri = malloc(uri_len);
522 ErrPrint("Heap: %s\n", strerror(errno));
526 snprintf(uri, uri_len, FILE_SCHEMA "%s", filename);
527 ret = livebox_request_update_by_id(uri);
532 EAPI unsigned long livebox_pixmap_id(struct livebox_buffer *handle)
534 return provider_buffer_pixmap_id(handle);
537 EAPI int livebox_release_buffer(struct livebox_buffer *handle)
539 struct livebox_buffer_data *user_data;
544 user_data = provider_buffer_user_data(handle);
547 provider_buffer_set_user_data(handle, NULL);
550 DbgPrint("Release buffer\n");
551 return provider_buffer_release(handle);
554 EAPI void *livebox_ref_buffer(struct livebox_buffer *handle)
556 struct livebox_buffer_data *user_data;
564 user_data = provider_buffer_user_data(handle);
568 if (user_data->accelerated) {
569 DbgPrint("H/W accelerated buffer is allocated\n");
573 ret = provider_buffer_get_size(handle, &w, &h, &size);
575 data = provider_buffer_ref(handle);
576 if (data && !ret && w > 0 && h > 0 && size > 0) {
577 memset(data, 0, w * h * size);
578 provider_buffer_sync(handle);
581 DbgPrint("Ref buffer %ds%d(%d)\n", w, h, size);
585 EAPI int livebox_unref_buffer(void *buffer)
590 DbgPrint("Unref buffer\n");
591 return provider_buffer_unref(buffer);
594 EAPI int livebox_sync_buffer(struct livebox_buffer *handle)
596 struct livebox_buffer_data *user_data;
603 user_data = provider_buffer_user_data(handle);
605 ErrPrint("Invalid buffer\n");
609 if (user_data->accelerated) {
610 DbgPrint("H/W Buffer allocated. skip the sync buffer\n");
614 pkgname = provider_buffer_pkgname(handle);
616 ErrPrint("Invalid buffer handler\n");
620 id = provider_buffer_id(handle);
622 ErrPrint("Invalid buffer handler\n");
626 DbgPrint("Sync buffer\n");
627 provider_buffer_sync(handle);
629 if (user_data->is_pd) {
630 if (provider_send_desc_updated(pkgname, id, NULL) < 0)
631 ErrPrint("Failed to send PD updated (%s)\n", id);
637 if (provider_buffer_get_size(handle, &w, &h, &pixel_size) < 0)
638 ErrPrint("Failed to get size (%s)\n", id);
640 if (provider_send_updated(pkgname, id, w, h, -1.0f, NULL, NULL) < 0)
641 ErrPrint("Failed to send updated (%s)\n", id);
646 EAPI int livebox_support_hw_buffer(struct livebox_buffer *handle)
651 return provider_buffer_pixmap_is_support_hw(handle);
654 EAPI int livebox_create_hw_buffer(struct livebox_buffer *handle)
656 struct livebox_buffer_data *user_data;
662 user_data = provider_buffer_user_data(handle);
666 if (user_data->accelerated)
669 ret = provider_buffer_pixmap_create_hw(handle);
670 user_data->accelerated = (ret == 0);
674 EAPI int livebox_destroy_hw_buffer(struct livebox_buffer *handle)
676 struct livebox_buffer_data *user_data;
680 user_data = provider_buffer_user_data(handle);
681 if (!user_data || !user_data->accelerated)
684 user_data->accelerated = 0;
686 return provider_buffer_pixmap_destroy_hw(handle);
689 EAPI void *livebox_buffer_hw_buffer(struct livebox_buffer *handle)
691 struct livebox_buffer_data *user_data;
696 user_data = provider_buffer_user_data(handle);
697 if (!user_data || !user_data->accelerated)
700 return provider_buffer_pixmap_hw_addr(handle);
703 EAPI int livebox_buffer_pre_render(struct livebox_buffer *handle)
705 struct livebox_buffer_data *user_data;
710 user_data = provider_buffer_user_data(handle);
714 if (!user_data->accelerated)
719 * Do preprocessing for accessing the H/W render buffer
721 return provider_buffer_pre_render(handle);
724 EAPI int livebox_buffer_post_render(struct livebox_buffer *handle)
729 struct livebox_buffer_data *user_data;
734 user_data = provider_buffer_user_data(handle);
738 if (!user_data->accelerated)
741 pkgname = provider_buffer_pkgname(handle);
743 ErrPrint("Invalid buffer handle\n");
747 id = provider_buffer_id(handle);
749 ErrPrint("Invalid buffer handler\n");
753 ret = provider_buffer_post_render(handle);
755 ErrPrint("Failed to post render processing\n");
759 if (user_data->is_pd == 1) {
760 if (provider_send_desc_updated(pkgname, id, NULL) < 0)
761 ErrPrint("Failed to send PD updated (%s)\n", id);
767 if (provider_buffer_get_size(handle, &w, &h, &pixel_size) < 0)
768 ErrPrint("Failed to get size (%s)\n", id);
770 if (provider_send_updated(pkgname, id, w, h, -1.0f, NULL, NULL) < 0)
771 ErrPrint("Failed to send updated (%s)\n", id);