2 * Copyright 2013 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 */
26 #include <com-core_packet.h>
28 #include <livebox-service.h>
29 #include <livebox-errno.h>
34 #include "livebox_internal.h"
37 #include "master_rpc.h"
39 #include "critical_log.h"
41 #define EAPI __attribute__((visibility("default")))
42 #define MINIMUM_EVENT s_info.event_filter
49 struct dlist *livebox_list;
50 struct dlist *event_list;
51 struct dlist *fault_list;
53 int prevent_overwrite;
60 .prevent_overwrite = 0,
61 .event_filter = 0.02f,
70 int (*handler)(struct livebox *handler, enum livebox_event_type event, void *data);
75 int (*handler)(enum livebox_fault_type event, const char *pkgname, const char *filename, const char *func, void *data);
79 static inline void default_create_cb(struct livebox *handler, int ret, void *data)
81 DbgPrint("Default created event handler: %d\n", ret);
84 static inline void default_delete_cb(struct livebox *handler, int ret, void *data)
86 DbgPrint("Default deleted event handler: %d\n", ret);
89 static inline void default_pinup_cb(struct livebox *handler, int ret, void *data)
91 DbgPrint("Default pinup event handler: %d\n", ret);
94 static inline void default_group_changed_cb(struct livebox *handler, int ret, void *data)
96 DbgPrint("Default group changed event handler: %d\n", ret);
99 static inline void default_period_changed_cb(struct livebox *handler, int ret, void *data)
101 DbgPrint("Default period changed event handler: %d\n", ret);
104 static inline void default_pd_created_cb(struct livebox *handler, int ret, void *data)
106 DbgPrint("Default PD created event handler: %d\n", ret);
109 static inline void default_pd_destroyed_cb(struct livebox *handler, int ret, void *data)
111 DbgPrint("Default PD destroyed event handler: %d\n", ret);
114 static inline void default_lb_size_changed_cb(struct livebox *handler, int ret, void *data)
116 DbgPrint("Default LB size changed event handler: %d\n", ret);
119 static inline __attribute__((always_inline)) struct cb_info *create_cb_info(ret_cb_t cb, void *data)
121 struct cb_info *info;
123 info = malloc(sizeof(*info));
125 CRITICAL_LOG("Heap: %s\n", strerror(errno));
134 static inline void destroy_cb_info(struct cb_info *info)
139 static void resize_cb(struct livebox *handler, const struct packet *result, void *data)
142 struct cb_info *info = data;
148 destroy_cb_info(info);
151 ret = LB_STATUS_ERROR_FAULT;
152 } else if (packet_get(result, "i", &ret) != 1) {
153 ErrPrint("Invalid argument\n");
154 ret = LB_STATUS_ERROR_INVALID;
159 * In case of resize request,
160 * The livebox handler will not have resized value right after this callback,
161 * It can only get the new size when it makes updates.
163 * So the user can only get the resized value(result) from the first update event
164 * after this request.
167 DbgPrint("Resize request is done, prepare the size changed event\n");
168 handler->size_changed_cb = cb;
169 handler->size_cbdata = cbdata;
171 DbgPrint("Resize request is failed: %d\n", ret);
172 cb(handler, ret, cbdata);
176 static void text_signal_cb(struct livebox *handler, const struct packet *result, void *data)
180 struct cb_info *info = data;
185 destroy_cb_info(info);
188 ret = LB_STATUS_ERROR_FAULT;
189 } else if (packet_get(result, "i", &ret) != 1) {
190 ErrPrint("Invalid argument\n");
191 ret = LB_STATUS_ERROR_INVALID;
195 cb(handler, ret, cbdata);
199 static void set_group_ret_cb(struct livebox *handler, const struct packet *result, void *data)
204 struct cb_info *info = data;
208 destroy_cb_info(info);
211 ret = LB_STATUS_ERROR_FAULT;
212 } else if (packet_get(result, "i", &ret) != 1) {
213 ErrPrint("Invalid argument\n");
214 ret = LB_STATUS_ERROR_INVALID;
217 if (ret == 0) { /*!< Group information is successfully changed */
218 handler->group_changed_cb = cb;
219 handler->group_cbdata = cbdata;
221 cb(handler, ret, cbdata);
227 static void period_ret_cb(struct livebox *handler, const struct packet *result, void *data)
229 struct cb_info *info = data;
236 destroy_cb_info(info);
239 ret = LB_STATUS_ERROR_FAULT;
240 } else if (packet_get(result, "i", &ret) != 1) {
241 ErrPrint("Invalid argument\n");
242 ret = LB_STATUS_ERROR_INVALID;
246 handler->period_changed_cb = cb;
247 handler->period_cbdata = cbdata;
249 cb(handler, ret, cbdata);
253 static void del_ret_cb(struct livebox *handler, const struct packet *result, void *data)
255 struct cb_info *info = data;
262 destroy_cb_info(info);
265 ErrPrint("Connection lost?\n");
266 ret = LB_STATUS_ERROR_FAULT;
267 } else if (packet_get(result, "i", &ret) != 1) {
268 ErrPrint("Invalid argument\n");
269 ret = LB_STATUS_ERROR_INVALID;
273 DbgPrint("Returns %d (waiting deleted event)\n", ret);
274 handler->deleted_cb = cb;
275 handler->deleted_cbdata = cbdata;
277 cb(handler, ret, cbdata);
282 * Do not call the deleted callback from here.
283 * master will send the "deleted" event.
284 * Then invoke this callback.
286 * if (handler->deleted_cb)
287 * handler->deleted_cb(handler, ret, handler->deleted_cbdata);
291 static void new_ret_cb(struct livebox *handler, const struct packet *result, void *data)
294 struct cb_info *info = data;
300 destroy_cb_info(info);
303 ret = LB_STATUS_ERROR_FAULT;
304 } else if (packet_get(result, "i", &ret) != 1) {
305 ret = LB_STATUS_ERROR_INVALID;
309 DbgPrint("new request is sent, just waiting the created event\n");
310 handler->created_cb = cb;
311 handler->created_cbdata = cbdata;
315 * Don't go anymore ;)
321 * It means the current instance is not created,
322 * so user has to know about this.
323 * notice it to user using "deleted" event.
325 cb(handler, ret, cbdata);
331 static void pd_create_cb(struct livebox *handler, const struct packet *result, void *data)
333 struct cb_info *info = data;
340 destroy_cb_info(data);
343 ret = LB_STATUS_ERROR_FAULT;
344 } else if (packet_get(result, "i", &ret) != 1) {
345 ret = LB_STATUS_ERROR_INVALID;
349 DbgPrint("PD Created event handler prepared\n");
350 handler->pd_created_cb = cb;
351 handler->pd_created_cbdata = cbdata;
353 DbgPrint("Failed to create a PD\n");
354 cb(handler, ret, cbdata);
358 static void activated_cb(struct livebox *handler, const struct packet *result, void *data)
361 struct cb_info *info = data;
364 const char *pkgname = "";
368 destroy_cb_info(info);
371 ret = LB_STATUS_ERROR_FAULT;
372 } else if (packet_get(result, "is", &ret, &pkgname) != 2) {
373 ret = LB_STATUS_ERROR_INVALID;
377 cb(handler, ret, cbdata);
380 static void pd_destroy_cb(struct livebox *handler, const struct packet *result, void *data)
385 struct cb_info *info = data;
389 destroy_cb_info(info);
392 DbgPrint("Result is NIL (may connection lost)\n");
393 ret = LB_STATUS_ERROR_FAULT;
394 } else if (packet_get(result, "i", &ret) != 1) {
395 DbgPrint("Invalid parameter\n");
396 ret = LB_STATUS_ERROR_INVALID;
400 DbgPrint("PD Destroyed callback prepared\n");
401 handler->pd_destroyed_cb = cb;
402 handler->pd_destroyed_cbdata = cbdata;
404 DbgPrint("PD is not desroyed (forcely reset, pd flag)\n");
405 handler->is_pd_created = 0;
406 cb(handler, ret, cbdata);
410 static void delete_cluster_cb(struct livebox *handler, const struct packet *result, void *data)
412 struct cb_info *info = data;
419 destroy_cb_info(info);
422 ret = LB_STATUS_ERROR_FAULT;
423 } else if (packet_get(result, "i", &ret) != 1) {
424 ret = LB_STATUS_ERROR_INVALID;
427 DbgPrint("Delete category returns: %d\n", ret);
430 cb(handler, ret, cbdata);
433 static void delete_category_cb(struct livebox *handler, const struct packet *result, void *data)
435 struct cb_info *info = data;
442 destroy_cb_info(info);
445 ret = LB_STATUS_ERROR_FAULT;
446 else if (packet_get(result, "i", &ret) != 1)
447 ret = LB_STATUS_ERROR_INVALID;
449 DbgPrint("Delete category returns: %d\n", ret);
452 cb(handler, ret, cbdata);
455 static void pixmap_acquired_cb(struct livebox *handler, const struct packet *result, void *data)
460 struct cb_info *info = data;
464 destroy_cb_info(info);
467 ret = 0; /* PIXMAP 0 means error */
468 else if (packet_get(result, "i", &ret) != 1)
472 cb(handler, ret, cbdata);
475 static void pinup_done_cb(struct livebox *handler, const struct packet *result, void *data)
480 struct cb_info *info = data;
484 destroy_cb_info(info);
487 ret = LB_STATUS_ERROR_FAULT;
488 else if (packet_get(result, "i", &ret) != 1)
489 ret = LB_STATUS_ERROR_INVALID;
492 handler->pinup_cb = cb;
493 handler->pinup_cbdata = cbdata;
495 cb(handler, ret, cbdata);
499 static int send_mouse_event(struct livebox *handler, const char *event, int x, int y)
501 struct packet *packet;
504 timestamp = util_timestamp();
505 packet = packet_create_noack(event, "ssdii", handler->pkgname, handler->id, timestamp, x, y);
507 ErrPrint("Failed to build param\n");
508 return LB_STATUS_ERROR_FAULT;
511 DbgPrint("Send: %dx%d\n", x, y);
513 return master_rpc_request_only(handler, packet);
516 EAPI int livebox_init(void *disp)
520 if (s_info.init_count > 0) {
522 return LB_STATUS_SUCCESS;
524 env = getenv("PROVIDER_DISABLE_PREVENT_OVERWRITE");
525 if (env && !strcasecmp(env, "true"))
526 s_info.prevent_overwrite = 1;
528 env = getenv("PROVIDER_EVENT_FILTER");
530 sscanf(env, "%lf", &MINIMUM_EVENT);
533 char filename[BUFSIZ];
534 snprintf(filename, sizeof(filename), "/tmp/%d.box.log", getpid());
535 __file_log_fp = fopen(filename, "w+t");
537 __file_log_fp = fdopen(1, "w+t");
539 critical_log_init("viewer");
540 livebox_service_init();
546 return LB_STATUS_SUCCESS;
549 EAPI int livebox_fini(void)
551 if (s_info.init_count <= 0) {
552 DbgPrint("Didn't initialized\n");
553 return LB_STATUS_ERROR_INVALID;
557 if (s_info.init_count > 0) {
558 DbgPrint("init count : %d\n", s_info.init_count);
559 return LB_STATUS_SUCCESS;
564 livebox_service_fini();
566 return LB_STATUS_SUCCESS;
569 static inline char *lb_pkgname(const char *pkgname)
573 lb = livebox_service_pkgname(pkgname);
575 if (util_validate_livebox_package(pkgname) == 0)
576 return strdup(pkgname);
583 * Just wrapping the livebox_add_with_size function.
585 EAPI struct livebox *livebox_add(const char *pkgname, const char *content, const char *cluster, const char *category, double period, ret_cb_t cb, void *data)
587 return livebox_add_with_size(pkgname, content, cluster, category, period, LB_SIZE_TYPE_UNKNOWN, cb, data);
590 EAPI struct livebox *livebox_add_with_size(const char *pkgname, const char *content, const char *cluster, const char *category, double period, int type, ret_cb_t cb, void *data)
592 struct livebox *handler;
593 struct packet *packet;
598 if (!pkgname || !cluster || !category) {
599 ErrPrint("Invalid arguments: pkgname[%p], cluster[%p], category[%p]\n",
600 pkgname, cluster, category);
604 if (type != LB_SIZE_TYPE_UNKNOWN)
605 livebox_service_get_size(type, &width, &height);
607 handler = calloc(1, sizeof(*handler));
609 ErrPrint("Error: %s\n", strerror(errno));
613 handler->pkgname = lb_pkgname(pkgname);
614 if (!handler->pkgname) {
619 if (livebox_service_is_enabled(handler->pkgname) == 0) {
620 DbgPrint("Livebox [%s](%s) is disabled package\n", handler->pkgname, pkgname);
621 free(handler->pkgname);
627 handler->content = strdup(content);
628 if (!handler->content) {
629 ErrPrint("Error: %s\n", strerror(errno));
630 free(handler->pkgname);
635 handler->content = livebox_service_content(handler->pkgname);
638 handler->cluster = strdup(cluster);
639 if (!handler->cluster) {
640 ErrPrint("Error: %s\n", strerror(errno));
641 free(handler->content);
642 free(handler->pkgname);
647 handler->category = strdup(category);
648 if (!handler->category) {
649 ErrPrint("Error: %s\n", strerror(errno));
650 free(handler->cluster);
651 free(handler->content);
652 free(handler->pkgname);
658 cb = default_create_cb;
660 /* Data provider will set this */
661 handler->lb.type = _LB_TYPE_FILE;
662 handler->pd.type = _PD_TYPE_SCRIPT;
663 handler->lb.period = period;
665 /* Used for handling the mouse event on a box */
666 handler->lb.mouse_event = livebox_service_mouse_event(handler->pkgname);
668 /* Cluster infomration is not determined yet */
669 handler->nr_of_sizes = 0x01;
671 handler->timestamp = util_timestamp();
672 handler->is_user = 1;
673 handler->visible = LB_SHOW;
675 s_info.livebox_list = dlist_append(s_info.livebox_list, handler);
677 packet = packet_create("new", "dssssdii", handler->timestamp, handler->pkgname, handler->content, cluster, category, period, width, height);
679 ErrPrint("Failed to create a new packet\n");
680 free(handler->category);
681 free(handler->cluster);
682 free(handler->content);
683 free(handler->pkgname);
688 ret = master_rpc_async_request(handler, packet, 0, new_ret_cb, create_cb_info(cb, data));
690 ErrPrint("Failed to send a new packet\n");
691 free(handler->category);
692 free(handler->cluster);
693 free(handler->content);
694 free(handler->pkgname);
699 DbgPrint("Successfully sent a new request ([%lf] %s)\n", handler->timestamp, handler->pkgname);
700 handler->state = CREATE;
701 return lb_ref(handler);
704 EAPI double livebox_period(struct livebox *handler)
706 if (!handler || handler->state != CREATE || !handler->id) {
707 ErrPrint("Handler is not valid\n");
711 return handler->lb.period;
714 EAPI int livebox_set_period(struct livebox *handler, double period, ret_cb_t cb, void *data)
716 struct packet *packet;
718 if (!handler || handler->state != CREATE || !handler->id) {
719 ErrPrint("Handler is not valid\n");
720 return LB_STATUS_ERROR_INVALID;
723 if (handler->period_changed_cb) {
724 ErrPrint("Previous request for changing period is not finished\n");
725 return LB_STATUS_ERROR_BUSY;
728 if (!handler->is_user) {
729 ErrPrint("CA Livebox is not able to change the period\n");
730 return LB_STATUS_ERROR_PERMISSION;
733 if (handler->lb.period == period) {
734 DbgPrint("No changes\n");
735 return LB_STATUS_ERROR_ALREADY;
738 packet = packet_create("set_period", "ssd", handler->pkgname, handler->id, period);
740 ErrPrint("Failed to build a packet %s\n", handler->pkgname);
741 return LB_STATUS_ERROR_FAULT;
745 cb = default_period_changed_cb;
747 return master_rpc_async_request(handler, packet, 0, period_ret_cb, create_cb_info(cb, data));
750 EAPI int livebox_del(struct livebox *handler, ret_cb_t cb, void *data)
753 ErrPrint("Handler is NIL\n");
754 return LB_STATUS_ERROR_INVALID;
757 if (handler->state != CREATE) {
758 ErrPrint("Handler is already deleted\n");
759 return LB_STATUS_ERROR_INVALID;
762 handler->state = DELETE;
767 * The id is not determined yet.
768 * It means a user didn't receive created event yet.
769 * Then just stop to delete procedure from here.
770 * Because the "created" event handler will release this.
771 * By the way, if the user adds any callback for getting return status of this,
775 cb(handler, 0, data);
776 return LB_STATUS_SUCCESS;
780 cb = default_delete_cb;
782 return lb_send_delete(handler, cb, data);
785 EAPI int livebox_set_fault_handler(int (*cb)(enum livebox_fault_type, const char *, const char *, const char *, void *), void *data)
787 struct fault_info *info;
790 return LB_STATUS_ERROR_INVALID;
792 info = malloc(sizeof(*info));
794 CRITICAL_LOG("Heap: %s\n", strerror(errno));
795 return LB_STATUS_ERROR_MEMORY;
799 info->user_data = data;
801 s_info.fault_list = dlist_append(s_info.fault_list, info);
802 return LB_STATUS_SUCCESS;
805 EAPI void *livebox_unset_fault_handler(int (*cb)(enum livebox_fault_type, const char *, const char *, const char *, void *))
807 struct fault_info *info;
810 dlist_foreach(s_info.fault_list, l, info) {
811 if (info->handler == cb) {
813 s_info.fault_list = dlist_remove(s_info.fault_list, l);
814 data = info->user_data;
824 EAPI int livebox_set_event_handler(int (*cb)(struct livebox *, enum livebox_event_type, void *), void *data)
826 struct event_info *info;
829 ErrPrint("Invalid argument cb is nil\n");
830 return LB_STATUS_ERROR_INVALID;
833 info = malloc(sizeof(*info));
835 CRITICAL_LOG("Heap: %s\n", strerror(errno));
836 return LB_STATUS_ERROR_MEMORY;
840 info->user_data = data;
842 s_info.event_list = dlist_append(s_info.event_list, info);
843 return LB_STATUS_SUCCESS;
846 EAPI void *livebox_unset_event_handler(int (*cb)(struct livebox *, enum livebox_event_type, void *))
848 struct event_info *info;
851 dlist_foreach(s_info.event_list, l, info) {
852 if (info->handler == cb) {
855 s_info.event_list = dlist_remove(s_info.event_list, l);
856 data = info->user_data;
866 EAPI int livebox_resize(struct livebox *handler, int type, ret_cb_t cb, void *data)
868 struct packet *packet;
873 ErrPrint("Handler is NIL\n");
874 return LB_STATUS_ERROR_INVALID;
877 if (handler->state != CREATE || !handler->id) {
878 ErrPrint("Handler is not valid\n");
879 return LB_STATUS_ERROR_INVALID;
882 if (handler->size_changed_cb) {
883 ErrPrint("Previous resize request is not finished yet\n");
884 return LB_STATUS_ERROR_BUSY;
887 if (!handler->is_user) {
888 ErrPrint("CA Livebox is not able to be resized\n");
889 return LB_STATUS_ERROR_PERMISSION;
892 if (livebox_service_get_size(type, &w, &h) != 0) {
893 ErrPrint("Invalid size type\n");
894 return LB_STATUS_ERROR_INVALID;
897 if (handler->lb.width == w && handler->lb.height == h) {
898 DbgPrint("No changes\n");
899 return LB_STATUS_ERROR_ALREADY;;
902 packet = packet_create("resize", "ssii", handler->pkgname, handler->id, w, h);
904 ErrPrint("Failed to build param\n");
905 return LB_STATUS_ERROR_FAULT;
909 cb = default_lb_size_changed_cb;
911 return master_rpc_async_request(handler, packet, 0, resize_cb, create_cb_info(cb, data));
914 EAPI int livebox_click(struct livebox *handler, double x, double y)
916 struct packet *packet;
921 ErrPrint("Handler is NIL\n");
922 return LB_STATUS_ERROR_INVALID;
925 if (handler->state != CREATE || !handler->id) {
926 ErrPrint("Handler is not valid\n");
927 return LB_STATUS_ERROR_INVALID;
930 if (handler->lb.auto_launch)
931 if (aul_open_app(handler->lb.auto_launch) < 0)
932 ErrPrint("Failed to launch app %s\n", handler->lb.auto_launch);
934 timestamp = util_timestamp();
935 packet = packet_create_noack("clicked", "sssddd", handler->pkgname, handler->id, "clicked", timestamp, x, y);
937 ErrPrint("Failed to build param\n");
938 return LB_STATUS_ERROR_FAULT;
941 ret = master_rpc_request_only(handler, packet);
942 DbgPrint("Click request: %d\n", ret);
944 if (!handler->lb.mouse_event && (handler->lb.type == _LB_TYPE_BUFFER || handler->lb.type == _LB_TYPE_SCRIPT)) {
945 int ret; /* Shadow variable */
946 ret = send_mouse_event(handler, "lb_mouse_down", x * handler->lb.width, y * handler->lb.height);
948 DbgPrint("Failed to send Down: %d\n", ret);
950 ret = send_mouse_event(handler, "lb_mouse_move", x * handler->lb.width, y * handler->lb.height);
952 DbgPrint("Failed to send Move: %d\n", ret);
954 ret = send_mouse_event(handler, "lb_mouse_up", x * handler->lb.width, y * handler->lb.height);
956 DbgPrint("Failed to send Up: %d\n", ret);
962 EAPI int livebox_has_pd(struct livebox *handler)
965 ErrPrint("Handler is NIL\n");
966 return LB_STATUS_ERROR_INVALID;
969 if (handler->state != CREATE || !handler->id) {
970 ErrPrint("Handler is not valid\n");
971 return LB_STATUS_ERROR_INVALID;
974 return !!handler->pd.data.fb;
977 EAPI int livebox_pd_is_created(struct livebox *handler)
980 ErrPrint("Handler is NIL\n");
981 return LB_STATUS_ERROR_INVALID;
984 if (!handler->pd.data.fb || handler->state != CREATE || !handler->id) {
985 ErrPrint("Handler is not valid\n");
986 return LB_STATUS_ERROR_INVALID;
989 return handler->is_pd_created;
992 EAPI int livebox_create_pd(struct livebox *handler, ret_cb_t cb, void *data)
994 return livebox_create_pd_with_position(handler, -1.0, -1.0, cb, data);
997 EAPI int livebox_create_pd_with_position(struct livebox *handler, double x, double y, ret_cb_t cb, void *data)
999 struct packet *packet;
1002 ErrPrint("Handler is NIL\n");
1003 return LB_STATUS_ERROR_INVALID;
1006 if (!handler->pd.data.fb || handler->state != CREATE || !handler->id) {
1007 ErrPrint("Handler is not valid\n");
1008 return LB_STATUS_ERROR_INVALID;
1011 if (handler->is_pd_created == 1) {
1012 DbgPrint("PD already created\n");
1013 return LB_STATUS_SUCCESS;
1016 packet = packet_create("create_pd", "ssdd", handler->pkgname, handler->id, x, y);
1018 ErrPrint("Failed to build param\n");
1019 return LB_STATUS_ERROR_FAULT;
1023 handler->pd_created_cb = default_pd_created_cb;
1025 return master_rpc_async_request(handler, packet, 0, pd_create_cb, create_cb_info(cb, data));
1028 EAPI int livebox_move_pd(struct livebox *handler, double x, double y)
1030 struct packet *packet;
1033 ErrPrint("Handler is NIL\n");
1034 return LB_STATUS_ERROR_INVALID;
1037 if (!handler->pd.data.fb || handler->state != CREATE || !handler->id) {
1038 ErrPrint("Handler is not valid\n");
1039 return LB_STATUS_ERROR_INVALID;
1042 if (!handler->is_pd_created) {
1043 DbgPrint("PD is not created\n");
1044 return LB_STATUS_ERROR_INVALID;
1047 packet = packet_create_noack("pd_move", "ssdd", handler->pkgname, handler->id, x, y);
1049 ErrPrint("Failed to build param\n");
1050 return LB_STATUS_ERROR_FAULT;
1053 return master_rpc_request_only(handler, packet);
1056 EAPI int livebox_activate(const char *pkgname, ret_cb_t cb, void *data)
1058 struct packet *packet;
1061 return LB_STATUS_ERROR_INVALID;
1063 packet = packet_create("activate_package", "s", pkgname);
1065 ErrPrint("Failed to build a param\n");
1066 return LB_STATUS_ERROR_FAULT;
1069 return master_rpc_async_request(NULL, packet, 0, activated_cb, create_cb_info(cb, data));
1072 EAPI int livebox_destroy_pd(struct livebox *handler, ret_cb_t cb, void *data)
1074 struct packet *packet;
1077 ErrPrint("Handler is NIL\n");
1078 return LB_STATUS_ERROR_INVALID;
1081 if (!handler->pd.data.fb || handler->state != CREATE || !handler->id) {
1082 ErrPrint("Handler is not valid\n");
1083 return LB_STATUS_ERROR_INVALID;
1086 if (!handler->is_pd_created) {
1087 ErrPrint("PD is not created\n");
1088 return LB_STATUS_ERROR_INVALID;
1091 packet = packet_create("destroy_pd", "ss", handler->pkgname, handler->id);
1093 ErrPrint("Failed to build a param\n");
1094 return LB_STATUS_ERROR_FAULT;
1098 cb = default_pd_destroyed_cb;
1100 return master_rpc_async_request(handler, packet, 0, pd_destroy_cb, create_cb_info(cb, data));
1103 EAPI int livebox_content_event(struct livebox *handler, enum content_event_type type, double x, double y)
1107 char cmd[32] = { '\0', };
1111 ErrPrint("Handler is NIL\n");
1112 return LB_STATUS_ERROR_INVALID;
1115 if (handler->state != CREATE || !handler->id) {
1116 ErrPrint("Handler is not valid\n");
1117 return LB_STATUS_ERROR_INVALID;
1120 if (type & CONTENT_EVENT_PD_MASK) {
1123 if (!handler->is_pd_created) {
1124 ErrPrint("PD is not created\n");
1125 return LB_STATUS_ERROR_INVALID;
1128 if (type & CONTENT_EVENT_MOUSE_MASK) {
1129 if (!handler->pd.data.fb) {
1130 ErrPrint("Handler is not valid\n");
1131 return LB_STATUS_ERROR_INVALID;
1134 if (type & CONTENT_EVENT_MOUSE_MOVE) {
1135 if (fabs(x - handler->pd.x) < MINIMUM_EVENT && fabs(y - handler->pd.y) < MINIMUM_EVENT)
1136 return LB_STATUS_ERROR_BUSY;
1137 } else if (type & CONTENT_EVENT_MOUSE_SET) {
1143 w = handler->pd.width;
1144 h = handler->pd.height;
1153 if (type & CONTENT_EVENT_MOUSE_MASK) {
1154 if (!handler->lb.mouse_event) {
1155 ErrPrint("Box is not support the mouse event\n");
1156 return LB_STATUS_ERROR_INVALID;
1159 if (!handler->lb.data.fb) {
1160 ErrPrint("Handler is not valid\n");
1161 return LB_STATUS_ERROR_INVALID;
1164 if (type & CONTENT_EVENT_MOUSE_MOVE) {
1165 if (fabs(x - handler->lb.x) < MINIMUM_EVENT && fabs(y - handler->lb.y) < MINIMUM_EVENT)
1166 return LB_STATUS_ERROR_BUSY;
1167 } else if (type & CONTENT_EVENT_MOUSE_SET) {
1173 w = handler->lb.width;
1174 h = handler->lb.height;
1183 * Must be short than 29 bytes.
1185 switch ((type & ~CONTENT_EVENT_PD_MASK)) {
1186 case CONTENT_EVENT_ACCESS_HIGHLIGHT | CONTENT_EVENT_ACCESS_MASK:
1187 strcpy(ptr, "_access_hl");
1189 case CONTENT_EVENT_ACCESS_HIGHLIGHT_PREV | CONTENT_EVENT_ACCESS_MASK:
1190 strcpy(ptr, "_access_hl_prev");
1192 case CONTENT_EVENT_ACCESS_HIGHLIGHT_NEXT | CONTENT_EVENT_ACCESS_MASK:
1193 strcpy(ptr, "_access_hl_next");
1195 case CONTENT_EVENT_ACCESS_ACTIVATE | CONTENT_EVENT_ACCESS_MASK:
1196 strcpy(ptr, "_access_activate");
1198 case CONTENT_EVENT_ACCESS_VALUE_CHANGE | CONTENT_EVENT_ACCESS_MASK:
1199 strcpy(ptr, "_access_value_change");
1201 case CONTENT_EVENT_ACCESS_SCROLL | CONTENT_EVENT_ACCESS_MASK:
1202 strcpy(ptr, "_access_scroll");
1204 case CONTENT_EVENT_MOUSE_ENTER | CONTENT_EVENT_MOUSE_MASK:
1205 strcpy(ptr, "_mouse_enter");
1207 case CONTENT_EVENT_MOUSE_LEAVE | CONTENT_EVENT_MOUSE_MASK:
1208 strcpy(ptr, "_mouse_leave");
1210 case CONTENT_EVENT_MOUSE_UP | CONTENT_EVENT_MOUSE_MASK:
1211 strcpy(ptr, "_mouse_up");
1213 case CONTENT_EVENT_MOUSE_DOWN | CONTENT_EVENT_MOUSE_MASK:
1214 strcpy(ptr, "_mouse_down");
1216 case CONTENT_EVENT_MOUSE_MOVE | CONTENT_EVENT_MOUSE_MASK:
1217 strcpy(ptr, "_mouse_move");
1219 case CONTENT_EVENT_MOUSE_SET | CONTENT_EVENT_MOUSE_MASK:
1220 strcpy(ptr, "_mouse_set");
1222 case CONTENT_EVENT_MOUSE_UNSET | CONTENT_EVENT_MOUSE_MASK:
1223 strcpy(ptr, "_mouse_unset");
1225 case CONTENT_EVENT_KEY_DOWN | CONTENT_EVENT_KEY_MASK:
1226 strcpy(ptr, "_key_down");
1228 case CONTENT_EVENT_KEY_UP | CONTENT_EVENT_KEY_MASK:
1229 strcpy(ptr, "_key_up");
1232 ErrPrint("Invalid event type\n");
1233 return LB_STATUS_ERROR_INVALID;
1236 return send_mouse_event(handler, cmd, x * w, y * h);
1239 EAPI const char *livebox_filename(struct livebox *handler)
1242 ErrPrint("Handler is NIL\n");
1246 if (handler->state != CREATE || !handler->id) {
1247 ErrPrint("Handler is not valid\n");
1251 if (handler->filename)
1252 return handler->filename;
1255 return util_uri_to_path(handler->id);
1258 EAPI int livebox_get_pdsize(struct livebox *handler, int *w, int *h)
1264 ErrPrint("Handler is NIL\n");
1265 return LB_STATUS_ERROR_INVALID;
1268 if (handler->state != CREATE || !handler->id) {
1269 ErrPrint("Handler is not valid\n");
1270 return LB_STATUS_ERROR_INVALID;
1278 if (!handler->is_pd_created) {
1279 DbgPrint("Buffer is not created yet [%dx%d]\n", *w, *h);
1280 *w = handler->pd.default_width;
1281 *h = handler->pd.default_height;
1283 *w = handler->pd.width;
1284 *h = handler->pd.height;
1287 return LB_STATUS_SUCCESS;
1290 EAPI int livebox_size(struct livebox *handler)
1296 ErrPrint("Handler is NIL\n");
1297 return LB_STATUS_ERROR_INVALID;
1300 if (handler->state != CREATE || !handler->id) {
1301 ErrPrint("Handler is not valid\n");
1302 return LB_STATUS_ERROR_INVALID;
1305 w = handler->lb.width;
1306 h = handler->lb.height;
1308 switch (handler->lb.type) {
1309 case _LB_TYPE_BUFFER:
1310 case _LB_TYPE_SCRIPT:
1311 if (!fb_is_created(handler->lb.data.fb)) {
1312 DbgPrint("Buffer is not created yet - reset size\n");
1321 return livebox_service_size_type(w, h);
1324 EAPI int livebox_set_group(struct livebox *handler, const char *cluster, const char *category, ret_cb_t cb, void *data)
1326 struct packet *packet;
1329 ErrPrint("Handler is NIL\n");
1330 return LB_STATUS_ERROR_INVALID;
1333 if (!cluster || !category || handler->state != CREATE || !handler->id) {
1334 ErrPrint("Invalid argument\n");
1335 return LB_STATUS_ERROR_INVALID;
1338 if (handler->group_changed_cb) {
1339 ErrPrint("Previous group changing request is not finished yet\n");
1340 return LB_STATUS_ERROR_BUSY;
1343 if (!handler->is_user) {
1344 ErrPrint("CA Livebox is not able to change the group\n");
1345 return LB_STATUS_ERROR_PERMISSION;
1348 if (!strcmp(handler->cluster, cluster) && !strcmp(handler->category, category)) {
1349 DbgPrint("No changes\n");
1350 return LB_STATUS_ERROR_ALREADY;
1353 packet = packet_create("change_group", "ssss", handler->pkgname, handler->id, cluster, category);
1355 ErrPrint("Failed to build a param\n");
1356 return LB_STATUS_ERROR_FAULT;
1360 cb = default_group_changed_cb;
1362 return master_rpc_async_request(handler, packet, 0, set_group_ret_cb, create_cb_info(cb, data));
1365 EAPI int livebox_get_group(struct livebox *handler, char ** const cluster, char ** const category)
1368 ErrPrint("Handler is NIL\n");
1369 return LB_STATUS_ERROR_INVALID;
1372 if (!cluster || !category || handler->state != CREATE || !handler->id) {
1373 ErrPrint("Invalid argument\n");
1374 return LB_STATUS_ERROR_INVALID;
1377 *cluster = handler->cluster;
1378 *category = handler->category;
1379 return LB_STATUS_SUCCESS;
1382 EAPI int livebox_get_supported_sizes(struct livebox *handler, int *cnt, int *size_list)
1387 if (!handler || !size_list) {
1388 ErrPrint("Invalid argument, handler(%p), size_list(%p)\n", handler, size_list);
1389 return LB_STATUS_ERROR_INVALID;
1392 if (!cnt || handler->state != CREATE || !handler->id) {
1393 ErrPrint("Handler is not valid\n");
1394 return LB_STATUS_ERROR_INVALID;
1397 for (j = i = 0; i < NR_OF_SIZE_LIST; i++) {
1398 if (handler->lb.size_list & (0x01 << i)) {
1402 size_list[j++] = (0x01 << i);
1407 return LB_STATUS_SUCCESS;
1410 EAPI const char *livebox_pkgname(struct livebox *handler)
1413 ErrPrint("Handler is NIL\n");
1417 if (handler->state != CREATE) {
1418 ErrPrint("Handler is not valid\n");
1422 return handler->pkgname;
1425 EAPI double livebox_priority(struct livebox *handler)
1428 ErrPrint("Handler is NIL\n");
1432 if (handler->state != CREATE || !handler->id) {
1433 ErrPrint("Handler is not valid (%p)\n", handler);
1437 return handler->lb.priority;
1440 EAPI int livebox_delete_cluster(const char *cluster, ret_cb_t cb, void *data)
1442 struct packet *packet;
1444 packet = packet_create("delete_cluster", "s", cluster);
1446 ErrPrint("Failed to build a param\n");
1447 return LB_STATUS_ERROR_FAULT;
1450 return master_rpc_async_request(NULL, packet, 0, delete_cluster_cb, create_cb_info(cb, data));
1453 EAPI int livebox_delete_category(const char *cluster, const char *category, ret_cb_t cb, void *data)
1455 struct packet *packet;
1457 packet = packet_create("delete_category", "ss", cluster, category);
1459 ErrPrint("Failed to build a param\n");
1460 return LB_STATUS_ERROR_FAULT;
1463 return master_rpc_async_request(NULL, packet, 0, delete_category_cb, create_cb_info(cb, data));
1466 EAPI enum livebox_lb_type livebox_lb_type(struct livebox *handler)
1469 ErrPrint("Handler is NIL\n");
1470 return LB_TYPE_INVALID;
1473 if (handler->state != CREATE || !handler->id) {
1474 ErrPrint("Handler is not valid\n");
1475 return LB_TYPE_INVALID;
1478 switch (handler->lb.type) {
1480 return LB_TYPE_IMAGE;
1481 case _LB_TYPE_BUFFER:
1482 case _LB_TYPE_SCRIPT:
1485 id = fb_id(handler->lb.data.fb);
1486 if (id && !strncasecmp(id, SCHEMA_PIXMAP, strlen(SCHEMA_PIXMAP)))
1487 return LB_TYPE_PIXMAP;
1489 return LB_TYPE_BUFFER;
1491 return LB_TYPE_TEXT;
1496 return LB_TYPE_INVALID;
1499 EAPI enum livebox_pd_type livebox_pd_type(struct livebox *handler)
1502 ErrPrint("Handler is NIL\n");
1503 return PD_TYPE_INVALID;
1506 if (handler->state != CREATE || !handler->id) {
1507 ErrPrint("Handler is not valid\n");
1508 return PD_TYPE_INVALID;
1511 switch (handler->pd.type) {
1513 return PD_TYPE_TEXT;
1514 case _PD_TYPE_BUFFER:
1515 case _PD_TYPE_SCRIPT:
1518 id = fb_id(handler->pd.data.fb);
1519 if (id && !strncasecmp(id, SCHEMA_PIXMAP, strlen(SCHEMA_PIXMAP)))
1520 return PD_TYPE_PIXMAP;
1522 return PD_TYPE_BUFFER;
1527 return PD_TYPE_INVALID;
1530 EAPI int livebox_set_pd_text_handler(struct livebox *handler, struct livebox_script_operators *ops)
1533 ErrPrint("Handler is NIL\n");
1534 return LB_STATUS_ERROR_INVALID;
1537 if (handler->state != CREATE) {
1538 ErrPrint("Handler is not valid\n");
1539 return LB_STATUS_ERROR_INVALID;
1542 memcpy(&handler->pd.data.ops, ops, sizeof(*ops));
1543 return LB_STATUS_SUCCESS;
1546 EAPI int livebox_set_text_handler(struct livebox *handler, struct livebox_script_operators *ops)
1549 ErrPrint("Handler is NIL\n");
1550 return LB_STATUS_ERROR_INVALID;
1553 if (handler->state != CREATE) {
1554 ErrPrint("Handler is not valid\n");
1555 return LB_STATUS_ERROR_INVALID;
1558 memcpy(&handler->lb.data.ops, ops, sizeof(*ops));
1559 return LB_STATUS_SUCCESS;
1562 EAPI int livebox_acquire_lb_pixmap(struct livebox *handler, ret_cb_t cb, void *data)
1564 struct packet *packet;
1568 ErrPrint("Handler is NIL\n");
1569 return LB_STATUS_ERROR_INVALID;
1572 if (handler->state != CREATE || !handler->id) {
1573 ErrPrint("Invalid handle\n");
1574 return LB_STATUS_ERROR_INVALID;
1577 if (handler->lb.type != _LB_TYPE_SCRIPT && handler->lb.type != _LB_TYPE_BUFFER) {
1578 ErrPrint("Handler is not valid type\n");
1579 return LB_STATUS_ERROR_INVALID;
1582 id = fb_id(handler->lb.data.fb);
1583 if (!id || strncasecmp(id, SCHEMA_PIXMAP, strlen(SCHEMA_PIXMAP)))
1584 return LB_STATUS_ERROR_INVALID;
1586 packet = packet_create("lb_acquire_pixmap", "ss", handler->pkgname, handler->id);
1588 ErrPrint("Failed to build a param\n");
1589 return LB_STATUS_ERROR_FAULT;
1592 return master_rpc_async_request(handler, packet, 0, pixmap_acquired_cb, create_cb_info(cb, data));
1595 EAPI int livebox_release_lb_pixmap(struct livebox *handler, int pixmap)
1597 struct packet *packet;
1600 ErrPrint("Handler is NIL\n");
1601 return LB_STATUS_ERROR_INVALID;
1604 if (handler->state != CREATE || !handler->id) {
1605 ErrPrint("Invalid handle\n");
1606 return LB_STATUS_ERROR_INVALID;
1609 if (handler->lb.type != _LB_TYPE_SCRIPT && handler->lb.type != _LB_TYPE_BUFFER) {
1610 ErrPrint("Handler is not valid type\n");
1611 return LB_STATUS_ERROR_INVALID;
1614 packet = packet_create_noack("lb_release_pixmap", "ssi", handler->pkgname, handler->id, pixmap);
1616 ErrPrint("Failed to build a param\n");
1617 return LB_STATUS_ERROR_INVALID;
1620 return master_rpc_request_only(handler, packet);
1623 EAPI int livebox_acquire_pd_pixmap(struct livebox *handler, ret_cb_t cb, void *data)
1625 struct packet *packet;
1629 ErrPrint("Handler is NIL\n");
1630 return LB_STATUS_ERROR_INVALID;
1633 if (handler->state != CREATE || !handler->id) {
1634 ErrPrint("Invalid handle\n");
1635 return LB_STATUS_ERROR_INVALID;
1638 if (handler->pd.type != _PD_TYPE_SCRIPT && handler->pd.type != _PD_TYPE_BUFFER) {
1639 ErrPrint("Handler is not valid type\n");
1640 return LB_STATUS_ERROR_INVALID;
1643 id = fb_id(handler->pd.data.fb);
1644 if (!id || strncasecmp(id, SCHEMA_PIXMAP, strlen(SCHEMA_PIXMAP)))
1645 return LB_STATUS_ERROR_INVALID;
1647 packet = packet_create("pd_acquire_pixmap", "ss", handler->pkgname, handler->id);
1649 ErrPrint("Failed to build a param\n");
1650 return LB_STATUS_ERROR_FAULT;
1653 return master_rpc_async_request(handler, packet, 0, pixmap_acquired_cb, create_cb_info(cb, data));
1656 EAPI int livebox_pd_pixmap(const struct livebox *handler)
1662 ErrPrint("Handler is NIL\n");
1666 if (handler->state != CREATE || !handler->id) {
1667 ErrPrint("Invalid handler\n");
1671 if (handler->pd.type != _PD_TYPE_SCRIPT && handler->pd.type != _PD_TYPE_BUFFER) {
1672 ErrPrint("Invalid handler\n");
1676 id = fb_id(handler->pd.data.fb);
1677 if (id && sscanf(id, SCHEMA_PIXMAP "%d", &pixmap) != 1) {
1678 ErrPrint("PIXMAP Id is not valid\n");
1685 EAPI int livebox_lb_pixmap(const struct livebox *handler)
1691 ErrPrint("Handler is NIL\n");
1695 if (handler->state != CREATE || !handler->id) {
1696 ErrPrint("Invalid handler\n");
1700 if (handler->lb.type != _LB_TYPE_SCRIPT && handler->lb.type != _LB_TYPE_BUFFER) {
1701 ErrPrint("Invalid handler\n");
1705 id = fb_id(handler->lb.data.fb);
1706 if (id && sscanf(id, SCHEMA_PIXMAP "%d", &pixmap) != 1) {
1707 ErrPrint("PIXMAP Id is not valid\n");
1714 EAPI int livebox_release_pd_pixmap(struct livebox *handler, int pixmap)
1716 struct packet *packet;
1719 ErrPrint("Handler is NIL\n");
1720 return LB_STATUS_ERROR_INVALID;
1723 if (handler->state != CREATE || !handler->id) {
1724 ErrPrint("Invalid handle\n");
1725 return LB_STATUS_ERROR_INVALID;
1728 if (handler->pd.type != _PD_TYPE_SCRIPT && handler->pd.type != _PD_TYPE_BUFFER) {
1729 ErrPrint("Handler is not valid type\n");
1730 return LB_STATUS_ERROR_INVALID;
1733 packet = packet_create_noack("pd_release_pixmap", "ssi", handler->pkgname, handler->id, pixmap);
1735 ErrPrint("Failed to build a param\n");
1736 return LB_STATUS_ERROR_FAULT;
1739 return master_rpc_request_only(handler, packet);
1742 EAPI void *livebox_acquire_fb(struct livebox *handler)
1745 ErrPrint("Handler is NIL\n");
1749 if (handler->state != CREATE || !handler->id) {
1750 ErrPrint("Invalid handle\n");
1754 if (handler->lb.type != _LB_TYPE_SCRIPT && handler->lb.type != _LB_TYPE_BUFFER) {
1755 ErrPrint("Handler is not valid type\n");
1759 return fb_acquire_buffer(handler->lb.data.fb);
1762 EAPI int livebox_release_fb(void *buffer)
1764 return fb_release_buffer(buffer);
1767 EAPI int livebox_fb_refcnt(void *buffer)
1769 return fb_refcnt(buffer);
1772 EAPI void *livebox_acquire_pdfb(struct livebox *handler)
1775 ErrPrint("Handler is NIL\n");
1779 if (handler->state != CREATE || !handler->id) {
1780 ErrPrint("Invalid handler\n");
1784 if (handler->pd.type != _PD_TYPE_SCRIPT && handler->pd.type != _PD_TYPE_BUFFER) {
1785 ErrPrint("Handler is not valid type\n");
1789 return fb_acquire_buffer(handler->pd.data.fb);
1792 EAPI int livebox_release_pdfb(void *buffer)
1794 return fb_release_buffer(buffer);
1797 EAPI int livebox_pdfb_refcnt(void *buffer)
1799 return fb_refcnt(buffer);
1802 EAPI int livebox_pdfb_bufsz(struct livebox *handler)
1805 ErrPrint("Handler is NIL\n");
1806 return LB_STATUS_ERROR_INVALID;
1809 if (handler->state != CREATE || !handler->id) {
1810 ErrPrint("Handler is not valid\n");
1811 return LB_STATUS_ERROR_INVALID;
1814 return fb_size(handler->pd.data.fb);
1817 EAPI int livebox_lbfb_bufsz(struct livebox *handler)
1820 ErrPrint("Handler is NIL\n");
1821 return LB_STATUS_ERROR_INVALID;
1824 if (handler->state != CREATE || !handler->id) {
1825 ErrPrint("Handler is not valid\n");
1826 return LB_STATUS_ERROR_INVALID;
1829 return fb_size(handler->lb.data.fb);
1832 EAPI int livebox_is_user(struct livebox *handler)
1835 ErrPrint("Handler is NIL\n");
1836 return LB_STATUS_ERROR_INVALID;
1839 if (handler->state != CREATE) {
1840 ErrPrint("Handler is invalid\n");
1841 return LB_STATUS_ERROR_INVALID;
1844 return handler->is_user;
1847 EAPI int livebox_set_pinup(struct livebox *handler, int flag, ret_cb_t cb, void *data)
1849 struct packet *packet;
1852 ErrPrint("Handler is NIL\n");
1853 return LB_STATUS_ERROR_INVALID;
1856 if (handler->state != CREATE || !handler->id) {
1857 ErrPrint("Handler is not valid\n");
1858 return LB_STATUS_ERROR_INVALID;
1861 if (handler->pinup_cb) {
1862 ErrPrint("Previous pinup request is not finished\n");
1863 return LB_STATUS_ERROR_BUSY;
1866 if (handler->is_pinned_up == flag) {
1867 DbgPrint("No changes\n");
1868 return LB_STATUS_ERROR_ALREADY;
1871 packet = packet_create("pinup_changed", "ssi", handler->pkgname, handler->id, flag);
1873 ErrPrint("Failed to build a param\n");
1874 return LB_STATUS_ERROR_FAULT;
1878 cb = default_pinup_cb;
1880 return master_rpc_async_request(handler, packet, 0, pinup_done_cb, create_cb_info(cb, data));
1883 EAPI int livebox_is_pinned_up(struct livebox *handler)
1886 ErrPrint("Handler is NIL\n");
1887 return LB_STATUS_ERROR_INVALID;
1890 if (handler->state != CREATE || !handler->id)
1891 return LB_STATUS_ERROR_INVALID;
1893 return handler->is_pinned_up;
1896 EAPI int livebox_has_pinup(struct livebox *handler)
1899 ErrPrint("Handler is NIL\n");
1900 return LB_STATUS_ERROR_INVALID;
1903 if (handler->state != CREATE || !handler->id)
1904 return LB_STATUS_ERROR_INVALID;
1906 return handler->lb.pinup_supported;
1909 EAPI int livebox_set_data(struct livebox *handler, void *data)
1912 ErrPrint("Handler is NIL\n");
1913 return LB_STATUS_ERROR_INVALID;
1916 if (handler->state != CREATE)
1917 return LB_STATUS_ERROR_INVALID;
1919 handler->data = data;
1920 return LB_STATUS_SUCCESS;
1923 EAPI void *livebox_get_data(struct livebox *handler)
1926 ErrPrint("Handler is NIL\n");
1930 if (handler->state != CREATE)
1933 return handler->data;
1936 EAPI int livebox_is_exists(const char *pkgname)
1940 lb = lb_pkgname(pkgname);
1949 EAPI const char *livebox_content(struct livebox *handler)
1952 ErrPrint("Handler is NIL\n");
1956 if (handler->state != CREATE)
1959 return handler->content;
1962 EAPI const char *livebox_category_title(struct livebox *handler)
1965 ErrPrint("Handler is NIL\n");
1969 if (handler->state != CREATE)
1972 return handler->title;
1975 EAPI int livebox_emit_text_signal(struct livebox *handler, const char *emission, const char *source, double sx, double sy, double ex, double ey, ret_cb_t cb, void *data)
1977 struct packet *packet;
1980 ErrPrint("Handler is NIL\n");
1981 return LB_STATUS_ERROR_INVALID;
1984 if ((handler->lb.type != _LB_TYPE_TEXT && handler->pd.type != _PD_TYPE_TEXT) || handler->state != CREATE || !handler->id) {
1985 ErrPrint("Handler is not valid\n");
1986 return LB_STATUS_ERROR_INVALID;
1995 packet = packet_create("text_signal", "ssssdddd",
1996 handler->pkgname, handler->id, emission, source, sx, sy, ex, ey);
1998 ErrPrint("Failed to build a param\n");
1999 return LB_STATUS_ERROR_FAULT;
2002 return master_rpc_async_request(handler, packet, 0, text_signal_cb, create_cb_info(cb, data));
2005 EAPI int livebox_subscribe_group(const char *cluster, const char *category)
2007 struct packet *packet;
2011 * Validate the group info using DB
2012 * If the group info is not valid, do not send this request
2015 packet = packet_create_noack("subscribe", "ss", cluster ? cluster : "", category ? category : "");
2017 ErrPrint("Failed to create a packet\n");
2018 return LB_STATUS_ERROR_FAULT;
2021 return master_rpc_request_only(NULL, packet);
2024 EAPI int livebox_unsubscribe_group(const char *cluster, const char *category)
2026 struct packet *packet;
2030 * Validate the group info using DB
2031 * If the group info is not valid, do not send this request
2032 * AND Check the subscribed or not too
2035 packet = packet_create_noack("unsubscribe", "ss", cluster ? cluster : "", category ? category : "");
2037 ErrPrint("Failed to create a packet\n");
2038 return LB_STATUS_ERROR_FAULT;
2041 return master_rpc_request_only(NULL, packet);
2044 EAPI int livebox_refresh(struct livebox *handler)
2046 struct packet *packet;
2049 ErrPrint("Hnalder is NIL\n");
2050 return LB_STATUS_ERROR_INVALID;
2053 if (handler->state != CREATE || !handler->id)
2054 return LB_STATUS_ERROR_INVALID;
2056 packet = packet_create_noack("update", "ss", handler->pkgname, handler->id);
2058 ErrPrint("Failed to create a packet\n");
2059 return LB_STATUS_ERROR_FAULT;
2062 return master_rpc_request_only(handler, packet);
2065 EAPI int livebox_refresh_group(const char *cluster, const char *category)
2067 struct packet *packet;
2069 if (!cluster || !category) {
2070 ErrPrint("Invalid argument\n");
2071 return LB_STATUS_ERROR_INVALID;
2074 packet = packet_create_noack("refresh_group", "ss", cluster, category);
2076 ErrPrint("Failed to create a packet\n");
2077 return LB_STATUS_ERROR_FAULT;
2080 return master_rpc_request_only(NULL, packet);
2083 EAPI int livebox_set_visibility(struct livebox *handler, enum livebox_visible_state state)
2085 struct packet *packet;
2089 ErrPrint("Handler is NIL\n");
2090 return LB_STATUS_ERROR_INVALID;
2093 if (handler->state != CREATE || !handler->id)
2094 return LB_STATUS_ERROR_INVALID;
2096 if (!handler->is_user) {
2097 /* System cluster livebox cannot be changed its visible states */
2098 if (state == LB_HIDE_WITH_PAUSE) {
2099 ErrPrint("CA Livebox is not able to change the visibility\n");
2100 return LB_STATUS_ERROR_PERMISSION;
2104 DbgPrint("Change the visibility %d <> %d, %s\n", handler->visible, state, handler->id);
2106 if (handler->visible == state)
2107 return LB_STATUS_ERROR_ALREADY;
2109 packet = packet_create_noack("change,visibility", "ssi", handler->pkgname, handler->id, (int)state);
2111 ErrPrint("Failed to create a packet\n");
2112 return LB_STATUS_ERROR_FAULT;
2115 ret = master_rpc_request_only(handler, packet);
2117 handler->visible = state;
2122 EAPI enum livebox_visible_state livebox_visibility(struct livebox *handler)
2125 ErrPrint("Handler is NIL\n");
2126 return LB_VISIBLE_ERROR;
2129 if (handler->state != CREATE || !handler->id)
2130 return LB_VISIBLE_ERROR;
2132 return handler->visible;
2135 int lb_set_group(struct livebox *handler, const char *cluster, const char *category)
2141 pc = strdup(cluster);
2143 CRITICAL_LOG("Heap: %s (cluster: %s)\n", strerror(errno), cluster);
2144 return LB_STATUS_ERROR_MEMORY;
2149 ps = strdup(category);
2151 CRITICAL_LOG("Heap: %s (category: %s)\n", strerror(errno), category);
2153 return LB_STATUS_ERROR_MEMORY;
2157 if (handler->cluster)
2158 free(handler->cluster);
2160 if (handler->category)
2161 free(handler->category);
2163 handler->cluster = pc;
2164 handler->category = ps;
2166 return LB_STATUS_SUCCESS;
2169 void lb_set_size(struct livebox *handler, int w, int h)
2171 handler->lb.width = w;
2172 handler->lb.height = h;
2175 void lb_set_pdsize(struct livebox *handler, int w, int h)
2177 handler->pd.width = w;
2178 handler->pd.height = h;
2181 void lb_set_default_pdsize(struct livebox *handler, int w, int h)
2183 handler->pd.default_width = w;
2184 handler->pd.default_height = h;
2187 void lb_invoke_fault_handler(enum livebox_fault_type event, const char *pkgname, const char *file, const char *func)
2191 struct fault_info *info;
2193 dlist_foreach_safe(s_info.fault_list, l, n, info) {
2194 if (info->handler(event, pkgname, file, func, info->user_data) == EXIT_FAILURE)
2195 s_info.fault_list = dlist_remove(s_info.fault_list, l);
2199 void lb_invoke_event_handler(struct livebox *handler, enum livebox_event_type event)
2203 struct event_info *info;
2205 dlist_foreach_safe(s_info.event_list, l, n, info) {
2206 if (info->handler(handler, event, info->user_data) == EXIT_FAILURE)
2207 s_info.event_list = dlist_remove(s_info.event_list, l);
2211 struct livebox *lb_find_livebox(const char *pkgname, const char *id)
2214 struct livebox *handler;
2216 dlist_foreach(s_info.livebox_list, l, handler) {
2220 if (!strcmp(handler->pkgname, pkgname) && !strcmp(handler->id, id))
2227 struct livebox *lb_find_livebox_by_timestamp(double timestamp)
2230 struct livebox *handler;
2232 dlist_foreach(s_info.livebox_list, l, handler) {
2233 if (handler->timestamp == timestamp)
2240 static inline char *get_file_kept_in_safe(const char *id)
2247 path = util_uri_to_path(id);
2249 ErrPrint("Invalid URI(%s)\n", id);
2256 if (s_info.prevent_overwrite) {
2257 new_path = strdup(path);
2259 ErrPrint("Heap: %s\n", strerror(errno));
2268 while (base_idx > 0 && path[base_idx] != '/') base_idx--;
2269 base_idx += (path[base_idx] == '/');
2271 new_path = malloc(len + 10);
2273 ErrPrint("Heap: %s\n", strerror(errno));
2277 strncpy(new_path, path, base_idx);
2278 snprintf(new_path + base_idx, len + 10 - base_idx, "reader/%s", path + base_idx);
2282 struct livebox *lb_new_livebox(const char *pkgname, const char *id, double timestamp)
2284 struct livebox *handler;
2286 handler = calloc(1, sizeof(*handler));
2288 ErrPrint("Failed to create a new livebox\n");
2292 handler->pkgname = strdup(pkgname);
2293 if (!handler->pkgname) {
2294 ErrPrint("%s\n", strerror(errno));
2299 handler->id = strdup(id);
2301 ErrPrint("%s\n", strerror(errno));
2302 free(handler->pkgname);
2307 handler->filename = get_file_kept_in_safe(id);
2308 if (!handler->filename) {
2309 handler->filename = strdup(util_uri_to_path(id));
2310 if (!handler->filename)
2311 ErrPrint("Error: %s\n", strerror(errno));
2314 handler->timestamp = timestamp;
2315 handler->lb.type = _LB_TYPE_FILE;
2316 handler->pd.type = _PD_TYPE_SCRIPT;
2317 handler->state = CREATE;
2318 handler->visible = LB_SHOW;
2320 s_info.livebox_list = dlist_append(s_info.livebox_list, handler);
2325 int lb_delete_all(void)
2329 struct livebox *handler;
2331 dlist_foreach_safe(s_info.livebox_list, l, n, handler) {
2332 lb_invoke_event_handler(handler, LB_EVENT_DELETED);
2336 return LB_STATUS_SUCCESS;
2339 int lb_set_content(struct livebox *handler, const char *content)
2341 if (handler->content) {
2342 free(handler->content);
2343 handler->content = NULL;
2347 handler->content = strdup(content);
2348 if (!handler->content) {
2349 CRITICAL_LOG("Heap: %s (content: %s)\n", strerror(errno), content);
2350 return LB_STATUS_ERROR_MEMORY;
2354 return LB_STATUS_SUCCESS;
2357 int lb_set_title(struct livebox *handler, const char *title)
2359 if (handler->title) {
2360 free(handler->title);
2361 handler->title = NULL;
2365 handler->title = strdup(title);
2366 if (!handler->title) {
2367 CRITICAL_LOG("Heap: %s (title: %s)\n", strerror(errno), title);
2368 return LB_STATUS_ERROR_MEMORY;
2372 return LB_STATUS_SUCCESS;
2375 void lb_set_size_list(struct livebox *handler, int size_list)
2377 handler->lb.size_list = size_list;
2380 void lb_set_auto_launch(struct livebox *handler, const char *auto_launch)
2382 if (!strlen(auto_launch))
2385 handler->lb.auto_launch = strdup(auto_launch);
2386 if (!handler->lb.auto_launch)
2387 ErrPrint("Heap: %s\n", strerror(errno));
2390 void lb_set_priority(struct livebox *handler, double priority)
2392 handler->lb.priority = priority;
2395 void lb_set_id(struct livebox *handler, const char *id)
2400 handler->id = strdup(id);
2402 ErrPrint("Error: %s\n", strerror(errno));
2404 if (handler->filename)
2405 free(handler->filename);
2407 handler->filename = get_file_kept_in_safe(id);
2408 if (!handler->filename) {
2409 handler->filename = strdup(util_uri_to_path(id));
2410 if (!handler->filename)
2411 ErrPrint("Error: %s\n", strerror(errno));
2415 int lb_set_lb_fb(struct livebox *handler, const char *filename)
2420 return LB_STATUS_ERROR_INVALID;
2422 fb = handler->lb.data.fb;
2423 if (fb && !strcmp(fb_id(fb), filename)) /*!< BUFFER is not changed, */
2424 return LB_STATUS_SUCCESS;
2426 handler->lb.data.fb = NULL;
2428 if (!filename || filename[0] == '\0') {
2431 return LB_STATUS_SUCCESS;
2434 handler->lb.data.fb = fb_create(filename, handler->lb.width, handler->lb.height);
2435 if (!handler->lb.data.fb) {
2436 ErrPrint("Faield to create a FB\n");
2439 return LB_STATUS_ERROR_FAULT;
2445 return LB_STATUS_SUCCESS;
2448 int lb_set_pd_fb(struct livebox *handler, const char *filename)
2453 return LB_STATUS_ERROR_INVALID;
2455 fb = handler->pd.data.fb;
2456 if (fb && !strcmp(fb_id(fb), filename)) {
2457 /* BUFFER is not changed, just update the content */
2458 return LB_STATUS_ERROR_EXIST;
2460 handler->pd.data.fb = NULL;
2462 if (!filename || filename[0] == '\0') {
2465 return LB_STATUS_SUCCESS;
2468 handler->pd.data.fb = fb_create(filename, handler->pd.width, handler->pd.height);
2469 if (!handler->pd.data.fb) {
2470 ErrPrint("Failed to create a FB\n");
2473 return LB_STATUS_ERROR_FAULT;
2478 return LB_STATUS_SUCCESS;
2481 struct fb_info *lb_get_lb_fb(struct livebox *handler)
2483 return handler->lb.data.fb;
2486 struct fb_info *lb_get_pd_fb(struct livebox *handler)
2488 return handler->pd.data.fb;
2491 void lb_set_user(struct livebox *handler, int user)
2493 handler->is_user = user;
2496 void lb_set_pinup(struct livebox *handler, int pinup_supported)
2498 handler->lb.pinup_supported = pinup_supported;
2501 void lb_set_text_lb(struct livebox *handler)
2503 handler->lb.type = _LB_TYPE_TEXT;
2506 void lb_set_text_pd(struct livebox *handler)
2508 handler->pd.type = _PD_TYPE_TEXT;
2511 int lb_text_lb(struct livebox *handler)
2513 return handler->lb.type == _LB_TYPE_TEXT;
2516 int lb_text_pd(struct livebox *handler)
2518 return handler->pd.type == _PD_TYPE_TEXT;
2521 void lb_set_period(struct livebox *handler, double period)
2523 handler->lb.period = period;
2526 struct livebox *lb_ref(struct livebox *handler)
2535 struct livebox *lb_unref(struct livebox *handler)
2541 if (handler->refcnt > 0)
2544 if (handler->filename)
2545 util_unlink(handler->filename);
2547 dlist_remove_data(s_info.livebox_list, handler);
2549 handler->state = DESTROYED;
2550 free(handler->cluster);
2551 free(handler->category);
2553 free(handler->pkgname);
2554 free(handler->filename);
2555 free(handler->lb.auto_launch);
2557 if (handler->lb.data.fb) {
2558 fb_destroy(handler->lb.data.fb);
2559 handler->lb.data.fb = NULL;
2562 if (handler->pd.data.fb) {
2563 fb_destroy(handler->pd.data.fb);
2564 handler->pd.data.fb = NULL;
2571 int lb_send_delete(struct livebox *handler, ret_cb_t cb, void *data)
2573 struct packet *packet;
2575 if (!cb && !!data) {
2576 ErrPrint("Invalid argument\n");
2577 return LB_STATUS_ERROR_INVALID;
2580 if (handler->deleted_cb) {
2581 ErrPrint("Already in-progress\n");
2582 return LB_STATUS_ERROR_BUSY;
2585 packet = packet_create("delete", "ss", handler->pkgname, handler->id);
2587 ErrPrint("Failed to build a param\n");
2589 cb(handler, LB_STATUS_ERROR_FAULT, data);
2591 return LB_STATUS_ERROR_FAULT;
2595 cb = default_delete_cb;
2597 return master_rpc_async_request(handler, packet, 0, del_ret_cb, create_cb_info(cb, data));
2600 EAPI int livebox_client_paused(void)
2602 struct packet *packet;
2604 packet = packet_create_noack("client_paused", "d", util_timestamp());
2606 ErrPrint("Failed to create a pause packet\n");
2607 return LB_STATUS_ERROR_FAULT;
2610 return master_rpc_request_only(NULL, packet);
2613 EAPI int livebox_client_resumed(void)
2615 struct packet *packet;
2617 packet = packet_create_noack("client_resumed", "d", util_timestamp());
2619 ErrPrint("Failed to create a resume packet\n");
2620 return LB_STATUS_ERROR_FAULT;
2623 return master_rpc_request_only(NULL, packet);