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