fix warning
[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
36 #include "widget_app.h"
37 #include "widget-log.h"
38 #include "widget-private.h"
39 #include "widget_app_internal.h"
40
41 #ifdef LOG_TAG
42 #undef LOG_TAG
43 #endif
44
45 #define STR_MAX_BUF 128
46 #define LOG_TAG "CAPI_WIDGET_APPLICATION"
47 #define K_REASON    "__WC_K_REASON__"
48
49 typedef enum _widget_obj_state_e {
50         WC_READY = 0,
51         WC_RUNNING = 1,
52         WC_PAUSED = 2,
53         WC_TERMINATED = 3
54 } widget_obj_state_e;
55
56 struct app_event_handler {
57         app_event_type_e type;
58         app_event_cb cb;
59         void *data;
60 };
61
62 struct app_event_info {
63         app_event_type_e type;
64         void *value;
65 };
66
67 struct _widget_class {
68         void *user_data;
69         widget_instance_lifecycle_callback_s ops;
70         char *classid;
71         struct _widget_class *next;
72         struct _widget_class *prev;
73 };
74
75 struct _widget_context {
76         char *id;
77         struct _widget_class *provider;
78         int state;
79         void *tag;
80         Evas_Object *win;
81         bundle *content;
82         widget_instance_lifecycle_callback_s ops;
83 };
84
85 typedef struct _widget_class widget_class_s;
86 typedef struct _widget_context widget_context_s;
87
88 static int caller_pid = 0;
89 static widget_app_lifecycle_callback_s *app_ops;
90 static void *app_user_data = NULL;
91 static char *appid = NULL;
92 static widget_class_h class_provider = NULL;
93 static GList *contexts = NULL;
94 static char *viewer_endpoint = NULL;
95
96 static gint __comp_by_id(gconstpointer a, gconstpointer b)
97 {
98         widget_context_s *wc = (widget_context_s*)a;
99
100         return strcmp(wc->id, (const char*)b);
101 }
102
103 static widget_context_s* __find_context_by_id(const char *id)
104 {
105         GList* ret = g_list_find_custom(contexts, id, __comp_by_id);
106
107         if (ret == NULL)
108                 return NULL;
109
110         return ret->data;
111 }
112
113 static int __send_update_status(const char *class_id, const char *instance_id,
114         int status, bundle *extra, int internal_only)
115 {
116         bundle *b = extra;
117
118         if (b == NULL)
119                 b = bundle_create();
120
121         bundle_add_str(b, WIDGET_K_ID, class_id);
122         bundle_add_str(b, WIDGET_K_INSTANCE, instance_id);
123         bundle_add_byte(b, WIDGET_K_STATUS, &status, sizeof(int));
124
125         aul_app_com_send(viewer_endpoint, b);
126
127         if (extra == NULL)
128                 bundle_free(b);
129
130         return 0;
131 }
132
133 static int __instance_create(widget_class_h handle, const char *id, bundle *b)
134 {
135         widget_context_s *wc = NULL;
136         int w = 0, h = 0;
137         char *w_str = NULL, *h_str = NULL;
138         char *remain = NULL;
139         int ret = 0;
140
141         wc = (widget_context_s *)malloc(sizeof(widget_context_s));
142         if (!wc)
143                 return WIDGET_ERROR_OUT_OF_MEMORY;
144
145         wc->state = WC_READY;
146         wc->id = g_strdup(id);
147         wc->provider = handle;
148         wc->win = NULL;
149
150         wc->content = bundle_dup(b);
151         bundle_get_str(b, WIDGET_K_WIDTH, &w_str);
152         bundle_get_str(b, WIDGET_K_HEIGHT, &h_str);
153
154         if (w_str) {
155                 w = (int)g_ascii_strtoll(w_str, &remain, 10);
156         }
157
158         if (h_str) {
159                 h = (int)g_ascii_strtoll(h_str, &remain, 10);
160         }
161
162         contexts = g_list_append(contexts, wc);
163
164         handle->ops.create(wc, b, w, h, handle->user_data);
165
166         ret = __send_update_status(handle->classid, wc->id, WIDGET_INSTANCE_EVENT_CREATE, b, 0);
167
168         return ret;
169 }
170
171 static int __instance_destroy(widget_class_h handle, const char *id, widget_destroy_type_e reason, bundle *b)
172 {
173         widget_context_s *wc = __find_context_by_id(id);
174         int ret = 0;
175
176         if (wc) {
177                 wc->state = WC_TERMINATED;
178                 handle->ops.destroy(wc, (widget_app_destroy_type_e)reason, b, handle->user_data);
179
180                 ret = __send_update_status(handle->classid, id, WIDGET_INSTANCE_EVENT_TERMINATE, b, 0);
181
182                 contexts = g_list_remove(contexts, wc);
183
184                 if (wc->id)
185                         free(wc->id);
186                 free(wc);
187         } else {
188                 _E("could not find widget obj: %s", id);
189                 ret = WIDGET_ERROR_INVALID_PARAMETER;
190         }
191
192         return ret;
193 }
194
195 static widget_class_h __find_class_handler(const char *class_id, widget_class_h handle)
196 {
197         if (!class_id || !handle)
198                 return NULL;
199
200         widget_class_h head = handle;
201
202         while (head) {
203                 if (head->classid && strcmp(head->classid, class_id) == 0)
204                         return head;
205
206                 head = head->next;
207         }
208
209         return NULL;
210 }
211
212 static void __control(bundle *b)
213 {
214         char *class_id = NULL;
215         char *id = NULL;
216         char *operation = NULL;
217         char *reason = NULL;
218         char *remain = NULL;
219         int destroy_type = WIDGET_DESTROY_TYPE_DEFAULT;
220
221         widget_class_h handle = NULL;
222         bundle_get_str(b, WIDGET_K_CLASS, &class_id);
223         /* for previous version compatibility, use appid for default class id */
224         if (class_id == NULL)
225                 class_id = appid;
226
227         bundle_get_str(b, WIDGET_K_INSTANCE, &id);
228         bundle_get_str(b, WIDGET_K_OPERATION, &operation);
229
230         handle = __find_class_handler(class_id, class_provider);
231         if (!handle) {
232                 _E("no handle provided: %s", class_id);
233                 goto error;
234         }
235
236         if (!operation) {
237                 _E("no operation provided");
238                 goto error;
239         }
240
241         if (strcmp(operation, "create") == 0) {
242                 __instance_create(handle, id, b);
243         } else if (strcmp(operation, "resize") == 0) {
244                 /* TODO */
245         } else if (strcmp(operation, "update") == 0) {
246                 /* TODO */
247         } else if (strcmp(operation, "destroy") == 0) {
248                 bundle_get_str(b, WIDGET_K_REASON, &reason);
249                 if (reason)
250                         destroy_type = (int)g_ascii_strtoll(reason, &remain, 10);
251
252                 __instance_destroy(handle, id, destroy_type, b);
253         } else if (strcmp(operation, "resume") == 0) {
254                 /* TODO */
255         } else if (strcmp(operation, "pause") == 0) {
256                 /* TODO */
257         }
258
259         return;
260 error:
261         LOGD("error on control");
262         return;
263 }
264
265 static void __show_all()
266 {
267         LOGD("resume");
268 }
269
270 static int __aul_handler(aul_type type, bundle *b, void *data)
271 {
272         char *caller = NULL;
273         char *remain = NULL;
274
275         switch (type) {
276         case AUL_START:
277                 if (b) {
278                         bundle_get_str(b, WIDGET_K_CALLER, &caller);
279                         if (caller)
280                                 caller_pid = g_ascii_strtoll(caller, &remain, 10);
281                         else {
282                                 /* using caller appid and query pid using caller appid? */
283                                 _E("no caller pid");
284                         }
285                 }
286
287                 __control(b);
288                 break;
289         case AUL_RESUME:
290                 __show_all();
291                 break;
292         case AUL_TERMINATE:
293                 widget_app_exit();
294                 break;
295         default:
296                 break;
297         }
298
299         return 0;
300 }
301
302
303 static int __before_loop(int argc, char **argv)
304 {
305         int r;
306         bundle *kb = NULL;
307         char *wayland_display = NULL;
308         char *xdg_runtime_dir = NULL;
309
310 #if !(GLIB_CHECK_VERSION(2, 36, 0))
311         g_type_init();
312 #endif
313
314         kb = bundle_import_from_argv(argc, argv);
315         if (kb) {
316                 bundle_get_str(kb, AUL_K_WAYLAND_WORKING_DIR, &xdg_runtime_dir);
317                 bundle_get_str(kb, AUL_K_WAYLAND_DISPLAY, &wayland_display);
318                 bundle_get_str(kb, WIDGET_K_ENDPOINT, &viewer_endpoint);
319
320                 if (xdg_runtime_dir)
321                         setenv("XDG_RUNTIME_DIR", xdg_runtime_dir, 1);
322
323                 if (wayland_display)
324                         setenv("WAYLAND_DISPLAY", wayland_display, 1);
325
326                 bundle_free(kb);
327                 kb = NULL;
328         } else {
329                 _E("failed to get launch argv");
330         }
331
332         elm_init(argc, argv);
333
334         r = aul_launch_init(__aul_handler, NULL);
335         if (r < 0) {
336                 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER, __FUNCTION__,
337                                         "Fail to call the aul_launch_init");
338         }
339
340         r = aul_launch_argv_handler(argc, argv);
341         if (r < 0) {
342                 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER, __FUNCTION__,
343                                         "Fail to call the aul_launch_argv_handler");
344         }
345
346         r = app_get_id(&appid);
347         if (r != APP_ERROR_NONE)
348                 return r;
349
350         class_provider = app_ops->create(app_user_data);
351         if (class_provider == NULL) {
352                 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER, __FUNCTION__,
353                                         "widget_class is NULL");
354         }
355
356         return WIDGET_ERROR_NONE;
357 }
358
359 static void __after_loop()
360 {
361         if (app_ops->terminate)
362                 app_ops->terminate(app_user_data);
363
364         elm_shutdown();
365 }
366
367 EXPORT_API int widget_app_main(int argc, char **argv,
368                                 widget_app_lifecycle_callback_s *callback, void *user_data)
369 {
370         int r;
371
372         if (argc <= 0 || argv == NULL || callback == NULL)
373                 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
374
375         if (callback->create == NULL)
376                 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER, __FUNCTION__, "widget_app_create_cb() callback must be registered");
377
378         app_ops = callback;
379         app_user_data = user_data;
380         r = __before_loop(argc, argv);
381         if (r < 0)
382                 return r;
383
384         ecore_main_loop_begin();
385         aul_status_update(STATUS_DYING);
386         __after_loop();
387
388         return WIDGET_ERROR_NONE;
389 }
390
391 EXPORT_API int widget_app_exit(void)
392 {
393         ecore_main_loop_quit();
394
395         return WIDGET_ERROR_NONE;
396 }
397
398 static gboolean __finish_event_cb(gpointer user_data)
399 {
400         if (user_data == NULL)
401                 return FALSE;
402
403         widget_context_s *wc = (widget_context_s *)user_data;
404
405         switch (wc->state) {
406         case WC_READY:
407
408                 break;
409         case WC_RUNNING:
410
411                 break;
412         case WC_PAUSED:
413
414                 break;
415         default:
416                 break;
417         }
418
419
420         return FALSE;
421 }
422
423 EXPORT_API int widget_app_terminate_context(widget_context_h context)
424 {
425         if (context == NULL)
426                 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
427
428         g_idle_add(__finish_event_cb, context);
429         return WIDGET_ERROR_NONE;
430 }
431
432 EXPORT_API int widget_app_foreach_context(widget_context_cb cb, void *data)
433 {
434         return WIDGET_ERROR_NONE;
435 }
436
437 EXPORT_API int widget_app_add_event_handler(app_event_handler_h *event_handler,
438                                         app_event_type_e event_type, app_event_cb callback,
439                                         void *user_data)
440 {
441         /* TODO */
442         return 0;
443 }
444
445 EXPORT_API int widget_app_remove_event_handler(app_event_handler_h
446                                                 event_handler)
447 {
448         /* TODO */
449         return 0;
450 }
451
452 EXPORT_API const char* widget_app_get_id(widget_context_h context)
453 {
454         return context->id;
455 }
456
457 EXPORT_API int widget_app_get_elm_win(widget_context_h context,
458                                         Evas_Object **win)
459 {
460         if (context == NULL || win == NULL)
461                 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
462
463         widget_context_s *cxt = (widget_context_s*)context;
464         Evas_Object *ret_win;
465         Ecore_Wl_Window *wl_win;
466
467         ret_win = elm_win_add(NULL, cxt->id, ELM_WIN_BASIC);
468         if (ret_win == NULL) {
469                 _E("failed to create window");
470                 return WIDGET_ERROR_FAULT;
471         }
472
473         wl_win = elm_win_wl_window_get(ret_win);
474         if (wl_win == NULL) {
475                 _E("failed to get wayland window");
476                 evas_object_del(ret_win);
477                 return WIDGET_ERROR_FAULT;
478         }
479
480         ecore_wl_window_class_name_set(wl_win, cxt->id);
481
482         *win = ret_win;
483         cxt->win = ret_win;
484
485         return WIDGET_ERROR_NONE;
486 }
487
488 widget_class_h _widget_class_create(widget_class_s *prev, const char *class_id,
489                 widget_instance_lifecycle_callback_s callback, void *user_data)
490 {
491         widget_class_s *wc = (widget_class_s*)malloc(sizeof(widget_class_s));
492
493         if (wc == NULL) {
494                 _E("failed to malloc : %s", __FUNCTION__);
495                 set_last_result(WIDGET_ERROR_OUT_OF_MEMORY);
496                 return NULL;
497         }
498
499         wc->classid = strdup(class_id);
500         wc->user_data = user_data;
501         wc->ops = callback;
502         wc->next = prev;
503         wc->prev = NULL;
504
505         set_last_result(WIDGET_ERROR_NONE);
506
507         if (prev)
508                 prev->prev = wc;
509
510         return wc;
511 }
512
513 EXPORT_API widget_class_h widget_app_class_add(widget_class_h widget_class, const char *class_id,
514                 widget_instance_lifecycle_callback_s callback, void *user_data)
515 {
516         if (class_id == NULL) {
517                 set_last_result(WIDGET_ERROR_INVALID_PARAMETER);
518                 return NULL;
519         }
520
521         return _widget_class_create(widget_class, class_id, callback, user_data);
522 }
523
524 EXPORT_API widget_class_h widget_app_class_create(widget_instance_lifecycle_callback_s callback, void *user_data)
525 {
526         return _widget_class_create(class_provider, appid, callback, user_data);
527 }
528
529 EXPORT_API int widget_app_context_set_tag(widget_context_h context, void *tag)
530 {
531         if (context == NULL)
532                 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
533
534         context->tag = tag;
535
536         return WIDGET_ERROR_NONE;
537 }
538
539 EXPORT_API int widget_app_context_get_tag(widget_context_h context, void **tag)
540 {
541         if (context == NULL || tag == NULL)
542                 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
543
544         *tag = context->tag;
545
546         return WIDGET_ERROR_NONE;
547 }
548
549 EXPORT_API int widget_app_context_set_content_info(widget_context_h context, bundle *content_info)
550 {
551         const char *class_id = NULL;
552         int ret = 0;
553         if (context == NULL || content_info == NULL)
554                 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
555
556         if (context->provider == NULL)
557                 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
558
559         class_id = context->provider->classid;
560
561         if (class_id == NULL)
562                 return widget_app_error(WIDGET_ERROR_FAULT, __FUNCTION__, NULL);
563
564         ret = __send_update_status(class_id, context->id, WIDGET_INSTANCE_EVENT_UPDATE, content_info, true);
565
566         if (ret < 0) {
567                 _E("failed to send content info: %s of %s (%d)", context->id, class_id, ret);
568                 return widget_app_error(WIDGET_ERROR_IO_ERROR, __FUNCTION__, NULL);
569         }
570
571         return WIDGET_ERROR_NONE;
572 }
573
574 EXPORT_API int widget_app_context_set_title(widget_context_h context, const char *title)
575 {
576         /* TODO
577          may use widget status update, or use surface title set.
578          */
579         return 0;
580 }
581