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());
859 * Some case requires to update the content of other box from livebox_XXX ABI.
860 * In that case this function can be used so we have not to filter it from here.
861 * ex) Setting accessibility.
862 * Press the assistive light, turned on, need to update other instances too.
863 * Then the box will use this function from livebox_clicked function.
867 EINA_LIST_FOREACH(s_info.item_list, l, item) {
868 if (!strcmp(item->inst->id, filename)) {
869 return append_pending_list(item);
873 return LB_STATUS_ERROR_NOT_EXIST;
876 int livebox_trigger_update_monitor(const char *filename, int is_pd)
881 if (so_current_op() != LIVEBOX_OP_UNKNOWN) {
882 ErrPrint("Current operation: %d\n", so_current_op());
883 return LB_STATUS_ERROR_INVALID;
888 len = strlen(filename) + strlen(".desc");
890 fname = malloc(len + 1);
892 ErrPrint("Heap: %s\n", strerror(errno));
893 return LB_STATUS_ERROR_MEMORY;
896 snprintf(fname, len, "%s.desc", filename);
898 fname = strdup(filename);
900 ErrPrint("Heap: %s\n", strerror(errno));
901 return LB_STATUS_ERROR_MEMORY;
905 if (access(fname, R_OK | W_OK) != 0) {
906 ErrPrint("access: %s (%s)\n", fname, strerror(errno));
907 ret = LB_STATUS_ERROR_IO;
909 ret = update_monitor_trigger_update_cb(fname, 0);
916 HAPI int lb_open_pd(const char *pkgname)
921 EINA_LIST_FOREACH(s_info.pd_list, l, tmp) {
922 if (!strcmp(pkgname, tmp)) {
927 tmp = strdup(pkgname);
929 ErrPrint("Heap: %s\n", strerror(errno));
930 return LB_STATUS_ERROR_MEMORY;
933 if (!s_info.pd_list) {
934 pending_timer_freeze();
937 s_info.pd_list = eina_list_append(s_info.pd_list, tmp);
940 * Find all instances from the pending list.
941 * Move them to pd_open_pending_timer
943 migrate_to_pd_open_pending_list(pkgname);
944 return LB_STATUS_SUCCESS;
947 HAPI int lb_close_pd(const char *pkgname)
953 EINA_LIST_FOREACH_SAFE(s_info.pd_list, l, n, tmp) {
954 if (strcmp(tmp, pkgname)) {
958 s_info.pd_list = eina_list_remove(s_info.pd_list, tmp);
961 if (!s_info.pd_list) {
962 pending_timer_thaw();
966 * Move all items in pd_open_pending_list
969 migrate_to_pending_list(pkgname);
970 return LB_STATUS_SUCCESS;
973 return LB_STATUS_ERROR_NOT_EXIST;
976 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)
978 struct instance *inst;
988 inst = so_find_instance(pkgname, id);
990 DbgPrint("Instance is already exists [%s - %s] content[%s], cluster[%s], category[%s], abi[%s]\n", pkgname, id, content_info, cluster, category, abi);
991 return LB_STATUS_SUCCESS;
994 if (!skip_need_to_create) {
995 ret = so_create_needed(pkgname, cluster, category, abi);
996 if (ret != NEED_TO_CREATE) {
997 return LB_STATUS_ERROR_PERMISSION;
1003 item = calloc(1, sizeof(*item));
1005 ErrPrint("Heap: %s (%s - %s, content[%s], cluster[%s], category[%s], abi[%s])\n", strerror(errno), pkgname, id, content_info, cluster, category, abi);
1006 return LB_STATUS_ERROR_MEMORY;
1009 ret = update_monitor_add(id, item);
1015 create_ret = so_create(pkgname, id, content_info, timeout, has_livebox_script, cluster, category, abi, &inst);
1016 if (create_ret < 0) {
1017 update_monitor_del(id, item);
1028 if (period > 0.0f && !s_info.secured) {
1029 item->timer = util_timer_add(period, updator_cb, item);
1031 ErrPrint("Failed to add timer (%s - %s, content[%s], cluster[%s], category[%s], abi[%s]\n", pkgname, id, content_info, cluster, category, abi);
1032 update_monitor_del(id, item);
1033 (void)so_destroy(inst);
1035 return LB_STATUS_ERROR_FAULT;
1038 if (s_info.paused) {
1042 DbgPrint("Local update timer is disabled: %lf (%d)\n", period, s_info.secured);
1046 s_info.item_list = eina_list_append(s_info.item_list, item);
1048 if (create_ret & NEED_TO_SCHEDULE) {
1049 DbgPrint("%s Returns NEED_TO_SCHEDULE\n", pkgname);
1050 (void)append_pending_list(item);
1053 if (create_ret & OUTPUT_UPDATED) {
1054 update_monitor_cnt(item);
1057 * To send a output info, get the info forcely.
1058 * but the output file monitor will do this again
1060 * This function will set the tmp_content and tmp_title
1061 * even if it has no updates on the content, title,
1062 * it will set them to NULL.
1064 if (so_get_output_info(inst, w, h, priority, out_content, out_title) == LB_STATUS_SUCCESS) {
1068 tmp = strdup(*out_content);
1070 ErrPrint("Memory: %s\n", strerror(errno));
1079 tmp = strdup(*out_title);
1081 ErrPrint("Memory: %s\n", strerror(errno));
1091 *priority = inst->priority;
1092 return need_to_create;
1095 HAPI int lb_destroy(const char *pkgname, const char *id)
1098 struct instance *inst;
1101 inst = so_find_instance(pkgname, id);
1103 ErrPrint("Instance %s - %s is not created\n", pkgname, id);
1104 return LB_STATUS_ERROR_INVALID;
1107 l = find_item(inst);
1109 ErrPrint("Instance is not found (%s - %s)\n", pkgname, id);
1110 return LB_STATUS_ERROR_NOT_EXIST;
1113 item = eina_list_data_get(l);
1114 s_info.item_list = eina_list_remove_list(s_info.item_list, l);
1116 if (s_info.update == item) {
1117 s_info.update = NULL;
1121 clear_from_pd_open_pending_list(item);
1122 clear_from_pending_list(item);
1123 ecore_timer_del(item->timer);
1126 if (item->monitor) {
1129 update_monitor_del(id, item);
1133 if (!item->monitor) {
1135 (void)so_destroy(inst);
1138 return LB_STATUS_SUCCESS;
1141 HAPI int lb_resize(const char *pkgname, const char *id, int w, int h)
1144 struct instance *inst;
1148 inst = so_find_instance(pkgname, id);
1150 ErrPrint("Instance %s - %s is not created (%dx%d)\n", pkgname, id, w, h);
1151 return LB_STATUS_ERROR_INVALID;
1154 l = find_item(inst);
1156 ErrPrint("Instance is not found (%s - %s, %dx%d)\n", pkgname, id, w, h);
1157 return LB_STATUS_ERROR_NOT_EXIST;
1160 item = eina_list_data_get(l);
1162 ret = so_resize(inst, w, h);
1167 if (ret & NEED_TO_SCHEDULE) {
1168 DbgPrint("%s Returns NEED_TO_SCHEDULE\n", pkgname);
1169 (void)append_pending_list(item);
1172 if (ret & OUTPUT_UPDATED) {
1173 update_monitor_cnt(item);
1176 return LB_STATUS_SUCCESS;
1179 HAPI char *lb_pinup(const char *pkgname, const char *id, int pinup)
1181 struct instance *inst;
1184 inst = so_find_instance(pkgname, id);
1186 ErrPrint("Instance %s - %s is not found (pinup[%d])\n", pkgname, id, pinup);
1190 ret = so_pinup(inst, pinup);
1194 HAPI int lb_set_period(const char *pkgname, const char *id, double period)
1197 struct instance *inst;
1200 inst = so_find_instance(pkgname, id);
1202 ErrPrint("Instance %s - %s is not found (period[%lf])\n", pkgname, id, period);
1203 return LB_STATUS_ERROR_INVALID;
1206 l = find_item(inst);
1208 ErrPrint("Instance is not found (%s - %s, period[%lf])\n", pkgname, id, period);
1209 return LB_STATUS_ERROR_NOT_EXIST;
1212 item = eina_list_data_get(l);
1214 if (period <= 0.0f) {
1216 ecore_timer_del(item->timer);
1221 util_timer_interval_set(item->timer, period);
1222 } else if (!s_info.secured) {
1223 item->timer = util_timer_add(period, updator_cb, item);
1225 ErrPrint("Failed to add timer (%s - %s)\n", pkgname, id);
1226 return LB_STATUS_ERROR_FAULT;
1229 if (s_info.paused) {
1235 return LB_STATUS_SUCCESS;
1238 HAPI int lb_clicked(const char *pkgname, const char *id, const char *event, double timestamp, double x, double y)
1241 struct instance *inst;
1245 inst = so_find_instance(pkgname, id);
1247 ErrPrint("Instance %s - %s is not exists (event[%s])\n", pkgname, id, event);
1248 return LB_STATUS_ERROR_INVALID;
1251 l = find_item(inst);
1253 ErrPrint("Instance is not found (%s - %s, event[%s])\n", pkgname, id, event);
1254 return LB_STATUS_ERROR_NOT_EXIST;
1257 item = eina_list_data_get(l);
1259 ret = so_clicked(inst, event, timestamp, x, y);
1264 if (ret & NEED_TO_SCHEDULE) {
1265 DbgPrint("%s Returns NEED_TO_SCHEDULE\n", pkgname);
1266 (void)append_pending_list(item);
1269 if (ret & OUTPUT_UPDATED) {
1270 update_monitor_cnt(item);
1273 return LB_STATUS_SUCCESS;
1276 HAPI int lb_script_event(const char *pkgname, const char *id, const char *emission, const char *source, struct event_info *event_info)
1279 struct instance *inst;
1283 inst = so_find_instance(pkgname, id);
1285 ErrPrint("Instance %s - %s is not exists (emission[%s], source[%s])\n", pkgname, id, emission, source);
1286 return LB_STATUS_ERROR_INVALID;
1289 l = find_item(inst);
1291 ErrPrint("Instance is not found (%s - %s, emissino[%s], source[%s])\n", pkgname, id, emission, source);
1292 return LB_STATUS_ERROR_NOT_EXIST;
1295 item = eina_list_data_get(l);
1297 if (emission && source && !strcmp(source, id)) {
1298 if (item->inst->item->has_livebox_script) {
1299 if (!strcmp(emission, "lb,show")) {
1300 item->is_lb_show = 1;
1302 if (item->is_lb_updated && !is_pended_item(item)) {
1303 reset_lb_updated_flag(item);
1306 source = util_uri_to_path(source);
1307 } else if (!strcmp(emission, "lb,hide")) {
1308 DbgPrint("Livebox(%s) script is hide now\n", id);
1309 item->is_lb_show = 0;
1311 source = util_uri_to_path(source);
1315 if (!strcmp(emission, "pd,show")) {
1316 item->is_pd_show = 1;
1317 source = util_uri_to_path(source);
1318 } else if (!strcmp(emission, "pd,hide")) {
1319 item->is_pd_show = 0;
1320 source = util_uri_to_path(source);
1324 ret = so_script_event(inst, emission, source, event_info);
1329 if (ret & NEED_TO_SCHEDULE) {
1330 DbgPrint("%s Returns NEED_TO_SCHEDULE\n", pkgname);
1331 (void)append_pending_list(item);
1334 if (ret & OUTPUT_UPDATED) {
1335 update_monitor_cnt(item);
1338 return LB_STATUS_SUCCESS;
1341 HAPI int lb_is_pinned_up(const char *pkgname, const char *id)
1344 struct instance *inst;
1347 inst = so_find_instance(pkgname, id);
1349 ErrPrint("Instance %s - %s is not created\n", pkgname, id);
1350 return LB_STATUS_ERROR_INVALID;
1353 l = find_item(inst);
1355 ErrPrint("Instance is not found(%s - %s)\n", pkgname, id);
1356 return LB_STATUS_ERROR_NOT_EXIST;
1359 item = eina_list_data_get(l);
1361 ErrPrint("Invalid item(%s - %s)\n", pkgname, id);
1362 return LB_STATUS_ERROR_FAULT;
1367 * Maybe this is not neccessary for this operation
1369 return so_is_pinned_up(inst);
1372 HAPI int lb_change_group(const char *pkgname, const char *id, const char *cluster, const char *category)
1375 struct instance *inst;
1379 inst = so_find_instance(pkgname, id);
1381 ErrPrint("Instance %s - %s is not created (cluster[%s], category[%s])\n", pkgname, id, cluster, category);
1382 return LB_STATUS_ERROR_INVALID;
1385 l = find_item(inst);
1387 ErrPrint("Instance is not found(%s - %s, cluster[%s], category[%s])\n", pkgname, id, cluster, category);
1388 return LB_STATUS_ERROR_NOT_EXIST;
1391 item = eina_list_data_get(l);
1393 ret = so_change_group(inst, cluster, category);
1398 if (ret & NEED_TO_SCHEDULE) {
1399 DbgPrint("%s Returns NEED_TO_SCHEDULE\n", pkgname);
1400 (void)append_pending_list(item);
1403 if (ret & OUTPUT_UPDATED) {
1404 update_monitor_cnt(item);
1407 return LB_STATUS_SUCCESS;
1410 static int lb_sys_event(struct instance *inst, struct item *item, int event)
1414 ret = so_sys_event(inst, event);
1419 if (ret & NEED_TO_SCHEDULE) {
1420 (void)append_pending_list(item);
1423 if (ret & OUTPUT_UPDATED) {
1424 update_monitor_cnt(item);
1427 return LB_STATUS_SUCCESS;
1430 HAPI int lb_system_event(const char *pkgname, const char *id, int event)
1433 struct instance *inst;
1436 inst = so_find_instance(pkgname, id);
1438 ErrPrint("instance %s - %s is not created\n");
1439 return LB_STATUS_ERROR_INVALID;
1442 l = find_item(inst);
1444 ErrPrint("Instance is not found(%s - %s)\n", pkgname, id);
1445 return LB_STATUS_ERROR_NOT_EXIST;
1448 item = eina_list_data_get(l);
1449 return lb_sys_event(inst, item, event);
1452 HAPI int lb_update(const char *pkgname, const char *id)
1455 struct instance *inst;
1458 inst = so_find_instance(pkgname, id);
1460 ErrPrint("Instance %s - %s is not created\n", pkgname, id);
1461 return LB_STATUS_ERROR_INVALID;
1464 l = find_item(inst);
1466 ErrPrint("Instance is not found(%s - %s)\n", pkgname, id);
1467 return LB_STATUS_ERROR_NOT_EXIST;
1470 item = eina_list_data_get(l);
1471 (void)append_pending_list(item);
1472 return LB_STATUS_SUCCESS;
1475 HAPI int lb_update_all(const char *pkgname, const char *cluster, const char *category)
1481 DbgPrint("Update content for %s\n", pkgname ? pkgname : "(all)");
1482 EINA_LIST_FOREACH_SAFE(s_info.item_list, l, n, item) {
1483 if (item->deleteme) {
1487 if (cluster && strcasecmp(item->inst->cluster, cluster)) {
1491 if (category && strcasecmp(item->inst->category, category)) {
1495 if (pkgname && strlen(pkgname)) {
1496 if (!strcmp(item->inst->item->pkgname, pkgname)) {
1497 (void)append_pending_list(item);
1500 (void)append_pending_list(item);
1504 return LB_STATUS_SUCCESS;
1507 HAPI int lb_delete_all_deleteme(void)
1514 EINA_LIST_FOREACH_SAFE(s_info.item_list, l, n, item) {
1515 if (!item->deleteme) {
1519 update_monitor_del(item->inst->id, item);
1520 (void)so_destroy(item->inst);
1525 DbgPrint("Delete all deleteme: %d\n", cnt);
1526 return LB_STATUS_SUCCESS;
1529 HAPI int lb_system_event_all(int event)
1535 EINA_LIST_FOREACH_SAFE(s_info.item_list, l, n, item) {
1536 if (item->deleteme) {
1540 DbgPrint("System event for %s (%d)\n", item->inst->id, event);
1541 lb_sys_event(item->inst, item, event);
1544 return LB_STATUS_SUCCESS;
1547 HAPI void lb_pause_all(void)
1554 pending_timer_freeze();
1556 EINA_LIST_FOREACH(s_info.item_list, l, item) {
1557 if (item->deleteme) {
1558 DbgPrint("Instance %s skip timer pause (deleteme)\n", item->inst->item->pkgname);
1562 if (item->is_paused) {
1568 lb_sys_event(item->inst, item, LB_SYS_EVENT_PAUSED);
1572 HAPI void lb_resume_all(void)
1580 pending_timer_thaw();
1582 EINA_LIST_FOREACH_SAFE(s_info.item_list, l, n, item) {
1583 if (item->deleteme) {
1584 DbgPrint("Instance %s skip timer resume (deleteme)\n", item->inst->item->pkgname);
1588 if (item->is_paused) {
1592 lb_sys_event(item->inst, item, LB_SYS_EVENT_RESUMED);
1594 if (item->updated_in_pause) {
1595 (void)append_pending_list(item);
1596 item->updated_in_pause = 0;
1601 * After send the resume callback, call this function.
1602 * Because the timer_thaw can call the update function.
1603 * Before resumed event is notified to the livebox,
1604 * Do not call update function
1606 if (timer_thaw(item) == UPDATE_ITEM_DELETED) {
1607 /* item is deleted */
1612 HAPI int lb_pause(const char *pkgname, const char *id)
1614 struct instance *inst;
1618 inst = so_find_instance(pkgname, id);
1620 return LB_STATUS_ERROR_INVALID;
1623 l = find_item(inst);
1625 ErrPrint("Instance is not found (%s - %s)\n", pkgname, id);
1626 return LB_STATUS_ERROR_NOT_EXIST;
1629 item = eina_list_data_get(l);
1631 return LB_STATUS_ERROR_FAULT;
1634 if (item->deleteme) {
1635 DbgPrint("Instance %s will be deleted (%s)\n", item->inst->item->pkgname, item->inst->id);
1636 return LB_STATUS_ERROR_BUSY;
1639 item->is_paused = 1;
1641 if (s_info.paused) {
1642 return LB_STATUS_SUCCESS;
1647 lb_sys_event(inst, item, LB_SYS_EVENT_PAUSED);
1649 return LB_STATUS_SUCCESS;
1652 HAPI int lb_resume(const char *pkgname, const char *id)
1654 struct instance *inst;
1659 inst = so_find_instance(pkgname, id);
1661 return LB_STATUS_ERROR_INVALID;
1664 l = find_item(inst);
1666 ErrPrint("Instance is not found (%s - %s)\n", pkgname, id);
1667 return LB_STATUS_ERROR_NOT_EXIST;
1670 item = eina_list_data_get(l);
1672 return LB_STATUS_ERROR_FAULT;
1675 if (item->deleteme) {
1676 DbgPrint("Instance %s will be deleted (%s)\n", item->inst->item->pkgname, item->inst->id);
1677 return LB_STATUS_ERROR_BUSY;
1680 item->is_paused = 0;
1682 if (s_info.paused) {
1683 return LB_STATUS_SUCCESS;
1686 lb_sys_event(inst, item, LB_SYS_EVENT_RESUMED);
1688 ret = timer_thaw(item);
1689 if (ret == UPDATE_ITEM_DELETED) {
1694 return LB_STATUS_SUCCESS;
1695 } else if (ret == UPDATE_INVOKED) {
1698 * if the update is successfully done, the updated_in_pause will be reset'd.
1699 * or append it to the pending list
1703 if (item->updated_in_pause) {
1704 (void)append_pending_list(item);
1705 item->updated_in_pause = 0;
1708 return LB_STATUS_SUCCESS;
1711 HAPI void lb_turn_secured_on(void)
1716 HAPI int lb_is_all_paused(void)
1718 return s_info.paused;