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.
27 #include <vconf-keys.h>
31 #include <com-core_packet.h>
32 #include <livebox-errno.h>
37 #include "livebox_internal.h"
38 #include "desc_parser.h"
41 #include "master_rpc.h"
43 #include "critical_log.h"
53 static struct packet *master_fault_package(pid_t pid, int handle, const struct packet *packet)
59 if (packet_get(packet, "sss", &pkgname, &id, &function) != 3) {
60 ErrPrint("Invalid arguments\n");
64 master_rpc_clear_fault_package(pkgname);
65 lb_invoke_fault_handler(LB_FAULT_DEACTIVATED, pkgname, id, function);
66 DbgPrint("%s(%s) is deactivated\n", pkgname, id);
70 static struct packet *master_hold_scroll(pid_t pid, int handle, const struct packet *packet)
72 struct livebox *handler;
78 ret = packet_get(packet, "ssi", &pkgname, &id, &seize);
80 ErrPrint("Invalid argument\n");
84 handler = lb_find_livebox(pkgname, id);
86 ErrPrint("Instance(%s) is not exists\n", id);
90 DbgPrint("[HOLD] %s %d\n", id, seize);
91 lb_invoke_event_handler(handler, seize ? LB_EVENT_HOLD_SCROLL : LB_EVENT_RELEASE_SCROLL);
97 static struct packet *master_pinup(pid_t pid, int handle, const struct packet *packet)
102 struct livebox *handler;
108 ret = packet_get(packet, "iisss", &status, &pinup, &pkgname, &id, &content);
110 ErrPrint("Invalid argument\n");
114 handler = lb_find_livebox(pkgname, id);
116 ErrPrint("Instance (%s) is not exists\n", id);
121 new_content = strdup(content);
123 free(handler->content);
124 handler->content = new_content;
125 handler->is_pinned_up = pinup;
127 ErrPrint("Heap: %s\n", strerror(errno));
128 status = LB_STATUS_ERROR_MEMORY;
132 if (handler->pinup_cb) {
136 /* Make sure that user can call pinup API in its result callback */
137 cb = handler->pinup_cb;
138 cbdata = handler->pinup_cbdata;
140 handler->pinup_cb = NULL;
141 handler->pinup_cbdata = NULL;
143 cb(handler, status, cbdata);
144 } else if (status == 0) {
145 lb_invoke_event_handler(handler, LB_EVENT_PINUP_CHANGED);
152 static struct packet *master_deleted(pid_t pid, int handle, const struct packet *packet)
157 struct livebox *handler;
159 if (packet_get(packet, "ssd", &pkgname, &id, ×tamp) != 3) {
160 ErrPrint("Invalid arguemnt\n");
164 handler = lb_find_livebox_by_timestamp(timestamp);
168 * This can be happens only if the user delete a livebox
169 * right after create it before receive created event.
174 /*!< Check validity of this "handler" */
175 if (handler->state != CREATE) {
176 if (handler->state != DELETE) {
179 * This is not possible
181 CRITICAL_LOG("Already deleted handler (%s - %s)\n", pkgname, id);
186 if (handler->created_cb) {
192 * "if (handler->id == NULL)"
194 * The instance is not created yet.
195 * But the master forcely destroy it and send destroyed event to this
196 * without the created event.
198 * It could be destroyed when a slave has critical error(fault)
199 * before creating an instance successfully.
201 if (handler->created_cb == handler->deleted_cb) {
202 if (handler->created_cbdata != handler->deleted_cbdata)
203 DbgPrint("cb is same but cbdata is different (%s - %s)\n", pkgname, id);
205 handler->deleted_cb = NULL;
206 handler->deleted_cbdata = NULL;
209 DbgPrint("Call the created cb with LB_STATUS_ERROR_CANCEL\n");
210 cb = handler->created_cb;
211 cbdata = handler->created_cbdata;
213 handler->created_cb = NULL;
214 handler->created_cbdata = NULL;
216 cb(handler, LB_STATUS_ERROR_CANCEL, cbdata);
217 } else if (handler->id) {
218 if (handler->deleted_cb) {
222 cb = handler->deleted_cb;
223 cbdata = handler->deleted_cbdata;
225 handler->deleted_cb = NULL;
226 handler->deleted_cbdata = NULL;
228 DbgPrint("Call the deleted cb\n");
229 cb(handler, LB_STATUS_SUCCESS, cbdata);
231 DbgPrint("Call the lb,deleted\n");
232 lb_invoke_event_handler(handler, LB_EVENT_DELETED);
236 DbgPrint("[%p] %s(%s) is deleted\n", handler, pkgname, id);
238 /* Just try to delete it, if a user didn't remove it from the live box list */
245 static struct packet *master_lb_update_begin(pid_t pid, int handle, const struct packet *packet)
247 struct livebox *handler;
256 ret = packet_get(packet, "ssdsss", &pkgname, &id, &priority, &content, &title, &fbfile);
258 ErrPrint("Invalid argument\n");
262 handler = lb_find_livebox(pkgname, id);
264 ErrPrint("Instance[%s] is not exists\n", id);
268 if (handler->state != CREATE) {
269 DbgPrint("(%s) is not created\n", id);
273 lb_set_priority(handler, priority);
274 lb_set_content(handler, content);
275 lb_set_title(handler, title);
279 * Width & Height is not changed in this case.
280 * If the active update is began, the size should not be changed,
281 * And if the size is changed, the provider should finish the updating first.
282 * And then begin updating again after change its size.
284 if (lb_get_lb_fb(handler)) {
285 lb_set_lb_fb(handler, fbfile);
287 ret = fb_sync(lb_get_lb_fb(handler));
288 if (ret != LB_STATUS_SUCCESS)
289 ErrPrint("Failed to do sync FB (%s - %s) (%d)\n", pkgname, fbfile, ret);
291 lb_invoke_event_handler(handler, LB_EVENT_LB_UPDATE_BEGIN);
293 ErrPrint("Invalid request[%s], %s\n", id, fbfile);
300 static struct packet *master_pd_update_begin(pid_t pid, int handle, const struct packet *packet)
302 struct livebox *handler;
308 ret = packet_get(packet, "sss", &pkgname, &id, &fbfile);
310 ErrPrint("Invalid argument\n");
314 handler = lb_find_livebox(pkgname, id);
316 ErrPrint("Instance[%s] is not exists\n", id);
320 if (handler->state != CREATE) {
321 DbgPrint("[%s] is not created\n", id);
325 if (lb_get_pd_fb(handler)) {
326 lb_set_lb_fb(handler, fbfile);
328 ret = fb_sync(lb_get_lb_fb(handler));
329 if (ret != LB_STATUS_SUCCESS)
330 ErrPrint("Failed to do sync FB (%s - %s) (%d)\n", pkgname, fbfile, ret);
332 lb_invoke_event_handler(handler, LB_EVENT_PD_UPDATE_BEGIN);
334 ErrPrint("Invalid request[%s], %s\n", id, fbfile);
341 static struct packet *master_lb_update_end(pid_t pid, int handle, const struct packet *packet)
343 struct livebox *handler;
348 ret = packet_get(packet, "ss", &pkgname, &id);
350 ErrPrint("Invalid argument\n");
354 handler = lb_find_livebox(pkgname, id);
356 ErrPrint("Instance[%s] is not exists\n", id);
360 if (handler->state != CREATE) {
361 DbgPrint("[%s] is not created\n", id);
365 if (lb_get_lb_fb(handler)) {
366 lb_invoke_event_handler(handler, LB_EVENT_LB_UPDATE_END);
368 ErrPrint("Invalid request[%s]\n", id);
375 static struct packet *master_access_status(pid_t pid, int handle, const struct packet *packet)
377 struct livebox *handler;
383 ret = packet_get(packet, "ssi", &pkgname, &id, &status);
385 ErrPrint("Invalid argument\n");
389 handler = lb_find_livebox(pkgname, id);
391 ErrPrint("Instance[%s] is not exists\n", id);
395 if (handler->state != CREATE) {
396 DbgPrint("[%s] is not created\n", id);
400 DbgPrint("Access status: %d\n", status);
402 if (handler->access_event_cb) {
406 cb = handler->access_event_cb;
407 cbdata = handler->access_event_cbdata;
409 handler->access_event_cb = NULL;
410 handler->access_event_cbdata = NULL;
412 cb(handler, status, cbdata);
414 ErrPrint("Invalid event[%s]\n", id);
420 static struct packet *master_pd_update_end(pid_t pid, int handle, const struct packet *packet)
422 struct livebox *handler;
427 ret = packet_get(packet, "ss", &pkgname, &id);
429 ErrPrint("Invalid argument\n");
433 handler = lb_find_livebox(pkgname, id);
435 ErrPrint("Instance[%s] is not exists\n", id);
439 if (handler->state != CREATE) {
440 DbgPrint("[%s] is not created\n", id);
444 if (lb_get_lb_fb(handler)) {
445 lb_invoke_event_handler(handler, LB_EVENT_PD_UPDATE_END);
447 ErrPrint("Invalid request[%s]", id);
454 static struct packet *master_lb_updated(pid_t pid, int handle, const struct packet *packet)
461 struct livebox *handler;
467 ret = packet_get(packet, "sssiidss",
469 &fbfile, &lb_w, &lb_h,
470 &priority, &content, &title);
472 ErrPrint("Invalid argument\n");
476 handler = lb_find_livebox(pkgname, id);
478 ErrPrint("instance(%s) is not exists\n", id);
482 if (handler->state != CREATE) {
485 * Already deleted by the user.
486 * Don't try to notice anything with this, Just ignore all events
487 * Beacuse the user doesn't wants know about this anymore
489 DbgPrint("(%s) is not exists, but updated\n", id);
493 lb_set_priority(handler, priority);
494 lb_set_content(handler, content);
495 lb_set_title(handler, title);
496 lb_set_size(handler, lb_w, lb_h);
498 if (lb_text_lb(handler)) {
499 (void)parse_desc(handler, livebox_filename(handler), 0);
502 * DESC parser will call the "text event callback".
503 * Don't need to call global event callback in this case.
506 } else if (lb_get_lb_fb(handler)) {
507 lb_set_lb_fb(handler, fbfile);
508 ret = fb_sync(lb_get_lb_fb(handler));
509 if (ret != LB_STATUS_SUCCESS)
510 ErrPrint("Failed to do sync FB (%s - %s) (%d)\n", pkgname, util_basename(util_uri_to_path(id)), ret);
512 ret = LB_STATUS_SUCCESS;
515 if (ret == LB_STATUS_SUCCESS)
516 lb_invoke_event_handler(handler, LB_EVENT_LB_UPDATED);
522 static struct packet *master_pd_created(pid_t pid, int handle, const struct packet *packet)
524 struct livebox *handler;
533 ret = packet_get(packet, "sssiii", &pkgname, &id, &buf_id, &width, &height, &status);
535 ErrPrint("Invalid argument\n");
539 handler = lb_find_livebox(pkgname, id);
541 ErrPrint("Instance(%s) is not exists\n", id);
545 if (handler->state != CREATE) {
546 ErrPrint("Instance(%s) is not created\n", id);
550 lb_set_pdsize(handler, width, height);
551 if (lb_text_pd(handler)) {
552 DbgPrint("Text TYPE does not need to handle this\n");
554 (void)lb_set_pd_fb(handler, buf_id);
555 ret = fb_sync(lb_get_pd_fb(handler));
557 ErrPrint("Failed to do sync FB (%s - %s)\n", pkgname, util_basename(util_uri_to_path(id)));
560 handler->is_pd_created = (status == 0);
562 if (handler->pd_created_cb) {
566 cb = handler->pd_created_cb;
567 cbdata = handler->pd_created_cbdata;
569 handler->pd_created_cb = NULL;
570 handler->pd_created_cbdata = NULL;
572 DbgPrint("pd_created_cb (%s) - %d\n", buf_id, status);
573 cb(handler, status, cbdata);
574 } else if (status == 0) {
575 DbgPrint("LB_EVENT_PD_CREATED (%s) - %d\n", buf_id, status);
576 lb_invoke_event_handler(handler, LB_EVENT_PD_CREATED);
583 static struct packet *master_pd_destroyed(pid_t pid, int handle, const struct packet *packet)
585 struct livebox *handler;
591 ret = packet_get(packet, "ssi", &pkgname, &id, &status);
593 ErrPrint("Invalid argument\n");
597 handler = lb_find_livebox(pkgname, id);
599 ErrPrint("Instance(%s) is not exists\n", id);
603 if (handler->state != CREATE) {
604 ErrPrint("Instance(%s) is not created\n", id);
608 handler->is_pd_created = 0;
610 if (handler->pd_destroyed_cb) {
614 cb = handler->pd_destroyed_cb;
615 cbdata = handler->pd_destroyed_cbdata;
617 handler->pd_destroyed_cb = NULL;
618 handler->pd_destroyed_cbdata = NULL;
620 DbgPrint("Invoke the PD Destroyed CB\n");
621 cb(handler, status, cbdata);
622 } else if (status == 0) {
623 DbgPrint("Invoke the LB_EVENT_PD_DESTROYED event\n");
624 lb_invoke_event_handler(handler, LB_EVENT_PD_DESTROYED);
631 static struct packet *master_pd_updated(pid_t pid, int handle, const struct packet *packet)
635 const char *descfile;
638 struct livebox *handler;
642 ret = packet_get(packet, "ssssii",
647 ErrPrint("Invalid argument\n");
651 handler = lb_find_livebox(pkgname, id);
653 ErrPrint("Instance(%s) is not exists\n", id);
657 if (handler->state != CREATE) {
660 * This handler is already deleted by the user.
661 * So don't try to notice anything about this anymore.
662 * Just ignore all events.
664 ErrPrint("Instance(%s) is not created\n", id);
668 lb_set_pdsize(handler, pd_w, pd_h);
670 if (lb_text_pd(handler)) {
671 (void)parse_desc(handler, descfile, 1);
673 (void)lb_set_pd_fb(handler, fbfile);
675 ret = fb_sync(lb_get_pd_fb(handler));
677 ErrPrint("Failed to do sync FB (%s - %s)\n", pkgname, util_basename(util_uri_to_path(id)));
679 lb_invoke_event_handler(handler, LB_EVENT_PD_UPDATED);
686 static struct packet *master_update_mode(pid_t pid, int handle, const struct packet *packet)
688 struct livebox *handler;
696 ErrPrint("Invalid packet\n");
700 ret = packet_get(packet, "ssii", &pkgname, &id, &status, &active_mode);
702 ErrPrint("Invalid argument\n");
706 DbgPrint("Update mode is changed: %d, %d, %s\n", status, active_mode, id);
708 handler = lb_find_livebox(pkgname, id);
710 ErrPrint("Livebox(%s) is not found\n", id);
714 if (handler->state != CREATE) {
715 ErrPrint("Livebox(%s) is not created yet\n", id);
719 if (status == LB_STATUS_SUCCESS)
720 lb_set_update_mode(handler, active_mode);
722 if (handler->update_mode_cb) {
726 cb = handler->update_mode_cb;
727 cbdata = handler->update_mode_cbdata;
729 handler->update_mode_cb = NULL;
730 handler->update_mode_cbdata = NULL;
732 cb(handler, status, cbdata);
734 lb_invoke_event_handler(handler, LB_EVENT_UPDATE_MODE_CHANGED);
741 static struct packet *master_size_changed(pid_t pid, int handle, const struct packet *packet)
743 struct livebox *handler;
754 ErrPrint("Invalid packet\n");
758 ret = packet_get(packet, "sssiiii", &pkgname, &id, &fbfile, &is_pd, &w, &h, &status);
760 ErrPrint("Invalid argument\n");
764 DbgPrint("Size is changed: %dx%d (%s), fb: [%s]\n", w, h, id, fbfile);
766 handler = lb_find_livebox(pkgname, id);
768 ErrPrint("Livebox(%s) is not found\n", id);
772 if (handler->state != CREATE) {
773 ErrPrint("Livebox(%s) is not created yet\n", id);
778 DbgPrint("PD is resized\n");
781 * PD is not able to resized by the client.
782 * PD is only can be managed by the provider.
783 * So the PD has no private resized event handler.
784 * Notify it via global event handler only.
787 lb_set_pdsize(handler, w, h);
788 lb_invoke_event_handler(handler, LB_EVENT_PD_SIZE_CHANGED);
790 ErrPrint("This is not possible. PD Size is changed but the return value is not ZERO (%d)\n", status);
794 DbgPrint("LB is successfully resized (%dx%d)\n", w, h);
795 lb_set_size(handler, w, h);
799 * If there is a created LB FB,
802 if (lb_get_lb_fb(handler)) {
803 lb_set_lb_fb(handler, fbfile);
805 ret = fb_sync(lb_get_lb_fb(handler));
807 ErrPrint("Failed to do sync FB (%s - %s)\n", pkgname, util_basename(util_uri_to_path(id)));
809 /* Just update the size info only. */
814 * I cannot believe client.
815 * So I added some log before & after call the user callback.
817 if (handler->size_changed_cb) {
821 cb = handler->size_changed_cb;
822 cbdata = handler->size_cbdata;
824 handler->size_changed_cb = NULL;
825 handler->size_cbdata = NULL;
827 cb(handler, status, cbdata);
829 lb_invoke_event_handler(handler, LB_EVENT_LB_SIZE_CHANGED);
832 DbgPrint("LB is not resized: %dx%d (%d)\n", w, h, status);
833 if (handler->size_changed_cb) {
837 cb = handler->size_changed_cb;
838 cbdata = handler->size_cbdata;
840 handler->size_changed_cb = NULL;
841 handler->size_cbdata = NULL;
843 cb(handler, status, cbdata);
852 static struct packet *master_period_changed(pid_t pid, int handle, const struct packet *packet)
854 struct livebox *handler;
861 ret = packet_get(packet, "idss", &status, &period, &pkgname, &id);
863 ErrPrint("Invalid argument\n");
867 handler = lb_find_livebox(pkgname, id);
869 ErrPrint("Livebox(%s) is not found\n", id);
873 if (handler->state != CREATE) {
874 ErrPrint("Livebox(%s) is not created\n", id);
878 DbgPrint("Update period is changed? %lf (%d)\n", period, status);
880 lb_set_period(handler, period);
882 if (handler->period_changed_cb) {
886 cb = handler->period_changed_cb;
887 cbdata = handler->period_cbdata;
889 handler->period_changed_cb = NULL;
890 handler->period_cbdata = NULL;
892 cb(handler, status, cbdata);
893 } else if (status == 0) {
894 lb_invoke_event_handler(handler, LB_EVENT_PERIOD_CHANGED);
901 static struct packet *master_group_changed(pid_t pid, int handle, const struct packet *packet)
903 struct livebox *handler;
908 const char *category;
911 ret = packet_get(packet, "ssiss", &pkgname, &id, &status, &cluster, &category);
913 ErrPrint("Invalid argument\n");
917 handler = lb_find_livebox(pkgname, id);
919 ErrPrint("Livebox(%s) is not exists\n", id);
923 if (handler->state != CREATE) {
926 * Do no access this handler,
927 * You cannot believe this handler anymore.
929 ErrPrint("Livebox(%s) is not created\n", id);
933 DbgPrint("Group is changed? [%s] / [%s] (%d)\n", cluster, category, status);
935 (void)lb_set_group(handler, cluster, category);
937 if (handler->group_changed_cb) {
941 cb = handler->group_changed_cb;
942 cbdata = handler->group_cbdata;
944 handler->group_changed_cb = NULL;
945 handler->group_cbdata = NULL;
947 cb(handler, status, cbdata);
948 } else if (status == 0) {
949 lb_invoke_event_handler(handler, LB_EVENT_GROUP_CHANGED);
956 static struct packet *master_created(pid_t pid, int handle, const struct packet *packet)
958 struct livebox *handler;
969 const char *category;
970 const char *lb_fname;
971 const char *pd_fname;
975 const char *auto_launch;
980 enum lb_type lb_type;
981 enum pd_type pd_type;
985 int old_state = DESTROYED;
989 ret = packet_get(packet, "dsssiiiisssssdiiiiidsi",
991 &pkgname, &id, &content,
992 &lb_w, &lb_h, &pd_w, &pd_h,
993 &cluster, &category, &lb_fname, &pd_fname,
994 &auto_launch, &priority, &size_list, &user, &pinup_supported,
995 &lb_type, &pd_type, &period, &title, &is_pinned_up);
997 ErrPrint("Invalid argument\n");
998 ret = LB_STATUS_ERROR_INVALID;
1002 ErrPrint("[%lf] pkgname: %s, id: %s, content: %s, "
1003 "pd_w: %d, pd_h: %d, lb_w: %d, lb_h: %d, "
1004 "cluster: %s, category: %s, lb_fname: \"%s\", pd_fname: \"%s\", "
1005 "auto_launch: %s, priority: %lf, size_list: %d, user: %d, pinup: %d, "
1006 "lb_type: %d, pd_type: %d, period: %lf, title: [%s], is_pinned_up: %d\n",
1007 timestamp, pkgname, id, content,
1008 pd_w, pd_h, lb_w, lb_h,
1009 cluster, category, lb_fname, pd_fname,
1010 auto_launch, priority, size_list, user, pinup_supported,
1011 lb_type, pd_type, period, title, is_pinned_up);
1013 handler = lb_find_livebox_by_timestamp(timestamp);
1015 handler = lb_new_livebox(pkgname, id, timestamp);
1017 ErrPrint("Failed to create a new livebox\n");
1018 ret = LB_STATUS_ERROR_FAULT;
1022 old_state = handler->state;
1024 if (handler->state != CREATE) {
1025 if (handler->state != DELETE) {
1028 * This is not possible!!!
1030 ErrPrint("Invalid handler\n");
1031 ret = LB_STATUS_ERROR_INVALID;
1037 * After get the delete states,
1038 * call the create callback with deleted result.
1042 old_state = handler->state;
1045 ErrPrint("Already created: timestamp[%lf] "
1046 "pkgname[%s], id[%s] content[%s] "
1047 "cluster[%s] category[%s] lb_fname[%s] pd_fname[%s]\n",
1048 timestamp, pkgname, id,
1049 content, cluster, category,
1050 lb_fname, pd_fname);
1052 ret = LB_STATUS_ERROR_ALREADY;
1056 lb_set_id(handler, id);
1059 lb_set_size(handler, lb_w, lb_h);
1060 handler->lb.type = lb_type;
1061 handler->is_pinned_up = is_pinned_up;
1066 case _LB_TYPE_SCRIPT:
1067 case _LB_TYPE_BUFFER:
1068 if (!strlen(lb_fname))
1070 lb_set_lb_fb(handler, lb_fname);
1071 ret = fb_sync(lb_get_lb_fb(handler));
1073 ErrPrint("Failed to do sync FB (%s - %s)\n", pkgname, util_basename(util_uri_to_path(id)));
1076 lb_set_text_lb(handler);
1082 handler->pd.type = pd_type;
1083 lb_set_pdsize(handler, pd_w, pd_h);
1084 lb_set_default_pdsize(handler, pd_w, pd_h);
1086 case _PD_TYPE_SCRIPT:
1087 case _PD_TYPE_BUFFER:
1088 if (!strlen(pd_fname))
1091 lb_set_pd_fb(handler, pd_fname);
1092 ret = fb_sync(lb_get_pd_fb(handler));
1094 ErrPrint("Failed to do sync FB (%s - %s)\n", pkgname, util_basename(util_uri_to_path(id)));
1097 lb_set_text_pd(handler);
1103 lb_set_priority(handler, priority);
1105 lb_set_size_list(handler, size_list);
1106 lb_set_group(handler, cluster, category);
1108 lb_set_content(handler, content);
1109 lb_set_title(handler, title);
1111 lb_set_user(handler, user);
1113 lb_set_auto_launch(handler, auto_launch);
1114 lb_set_pinup(handler, pinup_supported);
1116 lb_set_period(handler, period);
1120 if (handler->state == CREATE) {
1123 * These callback can change the handler->state.
1124 * So we have to use the "old_state" which stored state before call these callbacks
1127 if (handler->created_cb) {
1131 cb = handler->created_cb;
1132 cbdata = handler->created_cbdata;
1134 handler->created_cb = NULL;
1135 handler->created_cbdata = NULL;
1137 DbgPrint("Invoke the created_cb\n");
1138 cb(handler, ret, cbdata);
1140 DbgPrint("Invoke the lb,created\n");
1141 lb_invoke_event_handler(handler, LB_EVENT_CREATED);
1146 if (ret == 0 && old_state == DELETE) {
1147 DbgPrint("Send the delete request\n");
1148 lb_send_delete(handler, handler->created_cb, handler->created_cbdata);
1152 * handler->created_cb = NULL;
1153 * handler->created_cbdata = NULL;
1155 * Do not clear this to use this from the deleted event callback.
1156 * if this value is not cleared when the deleted event callback check it,
1157 * it means that the created function is not called yet.
1158 * Then the call the deleted event callback with LB_STATUS_ERROR_CANCEL errno.
1165 static struct method s_table[] = {
1167 .cmd = "lb_updated", /* pkgname, id, lb_w, lb_h, priority, ret */
1168 .handler = master_lb_updated,
1171 .cmd = "pd_updated", /* pkgname, id, descfile, pd_w, pd_h, ret */
1172 .handler = master_pd_updated,
1175 .cmd = "pd_created",
1176 .handler = master_pd_created,
1179 .cmd = "pd_destroyed",
1180 .handler = master_pd_destroyed,
1183 .cmd = "fault_package", /* pkgname, id, function, ret */
1184 .handler = master_fault_package,
1187 .cmd = "deleted", /* pkgname, id, timestamp, ret */
1188 .handler = master_deleted,
1191 .cmd = "created", /* timestamp, pkgname, id, content, lb_w, lb_h, pd_w, pd_h, cluster, category, lb_file, pd_file, auto_launch, priority, size_list, is_user, pinup_supported, text_lb, text_pd, period, ret */
1192 .handler = master_created,
1195 .cmd = "group_changed",
1196 .handler = master_group_changed,
1199 .cmd = "period_changed",
1200 .handler = master_period_changed,
1203 .cmd = "size_changed",
1204 .handler = master_size_changed,
1208 .handler = master_pinup,
1212 .handler = master_hold_scroll,
1216 .cmd = "update_mode",
1217 .handler = master_update_mode,
1221 .cmd = "lb_update_begin",
1222 .handler = master_lb_update_begin,
1225 .cmd = "lb_update_end",
1226 .handler = master_lb_update_end,
1230 .cmd = "pd_update_begin",
1231 .handler = master_pd_update_begin,
1234 .cmd = "pd_update_end",
1235 .handler = master_pd_update_end,
1239 .cmd = "access_status",
1240 .handler = master_access_status,
1249 static void acquire_cb(struct livebox *handler, const struct packet *result, void *data)
1252 DbgPrint("Result packet is not valid\n");
1256 if (packet_get(result, "i", &ret) != 1)
1257 ErrPrint("Invalid argument\n");
1259 DbgPrint("Acquire returns: %d\n", ret);
1265 static inline int make_connection(void)
1267 struct packet *packet;
1270 DbgPrint("Let's making connection!\n");
1272 s_info.fd = com_core_packet_client_init(CLIENT_SOCKET, 0, s_table);
1273 if (s_info.fd < 0) {
1274 ErrPrint("Try this again later\n");
1275 return LB_STATUS_ERROR_IO;
1278 packet = packet_create("acquire", "d", util_timestamp());
1280 com_core_packet_client_fini(s_info.fd);
1282 return LB_STATUS_ERROR_FAULT;
1285 ret = master_rpc_async_request(NULL, packet, 1, acquire_cb, NULL);
1287 ErrPrint("Master RPC returns %d\n", ret);
1288 com_core_packet_client_fini(s_info.fd);
1290 return LB_STATUS_ERROR_IO;
1293 return LB_STATUS_SUCCESS;
1296 static int connected_cb(int handle, void *data)
1298 master_rpc_check_and_fire_consumer();
1302 static void master_started_cb(keynode_t *node, void *data)
1306 if (vconf_get_bool(VCONFKEY_MASTER_STARTED, &state) < 0)
1307 ErrPrint("Unable to get [%s]\n", VCONFKEY_MASTER_STARTED);
1309 DbgPrint("Master state: %d\n", state);
1310 if (state == 1 && make_connection() == LB_STATUS_SUCCESS) {
1312 ret = vconf_ignore_key_changed(VCONFKEY_MASTER_STARTED, master_started_cb);
1313 DbgPrint("master_started vconf key de-registered [%d]\n", ret);
1317 static gboolean timeout_cb(gpointer data)
1319 if (vconf_notify_key_changed(VCONFKEY_MASTER_STARTED, master_started_cb, NULL) < 0)
1320 ErrPrint("Failed to add vconf for monitoring service state\n");
1322 DbgPrint("vconf event callback is registered\n");
1324 master_started_cb(NULL, NULL);
1326 s_info.timer_id = 0;
1330 static int disconnected_cb(int handle, void *data)
1332 if (s_info.fd != handle) {
1333 /*!< This handle is not my favor */
1337 s_info.fd = -1; /*!< Disconnected */
1339 master_rpc_clear_all_request();
1340 lb_invoke_fault_handler(LB_FAULT_PROVIDER_DISCONNECTED, MASTER_PKGNAME, "default", "disconnected");
1344 /* Try to reconnect after 1 sec later */
1345 if (!s_info.timer_id) {
1346 DbgPrint("Reconnecting timer is added\n");
1347 s_info.timer_id = g_timeout_add(1000, timeout_cb, NULL);
1348 if (s_info.timer_id == 0) {
1349 ErrPrint("Unable to add reconnecting timer\n");
1353 ErrPrint("Reconnecting timer is already exists\n");
1359 int client_init(void)
1361 com_core_add_event_callback(CONNECTOR_DISCONNECTED, disconnected_cb, NULL);
1362 com_core_add_event_callback(CONNECTOR_CONNECTED, connected_cb, NULL);
1363 if (vconf_notify_key_changed(VCONFKEY_MASTER_STARTED, master_started_cb, NULL) < 0)
1364 ErrPrint("Failed to add vconf for service state\n");
1366 DbgPrint("vconf event callback is registered\n");
1368 master_started_cb(NULL, NULL);
1377 const char *client_addr(void)
1379 return CLIENT_SOCKET;
1382 int client_fini(void)
1385 ret = vconf_ignore_key_changed(VCONFKEY_MASTER_STARTED, master_started_cb);
1387 DbgPrint("Ignore vconf key: %d\n", ret);
1388 com_core_packet_client_fini(s_info.fd);
1389 com_core_del_event_callback(CONNECTOR_DISCONNECTED, disconnected_cb, NULL);
1390 com_core_del_event_callback(CONNECTOR_CONNECTED, connected_cb, NULL);
1392 return LB_STATUS_SUCCESS;