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_PAUSED = 0x0100;
82 EAPI const int LB_SYS_EVENT_RESUMED = 0x0200;
84 EAPI struct livebox_desc *livebox_desc_open(const char *filename, int for_pd)
86 struct livebox_desc *handle;
89 handle = calloc(1, sizeof(*handle));
91 ErrPrint("Error: %s\n", strerror(errno));
97 len = strlen(filename) + strlen(".desc") + 1;
98 new_fname = malloc(len);
100 ErrPrint("Error: %s\n", strerror(errno));
104 snprintf(new_fname, len, "%s.desc", filename);
106 new_fname = strdup(filename);
108 ErrPrint("Error: %s\n", strerror(errno));
114 DbgPrint("Open a new file: %s\n", new_fname);
115 handle->fp = fopen(new_fname, "w+t");
118 ErrPrint("Failed to open a file: %s\n", strerror(errno));
126 EAPI int livebox_desc_close(struct livebox_desc *handle)
135 DbgPrint("Close and flush\n");
136 dlist_foreach_safe(handle->block_list, l, n, block) {
137 handle->block_list = dlist_remove(handle->block_list, l);
140 fprintf(handle->fp, "{\n");
142 fprintf(handle->fp, "type=%s\n", block->type);
143 DbgPrint("type=%s\n", block->type);
147 fprintf(handle->fp, "part=%s\n", block->part);
148 DbgPrint("part=%s\n", block->part);
152 fprintf(handle->fp, "data=%s\n", block->data);
153 DbgPrint("data=%s\n", block->data);
157 fprintf(handle->fp, "option=%s\n", block->option);
158 DbgPrint("option=%s\n", block->option);
162 fprintf(handle->fp, "id=%s\n", block->id);
163 DbgPrint("id=%s\n", block->id);
166 if (block->target_id) {
167 fprintf(handle->fp, "target=%s\n", block->target_id);
168 DbgPrint("target=%s\n", block->target_id);
171 fprintf(handle->fp, "}\n");
179 free(block->target_id);
188 EAPI int livebox_desc_set_category(struct livebox_desc *handle, const char *id, const char *category)
192 if (!handle || !category)
195 block = calloc(1, sizeof(*block));
199 block->type = strdup(LB_DESC_TYPE_INFO);
205 block->part = strdup("category");
212 block->data = strdup(category);
221 block->id = strdup(id);
231 block->idx = handle->last_idx++;
232 handle->block_list = dlist_append(handle->block_list, block);
236 EAPI int livebox_desc_set_size(struct livebox_desc *handle, const char *id, int w, int h)
244 block = calloc(1, sizeof(*block));
248 block->type = strdup(LB_DESC_TYPE_INFO);
254 block->part = strdup("size");
262 block->id = strdup(id);
271 snprintf(buffer, sizeof(buffer), "%dx%d", w, h);
272 block->data = strdup(buffer);
280 block->idx = handle->last_idx++;
281 handle->block_list = dlist_append(handle->block_list, block);
285 EAPI char *livebox_util_nl2br(const char *str)
299 ret = malloc(len + 1);
312 tmp = realloc(ret, len + 1);
339 EAPI int livebox_desc_set_id(struct livebox_desc *handle, int idx, const char *id)
344 dlist_foreach(handle->block_list, l, block) {
345 if (block->idx == idx) {
346 if (strcasecmp(block->type, LB_DESC_TYPE_SCRIPT)) {
347 ErrPrint("Invalid block is used\n");
351 free(block->target_id);
352 block->target_id = NULL;
354 if (!id || !strlen(id))
357 block->target_id = strdup(id);
358 if (!block->target_id) {
359 ErrPrint("Heap: %s\n", strerror(errno));
373 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)
377 if (!handle || !type)
386 block = calloc(1, sizeof(*block));
390 block->type = strdup(type);
396 block->part = strdup(part);
403 block->data = strdup(data);
412 block->option = strdup(option);
413 if (!block->option) {
423 block->id = strdup(id);
434 block->idx = handle->last_idx++;
435 handle->block_list = dlist_append(handle->block_list, block);
439 EAPI int livebox_desc_del_block(struct livebox_desc *handle, int idx)
444 dlist_foreach(handle->block_list, l, block) {
445 if (block->idx == idx) {
446 handle->block_list = dlist_remove(handle->block_list, l);
452 free(block->target_id);
461 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)
463 struct livebox_buffer_data *user_data;
465 struct livebox_buffer *handle;
469 if (!filename || !width || !height) {
470 ErrPrint("Invalid argument: %p(%dx%d)\n", filename, width, height);
474 user_data = calloc(1, sizeof(*user_data));
476 ErrPrint("Heap: %s\n", strerror(errno));
480 user_data->is_pd = is_pd;
482 uri_len = strlen(filename) + strlen(FILE_SCHEMA) + 1;
483 uri = malloc(uri_len);
485 ErrPrint("Heap: %s\n", strerror(errno));
490 snprintf(uri, uri_len, FILE_SCHEMA "%s", filename);
491 pkgname = livebox_find_pkgname(uri);
493 ErrPrint("Invalid Request\n");
499 handle = provider_buffer_acquire((!!is_pd) ? TYPE_PD : TYPE_LB, pkgname, uri, width, height, sizeof(int), handler, data);
500 DbgPrint("Acquire buffer for PD(%s), %s, %p\n", pkgname, uri, handle);
503 (void)provider_buffer_set_user_data(handle, user_data);
507 EAPI int livebox_request_update(const char *filename)
514 ErrPrint("Invalid argument\n");
518 uri_len = strlen(filename) + strlen(FILE_SCHEMA) + 1;
519 uri = malloc(uri_len);
521 ErrPrint("Heap: %s\n", strerror(errno));
525 snprintf(uri, uri_len, FILE_SCHEMA "%s", filename);
526 ret = livebox_request_update_by_id(uri);
531 EAPI unsigned long livebox_pixmap_id(struct livebox_buffer *handle)
533 return provider_buffer_pixmap_id(handle);
536 EAPI int livebox_release_buffer(struct livebox_buffer *handle)
538 struct livebox_buffer_data *user_data;
543 user_data = provider_buffer_user_data(handle);
546 provider_buffer_set_user_data(handle, NULL);
549 DbgPrint("Release buffer\n");
550 return provider_buffer_release(handle);
553 EAPI void *livebox_ref_buffer(struct livebox_buffer *handle)
555 struct livebox_buffer_data *user_data;
563 user_data = provider_buffer_user_data(handle);
567 if (user_data->accelerated) {
568 DbgPrint("H/W accelerated buffer is allocated\n");
572 ret = provider_buffer_get_size(handle, &w, &h, &size);
574 data = provider_buffer_ref(handle);
575 if (data && !ret && w > 0 && h > 0 && size > 0) {
576 memset(data, 0, w * h * size);
577 provider_buffer_sync(handle);
580 DbgPrint("Ref buffer %ds%d(%d)\n", w, h, size);
584 EAPI int livebox_unref_buffer(void *buffer)
589 DbgPrint("Unref buffer\n");
590 return provider_buffer_unref(buffer);
593 EAPI int livebox_sync_buffer(struct livebox_buffer *handle)
595 struct livebox_buffer_data *user_data;
602 user_data = provider_buffer_user_data(handle);
604 ErrPrint("Invalid buffer\n");
608 if (user_data->accelerated) {
609 DbgPrint("H/W Buffer allocated. skip the sync buffer\n");
613 pkgname = provider_buffer_pkgname(handle);
615 ErrPrint("Invalid buffer handler\n");
619 id = provider_buffer_id(handle);
621 ErrPrint("Invalid buffer handler\n");
625 DbgPrint("Sync buffer\n");
626 provider_buffer_sync(handle);
628 if (user_data->is_pd) {
629 if (provider_send_desc_updated(pkgname, id, NULL) < 0)
630 ErrPrint("Failed to send PD updated (%s)\n", id);
636 if (provider_buffer_get_size(handle, &w, &h, &pixel_size) < 0)
637 ErrPrint("Failed to get size (%s)\n", id);
639 if (provider_send_updated(pkgname, id, w, h, -1.0f, NULL, NULL) < 0)
640 ErrPrint("Failed to send updated (%s)\n", id);
645 EAPI int livebox_support_hw_buffer(struct livebox_buffer *handle)
650 return provider_buffer_pixmap_is_support_hw(handle);
653 EAPI int livebox_create_hw_buffer(struct livebox_buffer *handle)
655 struct livebox_buffer_data *user_data;
661 user_data = provider_buffer_user_data(handle);
665 if (user_data->accelerated)
668 ret = provider_buffer_pixmap_create_hw(handle);
669 user_data->accelerated = (ret == 0);
673 EAPI int livebox_destroy_hw_buffer(struct livebox_buffer *handle)
675 struct livebox_buffer_data *user_data;
679 user_data = provider_buffer_user_data(handle);
680 if (!user_data || !user_data->accelerated)
683 user_data->accelerated = 0;
685 return provider_buffer_pixmap_destroy_hw(handle);
688 EAPI void *livebox_buffer_hw_buffer(struct livebox_buffer *handle)
690 struct livebox_buffer_data *user_data;
695 user_data = provider_buffer_user_data(handle);
696 if (!user_data || !user_data->accelerated)
699 return provider_buffer_pixmap_hw_addr(handle);
702 EAPI int livebox_buffer_pre_render(struct livebox_buffer *handle)
704 struct livebox_buffer_data *user_data;
709 user_data = provider_buffer_user_data(handle);
713 if (!user_data->accelerated)
718 * Do preprocessing for accessing the H/W render buffer
720 return provider_buffer_pre_render(handle);
723 EAPI int livebox_buffer_post_render(struct livebox_buffer *handle)
728 struct livebox_buffer_data *user_data;
733 user_data = provider_buffer_user_data(handle);
737 if (!user_data->accelerated)
740 pkgname = provider_buffer_pkgname(handle);
742 ErrPrint("Invalid buffer handle\n");
746 id = provider_buffer_id(handle);
748 ErrPrint("Invalid buffer handler\n");
752 ret = provider_buffer_post_render(handle);
754 ErrPrint("Failed to post render processing\n");
758 if (user_data->is_pd == 1) {
759 if (provider_send_desc_updated(pkgname, id, NULL) < 0)
760 ErrPrint("Failed to send PD updated (%s)\n", id);
766 if (provider_buffer_get_size(handle, &w, &h, &pixel_size) < 0)
767 ErrPrint("Failed to get size (%s)\n", id);
769 if (provider_send_updated(pkgname, id, w, h, -1.0f, NULL, NULL) < 0)
770 ErrPrint("Failed to send updated (%s)\n", id);