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.
28 #include <vconf-keys.h>
32 #include <com-core_packet.h>
33 #include <livebox-errno.h>
34 #include <livebox-service.h>
35 #include <secure_socket.h>
40 #include "livebox_internal.h"
41 #include "desc_parser.h"
44 #include "master_rpc.h"
46 #include "file_service.h"
48 #include <tzplatform_config.h>
50 #define CLIENT_SOCKET tzplatform_mkpath(TZ_USER_SHARE,"live_magazine/.client.socket")
64 static struct packet *master_fault_package(pid_t pid, int handle, const struct packet *packet)
70 if (packet_get(packet, "sss", &pkgname, &id, &function) != 3) {
71 ErrPrint("Invalid arguments\n");
75 DbgPrint("[%s]\n", pkgname);
76 master_rpc_clear_fault_package(pkgname);
77 lb_invoke_fault_handler(LB_FAULT_DEACTIVATED, pkgname, id, function);
81 static struct packet *master_hold_scroll(pid_t pid, int handle, const struct packet *packet)
83 struct livebox_common *common;
84 struct livebox *livebox;
91 ret = packet_get(packet, "ssi", &pkgname, &id, &seize);
93 ErrPrint("Invalid argument\n");
97 common = lb_find_common_handle(pkgname, id);
99 ErrPrint("Instance(%s) is not exists\n", id);
103 DbgPrint("HOLD: [%s] seize(%d)\n", id, seize);
104 seize = seize ? LB_EVENT_HOLD_SCROLL : LB_EVENT_RELEASE_SCROLL;
105 dlist_foreach(common->livebox_list, l, livebox) {
106 lb_invoke_event_handler(livebox, seize);
113 static struct packet *master_pinup(pid_t pid, int handle, const struct packet *packet)
118 struct livebox *handler;
121 struct livebox_common *common;
127 ret = packet_get(packet, "iisss", &status, &pinup, &pkgname, &id, &content);
129 ErrPrint("Invalid argument\n");
133 common = lb_find_common_handle(pkgname, id);
135 ErrPrint("Instance (%s) is not exists\n", id);
139 if (status == (int)LB_STATUS_SUCCESS) {
140 new_content = strdup(content);
142 free(common->content);
143 common->content = new_content;
144 common->is_pinned_up = pinup;
146 ErrPrint("Heap: %s\n", strerror(errno));
147 status = LB_STATUS_ERROR_MEMORY;
151 common->request.pinup = 0;
153 dlist_foreach_safe(common->livebox_list, l, n, handler) {
154 if (handler->cbs.pinup.cb) {
158 /* Make sure that user can call pinup API in its result callback */
159 cb = handler->cbs.pinup.cb;
160 cbdata = handler->cbs.pinup.data;
162 handler->cbs.pinup.cb = NULL;
163 handler->cbs.pinup.data = NULL;
165 cb(handler, status, cbdata);
166 } else if (status == (int)LB_STATUS_SUCCESS) {
167 lb_invoke_event_handler(handler, LB_EVENT_PINUP_CHANGED);
175 static struct packet *master_deleted(pid_t pid, int handle, const struct packet *packet)
180 struct livebox *handler;
181 struct livebox_common *common;
186 if (packet_get(packet, "ssdi", &pkgname, &id, ×tamp, &reason) != 4) {
187 ErrPrint("Invalid arguemnt\n");
191 DbgPrint("[%s]\n", pkgname);
192 common = lb_find_common_handle_by_timestamp(timestamp);
196 * This can be happens only if the user delete a livebox
197 * right after create it before receive created event.
202 /*!< Check validity of this "handler" */
203 if (common->state != CREATE) {
204 if (common->state != DELETE) {
207 * This is not possible
209 ErrPrint("Already deleted handler (%s - %s)\n", pkgname, id);
214 common->request.deleted = 0;
216 * We should change the state of "common handler' before handling the callbacks.
217 * Because if user tries to create a new handle in the callbacks,
218 * find_sharable_common_handle will returns destroying object.
219 * Then we will get panic.
220 * To prevent it, we should change its state first.
222 common->state = DELETE;
224 dlist_foreach_safe(common->livebox_list, l, n, handler) {
225 if (handler->cbs.created.cb) {
231 * "if (handler->id == NULL) {"
233 * The instance is not created yet.
234 * But the master forcely destroy it and send destroyed event to this
235 * without the created event.
237 * It could be destroyed when a slave has critical error(fault)
238 * before creating an instance successfully.
240 if (handler->cbs.created.cb == handler->cbs.deleted.cb) {
241 if (handler->cbs.created.data != handler->cbs.deleted.data) {
242 DbgPrint("cb is same but cbdata is different (%s - %s)\n", pkgname, id);
245 handler->cbs.deleted.cb = NULL;
246 handler->cbs.deleted.data = NULL;
249 cb = handler->cbs.created.cb;
250 cbdata = handler->cbs.created.data;
252 handler->cbs.created.cb = NULL;
253 handler->cbs.created.data = NULL;
255 if (reason == (int)LB_STATUS_SUCCESS) {
256 reason = LB_STATUS_ERROR_CANCEL;
259 cb(handler, reason, cbdata);
260 } else if (common->id) {
261 if (handler->cbs.deleted.cb) {
265 cb = handler->cbs.deleted.cb;
266 cbdata = handler->cbs.deleted.data;
268 handler->cbs.deleted.cb = NULL;
269 handler->cbs.deleted.data = NULL;
271 cb(handler, reason, cbdata);
273 lb_invoke_event_handler(handler, LB_EVENT_DELETED);
277 /* Just try to delete it, if a user didn't remove it from the live box list */
278 lb_unref(handler, 1);
285 static struct packet *master_lb_update_begin(pid_t pid, int handle, const struct packet *packet)
287 struct livebox *handler;
288 struct livebox_common *common;
297 ret = packet_get(packet, "ssdsss", &pkgname, &id, &priority, &content, &title, &fbfile);
299 ErrPrint("Invalid argument\n");
303 common = lb_find_common_handle(pkgname, id);
305 ErrPrint("Instance[%s] is not exists\n", id);
309 if (common->state != CREATE) {
310 ErrPrint("(%s) is not created\n", id);
314 lb_set_priority(common, priority);
315 lb_set_content(common, content);
316 lb_set_title(common, title);
320 * Width & Height is not changed in this case.
321 * If the active update is began, the size should not be changed,
322 * And if the size is changed, the provider should finish the updating first.
323 * And then begin updating again after change its size.
325 if (lb_get_lb_fb(common)) {
326 (void)lb_set_lb_fb(common, fbfile);
328 ret = lb_sync_lb_fb(common);
330 if (ret != (int)LB_STATUS_SUCCESS) {
331 ErrPrint("Failed to do sync FB (%s - %s) (%d)\n", pkgname, fbfile, ret);
334 dlist_foreach(common->livebox_list, l, handler) {
335 lb_invoke_event_handler(handler, LB_EVENT_LB_UPDATE_BEGIN);
339 ErrPrint("Invalid request[%s], %s\n", id, fbfile);
346 static struct packet *master_pd_update_begin(pid_t pid, int handle, const struct packet *packet)
348 struct livebox *handler;
349 struct livebox_common *common;
355 ret = packet_get(packet, "sss", &pkgname, &id, &fbfile);
357 ErrPrint("Invalid argument\n");
361 common = lb_find_common_handle(pkgname, id);
363 ErrPrint("Instance[%s] is not exists\n", id);
367 if (common->state != CREATE) {
368 ErrPrint("[%s] is not created\n", id);
372 if (lb_get_pd_fb(common)) {
373 (void)lb_set_pd_fb(common, fbfile);
375 ret = lb_sync_pd_fb(common);
376 if (ret != (int)LB_STATUS_SUCCESS) {
377 ErrPrint("Failed to do sync FB (%s - %s) (%d)\n", pkgname, fbfile, ret);
380 dlist_foreach(common->livebox_list, l, handler) {
381 lb_invoke_event_handler(handler, LB_EVENT_PD_UPDATE_BEGIN);
385 ErrPrint("Invalid request[%s], %s\n", id, fbfile);
392 static struct packet *master_lb_update_end(pid_t pid, int handle, const struct packet *packet)
394 struct livebox *handler;
395 struct livebox_common *common;
400 ret = packet_get(packet, "ss", &pkgname, &id);
402 ErrPrint("Invalid argument\n");
406 common = lb_find_common_handle(pkgname, id);
408 ErrPrint("Instance[%s] is not exists\n", id);
412 if (common->state != CREATE) {
413 ErrPrint("[%s] is not created\n", id);
417 if (lb_get_lb_fb(common)) {
419 dlist_foreach(common->livebox_list, l, handler) {
420 lb_invoke_event_handler(handler, LB_EVENT_LB_UPDATE_END);
423 ErrPrint("Invalid request[%s]\n", id);
430 static struct packet *master_key_status(pid_t pid, int handle, const struct packet *packet)
432 struct livebox *handler;
433 struct livebox_common *common;
440 ret = packet_get(packet, "ssi", &pkgname, &id, &status);
442 ErrPrint("Invalid argument\n");
446 common = lb_find_common_handle(pkgname, id);
448 ErrPrint("Instance[%s] is not exists\n", id);
452 if (common->state != CREATE) {
453 ErrPrint("[%s] is not created\n", id);
457 common->request.key_event = 0;
458 dlist_foreach(common->livebox_list, l, handler) {
459 if (handler->cbs.key_event.cb) {
463 cb = handler->cbs.key_event.cb;
464 cbdata = handler->cbs.key_event.data;
466 handler->cbs.key_event.cb = NULL;
467 handler->cbs.key_event.data = NULL;
469 cb(handler, status, cbdata);
471 ErrPrint("Invalid event[%s]\n", id);
479 static struct packet *master_request_close_pd(pid_t pid, int handle, const struct packet *packet)
481 struct livebox *handler;
482 struct livebox_common *common;
489 ret = packet_get(packet, "ssi", &pkgname, &id, &reason);
491 ErrPrint("Invalid argument\n");
495 common = lb_find_common_handle(pkgname, id);
497 ErrPrint("Instance[%s] is not exists\n", id);
501 if (common->state != CREATE) {
502 ErrPrint("[%s] is not created\n", id);
506 if (!common->is_pd_created) {
507 DbgPrint("PD is not created, closing what?(%s)\n", id);
511 DbgPrint("Reason: %d\n", reason);
513 dlist_foreach(common->livebox_list, l, handler) {
514 lb_invoke_event_handler(handler, LB_EVENT_REQUEST_CLOSE_PD);
520 static struct packet *master_access_status(pid_t pid, int handle, const struct packet *packet)
522 struct livebox *handler;
523 struct livebox_common *common;
530 ret = packet_get(packet, "ssi", &pkgname, &id, &status);
532 ErrPrint("Invalid argument\n");
536 common = lb_find_common_handle(pkgname, id);
538 ErrPrint("Instance[%s] is not exists\n", id);
542 if (common->state != CREATE) {
543 ErrPrint("[%s] is not created\n", id);
547 common->request.access_event = 0;
548 dlist_foreach(common->livebox_list, l, handler) {
549 if (handler->cbs.access_event.cb) {
553 cb = handler->cbs.access_event.cb;
554 cbdata = handler->cbs.access_event.data;
556 handler->cbs.access_event.cb = NULL;
557 handler->cbs.access_event.data = NULL;
559 cb(handler, status, cbdata);
566 static struct packet *master_pd_update_end(pid_t pid, int handle, const struct packet *packet)
568 struct livebox *handler;
569 struct livebox_common *common;
574 ret = packet_get(packet, "ss", &pkgname, &id);
576 ErrPrint("Invalid argument\n");
580 common = lb_find_common_handle(pkgname, id);
582 ErrPrint("Instance[%s] is not exists\n", id);
586 if (common->state != CREATE) {
587 ErrPrint("[%s] is not created\n", id);
591 if (lb_get_lb_fb(common)) {
594 dlist_foreach(common->livebox_list, l, handler) {
595 lb_invoke_event_handler(handler, LB_EVENT_PD_UPDATE_END);
598 ErrPrint("Invalid request[%s]", id);
605 static struct packet *master_lb_updated(pid_t pid, int handle, const struct packet *packet)
612 const char *safe_file;
615 struct livebox *handler;
616 struct livebox_common *common;
622 ret = packet_get(packet, "sssiidsssss",
624 &fbfile, &lb_w, &lb_h,
625 &priority, &content, &title,
626 &safe_file, &icon, &name);
628 ErrPrint("Invalid argument\n");
632 common = lb_find_common_handle(pkgname, id);
634 ErrPrint("instance(%s) is not exists\n", id);
638 if (common->state != CREATE) {
641 * Already deleted by the user.
642 * Don't try to notice anything with this, Just ignore all events
643 * Beacuse the user doesn't wants know about this anymore
645 ErrPrint("(%s) is not exists, but updated\n", id);
649 lb_set_priority(common, priority);
650 lb_set_content(common, content);
651 lb_set_title(common, title);
652 lb_set_size(common, lb_w, lb_h);
653 lb_set_filename(common, safe_file);
655 if (lb_text_lb(common)) {
656 const char *common_filename;
658 common_filename = common->filename ? common->filename : util_uri_to_path(common->id);
660 (void)parse_desc(common, common_filename, 0);
663 * DESC parser will call the "text event callback".
664 * Don't need to call global event callback in this case.
667 } else if (lb_get_lb_fb(common)) {
670 * replace this with "flag" instead of "callback address"
672 if (conf_frame_drop_for_resizing() && common->request.size_changed) {
673 /* Just for skipping the update event callback call, After request to resize buffer, update event will be discarded */
674 DbgPrint("Discards obsoloted update event\n");
675 ret = LB_STATUS_ERROR_BUSY;
677 (void)lb_set_lb_fb(common, fbfile);
679 if (!conf_manual_sync()) {
680 ret = lb_sync_lb_fb(common);
681 if (ret != (int)LB_STATUS_SUCCESS) {
682 ErrPrint("Failed to do sync FB (%s - %s) (%d)\n", pkgname, util_basename(util_uri_to_path(id)), ret);
685 ret = LB_STATUS_SUCCESS;
689 ret = LB_STATUS_SUCCESS;
692 if (ret == (int)LB_STATUS_SUCCESS) {
696 dlist_foreach_safe(common->livebox_list, l, n, handler) {
697 lb_invoke_event_handler(handler, LB_EVENT_LB_UPDATED);
705 static struct packet *master_pd_created(pid_t pid, int handle, const struct packet *packet)
707 struct livebox *handler;
708 struct livebox_common *common;
719 ret = packet_get(packet, "sssiii", &pkgname, &id, &buf_id, &width, &height, &status);
721 ErrPrint("Invalid argument\n");
725 DbgPrint("[%s]\n", pkgname);
726 common = lb_find_common_handle(pkgname, id);
728 ErrPrint("Instance(%s) is not exists\n", id);
732 if (common->state != CREATE) {
733 ErrPrint("Instance(%s) is not created\n", id);
737 if (!common->request.pd_created) {
738 ErrPrint("PD create request is canceled\n");
742 common->is_pd_created = (status == (int)LB_STATUS_SUCCESS);
743 common->request.pd_created = 0;
745 if (common->is_pd_created) {
746 lb_set_pdsize(common, width, height);
747 if (lb_text_pd(common)) {
748 DbgPrint("Text TYPE does not need to handle this\n");
750 (void)lb_set_pd_fb(common, buf_id);
752 switch (common->pd.type) {
753 case _PD_TYPE_SCRIPT:
754 case _PD_TYPE_BUFFER:
755 switch (fb_type(lb_get_pd_fb(common))) {
756 case BUFFER_TYPE_FILE:
757 case BUFFER_TYPE_SHM:
758 lb_create_lock_file(common, 1);
760 case BUFFER_TYPE_PIXMAP:
761 case BUFFER_TYPE_ERROR:
771 ret = lb_sync_pd_fb(common);
773 ErrPrint("Failed to do sync FB (%s - %s)\n", pkgname, util_basename(util_uri_to_path(id)));
778 DbgPrint("PERF_DBOX\n");
779 dlist_foreach_safe(common->livebox_list, l, n, handler) {
780 if (handler->cbs.pd_created.cb) {
784 cb = handler->cbs.pd_created.cb;
785 cbdata = handler->cbs.pd_created.data;
787 handler->cbs.pd_created.cb = NULL;
788 handler->cbs.pd_created.data = NULL;
791 * Before call the Callback function,
792 * pd_create_cb must be reset.
793 * Because, in the create callback, user can call create_pd function again.
795 cb(handler, status, cbdata);
796 } else if (status == (int)LB_STATUS_SUCCESS) {
797 lb_invoke_event_handler(handler, LB_EVENT_PD_CREATED);
805 static struct packet *master_pd_destroyed(pid_t pid, int handle, const struct packet *packet)
807 struct livebox *handler;
809 struct livebox_common *common;
815 ret = packet_get(packet, "ssi", &pkgname, &id, &status);
817 ErrPrint("Invalid argument\n");
821 DbgPrint("[%s]\n", pkgname);
822 common = lb_find_common_handle(pkgname, id);
824 ErrPrint("Instance(%s) is not exists\n", id);
828 if (common->state != CREATE) {
829 ErrPrint("Instance(%s) is not created\n", id);
833 if (common->is_pd_created == 0) {
834 ErrPrint("PD is not created, event is ignored\n");
838 common->is_pd_created = 0;
839 common->request.pd_destroyed = 0;
841 dlist_foreach(common->livebox_list, l, handler) {
842 if (handler->cbs.pd_destroyed.cb) {
846 cb = handler->cbs.pd_destroyed.cb;
847 cbdata = handler->cbs.pd_destroyed.data;
849 handler->cbs.pd_destroyed.cb = NULL;
850 handler->cbs.pd_destroyed.data = NULL;
853 * Before call the Callback function,
854 * pd_destroyed_cb must be reset.
855 * Because, in the create callback, user can call destroy_pd function again.
857 cb(handler, status, cbdata);
858 } else if (status == (int)LB_STATUS_SUCCESS) {
859 lb_invoke_event_handler(handler, LB_EVENT_PD_DESTROYED);
865 * Lock file should be deleted after all callbacks are processed.
867 switch (common->pd.type) {
868 case _PD_TYPE_SCRIPT:
869 case _PD_TYPE_BUFFER:
870 switch (fb_type(lb_get_pd_fb(common))) {
871 case BUFFER_TYPE_FILE:
872 case BUFFER_TYPE_SHM:
873 lb_destroy_lock_file(common, 1);
875 case BUFFER_TYPE_PIXMAP:
876 case BUFFER_TYPE_ERROR:
890 static struct packet *master_pd_updated(pid_t pid, int handle, const struct packet *packet)
894 const char *descfile;
897 struct livebox *handler;
898 struct livebox_common *common;
903 ret = packet_get(packet, "ssssii",
908 ErrPrint("Invalid argument\n");
912 DbgPrint("[%s]\n", pkgname);
913 common = lb_find_common_handle(pkgname, id);
915 ErrPrint("Instance(%s) is not exists\n", id);
919 if (common->state != CREATE) {
922 * This handler is already deleted by the user.
923 * So don't try to notice anything about this anymore.
924 * Just ignore all events.
926 ErrPrint("Instance(%s) is not created\n", id);
930 lb_set_pdsize(common, pd_w, pd_h);
932 if (lb_text_pd(common)) {
933 (void)parse_desc(common, descfile, 1);
935 if (conf_frame_drop_for_resizing() && common->request.size_changed) {
936 /* Just for skipping the update event callback call, After request to resize buffer, update event will be discarded */
937 DbgPrint("Discards obsoloted update event\n");
939 (void)lb_set_pd_fb(common, fbfile);
941 if (!conf_manual_sync()) {
942 ret = lb_sync_pd_fb(common);
944 ErrPrint("Failed to do sync FB (%s - %s), %d\n", pkgname, util_basename(util_uri_to_path(id)), ret);
946 dlist_foreach(common->livebox_list, l, handler) {
947 lb_invoke_event_handler(handler, LB_EVENT_PD_UPDATED);
951 dlist_foreach(common->livebox_list, l, handler) {
952 lb_invoke_event_handler(handler, LB_EVENT_PD_UPDATED);
962 static struct packet *master_update_mode(pid_t pid, int handle, const struct packet *packet)
964 struct livebox *handler;
965 struct livebox_common *common;
975 ErrPrint("Invalid packet\n");
979 ret = packet_get(packet, "ssii", &pkgname, &id, &status, &active_mode);
981 ErrPrint("Invalid argument\n");
985 common = lb_find_common_handle(pkgname, id);
987 ErrPrint("Livebox(%s) is not found\n", id);
991 if (common->state != CREATE) {
992 ErrPrint("Livebox(%s) is not created yet\n", id);
996 if (status == (int)LB_STATUS_SUCCESS) {
997 lb_set_update_mode(common, active_mode);
1000 common->request.update_mode = 0;
1001 dlist_foreach_safe(common->livebox_list, l, n, handler) {
1002 if (handler->cbs.update_mode.cb) {
1006 cb = handler->cbs.update_mode.cb;
1007 cbdata = handler->cbs.update_mode.data;
1009 handler->cbs.update_mode.cb = NULL;
1010 handler->cbs.update_mode.data = NULL;
1012 cb(handler, status, cbdata);
1013 } else if (status == (int)LB_STATUS_SUCCESS) {
1014 lb_invoke_event_handler(handler, LB_EVENT_UPDATE_MODE_CHANGED);
1022 static struct packet *master_size_changed(pid_t pid, int handle, const struct packet *packet)
1024 struct livebox *handler;
1025 struct livebox_common *common;
1026 const char *pkgname;
1036 ErrPrint("Invalid packet\n");
1040 ret = packet_get(packet, "sssiiii", &pkgname, &id, &fbfile, &is_pd, &w, &h, &status);
1042 ErrPrint("Invalid argument\n");
1046 common = lb_find_common_handle(pkgname, id);
1048 ErrPrint("Livebox(%s) is not found\n", id);
1052 if (common->state != CREATE) {
1053 ErrPrint("Livebox(%s) is not created yet\n", id);
1057 common->request.size_changed = 0;
1061 * PD is not able to resized by the client.
1062 * PD is only can be managed by the provider.
1063 * So the PD has no private resized event handler.
1064 * Notify it via global event handler only.
1066 if (status == (int)LB_STATUS_SUCCESS) {
1069 lb_set_pdsize(common, w, h);
1070 dlist_foreach(common->livebox_list, l, handler) {
1071 lb_invoke_event_handler(handler, LB_EVENT_PD_SIZE_CHANGED);
1074 ErrPrint("This is not possible. PD Size is changed but the return value is not ZERO (%d)\n", status);
1080 if (status == (int)LB_STATUS_SUCCESS) {
1081 lb_set_size(common, w, h);
1085 * If there is a created LB FB,
1088 if (lb_get_lb_fb(common)) {
1089 (void)lb_set_lb_fb(common, fbfile);
1091 ret = lb_sync_lb_fb(common);
1093 ErrPrint("Failed to do sync FB (%s - %s)\n", pkgname, util_basename(util_uri_to_path(id)));
1096 /* Just update the size info only. */
1102 * I cannot believe client.
1103 * So I added some log before & after call the user callback.
1105 dlist_foreach_safe(common->livebox_list, l, n, handler) {
1106 if (handler->cbs.size_changed.cb) {
1110 cb = handler->cbs.size_changed.cb;
1111 cbdata = handler->cbs.size_changed.data;
1113 handler->cbs.size_changed.cb = NULL;
1114 handler->cbs.size_changed.data = NULL;
1116 cb(handler, status, cbdata);
1117 } else if (status == (int)LB_STATUS_SUCCESS) {
1118 lb_invoke_event_handler(handler, LB_EVENT_LB_SIZE_CHANGED);
1127 static struct packet *master_period_changed(pid_t pid, int handle, const struct packet *packet)
1129 struct livebox *handler;
1130 struct livebox_common *common;
1133 const char *pkgname;
1139 ret = packet_get(packet, "idss", &status, &period, &pkgname, &id);
1141 ErrPrint("Invalid argument\n");
1145 common = lb_find_common_handle(pkgname, id);
1147 ErrPrint("Livebox(%s) is not found\n", id);
1151 if (common->state != CREATE) {
1152 ErrPrint("Livebox(%s) is not created\n", id);
1156 if (status == (int)LB_STATUS_SUCCESS) {
1157 lb_set_period(common, period);
1160 common->request.period_changed = 0;
1162 dlist_foreach_safe(common->livebox_list, l, n, handler) {
1163 if (handler->cbs.period_changed.cb) {
1167 cb = handler->cbs.period_changed.cb;
1168 cbdata = handler->cbs.period_changed.data;
1170 handler->cbs.period_changed.cb = NULL;
1171 handler->cbs.period_changed.data = NULL;
1173 cb(handler, status, cbdata);
1174 } else if (status == (int)LB_STATUS_SUCCESS) {
1175 lb_invoke_event_handler(handler, LB_EVENT_PERIOD_CHANGED);
1183 static struct packet *master_group_changed(pid_t pid, int handle, const struct packet *packet)
1185 struct livebox *handler;
1186 struct livebox_common *common;
1189 const char *pkgname;
1192 const char *cluster;
1193 const char *category;
1196 ret = packet_get(packet, "ssiss", &pkgname, &id, &status, &cluster, &category);
1198 ErrPrint("Invalid argument\n");
1202 common = lb_find_common_handle(pkgname, id);
1204 ErrPrint("Livebox(%s) is not exists\n", id);
1208 if (common->state != CREATE) {
1211 * Do no access this handler,
1212 * You cannot believe this handler anymore.
1214 ErrPrint("Livebox(%s) is not created\n", id);
1218 if (status == (int)LB_STATUS_SUCCESS) {
1219 (void)lb_set_group(common, cluster, category);
1222 common->request.group_changed = 0;
1224 dlist_foreach_safe(common->livebox_list, l, n, handler) {
1225 if (handler->cbs.group_changed.cb) {
1229 cb = handler->cbs.group_changed.cb;
1230 cbdata = handler->cbs.group_changed.data;
1232 handler->cbs.group_changed.cb = NULL;
1233 handler->cbs.group_changed.data = NULL;
1235 cb(handler, status, cbdata);
1236 } else if (status == (int)LB_STATUS_SUCCESS) {
1237 lb_invoke_event_handler(handler, LB_EVENT_GROUP_CHANGED);
1245 static struct packet *master_created(pid_t pid, int handle, const struct packet *packet)
1247 struct livebox *handler;
1248 struct livebox_common *common;
1255 const char *pkgname;
1258 const char *content;
1259 const char *cluster;
1260 const char *category;
1261 const char *lb_fname;
1262 const char *pd_fname;
1266 const char *auto_launch;
1270 int pinup_supported;
1271 enum lb_type lb_type;
1272 enum pd_type pd_type;
1276 int old_state = DESTROYED;
1280 ret = packet_get(packet, "dsssiiiisssssdiiiiidsi",
1282 &pkgname, &id, &content,
1283 &lb_w, &lb_h, &pd_w, &pd_h,
1284 &cluster, &category, &lb_fname, &pd_fname,
1285 &auto_launch, &priority, &size_list, &user, &pinup_supported,
1286 &lb_type, &pd_type, &period, &title, &is_pinned_up);
1288 ErrPrint("Invalid argument\n");
1289 ret = LB_STATUS_ERROR_INVALID;
1293 ErrPrint("[%lf] pkgname: %s, id: %s, content: %s, "
1294 "pd_w: %d, pd_h: %d, lb_w: %d, lb_h: %d, "
1295 "cluster: %s, category: %s, lb_fname: \"%s\", pd_fname: \"%s\", "
1296 "auto_launch: %s, priority: %lf, size_list: %d, user: %d, pinup: %d, "
1297 "lb_type: %d, pd_type: %d, period: %lf, title: [%s], is_pinned_up: %d\n",
1298 timestamp, pkgname, id, content,
1299 pd_w, pd_h, lb_w, lb_h,
1300 cluster, category, lb_fname, pd_fname,
1301 auto_launch, priority, size_list, user, pinup_supported,
1302 lb_type, pd_type, period, title, is_pinned_up);
1304 common = lb_find_common_handle_by_timestamp(timestamp);
1306 handler = lb_new_livebox(pkgname, id, timestamp, cluster, category);
1308 ErrPrint("Failed to create a new livebox\n");
1309 ret = LB_STATUS_ERROR_FAULT;
1312 common = handler->common;
1313 old_state = common->state;
1315 if (common->state != CREATE) {
1316 if (common->state != DELETE) {
1319 * This is not possible!!!
1321 ErrPrint("Invalid handler\n");
1322 ret = LB_STATUS_ERROR_INVALID;
1328 * After get the delete states,
1329 * call the create callback with deleted result.
1333 old_state = common->state;
1336 ErrPrint("Already created: timestamp[%lf] "
1337 "pkgname[%s], id[%s] content[%s] "
1338 "cluster[%s] category[%s] lb_fname[%s] pd_fname[%s]\n",
1339 timestamp, pkgname, id,
1340 content, cluster, category,
1341 lb_fname, pd_fname);
1343 ret = LB_STATUS_ERROR_ALREADY;
1347 lb_set_id(common, id);
1350 common->request.created = 0;
1351 lb_set_size(common, lb_w, lb_h);
1352 common->lb.type = lb_type;
1353 common->is_pinned_up = is_pinned_up;
1358 case _LB_TYPE_SCRIPT:
1359 case _LB_TYPE_BUFFER:
1360 if (!strlen(lb_fname)) {
1363 (void)lb_set_lb_fb(common, lb_fname);
1367 * Livebox should create the lock file from here.
1368 * Even if the old_state == DELETE,
1369 * the lock file will be deleted from deleted event callback.
1371 switch (fb_type(lb_get_lb_fb(common))) {
1372 case BUFFER_TYPE_FILE:
1373 case BUFFER_TYPE_SHM:
1374 lb_create_lock_file(common, 0);
1376 case BUFFER_TYPE_PIXMAP:
1377 case BUFFER_TYPE_ERROR:
1382 ret = lb_sync_lb_fb(common);
1384 ErrPrint("Failed to do sync FB (%s - %s)\n", pkgname, util_basename(util_uri_to_path(id)));
1388 lb_set_text_lb(common);
1394 common->pd.type = pd_type;
1395 lb_set_pdsize(common, pd_w, pd_h);
1396 lb_set_default_pdsize(common, pd_w, pd_h);
1398 case _PD_TYPE_SCRIPT:
1399 case _PD_TYPE_BUFFER:
1400 if (!strlen(pd_fname)) {
1404 lb_set_pd_fb(common, pd_fname);
1406 ret = lb_sync_pd_fb(common);
1408 ErrPrint("Failed to do sync FB (%s - %s)\n", pkgname, util_basename(util_uri_to_path(id)));
1413 * PD doesn't need to create the lock file from here.
1414 * Just create it from PD_CREATED event.
1419 lb_set_text_pd(common);
1425 lb_set_priority(common, priority);
1427 lb_set_size_list(common, size_list);
1428 lb_set_group(common, cluster, category);
1430 lb_set_content(common, content);
1431 lb_set_title(common, title);
1433 lb_set_user(common, user);
1435 lb_set_auto_launch(common, auto_launch);
1436 lb_set_pinup(common, pinup_supported);
1438 lb_set_period(common, period);
1442 if (common->state == CREATE) {
1443 dlist_foreach(common->livebox_list, l, handler) {
1446 * These callback can change the handler->state.
1447 * So we have to use the "old_state" which stored state before call these callbacks
1450 if (handler->cbs.created.cb) {
1454 cb = handler->cbs.created.cb;
1455 cbdata = handler->cbs.created.data;
1457 handler->cbs.created.cb = NULL;
1458 handler->cbs.created.data = NULL;
1460 cb(handler, ret, cbdata);
1462 lb_invoke_event_handler(handler, LB_EVENT_CREATED);
1468 if (ret == 0 && old_state == DELETE) {
1471 DbgPrint("Take place an unexpected case [%d]\n", common->refcnt);
1472 dlist_foreach_safe(common->livebox_list, l, n, handler) {
1473 if (handler->cbs.created.cb) {
1474 if (!handler->common->request.deleted) {
1475 if (lb_send_delete(handler, common->delete_type, handler->cbs.created.cb, handler->cbs.created.data) < 0) {
1478 * Already sent or something else happens.
1479 * Callback will be called in any cases
1482 } else if (handler->state != DELETE) {
1483 handler->cbs.created.cb(handler, LB_STATUS_ERROR_CANCEL, handler->cbs.created.data);
1484 lb_unref(handler, 1);
1487 lb_invoke_event_handler(handler, LB_EVENT_DELETED);
1488 lb_unref(handler, 1);
1494 * handler->cbs.created.cb = NULL;
1495 * handler->cbs.created.data = NULL;
1497 * Do not clear this to use this from the deleted event callback.
1498 * if this value is not cleared when the deleted event callback check it,
1499 * it means that the created function is not called yet.
1500 * Then the call the deleted event callback with LB_STATUS_ERROR_CANCEL errno.
1507 static struct method s_table[] = {
1509 .cmd = "lb_updated", /* pkgname, id, lb_w, lb_h, priority, ret */
1510 .handler = master_lb_updated,
1513 .cmd = "pd_updated", /* pkgname, id, descfile, pd_w, pd_h, ret */
1514 .handler = master_pd_updated,
1517 .cmd = "pd_created",
1518 .handler = master_pd_created,
1521 .cmd = "pd_destroyed",
1522 .handler = master_pd_destroyed,
1525 .cmd = "fault_package", /* pkgname, id, function, ret */
1526 .handler = master_fault_package,
1529 .cmd = "deleted", /* pkgname, id, timestamp, ret */
1530 .handler = master_deleted,
1533 .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 */
1534 .handler = master_created,
1537 .cmd = "group_changed",
1538 .handler = master_group_changed,
1541 .cmd = "period_changed",
1542 .handler = master_period_changed,
1545 .cmd = "size_changed",
1546 .handler = master_size_changed,
1550 .handler = master_pinup,
1554 .handler = master_hold_scroll,
1558 .cmd = "update_mode",
1559 .handler = master_update_mode,
1563 .cmd = "lb_update_begin",
1564 .handler = master_lb_update_begin,
1567 .cmd = "lb_update_end",
1568 .handler = master_lb_update_end,
1572 .cmd = "pd_update_begin",
1573 .handler = master_pd_update_begin,
1576 .cmd = "pd_update_end",
1577 .handler = master_pd_update_end,
1581 .cmd = "access_status",
1582 .handler = master_access_status,
1585 .cmd = "key_status",
1586 .handler = master_key_status,
1590 .handler = master_request_close_pd,
1599 static void acquire_cb(struct livebox *handler, const struct packet *result, void *data)
1602 DbgPrint("Result packet is not valid\n");
1606 if (packet_get(result, "i", &ret) != 1) {
1607 ErrPrint("Invalid argument\n");
1609 DbgPrint("Acquire returns: %d\n", ret);
1616 static inline int make_connection(void)
1618 struct packet *packet;
1621 DbgPrint("Let's making connection!\n");
1623 s_info.fd = com_core_packet_client_init(client_addr(), 0, s_table);
1624 if (s_info.fd < 0) {
1625 ErrPrint("Try this again later\n");
1626 return LB_STATUS_ERROR_IO;
1629 packet = packet_create("acquire", "d", util_timestamp());
1631 com_core_packet_client_fini(s_info.fd);
1633 return LB_STATUS_ERROR_FAULT;
1636 ret = master_rpc_async_request(NULL, packet, 1, acquire_cb, NULL);
1638 ErrPrint("Master RPC returns %d\n", ret);
1639 com_core_packet_client_fini(s_info.fd);
1641 return LB_STATUS_ERROR_IO;
1644 return LB_STATUS_SUCCESS;
1647 static int connected_cb(int handle, void *data)
1649 master_rpc_check_and_fire_consumer();
1653 static void master_started_cb(keynode_t *node, void *data)
1657 if (vconf_get_bool(VCONFKEY_MASTER_STARTED, &state) < 0) {
1658 ErrPrint("Unable to get [%s]\n", VCONFKEY_MASTER_STARTED);
1661 DbgPrint("Master state: %d\n", state);
1662 if (state == 1 && make_connection() == (int)LB_STATUS_SUCCESS) {
1664 ret = vconf_ignore_key_changed(VCONFKEY_MASTER_STARTED, master_started_cb);
1666 DbgPrint("master_started vconf key de-registered [%d]\n", ret);
1671 static gboolean timeout_cb(gpointer data)
1673 if (vconf_notify_key_changed(VCONFKEY_MASTER_STARTED, master_started_cb, NULL) < 0) {
1674 ErrPrint("Failed to add vconf for monitoring service state\n");
1676 DbgPrint("vconf event callback is registered\n");
1679 master_started_cb(NULL, NULL);
1681 s_info.timer_id = 0;
1685 static int disconnected_cb(int handle, void *data)
1687 if (s_info.fd != handle) {
1688 /*!< This handle is not my favor */
1692 s_info.fd = -1; /*!< Disconnected */
1694 master_rpc_clear_all_request();
1695 lb_invoke_fault_handler(LB_FAULT_PROVIDER_DISCONNECTED, MASTER_PKGNAME, "default", "disconnected");
1699 /* Try to reconnect after 1 sec later */
1700 if (!s_info.timer_id) {
1701 DbgPrint("Reconnecting timer is added\n");
1702 s_info.timer_id = g_timeout_add(1000, timeout_cb, NULL);
1703 if (s_info.timer_id == 0) {
1704 ErrPrint("Unable to add reconnecting timer\n");
1708 ErrPrint("Reconnecting timer is already exists\n");
1714 int client_init(int use_thread)
1716 com_core_packet_use_thread(use_thread);
1718 s_info.client_addr = vconf_get_str(VCONFKEY_MASTER_CLIENT_ADDR);
1719 if (!s_info.client_addr) {
1720 s_info.client_addr = strdup(CLIENT_SOCKET);
1721 if (!s_info.client_addr) {
1722 ErrPrint("Heap: %s\n", strerror(errno));
1727 (void)file_service_init();
1729 DbgPrint("Server Address: %s\n", s_info.client_addr);
1731 com_core_add_event_callback(CONNECTOR_DISCONNECTED, disconnected_cb, NULL);
1732 com_core_add_event_callback(CONNECTOR_CONNECTED, connected_cb, NULL);
1733 if (vconf_notify_key_changed(VCONFKEY_MASTER_STARTED, master_started_cb, NULL) < 0) {
1734 ErrPrint("Failed to add vconf for service state\n");
1736 DbgPrint("vconf event callback is registered\n");
1739 master_started_cb(NULL, NULL);
1748 const char *client_addr(void)
1750 return s_info.client_addr;
1753 int client_fini(void)
1757 (void)file_service_fini();
1759 ret = vconf_ignore_key_changed(VCONFKEY_MASTER_STARTED, master_started_cb);
1761 DbgPrint("Ignore vconf key: %d\n", ret);
1764 com_core_del_event_callback(CONNECTOR_DISCONNECTED, disconnected_cb, NULL);
1765 com_core_del_event_callback(CONNECTOR_CONNECTED, connected_cb, NULL);
1766 com_core_packet_client_fini(s_info.fd);
1768 free(s_info.client_addr);
1769 s_info.client_addr = NULL;
1770 return LB_STATUS_SUCCESS;