Add frame drop & manual sync mode.
[platform/framework/web/livebox-viewer.git] / src / client.c
1 /*
2  * Copyright 2013  Samsung Electronics Co., Ltd
3  *
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
7  *
8  * http://floralicense.org/license/
9  *
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.
15  */
16
17 #include <stdio.h>
18 #include <unistd.h>
19 #include <errno.h>
20 #include <string.h>
21 #include <stdlib.h>
22 #include <pthread.h>
23
24 #include <dlog.h>
25 #include <glib.h>
26
27 #include <vconf.h>
28 #include <vconf-keys.h>
29
30 #include <packet.h>
31 #include <com-core.h>
32 #include <com-core_packet.h>
33 #include <livebox-errno.h>
34 #include <secure_socket.h>
35
36 #include "debug.h"
37 #include "client.h"
38 #include "livebox.h"
39 #include "livebox_internal.h"
40 #include "desc_parser.h"
41 #include "fb.h"
42 #include "util.h"
43 #include "master_rpc.h"
44 #include "conf.h"
45 #include "critical_log.h"
46 #include "file_service.h"
47
48 int errno;
49
50 static struct info {
51         int fd;
52         guint timer_id;
53         char *client_addr;
54 } s_info = {
55         .fd = -1,
56         .timer_id = 0,
57         .client_addr = NULL,
58 };
59
60 static struct packet *master_fault_package(pid_t pid, int handle, const struct packet *packet)
61 {
62         const char *pkgname;
63         const char *id;
64         const char *function;
65
66         if (packet_get(packet, "sss", &pkgname, &id, &function) != 3) {
67                 ErrPrint("Invalid arguments\n");
68                 return NULL;
69         }
70
71         master_rpc_clear_fault_package(pkgname);
72         lb_invoke_fault_handler(LB_FAULT_DEACTIVATED, pkgname, id, function);
73         return NULL;
74 }
75
76 static struct packet *master_hold_scroll(pid_t pid, int handle, const struct packet *packet)
77 {
78         struct livebox *handler;
79         const char *pkgname;
80         const char *id;
81         int seize;
82         int ret;
83
84         ret = packet_get(packet, "ssi", &pkgname, &id, &seize);
85         if (ret != 3) {
86                 ErrPrint("Invalid argument\n");
87                 goto out;
88         }
89
90         handler = lb_find_livebox(pkgname, id);
91         if (!handler) {
92                 ErrPrint("Instance(%s) is not exists\n", id);
93                 goto out;
94         }
95
96         DbgPrint("HOLD: %s %d\n", id, seize);
97         lb_invoke_event_handler(handler, seize ? LB_EVENT_HOLD_SCROLL : LB_EVENT_RELEASE_SCROLL);
98
99 out:
100         return NULL;
101 }
102
103 static struct packet *master_pinup(pid_t pid, int handle, const struct packet *packet)
104 {
105         const char *pkgname;
106         const char *id;
107         const char *content;
108         struct livebox *handler;
109         char *new_content;
110         int ret;
111         int status;
112         int pinup;
113
114         ret = packet_get(packet, "iisss", &status, &pinup, &pkgname, &id, &content);
115         if (ret != 5) {
116                 ErrPrint("Invalid argument\n");
117                 goto out;
118         }
119
120         handler = lb_find_livebox(pkgname, id);
121         if (!handler) {
122                 ErrPrint("Instance (%s) is not exists\n", id);
123                 goto out;
124         }
125
126         if (status == 0) {
127                 new_content = strdup(content);
128                 if (new_content) {
129                         free(handler->content);
130                         handler->content = new_content;
131                         handler->is_pinned_up = pinup;
132                 } else {
133                         ErrPrint("Heap: %s\n", strerror(errno));
134                         status = LB_STATUS_ERROR_MEMORY;
135                 }
136         }
137
138         if (handler->pinup_cb) {
139                 ret_cb_t cb;
140                 void *cbdata;
141
142                 /* Make sure that user can call pinup API in its result callback */
143                 cb = handler->pinup_cb;
144                 cbdata = handler->pinup_cbdata;
145
146                 handler->pinup_cb = NULL;
147                 handler->pinup_cbdata = NULL;
148
149                 cb(handler, status, cbdata);
150         } else if (status == 0) {
151                 lb_invoke_event_handler(handler, LB_EVENT_PINUP_CHANGED);
152         }
153
154 out:
155         return NULL;
156 }
157
158 static struct packet *master_deleted(pid_t pid, int handle, const struct packet *packet)
159 {
160         const char *pkgname;
161         const char *id;
162         double timestamp;
163         struct livebox *handler;
164
165         if (packet_get(packet, "ssd", &pkgname, &id, &timestamp) != 3) {
166                 ErrPrint("Invalid arguemnt\n");
167                 goto out;
168         }
169
170         handler = lb_find_livebox_by_timestamp(timestamp);
171         if (!handler) {
172                 /*!
173                  * \note
174                  * This can be happens only if the user delete a livebox
175                  * right after create it before receive created event.
176                  */
177                 goto out;
178         }
179
180         /*!< Check validity of this "handler" */
181         if (handler->state != CREATE) {
182                 if (handler->state != DELETE) {
183                         /*!
184                          * \note
185                          * This is not possible
186                          */
187                         CRITICAL_LOG("Already deleted handler (%s - %s)\n", pkgname, id);
188                         return NULL;
189                 }
190         }
191
192         if (handler->created_cb) {
193                 ret_cb_t cb;
194                 void *cbdata;
195                 /*!
196                  * \note
197                  *
198                  * "if (handler->id == NULL) {"
199                  *
200                  * The instance is not created yet.
201                  * But the master forcely destroy it and send destroyed event to this
202                  * without the created event.
203                  *
204                  * It could be destroyed when a slave has critical error(fault)
205                  * before creating an instance successfully.
206                  */
207                 if (handler->created_cb == handler->deleted_cb) {
208                         if (handler->created_cbdata != handler->deleted_cbdata) {
209                                 DbgPrint("cb is same but cbdata is different (%s - %s)\n", pkgname, id);
210                         }
211
212                         handler->deleted_cb = NULL;
213                         handler->deleted_cbdata = NULL;
214                 }
215
216                 cb = handler->created_cb;
217                 cbdata = handler->created_cbdata;
218
219                 handler->created_cb = NULL;
220                 handler->created_cbdata = NULL;
221
222                 cb(handler, LB_STATUS_ERROR_CANCEL, cbdata);
223         } else if (handler->id) {
224                 if (handler->deleted_cb) {
225                         ret_cb_t cb;
226                         void *cbdata;
227
228                         cb = handler->deleted_cb;
229                         cbdata = handler->deleted_cbdata;
230
231                         handler->deleted_cb = NULL;
232                         handler->deleted_cbdata = NULL;
233
234                         cb(handler, LB_STATUS_SUCCESS, cbdata);
235                 } else {
236                         lb_invoke_event_handler(handler, LB_EVENT_DELETED);
237                 }
238         }
239
240         /* Just try to delete it, if a user didn't remove it from the live box list */
241         lb_unref(handler);
242
243 out:
244         return NULL;
245 }
246
247 static struct packet *master_lb_update_begin(pid_t pid, int handle, const struct packet *packet)
248 {
249         struct livebox *handler;
250         const char *pkgname;
251         const char *id;
252         const char *content;
253         const char *title;
254         const char *fbfile;
255         double priority;
256         int ret;
257
258         ret = packet_get(packet, "ssdsss", &pkgname, &id, &priority, &content, &title, &fbfile);
259         if (ret != 6) {
260                 ErrPrint("Invalid argument\n");
261                 goto out;
262         }
263
264         handler = lb_find_livebox(pkgname, id);
265         if (!handler) {
266                 ErrPrint("Instance[%s] is not exists\n", id);
267                 goto out;
268         }
269
270         if (handler->state != CREATE) {
271                 ErrPrint("(%s) is not created\n", id);
272                 goto out;
273         }
274
275         lb_set_priority(handler, priority);
276         lb_set_content(handler, content);
277         lb_set_title(handler, title);
278
279         /*!
280          * \NOTE
281          * Width & Height is not changed in this case.
282          * If the active update is began, the size should not be changed,
283          * And if the size is changed, the provider should finish the updating first.
284          * And then begin updating again after change its size.
285          */
286         if (lb_get_lb_fb(handler)) {
287                 (void)lb_set_lb_fb(handler, fbfile);
288
289                 ret = fb_sync(lb_get_lb_fb(handler));
290                 if (ret != LB_STATUS_SUCCESS) {
291                         ErrPrint("Failed to do sync FB (%s - %s) (%d)\n", pkgname, fbfile, ret);
292                 } else {
293                         lb_invoke_event_handler(handler, LB_EVENT_LB_UPDATE_BEGIN);
294                 }
295         } else {
296                 ErrPrint("Invalid request[%s], %s\n", id, fbfile);
297         }
298
299 out:
300         return NULL;
301 }
302
303 static struct packet *master_pd_update_begin(pid_t pid, int handle, const struct packet *packet)
304 {
305         struct livebox *handler;
306         const char *pkgname;
307         const char *id;
308         const char *fbfile;
309         int ret;
310
311         ret = packet_get(packet, "sss", &pkgname, &id, &fbfile);
312         if (ret != 2) {
313                 ErrPrint("Invalid argument\n");
314                 goto out;
315         }
316
317         handler = lb_find_livebox(pkgname, id);
318         if (!handler) {
319                 ErrPrint("Instance[%s] is not exists\n", id);
320                 goto out;
321         }
322
323         if (handler->state != CREATE) {
324                 ErrPrint("[%s] is not created\n", id);
325                 goto out;
326         }
327
328         if (lb_get_pd_fb(handler)) {
329                 (void)lb_set_lb_fb(handler, fbfile);
330
331                 ret = fb_sync(lb_get_lb_fb(handler));
332                 if (ret != LB_STATUS_SUCCESS) {
333                         ErrPrint("Failed to do sync FB (%s - %s) (%d)\n", pkgname, fbfile, ret);
334                 } else {
335                         lb_invoke_event_handler(handler, LB_EVENT_PD_UPDATE_BEGIN);
336                 }
337         } else {
338                 ErrPrint("Invalid request[%s], %s\n", id, fbfile);
339         }
340
341 out:
342         return NULL;
343 }
344
345 static struct packet *master_lb_update_end(pid_t pid, int handle, const struct packet *packet)
346 {
347         struct livebox *handler;
348         const char *pkgname;
349         const char *id;
350         int ret;
351
352         ret = packet_get(packet, "ss", &pkgname, &id);
353         if (ret != 2) {
354                 ErrPrint("Invalid argument\n");
355                 goto out;
356         }
357
358         handler = lb_find_livebox(pkgname, id);
359         if (!handler) {
360                 ErrPrint("Instance[%s] is not exists\n", id);
361                 goto out;
362         }
363
364         if (handler->state != CREATE) {
365                 ErrPrint("[%s] is not created\n", id);
366                 goto out;
367         }
368
369         if (lb_get_lb_fb(handler)) {
370                 lb_invoke_event_handler(handler, LB_EVENT_LB_UPDATE_END);
371         } else {
372                 ErrPrint("Invalid request[%s]\n", id);
373         }
374
375 out:
376         return NULL;
377 }
378
379 static struct packet *master_access_status(pid_t pid, int handle, const struct packet *packet)
380 {
381         struct livebox *handler;
382         const char *pkgname;
383         const char *id;
384         int ret;
385         int status;
386
387         ret = packet_get(packet, "ssi", &pkgname, &id, &status);
388         if (ret != 3) {
389                 ErrPrint("Invalid argument\n");
390                 goto out;
391         }
392
393         handler = lb_find_livebox(pkgname, id);
394         if (!handler) {
395                 ErrPrint("Instance[%s] is not exists\n", id);
396                 goto out;
397         }
398
399         if (handler->state != CREATE) {
400                 ErrPrint("[%s] is not created\n", id);
401                 goto out;
402         }
403
404         if (handler->access_event_cb) {
405                 ret_cb_t cb;
406                 void *cbdata;
407
408                 cb = handler->access_event_cb;
409                 cbdata = handler->access_event_cbdata;
410
411                 handler->access_event_cb = NULL;
412                 handler->access_event_cbdata = NULL;
413
414                 cb(handler, status, cbdata);
415         } else {
416                 ErrPrint("Invalid event[%s]\n", id);
417         }
418 out:
419         return NULL;
420 }
421
422 static struct packet *master_pd_update_end(pid_t pid, int handle, const struct packet *packet)
423 {
424         struct livebox *handler;
425         const char *pkgname;
426         const char *id;
427         int ret;
428
429         ret = packet_get(packet, "ss", &pkgname, &id);
430         if (ret != 2) {
431                 ErrPrint("Invalid argument\n");
432                 goto out;
433         }
434
435         handler = lb_find_livebox(pkgname, id);
436         if (!handler) {
437                 ErrPrint("Instance[%s] is not exists\n", id);
438                 goto out;
439         }
440
441         if (handler->state != CREATE) {
442                 ErrPrint("[%s] is not created\n", id);
443                 goto out;
444         }
445
446         if (lb_get_lb_fb(handler)) {
447                 lb_invoke_event_handler(handler, LB_EVENT_PD_UPDATE_END);
448         } else {
449                 ErrPrint("Invalid request[%s]", id);
450         }
451
452 out:
453         return NULL;
454 }
455
456 static struct packet *master_lb_updated(pid_t pid, int handle, const struct packet *packet)
457 {
458         const char *pkgname;
459         const char *id;
460         const char *fbfile;
461         const char *content;
462         const char *title;
463         const char *safe_file;
464         struct livebox *handler;
465         int lb_w;
466         int lb_h;
467         double priority;
468         int ret;
469
470         ret = packet_get(packet, "sssiidsss",
471                                 &pkgname, &id,
472                                 &fbfile, &lb_w, &lb_h,
473                                 &priority, &content, &title,
474                                 &safe_file);
475         if (ret != 9) {
476                 ErrPrint("Invalid argument\n");
477                 goto out;
478         }
479
480         handler = lb_find_livebox(pkgname, id);
481         if (!handler) {
482                 ErrPrint("instance(%s) is not exists\n", id);
483                 goto out;
484         }
485
486         if (handler->state != CREATE) {
487                 /*!
488                  * \note
489                  * Already deleted by the user.
490                  * Don't try to notice anything with this, Just ignore all events
491                  * Beacuse the user doesn't wants know about this anymore
492                  */
493                 ErrPrint("(%s) is not exists, but updated\n", id);
494                 goto out;
495         }
496
497         lb_set_priority(handler, priority);
498         lb_set_content(handler, content);
499         lb_set_title(handler, title);
500         lb_set_size(handler, lb_w, lb_h);
501         lb_set_filename(handler, safe_file);
502
503         if (lb_text_lb(handler)) {
504                 (void)parse_desc(handler, livebox_filename(handler), 0);
505                 /*!
506                  * \note
507                  * DESC parser will call the "text event callback".
508                  * Don't need to call global event callback in this case.
509                  */
510                 goto out;
511         } else if (lb_get_lb_fb(handler)) {
512                 if (conf_frame_drop_for_resizing() && handler->size_changed_cb) {
513                         /* Just for skipping the update event callback call, After request to resize buffer, update event will be discarded */
514                         DbgPrint("Discards obsoloted update event\n");
515                         ret = LB_STATUS_ERROR_BUSY;
516                 } else {
517                         (void)lb_set_lb_fb(handler, fbfile);
518
519                         if (!conf_manual_sync()) {
520                                 ret = fb_sync(lb_get_lb_fb(handler));
521                                 if (ret != LB_STATUS_SUCCESS) {
522                                         ErrPrint("Failed to do sync FB (%s - %s) (%d)\n", pkgname, util_basename(util_uri_to_path(id)), ret);
523                                 }
524                         } else {
525                                 ret = LB_STATUS_SUCCESS;
526                         }
527                 }
528         } else {
529                 ret = LB_STATUS_SUCCESS;
530         }
531
532         if (ret == LB_STATUS_SUCCESS) {
533                 lb_invoke_event_handler(handler, LB_EVENT_LB_UPDATED);
534         }
535
536 out:
537         return NULL;
538 }
539
540 static struct packet *master_pd_created(pid_t pid, int handle, const struct packet *packet)
541 {
542         struct livebox *handler;
543         const char *pkgname;
544         const char *id;
545         const char *buf_id;
546         int width;
547         int height;
548         int ret;
549         int status;
550
551         ret = packet_get(packet, "sssiii", &pkgname, &id, &buf_id, &width, &height, &status);
552         if (ret != 6) {
553                 ErrPrint("Invalid argument\n");
554                 goto out;
555         }
556
557         handler = lb_find_livebox(pkgname, id);
558         if (!handler) {
559                 ErrPrint("Instance(%s) is not exists\n", id);
560                 goto out;
561         }
562
563         if (handler->state != CREATE) {
564                 ErrPrint("Instance(%s) is not created\n", id);
565                 goto out;
566         }
567
568         lb_set_pdsize(handler, width, height);
569         if (lb_text_pd(handler)) {
570                 DbgPrint("Text TYPE does not need to handle this\n");
571         } else {
572                 (void)lb_set_pd_fb(handler, buf_id);
573                 ret = fb_sync(lb_get_pd_fb(handler));
574                 if (ret < 0) {
575                         ErrPrint("Failed to do sync FB (%s - %s)\n", pkgname, util_basename(util_uri_to_path(id)));
576                 }
577         }
578
579         handler->is_pd_created = (status == 0);
580
581         if (handler->pd_created_cb) {
582                 ret_cb_t cb;
583                 void *cbdata;
584
585                 cb = handler->pd_created_cb;
586                 cbdata = handler->pd_created_cbdata;
587
588                 handler->pd_created_cb = NULL;
589                 handler->pd_created_cbdata = NULL;
590
591                 /*!
592                  * Before call the Callback function,
593                  * pd_create_cb must be reset.
594                  * Because, in the create callback, user can call create_pd function again.
595                  */
596                 DbgPrint("PERF_DBOX\n");
597                 cb(handler, status, cbdata);
598         } else if (handler->is_pd_created) {
599                 lb_invoke_event_handler(handler, LB_EVENT_PD_CREATED);
600         }
601
602 out:
603         return NULL;
604 }
605
606 static struct packet *master_pd_destroyed(pid_t pid, int handle, const struct packet *packet)
607 {
608         struct livebox *handler;
609         const char *pkgname;
610         const char *id;
611         int ret;
612         int status;
613
614         ret = packet_get(packet, "ssi", &pkgname, &id, &status);
615         if (ret != 3) {
616                 ErrPrint("Invalid argument\n");
617                 goto out;
618         }
619
620         handler = lb_find_livebox(pkgname, id);
621         if (!handler) {
622                 ErrPrint("Instance(%s) is not exists\n", id);
623                 goto out;
624         }
625
626         if (handler->state != CREATE) {
627                 ErrPrint("Instance(%s) is not created\n", id);
628                 goto out;
629         }
630
631         handler->is_pd_created = 0;
632
633         if (handler->pd_destroyed_cb) {
634                 ret_cb_t cb;
635                 void *cbdata;
636
637                 cb = handler->pd_destroyed_cb;
638                 cbdata = handler->pd_destroyed_cbdata;
639
640                 handler->pd_destroyed_cb = NULL;
641                 handler->pd_destroyed_cbdata = NULL;
642
643                 /*!
644                  * Before call the Callback function,
645                  * pd_destroyed_cb must be reset.
646                  * Because, in the create callback, user can call destroy_pd function again.
647                  */
648                 cb(handler, status, cbdata);
649         } else if (status == 0) {
650                 lb_invoke_event_handler(handler, LB_EVENT_PD_DESTROYED);
651         }
652
653 out:
654         return NULL;
655 }
656
657 static struct packet *master_pd_updated(pid_t pid, int handle, const struct packet *packet)
658 {
659         const char *pkgname;
660         const char *id;
661         const char *descfile;
662         const char *fbfile;
663         int ret;
664         struct livebox *handler;
665         int pd_w;
666         int pd_h;
667
668         ret = packet_get(packet, "ssssii",
669                                 &pkgname, &id,
670                                 &descfile, &fbfile,
671                                 &pd_w, &pd_h);
672         if (ret != 6) {
673                 ErrPrint("Invalid argument\n");
674                 goto out;
675         }
676
677         handler = lb_find_livebox(pkgname, id);
678         if (!handler) {
679                 ErrPrint("Instance(%s) is not exists\n", id);
680                 goto out;
681         }
682
683         if (handler->state != CREATE) {
684                 /*!
685                  * \note
686                  * This handler is already deleted by the user.
687                  * So don't try to notice anything about this anymore.
688                  * Just ignore all events.
689                  */
690                 ErrPrint("Instance(%s) is not created\n", id);
691                 goto out;
692         }
693
694         lb_set_pdsize(handler, pd_w, pd_h);
695
696         if (lb_text_pd(handler)) {
697                 (void)parse_desc(handler, descfile, 1);
698         } else {
699                 if (conf_frame_drop_for_resizing() && handler->size_changed_cb) {
700                         /* Just for skipping the update event callback call, After request to resize buffer, update event will be discarded */
701                         DbgPrint("Discards obsoloted update event\n");
702                 } else {
703                         (void)lb_set_pd_fb(handler, fbfile);
704
705                          if (!conf_manual_sync()) {
706                                  ret = fb_sync(lb_get_pd_fb(handler));
707                                  if (ret < 0) {
708                                          ErrPrint("Failed to do sync FB (%s - %s), %d\n", pkgname, util_basename(util_uri_to_path(id)), ret);
709                                  } else {
710                                          lb_invoke_event_handler(handler, LB_EVENT_PD_UPDATED);
711                                  }
712                         }
713                 }
714         }
715
716 out:
717         return NULL;
718 }
719
720 static struct packet *master_update_mode(pid_t pid, int handle, const struct packet *packet)
721 {
722         struct livebox *handler;
723         const char *pkgname;
724         const char *id;
725         int active_mode;
726         int status;
727         int ret;
728
729         if (!packet) {
730                 ErrPrint("Invalid packet\n");
731                 goto out;
732         }
733
734         ret = packet_get(packet, "ssii", &pkgname, &id, &status, &active_mode);
735         if (ret != 4) {
736                 ErrPrint("Invalid argument\n");
737                 goto out;
738         }
739
740         handler = lb_find_livebox(pkgname, id);
741         if (!handler) {
742                 ErrPrint("Livebox(%s) is not found\n", id);
743                 goto out;
744         }
745
746         if (handler->state != CREATE) {
747                 ErrPrint("Livebox(%s) is not created yet\n", id);
748                 goto out;
749         }
750
751         if (status == LB_STATUS_SUCCESS) {
752                 lb_set_update_mode(handler, active_mode);
753         }
754
755         if (handler->update_mode_cb) {
756                 ret_cb_t cb;
757                 void *cbdata;
758
759                 cb = handler->update_mode_cb;
760                 cbdata = handler->update_mode_cbdata;
761
762                 handler->update_mode_cb = NULL;
763                 handler->update_mode_cbdata = NULL;
764
765                 cb(handler, status, cbdata);
766         } else {
767                 lb_invoke_event_handler(handler, LB_EVENT_UPDATE_MODE_CHANGED);
768         }
769
770 out:
771         return NULL;
772 }
773
774 static struct packet *master_size_changed(pid_t pid, int handle, const struct packet *packet)
775 {
776         struct livebox *handler;
777         const char *pkgname;
778         const char *id;
779         const char *fbfile;
780         int status;
781         int ret;
782         int w;
783         int h;
784         int is_pd;
785
786         if (!packet) {
787                 ErrPrint("Invalid packet\n");
788                 goto out;
789         }
790
791         ret = packet_get(packet, "sssiiii", &pkgname, &id, &fbfile, &is_pd, &w, &h, &status);
792         if (ret != 7) {
793                 ErrPrint("Invalid argument\n");
794                 goto out;
795         }
796
797         handler = lb_find_livebox(pkgname, id);
798         if (!handler) {
799                 ErrPrint("Livebox(%s) is not found\n", id);
800                 goto out;
801         }
802
803         if (handler->state != CREATE) {
804                 ErrPrint("Livebox(%s) is not created yet\n", id);
805                 goto out;
806         }
807
808         if (is_pd) {
809                 /*!
810                  * \NOTE
811                  * PD is not able to resized by the client.
812                  * PD is only can be managed by the provider.
813                  * So the PD has no private resized event handler.
814                  * Notify it via global event handler only.
815                  */
816                 if (status == 0) {
817                         lb_set_pdsize(handler, w, h);
818                         lb_invoke_event_handler(handler, LB_EVENT_PD_SIZE_CHANGED);
819                 } else {
820                         ErrPrint("This is not possible. PD Size is changed but the return value is not ZERO (%d)\n", status);
821                 }
822         } else {
823                 if (status == 0) {
824                         lb_set_size(handler, w, h);
825
826                         /*!
827                          * \NOTE
828                          * If there is a created LB FB, 
829                          * Update it too.
830                          */
831                         if (lb_get_lb_fb(handler)) {
832                                 (void)lb_set_lb_fb(handler, fbfile);
833
834                                 ret = fb_sync(lb_get_lb_fb(handler));
835                                 if (ret < 0) {
836                                         ErrPrint("Failed to do sync FB (%s - %s)\n", pkgname, util_basename(util_uri_to_path(id)));
837                                 }
838
839                                 /* Just update the size info only. */
840                         }
841
842                         /*!
843                          * \NOTE
844                          * I cannot believe client.
845                          * So I added some log before & after call the user callback.
846                          */
847                         if (handler->size_changed_cb) {
848                                 ret_cb_t cb;
849                                 void *cbdata;
850
851                                 cb = handler->size_changed_cb;
852                                 cbdata = handler->size_cbdata;
853
854                                 handler->size_changed_cb = NULL;
855                                 handler->size_cbdata = NULL;
856
857                                 cb(handler, status, cbdata);
858                         } else {
859                                 lb_invoke_event_handler(handler, LB_EVENT_LB_SIZE_CHANGED);
860                         }
861                 } else {
862                         if (handler->size_changed_cb) {
863                                 ret_cb_t cb;
864                                 void *cbdata;
865
866                                 cb = handler->size_changed_cb;
867                                 cbdata = handler->size_cbdata;
868
869                                 handler->size_changed_cb = NULL;
870                                 handler->size_cbdata = NULL;
871
872                                 cb(handler, status, cbdata);
873                         }
874                 }
875         }
876
877 out:
878         return NULL;
879 }
880
881 static struct packet *master_period_changed(pid_t pid, int handle, const struct packet *packet)
882 {
883         struct livebox *handler;
884         const char *pkgname;
885         const char *id;
886         int ret;
887         double period;
888         int status;
889
890         ret = packet_get(packet, "idss", &status, &period, &pkgname, &id);
891         if (ret != 4) {
892                 ErrPrint("Invalid argument\n");
893                 goto out;
894         }
895
896         handler = lb_find_livebox(pkgname, id);
897         if (!handler) {
898                 ErrPrint("Livebox(%s) is not found\n", id);
899                 goto out;
900         }
901
902         if (handler->state != CREATE) {
903                 ErrPrint("Livebox(%s) is not created\n", id);
904                 goto out;
905         }
906
907         if (status == 0) {
908                 lb_set_period(handler, period);
909         }
910
911         if (handler->period_changed_cb) {
912                 ret_cb_t cb;
913                 void *cbdata;
914
915                 cb = handler->period_changed_cb;
916                 cbdata = handler->period_cbdata;
917
918                 handler->period_changed_cb = NULL;
919                 handler->period_cbdata = NULL;
920
921                 cb(handler, status, cbdata);
922         } else if (status == 0) {
923                 lb_invoke_event_handler(handler, LB_EVENT_PERIOD_CHANGED);
924         }
925
926 out:
927         return NULL;
928 }
929
930 static struct packet *master_group_changed(pid_t pid, int handle, const struct packet *packet)
931 {
932         struct livebox *handler;
933         const char *pkgname;
934         const char *id;
935         int ret;
936         const char *cluster;
937         const char *category;
938         int status;
939
940         ret = packet_get(packet, "ssiss", &pkgname, &id, &status, &cluster, &category);
941         if (ret != 5) {
942                 ErrPrint("Invalid argument\n");
943                 goto out;
944         }
945
946         handler = lb_find_livebox(pkgname, id);
947         if (!handler) {
948                 ErrPrint("Livebox(%s) is not exists\n", id);
949                 goto out;
950         }
951
952         if (handler->state != CREATE) {
953                 /*!
954                  * \note
955                  * Do no access this handler,
956                  * You cannot believe this handler anymore.
957                  */
958                 ErrPrint("Livebox(%s) is not created\n", id);
959                 goto out;
960         }
961
962         if (status == 0) {
963                 (void)lb_set_group(handler, cluster, category);
964         }
965
966         if (handler->group_changed_cb) {
967                 ret_cb_t cb;
968                 void *cbdata;
969
970                 cb = handler->group_changed_cb;
971                 cbdata = handler->group_cbdata;
972
973                 handler->group_changed_cb = NULL;
974                 handler->group_cbdata = NULL;
975
976                 cb(handler, status, cbdata);
977         } else if (status == 0) {
978                 lb_invoke_event_handler(handler, LB_EVENT_GROUP_CHANGED);
979         }
980
981 out:
982         return NULL;
983 }
984
985 static struct packet *master_created(pid_t pid, int handle, const struct packet *packet)
986 {
987         struct livebox *handler;
988
989         int lb_w;
990         int lb_h;
991         int pd_w;
992         int pd_h;
993         const char *pkgname;
994         const char *id;
995
996         const char *content;
997         const char *cluster;
998         const char *category;
999         const char *lb_fname;
1000         const char *pd_fname;
1001         const char *title;
1002
1003         double timestamp;
1004         const char *auto_launch;
1005         double priority;
1006         int size_list;
1007         int user;
1008         int pinup_supported;
1009         enum lb_type lb_type;
1010         enum pd_type pd_type;
1011         double period;
1012         int is_pinned_up;
1013
1014         int old_state = DESTROYED;
1015
1016         int ret;
1017
1018         ret = packet_get(packet, "dsssiiiisssssdiiiiidsi",
1019                         &timestamp,
1020                         &pkgname, &id, &content,
1021                         &lb_w, &lb_h, &pd_w, &pd_h,
1022                         &cluster, &category, &lb_fname, &pd_fname,
1023                         &auto_launch, &priority, &size_list, &user, &pinup_supported,
1024                         &lb_type, &pd_type, &period, &title, &is_pinned_up);
1025         if (ret != 22) {
1026                 ErrPrint("Invalid argument\n");
1027                 ret = LB_STATUS_ERROR_INVALID;
1028                 goto out;
1029         }
1030
1031         ErrPrint("[%lf] pkgname: %s, id: %s, content: %s, "
1032                 "pd_w: %d, pd_h: %d, lb_w: %d, lb_h: %d, "
1033                 "cluster: %s, category: %s, lb_fname: \"%s\", pd_fname: \"%s\", "
1034                 "auto_launch: %s, priority: %lf, size_list: %d, user: %d, pinup: %d, "
1035                 "lb_type: %d, pd_type: %d, period: %lf, title: [%s], is_pinned_up: %d\n",
1036                 timestamp, pkgname, id, content,
1037                 pd_w, pd_h, lb_w, lb_h,
1038                 cluster, category, lb_fname, pd_fname,
1039                 auto_launch, priority, size_list, user, pinup_supported,
1040                 lb_type, pd_type, period, title, is_pinned_up);
1041
1042         handler = lb_find_livebox_by_timestamp(timestamp);
1043         if (!handler) {
1044                 handler = lb_new_livebox(pkgname, id, timestamp);
1045                 if (!handler) {
1046                         ErrPrint("Failed to create a new livebox\n");
1047                         ret = LB_STATUS_ERROR_FAULT;
1048                         goto out;
1049                 }
1050
1051                 old_state = handler->state;
1052         } else {
1053                 if (handler->state != CREATE) {
1054                         if (handler->state != DELETE) {
1055                                 /*!
1056                                  * \note
1057                                  * This is not possible!!!
1058                                  */
1059                                 ErrPrint("Invalid handler\n");
1060                                 ret = LB_STATUS_ERROR_INVALID;
1061                                 goto out;
1062                         }
1063
1064                         /*!
1065                          * \note
1066                          * After get the delete states,
1067                          * call the create callback with deleted result.
1068                          */
1069                 }
1070
1071                 old_state = handler->state;
1072
1073                 if (handler->id) {
1074                         ErrPrint("Already created: timestamp[%lf] "
1075                                 "pkgname[%s], id[%s] content[%s] "
1076                                 "cluster[%s] category[%s] lb_fname[%s] pd_fname[%s]\n",
1077                                         timestamp, pkgname, id,
1078                                         content, cluster, category,
1079                                         lb_fname, pd_fname);
1080
1081                         ret = LB_STATUS_ERROR_ALREADY;
1082                         goto out;
1083                 }
1084
1085                 lb_set_id(handler, id);
1086         }
1087
1088         lb_set_size(handler, lb_w, lb_h);
1089         handler->lb.type = lb_type;
1090         handler->is_pinned_up = is_pinned_up;
1091
1092         switch (lb_type) {
1093         case _LB_TYPE_FILE:
1094                 break;
1095         case _LB_TYPE_SCRIPT:
1096         case _LB_TYPE_BUFFER:
1097                 if (!strlen(lb_fname)) {
1098                         break;
1099                 }
1100                 (void)lb_set_lb_fb(handler, lb_fname);
1101
1102                 ret = fb_sync(lb_get_lb_fb(handler));
1103                 if (ret < 0) {
1104                         ErrPrint("Failed to do sync FB (%s - %s)\n", pkgname, util_basename(util_uri_to_path(id)));
1105                 }
1106                 break;
1107         case _LB_TYPE_TEXT:
1108                 lb_set_text_lb(handler);
1109                 break;
1110         default:
1111                 break;
1112         }
1113
1114         handler->pd.type = pd_type;
1115         lb_set_pdsize(handler, pd_w, pd_h);
1116         lb_set_default_pdsize(handler, pd_w, pd_h);
1117         switch (pd_type) {
1118         case _PD_TYPE_SCRIPT:
1119         case _PD_TYPE_BUFFER:
1120                 if (!strlen(pd_fname)) {
1121                         break;
1122                 }
1123
1124                 lb_set_pd_fb(handler, pd_fname);
1125                 ret = fb_sync(lb_get_pd_fb(handler));
1126                 if (ret < 0) {
1127                         ErrPrint("Failed to do sync FB (%s - %s)\n", pkgname, util_basename(util_uri_to_path(id)));
1128                 }
1129                 break;
1130         case _PD_TYPE_TEXT:
1131                 lb_set_text_pd(handler);
1132                 break;
1133         default:
1134                 break;
1135         }
1136
1137         lb_set_priority(handler, priority);
1138
1139         lb_set_size_list(handler, size_list);
1140         lb_set_group(handler, cluster, category);
1141
1142         lb_set_content(handler, content);
1143         lb_set_title(handler, title);
1144
1145         lb_set_user(handler, user);
1146
1147         lb_set_auto_launch(handler, auto_launch);
1148         lb_set_pinup(handler, pinup_supported);
1149
1150         lb_set_period(handler, period);
1151
1152         ret = 0;
1153
1154         if (handler->state == CREATE) {
1155                 /*!
1156                  * \note
1157                  * These callback can change the handler->state.
1158                  * So we have to use the "old_state" which stored state before call these callbacks
1159                  */
1160
1161                 if (handler->created_cb) {
1162                         ret_cb_t cb;
1163                         void *cbdata;
1164
1165                         cb = handler->created_cb;
1166                         cbdata = handler->created_cbdata;
1167
1168                         handler->created_cb = NULL;
1169                         handler->created_cbdata = NULL;
1170
1171                         cb(handler, ret, cbdata);
1172                 } else {
1173                         lb_invoke_event_handler(handler, LB_EVENT_CREATED);
1174                 }
1175         }
1176
1177 out:
1178         if (ret == 0 && old_state == DELETE) {
1179                 lb_send_delete(handler, handler->created_cb, handler->created_cbdata);
1180
1181                 /*!
1182                  * \note
1183                  * handler->created_cb = NULL;
1184                  * handler->created_cbdata = NULL;
1185                  *
1186                  * Do not clear this to use this from the deleted event callback.
1187                  * if this value is not cleared when the deleted event callback check it,
1188                  * it means that the created function is not called yet.
1189                  * Then the call the deleted event callback with LB_STATUS_ERROR_CANCEL errno.
1190                  */
1191         }
1192
1193         return NULL;
1194 }
1195
1196 static struct method s_table[] = {
1197         {
1198                 .cmd = "lb_updated", /* pkgname, id, lb_w, lb_h, priority, ret */
1199                 .handler = master_lb_updated,
1200         },
1201         {
1202                 .cmd = "pd_updated", /* pkgname, id, descfile, pd_w, pd_h, ret */
1203                 .handler = master_pd_updated,
1204         },
1205         {
1206                 .cmd = "pd_created",
1207                 .handler = master_pd_created,
1208         },
1209         {
1210                 .cmd = "pd_destroyed",
1211                 .handler = master_pd_destroyed,
1212         },
1213         {
1214                 .cmd = "fault_package", /* pkgname, id, function, ret */
1215                 .handler = master_fault_package,
1216         },
1217         {
1218                 .cmd = "deleted", /* pkgname, id, timestamp, ret */
1219                 .handler = master_deleted,
1220         },
1221         {
1222                 .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 */
1223                 .handler = master_created,
1224         },
1225         {
1226                 .cmd = "group_changed",
1227                 .handler = master_group_changed,
1228         },
1229         {
1230                 .cmd = "period_changed",
1231                 .handler = master_period_changed,
1232         },
1233         {
1234                 .cmd = "size_changed",
1235                 .handler = master_size_changed,
1236         },
1237         {
1238                 .cmd = "pinup",
1239                 .handler = master_pinup,
1240         },
1241         {
1242                 .cmd = "scroll",
1243                 .handler = master_hold_scroll,
1244         },
1245
1246         {
1247                 .cmd = "update_mode",
1248                 .handler = master_update_mode,
1249         },
1250
1251         {
1252                 .cmd = "lb_update_begin",
1253                 .handler = master_lb_update_begin,
1254         },
1255         {
1256                 .cmd = "lb_update_end",
1257                 .handler = master_lb_update_end,
1258         },
1259
1260         {
1261                 .cmd = "pd_update_begin",
1262                 .handler = master_pd_update_begin,
1263         },
1264         {
1265                 .cmd = "pd_update_end",
1266                 .handler = master_pd_update_end,
1267         },
1268
1269         {
1270                 .cmd = "access_status",
1271                 .handler = master_access_status,
1272         },
1273
1274         {
1275                 .cmd = NULL,
1276                 .handler = NULL,
1277         },
1278 };
1279
1280 static void acquire_cb(struct livebox *handler, const struct packet *result, void *data)
1281 {
1282         if (!result) {
1283                 DbgPrint("Result packet is not valid\n");
1284         } else {
1285                 int ret;
1286
1287                 if (packet_get(result, "i", &ret) != 1) {
1288                         ErrPrint("Invalid argument\n");
1289                 } else {
1290                         DbgPrint("Acquire returns: %d\n", ret);
1291                 }
1292         }
1293
1294         return;
1295 }
1296
1297 static inline int make_connection(void)
1298 {
1299         struct packet *packet;
1300         int ret;
1301
1302         DbgPrint("Let's making connection!\n");
1303
1304         s_info.fd = com_core_packet_client_init(client_addr(), 0, s_table);
1305         if (s_info.fd < 0) {
1306                 ErrPrint("Try this again later\n");
1307                 return LB_STATUS_ERROR_IO;
1308         }
1309
1310         packet = packet_create("acquire", "d", util_timestamp());
1311         if (!packet) {
1312                 com_core_packet_client_fini(s_info.fd);
1313                 s_info.fd = -1;
1314                 return LB_STATUS_ERROR_FAULT;
1315         }
1316
1317         ret = master_rpc_async_request(NULL, packet, 1, acquire_cb, NULL);
1318         if (ret < 0) {
1319                 ErrPrint("Master RPC returns %d\n", ret);
1320                 com_core_packet_client_fini(s_info.fd);
1321                 s_info.fd = -1;
1322                 return LB_STATUS_ERROR_IO;
1323         }
1324
1325         return LB_STATUS_SUCCESS;
1326 }
1327
1328 static int connected_cb(int handle, void *data)
1329 {
1330         master_rpc_check_and_fire_consumer();
1331         return 0;
1332 }
1333
1334 static void master_started_cb(keynode_t *node, void *data)
1335 {
1336         int state = 0;
1337
1338         if (vconf_get_bool(VCONFKEY_MASTER_STARTED, &state) < 0) {
1339                 ErrPrint("Unable to get [%s]\n", VCONFKEY_MASTER_STARTED);
1340         }
1341
1342         DbgPrint("Master state: %d\n", state);
1343         if (state == 1 && make_connection() == LB_STATUS_SUCCESS) {
1344                 int ret;
1345                 ret = vconf_ignore_key_changed(VCONFKEY_MASTER_STARTED, master_started_cb);
1346                 DbgPrint("master_started vconf key de-registered [%d]\n", ret);
1347         }
1348 }
1349
1350 static gboolean timeout_cb(gpointer data)
1351 {
1352         if (vconf_notify_key_changed(VCONFKEY_MASTER_STARTED, master_started_cb, NULL) < 0) {
1353                 ErrPrint("Failed to add vconf for monitoring service state\n");
1354         } else {
1355                 DbgPrint("vconf event callback is registered\n");
1356         }
1357
1358         master_started_cb(NULL, NULL);
1359
1360         s_info.timer_id = 0;
1361         return FALSE;
1362 }
1363
1364 static int disconnected_cb(int handle, void *data)
1365 {
1366         if (s_info.fd != handle) {
1367                 /*!< This handle is not my favor */
1368                 return 0;
1369         }
1370
1371         s_info.fd = -1; /*!< Disconnected */
1372
1373         master_rpc_clear_all_request();
1374         lb_invoke_fault_handler(LB_FAULT_PROVIDER_DISCONNECTED, MASTER_PKGNAME, "default", "disconnected");
1375
1376         lb_delete_all();
1377
1378         /* Try to reconnect after 1 sec later */
1379         if (!s_info.timer_id) {
1380                 DbgPrint("Reconnecting timer is added\n");
1381                 s_info.timer_id = g_timeout_add(1000, timeout_cb, NULL);
1382                 if (s_info.timer_id == 0) {
1383                         ErrPrint("Unable to add reconnecting timer\n");
1384                         return 0;
1385                 }
1386         } else {
1387                 ErrPrint("Reconnecting timer is already exists\n");
1388         }
1389
1390         return 0;
1391 }
1392
1393 int client_init(void)
1394 {
1395         s_info.client_addr = vconf_get_str(VCONFKEY_MASTER_CLIENT_ADDR);
1396         if (!s_info.client_addr) {
1397                 s_info.client_addr = strdup(CLIENT_SOCKET);
1398                 if (!s_info.client_addr) {
1399                         ErrPrint("Heap: %s\n", strerror(errno));
1400                         return -ENOMEM;
1401                 }
1402         }
1403
1404         (void)file_service_init();
1405
1406         DbgPrint("Server Address: %s\n", s_info.client_addr);
1407
1408         com_core_add_event_callback(CONNECTOR_DISCONNECTED, disconnected_cb, NULL);
1409         com_core_add_event_callback(CONNECTOR_CONNECTED, connected_cb, NULL);
1410         if (vconf_notify_key_changed(VCONFKEY_MASTER_STARTED, master_started_cb, NULL) < 0) {
1411                 ErrPrint("Failed to add vconf for service state\n");
1412         } else {
1413                 DbgPrint("vconf event callback is registered\n");
1414         }
1415
1416         master_started_cb(NULL, NULL);
1417         return 0;
1418 }
1419
1420 int client_fd(void)
1421 {
1422         return s_info.fd;
1423 }
1424
1425 const char *client_addr(void)
1426 {
1427         return s_info.client_addr;
1428 }
1429
1430 int client_fini(void)
1431 {
1432         int ret;
1433
1434         (void)file_service_fini();
1435
1436         ret = vconf_ignore_key_changed(VCONFKEY_MASTER_STARTED, master_started_cb);
1437         if (ret < 0) {
1438                 DbgPrint("Ignore vconf key: %d\n", ret);
1439         }
1440
1441         com_core_del_event_callback(CONNECTOR_DISCONNECTED, disconnected_cb, NULL);
1442         com_core_del_event_callback(CONNECTOR_CONNECTED, connected_cb, NULL);
1443         com_core_packet_client_fini(s_info.fd);
1444         s_info.fd = -1;
1445         free(s_info.client_addr);
1446         s_info.client_addr = NULL;
1447         return LB_STATUS_SUCCESS;
1448 }
1449
1450 /* End of a file */