2 * Copyright 2013 Samsung Electronics Co., Ltd
4 * Licensed under the Flora License, Version 1.1 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://floralicense.org/license/
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 #include <stdlib.h> /* exit */
20 #include <unistd.h> /* access */
28 #include <livebox-errno.h>
30 #include "critical_log.h"
33 #include "so_handler.h"
35 #include "update_monitor.h"
39 #define IS_LB_SHOWN(itm) (!(itm)->inst->item->has_livebox_script || ((itm)->inst->item->has_livebox_script && (itm)->is_lb_show))
43 #define UPDATE_ITEM_DELETED (-1)
44 #define UPDATE_INVOKED (-2)
45 #define UPDATE_NOT_INVOKED (0)
49 struct instance *inst;
53 double update_interval;
54 int heavy_updating; /* Only for debugging message */
55 int is_paused; /* 1 is paused, 0 is resumed */
58 unsigned int updated_in_pause;
68 Eina_List *pending_list;
69 Ecore_Timer *pending_timer;
70 Eina_List *pd_open_pending_list;
71 Ecore_Timer *pd_open_pending_timer;
75 int pending_timer_freezed;
80 .pending_timer = NULL,
81 .pd_open_pending_list = NULL,
82 .pd_open_pending_timer = NULL,
86 .pending_timer_freezed = 0,
89 static Eina_Bool updator_cb(void *data);
91 static void pending_timer_freeze(void)
93 DbgPrint("Freezed Count: %d\n", s_info.pending_timer_freezed);
94 if (s_info.pending_timer && !s_info.pending_timer_freezed) {
95 DbgPrint("Freeze the pending timer\n");
96 ecore_timer_freeze(s_info.pending_timer);
99 s_info.pending_timer_freezed++;
102 static void pending_timer_thaw(void)
104 DbgPrint("Freezed Count: %d\n", s_info.pending_timer_freezed);
105 if (!s_info.pending_timer_freezed) {
109 s_info.pending_timer_freezed--;
110 if (s_info.pending_timer && !s_info.pending_timer_freezed) {
111 DbgPrint("Thaw the pending timer\n");
112 ecore_timer_thaw(s_info.pending_timer);
117 * -1 : PD is opened, but not mine
118 * 0 : PD is not opened
119 * 1 : my PD is opened
121 static inline int pd_is_opened(const char *pkgname)
128 EINA_LIST_FOREACH(s_info.pd_list, l, tmp) {
129 if (pkgname && !strcmp(pkgname, tmp)) {
136 return i > 0 ? -1 : 0;
139 static Eina_Bool pd_open_pended_cmd_consumer_cb(void *data)
143 item = eina_list_nth(s_info.pd_open_pending_list, 0);
149 return ECORE_CALLBACK_RENEW;
152 s_info.pd_open_pending_list = eina_list_remove(s_info.pd_open_pending_list, item);
155 * To prevent from checking the is_updated function
157 if (updator_cb(item) == ECORE_CALLBACK_CANCEL) {
158 /* Item is destroyed */
161 if (s_info.pd_open_pending_list) {
162 return ECORE_CALLBACK_RENEW;
166 s_info.pd_open_pending_timer = NULL;
167 return ECORE_CALLBACK_CANCEL;
170 static Eina_Bool pended_cmd_consumer_cb(void *data)
174 item = eina_list_nth(s_info.pending_list, 0);
179 if (s_info.update || pd_is_opened(item->inst->item->pkgname) < 0) {
180 return ECORE_CALLBACK_RENEW;
183 s_info.pending_list = eina_list_remove(s_info.pending_list, item);
186 * To prevent from checking the is_updated function
188 if (updator_cb(item) == ECORE_CALLBACK_CANCEL) {
189 /* item is destroyed */
192 if (s_info.pending_list) {
193 return ECORE_CALLBACK_RENEW;
197 s_info.pending_timer = NULL;
198 s_info.pending_timer_freezed = 0;
199 return ECORE_CALLBACK_CANCEL;
202 static inline __attribute__((always_inline)) int activate_pending_consumer(void)
204 if (s_info.pending_timer) {
208 s_info.pending_timer = ecore_timer_add(0.000001f, pended_cmd_consumer_cb, NULL);
209 if (!s_info.pending_timer) {
210 ErrPrint("Failed to add a new pended command consumer\n");
211 return LB_STATUS_ERROR_FAULT;
215 * Do not increase the freezed counter.
216 * Just freeze the timer.
218 if (s_info.pending_timer_freezed) {
219 ecore_timer_freeze(s_info.pending_timer);
225 static inline void deactivate_pending_consumer(void)
227 if (!s_info.pending_timer) {
231 ecore_timer_del(s_info.pending_timer);
232 s_info.pending_timer = NULL;
233 s_info.pending_timer_freezed = 0;
236 static inline void deactivate_pd_open_pending_consumer(void)
238 if (!s_info.pd_open_pending_timer) {
242 ecore_timer_del(s_info.pd_open_pending_timer);
243 s_info.pd_open_pending_timer = NULL;
246 static inline int __attribute__((always_inline)) activate_pd_open_pending_consumer(void)
248 if (s_info.pd_open_pending_timer) {
252 s_info.pd_open_pending_timer = ecore_timer_add(0.000001f, pd_open_pended_cmd_consumer_cb, NULL);
253 if (!s_info.pd_open_pending_timer) {
254 ErrPrint("Failed to add a new pended command consumer\n");
255 return LB_STATUS_ERROR_FAULT;
261 static inline void migrate_to_pd_open_pending_list(const char *pkgname)
268 EINA_LIST_FOREACH_SAFE(s_info.pending_list, l, n, item) {
269 if (strcmp(pkgname, item->inst->item->pkgname)) {
273 s_info.pending_list = eina_list_remove(s_info.pending_list, item);
274 s_info.pd_open_pending_list = eina_list_append(s_info.pd_open_pending_list, item);
278 if (s_info.pd_open_pending_list) {
279 activate_pd_open_pending_consumer();
282 if (!s_info.pending_list) {
283 deactivate_pending_consumer();
287 static inline void migrate_to_pending_list(const char *pkgname)
294 EINA_LIST_FOREACH_SAFE(s_info.pd_open_pending_list, l, n, item) {
295 if (strcmp(pkgname, item->inst->item->pkgname)) {
299 s_info.pd_open_pending_list = eina_list_remove(s_info.pd_open_pending_list, item);
300 s_info.pending_list = eina_list_append(s_info.pending_list, item);
304 if (s_info.pending_list) {
305 activate_pending_consumer();
308 if (!s_info.pd_open_pending_list) {
309 deactivate_pd_open_pending_consumer();
313 static inline int is_pended_item(struct item *item)
315 struct item *in_item;
316 if (pd_is_opened(item->inst->item->pkgname) == 1) {
317 in_item = eina_list_data_find(s_info.pd_open_pending_list, item);
319 in_item = eina_list_data_find(s_info.pending_list, item);
322 return (in_item == item);
325 static int append_pending_list(struct item *item)
327 if (pd_is_opened(item->inst->item->pkgname) == 1) {
328 if (eina_list_data_find(s_info.pd_open_pending_list, item) == item) {
329 DbgPrint("Already pended - %s\n", item->inst->item->pkgname);
330 return LB_STATUS_ERROR_EXIST;
333 if (activate_pd_open_pending_consumer() < 0) {
334 ErrPrint("Failed to activate PD open pending consumer\n");
335 return LB_STATUS_ERROR_FAULT;
338 s_info.pd_open_pending_list = eina_list_append(s_info.pd_open_pending_list, item);
340 if (eina_list_data_find(s_info.pending_list, item) == item) {
341 DbgPrint("Already pended - %s\n", item->inst->item->pkgname);
342 return LB_STATUS_ERROR_EXIST;
345 if (activate_pending_consumer() < 0) {
346 return LB_STATUS_ERROR_FAULT;
349 s_info.pending_list = eina_list_append(s_info.pending_list, item);
356 * This function can call the update callback
357 * return 0 if there is no changes
358 * return -1 the item is deleted
360 static inline int timer_thaw(struct item *item)
371 ecore_timer_thaw(item->timer);
372 period = ecore_timer_interval_get(item->timer);
373 pending = ecore_timer_pending_get(item->timer);
374 delay = util_time_delay_for_compensation(period) - pending;
375 ecore_timer_delay(item->timer, delay);
377 if (item->sleep_at == 0.0f) {
381 sleep_time = util_timestamp() - item->sleep_at;
382 item->sleep_at = 0.0f;
384 if (sleep_time > pending) {
385 if (updator_cb(item) == ECORE_CALLBACK_CANCEL) {
386 /* item is destroyed */
387 return UPDATE_ITEM_DELETED;
389 return UPDATE_INVOKED;
393 return UPDATE_NOT_INVOKED;
396 static void timer_freeze(struct item *item)
404 ecore_timer_freeze(item->timer);
406 if (ecore_timer_interval_get(item->timer) <= 1.0f) {
410 if (gettimeofday(&tv, NULL) < 0) {
411 ErrPrint("gettimeofday: %s\n", strerror(errno));
416 item->sleep_at = (double)tv.tv_sec + (double)tv.tv_usec / 1000000.0f;
419 static inline void update_monitor_cnt(struct item *item)
424 now = util_timestamp();
425 interval = now - item->update_interval;
429 * If the content update is processed in too short time,
430 * don't increase the monitor counter, instead of it
431 * set the heavy updating flag.
432 * And handling this heavy updating from the
433 * file update callback.
435 if (interval >= MINIMUM_UPDATE_INTERVAL) {
436 if (s_info.update == item) {
439 * If already in updating mode,
440 * reset the monitor_cnt to 1,
441 * all updated event will be merged into A inotify event
443 DbgPrint("While waiting updated event, content is updated [%s]\n", item->inst->id);
444 item->monitor_cnt = 1;
449 item->heavy_updating = 1;
452 item->update_interval = now;
455 static inline Eina_List *find_item(struct instance *inst)
460 EINA_LIST_FOREACH(s_info.item_list, l, item) {
461 if (item->inst == inst) {
469 static inline int output_handler(struct item *item)
474 if (item->monitor_cnt < 0 || item->heavy_updating) {
475 if (!item->heavy_updating) {
476 WarnPrint("%s has invalid monitor_cnt\n", item->inst->id);
479 item->heavy_updating = 0; /* Reset flag */
482 item->monitor_cnt = 0;
485 if (item->monitor_cnt == 0) {
487 fault_unmark_call(item->inst->item->pkgname, item->inst->id, "update,crashed", NO_ALARM);
491 ecore_timer_del(item->monitor);
492 item->monitor = NULL;
495 if (s_info.update == item) {
496 s_info.update = NULL;
499 if (item->deleteme) {
500 provider_send_deleted(item->inst->item->pkgname, item->inst->id);
501 (void)so_destroy(item->inst);
510 static int desc_updated_cb(const char *filename, void *data, int over)
515 WarnPrint("Event Q overflow\n");
520 DbgPrint("DESC %s is updated\n", filename);
521 if (item->is_pd_show) {
522 provider_send_desc_updated(item->inst->item->pkgname, item->inst->id, filename);
524 ErrPrint("But PD is not opened, Ignore this update (%s)\n", item->inst->id);
529 static int file_updated_cb(const char *filename, void *data, int over)
535 char *content = NULL;
540 WarnPrint("Event Q overflow\n");
545 ret = util_get_filesize(filename);
547 ErrPrint("Content is updated. but invalid. ret = %d (Update is ignored)\n", ret);
548 return EXIT_SUCCESS; /*!< To keep the callback */
551 ret = so_get_output_info(item->inst, &w, &h, &priority, &content, &title);
553 ErrPrint("livebox_get_info returns %d\n", ret);
554 return EXIT_SUCCESS; /*!< To keep the callback */
557 if (IS_LB_SHOWN(item)) {
558 provider_send_updated(item->inst->item->pkgname, item->inst->id,
559 item->inst->w, item->inst->h, item->inst->priority,
562 item->is_lb_updated++;
565 return output_handler(item);
568 static void reset_lb_updated_flag(struct item *item)
570 if (!item->is_lb_updated) {
574 DbgPrint("[%s] Updated %d times, (content: %s), (title: %s)\n",
575 item->inst->id, item->is_lb_updated,
576 item->inst->content, item->inst->title);
578 provider_send_updated(item->inst->item->pkgname, item->inst->id,
579 item->inst->w, item->inst->h, item->inst->priority,
580 item->inst->content, item->inst->title);
582 item->is_lb_updated = 0;
585 static inline int clear_from_pd_open_pending_list(struct item *item)
590 EINA_LIST_FOREACH(s_info.pd_open_pending_list, l, tmp) {
595 s_info.pd_open_pending_list = eina_list_remove_list(s_info.pd_open_pending_list, l);
596 if (!s_info.pd_open_pending_list) {
597 deactivate_pd_open_pending_consumer();
599 return LB_STATUS_SUCCESS;
602 return LB_STATUS_ERROR_NOT_EXIST;
605 static inline int clear_from_pending_list(struct item *item)
610 EINA_LIST_FOREACH(s_info.pending_list, l, tmp) {
615 s_info.pending_list = eina_list_remove_list(s_info.pending_list, l);
616 if (!s_info.pending_list) {
617 deactivate_pending_consumer();
619 return LB_STATUS_SUCCESS;
622 return LB_STATUS_ERROR_NOT_EXIST;
625 static Eina_Bool update_timeout_cb(void *data)
631 ErrPrint("UPDATE TIMEOUT ========> %s - %s\n", item->inst->item->pkgname, item->inst->id);
633 if (s_info.update != item) {
634 ErrPrint("Updating item is not matched\n");
637 fault_unmark_call(item->inst->item->pkgname, item->inst->id, "update,crashed", NO_ALARM);
638 fault_mark_call(item->inst->item->pkgname, item->inst->id, "update,timeout", NO_ALARM, DEFAULT_LIFE_TIMER);
639 s_info.update = NULL;
642 return ECORE_CALLBACK_CANCEL;
647 * This must has to return ECORE_CALLBACK_CANCEL, only if the item is deleted.
648 * So every caller, should manage the deleted item correctly.
650 static Eina_Bool updator_cb(void *data)
657 if (item->monitor) { /*!< If this item is already in update process */
658 return ECORE_CALLBACK_RENEW;
661 if (!IS_LB_SHOWN(item)) {
662 DbgPrint("%s is not shown yet. make delay for updates\n", item->inst->item->pkgname);
663 (void)append_pending_list(item);
664 return ECORE_CALLBACK_RENEW;
667 if (item->is_paused) {
668 item->updated_in_pause++;
669 DbgPrint("%s is paused[%d]. make delay for updating\n", item->inst->item->pkgname, item->updated_in_pause);
670 return ECORE_CALLBACK_RENEW;
673 item->updated_in_pause = 0;
675 ret = so_is_updated(item->inst);
677 if (so_need_to_destroy(item->inst) == NEED_TO_DESTROY) {
678 provider_send_deleted(item->inst->item->pkgname, item->inst->id);
679 lb_destroy(item->inst->item->pkgname, item->inst->id);
682 * Every caller of this, must not access the item from now.
684 return ECORE_CALLBACK_CANCEL;
687 reset_lb_updated_flag(item);
688 return ECORE_CALLBACK_RENEW;
691 if (s_info.update || pd_is_opened(item->inst->item->pkgname) < 0) {
692 DbgPrint("%s is busy\n", s_info.update ? s_info.update->inst->id : item->inst->id);
693 (void)append_pending_list(item);
694 return ECORE_CALLBACK_RENEW;
697 item->monitor = ecore_timer_add(item->inst->item->timeout, update_timeout_cb, item);
698 if (!item->monitor) {
699 ErrPrint("Failed to add update monitor %s(%s):%d\n",
700 item->inst->item->pkgname, item->inst->id, item->inst->item->timeout);
701 return ECORE_CALLBACK_RENEW;
704 ret = so_update(item->inst);
706 ecore_timer_del(item->monitor);
707 item->monitor = NULL;
708 reset_lb_updated_flag(item);
709 return ECORE_CALLBACK_RENEW;
714 * Counter of the event monitor is only used for asynchronous content updating,
715 * So reset it to 1 from here because the async updating is started now,
716 * even if it is accumulated by other event function before this.
718 item->monitor_cnt = 1;
722 * While waiting the Callback function call,
723 * Add this for finding the crash
725 fault_mark_call(item->inst->item->pkgname, item->inst->id, "update,crashed", NO_ALARM, DEFAULT_LIFE_TIMER);
727 if (ret & NEED_TO_SCHEDULE) {
728 (void)append_pending_list(item);
731 if (ret & OUTPUT_UPDATED) {
734 * In this case, there is potential issue
735 * 1. User added update CALLBACK -> Inotify event (Only once)
736 * > We have to detect this case. Is it possible to be a user callback called faster than inotify event handler?
737 * 2. Inotify event -> User added update CALLBACK -> Inotify event
738 * > Okay. What we want is this.
740 update_monitor_cnt(item);
745 * This should be updated after "update_monitor_cnt" function call,
746 * because the update_monitor_cnt function will see the s_info.update variable,
748 s_info.update = item;
750 return ECORE_CALLBACK_RENEW;
753 static inline void update_monitor_del(const char *id, struct item *item)
758 update_monitor_del_update_cb(util_uri_to_path(id), file_updated_cb);
760 len = strlen(util_uri_to_path(id)) + strlen(".desc") + 1;
763 ErrPrint("Heap: %s (%s.desc)\n", strerror(errno), util_uri_to_path(id));
767 snprintf(tmp, len, "%s.desc", util_uri_to_path(id));
768 update_monitor_del_update_cb(tmp, desc_updated_cb);
772 static inline int add_desc_update_monitor(const char *id, struct item *item)
778 len = strlen(util_uri_to_path(id)) + strlen(".desc") + 1;
779 filename = malloc(len);
781 ErrPrint("Heap: %s (%s.desc)\n", strerror(errno), util_uri_to_path(id));
782 return LB_STATUS_ERROR_MEMORY;
785 snprintf(filename, len, "%s.desc", util_uri_to_path(id));
786 ret = update_monitor_add_update_cb(filename, desc_updated_cb, item);
791 static inline int add_file_update_monitor(const char *id, struct item *item)
793 return update_monitor_add_update_cb(util_uri_to_path(id), file_updated_cb, item);
796 static inline int update_monitor_add(const char *id, struct item *item)
800 * item->inst is not available yet.
802 add_file_update_monitor(id, item);
803 add_desc_update_monitor(id, item);
804 return LB_STATUS_SUCCESS;
807 HAPI int lb_init(void)
809 return LB_STATUS_SUCCESS;
812 HAPI int lb_fini(void)
818 deactivate_pending_consumer();
819 deactivate_pd_open_pending_consumer();
821 EINA_LIST_FREE(s_info.pd_open_pending_list, item);
822 EINA_LIST_FREE(s_info.pending_list, item);
824 EINA_LIST_FOREACH_SAFE(s_info.item_list, l, n, item) {
825 provider_send_deleted(item->inst->item->pkgname, item->inst->id);
826 lb_destroy(item->inst->item->pkgname, item->inst->id);
829 return LB_STATUS_SUCCESS;
834 * Exported API for each liveboxes.
836 const char *livebox_find_pkgname(const char *filename)
841 EINA_LIST_FOREACH(s_info.item_list, l, item) {
842 if (!strcmp(item->inst->id, filename)) {
843 return item->inst->item->pkgname;
850 int livebox_request_update_by_id(const char *filename)
855 if (so_current_op() != LIVEBOX_OP_UNKNOWN) {
856 ErrPrint("Current operation: %d\n", so_current_op());
857 return LB_STATUS_ERROR_INVALID;
860 EINA_LIST_FOREACH(s_info.item_list, l, item) {
861 if (!strcmp(item->inst->id, filename)) {
862 return append_pending_list(item);
866 return LB_STATUS_ERROR_NOT_EXIST;
869 int livebox_trigger_update_monitor(const char *filename, int is_pd)
874 if (so_current_op() != LIVEBOX_OP_UNKNOWN) {
875 ErrPrint("Current operation: %d\n", so_current_op());
876 return LB_STATUS_ERROR_INVALID;
881 len = strlen(filename) + strlen(".desc");
883 fname = malloc(len + 1);
885 ErrPrint("Heap: %s\n", strerror(errno));
886 return LB_STATUS_ERROR_MEMORY;
889 snprintf(fname, len, "%s.desc", filename);
891 fname = strdup(filename);
893 ErrPrint("Heap: %s\n", strerror(errno));
894 return LB_STATUS_ERROR_MEMORY;
898 if (access(fname, R_OK | W_OK) != 0) {
899 ErrPrint("access: %s (%s)\n", fname, strerror(errno));
900 ret = LB_STATUS_ERROR_IO;
902 ret = update_monitor_trigger_update_cb(fname, 0);
909 HAPI int lb_open_pd(const char *pkgname)
914 EINA_LIST_FOREACH(s_info.pd_list, l, tmp) {
915 if (!strcmp(pkgname, tmp)) {
920 tmp = strdup(pkgname);
922 ErrPrint("Heap: %s\n", strerror(errno));
923 return LB_STATUS_ERROR_MEMORY;
926 if (!s_info.pd_list) {
927 pending_timer_freeze();
930 s_info.pd_list = eina_list_append(s_info.pd_list, tmp);
933 * Find all instances from the pending list.
934 * Move them to pd_open_pending_timer
936 migrate_to_pd_open_pending_list(pkgname);
937 return LB_STATUS_SUCCESS;
940 HAPI int lb_close_pd(const char *pkgname)
946 EINA_LIST_FOREACH_SAFE(s_info.pd_list, l, n, tmp) {
947 if (strcmp(tmp, pkgname)) {
951 s_info.pd_list = eina_list_remove(s_info.pd_list, tmp);
954 if (!s_info.pd_list) {
955 pending_timer_thaw();
959 * Move all items in pd_open_pending_list
962 migrate_to_pending_list(pkgname);
963 return LB_STATUS_SUCCESS;
966 return LB_STATUS_ERROR_NOT_EXIST;
969 HAPI int lb_create(const char *pkgname, const char *id, const char *content_info, int timeout, int has_livebox_script, double period, const char *cluster, const char *category, int *w, int *h, double *priority, int skip_need_to_create, const char *abi, char **out_content, char **out_title)
971 struct instance *inst;
981 inst = so_find_instance(pkgname, id);
983 DbgPrint("Instance is already exists [%s - %s] content[%s], cluster[%s], category[%s], abi[%s]\n", pkgname, id, content_info, cluster, category, abi);
984 return LB_STATUS_SUCCESS;
987 if (!skip_need_to_create) {
988 ret = so_create_needed(pkgname, cluster, category, abi);
989 if (ret != NEED_TO_CREATE) {
990 return LB_STATUS_ERROR_PERMISSION;
996 item = calloc(1, sizeof(*item));
998 ErrPrint("Heap: %s (%s - %s, content[%s], cluster[%s], category[%s], abi[%s])\n", strerror(errno), pkgname, id, content_info, cluster, category, abi);
999 return LB_STATUS_ERROR_MEMORY;
1002 ret = update_monitor_add(id, item);
1008 create_ret = so_create(pkgname, id, content_info, timeout, has_livebox_script, cluster, category, abi, &inst);
1009 if (create_ret < 0) {
1010 update_monitor_del(id, item);
1021 if (period > 0.0f && !s_info.secured) {
1022 item->timer = util_timer_add(period, updator_cb, item);
1024 ErrPrint("Failed to add timer (%s - %s, content[%s], cluster[%s], category[%s], abi[%s]\n", pkgname, id, content_info, cluster, category, abi);
1025 update_monitor_del(id, item);
1026 (void)so_destroy(inst);
1028 return LB_STATUS_ERROR_FAULT;
1031 if (s_info.paused) {
1035 DbgPrint("Local update timer is disabled: %lf (%d)\n", period, s_info.secured);
1039 s_info.item_list = eina_list_append(s_info.item_list, item);
1041 if (create_ret & NEED_TO_SCHEDULE) {
1042 DbgPrint("%s Returns NEED_TO_SCHEDULE\n", pkgname);
1043 (void)append_pending_list(item);
1046 if (create_ret & OUTPUT_UPDATED) {
1047 update_monitor_cnt(item);
1050 * To send a output info, get the info forcely.
1051 * but the output file monitor will do this again
1053 * This function will set the tmp_content and tmp_title
1054 * even if it has no updates on the content, title,
1055 * it will set them to NULL.
1057 if (so_get_output_info(inst, w, h, priority, out_content, out_title) == LB_STATUS_SUCCESS) {
1061 tmp = strdup(*out_content);
1063 ErrPrint("Memory: %s\n", strerror(errno));
1072 tmp = strdup(*out_title);
1074 ErrPrint("Memory: %s\n", strerror(errno));
1084 *priority = inst->priority;
1085 return need_to_create;
1088 HAPI int lb_destroy(const char *pkgname, const char *id)
1091 struct instance *inst;
1094 inst = so_find_instance(pkgname, id);
1096 ErrPrint("Instance %s - %s is not created\n", pkgname, id);
1097 return LB_STATUS_ERROR_INVALID;
1100 l = find_item(inst);
1102 ErrPrint("Instance is not found (%s - %s)\n", pkgname, id);
1103 return LB_STATUS_ERROR_NOT_EXIST;
1106 item = eina_list_data_get(l);
1107 s_info.item_list = eina_list_remove_list(s_info.item_list, l);
1109 if (s_info.update == item) {
1110 s_info.update = NULL;
1114 clear_from_pd_open_pending_list(item);
1115 clear_from_pending_list(item);
1116 ecore_timer_del(item->timer);
1119 if (item->monitor) {
1122 update_monitor_del(id, item);
1126 if (!item->monitor) {
1128 (void)so_destroy(inst);
1131 return LB_STATUS_SUCCESS;
1134 HAPI int lb_resize(const char *pkgname, const char *id, int w, int h)
1137 struct instance *inst;
1141 inst = so_find_instance(pkgname, id);
1143 ErrPrint("Instance %s - %s is not created (%dx%d)\n", pkgname, id, w, h);
1144 return LB_STATUS_ERROR_INVALID;
1147 l = find_item(inst);
1149 ErrPrint("Instance is not found (%s - %s, %dx%d)\n", pkgname, id, w, h);
1150 return LB_STATUS_ERROR_NOT_EXIST;
1153 item = eina_list_data_get(l);
1155 ret = so_resize(inst, w, h);
1160 if (ret & NEED_TO_SCHEDULE) {
1161 DbgPrint("%s Returns NEED_TO_SCHEDULE\n", pkgname);
1162 (void)append_pending_list(item);
1165 if (ret & OUTPUT_UPDATED) {
1166 update_monitor_cnt(item);
1169 return LB_STATUS_SUCCESS;
1172 HAPI char *lb_pinup(const char *pkgname, const char *id, int pinup)
1174 struct instance *inst;
1177 inst = so_find_instance(pkgname, id);
1179 ErrPrint("Instance %s - %s is not found (pinup[%d])\n", pkgname, id, pinup);
1183 ret = so_pinup(inst, pinup);
1187 HAPI int lb_set_period(const char *pkgname, const char *id, double period)
1190 struct instance *inst;
1193 inst = so_find_instance(pkgname, id);
1195 ErrPrint("Instance %s - %s is not found (period[%lf])\n", pkgname, id, period);
1196 return LB_STATUS_ERROR_INVALID;
1199 l = find_item(inst);
1201 ErrPrint("Instance is not found (%s - %s, period[%lf])\n", pkgname, id, period);
1202 return LB_STATUS_ERROR_NOT_EXIST;
1205 item = eina_list_data_get(l);
1207 if (period <= 0.0f) {
1209 ecore_timer_del(item->timer);
1214 util_timer_interval_set(item->timer, period);
1215 } else if (!s_info.secured) {
1216 item->timer = util_timer_add(period, updator_cb, item);
1218 ErrPrint("Failed to add timer (%s - %s)\n", pkgname, id);
1219 return LB_STATUS_ERROR_FAULT;
1222 if (s_info.paused) {
1228 return LB_STATUS_SUCCESS;
1231 HAPI int lb_clicked(const char *pkgname, const char *id, const char *event, double timestamp, double x, double y)
1234 struct instance *inst;
1238 inst = so_find_instance(pkgname, id);
1240 ErrPrint("Instance %s - %s is not exists (event[%s])\n", pkgname, id, event);
1241 return LB_STATUS_ERROR_INVALID;
1244 l = find_item(inst);
1246 ErrPrint("Instance is not found (%s - %s, event[%s])\n", pkgname, id, event);
1247 return LB_STATUS_ERROR_NOT_EXIST;
1250 item = eina_list_data_get(l);
1252 ret = so_clicked(inst, event, timestamp, x, y);
1257 if (ret & NEED_TO_SCHEDULE) {
1258 DbgPrint("%s Returns NEED_TO_SCHEDULE\n", pkgname);
1259 (void)append_pending_list(item);
1262 if (ret & OUTPUT_UPDATED) {
1263 update_monitor_cnt(item);
1266 return LB_STATUS_SUCCESS;
1269 HAPI int lb_script_event(const char *pkgname, const char *id, const char *emission, const char *source, struct event_info *event_info)
1272 struct instance *inst;
1276 inst = so_find_instance(pkgname, id);
1278 ErrPrint("Instance %s - %s is not exists (emission[%s], source[%s])\n", pkgname, id, emission, source);
1279 return LB_STATUS_ERROR_INVALID;
1282 l = find_item(inst);
1284 ErrPrint("Instance is not found (%s - %s, emissino[%s], source[%s])\n", pkgname, id, emission, source);
1285 return LB_STATUS_ERROR_NOT_EXIST;
1288 item = eina_list_data_get(l);
1290 if (emission && source && !strcmp(source, id)) {
1291 if (item->inst->item->has_livebox_script) {
1292 if (!strcmp(emission, "lb,show")) {
1293 item->is_lb_show = 1;
1295 if (item->is_lb_updated && !is_pended_item(item)) {
1296 reset_lb_updated_flag(item);
1299 source = util_uri_to_path(source);
1300 } else if (!strcmp(emission, "lb,hide")) {
1301 DbgPrint("Livebox(%s) script is hide now\n", id);
1302 item->is_lb_show = 0;
1304 source = util_uri_to_path(source);
1308 if (!strcmp(emission, "pd,show")) {
1309 item->is_pd_show = 1;
1310 source = util_uri_to_path(source);
1311 } else if (!strcmp(emission, "pd,hide")) {
1312 item->is_pd_show = 0;
1313 source = util_uri_to_path(source);
1317 ret = so_script_event(inst, emission, source, event_info);
1322 if (ret & NEED_TO_SCHEDULE) {
1323 DbgPrint("%s Returns NEED_TO_SCHEDULE\n", pkgname);
1324 (void)append_pending_list(item);
1327 if (ret & OUTPUT_UPDATED) {
1328 update_monitor_cnt(item);
1331 return LB_STATUS_SUCCESS;
1334 HAPI int lb_is_pinned_up(const char *pkgname, const char *id)
1337 struct instance *inst;
1340 inst = so_find_instance(pkgname, id);
1342 ErrPrint("Instance %s - %s is not created\n", pkgname, id);
1343 return LB_STATUS_ERROR_INVALID;
1346 l = find_item(inst);
1348 ErrPrint("Instance is not found(%s - %s)\n", pkgname, id);
1349 return LB_STATUS_ERROR_NOT_EXIST;
1352 item = eina_list_data_get(l);
1354 ErrPrint("Invalid item(%s - %s)\n", pkgname, id);
1355 return LB_STATUS_ERROR_FAULT;
1360 * Maybe this is not neccessary for this operation
1362 return so_is_pinned_up(inst);
1365 HAPI int lb_change_group(const char *pkgname, const char *id, const char *cluster, const char *category)
1368 struct instance *inst;
1372 inst = so_find_instance(pkgname, id);
1374 ErrPrint("Instance %s - %s is not created (cluster[%s], category[%s])\n", pkgname, id, cluster, category);
1375 return LB_STATUS_ERROR_INVALID;
1378 l = find_item(inst);
1380 ErrPrint("Instance is not found(%s - %s, cluster[%s], category[%s])\n", pkgname, id, cluster, category);
1381 return LB_STATUS_ERROR_NOT_EXIST;
1384 item = eina_list_data_get(l);
1386 ret = so_change_group(inst, cluster, category);
1391 if (ret & NEED_TO_SCHEDULE) {
1392 DbgPrint("%s Returns NEED_TO_SCHEDULE\n", pkgname);
1393 (void)append_pending_list(item);
1396 if (ret & OUTPUT_UPDATED) {
1397 update_monitor_cnt(item);
1400 return LB_STATUS_SUCCESS;
1403 static int lb_sys_event(struct instance *inst, struct item *item, int event)
1407 ret = so_sys_event(inst, event);
1412 if (ret & NEED_TO_SCHEDULE) {
1413 (void)append_pending_list(item);
1416 if (ret & OUTPUT_UPDATED) {
1417 update_monitor_cnt(item);
1420 return LB_STATUS_SUCCESS;
1423 HAPI int lb_system_event(const char *pkgname, const char *id, int event)
1426 struct instance *inst;
1429 inst = so_find_instance(pkgname, id);
1431 ErrPrint("instance %s - %s is not created\n");
1432 return LB_STATUS_ERROR_INVALID;
1435 l = find_item(inst);
1437 ErrPrint("Instance is not found(%s - %s)\n", pkgname, id);
1438 return LB_STATUS_ERROR_NOT_EXIST;
1441 item = eina_list_data_get(l);
1442 return lb_sys_event(inst, item, event);
1445 HAPI int lb_update(const char *pkgname, const char *id)
1448 struct instance *inst;
1451 inst = so_find_instance(pkgname, id);
1453 ErrPrint("Instance %s - %s is not created\n", pkgname, id);
1454 return LB_STATUS_ERROR_INVALID;
1457 l = find_item(inst);
1459 ErrPrint("Instance is not found(%s - %s)\n", pkgname, id);
1460 return LB_STATUS_ERROR_NOT_EXIST;
1463 item = eina_list_data_get(l);
1464 (void)append_pending_list(item);
1465 return LB_STATUS_SUCCESS;
1468 HAPI int lb_update_all(const char *pkgname, const char *cluster, const char *category)
1474 DbgPrint("Update content for %s\n", pkgname ? pkgname : "(all)");
1475 EINA_LIST_FOREACH_SAFE(s_info.item_list, l, n, item) {
1476 if (item->deleteme) {
1480 if (cluster && strcasecmp(item->inst->cluster, cluster)) {
1484 if (category && strcasecmp(item->inst->category, category)) {
1488 if (pkgname && strlen(pkgname)) {
1489 if (!strcmp(item->inst->item->pkgname, pkgname)) {
1490 (void)append_pending_list(item);
1493 (void)append_pending_list(item);
1497 return LB_STATUS_SUCCESS;
1500 HAPI int lb_delete_all_deleteme(void)
1507 EINA_LIST_FOREACH_SAFE(s_info.item_list, l, n, item) {
1508 if (!item->deleteme) {
1512 update_monitor_del(item->inst->id, item);
1513 (void)so_destroy(item->inst);
1518 DbgPrint("Delete all deleteme: %d\n", cnt);
1519 return LB_STATUS_SUCCESS;
1522 HAPI int lb_system_event_all(int event)
1528 EINA_LIST_FOREACH_SAFE(s_info.item_list, l, n, item) {
1529 if (item->deleteme) {
1533 DbgPrint("System event for %s (%d)\n", item->inst->id, event);
1534 lb_sys_event(item->inst, item, event);
1537 return LB_STATUS_SUCCESS;
1540 HAPI void lb_pause_all(void)
1547 pending_timer_freeze();
1549 EINA_LIST_FOREACH(s_info.item_list, l, item) {
1550 if (item->deleteme) {
1551 DbgPrint("Instance %s skip timer pause (deleteme)\n", item->inst->item->pkgname);
1555 if (item->is_paused) {
1561 lb_sys_event(item->inst, item, LB_SYS_EVENT_PAUSED);
1565 HAPI void lb_resume_all(void)
1573 pending_timer_thaw();
1575 EINA_LIST_FOREACH_SAFE(s_info.item_list, l, n, item) {
1576 if (item->deleteme) {
1577 DbgPrint("Instance %s skip timer resume (deleteme)\n", item->inst->item->pkgname);
1581 if (item->is_paused) {
1585 lb_sys_event(item->inst, item, LB_SYS_EVENT_RESUMED);
1587 if (item->updated_in_pause) {
1588 (void)append_pending_list(item);
1589 item->updated_in_pause = 0;
1594 * After send the resume callback, call this function.
1595 * Because the timer_thaw can call the update function.
1596 * Before resumed event is notified to the livebox,
1597 * Do not call update function
1599 if (timer_thaw(item) == UPDATE_ITEM_DELETED) {
1600 /* item is deleted */
1605 HAPI int lb_pause(const char *pkgname, const char *id)
1607 struct instance *inst;
1611 inst = so_find_instance(pkgname, id);
1613 return LB_STATUS_ERROR_INVALID;
1616 l = find_item(inst);
1618 ErrPrint("Instance is not found (%s - %s)\n", pkgname, id);
1619 return LB_STATUS_ERROR_NOT_EXIST;
1622 item = eina_list_data_get(l);
1624 return LB_STATUS_ERROR_FAULT;
1627 if (item->deleteme) {
1628 DbgPrint("Instance %s will be deleted (%s)\n", item->inst->item->pkgname, item->inst->id);
1629 return LB_STATUS_ERROR_BUSY;
1632 item->is_paused = 1;
1634 if (s_info.paused) {
1635 return LB_STATUS_SUCCESS;
1640 lb_sys_event(inst, item, LB_SYS_EVENT_PAUSED);
1642 return LB_STATUS_SUCCESS;
1645 HAPI int lb_resume(const char *pkgname, const char *id)
1647 struct instance *inst;
1652 inst = so_find_instance(pkgname, id);
1654 return LB_STATUS_ERROR_INVALID;
1657 l = find_item(inst);
1659 ErrPrint("Instance is not found (%s - %s)\n", pkgname, id);
1660 return LB_STATUS_ERROR_NOT_EXIST;
1663 item = eina_list_data_get(l);
1665 return LB_STATUS_ERROR_FAULT;
1668 if (item->deleteme) {
1669 DbgPrint("Instance %s will be deleted (%s)\n", item->inst->item->pkgname, item->inst->id);
1670 return LB_STATUS_ERROR_BUSY;
1673 item->is_paused = 0;
1675 if (s_info.paused) {
1676 return LB_STATUS_SUCCESS;
1679 lb_sys_event(inst, item, LB_SYS_EVENT_RESUMED);
1681 ret = timer_thaw(item);
1682 if (ret == UPDATE_ITEM_DELETED) {
1687 return LB_STATUS_SUCCESS;
1688 } else if (ret == UPDATE_INVOKED) {
1691 * if the update is successfully done, the updated_in_pause will be reset'd.
1692 * or append it to the pending list
1696 if (item->updated_in_pause) {
1697 (void)append_pending_list(item);
1698 item->updated_in_pause = 0;
1701 return LB_STATUS_SUCCESS;
1704 HAPI void lb_turn_secured_on(void)
1709 HAPI int lb_is_all_paused(void)
1711 return s_info.paused;