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 || width < 0 || height < 0) {
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[20] = { '\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;
1182 switch ((type & ~CONTENT_EVENT_PD_MASK)) {
1183 case CONTENT_EVENT_ACCESS_READ | CONTENT_EVENT_ACCESS_MASK:
1184 strcpy(ptr, "_access_read");
1186 case CONTENT_EVENT_ACCESS_READ_PREV | CONTENT_EVENT_ACCESS_MASK:
1187 strcpy(ptr, "_access_read_prev");
1189 case CONTENT_EVENT_ACCESS_READ_NEXT | CONTENT_EVENT_ACCESS_MASK:
1190 strcpy(ptr, "_access_read_next");
1192 case CONTENT_EVENT_ACCESS_ACTIVATE | CONTENT_EVENT_ACCESS_MASK:
1193 strcpy(ptr, "_access_activate");
1195 case CONTENT_EVENT_ACCESS_UP | CONTENT_EVENT_ACCESS_MASK:
1196 strcpy(ptr, "_access_up");
1198 case CONTENT_EVENT_ACCESS_DOWN | CONTENT_EVENT_ACCESS_MASK:
1199 strcpy(ptr, "_access_down");
1201 case CONTENT_EVENT_MOUSE_ENTER | CONTENT_EVENT_MOUSE_MASK:
1202 strcpy(ptr, "_mouse_enter");
1204 case CONTENT_EVENT_MOUSE_LEAVE | CONTENT_EVENT_MOUSE_MASK:
1205 strcpy(ptr, "_mouse_leave");
1207 case CONTENT_EVENT_MOUSE_UP | CONTENT_EVENT_MOUSE_MASK:
1208 strcpy(ptr, "_mouse_up");
1210 case CONTENT_EVENT_MOUSE_DOWN | CONTENT_EVENT_MOUSE_MASK:
1211 strcpy(ptr, "_mouse_down");
1213 case CONTENT_EVENT_MOUSE_MOVE | CONTENT_EVENT_MOUSE_MASK:
1214 strcpy(ptr, "_mouse_move");
1216 case CONTENT_EVENT_MOUSE_SET | CONTENT_EVENT_MOUSE_MASK:
1217 strcpy(ptr, "_mouse_set");
1219 case CONTENT_EVENT_MOUSE_UNSET | CONTENT_EVENT_MOUSE_MASK:
1220 strcpy(ptr, "_mouse_unset");
1222 case CONTENT_EVENT_KEY_DOWN | CONTENT_EVENT_KEY_MASK:
1223 strcpy(ptr, "_key_down");
1225 case CONTENT_EVENT_KEY_UP | CONTENT_EVENT_KEY_MASK:
1226 strcpy(ptr, "_key_up");
1229 ErrPrint("Invalid event type\n");
1230 return LB_STATUS_ERROR_INVALID;
1233 return send_mouse_event(handler, cmd, x * w, y * h);
1236 EAPI const char *livebox_filename(struct livebox *handler)
1239 ErrPrint("Handler is NIL\n");
1243 if (handler->state != CREATE || !handler->id) {
1244 ErrPrint("Handler is not valid\n");
1248 if (handler->filename)
1249 return handler->filename;
1252 return util_uri_to_path(handler->id);
1255 EAPI int livebox_get_pdsize(struct livebox *handler, int *w, int *h)
1261 ErrPrint("Handler is NIL\n");
1262 return LB_STATUS_ERROR_INVALID;
1265 if (handler->state != CREATE || !handler->id) {
1266 ErrPrint("Handler is not valid\n");
1267 return LB_STATUS_ERROR_INVALID;
1275 if (!handler->is_pd_created) {
1276 DbgPrint("Buffer is not created yet [%dx%d]\n", *w, *h);
1277 *w = handler->pd.default_width;
1278 *h = handler->pd.default_height;
1280 *w = handler->pd.width;
1281 *h = handler->pd.height;
1284 return LB_STATUS_SUCCESS;
1287 EAPI int livebox_size(struct livebox *handler)
1293 ErrPrint("Handler is NIL\n");
1294 return LB_STATUS_ERROR_INVALID;
1297 if (handler->state != CREATE || !handler->id) {
1298 ErrPrint("Handler is not valid\n");
1299 return LB_STATUS_ERROR_INVALID;
1302 w = handler->lb.width;
1303 h = handler->lb.height;
1305 switch (handler->lb.type) {
1306 case _LB_TYPE_BUFFER:
1307 case _LB_TYPE_SCRIPT:
1308 if (!fb_is_created(handler->lb.data.fb)) {
1309 DbgPrint("Buffer is not created yet - reset size\n");
1318 return livebox_service_size_type(w, h);
1321 EAPI int livebox_set_group(struct livebox *handler, const char *cluster, const char *category, ret_cb_t cb, void *data)
1323 struct packet *packet;
1326 ErrPrint("Handler is NIL\n");
1327 return LB_STATUS_ERROR_INVALID;
1330 if (!cluster || !category || handler->state != CREATE || !handler->id) {
1331 ErrPrint("Invalid argument\n");
1332 return LB_STATUS_ERROR_INVALID;
1335 if (handler->group_changed_cb) {
1336 ErrPrint("Previous group changing request is not finished yet\n");
1337 return LB_STATUS_ERROR_BUSY;
1340 if (!handler->is_user) {
1341 ErrPrint("CA Livebox is not able to change the group\n");
1342 return LB_STATUS_ERROR_PERMISSION;
1345 if (!strcmp(handler->cluster, cluster) && !strcmp(handler->category, category)) {
1346 DbgPrint("No changes\n");
1347 return LB_STATUS_ERROR_ALREADY;
1350 packet = packet_create("change_group", "ssss", handler->pkgname, handler->id, cluster, category);
1352 ErrPrint("Failed to build a param\n");
1353 return LB_STATUS_ERROR_FAULT;
1357 cb = default_group_changed_cb;
1359 return master_rpc_async_request(handler, packet, 0, set_group_ret_cb, create_cb_info(cb, data));
1362 EAPI int livebox_get_group(struct livebox *handler, char ** const cluster, char ** const category)
1365 ErrPrint("Handler is NIL\n");
1366 return LB_STATUS_ERROR_INVALID;
1369 if (!cluster || !category || handler->state != CREATE || !handler->id) {
1370 ErrPrint("Invalid argument\n");
1371 return LB_STATUS_ERROR_INVALID;
1374 *cluster = handler->cluster;
1375 *category = handler->category;
1376 return LB_STATUS_SUCCESS;
1379 EAPI int livebox_get_supported_sizes(struct livebox *handler, int *cnt, int *size_list)
1384 if (!handler || !size_list) {
1385 ErrPrint("Invalid argument, handler(%p), size_list(%p)\n", handler, size_list);
1386 return LB_STATUS_ERROR_INVALID;
1389 if (!cnt || handler->state != CREATE || !handler->id) {
1390 ErrPrint("Handler is not valid\n");
1391 return LB_STATUS_ERROR_INVALID;
1394 for (j = i = 0; i < NR_OF_SIZE_LIST; i++) {
1395 if (handler->lb.size_list & (0x01 << i)) {
1399 size_list[j++] = (0x01 << i);
1404 return LB_STATUS_SUCCESS;
1407 EAPI const char *livebox_pkgname(struct livebox *handler)
1410 ErrPrint("Handler is NIL\n");
1414 if (handler->state != CREATE) {
1415 ErrPrint("Handler is not valid\n");
1419 return handler->pkgname;
1422 EAPI double livebox_priority(struct livebox *handler)
1425 ErrPrint("Handler is NIL\n");
1429 if (handler->state != CREATE || !handler->id) {
1430 ErrPrint("Handler is not valid (%p)\n", handler);
1434 return handler->lb.priority;
1437 EAPI int livebox_delete_cluster(const char *cluster, ret_cb_t cb, void *data)
1439 struct packet *packet;
1441 packet = packet_create("delete_cluster", "s", cluster);
1443 ErrPrint("Failed to build a param\n");
1444 return LB_STATUS_ERROR_FAULT;
1447 return master_rpc_async_request(NULL, packet, 0, delete_cluster_cb, create_cb_info(cb, data));
1450 EAPI int livebox_delete_category(const char *cluster, const char *category, ret_cb_t cb, void *data)
1452 struct packet *packet;
1454 packet = packet_create("delete_category", "ss", cluster, category);
1456 ErrPrint("Failed to build a param\n");
1457 return LB_STATUS_ERROR_FAULT;
1460 return master_rpc_async_request(NULL, packet, 0, delete_category_cb, create_cb_info(cb, data));
1463 EAPI enum livebox_lb_type livebox_lb_type(struct livebox *handler)
1466 ErrPrint("Handler is NIL\n");
1467 return LB_TYPE_INVALID;
1470 if (handler->state != CREATE || !handler->id) {
1471 ErrPrint("Handler is not valid\n");
1472 return LB_TYPE_INVALID;
1475 switch (handler->lb.type) {
1477 return LB_TYPE_IMAGE;
1478 case _LB_TYPE_BUFFER:
1479 case _LB_TYPE_SCRIPT:
1482 id = fb_id(handler->lb.data.fb);
1483 if (id && !strncasecmp(id, SCHEMA_PIXMAP, strlen(SCHEMA_PIXMAP)))
1484 return LB_TYPE_PIXMAP;
1486 return LB_TYPE_BUFFER;
1488 return LB_TYPE_TEXT;
1493 return LB_TYPE_INVALID;
1496 EAPI enum livebox_pd_type livebox_pd_type(struct livebox *handler)
1499 ErrPrint("Handler is NIL\n");
1500 return PD_TYPE_INVALID;
1503 if (handler->state != CREATE || !handler->id) {
1504 ErrPrint("Handler is not valid\n");
1505 return PD_TYPE_INVALID;
1508 switch (handler->pd.type) {
1510 return PD_TYPE_TEXT;
1511 case _PD_TYPE_BUFFER:
1512 case _PD_TYPE_SCRIPT:
1515 id = fb_id(handler->pd.data.fb);
1516 if (id && !strncasecmp(id, SCHEMA_PIXMAP, strlen(SCHEMA_PIXMAP)))
1517 return PD_TYPE_PIXMAP;
1519 return PD_TYPE_BUFFER;
1524 return PD_TYPE_INVALID;
1527 EAPI int livebox_set_pd_text_handler(struct livebox *handler, struct livebox_script_operators *ops)
1530 ErrPrint("Handler is NIL\n");
1531 return LB_STATUS_ERROR_INVALID;
1534 if (handler->state != CREATE) {
1535 ErrPrint("Handler is not valid\n");
1536 return LB_STATUS_ERROR_INVALID;
1539 memcpy(&handler->pd.data.ops, ops, sizeof(*ops));
1540 return LB_STATUS_SUCCESS;
1543 EAPI int livebox_set_text_handler(struct livebox *handler, struct livebox_script_operators *ops)
1546 ErrPrint("Handler is NIL\n");
1547 return LB_STATUS_ERROR_INVALID;
1550 if (handler->state != CREATE) {
1551 ErrPrint("Handler is not valid\n");
1552 return LB_STATUS_ERROR_INVALID;
1555 memcpy(&handler->lb.data.ops, ops, sizeof(*ops));
1556 return LB_STATUS_SUCCESS;
1559 EAPI int livebox_acquire_lb_pixmap(struct livebox *handler, ret_cb_t cb, void *data)
1561 struct packet *packet;
1565 ErrPrint("Handler is NIL\n");
1566 return LB_STATUS_ERROR_INVALID;
1569 if (handler->state != CREATE || !handler->id) {
1570 ErrPrint("Invalid handle\n");
1571 return LB_STATUS_ERROR_INVALID;
1574 if (handler->lb.type != _LB_TYPE_SCRIPT && handler->lb.type != _LB_TYPE_BUFFER) {
1575 ErrPrint("Handler is not valid type\n");
1576 return LB_STATUS_ERROR_INVALID;
1579 id = fb_id(handler->lb.data.fb);
1580 if (!id || strncasecmp(id, SCHEMA_PIXMAP, strlen(SCHEMA_PIXMAP)))
1581 return LB_STATUS_ERROR_INVALID;
1583 packet = packet_create("lb_acquire_pixmap", "ss", handler->pkgname, handler->id);
1585 ErrPrint("Failed to build a param\n");
1586 return LB_STATUS_ERROR_FAULT;
1589 return master_rpc_async_request(handler, packet, 0, pixmap_acquired_cb, create_cb_info(cb, data));
1592 EAPI int livebox_release_lb_pixmap(struct livebox *handler, int pixmap)
1594 struct packet *packet;
1597 ErrPrint("Handler is NIL\n");
1598 return LB_STATUS_ERROR_INVALID;
1601 if (handler->state != CREATE || !handler->id) {
1602 ErrPrint("Invalid handle\n");
1603 return LB_STATUS_ERROR_INVALID;
1606 if (handler->lb.type != _LB_TYPE_SCRIPT && handler->lb.type != _LB_TYPE_BUFFER) {
1607 ErrPrint("Handler is not valid type\n");
1608 return LB_STATUS_ERROR_INVALID;
1611 packet = packet_create_noack("lb_release_pixmap", "ssi", handler->pkgname, handler->id, pixmap);
1613 ErrPrint("Failed to build a param\n");
1614 return LB_STATUS_ERROR_INVALID;
1617 return master_rpc_request_only(handler, packet);
1620 EAPI int livebox_acquire_pd_pixmap(struct livebox *handler, ret_cb_t cb, void *data)
1622 struct packet *packet;
1626 ErrPrint("Handler is NIL\n");
1627 return LB_STATUS_ERROR_INVALID;
1630 if (handler->state != CREATE || !handler->id) {
1631 ErrPrint("Invalid handle\n");
1632 return LB_STATUS_ERROR_INVALID;
1635 if (handler->pd.type != _PD_TYPE_SCRIPT && handler->pd.type != _PD_TYPE_BUFFER) {
1636 ErrPrint("Handler is not valid type\n");
1637 return LB_STATUS_ERROR_INVALID;
1640 id = fb_id(handler->pd.data.fb);
1641 if (!id || strncasecmp(id, SCHEMA_PIXMAP, strlen(SCHEMA_PIXMAP)))
1642 return LB_STATUS_ERROR_INVALID;
1644 packet = packet_create("pd_acquire_pixmap", "ss", handler->pkgname, handler->id);
1646 ErrPrint("Failed to build a param\n");
1647 return LB_STATUS_ERROR_FAULT;
1650 return master_rpc_async_request(handler, packet, 0, pixmap_acquired_cb, create_cb_info(cb, data));
1653 EAPI int livebox_pd_pixmap(const struct livebox *handler)
1659 ErrPrint("Handler is NIL\n");
1663 if (handler->state != CREATE || !handler->id) {
1664 ErrPrint("Invalid handler\n");
1668 if (handler->pd.type != _PD_TYPE_SCRIPT && handler->pd.type != _PD_TYPE_BUFFER) {
1669 ErrPrint("Invalid handler\n");
1673 id = fb_id(handler->pd.data.fb);
1674 if (id && sscanf(id, SCHEMA_PIXMAP "%d", &pixmap) != 1) {
1675 ErrPrint("PIXMAP Id is not valid\n");
1682 EAPI int livebox_lb_pixmap(const struct livebox *handler)
1688 ErrPrint("Handler is NIL\n");
1692 if (handler->state != CREATE || !handler->id) {
1693 ErrPrint("Invalid handler\n");
1697 if (handler->lb.type != _LB_TYPE_SCRIPT && handler->lb.type != _LB_TYPE_BUFFER) {
1698 ErrPrint("Invalid handler\n");
1702 id = fb_id(handler->lb.data.fb);
1703 if (id && sscanf(id, SCHEMA_PIXMAP "%d", &pixmap) != 1) {
1704 ErrPrint("PIXMAP Id is not valid\n");
1711 EAPI int livebox_release_pd_pixmap(struct livebox *handler, int pixmap)
1713 struct packet *packet;
1716 ErrPrint("Handler is NIL\n");
1717 return LB_STATUS_ERROR_INVALID;
1720 if (handler->state != CREATE || !handler->id) {
1721 ErrPrint("Invalid handle\n");
1722 return LB_STATUS_ERROR_INVALID;
1725 if (handler->pd.type != _PD_TYPE_SCRIPT && handler->pd.type != _PD_TYPE_BUFFER) {
1726 ErrPrint("Handler is not valid type\n");
1727 return LB_STATUS_ERROR_INVALID;
1730 packet = packet_create_noack("pd_release_pixmap", "ssi", handler->pkgname, handler->id, pixmap);
1732 ErrPrint("Failed to build a param\n");
1733 return LB_STATUS_ERROR_FAULT;
1736 return master_rpc_request_only(handler, packet);
1739 EAPI void *livebox_acquire_fb(struct livebox *handler)
1742 ErrPrint("Handler is NIL\n");
1746 if (handler->state != CREATE || !handler->id) {
1747 ErrPrint("Invalid handle\n");
1751 if (handler->lb.type != _LB_TYPE_SCRIPT && handler->lb.type != _LB_TYPE_BUFFER) {
1752 ErrPrint("Handler is not valid type\n");
1756 return fb_acquire_buffer(handler->lb.data.fb);
1759 EAPI int livebox_release_fb(void *buffer)
1761 return fb_release_buffer(buffer);
1764 EAPI int livebox_fb_refcnt(void *buffer)
1766 return fb_refcnt(buffer);
1769 EAPI void *livebox_acquire_pdfb(struct livebox *handler)
1772 ErrPrint("Handler is NIL\n");
1776 if (handler->state != CREATE || !handler->id) {
1777 ErrPrint("Invalid handler\n");
1781 if (handler->pd.type != _PD_TYPE_SCRIPT && handler->pd.type != _PD_TYPE_BUFFER) {
1782 ErrPrint("Handler is not valid type\n");
1786 return fb_acquire_buffer(handler->pd.data.fb);
1789 EAPI int livebox_release_pdfb(void *buffer)
1791 return fb_release_buffer(buffer);
1794 EAPI int livebox_pdfb_refcnt(void *buffer)
1796 return fb_refcnt(buffer);
1799 EAPI int livebox_pdfb_bufsz(struct livebox *handler)
1802 ErrPrint("Handler is NIL\n");
1803 return LB_STATUS_ERROR_INVALID;
1806 if (handler->state != CREATE || !handler->id) {
1807 ErrPrint("Handler is not valid\n");
1808 return LB_STATUS_ERROR_INVALID;
1811 return fb_size(handler->pd.data.fb);
1814 EAPI int livebox_lbfb_bufsz(struct livebox *handler)
1817 ErrPrint("Handler is NIL\n");
1818 return LB_STATUS_ERROR_INVALID;
1821 if (handler->state != CREATE || !handler->id) {
1822 ErrPrint("Handler is not valid\n");
1823 return LB_STATUS_ERROR_INVALID;
1826 return fb_size(handler->lb.data.fb);
1829 EAPI int livebox_is_user(struct livebox *handler)
1832 ErrPrint("Handler is NIL\n");
1833 return LB_STATUS_ERROR_INVALID;
1836 if (handler->state != CREATE) {
1837 ErrPrint("Handler is invalid\n");
1838 return LB_STATUS_ERROR_INVALID;
1841 return handler->is_user;
1844 EAPI int livebox_set_pinup(struct livebox *handler, int flag, ret_cb_t cb, void *data)
1846 struct packet *packet;
1849 ErrPrint("Handler is NIL\n");
1850 return LB_STATUS_ERROR_INVALID;
1853 if (handler->state != CREATE || !handler->id) {
1854 ErrPrint("Handler is not valid\n");
1855 return LB_STATUS_ERROR_INVALID;
1858 if (handler->pinup_cb) {
1859 ErrPrint("Previous pinup request is not finished\n");
1860 return LB_STATUS_ERROR_BUSY;
1863 if (handler->is_pinned_up == flag) {
1864 DbgPrint("No changes\n");
1865 return LB_STATUS_ERROR_ALREADY;
1868 packet = packet_create("pinup_changed", "ssi", handler->pkgname, handler->id, flag);
1870 ErrPrint("Failed to build a param\n");
1871 return LB_STATUS_ERROR_FAULT;
1875 cb = default_pinup_cb;
1877 return master_rpc_async_request(handler, packet, 0, pinup_done_cb, create_cb_info(cb, data));
1880 EAPI int livebox_is_pinned_up(struct livebox *handler)
1883 ErrPrint("Handler is NIL\n");
1884 return LB_STATUS_ERROR_INVALID;
1887 if (handler->state != CREATE || !handler->id)
1888 return LB_STATUS_ERROR_INVALID;
1890 return handler->is_pinned_up;
1893 EAPI int livebox_has_pinup(struct livebox *handler)
1896 ErrPrint("Handler is NIL\n");
1897 return LB_STATUS_ERROR_INVALID;
1900 if (handler->state != CREATE || !handler->id)
1901 return LB_STATUS_ERROR_INVALID;
1903 return handler->lb.pinup_supported;
1906 EAPI int livebox_set_data(struct livebox *handler, void *data)
1909 ErrPrint("Handler is NIL\n");
1910 return LB_STATUS_ERROR_INVALID;
1913 if (handler->state != CREATE)
1914 return LB_STATUS_ERROR_INVALID;
1916 handler->data = data;
1917 return LB_STATUS_SUCCESS;
1920 EAPI void *livebox_get_data(struct livebox *handler)
1923 ErrPrint("Handler is NIL\n");
1927 if (handler->state != CREATE)
1930 return handler->data;
1933 EAPI int livebox_is_exists(const char *pkgname)
1937 lb = lb_pkgname(pkgname);
1946 EAPI const char *livebox_content(struct livebox *handler)
1949 ErrPrint("Handler is NIL\n");
1953 if (handler->state != CREATE)
1956 return handler->content;
1959 EAPI const char *livebox_category_title(struct livebox *handler)
1962 ErrPrint("Handler is NIL\n");
1966 if (handler->state != CREATE)
1969 return handler->title;
1972 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)
1974 struct packet *packet;
1977 ErrPrint("Handler is NIL\n");
1978 return LB_STATUS_ERROR_INVALID;
1981 if ((handler->lb.type != _LB_TYPE_TEXT && handler->pd.type != _PD_TYPE_TEXT) || handler->state != CREATE || !handler->id) {
1982 ErrPrint("Handler is not valid\n");
1983 return LB_STATUS_ERROR_INVALID;
1992 packet = packet_create("text_signal", "ssssdddd",
1993 handler->pkgname, handler->id, emission, source, sx, sy, ex, ey);
1995 ErrPrint("Failed to build a param\n");
1996 return LB_STATUS_ERROR_FAULT;
1999 return master_rpc_async_request(handler, packet, 0, text_signal_cb, create_cb_info(cb, data));
2002 EAPI int livebox_subscribe_group(const char *cluster, const char *category)
2004 struct packet *packet;
2008 * Validate the group info using DB
2009 * If the group info is not valid, do not send this request
2012 packet = packet_create_noack("subscribe", "ss", cluster ? cluster : "", category ? category : "");
2014 ErrPrint("Failed to create a packet\n");
2015 return LB_STATUS_ERROR_FAULT;
2018 return master_rpc_request_only(NULL, packet);
2021 EAPI int livebox_unsubscribe_group(const char *cluster, const char *category)
2023 struct packet *packet;
2027 * Validate the group info using DB
2028 * If the group info is not valid, do not send this request
2029 * AND Check the subscribed or not too
2032 packet = packet_create_noack("unsubscribe", "ss", cluster ? cluster : "", category ? category : "");
2034 ErrPrint("Failed to create a packet\n");
2035 return LB_STATUS_ERROR_FAULT;
2038 return master_rpc_request_only(NULL, packet);
2041 EAPI int livebox_refresh(struct livebox *handler)
2043 struct packet *packet;
2046 ErrPrint("Hnalder is NIL\n");
2047 return LB_STATUS_ERROR_INVALID;
2050 if (handler->state != CREATE || !handler->id)
2051 return LB_STATUS_ERROR_INVALID;
2053 packet = packet_create_noack("update", "ss", handler->pkgname, handler->id);
2055 ErrPrint("Failed to create a packet\n");
2056 return LB_STATUS_ERROR_FAULT;
2059 return master_rpc_request_only(handler, packet);
2062 EAPI int livebox_refresh_group(const char *cluster, const char *category)
2064 struct packet *packet;
2066 if (!cluster || !category) {
2067 ErrPrint("Invalid argument\n");
2068 return LB_STATUS_ERROR_INVALID;
2071 packet = packet_create_noack("refresh_group", "ss", cluster, category);
2073 ErrPrint("Failed to create a packet\n");
2074 return LB_STATUS_ERROR_FAULT;
2077 return master_rpc_request_only(NULL, packet);
2080 EAPI int livebox_set_visibility(struct livebox *handler, enum livebox_visible_state state)
2082 struct packet *packet;
2086 ErrPrint("Handler is NIL\n");
2087 return LB_STATUS_ERROR_INVALID;
2090 if (handler->state != CREATE || !handler->id)
2091 return LB_STATUS_ERROR_INVALID;
2093 if (!handler->is_user) {
2094 /* System cluster livebox cannot be changed its visible states */
2095 if (state == LB_HIDE_WITH_PAUSE) {
2096 ErrPrint("CA Livebox is not able to change the visibility\n");
2097 return LB_STATUS_ERROR_PERMISSION;
2101 DbgPrint("Change the visibility %d <> %d, %s\n", handler->visible, state, handler->id);
2103 if (handler->visible == state)
2104 return LB_STATUS_ERROR_ALREADY;
2106 packet = packet_create_noack("change,visibility", "ssi", handler->pkgname, handler->id, (int)state);
2108 ErrPrint("Failed to create a packet\n");
2109 return LB_STATUS_ERROR_FAULT;
2112 ret = master_rpc_request_only(handler, packet);
2114 handler->visible = state;
2119 EAPI enum livebox_visible_state livebox_visibility(struct livebox *handler)
2122 ErrPrint("Handler is NIL\n");
2123 return LB_VISIBLE_ERROR;
2126 if (handler->state != CREATE || !handler->id)
2127 return LB_VISIBLE_ERROR;
2129 return handler->visible;
2132 int lb_set_group(struct livebox *handler, const char *cluster, const char *category)
2138 pc = strdup(cluster);
2140 CRITICAL_LOG("Heap: %s (cluster: %s)\n", strerror(errno), cluster);
2141 return LB_STATUS_ERROR_MEMORY;
2146 ps = strdup(category);
2148 CRITICAL_LOG("Heap: %s (category: %s)\n", strerror(errno), category);
2150 return LB_STATUS_ERROR_MEMORY;
2154 if (handler->cluster)
2155 free(handler->cluster);
2157 if (handler->category)
2158 free(handler->category);
2160 handler->cluster = pc;
2161 handler->category = ps;
2163 return LB_STATUS_SUCCESS;
2166 void lb_set_size(struct livebox *handler, int w, int h)
2168 handler->lb.width = w;
2169 handler->lb.height = h;
2172 void lb_set_pdsize(struct livebox *handler, int w, int h)
2174 handler->pd.width = w;
2175 handler->pd.height = h;
2178 void lb_set_default_pdsize(struct livebox *handler, int w, int h)
2180 handler->pd.default_width = w;
2181 handler->pd.default_height = h;
2184 void lb_invoke_fault_handler(enum livebox_fault_type event, const char *pkgname, const char *file, const char *func)
2188 struct fault_info *info;
2190 dlist_foreach_safe(s_info.fault_list, l, n, info) {
2191 if (info->handler(event, pkgname, file, func, info->user_data) == EXIT_FAILURE)
2192 s_info.fault_list = dlist_remove(s_info.fault_list, l);
2196 void lb_invoke_event_handler(struct livebox *handler, enum livebox_event_type event)
2200 struct event_info *info;
2202 dlist_foreach_safe(s_info.event_list, l, n, info) {
2203 if (info->handler(handler, event, info->user_data) == EXIT_FAILURE)
2204 s_info.event_list = dlist_remove(s_info.event_list, l);
2208 struct livebox *lb_find_livebox(const char *pkgname, const char *id)
2211 struct livebox *handler;
2213 dlist_foreach(s_info.livebox_list, l, handler) {
2217 if (!strcmp(handler->pkgname, pkgname) && !strcmp(handler->id, id))
2224 struct livebox *lb_find_livebox_by_timestamp(double timestamp)
2227 struct livebox *handler;
2229 dlist_foreach(s_info.livebox_list, l, handler) {
2230 if (handler->timestamp == timestamp)
2237 static inline char *get_file_kept_in_safe(const char *id)
2244 path = util_uri_to_path(id);
2246 ErrPrint("Invalid URI(%s)\n", id);
2253 if (s_info.prevent_overwrite) {
2254 new_path = strdup(path);
2256 ErrPrint("Heap: %s\n", strerror(errno));
2265 while (base_idx > 0 && path[base_idx] != '/') base_idx--;
2266 base_idx += (path[base_idx] == '/');
2268 new_path = malloc(len + 10);
2270 ErrPrint("Heap: %s\n", strerror(errno));
2274 strncpy(new_path, path, base_idx);
2275 snprintf(new_path + base_idx, len + 10 - base_idx, "reader/%s", path + base_idx);
2279 struct livebox *lb_new_livebox(const char *pkgname, const char *id, double timestamp)
2281 struct livebox *handler;
2283 handler = calloc(1, sizeof(*handler));
2285 ErrPrint("Failed to create a new livebox\n");
2289 handler->pkgname = strdup(pkgname);
2290 if (!handler->pkgname) {
2291 ErrPrint("%s\n", strerror(errno));
2296 handler->id = strdup(id);
2298 ErrPrint("%s\n", strerror(errno));
2299 free(handler->pkgname);
2304 handler->filename = get_file_kept_in_safe(id);
2305 if (!handler->filename) {
2306 handler->filename = strdup(util_uri_to_path(id));
2307 if (!handler->filename)
2308 ErrPrint("Error: %s\n", strerror(errno));
2311 handler->timestamp = timestamp;
2312 handler->lb.type = _LB_TYPE_FILE;
2313 handler->pd.type = _PD_TYPE_SCRIPT;
2314 handler->state = CREATE;
2315 handler->visible = LB_SHOW;
2317 s_info.livebox_list = dlist_append(s_info.livebox_list, handler);
2322 int lb_delete_all(void)
2326 struct livebox *handler;
2328 dlist_foreach_safe(s_info.livebox_list, l, n, handler) {
2329 lb_invoke_event_handler(handler, LB_EVENT_DELETED);
2333 return LB_STATUS_SUCCESS;
2336 int lb_set_content(struct livebox *handler, const char *content)
2338 if (handler->content) {
2339 free(handler->content);
2340 handler->content = NULL;
2344 handler->content = strdup(content);
2345 if (!handler->content) {
2346 CRITICAL_LOG("Heap: %s (content: %s)\n", strerror(errno), content);
2347 return LB_STATUS_ERROR_MEMORY;
2351 return LB_STATUS_SUCCESS;
2354 int lb_set_title(struct livebox *handler, const char *title)
2356 if (handler->title) {
2357 free(handler->title);
2358 handler->title = NULL;
2362 handler->title = strdup(title);
2363 if (!handler->title) {
2364 CRITICAL_LOG("Heap: %s (title: %s)\n", strerror(errno), title);
2365 return LB_STATUS_ERROR_MEMORY;
2369 return LB_STATUS_SUCCESS;
2372 void lb_set_size_list(struct livebox *handler, int size_list)
2374 handler->lb.size_list = size_list;
2377 void lb_set_auto_launch(struct livebox *handler, const char *auto_launch)
2379 if (!strlen(auto_launch))
2382 handler->lb.auto_launch = strdup(auto_launch);
2383 if (!handler->lb.auto_launch)
2384 ErrPrint("Heap: %s\n", strerror(errno));
2387 void lb_set_priority(struct livebox *handler, double priority)
2389 handler->lb.priority = priority;
2392 void lb_set_id(struct livebox *handler, const char *id)
2397 handler->id = strdup(id);
2399 ErrPrint("Error: %s\n", strerror(errno));
2401 if (handler->filename)
2402 free(handler->filename);
2404 handler->filename = get_file_kept_in_safe(id);
2405 if (!handler->filename) {
2406 handler->filename = strdup(util_uri_to_path(id));
2407 if (!handler->filename)
2408 ErrPrint("Error: %s\n", strerror(errno));
2412 int lb_set_lb_fb(struct livebox *handler, const char *filename)
2417 return LB_STATUS_ERROR_INVALID;
2419 fb = handler->lb.data.fb;
2420 if (fb && !strcmp(fb_id(fb), filename)) /*!< BUFFER is not changed, */
2421 return LB_STATUS_SUCCESS;
2423 handler->lb.data.fb = NULL;
2425 if (!filename || filename[0] == '\0') {
2428 return LB_STATUS_SUCCESS;
2431 handler->lb.data.fb = fb_create(filename, handler->lb.width, handler->lb.height);
2432 if (!handler->lb.data.fb) {
2433 ErrPrint("Faield to create a FB\n");
2436 return LB_STATUS_ERROR_FAULT;
2442 return LB_STATUS_SUCCESS;
2445 int lb_set_pd_fb(struct livebox *handler, const char *filename)
2450 return LB_STATUS_ERROR_INVALID;
2452 fb = handler->pd.data.fb;
2453 if (fb && !strcmp(fb_id(fb), filename)) {
2454 /* BUFFER is not changed, just update the content */
2455 return LB_STATUS_ERROR_EXIST;
2457 handler->pd.data.fb = NULL;
2459 if (!filename || filename[0] == '\0') {
2462 return LB_STATUS_SUCCESS;
2465 handler->pd.data.fb = fb_create(filename, handler->pd.width, handler->pd.height);
2466 if (!handler->pd.data.fb) {
2467 ErrPrint("Failed to create a FB\n");
2470 return LB_STATUS_ERROR_FAULT;
2475 return LB_STATUS_SUCCESS;
2478 struct fb_info *lb_get_lb_fb(struct livebox *handler)
2480 return handler->lb.data.fb;
2483 struct fb_info *lb_get_pd_fb(struct livebox *handler)
2485 return handler->pd.data.fb;
2488 void lb_set_user(struct livebox *handler, int user)
2490 handler->is_user = user;
2493 void lb_set_pinup(struct livebox *handler, int pinup_supported)
2495 handler->lb.pinup_supported = pinup_supported;
2498 void lb_set_text_lb(struct livebox *handler)
2500 handler->lb.type = _LB_TYPE_TEXT;
2503 void lb_set_text_pd(struct livebox *handler)
2505 handler->pd.type = _PD_TYPE_TEXT;
2508 int lb_text_lb(struct livebox *handler)
2510 return handler->lb.type == _LB_TYPE_TEXT;
2513 int lb_text_pd(struct livebox *handler)
2515 return handler->pd.type == _PD_TYPE_TEXT;
2518 void lb_set_period(struct livebox *handler, double period)
2520 handler->lb.period = period;
2523 struct livebox *lb_ref(struct livebox *handler)
2532 struct livebox *lb_unref(struct livebox *handler)
2538 if (handler->refcnt > 0)
2541 if (handler->filename)
2542 util_unlink(handler->filename);
2544 dlist_remove_data(s_info.livebox_list, handler);
2546 handler->state = DESTROYED;
2547 free(handler->cluster);
2548 free(handler->category);
2550 free(handler->pkgname);
2551 free(handler->filename);
2552 free(handler->lb.auto_launch);
2554 if (handler->lb.data.fb) {
2555 fb_destroy(handler->lb.data.fb);
2556 handler->lb.data.fb = NULL;
2559 if (handler->pd.data.fb) {
2560 fb_destroy(handler->pd.data.fb);
2561 handler->pd.data.fb = NULL;
2568 int lb_send_delete(struct livebox *handler, ret_cb_t cb, void *data)
2570 struct packet *packet;
2572 if (!cb && !!data) {
2573 ErrPrint("Invalid argument\n");
2574 return LB_STATUS_ERROR_INVALID;
2577 if (handler->deleted_cb) {
2578 ErrPrint("Already in-progress\n");
2579 return LB_STATUS_ERROR_BUSY;
2582 packet = packet_create("delete", "ss", handler->pkgname, handler->id);
2584 ErrPrint("Failed to build a param\n");
2586 cb(handler, LB_STATUS_ERROR_FAULT, data);
2588 return LB_STATUS_ERROR_FAULT;
2592 cb = default_delete_cb;
2594 return master_rpc_async_request(handler, packet, 0, del_ret_cb, create_cb_info(cb, data));
2597 EAPI int livebox_client_paused(void)
2599 struct packet *packet;
2601 packet = packet_create_noack("client_paused", "d", util_timestamp());
2603 ErrPrint("Failed to create a pause packet\n");
2604 return LB_STATUS_ERROR_FAULT;
2607 return master_rpc_request_only(NULL, packet);
2610 EAPI int livebox_client_resumed(void)
2612 struct packet *packet;
2614 packet = packet_create_noack("client_resumed", "d", util_timestamp());
2616 ErrPrint("Failed to create a resume packet\n");
2617 return LB_STATUS_ERROR_FAULT;
2620 return master_rpc_request_only(NULL, packet);