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 */
27 #include <livebox-errno.h>
29 #include "critical_log.h"
32 #include "so_handler.h"
34 #include "update_monitor.h"
38 #define IS_LB_SHOWN(itm) (!(itm)->inst->item->has_livebox_script || ((itm)->inst->item->has_livebox_script && (itm)->is_lb_show))
44 struct instance *inst;
48 double update_interval;
49 int heavy_updating; /* Only for debugging message */
50 int is_paused; /* 1 is paused, 0 is resumed */
61 Eina_List *pending_list;
62 Ecore_Timer *pending_timer;
63 Eina_List *pd_open_pending_list;
64 Ecore_Timer *pd_open_pending_timer;
68 int pending_timer_freezed;
73 .pending_timer = NULL,
74 .pd_open_pending_list = NULL,
75 .pd_open_pending_timer = NULL,
79 .pending_timer_freezed = 0,
82 static Eina_Bool updator_cb(void *data);
84 static void pending_timer_freeze(void)
86 DbgPrint("Freezed Count: %d\n", s_info.pending_timer_freezed);
87 if (s_info.pending_timer && !s_info.pending_timer_freezed) {
88 DbgPrint("Freeze the pending timer\n");
89 ecore_timer_freeze(s_info.pending_timer);
92 s_info.pending_timer_freezed++;
95 static void pending_timer_thaw(void)
97 DbgPrint("Freezed Count: %d\n", s_info.pending_timer_freezed);
98 if (!s_info.pending_timer_freezed)
101 s_info.pending_timer_freezed--;
102 if (s_info.pending_timer && !s_info.pending_timer_freezed) {
103 DbgPrint("Thaw the pending timer\n");
104 ecore_timer_thaw(s_info.pending_timer);
109 * -1 : PD is opened, but not mine
110 * 0 : PD is not opened
111 * 1 : my PD is opened
113 static inline int pd_is_opened(const char *pkgname)
120 EINA_LIST_FOREACH(s_info.pd_list, l, tmp) {
121 if (pkgname && !strcmp(pkgname, tmp))
127 return i > 0 ? -1 : 0;
130 static Eina_Bool pd_open_pended_cmd_consumer_cb(void *data)
134 item = eina_list_nth(s_info.pd_open_pending_list, 0);
139 return ECORE_CALLBACK_RENEW;
141 s_info.pd_open_pending_list = eina_list_remove(s_info.pd_open_pending_list, item);
144 * To prevent from checking the is_updated function
146 (void)updator_cb(item);
147 if (s_info.pd_open_pending_list)
148 return ECORE_CALLBACK_RENEW;
151 s_info.pd_open_pending_timer = NULL;
152 return ECORE_CALLBACK_CANCEL;
155 static Eina_Bool pended_cmd_consumer_cb(void *data)
159 item = eina_list_nth(s_info.pending_list, 0);
163 if (s_info.update || pd_is_opened(item->inst->item->pkgname) < 0)
164 return ECORE_CALLBACK_RENEW;
166 s_info.pending_list = eina_list_remove(s_info.pending_list, item);
169 * To prevent from checking the is_updated function
171 (void)updator_cb(item);
172 if (s_info.pending_list)
173 return ECORE_CALLBACK_RENEW;
176 s_info.pending_timer = NULL;
177 s_info.pending_timer_freezed = 0;
178 return ECORE_CALLBACK_CANCEL;
181 static inline __attribute__((always_inline)) int activate_pending_consumer(void)
183 if (s_info.pending_timer)
186 s_info.pending_timer = ecore_timer_add(0.000001f, pended_cmd_consumer_cb, NULL);
187 if (!s_info.pending_timer) {
188 ErrPrint("Failed to add a new pended command consumer\n");
189 return LB_STATUS_ERROR_FAULT;
193 * Do not increase the freezed counter.
194 * Just freeze the timer.
196 if (s_info.pending_timer_freezed)
197 ecore_timer_freeze(s_info.pending_timer);
202 static inline void deactivate_pending_consumer(void)
204 if (!s_info.pending_timer)
207 ecore_timer_del(s_info.pending_timer);
208 s_info.pending_timer = NULL;
209 s_info.pending_timer_freezed = 0;
212 static inline void deactivate_pd_open_pending_consumer(void)
214 if (!s_info.pd_open_pending_timer)
217 ecore_timer_del(s_info.pd_open_pending_timer);
218 s_info.pd_open_pending_timer = NULL;
221 static inline int __attribute__((always_inline)) activate_pd_open_pending_consumer(void)
223 if (s_info.pd_open_pending_timer)
226 s_info.pd_open_pending_timer = ecore_timer_add(0.000001f, pd_open_pended_cmd_consumer_cb, NULL);
227 if (!s_info.pd_open_pending_timer) {
228 ErrPrint("Failed to add a new pended command consumer\n");
229 return LB_STATUS_ERROR_FAULT;
235 static inline void migrate_to_pd_open_pending_list(const char *pkgname)
242 EINA_LIST_FOREACH_SAFE(s_info.pending_list, l, n, item) {
243 if (strcmp(pkgname, item->inst->item->pkgname))
246 s_info.pending_list = eina_list_remove(s_info.pending_list, item);
247 s_info.pd_open_pending_list = eina_list_append(s_info.pd_open_pending_list, item);
251 if (s_info.pd_open_pending_list)
252 activate_pd_open_pending_consumer();
254 if (!s_info.pending_list)
255 deactivate_pending_consumer();
258 static inline void migrate_to_pending_list(const char *pkgname)
265 EINA_LIST_FOREACH_SAFE(s_info.pd_open_pending_list, l, n, item) {
266 if (strcmp(pkgname, item->inst->item->pkgname))
269 s_info.pd_open_pending_list = eina_list_remove(s_info.pd_open_pending_list, item);
270 s_info.pending_list = eina_list_append(s_info.pending_list, item);
274 if (s_info.pending_list)
275 activate_pending_consumer();
277 if (!s_info.pd_open_pending_list)
278 deactivate_pd_open_pending_consumer();
281 static inline int append_pending_list(struct item *item)
283 if (pd_is_opened(item->inst->item->pkgname) == 1) {
284 if (eina_list_data_find(s_info.pd_open_pending_list, item) == item) {
285 DbgPrint("Already pended - %s\n", item->inst->item->pkgname);
286 return LB_STATUS_ERROR_EXIST;
289 if (activate_pd_open_pending_consumer() < 0) {
290 ErrPrint("Failed to activate PD open pending consumer\n");
291 return LB_STATUS_ERROR_FAULT;
294 s_info.pd_open_pending_list = eina_list_append(s_info.pd_open_pending_list, item);
296 if (eina_list_data_find(s_info.pending_list, item) == item) {
297 DbgPrint("Already pended - %s\n", item->inst->item->pkgname);
298 return LB_STATUS_ERROR_EXIST;
301 if (activate_pending_consumer() < 0)
302 return LB_STATUS_ERROR_FAULT;
304 s_info.pending_list = eina_list_append(s_info.pending_list, item);
309 static inline void timer_thaw(struct item *item)
319 ecore_timer_thaw(item->timer);
320 period = ecore_timer_interval_get(item->timer);
321 pending = ecore_timer_pending_get(item->timer);
322 delay = util_time_delay_for_compensation(period) - pending;
323 ecore_timer_delay(item->timer, delay);
325 if (item->sleep_at == 0.0f)
328 sleep_time = util_timestamp() - item->sleep_at;
329 if (sleep_time > pending)
330 (void)updator_cb(item);
332 item->sleep_at = 0.0f;
335 static inline void timer_freeze(struct item *item)
342 ecore_timer_freeze(item->timer);
344 if (ecore_timer_interval_get(item->timer) <= 1.0f)
347 if (gettimeofday(&tv, NULL) < 0) {
348 ErrPrint("gettimeofday: %s\n", strerror(errno));
353 item->sleep_at = (double)tv.tv_sec + (double)tv.tv_usec / 1000000.0f;
356 static inline void update_monitor_cnt(struct item *item)
361 now = util_timestamp();
362 interval = now - item->update_interval;
366 * If the content update is processed in too short time,
367 * don't increase the monitor counter, instead of it
368 * set the heavy updating flag.
369 * And handling this heavy updating from the
370 * file update callback.
372 if (interval >= MINIMUM_UPDATE_INTERVAL) {
373 if (s_info.update == item) {
376 * If already in updating mode,
377 * reset the monitor_cnt to 1,
378 * all updated event will be merged into A inotify event
380 DbgPrint("While waiting updated event, content is updated [%s]\n", item->inst->id);
381 item->monitor_cnt = 1;
386 item->heavy_updating = 1;
389 item->update_interval = now;
392 static inline Eina_List *find_item(struct instance *inst)
397 EINA_LIST_FOREACH(s_info.item_list, l, item) {
398 if (item->inst == inst)
405 static inline int output_handler(struct item *item)
410 if (item->monitor_cnt < 0 || item->heavy_updating) {
411 if (!item->heavy_updating) {
412 WarnPrint("%s has invalid monitor_cnt\n", item->inst->id);
415 item->heavy_updating = 0; /* Reset flag */
418 item->monitor_cnt = 0;
421 if (item->monitor_cnt == 0) {
423 fault_unmark_call(item->inst->item->pkgname, item->inst->id, "update,crashed", NO_ALARM);
426 ecore_timer_del(item->monitor);
427 item->monitor = NULL;
430 if (s_info.update == item)
431 s_info.update = NULL;
433 if (item->deleteme) {
434 provider_send_deleted(item->inst->item->pkgname, item->inst->id);
435 (void)so_destroy(item->inst);
444 static int desc_updated_cb(const char *filename, void *data, int over)
449 WarnPrint("Event Q overflow\n");
453 DbgPrint("DESC %s is updated\n", filename);
454 if (item->is_pd_show) {
455 provider_send_desc_updated(item->inst->item->pkgname, item->inst->id, filename);
457 ErrPrint("But PD is not opened, Ignore this update (%s)\n", item->inst->id);
462 static int file_updated_cb(const char *filename, void *data, int over)
468 char *content = NULL;
473 WarnPrint("Event Q overflow\n");
477 ret = util_get_filesize(filename);
479 ErrPrint("Content is updated. but invalid. ret = %d (Update is ignored)\n", ret);
480 return EXIT_SUCCESS; /*!< To keep the callback */
483 ret = so_get_output_info(item->inst, &w, &h, &priority, &content, &title);
485 ErrPrint("livebox_get_info returns %d\n", ret);
486 return EXIT_SUCCESS; /*!< To keep the callback */
489 if (IS_LB_SHOWN(item)) {
490 provider_send_updated(item->inst->item->pkgname, item->inst->id,
491 item->inst->w, item->inst->h, item->inst->priority, content, title);
493 item->is_lb_updated++;
496 DbgPrint("CONTENT %s is updated [%d]\n", filename, item->is_lb_updated);
497 return output_handler(item);
500 static inline int clear_from_pd_open_pending_list(struct item *item)
505 EINA_LIST_FOREACH(s_info.pd_open_pending_list, l, tmp) {
509 s_info.pd_open_pending_list = eina_list_remove_list(s_info.pd_open_pending_list, l);
510 if (!s_info.pd_open_pending_list)
511 deactivate_pd_open_pending_consumer();
512 return LB_STATUS_SUCCESS;
515 return LB_STATUS_ERROR_NOT_EXIST;
518 static inline int clear_from_pending_list(struct item *item)
523 EINA_LIST_FOREACH(s_info.pending_list, l, tmp) {
527 s_info.pending_list = eina_list_remove_list(s_info.pending_list, l);
528 if (!s_info.pending_list)
529 deactivate_pending_consumer();
530 return LB_STATUS_SUCCESS;
533 return LB_STATUS_ERROR_NOT_EXIST;
536 static Eina_Bool update_timeout_cb(void *data)
542 ErrPrint("UPDATE TIMEOUT ========> %s - %s\n", item->inst->item->pkgname, item->inst->id);
544 if (s_info.update != item)
545 ErrPrint("Updating item is not matched\n");
547 fault_unmark_call(item->inst->item->pkgname, item->inst->id, "update,crashed", NO_ALARM);
548 fault_mark_call(item->inst->item->pkgname, item->inst->id, "update,timeout", NO_ALARM, DEFAULT_LIFE_TIMER);
549 s_info.update = NULL;
552 return ECORE_CALLBACK_CANCEL;
555 static Eina_Bool updator_cb(void *data)
562 if (item->monitor) {/*!< If this item is already in update process */
563 return ECORE_CALLBACK_RENEW;
566 if (!IS_LB_SHOWN(item)) {
567 DbgPrint("%s is not shown yet. delaying updates\n", item->inst->item->pkgname);
568 (void)append_pending_list(item);
569 return ECORE_CALLBACK_RENEW;
572 ret = so_is_updated(item->inst);
574 if (so_need_to_destroy(item->inst) == NEED_TO_DESTROY) {
575 provider_send_deleted(item->inst->item->pkgname, item->inst->id);
576 lb_destroy(item->inst->item->pkgname, item->inst->id);
578 ecore_timer_del(item->timer);
580 return ECORE_CALLBACK_CANCEL;
583 return ECORE_CALLBACK_RENEW;
586 if (s_info.update || pd_is_opened(item->inst->item->pkgname) < 0) {
587 DbgPrint("%s is busy\n", s_info.update ? s_info.update->inst->id : item->inst->id);
588 (void)append_pending_list(item);
589 return ECORE_CALLBACK_RENEW;
592 item->monitor = ecore_timer_add(item->inst->item->timeout, update_timeout_cb, item);
593 if (!item->monitor) {
594 ErrPrint("Failed to add update monitor %s(%s):%d\n",
595 item->inst->item->pkgname, item->inst->id, item->inst->item->timeout);
596 return ECORE_CALLBACK_RENEW;
599 ret = so_update(item->inst);
601 ecore_timer_del(item->monitor);
602 item->monitor = NULL;
603 return ECORE_CALLBACK_RENEW;
608 * Counter of the event monitor is only used for asynchronous content updating,
609 * So reset it to 1 from here because the async updating is started now,
610 * even if it is accumulated by other event function before this.
612 item->monitor_cnt = 1;
616 * While waiting the Callback function call,
617 * Add this for finding the crash
619 fault_mark_call(item->inst->item->pkgname, item->inst->id, "update,crashed", NO_ALARM, DEFAULT_LIFE_TIMER);
621 if (ret & NEED_TO_SCHEDULE)
622 (void)append_pending_list(item);
624 if (ret & OUTPUT_UPDATED) {
627 * In this case, there is potential issue
628 * 1. User added update CALLBACK -> Inotify event (Only once)
629 * > We have to detect this case. Is it possible to be a user callback called faster than inotify event handler?
630 * 2. Inotify event -> User added update CALLBACK -> Inotify event
631 * > Okay. What we want is this.
633 update_monitor_cnt(item);
638 * This should be updated after "update_monitor_cnt" function call,
639 * because the update_monitor_cnt function will see the s_info.update variable,
641 s_info.update = item;
643 return ECORE_CALLBACK_RENEW;
646 static inline void update_monitor_del(const char *id, struct item *item)
651 update_monitor_del_update_cb(util_uri_to_path(id), file_updated_cb);
653 len = strlen(util_uri_to_path(id)) + strlen(".desc") + 1;
656 ErrPrint("Heap: %s (%s.desc)\n", strerror(errno), util_uri_to_path(id));
660 snprintf(tmp, len, "%s.desc", util_uri_to_path(id));
661 update_monitor_del_update_cb(tmp, desc_updated_cb);
665 static inline int add_desc_update_monitor(const char *id, struct item *item)
670 len = strlen(util_uri_to_path(id)) + strlen(".desc") + 1;
671 filename = malloc(len);
673 ErrPrint("Heap: %s (%s.desc)\n", strerror(errno), util_uri_to_path(id));
674 return LB_STATUS_ERROR_MEMORY;
677 snprintf(filename, len, "%s.desc", util_uri_to_path(id));
678 return update_monitor_add_update_cb(filename, desc_updated_cb, item);
681 static inline int add_file_update_monitor(const char *id, struct item *item)
685 filename = strdup(util_uri_to_path(id));
687 ErrPrint("Heap: %s (%s)\n", strerror(errno), id);
688 return LB_STATUS_ERROR_MEMORY;
691 return update_monitor_add_update_cb(filename, file_updated_cb, item);
694 static inline int update_monitor_add(const char *id, struct item *item)
698 * item->inst is not available yet.
700 add_file_update_monitor(id, item);
701 add_desc_update_monitor(id, item);
702 return LB_STATUS_SUCCESS;
705 HAPI int lb_init(void)
707 return LB_STATUS_SUCCESS;
710 HAPI int lb_fini(void)
716 deactivate_pending_consumer();
717 deactivate_pd_open_pending_consumer();
719 EINA_LIST_FREE(s_info.pd_open_pending_list, item);
720 EINA_LIST_FREE(s_info.pending_list, item);
722 EINA_LIST_FOREACH_SAFE(s_info.item_list, l, n, item) {
723 provider_send_deleted(item->inst->item->pkgname, item->inst->id);
724 lb_destroy(item->inst->item->pkgname, item->inst->id);
727 return LB_STATUS_SUCCESS;
732 * Exported API for each liveboxes.
734 const char *livebox_find_pkgname(const char *filename)
739 EINA_LIST_FOREACH(s_info.item_list, l, item) {
740 if (!strcmp(item->inst->id, filename))
741 return item->inst->item->pkgname;
747 int livebox_request_update_by_id(const char *filename)
752 EINA_LIST_FOREACH(s_info.item_list, l, item) {
753 if (!strcmp(item->inst->id, filename)) {
754 return append_pending_list(item);
758 return LB_STATUS_ERROR_NOT_EXIST;
761 HAPI int lb_open_pd(const char *pkgname)
766 EINA_LIST_FOREACH(s_info.pd_list, l, tmp) {
767 if (!strcmp(pkgname, tmp))
771 tmp = strdup(pkgname);
773 ErrPrint("Heap: %s\n", strerror(errno));
774 return LB_STATUS_ERROR_MEMORY;
778 pending_timer_freeze();
780 s_info.pd_list = eina_list_append(s_info.pd_list, tmp);
783 * Find all instances from the pending list.
784 * Move them to pd_open_pending_timer
786 migrate_to_pd_open_pending_list(pkgname);
787 return LB_STATUS_SUCCESS;
790 HAPI int lb_close_pd(const char *pkgname)
796 EINA_LIST_FOREACH_SAFE(s_info.pd_list, l, n, tmp) {
797 if (strcmp(tmp, pkgname))
800 s_info.pd_list = eina_list_remove(s_info.pd_list, tmp);
804 pending_timer_thaw();
807 * Move all items in pd_open_pending_list
810 migrate_to_pending_list(pkgname);
811 return LB_STATUS_SUCCESS;
814 return LB_STATUS_ERROR_NOT_EXIST;
817 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)
819 struct instance *inst;
829 inst = so_find_instance(pkgname, id);
831 DbgPrint("Instance is already exists [%s - %s] content[%s], cluster[%s], category[%s], abi[%s]\n", pkgname, id, content_info, cluster, category, abi);
832 return LB_STATUS_SUCCESS;
835 if (!skip_need_to_create) {
836 ret = so_create_needed(pkgname, cluster, category, abi);
837 if (ret != NEED_TO_CREATE)
838 return LB_STATUS_ERROR_PERMISSION;
843 item = calloc(1, sizeof(*item));
845 ErrPrint("Heap: %s (%s - %s, content[%s], cluster[%s], category[%s], abi[%s])\n", strerror(errno), pkgname, id, content_info, cluster, category, abi);
846 return LB_STATUS_ERROR_MEMORY;
849 ret = update_monitor_add(id, item);
855 create_ret = so_create(pkgname, id, content_info, timeout, has_livebox_script, cluster, category, abi, &inst);
856 if (create_ret < 0) {
857 update_monitor_del(id, item);
868 if (period > 0.0f && !s_info.secured) {
869 item->timer = util_timer_add(period, updator_cb, item);
871 ErrPrint("Failed to add timer (%s - %s, content[%s], cluster[%s], category[%s], abi[%s]\n", pkgname, id, content_info, cluster, category, abi);
872 update_monitor_del(id, item);
873 (void)so_destroy(inst);
875 return LB_STATUS_ERROR_FAULT;
881 DbgPrint("Local update timer is disabled: %lf (%d)\n", period, s_info.secured);
885 s_info.item_list = eina_list_append(s_info.item_list, item);
887 if (create_ret & NEED_TO_SCHEDULE) {
888 DbgPrint("%s Returns NEED_TO_SCHEDULE\n", pkgname);
889 (void)append_pending_list(item);
892 if (create_ret & OUTPUT_UPDATED) {
893 update_monitor_cnt(item);
896 * To send a output info, get the info forcely.
897 * but the output file monitor will do this again
899 * This function will set the tmp_content and tmp_title
900 * even if it has no updates on the content, title,
901 * it will set them to NULL.
903 if (so_get_output_info(inst, w, h, priority, out_content, out_title) == LB_STATUS_SUCCESS) {
907 tmp = strdup(*out_content);
909 ErrPrint("Memory: %s\n", strerror(errno));
917 tmp = strdup(*out_title);
919 ErrPrint("Memory: %s\n", strerror(errno));
928 *priority = inst->priority;
929 return need_to_create;
932 HAPI int lb_destroy(const char *pkgname, const char *id)
935 struct instance *inst;
938 inst = so_find_instance(pkgname, id);
940 ErrPrint("Instance %s - %s is not created\n", pkgname, id);
941 return LB_STATUS_ERROR_INVALID;
946 ErrPrint("Instance is not found (%s - %s)\n", pkgname, id);
947 return LB_STATUS_ERROR_NOT_EXIST;
950 item = eina_list_data_get(l);
951 s_info.item_list = eina_list_remove_list(s_info.item_list, l);
953 if (s_info.update == item)
954 s_info.update = NULL;
957 clear_from_pd_open_pending_list(item);
958 clear_from_pending_list(item);
959 ecore_timer_del(item->timer);
965 update_monitor_del(id, item);
968 if (!item->monitor) {
970 (void)so_destroy(inst);
973 return LB_STATUS_SUCCESS;
976 HAPI int lb_resize(const char *pkgname, const char *id, int w, int h)
979 struct instance *inst;
983 inst = so_find_instance(pkgname, id);
985 ErrPrint("Instance %s - %s is not created (%dx%d)\n", pkgname, id, w, h);
986 return LB_STATUS_ERROR_INVALID;
991 ErrPrint("Instance is not found (%s - %s, %dx%d)\n", pkgname, id, w, h);
992 return LB_STATUS_ERROR_NOT_EXIST;
995 item = eina_list_data_get(l);
997 ret = so_resize(inst, w, h);
1001 if (ret & NEED_TO_SCHEDULE) {
1002 DbgPrint("%s Returns NEED_TO_SCHEDULE\n", pkgname);
1003 (void)append_pending_list(item);
1006 if (ret & OUTPUT_UPDATED)
1007 update_monitor_cnt(item);
1009 return LB_STATUS_SUCCESS;
1012 HAPI char *lb_pinup(const char *pkgname, const char *id, int pinup)
1014 struct instance *inst;
1017 inst = so_find_instance(pkgname, id);
1019 ErrPrint("Instance %s - %s is not found (pinup[%d])\n", pkgname, id, pinup);
1023 ret = so_pinup(inst, pinup);
1027 HAPI int lb_set_period(const char *pkgname, const char *id, double period)
1030 struct instance *inst;
1033 inst = so_find_instance(pkgname, id);
1035 ErrPrint("Instance %s - %s is not found (period[%lf])\n", pkgname, id, period);
1036 return LB_STATUS_ERROR_INVALID;
1039 l = find_item(inst);
1041 ErrPrint("Instance is not found (%s - %s, period[%lf])\n", pkgname, id, period);
1042 return LB_STATUS_ERROR_NOT_EXIST;
1045 item = eina_list_data_get(l);
1047 if (period <= 0.0f) {
1049 ecore_timer_del(item->timer);
1054 util_timer_interval_set(item->timer, period);
1055 } else if (!s_info.secured) {
1056 item->timer = util_timer_add(period, updator_cb, item);
1058 ErrPrint("Failed to add timer (%s - %s)\n", pkgname, id);
1059 return LB_STATUS_ERROR_FAULT;
1067 return LB_STATUS_SUCCESS;
1070 HAPI int lb_clicked(const char *pkgname, const char *id, const char *event, double timestamp, double x, double y)
1073 struct instance *inst;
1077 inst = so_find_instance(pkgname, id);
1079 ErrPrint("Instance %s - %s is not exists (event[%s])\n", pkgname, id, event);
1080 return LB_STATUS_ERROR_INVALID;
1083 l = find_item(inst);
1085 ErrPrint("Instance is not found (%s - %s, event[%s])\n", pkgname, id, event);
1086 return LB_STATUS_ERROR_NOT_EXIST;
1089 item = eina_list_data_get(l);
1091 ret = so_clicked(inst, event, timestamp, x, y);
1095 if (ret & NEED_TO_SCHEDULE) {
1096 DbgPrint("%s Returns NEED_TO_SCHEDULE\n", pkgname);
1097 (void)append_pending_list(item);
1100 if (ret & OUTPUT_UPDATED)
1101 update_monitor_cnt(item);
1103 return LB_STATUS_SUCCESS;
1106 HAPI int lb_script_event(const char *pkgname, const char *id, const char *emission, const char *source, struct event_info *event_info)
1109 struct instance *inst;
1113 inst = so_find_instance(pkgname, id);
1115 ErrPrint("Instance %s - %s is not exists (emission[%s], source[%s])\n", pkgname, id, emission, source);
1116 return LB_STATUS_ERROR_INVALID;
1119 l = find_item(inst);
1121 ErrPrint("Instance is not found (%s - %s, emissino[%s], source[%s])\n", pkgname, id, emission, source);
1122 return LB_STATUS_ERROR_NOT_EXIST;
1125 item = eina_list_data_get(l);
1127 if (emission && source && !strcmp(source, id)) {
1128 if (item->inst->item->has_livebox_script) {
1129 if (!strcmp(emission, "lb,show")) {
1130 item->is_lb_show = 1;
1132 DbgPrint("[%s] Updated %d times, (content: %s), (title: %s)\n", id, item->is_lb_updated, item->inst->content, item->inst->title);
1133 if (item->is_lb_updated) {
1134 provider_send_updated(item->inst->item->pkgname, item->inst->id,
1135 item->inst->w, item->inst->h, item->inst->priority, item->inst->content, item->inst->title);
1136 item->is_lb_updated = 0;
1139 source = util_uri_to_path(source);
1140 } else if (!strcmp(emission, "lb,hide")) {
1141 DbgPrint("Livebox(%s) script is hide now\n", id);
1142 item->is_lb_show = 0;
1144 source = util_uri_to_path(source);
1148 if (!strcmp(emission, "pd,show")) {
1149 item->is_pd_show = 1;
1150 source = util_uri_to_path(source);
1151 } else if (!strcmp(emission, "pd,hide")) {
1152 item->is_pd_show = 0;
1153 source = util_uri_to_path(source);
1157 ret = so_script_event(inst, emission, source, event_info);
1161 if (ret & NEED_TO_SCHEDULE) {
1162 DbgPrint("%s Returns NEED_TO_SCHEDULE\n", pkgname);
1163 (void)append_pending_list(item);
1166 if (ret & OUTPUT_UPDATED)
1167 update_monitor_cnt(item);
1169 return LB_STATUS_SUCCESS;
1172 HAPI int lb_is_pinned_up(const char *pkgname, const char *id)
1175 struct instance *inst;
1178 inst = so_find_instance(pkgname, id);
1180 ErrPrint("Instance %s - %s is not created\n", pkgname, id);
1181 return LB_STATUS_ERROR_INVALID;
1184 l = find_item(inst);
1186 ErrPrint("Instance is not found(%s - %s)\n", pkgname, id);
1187 return LB_STATUS_ERROR_NOT_EXIST;
1190 item = eina_list_data_get(l);
1192 ErrPrint("Invalid item(%s - %s)\n", pkgname, id);
1193 return LB_STATUS_ERROR_FAULT;
1198 * Maybe this is not neccessary for this operation
1200 return so_is_pinned_up(inst);
1203 HAPI int lb_change_group(const char *pkgname, const char *id, const char *cluster, const char *category)
1206 struct instance *inst;
1210 inst = so_find_instance(pkgname, id);
1212 ErrPrint("Instance %s - %s is not created (cluster[%s], category[%s])\n", pkgname, id, cluster, category);
1213 return LB_STATUS_ERROR_INVALID;
1216 l = find_item(inst);
1218 ErrPrint("Instance is not found(%s - %s, cluster[%s], category[%s])\n", pkgname, id, cluster, category);
1219 return LB_STATUS_ERROR_NOT_EXIST;
1222 item = eina_list_data_get(l);
1224 ret = so_change_group(inst, cluster, category);
1228 if (ret & NEED_TO_SCHEDULE) {
1229 DbgPrint("%s Returns NEED_TO_SCHEDULE\n", pkgname);
1230 (void)append_pending_list(item);
1233 if (ret & OUTPUT_UPDATED)
1234 update_monitor_cnt(item);
1236 return LB_STATUS_SUCCESS;
1239 static inline int lb_sys_event(struct instance *inst, struct item *item, int event)
1243 ret = so_sys_event(inst, event);
1247 if (ret & NEED_TO_SCHEDULE)
1248 (void)append_pending_list(item);
1250 if (ret & OUTPUT_UPDATED)
1251 update_monitor_cnt(item);
1253 return LB_STATUS_SUCCESS;
1256 HAPI int lb_system_event(const char *pkgname, const char *id, int event)
1259 struct instance *inst;
1262 inst = so_find_instance(pkgname, id);
1264 ErrPrint("instance %s - %s is not created\n");
1265 return LB_STATUS_ERROR_INVALID;
1268 l = find_item(inst);
1270 ErrPrint("Instance is not found(%s - %s)\n", pkgname, id);
1271 return LB_STATUS_ERROR_NOT_EXIST;
1274 item = eina_list_data_get(l);
1275 return lb_sys_event(inst, item, event);
1278 HAPI int lb_update(const char *pkgname, const char *id)
1281 struct instance *inst;
1284 inst = so_find_instance(pkgname, id);
1286 ErrPrint("Instance %s - %s is not created\n", pkgname, id);
1287 return LB_STATUS_ERROR_INVALID;
1290 l = find_item(inst);
1292 ErrPrint("Instance is not found(%s - %s)\n", pkgname, id);
1293 return LB_STATUS_ERROR_NOT_EXIST;
1296 item = eina_list_data_get(l);
1297 (void)append_pending_list(item);
1298 return LB_STATUS_SUCCESS;
1301 HAPI int lb_update_all(const char *pkgname, const char *cluster, const char *category)
1307 DbgPrint("Update content for %s\n", pkgname ? pkgname : "(all)");
1308 EINA_LIST_FOREACH_SAFE(s_info.item_list, l, n, item) {
1312 if (cluster && strcasecmp(item->inst->cluster, cluster))
1315 if (category && strcasecmp(item->inst->category, category))
1318 if (pkgname && strlen(pkgname)) {
1319 if (!strcmp(item->inst->item->pkgname, pkgname)) {
1320 (void)append_pending_list(item);
1323 (void)append_pending_list(item);
1327 return LB_STATUS_SUCCESS;
1330 HAPI int lb_delete_all_deleteme(void)
1337 EINA_LIST_FOREACH_SAFE(s_info.item_list, l, n, item) {
1338 if (!item->deleteme)
1341 s_info.item_list = eina_list_remove(s_info.item_list, item);
1343 update_monitor_del(item->inst->id, item);
1344 (void)so_destroy(item->inst);
1349 DbgPrint("Delete all deleteme: %d\n", cnt);
1350 return LB_STATUS_SUCCESS;
1353 HAPI int lb_system_event_all(int event)
1359 EINA_LIST_FOREACH_SAFE(s_info.item_list, l, n, item) {
1363 DbgPrint("System event for %s (%d)\n", item->inst->id, event);
1364 lb_sys_event(item->inst, item, event);
1367 return LB_STATUS_SUCCESS;
1370 HAPI void lb_pause_all(void)
1377 pending_timer_freeze();
1379 EINA_LIST_FOREACH(s_info.item_list, l, item) {
1380 if (item->deleteme) {
1381 DbgPrint("Instance %s skip timer pause (deleteme)\n", item->inst->item->pkgname);
1385 if (item->is_paused)
1390 lb_sys_event(item->inst, item, LB_SYS_EVENT_PAUSED);
1394 HAPI void lb_resume_all(void)
1401 pending_timer_thaw();
1403 EINA_LIST_FOREACH(s_info.item_list, l, item) {
1404 if (item->deleteme) {
1405 DbgPrint("Instance %s skip timer resume (deleteme)\n", item->inst->item->pkgname);
1409 if (item->is_paused)
1414 lb_sys_event(item->inst, item, LB_SYS_EVENT_RESUMED);
1418 HAPI int lb_pause(const char *pkgname, const char *id)
1420 struct instance *inst;
1424 inst = so_find_instance(pkgname, id);
1426 return LB_STATUS_ERROR_INVALID;
1428 l = find_item(inst);
1430 ErrPrint("Instance is not found (%s - %s)\n", pkgname, id);
1431 return LB_STATUS_ERROR_NOT_EXIST;
1434 item = eina_list_data_get(l);
1436 return LB_STATUS_ERROR_FAULT;
1438 if (item->deleteme) {
1439 DbgPrint("Instance %s will be deleted (%s)\n", item->inst->item->pkgname, item->inst->id);
1440 return LB_STATUS_ERROR_BUSY;
1443 item->is_paused = 1;
1446 return LB_STATUS_SUCCESS;
1450 lb_sys_event(inst, item, LB_SYS_EVENT_PAUSED);
1452 return LB_STATUS_SUCCESS;
1455 HAPI int lb_resume(const char *pkgname, const char *id)
1457 struct instance *inst;
1461 inst = so_find_instance(pkgname, id);
1463 return LB_STATUS_ERROR_INVALID;
1465 l = find_item(inst);
1467 ErrPrint("Instance is not found (%s - %s)\n", pkgname, id);
1468 return LB_STATUS_ERROR_NOT_EXIST;
1471 item = eina_list_data_get(l);
1473 return LB_STATUS_ERROR_FAULT;
1475 if (item->deleteme) {
1476 DbgPrint("Instance %s will be deleted (%s)\n", item->inst->item->pkgname, item->inst->id);
1477 return LB_STATUS_ERROR_BUSY;
1480 item->is_paused = 0;
1483 return LB_STATUS_SUCCESS;
1487 lb_sys_event(inst, item, LB_SYS_EVENT_RESUMED);
1488 return LB_STATUS_SUCCESS;
1491 HAPI void lb_turn_secured_on(void)
1496 HAPI int lb_is_all_paused(void)
1498 return s_info.paused;