fix widget status notify
[platform/core/appfw/appcore-widget.git] / src / widget_app.c
1 /*
2  * Copyright (c) 2015 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, WIDGET_INSTANCE_EVENT_CREATE, b, 0);
187
188         return ret;
189 }
190
191 static int __instance_destroy(widget_class_h handle, const char *id, widget_destroy_type_e reason, bundle *b)
192 {
193         widget_context_s *wc = __find_context_by_id(id);
194         int ret = 0;
195
196         if (wc) {
197                 wc->state = WC_TERMINATED;
198                 handle->ops.destroy(wc, (widget_app_destroy_type_e)reason, b, handle->user_data);
199
200                 ret = __send_update_status(handle->classid, id, WIDGET_INSTANCE_EVENT_TERMINATE, b, 0);
201
202                 contexts = g_list_remove(contexts, wc);
203
204                 if (wc->id)
205                         free(wc->id);
206                 free(wc);
207         } else {
208                 _E("could not find widget obj: %s", id);
209                 ret = WIDGET_ERROR_INVALID_PARAMETER;
210         }
211
212         return ret;
213 }
214
215 static widget_class_h __find_class_handler(const char *class_id, widget_class_h handle)
216 {
217         if (!class_id || !handle)
218                 return NULL;
219
220         widget_class_h head = handle;
221
222         while (head) {
223                 if (head->classid && strcmp(head->classid, class_id) == 0)
224                         return head;
225
226                 head = head->next;
227         }
228
229         return NULL;
230 }
231
232 static void __control(bundle *b)
233 {
234         char *class_id = NULL;
235         char *id = NULL;
236         char *operation = NULL;
237         char *reason = NULL;
238         char *remain = NULL;
239         int destroy_type = WIDGET_DESTROY_TYPE_DEFAULT;
240
241         widget_class_h handle = NULL;
242         bundle_get_str(b, WIDGET_K_CLASS, &class_id);
243         /* for previous version compatibility, use appid for default class id */
244         if (class_id == NULL)
245                 class_id = appid;
246
247         bundle_get_str(b, WIDGET_K_INSTANCE, &id);
248         bundle_get_str(b, WIDGET_K_OPERATION, &operation);
249
250         handle = __find_class_handler(class_id, class_provider);
251         if (!handle) {
252                 _E("no handle provided: %s", class_id);
253                 goto error;
254         }
255
256         if (!operation) {
257                 _E("no operation provided");
258                 goto error;
259         }
260
261         if (strcmp(operation, "create") == 0) {
262                 __instance_create(handle, id, b);
263         } else if (strcmp(operation, "resize") == 0) {
264                 /* TODO */
265         } else if (strcmp(operation, "update") == 0) {
266                 /* TODO */
267         } else if (strcmp(operation, "destroy") == 0) {
268                 bundle_get_str(b, WIDGET_K_REASON, &reason);
269                 if (reason)
270                         destroy_type = (int)g_ascii_strtoll(reason, &remain, 10);
271
272                 __instance_destroy(handle, id, destroy_type, b);
273         } else if (strcmp(operation, "resume") == 0) {
274                 /* TODO */
275         } else if (strcmp(operation, "pause") == 0) {
276                 /* TODO */
277         }
278
279         return;
280 error:
281         LOGD("error on control");
282         return;
283 }
284
285 static void __show_all()
286 {
287         LOGD("resume");
288 }
289
290 static int __aul_handler(aul_type type, bundle *b, void *data)
291 {
292         char *caller = NULL;
293         char *remain = NULL;
294
295         switch (type) {
296         case AUL_START:
297                 if (b) {
298                         bundle_get_str(b, WIDGET_K_CALLER, &caller);
299                         if (caller)
300                                 caller_pid = g_ascii_strtoll(caller, &remain, 10);
301                         else {
302                                 /* using caller appid and query pid using caller appid? */
303                                 _E("no caller pid");
304                         }
305                 }
306
307                 __control(b);
308                 break;
309         case AUL_RESUME:
310                 __show_all();
311                 break;
312         case AUL_TERMINATE:
313                 widget_app_exit();
314                 break;
315         default:
316                 break;
317         }
318
319         return 0;
320 }
321
322
323 static int __before_loop(int argc, char **argv)
324 {
325         int r;
326         bundle *kb = NULL;
327         char *wayland_display = NULL;
328         char *xdg_runtime_dir = NULL;
329
330 #if !(GLIB_CHECK_VERSION(2, 36, 0))
331         g_type_init();
332 #endif
333
334         kb = bundle_import_from_argv(argc, argv);
335         if (kb) {
336                 bundle_get_str(kb, AUL_K_WAYLAND_WORKING_DIR, &xdg_runtime_dir);
337                 bundle_get_str(kb, AUL_K_WAYLAND_DISPLAY, &wayland_display);
338                 bundle_get_str(kb, WIDGET_K_ENDPOINT, &viewer_endpoint);
339                 _E("viewer endpoint :%s", viewer_endpoint);
340                 viewer_endpoint = strdup(viewer_endpoint);
341
342                 if (xdg_runtime_dir)
343                         setenv("XDG_RUNTIME_DIR", xdg_runtime_dir, 1);
344
345                 if (wayland_display)
346                         setenv("WAYLAND_DISPLAY", wayland_display, 1);
347
348                 bundle_free(kb);
349                 kb = NULL;
350         } else {
351                 _E("failed to get launch argv");
352         }
353
354         elm_init(argc, argv);
355
356         r = aul_launch_init(__aul_handler, NULL);
357         if (r < 0) {
358                 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER, __FUNCTION__,
359                                         "Fail to call the aul_launch_init");
360         }
361
362         r = aul_launch_argv_handler(argc, argv);
363         if (r < 0) {
364                 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER, __FUNCTION__,
365                                         "Fail to call the aul_launch_argv_handler");
366         }
367
368         r = app_get_id(&appid);
369         if (r != APP_ERROR_NONE)
370                 return r;
371
372         class_provider = app_ops->create(app_user_data);
373         if (class_provider == NULL) {
374                 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER, __FUNCTION__,
375                                         "widget_class is NULL");
376         }
377
378         return WIDGET_ERROR_NONE;
379 }
380
381 static void __after_loop()
382 {
383         if (app_ops->terminate)
384                 app_ops->terminate(app_user_data);
385
386         if (viewer_endpoint)
387                 free(viewer_endpoint);
388
389         elm_shutdown();
390 }
391
392 EXPORT_API int widget_app_main(int argc, char **argv,
393                                 widget_app_lifecycle_callback_s *callback, void *user_data)
394 {
395         int r;
396
397         if (!_is_widget_feature_enabled()) {
398                 _E("not supported");
399                 return WIDGET_ERROR_NOT_SUPPORTED;
400         }
401
402         if (argc <= 0 || argv == NULL || callback == NULL)
403                 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
404
405         if (callback->create == NULL)
406                 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER, __FUNCTION__, "widget_app_create_cb() callback must be registered");
407
408         app_ops = callback;
409         app_user_data = user_data;
410         r = __before_loop(argc, argv);
411         if (r < 0)
412                 return r;
413
414         ecore_main_loop_begin();
415         aul_status_update(STATUS_DYING);
416         __after_loop();
417
418         return WIDGET_ERROR_NONE;
419 }
420
421 EXPORT_API int widget_app_exit(void)
422 {
423         if (!_is_widget_feature_enabled()) {
424                 _E("not supported");
425                 return WIDGET_ERROR_NOT_SUPPORTED;
426         }
427
428         ecore_main_loop_quit();
429
430         return WIDGET_ERROR_NONE;
431 }
432
433 static gboolean __finish_event_cb(gpointer user_data)
434 {
435         if (user_data == NULL)
436                 return FALSE;
437
438         widget_context_s *wc = (widget_context_s *)user_data;
439
440         switch (wc->state) {
441         case WC_READY:
442
443                 break;
444         case WC_RUNNING:
445
446                 break;
447         case WC_PAUSED:
448
449                 break;
450         default:
451                 break;
452         }
453
454
455         return FALSE;
456 }
457
458 EXPORT_API int widget_app_terminate_context(widget_context_h context)
459 {
460         if (!_is_widget_feature_enabled()) {
461                 _E("not supported");
462                 return WIDGET_ERROR_NOT_SUPPORTED;
463         }
464
465         if (context == NULL)
466                 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
467
468         g_idle_add(__finish_event_cb, context);
469         return WIDGET_ERROR_NONE;
470 }
471
472 EXPORT_API int widget_app_foreach_context(widget_context_cb cb, void *data)
473 {
474         if (!_is_widget_feature_enabled()) {
475                 _E("not supported");
476                 return WIDGET_ERROR_NOT_SUPPORTED;
477         }
478
479         if (!cb)
480                 return WIDGET_ERROR_INVALID_PARAMETER;
481
482         return WIDGET_ERROR_NONE;
483 }
484
485 EXPORT_API int widget_app_add_event_handler(app_event_handler_h *event_handler,
486                                         app_event_type_e event_type, app_event_cb callback,
487                                         void *user_data)
488 {
489         if (!_is_widget_feature_enabled()) {
490                 _E("not supported");
491                 return WIDGET_ERROR_NOT_SUPPORTED;
492         }
493
494         /* TODO */
495         if (!event_handler || !callback)
496                 return WIDGET_ERROR_INVALID_PARAMETER;
497
498         switch (event_type) {
499         case APP_EVENT_LOW_MEMORY:
500         case APP_EVENT_LOW_BATTERY:
501         case APP_EVENT_LANGUAGE_CHANGED:
502         case APP_EVENT_DEVICE_ORIENTATION_CHANGED:
503         case APP_EVENT_REGION_FORMAT_CHANGED:
504         case APP_EVENT_SUSPENDED_STATE_CHANGED:
505
506                 break;
507         default:
508                 return WIDGET_ERROR_INVALID_PARAMETER;
509         }
510
511         return WIDGET_ERROR_NONE;
512 }
513
514 EXPORT_API int widget_app_remove_event_handler(app_event_handler_h
515                                                 event_handler)
516 {
517         if (!_is_widget_feature_enabled()) {
518                 _E("not supported");
519                 return WIDGET_ERROR_NOT_SUPPORTED;
520         }
521
522         /* TODO */
523         if (!event_handler)
524                 return WIDGET_ERROR_INVALID_PARAMETER;
525
526         return WIDGET_ERROR_NONE;
527 }
528
529 EXPORT_API const char* widget_app_get_id(widget_context_h context)
530 {
531         if (!_is_widget_feature_enabled()) {
532                 _E("not supported");
533                 set_last_result(WIDGET_ERROR_NOT_SUPPORTED);
534                 return NULL;
535         }
536
537         if (!context) {
538                 set_last_result(WIDGET_ERROR_INVALID_PARAMETER);
539                 return NULL;
540         }
541
542         return context->id;
543 }
544
545 EXPORT_API int widget_app_get_elm_win(widget_context_h context,
546                                         Evas_Object **win)
547 {
548         widget_context_s *cxt = (widget_context_s*)context;
549         Evas_Object *ret_win;
550         Ecore_Wl_Window *wl_win;
551
552         if (!_is_widget_feature_enabled()) {
553                 _E("not supported");
554                 return WIDGET_ERROR_NOT_SUPPORTED;
555         }
556
557         if (context == NULL || win == NULL)
558                 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
559
560         ret_win = elm_win_add(NULL, cxt->id, ELM_WIN_BASIC);
561         if (ret_win == NULL) {
562                 _E("failed to create window");
563                 return WIDGET_ERROR_FAULT;
564         }
565
566         wl_win = elm_win_wl_window_get(ret_win);
567         if (wl_win == NULL) {
568                 _E("failed to get wayland window");
569                 evas_object_del(ret_win);
570                 return WIDGET_ERROR_FAULT;
571         }
572
573         ecore_wl_window_class_name_set(wl_win, cxt->id);
574
575         *win = ret_win;
576         cxt->win = ret_win;
577
578         return WIDGET_ERROR_NONE;
579 }
580
581 widget_class_h _widget_class_create(widget_class_s *prev, const char *class_id,
582                 widget_instance_lifecycle_callback_s callback, void *user_data)
583 {
584         widget_class_s *wc;
585
586         if (!_is_widget_feature_enabled()) {
587                 _E("not supported");
588                 set_last_result(WIDGET_ERROR_NOT_SUPPORTED);
589                 return NULL;
590         }
591
592         if (class_id == NULL) {
593                 set_last_result(WIDGET_ERROR_INVALID_PARAMETER);
594                 return NULL;
595         }
596
597         wc = (widget_class_s *)malloc(sizeof(widget_class_s));
598         if (wc == NULL) {
599                 _E("failed to malloc : %s", __FUNCTION__);
600                 set_last_result(WIDGET_ERROR_OUT_OF_MEMORY);
601                 return NULL;
602         }
603
604         wc->classid = strdup(class_id);
605         wc->user_data = user_data;
606         wc->ops = callback;
607         wc->next = prev;
608         wc->prev = NULL;
609
610         set_last_result(WIDGET_ERROR_NONE);
611
612         if (prev)
613                 prev->prev = wc;
614
615         return wc;
616 }
617
618 EXPORT_API widget_class_h widget_app_class_add(widget_class_h widget_class, const char *class_id,
619                 widget_instance_lifecycle_callback_s callback, void *user_data)
620 {
621         return _widget_class_create(widget_class, class_id, callback, user_data);
622 }
623
624 EXPORT_API widget_class_h widget_app_class_create(widget_instance_lifecycle_callback_s callback, void *user_data)
625 {
626         return _widget_class_create(class_provider, appid, callback, user_data);
627 }
628
629 EXPORT_API int widget_app_context_set_tag(widget_context_h context, void *tag)
630 {
631         if (!_is_widget_feature_enabled()) {
632                 _E("not supported");
633                 return WIDGET_ERROR_NOT_SUPPORTED;
634         }
635
636         if (context == NULL)
637                 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
638
639         context->tag = tag;
640
641         return WIDGET_ERROR_NONE;
642 }
643
644 EXPORT_API int widget_app_context_get_tag(widget_context_h context, void **tag)
645 {
646         if (!_is_widget_feature_enabled()) {
647                 _E("not supported");
648                 return WIDGET_ERROR_NOT_SUPPORTED;
649         }
650
651         if (context == NULL || tag == NULL)
652                 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
653
654         *tag = context->tag;
655
656         return WIDGET_ERROR_NONE;
657 }
658
659 EXPORT_API int widget_app_context_set_content_info(widget_context_h context, bundle *content_info)
660 {
661         const char *class_id = NULL;
662         int ret = 0;
663
664         if (!_is_widget_feature_enabled()) {
665                 _E("not supported");
666                 return WIDGET_ERROR_NOT_SUPPORTED;
667         }
668
669         if (context == NULL || content_info == NULL)
670                 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
671
672         if (context->provider == NULL)
673                 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
674
675         class_id = context->provider->classid;
676
677         if (class_id == NULL)
678                 return widget_app_error(WIDGET_ERROR_FAULT, __FUNCTION__, NULL);
679
680         ret = __send_update_status(class_id, context->id, WIDGET_INSTANCE_EVENT_UPDATE, content_info, true);
681
682         if (ret < 0) {
683                 _E("failed to send content info: %s of %s (%d)", context->id, class_id, ret);
684                 return widget_app_error(WIDGET_ERROR_IO_ERROR, __FUNCTION__, NULL);
685         }
686
687         return WIDGET_ERROR_NONE;
688 }
689
690 EXPORT_API int widget_app_context_set_title(widget_context_h context, const char *title)
691 {
692         if (!_is_widget_feature_enabled()) {
693                 _E("not supported");
694                 return WIDGET_ERROR_NOT_SUPPORTED;
695         }
696
697         /* TODO
698          call elm_win_title_set()
699          */
700
701         if (!context || !title)
702                 return WIDGET_ERROR_INVALID_PARAMETER;
703
704         return WIDGET_ERROR_NONE;
705 }
706