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"
42 struct instance *inst;
46 double update_interval;
47 int heavy_updating; /* Only for debugging message */
48 int is_paused; /* 1 is paused, 0 is resumed */
59 Eina_List *pending_list;
60 Ecore_Timer *pending_timer;
61 Eina_List *pd_open_pending_list;
62 Ecore_Timer *pd_open_pending_timer;
66 int pending_timer_freezed;
71 .pending_timer = NULL,
72 .pd_open_pending_list = NULL,
73 .pd_open_pending_timer = NULL,
77 .pending_timer_freezed = 0,
80 static Eina_Bool updator_cb(void *data);
82 static void pending_timer_freeze(void)
84 DbgPrint("Freezed Count: %d\n", s_info.pending_timer_freezed);
85 if (s_info.pending_timer && !s_info.pending_timer_freezed) {
86 DbgPrint("Freeze the pending timer\n");
87 ecore_timer_freeze(s_info.pending_timer);
90 s_info.pending_timer_freezed++;
93 static void pending_timer_thaw(void)
95 DbgPrint("Freezed Count: %d\n", s_info.pending_timer_freezed);
96 if (!s_info.pending_timer_freezed)
99 s_info.pending_timer_freezed--;
100 if (s_info.pending_timer && !s_info.pending_timer_freezed) {
101 DbgPrint("Thaw the pending timer\n");
102 ecore_timer_thaw(s_info.pending_timer);
107 * -1 : PD is opened, but not mine
108 * 0 : PD is not opened
109 * 1 : my PD is opened
111 static inline int pd_is_opened(const char *pkgname)
118 EINA_LIST_FOREACH(s_info.pd_list, l, tmp) {
119 if (pkgname && !strcmp(pkgname, tmp)) {
120 DbgPrint("PD(%s) is opened\n", pkgname);
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);
142 DbgPrint("Consuming pended item: %s\n", item->inst->id);
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 DbgPrint("open pd pending list exhausted\n");
154 return ECORE_CALLBACK_CANCEL;
157 static Eina_Bool pended_cmd_consumer_cb(void *data)
161 item = eina_list_nth(s_info.pending_list, 0);
165 if (s_info.update || pd_is_opened(item->inst->item->pkgname) < 0)
166 return ECORE_CALLBACK_RENEW;
168 s_info.pending_list = eina_list_remove(s_info.pending_list, item);
169 DbgPrint("Consuming pended item: %s\n", item->inst->id);
172 * To prevent from checking the is_updated function
174 (void)updator_cb(item);
175 if (s_info.pending_list)
176 return ECORE_CALLBACK_RENEW;
179 s_info.pending_timer = NULL;
180 s_info.pending_timer_freezed = 0;
181 DbgPrint("pending list exhausted\n");
182 return ECORE_CALLBACK_CANCEL;
185 static inline __attribute__((always_inline)) int activate_pending_consumer(void)
187 if (s_info.pending_timer)
190 s_info.pending_timer = ecore_timer_add(0.000001f, pended_cmd_consumer_cb, NULL);
191 if (!s_info.pending_timer) {
192 ErrPrint("Failed to add a new pended command consumer\n");
193 return LB_STATUS_ERROR_FAULT;
197 * Do not increase the freezed counter.
198 * Just freeze the timer.
200 if (s_info.pending_timer_freezed) {
201 DbgPrint("Pending timer created and freezed\n");
202 ecore_timer_freeze(s_info.pending_timer);
208 static inline void deactivate_pending_consumer(void)
210 if (!s_info.pending_timer)
213 ecore_timer_del(s_info.pending_timer);
214 s_info.pending_timer = NULL;
215 s_info.pending_timer_freezed = 0;
216 DbgPrint("Clear the pending timer\n");
219 static inline void deactivate_pd_open_pending_consumer(void)
221 if (!s_info.pd_open_pending_timer)
224 ecore_timer_del(s_info.pd_open_pending_timer);
225 s_info.pd_open_pending_timer = NULL;
226 DbgPrint("Clear the open_pd_pending timer\n");
229 static inline int __attribute__((always_inline)) activate_pd_open_pending_consumer(void)
231 if (s_info.pd_open_pending_timer)
234 s_info.pd_open_pending_timer = ecore_timer_add(0.000001f, pd_open_pended_cmd_consumer_cb, NULL);
235 if (!s_info.pd_open_pending_timer) {
236 ErrPrint("Failed to add a new pended command consumer\n");
237 return LB_STATUS_ERROR_FAULT;
243 static inline void migrate_to_pd_open_pending_list(const char *pkgname)
250 EINA_LIST_FOREACH_SAFE(s_info.pending_list, l, n, item) {
251 if (strcmp(pkgname, item->inst->item->pkgname))
254 s_info.pending_list = eina_list_remove(s_info.pending_list, item);
255 s_info.pd_open_pending_list = eina_list_append(s_info.pd_open_pending_list, item);
259 if (s_info.pd_open_pending_list) {
260 DbgPrint("Activate PD open pending consumer (%s)\n", pkgname);
261 activate_pd_open_pending_consumer();
264 if (!s_info.pending_list)
265 deactivate_pending_consumer();
267 DbgPrint("%d items are migrated\n", cnt);
270 static inline void migrate_to_pending_list(const char *pkgname)
277 EINA_LIST_FOREACH_SAFE(s_info.pd_open_pending_list, l, n, item) {
278 if (strcmp(pkgname, item->inst->item->pkgname))
281 s_info.pd_open_pending_list = eina_list_remove(s_info.pd_open_pending_list, item);
282 s_info.pending_list = eina_list_append(s_info.pending_list, item);
286 if (s_info.pending_list)
287 activate_pending_consumer();
289 if (!s_info.pd_open_pending_list)
290 deactivate_pd_open_pending_consumer();
292 DbgPrint("%d items are migrated\n", cnt);
295 static inline int append_pending_list(struct item *item)
297 if (pd_is_opened(item->inst->item->pkgname) == 1) {
298 if (eina_list_data_find(s_info.pd_open_pending_list, item) == item) {
299 DbgPrint("Already pended - %s\n", item->inst->item->pkgname);
300 return LB_STATUS_ERROR_EXIST;
303 DbgPrint("Activate PD open pending consumer (%s)\n", item->inst->item->pkgname);
304 if (activate_pd_open_pending_consumer() < 0) {
305 ErrPrint("Failed to activate PD open pending consumer\n");
306 return LB_STATUS_ERROR_FAULT;
309 s_info.pd_open_pending_list = eina_list_append(s_info.pd_open_pending_list, item);
311 if (eina_list_data_find(s_info.pending_list, item) == item) {
312 DbgPrint("Already pended - %s\n", item->inst->item->pkgname);
313 return LB_STATUS_ERROR_EXIST;
316 if (activate_pending_consumer() < 0)
317 return LB_STATUS_ERROR_FAULT;
319 s_info.pending_list = eina_list_append(s_info.pending_list, item);
324 static inline void timer_thaw(struct item *item)
331 ecore_timer_thaw(item->timer);
332 period = ecore_timer_interval_get(item->timer);
333 pending = ecore_timer_pending_get(item->timer);
334 delay = util_time_delay_for_compensation(period) - pending;
335 ecore_timer_delay(item->timer, delay);
336 DbgPrint("Compensated: %lf\n", delay);
338 if (item->sleep_at == 0.0f)
341 sleep_time = util_timestamp() - item->sleep_at;
342 if (sleep_time > pending) {
343 DbgPrint("Update time elapsed\n");
344 (void)updator_cb(item);
347 item->sleep_at = 0.0f;
350 static inline void timer_freeze(struct item *item)
353 ecore_timer_freeze(item->timer);
355 if (ecore_timer_interval_get(item->timer) <= 1.0f)
358 if (gettimeofday(&tv, NULL) < 0) {
359 ErrPrint("gettimeofday: %s\n", strerror(errno));
364 item->sleep_at = (double)tv.tv_sec + (double)tv.tv_usec / 1000000.0f;
367 static inline void update_monitor_cnt(struct item *item)
372 now = util_timestamp();
373 interval = now - item->update_interval;
377 * If the content update is processed in too short time,
378 * don't increase the monitor counter, instead of it
379 * set the heavy updating flag.
380 * And handling this heavy updating from the
381 * file update callback.
383 if (interval >= MINIMUM_UPDATE_INTERVAL)
386 item->heavy_updating = 1;
388 item->update_interval = now;
391 static inline Eina_List *find_item(struct instance *inst)
396 EINA_LIST_FOREACH(s_info.item_list, l, item) {
397 if (item->inst == inst)
404 static inline int output_handler(struct item *item)
409 if (item->monitor_cnt < 0 || item->heavy_updating) {
410 if (!item->heavy_updating) {
411 WarnPrint("%s has invalid monitor_cnt\n", item->inst->id);
414 item->heavy_updating = 0; /* Reset flag */
417 item->monitor_cnt = 0;
420 if (item->monitor_cnt == 0) {
422 fault_unmark_call(item->inst->item->pkgname, item->inst->id, "update,crashed", NO_ALARM);
425 ecore_timer_del(item->monitor);
426 item->monitor = NULL;
429 if (s_info.update == item)
430 s_info.update = NULL;
432 if (item->deleteme) {
433 provider_send_deleted(item->inst->item->pkgname, item->inst->id);
434 (void)so_destroy(item->inst);
443 static int desc_updated_cb(const char *filename, void *data, int over)
448 WarnPrint("Event Q overflow\n");
452 DbgPrint("DESC %s is updated\n", filename);
453 if (item->is_pd_show) {
454 provider_send_desc_updated(item->inst->item->pkgname, item->inst->id, filename);
456 ErrPrint("But PD is not opened, Ignore this update (%s)\n", item->inst->id);
461 static int file_updated_cb(const char *filename, void *data, int over)
467 char *content = NULL;
472 WarnPrint("Event Q overflow\n");
476 ret = util_get_filesize(filename);
478 ErrPrint("Content is updated. but invalid. ret = %d (Update is ignored)\n", ret);
479 return EXIT_SUCCESS; /*!< To keep the callback */
482 ret = so_get_output_info(item->inst, &w, &h, &priority, &content, &title);
484 ErrPrint("livebox_get_info returns %d\n", ret);
485 return EXIT_SUCCESS; /*!< To keep the callback */
488 if (!item->inst->item->has_livebox_script || (item->inst->item->has_livebox_script && item->is_lb_show)) {
489 provider_send_updated(item->inst->item->pkgname, item->inst->id,
490 item->inst->w, item->inst->h, item->inst->priority, content, title);
492 DbgPrint("Livebox script is not ready yet\n");
493 item->is_lb_updated++;
496 return output_handler(item);
499 static inline int clear_from_pd_open_pending_list(struct item *item)
504 EINA_LIST_FOREACH(s_info.pd_open_pending_list, l, tmp) {
508 s_info.pd_open_pending_list = eina_list_remove_list(s_info.pd_open_pending_list, l);
509 if (!s_info.pd_open_pending_list)
510 deactivate_pd_open_pending_consumer();
511 return LB_STATUS_SUCCESS;
514 return LB_STATUS_ERROR_NOT_EXIST;
517 static inline int clear_from_pending_list(struct item *item)
522 EINA_LIST_FOREACH(s_info.pending_list, l, tmp) {
526 s_info.pending_list = eina_list_remove_list(s_info.pending_list, l);
527 if (!s_info.pending_list)
528 deactivate_pending_consumer();
529 return LB_STATUS_SUCCESS;
532 return LB_STATUS_ERROR_NOT_EXIST;
535 static Eina_Bool update_timeout_cb(void *data)
541 DbgPrint("UPDATE TIMEOUT ========> %s - %s\n", item->inst->item->pkgname, item->inst->id);
543 if (s_info.update != item)
544 ErrPrint("Updating item is not matched\n");
546 fault_unmark_call(item->inst->item->pkgname, item->inst->id, "update,crashed", NO_ALARM);
547 fault_mark_call(item->inst->item->pkgname, item->inst->id, "update,timeout", NO_ALARM, DEFAULT_LIFE_TIMER);
548 s_info.update = NULL;
551 return ECORE_CALLBACK_CANCEL;
554 static Eina_Bool updator_cb(void *data)
561 if (item->monitor) {/*!< If this item is already in update process */
562 return ECORE_CALLBACK_RENEW;
565 ret = so_is_updated(item->inst);
567 if (so_need_to_destroy(item->inst) == NEED_TO_DESTROY) {
568 provider_send_deleted(item->inst->item->pkgname, item->inst->id);
569 lb_destroy(item->inst->item->pkgname, item->inst->id);
571 ecore_timer_del(item->timer);
573 return ECORE_CALLBACK_CANCEL;
576 return ECORE_CALLBACK_RENEW;
579 if (s_info.update || pd_is_opened(item->inst->item->pkgname) < 0) {
580 DbgPrint("%s is busy\n", s_info.update ? s_info.update->inst->id : item->inst->id);
581 (void)append_pending_list(item);
582 return ECORE_CALLBACK_RENEW;
585 item->monitor = ecore_timer_add(item->inst->item->timeout, update_timeout_cb, item);
586 if (!item->monitor) {
587 ErrPrint("Failed to add update monitor %s(%s):%d\n",
588 item->inst->item->pkgname, item->inst->id, item->inst->item->timeout);
589 return ECORE_CALLBACK_RENEW;
594 * Counter of the event monitor is only used for asynchronous content updating,
595 * So reset it to 1 from here because the async updating is started now,
596 * even if it is accumulated by other event function before this.
598 item->monitor_cnt = 1;
600 s_info.update = item;
602 ret = so_update(item->inst);
606 ecore_timer_del(item->monitor);
607 item->monitor = NULL;
608 s_info.update = NULL;
609 return ECORE_CALLBACK_RENEW;
614 * While waiting the Callback function call,
615 * Add this for finding the crash
617 fault_mark_call(item->inst->item->pkgname, item->inst->id, "update,crashed", NO_ALARM, DEFAULT_LIFE_TIMER);
619 if (ret & NEED_TO_SCHEDULE) {
620 (void)append_pending_list(item);
623 if (ret & OUTPUT_UPDATED)
624 update_monitor_cnt(item);
626 return ECORE_CALLBACK_RENEW;
629 static inline void update_monitor_del(const char *id, struct item *item)
634 update_monitor_del_update_cb(util_uri_to_path(id), file_updated_cb);
636 len = strlen(util_uri_to_path(id)) + strlen(".desc") + 1;
639 ErrPrint("Heap: %s (%s.desc)\n", strerror(errno), util_uri_to_path(id));
643 snprintf(tmp, len, "%s.desc", util_uri_to_path(id));
644 update_monitor_del_update_cb(tmp, desc_updated_cb);
648 static inline int add_desc_update_monitor(const char *id, struct item *item)
653 len = strlen(util_uri_to_path(id)) + strlen(".desc") + 1;
654 filename = malloc(len);
656 ErrPrint("Heap: %s (%s.desc)\n", strerror(errno), util_uri_to_path(id));
657 return LB_STATUS_ERROR_MEMORY;
660 snprintf(filename, len, "%s.desc", util_uri_to_path(id));
661 DbgPrint("Add DESC monitor: %s\n", filename);
662 return update_monitor_add_update_cb(filename, desc_updated_cb, item);
665 static inline int add_file_update_monitor(const char *id, struct item *item)
669 filename = strdup(util_uri_to_path(id));
671 ErrPrint("Heap: %s (%s)\n", strerror(errno), id);
672 return LB_STATUS_ERROR_MEMORY;
675 return update_monitor_add_update_cb(filename, file_updated_cb, item);
678 static inline int update_monitor_add(const char *id, struct item *item)
682 * item->inst is not available yet.
684 add_file_update_monitor(id, item);
685 add_desc_update_monitor(id, item);
686 return LB_STATUS_SUCCESS;
689 HAPI int lb_init(void)
691 return LB_STATUS_SUCCESS;
694 HAPI int lb_fini(void)
700 deactivate_pending_consumer();
701 deactivate_pd_open_pending_consumer();
703 EINA_LIST_FREE(s_info.pd_open_pending_list, item);
704 EINA_LIST_FREE(s_info.pending_list, item);
706 EINA_LIST_FOREACH_SAFE(s_info.item_list, l, n, item) {
707 provider_send_deleted(item->inst->item->pkgname, item->inst->id);
708 lb_destroy(item->inst->item->pkgname, item->inst->id);
711 return LB_STATUS_SUCCESS;
716 * Exported API for each liveboxes.
718 const char *livebox_find_pkgname(const char *filename)
723 EINA_LIST_FOREACH(s_info.item_list, l, item) {
724 if (!strcmp(item->inst->id, filename))
725 return item->inst->item->pkgname;
731 int livebox_request_update_by_id(const char *filename)
736 EINA_LIST_FOREACH(s_info.item_list, l, item) {
737 if (!strcmp(item->inst->id, filename)) {
738 return append_pending_list(item);
742 return LB_STATUS_ERROR_NOT_EXIST;
745 HAPI int lb_open_pd(const char *pkgname)
750 EINA_LIST_FOREACH(s_info.pd_list, l, tmp) {
751 if (!strcmp(pkgname, tmp))
755 tmp = strdup(pkgname);
757 ErrPrint("Heap: %s\n", strerror(errno));
758 return LB_STATUS_ERROR_MEMORY;
762 pending_timer_freeze();
764 s_info.pd_list = eina_list_append(s_info.pd_list, tmp);
767 * Find all instances from the pending list.
768 * Move them to pd_open_pending_timer
770 migrate_to_pd_open_pending_list(pkgname);
771 return LB_STATUS_SUCCESS;
774 HAPI int lb_close_pd(const char *pkgname)
780 EINA_LIST_FOREACH_SAFE(s_info.pd_list, l, n, tmp) {
781 if (strcmp(tmp, pkgname))
784 s_info.pd_list = eina_list_remove(s_info.pd_list, tmp);
788 pending_timer_thaw();
791 * Move all items in pd_open_pending_list
794 migrate_to_pending_list(pkgname);
795 return LB_STATUS_SUCCESS;
798 return LB_STATUS_ERROR_NOT_EXIST;
801 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)
803 struct instance *inst;
813 inst = so_find_instance(pkgname, id);
815 DbgPrint("Instance is already exists [%s - %s] content[%s], cluster[%s], category[%s], abi[%s]\n", pkgname, id, content_info, cluster, category, abi);
816 return LB_STATUS_SUCCESS;
819 if (!skip_need_to_create) {
820 ret = so_create_needed(pkgname, cluster, category, abi);
821 if (ret != NEED_TO_CREATE)
822 return LB_STATUS_ERROR_PERMISSION;
827 item = calloc(1, sizeof(*item));
829 ErrPrint("Heap: %s (%s - %s, content[%s], cluster[%s], category[%s], abi[%s])\n", strerror(errno), pkgname, id, content_info, cluster, category, abi);
830 return LB_STATUS_ERROR_MEMORY;
833 ret = update_monitor_add(id, item);
839 DbgPrint("Content: [%s]\n", content_info);
840 create_ret = so_create(pkgname, id, content_info, timeout, has_livebox_script, cluster, category, abi, &inst);
841 if (create_ret < 0) {
842 update_monitor_del(id, item);
853 if (period > 0.0f && !s_info.secured) {
854 item->timer = util_timer_add(period, updator_cb, item);
856 ErrPrint("Failed to add timer (%s - %s, content[%s], cluster[%s], category[%s], abi[%s]\n", pkgname, id, content_info, cluster, category, abi);
857 update_monitor_del(id, item);
860 return LB_STATUS_ERROR_FAULT;
866 DbgPrint("Local update timer is disabled: %lf (%d)\n", period, s_info.secured);
870 s_info.item_list = eina_list_append(s_info.item_list, item);
872 if (create_ret & NEED_TO_SCHEDULE) {
873 DbgPrint("%s Returns NEED_TO_SCHEDULE\n", pkgname);
874 (void)append_pending_list(item);
877 if (create_ret & OUTPUT_UPDATED) {
878 update_monitor_cnt(item);
881 * To send a output info, get the info forcely.
882 * but the output file monitor will do this again
884 * This function will set the tmp_content and tmp_title
885 * even if it has no updates on the content, title,
886 * it will set them to NULL.
888 if (so_get_output_info(inst, w, h, priority, out_content, out_title) == LB_STATUS_SUCCESS) {
892 tmp = strdup(*out_content);
894 ErrPrint("Memory: %s\n", strerror(errno));
902 tmp = strdup(*out_title);
904 ErrPrint("Memory: %s\n", strerror(errno));
913 *priority = inst->priority;
914 return need_to_create;
917 HAPI int lb_destroy(const char *pkgname, const char *id)
920 struct instance *inst;
923 inst = so_find_instance(pkgname, id);
925 ErrPrint("Instance %s - %s is not created\n", pkgname, id);
926 return LB_STATUS_ERROR_INVALID;
931 ErrPrint("Instance is not found (%s - %s)\n", pkgname, id);
932 return LB_STATUS_ERROR_NOT_EXIST;
935 item = eina_list_data_get(l);
936 s_info.item_list = eina_list_remove_list(s_info.item_list, l);
938 if (s_info.update == item)
939 s_info.update = NULL;
942 clear_from_pd_open_pending_list(item);
943 clear_from_pending_list(item);
944 ecore_timer_del(item->timer);
950 update_monitor_del(id, item);
953 if (!item->monitor) {
955 (void)so_destroy(inst);
958 return LB_STATUS_SUCCESS;
961 HAPI int lb_resize(const char *pkgname, const char *id, int w, int h)
964 struct instance *inst;
968 inst = so_find_instance(pkgname, id);
970 ErrPrint("Instance %s - %s is not created (%dx%d)\n", pkgname, id, w, h);
971 return LB_STATUS_ERROR_INVALID;
976 ErrPrint("Instance is not found (%s - %s, %dx%d)\n", pkgname, id, w, h);
977 return LB_STATUS_ERROR_NOT_EXIST;
980 item = eina_list_data_get(l);
982 ret = so_resize(inst, w, h);
986 if (ret & NEED_TO_SCHEDULE) {
987 DbgPrint("%s Returns NEED_TO_SCHEDULE\n", pkgname);
988 (void)append_pending_list(item);
991 if (ret & OUTPUT_UPDATED)
992 update_monitor_cnt(item);
994 return LB_STATUS_SUCCESS;
997 HAPI char *lb_pinup(const char *pkgname, const char *id, int pinup)
999 struct instance *inst;
1002 inst = so_find_instance(pkgname, id);
1004 ErrPrint("Instance %s - %s is not found (pinup[%d])\n", pkgname, id, pinup);
1008 ret = so_pinup(inst, pinup);
1012 HAPI int lb_set_period(const char *pkgname, const char *id, double period)
1015 struct instance *inst;
1018 inst = so_find_instance(pkgname, id);
1020 ErrPrint("Instance %s - %s is not found (period[%lf])\n", pkgname, id, period);
1021 return LB_STATUS_ERROR_INVALID;
1024 l = find_item(inst);
1026 ErrPrint("Instance is not found (%s - %s, period[%lf])\n", pkgname, id, period);
1027 return LB_STATUS_ERROR_NOT_EXIST;
1030 item = eina_list_data_get(l);
1032 if (period <= 0.0f) {
1034 ecore_timer_del(item->timer);
1039 util_timer_interval_set(item->timer, period);
1040 } else if (!s_info.secured) {
1041 item->timer = util_timer_add(period, updator_cb, item);
1043 ErrPrint("Failed to add timer (%s - %s)\n", pkgname, id);
1044 return LB_STATUS_ERROR_FAULT;
1052 return LB_STATUS_SUCCESS;
1055 HAPI int lb_clicked(const char *pkgname, const char *id, const char *event, double timestamp, double x, double y)
1058 struct instance *inst;
1062 inst = so_find_instance(pkgname, id);
1064 ErrPrint("Instance %s - %s is not exists (event[%s])\n", pkgname, id, event);
1065 return LB_STATUS_ERROR_INVALID;
1068 l = find_item(inst);
1070 ErrPrint("Instance is not found (%s - %s, event[%s])\n", pkgname, id, event);
1071 return LB_STATUS_ERROR_NOT_EXIST;
1074 item = eina_list_data_get(l);
1076 ret = so_clicked(inst, event, timestamp, x, y);
1080 if (ret & NEED_TO_SCHEDULE) {
1081 DbgPrint("%s Returns NEED_TO_SCHEDULE\n", pkgname);
1082 (void)append_pending_list(item);
1085 if (ret & OUTPUT_UPDATED)
1086 update_monitor_cnt(item);
1088 return LB_STATUS_SUCCESS;
1091 HAPI int lb_script_event(const char *pkgname, const char *id, const char *emission, const char *source, struct event_info *event_info)
1094 struct instance *inst;
1098 inst = so_find_instance(pkgname, id);
1100 ErrPrint("Instance %s - %s is not exists (emission[%s], source[%s])\n", pkgname, id, emission, source);
1101 return LB_STATUS_ERROR_INVALID;
1104 l = find_item(inst);
1106 ErrPrint("Instance is not found (%s - %s, emissino[%s], source[%s])\n", pkgname, id, emission, source);
1107 return LB_STATUS_ERROR_NOT_EXIST;
1110 item = eina_list_data_get(l);
1112 DbgPrint("source(%s) emission(%s) %d\n", source, emission, item->inst->item->has_livebox_script);
1113 if (emission && source && !strcmp(source, util_uri_to_path(id))) {
1114 if (item->inst->item->has_livebox_script) {
1115 if (!strcmp(emission, "lb,show")) {
1116 DbgPrint("Livebox(%s) script is ready now\n", id);
1117 item->is_lb_show = 1;
1119 DbgPrint("Updated %d times, (content: %s), (title: %s)\n", item->is_lb_updated, item->inst->content, item->inst->title);
1120 if (item->is_lb_updated) {
1121 provider_send_updated(item->inst->item->pkgname, item->inst->id,
1122 item->inst->w, item->inst->h, item->inst->priority, item->inst->content, item->inst->title);
1123 item->is_lb_updated = 0;
1125 } else if (!strcmp(emission, "lb,hide")) {
1126 DbgPrint("Livebox(%s) script is hide now\n", id);
1127 item->is_lb_show = 0;
1131 if (!strcmp(emission, "pd,show")) {
1132 item->is_pd_show = 1;
1133 } else if (!strcmp(emission, "pd,hide")) {
1134 item->is_pd_show = 0;
1138 ret = so_script_event(inst, emission, source, event_info);
1142 if (ret & NEED_TO_SCHEDULE) {
1143 DbgPrint("%s Returns NEED_TO_SCHEDULE\n", pkgname);
1144 (void)append_pending_list(item);
1147 if (ret & OUTPUT_UPDATED)
1148 update_monitor_cnt(item);
1150 return LB_STATUS_SUCCESS;
1153 HAPI int lb_is_pinned_up(const char *pkgname, const char *id)
1156 struct instance *inst;
1159 inst = so_find_instance(pkgname, id);
1161 ErrPrint("Instance %s - %s is not created\n", pkgname, id);
1162 return LB_STATUS_ERROR_INVALID;
1165 l = find_item(inst);
1167 ErrPrint("Instance is not found(%s - %s)\n", pkgname, id);
1168 return LB_STATUS_ERROR_NOT_EXIST;
1171 item = eina_list_data_get(l);
1173 ErrPrint("Invalid item(%s - %s)\n", pkgname, id);
1174 return LB_STATUS_ERROR_FAULT;
1179 * Maybe this is not neccessary for this operation
1181 return so_is_pinned_up(inst);
1184 HAPI int lb_change_group(const char *pkgname, const char *id, const char *cluster, const char *category)
1187 struct instance *inst;
1191 inst = so_find_instance(pkgname, id);
1193 ErrPrint("Instance %s - %s is not created (cluster[%s], category[%s])\n", pkgname, id, cluster, category);
1194 return LB_STATUS_ERROR_INVALID;
1197 l = find_item(inst);
1199 ErrPrint("Instance is not found(%s - %s, cluster[%s], category[%s])\n", pkgname, id, cluster, category);
1200 return LB_STATUS_ERROR_NOT_EXIST;
1203 item = eina_list_data_get(l);
1205 ret = so_change_group(inst, cluster, category);
1209 if (ret & NEED_TO_SCHEDULE) {
1210 DbgPrint("%s Returns NEED_TO_SCHEDULE\n", pkgname);
1211 (void)append_pending_list(item);
1214 if (ret & OUTPUT_UPDATED)
1215 update_monitor_cnt(item);
1217 return LB_STATUS_SUCCESS;
1220 static inline int lb_sys_event(struct instance *inst, struct item *item, int event)
1224 ret = so_sys_event(inst, event);
1228 if (ret & NEED_TO_SCHEDULE)
1229 (void)append_pending_list(item);
1231 if (ret & OUTPUT_UPDATED)
1232 update_monitor_cnt(item);
1234 return LB_STATUS_SUCCESS;
1237 HAPI int lb_system_event(const char *pkgname, const char *id, int event)
1240 struct instance *inst;
1243 inst = so_find_instance(pkgname, id);
1245 ErrPrint("instance %s - %s is not created\n");
1246 return LB_STATUS_ERROR_INVALID;
1249 l = find_item(inst);
1251 ErrPrint("Instance is not found(%s - %s)\n", pkgname, id);
1252 return LB_STATUS_ERROR_NOT_EXIST;
1255 item = eina_list_data_get(l);
1256 return lb_sys_event(inst, item, event);
1259 HAPI int lb_update(const char *pkgname, const char *id)
1262 struct instance *inst;
1265 inst = so_find_instance(pkgname, id);
1267 ErrPrint("Instance %s - %s is not created\n", pkgname, id);
1268 return LB_STATUS_ERROR_INVALID;
1271 l = find_item(inst);
1273 ErrPrint("Instance is not found(%s - %s)\n", pkgname, id);
1274 return LB_STATUS_ERROR_NOT_EXIST;
1277 item = eina_list_data_get(l);
1278 (void)append_pending_list(item);
1279 return LB_STATUS_SUCCESS;
1282 HAPI int lb_update_all(const char *pkgname, const char *cluster, const char *category)
1288 DbgPrint("Update content for %s\n", pkgname ? pkgname : "(all)");
1289 EINA_LIST_FOREACH_SAFE(s_info.item_list, l, n, item) {
1293 if (cluster && strcasecmp(item->inst->cluster, cluster))
1296 if (category && strcasecmp(item->inst->category, category))
1299 if (pkgname && strlen(pkgname)) {
1300 if (!strcmp(item->inst->item->pkgname, pkgname)) {
1301 (void)append_pending_list(item);
1304 (void)append_pending_list(item);
1308 return LB_STATUS_SUCCESS;
1311 HAPI int lb_system_event_all(int event)
1317 EINA_LIST_FOREACH_SAFE(s_info.item_list, l, n, item) {
1321 DbgPrint("System event for %s (%d)\n", item->inst->id, event);
1322 lb_sys_event(item->inst, item, event);
1325 return LB_STATUS_SUCCESS;
1328 HAPI void lb_pause_all(void)
1335 pending_timer_freeze();
1337 EINA_LIST_FOREACH(s_info.item_list, l, item) {
1338 if (item->deleteme) {
1339 DbgPrint("Instance %s skip timer pause (deleteme)\n", item->inst->item->pkgname);
1343 if (item->is_paused) {
1344 DbgPrint("Instance %s is already paused\n", item->inst->id);
1349 DbgPrint("Instance %s freeze timer\n", item->inst->item->pkgname);
1352 DbgPrint("Instance %s has no timer\n", item->inst->item->pkgname);
1355 lb_sys_event(item->inst, item, LB_SYS_EVENT_PAUSED);
1359 HAPI void lb_resume_all(void)
1366 pending_timer_thaw();
1368 EINA_LIST_FOREACH(s_info.item_list, l, item) {
1369 if (item->deleteme) {
1370 DbgPrint("Instance %s skip timer resume (deleteme)\n", item->inst->item->pkgname);
1374 if (item->is_paused) {
1375 DbgPrint("Instance %s is still paused\n", item->inst->id);
1380 DbgPrint("Instance %s resume timer\n", item->inst->item->pkgname);
1384 lb_sys_event(item->inst, item, LB_SYS_EVENT_RESUMED);
1388 HAPI int lb_pause(const char *pkgname, const char *id)
1390 struct instance *inst;
1394 inst = so_find_instance(pkgname, id);
1396 return LB_STATUS_ERROR_INVALID;
1398 l = find_item(inst);
1400 ErrPrint("Instance is not found (%s - %s)\n", pkgname, id);
1401 return LB_STATUS_ERROR_NOT_EXIST;
1404 item = eina_list_data_get(l);
1406 return LB_STATUS_ERROR_FAULT;
1408 if (item->deleteme) {
1409 DbgPrint("Instance %s will be deleted (%s)\n", item->inst->item->pkgname, item->inst->id);
1410 return LB_STATUS_ERROR_BUSY;
1413 item->is_paused = 1;
1415 if (s_info.paused) {
1416 DbgPrint("Already paused: %s\n", item->inst->id);
1417 return LB_STATUS_SUCCESS;
1423 lb_sys_event(inst, item, LB_SYS_EVENT_PAUSED);
1425 return LB_STATUS_SUCCESS;
1428 HAPI int lb_resume(const char *pkgname, const char *id)
1430 struct instance *inst;
1434 inst = so_find_instance(pkgname, id);
1436 return LB_STATUS_ERROR_INVALID;
1438 l = find_item(inst);
1440 ErrPrint("Instance is not found (%s - %s)\n", pkgname, id);
1441 return LB_STATUS_ERROR_NOT_EXIST;
1444 item = eina_list_data_get(l);
1446 return LB_STATUS_ERROR_FAULT;
1448 if (item->deleteme) {
1449 DbgPrint("Instance %s will be deleted (%s)\n", item->inst->item->pkgname, item->inst->id);
1450 return LB_STATUS_ERROR_BUSY;
1453 item->is_paused = 0;
1455 if (s_info.paused) {
1456 DbgPrint("Instance %s is still paused\n", item->inst->id);
1457 return LB_STATUS_SUCCESS;
1463 lb_sys_event(inst, item, LB_SYS_EVENT_RESUMED);
1464 return LB_STATUS_SUCCESS;
1467 HAPI void lb_turn_secured_on(void)
1472 HAPI int lb_is_all_paused(void)
1474 return s_info.paused;