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"
61 static struct packet *master_fault_package(pid_t pid, int handle, const struct packet *packet)
67 if (packet_get(packet, "sss", &pkgname, &id, &function) != 3) {
68 ErrPrint("Invalid arguments\n");
72 DbgPrint("[%s]\n", pkgname);
73 master_rpc_clear_fault_package(pkgname);
74 lb_invoke_fault_handler(LB_FAULT_DEACTIVATED, pkgname, id, function);
78 static struct packet *master_hold_scroll(pid_t pid, int handle, const struct packet *packet)
80 struct livebox_common *common;
81 struct livebox *livebox;
88 ret = packet_get(packet, "ssi", &pkgname, &id, &seize);
90 ErrPrint("Invalid argument\n");
94 common = lb_find_common_handle(pkgname, id);
96 ErrPrint("Instance(%s) is not exists\n", id);
100 DbgPrint("HOLD: [%s] seize(%d)\n", id, seize);
101 seize = seize ? LB_EVENT_HOLD_SCROLL : LB_EVENT_RELEASE_SCROLL;
102 dlist_foreach(common->livebox_list, l, livebox) {
103 lb_invoke_event_handler(livebox, seize);
110 static struct packet *master_pinup(pid_t pid, int handle, const struct packet *packet)
115 struct livebox *handler;
118 struct livebox_common *common;
124 ret = packet_get(packet, "iisss", &status, &pinup, &pkgname, &id, &content);
126 ErrPrint("Invalid argument\n");
130 common = lb_find_common_handle(pkgname, id);
132 ErrPrint("Instance (%s) is not exists\n", id);
136 if (status == LB_STATUS_SUCCESS) {
137 new_content = strdup(content);
139 free(common->content);
140 common->content = new_content;
141 common->is_pinned_up = pinup;
143 ErrPrint("Heap: %s\n", strerror(errno));
144 status = LB_STATUS_ERROR_MEMORY;
148 common->request.pinup = 0;
150 dlist_foreach_safe(common->livebox_list, l, n, handler) {
151 if (handler->cbs.pinup.cb) {
155 /* Make sure that user can call pinup API in its result callback */
156 cb = handler->cbs.pinup.cb;
157 cbdata = handler->cbs.pinup.data;
159 handler->cbs.pinup.cb = NULL;
160 handler->cbs.pinup.data = NULL;
162 cb(handler, status, cbdata);
163 } else if (status == LB_STATUS_SUCCESS) {
164 lb_invoke_event_handler(handler, LB_EVENT_PINUP_CHANGED);
172 static struct packet *master_deleted(pid_t pid, int handle, const struct packet *packet)
177 struct livebox *handler;
178 struct livebox_common *common;
183 if (packet_get(packet, "ssdi", &pkgname, &id, ×tamp, &reason) != 4) {
184 ErrPrint("Invalid arguemnt\n");
188 DbgPrint("[%s]\n", pkgname);
189 common = lb_find_common_handle_by_timestamp(timestamp);
193 * This can be happens only if the user delete a livebox
194 * right after create it before receive created event.
199 /*!< Check validity of this "handler" */
200 if (common->state != CREATE) {
201 if (common->state != DELETE) {
204 * This is not possible
206 ErrPrint("Already deleted handler (%s - %s)\n", pkgname, id);
211 common->request.deleted = 0;
213 * We should change the state of "common handler' before handling the callbacks.
214 * Because if user tries to create a new handle in the callbacks,
215 * find_sharable_common_handle will returns destroying object.
216 * Then we will get panic.
217 * To prevent it, we should change its state first.
219 common->state = DELETE;
221 dlist_foreach_safe(common->livebox_list, l, n, handler) {
222 if (handler->cbs.created.cb) {
228 * "if (handler->id == NULL) {"
230 * The instance is not created yet.
231 * But the master forcely destroy it and send destroyed event to this
232 * without the created event.
234 * It could be destroyed when a slave has critical error(fault)
235 * before creating an instance successfully.
237 if (handler->cbs.created.cb == handler->cbs.deleted.cb) {
238 if (handler->cbs.created.data != handler->cbs.deleted.data) {
239 DbgPrint("cb is same but cbdata is different (%s - %s)\n", pkgname, id);
242 handler->cbs.deleted.cb = NULL;
243 handler->cbs.deleted.data = NULL;
246 cb = handler->cbs.created.cb;
247 cbdata = handler->cbs.created.data;
249 handler->cbs.created.cb = NULL;
250 handler->cbs.created.data = NULL;
252 if (reason == LB_STATUS_SUCCESS) {
253 reason = LB_STATUS_ERROR_CANCEL;
256 cb(handler, reason, cbdata);
257 } else if (common->id) {
258 if (handler->cbs.deleted.cb) {
262 cb = handler->cbs.deleted.cb;
263 cbdata = handler->cbs.deleted.data;
265 handler->cbs.deleted.cb = NULL;
266 handler->cbs.deleted.data = NULL;
268 cb(handler, reason, cbdata);
270 lb_invoke_event_handler(handler, LB_EVENT_DELETED);
274 /* Just try to delete it, if a user didn't remove it from the live box list */
275 lb_unref(handler, 1);
282 static struct packet *master_lb_update_begin(pid_t pid, int handle, const struct packet *packet)
284 struct livebox *handler;
285 struct livebox_common *common;
294 ret = packet_get(packet, "ssdsss", &pkgname, &id, &priority, &content, &title, &fbfile);
296 ErrPrint("Invalid argument\n");
300 common = lb_find_common_handle(pkgname, id);
302 ErrPrint("Instance[%s] is not exists\n", id);
306 if (common->state != CREATE) {
307 ErrPrint("(%s) is not created\n", id);
311 lb_set_priority(common, priority);
312 lb_set_content(common, content);
313 lb_set_title(common, title);
317 * Width & Height is not changed in this case.
318 * If the active update is began, the size should not be changed,
319 * And if the size is changed, the provider should finish the updating first.
320 * And then begin updating again after change its size.
322 if (lb_get_lb_fb(common)) {
323 (void)lb_set_lb_fb(common, fbfile);
325 ret = lb_sync_lb_fb(common);
327 if (ret != LB_STATUS_SUCCESS) {
328 ErrPrint("Failed to do sync FB (%s - %s) (%d)\n", pkgname, fbfile, ret);
331 dlist_foreach(common->livebox_list, l, handler) {
332 lb_invoke_event_handler(handler, LB_EVENT_LB_UPDATE_BEGIN);
336 ErrPrint("Invalid request[%s], %s\n", id, fbfile);
343 static struct packet *master_pd_update_begin(pid_t pid, int handle, const struct packet *packet)
345 struct livebox *handler;
346 struct livebox_common *common;
352 ret = packet_get(packet, "sss", &pkgname, &id, &fbfile);
354 ErrPrint("Invalid argument\n");
358 common = lb_find_common_handle(pkgname, id);
360 ErrPrint("Instance[%s] is not exists\n", id);
364 if (common->state != CREATE) {
365 ErrPrint("[%s] is not created\n", id);
369 if (lb_get_pd_fb(common)) {
370 (void)lb_set_pd_fb(common, fbfile);
372 ret = lb_sync_pd_fb(common);
373 if (ret != LB_STATUS_SUCCESS) {
374 ErrPrint("Failed to do sync FB (%s - %s) (%d)\n", pkgname, fbfile, ret);
377 dlist_foreach(common->livebox_list, l, handler) {
378 lb_invoke_event_handler(handler, LB_EVENT_PD_UPDATE_BEGIN);
382 ErrPrint("Invalid request[%s], %s\n", id, fbfile);
389 static struct packet *master_lb_update_end(pid_t pid, int handle, const struct packet *packet)
391 struct livebox *handler;
392 struct livebox_common *common;
397 ret = packet_get(packet, "ss", &pkgname, &id);
399 ErrPrint("Invalid argument\n");
403 common = lb_find_common_handle(pkgname, id);
405 ErrPrint("Instance[%s] is not exists\n", id);
409 if (common->state != CREATE) {
410 ErrPrint("[%s] is not created\n", id);
414 if (lb_get_lb_fb(common)) {
416 dlist_foreach(common->livebox_list, l, handler) {
417 lb_invoke_event_handler(handler, LB_EVENT_LB_UPDATE_END);
420 ErrPrint("Invalid request[%s]\n", id);
427 static struct packet *master_key_status(pid_t pid, int handle, const struct packet *packet)
429 struct livebox *handler;
430 struct livebox_common *common;
437 ret = packet_get(packet, "ssi", &pkgname, &id, &status);
439 ErrPrint("Invalid argument\n");
443 common = lb_find_common_handle(pkgname, id);
445 ErrPrint("Instance[%s] is not exists\n", id);
449 if (common->state != CREATE) {
450 ErrPrint("[%s] is not created\n", id);
454 common->request.key_event = 0;
455 dlist_foreach(common->livebox_list, l, handler) {
456 if (handler->cbs.key_event.cb) {
460 cb = handler->cbs.key_event.cb;
461 cbdata = handler->cbs.key_event.data;
463 handler->cbs.key_event.cb = NULL;
464 handler->cbs.key_event.data = NULL;
466 cb(handler, status, cbdata);
468 ErrPrint("Invalid event[%s]\n", id);
476 static struct packet *master_request_close_pd(pid_t pid, int handle, const struct packet *packet)
478 struct livebox *handler;
479 struct livebox_common *common;
486 ret = packet_get(packet, "ssi", &pkgname, &id, &reason);
488 ErrPrint("Invalid argument\n");
492 common = lb_find_common_handle(pkgname, id);
494 ErrPrint("Instance[%s] is not exists\n", id);
498 if (common->state != CREATE) {
499 ErrPrint("[%s] is not created\n", id);
503 if (!common->is_pd_created) {
504 DbgPrint("PD is not created, closing what?(%s)\n", id);
508 DbgPrint("Reason: %d\n", reason);
510 dlist_foreach(common->livebox_list, l, handler) {
511 lb_invoke_event_handler(handler, LB_EVENT_REQUEST_CLOSE_PD);
517 static struct packet *master_access_status(pid_t pid, int handle, const struct packet *packet)
519 struct livebox *handler;
520 struct livebox_common *common;
527 ret = packet_get(packet, "ssi", &pkgname, &id, &status);
529 ErrPrint("Invalid argument\n");
533 common = lb_find_common_handle(pkgname, id);
535 ErrPrint("Instance[%s] is not exists\n", id);
539 if (common->state != CREATE) {
540 ErrPrint("[%s] is not created\n", id);
544 common->request.access_event = 0;
545 dlist_foreach(common->livebox_list, l, handler) {
546 if (handler->cbs.access_event.cb) {
550 cb = handler->cbs.access_event.cb;
551 cbdata = handler->cbs.access_event.data;
553 handler->cbs.access_event.cb = NULL;
554 handler->cbs.access_event.data = NULL;
556 cb(handler, status, cbdata);
563 static struct packet *master_pd_update_end(pid_t pid, int handle, const struct packet *packet)
565 struct livebox *handler;
566 struct livebox_common *common;
571 ret = packet_get(packet, "ss", &pkgname, &id);
573 ErrPrint("Invalid argument\n");
577 common = lb_find_common_handle(pkgname, id);
579 ErrPrint("Instance[%s] is not exists\n", id);
583 if (common->state != CREATE) {
584 ErrPrint("[%s] is not created\n", id);
588 if (lb_get_lb_fb(common)) {
591 dlist_foreach(common->livebox_list, l, handler) {
592 lb_invoke_event_handler(handler, LB_EVENT_PD_UPDATE_END);
595 ErrPrint("Invalid request[%s]", id);
602 static struct packet *master_lb_updated(pid_t pid, int handle, const struct packet *packet)
609 const char *safe_file;
612 struct livebox *handler;
613 struct livebox_common *common;
619 ret = packet_get(packet, "sssiidsssss",
621 &fbfile, &lb_w, &lb_h,
622 &priority, &content, &title,
623 &safe_file, &icon, &name);
625 ErrPrint("Invalid argument\n");
629 common = lb_find_common_handle(pkgname, id);
631 ErrPrint("instance(%s) is not exists\n", id);
635 if (common->state != CREATE) {
638 * Already deleted by the user.
639 * Don't try to notice anything with this, Just ignore all events
640 * Beacuse the user doesn't wants know about this anymore
642 ErrPrint("(%s) is not exists, but updated\n", id);
646 lb_set_priority(common, priority);
647 lb_set_content(common, content);
648 lb_set_title(common, title);
649 lb_set_size(common, lb_w, lb_h);
650 lb_set_filename(common, safe_file);
652 if (lb_text_lb(common)) {
653 const char *common_filename;
655 common_filename = common->filename ? common->filename : util_uri_to_path(common->id);
657 (void)parse_desc(common, common_filename, 0);
660 * DESC parser will call the "text event callback".
661 * Don't need to call global event callback in this case.
664 } else if (lb_get_lb_fb(common)) {
667 * replace this with "flag" instead of "callback address"
669 if (conf_frame_drop_for_resizing() && common->request.size_changed) {
670 /* Just for skipping the update event callback call, After request to resize buffer, update event will be discarded */
671 DbgPrint("Discards obsoloted update event\n");
672 ret = LB_STATUS_ERROR_BUSY;
674 (void)lb_set_lb_fb(common, fbfile);
676 if (!conf_manual_sync()) {
677 ret = lb_sync_lb_fb(common);
678 if (ret != LB_STATUS_SUCCESS) {
679 ErrPrint("Failed to do sync FB (%s - %s) (%d)\n", pkgname, util_basename(util_uri_to_path(id)), ret);
682 ret = LB_STATUS_SUCCESS;
686 ret = LB_STATUS_SUCCESS;
689 if (ret == LB_STATUS_SUCCESS) {
693 dlist_foreach_safe(common->livebox_list, l, n, handler) {
694 lb_invoke_event_handler(handler, LB_EVENT_LB_UPDATED);
702 static struct packet *master_pd_created(pid_t pid, int handle, const struct packet *packet)
704 struct livebox *handler;
705 struct livebox_common *common;
716 ret = packet_get(packet, "sssiii", &pkgname, &id, &buf_id, &width, &height, &status);
718 ErrPrint("Invalid argument\n");
722 DbgPrint("[%s]\n", pkgname);
723 common = lb_find_common_handle(pkgname, id);
725 ErrPrint("Instance(%s) is not exists\n", id);
729 if (common->state != CREATE) {
730 ErrPrint("Instance(%s) is not created\n", id);
734 if (!common->request.pd_created) {
735 ErrPrint("PD create request is canceled\n");
739 common->is_pd_created = (status == LB_STATUS_SUCCESS);
740 common->request.pd_created = 0;
742 if (common->is_pd_created) {
743 lb_set_pdsize(common, width, height);
744 if (lb_text_pd(common)) {
745 DbgPrint("Text TYPE does not need to handle this\n");
747 (void)lb_set_pd_fb(common, buf_id);
749 switch (common->pd.type) {
750 case _PD_TYPE_SCRIPT:
751 case _PD_TYPE_BUFFER:
752 switch (fb_type(lb_get_pd_fb(common))) {
753 case BUFFER_TYPE_FILE:
754 case BUFFER_TYPE_SHM:
755 lb_create_lock_file(common, 1);
757 case BUFFER_TYPE_PIXMAP:
758 case BUFFER_TYPE_ERROR:
768 ret = lb_sync_pd_fb(common);
770 ErrPrint("Failed to do sync FB (%s - %s)\n", pkgname, util_basename(util_uri_to_path(id)));
775 DbgPrint("PERF_DBOX\n");
776 dlist_foreach_safe(common->livebox_list, l, n, handler) {
777 if (handler->cbs.pd_created.cb) {
781 cb = handler->cbs.pd_created.cb;
782 cbdata = handler->cbs.pd_created.data;
784 handler->cbs.pd_created.cb = NULL;
785 handler->cbs.pd_created.data = NULL;
788 * Before call the Callback function,
789 * pd_create_cb must be reset.
790 * Because, in the create callback, user can call create_pd function again.
792 cb(handler, status, cbdata);
793 } else if (status == LB_STATUS_SUCCESS) {
794 lb_invoke_event_handler(handler, LB_EVENT_PD_CREATED);
802 static struct packet *master_pd_destroyed(pid_t pid, int handle, const struct packet *packet)
804 struct livebox *handler;
806 struct livebox_common *common;
812 ret = packet_get(packet, "ssi", &pkgname, &id, &status);
814 ErrPrint("Invalid argument\n");
818 DbgPrint("[%s]\n", pkgname);
819 common = lb_find_common_handle(pkgname, id);
821 ErrPrint("Instance(%s) is not exists\n", id);
825 if (common->state != CREATE) {
826 ErrPrint("Instance(%s) is not created\n", id);
830 if (common->is_pd_created == 0) {
831 ErrPrint("PD is not created, event is ignored\n");
835 common->is_pd_created = 0;
836 common->request.pd_destroyed = 0;
838 dlist_foreach(common->livebox_list, l, handler) {
839 if (handler->cbs.pd_destroyed.cb) {
843 cb = handler->cbs.pd_destroyed.cb;
844 cbdata = handler->cbs.pd_destroyed.data;
846 handler->cbs.pd_destroyed.cb = NULL;
847 handler->cbs.pd_destroyed.data = NULL;
850 * Before call the Callback function,
851 * pd_destroyed_cb must be reset.
852 * Because, in the create callback, user can call destroy_pd function again.
854 cb(handler, status, cbdata);
855 } else if (status == LB_STATUS_SUCCESS) {
856 lb_invoke_event_handler(handler, LB_EVENT_PD_DESTROYED);
862 * Lock file should be deleted after all callbacks are processed.
864 switch (common->pd.type) {
865 case _PD_TYPE_SCRIPT:
866 case _PD_TYPE_BUFFER:
867 switch (fb_type(lb_get_pd_fb(common))) {
868 case BUFFER_TYPE_FILE:
869 case BUFFER_TYPE_SHM:
870 lb_destroy_lock_file(common, 1);
872 case BUFFER_TYPE_PIXMAP:
873 case BUFFER_TYPE_ERROR:
887 static struct packet *master_pd_updated(pid_t pid, int handle, const struct packet *packet)
891 const char *descfile;
894 struct livebox *handler;
895 struct livebox_common *common;
900 ret = packet_get(packet, "ssssii",
905 ErrPrint("Invalid argument\n");
909 DbgPrint("[%s]\n", pkgname);
910 common = lb_find_common_handle(pkgname, id);
912 ErrPrint("Instance(%s) is not exists\n", id);
916 if (common->state != CREATE) {
919 * This handler is already deleted by the user.
920 * So don't try to notice anything about this anymore.
921 * Just ignore all events.
923 ErrPrint("Instance(%s) is not created\n", id);
927 lb_set_pdsize(common, pd_w, pd_h);
929 if (lb_text_pd(common)) {
930 (void)parse_desc(common, descfile, 1);
932 if (conf_frame_drop_for_resizing() && common->request.size_changed) {
933 /* Just for skipping the update event callback call, After request to resize buffer, update event will be discarded */
934 DbgPrint("Discards obsoloted update event\n");
936 (void)lb_set_pd_fb(common, fbfile);
938 if (!conf_manual_sync()) {
939 ret = lb_sync_pd_fb(common);
941 ErrPrint("Failed to do sync FB (%s - %s), %d\n", pkgname, util_basename(util_uri_to_path(id)), ret);
943 dlist_foreach(common->livebox_list, l, handler) {
944 lb_invoke_event_handler(handler, LB_EVENT_PD_UPDATED);
948 dlist_foreach(common->livebox_list, l, handler) {
949 lb_invoke_event_handler(handler, LB_EVENT_PD_UPDATED);
959 static struct packet *master_update_mode(pid_t pid, int handle, const struct packet *packet)
961 struct livebox *handler;
962 struct livebox_common *common;
972 ErrPrint("Invalid packet\n");
976 ret = packet_get(packet, "ssii", &pkgname, &id, &status, &active_mode);
978 ErrPrint("Invalid argument\n");
982 common = lb_find_common_handle(pkgname, id);
984 ErrPrint("Livebox(%s) is not found\n", id);
988 if (common->state != CREATE) {
989 ErrPrint("Livebox(%s) is not created yet\n", id);
993 if (status == LB_STATUS_SUCCESS) {
994 lb_set_update_mode(common, active_mode);
997 common->request.update_mode = 0;
998 dlist_foreach_safe(common->livebox_list, l, n, handler) {
999 if (handler->cbs.update_mode.cb) {
1003 cb = handler->cbs.update_mode.cb;
1004 cbdata = handler->cbs.update_mode.data;
1006 handler->cbs.update_mode.cb = NULL;
1007 handler->cbs.update_mode.data = NULL;
1009 cb(handler, status, cbdata);
1010 } else if (status == LB_STATUS_SUCCESS) {
1011 lb_invoke_event_handler(handler, LB_EVENT_UPDATE_MODE_CHANGED);
1019 static struct packet *master_size_changed(pid_t pid, int handle, const struct packet *packet)
1021 struct livebox *handler;
1022 struct livebox_common *common;
1023 const char *pkgname;
1033 ErrPrint("Invalid packet\n");
1037 ret = packet_get(packet, "sssiiii", &pkgname, &id, &fbfile, &is_pd, &w, &h, &status);
1039 ErrPrint("Invalid argument\n");
1043 common = lb_find_common_handle(pkgname, id);
1045 ErrPrint("Livebox(%s) is not found\n", id);
1049 if (common->state != CREATE) {
1050 ErrPrint("Livebox(%s) is not created yet\n", id);
1054 common->request.size_changed = 0;
1058 * PD is not able to resized by the client.
1059 * PD is only can be managed by the provider.
1060 * So the PD has no private resized event handler.
1061 * Notify it via global event handler only.
1063 if (status == LB_STATUS_SUCCESS) {
1066 lb_set_pdsize(common, w, h);
1067 dlist_foreach(common->livebox_list, l, handler) {
1068 lb_invoke_event_handler(handler, LB_EVENT_PD_SIZE_CHANGED);
1071 ErrPrint("This is not possible. PD Size is changed but the return value is not ZERO (%d)\n", status);
1077 if (status == LB_STATUS_SUCCESS) {
1078 lb_set_size(common, w, h);
1082 * If there is a created LB FB,
1085 if (lb_get_lb_fb(common)) {
1086 (void)lb_set_lb_fb(common, fbfile);
1088 ret = lb_sync_lb_fb(common);
1090 ErrPrint("Failed to do sync FB (%s - %s)\n", pkgname, util_basename(util_uri_to_path(id)));
1093 /* Just update the size info only. */
1099 * I cannot believe client.
1100 * So I added some log before & after call the user callback.
1102 dlist_foreach_safe(common->livebox_list, l, n, handler) {
1103 if (handler->cbs.size_changed.cb) {
1107 cb = handler->cbs.size_changed.cb;
1108 cbdata = handler->cbs.size_changed.data;
1110 handler->cbs.size_changed.cb = NULL;
1111 handler->cbs.size_changed.data = NULL;
1113 cb(handler, status, cbdata);
1114 } else if (status == LB_STATUS_SUCCESS) {
1115 lb_invoke_event_handler(handler, LB_EVENT_LB_SIZE_CHANGED);
1124 static struct packet *master_period_changed(pid_t pid, int handle, const struct packet *packet)
1126 struct livebox *handler;
1127 struct livebox_common *common;
1130 const char *pkgname;
1136 ret = packet_get(packet, "idss", &status, &period, &pkgname, &id);
1138 ErrPrint("Invalid argument\n");
1142 common = lb_find_common_handle(pkgname, id);
1144 ErrPrint("Livebox(%s) is not found\n", id);
1148 if (common->state != CREATE) {
1149 ErrPrint("Livebox(%s) is not created\n", id);
1153 if (status == LB_STATUS_SUCCESS) {
1154 lb_set_period(common, period);
1157 common->request.period_changed = 0;
1159 dlist_foreach_safe(common->livebox_list, l, n, handler) {
1160 if (handler->cbs.period_changed.cb) {
1164 cb = handler->cbs.period_changed.cb;
1165 cbdata = handler->cbs.period_changed.data;
1167 handler->cbs.period_changed.cb = NULL;
1168 handler->cbs.period_changed.data = NULL;
1170 cb(handler, status, cbdata);
1171 } else if (status == LB_STATUS_SUCCESS) {
1172 lb_invoke_event_handler(handler, LB_EVENT_PERIOD_CHANGED);
1180 static struct packet *master_group_changed(pid_t pid, int handle, const struct packet *packet)
1182 struct livebox *handler;
1183 struct livebox_common *common;
1186 const char *pkgname;
1189 const char *cluster;
1190 const char *category;
1193 ret = packet_get(packet, "ssiss", &pkgname, &id, &status, &cluster, &category);
1195 ErrPrint("Invalid argument\n");
1199 common = lb_find_common_handle(pkgname, id);
1201 ErrPrint("Livebox(%s) is not exists\n", id);
1205 if (common->state != CREATE) {
1208 * Do no access this handler,
1209 * You cannot believe this handler anymore.
1211 ErrPrint("Livebox(%s) is not created\n", id);
1215 if (status == LB_STATUS_SUCCESS) {
1216 (void)lb_set_group(common, cluster, category);
1219 common->request.group_changed = 0;
1221 dlist_foreach_safe(common->livebox_list, l, n, handler) {
1222 if (handler->cbs.group_changed.cb) {
1226 cb = handler->cbs.group_changed.cb;
1227 cbdata = handler->cbs.group_changed.data;
1229 handler->cbs.group_changed.cb = NULL;
1230 handler->cbs.group_changed.data = NULL;
1232 cb(handler, status, cbdata);
1233 } else if (status == LB_STATUS_SUCCESS) {
1234 lb_invoke_event_handler(handler, LB_EVENT_GROUP_CHANGED);
1242 static struct packet *master_created(pid_t pid, int handle, const struct packet *packet)
1244 struct livebox *handler;
1245 struct livebox_common *common;
1252 const char *pkgname;
1255 const char *content;
1256 const char *cluster;
1257 const char *category;
1258 const char *lb_fname;
1259 const char *pd_fname;
1263 const char *auto_launch;
1267 int pinup_supported;
1268 enum lb_type lb_type;
1269 enum pd_type pd_type;
1273 int old_state = DESTROYED;
1277 ret = packet_get(packet, "dsssiiiisssssdiiiiidsi",
1279 &pkgname, &id, &content,
1280 &lb_w, &lb_h, &pd_w, &pd_h,
1281 &cluster, &category, &lb_fname, &pd_fname,
1282 &auto_launch, &priority, &size_list, &user, &pinup_supported,
1283 &lb_type, &pd_type, &period, &title, &is_pinned_up);
1285 ErrPrint("Invalid argument\n");
1286 ret = LB_STATUS_ERROR_INVALID;
1290 ErrPrint("[%lf] pkgname: %s, id: %s, content: %s, "
1291 "pd_w: %d, pd_h: %d, lb_w: %d, lb_h: %d, "
1292 "cluster: %s, category: %s, lb_fname: \"%s\", pd_fname: \"%s\", "
1293 "auto_launch: %s, priority: %lf, size_list: %d, user: %d, pinup: %d, "
1294 "lb_type: %d, pd_type: %d, period: %lf, title: [%s], is_pinned_up: %d\n",
1295 timestamp, pkgname, id, content,
1296 pd_w, pd_h, lb_w, lb_h,
1297 cluster, category, lb_fname, pd_fname,
1298 auto_launch, priority, size_list, user, pinup_supported,
1299 lb_type, pd_type, period, title, is_pinned_up);
1301 common = lb_find_common_handle_by_timestamp(timestamp);
1303 handler = lb_new_livebox(pkgname, id, timestamp, cluster, category);
1305 ErrPrint("Failed to create a new livebox\n");
1306 ret = LB_STATUS_ERROR_FAULT;
1309 common = handler->common;
1310 old_state = common->state;
1312 if (common->state != CREATE) {
1313 if (common->state != DELETE) {
1316 * This is not possible!!!
1318 ErrPrint("Invalid handler\n");
1319 ret = LB_STATUS_ERROR_INVALID;
1325 * After get the delete states,
1326 * call the create callback with deleted result.
1330 old_state = common->state;
1333 ErrPrint("Already created: timestamp[%lf] "
1334 "pkgname[%s], id[%s] content[%s] "
1335 "cluster[%s] category[%s] lb_fname[%s] pd_fname[%s]\n",
1336 timestamp, pkgname, id,
1337 content, cluster, category,
1338 lb_fname, pd_fname);
1340 ret = LB_STATUS_ERROR_ALREADY;
1344 lb_set_id(common, id);
1347 common->request.created = 0;
1348 lb_set_size(common, lb_w, lb_h);
1349 common->lb.type = lb_type;
1350 common->is_pinned_up = is_pinned_up;
1355 case _LB_TYPE_SCRIPT:
1356 case _LB_TYPE_BUFFER:
1357 if (!strlen(lb_fname)) {
1360 (void)lb_set_lb_fb(common, lb_fname);
1364 * Livebox should create the lock file from here.
1365 * Even if the old_state == DELETE,
1366 * the lock file will be deleted from deleted event callback.
1368 switch (fb_type(lb_get_lb_fb(common))) {
1369 case BUFFER_TYPE_FILE:
1370 case BUFFER_TYPE_SHM:
1371 lb_create_lock_file(common, 0);
1373 case BUFFER_TYPE_PIXMAP:
1374 case BUFFER_TYPE_ERROR:
1379 ret = lb_sync_lb_fb(common);
1381 ErrPrint("Failed to do sync FB (%s - %s)\n", pkgname, util_basename(util_uri_to_path(id)));
1385 lb_set_text_lb(common);
1391 common->pd.type = pd_type;
1392 lb_set_pdsize(common, pd_w, pd_h);
1393 lb_set_default_pdsize(common, pd_w, pd_h);
1395 case _PD_TYPE_SCRIPT:
1396 case _PD_TYPE_BUFFER:
1397 if (!strlen(pd_fname)) {
1401 lb_set_pd_fb(common, pd_fname);
1403 ret = lb_sync_pd_fb(common);
1405 ErrPrint("Failed to do sync FB (%s - %s)\n", pkgname, util_basename(util_uri_to_path(id)));
1410 * PD doesn't need to create the lock file from here.
1411 * Just create it from PD_CREATED event.
1416 lb_set_text_pd(common);
1422 lb_set_priority(common, priority);
1424 lb_set_size_list(common, size_list);
1425 lb_set_group(common, cluster, category);
1427 lb_set_content(common, content);
1428 lb_set_title(common, title);
1430 lb_set_user(common, user);
1432 lb_set_auto_launch(common, auto_launch);
1433 lb_set_pinup(common, pinup_supported);
1435 lb_set_period(common, period);
1439 if (common->state == CREATE) {
1440 dlist_foreach(common->livebox_list, l, handler) {
1443 * These callback can change the handler->state.
1444 * So we have to use the "old_state" which stored state before call these callbacks
1447 if (handler->cbs.created.cb) {
1451 cb = handler->cbs.created.cb;
1452 cbdata = handler->cbs.created.data;
1454 handler->cbs.created.cb = NULL;
1455 handler->cbs.created.data = NULL;
1457 cb(handler, ret, cbdata);
1459 lb_invoke_event_handler(handler, LB_EVENT_CREATED);
1465 if (ret == 0 && old_state == DELETE) {
1468 DbgPrint("Take place unexpected case\n");
1469 cnt = common->refcnt;
1471 l = dlist_nth(common->livebox_list, 0);
1472 handler = dlist_data(l);
1474 if (handler->cbs.created.cb) {
1475 if (!handler->common->request.deleted) {
1476 if (lb_send_delete(handler, common->delete_type, handler->cbs.created.cb, handler->cbs.created.data) < 0) {
1479 * Already sent or something else happens.
1480 * Callback will be called in any cases
1483 } else if (handler->state != DELETE) {
1484 handler->cbs.created.cb(handler, LB_STATUS_ERROR_CANCEL, handler->cbs.created.data);
1485 lb_unref(handler, 1);
1488 lb_invoke_event_handler(handler, LB_EVENT_DELETED);
1489 lb_unref(handler, 1);
1497 * handler->cbs.created.cb = NULL;
1498 * handler->cbs.created.data = NULL;
1500 * Do not clear this to use this from the deleted event callback.
1501 * if this value is not cleared when the deleted event callback check it,
1502 * it means that the created function is not called yet.
1503 * Then the call the deleted event callback with LB_STATUS_ERROR_CANCEL errno.
1510 static struct method s_table[] = {
1512 .cmd = "lb_updated", /* pkgname, id, lb_w, lb_h, priority, ret */
1513 .handler = master_lb_updated,
1516 .cmd = "pd_updated", /* pkgname, id, descfile, pd_w, pd_h, ret */
1517 .handler = master_pd_updated,
1520 .cmd = "pd_created",
1521 .handler = master_pd_created,
1524 .cmd = "pd_destroyed",
1525 .handler = master_pd_destroyed,
1528 .cmd = "fault_package", /* pkgname, id, function, ret */
1529 .handler = master_fault_package,
1532 .cmd = "deleted", /* pkgname, id, timestamp, ret */
1533 .handler = master_deleted,
1536 .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 */
1537 .handler = master_created,
1540 .cmd = "group_changed",
1541 .handler = master_group_changed,
1544 .cmd = "period_changed",
1545 .handler = master_period_changed,
1548 .cmd = "size_changed",
1549 .handler = master_size_changed,
1553 .handler = master_pinup,
1557 .handler = master_hold_scroll,
1561 .cmd = "update_mode",
1562 .handler = master_update_mode,
1566 .cmd = "lb_update_begin",
1567 .handler = master_lb_update_begin,
1570 .cmd = "lb_update_end",
1571 .handler = master_lb_update_end,
1575 .cmd = "pd_update_begin",
1576 .handler = master_pd_update_begin,
1579 .cmd = "pd_update_end",
1580 .handler = master_pd_update_end,
1584 .cmd = "access_status",
1585 .handler = master_access_status,
1588 .cmd = "key_status",
1589 .handler = master_key_status,
1593 .handler = master_request_close_pd,
1602 static void acquire_cb(struct livebox *handler, const struct packet *result, void *data)
1605 DbgPrint("Result packet is not valid\n");
1609 if (packet_get(result, "i", &ret) != 1) {
1610 ErrPrint("Invalid argument\n");
1612 DbgPrint("Acquire returns: %d\n", ret);
1619 static inline int make_connection(void)
1621 struct packet *packet;
1624 DbgPrint("Let's making connection!\n");
1626 s_info.fd = com_core_packet_client_init(client_addr(), 0, s_table);
1627 if (s_info.fd < 0) {
1628 ErrPrint("Try this again later\n");
1629 return LB_STATUS_ERROR_IO;
1632 packet = packet_create("acquire", "d", util_timestamp());
1634 com_core_packet_client_fini(s_info.fd);
1636 return LB_STATUS_ERROR_FAULT;
1639 ret = master_rpc_async_request(NULL, packet, 1, acquire_cb, NULL);
1641 ErrPrint("Master RPC returns %d\n", ret);
1642 com_core_packet_client_fini(s_info.fd);
1644 return LB_STATUS_ERROR_IO;
1647 return LB_STATUS_SUCCESS;
1650 static int connected_cb(int handle, void *data)
1652 master_rpc_check_and_fire_consumer();
1656 static void master_started_cb(keynode_t *node, void *data)
1660 if (vconf_get_bool(VCONFKEY_MASTER_STARTED, &state) < 0) {
1661 ErrPrint("Unable to get [%s]\n", VCONFKEY_MASTER_STARTED);
1664 DbgPrint("Master state: %d\n", state);
1665 if (state == 1 && make_connection() == LB_STATUS_SUCCESS) {
1667 ret = vconf_ignore_key_changed(VCONFKEY_MASTER_STARTED, master_started_cb);
1668 DbgPrint("master_started vconf key de-registered [%d]\n", ret);
1672 static gboolean timeout_cb(gpointer data)
1674 if (vconf_notify_key_changed(VCONFKEY_MASTER_STARTED, master_started_cb, NULL) < 0) {
1675 ErrPrint("Failed to add vconf for monitoring service state\n");
1677 DbgPrint("vconf event callback is registered\n");
1680 master_started_cb(NULL, NULL);
1682 s_info.timer_id = 0;
1686 static int disconnected_cb(int handle, void *data)
1688 if (s_info.fd != handle) {
1689 /*!< This handle is not my favor */
1693 s_info.fd = -1; /*!< Disconnected */
1695 master_rpc_clear_all_request();
1696 lb_invoke_fault_handler(LB_FAULT_PROVIDER_DISCONNECTED, MASTER_PKGNAME, "default", "disconnected");
1700 /* Try to reconnect after 1 sec later */
1701 if (!s_info.timer_id) {
1702 DbgPrint("Reconnecting timer is added\n");
1703 s_info.timer_id = g_timeout_add(1000, timeout_cb, NULL);
1704 if (s_info.timer_id == 0) {
1705 ErrPrint("Unable to add reconnecting timer\n");
1709 ErrPrint("Reconnecting timer is already exists\n");
1715 int client_init(int use_thread)
1717 com_core_packet_use_thread(use_thread);
1719 s_info.client_addr = vconf_get_str(VCONFKEY_MASTER_CLIENT_ADDR);
1720 if (!s_info.client_addr) {
1721 s_info.client_addr = strdup(CLIENT_SOCKET);
1722 if (!s_info.client_addr) {
1723 ErrPrint("Heap: %s\n", strerror(errno));
1728 (void)file_service_init();
1730 DbgPrint("Server Address: %s\n", s_info.client_addr);
1732 com_core_add_event_callback(CONNECTOR_DISCONNECTED, disconnected_cb, NULL);
1733 com_core_add_event_callback(CONNECTOR_CONNECTED, connected_cb, NULL);
1734 if (vconf_notify_key_changed(VCONFKEY_MASTER_STARTED, master_started_cb, NULL) < 0) {
1735 ErrPrint("Failed to add vconf for service state\n");
1737 DbgPrint("vconf event callback is registered\n");
1740 master_started_cb(NULL, NULL);
1749 const char *client_addr(void)
1751 return s_info.client_addr;
1754 int client_fini(void)
1758 (void)file_service_fini();
1760 ret = vconf_ignore_key_changed(VCONFKEY_MASTER_STARTED, master_started_cb);
1762 DbgPrint("Ignore vconf key: %d\n", ret);
1765 com_core_del_event_callback(CONNECTOR_DISCONNECTED, disconnected_cb, NULL);
1766 com_core_del_event_callback(CONNECTOR_CONNECTED, connected_cb, NULL);
1767 com_core_packet_client_fini(s_info.fd);
1769 free(s_info.client_addr);
1770 s_info.client_addr = NULL;
1771 return LB_STATUS_SUCCESS;