Fix the widget_app_get_id()
[platform/core/appfw/appcore-widget.git] / src / widget_app.c
1 /*
2  * Copyright (c) 2015 - 2016 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (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://www.apache.org/licenses/LICENSE-2.0
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
18 #include <stdlib.h>
19
20 #include <bundle.h>
21 #include <bundle_internal.h>
22 #include <aul.h>
23 #include <dlog.h>
24 #include <glib.h>
25 #include <glib-object.h>
26 #include <stdlib.h>
27 #include <app_control.h>
28 #include <app_control_internal.h>
29 #include <Elementary.h>
30 #include <widget_errno.h>
31 #include <widget_instance.h>
32 #include <widget_service_internal.h>
33 #include <aul_app_com.h>
34 #include <Ecore_Wayland.h>
35 #include <system_info.h>
36
37 #include "widget_app.h"
38 #include "widget-log.h"
39 #include "widget-private.h"
40 #include "widget_app_internal.h"
41
42 #ifdef LOG_TAG
43 #undef LOG_TAG
44 #endif
45
46 #define STR_MAX_BUF 128
47 #define LOG_TAG "CAPI_WIDGET_APPLICATION"
48 #define K_REASON    "__WC_K_REASON__"
49
50 typedef enum _widget_obj_state_e {
51         WC_READY = 0,
52         WC_RUNNING = 1,
53         WC_PAUSED = 2,
54         WC_TERMINATED = 3
55 } widget_obj_state_e;
56
57 struct app_event_handler {
58         app_event_type_e type;
59         app_event_cb cb;
60         void *data;
61 };
62
63 struct app_event_info {
64         app_event_type_e type;
65         void *value;
66 };
67
68 struct _widget_class {
69         void *user_data;
70         widget_instance_lifecycle_callback_s ops;
71         char *classid;
72         struct _widget_class *next;
73         struct _widget_class *prev;
74 };
75
76 struct _widget_context {
77         char *id;
78         struct _widget_class *provider;
79         int state;
80         void *tag;
81         Evas_Object *win;
82         bundle *content;
83         widget_instance_lifecycle_callback_s ops;
84 };
85
86 typedef struct _widget_class widget_class_s;
87 typedef struct _widget_context widget_context_s;
88
89 static int caller_pid = 0;
90 static widget_app_lifecycle_callback_s *app_ops;
91 static void *app_user_data = NULL;
92 static char *appid = NULL;
93 static widget_class_h class_provider = NULL;
94 static GList *contexts = NULL;
95 static char *viewer_endpoint = NULL;
96
97 static inline bool _is_widget_feature_enabled(void)
98 {
99         static bool feature = false;
100         static bool retrieved = false;
101         int ret;
102
103         if (retrieved == true)
104                 return feature;
105
106         ret = system_info_get_platform_bool(
107                         "http://tizen.org/feature/shell.appwidget", &feature);
108         if (ret != SYSTEM_INFO_ERROR_NONE) {
109                 _E("failed to get system info");
110                 return false;
111         }
112
113         retrieved = true;
114
115         return feature;
116 }
117
118 static gint __comp_by_id(gconstpointer a, gconstpointer b)
119 {
120         widget_context_s *wc = (widget_context_s *)a;
121
122         return strcmp(wc->id, (const char *)b);
123 }
124
125 static widget_context_s *__find_context_by_id(const char *id)
126 {
127         GList *ret = g_list_find_custom(contexts, id, __comp_by_id);
128
129         if (ret == NULL)
130                 return NULL;
131
132         return ret->data;
133 }
134
135 static int __send_update_status(const char *class_id, const char *instance_id,
136         int status, bundle *extra, int internal_only)
137 {
138         bundle *b = extra;
139
140         if (b == NULL)
141                 b = bundle_create();
142
143         bundle_add_str(b, WIDGET_K_ID, class_id);
144         bundle_add_str(b, WIDGET_K_INSTANCE, instance_id);
145         bundle_add_byte(b, WIDGET_K_STATUS, &status, sizeof(int));
146
147         _E("send update %s(%d) to %s", instance_id, status, viewer_endpoint);
148         aul_app_com_send(viewer_endpoint, b);
149
150         if (extra == NULL)
151                 bundle_free(b);
152
153         return 0;
154 }
155
156 static int __instance_create(widget_class_h handle, const char *id, bundle *b)
157 {
158         widget_context_s *wc = NULL;
159         int w = 0, h = 0;
160         char *w_str = NULL, *h_str = NULL;
161         char *remain = NULL;
162         int ret = 0;
163
164         wc = (widget_context_s *)malloc(sizeof(widget_context_s));
165         if (!wc)
166                 return WIDGET_ERROR_OUT_OF_MEMORY;
167
168         wc->state = WC_READY;
169         wc->id = g_strdup(id);
170         wc->provider = handle;
171         wc->win = NULL;
172
173         wc->content = bundle_dup(b);
174         bundle_get_str(b, WIDGET_K_WIDTH, &w_str);
175         bundle_get_str(b, WIDGET_K_HEIGHT, &h_str);
176
177         if (w_str)
178                 w = (int)g_ascii_strtoll(w_str, &remain, 10);
179
180         if (h_str)
181                 h = (int)g_ascii_strtoll(h_str, &remain, 10);
182
183         contexts = g_list_append(contexts, wc);
184
185         handle->ops.create(wc, b, w, h, handle->user_data);
186         ret = __send_update_status(handle->classid, wc->id,
187                         WIDGET_INSTANCE_EVENT_CREATE, b, 0);
188
189         return ret;
190 }
191
192 static int __instance_destroy(widget_class_h handle, const char *id,
193                 widget_destroy_type_e reason, bundle *b)
194 {
195         widget_context_s *wc = __find_context_by_id(id);
196         int ret = 0;
197
198         if (wc) {
199                 wc->state = WC_TERMINATED;
200                 handle->ops.destroy(wc, (widget_app_destroy_type_e)reason, b,
201                                 handle->user_data);
202
203                 ret = __send_update_status(handle->classid, id,
204                                 WIDGET_INSTANCE_EVENT_TERMINATE, b, 0);
205
206                 contexts = g_list_remove(contexts, wc);
207
208                 if (wc->id)
209                         free(wc->id);
210                 free(wc);
211         } else {
212                 _E("could not find widget obj: %s", id);
213                 ret = WIDGET_ERROR_INVALID_PARAMETER;
214         }
215
216         return ret;
217 }
218
219 static widget_class_h __find_class_handler(const char *class_id,
220                 widget_class_h handle)
221 {
222         if (!class_id || !handle)
223                 return NULL;
224
225         widget_class_h head = handle;
226
227         while (head) {
228                 if (head->classid && strcmp(head->classid, class_id) == 0)
229                         return head;
230
231                 head = head->next;
232         }
233
234         return NULL;
235 }
236
237 static void __control(bundle *b)
238 {
239         char *class_id = NULL;
240         char *id = NULL;
241         char *operation = NULL;
242         char *reason = NULL;
243         char *remain = NULL;
244         int destroy_type = WIDGET_DESTROY_TYPE_DEFAULT;
245
246         widget_class_h handle = NULL;
247         bundle_get_str(b, WIDGET_K_CLASS, &class_id);
248         /* for previous version compatibility, use appid for default class id */
249         if (class_id == NULL)
250                 class_id = appid;
251
252         bundle_get_str(b, WIDGET_K_INSTANCE, &id);
253         bundle_get_str(b, WIDGET_K_OPERATION, &operation);
254
255         handle = __find_class_handler(class_id, class_provider);
256         if (!handle) {
257                 _E("no handle provided: %s", class_id);
258                 goto error;
259         }
260
261         if (!operation) {
262                 _E("no operation provided");
263                 goto error;
264         }
265
266         if (strcmp(operation, "create") == 0) {
267                 __instance_create(handle, id, b);
268         } else if (strcmp(operation, "resize") == 0) {
269                 /* TODO */
270         } else if (strcmp(operation, "update") == 0) {
271                 /* TODO */
272         } else if (strcmp(operation, "destroy") == 0) {
273                 bundle_get_str(b, WIDGET_K_REASON, &reason);
274                 if (reason)
275                         destroy_type = (int)g_ascii_strtoll(reason, &remain,
276                                         10);
277
278                 __instance_destroy(handle, id, destroy_type, b);
279         } else if (strcmp(operation, "resume") == 0) {
280                 /* TODO */
281         } else if (strcmp(operation, "pause") == 0) {
282                 /* TODO */
283         }
284
285         return;
286 error:
287         LOGD("error on control");
288         return;
289 }
290
291 static void __show_all()
292 {
293         LOGD("resume");
294 }
295
296 static int __aul_handler(aul_type type, bundle *b, void *data)
297 {
298         char *caller = NULL;
299         char *remain = NULL;
300
301         switch (type) {
302         case AUL_START:
303                 if (b) {
304                         bundle_get_str(b, WIDGET_K_CALLER, &caller);
305                         if (caller) {
306                                 caller_pid = g_ascii_strtoll(caller, &remain,
307                                                 10);
308                         } else {
309                                 /* using caller appid and query pid using caller appid? */
310                                 _E("no caller pid");
311                         }
312                 }
313
314                 __control(b);
315                 break;
316         case AUL_RESUME:
317                 __show_all();
318                 break;
319         case AUL_TERMINATE:
320                 widget_app_exit();
321                 break;
322         default:
323                 break;
324         }
325
326         return 0;
327 }
328
329 static char *__get_domain_name(char *appid)
330 {
331         char *name_token;
332
333         if (appid == NULL) {
334                 _E("appid is NULL");
335                 return NULL;
336         }
337
338         name_token = strrchr(appid, '.');
339
340         if (name_token == NULL) {
341                 _E("appid is invalid");
342                 return appid;
343         }
344
345         name_token++;
346
347         return name_token;
348 }
349
350 extern int _set_i18n(const char *name);
351
352 static int __before_loop(int argc, char **argv)
353 {
354         int r;
355         bundle *kb = NULL;
356         char *wayland_display = NULL;
357         char *xdg_runtime_dir = NULL;
358         char *name;
359
360 #if !(GLIB_CHECK_VERSION(2, 36, 0))
361         g_type_init();
362 #endif
363
364         kb = bundle_import_from_argv(argc, argv);
365         if (kb) {
366                 bundle_get_str(kb, AUL_K_WAYLAND_WORKING_DIR, &xdg_runtime_dir);
367                 bundle_get_str(kb, AUL_K_WAYLAND_DISPLAY, &wayland_display);
368                 bundle_get_str(kb, WIDGET_K_ENDPOINT, &viewer_endpoint);
369                 if (viewer_endpoint) {
370                         _E("viewer endpoint :%s", viewer_endpoint);
371                         viewer_endpoint = strdup(viewer_endpoint);
372                 } else {
373                         _E("endpoint is missing");
374                 }
375
376                 if (xdg_runtime_dir)
377                         setenv("XDG_RUNTIME_DIR", xdg_runtime_dir, 1);
378
379                 if (wayland_display)
380                         setenv("WAYLAND_DISPLAY", wayland_display, 1);
381
382                 bundle_free(kb);
383                 kb = NULL;
384         } else {
385                 _E("failed to get launch argv");
386         }
387
388         elm_init(argc, argv);
389
390         r = aul_launch_init(__aul_handler, NULL);
391         if (r < 0) {
392                 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
393                                 __FUNCTION__,
394                                 "Fail to call the aul_launch_init");
395         }
396
397         r = aul_launch_argv_handler(argc, argv);
398         if (r < 0) {
399                 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
400                                 __FUNCTION__,
401                                 "Fail to call the aul_launch_argv_handler");
402         }
403
404         r = app_get_id(&appid);
405         if (r != APP_ERROR_NONE)
406                 return r;
407
408         name = __get_domain_name(appid);
409
410         if (name == NULL) {
411                 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
412                                 __FUNCTION__,
413                                 "Fail to call __get_domain_name");
414         }
415
416         r = _set_i18n(name);
417
418         if (r < 0) {
419                 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
420                                 __FUNCTION__,
421                                 "Fail to call _set_i18n");
422         }
423
424         class_provider = app_ops->create(app_user_data);
425         if (class_provider == NULL) {
426                 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
427                                 __FUNCTION__, "widget_class is NULL");
428         }
429
430         return WIDGET_ERROR_NONE;
431 }
432
433 static void __after_loop()
434 {
435         if (app_ops->terminate)
436                 app_ops->terminate(app_user_data);
437
438         if (viewer_endpoint)
439                 free(viewer_endpoint);
440
441         elm_shutdown();
442 }
443
444 EXPORT_API int widget_app_main(int argc, char **argv,
445                 widget_app_lifecycle_callback_s *callback, void *user_data)
446 {
447         int r;
448
449         if (!_is_widget_feature_enabled()) {
450                 _E("not supported");
451                 return WIDGET_ERROR_NOT_SUPPORTED;
452         }
453
454         if (argc <= 0 || argv == NULL || callback == NULL)
455                 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
456                                 __FUNCTION__, NULL);
457
458         if (callback->create == NULL)
459                 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
460                                 __FUNCTION__,
461                                 "widget_app_create_cb() callback must be "
462                                 "registered");
463
464         app_ops = callback;
465         app_user_data = user_data;
466         r = __before_loop(argc, argv);
467         if (r < 0)
468                 return r;
469
470         ecore_main_loop_begin();
471         aul_status_update(STATUS_DYING);
472         __after_loop();
473
474         return WIDGET_ERROR_NONE;
475 }
476
477 EXPORT_API int widget_app_exit(void)
478 {
479         if (!_is_widget_feature_enabled()) {
480                 _E("not supported");
481                 return WIDGET_ERROR_NOT_SUPPORTED;
482         }
483
484         ecore_main_loop_quit();
485
486         return WIDGET_ERROR_NONE;
487 }
488
489 static gboolean __finish_event_cb(gpointer user_data)
490 {
491         if (user_data == NULL)
492                 return FALSE;
493
494         widget_context_s *wc = (widget_context_s *)user_data;
495
496         switch (wc->state) {
497         case WC_READY:
498
499                 break;
500         case WC_RUNNING:
501
502                 break;
503         case WC_PAUSED:
504
505                 break;
506         default:
507                 break;
508         }
509
510
511         return FALSE;
512 }
513
514 EXPORT_API int widget_app_terminate_context(widget_context_h context)
515 {
516         if (!_is_widget_feature_enabled()) {
517                 _E("not supported");
518                 return WIDGET_ERROR_NOT_SUPPORTED;
519         }
520
521         if (context == NULL)
522                 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
523                                 __FUNCTION__, NULL);
524
525         g_idle_add(__finish_event_cb, context);
526         return WIDGET_ERROR_NONE;
527 }
528
529 EXPORT_API int widget_app_foreach_context(widget_context_cb cb, void *data)
530 {
531         if (!_is_widget_feature_enabled()) {
532                 _E("not supported");
533                 return WIDGET_ERROR_NOT_SUPPORTED;
534         }
535
536         if (!cb)
537                 return WIDGET_ERROR_INVALID_PARAMETER;
538
539         return WIDGET_ERROR_NONE;
540 }
541
542 EXPORT_API int widget_app_add_event_handler(app_event_handler_h *event_handler,
543                 app_event_type_e event_type, app_event_cb callback,
544                 void *user_data)
545 {
546         if (!_is_widget_feature_enabled()) {
547                 _E("not supported");
548                 return WIDGET_ERROR_NOT_SUPPORTED;
549         }
550
551         /* TODO */
552         if (!event_handler || !callback)
553                 return WIDGET_ERROR_INVALID_PARAMETER;
554
555         switch (event_type) {
556         case APP_EVENT_LOW_MEMORY:
557         case APP_EVENT_LOW_BATTERY:
558         case APP_EVENT_LANGUAGE_CHANGED:
559         case APP_EVENT_DEVICE_ORIENTATION_CHANGED:
560         case APP_EVENT_REGION_FORMAT_CHANGED:
561         case APP_EVENT_SUSPENDED_STATE_CHANGED:
562
563                 break;
564         default:
565                 return WIDGET_ERROR_INVALID_PARAMETER;
566         }
567
568         return WIDGET_ERROR_NONE;
569 }
570
571 EXPORT_API int widget_app_remove_event_handler(app_event_handler_h
572                                                 event_handler)
573 {
574         if (!_is_widget_feature_enabled()) {
575                 _E("not supported");
576                 return WIDGET_ERROR_NOT_SUPPORTED;
577         }
578
579         /* TODO */
580         if (!event_handler)
581                 return WIDGET_ERROR_INVALID_PARAMETER;
582
583         return WIDGET_ERROR_NONE;
584 }
585
586 EXPORT_API const char *widget_app_get_id(widget_context_h context)
587 {
588         if (!_is_widget_feature_enabled()) {
589                 _E("not supported");
590                 set_last_result(WIDGET_ERROR_NOT_SUPPORTED);
591                 return NULL;
592         }
593
594         if (!context) {
595                 set_last_result(WIDGET_ERROR_INVALID_PARAMETER);
596                 return NULL;
597         }
598
599         set_last_result(WIDGET_ERROR_NONE);
600         return context->id;
601 }
602
603 EXPORT_API int widget_app_get_elm_win(widget_context_h context,
604                                         Evas_Object **win)
605 {
606         widget_context_s *cxt = (widget_context_s *)context;
607         Evas_Object *ret_win;
608         Ecore_Wl_Window *wl_win;
609
610         if (!_is_widget_feature_enabled()) {
611                 _E("not supported");
612                 return WIDGET_ERROR_NOT_SUPPORTED;
613         }
614
615         if (context == NULL || win == NULL)
616                 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
617                                 __FUNCTION__, NULL);
618
619         ret_win = elm_win_add(NULL, cxt->id, ELM_WIN_BASIC);
620         if (ret_win == NULL) {
621                 _E("failed to create window");
622                 return WIDGET_ERROR_FAULT;
623         }
624
625         wl_win = elm_win_wl_window_get(ret_win);
626         if (wl_win == NULL) {
627                 _E("failed to get wayland window");
628                 evas_object_del(ret_win);
629                 return WIDGET_ERROR_FAULT;
630         }
631
632         ecore_wl_window_class_name_set(wl_win, cxt->id);
633
634         *win = ret_win;
635         cxt->win = ret_win;
636
637         return WIDGET_ERROR_NONE;
638 }
639
640 widget_class_h _widget_class_create(widget_class_s *prev, const char *class_id,
641                 widget_instance_lifecycle_callback_s callback, void *user_data)
642 {
643         widget_class_s *wc;
644
645         if (!_is_widget_feature_enabled()) {
646                 _E("not supported");
647                 set_last_result(WIDGET_ERROR_NOT_SUPPORTED);
648                 return NULL;
649         }
650
651         if (class_id == NULL) {
652                 set_last_result(WIDGET_ERROR_INVALID_PARAMETER);
653                 return NULL;
654         }
655
656         wc = (widget_class_s *)malloc(sizeof(widget_class_s));
657         if (wc == NULL) {
658                 _E("failed to malloc : %s", __FUNCTION__);
659                 set_last_result(WIDGET_ERROR_OUT_OF_MEMORY);
660                 return NULL;
661         }
662
663         wc->classid = strdup(class_id);
664         wc->user_data = user_data;
665         wc->ops = callback;
666         wc->next = prev;
667         wc->prev = NULL;
668
669         set_last_result(WIDGET_ERROR_NONE);
670
671         if (prev)
672                 prev->prev = wc;
673
674         return wc;
675 }
676
677 EXPORT_API widget_class_h widget_app_class_add(widget_class_h widget_class,
678                 const char *class_id,
679                 widget_instance_lifecycle_callback_s callback, void *user_data)
680 {
681         return _widget_class_create(widget_class, class_id, callback,
682                         user_data);
683 }
684
685 EXPORT_API widget_class_h widget_app_class_create(
686                 widget_instance_lifecycle_callback_s callback, void *user_data)
687 {
688         return _widget_class_create(class_provider, appid, callback, user_data);
689 }
690
691 EXPORT_API int widget_app_context_set_tag(widget_context_h context, void *tag)
692 {
693         if (!_is_widget_feature_enabled()) {
694                 _E("not supported");
695                 return WIDGET_ERROR_NOT_SUPPORTED;
696         }
697
698         if (context == NULL)
699                 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
700                                 __FUNCTION__, NULL);
701
702         context->tag = tag;
703
704         return WIDGET_ERROR_NONE;
705 }
706
707 EXPORT_API int widget_app_context_get_tag(widget_context_h context, void **tag)
708 {
709         if (!_is_widget_feature_enabled()) {
710                 _E("not supported");
711                 return WIDGET_ERROR_NOT_SUPPORTED;
712         }
713
714         if (context == NULL || tag == NULL)
715                 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
716                                 __FUNCTION__, NULL);
717
718         *tag = context->tag;
719
720         return WIDGET_ERROR_NONE;
721 }
722
723 EXPORT_API int widget_app_context_set_content_info(widget_context_h context,
724                 bundle *content_info)
725 {
726         const char *class_id = NULL;
727         int ret = 0;
728
729         if (!_is_widget_feature_enabled()) {
730                 _E("not supported");
731                 return WIDGET_ERROR_NOT_SUPPORTED;
732         }
733
734         if (context == NULL || content_info == NULL)
735                 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
736                                 __FUNCTION__, NULL);
737
738         if (context->provider == NULL)
739                 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
740                                 __FUNCTION__, NULL);
741
742         class_id = context->provider->classid;
743
744         if (class_id == NULL)
745                 return widget_app_error(WIDGET_ERROR_FAULT, __FUNCTION__, NULL);
746
747         ret = __send_update_status(class_id, context->id,
748                         WIDGET_INSTANCE_EVENT_UPDATE, content_info, true);
749
750         if (ret < 0) {
751                 _E("failed to send content info: %s of %s (%d)", context->id,
752                                 class_id, ret);
753                 return widget_app_error(WIDGET_ERROR_IO_ERROR, __FUNCTION__,
754                                 NULL);
755         }
756
757         return WIDGET_ERROR_NONE;
758 }
759
760 EXPORT_API int widget_app_context_set_title(widget_context_h context,
761                 const char *title)
762 {
763         if (!_is_widget_feature_enabled()) {
764                 _E("not supported");
765                 return WIDGET_ERROR_NOT_SUPPORTED;
766         }
767
768         /* TODO
769          call elm_win_title_set()
770          */
771
772         if (!context || !title)
773                 return WIDGET_ERROR_INVALID_PARAMETER;
774
775         return WIDGET_ERROR_NONE;
776 }
777