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"
60 static struct packet *master_fault_package(pid_t pid, int handle, const struct packet *packet)
66 if (packet_get(packet, "sss", &pkgname, &id, &function) != 3) {
67 ErrPrint("Invalid arguments\n");
71 master_rpc_clear_fault_package(pkgname);
72 lb_invoke_fault_handler(LB_FAULT_DEACTIVATED, pkgname, id, function);
76 static struct packet *master_hold_scroll(pid_t pid, int handle, const struct packet *packet)
78 struct livebox *handler;
84 ret = packet_get(packet, "ssi", &pkgname, &id, &seize);
86 ErrPrint("Invalid argument\n");
90 handler = lb_find_livebox(pkgname, id);
92 ErrPrint("Instance(%s) is not exists\n", id);
96 DbgPrint("HOLD: %s %d\n", id, seize);
97 lb_invoke_event_handler(handler, seize ? LB_EVENT_HOLD_SCROLL : LB_EVENT_RELEASE_SCROLL);
103 static struct packet *master_pinup(pid_t pid, int handle, const struct packet *packet)
108 struct livebox *handler;
114 ret = packet_get(packet, "iisss", &status, &pinup, &pkgname, &id, &content);
116 ErrPrint("Invalid argument\n");
120 handler = lb_find_livebox(pkgname, id);
122 ErrPrint("Instance (%s) is not exists\n", id);
127 new_content = strdup(content);
129 free(handler->content);
130 handler->content = new_content;
131 handler->is_pinned_up = pinup;
133 ErrPrint("Heap: %s\n", strerror(errno));
134 status = LB_STATUS_ERROR_MEMORY;
138 if (handler->pinup_cb) {
142 /* Make sure that user can call pinup API in its result callback */
143 cb = handler->pinup_cb;
144 cbdata = handler->pinup_cbdata;
146 handler->pinup_cb = NULL;
147 handler->pinup_cbdata = NULL;
149 cb(handler, status, cbdata);
150 } else if (status == 0) {
151 lb_invoke_event_handler(handler, LB_EVENT_PINUP_CHANGED);
158 static struct packet *master_deleted(pid_t pid, int handle, const struct packet *packet)
163 struct livebox *handler;
166 if (packet_get(packet, "ssdi", &pkgname, &id, ×tamp, &reason) != 4) {
167 ErrPrint("Invalid arguemnt\n");
171 handler = lb_find_livebox_by_timestamp(timestamp);
175 * This can be happens only if the user delete a livebox
176 * right after create it before receive created event.
181 /*!< Check validity of this "handler" */
182 if (handler->state != CREATE) {
183 if (handler->state != DELETE) {
186 * This is not possible
188 ErrPrint("Already deleted handler (%s - %s)\n", pkgname, id);
193 if (handler->created_cb) {
199 * "if (handler->id == NULL) {"
201 * The instance is not created yet.
202 * But the master forcely destroy it and send destroyed event to this
203 * without the created event.
205 * It could be destroyed when a slave has critical error(fault)
206 * before creating an instance successfully.
208 if (handler->created_cb == handler->deleted_cb) {
209 if (handler->created_cbdata != handler->deleted_cbdata) {
210 DbgPrint("cb is same but cbdata is different (%s - %s)\n", pkgname, id);
213 handler->deleted_cb = NULL;
214 handler->deleted_cbdata = NULL;
217 cb = handler->created_cb;
218 cbdata = handler->created_cbdata;
220 handler->created_cb = NULL;
221 handler->created_cbdata = NULL;
223 if (reason == LB_STATUS_SUCCESS) {
224 reason = LB_STATUS_ERROR_CANCEL;
226 cb(handler, reason, cbdata);
227 } else if (handler->id) {
228 if (handler->deleted_cb) {
232 cb = handler->deleted_cb;
233 cbdata = handler->deleted_cbdata;
235 handler->deleted_cb = NULL;
236 handler->deleted_cbdata = NULL;
238 cb(handler, reason, cbdata);
240 lb_invoke_event_handler(handler, LB_EVENT_DELETED);
246 * Lock file should be deleted after all callbacks are processed.
248 lb_destroy_lock_file(handler, 0);
250 /* Just try to delete it, if a user didn't remove it from the live box list */
257 static struct packet *master_lb_update_begin(pid_t pid, int handle, const struct packet *packet)
259 struct livebox *handler;
268 ret = packet_get(packet, "ssdsss", &pkgname, &id, &priority, &content, &title, &fbfile);
270 ErrPrint("Invalid argument\n");
274 handler = lb_find_livebox(pkgname, id);
276 ErrPrint("Instance[%s] is not exists\n", id);
280 if (handler->state != CREATE) {
281 ErrPrint("(%s) is not created\n", id);
285 lb_set_priority(handler, priority);
286 lb_set_content(handler, content);
287 lb_set_title(handler, title);
291 * Width & Height is not changed in this case.
292 * If the active update is began, the size should not be changed,
293 * And if the size is changed, the provider should finish the updating first.
294 * And then begin updating again after change its size.
296 if (lb_get_lb_fb(handler)) {
297 (void)lb_set_lb_fb(handler, fbfile);
299 ret = livebox_sync_lb_fb(handler);
300 if (ret != LB_STATUS_SUCCESS) {
301 ErrPrint("Failed to do sync FB (%s - %s) (%d)\n", pkgname, fbfile, ret);
303 lb_invoke_event_handler(handler, LB_EVENT_LB_UPDATE_BEGIN);
306 ErrPrint("Invalid request[%s], %s\n", id, fbfile);
313 static struct packet *master_pd_update_begin(pid_t pid, int handle, const struct packet *packet)
315 struct livebox *handler;
321 ret = packet_get(packet, "sss", &pkgname, &id, &fbfile);
323 ErrPrint("Invalid argument\n");
327 handler = lb_find_livebox(pkgname, id);
329 ErrPrint("Instance[%s] is not exists\n", id);
333 if (handler->state != CREATE) {
334 ErrPrint("[%s] is not created\n", id);
338 if (lb_get_pd_fb(handler)) {
339 (void)lb_set_lb_fb(handler, fbfile);
341 ret = livebox_sync_lb_fb(handler);
342 if (ret != LB_STATUS_SUCCESS) {
343 ErrPrint("Failed to do sync FB (%s - %s) (%d)\n", pkgname, fbfile, ret);
345 lb_invoke_event_handler(handler, LB_EVENT_PD_UPDATE_BEGIN);
348 ErrPrint("Invalid request[%s], %s\n", id, fbfile);
355 static struct packet *master_lb_update_end(pid_t pid, int handle, const struct packet *packet)
357 struct livebox *handler;
362 ret = packet_get(packet, "ss", &pkgname, &id);
364 ErrPrint("Invalid argument\n");
368 handler = lb_find_livebox(pkgname, id);
370 ErrPrint("Instance[%s] is not exists\n", id);
374 if (handler->state != CREATE) {
375 ErrPrint("[%s] is not created\n", id);
379 if (lb_get_lb_fb(handler)) {
380 lb_invoke_event_handler(handler, LB_EVENT_LB_UPDATE_END);
382 ErrPrint("Invalid request[%s]\n", id);
389 static struct packet *master_key_status(pid_t pid, int handle, const struct packet *packet)
391 struct livebox *handler;
397 ret = packet_get(packet, "ssi", &pkgname, &id, &status);
399 ErrPrint("Invalid argument\n");
403 handler = lb_find_livebox(pkgname, id);
405 ErrPrint("Instance[%s] is not exists\n", id);
409 if (handler->state != CREATE) {
410 ErrPrint("[%s] is not created\n", id);
414 if (handler->key_event_cb) {
418 cb = handler->key_event_cb;
419 cbdata = handler->key_event_cbdata;
421 handler->key_event_cb = NULL;
422 handler->key_event_cbdata = NULL;
424 cb(handler, status, cbdata);
426 ErrPrint("Invalid event[%s]\n", id);
432 static struct packet *master_request_close_pd(pid_t pid, int handle, const struct packet *packet)
434 struct livebox *handler;
440 ret = packet_get(packet, "ssi", &pkgname, &id, &reason);
442 ErrPrint("Invalid argument\n");
446 handler = lb_find_livebox(pkgname, id);
448 ErrPrint("Instance[%s] is not exists\n", id);
452 if (handler->state != CREATE) {
453 ErrPrint("[%s] is not created\n", id);
457 DbgPrint("Reason: %d\n", reason);
458 lb_invoke_event_handler(handler, LB_EVENT_REQUEST_CLOSE_PD);
463 static struct packet *master_access_status(pid_t pid, int handle, const struct packet *packet)
465 struct livebox *handler;
471 ret = packet_get(packet, "ssi", &pkgname, &id, &status);
473 ErrPrint("Invalid argument\n");
477 handler = lb_find_livebox(pkgname, id);
479 ErrPrint("Instance[%s] is not exists\n", id);
483 if (handler->state != CREATE) {
484 ErrPrint("[%s] is not created\n", id);
488 if (handler->access_event_cb) {
492 cb = handler->access_event_cb;
493 cbdata = handler->access_event_cbdata;
495 handler->access_event_cb = NULL;
496 handler->access_event_cbdata = NULL;
498 cb(handler, status, cbdata);
500 ErrPrint("Invalid event[%s]\n", id);
506 static struct packet *master_pd_update_end(pid_t pid, int handle, const struct packet *packet)
508 struct livebox *handler;
513 ret = packet_get(packet, "ss", &pkgname, &id);
515 ErrPrint("Invalid argument\n");
519 handler = lb_find_livebox(pkgname, id);
521 ErrPrint("Instance[%s] is not exists\n", id);
525 if (handler->state != CREATE) {
526 ErrPrint("[%s] is not created\n", id);
530 if (lb_get_lb_fb(handler)) {
531 lb_invoke_event_handler(handler, LB_EVENT_PD_UPDATE_END);
533 ErrPrint("Invalid request[%s]", id);
540 static struct packet *master_lb_updated(pid_t pid, int handle, const struct packet *packet)
547 const char *safe_file;
550 struct livebox *handler;
556 ret = packet_get(packet, "sssiidsssss",
558 &fbfile, &lb_w, &lb_h,
559 &priority, &content, &title,
560 &safe_file, &icon, &name);
562 ErrPrint("Invalid argument\n");
566 handler = lb_find_livebox(pkgname, id);
568 ErrPrint("instance(%s) is not exists\n", id);
572 if (handler->state != CREATE) {
575 * Already deleted by the user.
576 * Don't try to notice anything with this, Just ignore all events
577 * Beacuse the user doesn't wants know about this anymore
579 ErrPrint("(%s) is not exists, but updated\n", id);
583 lb_set_priority(handler, priority);
584 lb_set_content(handler, content);
585 lb_set_title(handler, title);
586 lb_set_size(handler, lb_w, lb_h);
587 lb_set_filename(handler, safe_file);
589 if (lb_text_lb(handler)) {
590 (void)parse_desc(handler, livebox_filename(handler), 0);
593 * DESC parser will call the "text event callback".
594 * Don't need to call global event callback in this case.
597 } else if (lb_get_lb_fb(handler)) {
598 if (conf_frame_drop_for_resizing() && handler->size_changed_cb) {
599 /* Just for skipping the update event callback call, After request to resize buffer, update event will be discarded */
600 DbgPrint("Discards obsoloted update event\n");
601 ret = LB_STATUS_ERROR_BUSY;
603 (void)lb_set_lb_fb(handler, fbfile);
605 if (!conf_manual_sync()) {
606 ret = livebox_sync_lb_fb(handler);
607 if (ret != LB_STATUS_SUCCESS) {
608 ErrPrint("Failed to do sync FB (%s - %s) (%d)\n", pkgname, util_basename(util_uri_to_path(id)), ret);
611 ret = LB_STATUS_SUCCESS;
615 ret = LB_STATUS_SUCCESS;
618 if (ret == LB_STATUS_SUCCESS) {
619 lb_invoke_event_handler(handler, LB_EVENT_LB_UPDATED);
626 static struct packet *master_pd_created(pid_t pid, int handle, const struct packet *packet)
628 struct livebox *handler;
637 ret = packet_get(packet, "sssiii", &pkgname, &id, &buf_id, &width, &height, &status);
639 ErrPrint("Invalid argument\n");
643 handler = lb_find_livebox(pkgname, id);
645 ErrPrint("Instance(%s) is not exists\n", id);
649 if (handler->state != CREATE) {
650 ErrPrint("Instance(%s) is not created\n", id);
654 lb_set_pdsize(handler, width, height);
655 if (lb_text_pd(handler)) {
656 DbgPrint("Text TYPE does not need to handle this\n");
658 (void)lb_set_pd_fb(handler, buf_id);
659 ret = livebox_sync_pd_fb(handler);
661 ErrPrint("Failed to do sync FB (%s - %s)\n", pkgname, util_basename(util_uri_to_path(id)));
665 handler->is_pd_created = (status == 0);
667 switch (handler->pd.type) {
668 case _PD_TYPE_SCRIPT:
669 case _PD_TYPE_BUFFER:
670 switch (fb_type(lb_get_pd_fb(handler))) {
671 case BUFFER_TYPE_FILE:
672 case BUFFER_TYPE_SHM:
673 lb_create_lock_file(handler, 1);
675 case BUFFER_TYPE_PIXMAP:
676 case BUFFER_TYPE_ERROR:
686 if (handler->pd_created_cb) {
690 cb = handler->pd_created_cb;
691 cbdata = handler->pd_created_cbdata;
693 handler->pd_created_cb = NULL;
694 handler->pd_created_cbdata = NULL;
697 * Before call the Callback function,
698 * pd_create_cb must be reset.
699 * Because, in the create callback, user can call create_pd function again.
701 DbgPrint("PERF_DBOX\n");
702 cb(handler, status, cbdata);
703 } else if (handler->is_pd_created) {
704 lb_invoke_event_handler(handler, LB_EVENT_PD_CREATED);
711 static struct packet *master_pd_destroyed(pid_t pid, int handle, const struct packet *packet)
713 struct livebox *handler;
719 ret = packet_get(packet, "ssi", &pkgname, &id, &status);
721 ErrPrint("Invalid argument\n");
725 handler = lb_find_livebox(pkgname, id);
727 ErrPrint("Instance(%s) is not exists\n", id);
731 if (handler->state != CREATE) {
732 ErrPrint("Instance(%s) is not created\n", id);
736 handler->is_pd_created = 0;
738 if (handler->pd_destroyed_cb) {
742 cb = handler->pd_destroyed_cb;
743 cbdata = handler->pd_destroyed_cbdata;
745 handler->pd_destroyed_cb = NULL;
746 handler->pd_destroyed_cbdata = NULL;
749 * Before call the Callback function,
750 * pd_destroyed_cb must be reset.
751 * Because, in the create callback, user can call destroy_pd function again.
753 cb(handler, status, cbdata);
754 } else if (status == 0) {
755 lb_invoke_event_handler(handler, LB_EVENT_PD_DESTROYED);
760 * Lock file should be deleted after all callbacks are processed.
762 switch (handler->pd.type) {
763 case _PD_TYPE_SCRIPT:
764 case _PD_TYPE_BUFFER:
765 switch (fb_type(lb_get_pd_fb(handler))) {
766 case BUFFER_TYPE_FILE:
767 case BUFFER_TYPE_SHM:
768 lb_destroy_lock_file(handler, 1);
770 case BUFFER_TYPE_PIXMAP:
771 case BUFFER_TYPE_ERROR:
785 static struct packet *master_pd_updated(pid_t pid, int handle, const struct packet *packet)
789 const char *descfile;
792 struct livebox *handler;
796 ret = packet_get(packet, "ssssii",
801 ErrPrint("Invalid argument\n");
805 handler = lb_find_livebox(pkgname, id);
807 ErrPrint("Instance(%s) is not exists\n", id);
811 if (handler->state != CREATE) {
814 * This handler is already deleted by the user.
815 * So don't try to notice anything about this anymore.
816 * Just ignore all events.
818 ErrPrint("Instance(%s) is not created\n", id);
822 lb_set_pdsize(handler, pd_w, pd_h);
824 if (lb_text_pd(handler)) {
825 (void)parse_desc(handler, descfile, 1);
827 if (conf_frame_drop_for_resizing() && handler->size_changed_cb) {
828 /* Just for skipping the update event callback call, After request to resize buffer, update event will be discarded */
829 DbgPrint("Discards obsoloted update event\n");
831 (void)lb_set_pd_fb(handler, fbfile);
833 if (!conf_manual_sync()) {
834 ret = livebox_sync_pd_fb(handler);
836 ErrPrint("Failed to do sync FB (%s - %s), %d\n", pkgname, util_basename(util_uri_to_path(id)), ret);
838 lb_invoke_event_handler(handler, LB_EVENT_PD_UPDATED);
841 lb_invoke_event_handler(handler, LB_EVENT_PD_UPDATED);
850 static struct packet *master_update_mode(pid_t pid, int handle, const struct packet *packet)
852 struct livebox *handler;
860 ErrPrint("Invalid packet\n");
864 ret = packet_get(packet, "ssii", &pkgname, &id, &status, &active_mode);
866 ErrPrint("Invalid argument\n");
870 handler = lb_find_livebox(pkgname, id);
872 ErrPrint("Livebox(%s) is not found\n", id);
876 if (handler->state != CREATE) {
877 ErrPrint("Livebox(%s) is not created yet\n", id);
881 if (status == LB_STATUS_SUCCESS) {
882 lb_set_update_mode(handler, active_mode);
885 if (handler->update_mode_cb) {
889 cb = handler->update_mode_cb;
890 cbdata = handler->update_mode_cbdata;
892 handler->update_mode_cb = NULL;
893 handler->update_mode_cbdata = NULL;
895 cb(handler, status, cbdata);
897 lb_invoke_event_handler(handler, LB_EVENT_UPDATE_MODE_CHANGED);
904 static struct packet *master_size_changed(pid_t pid, int handle, const struct packet *packet)
906 struct livebox *handler;
917 ErrPrint("Invalid packet\n");
921 ret = packet_get(packet, "sssiiii", &pkgname, &id, &fbfile, &is_pd, &w, &h, &status);
923 ErrPrint("Invalid argument\n");
927 handler = lb_find_livebox(pkgname, id);
929 ErrPrint("Livebox(%s) is not found\n", id);
933 if (handler->state != CREATE) {
934 ErrPrint("Livebox(%s) is not created yet\n", id);
941 * PD is not able to resized by the client.
942 * PD is only can be managed by the provider.
943 * So the PD has no private resized event handler.
944 * Notify it via global event handler only.
947 lb_set_pdsize(handler, w, h);
948 lb_invoke_event_handler(handler, LB_EVENT_PD_SIZE_CHANGED);
950 ErrPrint("This is not possible. PD Size is changed but the return value is not ZERO (%d)\n", status);
954 lb_set_size(handler, w, h);
958 * If there is a created LB FB,
961 if (lb_get_lb_fb(handler)) {
962 (void)lb_set_lb_fb(handler, fbfile);
964 ret = livebox_sync_lb_fb(handler);
966 ErrPrint("Failed to do sync FB (%s - %s)\n", pkgname, util_basename(util_uri_to_path(id)));
969 /* Just update the size info only. */
974 * I cannot believe client.
975 * So I added some log before & after call the user callback.
977 if (handler->size_changed_cb) {
981 cb = handler->size_changed_cb;
982 cbdata = handler->size_cbdata;
984 handler->size_changed_cb = NULL;
985 handler->size_cbdata = NULL;
987 cb(handler, status, cbdata);
989 lb_invoke_event_handler(handler, LB_EVENT_LB_SIZE_CHANGED);
992 if (handler->size_changed_cb) {
996 cb = handler->size_changed_cb;
997 cbdata = handler->size_cbdata;
999 handler->size_changed_cb = NULL;
1000 handler->size_cbdata = NULL;
1002 cb(handler, status, cbdata);
1011 static struct packet *master_period_changed(pid_t pid, int handle, const struct packet *packet)
1013 struct livebox *handler;
1014 const char *pkgname;
1020 ret = packet_get(packet, "idss", &status, &period, &pkgname, &id);
1022 ErrPrint("Invalid argument\n");
1026 handler = lb_find_livebox(pkgname, id);
1028 ErrPrint("Livebox(%s) is not found\n", id);
1032 if (handler->state != CREATE) {
1033 ErrPrint("Livebox(%s) is not created\n", id);
1038 lb_set_period(handler, period);
1041 if (handler->period_changed_cb) {
1045 cb = handler->period_changed_cb;
1046 cbdata = handler->period_cbdata;
1048 handler->period_changed_cb = NULL;
1049 handler->period_cbdata = NULL;
1051 cb(handler, status, cbdata);
1052 } else if (status == 0) {
1053 lb_invoke_event_handler(handler, LB_EVENT_PERIOD_CHANGED);
1060 static struct packet *master_group_changed(pid_t pid, int handle, const struct packet *packet)
1062 struct livebox *handler;
1063 const char *pkgname;
1066 const char *cluster;
1067 const char *category;
1070 ret = packet_get(packet, "ssiss", &pkgname, &id, &status, &cluster, &category);
1072 ErrPrint("Invalid argument\n");
1076 handler = lb_find_livebox(pkgname, id);
1078 ErrPrint("Livebox(%s) is not exists\n", id);
1082 if (handler->state != CREATE) {
1085 * Do no access this handler,
1086 * You cannot believe this handler anymore.
1088 ErrPrint("Livebox(%s) is not created\n", id);
1093 (void)lb_set_group(handler, cluster, category);
1096 if (handler->group_changed_cb) {
1100 cb = handler->group_changed_cb;
1101 cbdata = handler->group_cbdata;
1103 handler->group_changed_cb = NULL;
1104 handler->group_cbdata = NULL;
1106 cb(handler, status, cbdata);
1107 } else if (status == 0) {
1108 lb_invoke_event_handler(handler, LB_EVENT_GROUP_CHANGED);
1115 static struct packet *master_created(pid_t pid, int handle, const struct packet *packet)
1117 struct livebox *handler;
1123 const char *pkgname;
1126 const char *content;
1127 const char *cluster;
1128 const char *category;
1129 const char *lb_fname;
1130 const char *pd_fname;
1134 const char *auto_launch;
1138 int pinup_supported;
1139 enum lb_type lb_type;
1140 enum pd_type pd_type;
1144 int old_state = DESTROYED;
1148 ret = packet_get(packet, "dsssiiiisssssdiiiiidsi",
1150 &pkgname, &id, &content,
1151 &lb_w, &lb_h, &pd_w, &pd_h,
1152 &cluster, &category, &lb_fname, &pd_fname,
1153 &auto_launch, &priority, &size_list, &user, &pinup_supported,
1154 &lb_type, &pd_type, &period, &title, &is_pinned_up);
1156 ErrPrint("Invalid argument\n");
1157 ret = LB_STATUS_ERROR_INVALID;
1161 ErrPrint("[%lf] pkgname: %s, id: %s, content: %s, "
1162 "pd_w: %d, pd_h: %d, lb_w: %d, lb_h: %d, "
1163 "cluster: %s, category: %s, lb_fname: \"%s\", pd_fname: \"%s\", "
1164 "auto_launch: %s, priority: %lf, size_list: %d, user: %d, pinup: %d, "
1165 "lb_type: %d, pd_type: %d, period: %lf, title: [%s], is_pinned_up: %d\n",
1166 timestamp, pkgname, id, content,
1167 pd_w, pd_h, lb_w, lb_h,
1168 cluster, category, lb_fname, pd_fname,
1169 auto_launch, priority, size_list, user, pinup_supported,
1170 lb_type, pd_type, period, title, is_pinned_up);
1172 handler = lb_find_livebox_by_timestamp(timestamp);
1174 handler = lb_new_livebox(pkgname, id, timestamp);
1176 ErrPrint("Failed to create a new livebox\n");
1177 ret = LB_STATUS_ERROR_FAULT;
1181 old_state = handler->state;
1183 if (handler->state != CREATE) {
1184 if (handler->state != DELETE) {
1187 * This is not possible!!!
1189 ErrPrint("Invalid handler\n");
1190 ret = LB_STATUS_ERROR_INVALID;
1196 * After get the delete states,
1197 * call the create callback with deleted result.
1201 old_state = handler->state;
1204 ErrPrint("Already created: timestamp[%lf] "
1205 "pkgname[%s], id[%s] content[%s] "
1206 "cluster[%s] category[%s] lb_fname[%s] pd_fname[%s]\n",
1207 timestamp, pkgname, id,
1208 content, cluster, category,
1209 lb_fname, pd_fname);
1211 ret = LB_STATUS_ERROR_ALREADY;
1215 lb_set_id(handler, id);
1218 lb_set_size(handler, lb_w, lb_h);
1219 handler->lb.type = lb_type;
1220 handler->is_pinned_up = is_pinned_up;
1225 case _LB_TYPE_SCRIPT:
1226 case _LB_TYPE_BUFFER:
1227 if (!strlen(lb_fname)) {
1230 (void)lb_set_lb_fb(handler, lb_fname);
1234 * Livebox should create the lock file from here.
1235 * Even if the old_state == DELETE,
1236 * the lock file will be deleted from deleted event callback.
1238 switch (fb_type(lb_get_lb_fb(handler))) {
1239 case BUFFER_TYPE_FILE:
1240 case BUFFER_TYPE_SHM:
1241 lb_create_lock_file(handler, 0);
1243 case BUFFER_TYPE_PIXMAP:
1244 case BUFFER_TYPE_ERROR:
1249 ret = livebox_sync_lb_fb(handler);
1251 ErrPrint("Failed to do sync FB (%s - %s)\n", pkgname, util_basename(util_uri_to_path(id)));
1255 lb_set_text_lb(handler);
1261 handler->pd.type = pd_type;
1262 lb_set_pdsize(handler, pd_w, pd_h);
1263 lb_set_default_pdsize(handler, pd_w, pd_h);
1265 case _PD_TYPE_SCRIPT:
1266 case _PD_TYPE_BUFFER:
1267 if (!strlen(pd_fname)) {
1271 lb_set_pd_fb(handler, pd_fname);
1272 ret = livebox_sync_pd_fb(handler);
1274 ErrPrint("Failed to do sync FB (%s - %s)\n", pkgname, util_basename(util_uri_to_path(id)));
1279 * PD doesn't need to create the lock file from here.
1280 * Just create it from PD_CREATED event.
1285 lb_set_text_pd(handler);
1291 lb_set_priority(handler, priority);
1293 lb_set_size_list(handler, size_list);
1294 lb_set_group(handler, cluster, category);
1296 lb_set_content(handler, content);
1297 lb_set_title(handler, title);
1299 lb_set_user(handler, user);
1301 lb_set_auto_launch(handler, auto_launch);
1302 lb_set_pinup(handler, pinup_supported);
1304 lb_set_period(handler, period);
1308 if (handler->state == CREATE) {
1311 * These callback can change the handler->state.
1312 * So we have to use the "old_state" which stored state before call these callbacks
1315 if (handler->created_cb) {
1319 cb = handler->created_cb;
1320 cbdata = handler->created_cbdata;
1322 handler->created_cb = NULL;
1323 handler->created_cbdata = NULL;
1325 cb(handler, ret, cbdata);
1327 lb_invoke_event_handler(handler, LB_EVENT_CREATED);
1332 if (ret == 0 && old_state == DELETE) {
1333 lb_send_delete(handler, handler->delete_type, handler->created_cb, handler->created_cbdata);
1337 * handler->created_cb = NULL;
1338 * handler->created_cbdata = NULL;
1340 * Do not clear this to use this from the deleted event callback.
1341 * if this value is not cleared when the deleted event callback check it,
1342 * it means that the created function is not called yet.
1343 * Then the call the deleted event callback with LB_STATUS_ERROR_CANCEL errno.
1350 static struct method s_table[] = {
1352 .cmd = "lb_updated", /* pkgname, id, lb_w, lb_h, priority, ret */
1353 .handler = master_lb_updated,
1356 .cmd = "pd_updated", /* pkgname, id, descfile, pd_w, pd_h, ret */
1357 .handler = master_pd_updated,
1360 .cmd = "pd_created",
1361 .handler = master_pd_created,
1364 .cmd = "pd_destroyed",
1365 .handler = master_pd_destroyed,
1368 .cmd = "fault_package", /* pkgname, id, function, ret */
1369 .handler = master_fault_package,
1372 .cmd = "deleted", /* pkgname, id, timestamp, ret */
1373 .handler = master_deleted,
1376 .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 */
1377 .handler = master_created,
1380 .cmd = "group_changed",
1381 .handler = master_group_changed,
1384 .cmd = "period_changed",
1385 .handler = master_period_changed,
1388 .cmd = "size_changed",
1389 .handler = master_size_changed,
1393 .handler = master_pinup,
1397 .handler = master_hold_scroll,
1401 .cmd = "update_mode",
1402 .handler = master_update_mode,
1406 .cmd = "lb_update_begin",
1407 .handler = master_lb_update_begin,
1410 .cmd = "lb_update_end",
1411 .handler = master_lb_update_end,
1415 .cmd = "pd_update_begin",
1416 .handler = master_pd_update_begin,
1419 .cmd = "pd_update_end",
1420 .handler = master_pd_update_end,
1424 .cmd = "access_status",
1425 .handler = master_access_status,
1428 .cmd = "key_status",
1429 .handler = master_key_status,
1433 .handler = master_request_close_pd,
1442 static void acquire_cb(struct livebox *handler, const struct packet *result, void *data)
1445 DbgPrint("Result packet is not valid\n");
1449 if (packet_get(result, "i", &ret) != 1) {
1450 ErrPrint("Invalid argument\n");
1452 DbgPrint("Acquire returns: %d\n", ret);
1459 static inline int make_connection(void)
1461 struct packet *packet;
1464 DbgPrint("Let's making connection!\n");
1466 s_info.fd = com_core_packet_client_init(client_addr(), 0, s_table);
1467 if (s_info.fd < 0) {
1468 ErrPrint("Try this again later\n");
1469 return LB_STATUS_ERROR_IO;
1472 packet = packet_create("acquire", "d", util_timestamp());
1474 com_core_packet_client_fini(s_info.fd);
1476 return LB_STATUS_ERROR_FAULT;
1479 ret = master_rpc_async_request(NULL, packet, 1, acquire_cb, NULL);
1481 ErrPrint("Master RPC returns %d\n", ret);
1482 com_core_packet_client_fini(s_info.fd);
1484 return LB_STATUS_ERROR_IO;
1487 return LB_STATUS_SUCCESS;
1490 static int connected_cb(int handle, void *data)
1492 master_rpc_check_and_fire_consumer();
1496 static void master_started_cb(keynode_t *node, void *data)
1500 if (vconf_get_bool(VCONFKEY_MASTER_STARTED, &state) < 0) {
1501 ErrPrint("Unable to get [%s]\n", VCONFKEY_MASTER_STARTED);
1504 DbgPrint("Master state: %d\n", state);
1505 if (state == 1 && make_connection() == LB_STATUS_SUCCESS) {
1507 ret = vconf_ignore_key_changed(VCONFKEY_MASTER_STARTED, master_started_cb);
1508 DbgPrint("master_started vconf key de-registered [%d]\n", ret);
1512 static gboolean timeout_cb(gpointer data)
1514 if (vconf_notify_key_changed(VCONFKEY_MASTER_STARTED, master_started_cb, NULL) < 0) {
1515 ErrPrint("Failed to add vconf for monitoring service state\n");
1517 DbgPrint("vconf event callback is registered\n");
1520 master_started_cb(NULL, NULL);
1522 s_info.timer_id = 0;
1526 static int disconnected_cb(int handle, void *data)
1528 if (s_info.fd != handle) {
1529 /*!< This handle is not my favor */
1533 s_info.fd = -1; /*!< Disconnected */
1535 master_rpc_clear_all_request();
1536 lb_invoke_fault_handler(LB_FAULT_PROVIDER_DISCONNECTED, MASTER_PKGNAME, "default", "disconnected");
1540 /* Try to reconnect after 1 sec later */
1541 if (!s_info.timer_id) {
1542 DbgPrint("Reconnecting timer is added\n");
1543 s_info.timer_id = g_timeout_add(1000, timeout_cb, NULL);
1544 if (s_info.timer_id == 0) {
1545 ErrPrint("Unable to add reconnecting timer\n");
1549 ErrPrint("Reconnecting timer is already exists\n");
1555 int client_init(int use_thread)
1557 com_core_packet_use_thread(use_thread);
1559 s_info.client_addr = vconf_get_str(VCONFKEY_MASTER_CLIENT_ADDR);
1560 if (!s_info.client_addr) {
1561 s_info.client_addr = strdup(CLIENT_SOCKET);
1562 if (!s_info.client_addr) {
1563 ErrPrint("Heap: %s\n", strerror(errno));
1568 (void)file_service_init();
1570 DbgPrint("Server Address: %s\n", s_info.client_addr);
1572 com_core_add_event_callback(CONNECTOR_DISCONNECTED, disconnected_cb, NULL);
1573 com_core_add_event_callback(CONNECTOR_CONNECTED, connected_cb, NULL);
1574 if (vconf_notify_key_changed(VCONFKEY_MASTER_STARTED, master_started_cb, NULL) < 0) {
1575 ErrPrint("Failed to add vconf for service state\n");
1577 DbgPrint("vconf event callback is registered\n");
1580 master_started_cb(NULL, NULL);
1589 const char *client_addr(void)
1591 return s_info.client_addr;
1594 int client_fini(void)
1598 (void)file_service_fini();
1600 ret = vconf_ignore_key_changed(VCONFKEY_MASTER_STARTED, master_started_cb);
1602 DbgPrint("Ignore vconf key: %d\n", ret);
1605 com_core_del_event_callback(CONNECTOR_DISCONNECTED, disconnected_cb, NULL);
1606 com_core_del_event_callback(CONNECTOR_CONNECTED, connected_cb, NULL);
1607 com_core_packet_client_fini(s_info.fd);
1609 free(s_info.client_addr);
1610 s_info.client_addr = NULL;
1611 return LB_STATUS_SUCCESS;