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))
45 struct instance *inst;
49 double update_interval;
50 int heavy_updating; /* Only for debugging message */
51 int is_paused; /* 1 is paused, 0 is resumed */
62 Eina_List *pending_list;
63 Ecore_Timer *pending_timer;
64 Eina_List *pd_open_pending_list;
65 Ecore_Timer *pd_open_pending_timer;
69 int pending_timer_freezed;
74 .pending_timer = NULL,
75 .pd_open_pending_list = NULL,
76 .pd_open_pending_timer = NULL,
80 .pending_timer_freezed = 0,
83 static Eina_Bool updator_cb(void *data);
85 static void pending_timer_freeze(void)
87 DbgPrint("Freezed Count: %d\n", s_info.pending_timer_freezed);
88 if (s_info.pending_timer && !s_info.pending_timer_freezed) {
89 DbgPrint("Freeze the pending timer\n");
90 ecore_timer_freeze(s_info.pending_timer);
93 s_info.pending_timer_freezed++;
96 static void pending_timer_thaw(void)
98 DbgPrint("Freezed Count: %d\n", s_info.pending_timer_freezed);
99 if (!s_info.pending_timer_freezed)
102 s_info.pending_timer_freezed--;
103 if (s_info.pending_timer && !s_info.pending_timer_freezed) {
104 DbgPrint("Thaw the pending timer\n");
105 ecore_timer_thaw(s_info.pending_timer);
110 * -1 : PD is opened, but not mine
111 * 0 : PD is not opened
112 * 1 : my PD is opened
114 static inline int pd_is_opened(const char *pkgname)
121 EINA_LIST_FOREACH(s_info.pd_list, l, tmp) {
122 if (pkgname && !strcmp(pkgname, tmp))
128 return i > 0 ? -1 : 0;
131 static Eina_Bool pd_open_pended_cmd_consumer_cb(void *data)
135 item = eina_list_nth(s_info.pd_open_pending_list, 0);
140 return ECORE_CALLBACK_RENEW;
142 s_info.pd_open_pending_list = eina_list_remove(s_info.pd_open_pending_list, item);
145 * To prevent from checking the is_updated function
147 (void)updator_cb(item);
148 if (s_info.pd_open_pending_list)
149 return ECORE_CALLBACK_RENEW;
152 s_info.pd_open_pending_timer = NULL;
153 return ECORE_CALLBACK_CANCEL;
156 static Eina_Bool pended_cmd_consumer_cb(void *data)
160 item = eina_list_nth(s_info.pending_list, 0);
164 if (s_info.update || pd_is_opened(item->inst->item->pkgname) < 0)
165 return ECORE_CALLBACK_RENEW;
167 s_info.pending_list = eina_list_remove(s_info.pending_list, item);
170 * To prevent from checking the is_updated function
172 (void)updator_cb(item);
173 if (s_info.pending_list)
174 return ECORE_CALLBACK_RENEW;
177 s_info.pending_timer = NULL;
178 s_info.pending_timer_freezed = 0;
179 return ECORE_CALLBACK_CANCEL;
182 static inline __attribute__((always_inline)) int activate_pending_consumer(void)
184 if (s_info.pending_timer)
187 s_info.pending_timer = ecore_timer_add(0.000001f, pended_cmd_consumer_cb, NULL);
188 if (!s_info.pending_timer) {
189 ErrPrint("Failed to add a new pended command consumer\n");
190 return LB_STATUS_ERROR_FAULT;
194 * Do not increase the freezed counter.
195 * Just freeze the timer.
197 if (s_info.pending_timer_freezed)
198 ecore_timer_freeze(s_info.pending_timer);
203 static inline void deactivate_pending_consumer(void)
205 if (!s_info.pending_timer)
208 ecore_timer_del(s_info.pending_timer);
209 s_info.pending_timer = NULL;
210 s_info.pending_timer_freezed = 0;
213 static inline void deactivate_pd_open_pending_consumer(void)
215 if (!s_info.pd_open_pending_timer)
218 ecore_timer_del(s_info.pd_open_pending_timer);
219 s_info.pd_open_pending_timer = NULL;
222 static inline int __attribute__((always_inline)) activate_pd_open_pending_consumer(void)
224 if (s_info.pd_open_pending_timer)
227 s_info.pd_open_pending_timer = ecore_timer_add(0.000001f, pd_open_pended_cmd_consumer_cb, NULL);
228 if (!s_info.pd_open_pending_timer) {
229 ErrPrint("Failed to add a new pended command consumer\n");
230 return LB_STATUS_ERROR_FAULT;
236 static inline void migrate_to_pd_open_pending_list(const char *pkgname)
243 EINA_LIST_FOREACH_SAFE(s_info.pending_list, l, n, item) {
244 if (strcmp(pkgname, item->inst->item->pkgname))
247 s_info.pending_list = eina_list_remove(s_info.pending_list, item);
248 s_info.pd_open_pending_list = eina_list_append(s_info.pd_open_pending_list, item);
252 if (s_info.pd_open_pending_list)
253 activate_pd_open_pending_consumer();
255 if (!s_info.pending_list)
256 deactivate_pending_consumer();
259 static inline void migrate_to_pending_list(const char *pkgname)
266 EINA_LIST_FOREACH_SAFE(s_info.pd_open_pending_list, l, n, item) {
267 if (strcmp(pkgname, item->inst->item->pkgname))
270 s_info.pd_open_pending_list = eina_list_remove(s_info.pd_open_pending_list, item);
271 s_info.pending_list = eina_list_append(s_info.pending_list, item);
275 if (s_info.pending_list)
276 activate_pending_consumer();
278 if (!s_info.pd_open_pending_list)
279 deactivate_pd_open_pending_consumer();
282 static inline int append_pending_list(struct item *item)
284 if (pd_is_opened(item->inst->item->pkgname) == 1) {
285 if (eina_list_data_find(s_info.pd_open_pending_list, item) == item) {
286 DbgPrint("Already pended - %s\n", item->inst->item->pkgname);
287 return LB_STATUS_ERROR_EXIST;
290 if (activate_pd_open_pending_consumer() < 0) {
291 ErrPrint("Failed to activate PD open pending consumer\n");
292 return LB_STATUS_ERROR_FAULT;
295 s_info.pd_open_pending_list = eina_list_append(s_info.pd_open_pending_list, item);
297 if (eina_list_data_find(s_info.pending_list, item) == item) {
298 DbgPrint("Already pended - %s\n", item->inst->item->pkgname);
299 return LB_STATUS_ERROR_EXIST;
302 if (activate_pending_consumer() < 0)
303 return LB_STATUS_ERROR_FAULT;
305 s_info.pending_list = eina_list_append(s_info.pending_list, item);
310 static inline void timer_thaw(struct item *item)
320 ecore_timer_thaw(item->timer);
321 period = ecore_timer_interval_get(item->timer);
322 pending = ecore_timer_pending_get(item->timer);
323 delay = util_time_delay_for_compensation(period) - pending;
324 ecore_timer_delay(item->timer, delay);
326 if (item->sleep_at == 0.0f)
329 sleep_time = util_timestamp() - item->sleep_at;
330 if (sleep_time > pending)
331 (void)updator_cb(item);
333 item->sleep_at = 0.0f;
336 static inline void timer_freeze(struct item *item)
343 ecore_timer_freeze(item->timer);
345 if (ecore_timer_interval_get(item->timer) <= 1.0f)
348 if (gettimeofday(&tv, NULL) < 0) {
349 ErrPrint("gettimeofday: %s\n", strerror(errno));
354 item->sleep_at = (double)tv.tv_sec + (double)tv.tv_usec / 1000000.0f;
357 static inline void update_monitor_cnt(struct item *item)
362 now = util_timestamp();
363 interval = now - item->update_interval;
367 * If the content update is processed in too short time,
368 * don't increase the monitor counter, instead of it
369 * set the heavy updating flag.
370 * And handling this heavy updating from the
371 * file update callback.
373 if (interval >= MINIMUM_UPDATE_INTERVAL) {
374 if (s_info.update == item) {
377 * If already in updating mode,
378 * reset the monitor_cnt to 1,
379 * all updated event will be merged into A inotify event
381 DbgPrint("While waiting updated event, content is updated [%s]\n", item->inst->id);
382 item->monitor_cnt = 1;
387 item->heavy_updating = 1;
390 item->update_interval = now;
393 static inline Eina_List *find_item(struct instance *inst)
398 EINA_LIST_FOREACH(s_info.item_list, l, item) {
399 if (item->inst == inst)
406 static inline int output_handler(struct item *item)
411 if (item->monitor_cnt < 0 || item->heavy_updating) {
412 if (!item->heavy_updating) {
413 WarnPrint("%s has invalid monitor_cnt\n", item->inst->id);
416 item->heavy_updating = 0; /* Reset flag */
419 item->monitor_cnt = 0;
422 if (item->monitor_cnt == 0) {
424 fault_unmark_call(item->inst->item->pkgname, item->inst->id, "update,crashed", NO_ALARM);
427 ecore_timer_del(item->monitor);
428 item->monitor = NULL;
431 if (s_info.update == item)
432 s_info.update = NULL;
434 if (item->deleteme) {
435 provider_send_deleted(item->inst->item->pkgname, item->inst->id);
436 (void)so_destroy(item->inst);
445 static int desc_updated_cb(const char *filename, void *data, int over)
450 WarnPrint("Event Q overflow\n");
454 DbgPrint("DESC %s is updated\n", filename);
455 if (item->is_pd_show) {
456 provider_send_desc_updated(item->inst->item->pkgname, item->inst->id, filename);
458 ErrPrint("But PD is not opened, Ignore this update (%s)\n", item->inst->id);
463 static int file_updated_cb(const char *filename, void *data, int over)
469 char *content = NULL;
474 WarnPrint("Event Q overflow\n");
478 ret = util_get_filesize(filename);
480 ErrPrint("Content is updated. but invalid. ret = %d (Update is ignored)\n", ret);
481 return EXIT_SUCCESS; /*!< To keep the callback */
484 ret = so_get_output_info(item->inst, &w, &h, &priority, &content, &title);
486 ErrPrint("livebox_get_info returns %d\n", ret);
487 return EXIT_SUCCESS; /*!< To keep the callback */
490 if (IS_LB_SHOWN(item)) {
491 provider_send_updated(item->inst->item->pkgname, item->inst->id,
492 item->inst->w, item->inst->h, item->inst->priority, content, title);
494 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 int livebox_trigger_update_monitor(const char *filename, int is_pd)
768 len = strlen(filename) + strlen(".desc");
770 fname = malloc(len + 1);
772 ErrPrint("Heap: %s\n", strerror(errno));
773 return LB_STATUS_ERROR_MEMORY;
776 snprintf(fname, len, "%s.desc", filename);
778 fname = strdup(filename);
780 ErrPrint("Heap: %s\n", strerror(errno));
781 return LB_STATUS_ERROR_MEMORY;
785 if (access(fname, R_OK | W_OK) != 0) {
786 ErrPrint("access: %s (%s)\n", fname, strerror(errno));
787 ret = LB_STATUS_ERROR_IO;
789 ret = update_monitor_trigger_update_cb(fname, 0);
796 HAPI int lb_open_pd(const char *pkgname)
801 EINA_LIST_FOREACH(s_info.pd_list, l, tmp) {
802 if (!strcmp(pkgname, tmp))
806 tmp = strdup(pkgname);
808 ErrPrint("Heap: %s\n", strerror(errno));
809 return LB_STATUS_ERROR_MEMORY;
813 pending_timer_freeze();
815 s_info.pd_list = eina_list_append(s_info.pd_list, tmp);
818 * Find all instances from the pending list.
819 * Move them to pd_open_pending_timer
821 migrate_to_pd_open_pending_list(pkgname);
822 return LB_STATUS_SUCCESS;
825 HAPI int lb_close_pd(const char *pkgname)
831 EINA_LIST_FOREACH_SAFE(s_info.pd_list, l, n, tmp) {
832 if (strcmp(tmp, pkgname))
835 s_info.pd_list = eina_list_remove(s_info.pd_list, tmp);
839 pending_timer_thaw();
842 * Move all items in pd_open_pending_list
845 migrate_to_pending_list(pkgname);
846 return LB_STATUS_SUCCESS;
849 return LB_STATUS_ERROR_NOT_EXIST;
852 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)
854 struct instance *inst;
864 inst = so_find_instance(pkgname, id);
866 DbgPrint("Instance is already exists [%s - %s] content[%s], cluster[%s], category[%s], abi[%s]\n", pkgname, id, content_info, cluster, category, abi);
867 return LB_STATUS_SUCCESS;
870 if (!skip_need_to_create) {
871 ret = so_create_needed(pkgname, cluster, category, abi);
872 if (ret != NEED_TO_CREATE)
873 return LB_STATUS_ERROR_PERMISSION;
878 item = calloc(1, sizeof(*item));
880 ErrPrint("Heap: %s (%s - %s, content[%s], cluster[%s], category[%s], abi[%s])\n", strerror(errno), pkgname, id, content_info, cluster, category, abi);
881 return LB_STATUS_ERROR_MEMORY;
884 ret = update_monitor_add(id, item);
890 create_ret = so_create(pkgname, id, content_info, timeout, has_livebox_script, cluster, category, abi, &inst);
891 if (create_ret < 0) {
892 update_monitor_del(id, item);
903 if (period > 0.0f && !s_info.secured) {
904 item->timer = util_timer_add(period, updator_cb, item);
906 ErrPrint("Failed to add timer (%s - %s, content[%s], cluster[%s], category[%s], abi[%s]\n", pkgname, id, content_info, cluster, category, abi);
907 update_monitor_del(id, item);
908 (void)so_destroy(inst);
910 return LB_STATUS_ERROR_FAULT;
916 DbgPrint("Local update timer is disabled: %lf (%d)\n", period, s_info.secured);
920 s_info.item_list = eina_list_append(s_info.item_list, item);
922 if (create_ret & NEED_TO_SCHEDULE) {
923 DbgPrint("%s Returns NEED_TO_SCHEDULE\n", pkgname);
924 (void)append_pending_list(item);
927 if (create_ret & OUTPUT_UPDATED) {
928 update_monitor_cnt(item);
931 * To send a output info, get the info forcely.
932 * but the output file monitor will do this again
934 * This function will set the tmp_content and tmp_title
935 * even if it has no updates on the content, title,
936 * it will set them to NULL.
938 if (so_get_output_info(inst, w, h, priority, out_content, out_title) == LB_STATUS_SUCCESS) {
942 tmp = strdup(*out_content);
944 ErrPrint("Memory: %s\n", strerror(errno));
952 tmp = strdup(*out_title);
954 ErrPrint("Memory: %s\n", strerror(errno));
963 *priority = inst->priority;
964 return need_to_create;
967 HAPI int lb_destroy(const char *pkgname, const char *id)
970 struct instance *inst;
973 inst = so_find_instance(pkgname, id);
975 ErrPrint("Instance %s - %s is not created\n", pkgname, id);
976 return LB_STATUS_ERROR_INVALID;
981 ErrPrint("Instance is not found (%s - %s)\n", pkgname, id);
982 return LB_STATUS_ERROR_NOT_EXIST;
985 item = eina_list_data_get(l);
986 s_info.item_list = eina_list_remove_list(s_info.item_list, l);
988 if (s_info.update == item)
989 s_info.update = NULL;
992 clear_from_pd_open_pending_list(item);
993 clear_from_pending_list(item);
994 ecore_timer_del(item->timer);
1000 update_monitor_del(id, item);
1003 if (!item->monitor) {
1005 (void)so_destroy(inst);
1008 return LB_STATUS_SUCCESS;
1011 HAPI int lb_resize(const char *pkgname, const char *id, int w, int h)
1014 struct instance *inst;
1018 inst = so_find_instance(pkgname, id);
1020 ErrPrint("Instance %s - %s is not created (%dx%d)\n", pkgname, id, w, h);
1021 return LB_STATUS_ERROR_INVALID;
1024 l = find_item(inst);
1026 ErrPrint("Instance is not found (%s - %s, %dx%d)\n", pkgname, id, w, h);
1027 return LB_STATUS_ERROR_NOT_EXIST;
1030 item = eina_list_data_get(l);
1032 ret = so_resize(inst, w, h);
1036 if (ret & NEED_TO_SCHEDULE) {
1037 DbgPrint("%s Returns NEED_TO_SCHEDULE\n", pkgname);
1038 (void)append_pending_list(item);
1041 if (ret & OUTPUT_UPDATED)
1042 update_monitor_cnt(item);
1044 return LB_STATUS_SUCCESS;
1047 HAPI char *lb_pinup(const char *pkgname, const char *id, int pinup)
1049 struct instance *inst;
1052 inst = so_find_instance(pkgname, id);
1054 ErrPrint("Instance %s - %s is not found (pinup[%d])\n", pkgname, id, pinup);
1058 ret = so_pinup(inst, pinup);
1062 HAPI int lb_set_period(const char *pkgname, const char *id, double period)
1065 struct instance *inst;
1068 inst = so_find_instance(pkgname, id);
1070 ErrPrint("Instance %s - %s is not found (period[%lf])\n", pkgname, id, period);
1071 return LB_STATUS_ERROR_INVALID;
1074 l = find_item(inst);
1076 ErrPrint("Instance is not found (%s - %s, period[%lf])\n", pkgname, id, period);
1077 return LB_STATUS_ERROR_NOT_EXIST;
1080 item = eina_list_data_get(l);
1082 if (period <= 0.0f) {
1084 ecore_timer_del(item->timer);
1089 util_timer_interval_set(item->timer, period);
1090 } else if (!s_info.secured) {
1091 item->timer = util_timer_add(period, updator_cb, item);
1093 ErrPrint("Failed to add timer (%s - %s)\n", pkgname, id);
1094 return LB_STATUS_ERROR_FAULT;
1102 return LB_STATUS_SUCCESS;
1105 HAPI int lb_clicked(const char *pkgname, const char *id, const char *event, double timestamp, double x, double y)
1108 struct instance *inst;
1112 inst = so_find_instance(pkgname, id);
1114 ErrPrint("Instance %s - %s is not exists (event[%s])\n", pkgname, id, event);
1115 return LB_STATUS_ERROR_INVALID;
1118 l = find_item(inst);
1120 ErrPrint("Instance is not found (%s - %s, event[%s])\n", pkgname, id, event);
1121 return LB_STATUS_ERROR_NOT_EXIST;
1124 item = eina_list_data_get(l);
1126 ret = so_clicked(inst, event, timestamp, x, y);
1130 if (ret & NEED_TO_SCHEDULE) {
1131 DbgPrint("%s Returns NEED_TO_SCHEDULE\n", pkgname);
1132 (void)append_pending_list(item);
1135 if (ret & OUTPUT_UPDATED)
1136 update_monitor_cnt(item);
1138 return LB_STATUS_SUCCESS;
1141 HAPI int lb_script_event(const char *pkgname, const char *id, const char *emission, const char *source, struct event_info *event_info)
1144 struct instance *inst;
1148 inst = so_find_instance(pkgname, id);
1150 ErrPrint("Instance %s - %s is not exists (emission[%s], source[%s])\n", pkgname, id, emission, source);
1151 return LB_STATUS_ERROR_INVALID;
1154 l = find_item(inst);
1156 ErrPrint("Instance is not found (%s - %s, emissino[%s], source[%s])\n", pkgname, id, emission, source);
1157 return LB_STATUS_ERROR_NOT_EXIST;
1160 item = eina_list_data_get(l);
1162 if (emission && source && !strcmp(source, id)) {
1163 if (item->inst->item->has_livebox_script) {
1164 if (!strcmp(emission, "lb,show")) {
1165 item->is_lb_show = 1;
1167 DbgPrint("[%s] Updated %d times, (content: %s), (title: %s)\n", id, item->is_lb_updated, item->inst->content, item->inst->title);
1168 if (item->is_lb_updated) {
1169 provider_send_updated(item->inst->item->pkgname, item->inst->id,
1170 item->inst->w, item->inst->h, item->inst->priority, item->inst->content, item->inst->title);
1171 item->is_lb_updated = 0;
1174 source = util_uri_to_path(source);
1175 } else if (!strcmp(emission, "lb,hide")) {
1176 DbgPrint("Livebox(%s) script is hide now\n", id);
1177 item->is_lb_show = 0;
1179 source = util_uri_to_path(source);
1183 if (!strcmp(emission, "pd,show")) {
1184 item->is_pd_show = 1;
1185 source = util_uri_to_path(source);
1186 } else if (!strcmp(emission, "pd,hide")) {
1187 item->is_pd_show = 0;
1188 source = util_uri_to_path(source);
1192 ret = so_script_event(inst, emission, source, event_info);
1196 if (ret & NEED_TO_SCHEDULE) {
1197 DbgPrint("%s Returns NEED_TO_SCHEDULE\n", pkgname);
1198 (void)append_pending_list(item);
1201 if (ret & OUTPUT_UPDATED)
1202 update_monitor_cnt(item);
1204 return LB_STATUS_SUCCESS;
1207 HAPI int lb_is_pinned_up(const char *pkgname, const char *id)
1210 struct instance *inst;
1213 inst = so_find_instance(pkgname, id);
1215 ErrPrint("Instance %s - %s is not created\n", pkgname, id);
1216 return LB_STATUS_ERROR_INVALID;
1219 l = find_item(inst);
1221 ErrPrint("Instance is not found(%s - %s)\n", pkgname, id);
1222 return LB_STATUS_ERROR_NOT_EXIST;
1225 item = eina_list_data_get(l);
1227 ErrPrint("Invalid item(%s - %s)\n", pkgname, id);
1228 return LB_STATUS_ERROR_FAULT;
1233 * Maybe this is not neccessary for this operation
1235 return so_is_pinned_up(inst);
1238 HAPI int lb_change_group(const char *pkgname, const char *id, const char *cluster, const char *category)
1241 struct instance *inst;
1245 inst = so_find_instance(pkgname, id);
1247 ErrPrint("Instance %s - %s is not created (cluster[%s], category[%s])\n", pkgname, id, cluster, category);
1248 return LB_STATUS_ERROR_INVALID;
1251 l = find_item(inst);
1253 ErrPrint("Instance is not found(%s - %s, cluster[%s], category[%s])\n", pkgname, id, cluster, category);
1254 return LB_STATUS_ERROR_NOT_EXIST;
1257 item = eina_list_data_get(l);
1259 ret = so_change_group(inst, cluster, category);
1263 if (ret & NEED_TO_SCHEDULE) {
1264 DbgPrint("%s Returns NEED_TO_SCHEDULE\n", pkgname);
1265 (void)append_pending_list(item);
1268 if (ret & OUTPUT_UPDATED)
1269 update_monitor_cnt(item);
1271 return LB_STATUS_SUCCESS;
1274 static inline int lb_sys_event(struct instance *inst, struct item *item, int event)
1278 ret = so_sys_event(inst, event);
1282 if (ret & NEED_TO_SCHEDULE)
1283 (void)append_pending_list(item);
1285 if (ret & OUTPUT_UPDATED)
1286 update_monitor_cnt(item);
1288 return LB_STATUS_SUCCESS;
1291 HAPI int lb_system_event(const char *pkgname, const char *id, int event)
1294 struct instance *inst;
1297 inst = so_find_instance(pkgname, id);
1299 ErrPrint("instance %s - %s is not created\n");
1300 return LB_STATUS_ERROR_INVALID;
1303 l = find_item(inst);
1305 ErrPrint("Instance is not found(%s - %s)\n", pkgname, id);
1306 return LB_STATUS_ERROR_NOT_EXIST;
1309 item = eina_list_data_get(l);
1310 return lb_sys_event(inst, item, event);
1313 HAPI int lb_update(const char *pkgname, const char *id)
1316 struct instance *inst;
1319 inst = so_find_instance(pkgname, id);
1321 ErrPrint("Instance %s - %s is not created\n", pkgname, id);
1322 return LB_STATUS_ERROR_INVALID;
1325 l = find_item(inst);
1327 ErrPrint("Instance is not found(%s - %s)\n", pkgname, id);
1328 return LB_STATUS_ERROR_NOT_EXIST;
1331 item = eina_list_data_get(l);
1332 (void)append_pending_list(item);
1333 return LB_STATUS_SUCCESS;
1336 HAPI int lb_update_all(const char *pkgname, const char *cluster, const char *category)
1342 DbgPrint("Update content for %s\n", pkgname ? pkgname : "(all)");
1343 EINA_LIST_FOREACH_SAFE(s_info.item_list, l, n, item) {
1347 if (cluster && strcasecmp(item->inst->cluster, cluster))
1350 if (category && strcasecmp(item->inst->category, category))
1353 if (pkgname && strlen(pkgname)) {
1354 if (!strcmp(item->inst->item->pkgname, pkgname)) {
1355 (void)append_pending_list(item);
1358 (void)append_pending_list(item);
1362 return LB_STATUS_SUCCESS;
1365 HAPI int lb_delete_all_deleteme(void)
1372 EINA_LIST_FOREACH_SAFE(s_info.item_list, l, n, item) {
1373 if (!item->deleteme)
1376 s_info.item_list = eina_list_remove(s_info.item_list, item);
1378 update_monitor_del(item->inst->id, item);
1379 (void)so_destroy(item->inst);
1384 DbgPrint("Delete all deleteme: %d\n", cnt);
1385 return LB_STATUS_SUCCESS;
1388 HAPI int lb_system_event_all(int event)
1394 EINA_LIST_FOREACH_SAFE(s_info.item_list, l, n, item) {
1398 DbgPrint("System event for %s (%d)\n", item->inst->id, event);
1399 lb_sys_event(item->inst, item, event);
1402 return LB_STATUS_SUCCESS;
1405 HAPI void lb_pause_all(void)
1412 pending_timer_freeze();
1414 EINA_LIST_FOREACH(s_info.item_list, l, item) {
1415 if (item->deleteme) {
1416 DbgPrint("Instance %s skip timer pause (deleteme)\n", item->inst->item->pkgname);
1420 if (item->is_paused)
1425 lb_sys_event(item->inst, item, LB_SYS_EVENT_PAUSED);
1429 HAPI void lb_resume_all(void)
1436 pending_timer_thaw();
1438 EINA_LIST_FOREACH(s_info.item_list, l, item) {
1439 if (item->deleteme) {
1440 DbgPrint("Instance %s skip timer resume (deleteme)\n", item->inst->item->pkgname);
1444 if (item->is_paused)
1449 lb_sys_event(item->inst, item, LB_SYS_EVENT_RESUMED);
1453 HAPI int lb_pause(const char *pkgname, const char *id)
1455 struct instance *inst;
1459 inst = so_find_instance(pkgname, id);
1461 return LB_STATUS_ERROR_INVALID;
1463 l = find_item(inst);
1465 ErrPrint("Instance is not found (%s - %s)\n", pkgname, id);
1466 return LB_STATUS_ERROR_NOT_EXIST;
1469 item = eina_list_data_get(l);
1471 return LB_STATUS_ERROR_FAULT;
1473 if (item->deleteme) {
1474 DbgPrint("Instance %s will be deleted (%s)\n", item->inst->item->pkgname, item->inst->id);
1475 return LB_STATUS_ERROR_BUSY;
1478 item->is_paused = 1;
1481 return LB_STATUS_SUCCESS;
1485 lb_sys_event(inst, item, LB_SYS_EVENT_PAUSED);
1487 return LB_STATUS_SUCCESS;
1490 HAPI int lb_resume(const char *pkgname, const char *id)
1492 struct instance *inst;
1496 inst = so_find_instance(pkgname, id);
1498 return LB_STATUS_ERROR_INVALID;
1500 l = find_item(inst);
1502 ErrPrint("Instance is not found (%s - %s)\n", pkgname, id);
1503 return LB_STATUS_ERROR_NOT_EXIST;
1506 item = eina_list_data_get(l);
1508 return LB_STATUS_ERROR_FAULT;
1510 if (item->deleteme) {
1511 DbgPrint("Instance %s will be deleted (%s)\n", item->inst->item->pkgname, item->inst->id);
1512 return LB_STATUS_ERROR_BUSY;
1515 item->is_paused = 0;
1518 return LB_STATUS_SUCCESS;
1522 lb_sys_event(inst, item, LB_SYS_EVENT_RESUMED);
1523 return LB_STATUS_SUCCESS;
1526 HAPI void lb_turn_secured_on(void)
1531 HAPI int lb_is_all_paused(void)
1533 return s_info.paused;